llvm / llvm/llvm-project

[clang][OpenMP] Assertion `D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context"' failed.

Open
#210,018 2 comments 0 reactions 0 assignees View on GitHub
clang:frontend clang:openmp crash generated by fuzzer
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Reproducer:
https://godbolt.org/z/EEM1aq7Wv
```cpp
void foo() {
template for (auto x : {1, 2}) {
#pragma omp simd
for (int i = 0; i < 2; i++)
;
}
}
```

Backtrace:
```console
clang++: /root/llvm-project/llvm/tools/clang/lib/AST/DeclBase.cpp:1801: void clang::DeclContext::addHiddenDecl(clang::Decl*): Assertion `D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++2c -fopenmp
1. :4:5: current parser token 'for'
2. :1:12: parsing function body 'foo'
3. :1:12: in compound statement ('{}')
4. :2:34: in compound statement ('{}')
#0 0x00000000043f3668 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43f3668)
#1 0x00000000043f0524 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43f0524)
#2 0x00000000043f0b4c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43f0b4c)
#3 0x000000000432fb88 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x0000716b23445330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x0000716b2349eb2c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9eb2c)
#6 0x0000716b2344527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x0000716b234288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x0000716b2342881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x0000716b2343b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x0000000007f41240 clang::DeclContext::addDecl(clang::Decl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7f41240)
#11 0x000000000767a04a clang::Sema::CreateCapturedStmtRecordDecl(clang::CapturedDecl*&, clang::SourceLocation, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x767a04a)
#12 0x0000000007683a39 clang::Sema::ActOnCapturedRegionStart(clang::SourceLocation, clang::Scope*, clang::CapturedRegionKind, llvm::ArrayRef>, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7683a39)
#13 0x00000000074f5a1d processCapturedRegions(clang::Sema&, llvm::omp::Directive, clang::Scope*, clang::SourceLocation) SemaOpenMP.cpp:0:0
#14 0x00000000074f635f clang::SemaOpenMP::ActOnOpenMPRegionStart(llvm::omp::Directive, clang::Scope*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x74f635f)
#15 0x0000000006ddb516 clang::Parser::ParseOpenMPExecutableDirective(clang::Parser::ParsedStmtContext, llvm::omp::Directive, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ddb516)
#16 0x0000000006de65b5 clang::Parser::ParseOpenMPDeclarativeOrExecutableDirective(clang::Parser::ParsedStmtContext, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6de65b5)
#17 0x0000000006e070f4 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e070f4)
#18 0x0000000006e08959 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e08959)
#19 0x0000000006e10dee clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e10dee)
#20 0x0000000006e11534 void llvm::function_ref::callback_fn(long) ParseStmt.cpp:0:0
#21 0x00000000087fec81 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x87fec81)
#22 0x0000000006e01285 clang::Parser::ParseCompoundStatement(bool, unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e01285)
#23 0x0000000006e07067 clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e07067)
#24 0x0000000006e08959 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e08959)
#25 0x0000000006e0996b clang::Parser::ParseStatement(clang::SourceLocation*, clang::Parser::ParsedStmtContext, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e0996b)
#26 0x0000000006e0a5f4 clang::Parser::ParseForStatement(clang::SourceLocation*, clang::LabelDecl*, clang::CXXExpansionStmtDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e0a5f4)
#27 0x0000000006e0b8f5 clang::Parser::ParseExpansionStatement(clang::SourceLocation*, clang::LabelDecl*, clang::SourceLocation) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e0b8f5)
#28 0x0000000006e07eca clang::Parser::ParseStatementOrDeclarationAfterAttributes(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e07eca)
#29 0x0000000006e08959 clang::Parser::ParseStatementOrDeclaration(llvm::SmallVector&, clang::Parser::ParsedStmtContext, clang::SourceLocation*, clang::LabelDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e08959)
#30 0x0000000006e10dee clang::Parser::ParseCompoundStatementBody(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e10dee)
#31 0x0000000006e1160a clang::Parser::ParseFunctionStatementBody(clang::Decl*, clang::Parser::ParseScope&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e1160a)
#32 0x0000000006d0f973 clang::Parser::ParseFunctionDefinition(clang::ParsingDeclarator&, clang::Parser::ParsedTemplateInfo const&, clang::LateParsedAttrList*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d0f973)
#33 0x0000000006d59266 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::Parser::ParsedTemplateInfo&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d59266)
#34 0x0000000006d03dcb clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d03dcb)
#35 0x0000000006d045bf clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d045bf)
#36 0x0000000006d10dd1 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d10dd1)
#37 0x0000000006d11ce5 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d11ce5)
#38 0x0000000006d120d0 clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6d120d0)
#39 0x0000000006ceebc3 clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6ceebc3)
#40 0x0000000004ef1628 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ef1628)
#41 0x000000000521ccc8 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x521ccc8)
#42 0x00000000051aa16d clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x51aa16d)
#43 0x00000000052f53fd clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x52f53fd)
#44 0x0000000000da6c9c cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda6c9c)
#45 0x0000000000d9d8aa ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#46 0x0000000000d9da2d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#47 0x0000000004f688f9 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#48 0x000000000432ffc4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x432ffc4)
#49 0x0000000004f69465 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#50 0x0000000004f21e32 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f21e32)
#51 0x0000000004f26667 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f26667)
#52 0x0000000004f30bc5 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4f30bc5)
#53 0x0000000000da318a clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xda318a)
#54 0x0000000000cbef4a main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcbef4a)
#55 0x0000716b2342a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#56 0x0000716b2342a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#57 0x0000000000d9d255 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd9d255)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)
Compiler returned: 134
```

Contributor guide

Open the contributing guide

Research direction

Start by compiling the Godbolt reproducer with -std=c++2c and -fopenmp. Trace the stack through SemaOpenMP::ActOnOpenMPRegionStart, processCapturedRegions, Sema::ActOnCapturedRegionStart, and DeclBase.cpp's DeclContext::addHiddenDecl. Done means this input no longer triggers the lexical-context assertion or crashes the clang frontend.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.