| Issue |
239491
|
| Summary |
[lld][COFF] lld-link reaches cantFail on a truncated S_OBJNAME or S_COMPILE3 record
|
| Labels |
lld:COFF,
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.
`ObjFile::initializeFlags` reads the first two CodeView symbol records in `.debug$S`, looking for `S_COMPILE3` and `S_OBJNAME`. If one of them is too short for its kind, `SymbolDeserializer::deserializeAs` returns a stream error. `initializeFlags` wraps that call in `cantFail`. An assertions build aborts, and a release build reaches undefined behavior and links without a diagnostic. `initializeFlags` already handles errors from `readSymbolFromStream` for the same records by returning early.
**Version**: main at 4f2748f0ac25 (2026-11-06), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`, on x86_64 Linux.
### Reproducer
`.debug$S` is the CodeView signature (4), then one symbols subsection (`0xF1`, length 4). It holds a single record with `RecordLen = 2` and kind `S_OBJNAME` (`0x1101`), so the record has no payload:
```sh
cat > objname.yaml <<'YAML'
--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 16
SectionData: C3
- Name: '.debug$S'
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: '04000000F10000000400000002000111'
symbols:
- Name: f
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj objname.yaml -o objname.obj
lld-link /dll /noentry /machine:x64 /out:objname.dll objname.obj
```
The `S_COMPILE3` path behaves the same way: with kind `0x113C` (`SectionData: '04000000F10000000400000002003C11'`), the same command aborts in the other `cantFail`. A well-formed `S_OBJNAME` (signature 0, name `x`: `'04000000F10000000A000000080001110000000078000000'`) links (status 0). `llvm-readobj --codeview` rejects both short records with the same stream error.
### Observed
```
Failure value returned from cantFail wrapped call
Stream Error: The stream is too short to perform the requested operation.
UNREACHABLE executed at llvm/include/llvm/Support/Error.h:810!
```
```
lld::coff::ObjFile::initializeFlags()
lld::coff::ObjFile::parse()
lld::coff::LinkerDriver::addFile(lld::coff::InputFile*)
lld::coff::LinkerDriver::addObjectFile(lld::coff::COFFLinkerContext&, llvm::MemoryBufferRef, llvm::StringRef, unsigned long, bool)
```
### Expected
A warning or error about the malformed record, or the same early return used for unreadable records. No `cantFail`.
### Analysis
`initializeFlags` returns early when `readSymbolFromStream` fails, but uses `cantFail` on both payload deserializations:
https://github.com/llvm/llvm-project/blob/4f2748f0ac25a1538e09d50f011c67ec828a205e/lld/COFF/InputFiles.cpp#L1005-L1022
`cantFail` sends the error to `llvm_unreachable`:
https://github.com/llvm/llvm-project/blob/4f2748f0ac25a1538e09d50f011c67ec828a205e/llvm/include/llvm/Support/Error.h#L797-L811
Handling the `Expected` from `deserializeAs` the same way as the one from `readSymbolFromStream` (consume the error and return, or warn) would fix both cases. Without assertions, `objName.Signature` and `cs.Flags` are read from a failed deserialization, so `pchSignature` and `hotPatchable` can be set from garbage.
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