llvm / llvm/llvm-project

[BUG] ICE in clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**)

Open
#204,760 1 comment 0 reactions 0 assignees View on GitHub
clang:codegen crash-on-invalid generated by fuzzer
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Bug Report: ICE in clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**)

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() { __complex__ b = __builtin_constant_p(b); }

```

## Crash Output

```
/root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c:1:1: error: type specifier missing, defaults to 'int'; ISO C99 and later do not support implicit int [-Wimplicit-int]
1 | main ()
| ^
| int
/root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c:6:5: error: call to undeclared library function 'abort' with type 'void (void) __attribute__((noreturn))'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
6 | abort ();
| ^
/root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c:6:5: note: include the header or explicitly provide a declaration for 'abort'
/root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c:13:3: error: call to undeclared library function 'exit' with type 'void (int) __attribute__((noreturn))'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
13 | exit (0);
| ^
/root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c:13:3: note: include the header or explicitly provide a declaration for 'exit'
clang-22: /root/compilers/llvm-project-release/clang/lib/CodeGen/CGExprScalar.cpp:5989: llvm::Value* clang::CodeGen::CodeGenFunction::EmitScalarExpr(const clang::Expr*, bool): Assertion \`E && hasScalarEvaluationKind(E->getType()) && "Invalid scalar expression to emit"' 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 IeitigMULBFg_surfuzz_15.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/IeitigMULBFg_surfuzz_15-5082b0.o -x c /root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c
1\. parser at end of file
2\. /root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c:1:1: LLVM IR generation of declaration 'main'
3\. /root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c:1:1: Generating code for declaration 'main'
#0 0x000055e64f7ae2d2 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f62d2)
#1 0x000055e64f7aacaf llvm::sys::RunSignalHandlers() (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f2caf)
#2 0x000055e64f7aadfc SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007fe981a85520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007fe981ad9a7c __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76
#5 0x00007fe981ad9a7c __pthread_kill_internal ./nptl/./nptl/pthread_kill.c:78:10
#6 0x00007fe981ad9a7c pthread_kill ./nptl/./nptl/pthread_kill.c:89:10
#7 0x00007fe981a85476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007fe981a6b7f3 abort ./stdlib/./stdlib/abort.c:81:7
#9 0x00007fe981a6b71b _nl_load_domain ./intl/./intl/loadmsgcat.c:1177:9
#10 0x00007fe981a7ce96 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x000055e64fb83278 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x48cb278)
#12 0x000055e64ffe8ecb clang::CodeGen::CodeGenFunction::EmitBuiltinExpr(clang::GlobalDecl, unsigned int, clang::CallExpr const*, clang::CodeGen::ReturnValueSlot) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4d30ecb)
#13 0x000055e64fb1a9c0 clang::CodeGen::CodeGenFunction::EmitCallExpr(clang::CallExpr const*, clang::CodeGen::ReturnValueSlot, llvm::CallBase**) (/root/objects/llvm-project-release.obj/bin/clang-22+0x48629c0)
#14 0x000055e64fb8a9d9 (anonymous namespace)::ScalarExprEmitter::VisitCallExpr(clang::CallExpr const*) CGExprScalar.cpp:0:0
#15 0x000055e64fb83232 clang::CodeGen::CodeGenFunction::EmitScalarExpr(clang::Expr const*, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x48cb232)
#16 0x000055e64faec4ed clang::CodeGen::CodeGenFunction::EvaluateExprAsBool(clang::Expr const*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x48344ed)
#17 0x000055e64fd2d897 clang::CodeGen::CodeGenFunction::EmitBranchOnBoolExpr(clang::Expr const*, llvm::BasicBlock*, llvm::BasicBlock*, unsigned long, clang::Stmt::Likelihood, clang::Expr const*, clang::VarDecl const*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4a75897)
#18 0x000055e64fb4a02f (anonymous namespace)::ComplexExprEmitter::VisitAbstractConditionalOperator(clang::AbstractConditionalOperator const*) CGExprComplex.cpp:0:0
#19 0x000055e64fb4b376 clang::StmtVisitorBase >::Visit(clang::Stmt*) CGExprComplex.cpp:0:0
#20 0x000055e64fb4daae (anonymous namespace)::ComplexExprEmitter::Visit(clang::Expr*) CGExprComplex.cpp:0:0
#21 0x000055e64fb50834 (anonymous namespace)::ComplexExprEmitter::EmitBinOps(clang::BinaryOperator const*, clang::QualType) CGExprComplex.cpp:0:0
#22 0x000055e64fb4aec9 clang::StmtVisitorBase >::Visit(clang::Stmt*) CGExprComplex.cpp:0:0
#23 0x000055e64fb4daae (anonymous namespace)::ComplexExprEmitter::Visit(clang::Expr*) CGExprComplex.cpp:0:0
#24 0x000055e64fb4db9e (anonymous namespace)::ComplexExprEmitter::EmitCast(clang::CastKind, clang::Expr*, clang::QualType) CGExprComplex.cpp:0:0
#25 0x000055e64fb4ebae clang::CodeGen::CodeGenFunction::EmitComplexExpr(clang::Expr const*, bool, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4896bae)
#26 0x000055e65006dc8f clang::CodeGen::CodeGenFunction::EmitExprAsInit(clang::Expr const*, clang::ValueDecl const*, clang::CodeGen::LValue, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4db5c8f)
#27 0x000055e650078df0 clang::CodeGen::CodeGenFunction::EmitAutoVarInit(clang::CodeGen::CodeGenFunction::AutoVarEmission const&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4dc0df0)
#28 0x000055e65007cb50 clang::CodeGen::CodeGenFunction::EmitVarDecl(clang::VarDecl const&) (.part.0) CGDecl.cpp:0:0
#29 0x000055e65007d3b0 clang::CodeGen::CodeGenFunction::EmitDecl(clang::Decl const&, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4dc53b0)
#30 0x000055e64fca895c clang::CodeGen::CodeGenFunction::EmitDeclStmt(clang::DeclStmt const&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x49f095c)
#31 0x000055e64fcbfcc5 clang::CodeGen::CodeGenFunction::EmitSimpleStmt(clang::Stmt const*, llvm::ArrayRef) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4a07cc5)
#32 0x000055e64fcb6211 clang::CodeGen::CodeGenFunction::EmitStmt(clang::Stmt const*, llvm::ArrayRef) (/root/objects/llvm-project-release.obj/bin/clang-22+0x49fe211)
#33 0x000055e64fcbf78b clang::CodeGen::CodeGenFunction::EmitCompoundStmtWithoutScope(clang::CompoundStmt const&, bool, clang::CodeGen::AggValueSlot) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4a0778b)
#34 0x000055e64fd279e3 clang::CodeGen::CodeGenFunction::EmitFunctionBody(clang::Stmt const*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4a6f9e3)
#35 0x000055e64fd3ce84 clang::CodeGen::CodeGenFunction::GenerateCode(clang::GlobalDecl, llvm::Function*, clang::CodeGen::CGFunctionInfo const&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4a84e84)
#36 0x000055e64fda0f0d clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4ae8f0d)
#37 0x000055e64fd9bef5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4ae3ef5)
#38 0x000055e64fd9cb83 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4ae4b83)
#39 0x000055e64fda6807 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#40 0x000055e650122d71 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#41 0x000055e650113cc1 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4e5bcc1)
#42 0x000055e651fbf874 clang::ParseAST(clang::Sema&, bool, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d07874)
#43 0x000055e650437987 clang::FrontendAction::Execute() (/root/objects/llvm-project-release.obj/bin/clang-22+0x517f987)
#44 0x000055e6503bf0e9 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x51070e9)
#45 0x000055e65053495e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x527c95e)
#46 0x000055e64c23f657 cc1_main(llvm::ArrayRef, char const*, void*) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf87657)
#47 0x000055e64c234694 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#48 0x000055e64c239907 clang_main(int, char**, llvm::ToolContext const&) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf81907)
#49 0x000055e64c0f70ea main (/root/objects/llvm-project-release.obj/bin/clang-22+0xe3f0ea)
#50 0x00007fe981a6cd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#51 0x00007fe981a6ce40 call_init ./csu/../csu/libc-start.c:128:20
#52 0x00007fe981a6ce40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#53 0x000055e64c233cb5 _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/IeitigMULBFg_surfuzz_15-4ffd98.c
clang: note: diagnostic msg: /tmp/IeitigMULBFg_surfuzz_15-4ffd98.sh
clang: note: diagnostic msg:
********************
EXIT_CODE: 1
```

## Preprocessed Source & Run Script

**Preprocessed source** (`/tmp/IeitigMULBFg_surfuzz_15-4ffd98.c`):

```c
# 1 ""
# 1 "/root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.c"
main ()
{
__complex__ double c = 0.0q;
__complex__ float cst = (__builtin_constant_p (c)? 0.0q : 1.0q / c) + 1.0q;
if (__imag__ c!= 0.0)
abort ();
if (__real__ c!= 0.0)
abort ();
if ((__complex__ double) 0.0 != (__complex__ double) (-0.0))
abort ();
if (0.0 != -0.0)
abort ();
exit (0);
}

```

**Run script** (`/tmp/IeitigMULBFg_surfuzz_15-4ffd98.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,0/IeitigMULBFg_surfuzz_15.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" "IeitigMULBFg_surfuzz_15.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/IeitigMULBFg_surfuzz_15-5082b0.o" "-x" "c" "/root/outputs/experiments-on-metamut/surfuzz,gcc,0/IeitigMULBFg_surfuzz_15.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" "IeitigMULBFg_surfuzz_15.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" "IeitigMULBFg_surfuzz_15-4ffd98.c"

```

* found via a fuzzer

[IeitigMULBFg_surfuzz_15-4ffd98.c](https://github.com/user-attachments/files/29128247/IeitigMULBFg_surfuzz_15-4ffd98.c)
[IeitigMULBFg_surfuzz_15-4ffd98.sh](https://github.com/user-attachments/files/29128246/IeitigMULBFg_surfuzz_15-4ffd98.sh)

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with the reduced C testcase and the clang -O0 command. Start in clang/lib/CodeGen/CGExprScalar.cpp at the reported EmitScalarExpr assertion, then trace the EmitBuiltinExpr and EmitCallExpr entries shown in the stack dump. Done means this testcase no longer triggers the frontend assertion and has regression coverage.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.