A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
[stack-switching] Stack initialisation checks. (#15110)
Daniel Hillerströme9b470e6d770
diff --git a/crates/wasmtime/src/runtime/vm/stack_switching.rs b/crates/wasmtime/src/runtime/vm/stack_switching.rsindex 235af7d75a..b79e3b720f 100644--- a/crates/wasmtime/src/runtime/vm/stack_switching.rs+++ b/crates/wasmtime/src/runtime/vm/stack_switching.rs@@ -209,6 +209,8 @@ pub fn cont_new(result_count: u32,gc_refs: bool,) -> crate::Result<*mut VMContRef> {+ let runtime_headroom = stack::continuation_runtime_headroom(param_count, result_count)?;+let instance = store.instance_mut(instance);let caller_vmctx = instance.vmctx();@@ -233,6 +235,7 @@ pub fn cont_new(param_count,result_count,gc_refs,+ runtime_headroom,)?;// Now that the initial stack pointer was set by the initializationdiff --git a/crates/wasmtime/src/runtime/vm/stack_switching/stack.rs b/crates/wasmtime/src/runtime/vm/stack_switching/stack.rsindex e300ffd13c..a0698eb372 100644--- a/crates/wasmtime/src/runtime/vm/stack_switching/stack.rs+++ b/crates/wasmtime/src/runtime/vm/stack_switching/stack.rs@@ -2,11 +2,59 @@//! specialized for executing stack switching continuations.use crate::Result;+#[cfg(feature = "stack-switching")]+use crate::prelude::*;use core::ops::Range;use crate::runtime::vm::VMPayloads;+#[cfg(feature = "stack-switching")]+use crate::runtime::vm::ValRaw;+#[cfg(all(feature = "stack-switching", has_virtual_memory))]+use crate::runtime::vm::host_page_size;use crate::runtime::vm::{VMContext, VMFuncRef};+/// Calculate the native stack space needed to reach the+/// continuation's entry function.+///+/// On x86_64, the array-to-Wasm trampoline needs at most one+/// `ValRaw`-sized spill slot per live parameter or result and one+/// dynamically allocated outgoing slot per parameter for a core Wasm+/// entry continuation function. We retain one host page for the+/// continuation entry stub, Rust's `fiber_start` frame, and call+/// setup.+///+/// NOTE(dhil): It may be more robust to add stack frame metadata+/// directly to compiled artifacts, however, since stack-switching has+/// status of an experimental feature we deliberately opt for deriving+/// the bound based on the signature as it is non-invasive.+#[cfg(feature = "stack-switching")]+pub(crate) fn continuation_runtime_headroom(+ parameter_count: u32,+ return_value_count: u32,+) -> Result<usize> {+ let fixed_headroom = if cfg!(has_virtual_memory) {+ host_page_size()+ } else {+ // Targets without virtual memory use the dummy continuation+ // stack implementation, so this value is never used to+ // initialize a stack.+ 0usize+ };++ let slot_size = core::mem::size_of::<ValRaw>();+ let trampoline_values_size = usize::try_from(parameter_count.max(return_value_count))?+ .checked_mul(slot_size)+ .ok_or_else(|| format_err!("continuation trampoline stack size overflow"))?;+ let outgoing_args_size = usize::try_from(parameter_count)?+ .checked_mul(slot_size)+ .ok_or_else(|| format_err!("continuation parameter stack size overflow"))?;+ let headroom = fixed_headroom+ .checked_add(trampoline_values_size)+ .and_then(|n| n.checked_add(outgoing_args_size))+ .ok_or_else(|| format_err!("continuation runtime headroom overflow"))?;+ Ok(headroom)+}+cfg_select! {all(feature = "stack-switching", unix, target_arch = "x86_64") => {mod unix;@@ -116,6 +164,7 @@ impl VMContinuationStack {parameter_count: u32,return_value_count: u32,gc_refs: bool,+ runtime_headroom: usize,) -> Result<()> {self.0.initialize(func_ref,@@ -124,6 +173,7 @@ impl VMContinuationStack {parameter_count,return_value_count,gc_refs,+ runtime_headroom,)}}diff --git a/crates/wasmtime/src/runtime/vm/stack_switching/stack/dummy.rs b/crates/wasmtime/src/runtime/vm/stack_switching/stack/dummy.rsindex 8634ca69a0..ea937c2469 100644--- a/crates/wasmtime/src/runtime/vm/stack_switching/stack/dummy.rs+++ b/crates/wasmtime/src/runtime/vm/stack_switching/stack/dummy.rs@@ -68,6 +68,7 @@ impl VMContinuationStack {_parameter_count: u32,_return_value_count: u32,_gc_refs: bool,+ _runtime_headroom: usize,) -> Result<()> {Ok(())}diff --git a/crates/wasmtime/src/runtime/vm/stack_switching/stack/unix.rs b/crates/wasmtime/src/runtime/vm/stack_switching/stack/unix.rsindex ffc437a004..b3550a4915 100644--- a/crates/wasmtime/src/runtime/vm/stack_switching/stack/unix.rs+++ b/crates/wasmtime/src/runtime/vm/stack_switching/stack/unix.rs@@ -83,6 +83,77 @@ pub enum Allocator {#[cfg(asan)]static ASAN_STACKS: Mutex<Vec<(usize, usize)>> = Mutex::new(Vec::new());+struct ContinuationStackLayout {+ args_capacity: u32,+ args_data_size: usize,+ dynamic_data_size: usize,+ total_control_size: usize,+ required_stack_size: usize,+}++impl ContinuationStackLayout {+ fn new(+ parameter_count: u32,+ return_value_count: u32,+ gc_refs: bool,+ runtime_headroom: usize,+ ) -> Result<Self> {+ let args_capacity = std::cmp::max(parameter_count, return_value_count);+ let args_data_size = usize::try_from(args_capacity)?+ .checked_mul(std::mem::size_of::<ValRaw>())+ .ok_or_else(|| {+ format_err!(+ "continuation function type with {args_capacity} args \+ overflows stack control data size calculation"+ )+ })?;+ // Keep the fixed startup data 16-byte aligned.+ let gc_refs_data_size = if cfg!(feature = "gc") && gc_refs {+ usize::try_from(args_capacity)?+ .checked_add(15)+ .map(|s| s & !15)+ .ok_or_else(|| {+ format_err!(+ "continuation function type with {args_capacity} args \+ overflows stack control data size calculation"+ )+ })?+ } else {+ 0+ };+ let dynamic_data_size = args_data_size+ .checked_add(gc_refs_data_size)+ .ok_or_else(|| {+ format_err!(+ "continuation function type with {args_capacity} args \+ overflows stack control data size calculation"+ )+ })?;+ let total_control_size = dynamic_data_size.checked_add(0x40).ok_or_else(|| {+ format_err!(+ "continuation function type with {args_capacity} args \+ overflows stack control data size calculation"+ )+ })?;+ let required_stack_size = total_control_size+ .checked_add(runtime_headroom)+ .ok_or_else(|| {+ format_err!(+ "continuation function type with {args_capacity} args \+ overflows required stack size calculation"+ )+ })?;++ Ok(Self {+ args_capacity,+ args_data_size,+ dynamic_data_size,+ total_control_size,+ required_stack_size,+ })+ }+}+#[derive(Debug)]#[repr(C)]pub struct VMContinuationStack {@@ -291,7 +362,41 @@ impl VMContinuationStack {parameter_count: u32,return_value_count: u32,gc_refs: bool,+ runtime_headroom: usize,) -> Result<()> {+ let layout = ContinuationStackLayout::new(+ parameter_count,+ return_value_count,+ gc_refs,+ runtime_headroom,+ )?;++ // We must that ensure the control data (fixed header + args+ // buffer) fits within the usable stack space while leaving+ // enough room to enter and run the continuation.+ let usable_len = match self.allocator {+ Allocator::Mmap =>+ // A Mmap allocation includes a guard page, which is+ // not writable. Thus, we subtract the guard page to+ // calculate the effective usable stack space.+ {+ self.len.saturating_sub(host_page_size())+ }+ Allocator::Custom => self.len,+ };+ ensure!(+ layout.total_control_size <= usable_len,+ "continuation function type requires {} bytes of stack control data, \+ which exceeds the {usable_len}-byte usable stack allocation",+ layout.total_control_size,+ );+ ensure!(+ layout.required_stack_size <= usable_len,+ "continuation function type requires {} bytes of stack control data \+ and {runtime_headroom} bytes of runtime headroom, which \+ exceeds the {usable_len}-byte usable stack allocation",+ layout.total_control_size,+ );let tos = self.top.as_ptr();unsafe {@@ -302,82 +407,26 @@ impl VMContinuationStack {let payloads = &mut *args;let args_ref = &mut payloads.buffer;- let args_capacity = std::cmp::max(parameter_count, return_value_count);// The args object must currently be empty.debug_assert_eq!(args_ref.capacity, 0);debug_assert_eq!(args_ref.length, 0);- let args_data_size = usize::try_from(args_capacity)?- .checked_mul(std::mem::size_of::<ValRaw>())- .ok_or_else(|| {- format_err!(- "continuation function type with {args_capacity} args \- overflows stack control data size calculation"- )- })?;- // Keep the fixed startup data 16-byte aligned.- let gc_refs_data_size = if cfg!(feature = "gc") && gc_refs {- usize::try_from(args_capacity)?- .checked_add(15)- .map(|s| s & !15)- .ok_or_else(|| {- format_err!(- "continuation function type with {args_capacity} args \- overflows stack control data size calculation"- )- })?- } else {- 0- };- let dynamic_data_size =- args_data_size- .checked_add(gc_refs_data_size)- .ok_or_else(|| {- format_err!(- "continuation function type with {args_capacity} args \- overflows stack control data size calculation"- )- })?;- let total_control_size = dynamic_data_size.checked_add(0x40).ok_or_else(|| {- format_err!(- "continuation function type with {args_capacity} args \- overflows stack control data size calculation"- )- })?;-- // Ensure the control data (fixed header + args buffer) fits- // within the usable stack space. For Mmap allocations,- // self.len includes the guard page, which is not writable.- // Without subtracting the guard page, a high-arity function- // type could pass this check but write into the guard page,- // causing a segfault (see #13703).- let page_size = host_page_size();- let usable_len = match self.allocator {- Allocator::Mmap => self.len.saturating_sub(page_size),- Allocator::Custom => self.len,- };- ensure!(- total_control_size <= usable_len,- "continuation function type requires {total_control_size} bytes \- of stack control data, which exceeds the {usable_len}-byte \- usable stack allocation",- );- let args_data_ptr = if args_capacity == 0 {+ let args_data_ptr = if layout.args_capacity == 0 {ptr::null_mut()} else {- tos.sub(0x20 + args_data_size)+ tos.sub(0x20 + layout.args_data_size)};- args_ref.capacity = args_capacity;+ args_ref.capacity = layout.args_capacity;args_ref.data = NonNull::new(args_data_ptr).map(VmPtr::from);if cfg!(feature = "gc") && gc_refs {- let data = if args_capacity == 0 {+ let data = if layout.args_capacity == 0 {ptr::null_mut()} else {- tos.sub(0x20 + dynamic_data_size)+ tos.sub(0x20 + layout.dynamic_data_size)};- if args_capacity > 0 {- data.write_bytes(0, usize::try_from(args_capacity)?);+ if layout.args_capacity > 0 {+ data.write_bytes(0, usize::try_from(layout.args_capacity)?);}payloads.gc_ref_data = NonNull::new(data).map(VmPtr::from);}@@ -386,17 +435,17 @@ impl VMContinuationStack {// Data near top of stack:(0x08, wasmtime_continuation_start_address().addr()),(0x10, tos.sub(0x10).addr()),- (0x18, tos.sub(total_control_size).addr()),- (0x20, usize::try_from(args_capacity)?),+ (0x18, tos.sub(layout.total_control_size).addr()),+ (0x20, usize::try_from(layout.args_capacity)?),// Data after the args buffer:- (0x28 + dynamic_data_size, func_ref.addr()),- (0x30 + dynamic_data_size, caller_vmctx.addr()),+ (0x28 + layout.dynamic_data_size, func_ref.addr()),+ (0x30 + layout.dynamic_data_size, caller_vmctx.addr()),(- 0x38 + dynamic_data_size,+ 0x38 + layout.dynamic_data_size,(args_ref as *mut VMHostArray).addr(),),(- 0x40 + dynamic_data_size,+ 0x40 + layout.dynamic_data_size,usize::try_from(return_value_count)?,),];diff --git a/tests/all/stack_switching.rs b/tests/all/stack_switching.rsindex 0cc7c317c9..56c86d3040 100644--- a/tests/all/stack_switching.rs+++ b/tests/all/stack_switching.rs@@ -1350,3 +1350,73 @@ fn corruption_of_callthread_state_when_host_function_is_called_on_continuation()assert_eq!(result, 1);Ok(())}++#[test]+#[cfg_attr(miri, ignore)]+fn cont_new_rejects_signature_without_runtime_headroom() -> Result<()> {+ let mut config = Config::new();+ config.wasm_stack_switching(true);+ config.wasm_function_references(true);+ config.wasm_exceptions(true);++ // A 32-KiB production stack has ample room for ordinary continuations and+ // can even initialize the 511-parameter signature from #13020. A still+ // larger signature must be rejected when its persistent control data,+ // compiled array-to-Wasm trampoline frame, and outgoing arguments cannot+ // all fit.+ config.async_stack_size(32 << 10);+ config.max_wasm_stack(4 << 10);++ let engine = Engine::new(&config)?;+ let mut store = Store::new(&engine, ());++ let medium_params = " i32".repeat(511);+ let medium_args = " (i32.const 0)".repeat(511);+ let large_params = " i32".repeat(800);+ let module = Module::new(+ &engine,+ format!(+ r#"+ (module+ (type $small_ft (func))+ (type $small_ct (cont $small_ft))+ (type $medium_ft (func (param{medium_params})))+ (type $medium_ct (cont $medium_ft))+ (type $large_ft (func (param{large_params})))+ (type $large_ct (cont $large_ft))++ (func $small_target)+ (func $medium_target (type $medium_ft))+ (func $large_target (type $large_ft))+ (elem declare func $small_target $medium_target $large_target)++ (func (export "small")+ (drop (cont.new $small_ct (ref.func $small_target))))+ (func (export "medium")+ (resume $medium_ct {medium_args}+ (cont.new $medium_ct (ref.func $medium_target))))+ (func (export "large")+ (drop (cont.new $large_ct (ref.func $large_target))))+ )+ "#,+ ),+ )?;+ let instance = Instance::new(&mut store, &module, &[])?;++ let small = instance.get_typed_func::<(), ()>(&mut store, "small")?;+ small.call(&mut store, ())?;++ let medium = instance.get_typed_func::<(), ()>(&mut store, "medium")?;+ medium.call(&mut store, ())?;++ let large = instance.get_typed_func::<(), ()>(&mut store, "large")?;+ let error = large+ .call(&mut store, ())+ .expect_err("large continuation signature should exceed the stack allocation");+ assert!(+ format!("{error:#}").contains("bytes of runtime headroom"),+ "unexpected error: {error:#}",+ );++ Ok(())+}