Skip to content

Clang 21 crash (stack dump) when parsing #pragma clang attribute push inside function body #137861

@mariete1223

Description

@mariete1223

Summary:

Clang 21 crashes when parsing a #pragma clang attribute push directive inside a function body, wrapping a compound statement {}.
Instead of rejecting the invalid pragma usage or handling it gracefully, Clang crashes during parsing and statement attribute processing (ProcessStmtAttribute).

Stack trace

Stack dump:
0.      Program arguments: /usr/local/bin/clang-21 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name prueba.cpp -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 -fcoverage-compilation-dir=/root -resource-dir /usr/local/lib/clang/21 -internal-isystem /usr/local/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=c2x -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/prueba-0fe160.o -x c prueba.cpp
1.      prueba.cpp:11:1: current parser token '{'
2.      prueba.cpp:5:11: parsing function body 'main'
3.      prueba.cpp:5:11: in compound statement ('{}')
 #0 0x000055c08fa8118f llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/usr/local/bin/clang-21+0x3c4a18f)
 #1 0x000055c08fa7ebd4 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x00007f2863c40420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #3 0x000055c0931f2523 clang::Stmt::getBeginLoc() const (/usr/local/bin/clang-21+0x73bb523)
 #4 0x000055c092644dcb ProcessStmtAttribute(clang::Sema&, clang::Stmt*, clang::ParsedAttr const&, clang::SourceRange) SemaStmtAttr.cpp:0:0
 #5 0x000055c092648382 clang::Sema::ProcessStmtAttributes(clang::Stmt*, clang::ParsedAttributes const&, llvm::SmallVectorImpl<clang::Attr const*>&) (/usr/local/bin/clang-21+0x6811382)
 #6 0x000055c09260db87 clang::Sema::ActOnAttributedStmt(clang::ParsedAttributes const&, clang::Stmt*) (/usr/local/bin/clang-21+0x67d6b87)
 #7 0x000055c091daa315 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector<clang::Stmt*, 32u>&, clang::Parser::ParsedStmtContext, clang::SourceLocation*) (/usr/local/bin/clang-21+0x5f73315)
 #8 0x000055c091dab46c clang::Parser::ParseCompoundStatementBody(bool) (/usr/local/bin/clang-21+0x5f7446c)
 #9 0x000055c091dad7e2 clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/usr/local/bin/clang-21+0x5f767e2)
#10 0x000055c091ca4e83 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::LateParsedAttrList*) (/usr/local/bin/clang-21+0x5e6de83)
#11 0x000055c091ceef05 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/usr/local/bin/clang-21+0x5eb7f05)
#12 0x000055c091c9d62d clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/usr/local/bin/clang-21+0x5e6662d)
#13 0x000055c091c9e16e clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/usr/local/bin/clang-21+0x5e6716e)
#14 0x000055c091ca7d73 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/usr/local/bin/clang-21+0x5e70d73)
#15 0x000055c091ca96a8 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/usr/local/bin/clang-21+0x5e726a8)
#16 0x000055c091c9762a clang::ParseAST(clang::Sema&, bool, bool) (/usr/local/bin/clang-21+0x5e6062a)
#17 0x000055c090405e45 clang::CodeGenAction::ExecuteAction() (/usr/local/bin/clang-21+0x45cee45)
#18 0x000055c0906ea9b8 clang::FrontendAction::Execute() (/usr/local/bin/clang-21+0x48b39b8)
#19 0x000055c090668d3b clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/local/bin/clang-21+0x4831d3b)
#20 0x000055c0907dcc7b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/local/bin/clang-21+0x49a5c7b)
#21 0x000055c08ccd9f51 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/local/bin/clang-21+0xea2f51)
#22 0x000055c08ccd25fd ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#23 0x000055c08ccd599f clang_main(int, char**, llvm::ToolContext const&) (/usr/local/bin/clang-21+0xe9e99f)
#24 0x000055c08cba830b main (/usr/local/bin/clang-21+0xd7130b)
#25 0x00007f28636ee083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#26 0x000055c08ccd207e _start (/usr/local/bin/clang-21+0xe9b07e)

Program

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
__attribute__ ((deprecated))
int my_val = 5;
const char* my_string = "Hello, World!\n";
__attribute__((aligned(64)))
#pragma clang attribute push(__attribute__((cleanup(cleanup1))))
{
char* p = malloc(100);
free(p);
 }
#pragma clang attribute pop
}

To quickly reproduce

https://gcc.godbolt.org/z/3xc5qaM4T

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partycrashPrefer [crash-on-valid] or [crash-on-invalid]

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions