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 segfaults with Non-Type Template Parameters and -Weverything #57369

Closed
SuperLuigiLinked opened this issue Aug 25, 2022 · 3 comments
Closed
Assignees
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@SuperLuigiLinked
Copy link

SuperLuigiLinked commented Aug 25, 2022

The following code causes a Segmentation Fault when -Weverything is specified, starting from around Clang version 12.0.0 and up:

template <int num>
class TemplateClass {};

template <TemplateClass nttp>
void func() {}

I tested this on various versions of Clang using Compiler Explorer: https://godbolt.org/z/8G11GE8eq
(As an aside, MSVC and GCC are able to compile the example just fine, and Clang compiles it successfully without -Weverything)

Here is the output from x86-64 clang (trunk) for reference:

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-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++2b -Weverything <source>
1.	<source>:4:29: current parser token '>'
 #0 0x00005594cd929d34 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x00005594cd927b9c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3945b9c)
 #2 0x00005594cd865828 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #3 0x00007fd53134e420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00005594d0aabf1d ConvertTypeToDiagnosticString(clang::ASTContext&, clang::QualType, llvm::ArrayRef<std::pair<clang::DiagnosticsEngine::ArgumentKind, long>>, llvm::ArrayRef<long>) ASTDiagnostic.cpp:0:0
 #5 0x00005594d0aacf0c clang::FormatASTNodeDiagnosticArgument(clang::DiagnosticsEngine::ArgumentKind, long, llvm::StringRef, llvm::StringRef, llvm::ArrayRef<std::pair<clang::DiagnosticsEngine::ArgumentKind, long>>, llvm::SmallVectorImpl<char>&, void*, llvm::ArrayRef<long>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x6acaf0c)
 #6 0x00005594cdc76bd7 clang::Diagnostic::FormatDiagnostic(char const*, char const*, llvm::SmallVectorImpl<char>&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c94bd7)
 #7 0x00005594ce4eff84 clang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x450df84)
 #8 0x00005594cdc7cc71 clang::DiagnosticIDs::ProcessDiag(clang::DiagnosticsEngine&) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c9ac71)
 #9 0x00005594cdc7131b clang::DiagnosticsEngine::EmitCurrentDiagnostic(bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3c8f31b)
#10 0x00005594cfec707b clang::Sema::EmitCurrentDiagnostic(unsigned int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5ee507b)
#11 0x00005594cfec79ff clang::Sema::ImmediateDiagBuilder::~ImmediateDiagBuilder() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5ee59ff)
#12 0x00005594cfec822b clang::Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5ee622b)
#13 0x00005594d0683150 clang::Sema::ActOnNonTypeTemplateParameter(clang::Scope*, clang::Declarator&, unsigned int, unsigned int, clang::SourceLocation, clang::Expr*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x66a1150)
#14 0x00005594cfe6cc6d clang::Parser::ParseNonTypeTemplateParameter(unsigned int, unsigned int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5e8ac6d)
#15 0x00005594cfe75a19 clang::Parser::ParseTemplateParameter(unsigned int, unsigned int) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5e93a19)
#16 0x00005594cfe76769 clang::Parser::ParseTemplateParameterList(unsigned int, llvm::SmallVectorImpl<clang::NamedDecl*>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5e94769)
#17 0x00005594cfe76c06 clang::Parser::ParseTemplateParameters(clang::Parser::MultiParseScope&, unsigned int, llvm::SmallVectorImpl<clang::NamedDecl*>&, clang::SourceLocation&, clang::SourceLocation&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5e94c06)
#18 0x00005594cfe796a6 clang::Parser::ParseTemplateDeclarationOrSpecialization(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5e976a6)
#19 0x00005594cfe79ad3 clang::Parser::ParseDeclarationStartingWithTemplate(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::AccessSpecifier) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5e97ad3)
#20 0x00005594cfdaa5b5 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5dc85b5)
#21 0x00005594cfd7bb36 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5d99b36)
#22 0x00005594cfd7d8fa clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5d9b8fa)
#23 0x00005594cfd6d27a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x5d8b27a)
#24 0x00005594ceb89f95 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x4ba7f95)
#25 0x00005594ce494441 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-trunk/bin/clang+++0x44b2441)
#26 0x00005594ce41b0e3 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x44390e3)
#27 0x00005594ce57296b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x459096b)
#28 0x00005594cb244ca4 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x1262ca4)
#29 0x00005594cb23e29b ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#30 0x00005594ce285659 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#31 0x00005594cd865f97 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x3883f97)
#32 0x00005594ce285c4c clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (.part.0) Job.cpp:0:0
#33 0x00005594ce24f769 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x426d769)
#34 0x00005594ce25015d clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const (/opt/compiler-explorer/clang-trunk/bin/clang+++0x426e15d)
#35 0x00005594ce25a86c clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x427886c)
#36 0x00005594cb2429c9 clang_main(int, char**) (/opt/compiler-explorer/clang-trunk/bin/clang+++0x12609c9)
#37 0x00007fd530dfc083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#38 0x00005594cb23deae _start (/opt/compiler-explorer/clang-trunk/bin/clang+++0x125beae)
clang-16: error: clang frontend command failed with exit code 139 (use -v to see invocation)
Compiler returned: 139
@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels Aug 25, 2022
@shafik
Copy link
Collaborator

shafik commented Aug 26, 2022

In Sema::ActOnNonTypeTemplateParameter(...) is assuming that TInfo->getType()->getContainedAutoType() is always valid in this case we get back nullptr.

I think a possible fix is:

(TInfo->getType()->getContainedAutoType() ?
                   QualType( TInfo->getType()->getContainedAutoType(), 0) : TInfo->getType())

but it is not obvious if the output is what is intended.

@int6
Copy link
Contributor

int6 commented Aug 28, 2022

if (TInfo->getType()->isUndeducedType()) {
Diag(D.getIdentifierLoc(),
diag::warn_cxx14_compat_template_nontype_parm_auto_type)
<< QualType(TInfo->getType()->getContainedAutoType(), 0);
}

@shafik shafik self-assigned this Aug 31, 2022
@shafik
Copy link
Collaborator

shafik commented Aug 31, 2022

proposed fix: https://reviews.llvm.org/D132990

@shafik shafik closed this as completed in f8a37a6 Sep 18, 2022
veselypeta pushed a commit to veselypeta/cherillvm that referenced this issue May 10, 2024
…has a placeholder type using getContainedAutoType()

Based on the changes introduced by 15361a2 it
looks like C++17 compatibility diagnostic should have been checking
getContainedAutoType().

This fixes: llvm/llvm-project#57369
  llvm/llvm-project#57643
  llvm/llvm-project#57793

Differential Revision: https://reviews.llvm.org/D132990
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
Development

No branches or pull requests

4 participants