| Issue |
242862
|
| Summary |
[clang][Comments] Infinite recursion in Sema::resolveParamCommandIndexes -> SIGSEGV when AST-dumping after a fatal missing-include error with -ferror-limit=0
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
HaythamBi
|
# [clang][Comments] Infinite recursion / stack overflow (SIGSEGV) in `clang::comments::Sema::resolveParamCommandIndexes` when dumping AST (`-ast-dump=json`) after a fatal missing-include error with `-ferror-limit=0`
## Summary
`clang-19` crashes with a segmentation fault (stack overflow due to infinite recursion) in
`clang::comments::Sema::resolveParamCommandIndexes(const FullComment *)` when:
1. A translation unit has a **fatal `#include "..."` resolution failure** (header not found), and
2. `-ferror-limit=0` is passed (so clang keeps parsing/recovering past the fatal error instead of aborting), and
3. The frontend is asked to dump the AST as JSON (`-Xclang -ast-dump=json`), and
4. The affected declarations have Doxygen-style (`/** ... */`) documentation comments attached (common in large, documented C codebases).
The crash happens while `JSONDumper`/`RawComment::parse` is walking a **declaration that was synthesized/corrupted by clang's error-recovery path** after the fatal missing-include error (observed in our case as a bogus recovery `TypedefDecl` and an `isInvalid=true` function-pointer `FieldDecl`). `resolveParamCommandIndexes` appears to recurse on itself with the *same* `FullComment`/declaration, with no termination condition, until the stack overflows.
This is 100% deterministic for the affected file (confirmed by running the exact same file+flags 3× in isolation — identical crash, identical truncation point every time), i.e. it is **not** a race condition or signal-handling artifact — it's a genuine infinite-recursion bug in the comment-indexing logic reachable via normal error recovery.
## Environment
- `clang version 19.1.7`
- Reproduced identically on two independent builds:
- Homebrew `llvm@19` on `arm64-apple-darwin25.6.0`
- Alpine `clang19-19.1.7-r6` on `aarch64-alpine-linux-musl` (inside a clean `alpine:3.22` container)
- Both report: `Thread model: posix`
## Exact command line (sanitized)
```
clang-19 \
-Wno-error=implicit-function-declaration \
-Wno-error=missing-declarations \
-Wno-error=int-conversion \
-D_FORTIFY_SOURCE=0 \
-Wno-format-security \
-Xclang -ast-dump=json \
-E \
-ferror-limit=0 \
<source.c>
```
(`-E` + `-Xclang -ast-dump=json` together is intentional in our tool's invocation — the `-Xclang` flag overrides the frontend action to AST-dump-as-JSON even though `-E` is also present; this reproduces identically with or without `-E`.)
## Observed failure
```
<source.c>:NN:10: fatal error: '<some_project_internal_header>.h' file not found
NN | #include "<some_project_internal_header>.h"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: clang-19 ... -Xclang -ast-dump=json -E -ferror-limit=0 <source.c>
1. <eof> parser at end of file
clang-19: error: clang frontend command failed with exit code 139 (use -v to see invocation)
```
## Full symbolized crash backtrace (from the macOS/Homebrew build, same shape on Alpine)
```
#0 0x00000001157b31a0 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int)
#1 0x00000001157b0ea8 llvm::sys::RunSignalHandlers()
#2 0x00000001157b2754 llvm::sys::CleanupOnSignal(unsigned long)
#3 0x00000001156ee920 CrashRecoverySignalHandler(int)
#4 0x0000000187db1744 (libsystem_platform.dylib)
#5 0x0000000109643c9c clang::comments::Sema::resolveParamCommandIndexes(clang::comments::FullComment const*)
#6 0x0000000109643c9c clang::comments::Sema::resolveParamCommandIndexes(clang::comments::FullComment const*) <-- frame #5 calling itself with the same address/argument: infinite recursion
#7 0x0000000109643960 clang::comments::Sema::actOnFullComment(llvm::ArrayRef<clang::comments::BlockContentComment*>)
#8 0x000000010963e170 clang::comments::Parser::parseFullComment()
#9 0x00000001099afb88 clang::RawComment::parse(clang::ASTContext const&, clang::Preprocessor const*, clang::Decl const*) const
#10 0x00000001094f4bc4 clang::ASTNodeTraverser<clang::JSONDumper, clang::JSONNodeDumper>::Visit(clang::Decl const*, bool)::'lambda'()::operator()() const
#11 0x0000000109503dec clang::NodeStreamer::AddChild<...>(...)
#12 0x00000001094f4870 clang::NodeStreamer::AddChild<...>(...)
#13 0x00000001094f4c94 clang::ASTNodeTraverser<...>::Visit(...)::'lambda'()::operator()() const
#14 0x00000001094f4618 clang::NodeStreamer::AddChild<...>(...)
#15 0x00000001094e7628 clang::Decl::dump(llvm::raw_ostream&, bool, clang::ASTDumpOutputFormat) const
#16 0x000000010afde5e4 (anonymous namespace)::ASTPrinter::print(clang::Decl*)
#17 0x000000010937f340 clang::ParseAST(clang::Sema&, bool, bool)
#18 0x000000010b08c6e0 clang::FrontendAction::Execute()
#19 0x000000010b014918 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&)
#20 0x000000010b0f84bc clang::ExecuteCompilerInvocation(clang::CompilerInstance*)
#21 0x0000000104706674 cc1_main(...)
#22 0x0000000104703cc8 ExecuteCC1Tool(...)
#23 0x000000010acd2b38 llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(...)::$_0>(long)
#24 0x00000001156ee664 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>)
#25 0x000000010acd23bc clang::driver::CC1Command::Execute(...)
#26 0x000000010ac97ef0 clang::driver::Compilation::ExecuteCommand(...)
#27 0x000000010ac981b0 clang::driver::Compilation::ExecuteJobs(...)
#28 0x000000010acb39a4 clang::driver::Driver::ExecuteCompilation(...)
#29 0x0000000104702c04 clang_main(int, char**, llvm::ToolContext const&)
#30 0x0000000104711328 main
```
Frames `#5` and `#6` are **identical addresses** (`0x109643c9c`), i.e. `resolveParamCommandIndexes` is calling itself recursively with no change in state — a classic unbounded-recursion bug, not a deep-but-finite call chain.
## Shape of the construct that triggers it (best-effort reconstruction, not independently verified to crash in isolation)
The crash occurs while JSON-dumping a `struct` whose fields are documented with standard Doxygen block comments (`/** ... @note ... */`), where the **last field is a function-pointer-typed `FieldDecl`** that clang's error recovery has marked `isInvalid: true` (its parameter list references a type that could not be resolved because of the earlier fatal missing-include error). Shape (renamed/sanitized, illustrative only):
```c
#include "project_internal_config.h" // <- fails to resolve: fatal error, but -ferror-limit=0 keeps going
typedef unsigned int uidx;
typedef struct Expr Expr;
struct Operator;
struct SomeHandle {
/** implementation-specific state */
void *state;
/** number of unique keys stored (not counting reserved 0)
* @note some clarifying note here.
*/
uidx num_keys;
/** the actual values
* @note some other clarifying note here.
*/
Expr **values;
/** installs a key into the table */
int (*put_keys)(struct SomeHandle *, uidx *, uidx *, Expr **, uidx *, uidx *, UnresolvedParamType *);
};
```
**Note:** I was not able to get this exact shape to crash in isolation with clang 19.1.7 in a handful of attempts — the real crash only reproduces with the actual ~large, heavily-commented production translation unit after the real cascading recovery state (we observed, in a separate related run, that clang's recovery synthesizes additional corrupted declarations, e.g. a bogus `TypedefDecl "bool" : int(int*)`, further upstream in the same TU). I'm including this shape only to help narrow down the search; I can provide the full (large, ~40MB truncated) `-ast-dump=json` output and/or a preprocessed `.i` file privately if that would help maintainers, since the real source is proprietary and I'd rather not post it publicly here.
## Reproduction determinism
Ran the identical file + identical flags **3 times sequentially** (no concurrency) on both macOS (Homebrew clang 19.1.7) and inside a clean `alpine:3.22` container with `clang19-19.1.7-r6` (matching package versions) — **100% reproducible**, byte-identical truncation point and crash signature every single time. Also confirmed this is unrelated to any platform macro predefinition (`-Dlinux -D__linux__` etc. make zero difference — byte-identical output with or without them).
## Expected behavior
`-ferror-limit=0` is documented to let the frontend keep parsing past errors on a best-effort basis; it should not be possible for downstream AST/comment processing to enter unbounded recursion and crash the process (ideally it should emit the error-recovered declaration without comments, or bail out of comment-parsing for that specific invalid declaration, rather than stack-overflowing the whole process).
## Impact
In our use case (a static-analysis tool that invokes `clang -ast-dump=json` on arbitrary/incomplete customer C source trees as a best-effort AST extraction step), this is a reliable DoS: any source file reaching this recovery state will **always** crash clang with SIGSEGV, producing a truncated/unusable AST dump, rather than the (expected and otherwise correctly handled) non-crashing "best effort with errors" outcome that `-ferror-limit=0` is meant to provide.
## Additional notes
- The clang **driver** process survives the crash of the `cc1` child (via `CrashRecoveryContext`) and reports a plain exit code `1` to its own parent — i.e., callers that only check `$? == 0` vs `!= 0` cannot distinguish "ordinary compile errors" from "the frontend segfaulted," they must parse stderr for `"clang frontend command failed with exit code 139"` / `"Segmentation fault"` instead.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs