llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Crash formatting `note_constexpr_access_uninit` for `*p` into a returned call frame: `AK_Dereference` overflows `access_kind_subobject` (regression in 23)

Open ojrosten opened this issue 1 month ago • 3 comments

Consider the following ill-formed code:

constexpr const int* f() { int i = 0; return &i; }
static_assert(*f() == 0);

Since clang 23.1.0, the compiler crashes.

This was found using Homebrew 23.1.4 but reproduces on compiler explorer:

https://godbolt.org/z/aajn8jMsf

repro-0d8479.cpp repro-0d8479.sh

Crashing information:

clang++: /root/llvm-project/llvm/tools/clang/lib/Basic/Diagnostic.cpp:854: void HandleSelectModifier(const clang::Diagnostic&, unsigned int, const char*, unsigned int, llvm::SmallVectorImpl<char>&): Assertion `NextVal != ArgumentEnd && "Value for integer select modifier was" " larger than the number of options in the diagnostic string!"' 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++23 -fsyntax-only <source>
1.	<eof> parser at end of file
 #0 0x0000000004ed1c58 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ed1c58)
 #1 0x0000000004eceeb4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4eceeb4)
 #2 0x0000000004ecf4ec llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ecf4ec)
 #3 0x0000000004e08748 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #4 0x00007fad70a45330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #5 0x00007fad70a9ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
 #6 0x00007fad70a4527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
 #7 0x00007fad70a288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
 #8 0x00007fad70a2881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
 #9 0x00007fad70a3b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x000000000527e2e9 clang::Diagnostic::FormatDiagnostic(char const*, char const*, llvm::SmallVectorImpl<char>&) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527e2e9)
#11 0x0000000005d533f6 clang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5d533f6)
#12 0x000000000527a7c0 clang::DiagnosticsEngine::Report(clang::DiagnosticsEngine::Level, clang::Diagnostic const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527a7c0)
#13 0x000000000527aed5 clang::DiagnosticsEngine::ProcessDiag(clang::DiagnosticBuilder const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527aed5)
#14 0x000000000527afb5 clang::DiagnosticsEngine::EmitDiagnostic(clang::DiagnosticBuilder const&, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527afb5)
#15 0x00000000090b1de6 clang::Sema::EmitDiagnostic(unsigned int, clang::DiagnosticBuilder const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x90b1de6)
#16 0x0000000009138972 clang::SemaBase::ImmediateDiagBuilder::~ImmediateDiagBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x9138972)
#17 0x0000000009093ac8 clang::SemaBase::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x9093ac8)
#18 0x00000000094b708d clang::Sema::VerifyIntegerConstantExpression(clang::Expr*, llvm::APSInt*, clang::Sema::VerifyICEDiagnoser&, clang::AllowFoldKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x94b708d)
#19 0x00000000094b7322 clang::Sema::VerifyIntegerConstantExpression(clang::Expr*, llvm::APSInt*, unsigned int, clang::AllowFoldKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x94b7322)
#20 0x00000000093c212a clang::Sema::BuildStaticAssertDeclaration(clang::SourceLocation, clang::Expr*, clang::Expr*, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x93c212a)
#21 0x0000000008f64410 clang::Parser::ParseStaticAssertDeclaration(clang::SourceLocation&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f64410)
#22 0x0000000008f5b5a6 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f5b5a6)
#23 0x0000000008f10290 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f10290)
#24 0x0000000008f11185 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f11185)
#25 0x0000000008eed47a clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8eed47a)
#26 0x0000000005ced358 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5ced358)
#27 0x0000000005c79ffd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5c79ffd)
#28 0x0000000005dc3823 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5dc3823)
#29 0x000000000171a6c8 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x171a6c8)
#30 0x00000000017115aa ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#31 0x0000000001711733 int llvm::function_ref<int (llvm::SmallVectorImpl<char const*>&)>::callback_fn<clang_main(int, char**, llvm::ToolContext const&)::'lambda'(llvm::SmallVectorImpl<char const*>&)>(long, llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#32 0x0000000005a3ca79 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
#33 0x0000000004e08b44 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4e08b44)
#34 0x0000000005a3d5e5 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
#35 0x00000000059fad52 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x59fad52)
#36 0x00000000059fe00c 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+++0x59fe00c)
#37 0x0000000005a0a115 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5a0a115)
#38 0x0000000001716cfa clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1716cfa)
#39 0x0000000001684503 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1684503)
#40 0x00007fad70a2a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#41 0x00007fad70a2a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#42 0x0000000001710f55 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1710f55)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)

ojrosten avatar Oct 29 '26 11:10 ojrosten

This seems related to, but distinct from https://github.com/llvm/llvm-project/issues/220256

ojrosten avatar Oct 29 '26 11:10 ojrosten

@llvm/issue-subscribers-clang-frontend

Author: Oliver Rosten (ojrosten)

Consider the following ill-formed code:
constexpr const int* f() { int i = 0; return &amp;i; }
static_assert(*f() == 0);

Since clang 23.1.0, the compiler crashes.

This was found using Homebrew 23.1.4 but reproduces on compiler explorer:

https://godbolt.org/z/aajn8jMsf

repro-0d8479.cpp repro-0d8479.sh

Crashing information:

clang++: /root/llvm-project/llvm/tools/clang/lib/Basic/Diagnostic.cpp:854: void HandleSelectModifier(const clang::Diagnostic&amp;, unsigned int, const char*, unsigned int, llvm::SmallVectorImpl&lt;char&gt;&amp;): Assertion `NextVal != ArgumentEnd &amp;&amp; "Value for integer select modifier was" " larger than the number of options in the diagnostic string!"' 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++23 -fsyntax-only &lt;source&gt;
1.	&lt;eof&gt; parser at end of file
 #<!-- -->0 0x0000000004ed1c58 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ed1c58)
 #<!-- -->1 0x0000000004eceeb4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4eceeb4)
 #<!-- -->2 0x0000000004ecf4ec llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4ecf4ec)
 #<!-- -->3 0x0000000004e08748 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
 #<!-- -->4 0x00007fad70a45330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #<!-- -->5 0x00007fad70a9ec0c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9ec0c)
 #<!-- -->6 0x00007fad70a4527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
 #<!-- -->7 0x00007fad70a288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
 #<!-- -->8 0x00007fad70a2881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
 #<!-- -->9 0x00007fad70a3b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#<!-- -->10 0x000000000527e2e9 clang::Diagnostic::FormatDiagnostic(char const*, char const*, llvm::SmallVectorImpl&lt;char&gt;&amp;) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527e2e9)
#<!-- -->11 0x0000000005d533f6 clang::TextDiagnosticPrinter::HandleDiagnostic(clang::DiagnosticsEngine::Level, clang::Diagnostic const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5d533f6)
#<!-- -->12 0x000000000527a7c0 clang::DiagnosticsEngine::Report(clang::DiagnosticsEngine::Level, clang::Diagnostic const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527a7c0)
#<!-- -->13 0x000000000527aed5 clang::DiagnosticsEngine::ProcessDiag(clang::DiagnosticBuilder const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527aed5)
#<!-- -->14 0x000000000527afb5 clang::DiagnosticsEngine::EmitDiagnostic(clang::DiagnosticBuilder const&amp;, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x527afb5)
#<!-- -->15 0x00000000090b1de6 clang::Sema::EmitDiagnostic(unsigned int, clang::DiagnosticBuilder const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x90b1de6)
#<!-- -->16 0x0000000009138972 clang::SemaBase::ImmediateDiagBuilder::~ImmediateDiagBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x9138972)
#<!-- -->17 0x0000000009093ac8 clang::SemaBase::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x9093ac8)
#<!-- -->18 0x00000000094b708d clang::Sema::VerifyIntegerConstantExpression(clang::Expr*, llvm::APSInt*, clang::Sema::VerifyICEDiagnoser&amp;, clang::AllowFoldKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x94b708d)
#<!-- -->19 0x00000000094b7322 clang::Sema::VerifyIntegerConstantExpression(clang::Expr*, llvm::APSInt*, unsigned int, clang::AllowFoldKind) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x94b7322)
#<!-- -->20 0x00000000093c212a clang::Sema::BuildStaticAssertDeclaration(clang::SourceLocation, clang::Expr*, clang::Expr*, clang::SourceLocation, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x93c212a)
#<!-- -->21 0x0000000008f64410 clang::Parser::ParseStaticAssertDeclaration(clang::SourceLocation&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f64410)
#<!-- -->22 0x0000000008f5b5a6 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&amp;, clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::SourceLocation*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f5b5a6)
#<!-- -->23 0x0000000008f10290 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&amp;, clang::ParsedAttributes&amp;, clang::ParsingDeclSpec*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f10290)
#<!-- -->24 0x0000000008f11185 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&lt;clang::DeclGroupRef&gt;&amp;, clang::Sema::ModuleImportState&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8f11185)
#<!-- -->25 0x0000000008eed47a clang::ParseAST(clang::Sema&amp;, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x8eed47a)
#<!-- -->26 0x0000000005ced358 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5ced358)
#<!-- -->27 0x0000000005c79ffd clang::CompilerInstance::ExecuteAction(clang::FrontendAction&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5c79ffd)
#<!-- -->28 0x0000000005dc3823 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5dc3823)
#<!-- -->29 0x000000000171a6c8 cc1_main(llvm::ArrayRef&lt;char const*&gt;, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x171a6c8)
#<!-- -->30 0x00000000017115aa ExecuteCC1Tool(llvm::SmallVectorImpl&lt;char const*&gt;&amp;, llvm::ToolContext const&amp;, llvm::IntrusiveRefCntPtr&lt;llvm::vfs::FileSystem&gt;) driver.cpp:0:0
#<!-- -->31 0x0000000001711733 int llvm::function_ref&lt;int (llvm::SmallVectorImpl&lt;char const*&gt;&amp;)&gt;::callback_fn&lt;clang_main(int, char**, llvm::ToolContext const&amp;)::'lambda'(llvm::SmallVectorImpl&lt;char const*&gt;&amp;)&gt;(long, llvm::SmallVectorImpl&lt;char const*&gt;&amp;) driver.cpp:0:0
#<!-- -->32 0x0000000005a3ca79 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const::'lambda'()&gt;(long) Job.cpp:0:0
#<!-- -->33 0x0000000004e08b44 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref&lt;void ()&gt;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4e08b44)
#<!-- -->34 0x0000000005a3d5e5 clang::driver::CC1Command::Execute(llvm::ArrayRef&lt;std::optional&lt;llvm::StringRef&gt;&gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;&gt;*, bool*) const (.part.0) Job.cpp:0:0
#<!-- -->35 0x00000000059fad52 clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&amp;, clang::driver::Command const*&amp;, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x59fad52)
#<!-- -->36 0x00000000059fe00c clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x59fe00c)
#<!-- -->37 0x0000000005a0a115 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&amp;, llvm::SmallVectorImpl&lt;std::pair&lt;int, clang::driver::Command const*&gt;&gt;&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5a0a115)
#<!-- -->38 0x0000000001716cfa clang_main(int, char**, llvm::ToolContext const&amp;) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1716cfa)
#<!-- -->39 0x0000000001684503 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1684503)
#<!-- -->40 0x00007fad70a2a1ca (/lib/x86_64-linux-gnu/libc.so.6+0x2a1ca)
#<!-- -->41 0x00007fad70a2a28b __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28b)
#<!-- -->42 0x0000000001710f55 _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1710f55)
clang++: error: clang frontend command failed due to signal (use -v to see invocation)

I believe the issue is that https://github.com/llvm/llvm-project/blob/31233848bd6a93f47582c0c79a4b8f01844c08ef/clang/include/clang/Basic/DiagnosticASTKinds.td#L191 was never updated for AK_Derefence when we did https://github.com/llvm/llvm-project/commit/9e5470e7d6ea1ad4fe25a9416706d769e41a03c1.

Pretty sure we just need to add |read of to end of the list of selections for access_kind_subobject and add |<ERROR> to the end of the list for access_kind_volatile. CC @cor3ntin

AaronBallman avatar Oct 29 '26 18:10 AaronBallman