Skip to content

Commit b9b0ba4

Browse files
authored
add wasmtime-wasi-io and custom async executor to min-platform example (bytecodealliance#10128)
* add wasmtime-wasi-io and custom async executor to min-platform example * make it possible to find example from wasmtime-wasi-io docs prtest:full * fix comment * add wasm32-wasip2 target for min-platform ci and enable signals based traps when running with wasi disabled, because at the moment without signals based traps no native code can be loaded so the embedding never actually executes wasm. this ensures the heap size setting when not(feature = "wasi") is checked by execution * fix cbindgen version in ci
1 parent 2564177 commit b9b0ba4

File tree

12 files changed

+946
-5
lines changed

12 files changed

+946
-5
lines changed

.github/workflows/main.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,9 @@ jobs:
951951
with:
952952
submodules: true
953953
- uses: ./.github/actions/install-rust
954-
- run: cargo install cbindgen --vers "^0.27" --locked
954+
- run: cargo install cbindgen --vers "^0.28" --locked
955955
- run: rustup target add x86_64-unknown-none
956+
- run: rustup target add wasm32-wasip2
956957
- run: ./build.sh x86_64-unknown-none
957958
working-directory: ./examples/min-platform
958959

@@ -966,6 +967,12 @@ jobs:
966967
env:
967968
WASMTIME_SIGNALS_BASED_TRAPS: 1
968969

970+
- run: ./build.sh x86_64-unknown-none
971+
working-directory: ./examples/min-platform
972+
env:
973+
WASMTIME_SIGNALS_BASED_TRAPS: 1
974+
MIN_PLATFORM_TEST_DISABLE_WASI: 1
975+
969976
# Add the `wasmtime-platform.h` file as a release artifact
970977
- uses: actions/upload-artifact@v4
971978
with:

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/wasi-io/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
//! and write to streams.
1414
//!
1515
//! This crate is designed to have no unnecessary dependencies and, in
16-
//! particular, to be #![no_std].
16+
//! particular, to be #![no_std]. For an example no_std embedding, see
17+
//! [`/examples/min-platform`](https://github.com/bytecodealliance/wasmtime/tree/main/examples/min-platform)
18+
//! at the root of the wasmtime repo.
1719
1820
#![no_std]
1921

@@ -30,6 +32,9 @@ pub mod streams;
3032
#[doc(no_inline)]
3133
pub use async_trait::async_trait;
3234

35+
#[doc(no_inline)]
36+
pub use ::bytes;
37+
3338
use alloc::boxed::Box;
3439
use wasmtime::component::ResourceTable;
3540

examples/min-platform/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ object = { workspace = true, features = ['std'] }
1515
wasmtime = { workspace = true, features = ['cranelift', 'wat'] }
1616

1717
[features]
18+
default = ["wasi"]
1819
custom = []
20+
wasi = [ "wasmtime/component-model" ]

examples/min-platform/build.sh

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ if [ "$WASMTIME_SIGNALS_BASED_TRAPS" = "1" ]; then
2626
features="$features,custom"
2727
fi
2828

29+
if [ "$MIN_PLATFORM_EXAMPLE_DISABLE_WASI" != "1" ]; then
30+
features="$features,wasi"
31+
cargo build \
32+
--manifest-path=$REPO_DIR/examples/wasm/Cargo.toml \
33+
--target wasm32-wasip2 \
34+
--release
35+
WASI_EXAMPLE_PATH=$REPO_DIR/target/wasm32-wasip2/release/wasi.wasm
36+
fi
37+
2938
# First compile the C implementation of the platform symbols that will be
3039
# required by our embedding. This is the `embedding/wasmtime-platform.c` file.
3140
# The header file used is generated from Rust source code with the `cbindgen`
@@ -49,6 +58,7 @@ clang -shared -O2 -o "$HOST_DIR/libwasmtime-platform.so" "$EMBEDDING_DIR/wasmtim
4958
cargo build \
5059
--manifest-path $EMBEDDING_DIR/Cargo.toml \
5160
--target $target \
61+
--no-default-features \
5262
--features "$features" \
5363
--release
5464
cc \
@@ -61,7 +71,8 @@ cc \
6171

6272
# The final step here is running the host, in the current directory, which will
6373
# load the embedding and execute it.
64-
cargo run --manifest-path "$HOST_DIR/Cargo.toml" --release --features "$features" -- \
74+
cargo run --manifest-path "$HOST_DIR/Cargo.toml" --release --no-default-features --features "$features" -- \
6575
"$target" \
6676
"$HOST_DIR/libembedding.so" \
67-
"$HOST_DIR/libwasmtime-platform.so"
77+
"$HOST_DIR/libwasmtime-platform.so" \
78+
$WASI_EXAMPLE_PATH

examples/min-platform/embedding/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ anyhow = { workspace = true }
1616
# format so `cranelift` and `wat` are enabled.
1717
wasmtime = { workspace = true, features = ['runtime'] }
1818

19+
# Following deps are only required for wasi portion:
20+
wasmtime-wasi-io = { workspace = true, optional = true }
21+
1922
# Memory allocator used in this example (not required, however)
2023
dlmalloc = "0.2.4"
2124

@@ -25,4 +28,10 @@ test = false
2528
doctest = false
2629

2730
[features]
31+
default = ["wasi"]
2832
custom = ['wasmtime/custom-virtual-memory', 'wasmtime/custom-native-signals']
33+
wasi = [
34+
'wasmtime/component-model',
35+
'wasmtime/async',
36+
'dep:wasmtime-wasi-io',
37+
]

examples/min-platform/embedding/src/allocator.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ unsafe impl GlobalAlloc for MyGlobalDmalloc {
6363
}
6464
}
6565

66+
#[cfg(not(feature = "wasi"))]
6667
const INITIAL_HEAP_SIZE: usize = 64 * 1024;
68+
// The wasi component requires a larger heap than the module tests
69+
#[cfg(feature = "wasi")]
70+
const INITIAL_HEAP_SIZE: usize = 4 * 1024 * 1024;
71+
6772
static mut INITIAL_HEAP: [u8; INITIAL_HEAP_SIZE] = [0; INITIAL_HEAP_SIZE];
6873
static mut INITIAL_HEAP_ALLOCATED: bool = false;
6974

examples/min-platform/embedding/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use wasmtime::{Engine, Instance, Linker, Module, Store};
1010
mod allocator;
1111
mod panic;
1212

13+
#[cfg(feature = "wasi")]
14+
mod wasi;
15+
1316
/// Entrypoint of this embedding.
1417
///
1518
/// This takes a number of parameters which are the precompiled module AOT

0 commit comments

Comments
 (0)