Repository navigation
fix(dwarf): remap DW_OP_WASM_location local and global indices - #339
Conversation
There was a problem hiding this comment.
✅ When generate_dwarf is on, this PR keeps DW_OP_WASM_location pointing at the right local or global after walrus regroups locals by type, drops unused ones and gc removes globals. It's the follow-up to the DWARF conversion work in #338 and fixes debuggers showing the wrong variable's value in optimized Rust builds. At parse time it records the input module's local and global indices. On emit, it works out which function an expression belongs to: the start of a location-list entry, or for an exprloc, the nearest DW_AT_low_pc on the entry or its ancestors. It then patches the index bytes, keeping the old width, before handing the expression to gimli's normal convert_expression; editing gimli's converted write::Expression is not possible. I checked the lookups against push_local, which records params first, and against emit_locals (args first, then by type), as well as the input_function boundary handling for low_pc at the size LEB and the padded LEB writer, and found no problems. Two small paths have no tests: an index that shrinks but keeps its padding, and dropping an expression that branches over an index that had to grow. Both are simple enough that this doesn't block merging. The new private field on ModuleDebugData is a breaking change, and the CHANGELOG notes it.
c34f4ed to
d9c692d
Compare
c3c3de3 to
2ccdaa7
Compare
|
Both untested paths from the review are covered now, in d9c692d. Writing the test showed the padding never reached the output: gimli's |
walrus emits each function's locals grouped by type, without the unused ones, and gc can remove globals, so the indices `DW_OP_WASM_location` names change. They were copied unchanged: in optimized Rust builds 10-24% of local indices in location expressions named the wrong local. With `generate_dwarf`, record the input module's local indices of each function and its global indices at parse time, and map every `DW_OP_WASM_location` through them to the emitted indices. A local index belongs to the function containing the location list entry, or the one containing the nearest `DW_AT_low_pc` among the entry and its ancestors (LLVM's `DW_AT_low_pc` is the address of the body's size, so that counts as inside the body). Locations of removed locals and globals are dropped; entries in code that isn't in the module, such as functions the linker discarded, are left as they are. gimli's converted expressions can't be edited, so the input is read through `EndianRcSlice` instead of `EndianSlice`. A patched expression is then an ordinary reader that `convert_expression` converts, including the `DW_OP_convert` base type references LLVM writes next to `DW_OP_WASM_location` in DWARF 5. New indices are padded to the old width so branch offsets stay valid. Measured on the same Rust and C builds as the previous commit: stale local indices go from 10-24% to 0 on every optimized build (49,603/49,603 at Rust -O3); emit time goes up about 5%. `dwarf_lists` used `DW_OP_WASM_location 0 0` in functions with no locals, which is now dropped; it uses a constant instead.
A remapped `DW_OP_WASM_location` index was padded to its old width so that `DW_OP_bra` and `DW_OP_skip` offsets stayed valid, and an expression with a branch over an index that had to grow was dropped. gimli re-encodes the converted expression with minimal LEB128s anyway, so the padding never reached the output. Write each new index in its minimal width and recompute the branch offsets instead, so no expression is dropped. Test a shrinking and a growing index, each with and without a branch over it.
d9c692d to
1a49041
Compare
Stacked on #338, and based on its branch. Only the top commit is new here.
walrus emits each function's locals grouped by type, without the unused ones, and gc can remove globals. So the indices that
DW_OP_WASM_locationnames change, but they were copied unchanged. In optimized Rust builds, 10–24% of the local indices in location expressions named the wrong local, so debuggers showed another variable's value.Change
generate_dwarf, parsing records the input module's local indices for each function and its global indices, inModuleDebugData.DW_OP_WASM_locationlocal or global index is mapped through those tables to the index walrus emits. Which function a local index belongs to comes from:exprlocattribute (DW_AT_location,DW_AT_frame_base, …), the function containing the nearestDW_AT_low_pcamong the entry and its ancestors. LLVM'sDW_AT_low_pcis the body start, after its size, whereoriginal_rangestarts. (An earlier version of this description said it was the size byte; that was wrong.)low_pcof0xffffffff), are left as they are.write::Expressioncan't be edited, so walrus now reads the input throughEndianRcSliceinstead ofEndianSlice. The debug sections are copied once. A patched expression is then an ordinary reader, andconvert_expressionconverts it with everything else in it, including theDW_OP_convertbase-type references LLVM writes next toDW_OP_WASM_locationin DWARF 5. A new index can need more or fewer LEB128 bytes than the old one, so the offsets ofDW_OP_braandDW_OP_skipare recomputed around it. gimli then re-encodes the expression, so padding wouldn't survive anyway.ModuleDebugDatagains a private field, so it can no longer be built with a struct expression (Defaultstill works). This is noted in the CHANGELOG.Testing
crates/tests/tests/dwarf_wasm_locations.rsbuilds a function whose i64 and i32 locals walrus reorders, plus an unused local and a global that gc removes. Its DWARF covers locals inexprlocand in a location list, a fixed-width (kind 3) global frame base, a LEB128 global, a local combined withDW_OP_convert, and a discarded function. The test checks each emitted index against the instructions walrus emitted. It fails on #338. A second test,wasm_location_index_widths, covers an index that shrinks from 2 bytes to 1 and one that grows from 1 to 2, each with and without aDW_OP_braover it.On the same Rust (serde_json + regex) and C builds as #338, using the instruction-aligning checker:
DW_OP_WASM_locationlocal indices stale, Rust -O3At Rust -O0, 2 of 10,980 local indices still differ from the checker's expectation. That build also has the dead-instruction residual described in #338.
The checker also flags 225 frame bases whose index is out of range. They all belong to subprograms that the linker discarded (
low_pc0xffffffff), so walrus leaves them unchanged, and the checker attributes them to a live function by mistake.