| Issue |
235251
|
| Summary |
[lld][WebAssembly] wasm-ld crashes on an object whose Function section declares functions but which has no Code section
|
| Labels |
lld
|
| 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` segfaults while reading a relocatable object whose Function section declares functions but which has no Code section. `WasmObjectFile` accepts the object, because the function count is only checked inside `parseCodeSection`. Then `InputFunction`'s constructor dereferences the null `ObjFile::codeSection`.
**Versions tested** (host x86_64 Linux):
- LLVM 23.1.4, the latest release: official `LLVM-23.1.4-Linux-X64.tar.zst` from the GitHub release page. SIGSEGV.
- main at c41dd6bb2d26 (2026-10-22), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`. SIGSEGV.
- LLD 21.1.8 (Ubuntu `lld-21` package): SIGSEGV.
### Reproducer
```sh
cat > nocode.yaml <<'EOF'
--- !WASM
FileHeader:
Version: 0x1
Sections:
- Type: TYPE
Signatures:
- Index: 0
ParamTypes: []
ReturnTypes: [ I32 ]
- Type: FUNCTION
FunctionTypes: [ 0, 0 ]
- Type: CUSTOM
Name: linking
Version: 2
SymbolTable:
- Index: 0
Kind: FUNCTION
Name: entry
Flags: [ ]
Function: 0
- Index: 1
Kind: FUNCTION
Name: target
Flags: [ ]
Function: 1
EOF
yaml2obj nocode.yaml -o nocode.o
wasm-ld --no-entry --export=entry nocode.o -o nocode.wasm
```
`llvm-readobj --sections nocode.o` lists the object without complaint (TYPE, FUNCTION, CUSTOM). With a Code section holding the two function bodies, the same link succeeds. The original fuzzer input had a Code section whose section ID was changed to DataCount (12), which has the same effect and also crashes. Changing it to Data (11) instead gives a clean `invalid segment size` error.
### Observed
LLVM 23.1.4 (release build), complete output:
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /path/to/llvm-23.1.4/bin/wasm-ld --no-entry --export=entry nocode.o -o nocode.wasm
#0 0x000058bc6e0d3a2a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/path/to/llvm-23.1.4/bin/wasm-ld+0x5d6ea2a)
#1 0x000058bc6e0d45cd SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#2 0x00007db5e271ecb0 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x45cb0)
#3 0x000058bc6e58fa68 lld::wasm::ObjFile::parse(bool) (/path/to/llvm-23.1.4/bin/wasm-ld+0x622aa68)
#4 0x000058bc6e59bfa3 lld::wasm::SymbolTable::addFile(lld::wasm::InputFile*, llvm::StringRef) (/path/to/llvm-23.1.4/bin/wasm-ld+0x6236fa3)
#5 0x000058bc6e582d4f lld::wasm::(anonymous namespace)::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) Driver.cpp:0:0
#6 0x000058bc6e57cb41 lld::wasm::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/path/to/llvm-23.1.4/bin/wasm-ld+0x6217b41)
#7 0x000058bc6e0d73bf lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (/path/to/llvm-23.1.4/bin/wasm-ld+0x5d723bf)
#8 0x000058bc6dc6b4c3 lld_main(int, char**, llvm::ToolContext const&) (/path/to/llvm-23.1.4/bin/wasm-ld+0x59064c3)
#9 0x000058bc6dc6b613 main (/path/to/llvm-23.1.4/bin/wasm-ld+0x5906613)
#10 0x00007db5e2703601 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a601)
#11 0x00007db5e2703718 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a718)
#12 0x000058bc6deef685 _start (/path/to/llvm-23.1.4/bin/wasm-ld+0x5b8a685)
Segmentation fault (core dumped)
```
<details><summary>Backtrace of main c41dd6bb2d26, Release + assertions (gdb)</summary>
```
#0 lld::wasm::InputFunction::InputFunction(llvm::wasm::WasmSignature const&, llvm::wasm::WasmFunction const*, lld::wasm::ObjFile*)
#1 lld::wasm::ObjFile::parse(bool)
#2 lld::wasm::SymbolTable::addFile(lld::wasm::InputFile*, llvm::StringRef)
#3 lld::wasm::(anonymous namespace)::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
#4 lld::wasm::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
#5 lld::unsafeLldMain(...)
#6 lld_main(int, char**, llvm::ToolContext const&)
#7 main
```
</details>
### Expected
An error for the malformed object, for example "invalid function count" or "missing code section", instead of a crash.
### Analysis
`InputFunction::InputFunction` in `lld/wasm/InputChunks.h` (line 268 at c41dd6bb2d26):
```cpp
inputSectionOffset = function->CodeSectionOffset;
rawData =
file->codeSection->Content.slice(inputSectionOffset, function->Size);
```
`ObjFile::codeSection` is only set when the object has a Code section (`lld/wasm/InputFiles.cpp`, around line 543). On the libObject side, `WasmObjectFile::parseCodeSection` (`llvm/lib/Object/WasmObjectFile.cpp`) is the only place that compares the Function and Code counts:
```cpp
uint32_t FunctionCount = readVaruint32(Ctx);
if (FunctionCount != Functions.size()) {
return make_error<GenericBinaryError>("invalid function count",
object_error::parse_failed);
}
```
So with no Code section at all, a non-empty Function section is never checked. The wasm spec requires the two vectors to have the same length (a missing Code section counts as empty). Checking `!Functions.empty() && no Code section` after all sections are parsed in `WasmObjectFile` would reject the object for every tool. A null check on `codeSection` in `ObjFile::parse` would fix `wasm-ld` alone.
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