Path: blob/main/crates/test-programs/src/bin/async_round_trip_stackful.rs
1693 views
#![expect(unsafe_op_in_unsafe_fn, reason = "old code, not worth updating yet")]12// This tests callback-less (AKA stackful) async exports.3//4// Testing this case using Rust's LLVM-based toolchain is tricky because, as of5// this writing, LLVM does not produce reentrance-safe code. Specifically, it6// allocates a single shadow stack for use whenever a program needs to take the7// address of a stack variable, which makes concurrent execution of multiple8// Wasm stacks in the same instance hazardous.9//10// Given the above, we write code directly against the component model ABI11// rather than use `wit-bindgen`, and we carefully avoid use of the shadow stack12// across yield points such as calls to `waitable-set.wait` in order to keep the13// code reentrant.1415mod bindings {16wit_bindgen::generate!({17path: "../misc/component-async-tests/wit",18world: "round-trip",19});20}2122use {23std::alloc::{self, Layout},24test_programs::async_::{25EVENT_SUBTASK, STATUS_RETURNED, subtask_drop, waitable_join, waitable_set_drop,26waitable_set_new, waitable_set_wait,27},28};2930#[cfg(target_arch = "wasm32")]31#[link(wasm_import_module = "[export]local:local/baz")]32unsafe extern "C" {33#[link_name = "[task-return][async]foo"]34fn task_return_foo(ptr: *mut u8, len: usize);35}36#[cfg(not(target_arch = "wasm32"))]37unsafe extern "C" fn task_return_foo(_ptr: *mut u8, _len: usize) {38unreachable!()39}4041#[cfg(target_arch = "wasm32")]42#[link(wasm_import_module = "local:local/baz")]43unsafe extern "C" {44#[link_name = "[async-lower][async]foo"]45fn import_foo(ptr: *mut u8, len: usize, results: *mut u8) -> u32;46}47#[cfg(not(target_arch = "wasm32"))]48unsafe extern "C" fn import_foo(_ptr: *mut u8, _len: usize, _results: *mut u8) -> u32 {49unreachable!()50}5152#[unsafe(export_name = "[async-lift-stackful]local:local/baz#[async]foo")]53unsafe extern "C" fn export_foo(ptr: *mut u8, len: usize) {54// Note that we're careful not to take the address of any stack-allocated55// value here. We need to avoid relying on the LLVM-generated shadow stack56// in order to correctly support reentrancy. It's okay to call functions57// which use the shadow stack, as long as they pop everything off before we58// reach a yield point such as a call to `waitable-set.wait`.5960let s = format!(61"{} - entered guest",62String::from_utf8(Vec::from_raw_parts(ptr, len, len)).unwrap()63);6465let layout = Layout::from_size_align(8, 4).unwrap();6667let results = alloc::alloc(layout);6869let result = import_foo(s.as_ptr().cast_mut(), s.len(), results);70let mut status = result & 0xf;71let call = result >> 4;72let set = waitable_set_new();73if call != 0 {74waitable_join(call, set);75}76while status != STATUS_RETURNED {77// Note the use of `Box` here to avoid taking the address of a stack78// allocation.79let payload = Box::into_raw(Box::new([0i32; 2]));80let event = waitable_set_wait(set, payload.cast());81let payload = Box::from_raw(payload);82if event == EVENT_SUBTASK {83assert_eq!(call, payload[0] as u32);84status = payload[1] as u32;85if status == STATUS_RETURNED {86subtask_drop(call);87waitable_set_drop(set);88}89}90}9192let len = *results.add(4).cast::<usize>();93let s = format!(94"{} - exited guest",95String::from_utf8(Vec::from_raw_parts(*results.cast::<*mut u8>(), len, len)).unwrap()96);97alloc::dealloc(results, layout);9899task_return_foo(s.as_ptr().cast_mut(), s.len());100}101102// Unused function; required since this file is built as a `bin`:103fn main() {}104105106