[clang] [llvm] [WinEH] Fix crash, object unwinding in the except block (PR #172287)

via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 15 08:23:53 PST 2025


https://github.com/GkvJwa updated https://github.com/llvm/llvm-project/pull/172287

>From 9e6e80497273ae24fb20100064d6fc45311c1e2d Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Mon, 15 Dec 2025 19:51:18 +0800
Subject: [PATCH 1/3] [WinEH] Fix crash, object unwinding in the except block

---
 llvm/lib/CodeGen/WinEHPrepare.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/WinEHPrepare.cpp b/llvm/lib/CodeGen/WinEHPrepare.cpp
index 372fc50eb546e..94a08fb3a5f57 100644
--- a/llvm/lib/CodeGen/WinEHPrepare.cpp
+++ b/llvm/lib/CodeGen/WinEHPrepare.cpp
@@ -274,7 +274,8 @@ void llvm::calculateCXXStateForAsynchEH(const BasicBlock *BB, int State,
         State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
       else if (Fn && Fn->isIntrinsic() &&
                (Fn->getIntrinsicID() == Intrinsic::seh_scope_end ||
-                Fn->getIntrinsicID() == Intrinsic::seh_try_end)) {
+                Fn->getIntrinsicID() == Intrinsic::seh_try_end) &&
+               State > 0) {
         // In case of conditional ctor, let's retrieve State from Invoke
         State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
         // end of current state, retrive new state from UnwindMap
@@ -327,7 +328,8 @@ void llvm::calculateSEHStateForAsynchEH(const BasicBlock *BB, int State,
       const Constant *FilterOrNull = cast<Constant>(
           cast<CatchPadInst>(It)->getArgOperand(0)->stripPointerCasts());
       const Function *Filter = dyn_cast<Function>(FilterOrNull);
-      if (!Filter || !Filter->getName().starts_with("__IsLocalUnwind"))
+      if ((!Filter || !Filter->getName().starts_with("__IsLocalUnwind")) &&
+          State > 0)
         State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State
     } else if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) &&
                State > 0) {
@@ -341,7 +343,7 @@ void llvm::calculateSEHStateForAsynchEH(const BasicBlock *BB, int State,
         // Retrive the new State from seh_try_begin
         State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)];
       else if (Fn && Fn->isIntrinsic() &&
-               Fn->getIntrinsicID() == Intrinsic::seh_try_end)
+               Fn->getIntrinsicID() == Intrinsic::seh_try_end && State > 0)
         // end of current state, retrive new state from UnwindMap
         State = EHInfo.SEHUnwindMap[State].ToState;
     }

>From c765173b18d52cb139f43ab768b078dc0c2709d7 Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Tue, 16 Dec 2025 00:16:26 +0800
Subject: [PATCH 2/3] Add test

---
 .../windows-seh-EHa-CppExceptUnwind.cpp       | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)
 create mode 100644 clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp

diff --git a/clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp b/clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp
new file mode 100644
index 0000000000000..12869504c949e
--- /dev/null
+++ b/clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -triple x86_64-windows -fasync-exceptions -fcxx-exceptions -fexceptions -fms-extensions \
+// RUN: -x c++ -Wno-unused-but-set-variable -emit-obj %s -o - 2>&1 | FileCheck %s
+
+
+class aa {
+ public:
+  static aa bb();
+
+  ~aa();
+};
+
+void Foo(const int* p) {
+  int d = 0;
+  __try {
+    d = *p;
+  } __except (1) {
+    ::aa::bb();
+  }
+}
+
+// CHECK-NOT: Stack dump:
+// CHECK-NOT: Exception Code:
\ No newline at end of file

>From 10c244b7a3dba062893cc50fcb3c6dfe255ddead Mon Sep 17 00:00:00 2001
From: GkvJwa <gkvjwa at gmail.com>
Date: Tue, 16 Dec 2025 00:22:03 +0800
Subject: [PATCH 3/3] Add space

---
 clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp b/clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp
index 12869504c949e..7751abc2741ec 100644
--- a/clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp
+++ b/clang/test/CodeGen/windows-seh-EHa-CppExceptUnwind.cpp
@@ -19,4 +19,4 @@ void Foo(const int* p) {
 }
 
 // CHECK-NOT: Stack dump:
-// CHECK-NOT: Exception Code:
\ No newline at end of file
+// CHECK-NOT: Exception Code:



More information about the cfe-commits mailing list