A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix a segfault with guest-debug growing memories (#15232)
Alex Crichton9ba84c8891d7
diff --git a/crates/cranelift/src/translate/code_translator.rs b/crates/cranelift/src/translate/code_translator.rsindex 65aab77fb1..f1205bcb40 100644--- a/crates/cranelift/src/translate/code_translator.rs+++ b/crates/cranelift/src/translate/code_translator.rs@@ -3838,10 +3838,14 @@ fn align_atomic_addr(/// Like `prepare_addr` but for atomic accesses.///+/// If the access stores `stored`, checks for watchpoints (see `watch_store`)+/// after the alignment check and before computing the native address.+////// Returns `None` when the Wasm access will unconditionally trap.fn prepare_atomic_addr(memarg: &MemArg,loaded_bytes: u8,+ stored: Option<Value>,builder: &mut FunctionBuilder,environ: &mut FuncEnvironment<'_>,) -> WasmResult<Reachability<(MemFlagsData, Value, Value)>> {@@ -3849,6 +3853,13 @@ fn prepare_atomic_addr(return Err(wasm_unsupported!("acquire-release-atomics"));}align_atomic_addr(memarg, loaded_bytes, builder, environ);+ if let Some(stored) = stored {+ if let Reachability::Unreachable =+ watch_store(memarg, loaded_bytes, stored, builder, environ)?+ {+ return Ok(Reachability::Unreachable);+ }+ }prepare_addr(memarg, loaded_bytes, builder, environ)}@@ -3889,13 +3900,16 @@ fn translate_store(let val_ty = builder.func.dfg.value_type(val);let mem_op_size = mem_op_size(opcode, val_ty);+ unwrap_or_return_unreachable_state!(+ environ,+ watch_store(memarg, mem_op_size, val, builder, environ)?+ );let (flags, wasm_index, base) = unwrap_or_return_unreachable_state!(environ,prepare_addr(memarg, mem_op_size, builder, environ)?);environ.before_store(builder, mem_op_size, wasm_index, memarg.offset);- watch_store(memarg, mem_op_size, wasm_index, val, builder, environ);let flags = builder.func.dfg.mem_flags.insert(flags).unwrap();builder@@ -3904,42 +3918,45 @@ fn translate_store(Ok(())}-/// Before a store of `value`, `size` bytes at the Wasm address `index` plus-/// `memarg.offset`, checks the memory's shadow for watchpoints, if it has one.+/// Before a store of `value`, `size` bytes at the Wasm address at the top of+/// the stack plus `memarg.offset`, checks the memory's shadow for watchpoints,+/// if it has one.////// The shadow is as large as the memory, so the shadow's bounds check passes/// exactly when the store's does.fn watch_store(memarg: &MemArg,size: u8,- index: Value,value: Value,builder: &mut FunctionBuilder,environ: &mut FuncEnvironment<'_>,-) {+) -> WasmResult<Reachability<()>> {let memory = match memarg.target {MemArgTarget::Memory(index) => MemoryIndex::from_u32(index),- // Callers have already rejected array accesses in `prepare_addr`.- MemArgTarget::Array(_) => unreachable!(),+ MemArgTarget::Array(_) => return Err(wasm_unsupported!("multibyte-array-access proposal")),};let heap = environ.get_or_create_heap(builder.func, memory);let Some(shadow) = environ.heaps()[heap].shadow else {- return;+ return Ok(Reachability::Reachable(()));};let shadow = environ.heaps()[shadow].clone();- if let Reachability::Reachable(shadow_addr) =- heap_addr(memarg, size, &shadow, index, builder, environ)- {- environ.watch_store(- builder,- memory,- shadow_addr,- index,- memarg.offset,- size,- value,- );- }+ let index = environ.stacks.pop1();+ let shadow_addr = heap_addr(memarg, size, &shadow, index, builder, environ);+ let Reachability::Reachable(shadow_addr) = shadow_addr else {+ environ.stacks.push1(index);+ return Ok(Reachability::Unreachable);+ };+ environ.watch_store(+ builder,+ memory,+ shadow_addr,+ index,+ memarg.offset,+ size,+ value,+ );+ environ.stacks.push1(index);+ Ok(Reachability::Reachable(()))}fn mem_op_size(opcode: ir::Opcode, ty: Type) -> u8 {@@ -3991,18 +4008,17 @@ fn translate_atomic_rmw(arg2 = builder.ins().ireduce(access_ty, arg2);}- let (flags, index, addr) = unwrap_or_return_unreachable_state!(+ let (flags, _, addr) = unwrap_or_return_unreachable_state!(environ,prepare_atomic_addr(memarg,u8::try_from(access_ty.bytes()).unwrap(),+ Some(arg2),builder,environ,)?);- let size = u8::try_from(access_ty.bytes()).unwrap();- watch_store(memarg, size, index, arg2, builder, environ);let mut res = builder.ins().atomic_rmw(access_ty, flags, op, addr, arg2);if access_ty != widened_ty {res = builder.ins().uextend(widened_ty, res);@@ -4048,17 +4064,16 @@ fn translate_atomic_cas(replacement = builder.ins().ireduce(access_ty, replacement);}- let (flags, index, addr) = unwrap_or_return_unreachable_state!(+ let (flags, _, addr) = unwrap_or_return_unreachable_state!(environ,prepare_atomic_addr(memarg,u8::try_from(access_ty.bytes()).unwrap(),+ Some(replacement),builder,environ,)?);- let size = u8::try_from(access_ty.bytes()).unwrap();- watch_store(memarg, size, index, replacement, builder, environ);let mut res = builder.ins().atomic_cas(flags, addr, expected, replacement);if access_ty != widened_ty {res = builder.ins().uextend(widened_ty, res);@@ -4096,6 +4111,7 @@ fn translate_atomic_load(prepare_atomic_addr(memarg,u8::try_from(access_ty.bytes()).unwrap(),+ None,builder,environ,)?@@ -4138,17 +4154,16 @@ fn translate_atomic_store(data = builder.ins().ireduce(access_ty, data);}- let (flags, index, addr) = unwrap_or_return_unreachable_state!(+ let (flags, _, addr) = unwrap_or_return_unreachable_state!(environ,prepare_atomic_addr(memarg,u8::try_from(access_ty.bytes()).unwrap(),+ Some(data),builder,environ,)?);- let size = u8::try_from(access_ty.bytes()).unwrap();- watch_store(memarg, size, index, data, builder, environ);builder.ins().atomic_store(flags, data, addr);Ok(())}diff --git a/tests/all/debug.rs b/tests/all/debug.rsindex c46d35208c..9f10d4b9b5 100644--- a/tests/all/debug.rs+++ b/tests/all/debug.rs@@ -2379,6 +2379,77 @@ async fn watchpoints_on_imported_memory_follow_growth() -> wasmtime::Result<()>Ok(())}+#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn watchpoints_handler_may_grow_memory() -> wasmtime::Result<()> {+ #[derive(Clone, Default)]+ struct WatchGrower;++ impl DebugHandler for WatchGrower {+ type Data = ();+ async fn handle(&self, mut store: StoreContextMut<'_, ()>, event: DebugEvent<'_>) {+ if let DebugEvent::Watchpoint(hit) = event {+ hit.memory.grow(&mut store, 1).unwrap();+ }+ }+ }++ // Pulley doesn't support the wasm threads proposal, so skip this test on+ // those targets.+ if Engine::default().is_pulley() {+ return Ok(());+ }++ let (module, mut store) = get_module_and_store(+ |config| {+ // With no reservation beyond the memory's size, the memory moves+ // each time the handler grows it.+ config+ .memory_reservation(0)+ .memory_reservation_for_growth(0);+ },+ r#"+ (module+ (memory (export "memory") 1)+ (func (export "store") (param i32 i32)+ (i32.store (local.get 0) (local.get 1)))+ (func (export "atomic_store") (param i32 i32)+ (i32.atomic.store (local.get 0) (local.get 1)))+ (func (export "atomic_rmw") (param i32 i32) (result i32)+ (i32.atomic.rmw.add (local.get 0) (local.get 1)))+ (func (export "atomic_cas") (param i32 i32) (result i32)+ (i32.atomic.rmw.cmpxchg (local.get 0) (i32.const 0) (local.get 1)))+ (func (export "store_twice") (param i32 i32)+ (i32.store (local.get 0) (local.get 1))+ (i32.store offset=4 (local.get 0) (local.get 1))))+ "#,+ )?;+ let grower = WatchGrower::default();+ store.set_debug_handler(grower.clone());+ let instance = Instance::new_async(&mut store, &module, &[]).await?;+ let memory = instance.get_memory(&mut store, "memory").unwrap();+ store.edit_breakpoints().unwrap().watch(memory, 0..64)?;++ let store_i32 = instance.get_typed_func::<(i32, i32), ()>(&mut store, "store")?;+ store_i32.call_async(&mut store, (0, 0x11111111)).await?;+ let atomic_store = instance.get_typed_func::<(i32, i32), ()>(&mut store, "atomic_store")?;+ atomic_store.call_async(&mut store, (4, 0x22222222)).await?;+ let atomic_rmw = instance.get_typed_func::<(i32, i32), i32>(&mut store, "atomic_rmw")?;+ assert_eq!(atomic_rmw.call_async(&mut store, (0, 1)).await?, 0x11111111);+ let atomic_cas = instance.get_typed_func::<(i32, i32), i32>(&mut store, "atomic_cas")?;+ assert_eq!(atomic_cas.call_async(&mut store, (8, 0x33333333)).await?, 0);+ let store_twice = instance.get_typed_func::<(i32, i32), ()>(&mut store, "store_twice")?;+ store_twice.call_async(&mut store, (12, 0x44444444)).await?;++ assert_eq!(memory.size(&store), 7);+ let data = memory.data(&store);+ assert_eq!(&data[0..4], &0x11111112_u32.to_le_bytes());+ assert_eq!(&data[4..8], &0x22222222_u32.to_le_bytes());+ assert_eq!(&data[8..12], &0x33333333_u32.to_le_bytes());+ assert_eq!(&data[12..20], &[0x44; 8]);+ Ok(())+}+#[tokio::test]#[cfg_attr(miri, ignore)]async fn watchpoints_out_of_bounds_stores_trap() -> wasmtime::Result<()> {diff --git a/tests/disas/memory-watchpoints.wat b/tests/disas/memory-watchpoints.watindex 38ef60f3d4..fd9381370d 100644--- a/tests/disas/memory-watchpoints.wat+++ b/tests/disas/memory-watchpoints.wat@@ -17,8 +17,8 @@;; region3 = 215 "";; region4 = 105 "";; region5 = 133 ""-;; region6 = 171 ""-;; region7 = 153 ""+;; region6 = 153 ""+;; region7 = 171 "";; sig0 = (i64 vmctx, i8) tail;; sig1 = (i64 vmctx) tail;; sig2 = (i64) preserve_all@@ -40,8 +40,8 @@;; @001d brif v9, block2, block3;;;; block2 cold:-;; @0022 v19 = iconst.i8 0-;; <ss0, 29, 4294967295> @001d call fn0(v0, v19) ; v19 = 0+;; @0022 v20 = iconst.i8 0+;; <ss0, 29, 4294967295> @001d call fn0(v0, v20) ; v20 = 0;; <ss0, 29, 4294967295> @001d call fn1(v0);; @001d trap user1;;@@ -55,12 +55,13 @@;; @0020 v13 = stack_addr.i64 ss0+24;; @0020 store.i64 notrap region2 v3, v13;; <ss0, 34, 1> @0022 call fn2(v0)-;; @0022 v15 = load.i64 notrap aligned region4 v0+64+;; @0022 v15 = load.i64 notrap aligned readonly can_move region5 v0+72+;; @0022 v16 = load.i64 notrap aligned region4 v15+8;; @0022 v14 = uextend.i64 v2-;; @0022 v16 = iconst.i64 16-;; @0022 v17 = isub v15, v16 ; v16 = 16-;; @0022 v18 = icmp ugt v14, v17-;; @0022 brif v18, block4, block5+;; @0022 v17 = iconst.i64 16+;; @0022 v18 = isub v16, v17 ; v17 = 16+;; @0022 v19 = icmp ugt v14, v18+;; @0022 brif v19, block4, block5;;;; block4 cold:;; @0022 v54 = iconst.i8 1@@ -69,42 +70,41 @@;; @0022 trap user1;;;; block5:-;; @0022 v27 = load.i64 notrap aligned readonly can_move region5 v0+72-;; @0022 v28 = load.i64 notrap aligned region4 v27+8-;; @0022 v48 = iconst.i64 16-;; @0022 v49 = isub v28, v48 ; v48 = 16-;; @0022 v31 = icmp.i64 ugt v14, v49-;; @0022 brif v31, block6, block7+;; @0022 v24 = load.i64 notrap aligned readonly can_move region3 v15+;; @0022 v25 = iadd v24, v14+;; @0022 v26 = iconst.i64 8+;; @0022 v27 = iadd v25, v26 ; v26 = 8+;; @0022 v28 = load.i64 region6 v27+;; @0022 brif v28, block6, block7;;;; block6 cold:-;; @0022 v34 = iconst.i8 1-;; <ss0, 34, 4294967295> @0022 call fn0(v0, v34) ; v34 = 1-;; <ss0, 34, 4294967295> @0022 call fn1(v0)-;; @0022 trap user1+;; @0022 v30 = iconst.i32 0+;; @0022 v48 = iconst.i64 8+;; @0022 v49 = iadd.i64 v14, v48 ; v48 = 8+;; @0022 v34 = iconst.i32 8+;; @0022 v29 = iconst.i64 0+;; <ss0, 34, 4294967295> @0022 v35 = call fn3(v0, v30, v49, v34, v3, v29) ; v30 = 0, v34 = 8, v29 = 0+;; @0022 jump block7;;;; block7:-;; @0022 v36 = load.i64 notrap aligned readonly can_move region3 v27-;; @0022 v37 = iadd v36, v14-;; @0022 v24 = iconst.i64 8-;; @0022 v39 = iadd v37, v24 ; v24 = 8-;; @0022 v40 = load.i64 region7 v39+;; @0022 v37 = load.i64 notrap aligned region4 v0+64+;; @0022 v50 = iconst.i64 16+;; @0022 v51 = isub v37, v50 ; v50 = 16+;; @0022 v40 = icmp.i64 ugt v14, v51;; @0022 brif v40, block8, block9;;;; block8 cold:-;; @0022 v42 = iconst.i32 0-;; @0022 v50 = iconst.i64 8-;; @0022 v51 = iadd.i64 v14, v50 ; v50 = 8-;; @0022 v46 = iconst.i32 8-;; @0022 v41 = iconst.i64 0-;; <ss0, 34, 4294967295> @0022 v47 = call fn3(v0, v42, v51, v46, v3, v41) ; v42 = 0, v46 = 8, v41 = 0-;; @0022 jump block9+;; @0022 v43 = iconst.i8 1+;; <ss0, 34, 4294967295> @0022 call fn0(v0, v43) ; v43 = 1+;; <ss0, 34, 4294967295> @0022 call fn1(v0)+;; @0022 trap user1;;;; block9:-;; @0022 v22 = load.i64 notrap aligned readonly can_move region3 v0+56-;; @0022 v23 = iadd v22, v14+;; @0022 v44 = load.i64 notrap aligned readonly can_move region3 v0+56+;; @0022 v45 = iadd v44, v14;; @0022 v52 = iconst.i64 8-;; @0022 v53 = iadd v23, v52 ; v52 = 8-;; @0022 store.i64 little region6 v3, v53+;; @0022 v53 = iadd v45, v52 ; v52 = 8+;; @0022 store.i64 little region7 v3, v53;; <ss0, 37, 4294967295> @0025 call fn2(v0);; @0025 jump block1;;