llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Adi Salimgereyev (adislm) <details> <summary>Changes</summary> `__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. --- Full diff: https://github.com/llvm/llvm-project/pull/246891.diff 2 Files Affected: - (modified) clang/lib/Sema/SemaChecking.cpp (+23) - (added) clang/test/Sema/seh-abnormal-termination.c (+33) ``````````diff 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 `````````` </details> https://github.com/llvm/llvm-project/pull/246891 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
