Validate UTF-8 · Stasis 🤝 Claude Sonnet 5.5 (Anthropic) · #3 Smallest gzipped

SHA-256 b0c9b79adee2da43b04bde41e1ad633ceea8f21596080288fc926c1997a1bd8b

View source (5.4 KiB of Stasis)
// Stasis validate_utf8 (scalar). Built with Stasis commit 54181959 (stasis package --target web), then adapted to this site's
// interface by the glue script below (node + binaryen 132): it drops Stasis's own exports, pins memory at 9 pages, adds input_ptr /
// input_bytes_cap / output_bytes_cap and a render() that returns the text true or false, then runs wasm-opt -O3 --strip-debug.
// The glue contains no validation logic; the validation is the Stasis source at the end of this file.
//
// ---- glue.js ----
// // Fit a Stasis web module to the challenge interface. No validation logic here:
// // drop Stasis's exports, pin the memory size, add the buffer accessors and render().
// // usage: node glue.js game.wasm out.wasm [wasm-opt level]
// const fs = require('fs'), { execFileSync } = require('child_process');
// const [src, out, level = '-O3'] = process.argv.slice(2);
// const bin = 'D:/code/tools/binaryen-version_132/bin/';
// const wat = out + '.wat';
// execFileSync(bin + 'wasm-dis.exe', [src, '-o', wat]);
// let t = fs.readFileSync(wat, 'utf8').split(String.fromCharCode(13)).join('');
// const validate = t.match(/\(export "stasis_host_v1_validate" \(func (\$[^)]+)\)\)/)[1];
// let pages = Number(t.match(/\(memory \$\S+ (\d+)/)[1]);
// if (pages * 65536 - 16 < 524288 + 2304) pages++;  // keep the output text clear of Stasis globals
// t = t.replace(/^ \(export "(?!memory").*\n/gm, '');
// t = t.replace(/\(memory (\$\S+) \d+[^)]*\)/, `(memory $1 ${pages} ${pages})`);
// const CAP = 524288, T = pages * 65536 - 16, F = T + 4;
// const glue = ` (data (i32.const ${T}) "truefalse")
//  (func $input_ptr (export "input_ptr") (result i32) (i32.const 0))
//  (func $input_bytes_cap (export "input_bytes_cap") (result i32) (i32.const ${CAP}))
//  (func $output_bytes_cap (export "output_bytes_cap") (result i32) (i32.const 5))
//  (func $abi_render (export "render") (param $n i32) (result i64)
//   (if (result i64) (call ${validate} (local.get $n))
//    (then (i64.const ${(BigInt(T) << 32n) | 4n}))
//    (else (i64.const ${(BigInt(F) << 32n) | 5n}))))
// `;
// const end = t.lastIndexOf(')');
// t = t.slice(0, end) + glue + t.slice(end);
// fs.writeFileSync(wat, t);
// execFileSync(bin + 'wasm-as.exe', [wat, '-o', out + '.raw']);
// execFileSync(bin + 'wasm-opt.exe', [level, '--strip-debug', '--strip-producers', out + '.raw', '-o', out]);
// fs.unlinkSync(wat); fs.unlinkSync(out + '.raw');
// const m = new WebAssembly.Module(fs.readFileSync(out));
// console.log(out, 'pages', pages, 'imports', WebAssembly.Module.imports(m).length, 'exports', WebAssembly.Module.exports(m).map(e => e.name).join(','), 'bytes', fs.statSync(out).size);
//
// ---- validate_utf8.stasis ----
import "/vendor/stasis/stdlib/stdlib.stasis";

// The text to check. The wrapper that fits the calculated.world interface writes the input here.
global input: u8[524288];

// Is the text in `input` valid UTF-8? One byte at a time.
// A character starts with a lead byte, which says how many continuation bytes (80 to BF) must follow.
function is_valid_utf8(byte_count: i32): bool {
    let length: i32 = byte_count;
    if (length < 0) { length = 0; }
    if (length > 524288) { length = 524288; }

    let continuations_left: i32 = 0;   // continuation bytes the current character still needs
    let next_byte_low: i32 = 128;      // the next continuation byte must be in next_byte_low .. next_byte_high
    let next_byte_high: i32 = 191;

    for (let position: i32 = 0; position < length; position += 1) {
        let byte: i32 = to_i32(input[position]);
        if (continuations_left == 0) {
            if (byte >= 128) {
                // a lead byte: its value gives the character's length and restricts the byte after it
                if (byte < 194) { return false; }              // 80 to BF is a continuation byte; C0 and C1 would be a long form of ASCII
                else if (byte < 224) { continuations_left = 1; }  // 2-byte character
                else if (byte < 240) {                           // 3-byte character
                    continuations_left = 2;
                    if (byte == 224) { next_byte_low = 160; }    // E0 80 to E0 9F would be a long form
                    if (byte == 237) { next_byte_high = 159; }   // ED A0 and above would be a surrogate
                } else if (byte < 245) {                         // 4-byte character
                    continuations_left = 3;
                    if (byte == 240) { next_byte_low = 144; }    // F0 80 to F0 8F would be a long form
                    if (byte == 244) { next_byte_high = 143; }   // F4 90 and above is past U+10FFFF
                } else { return false; }                         // F5 to FF never appear
            }
        } else {
            if (byte < next_byte_low || byte > next_byte_high) { return false; }
            continuations_left -= 1;
            next_byte_low = 128;
            next_byte_high = 191;
        }
    }
    // a character cut short at the end is invalid
    return continuations_left == 0;
}

// What the calculated.world wrapper calls. Host exports return a whole number, so the bool becomes 1 or 0.
function @host_export(validate) validate(byte_count: i32): i32 {
    if (is_valid_utf8(byte_count)) { return 1; }
    return 0;
}

// Stasis programs need these three functions; this one has nothing to run or draw.
function main(): i32 {
    return 0;
}

function tick(): i32 {
    return 0;
}

function render(): i32 {
    return 0;
}

Scalar, no SIMD. Written in StasisLang (a statically compiled language for games). The module is the compiler's output adapted to this challenge's interface by a small glue script (included as comments at the top of the source file): it renames exports, pins memory at 9 pages and adds render(). The glue has no validation logic.

Build details

Compiler, size, memory and checksum
Submitted
2 December 2026 at 19:02 UTC
Language
Stasis
Compiler
stasis commit 54181959 (StasisLang main after PR 986), then binaryen 132 glue stasis package --target web (wasm-opt -Oz inside); glue.js then wasm-opt -O3 --strip-debug
Size
0.4 KiB, gzip 0.3 KiB
Download
~150 ms on Slow 4G (Lighthouse’s 1.6 Mbps, 150 ms round trips)
Memory
9 pages (576 KiB)
SIMD
no
AI models
Claude Sonnet 5.5 (Anthropic)
Functions, loops, branches and other static counts
Count Value
Functions 4
Instructions 140
Loops 1
Conditional branches 10
Indirect calls 0
Memory loads 1
Memory stores 0
SIMD instructions 0
Potentially trapping instructions 1

Instruction sites in the binary, not executions, counted by qip.dev’s wasm-counts.