Skip to content

Commit ba341ab

Browse files
Add support for debug printing to the wasi_snapshot_preview1 adapter (#371)
* add a trivial host-wasmtime-rust impl for the testwasi.wit interface and use it in the gen-host-wasmtime-rust runtime tests * wasi-snapshot-preview1 adapter: export a `log` function that prints from fd_write Co-authored-by: Dan Gohman <[email protected]> * add a debug print to a runtime test, to show it works [phickey@pch-tower:crates/gen-host-wasmtime-rust]% cargo test -p wit-bindgen-gen-host-wasmtime-rust --test runtime -- unions_rust --nocapture Finished test [unoptimized + debuginfo] target(s) in 0.12s Running tests/runtime.rs (/home/phickey/src/wit-bindgen/target/debug/deps/runtime-d5315b66d02bf15e) running 1 test There is power in a union! test unions_rust::test ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 20 filtered out; finished in 0.31s * clarify Co-authored-by: Dan Gohman <[email protected]>
1 parent 5e5d63f commit ba341ab

File tree

9 files changed

+85
-4
lines changed

9 files changed

+85
-4
lines changed

Cargo.lock

Lines changed: 10 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ members = [
1010
"crates/wit-bindgen-demo",
1111
"crates/wit-component",
1212
"crates/wasi_snapshot_preview1",
13+
"crates/wasi_snapshot_preview1/host-wasmtime-rust",
1314
]
1415
resolver = "2"
1516

crates/gen-host-wasmtime-rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ test-helpers = { path = '../test-helpers' }
2020
wasmtime = { workspace = true }
2121
wasmtime-wasi = { workspace = true }
2222
wit-bindgen-host-wasmtime-rust = { workspace = true, features = ['tracing'] }
23+
wit-bindgen-testwasi-host-wasmtime-rust = { path = "../wasi_snapshot_preview1/host-wasmtime-rust" }
2324

2425
tracing = { version = "0.1.26" }
2526
tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"]}

crates/gen-host-wasmtime-rust/tests/runtime.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use wasmtime::{
55
component::{Component, Instance, Linker},
66
Config, Engine, Store,
77
};
8+
use wit_bindgen_testwasi_host_wasmtime_rust as testwasi;
89

910
test_helpers::runtime_tests_wasmtime!();
1011

@@ -20,6 +21,7 @@ fn default_config() -> Result<Config> {
2021

2122
struct Context<I> {
2223
imports: I,
24+
testwasi: testwasi::TestWasi,
2325
}
2426

2527
fn instantiate<I: Default, T>(
@@ -36,11 +38,13 @@ fn instantiate<I: Default, T>(
3638

3739
let mut linker = Linker::new(&engine);
3840
add_imports(&mut linker)?;
41+
testwasi::add_to_linker(&mut linker, |cx| &mut cx.testwasi)?;
3942

4043
let mut store = Store::new(
4144
&engine,
4245
Context {
4346
imports: I::default(),
47+
testwasi: testwasi::TestWasi::default(),
4448
},
4549
);
4650
let (exports, _instance) = mk_exports(&mut store, &module, &mut linker)?;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "wit-bindgen-testwasi-host-wasmtime-rust"
3+
authors = ["Alex Crichton <[email protected]>"]
4+
version.workspace = true
5+
edition.workspace = true
6+
7+
[dependencies]
8+
anyhow = { workspace = true }
9+
wasmtime = { workspace = true }
10+
wit-bindgen-host-wasmtime-rust = { workspace = true }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
wit_bindgen_host_wasmtime_rust::export!("../testwasi.wit");
2+
3+
pub use testwasi::add_to_linker;
4+
5+
#[derive(Default)]
6+
pub struct TestWasi;
7+
8+
impl testwasi::Testwasi for TestWasi {
9+
fn log(&mut self, bytes: Vec<u8>) {
10+
match std::str::from_utf8(&bytes) {
11+
Ok(s) => print!("{}", s),
12+
Err(_) => println!("\nbinary: {:?}", bytes),
13+
}
14+
}
15+
}

crates/wasi_snapshot_preview1/src/lib.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,30 @@
1313
//! imports will be adapted to a custom `wit-bindgen`-specific host `*.wit` file
1414
//! which is only suitable for `wit-bindgen` tests.
1515
16+
#![no_std]
1617
#![allow(unused_variables)]
1718

18-
use std::arch::wasm32::unreachable;
19+
use core::arch::wasm32::unreachable;
1920
use wasi::*;
2021

22+
wit_bindgen_guest_rust::import!({ paths: ["testwasi.wit"], no_std });
23+
24+
// Nothing in this wasm module should end up needing cabi_realloc. However, if
25+
// we don't define this trapping implementation of the export, we'll pull in
26+
// the one from wit_bindgen_guest_rust, which will pull in the libc allocator
27+
// and a bunch of panic related machinery from std, which will use vtables
28+
// and therefore create a Wasm ElementSection, which will make the resulting
29+
// wasm unusable as an adapter module.
30+
#[no_mangle]
31+
unsafe extern "C" fn cabi_realloc(
32+
old_ptr: *mut u8,
33+
old_len: usize,
34+
align: usize,
35+
new_len: usize,
36+
) -> *mut u8 {
37+
unreachable()
38+
}
39+
2140
#[no_mangle]
2241
pub extern "C" fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Errno {
2342
ERRNO_SUCCESS
@@ -58,11 +77,29 @@ pub extern "C" fn clock_time_get(
5877
#[no_mangle]
5978
pub extern "C" fn fd_write(
6079
fd: Fd,
61-
iovs_ptr: *const Ciovec,
62-
iovs_len: usize,
80+
mut iovs_ptr: *const Ciovec,
81+
mut iovs_len: usize,
6382
nwritten: *mut Size,
6483
) -> Errno {
65-
unreachable()
84+
unsafe {
85+
// Advance to the first non-empty buffer.
86+
while iovs_len != 0 && (*iovs_ptr).buf_len == 0 {
87+
iovs_ptr = iovs_ptr.add(1);
88+
iovs_len -= 1;
89+
}
90+
if iovs_len == 0 {
91+
*nwritten = 0;
92+
return ERRNO_SUCCESS;
93+
}
94+
95+
let ptr = (*iovs_ptr).buf;
96+
let len = (*iovs_ptr).buf_len;
97+
98+
testwasi::log(core::slice::from_raw_parts(ptr, len));
99+
100+
*nwritten = len;
101+
}
102+
ERRNO_SUCCESS
66103
}
67104

68105
#[no_mangle]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
log: func(bytes: list<u8>)

tests/runtime/unions/wasm.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ impl exports::Exports for Exports {
157157
identify_distinguishable_num(DistinguishableNum::I64(1)),
158158
1
159159
));
160+
161+
println!("There is power in a union!");
160162
}
161163

162164
fn add_one_integer(num: AllIntegers) -> AllIntegers {

0 commit comments

Comments
 (0)