For the complete documentation index, see llms.txt. This page is also available as Markdown.

Wasm Validator

The +validator library validates WebAssembly modules for correctness according to the WebAssembly Core Specification. It performs static analysis to ensure type safety, proper memory access, and correct control flow before execution.

The validator operates on parsed $module structures from the +parser core and provides error messages for invalid modules.

+validator

++  validator
  =,  wasm-sur
  |%
  +$  glob-type  [v=valtype m=?(%con %var)]
  +$  glob-types  (list glob-type)
  ::  ...
  --

Core validator namespace. Exposes the nested +wasm-sur namespace and defines validator-specific types.

$glob-type

+$  glob-type  [v=valtype m=?(%con %var)]

Global variable type combining a $valtype with mutability:

  • .v: Value type of the global variable.

  • .m: Mutability - %con for constant, %var for variable.

$glob-types

List of all global variable types.

+output

Validation output types. The .funcs, .tables, and .globs lists are built in reverse order during parsing, so must be +flopped to get the right order.

  • .funcs: List of imported function types.

  • .tables: List of imported $tables.

  • .memo: Optional imported memory $limits.

  • .globs: List of imported global types.

$store

Validation store containing all available types in the correct order for validation context. Order matters because the store provides the validation context where imported items come first, followed by locally defined items, matching the WebAssembly spec's indexing requirements.

+result-form

Generic result type parameterized by return type. Either a result of the given $mold or an error message cord.

+result

Monadic error handling system for validation. If the first validation fails, returns the error message; otherwise applies the continuation function.

+snug

Safe list indexing. Returns element at index .a in list .b, or an error message indicating the failure location.

+validate-module

Main module validation function. Validates a $module by sequentially checking all sections in dependency order.

+v-import-section

Validates the module's $import-section. Checks:

  • Function imports reference valid type indices.

  • Table imports have valid $limits.

  • At most one memory can be imported.

  • Global imports have proper types.

+validate-limits

Validates $limits ensuring maximum is greater than or equal to minimum.

+v-function-section

Validates the $function-section by checking that all function type indices reference valid entries in the type section.

+v-table-section

Validates the $table-section by checking table $limits.

+v-memory-section

Validates the $memory-section. Ensures:

  • At most one memory is defined.

  • Memory limits are valid.

  • Maximum memory size doesn't exceed 2^16 pages (4GB).

+v-global-section

Validates the $global-section. Checks:

  • Initializer expressions match declared types.

  • Function references are valid.

  • Global references in initializers refer to imported constants.

+v-export-section

Validates the $export-section. Checks:

  • Export names are unique.

  • Export indices are within bounds for their respective types.

+v-start-section

Validates the $start-section ensuring the start function has no parameters and no return values.

+v-elem-section

Validates the $elem-section. Checks:

  • Element initializers match table reference types.

  • Function references are valid.

  • Table indices are within bounds.

  • Offset expressions have correct types.

+v-datacnt-section

Returns the data count section, no additional validation needed.

+v-code-section

Validates the $code-section. Checks:

  • Code and function sections have matching lengths.

  • Each function body validates against its declared type.

+v-data-section

Validates the $data-section. Checks:

  • Data count matches actual data segments.

  • Memory exists for active data segments.

  • Offset expressions have correct types.

+validate-code

Validates a single function's $code against its expected $func-type. Sets up the validation context with parameters and local variables, then validates the function body.

+validate-expr

Validates an $expression (sequence of instructions) within a given context. Validates:

  • Local variable types.

  • Stack type state.

  • Control flow frames for branch validation.

Handles stack-polymorphic instructions (unreachable, br, br-table, return) specially as they don't return normally.

+validate-instr

Validates a single $instruction against the current stack and context. Handles:

  • Value-polymorphic instructions (drop, select).

  • Block instructions (block, loop, if).

  • Control flow instructions (br-if, call, call-indirect).

  • Reference instructions (ref-is-null).

For most instructions, delegates to +get-type to determine the instruction's type signature.

+get-type

Returns the type signature of an $instruction as a $func-type (parameter types and result types). Handles:

  • Constant instructions (const).

  • Unary numeric operations (eqz, clz, ctz, popcnt, abs, neg, etc.).

  • Binary numeric operations (add, sub, mul, div, rem, etc.).

  • Comparison operations (eq, ne, lt, gt, le, ge).

  • Reference operations (ref-null, ref-func).

  • Variable operations (local-get, local-set, local-tee, global-get, global-set).

  • Table operations (table-get, table-set, table-init, etc.).

  • Memory operations (load, store, memory-size, memory-grow, etc.).

  • Vector instructions (delegated to +get-type-vec).

+get-type-vec

Returns the type signature for SIMD vector instructions. Handles:

  • Vector memory operations (load, store, load-lane, store-lane).

  • Vector constants and manipulation (const, shuffle, extract, replace).

  • Vector logical operations (swizzle, splat).

  • Vector comparison and arithmetic operations.

  • Vector conversion operations.

+from-coin

Extracts the $valtype from a $coin-wasm.

+byte-width

Returns the byte width of a numeric or vector type.

+dim-lane

Returns the number of lanes in a 128-bit vector for the given $lane-type.

+unpack

Converts a $lane-type to its corresponding $num-type, promoting %i8 and %i16 to %i32.

Last updated