https://github.com/adislm updated https://github.com/llvm/llvm-project/pull/246891
>From 22e8232da659245ce46a3e2748877007b30d9949 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. Previously the builtin was only available in Microsoft mode, and Borland mode diagnosed these names by poisoning the identifiers in the parser. Make the builtin available in Borland mode too and drop the corresponding poisoning, so both modes share one check and emit a single diagnostic. Fixes #213060 --- clang/include/clang/Basic/Builtins.h | 7 ++-- clang/include/clang/Basic/Builtins.td | 2 +- clang/include/clang/Basic/BuiltinsBase.td | 1 + clang/include/clang/Parse/Parser.h | 3 -- .../clang/Parse/RAIIObjectsForParser.h | 19 ++++------- clang/lib/Basic/Builtins.cpp | 4 +++ clang/lib/Parse/ParseStmt.cpp | 4 --- clang/lib/Parse/Parser.cpp | 10 +----- clang/lib/Sema/SemaChecking.cpp | 23 +++++++++++++ clang/test/Sema/__try.c | 8 ++--- clang/test/Sema/seh-abnormal-termination.c | 34 +++++++++++++++++++ 11 files changed, 79 insertions(+), 36 deletions(-) create mode 100644 clang/test/Sema/seh-abnormal-termination.c diff --git a/clang/include/clang/Basic/Builtins.h b/clang/include/clang/Basic/Builtins.h index 0420b5547a5682..d2482f312d07e9 100644 --- a/clang/include/clang/Basic/Builtins.h +++ b/clang/include/clang/Basic/Builtins.h @@ -32,7 +32,7 @@ class TargetInfo; class IdentifierTable; class LangOptions; -enum LanguageID : uint16_t { +enum LanguageID : uint32_t { GNU_LANG = 0x1, // builtin requires GNU mode. C_LANG = 0x2, // builtin for c only. CXX_LANG = 0x4, // builtin for cplusplus only. @@ -49,9 +49,12 @@ enum LanguageID : uint16_t { C23_LANG = 0x2000, // builtin requires C23 or later. C2Y_LANG = 0x4000, // builtin requires C2y or later. REFLECTION_LANG = 0x8000, // builtin requires use of 'freflection' option. + MS_OR_BORLAND_LANG = 0x10000, // builtin requires MS or Borland mode. ALL_LANGUAGES = C_LANG | CXX_LANG | OBJC_LANG, // builtin for all languages. ALL_GNU_LANGUAGES = ALL_LANGUAGES | GNU_LANG, // builtin requires GNU mode. - ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG // builtin requires MS mode. + ALL_MS_LANGUAGES = ALL_LANGUAGES | MS_LANG, // builtin requires MS mode. + ALL_MS_OR_BORLAND_LANGUAGES = + ALL_LANGUAGES | MS_OR_BORLAND_LANG // builtin requires MS or Borland mode. }; struct HeaderDesc { diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index 820123e8f6a0f4..bb31a9b2879e67 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -2960,7 +2960,7 @@ def ExceptionInfo : MSLangBuiltin { let Prototype = "void*()"; } -def AbnormalTermination : MSLangBuiltin { +def AbnormalTermination : MSOrBorlandLangBuiltin { let Spellings = ["__abnormal_termination", "_abnormal_termination"]; let Attributes = [NoThrow]; let Prototype = "int()"; diff --git a/clang/include/clang/Basic/BuiltinsBase.td b/clang/include/clang/Basic/BuiltinsBase.td index 86ead09e99aba1..4d282c1ae529e0 100644 --- a/clang/include/clang/Basic/BuiltinsBase.td +++ b/clang/include/clang/Basic/BuiltinsBase.td @@ -202,6 +202,7 @@ class LangBuiltin<string languages> : Builtin { } class MSLangBuiltin : LangBuiltin<"ALL_MS_LANGUAGES">; +class MSOrBorlandLangBuiltin : LangBuiltin<"ALL_MS_OR_BORLAND_LANGUAGES">; class CoroLangBuiltin : LangBuiltin<"COR_LANG">; class OCLPipeLangBuiltin : LangBuiltin<"OCL_PIPE">; class OCL_DSELangBuiltin : LangBuiltin<"OCL_DSE">; diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h index 4f9087b29c7b48..99f86fa33927cc 100644 --- a/clang/include/clang/Parse/Parser.h +++ b/clang/include/clang/Parse/Parser.h @@ -664,9 +664,6 @@ class Parser : public CodeCompletionHandler { // __except filter expression IdentifierInfo *Ident__exception_info, *Ident___exception_info, *Ident_GetExceptionInfo; - // __finally - IdentifierInfo *Ident__abnormal_termination, *Ident___abnormal_termination, - *Ident_AbnormalTermination; /// Contextual keywords for Microsoft extensions. IdentifierInfo *Ident__except; diff --git a/clang/include/clang/Parse/RAIIObjectsForParser.h b/clang/include/clang/Parse/RAIIObjectsForParser.h index 7b2c9c6c049a45..08f75ba7f388ae 100644 --- a/clang/include/clang/Parse/RAIIObjectsForParser.h +++ b/clang/include/clang/Parse/RAIIObjectsForParser.h @@ -412,27 +412,20 @@ namespace clang { }; class PoisonSEHIdentifiersRAIIObject { - PoisonIdentifierRAIIObject Ident_AbnormalTermination; PoisonIdentifierRAIIObject Ident_GetExceptionCode; PoisonIdentifierRAIIObject Ident_GetExceptionInfo; - PoisonIdentifierRAIIObject Ident__abnormal_termination; PoisonIdentifierRAIIObject Ident__exception_code; PoisonIdentifierRAIIObject Ident__exception_info; - PoisonIdentifierRAIIObject Ident___abnormal_termination; PoisonIdentifierRAIIObject Ident___exception_code; PoisonIdentifierRAIIObject Ident___exception_info; public: PoisonSEHIdentifiersRAIIObject(Parser &Self, bool NewValue) - : Ident_AbnormalTermination(Self.Ident_AbnormalTermination, NewValue), - Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue), - Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue), - Ident__abnormal_termination(Self.Ident__abnormal_termination, NewValue), - Ident__exception_code(Self.Ident__exception_code, NewValue), - Ident__exception_info(Self.Ident__exception_info, NewValue), - Ident___abnormal_termination(Self.Ident___abnormal_termination, NewValue), - Ident___exception_code(Self.Ident___exception_code, NewValue), - Ident___exception_info(Self.Ident___exception_info, NewValue) { - } + : Ident_GetExceptionCode(Self.Ident_GetExceptionCode, NewValue), + Ident_GetExceptionInfo(Self.Ident_GetExceptionInfo, NewValue), + Ident__exception_code(Self.Ident__exception_code, NewValue), + Ident__exception_info(Self.Ident__exception_info, NewValue), + Ident___exception_code(Self.Ident___exception_code, NewValue), + Ident___exception_info(Self.Ident___exception_info, NewValue) {} }; /// RAII class that helps handle the parsing of an open/close delimiter diff --git a/clang/lib/Basic/Builtins.cpp b/clang/lib/Basic/Builtins.cpp index 7ea55b268cf8f8..70bc91272bfad3 100644 --- a/clang/lib/Basic/Builtins.cpp +++ b/clang/lib/Basic/Builtins.cpp @@ -172,6 +172,10 @@ static bool builtinIsSupported(const llvm::StringTable &Strings, /* MSMode Unsupported */ if (!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG)) return false; + /* MSOrBorlandMode Unsupported */ + if (!(LangOpts.MicrosoftExt || LangOpts.Borland) && + (BuiltinInfo.Langs & MS_OR_BORLAND_LANG)) + return false; /* HLSLMode Unsupported */ if (!LangOpts.HLSL && (BuiltinInfo.Langs & HLSL_LANG)) return false; diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp index 4c3c4e5117813d..3693f56d19f0a4 100644 --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -680,10 +680,6 @@ StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) { } StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyLoc) { - PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false), - raii2(Ident___abnormal_termination, false), - raii3(Ident_AbnormalTermination, false); - if (Tok.isNot(tok::l_brace)) return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace); diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp index fbfc632c47e45b..04a4274b987f5f 100644 --- a/clang/lib/Parse/Parser.cpp +++ b/clang/lib/Parse/Parser.cpp @@ -562,10 +562,8 @@ void Parser::Initialize() { Ident_except = nullptr; Ident__exception_code = Ident__exception_info = nullptr; - Ident__abnormal_termination = Ident___exception_code = nullptr; - Ident___exception_info = Ident___abnormal_termination = nullptr; + Ident___exception_code = Ident___exception_info = nullptr; Ident_GetExceptionCode = Ident_GetExceptionInfo = nullptr; - Ident_AbnormalTermination = nullptr; if(getLangOpts().Borland) { Ident__exception_info = PP.getIdentifierInfo("_exception_info"); @@ -574,9 +572,6 @@ void Parser::Initialize() { Ident__exception_code = PP.getIdentifierInfo("_exception_code"); Ident___exception_code = PP.getIdentifierInfo("__exception_code"); Ident_GetExceptionCode = PP.getIdentifierInfo("GetExceptionCode"); - Ident__abnormal_termination = PP.getIdentifierInfo("_abnormal_termination"); - Ident___abnormal_termination = PP.getIdentifierInfo("__abnormal_termination"); - Ident_AbnormalTermination = PP.getIdentifierInfo("AbnormalTermination"); PP.SetPoisonReason(Ident__exception_code,diag::err_seh___except_block); PP.SetPoisonReason(Ident___exception_code,diag::err_seh___except_block); @@ -584,9 +579,6 @@ void Parser::Initialize() { PP.SetPoisonReason(Ident__exception_info,diag::err_seh___except_filter); PP.SetPoisonReason(Ident___exception_info,diag::err_seh___except_filter); PP.SetPoisonReason(Ident_GetExceptionInfo,diag::err_seh___except_filter); - PP.SetPoisonReason(Ident__abnormal_termination,diag::err_seh___finally_block); - PP.SetPoisonReason(Ident___abnormal_termination,diag::err_seh___finally_block); - PP.SetPoisonReason(Ident_AbnormalTermination,diag::err_seh___finally_block); } Actions.Initialize(); diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index aa53825cd0298a..7a6629b364b73f 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/__try.c b/clang/test/Sema/__try.c index 06360cb0a5dcf0..672c2200961fe7 100644 --- a/clang/test/Sema/__try.c +++ b/clang/test/Sema/__try.c @@ -152,12 +152,12 @@ void TEST(void) { (void)AbnormalTermination(); // expected-error{{only allowed in __finally block}} __try { - (void)AbnormalTermination; // expected-error{{only allowed in __finally block}} - (void)__abnormal_termination; // expected-error{{only allowed in __finally block}} + (void)AbnormalTermination; // expected-error{{builtin functions must be directly called}} + (void)__abnormal_termination; // expected-error{{builtin functions must be directly called}} } __except( 1 ) { - (void)AbnormalTermination; // expected-error{{only allowed in __finally block}} - (void)__abnormal_termination; // expected-error{{only allowed in __finally block}} + (void)AbnormalTermination; // expected-error{{builtin functions must be directly called}} + (void)__abnormal_termination; // expected-error{{builtin functions must be directly called}} } __try { diff --git a/clang/test/Sema/seh-abnormal-termination.c b/clang/test/Sema/seh-abnormal-termination.c new file mode 100644 index 00000000000000..fc81a8a660bb95 --- /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
