Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang] Ill-formed default argument treated as building a deduction guide causes crash #65522

Closed
shafik opened this issue Sep 6, 2023 · 9 comments · Fixed by #67373
Closed
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party crash-on-invalid

Comments

@shafik
Copy link
Collaborator

shafik commented Sep 6, 2023

Given the following C++:

template<typename A3>
class B3 : A3 {
  template<bool = C3<B3>()> 
  B3();
}; B3(); 

This causes a crash on clang trunk: https://godbolt.org/z/dK43xx1KT

This was introduced by: https://reviews.llvm.org/D139837 and is currently blocking: https://reviews.llvm.org/D148474

Unreachable hit:

UNREACHABLE executed at /root/llvm-project/clang/lib/Sema/SemaTemplateInstantiate.cpp:1068!

Backtrace:

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: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -gdwarf-4 -g -o /app/output.s -mllvm --x86-asm-syntax=intel -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics -std=c++17 <source>
1.	<source>:5:8: current parser token ';'
 #0 0x00000000036bc8f8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x36bc8f8)
 #1 0x00000000036ba5bc llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x36ba5bc)
 #2 0x00000000036039d8 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007f2295820420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007f22952e300b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
 #5 0x00007f22952c2859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
 #6 0x000000000360ec8a (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x360ec8a)
 #7 0x0000000006920118 clang::Sema::PrintInstantiationStack() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6920118)
 #8 0x0000000005f78090 clang::Sema::EmitCurrentDiagnostic(unsigned int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f78090)
 #9 0x0000000005f783d1 clang::Sema::ImmediateDiagBuilder::~ImmediateDiagBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f783d1)
#10 0x0000000005f78c20 clang::Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f78c20)
#11 0x00000000063c15f6 clang::Sema::ActOnCallExpr(clang::Scope*, clang::Expr*, clang::SourceLocation, llvm::MutableArrayRef<clang::Expr*>, clang::SourceLocation, clang::Expr*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x63c15f6)
#12 0x0000000006932db0 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformCallExpr(clang::CallExpr*) SemaTemplateInstantiate.cpp:0:0
#13 0x0000000006929238 clang::TreeTransform<(anonymous namespace)::TemplateInstantiator>::TransformExpr(clang::Expr*) SemaTemplateInstantiate.cpp:0:0
#14 0x000000000695b288 clang::Sema::SubstExpr(clang::Expr*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x695b288)
#15 0x00000000069703a4 clang::TemplateDeclInstantiator::VisitNonTypeTemplateParmDecl(clang::NonTypeTemplateParmDecl*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x69703a4)
#16 0x00000000069bdb34 void llvm::function_ref<void ()>::callback_fn<clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&)::'lambda'()>(long) SemaTemplateInstantiateDecl.cpp:0:0
#17 0x0000000005f67b51 clang::Sema::runWithSufficientStackSpace(clang::SourceLocation, llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f67b51)
#18 0x000000000696ddaa clang::Sema::SubstDecl(clang::Decl*, clang::DeclContext*, clang::MultiLevelTemplateArgumentList const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x696ddaa)
#19 0x0000000006869add clang::Sema::DeclareImplicitDeductionGuides(clang::TemplateDecl*, clang::SourceLocation) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6869add)
#20 0x00000000065b2a37 LookupDirect(clang::Sema&, clang::LookupResult&, clang::DeclContext const*) SemaLookup.cpp:0:0
#21 0x00000000065b36fe CppNamespaceLookup(clang::Sema&, clang::LookupResult&, clang::ASTContext&, clang::DeclContext const*, (anonymous namespace)::UnqualUsingDirectiveSet&) (.isra.0) SemaLookup.cpp:0:0
#22 0x00000000065a1429 clang::Sema::CppLookupName(clang::LookupResult&, clang::Scope*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x65a1429)
#23 0x00000000065a2195 clang::Sema::LookupName(clang::LookupResult&, clang::Scope*, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x65a2195)
#24 0x000000000618e430 clang::Sema::HandleDeclarator(clang::Scope*, clang::Declarator&, llvm::MutableArrayRef<clang::TemplateParameterList*>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x618e430)
#25 0x000000000618f2f1 clang::Sema::ActOnDeclarator(clang::Scope*, clang::Declarator&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x618f2f1)
#26 0x0000000005e3e9a0 clang::Parser::ParseDeclarationAfterDeclaratorAndAttributes(clang::Declarator&, clang::Parser::ParsedTemplateInfo const&, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e3e9a0)
#27 0x0000000005e4c7e0 clang::Parser::ParseDeclGroup(clang::ParsingDeclSpec&, clang::DeclaratorContext, clang::ParsedAttributes&, clang::SourceLocation*, clang::Parser::ForRangeInit*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e4c7e0)
#28 0x0000000005e197bb clang::Parser::ParseDeclOrFunctionDefInternal(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e197bb)
#29 0x0000000005e19eef clang::Parser::ParseDeclarationOrFunctionDefinition(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*, clang::AccessSpecifier) (.part.0) Parser.cpp:0:0
#30 0x0000000005e20604 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e20604)
#31 0x0000000005e20e0d clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e20e0d)
#32 0x0000000005e1516a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e1516a)
#33 0x000000000491dcf8 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x491dcf8)
#34 0x000000000417be69 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x417be69)
#35 0x00000000040fe7be clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x40fe7be)
#36 0x000000000425c386 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x425c386)
#37 0x0000000000bd4b6e cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xbd4b6e)
#38 0x0000000000bcd22a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#39 0x0000000003f5f5f9 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#40 0x0000000003603e84 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3603e84)
#41 0x0000000003f5fbef clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#42 0x0000000003f27f75 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3f27f75)
#43 0x0000000003f289dd clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3f289dd)
#44 0x0000000003f30905 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3f30905)
#45 0x0000000000bd2e0c clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xbd2e0c)
#46 0x0000000000aced61 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xaced61)
#47 0x00007f22952c4083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#48 0x0000000000bccd0e _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0xbccd0e)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
@shafik shafik added clang:frontend Language frontend issues, e.g. anything involving "Sema" crash-on-invalid confirmed Verified by a second party labels Sep 6, 2023
@github-actions github-actions bot added clang Clang issues not falling into any other category new issue labels Sep 6, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 6, 2023

@llvm/issue-subscribers-clang-frontend

@shafik shafik removed the new issue label Sep 6, 2023
@EugeneZelenko EugeneZelenko removed the clang Clang issues not falling into any other category label Sep 6, 2023
@shafik
Copy link
Collaborator Author

shafik commented Sep 7, 2023

CC @yuanfang-chen

@yuanfang-chen
Copy link
Collaborator

Hi @shafik, sorry for the late response. I think you could just make it diagnose like a regular instantiation context (CodeSynthesisContext::TemplateInstantiation,

unsigned DiagID;
if (Function->getPrimaryTemplate())
DiagID = diag::note_function_template_spec_here;
else
DiagID = diag::note_template_member_function_here;
Diags.Report(Active->PointOfInstantiation, DiagID)
<< Function
<< Active->InstantiationRange;
) with the ctor as the instantiation range.

@shafik
Copy link
Collaborator Author

shafik commented Sep 9, 2023

So I tried a quick experiment and moved the BuildingDeductionGuides case before TemplateInstantiation:

    case CodeSynthesisContext::BuildingDeductionGuides:  
    case CodeSynthesisContext::TemplateInstantiation: {

and I am getting a ClassTemplateDecl so I hit this code:

 } else if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(D)) {
        Diags.Report(Active->PointOfInstantiation,
                     diag::note_template_class_instantiation_here)
            << CTD << Active->InstantiationRange;

and that looks consistent with your change which takes TemplateDecl *:

 InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
                          TemplateDecl *Entity, BuildingDeductionGuidesTag,
                          SourceRange InstantiationRange = SourceRange());

I am not super familiar with this area, so is the ClassTemplateDecl case the only case to cover, which it seems like at first glance?

If not, can you give a more detailed explanation.

@yuanfang-chen
Copy link
Collaborator

I'm trying to fix this locally. However arc patch --nobranch D148474 seems to not work. Is this due to the Github PR migration?

@shafik
Copy link
Collaborator Author

shafik commented Sep 11, 2023

All new pull request need to be done on github, you can find documentation here: https://llvm.org/docs/GitHub.html

Also on discord there is the github-pull-requests channel

@yuanfang-chen
Copy link
Collaborator

Is D148474 on GitHub PR yet? I'm trying to base my fix on D148474. So you could include the fix in D148474.

@shafik
Copy link
Collaborator Author

shafik commented Sep 12, 2023

Is D148474 on GitHub PR yet? I'm trying to base my fix on D148474. So you could include the fix in D148474.

No, I have not moved it over. I was hoping to try and get it done w/o moving it over.

The code I provided in this bug report is one of my tests from that PR but it crashes on trunk now: https://godbolt.org/z/cP8dcrWP6 and so you can implement a fix w/o my PR.

@yuanfang-chen
Copy link
Collaborator

Thanks. I'll get back soon.

shafik added a commit to shafik/llvm-project that referenced this issue Sep 26, 2023
In some cases where ill-formed code could be interpreted as a deduction guide
we can crash because we reach an unreachable path. This fixes this issue by
introducing a diagnostic instead.

Fixes: llvm#65522
shafik added a commit that referenced this issue Oct 2, 2023
#67373)

In some cases where ill-formed code could be interpreted as a deduction
guide we can crash because we reach an unreachable path. This fixes this
issue by introducing a diagnostic instead.

Fixes: #65522
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party crash-on-invalid
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants