#wasm-interpreter #ast #syntax-tree #validation

wain-validate

WebAssembly syntax tree validator for wain project

6 releases

0.1.5 Nov 18, 2023
0.1.4 Jun 11, 2020
0.1.3 May 24, 2020
0.1.2 Apr 4, 2020
0.1.1 Mar 29, 2020

#1457 in WebAssembly


Used in 2 crates

MIT license

74KB
1.5K SLoC

wain-validate

crates.io CI

wain-validate is a crate to validate a parsed WebAssembly abstract syntax tree. Validation logic is defined in spec

This crate is part of larger wain project.

Installation

[dependencies]
wain-validate = "0"

Usage

It takes a reference to wain_ast::Root value and validates it. The value can be generated by wain-syntax-binary and wain-syntax-text parsers:

Using wain_validate::validate() is the easiest way.

extern crate wain_syntax_binary;
extern crate wain_validate;

use std::fs;
use wain_syntax_binary::parse;
use wain_validate::validate;

let source = fs::read("foo.wasm").unwrap();
let tree = parse(&source).unwrap();

if let Err(err) = validate(&tree) {
    eprintln!("This .wasm file is invalid!: {}", err);
}

Working examples can be seen at examples/api/ directory

Please read documentation (not yet) for details.

Implementation

Conforming spec, following things are validated:

  • In Wasm, every reference is an index. It validates all indices are not out of bounds
  • Wasm is designed to check stack operations statically. It validates instructions sequences with emulating stack state
  • Type check is best-effort due to polymorphic instruction select. Since almost all instructions are not polymorphic, almost all type checks can be done in validation

Conforming the spec, wain validates instructions after unreachable instruction. For example,

(unreachable) (i64.const 0) (i32.add)

i32.add is invalid because it should take two i32 values from stack but at least one i64 value is in the stack.

License

the MIT license

Dependencies