Skip to content

Commit 7141379

Browse files
committed
Convert some WebAssembly run-make tests to Rust
This commit rewrites a number of `run-make` tests centered around wasm to instead use `rmake.rs` and additionally use the `wasm32-wasip1` target instead of `wasm32-unknown-unknown`. Testing no longer requires Node.js and additionally uses the `wasmparser` crate from crates.io to parse outputs and power assertions.
1 parent d255c6a commit 7141379

File tree

35 files changed

+439
-336
lines changed

35 files changed

+439
-336
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,6 +3298,9 @@ dependencies = [
32983298
[[package]]
32993299
name = "run_make_support"
33003300
version = "0.0.0"
3301+
dependencies = [
3302+
"wasmparser",
3303+
]
33013304

33023305
[[package]]
33033306
name = "rust-demangler"

src/tools/run-make-support/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7+
wasmparser = "0.118.2"

src/tools/run-make-support/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ use std::env;
22
use std::path::{Path, PathBuf};
33
use std::process::{Command, Output};
44

5+
pub use wasmparser;
6+
7+
pub fn out_dir() -> PathBuf {
8+
env::var_os("TMPDIR").unwrap().into()
9+
}
10+
511
fn setup_common_build_cmd() -> Command {
612
let rustc = env::var("RUSTC").unwrap();
713
let mut cmd = Command::new(rustc);
8-
cmd.arg("--out-dir")
9-
.arg(env::var("TMPDIR").unwrap())
10-
.arg("-L")
11-
.arg(env::var("TMPDIR").unwrap());
14+
cmd.arg("--out-dir").arg(out_dir()).arg("-L").arg(out_dir());
1215
cmd
1316
}
1417

@@ -45,6 +48,11 @@ impl RustcInvocationBuilder {
4548
self
4649
}
4750

51+
pub fn args(&mut self, args: &[&str]) -> &mut RustcInvocationBuilder {
52+
self.cmd.args(args);
53+
self
54+
}
55+
4856
#[track_caller]
4957
pub fn run(&mut self) -> Output {
5058
let caller_location = std::panic::Location::caller();

tests/run-make/wasm-abi/Makefile

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/run-make/wasm-abi/foo.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/run-make/wasm-abi/host.wat

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(module
2+
(func (export "two_i32") (result i32 i32)
3+
i32.const 100
4+
i32.const 101)
5+
(func (export "two_i64") (result i64 i64)
6+
i64.const 102
7+
i64.const 103)
8+
(func (export "two_f32") (result f32 f32)
9+
f32.const 104
10+
f32.const 105)
11+
(func (export "two_f64") (result f64 f64)
12+
f64.const 106
13+
f64.const 107)
14+
15+
(func (export "mishmash") (result f64 f32 i32 i64 i32 i32)
16+
f64.const 108
17+
f32.const 109
18+
i32.const 110
19+
i64.const 111
20+
i32.const 112
21+
i32.const 113)
22+
)

tests/run-make/wasm-abi/rmake.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{out_dir, rustc};
4+
use std::path::Path;
5+
use std::process::Command;
6+
7+
fn main() {
8+
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
9+
return;
10+
}
11+
12+
rustc().arg("foo.rs").arg("--target=wasm32-wasip1").run();
13+
let file = out_dir().join("foo.wasm");
14+
15+
let has_wasmtime = match Command::new("wasmtime").arg("--version").output() {
16+
Ok(s) => s.status.success(),
17+
_ => false,
18+
};
19+
if !has_wasmtime {
20+
println!("skipping test, wasmtime isn't available");
21+
return;
22+
}
23+
24+
run(&file, "return_two_i32", "1\n2\n");
25+
run(&file, "return_two_i64", "3\n4\n");
26+
run(&file, "return_two_f32", "5\n6\n");
27+
run(&file, "return_two_f64", "7\n8\n");
28+
run(&file, "return_mishmash", "9\n10\n11\n12\n13\n14\n");
29+
run(&file, "call_imports", "");
30+
}
31+
32+
fn run(file: &Path, method: &str, expected_output: &str) {
33+
let output = Command::new("wasmtime")
34+
.arg("run")
35+
.arg("--preload=host=host.wat")
36+
.arg("--invoke")
37+
.arg(method)
38+
.arg(file)
39+
.output()
40+
.unwrap();
41+
assert!(output.status.success());
42+
assert_eq!(expected_output, String::from_utf8_lossy(&output.stdout));
43+
}

tests/run-make/wasm-custom-section/Makefile

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/run-make/wasm-custom-section/foo.js

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{out_dir, rustc, wasmparser};
4+
use std::collections::HashMap;
5+
6+
fn main() {
7+
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
8+
return;
9+
}
10+
11+
rustc().arg("foo.rs").arg("--target=wasm32-wasip1").run();
12+
rustc().arg("bar.rs").arg("--target=wasm32-wasip1").arg("-Clto").arg("-O").run();
13+
14+
let file = std::fs::read(&out_dir().join("bar.wasm")).unwrap();
15+
16+
let mut custom = HashMap::new();
17+
for payload in wasmparser::Parser::new(0).parse_all(&file) {
18+
let payload = payload.unwrap();
19+
if let wasmparser::Payload::CustomSection(s) = payload {
20+
let prev = custom.insert(s.name(), s.data());
21+
assert!(prev.is_none());
22+
}
23+
}
24+
25+
assert_eq!(custom.remove("foo"), Some(&[5, 6, 1, 2][..]));
26+
assert_eq!(custom.remove("bar"), Some(&[3, 4][..]));
27+
assert_eq!(custom.remove("baz"), Some(&[7, 8][..]));
28+
}

tests/run-make/wasm-custom-sections-opt/Makefile

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/run-make/wasm-custom-sections-opt/foo.js

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{out_dir, rustc, wasmparser};
4+
use std::collections::HashMap;
5+
use std::path::Path;
6+
7+
fn main() {
8+
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
9+
return;
10+
}
11+
12+
rustc().arg("foo.rs").arg("--target=wasm32-wasip1").arg("-O").run();
13+
verify(&out_dir().join("foo.wasm"));
14+
}
15+
16+
fn verify(path: &Path) {
17+
eprintln!("verify {path:?}");
18+
let file = std::fs::read(&path).unwrap();
19+
20+
let mut custom = HashMap::new();
21+
for payload in wasmparser::Parser::new(0).parse_all(&file) {
22+
let payload = payload.unwrap();
23+
if let wasmparser::Payload::CustomSection(s) = payload {
24+
let prev = custom.insert(s.name(), s.data());
25+
assert!(prev.is_none());
26+
}
27+
}
28+
29+
assert_eq!(custom.remove("foo"), Some(&[1, 2, 3, 4][..]));
30+
}

tests/run-make/wasm-export-all-symbols/Makefile

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
extern crate run_make_support;
2+
3+
use run_make_support::{out_dir, rustc, wasmparser};
4+
use std::collections::HashMap;
5+
use std::path::Path;
6+
use wasmparser::ExternalKind::*;
7+
8+
fn main() {
9+
if std::env::var("TARGET").unwrap() != "wasm32-wasip1" {
10+
return;
11+
}
12+
13+
test(&[]);
14+
test(&["-O"]);
15+
test(&["-Clto"]);
16+
}
17+
18+
fn test(args: &[&str]) {
19+
eprintln!("running with {args:?}");
20+
rustc().arg("bar.rs").arg("--target=wasm32-wasip1").args(args).run();
21+
rustc().arg("foo.rs").arg("--target=wasm32-wasip1").args(args).run();
22+
rustc().arg("main.rs").arg("--target=wasm32-wasip1").args(args).run();
23+
24+
verify_exports(
25+
&out_dir().join("foo.wasm"),
26+
&[("foo", Func), ("FOO", Global), ("memory", Memory)],
27+
);
28+
verify_exports(
29+
&out_dir().join("main.wasm"),
30+
&[
31+
("foo", Func),
32+
("FOO", Global),
33+
("_start", Func),
34+
("__main_void", Func),
35+
("memory", Memory),
36+
],
37+
);
38+
}
39+
40+
fn verify_exports(path: &Path, exports: &[(&str, wasmparser::ExternalKind)]) {
41+
println!("verify {path:?}");
42+
let file = std::fs::read(path).unwrap();
43+
let mut wasm_exports = HashMap::new();
44+
for payload in wasmparser::Parser::new(0).parse_all(&file) {
45+
let payload = payload.unwrap();
46+
if let wasmparser::Payload::ExportSection(s) = payload {
47+
for export in s {
48+
let export = export.unwrap();
49+
wasm_exports.insert(export.name, export.kind);
50+
}
51+
}
52+
}
53+
54+
eprintln!("found exports {wasm_exports:?}");
55+
56+
assert_eq!(exports.len(), wasm_exports.len());
57+
for (export, expected_kind) in exports {
58+
assert_eq!(wasm_exports[export], *expected_kind);
59+
}
60+
}

tests/run-make/wasm-export-all-symbols/verify.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/run-make/wasm-import-module/Makefile

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)