https://github.com/adislm created 
https://github.com/llvm/llvm-project/pull/246891

`__abnormal_termination()` is code-generated by returning the first parameter 
of the outlined `__finally` helper. Outside of a `__finally` block, 
`EmitSEHAbnormalTermination()` reads `arg_begin()` of the current function, 
which may have no parameters, and clang crashes.

This adds the same kind of SEH context check that already exists for 
`__exception_code` and `__exception_info`: a call is rejected unless it is in a 
`__finally` block belonging to the current function. Requiring the block to 
belong to the current function also rejects calls from a lambda or nested 
block, which are emitted as separate functions and would read the wrong 
argument.

Test: `clang/test/Sema/seh-abnormal-termination.c`, run in both C and C++ 
modes, including the lambda case.

Fixes #213060

---
This contribution was developed with AI assistance (Claude Code) and reviewed 
and tested by me.

>From c92b2574148977d33b946655ed2aed997ffb1ee2 Mon Sep 17 00:00:00 2001
From: abs0luty <[email protected]>
Date: Sun, 29 Nov 2026 01:08:59 +0400
Subject: [PATCH] [Sema] Diagnose __abnormal_termination outside of __finally
 blocks

__abnormal_termination() is implemented by returning the first parameter
of the outlined __finally helper. CodeGen looks at the first argument of
the current function, so using the builtin outside of a __finally block
dereferences the argument list of a function that may have none and
crashes.

Reject such uses, mirroring the existing checks for __exception_code and
__exception_info. Also reject uses from a nested function or lambda,
since those are emitted as separate functions and would read the wrong
argument.

Fixes #213060

Assisted-by: Claude Code
---
 clang/lib/Sema/SemaChecking.cpp            | 23 +++++++++++++++
 clang/test/Sema/seh-abnormal-termination.c | 33 ++++++++++++++++++++++
 2 files changed, 56 insertions(+)
 create mode 100644 clang/test/Sema/seh-abnormal-termination.c

diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index aa53825cd0298..7a6629b364b73 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -1787,6 +1787,24 @@ static bool BuiltinSEHScopeCheck(Sema &SemaRef, CallExpr 
*TheCall,
   return false;
 }
 
+// __abnormal_termination() expands to the first parameter of the outlined
+// __finally helper, so it is only valid inside a __finally block that belongs
+// to the function currently being parsed.
+static bool BuiltinSEHFinallyCheck(Sema &SemaRef, CallExpr *TheCall) {
+  if (SemaRef.inTemplateInstantiation())
+    return false;
+
+  Scope *S = SemaRef.getCurScope();
+  if (!SemaRef.CurrentSEHFinally.empty() &&
+      SemaRef.CurrentSEHFinally.back()->getFnParent() == S->getFnParent())
+    return false;
+
+  auto *DRE = cast<DeclRefExpr>(TheCall->getCallee()->IgnoreParenCasts());
+  SemaRef.Diag(TheCall->getExprLoc(), diag::err_seh___finally_block)
+      << DRE->getDecl()->getIdentifier();
+  return true;
+}
+
 // In OpenCL, __builtin_alloca_* should return a pointer to address space
 // that corresponds to the stack address space i.e private address space.
 static void builtinAllocaAddrSpace(Sema &S, CallExpr *TheCall) {
@@ -3799,6 +3817,11 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, 
unsigned BuiltinID,
                              diag::err_seh___except_filter))
       return ExprError();
     break;
+  case Builtin::BI__abnormal_termination:
+  case Builtin::BI_abnormal_termination:
+    if (BuiltinSEHFinallyCheck(*this, TheCall))
+      return ExprError();
+    break;
   case Builtin::BI__GetExceptionInfo:
     if (checkArgCount(TheCall, 1))
       return ExprError();
diff --git a/clang/test/Sema/seh-abnormal-termination.c 
b/clang/test/Sema/seh-abnormal-termination.c
new file mode 100644
index 0000000000000..3dcda2e09831e
--- /dev/null
+++ b/clang/test/Sema/seh-abnormal-termination.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -triple x86_64-windows -fms-extensions -fsyntax-only 
-verify %s
+// RUN: %clang_cc1 -triple x86_64-windows -fms-extensions -fsyntax-only 
-verify -x c++ %s
+
+void outside_finally(void) {
+  (void)__abnormal_termination(); // expected-error{{only allowed in __finally 
block}}
+  (void)_abnormal_termination();  // expected-error{{only allowed in __finally 
block}}
+}
+
+void outside_finally_handlers(void) {
+  __try {
+    (void)__abnormal_termination(); // expected-error{{only allowed in 
__finally block}}
+  } __except (1) {
+    (void)__abnormal_termination(); // expected-error{{only allowed in 
__finally block}}
+  }
+}
+
+void inside_finally(void) {
+  __try {
+  } __finally {
+    (void)__abnormal_termination();
+    (void)_abnormal_termination();
+  }
+}
+
+#ifdef __cplusplus
+void lambda_inside_finally(void) {
+  __try {
+  } __finally {
+    auto L = [] { return __abnormal_termination(); }; // expected-error{{only 
allowed in __finally block}}
+    (void)L();
+  }
+}
+#endif

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to