Repository navigation
Conversation
Fixes bytecodealliance#12188. With guest debugging enabled, every linear memory now has a 1-to-1 shadow memory. This shadow stores, for every byte, a byte of flags. The one currently defined flag is a "write watchpoint" flag, which causes a guest-debug event to be emitted whenever the corresponding memory byte is written to. Compiled guest code's store instructions, `memory.fill`, `memory.copy`, and `memory.init` will all hit a write watchpoint. Right now, external (host-API) writes to memories do not. Shared memories, and the linear memories underlying GC heaps, are not watched. Shadow memories, like ordinary linear memories, are defined by a VM struct that contains a base pointer; when a memory is imported by another instance within a store, it gets a pointer to this VM struct as well. This single base pointer location allows the shadow memory to grow when the corresponding linear memory does. `Memory::debug_watch` in the host API watches or unwatches a byte range, and a write to a watched byte raises `DebugEvent::Watchpoint` with the memory, address, length, and (for stores) value, before the write happens. The debugger crate reports these as `DebugRunResult::Watchpoint`, and its WIT API now has `memory.add-watchpoint`/`remove-watchpoint` methods and a `watchpoint` event.
Implement GDB remote protocol write watchpoints (`Z2`/`z2`) in the gdbstub component; read and access watchpoints are rejected. Wasmtime stops before a watched write, but gdb and LLDB expect watchpoints to trigger after it and then compare old and new values, so the stub steps over the write before reporting the stop, at the first watched address the write covers. The debuggee watches bytes, not ranges, so the stub tracks its client's (possibly overlapping) watchpoints and re-watches the bytes that the remaining ones cover when one is removed.
Label Messager: wasmtime:configIt looks like you are changing Wasmtime's configuration options. Make sure to
DetailsTo modify this label's message, edit the To add new label messages or remove existing label messages, edit the |
fitzgen
left a comment
There was a problem hiding this comment.
Overall, looks reasonable to me, but I'm a little unsure about the bounds checking for shadow memories. Can you verify that out of bounds wasm stores and bulk memory libcalls don't result in out of bounds loads from the shadow memory, regardless whether we have guard pages configured or not? Probably good to have a test for this too. Also, double checking: do we turn debugging on randomly in the fuzzer config? Would want to make sure that this also gets enabled randomly through that in our fuzzing.
| /// The base of this linear memory's shadow bytes, when stores check them | ||
| /// for watchpoints (`Tunables::memory_watchpoints`). | ||
| pub shadow: Option<VmctxLoadChain>, |
There was a problem hiding this comment.
How does bounds checking work for these shadow memories? Are they guaranteed to have the same guards and movable/immovable base pointers as normal memories? Seems like a good thing to document here.
There was a problem hiding this comment.
The basic idea is that we always probe the main memory first, even on a store; so we cannot reach the shadow memory access until we've validated that the access is in-bounds (and then the shadow memory is grown along with the memory it shadows so it's always the same size).
The libcalls that also can hit watchpoints (for bulk ops) do explicit bounds-checks before accessing.
I'll add a comment!
| let low = builder.ins().load(I64, shadow_flags, shadow_addr, 0); | ||
| let high = builder.ins().load(I64, shadow_flags, shadow_addr, 8); |
There was a problem hiding this comment.
Why two explicit 64-bit loads instead of an i128 load?
There was a problem hiding this comment.
We do a brif on the result, so I think the goal here was to try to avoid a brif on an i128 (instead OR'ing the halves together); but we have a lowering case for that so I should just use it. Will fix.
| fn to_u64(builder: &mut FunctionBuilder<'_>, value: ir::Value) -> ir::Value { | ||
| if builder.func.dfg.value_type(value) == I64 { | ||
| value | ||
| } else { | ||
| builder.ins().uextend(I64, value) | ||
| } | ||
| } |
There was a problem hiding this comment.
Maybe assert that the given value's type is an integer and is less than or equal to 64 bits wide?
alexcrichton
left a comment
There was a problem hiding this comment.
At a high level, could this integrate with the normal memory allocator? It feels a bit odd to forcibly use malloc to allocate these memories and also perhaps a performance hazard waiting to happen. Fuzzing-wise I'm also not certain of this where we more-or-less rely on the fuzzer not seeing the memory allocation of linear memories because otherwise large reservations would immediately OOM.
I'm also personally always wary of adding more paths to do more things related to memory -- it's a pretty security/correctness critical part so I'd favor reuse of existing primitives over the creation of new ones.
|
(Getting back to this now after the weekend, haven't addressed Nick's comments yet, sorry)
I can definitely explore this direction more; my own early thoughts on that, though, were that it might break many more invariants (the defined-memories index space would no longer correspond to Wasm's memory index space above the imports).
My thoughts were (IMHO at least): it's not a concern for debugging-enabled configs: such a config is already going to be pretty slow (due to the instrumentation), moreso with a "watchpoint barrier" at every store. I would, for example, definitely not want to try to integrate watchpoints with the pooling allocator if it takes explicit extra work to do that, because such a config feels like an antipattern to me: one should not need to optimize instantiation time when one is doing interactive debugging with wasmtime-cli and expects more-or-less one instantiation for the process lifetime. So it's additional configation testing surface that doesn't actually serve a user need (again IMHO). Happy to be convinced otherwise though! And I'm very sympathetic to "don't add special cases" so maybe it's actually easier to just ride along with the configured allocator. I'll play with it some more. |
|
I was thinking more like what you have now where a memory points to another memory and it's got a special area in the My main worry is that by adding a new thing on the side we'll end up needing features for that over time and/or have bugs about how its subtly different from the "other thing". For example having to be so careful here about validating bounds checks already has me a bit worried, although I don't see any concrete issue doing a load first and then looking at the shadow memory (we can say we actually fix the store-across-two-pages-but-the-second-is-invalid problem!). If it's too onerous to implement this though it's probably not worth pursuing as it's all scoped ideally to debug-mode-only anyway. |
Fixes #12188.
With guest debugging enabled, every linear memory now has a 1-to-1 shadow memory. This shadow stores, for every byte, a byte of flags. The one currently defined flag is a "write watchpoint" flag, which causes a guest-debug event to be emitted whenever the corresponding memory byte is written to.
Compiled guest code's store instructions,
memory.fill,memory.copy, andmemory.initwill all hit a write watchpoint. Right now, external (host-API) writes to memories do not. Shared memories, and the linear memories underlying GC heaps, are not watched.Shadow memories, like ordinary linear memories, are defined by a VM struct that contains a base pointer; when a memory is imported by another instance within a store, it gets a pointer to this VM struct as well. This single base pointer location allows the shadow memory to grow when the corresponding linear memory does.
Memory::debug_watchin the host API watches or unwatches a byte range, and a write to a watched byte raisesDebugEvent::Watchpointwith the memory, address, length, and (for stores) value, before the write happens.The debugger crate reports these as
DebugRunResult::Watchpoint, and its WIT API now hasmemory.add-watchpoint/remove-watchpointmethods and awatchpointevent.Supported is added in the gdbstub adapter for these watchpoints as well.