llvm / llvm/llvm-project

[Clang] Crash when an instantiated local extern declaration's previous declaration is in a discarded branch of an `if constexpr`

Open
#223,003 3 comments 0 reactions 0 assignees View on GitHub
clang:frontend confirmed crash-on-valid
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Consider (https://godbolt.org/z/xPG5c1398):
```c++
int foo8_decl;

template
void foo8() {
if constexpr (x)
extern int foo8_decl; // A
else
extern int foo8_decl; // B
}

template void foo8();
```

During template instantiation, we attempt to instantiate B and `findInstantiationOf` looks for an existing instantiation of the ‘previous declaration’ of the variable. However, the previous declaration is A, which wasn’t instantiated in `foo8` because it’s in a discarded branch of an `if constexpr`, which causes us to assert.

Assertion:
```console
clang++: /root/llvm-project/llvm/tools/clang/lib/Sema/SemaTemplateInstantiate.cpp:4811:
llvm::PointerUnion*>* clang::LocalInstantiationScope::findInstantiationOf(const clang::Decl*):
Assertion `isa(D) && "declaration not instantiated in this scope"' failed.
```

Stack Trace:
```console
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
1. :11:28: current parser token ';'
2. :4:6: instantiating function definition 'foo8'
#0 0x00000000044b6298 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44b6298)
#1 0x00000000044b34f4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44b34f4)
#2 0x00000000044b3b2c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44b3b2c)
#3 0x00000000043f00f8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x0000713183045330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x000071318309ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
#6 0x000071318304527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x00007131830288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x000071318302881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x000071318303b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x00000000079ad0d5 clang::LocalInstantiationScope::findInstantiationOf(clang::Decl const*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x79ad0d5)
#11 0x0000000007aa30b6 clang::Sema::FindInstantiatedDecl(clang::SourceLocation, clang::NamedDecl*, clang::MultiLevelTemplateArgumentList const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7aa30b6)
#12 0x0000000007aa7002 clang::Sema::BuildVariableInstantiation(clang::VarDecl*, clang::VarDecl*, clang::MultiLevelTemplateArgumentList const&, llvm::SmallVector*, clang::DeclContext*, clang::LocalInstantiationScope*, bool, clang::VarTemplateSpecializationDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7aa7002)
#13 0x0000000007ab299d clang::TemplateDeclInstantiator::VisitVarDecl(clang::VarDecl*, bool, llvm::ArrayRef*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7ab299d)
#14 0x0000000007ab84d4 void llvm::function_ref::callback_fn(long) SemaTemplateInstantiateDecl.cpp:0:0
#15 0x0000000008950e31 clang::StackExhaustionHandler::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8950e31)
#16 0x0000000007a4e08a clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7a4e08a)
#17 0x00000000079b3135 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformDeclStmt(clang::DeclStmt*) SemaTemplateInstantiate.cpp:0:0
#18 0x00000000079fd630 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformIfStmt(clang::IfStmt*) SemaTemplateInstantiate.cpp:0:0
#19 0x00000000079fb175 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCompoundStmt(clang::CompoundStmt*, bool) SemaTemplateInstantiate.cpp:0:0
#20 0x00000000079fbcbe clang::Sema::SubstStmt(clang::Stmt*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x79fbcbe)
#21 0x0000000007aaab9c clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x7aaab9c)
#22 0x000000000789ccad clang::Sema::ActOnExplicitInstantiation(clang::Scope*, clang::SourceLocation, clang::SourceLocation, clang::Declarator&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x789ccad)
#23 0x0000000006e52cb3 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e52cb3)
#24 0x0000000006e72fb9 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+++0x6e72fb9)
#25 0x0000000006f393e0 clang::Parser::ParseDeclarationAfterTemplate(clang::DeclaratorContext, clang::Parser::ParsedTemplateInfo&, clang::ParsingDeclRAIIObject&, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f393e0)
#26 0x0000000006f39f1c clang::Parser::ParseExplicitInstantiation(clang::DeclaratorContext, clang::SourceLocation, clang::SourceLocation, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f39f1c)
#27 0x0000000006f470d8 clang::Parser::ParseDeclarationStartingWithTemplate(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6f470d8)
#28 0x0000000006e74a60 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e74a60)
#29 0x0000000006e2b350 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e2b350)
#30 0x0000000006e2c265 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e2c265)
#31 0x0000000006e0875a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6e0875a)
#32 0x0000000004fca298 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4fca298)
#33 0x00000000052eae58 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x52eae58)
#34 0x0000000005278acd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5278acd)
#35 0x00000000053bf67d clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x53bf67d)
#36 0x0000000000d8f1fc cc1_main(llvm::ArrayRef, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd8f1fc)
#37 0x0000000000d85f1a ExecuteCC1Tool(llvm::SmallVectorImpl&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr) driver.cpp:0:0
#38 0x0000000000d8609d int llvm::function_ref&)>::callback_fn&)>(long, llvm::SmallVectorImpl&) driver.cpp:0:0
#39 0x000000000503e4c9 void llvm::function_ref::callback_fn>, std::__cxx11::basic_string, std::allocator>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#40 0x00000000043f04f4 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43f04f4)
#41 0x000000000503f035 clang::driver::CC1Command::Execute(llvm::ArrayRef>, std::__cxx11::basic_string, std::allocator>*, bool*) const (.part.0) Job.cpp:0:0
#42 0x0000000004ffd462 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ffd462)
#43 0x0000000005001dc7 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5001dc7)
#44 0x000000000500c325 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x500c325)
#45 0x0000000000d8b70a clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xd8b70a)
#46 0x0000000000cc90aa main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xcc90aa)
```

Contributor guide

Open the contributing guide

Research direction

Start in llvm/tools/clang/lib/Sema/SemaTemplateInstantiate.cpp at LocalInstantiationScope::findInstantiationOf and follow the call chain through FindInstantiatedDecl, BuildVariableInstantiation, and TransformIfStmt. Reproduce the provided foo8 example with an assertion-enabled Clang build, then add coverage for explicit instantiation through a discarded if constexpr branch and confirm it no longer crashes.

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
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.