[BUG] ICE in (anonymous namespace)::PointerExprEvaluator::VisitCastExpr(clang::CastExpr const*)
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## Bug Report: ICE in (anonymous namespace)::PointerExprEvaluator::VisitCastExpr(clang::CastExpr const*)
Clang crashes when compiling the following C code.
**Compiler**: clang-22 (LLVM 22.1.5, commit 5ea218a153f4d2f815b8244eab3e4b4ba5e00e6c)
**Compilation flags**: -O0
**Platform**: x86_64-linux-gnu
**Component**: frontend
This is a **frontend** crash (reproduces with `-emit-llvm -Xclang -disable-llvm-passes`).
## Reduced Test Case
> Minimized from the original fuzzer-generated input using `creduce`.
```sh
clang -O0 test.c -o /dev/null
```
```c
a() {
**(char (*)[])0
```
## Crash Output
```
clang-22: /root/compilers/llvm-project-release/clang/include/clang/AST/TypeBase.h:953: const clang::ExtQualsTypeCommonBase* clang::QualType::getCommonPtr() const: Assertion \`!isNull() && "Cannot retrieve a NULL type pointer"' failed.
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: /root/objects/llvm-project-release.obj/bin/clang-22 -cc1 -triple x86_64-unknown-linux-gnu -O0 -emit-obj -dumpdir /dev/null- -disable-free -clear-ast-before-backend -main-file-name aIShgXQXTDVf_surfuzz_6.c -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/data -fcoverage-compilation-dir=/root/data -resource-dir /root/objects/llvm-project-release.obj/lib/clang/22 -internal-isystem /root/objects/llvm-project-release.obj/lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -faddrsig -fdwarf2-cfi-asm -o /tmp/aIShgXQXTDVf_surfuzz_6-40b95d.o -x c /root/outputs/experiments-on-metamut/surfuzz,gcc,3/aIShgXQXTDVf_surfuzz_6.c
1\. /root/outputs/experiments-on-metamut/surfuzz,gcc,3/aIShgXQXTDVf_surfuzz_6.c:2:38: current parser token ','
2\. /root/outputs/experiments-on-metamut/surfuzz,gcc,3/aIShgXQXTDVf_surfuzz_6.c:1:43: parsing function body 'PotentiallyEvaluatedSizeofWarn'
3\. /root/outputs/experiments-on-metamut/surfuzz,gcc,3/aIShgXQXTDVf_surfuzz_6.c:1:43: in compound statement ('{}')
#0 0x0000564c890f52d2 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f62d2)
#1 0x0000564c890f1caf llvm::sys::RunSignalHandlers() (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f2caf)
#2 0x0000564c890f1dfc SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007f6a30861520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007f6a308b5a7c __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76
#5 0x00007f6a308b5a7c __pthread_kill_internal ./nptl/./nptl/pthread_kill.c:78:10
#6 0x00007f6a308b5a7c pthread_kill ./nptl/./nptl/pthread_kill.c:89:10
#7 0x00007f6a30861476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007f6a308477f3 abort ./stdlib/./stdlib/abort.c:81:7
#9 0x00007f6a3084771b _nl_load_domain ./intl/./intl/loadmsgcat.c:1177:9
#10 0x00007f6a30858e96 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x0000564c89534efe clang::QualType::getNonReferenceType() const (/root/objects/llvm-project-release.obj/bin/clang-22+0x4935efe)
#12 0x0000564c8cc29ba1 (anonymous namespace)::LValue::addUnsizedArray((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::QualType) ExprConstant.cpp:0:0
#13 0x0000564c8ccdce74 (anonymous namespace)::PointerExprEvaluator::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#14 0x0000564c8cc58a82 EvaluatePointer(clang::Expr const*, (anonymous namespace)::LValue&, (anonymous namespace)::EvalInfo&, bool) ExprConstant.cpp:0:0
#15 0x0000564c8cc58c24 (anonymous namespace)::LValueExprEvaluator::VisitUnaryDeref(clang::UnaryOperator const*) ExprConstant.cpp:0:0
#16 0x0000564c8cc4fa99 EvaluateLValue(clang::Expr const*, (anonymous namespace)::LValue&, (anonymous namespace)::EvalInfo&, bool) ExprConstant.cpp:0:0
#17 0x0000564c8ccd8f43 (anonymous namespace)::IntExprEvaluator::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#18 0x0000564c8ccd88fb (anonymous namespace)::IntExprEvaluator::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#19 0x0000564c8cc747e0 Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) ExprConstant.cpp:0:0
#20 0x0000564c8cc751b1 EvaluateAsRValue((anonymous namespace)::EvalInfo&, clang::Expr const*, clang::APValue&) ExprConstant.cpp:0:0
#21 0x0000564c8cc7ef98 clang::Expr::EvaluateAsInt(clang::Expr::EvalResult&, clang::ASTContext const&, clang::Expr::SideEffectsKind, bool) const (/root/objects/llvm-project-release.obj/bin/clang-22+0x807ff98)
#22 0x0000564c8be24816 DiagnoseBadShiftValues(clang::Sema&, clang::ActionResult&, clang::ActionResult&, clang::SourceLocation, clang::BinaryOperatorKind, clang::QualType) SemaExpr.cpp:0:0
#23 0x0000564c8be7de62 clang::Sema::CheckShiftOperands(clang::ActionResult&, clang::ActionResult&, clang::SourceLocation, clang::BinaryOperatorKind, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x727ee62)
#24 0x0000564c8be8cbc1 clang::Sema::CreateBuiltinBinOp(clang::SourceLocation, clang::BinaryOperatorKind, clang::Expr*, clang::Expr*, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x728dbc1)
#25 0x0000564c8be8f041 clang::Sema::ActOnBinOp(clang::Scope*, clang::SourceLocation, clang::tok::TokenKind, clang::Expr*, clang::Expr*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x7290041)
#26 0x0000564c8b99597e clang::Parser::ParseRHSOfBinaryExpression(clang::ActionResult, clang::prec::Level) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9697e)
#27 0x0000564c8b997d6d clang::Parser::ParseExpression(clang::TypoCorrectionTypeBehavior) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d98d6d)
#28 0x0000564c8b99b8dd clang::Parser::ParseParenExpression(clang::ParenParseOption&, bool, clang::ParenExprKind, clang::TypoCorrectionTypeBehavior, clang::OpaquePtr&, clang::SourceLocation&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9c8dd)
#29 0x0000564c8b99ccdd clang::Parser::ParseExprAfterUnaryExprOrTypeTrait(clang::Token const&, bool&, clang::OpaquePtr&, clang::SourceRange&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9dcdd)
#30 0x0000564c8b99d071 clang::Parser::ParseUnaryExprOrTypeTraitExpression() (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9e071)
#31 0x0000564c8b992971 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d93971)
#32 0x0000564c8b9946b9 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, clang::TypoCorrectionTypeBehavior, bool, bool*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d956b9)
#33 0x0000564c8b99475c clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9575c)
#34 0x0000564c8b99488a clang::Parser::ParseSimpleExpressionList(llvm::SmallVectorImpl&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9588a)
#35 0x0000564c8b99c0f4 clang::Parser::ParseParenExpression(clang::ParenParseOption&, bool, clang::ParenExprKind, clang::TypoCorrectionTypeBehavior, clang::OpaquePtr&, clang::SourceLocation&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9d0f4)
#36 0x0000564c8b992747 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d93747)
#37 0x0000564c8b9946b9 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, clang::TypoCorrectionTypeBehavior, bool, bool*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d956b9)
#38 0x0000564c8b99c058 clang::Parser::ParseParenExpression(clang::ParenParseOption&, bool, clang::ParenExprKind, clang::TypoCorrectionTypeBehavior, clang::OpaquePtr&, clang::SourceLocation&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9d058)
#39 0x0000564c8b992747 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, bool&, clang::TypoCorrectionTypeBehavior, bool, bool*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d93747)
#40 0x0000564c8b9946b9 clang::Parser::ParseCastExpression(clang::CastParseKind, bool, clang::TypoCorrectionTypeBehavior, bool, bool*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d956b9)
#41 0x0000564c8b99475c clang::Parser::ParseAssignmentExpression(clang::TypoCorrectionTypeBehavior) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d9575c)
#42 0x0000564c8b997d6d clang::Parser::ParseExpression(clang::TypoCorrectionTypeBehavior) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d98d6d)
#43 0x0000564c8ba1cadd clang::Parser::ParseExprStatement(clang::Parser::ParsedStmtContext) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6e1dadd)
#44 0x0000564c8ba15d80 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6e16d80)
#45 0x0000564c8ba16703 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6e17703)
#46 0x0000564c8ba1d361 clang::Parser::ParseCompoundStatementBody(bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6e1e361)
#47 0x0000564c8ba1db3f clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6e1eb3f)
#48 0x0000564c8b921cab clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d22cab)
#49 0x0000564c8b96442d clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d6542d)
#50 0x0000564c8b91c7e1 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d1d7e1)
#51 0x0000564c8b91d00f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d1e00f)
#52 0x0000564c8b925896 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d26896)
#53 0x0000564c8b926915 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d27915)
#54 0x0000564c8b926db3 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d27db3)
#55 0x0000564c8b90685a clang::ParseAST(clang::Sema&, bool, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d0785a)
#56 0x0000564c89d7e987 clang::FrontendAction::Execute() (/root/objects/llvm-project-release.obj/bin/clang-22+0x517f987)
#57 0x0000564c89d060e9 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x51070e9)
#58 0x0000564c89e7b95e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x527c95e)
#59 0x0000564c85b86657 cc1_main(llvm::ArrayRef, char const*, void*) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf87657)
#60 0x0000564c85b7b694 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#61 0x0000564c85b80907 clang_main(int, char**, llvm::ToolContext const&) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf81907)
#62 0x0000564c85a3e0ea main (/root/objects/llvm-project-release.obj/bin/clang-22+0xe3f0ea)
#63 0x00007f6a30848d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#64 0x00007f6a30848e40 call_init ./csu/../csu/libc-start.c:128:20
#65 0x00007f6a30848e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#66 0x0000564c85b7acb5 _start (/root/objects/llvm-project-release.obj/bin/clang-22+0xf7bcb5)
clang: error: unable to execute command: Aborted (core dumped)
clang: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 22.1.5 (https://github.com/llvm/llvm-project.git 5ea218a153f4d2f815b8244eab3e4b4ba5e00e6c)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /root/objects/llvm-project-release.obj/bin
Build config: \+assertions
clang: note: diagnostic msg:
********************
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang: note: diagnostic msg: /tmp/aIShgXQXTDVf_surfuzz_6-18bd52.c
clang: note: diagnostic msg: /tmp/aIShgXQXTDVf_surfuzz_6-18bd52.sh
clang: note: diagnostic msg:
********************
EXIT_CODE: 1
```
## Preprocessed Source & Run Script
**Preprocessed source** (`/tmp/aIShgXQXTDVf_surfuzz_6-18bd52.c`):
```c
# 1 ""
# 1 "/root/outputs/experiments-on-metamut/surfuzz,gcc,3/aIShgXQXTDVf_surfuzz_6.c"
int PotentiallyEvaluatedSizeofWarn(int n) {
(void)(sizeof(**((char(*)[n])0) <<3,3));
}
void PotentiallyEvaluatedSizeofTypeofWarn(int n) {
(void)(sizeof(__typeof((*(int (*)(void))[0 (1 << 3, n)])0) <<3,3));
}
void PotentiallyEvaluatedBitwiseWarn(int n) {
(void)(~(0 << 32,(1<<(n?3:2))));
}
void PotentiallyEvaluatedTypeofWarn(int n) {
__typeof(*(0 << 32,(int(*)[n])0)) x;
(void)x;
}
void PotentiallyEvaluatedArrayBoundWarn(int n) {
(void)*(int(*)[(0 << 32,n)])0;
}
```
**Run script** (`/tmp/aIShgXQXTDVf_surfuzz_6-18bd52.sh`):
```sh
# Crash reproducer for clang version 22.1.5 (https://github.com/llvm/llvm-project.git 5ea218a153f4d2f815b8244eab3e4b4ba5e00e6c)
# Driver args: "-O0" "/root/outputs/experiments-on-metamut/surfuzz,gcc,3/aIShgXQXTDVf_surfuzz_6.c" "-o" "/dev/null"
# Original command: "/root/objects/llvm-project-release.obj/bin/clang-22" "-cc1" "-triple" "x86_64-unknown-linux-gnu" "-O0" "-emit-obj" "-dumpdir" "/dev/null-" "-disable-free" "-clear-ast-before-backend" "-main-file-name" "aIShgXQXTDVf_surfuzz_6.c" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/root/data" "-fcoverage-compilation-dir=/root/data" "-resource-dir" "/root/objects/llvm-project-release.obj/lib/clang/22" "-internal-isystem" "/root/objects/llvm-project-release.obj/lib/clang/22/include" "-internal-isystem" "/usr/local/include" "-internal-isystem" "/usr/lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include" "-internal-externc-isystem" "/usr/include/x86_64-linux-gnu" "-internal-externc-isystem" "/include" "-internal-externc-isystem" "/usr/include" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf" "-faddrsig" "-fdwarf2-cfi-asm" "-o" "/tmp/aIShgXQXTDVf_surfuzz_6-40b95d.o" "-x" "c" "/root/outputs/experiments-on-metamut/surfuzz,gcc,3/aIShgXQXTDVf_surfuzz_6.c"
"/root/objects/llvm-project-release.obj/bin/clang-22" "-cc1" "-triple" "x86_64-unknown-linux-gnu" "-O0" "-emit-obj" "-dumpdir" "/dev/null-" "-disable-free" "-clear-ast-before-backend" "-main-file-name" "aIShgXQXTDVf_surfuzz_6.c" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-mframe-pointer=all" "-fmath-errno" "-ffp-contract=on" "-fno-rounding-math" "-mconstructor-aliases" "-funwind-tables=2" "-target-cpu" "x86-64" "-tune-cpu" "generic" "-debugger-tuning=gdb" "-fdebug-compilation-dir=/root/data" "-fcoverage-compilation-dir=/root/data" "-ferror-limit" "19" "-fgnuc-version=4.2.1" "-fskip-odr-check-in-gmf" "-faddrsig" "-fdwarf2-cfi-asm" "-x" "c" "aIShgXQXTDVf_surfuzz_6-18bd52.c"
```
* found via a fuzzer
[aIShgXQXTDVf_surfuzz_6-18bd52.c](https://github.com/user-attachments/files/29127700/aIShgXQXTDVf_surfuzz_6-18bd52.c)
[aIShgXQXTDVf_surfuzz_6-18bd52.sh](https://github.com/user-attachments/files/29127701/aIShgXQXTDVf_surfuzz_6-18bd52.sh)
Contributor guide
Research direction
Start with the reduced C test case and reproduce the crash using the provided clang -O0 command. Read ExprConstant.cpp around PointerExprEvaluator::VisitCastExpr and LValue::addUnsizedArray, then add a regression test using the reproducer; done means clang no longer hits the QualType null-type assertion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100