Skip to content

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

Tag debug guest state slot stores with alias regions (#13805)

Nick Fitzgerald11350e99f753
diff --git a/cranelift/reader/src/parser.rs b/cranelift/reader/src/parser.rs
index e03734c952..2e8489b08a 100644
--- a/cranelift/reader/src/parser.rs
+++ b/cranelift/reader/src/parser.rs
@@ -2072,10 +2072,12 @@ impl<'a> Parser<'a> {
| Some(Token::LAngle) => true,
_ => false,
} {
- let srcloc = self.optional_srcloc()?;
-
+ // Debug tags are written before the source location (see
+ // `write_debug_tags`), so parse them in that order.
let debug_tags = self.optional_debug_tags()?;
+ let srcloc = self.optional_srcloc()?;
+
// We need to parse instruction results here because they are shared
// between the parsing of value aliases and the parsing of instructions.
//
diff --git a/crates/cranelift/src/func_environ.rs b/crates/cranelift/src/func_environ.rs
index 51d8b13d35..22a346d7d0 100644
--- a/crates/cranelift/src/func_environ.rs
+++ b/crates/cranelift/src/func_environ.rs
@@ -1192,15 +1192,14 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
let offset = b.add_local(FrameValType::from(ty));
if let Some(init) = init {
let slot = *slot;
+ let region = self.alias_regions.stack_slot_region(builder.func, slot);
let address = builder
.ins()
.stack_addr(self.pointer_type(), slot, offset.offset());
- builder.ins().store(
- self.memflags_for_debug_slot_value_wasm_ty(ty),
- init,
- address,
- 0,
- );
+ let flags = self
+ .memflags_for_debug_slot_value_wasm_ty(ty)
+ .with_alias_region(Some(region));
+ builder.ins().store(flags, init, address, 0);
}
}
}
@@ -1244,16 +1243,15 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
self.stacks.stack_shape.push(this_shape);
let value = self.stacks.stack[i];
+ let region = self.alias_regions.stack_slot_region(builder.func, slot);
let address =
builder
.ins()
.stack_addr(self.pointer_type(), slot, offset.offset());
- builder.ins().store(
- self.memflags_for_debug_slot_value_wasm_ty(wasm_ty),
- value,
- address,
- 0,
- );
+ let flags = self
+ .memflags_for_debug_slot_value_wasm_ty(wasm_ty)
+ .with_alias_region(Some(region));
+ builder.ins().store(flags, value, address, 0);
} else {
// Unreachable code with unknown type -- no
// flushes for this or later-pushed values.
@@ -1303,40 +1301,45 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
/// Store a new value for a local in the state slot, if present.
pub(crate) fn state_slot_local_set(
- &self,
+ &mut self,
builder: &mut FunctionBuilder,
local: u32,
value: ir::Value,
) {
- if let Some((slot, b)) = &self.state_slot {
- let offset = b.local_offset(local);
- let address = builder
- .ins()
- .stack_addr(self.pointer_type(), *slot, offset.offset());
- let ty = builder.func.dfg.value_type(value);
- builder.ins().store(
- self.memflags_for_debug_slot_value_clif_ty(ty),
- value,
- address,
- 0,
- );
- }
+ let Some((slot, offset)) = self
+ .state_slot
+ .as_ref()
+ .map(|(slot, b)| (*slot, b.local_offset(local)))
+ else {
+ return;
+ };
+ let region = self.alias_regions.stack_slot_region(builder.func, slot);
+ let address = builder
+ .ins()
+ .stack_addr(self.pointer_type(), slot, offset.offset());
+ let ty = builder.func.dfg.value_type(value);
+ let flags = self
+ .memflags_for_debug_slot_value_clif_ty(ty)
+ .with_alias_region(Some(region));
+ builder.ins().store(flags, value, address, 0);
}
fn update_state_slot_vmctx(&mut self, builder: &mut FunctionBuilder) {
if let &Some((slot, _)) = &self.state_slot {
let vmctx = self.vmctx_val(&mut builder.cursor());
- // N.B.: we always store vmctx at offset 0 in the
- // slot. This is relied upon in
- // crates/wasmtime/src/runtime/debug.rs in
- // `raw_instance()`. See also the slot layout computation in crates/environ/src/
+ // N.B.: we always store vmctx at offset 0 in the slot. This is
+ // relied upon in `crates/wasmtime/src/runtime/debug.rs` in
+ // `raw_instance()`. See also the slot layout computation in
+ // `crates/environ/src/`.
//
- // This is a native-endian store (the only mode for
- // `stack_store`) because it is read by host code directly
- // as a pointer.
- builder
- .ins()
- .stack_store(self.pointer_type(), vmctx, slot, 0);
+ // This is a native-endian store because it is read by host code
+ // directly as a pointer.
+ let region = self.alias_regions.stack_slot_region(builder.func, slot);
+ let address = builder.ins().stack_addr(self.pointer_type(), slot, 0);
+ let flags = ir::MemFlagsData::new()
+ .with_notrap()
+ .with_alias_region(Some(region));
+ builder.ins().store(flags, vmctx, address, 0);
}
}
diff --git a/tests/disas/guest-debugging.wat b/tests/disas/guest-debugging.wat
new file mode 100644
index 0000000000..6f66d03319
--- /dev/null
+++ b/tests/disas/guest-debugging.wat
@@ -0,0 +1,81 @@
+;;! target = "x86_64"
+;;! test = "optimize"
+;;! flags = ["-Dguest-debug=yes"]
+
+(module
+ (func (param i32) (result i32)
+ (local $tmp0 i32)
+ (local $tmp1 i32)
+
+ i32.const 42
+ local.set $tmp0
+
+ local.get $tmp0
+ local.get 0
+ i32.add
+ local.set $tmp1
+
+ local.get $tmp1
+ )
+)
+;; function u0:0(i64 vmctx, i64, i32) -> i32 tail {
+;; ss0 = explicit_slot 28, key = 0
+;; region0 = 8 "VMContext+0x8"
+;; region1 = 134217752 "VMStoreContext+0x18"
+;; region2 = 3087007744 "Stack(ss0)"
+;; sig0 = (i64 vmctx, i8) tail
+;; sig1 = (i64 vmctx) tail
+;; sig2 = (i64) preserve_all
+;; fn0 = colocated u805306368:40 sig0
+;; fn1 = colocated u805306368:41 sig1
+;; fn2 = colocated patchable u1073741824:46 sig2
+;;
+;; block0(v0: i64, v1: i64, v2: i32):
+;; @0018 v3 = stack_addr.i64 ss0+8
+;; @0018 store notrap region2 v2, v3
+;; @0019 v4 = iconst.i32 0
+;; @0019 v5 = stack_addr.i64 ss0+12
+;; @0019 store notrap region2 v4, v5 ; v4 = 0
+;; @0019 v6 = stack_addr.i64 ss0+16
+;; @0019 store notrap region2 v4, v6 ; v4 = 0
+;; @0019 v7 = load.i64 notrap aligned readonly can_move region0 v0+8
+;; @0019 v8 = load.i64 notrap aligned region1 v7+24
+;; @0019 v9 = get_stack_pointer.i64
+;; @0019 v10 = icmp ult v9, v8
+;; @0019 brif v10, block2, block3
+;;
+;; block2 cold:
+;; @0019 v11 = iconst.i8 0
+;; @0019 call fn0(v0, v11) ; v11 = 0
+;; <ss0, 25, 4294967295> @0019 call fn1(v0)
+;; @0019 trap user1
+;;
+;; block3:
+;; @0019 v12 = stack_addr.i64 ss0
+;; @0019 store.i64 notrap region2 v0, v12
+;; <ss0, 27, 4294967295> @001b call fn2(v0)
+;; @001b v13 = iconst.i32 42
+;; @001b v14 = stack_addr.i64 ss0+20
+;; @001b store notrap region2 v13, v14 ; v13 = 42
+;; <ss0, 29, 0> @001d call fn2(v0)
+;; @001d store notrap region2 v13, v5 ; v13 = 42
+;; <ss0, 31, 4294967295> @001f call fn2(v0)
+;; @001f store notrap region2 v13, v14 ; v13 = 42
+;; <ss0, 33, 0> @0021 call fn2(v0)
+;; @0021 v17 = stack_addr.i64 ss0+24
+;; @0021 store.i32 notrap region2 v2, v17
+;; <ss0, 35, 1> @0023 call fn2(v0)
+;; v23 = iadd.i32 v2, v13 ; v13 = 42
+;; @0023 store notrap region2 v23, v14
+;; <ss0, 36, 0> @0024 call fn2(v0)
+;; @0024 store notrap region2 v23, v6
+;; <ss0, 38, 4294967295> @0026 call fn2(v0)
+;; @0026 store notrap region2 v23, v14
+;; <ss0, 40, 0> @0028 call fn2(v0)
+;; @0028 jump block1
+;;
+;; block1:
+;; v25 = iadd.i32 v2, v13 ; v13 = 42
+;; @0028 store notrap region2 v25, v14
+;; @0028 return v25
+;; }