| Issue |
243905
|
| Summary |
[clangd] SIGSEGV in clang::comments::Lexer::lex formatting doc comment from stale module BMI
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
Anthony-Gaudino
|
## Summary
`clangd --check` (and background indexing) segfaults while formatting a documentation comment belonging to a declaration deserialized from a **stale** C++20 module BMI — i.e. the module source was edited (shifting comment locations) after the `.pcm` was built, and a dependent TU calls a documented function from that BMI. No crash occurs when the BMI is in sync with its source, and no crash occurs when checking the module itself.
## Environment
- `clangd version 23.1.1` (`Features: linux`, `Platform: x86_64-pc-linux-gnu`)
- `clang version 23.1.1`, `Target: x86_64-pc-linux-gnu`
- OS: Linux 7.2.9-1-cachyos x86_64 GNU/Linux
## Reproduction
`SA.cppm`:
```cpp
export module stale.a;
export namespace stale {
/** Add one.
*
* @param[in] x - Input value.
*
* @return x plus one.
*/
[[nodiscard]] constexpr int f(int x) noexcept {
return x + 1;
}
} // namespace stale
```
`SB.cppm`:
```cpp
export module stale.b;
import stale.a;
namespace stale {
int calls(int v) {
return f(v);
}
} // namespace stale
```
Steps:
1. Build the BMI from the pristine source: `clang++ -std=c++26 --precompile SA.cppm -o SA.pcm`
2. Edit `SA.cppm` WITHOUT rebuilding (insert 4 blank lines before the doc comment).
3. Check the dependent TU (with `compile_commands.json` mapping `stale.a` to the stale `SA.pcm`):
`clangd --check=SB.cppm --compile-commands-dir=. --clang-tidy=false --background-index=false --log=error`
`compile_commands.json`:
```json
[{"directory": "<dir>",
"command": "/usr/bin/clang++ -std=c++26 -fmodule-file=stale.a=<dir>/SA.pcm <dir>/SB.cppm",
"file": "<dir>/SB.cppm", "output": "<dir>/SB.o"}]
```
## Expected Behavior
No crash. `clang++` itself accepts this exact stale BMI and compiles the importer cleanly:
```sh
clang++ -std=c++26 -fmodule-file=stale.a=SA.pcm -fsyntax-only SB.cppm
# exit 0, no diagnostics
```
so the BMI is loadable by design — clangd should not SIGSEGV when formatting a comment from it. Either validate the BMI against its current source before pulling comments out of it, or bounds-check the sliced range in the `RawComment` → `SourceManager` formatting path so a mismatch degrades to a missing comment instead of a lexer overrun.
## Actual Behavior
Deterministic SIGSEGV (exit 139) on every run:
```
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: /usr/bin/clangd --check=/tmp/opencode/stale/SB.cppm --compile-commands-dir=/tmp/opencode/staledb --clang-tidy=false --background-index=false --log=error
#0 0x00007fc00922a460 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/lib/libLLVM.so.23.1+0x8a2a460)
#1 0x00007fc00922aeea (/usr/lib/libLLVM.so.23.1+0x8a2aeea)
#2 0x00007fc000044cb0 (/usr/lib/libc.so.6+0x44cb0)
#3 0x00007fc00c30ba0b clang::comments::Lexer::lex(clang::comments::Token&) (/usr/lib/libclang-cpp.so.23.1+0x210ba0b)
#4 0x00007fc00c30b23f (/usr/lib/libclang-cpp.so.23.1+0x210b23f)
#5 0x00007fc00c30b04f clang::RawComment::getFormattedLines(clang::SourceManager const&, clang::DiagnosticsEngine&) const (/usr/lib/libclang-cpp.so.23.1+0x210b04f)
#6 0x00007fc00c3099c4 clang::RawComment::getFormattedText[abi:cxx11](clang::SourceManager const&, clang::DiagnosticsEngine&) const (/usr/lib/libclang-cpp.so.23.1+0x21099c4)
#7 0x0000557224c78e66 (/usr/bin/clangd+0x3bce66)
#8 0x000055722523f065 (/usr/bin/clangd+0x983065)
#9 0x0000557224e01ca2 (/usr/bin/clangd+0x545ca2)
#10 0x0000557224cb1c45 (/usr/bin/clangd+0x3f5c45)
#11 0x00007fc000027c8e (/usr/lib/libc.so.6+0x27c8e)
#12 0x00007fc000027dcb __libc_start_main (/usr/lib/libc.so.6+0x27dcb)
#13 0x0000557225398555 (/usr/bin/clangd+0xadc555)
```
## Additional Context
What I ruled out while minimizing (all verified with self-consistent hand-built BMIs, none of which crash):
- Comment text/shape: identical doc comments, 50-comment BMIs, fenced ```cpp blocks, `@param[in]`, backticks, file-level `/** @file */` docs.
- Module structure: named modules, partitions importing sibling partitions, global module fragments, `import std`, full production TU flags.
- The trigger needs all three: (1) stale BMI, (2) an importer that *calls* (not merely imports, or uses types from) a documented function, (3) comment formatting during indexing. Import-only TUs and self-checks of the module itself never crash.
- Config-independent: reproduces with `--enable-config=false`.
Real-world impact: hit in a large C++26 modular codebase where one partition's source was edited (doc polish + blank lines) after its BMI was built (source 15:15 vs BMI 14:59; the in-sync sibling partition never crashed). Every background-indexing pass crashed (`Server process exited with signal SIGSEGV`, then `crashed 5 times in the last 3 minutes. The server will not be restarted`), and opening any TU that transitively called the stale module's functions killed the server. Rebuilding the single stale BMI eliminated all crashes.
Possibly related: #34681 (open) — libclang crashes in the same `clang::comments::Lexer::lex` when a comment is sliced from a buffer that no longer matches (there: file edited mid-parse). Same lexer fragility, different vector (libclang C API vs stale module BMI via clangd); not a duplicate.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs