From 7a28de9e9b43a3db0caa1e382a07abb0915eb30f Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Mon, 28 Oct 2024 11:50:33 -0600 Subject: [PATCH] update and fix examples (#120) An earlier commit which updated various dependencies (most notably, Wasmtime) unmasked a bug in `add_wasi_and_stubs` such that we were overwriting the host functions added by `wasmtime-wasi` with trapping stubs. This wasn't a problem previously because there was only one version of WASIp2: 0.2.0. Now that we have 0.2.1, 0.2.2, etc., with corresponding support in Wasmtime, we need to be a bit more careful. Signed-off-by: Joel Dice --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/cli/README.md | 10 +++---- examples/http/README.md | 10 +++---- examples/matrix-math/README.md | 10 +++---- examples/sandbox/README.md | 6 ++--- examples/tcp/README.md | 10 +++---- pyproject.toml | 2 +- src/lib.rs | 49 +++++++++++++++++++++++----------- 9 files changed, 59 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 45fa439..318488d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -391,7 +391,7 @@ dependencies = [ [[package]] name = "componentize-py" -version = "0.15.0" +version = "0.15.1" dependencies = [ "anyhow", "async-trait", diff --git a/Cargo.toml b/Cargo.toml index 53b309f..92339f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "componentize-py" -version = "0.15.0" +version = "0.15.1" edition = "2021" exclude = ["cpython"] diff --git a/examples/cli/README.md b/examples/cli/README.md index 887178d..7ae316c 100644 --- a/examples/cli/README.md +++ b/examples/cli/README.md @@ -9,16 +9,16 @@ run a Python-based component targetting the [wasi-cli] `command` world. ## Prerequisites -* `Wasmtime` 18.0.0 or later -* `componentize-py` 0.15.0 +* `Wasmtime` 26.0.0 or later +* `componentize-py` 0.15.1 Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If you don't have `cargo`, you can download and install from -https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0. +https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0. ``` -cargo install --version 18.0.0 wasmtime-cli -pip install componentize-py==0.15.0 +cargo install --version 26.0.0 wasmtime-cli +pip install componentize-py==0.15.1 ``` ## Running the demo diff --git a/examples/http/README.md b/examples/http/README.md index 7376fd1..f7a9d91 100644 --- a/examples/http/README.md +++ b/examples/http/README.md @@ -9,16 +9,16 @@ run a Python-based component targetting the [wasi-http] `proxy` world. ## Prerequisites -* `Wasmtime` 18.0.0 or later -* `componentize-py` 0.15.0 +* `Wasmtime` 26.0.0 or later +* `componentize-py` 0.15.1 Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If you don't have `cargo`, you can download and install from -https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0. +https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0. ``` -cargo install --version 18.0.0 wasmtime-cli -pip install componentize-py==0.15.0 +cargo install --version 26.0.0 wasmtime-cli +pip install componentize-py==0.15.1 ``` ## Running the demo diff --git a/examples/matrix-math/README.md b/examples/matrix-math/README.md index a75915b..50793db 100644 --- a/examples/matrix-math/README.md +++ b/examples/matrix-math/README.md @@ -10,8 +10,8 @@ within a guest component. ## Prerequisites -* `wasmtime` 18.0.0 or later -* `componentize-py` 0.15.0 +* `wasmtime` 26.0.0 or later +* `componentize-py` 0.15.1 * `NumPy`, built for WASI Note that we use an unofficial build of NumPy since the upstream project does @@ -19,11 +19,11 @@ not yet publish WASI builds. Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If you don't have `cargo`, you can download and install from -https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0. +https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0. ``` -cargo install --version 18.0.0 wasmtime-cli -pip install componentize-py==0.15.0 +cargo install --version 26.0.0 wasmtime-cli +pip install componentize-py==0.15.1 curl -OL https://github.com/dicej/wasi-wheels/releases/download/v0.0.1/numpy-wasi.tar.gz tar xf numpy-wasi.tar.gz ``` diff --git a/examples/sandbox/README.md b/examples/sandbox/README.md index 412b958..9ab7ec8 100644 --- a/examples/sandbox/README.md +++ b/examples/sandbox/README.md @@ -7,11 +7,11 @@ sandboxed Python code snippets from within a Python app. ## Prerequisites -* `wasmtime-py` 18.0.0 or later -* `componentize-py` 0.15.0 +* `wasmtime-py` 25.0.0 or later +* `componentize-py` 0.15.1 ``` -pip install componentize-py==0.15.0 wasmtime==18.0.2 +pip install componentize-py==0.15.1 wasmtime==25.0.0 ``` ## Running the demo diff --git a/examples/tcp/README.md b/examples/tcp/README.md index fb910a5..947c473 100644 --- a/examples/tcp/README.md +++ b/examples/tcp/README.md @@ -10,16 +10,16 @@ making an outbound TCP request using `wasi-sockets`. ## Prerequisites -* `Wasmtime` 18.0.0 or later -* `componentize-py` 0.15.0 +* `Wasmtime` 26.0.0 or later +* `componentize-py` 0.15.1 Below, we use [Rust](https://rustup.rs/)'s `cargo` to install `Wasmtime`. If you don't have `cargo`, you can download and install from -https://github.com/bytecodealliance/wasmtime/releases/tag/v18.0.0. +https://github.com/bytecodealliance/wasmtime/releases/tag/v26.0.0. ``` -cargo install --version 18.0.0 wasmtime-cli -pip install componentize-py==0.15.0 +cargo install --version 26.0.0 wasmtime-cli +pip install componentize-py==0.15.1 ``` ## Running the demo diff --git a/pyproject.toml b/pyproject.toml index 0af9ac7..b57cf14 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ features = ["pyo3/extension-module"] [project] name = "componentize-py" -version = "0.15.0" +version = "0.15.1" description = "Tool to package Python applications as WebAssembly components" readme = "README.md" license = { file = "LICENSE" } diff --git a/src/lib.rs b/src/lib.rs index d9b93ed..0156507 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -601,25 +601,32 @@ fn add_wasi_and_stubs( for (interface_name, stubs) in stubs { if let Some(interface_name) = interface_name { - if let Ok(mut instance) = linker.instance(&interface_name) { - for stub in stubs { - let interface_name = interface_name.clone(); - match stub { - Stub::Function(name) => instance.func_new(name, { - let name = name.clone(); - move |_, _, _| { - Err(anyhow!("called trapping stub: {interface_name}#{name}")) - } - }), - Stub::Resource(name) => instance - .resource(name, ResourceType::host::<()>(), { + // Note that we do _not_ stub interfaces which appear to be part of WASIp2 since those should be + // provided by the `wasmtime_wasi::add_to_linker_async` call above, and adding stubs to those same + // interfaces would just cause trouble. + if !is_wasip2_cli(&interface_name) { + if let Ok(mut instance) = linker.instance(&interface_name) { + for stub in stubs { + let interface_name = interface_name.clone(); + match stub { + Stub::Function(name) => instance.func_new(name, { let name = name.clone(); - move |_, _| { + move |_, _, _| { Err(anyhow!("called trapping stub: {interface_name}#{name}")) } - }) - .map(drop), - }?; + }), + Stub::Resource(name) => instance + .resource(name, ResourceType::host::<()>(), { + let name = name.clone(); + move |_, _| { + Err(anyhow!( + "called trapping stub: {interface_name}#{name}" + )) + } + }) + .map(drop), + }?; + } } } } else { @@ -643,3 +650,13 @@ fn add_wasi_and_stubs( Ok(()) } + +fn is_wasip2_cli(interface_name: &str) -> bool { + (interface_name.starts_with("wasi:cli/") + || interface_name.starts_with("wasi:clocks/") + || interface_name.starts_with("wasi:random/") + || interface_name.starts_with("wasi:io/") + || interface_name.starts_with("wasi:filesystem/") + || interface_name.starts_with("wasi:sockets/")) + && interface_name.contains("@0.2.") +}