[clang] [WinEH] Fix crash, object unwinding in the except block (PR #172287)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 22 00:58:42 PST 2026
https://github.com/GkvJwa updated https://github.com/llvm/llvm-project/pull/172287
>From 3be184c8b6d26d66f27e32d95a2da3a5ca962384 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Thu, 22 Jan 2026 16:59:50 +0800
Subject: [PATCH] Prevent functions has object unwinding when use seh on
windows
---
.../clang/Basic/DiagnosticCommonKinds.td | 2 ++
clang/lib/CodeGen/CGDecl.cpp | 7 ++++-
clang/test/CodeGenCXX/exceptions-seh.cpp | 29 +++++++++++++++++++
3 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td
index 6e50e225a8cc1..8eadbf2ba9042 100644
--- a/clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -286,6 +286,8 @@ def err_seh___except_filter : Error<
"%0 only allowed in __except filter expression">;
def err_seh___finally_block : Error<
"%0 only allowed in __finally block">;
+def err_seh_object_unwinding : Error<
+ "'__try' is not permitted in functions that require object unwinding">;
// Sema && AST
def note_invalid_subexpr_in_const_expr : Note<
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 8b1cd83af2396..3d9df61d4dab3 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2212,8 +2212,13 @@ void CodeGenFunction::EmitAutoVarCleanups(const AutoVarEmission &emission) {
const VarDecl &D = *emission.Variable;
// Check the type for a cleanup.
- if (QualType::DestructionKind dtorKind = D.needsDestruction(getContext()))
+ if (QualType::DestructionKind dtorKind = D.needsDestruction(getContext())) {
+ // Check if we're in a SEH block, prevent it
+ if (currentFunctionUsesSEHTry())
+ getContext().getDiagnostics().Report(D.getLocation(),
+ diag::err_seh_object_unwinding);
emitAutoVarTypeCleanup(emission, dtorKind);
+ }
// In GC mode, honor objc_precise_lifetime.
if (getLangOpts().getGC() != LangOptions::NonGC &&
diff --git a/clang/test/CodeGenCXX/exceptions-seh.cpp b/clang/test/CodeGenCXX/exceptions-seh.cpp
index bb374dd1f5bd5..22665a0c8fcc3 100644
--- a/clang/test/CodeGenCXX/exceptions-seh.cpp
+++ b/clang/test/CodeGenCXX/exceptions-seh.cpp
@@ -4,6 +4,12 @@
// RUN: %clang_cc1 -std=c++11 -fblocks -fms-extensions %s -triple=x86_64-windows-msvc -emit-llvm \
// RUN: -o - -mconstructor-aliases -O1 -disable-llvm-passes | \
// RUN: FileCheck %s --check-prefix=CHECK --check-prefix=NOCXX
+// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
+// RUN: -fms-extensions -x c++ -emit-llvm -verify %s -DERR1
+// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
+// RUN: -fms-extensions -x c++ -emit-llvm -verify %s -DERR2
+// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions \
+// RUN: -fms-extensions -x c++ -emit-llvm -verify %s -DERR3
extern "C" unsigned long _exception_code();
extern "C" void might_throw();
@@ -175,3 +181,26 @@ void use_inline() {
// CHECK: attributes #[[NOINLINE]] = { {{.*noinline.*}} }
void seh_in_noexcept() noexcept { __try {} __finally {} }
+
+#if defined(ERR1)
+void seh_unwinding() {
+ __try {
+ HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
+ } __except (1) {
+ }
+}
+#elif defined(ERR2)
+void seh_unwinding() {
+ __try {
+ } __except (1) {
+ HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
+ }
+}
+#elif defined(ERR3)
+void seh_unwinding() {
+ HasCleanup x; // expected-error{{'__try' is not permitted in functions that require object unwinding}}
+ __try {
+ } __except (1) {
+ }
+}
+#endif
More information about the cfe-commits
mailing list