Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Update fibers to avoid no-return functions (#12928)

Alex Crichtonf3156fe00de0
diff --git a/crates/fiber/src/lib.rs b/crates/fiber/src/lib.rs
index ba0effdb9f..6192069778 100644
--- a/crates/fiber/src/lib.rs
+++ b/crates/fiber/src/lib.rs
@@ -255,7 +255,7 @@ impl<Resume, Yield, Return> Suspend<Resume, Yield, Return> {
inner: imp::Suspend,
initial: Resume,
func: impl FnOnce(Resume, &mut Suspend<Resume, Yield, Return>) -> Return,
- ) {
+ ) -> imp::Suspend {
let mut suspend = Suspend {
inner,
_phantom: PhantomData,
@@ -278,7 +278,8 @@ impl<Resume, Yield, Return> Suspend<Resume, Yield, Return> {
#[cfg(not(feature = "std"))]
let result = RunResult::Returned((func)(initial, &mut suspend));
- suspend.inner.exit::<Resume, Yield, Return>(result);
+ suspend.inner.start_exit::<Resume, Yield, Return>(result);
+ suspend.inner
}
}
@@ -431,7 +432,7 @@ mod tests {
#[test]
fn fiber_stack_max_size() {
- if cfg!(windows) {
+ if cfg!(windows) || cfg!(miri) {
return;
}
assert!(FiberStack::new(usize::MAX, true).is_err());
diff --git a/crates/fiber/src/miri.rs b/crates/fiber/src/miri.rs
index a504988826..8382318c2d 100644
--- a/crates/fiber/src/miri.rs
+++ b/crates/fiber/src/miri.rs
@@ -116,13 +116,17 @@ where
// Execute this fiber through `Suspend::execute` and once that's done
// deallocate the `state` that we have.
let state = Arc::into_raw(state);
- super::Suspend::<A, B, C>::execute(
+ let mut suspend = super::Suspend::<A, B, C>::execute(
Suspend {
state: state.cast(),
},
init,
func.0,
);
+ match suspend.block_until_notified::<A, B, C>() {
+ State::Exiting => {}
+ _ => unreachable!(),
+ }
unsafe {
drop(Arc::from_raw(state));
}
@@ -150,8 +154,7 @@ impl Fiber {
let state = state.clone();
let func = IgnoreSendSync(func);
move || run(state, func)
- })
- .unwrap()
+ })?
};
// Cast the fiber back into a raw pointer to lose the type parameters
@@ -210,7 +213,7 @@ impl Fiber {
}
impl Suspend {
- fn suspend<A, B, C>(&mut self, result: RunResult<A, B, C>) -> State<A, B, C> {
+ fn set_result<A, B, C>(&mut self, result: RunResult<A, B, C>) {
let state = unsafe { self.state() };
let mut lock = state.state.lock().unwrap();
@@ -219,9 +222,11 @@ impl Suspend {
assert!(matches!(*lock, State::None));
*lock = State::SuspendWith(result);
state.cond.notify_one();
+ }
- // Wait for the resumption to come back, which is returned from this
- // method.
+ fn block_until_notified<A, B, C>(&mut self) -> State<A, B, C> {
+ let state = unsafe { self.state() };
+ let mut lock = state.state.lock().unwrap();
lock = state
.cond
.wait_while(lock, |s| {
@@ -232,17 +237,18 @@ impl Suspend {
}
pub(crate) fn switch<A, B, C>(&mut self, result: RunResult<A, B, C>) -> A {
- match self.suspend(result) {
+ self.set_result(result);
+
+ // Wait for the resumption to come back, which is returned from this
+ // method.
+ match self.block_until_notified::<A, B, C>() {
State::ResumeWith(RunResult::Resuming(a)) => a,
_ => unreachable!(),
}
}
- pub(crate) fn exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
- match self.suspend(result) {
- State::Exiting => {}
- _ => unreachable!(),
- }
+ pub(crate) fn start_exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
+ self.set_result(result);
}
unsafe fn state<A, B, C>(&self) -> &SharedFiberState<A, B, C> {
diff --git a/crates/fiber/src/nostd.rs b/crates/fiber/src/nostd.rs
index 09c66ca523..594a918d61 100644
--- a/crates/fiber/src/nostd.rs
+++ b/crates/fiber/src/nostd.rs
@@ -115,14 +115,16 @@ pub struct Suspend {
top_of_stack: *mut u8,
}
-extern "C" fn fiber_start<F, A, B, C>(arg0: *mut u8, top_of_stack: *mut u8)
+extern "C" fn fiber_start<F, A, B, C>(arg0: *mut u8, top_of_stack: *mut u8) -> *mut u8
where
F: FnOnce(A, &mut super::Suspend<A, B, C>) -> C,
{
unsafe {
let inner = Suspend { top_of_stack };
let initial = inner.take_resume::<A, B, C>();
- super::Suspend::<A, B, C>::execute(inner, initial, Box::from_raw(arg0.cast::<F>()))
+ let inner =
+ super::Suspend::<A, B, C>::execute(inner, initial, Box::from_raw(arg0.cast::<F>()));
+ inner.top_of_stack
}
}
@@ -176,9 +178,10 @@ impl Suspend {
}
}
- pub(crate) fn exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
- self.switch(result);
- unreachable!();
+ pub(crate) fn start_exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
+ unsafe {
+ (*self.result_location::<A, B, C>()).set(result);
+ }
}
unsafe fn take_resume<A, B, C>(&self) -> A {
diff --git a/crates/fiber/src/stackswitch.rs b/crates/fiber/src/stackswitch.rs
index bf1842f225..a249e86c95 100644
--- a/crates/fiber/src/stackswitch.rs
+++ b/crates/fiber/src/stackswitch.rs
@@ -65,7 +65,7 @@ mod unsupported {
pub(crate) unsafe fn wasmtime_fiber_init(
_top_of_stack: *mut u8,
- _entry: extern "C" fn(*mut u8, *mut u8),
+ _entry: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
_entry_arg0: *mut u8,
) {
unreachable!();
diff --git a/crates/fiber/src/stackswitch/aarch64.rs b/crates/fiber/src/stackswitch/aarch64.rs
index f14e062884..c9ae046342 100644
--- a/crates/fiber/src/stackswitch/aarch64.rs
+++ b/crates/fiber/src/stackswitch/aarch64.rs
@@ -92,7 +92,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* x0 */) {
pub(crate) unsafe fn wasmtime_fiber_init(
top_of_stack: *mut u8,
- entry_point: extern "C" fn(*mut u8, *mut u8),
+ entry_point: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
entry_arg0: *mut u8, // x2
) {
#[repr(C)]
@@ -132,6 +132,7 @@ pub(crate) unsafe fn wasmtime_fiber_init(
x19: top_of_stack,
x20: entry_point as *mut u8,
x21: entry_arg0,
+ x22: wasmtime_fiber_switch_ as *mut u8,
// We set up the newly initialized fiber, so that it resumes
// execution from wasmtime_fiber_start(). As a result, we need a
@@ -208,6 +209,11 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
// ... and then we call the function! Note that this is a function call
// so our frame stays on the stack to backtrace through.
blr x20
+
+ // The entry function returns where to switch to as the final switch, so
+ // that's performed here in inline assembly.
+ blr x22
+
// Unreachable, here for safety. This should help catch unexpected
// behaviors. Use a noticeable payload so one can grep for it in the
// codebase.
diff --git a/crates/fiber/src/stackswitch/arm.rs b/crates/fiber/src/stackswitch/arm.rs
index fe1937f46c..d145178a73 100644
--- a/crates/fiber/src/stackswitch/arm.rs
+++ b/crates/fiber/src/stackswitch/arm.rs
@@ -36,7 +36,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* r0 */) {
pub(crate) unsafe fn wasmtime_fiber_init(
top_of_stack: *mut u8,
- entry_point: extern "C" fn(*mut u8, *mut u8),
+ entry_point: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
entry_arg0: *mut u8,
) {
#[repr(C)]
@@ -60,6 +60,7 @@ pub(crate) unsafe fn wasmtime_fiber_init(
unsafe {
let initial_stack = top_of_stack.cast::<InitialStack>().sub(1);
initial_stack.write(InitialStack {
+ r8: wasmtime_fiber_switch_ as *mut u8,
r9: entry_arg0,
r10: entry_point as *mut u8,
r11: top_of_stack,
@@ -103,6 +104,7 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
mov r1, r11
mov r0, r9
blx r10
+ blx r8
.cfi_endproc
",
);
diff --git a/crates/fiber/src/stackswitch/riscv32imac.rs b/crates/fiber/src/stackswitch/riscv32imac.rs
index 6072b6537e..eac7b84826 100644
--- a/crates/fiber/src/stackswitch/riscv32imac.rs
+++ b/crates/fiber/src/stackswitch/riscv32imac.rs
@@ -72,7 +72,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* a0 */) {
pub(crate) unsafe fn wasmtime_fiber_init(
top_of_stack: *mut u8,
- entry_point: extern "C" fn(*mut u8, *mut u8),
+ entry_point: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
entry_arg0: *mut u8,
) {
#[repr(C)]
@@ -106,6 +106,7 @@ pub(crate) unsafe fn wasmtime_fiber_init(
initial_stack.write(InitialStack {
s1: entry_point as *mut u8,
s2: entry_arg0,
+ s3: wasmtime_fiber_switch_ as *mut u8,
fp: top_of_stack,
ra: wasmtime_fiber_start as *mut u8,
last_sp: initial_stack.cast(),
@@ -147,6 +148,7 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
mv a0, s2
mv a1, fp
jalr s1
+ jalr s3
// .4byte 0 will cause panic.
// for safety just like x86_64.rs and riscv64.rs.
.4byte 0
diff --git a/crates/fiber/src/stackswitch/riscv64.rs b/crates/fiber/src/stackswitch/riscv64.rs
index f517995fd4..113a49ccbc 100644
--- a/crates/fiber/src/stackswitch/riscv64.rs
+++ b/crates/fiber/src/stackswitch/riscv64.rs
@@ -91,7 +91,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* a0 */) {
pub(crate) unsafe fn wasmtime_fiber_init(
top_of_stack: *mut u8,
- entry_point: extern "C" fn(*mut u8, *mut u8),
+ entry_point: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
entry_arg0: *mut u8,
) {
#[repr(C)]
@@ -126,6 +126,7 @@ pub(crate) unsafe fn wasmtime_fiber_init(
initial_stack.write(InitialStack {
s1: entry_point as *mut u8,
s2: entry_arg0,
+ s3: wasmtime_fiber_switch_ as *mut u8,
fp: top_of_stack,
ra: wasmtime_fiber_start as *mut u8,
last_sp: initial_stack.cast(),
@@ -143,10 +144,10 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
.cfi_escape 0x0f, /* DW_CFA_def_cfa_expression */ \
- 5, /* the byte length of this expression */ \
- 0x52, /* DW_OP_reg2 (sp) */ \
- 0x06, /* DW_OP_deref */ \
- 0x08, 0xd0, /* DW_OP_const1u 0xc8 */ \
+ 5, /* the byte length of this expression */ \
+ 0x52, /* DW_OP_reg2 (sp) */ \
+ 0x06, /* DW_OP_deref */ \
+ 0x08, 0xd0, /* DW_OP_const1u 0xc8 */ \
0x22 /* DW_OP_plus */
@@ -178,7 +179,9 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
mv a0, s2
mv a1, fp
- jalr s1
+ jalr s1 // entry_point
+ jalr s3 // wasmtime_fiber_switch_
+
// .4byte 0 will cause panic.
// for safety just like x86_64.rs.
.4byte 0
diff --git a/crates/fiber/src/stackswitch/s390x.rs b/crates/fiber/src/stackswitch/s390x.rs
index 0d376b4647..52bbdbd2cc 100644
--- a/crates/fiber/src/stackswitch/s390x.rs
+++ b/crates/fiber/src/stackswitch/s390x.rs
@@ -52,7 +52,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* x0 */) {
pub(crate) unsafe fn wasmtime_fiber_init(
top_of_stack: *mut u8,
- entry_point: extern "C" fn(*mut u8, *mut u8),
+ entry_point: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
entry_arg0: *mut u8,
) {
#[repr(C)]
@@ -104,6 +104,7 @@ pub(crate) unsafe fn wasmtime_fiber_init(
r6: top_of_stack,
r7: entry_point as *mut u8,
r8: entry_arg0,
+ r9: wasmtime_fiber_switch_ as *mut u8,
last_sp: initial_stack.cast(),
..InitialStack::default()
@@ -145,6 +146,10 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
// ... and then we call the function! Note that this is a function call so
// our frame stays on the stack to backtrace through.
basr %r14, %r7 // entry_point
+
+ // Perform the final switch.
+ basr %r14, %r9 // wasmtime_fiber_switch_
+
// .. technically we shouldn't get here, so just trap.
.word 0x0000
.cfi_endproc
diff --git a/crates/fiber/src/stackswitch/x86.rs b/crates/fiber/src/stackswitch/x86.rs
index 15b05ef92e..1a09240dd4 100644
--- a/crates/fiber/src/stackswitch/x86.rs
+++ b/crates/fiber/src/stackswitch/x86.rs
@@ -47,7 +47,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8) {
pub(crate) unsafe fn wasmtime_fiber_init(
top_of_stack: *mut u8,
- entry_point: extern "C" fn(*mut u8, *mut u8),
+ entry_point: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
entry_arg0: *mut u8,
) {
// Our stack from top-to-bottom looks like:
@@ -84,6 +84,7 @@ pub(crate) unsafe fn wasmtime_fiber_init(
let initial_stack = top_of_stack.cast::<InitialStack>().sub(1);
initial_stack.write(InitialStack {
ebp: entry_point as *mut u8,
+ esi: wasmtime_fiber_switch_ as *mut u8,
return_address: wasmtime_fiber_start as *mut u8,
arg1: entry_arg0,
arg2: top_of_stack,
@@ -112,8 +113,15 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
.cfi_rel_offset edi, -20
// Our arguments and stack alignment are all prepped by
- // `wasmtime_fiber_init`.
+ // `wasmtime_fiber_init`. After calling `entry_point` clean up the
+ // stack arguments.
call ebp
+ pop edi
+ pop edi
+
+ // Call `wasmtime_fiber_switch_`, pushing its argument onto the stack.
+ push eax
+ call esi
ud2
.cfi_endproc
",
diff --git a/crates/fiber/src/stackswitch/x86_64.rs b/crates/fiber/src/stackswitch/x86_64.rs
index eb3ec36980..5cea3ac715 100644
--- a/crates/fiber/src/stackswitch/x86_64.rs
+++ b/crates/fiber/src/stackswitch/x86_64.rs
@@ -50,7 +50,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* rdi */) {
pub(crate) unsafe fn wasmtime_fiber_init(
top_of_stack: *mut u8,
- entry_point: extern "C" fn(*mut u8, *mut u8),
+ entry_point: extern "C" fn(*mut u8, *mut u8) -> *mut u8,
entry_arg0: *mut u8,
) {
#[repr(C)]
@@ -72,6 +72,7 @@ pub(crate) unsafe fn wasmtime_fiber_init(
unsafe {
let initial_stack = top_of_stack.cast::<InitialStack>().sub(1);
initial_stack.write(InitialStack {
+ r13: wasmtime_fiber_switch_ as *mut u8,
r12: entry_arg0,
rbx: entry_point as *mut u8,
rbp: top_of_stack,
@@ -155,12 +156,18 @@ unsafe extern "C" fn wasmtime_fiber_start() -> ! {
// `wasmtime_fiber_init` routine arranged the various values to be
// materialized into the registers used here. Our job is to then move
// the values into the ABI-defined registers and call the entry-point.
- // Note that `call` is used here to leave this frame on the stack so we
- // can use the dwarf info here for unwinding. The trailing `ud2` is just
- // for safety.
mov rdi, r12
mov rsi, rbp
- call rbx
+ call rbx // entry_point
+
+ // Once the entrypoint has returned the final switch for this fiber is
+ // executed. The address of the routine is in `r13` and its argument is
+ // the return value of the startup routine.
+ mov rdi, rax
+ call r13 // wasmtime_fiber_switch_
+
+ // This should never be reached, but in case it accidentally does then
+ // terminate immediately.
ud2
.cfi_endproc
",
diff --git a/crates/fiber/src/unix.rs b/crates/fiber/src/unix.rs
index fc202a2e79..71fe9d4709 100644
--- a/crates/fiber/src/unix.rs
+++ b/crates/fiber/src/unix.rs
@@ -221,7 +221,7 @@ pub struct Suspend {
previous: asan::PreviousStack,
}
-extern "C" fn fiber_start<F, A, B, C>(arg0: *mut u8, top_of_stack: *mut u8)
+extern "C" fn fiber_start<F, A, B, C>(arg0: *mut u8, top_of_stack: *mut u8) -> *mut u8
where
F: FnOnce(A, &mut super::Suspend<A, B, C>) -> C,
{
@@ -235,7 +235,10 @@ where
previous,
};
let initial = inner.take_resume::<A, B, C>();
- super::Suspend::<A, B, C>::execute(inner, initial, Box::from_raw(arg0.cast::<F>()))
+ let mut inner =
+ super::Suspend::<A, B, C>::execute(inner, initial, Box::from_raw(arg0.cast::<F>()));
+ asan::fiber_exit(&mut inner.previous);
+ inner.top_of_stack
}
}
@@ -269,11 +272,7 @@ impl Fiber {
let addr = stack.top().unwrap().cast::<usize>().offset(-1);
addr.write(result as *const _ as usize);
- asan::fiber_switch(
- stack.top().unwrap(),
- false,
- &mut asan::PreviousStack::new(stack),
- );
+ asan::fiber_switch(stack.top().unwrap(), &mut asan::PreviousStack::new(stack));
// null this out to help catch use-after-free
addr.write(0);
@@ -286,22 +285,19 @@ impl Fiber {
impl Suspend {
pub(crate) fn switch<A, B, C>(&mut self, result: RunResult<A, B, C>) -> A {
unsafe {
- let is_finishing = match &result {
- RunResult::Returned(_) | RunResult::Panicked(_) => true,
- RunResult::Executing | RunResult::Resuming(_) | RunResult::Yield(_) => false,
- };
// Calculate 0xAff8 and then write to it
(*self.result_location::<A, B, C>()).set(result);
- asan::fiber_switch(self.top_of_stack, is_finishing, &mut self.previous);
+ asan::fiber_switch(self.top_of_stack, &mut self.previous);
self.take_resume::<A, B, C>()
}
}
- pub(crate) fn exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
- self.switch(result);
- unreachable!()
+ pub(crate) fn start_exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
+ unsafe {
+ (*self.result_location::<A, B, C>()).set(result);
+ }
}
unsafe fn take_resume<A, B, C>(&self) -> A {
@@ -378,23 +374,10 @@ mod asan {
/// final time; customizes how asan intrinsics are invoked.
/// * `prev` - the stack we're switching to initially and saves the
/// stack to return to upon resumption.
- pub unsafe fn fiber_switch(
- top_of_stack: *mut u8,
- is_finishing: bool,
- prev: &mut PreviousStack,
- ) {
+ pub unsafe fn fiber_switch(top_of_stack: *mut u8, prev: &mut PreviousStack) {
assert!(super::SUPPORTED_ARCH);
let mut private_asan_pointer = std::ptr::null_mut();
- // If this fiber is finishing then NULL is passed to asan to let it know
- // that it can deallocate the "fake stack" that it's tracking for this
- // fiber.
- let private_asan_pointer_ref = if is_finishing {
- None
- } else {
- Some(&mut private_asan_pointer)
- };
-
// NB: in fiddling with asan an optimizations and such it appears that
// these functions need to be "very close to each other". If other Rust
// functions are invoked or added as an abstraction here that appears to
@@ -402,12 +385,19 @@ mod asan {
// module as-is where this function exists to have these three
// functions very close to one another.
unsafe {
- __sanitizer_start_switch_fiber(private_asan_pointer_ref, prev.bottom, prev.size);
+ __sanitizer_start_switch_fiber(Some(&mut private_asan_pointer), prev.bottom, prev.size);
super::wasmtime_fiber_switch(top_of_stack);
__sanitizer_finish_switch_fiber(private_asan_pointer, &mut prev.bottom, &mut prev.size);
}
}
+ pub unsafe fn fiber_exit(prev: &mut PreviousStack) {
+ assert!(super::SUPPORTED_ARCH);
+ unsafe {
+ __sanitizer_start_switch_fiber(None, prev.bottom, prev.size);
+ }
+ }
+
/// Hook for when a fiber first starts, used to configure ASAN.
pub unsafe fn fiber_start_complete() -> PreviousStack {
let mut ret = PreviousStack::default();
@@ -523,17 +513,15 @@ mod asan_disabled {
}
}
- pub unsafe fn fiber_switch(
- top_of_stack: *mut u8,
- _is_finishing: bool,
- _prev: &mut PreviousStack,
- ) {
+ pub unsafe fn fiber_switch(top_of_stack: *mut u8, _prev: &mut PreviousStack) {
assert!(super::SUPPORTED_ARCH);
unsafe {
super::wasmtime_fiber_switch(top_of_stack);
}
}
+ pub unsafe fn fiber_exit(_prev: &mut PreviousStack) {}
+
#[inline]
pub unsafe fn fiber_start_complete() -> PreviousStack {
PreviousStack
diff --git a/crates/fiber/src/windows.rs b/crates/fiber/src/windows.rs
index f3d66c9460..2a85d1ca89 100644
--- a/crates/fiber/src/windows.rs
+++ b/crates/fiber/src/windows.rs
@@ -90,7 +90,14 @@ where
(*state).initial_closure.set(ptr::null_mut());
let suspend = Suspend { state };
let initial = suspend.take_resume::<A, B, C>();
- super::Suspend::<A, B, C>::execute(suspend, initial, *func);
+ let suspend = super::Suspend::<A, B, C>::execute(suspend, initial, *func);
+
+ let parent = (*suspend.state).parent.get();
+ debug_assert!(!parent.is_null());
+
+ // Technically this means that this function `fiber_start` never
+ // returns, but that's how the fibers API works on Windows it seems.
+ SwitchToFiber(parent);
}
}
@@ -187,9 +194,10 @@ impl Suspend {
}
}
- pub(crate) fn exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
- self.switch(result);
- unreachable!()
+ pub(crate) fn start_exit<A, B, C>(&mut self, result: RunResult<A, B, C>) {
+ unsafe {
+ (*self.result_location::<A, B, C>()).set(result);
+ }
}
unsafe fn take_resume<A, B, C>(&self) -> A {