clang crashes on valid C++ code: Assertion `Permissive || !(!LabelAndGotoScopes.count(TheLabel->getStmt()))' failed
| Bugzilla Link | 30321 |
| Version | trunk |
| OS | All |
| CC | @DougGregor,@rnk |
Extended Description
The code is accepted by GCC. It crashes at least all versions of Clang since 2.9.
$ clang++ -v
clang version 4.0.0 (trunk 280730)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/clang-trunk/bin
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.6.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.8.5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.3
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.1.1
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
$
$ g++-6.2 -c small.cpp
$
$ clang++ -c small.cpp
small.cpp:4:15: error: use of undeclared label 'T'
void *p = &&T;
^
clang-4.0: /tmp/llvm-builder/llvm-source-trunk/tools/clang/lib/Sema/JumpDiagnostics.cpp:678: void {anonymous}::JumpScopeChecker::VerifyIndirectJumps(): Assertion `Permissive || !(!LabelAndGotoScopes.count(TheLabel->getStmt()))' failed.
#​0 0x0000000001e484f5 llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/usr/local/clang-trunk/bin/clang-4.0+0x1e484f5)
#​1 0x0000000001e465de llvm::sys::RunSignalHandlers() (/usr/local/clang-trunk/bin/clang-4.0+0x1e465de)
#​2 0x0000000001e46740 SignalHandler(int) (/usr/local/clang-trunk/bin/clang-4.0+0x1e46740)
#​3 0x00007f1a160f2340 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10340)
#​4 0x00007f1a14ee3cc9 gsignal /build/eglibc-3GlaMS/eglibc-2.19/signal/../nptl/sysdeps/unix/sysv/linux/raise.c:56:0
#​5 0x00007f1a14ee70d8 abort /build/eglibc-3GlaMS/eglibc-2.19/stdlib/abort.c:91:0
#​6 0x00007f1a14edcb86 __assert_fail_base /build/eglibc-3GlaMS/eglibc-2.19/assert/assert.c:92:0
#​7 0x00007f1a14edcc32 (/lib/x86_64-linux-gnu/libc.so.6+0x2fc32)
#​8 0x0000000003037468 (anonymous namespace)::JumpScopeChecker::VerifyIndirectJumps() (/usr/local/clang-trunk/bin/clang-4.0+0x3037468)
#​9 0x0000000003039970 clang::Sema::DiagnoseInvalidJumps(clang::Stmt*) (/usr/local/clang-trunk/bin/clang-4.0+0x3039970)
#​10 0x0000000002c48a8f clang::Sema::ActOnFinishFunctionBody(clang::Decl*, clang::Stmt*, bool) (/usr/local/clang-trunk/bin/clang-4.0+0x2c48a8f)
#​11 0x0000000002fe3791 clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/usr/local/clang-trunk/bin/clang-4.0+0x2fe3791)
#​12 0x0000000002fe20e7 clang::Sema::PerformPendingInstantiations(bool) (/usr/local/clang-trunk/bin/clang-4.0+0x2fe20e7)
#​13 0x0000000002b9080c clang::Sema::ActOnEndOfTranslationUnit() (/usr/local/clang-trunk/bin/clang-4.0+0x2b9080c)
#​14 0x0000000002982085 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&) (/usr/local/clang-trunk/bin/clang-4.0+0x2982085)
#​15 0x0000000002979393 clang::ParseAST(clang::Sema&, bool, bool) (/usr/local/clang-trunk/bin/clang-4.0+0x2979393)
#​16 0x00000000025ea82e clang::CodeGenAction::ExecuteAction() (/usr/local/clang-trunk/bin/clang-4.0+0x25ea82e)
#​17 0x00000000022e8cce clang::FrontendAction::Execute() (/usr/local/clang-trunk/bin/clang-4.0+0x22e8cce)
#​18 0x00000000022be326 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/local/clang-trunk/bin/clang-4.0+0x22be326)
#​19 0x00000000023783d2 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/local/clang-trunk/bin/clang-4.0+0x23783d2)
#​20 0x0000000000b567a0 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/local/clang-trunk/bin/clang-4.0+0xb567a0)
#​21 0x0000000000b0123f main (/usr/local/clang-trunk/bin/clang-4.0+0xb0123f)
#​22 0x00007f1a14eceec5 __libc_start_main /build/eglibc-3GlaMS/eglibc-2.19/csu/libc-start.c:321:0
#​23 0x0000000000b528fe _start (/usr/local/clang-trunk/bin/clang-4.0+0xb528fe)
Stack dump:
0. Program arguments: /usr/local/clang-trunk/bin/clang-4.0 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -main-file-name small.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -coverage-notes-file /data2/small.gcno -resource-dir /usr/local/clang-trunk/bin/../lib/clang/4.0.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../include/x86_64-linux-gnu/c++/4.9 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../include/x86_64-linux-gnu/c++/4.9 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/backward -internal-isystem /usr/local/include -internal-isystem /usr/local/clang-trunk/bin/../lib/clang/4.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /data2 -ferror-limit 19 -fmessage-length 153 -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o small.o -x c++ small.cpp
1. <eof> parser at end of file
2. small.cpp:1:30: instantiating function definition 'f<int>'
clang-4.0: error: unable to execute command: Aborted (core dumped)
clang-4.0: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 4.0.0 (trunk 280730)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/clang-trunk/bin
clang-4.0: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script.
clang-4.0: note: diagnostic msg:
********************
PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-4.0: note: diagnostic msg: /tmp/small-b2f8d1.cpp
clang-4.0: note: diagnostic msg: /tmp/small-b2f8d1.sh
clang-4.0: note: diagnostic msg:
********************
template < typename T > void f ()
{
T:
void *p = &&T;
goto *p;
}
int main ()
{
f < int > ();
return 0;
}
The issue is that JumpDiagnostics doesn't know that errors occurred in this function prior to template instatiation:
#define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
: S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
I don't know a good way to check if there were errors while processing the template prior to instantiation. We could get away with setting Permissive to true if the current function is an instantiation, but then we might blind ourselves to some bugs.
Still crashes on current main: https://clang.godbolt.org/z/KGE5vqeq9
@llvm/issue-subscribers-clang-frontend
Still crashing as of post-16 trunk (checked via the link above):
<source>:4:13: error: use of undeclared label 'T'
4 | void *p = &&T;
| ^
clang++: /root/llvm-project/clang/lib/Sema/JumpDiagnostics.cpp:761:
void {anonymous}::JumpScopeChecker::VerifyIndirectOrAsmJumps(bool):
Assertion `Permissive || !(!LabelAndGotoScopes.count(TheLabel->getStmt()))' failed.
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 <source>
1. <eof> parser at end of file
2. <source>:1:30: instantiating function definition 'f<int>'
#0 0x000055f4a129ac1a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a9ac1a)
#1 0x000055f4a129884c llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x3a9884c)
#2 0x000055f4a11e7020 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#3 0x00007faca4cdf420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
#4 0x00007faca47ac00b raise (/lib/x86_64-linux-gnu/libc.so.6+0x4300b)
#5 0x00007faca478b859 abort (/lib/x86_64-linux-gnu/libc.so.6+0x22859)
#6 0x00007faca478b729 (/lib/x86_64-linux-gnu/libc.so.6+0x22729)
#7 0x00007faca479cfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
#8 0x000055f4a40e9fd8 (anonymous namespace)::JumpScopeChecker::VerifyIndirectOrAsmJumps(bool) JumpDiagnostics.cpp:0:0
#9 0x000055f4a40ea75c clang::Sema::DiagnoseInvalidJumps(clang::Stmt*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x68ea75c)
#10 0x000055f4a391a58f clang::Sema::ActOnFinishFunctionBody(clang::Decl*, clang::Stmt*, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x611a58f)
#11 0x000055f4a4028b0c clang::Sema::InstantiateFunctionDefinition(clang::SourceLocation, clang::FunctionDecl*, bool, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6828b0c)
#12 0x000055f4a4027bff clang::Sema::PerformPendingInstantiations(bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x6827bff)
#13 0x000055f4a377c2ab clang::Sema::ActOnEndOfTranslationUnitFragment(clang::Sema::TUFragmentKind) (.part.1898) Sema.cpp:0:0
#14 0x000055f4a377c9da clang::Sema::ActOnEndOfTranslationUnit() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5f7c9da)
#15 0x000055f4a364534b clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e4534b)
#16 0x000055f4a363a22b clang::ParseAST(clang::Sema&, bool, bool) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x5e3a22b)
#17 0x000055f4a2367560 clang::CodeGenAction::ExecuteAction() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4b67560)
#18 0x000055f4a1c892e9 clang::FrontendAction::Execute() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x44892e9)
#19 0x000055f4a1c1dc42 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x441dc42)
#20 0x000055f4a1d60c9f clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4560c9f)
#21 0x000055f49ea22cc3 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1222cc3)
#22 0x000055f49ea1ade8 ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&) driver.cpp:0:0
#23 0x000055f4a1a912f9 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
#24 0x000055f4a11e74a5 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x39e74a5)
#25 0x000055f4a1a93eee clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4293eee)
#26 0x000055f4a1a6060b clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x426060b)
#27 0x000055f4a1a611e3 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+++0x42611e3)
#28 0x000055f4a1a69fd6 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4269fd6)
#29 0x000055f49ea20a8e clang_main(int, char**, llvm::ToolContext const&) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1220a8e)
#30 0x000055f49e951cb1 main (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x1151cb1)
#31 0x00007faca478d083 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24083)
#32 0x000055f49ea1a4da _start (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x121a4da)
clang++: error: clang frontend command failed with exit code 134 (use -v to see invocation)
Compiler returned: 134
CC @cor3ntin
FYI, this seems unrelated to #63682
Prerequisites already established in this thread (no new claims): rnk's 2016 root-cause analysis — JumpScopeChecker's Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) is false during template instantiation because the unrecoverable errors ("use of undeclared label") were recorded while parsing the template pattern, not the instantiation; the assert then fires on !LabelAndGotoScopes.count(TheLabel->getStmt()) for the label-less LabelDecl; rnk floated "set Permissive to true if the current function is an instantiation" as a workaround; fhahn (2022) and Endilll (2023) confirmed the crash persists on assertions-enabled trunk; cor3ntin ruled out a relation to #63682.
New evidence, measured 2026-12-01:
-
Release (NDEBUG) builds do not crash — and that must not be read as "fixed". Homebrew clang 22.1.8 / 23.1.4 and Apple clang 21.0.0 all process the reproducer to a clean diagnostic and exit 1 (
use of undeclared label 'T', one error, no crash). I verified these are assertions-off builds:strings libclang-cpp.dylibcontains neitherLabelAndGotoScopesnor any genericAssertion `string (Homebrew ships the frontend inlibclang-cpp.dylib). With the assert compiled out,CHECK_PERMISSIVE(x)degenerates to(Permissive && (x)), which short-circuits atPermissive == false— soTheLabel->getStmt()is never evaluated and the loop just skips the (unreliable) diagnostic. Practical takeaway for anyone re-verifying this issue: an assertions-enabled build is mandatory; release-toolchain results are uninformative here. -
The root cause is still present on current main. As of the JumpDiagnostics.cpp currently on
main, the constructor still readsPermissive(s.hasAnyUnrecoverableErrorsInThisFunction()), andVerifyIndirectJumps()still contains the exactCHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt()))from the original report; unlikeVerifyJumps()(which gained agetStmt()null guard for MS inline-asm labels),VerifyIndirectJumps()has no such guard. So the crash should still reproduce on clang-assertions-trunk, consistent with Endilll's 2023 godbolt link; I could not run an assertions build locally to confirm the post-2023 state directly. -
Minimal repro for convenience (identical to the original, ~12 lines):
template < typename T > void f () { T: void *p = &&T; goto *p; } int main () { f < int > (); }
Nothing new on the fix direction: rnk's two options from 2016 (Permissive=true for instantiations, or a channel for template-definition errors) still describe the state of the art as far as I can tell; posting only to save the next person the release-build false negative.