https://github.com/adislm updated https://github.com/llvm/llvm-project/pull/246891
>From 86cbdd4b82446b6e17df8d0658e8d380a7f8ab9b 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. Borland mode already diagnoses these calls through the poisoned identifier set up by the parser, so skip the new check there to avoid emitting a second diagnostic when both language modes are enabled. Fixes #213060 --- clang/lib/Sema/SemaChecking.cpp | 25 ++++++++++++++++ clang/test/Sema/seh-abnormal-termination.c | 34 ++++++++++++++++++++++ 2 files changed, 59 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..fb3fb849623c4 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -1787,6 +1787,26 @@ 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) { + // Borland mode diagnoses these through the poisoned identifier set up by the + // parser, so don't emit a second diagnostic here. + if (SemaRef.inTemplateInstantiation() || SemaRef.getLangOpts().Borland) + 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 +3819,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..fc81a8a660bb9 --- /dev/null +++ b/clang/test/Sema/seh-abnormal-termination.c @@ -0,0 +1,34 @@ +// 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 +// RUN: %clang_cc1 -triple x86_64-windows -fms-extensions -fborland-extensions -fsyntax-only -verify %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
