llvm / llvm/llvm-project

[BUG] ICE in clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*)

Open
#204,756 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::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*)

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
typedef *a;
__attribute__((interrupt)) void b(a) {}

```

## Crash Output

```
clang-22: /root/compilers/llvm-project-release/llvm/include/llvm/Support/Casting.h:560: decltype(auto) llvm::cast(const From&) [with To \= clang::PointerType; From \= clang::QualType]: Assertion \`isa(Val) && "cast() argument of incompatible type!"' 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 MCRMVIQIpNqE_surfuzz_19.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/MCRMVIQIpNqE_surfuzz_19-15a7da.o -x c /root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c
1\. /root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c:12:1: current parser token '__attribute__'
2\. /root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c:9:33: LLVM IR generation of declaration 'foo1'
3\. /root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c:9:33: Generating code for declaration 'foo1'
#0 0x000055eb918da2d2 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f62d2)
#1 0x000055eb918d6caf llvm::sys::RunSignalHandlers() (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f2caf)
#2 0x000055eb918d6dfc SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007fe5dd1c4520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007fe5dd218a7c __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76
#5 0x00007fe5dd218a7c __pthread_kill_internal ./nptl/./nptl/pthread_kill.c:78:10
#6 0x00007fe5dd218a7c pthread_kill ./nptl/./nptl/pthread_kill.c:89:10
#7 0x00007fe5dd1c4476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007fe5dd1aa7f3 abort ./stdlib/./stdlib/abort.c:81:7
#9 0x00007fe5dd1aa71b _nl_load_domain ./intl/./intl/loadmsgcat.c:1177:9
#10 0x00007fe5dd1bbe96 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x000055eb920a3456 addX86InterruptAttrs(clang::FunctionDecl const*, llvm::GlobalValue*, clang::CodeGen::CodeGenModule&) X86.cpp:0:0
#12 0x000055eb91eccf2a clang::CodeGen::CodeGenModule::EmitGlobalFunctionDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4ae8f2a)
#13 0x000055eb91ec7ef5 clang::CodeGen::CodeGenModule::EmitGlobalDefinition(clang::GlobalDecl, llvm::GlobalValue*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4ae3ef5)
#14 0x000055eb91ec8b83 clang::CodeGen::CodeGenModule::EmitGlobal(clang::GlobalDecl) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4ae4b83)
#15 0x000055eb91ed2807 clang::CodeGen::CodeGenModule::EmitTopLevelDecl(clang::Decl*) (.part.0) CodeGenModule.cpp:0:0
#16 0x000055eb9224ed71 (anonymous namespace)::CodeGeneratorImpl::HandleTopLevelDecl(clang::DeclGroupRef) ModuleBuilder.cpp:0:0
#17 0x000055eb9223fcc1 clang::BackendConsumer::HandleTopLevelDecl(clang::DeclGroupRef) (/root/objects/llvm-project-release.obj/bin/clang-22+0x4e5bcc1)
#18 0x000055eb940eb874 clang::ParseAST(clang::Sema&, bool, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d07874)
#19 0x000055eb92563987 clang::FrontendAction::Execute() (/root/objects/llvm-project-release.obj/bin/clang-22+0x517f987)
#20 0x000055eb924eb0e9 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x51070e9)
#21 0x000055eb9266095e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x527c95e)
#22 0x000055eb8e36b657 cc1_main(llvm::ArrayRef, char const*, void*) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf87657)
#23 0x000055eb8e360694 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#24 0x000055eb8e365907 clang_main(int, char**, llvm::ToolContext const&) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf81907)
#25 0x000055eb8e2230ea main (/root/objects/llvm-project-release.obj/bin/clang-22+0xe3f0ea)
#26 0x00007fe5dd1abd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#27 0x00007fe5dd1abe40 call_init ./csu/../csu/libc-start.c:128:20
#28 0x00007fe5dd1abe40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#29 0x000055eb8e35fcb5 _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/MCRMVIQIpNqE_surfuzz_19-b308b9.c
clang: note: diagnostic msg: /tmp/MCRMVIQIpNqE_surfuzz_19-b308b9.sh
clang: note: diagnostic msg:
********************
EXIT_CODE: 1
```

## Preprocessed Source & Run Script

**Preprocessed source** (`/tmp/MCRMVIQIpNqE_surfuzz_19-b308b9.c`):

```c
# 1 ""
# 1 "/root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c"
#ifdef __x86_64__
typedef __UINT64_TYPE__ uword;
#else
# 4 "/root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c"
typedef __UINT32_TYPE__ uword;
#endif
# 6 "/root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c"
typedef void (*fn)(void);

// OK -- same prototype as glibc's.
__attribute__((interrupt)) void foo1(fn a1) {}

// Wrong arg type (expected void *), which triggers a warning; still ok, though.
__attribute__((interrupt)) void foo2(fn *a) {}

// Wrong arg type (expected fn), which triggers a warning.
__attribute__((interrupt)) void foo3(int *a) {}

// Wrong return type (expected void), which triggers a warning.
#ifdef __x86_64__
__attribute__((interrupt)) int foo4(fn a) { return 0; }
#else
# 21 "/root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c"
__attribute__((interrupt)) int foo4(fn a) { return; }
#endif
# 23 "/root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c"

// Returning the wrong value (0), which triggers a warning.
#ifdef __x86_64__
__attribute__((interrupt)) int foo5(fn a) { return (int)a; }
#endif
# 28 "/root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.c"

// The first param needs to be named "a".
__attribute__((interrupt)) void foo6(void a(void)) {}

// The first param is different (fn * instead of fn).
__
__attribute__((interrupt)) void foo8(int *a) {}

```

**Run script** (`/tmp/MCRMVIQIpNqE_surfuzz_19-b308b9.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,1/MCRMVIQIpNqE_surfuzz_19.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" "MCRMVIQIpNqE_surfuzz_19.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/MCRMVIQIpNqE_surfuzz_19-15a7da.o" "-x" "c" "/root/outputs/experiments-on-metamut/surfuzz,gcc,1/MCRMVIQIpNqE_surfuzz_19.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" "MCRMVIQIpNqE_surfuzz_19.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" "MCRMVIQIpNqE_surfuzz_19-b308b9.c"

```

* found via a fuzzer

[MCRMVIQIpNqE_surfuzz_19-b308b9.c](https://github.com/user-attachments/files/29128161/MCRMVIQIpNqE_surfuzz_19-b308b9.c)
[MCRMVIQIpNqE_surfuzz_19-b308b9.sh](https://github.com/user-attachments/files/29128162/MCRMVIQIpNqE_surfuzz_19-b308b9.sh)

Contributor guide

Open the contributing guide

Research direction

Run the attached reduced C test case and run script with clang-22 to reproduce the frontend crash. Start at addX86InterruptAttrs in X86.cpp and the CodeGenModule::EmitGlobalDefinition stack entry, then inspect the interrupt-attribute handling. Done means the invalid declaration no longer causes an assertion, with a regression test covering the reproducer.

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.