llvm / llvm/llvm-project

[BUG] ICE in clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*)

Open
#204,762 1 comment 0 reactions 0 assignees View on GitHub
clang:frontend 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::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*)

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
extern int a __attribute__((alias(""))) = 42

```

## Crash Output

```
/root/outputs/experiments-on-metamut/surfuzz,clang,3/DBbCyOwnsznl_surfuzz_14.c:8:12: error: redefinition of 'g1'
8 | extern int g1 __attribute__((alias("bar1"))) \= 42;
| ^
/root/outputs/experiments-on-metamut/surfuzz,clang,3/DBbCyOwnsznl_surfuzz_14.c:7:12: note: previous definition is here
7 | extern int g1 __attribute((alias("g0")));
| ^
clang-22: /root/compilers/llvm-project-release/clang/lib/Sema/SemaDecl.cpp:7151: void checkAliasAttr(clang::Sema&, clang::NamedDecl&): Assertion \`VD->isThisDeclarationADefinition() && !VD->isExternallyVisible() && "Broken AliasAttr handled late!"' 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 DBbCyOwnsznl_surfuzz_14.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/DBbCyOwnsznl_surfuzz_14-abaef8.o -x c /root/outputs/experiments-on-metamut/surfuzz,clang,3/DBbCyOwnsznl_surfuzz_14.c
1\. /root/outputs/experiments-on-metamut/surfuzz,clang,3/DBbCyOwnsznl_surfuzz_14.c:8:50: current parser token ';'
#0 0x00005642915522d2 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f62d2)
#1 0x000056429154ecaf llvm::sys::RunSignalHandlers() (/root/objects/llvm-project-release.obj/bin/clang-22+0x44f2caf)
#2 0x000056429154edfc SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007fa5857fa520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007fa58584ea7c __pthread_kill_implementation ./nptl/./nptl/pthread_kill.c:44:76
#5 0x00007fa58584ea7c __pthread_kill_internal ./nptl/./nptl/pthread_kill.c:78:10
#6 0x00007fa58584ea7c pthread_kill ./nptl/./nptl/pthread_kill.c:89:10
#7 0x00007fa5857fa476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007fa5857e07f3 abort ./stdlib/./stdlib/abort.c:81:7
#9 0x00007fa5857e071b _nl_load_domain ./intl/./intl/loadmsgcat.c:1177:9
#10 0x00007fa5857f1e96 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x000056429411f887 (/root/objects/llvm-project-release.obj/bin/clang-22+0x70c3887)
#12 0x000056429411f9a9 clang::Sema::FinalizeDeclaration(clang::Decl*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x70c39a9)
#13 0x0000564293da9369 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d4d369)
#14 0x0000564293dc0ce3 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+0x6d64ce3)
#15 0x0000564293d797e1 clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d1d7e1)
#16 0x0000564293d7a00f clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d1e00f)
#17 0x0000564293d82896 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d26896)
#18 0x0000564293d83915 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d27915)
#19 0x0000564293d6388a clang::ParseAST(clang::Sema&, bool, bool) (/root/objects/llvm-project-release.obj/bin/clang-22+0x6d0788a)
#20 0x00005642921db987 clang::FrontendAction::Execute() (/root/objects/llvm-project-release.obj/bin/clang-22+0x517f987)
#21 0x00005642921630e9 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/root/objects/llvm-project-release.obj/bin/clang-22+0x51070e9)
#22 0x00005642922d895e clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/root/objects/llvm-project-release.obj/bin/clang-22+0x527c95e)
#23 0x000056428dfe3657 cc1_main(llvm::ArrayRef, char const*, void*) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf87657)
#24 0x000056428dfd8694 ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#25 0x000056428dfdd907 clang_main(int, char**, llvm::ToolContext const&) (/root/objects/llvm-project-release.obj/bin/clang-22+0xf81907)
#26 0x000056428de9b0ea main (/root/objects/llvm-project-release.obj/bin/clang-22+0xe3f0ea)
#27 0x00007fa5857e1d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#28 0x00007fa5857e1e40 call_init ./csu/../csu/libc-start.c:128:20
#29 0x00007fa5857e1e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#30 0x000056428dfd7cb5 _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/DBbCyOwnsznl_surfuzz_14-c5285a.c
clang: note: diagnostic msg: /tmp/DBbCyOwnsznl_surfuzz_14-c5285a.sh
clang: note: diagnostic msg:
********************
EXIT_CODE: 1
```

## Preprocessed Source & Run Script

**Preprocessed source** (`/tmp/DBbCyOwnsznl_surfuzz_14-c5285a.c`):

```c
# 1 ""
# 1 "/root/outputs/experiments-on-metamut/surfuzz,clang,3/DBbCyOwnsznl_surfuzz_14.c"
int g0;
const int f1 = 42;
static int bar1 = 42;
const int wacom_usb_ids[] = {1, 1, 2, 3, 5, 8, 13, 0};
extern const int __mod_usb_device_table __attribute__ ((alias("wacom_usb_ids")));
extern int g1;
extern int g1 __attribute((alias("g0")));
extern int g1 __attribute__((alias("bar1"))) = 42;
#define baz2 bar1
static const int __attribute__((alias("bar1"))) baz2 __attribute__((weak)) = 42;
extern int baz2 __attribute__((weak));
static int g1, g1a = 0 __attribute__((alias("g1")));
extern int g2 __attribute__((alias("baz2"))) = 0;
extern const int f4;
int f4 __attribute__((weakref));
void f0(void) { }
extern void f1(void);
extern void f1(void) __attribute((alias("f0")));
static inline int foo1() { return 0; }
int foo() __attribute__((alias("foo1")));
int bar() __attribute__((alias("bar1")));
extern int test6();
void test7() { test6(); }
int test6() __attribute__((alias("test7")));
static int inner(int a) { return 0; }
static int inner_weak(int a) { return 0; }
extern __typeof(inner) inner_a __attribute__((alias("inner")));
static __typeof(inner_weak) inner_weak_a __attribute__((weakref, alias("inner_weak")));
int outer(int a) { return inner(a); }
int outer_weak(int a) { return inner_weak_a(a); }
void test8_bar() {}
void test8_foo() __attribute__((weak, alias("test8_bar")));
void test8_zed() __attribute__((alias("test8_foo")));
void test9_bar(void) { }
void test9_zed(void) __attribute__((section("test")));
void test9_zed(void) __attribute__((alias("test9_bar")));
int test10;
static int test10_foo __attribute__((alias("test10")));
void test11(void) {}
static void test11_foo(void) __attribute__((alias("test11")));
void test12(void) {}
inline void test12_alias(void) __attribute__((gnu_inline, alias("test12")));
enum a_type { test13_a };
void test13(enum a_type y) {}
void test13_alias(enum undeclared_type y) __attribute__((alias ("test13")));

```

**Run script** (`/tmp/DBbCyOwnsznl_surfuzz_14-c5285a.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,clang,3/DBbCyOwnsznl_surfuzz_14.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" "DBbCyOwnsznl_surfuzz_14.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/DBbCyOwnsznl_surfuzz_14-abaef8.o" "-x" "c" "/root/outputs/experiments-on-metamut/surfuzz,clang,3/DBbCyOwnsznl_surfuzz_14.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" "DBbCyOwnsznl_surfuzz_14.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" "DBbCyOwnsznl_surfuzz_14-c5285a.c"

```

* found via a fuzzer

[DBbCyOwnsznl_surfuzz_14-c5285a.c](https://github.com/user-attachments/files/29128320/DBbCyOwnsznl_surfuzz_14-c5285a.c)
[DBbCyOwnsznl_surfuzz_14-c5285a.sh](https://github.com/user-attachments/files/29128319/DBbCyOwnsznl_surfuzz_14-c5285a.sh)

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with the reduced C case and the provided clang command. Start in clang/lib/Sema/SemaDecl.cpp at checkAliasAttr and follow the call from Parser::ParseDeclarationAfterDeclaratorAndAttributes. Done means the reduced input no longer triggers the assertion, with regression coverage for the reported alias declaration case.

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.