Skip to content

Commit 2413c71

Browse files
authored
Test a wasm target in CI (#1112)
* Scrub platform-specific errors from wasm-compose tests Helps them be a bit more cross-platform by not asserting the precise error message is the same across platforms. * Remove libfuzzer-sys dep from wasm-smith on wasm Fuzzing doesn't compile for the wasm target and this is only required for a doc test which doesn't run on wasm anyway. * Don't build `wasm-tools shrink` for wasm This relies on process support which wasm/WASI don't have, so always omit this subcommand. * Skip the `cli` test on wasm targets This involves spawning processes which wasm can't do * Fix wasm-mutate's tests on 32-bit targets The tests are currently tuned for 64-bit targets presumably due to the pattern of the RNG which differs based on the size of `usize`. This updates some limits and tests to pass on 32-bit, namely wasm, as well. * Test on wasm32-wasi in CI * Minimize some assertions Keep the same limits for 64-bit targets but don't make the assertions platform-specific.
1 parent 4a568a3 commit 2413c71

File tree

14 files changed

+85
-23
lines changed

14 files changed

+85
-23
lines changed

.github/workflows/main.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ jobs:
4848
- run: cmake -S ${{github.workspace}}/examples -B ${{github.workspace}}/examples/build -DCMAKE_BUILD_TYPE=Release
4949
- run: cmake --build ${{github.workspace}}/examples/build --config Release
5050

51+
wasm:
52+
name: Test on WebAssembly
53+
runs-on: ubuntu-latest
54+
steps:
55+
- uses: actions/checkout@v2
56+
with:
57+
submodules: true
58+
- name: Install Rust
59+
run: rustup update stable --no-self-update && rustup default stable
60+
- run: rustup target add wasm32-wasi
61+
- run: |
62+
tag=v10.0.1
63+
curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/${tag}/wasmtime-${tag}-x86_64-linux.tar.xz
64+
tar xf wasmtime-${tag}-x86_64-linux.tar.xz
65+
echo `pwd`/wasmtime-${tag}-x86_64-linux >> $GITHUB_PATH
66+
echo CARGO_TARGET_WASM32_WASI_RUNNER='wasmtime run --dir . --' >> $GITHUB_ENV
67+
echo CARGO_BUILD_TARGET='wasm32-wasi' >> $GITHUB_ENV
68+
- run: |
69+
cargo test --workspace \
70+
--exclude fuzz-stats \
71+
--exclude wasm-tools-fuzz \
72+
--exclude wasm-mutate-stats
73+
5174
rustfmt:
5275
name: Rustfmt
5376
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ wasm-smith = { workspace = true, features = ["_internal_cli"], optional = true }
8484

8585
# Dependencies of `shrink`
8686
wasm-shrink = { workspace = true, features = ["clap"], optional = true }
87-
is_executable = { version = "1.0.1", optional = true }
8887

8988
# Dependencies of `mutate`
9089
wasm-mutate = { workspace = true, features = ["clap"], optional = true }
@@ -117,6 +116,9 @@ wit-smith = { workspace = true, features = ["clap"], optional = true }
117116
addr2line = { version = "0.20.0", optional = true }
118117
gimli = { version = "0.27.2", optional = true }
119118

119+
[target.'cfg(not(target_family = "wasm"))'.dependencies]
120+
is_executable = { version = "1.0.1", optional = true }
121+
120122
[dev-dependencies]
121123
serde_json = "1.0"
122124
tempfile = "3.1"

crates/wasm-compose/tests/compose.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::{bail, Context, Result};
22
use pretty_assertions::assert_eq;
33
use std::fs;
4+
use std::io;
45
use wasm_compose::{composer::ComponentComposer, config::Config};
56
use wasmparser::{Validator, WasmFeatures};
67

@@ -51,13 +52,29 @@ fn component_composing() -> Result<()> {
5152
let (output, baseline_path) = if error_path.is_file() {
5253
match r {
5354
Ok(_) => bail!("composition should fail for test case `{}`", test_case),
54-
Err(e) => (
55-
format!("{:?}", e).replace('\\', "/").replace(
56-
"The system cannot find the file specified.",
57-
"No such file or directory",
58-
),
59-
&error_path,
60-
),
55+
Err(e) => {
56+
let mut err = format!("{e}");
57+
let causes = e.chain();
58+
let ncauses = causes.len();
59+
if ncauses > 1 {
60+
err.push_str("\n\nCaused by:");
61+
}
62+
for (i, cause) in causes.skip(1).enumerate() {
63+
err.push_str("\n ");
64+
if ncauses > 2 {
65+
err.push_str(&format!("{i}: "))
66+
}
67+
match cause.downcast_ref::<io::Error>() {
68+
Some(_) => {
69+
err.push_str("platform-specific error");
70+
}
71+
None => {
72+
err.push_str(&cause.to_string());
73+
}
74+
}
75+
}
76+
(err.replace('\\', "/"), &error_path)
77+
}
6178
}
6279
} else {
6380
let bytes =

crates/wasm-compose/tests/compositions/component-missing/error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ failed to parse component `tests/compositions/component-missing/a.wat`
22

33
Caused by:
44
0: failed to read from `tests/compositions/component-missing/a.wat`
5-
1: No such file or directory (os error 2)
5+
1: platform-specific error

crates/wasm-compose/tests/compositions/component-not-wat/error.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ failed to parse component `tests/compositions/component-not-wat/root.wat`
22

33
Caused by:
44
expected `(`
5-
--> tests/compositions/component-not-wat/root.wat:1:1
6-
|
7-
1 | not valid
8-
| ^
5+
--> tests/compositions/component-not-wat/root.wat:1:1
6+
|
7+
1 | not valid
8+
| ^

crates/wasm-compose/tests/compositions/missing-explicit-dep/error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ failed to parse component `tests/compositions/missing-explicit-dep/a.wat`
22

33
Caused by:
44
0: failed to read from `tests/compositions/missing-explicit-dep/a.wat`
5-
1: No such file or directory (os error 2)
5+
1: platform-specific error

crates/wasm-compose/tests/compositions/missing-root/error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ failed to parse component `tests/compositions/missing-root/root.wat`
22

33
Caused by:
44
0: failed to read from `tests/compositions/missing-root/root.wat`
5-
1: No such file or directory (os error 2)
5+
1: platform-specific error

crates/wasm-mutate/src/mutators.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl WasmMutate<'_> {
136136

137137
assert!(can_mutate);
138138

139-
let attempts = 100;
139+
let attempts = 2000;
140140
let mut last_mutation = None;
141141

142142
for _ in 0..attempts {

crates/wasm-mutate/src/mutators/peephole.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ mod tests {
569569

570570
/// Condition to apply the unfold operator
571571
/// check that the var is a constant
572+
#[allow(dead_code)]
572573
fn is_const(vari: &'static str) -> impl Fn(&mut EG, Id, &Subst) -> bool {
573574
move |egraph: &mut EG, _, subst| {
574575
let var = vari.parse();
@@ -608,7 +609,11 @@ mod tests {
608609
}
609610
}
610611

612+
// Random numbers vary by pointer-width presumably due to `usize` at some
613+
// point factoring in, and this test is only known to pass within a
614+
// reasonable amount of time on 64-bit platforms.
611615
#[test]
616+
#[cfg(target_pointer_width = "64")]
612617
fn test_peep_unfold2() {
613618
let rules: &[Rewrite<super::Lang, PeepholeMutationAnalysis>] = &[
614619
rewrite!("unfold-2"; "?x" => "(i32.unfold ?x)" if is_const("?x") if is_type("?x", PrimitiveTypeInfo::I32)),
@@ -1588,7 +1593,7 @@ mod tests {
15881593
seed: u64,
15891594
) {
15901595
let mut config = WasmMutate::default();
1591-
config.fuel(300);
1596+
config.fuel(10000);
15921597
config.seed(seed);
15931598

15941599
let mutator = PeepholeMutator::new_with_rules(3, rules.to_vec());

crates/wasm-smith/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ wasmparser = { workspace = true }
2626

2727
[dev-dependencies]
2828
criterion = { workspace = true }
29-
libfuzzer-sys = { workspace = true }
3029
rand = { workspace = true }
3130
wasmprinter = { path = "../wasmprinter" }
3231
wat = { path = "../wat" }
3332

33+
[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
34+
libfuzzer-sys = { workspace = true }
35+
3436
[features]
3537
_internal_cli = ["serde", "flagset/serde"]

0 commit comments

Comments
 (0)