Skip to content

Map code without a DWARF line to no location in wasm source maps - #28033

Merged
kripken merged 3 commits into
emscripten-core:mainfrom
JesusFileto:sourcemap-line-zero
Oct 22, 2026
Merged

kripken merged 3 commits into
emscripten-core:mainfrom
JesusFileto:sourcemap-line-zero

Conversation

@JesusFileto

Copy link
Copy Markdown
Contributor

wasm-sourcemap.py drops DWARF rows with line 0, which LLVM uses for code that does not come from a single source line, for example when it merges several calls to abort() into one. The previous mapping then covers that code too, so it is reported at an unrelated line.

This writes an entry with only the wasm offset at these rows instead. An entry without a source location ends the previous mapping, so the code gets no location. Binaryen does the same when it writes source maps (WebAssembly/binaryen#5906): one such entry for each run of code without a location, and none before the first mapping. Binaryen and emsymbolizer already read these entries. When this tool was written, Binaryen could not, which is likely why the rows were dropped.

Each entry adds about two bytes. The new test checks that code at line 0 maps to no location and that consecutive line 0 rows share one entry.

DWARF uses line 0 for code that does not come from a single source
line, for example when LLVM merges several calls to abort() into
one. wasm-sourcemap.py dropped these rows, so the previous mapping
covered that code too and it was reported at an unrelated line.

Write an entry with only the wasm offset at these rows instead, so
the code gets no location. Binaryen writes source maps the same way
(WebAssembly/binaryen#5906). Each entry adds about two bytes.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the WebAssembly sourcemap generator to correctly handle DWARF line 0 entries by ending the previous mapping with an unmapped segment, and adds a corresponding test case to verify this behavior. The review feedback suggests a minor optimization in tools/wasm-sourcemap.py to move the address calculation inside the conditional block, avoiding redundant operations when the conditions are not met.

Comment thread tools/wasm-sourcemap.py Resolved
@sbc100
sbc100 requested a review from kripken October 20, 2026 20:30
@sbc100

sbc100 commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

@kripken you might be best to review this since you wrote WebAssembly/binaryen#5906. Also @dschuff

Comment thread tools/wasm-sourcemap.py
address = entry['address'] + code_section_offset
mappings.append(encode_vlq(address - last_address))
last_address = address
last_was_unmapped = True

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not a source maps expert, unfortunately. Why is it valid to simply append this encoded diff to mappings? How do the users of mappings know what to find at that index? (Below in the old code I see a lot more things appended than this, so I'm not sure why this is also valid.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The diff that is recorded in mappings is a segment that is either 1, 4 or 5 fields based on the Ecma Source Map spec.

When the DWARF code does not point to any code in the original codebase based on compiler optimizations, there should only be a diff to the last segment that was appended to mappings. Previously, an offset would be matched based on the closest previous segment in the source map, which can be inaccurate if the compiler used the same unmapped code in multiple disparate parts of the codebase. Now, instead of giving inaccurate code locations it can be stated that the offset cannot be mapped to the original codebase.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does src/source_map_support.js need updates for this?

Comment thread tools/wasm-sourcemap.py
if line == 0:
if mappings and not last_was_unmapped:
address = entry['address'] + code_section_offset
mappings.append(encode_vlq(address - last_address))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it valid for this to be 0? (which seems possible if DWARF emits the same address more than once?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yea its possible for DWARF to emit the address more than once, the spec only forbids negative offsets from being written in the source map, but multiple line entries can exist for one address. I believe that in emscripten the last entry is taken as the source of truth.

WasmSourceMap.lookup() returned an object with an undefined file for
a segment with only an offset, so emscripten_pc_get_file passed
undefined to stringToNewUTF8 and threw. Return null instead, as for
offsets before the first mapping. Also clarify the comment in
wasm-sourcemap.py.
@JesusFileto

JesusFileto commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor Author

Does src/source_map_support.js need updates for this?

Just looked at the file and your right, we need to add an undefined check to lookup() as it would fail if there was only an offset being printed in the source map. Making a change to return null for these cases since no code information can be obtained for length 1 segments in the last commit.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, looks good.

@kripken
kripken merged commit 45a6cff into emscripten-core:main Oct 22, 2026
42 checks passed
JesusFileto added a commit to JesusFileto/workers-rs that referenced this pull request Nov 2, 2026
emcc's source maps skip DWARF rows at line 0, which LLVM uses for code
without a single source line, so that code maps to the line before it.
The fix, emscripten-core/emscripten#28033, first ships in 6.0.14; apply
its source map part to the pinned 6.0.10 like the other backports, and
drop it when the pin moves past it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants