https://github.com/adislm updated https://github.com/llvm/llvm-project/pull/246891
>From 85fbf0698523caf5057a7dc5bcfa62a3c03c9ed0 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 --- 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
