Issue 239389
Summary [lld][wasm] wasm-ld crashes on an export of an imported function in an object without a symbol table
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 on a relocatable object that has no symbol table and exports an imported function. `WasmObjectFile` builds the symbols from the export section. Since #123427 it accepts a function export that names an import, but it leaves the symbol's flags at 0, so the symbol counts as defined. `ObjFile::createDefined` then indexes `functions` with `ElementIndex - getNumImportedFunctions()`, which underflows.

This is a regression in LLVM 20. Ubuntu `wasm-ld` 18.1.8 and 19.1.7 reject the object with `error: exp.o: invalid function export`. 20.1.8, 21.1.8, 22.1.2 and main segfault or fail an assertion. The change is 9fdc38c81c7d ([WebAssembly][Object] Support more elem segment flags, #123427).

**Version**: main at 7374c58a131c (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 > exp.yaml <<'YAML'
--- !WASM
FileHeader:
  Version:         0x1
Sections:
  - Type: TYPE
    Signatures:
      - Index:           0
        ParamTypes: []
        ReturnTypes:     []
  - Type:            IMPORT
    Imports:
 - Module:          env
        Field:           g
        Kind: FUNCTION
        SigIndex:        0
  - Type:            FUNCTION
 FunctionTypes:   [ 0 ]
  - Type:            EXPORT
    Exports:
      - Name:            f
        Kind:            FUNCTION
        Index: 0
  - Type:            CODE
    Functions:
      - Index:           1
 Locals:          []
        Body:            0B
  - Type: CUSTOM
    Name:            linking
    Version:         2
YAML
yaml2obj exp.yaml -o exp.o
wasm-ld --no-entry -o exp.wasm exp.o
```

Function 0 is the import `env.g`, and the export `f` names it. The `linking` section is present but has no symbol table. With the export's `Index: 1` (the defined function) instead of 0, the same command links (status 0).

### Observed

```
.../bits/stl_vector.h:1253: reference std::vector<lld::wasm::InputFunction *>::operator[](size_type) [_Tp = lld::wasm::InputFunction *, _Alloc = std::allocator<lld::wasm::InputFunction *>]: Assertion '__n < this->size()' failed.
```

```
lld::wasm::ObjFile::createDefined(llvm::object::WasmSymbol const&)
lld::wasm::ObjFile::parse(bool)
lld::wasm::SymbolTable::addFile(lld::wasm::InputFile*, llvm::StringRef)
lld::wasm::(anonymous namespace)::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
```

Release builds (20.1.8 to 22.1.2) segfault in `ObjFile::createDefined`.

### Expected

An error, as in 19.1.7 (`invalid function export`). Alternatively the export could be turned into an undefined symbol for `g`, which is what the comment added in #123427 says should happen.

### Analysis

Without a symbol table, the symbols come from `parseExportSection`. Since #123427, a function export only needs a valid function index. For an imported function it skips the definition lookup and leaves `Info.Flags = 0`:

https://github.com/llvm/llvm-project/blob/7374c58a131c5ec367ee95e83ddedb7c450cfe77/llvm/lib/Object/WasmObjectFile.cpp#L1541-L1556

The symbol is added with no `WASM_SYMBOL_UNDEFINED` flag and a null signature:

https://github.com/llvm/llvm-project/blob/7374c58a131c5ec367ee95e83ddedb7c450cfe77/llvm/lib/Object/WasmObjectFile.cpp#L1600-L1601

so `WasmSymbol::isDefined()` is true:

https://github.com/llvm/llvm-project/blob/7374c58a131c5ec367ee95e83ddedb7c450cfe77/llvm/include/llvm/Object/Wasm.h#L72-L76

`ObjFile::parse` passes defined symbols to `createDefined`, which indexes the object's defined functions with `0 - getNumImportedFunctions()`:

https://github.com/llvm/llvm-project/blob/7374c58a131c5ec367ee95e83ddedb7c450cfe77/lld/wasm/InputFiles.cpp#L618-L621

https://github.com/llvm/llvm-project/blob/7374c58a131c5ec367ee95e83ddedb7c450cfe77/lld/wasm/InputFiles.cpp#L669-L671

The symbol-table path already rejects a defined function symbol with an imported index (`invalid function symbol index`). Two fixes would work: set `WASM_SYMBOL_UNDEFINED` (and the import's signature) on export-derived symbols for imported functions, as the comment intends, or have `wasm-ld` reject such objects.

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

Reply via email to