Skip to content

Commit fc38fef

Browse files
committed
make a basic hello world work on wasip2
1 parent eb49cb1 commit fc38fef

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ degree documented below):
220220
- `solaris` / `illumos`: maintained by @devnexen. Supports the entire test suite.
221221
- `freebsd`: maintained by @YohDeadfall and @LorrensP-2158466. Supports the entire test suite.
222222
- `android`: **maintainer wanted**. Support very incomplete, but a basic "hello world" works.
223-
- `wasi`: **maintainer wanted**. Support very incomplete, not even standard output works, but an empty `main` function works.
223+
- `wasi`: **maintainer wanted**. Support very incomplete, but a basic "hello world" works.
224224
- For targets on other operating systems, Miri might fail before even reaching the `main` function.
225225

226226
However, even for targets that we do support, the degree of support for accessing platform APIs

ci/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ case $HOST_TARGET in
153153
BASIC="empty_main integer heap_alloc libc-mem vec string btreemap" # ensures we have the basics: pre-main code, system allocator
154154
UNIX="hello panic/panic panic/unwind concurrency/simple atomic libc-mem libc-misc libc-random env num_cpus" # the things that are very similar across all Unixes, and hence easily supported there
155155
TEST_TARGET=aarch64-linux-android run_tests_minimal $BASIC $UNIX time hashmap random thread sync concurrency epoll eventfd
156-
TEST_TARGET=wasm32-wasip2 run_tests_minimal $BASIC wasm
156+
TEST_TARGET=wasm32-wasip2 run_tests_minimal $BASIC hello wasm
157157
TEST_TARGET=wasm32-unknown-unknown run_tests_minimal no_std empty_main wasm # this target doesn't really have std
158158
TEST_TARGET=thumbv7em-none-eabihf run_tests_minimal no_std
159159
;;

src/shims/wasi/foreign_items.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,74 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
3535
this.write_pointer(res, dest)?;
3636
}
3737

38+
// Standard input/output
39+
// FIXME: These shims are hacks that just get basic stdout/stderr working. We can't
40+
// constrain them to "std" since std itself uses the wasi crate for this.
41+
"get-stdout" => {
42+
let [] =
43+
this.check_shim_sig(shim_sig!(extern "C" fn() -> i32), link_name, abi, args)?;
44+
this.write_scalar(Scalar::from_i32(1), dest)?; // POSIX FD number for stdout
45+
}
46+
"get-stderr" => {
47+
let [] =
48+
this.check_shim_sig(shim_sig!(extern "C" fn() -> i32), link_name, abi, args)?;
49+
this.write_scalar(Scalar::from_i32(2), dest)?; // POSIX FD number for stderr
50+
}
51+
"[resource-drop]output-stream" => {
52+
let [handle] =
53+
this.check_shim_sig(shim_sig!(extern "C" fn(i32) -> ()), link_name, abi, args)?;
54+
let handle = this.read_scalar(handle)?.to_i32()?;
55+
56+
if !(handle == 1 || handle == 2) {
57+
throw_unsup_format!("wasm output-stream: unsupported handle");
58+
}
59+
// We don't actually close these FDs, so this is a NOP.
60+
}
61+
"[method]output-stream.blocking-write-and-flush" => {
62+
let [handle, buf, len, ret_area] = this.check_shim_sig(
63+
shim_sig!(extern "C" fn(i32, *mut _, usize, *mut _) -> ()),
64+
link_name,
65+
abi,
66+
args,
67+
)?;
68+
let handle = this.read_scalar(handle)?.to_i32()?;
69+
let buf = this.read_pointer(buf)?;
70+
let len = this.read_target_usize(len)?;
71+
let ret_area = this.read_pointer(ret_area)?;
72+
73+
if len > 4096 {
74+
throw_unsup_format!(
75+
"wasm output-stream.blocking-write-and-flush: buffer too big"
76+
);
77+
}
78+
let len = usize::try_from(len).unwrap();
79+
let Some(fd) = this.machine.fds.get(handle) else {
80+
throw_unsup_format!(
81+
"wasm output-stream.blocking-write-and-flush: unsupported handle"
82+
);
83+
};
84+
fd.write(
85+
this.machine.communicate(),
86+
buf,
87+
len,
88+
this,
89+
callback!(
90+
@capture<'tcx> {
91+
len: usize,
92+
ret_area: Pointer,
93+
}
94+
|this, result: Result<usize, IoError>| {
95+
if !matches!(result, Ok(l) if l == len) {
96+
throw_unsup_format!("wasm output-stream.blocking-write-and-flush: returning errors is not supported");
97+
}
98+
// 0 in the first byte of the ret_area indicates success.
99+
let ret = this.ptr_to_mplace(ret_area, this.machine.layouts.u8);
100+
this.write_null(&ret)?;
101+
interp_ok(())
102+
}),
103+
)?;
104+
}
105+
38106
_ => return interp_ok(EmulateItemResult::NotSupported),
39107
}
40108
interp_ok(EmulateItemResult::NeedsReturn)

0 commit comments

Comments
 (0)