Skip to content

fix(dwarf): remap DW_OP_WASM_location local and global indices - #339

Merged
guybedford merged 2 commits into
mainfrom
dwarf-wasm-locals
Nov 12, 2026
Merged

guybedford merged 2 commits into
mainfrom
dwarf-wasm-locals

Conversation

@logan-gatlin

@logan-gatlin logan-gatlin commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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_location names 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

  • With generate_dwarf, parsing records the input module's local indices for each function and its global indices, in ModuleDebugData.
  • On emit, every DW_OP_WASM_location local or global index is mapped through those tables to the index walrus emits. Which function a local index belongs to comes from:
    • for a location list entry, the function containing the entry's start;
    • for an exprloc attribute (DW_AT_location, DW_AT_frame_base, …), the function containing the nearest DW_AT_low_pc among the entry and its ancestors. LLVM's DW_AT_low_pc is the body start, after its size, where original_range starts. (An earlier version of this description said it was the size byte; that was wrong.)
  • Locations of locals and globals that walrus removed are dropped, so the variable shows as optimized out. Entries for code that isn't in the module, such as functions the linker discarded (low_pc of 0xffffffff), are left as they are.
  • gimli's converted write::Expression can't be edited, so walrus now reads the input through EndianRcSlice instead of EndianSlice. The debug sections are copied once. A patched expression is then an ordinary reader, and convert_expression converts it with everything else in it, including the DW_OP_convert base-type references LLVM writes next to DW_OP_WASM_location in DWARF 5. A new index can need more or fewer LEB128 bytes than the old one, so the offsets of DW_OP_bra and DW_OP_skip are recomputed around it. gimli then re-encodes the expression, so padding wouldn't survive anyway.

ModuleDebugData gains a private field, so it can no longer be built with a struct expression (Default still works). This is noted in the CHANGELOG.

Testing

crates/tests/tests/dwarf_wasm_locations.rs builds a function whose i64 and i32 locals walrus reorders, plus an unused local and a global that gc removes. Its DWARF covers locals in exprloc and in a location list, a fixed-width (kind 3) global frame base, a LEB128 global, a local combined with DW_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 a DW_OP_bra over it.

On the same Rust (serde_json + regex) and C builds as #338, using the instruction-aligning checker:

#338 this PR
DW_OP_WASM_location local indices stale, Rust -O3 23.9% (11,873) 0 (49,603/49,603 correct)
… Rust -Oz 10.5% (2,085) 0 (19,877/19,877)
… C -O2 / -O0 0 0
Global indices correct correct
Emit time, Rust -O3 333 ms 361 ms

At 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_pc 0xffffffff), so walrus leaves them unchanged, and the checker attributes them to a live function by mistake.

@wasm-bindgen-bot wasm-bindgen-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@wasm-bindgen-bot wasm-bindgen-bot Bot added the merge-ready The bot's review of its own PR found no concerns; ready for a human approval label Nov 12, 2026
@wasm-bindgen-bot wasm-bindgen-bot Bot removed the merge-ready The bot's review of its own PR found no concerns; ready for a human approval label Nov 12, 2026
@logan-gatlin

Copy link
Copy Markdown
Contributor Author

Both untested paths from the review are covered now, in d9c692d. Writing the test showed the padding never reached the output: gimli's convert_expression re-encodes the expression with minimal LEB128s and recomputes branch offsets. So walrus now writes each new index at its minimal width and recomputes the DW_OP_bra/DW_OP_skip offsets around it, and no expression is dropped for growing. 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 a branch over it. This branch is also rebased onto the updated #338.

@wasm-bindgen-bot wasm-bindgen-bot Bot added the merge-ready The bot's review of its own PR found no concerns; ready for a human approval label Nov 12, 2026
Base automatically changed from dwarf-convert to main November 12, 2026 21:09
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.
@wasm-bindgen-bot wasm-bindgen-bot Bot added merge-ready The bot's review of its own PR found no concerns; ready for a human approval and removed merge-ready The bot's review of its own PR found no concerns; ready for a human approval labels Nov 12, 2026
@guybedford
guybedford merged commit 1d9c51c into main Nov 12, 2026
20 checks passed
@guybedford
guybedford deleted the dwarf-wasm-locals branch November 12, 2026 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-ready The bot's review of its own PR found no concerns; ready for a human approval

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants