diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index bc723e8..32be17d 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -177,4 +177,6 @@ Each cites `idml-values.md`. | `tools/compare.py` | fidelity against the reference IDML files, schema validation | | `tools/diff_outputs.py` | byte-for-byte output of two revisions over the corpus | +| `tools/check_wasm.sh` | the library builds for `wasm32-unknown-unknown`, a conversion there imports no host function, and its output is the same as native | + | `indd audit`, `tools/audit_corpus.py` | what the converter does not read yet | diff --git a/CLAUDE.md b/CLAUDE.md index 24bbe39..e6df6a0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -65,7 +65,14 @@ separately (rule and evidence: `docs/measurement.md`). ## Commands - `cargo test` (all tests), `cargo clippy --all-targets`, `cargo fmt`. +- `tools/check_wasm.sh [file.indd...]`: build the library for + `wasm32-unknown-unknown` (`rustup target add wasm32-unknown-unknown`), + check that a conversion there imports no host function, and compare + its output with the native output for the fixtures (or the files + given). Needs Node.js for the last two steps. The library's conversion + path must not use `std::fs`, clocks or threads. - `python3 -I tools/fetch_fixtures.py`: fetch the fixture files before + `cargo test`. To add a fixture, add its pinned URL, size and SHA-256 to `tests/fixtures/manifest.json` and its licence and source to `tests/fixtures/README.md`. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 43ece0d..84e042d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -45,6 +45,9 @@ accepted. document. 4. For schema validation, get the IDML RelaxNG schemas and Jing and keep them outside the repository (see `tools/validate.sh`). +5. For the WebAssembly check, run `rustup target add + wasm32-unknown-unknown` and install Node.js. + ## Checks @@ -56,7 +59,9 @@ cargo fmt cargo clippy --all-targets cargo test --release cargo doc --no-deps +tools/check_wasm.sh # WebAssembly build, same output as native python3 -I tools/diff_outputs.py # output of HEAD vs the working tree + cargo build --release && python3 -I tools/compare.py --exclude own/ ``` diff --git a/README.md b/README.md index a3d4d87..7ef710c 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,11 @@ for warning in &conversion.warnings { writer. A conversion keeps no global state, so several documents can be converted on different threads at once. +The library also builds for WebAssembly (`wasm32-unknown-unknown`): +`indd::convert` needs no file system or other host function, and gives +the same package as a native build (`tools/check_wasm.sh`). + + ## Supported versions | Versions | Distinct samples (little-endian, big-endian) | State | diff --git a/tools/check_wasm.sh b/tools/check_wasm.sh new file mode 100755 index 0000000..239bd16 --- /dev/null +++ b/tools/check_wasm.sh @@ -0,0 +1,155 @@ +#!/bin/sh +# Check that the library builds for WebAssembly and converts there the +# same as natively. +# +# Usage: tools/check_wasm.sh [file.indd...] +# +# 1. Builds the library for wasm32-unknown-unknown. The `indd` binary is +# not built: it reads and writes files. +# 2. Builds a probe module: a cdylib, generated under the target +# directory, that exports a function calling `indd::convert`. Node.js +# checks that the module imports nothing, so a conversion needs no +# file system, clock or other host function. +# 3. Converts each file given, or else each fetched fixture, in the probe +# module and compares the package byte for byte with the output of the +# native `indd convert`. +# +# Needs the target (`rustup target add wasm32-unknown-unknown`). Steps 2 +# and 3 need Node.js and are skipped without it. Exits 1 if a check fails. +set -eu +root=$(cd "$(dirname "$0")/.." && pwd) +target=${CARGO_TARGET_DIR:-$root/target} +case $target in /*) ;; *) target=$root/$target ;; esac +export CARGO_TARGET_DIR="$target" + +if ! rustup target list --installed | grep -qx wasm32-unknown-unknown; then + echo "missing target: rustup target add wasm32-unknown-unknown" >&2 + exit 1 +fi + +echo "library: cargo build --lib --target wasm32-unknown-unknown" >&2 +cargo build -q --release --lib --target wasm32-unknown-unknown --manifest-path "$root/Cargo.toml" + +if ! command -v node >/dev/null 2>&1; then + echo "node not found; skipping the probe module" >&2 + exit 0 +fi + +work=$target/wasm-check +probe=$work/probe +mkdir -p "$probe/src" "$work/out" +cat >"$probe/Cargo.toml" <"$probe/src/lib.rs" <<'EOF' +//! Probe for tools/check_wasm.sh: `indd::convert` for a WebAssembly host. + +use std::sync::Mutex; + +static OUTPUT: Mutex> = Mutex::new(Vec::new()); + +/// Room for `len` bytes, for the host to fill and pass to `convert`. +#[unsafe(no_mangle)] +pub extern "C" fn alloc(len: usize) -> *mut u8 { + std::mem::ManuallyDrop::new(Vec::::with_capacity(len)).as_mut_ptr() +} + +/// Convert the `len` bytes at `input`, with the document name in the +/// `name_len` bytes at `name` (both from `alloc`). Returns the length of +/// the package, which `output` points to, or -1 if the conversion fails. +/// +/// # Safety +/// +/// Both buffers come from `alloc` with these lengths and are filled. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn convert(input: *mut u8, len: usize, name: *mut u8, name_len: usize) -> isize { + let input = unsafe { Vec::from_raw_parts(input, len, len) }; + let name = unsafe { Vec::from_raw_parts(name, name_len, name_len) }; + let (Ok(c), Ok(mut out)) = (indd::convert(&input, &String::from_utf8_lossy(&name)), OUTPUT.lock()) else { + return -1; + }; + *out = c.idml; + out.len() as isize +} + +/// The package of the last conversion. +#[unsafe(no_mangle)] +pub extern "C" fn output() -> *const u8 { + OUTPUT.lock().map_or(std::ptr::null(), |o| o.as_ptr()) +} +EOF +cat >"$work/run.mjs" <<'EOF' +// Usage: node run.mjs probe.wasm [input name expected]... +// `expected` is the native package, or "-" if the native conversion failed. +import { readFileSync } from "node:fs"; + +const [wasm, ...args] = process.argv.slice(2); +const module = new WebAssembly.Module(readFileSync(wasm)); +const imports = WebAssembly.Module.imports(module); +if (imports.length > 0) { + console.log(`probe module imports: ${imports.map((i) => `${i.module}.${i.name}`).join(" ")}`); + process.exit(1); +} +const { exports } = new WebAssembly.Instance(module, {}); +const put = (bytes) => { + const at = exports.alloc(bytes.length); + new Uint8Array(exports.memory.buffer, at, bytes.length).set(bytes); + return at; +}; +let failed = 0; +for (let i = 0; i < args.length; i += 3) { + const [input, name, expected] = args.slice(i, i + 3); + const bytes = readFileSync(input); + const label = new TextEncoder().encode(name); + const n = exports.convert(put(bytes), bytes.length, put(label), label.length); + let result; + if (n < 0) { + result = expected === "-" ? "same (both fail)" : "differs: fails in WebAssembly only"; + } else if (expected === "-") { + result = "differs: fails natively only"; + } else { + const out = Buffer.from(new Uint8Array(exports.memory.buffer, exports.output(), n)); + result = out.equals(readFileSync(expected)) ? `same (${n} bytes)` : "differs"; + } + if (result.startsWith("differs")) failed++; + console.log(`${input}: ${result}`); +} +console.log(`${args.length / 3} files, ${failed} differ; no imports`); +process.exit(failed > 0 ? 1 : 0); +EOF + +echo "probe module: cargo build --target wasm32-unknown-unknown" >&2 +cargo build -q --release --target wasm32-unknown-unknown --manifest-path "$probe/Cargo.toml" +cargo build -q --release --bin indd --manifest-path "$root/Cargo.toml" + +if [ $# -eq 0 ]; then + for f in "$root"/tests/fixtures/files/*/*.indd "$root"/tests/fixtures/files/*/*.indt; do + [ -f "$f" ] && set -- "$@" "$f" + done + if [ $# -eq 0 ]; then + echo "no files given and no fixtures fetched; checking imports only" >&2 + fi +fi +i=0 +for f; do + shift + i=$((i + 1)) + expected=$work/out/$i.idml + if ! "$target/release/indd" convert "$f" "$expected" 2>/dev/null; then + expected=- + fi + set -- "$@" "$f" "$(basename "$f")" "$expected" +done +node "$work/run.mjs" "$target/wasm32-unknown-unknown/release/indd_wasm_probe.wasm" "$@"