Issue 240065
Summary [clang-cl] /EHa crashes when a goto enters a scope without active EH cleanup
Labels new issue
Assignees
Reporter c3c
    Clang crashes while generating LLVM IR for accepted Microsoft-extension C++
when `/EHa` is enabled and a `goto` jumps over a local initialization to a
label that has no active EH cleanup scope.

This reduced case does not read the bypassed variable:

```cpp
void sink();

void reproduce(bool skip_initialization) {
  if (skip_initialization)
    goto cleanup;

  int value = 0;
 (void)value;

cleanup:
  sink();
}
```

Reproducer command:

```console
clang-cl /nologo /c /Od /EHa /clang:-emit-llvm \
 --target=x86_64-pc-windows-msvc /Foreproducer.bc -- reproducer.cpp
```

Actual result:

```text
warning: jump from this goto statement to its label is a Microsoft extension
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/
...
llvm::BasicBlock::getFirstNonPHIIt() const
clang::CodeGen::CodeGenFunction::GenerateCode(...)
```

Expected result: Clang may retain the Microsoft-extension diagnostic, but it
must not crash and should emit verifier-clean IR.

Tested with the official LLVM 23.1.5 Linux x64 release archive:

```text
clang version 23.1.5
(https://github.com/llvm/llvm-project 392dfff97d29b59d77e5cac6d4b6ef2585fe7117)
Target: x86_64-pc-windows-msvc
```

The source of the crash appears to be `CodeGenFunction::EmitLabelStmt` in
`clang/lib/CodeGen/CGStmt.cpp`:

```cpp
if (getLangOpts().EHAsynch && S.isSideEntry())
  EmitSehCppScopeBegin();
```

`EmitSehCppScopeBegin()` calls `EmitSehScope()`, which requires a non-null
`getInvokeDest()` and asserts that requirement. A label can be a side entry
because it bypasses initialization even when no EH cleanup scope exists, so
there is no invoke destination in this case. The other caller in
`EHScopeStack::pushCleanup()` already checks `CGF->getInvokeDest()` before
emitting `llvm.seh.scope.begin`.

This guard fixes the reproducer:

```diff
-  if (getLangOpts().EHAsynch && S.isSideEntry())
+  if (getLangOpts().EHAsynch && S.isSideEntry() && getInvokeDest())
 EmitSehCppScopeBegin();
```

With that change, the command succeeds and `opt -passes=verify` accepts the
result. The same unguarded call is still present on `main` as of 2026-11-07.

Suggested regression test:

```cpp
// RUN: %clang_cc1 -triple x86_64-windows-msvc -fasync-exceptions \
// RUN:   -fcxx-exceptions -fexceptions -fms-extensions -fms-compatibility \
// RUN:   -Wno-microsoft-goto \
// RUN:   -emit-llvm %s -o - | FileCheck %s

void sink();

// CHECK-LABEL: define dso_local void @"?side_entry_without_eh_scope@@YAX_N@Z"
// CHECK-NOT: llvm.seh.scope.begin
// CHECK: call void @"?sink@@YAXXZ"()
// CHECK: ret void
void side_entry_without_eh_scope(bool skip_initialization) {
  if (skip_initialization)
    goto cleanup;

  int value = 0;
 (void)value;

cleanup:
  sink();
}
```

The regression command and checks pass with the guarded implementation.
Report generated with AI assistance.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to