Add tests for the interface types output of wasm-bindgen (#1898)

* Add tests for the interface types output of wasm-bindgen

This commit expands the test suite with assertions about the output of
the interface types pass in wasm-bindgen. The goal here is to actually
assert that we produce the right output and have a suite of reference
files to show how the interface types output is changing over time.

The `reference` test suite added in the previous PR has been updated to
work for interface types as well, generating `*.wit` file assertions
which are printed via the `wit-printer` crate on crates.io.

Along the way a number of bugs were fixed with the interface types
output, such as:

* Non-determinism in output caused by iteration of a `HashMap`

* Avoiding JS generation entirely in interface types mode, ensuring that
  we don't export extraneous intrinsics that aren't otherwise needed.

* Fixing location of the stack pointer for modules where it's GC'd out.
  It's now rooted in the aux section of wasm-bindgen so it's available
  to later passes, like the multi-value pass.

* Interface types emission now works in debug mode, meaning the
  `--release` flag is no longer required. This previously did not work
  because the `__wbindgen_throw` intrinsic was required in debug mode.
  This comes about because of the `malloc_failure` and `internal_error`
  functions in the anyref pass. The purpose of these functions is to
  signal fatal runtime errors, if any, in a way that's usable to the
  user. For wasm interface types though we can replace calls to these
  functions with `unreachable` to avoid needing to import the
  intrinsic. This has the accidental side effect of making
  `wasm_bindgen::throw_str` "just work" with wasm interface types by
  aborting the program, but that's not actually entirely intended. It's
  hoped that a split of a `wasm-bindgen-core` crate would solve this
  issue for the future.

* Run the wasm interface types validator in tests

* Add more gc roots for adapter gc

* Improve stack pointer detection

The stack pointer is never initialized to zero, but some other mutable
globals are (TLS, thread ID, etc), so let's filter those out.
This commit is contained in:
Alex Crichton
2019-12-04 15:19:48 -06:00
committed by GitHub
parent b9c93a3c24
commit 203d86f343
22 changed files with 699 additions and 168 deletions

View File

@ -74,17 +74,18 @@ fn runtest(test: &Path) -> Result<()> {
repo_root().display(),
test.display(),
);
let interface_types = contents.contains("// interface-types");
fs::write(td.path().join("Cargo.toml"), manifest)?;
let target_dir = target_dir();
exec(
Command::new("cargo")
.current_dir(td.path())
.arg("build")
.arg("--target")
.arg("wasm32-unknown-unknown")
.env("CARGO_TARGET_DIR", &target_dir),
)?;
let mut cargo = Command::new("cargo");
cargo
.current_dir(td.path())
.arg("build")
.arg("--target")
.arg("wasm32-unknown-unknown")
.env("CARGO_TARGET_DIR", &target_dir);
exec(&mut cargo)?;
let wasm = target_dir
.join("wasm32-unknown-unknown")
@ -100,26 +101,33 @@ fn runtest(test: &Path) -> Result<()> {
if contents.contains("// enable-anyref") {
bindgen.env("WASM_BINDGEN_ANYREF", "1");
}
if interface_types {
bindgen.env("WASM_INTERFACE_TYPES", "1");
}
exec(&mut bindgen)?;
let js = fs::read_to_string(td.path().join("reference_test.js"))?;
let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?;
let js_assertion = test.with_extension("js");
let wat_assertion = test.with_extension("wat");
if env::var("BLESS").is_ok() {
fs::write(js_assertion, js)?;
fs::write(wat_assertion, wat)?;
return Ok(());
if interface_types {
let wasm = td.path().join("reference_test.wasm");
wit_validator::validate(&fs::read(&wasm)?)?;
let wit = sanitize_wasm(&wasm)?;
assert_same(&wit, &test.with_extension("wit"))?;
} else {
let js = fs::read_to_string(td.path().join("reference_test.js"))?;
assert_same(&js, &test.with_extension("js"))?;
let wat = sanitize_wasm(&td.path().join("reference_test_bg.wasm"))?;
assert_same(&wat, &test.with_extension("wat"))?;
}
let js_expected = fs::read_to_string(&js_assertion)?;
let wat_expected = fs::read_to_string(&wat_assertion)?;
diff(&js_expected, &js)?;
diff(&wat_expected, &wat)?;
Ok(())
}
fn assert_same(output: &str, expected: &Path) -> Result<()> {
if env::var("BLESS").is_ok() {
fs::write(expected, output)?;
} else {
let expected = fs::read_to_string(&expected)?;
diff(&expected, output)?;
}
Ok(())
}
@ -127,7 +135,9 @@ fn sanitize_wasm(wasm: &Path) -> Result<String> {
// Clean up the wasm module by removing all function
// implementations/instructions, data sections, etc. This'll help us largely
// only deal with exports/imports which is all we're really interested in.
let mut module = walrus::Module::from_file(wasm)?;
let mut module = walrus::ModuleConfig::new()
.on_parse(wit_walrus::on_parse)
.parse_file(wasm)?;
for func in module.funcs.iter_mut() {
let local = match &mut func.kind {
walrus::FunctionKind::Local(l) => l,
@ -155,7 +165,7 @@ fn sanitize_wasm(wasm: &Path) -> Result<String> {
module.exports.delete(id);
}
walrus::passes::gc::run(&mut module);
let mut wat = wasmprinter::print_bytes(&module.emit_wasm())?;
let mut wat = wit_printer::print_bytes(&module.emit_wasm())?;
wat.push_str("\n");
Ok(wat)
}