Skip to content

Commit 8a8bc79

Browse files
authored
Add support for WASI p1 plugins to support Rust 1.91.0 (#1067)
* Add support for WASI p1 plugins to support Rust 1.91.0 * Permit StreamIO in P2 target * Update flake.nix * Add extern C to WASI p1 exports * Add unsafe to export name * Add documentation on stream IO API and WASI * Fix whitespace
1 parent 0c3faca commit 8a8bc79

33 files changed

Lines changed: 573 additions & 106 deletions

.cargo/config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# Cargo doesn't read directives in individual crates when invoking build
33
# commands from the workspace root, hence adding it at the workspace root.
44
# https://doc.rust-lang.org/cargo/reference/config.html
5+
[target.wasm32-wasip1]
6+
rustflags = ["-C", "target-feature=+simd128"]
7+
runner = "wasmtime --dir=."
8+
59
[target.wasm32-wasip2]
610
rustflags = ["-C", "target-feature=+simd128"]
711
runner = "wasmtime --dir=."

.github/workflows/build-assets.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ jobs:
3131
uses: actions/upload-artifact@v5
3232
with:
3333
name: plugin
34-
path: target/wasm32-wasip2/release/plugin.wasm
34+
path: target/wasm32-wasip1/release/plugin.wasm
3535

3636
- name: Wizen and archive wizened plugin
3737
run: |
38-
cargo run --package=javy-plugin-processing target/wasm32-wasip2/release/plugin.wasm target/wasm32-wasip2/release/plugin_wizened.wasm
39-
gzip -k -f target/wasm32-wasip2/release/plugin_wizened.wasm && mv target/wasm32-wasip2/release/plugin_wizened.wasm.gz plugin.wasm.gz
38+
cargo run --package=javy-plugin-processing target/wasm32-wasip1/release/plugin.wasm target/wasm32-wasip1/release/plugin_wizened.wasm
39+
gzip -k -f target/wasm32-wasip1/release/plugin_wizened.wasm && mv target/wasm32-wasip1/release/plugin_wizened.wasm.gz plugin.wasm.gz
4040
4141
- name: Upload archived plugin to artifacts
4242
uses: actions/upload-artifact@v5
@@ -124,7 +124,7 @@ jobs:
124124
- uses: actions/download-artifact@v6
125125
with:
126126
name: plugin
127-
path: target/wasm32-wasip2/release/
127+
path: target/wasm32-wasip1/release/
128128

129129
- name: Build CLI ${{ matrix.os }}
130130
run: cargo build --release --target ${{ matrix.target }} --package javy-cli

Cargo.lock

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"crates/plugin-processing",
99
"crates/test-invalid-plugin",
1010
"crates/test-macros",
11+
"crates/test-plugin-wasip1",
1112
"crates/test-plugin-wasip2",
1213
"crates/runner",
1314
"fuzz",
@@ -34,7 +35,6 @@ uuid = { version = "1.18", features = ["v4"] }
3435
serde = { version = "1.0", default-features = false }
3536
serde_json = "1.0"
3637
walrus = "0.24.3"
37-
wit-bindgen = "0.47.0"
3838
wasmparser = "0.240.0"
3939

4040
[profile.release]

Makefile

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ lint-wasi-targets: fmt-check
1414
--exclude=javy-codegen \
1515
--exclude=javy-plugin-processing \
1616
--exclude=javy-runner \
17+
--exclude=javy-test-plugin-wasip2
18+
--exclude=javy-fuzz \
19+
--target=wasm32-wasip1 --all-targets --all-features -- -D warnings
20+
cargo clippy --workspace \
21+
--exclude=javy-cli \
22+
--exclude=javy-codegen \
23+
--exclude=javy-plugin \
24+
--exclude=javy-plugin-processing \
25+
--exclude=javy-runner \
26+
--exclude=javy-test-plugin-wasip1 \
1727
--exclude=javy-fuzz \
1828
--target=wasm32-wasip2 --all-targets --all-features -- -D warnings
1929

@@ -24,6 +34,18 @@ test-wasi-targets:
2434
--exclude=javy-plugin-processing \
2535
--exclude=javy-runner \
2636
--exclude=javy-fuzz \
37+
--exclude=javy-test-plugin-wasip1 \
38+
--exclude=javy-test-plugin-wasip2 \
39+
--exclude=javy-test-invalid-plugin \
40+
--target=wasm32-wasip1 --each-feature -- --nocapture
41+
cargo hack test --workspace \
42+
--exclude=javy-cli \
43+
--exclude=javy-codegen \
44+
--exclude=javy-plugin \
45+
--exclude=javy-plugin-processing \
46+
--exclude=javy-runner \
47+
--exclude=javy-fuzz \
48+
--exclude=javy-test-plugin-wasip1 \
2749
--exclude=javy-test-plugin-wasip2 \
2850
--exclude=javy-test-invalid-plugin \
2951
--target=wasm32-wasip2 --each-feature -- --nocapture
@@ -37,6 +59,7 @@ lint-native-targets: fmt-check build-default-plugin
3759
--exclude=javy-plugin-api \
3860
--exclude=javy-plugin \
3961
--exclude=javy-test-invalid-plugin \
62+
--exclude=javy-test-plugin-wasip1 \
4063
--exclude=javy-test-plugin-wasip2 \
4164
--release --all-targets --all-features -- -D warnings
4265

@@ -46,6 +69,7 @@ test-native-targets: build-default-plugin build-test-plugins
4669
--exclude=javy-plugin-api \
4770
--exclude=javy-plugin \
4871
--exclude=javy-test-invalid-plugin \
72+
--exclude=javy-test-plugin-wasip1 \
4973
--exclude=javy-test-plugin-wasip2 \
5074
--release --each-feature -- --nocapture
5175

@@ -68,8 +92,12 @@ test-all: wasi-targets native-targets test-wpt
6892
target/release/javy: build-default-plugin
6993
CARGO_PROFILE_RELEASE_LTO=off cargo build -p=javy-cli --release
7094

71-
target/wasm32-wasip2/release/plugin.wasm:
72-
cargo build -p=javy-plugin --target=wasm32-wasip2 --release
95+
target/wasm32-wasip1/release/plugin.wasm:
96+
cargo build -p=javy-plugin --target=wasm32-wasip1 --release
97+
98+
target/wasm32-wasip1/release/test_plugin.wasm:
99+
cargo build -p=javy-test-plugin-wasip1 --target=wasm32-wasip1 --release
100+
cargo run --package=javy-plugin-processing --release -- target/wasm32-wasip1/release/test_plugin.wasm target/wasm32-wasip1/release/test_plugin.wasm
73101

74102
target/wasm32-wasip2/release/test_plugin.wasm:
75103
cargo build -p=javy-test-plugin-wasip2 --target=wasm32-wasip2 --release
@@ -81,10 +109,10 @@ target/wasm32-unknown-unknown/release/test_invalid_plugin.wasm:
81109
cli: target/release/javy
82110

83111
# Build the default plugin
84-
build-default-plugin: target/wasm32-wasip2/release/plugin.wasm
112+
build-default-plugin: target/wasm32-wasip1/release/plugin.wasm
85113

86114
# Build auxiliary plugins, for testing
87-
build-test-plugins: target/wasm32-wasip2/release/plugin.wasm target/wasm32-wasip2/release/test_plugin.wasm target/wasm32-unknown-unknown/release/test_invalid_plugin.wasm
115+
build-test-plugins: build-default-plugin target/wasm32-wasip1/release/test_plugin.wasm target/wasm32-wasip2/release/test_plugin.wasm target/wasm32-unknown-unknown/release/test_invalid_plugin.wasm
88116

89117
# === Misc ===
90118
clean:

crates/cli/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ javy-plugin-processing = { path = "../plugin-processing" }
2323

2424
[dev-dependencies]
2525
brotli = { workspace = true }
26-
wasmparser = { workspace = true }
2726
javy-runner = { path = "../runner/" }
2827
javy-test-macros = { path = "../test-macros/" }
2928
wasmtime-wasi = { workspace = true }

crates/cli/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn copy_plugin() -> Result<()> {
3434
.unwrap()
3535
.parent()
3636
.unwrap()
37-
.join("target/wasm32-wasip2/release");
37+
.join("target/wasm32-wasip1/release");
3838
let plugin_path = module_path.join("plugin.wasm");
3939
let plugin_wizened_path = module_path.join("plugin_wizened.wasm");
4040

crates/cli/src/plugin.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ impl<'a> UninitializedPlugin<'a> {
5353
}
5454

5555
fn validate(plugin_bytes: &'a [u8]) -> Result<()> {
56-
let plugin_bytes = match javy_plugin_processing::extract_core_module(plugin_bytes) {
57-
Err(e) => bail!("Could not process plugin: {e}"),
58-
Ok(plugin_bytes) => plugin_bytes,
59-
};
56+
let plugin_bytes =
57+
match javy_plugin_processing::extract_core_module_if_necessary(plugin_bytes) {
58+
Err(e) => bail!("Could not process plugin: {e}"),
59+
Ok(plugin_bytes) => plugin_bytes,
60+
};
6061
Plugin::validate(&plugin_bytes)
6162
}
6263
}
@@ -74,25 +75,25 @@ mod tests {
7475
let error = UninitializedPlugin::new(&[]).err().unwrap();
7576
assert_eq!(
7677
error.to_string(),
77-
"Could not process plugin: Expected Wasm component, received unknown file type"
78+
"Could not process plugin: Expected Wasm module or component, received unknown file type"
7879
);
7980
Ok(())
8081
}
8182

8283
#[test]
83-
fn test_validate_plugin_with_module() -> Result<()> {
84+
fn test_validate_plugin_with_module_with_everything_missing() -> Result<()> {
8485
let mut module = walrus::Module::with_config(ModuleConfig::default());
8586
let plugin_bytes = module.emit_wasm();
8687
let error = UninitializedPlugin::new(&plugin_bytes).err().unwrap();
8788
assert_eq!(
8889
error.to_string(),
89-
"Could not process plugin: Expected Wasm component, received Wasm module"
90+
"Could not process plugin: missing export for function named `initialize-runtime`, missing export for function named `compile-src`, missing export for function named `invoke`, missing exported memory named `memory`, missing custom section named `import_namespace`"
9091
);
9192
Ok(())
9293
}
9394

9495
#[test]
95-
fn test_validate_plugin_with_everything_missing() -> Result<()> {
96+
fn test_validate_plugin_with_component_with_everything_missing() -> Result<()> {
9697
let mut empty_module = walrus::Module::with_config(ModuleConfig::default());
9798
let plugin_bytes = encode_as_component(&empty_module.emit_wasm())?;
9899
let error = UninitializedPlugin::new(&plugin_bytes).err().unwrap();

crates/cli/tests/dynamic_linking_test.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,43 @@ fn test_using_runtime_flag_with_dynamic_triggers_error(builder: &mut Builder) ->
8585
Ok(())
8686
}
8787

88+
#[javy_cli_test(dyn = true)]
89+
fn test_using_wasip1_plugin_with_dynamic_works(builder: &mut Builder) -> Result<()> {
90+
let plugin = Plugin::UserWasiP1;
91+
let mut runner = builder
92+
.plugin(plugin)
93+
.preload(plugin.namespace().into(), plugin.path())
94+
.input("plugin.js")
95+
.build()?;
96+
97+
let result = runner.exec(vec![]);
98+
assert!(result.is_ok(), "Expected ok but got {result:?}");
99+
100+
Ok(())
101+
}
102+
103+
#[javy_cli_test(dyn = true)]
104+
fn test_using_wasip1_plugin_with_export_works(builder: &mut Builder) -> Result<()> {
105+
let plugin = Plugin::UserWasiP1;
106+
let mut runner = builder
107+
.plugin(plugin)
108+
.preload(plugin.namespace().into(), plugin.path())
109+
.input("plugin-exports.js")
110+
.wit("plugin-exports.wit")
111+
.world("plugin")
112+
.build()?;
113+
114+
let result = runner.exec_func("fn", vec![]);
115+
assert!(result.is_ok(), "Expected ok but got {result:?}");
116+
117+
Ok(())
118+
}
119+
88120
#[javy_cli_test(dyn = true)]
89121
fn test_using_wasip2_plugin_with_dynamic_works(builder: &mut Builder) -> Result<()> {
90122
let plugin = Plugin::UserWasiP2;
91123
let mut runner = builder
92-
.plugin(Plugin::UserWasiP2)
124+
.plugin(plugin)
93125
.preload(plugin.namespace().into(), plugin.path())
94126
.input("plugin.js")
95127
.build()?;
@@ -99,3 +131,20 @@ fn test_using_wasip2_plugin_with_dynamic_works(builder: &mut Builder) -> Result<
99131

100132
Ok(())
101133
}
134+
135+
#[javy_cli_test(dyn = true)]
136+
fn test_using_wasip2_plugin_with_export_works(builder: &mut Builder) -> Result<()> {
137+
let plugin = Plugin::UserWasiP2;
138+
let mut runner = builder
139+
.plugin(plugin)
140+
.preload(plugin.namespace().into(), plugin.path())
141+
.input("plugin-exports.js")
142+
.wit("plugin-exports.wit")
143+
.world("plugin")
144+
.build()?;
145+
146+
let result = runner.exec_func("fn", vec![]);
147+
assert!(result.is_ok(), "Expected ok but got {result:?}");
148+
149+
Ok(())
150+
}

0 commit comments

Comments
 (0)