| Issue |
239377
|
| Summary |
[lld][wasm] wasm-ld crashes on an undefined constructor instead of reporting it
|
| Labels |
lld:wasm,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
I'm Ananta Srikar Puranam, a PhD student at Washington State University. I'm reporting this bug on behalf of my team: Emily West, Dr. Swarn Priya, Dr. Peng Gao and Dr. Basavesh Ammanaghatta Shivakumar.
`wasm-ld` crashes when an object lists an undefined function as a constructor (an `.init_array` entry), unless undefined symbols are allowed. It never reports the constructor as undefined. `createCallCtorsFunction` then asks it for a function index, and `FunctionSymbol::getFunctionIndex` falls through to `cast<DefinedFunction>`. An assertions build fails the cast; release builds segfault.
**Version**: main at 1ddcaf6c5464 (2026-11-05), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld -DLLVM_TARGETS_TO_BUILD="X86;WebAssembly"`, on x86_64 Linux.
### Reproducer
```sh
cat > ctor.s <<'EOF'
.functype ctor () -> ()
.section .init_array,"",@
.p2align 2
.int32 ctor
EOF
llvm-mc -triple=wasm32-unknown-unknown -filetype=obj ctor.s -o ctor.o
wasm-ld --no-entry -o ctor.wasm ctor.o
```
With `--allow-undefined`, the same command links (status 0). `--unresolved-symbols=ignore-all` and `--gc-sections` don't avoid the crash. If the object also calls another undefined function `g`, `wasm-ld` reports `undefined symbol: g` but not `ctor`, and then crashes the same way.
### Observed
There is no diagnostic before the crash:
```
wasm-ld: llvm/include/llvm/Support/Casting.h:572: decltype(auto) llvm::cast(From *) [To = lld::wasm::DefinedFunction, From = const lld::wasm::FunctionSymbol]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
```
```
lld::wasm::(anonymous namespace)::Writer::run()
lld::wasm::writeResult()
lld::wasm::(anonymous namespace)::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
lld::wasm::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
```
`createCallCtorsFunction` and `FunctionSymbol::getFunctionIndex` are inlined into `Writer::run` in this build.
### Expected
`error: ctor.o: undefined symbol: ctor`, the same as for an undefined function that is called, instead of a crash.
### Analysis
Undefined symbols are reported only while scanning relocations:
https://github.com/llvm/llvm-project/blob/1ddcaf6c546456f0966cb78b098a79e6262a3c3c/lld/wasm/Relocations.cpp#L190-L193
https://github.com/llvm/llvm-project/blob/1ddcaf6c546456f0966cb78b098a79e6262a3c3c/lld/wasm/Relocations.cpp#L47-L51
Constructors are listed in the linking section's init functions, not referenced by relocations. `markLive` marks them live directly:
https://github.com/llvm/llvm-project/blob/1ddcaf6c546456f0966cb78b098a79e6262a3c3c/lld/wasm/MarkLive.cpp#L87-L97
So an undefined constructor is never reported, imported or given a stub. `calculateInitFunctions` adds it to `initFunctions` anyway:
https://github.com/llvm/llvm-project/blob/1ddcaf6c546456f0966cb78b098a79e6262a3c3c/lld/wasm/Writer.cpp#L1829-L1840
`createCallCtorsFunction` calls `getFunctionIndex()` on each entry:
https://github.com/llvm/llvm-project/blob/1ddcaf6c546456f0966cb78b098a79e6262a3c3c/lld/wasm/Writer.cpp#L1719-L1721
The symbol has no stub and no assigned index, so `getFunctionIndex` reaches `cast<DefinedFunction>`:
https://github.com/llvm/llvm-project/blob/1ddcaf6c546456f0966cb78b098a79e6262a3c3c/lld/wasm/Symbols.cpp#L232-L240
#62243 hit the same `createCallCtorsFunction` crash when LTO left an init function undefined. It was closed by #73095, which fixed the LTO input order. This reproducer involves no LTO. Reporting undefined init functions the way undefined relocation targets are reported (for example in `calculateInitFunctions`, or alongside `scanRelocations`) would fix it. Under `--allow-undefined` they are already imported correctly.
This report was drafted with help from an AI assistant (Claude); I reviewed it and reproduced it myself.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs