| Issue |
243353
|
| Summary |
[lld][wasm] wasm-ld writes out of bounds for an element segment function index past the function count
|
| Labels |
lld:wasm,
crash
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
`WasmObjectFile::parseElemSection` stores each element segment function index without checking it against the function index space. `lld::wasm::ObjFile::parse` then sizes `tableEntriesRel` and `tableEntries` to the number of functions and writes both at that index. An object whose element segment names a function index past the function count makes an assertions build fail libstdc++'s vector bounds check. Builds without assertions write out of bounds: Ubuntu wasm-ld 21.1.8 links the object below with status 0, segfaults with index 100000, and aborts with `free(): invalid size` on a slightly larger object.
**Version**: main at c7014922f582 (2026-11-16), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`, on x86_64 Linux.
### Reproducer
```sh
cat > elem.yaml <<'YAML'
--- !WASM
FileHeader:
Version: 0x1
Sections:
- Type: TYPE
Signatures:
- Index: 0
ParamTypes: []
ReturnTypes: []
- Type: IMPORT
Imports:
- Module: env
Field: __indirect_function_table
Kind: TABLE
Table:
Index: 0
ElemType: FUNCREF
Limits:
Minimum: 0x2
- Type: FUNCTION
FunctionTypes: [ 0 ]
- Type: ELEM
Segments:
- Offset:
Opcode: I32_CONST
Value: 1
Functions: [ 70 ]
- Type: CODE
Functions:
- Index: 0
Locals: []
Body: 0B
- Type: CUSTOM
Name: linking
Version: 2
SymbolTable:
- Index: 0
Kind: FUNCTION
Name: f
Flags: [ ]
Function: 0
...
YAML
yaml2obj elem.yaml -o elem.o
wasm-ld --no-entry --export-all -o elem.wasm elem.o
```
The object has one function, and the element segment names function 70. With `Functions: [ 0 ]`, the same object links (status 0).
### Observed
```
libstdc++/bits/stl_vector.h:1263: reference std::vector<unsigned int>::operator[](size_type) [_Tp = unsigned int, _Alloc = std::allocator<unsigned int>]: Assertion '__n < this->size()' failed.
```
```
lld::wasm::ObjFile::parse(bool)
lld::wasm::SymbolTable::addFile(lld::wasm::InputFile*, llvm::StringRef)
lld::wasm::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
```
### Expected
An error for the invalid function index.
### Analysis
The writes:
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/wasm/InputFiles.cpp#L500-L521
The index is stored as read:
https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/llvm/lib/Object/WasmObjectFile.cpp#L1832-L1836
`WasmObjectFile::isValidFunctionIndex` already exists, and the function section is parsed before the element section, so `parseElemSection` could reject an invalid index with a `malformedError`. That would fix `wasm-ld` and every other `WasmObjectFile` user.
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