Skip to content

wasm: a load or a store through a function's address, left by speculative direct calls, is dropped - #814

Merged
ASDAlexander77 merged 1 commit into
mainfrom
wasm64-generator-reloc
Nov 11, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
wasm64-generator-reloc

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Fixes wasm-ld's "invalid data relocation" error for a generator's next on wasm64. It only appeared in optimized builds (--opt).

What went wrong

  • SpeculativeDirectCallPass checks a function pointer against every function of the same type, and inlines each one behind its own check.
  • In 00generator3, the loop over IntIter also gets a check for foo3's next. That check never succeeds at run time.
  • Inside that dead branch, the optimizer knows the pointer equals foo3's next. So what the inlined body reads through this becomes a read of the function's own address: load ptr, ptr @...next.
  • Natively that read is never executed. On wasm, code isn't in linear memory, so the relocation can't be resolved and wasm-ld rejects it.

The fix (transform.cpp, WasmFunctionDataAccessPass)

  • After the optimization pipeline, for wasm only, a load whose address is a function gives poison, and such a store is dropped. Either access is undefined anyway.
  • changeToUnreachable would also be correct, but it crashed the compiler on 00owned_generator_locals, so the pass uses poison instead.

Tests

  • 00generator3, 01iterator and 00owned_generator_locals are removed from wasm_skips.txt. They now run on wasm64.
  • Native ctest: 6970/6970.
  • wasm suites, release and debug, 0 failures. wasm64 now passes as many tests as wasm32:
Suite none / rc own
tester, wasm32 and wasm64 575 pass 421 pass
defaultlib 181 pass 180 pass

🤖 Generated with Claude Code

…tive direct calls, is dropped

SpeculativeDirectCallPass tests a function value against every function of its type and inlines each
under its test. In a test the program never passes (IntIter's loop asking "is it foo3's `next`?"), the
optimizer, knowing the pointer is the function, turned what the inlined body reads through its `this`
into a read of the function's own address. Natively that is a read nobody makes; on wasm, where code is
not in the linear memory, it is a relocation the linker cannot resolve - wasm-ld: "invalid data
relocation" for a generator's `next` (seen on wasm64, and possible on wasm32). After the optimization
pipeline, for wasm, such a load gives poison and such a store is dropped: either is undefined anyway.

00generator3, 01iterator and 00owned_generator_locals now run on wasm64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 21f73b0 into main Nov 11, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the wasm64-generator-reloc branch November 11, 2026 22:53
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.

1 participant