From 4c47cb12a635c885a39dca05b00cb626ba14a3f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B9=85=E6=97=A5?= <609079623@qq.com> Date: Mon, 4 Aug 2025 17:52:53 +0800 Subject: [PATCH] pac: Add 'fast_pac_protected_ptr' attribute to protect the pointer's own security. Signed-off-by: Zhang Shouxu --- clang/include/clang/Basic/Attr.td | 7 +++ clang/include/clang/Basic/AttrDocs.td | 9 +++ clang/lib/Pac/PacDfi.cpp | 60 ++++++++++++------- clang/lib/Sema/SemaDeclAttr.cpp | 3 + ...a-attribute-supported-attributes-list.test | 3 + 5 files changed, 62 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 453ae6ed1ab6..d11c53e4deb4 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -1760,6 +1760,13 @@ def PacPtrTag : InheritableAttr { let SimpleHandler = 1; } +def FastPacPtrTag : InheritableAttr { + let Spellings = [GCC<"fast_pac_protected_ptr">]; + let Subjects = SubjectList<[Field], ErrorDiag>; + let Documentation = [FastPacPtrTagDocs]; + let SimpleHandler = 1; +} + def PacExclTag : InheritableAttr { let Spellings = [GCC<"pac_excl">]; let Subjects = SubjectList<[Field], ErrorDiag>; diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index f1ee3c291e31..c8296c2bed30 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -1370,6 +1370,15 @@ The ``pac_protected_ptr`` attribute asks the compiler to protect the struct poin }]; } +def FastPacPtrTagDocs : Documentation { + let Category = DocCatField; + let Content = [{ +The ``fast_pac_protected_ptr`` attribute asks the compiler to protect +the struct pointer type field with PA. Compared to the ``pac_protected_ptr``, +it is more aggressive. + }]; +} + def PacExclTagDocs : Documentation { let Category = DocCatField; let Content = [{ diff --git a/clang/lib/Pac/PacDfi.cpp b/clang/lib/Pac/PacDfi.cpp index 84c4d3dcfde9..b36d63a03f7a 100644 --- a/clang/lib/Pac/PacDfi.cpp +++ b/clang/lib/Pac/PacDfi.cpp @@ -26,6 +26,7 @@ using namespace clang; std::map RecordDecl2StructName; std::map> PacPtrNameInfos; +std::map> FastPacPtrNameInfos; std::map> PacFieldNameInfos; std::map> PacFieldExclNameInfos; std::map> PacFieldSeqlNameInfos; @@ -98,6 +99,7 @@ void PacDfiParseStruct(RecordDecl *TagDecl, ASTContext &Ctx, DiagnosticsEngine & // find pac_tag attr fields, and insert new fields std::vector PacPtrNames; + std::vector FastPacPtrNames; std::vector PacFieldNames; std::vector PacFieldExclNames; std::vector PacFieldSeqlNames; @@ -147,6 +149,15 @@ void PacDfiParseStruct(RecordDecl *TagDecl, ASTContext &Ctx, DiagnosticsEngine & continue; } PacPtrNames.push_back(Field); + } else if (Field->hasAttr()) { + // fast_pac_protected_ptr [only] support pointer type. + if (!ElemTy->isAnyPointerType()) { + Diags.Report(Field->getLocation(), + diag::warn_unsupported_pac_dfi_type) << Field->getType() + << Field->getAttr()->getSpelling(); + continue; + } + FastPacPtrNames.push_back(Field); } } @@ -163,6 +174,9 @@ void PacDfiParseStruct(RecordDecl *TagDecl, ASTContext &Ctx, DiagnosticsEngine & if (!PacPtrNames.empty()) { PacPtrNameInfos.insert(std::make_pair(TagDecl, PacPtrNames)); } + if (!FastPacPtrNames.empty()) { + FastPacPtrNameInfos.insert(std::make_pair(TagDecl, FastPacPtrNames)); + } if (!PacFieldExclNames.empty()) { PacFieldExclNameInfos.insert(std::make_pair(TagDecl, PacFieldExclNames)); } @@ -221,27 +235,33 @@ void pacDfiCreateNameMetaData( } } -void PacDfiEmitStructFieldsMetadata(llvm::Module &M, CodeGen::CodeGenModule *CGM, - std::function func) +void PacDfiEmitStructFieldsMetadata(llvm::Module &M, + CodeGen::CodeGenModule *CGM, std::function func) { - if (!llvm::PARTS::useDataFieldTag()) { - return; - } - // emit struct fields that need to protect with PA - if (!PacFieldNameInfos.empty()) { - pacDfiCreateNameMetaData(PacFieldNameInfos, "pa_field_info", M, CGM, - func); - } - if (!PacPtrNameInfos.empty()) { - pacDfiCreateNameMetaData(PacPtrNameInfos, "pa_ptr_field_info", M, CGM, - func); - } - if (!PacFieldExclNameInfos.empty()) { - pacDfiCreateNameMetaData(PacFieldExclNameInfos, "pa_excl_field_info", M, CGM, func); - } - if (!PacFieldSeqlNameInfos.empty()) { - pacDfiCreateNameMetaData(PacFieldSeqlNameInfos, "pa_seql_field_info", M, CGM, func); - } + if (!llvm::PARTS::useDataFieldTag()) { + return; + } + // emit struct fields that need to protect with PA + if (!PacFieldNameInfos.empty()) { + pacDfiCreateNameMetaData(PacFieldNameInfos, "pa_field_info", M, CGM, func); + } + if (!PacPtrNameInfos.empty()) { + pacDfiCreateNameMetaData(PacPtrNameInfos, + "pa_ptr_field_info", M, CGM, func); + } + if (!FastPacPtrNameInfos.empty()) { + pacDfiCreateNameMetaData(FastPacPtrNameInfos, + "fast_pa_ptr_field_info", M, CGM, func); + } + if (!PacFieldExclNameInfos.empty()) { + pacDfiCreateNameMetaData(PacFieldExclNameInfos, + "pa_excl_field_info", M, CGM, func); + } + if (!PacFieldSeqlNameInfos.empty()) { + pacDfiCreateNameMetaData(PacFieldSeqlNameInfos, + "pa_seql_field_info", M, CGM, func); + } } void PacDfiRecordDecl2StructName(const RecordDecl *RD, llvm::StructType *Entry) diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 8a105b988c46..3e3944a12866 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -9165,6 +9165,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL, case ParsedAttr::AT_PacPtrTag: handleSimpleAttribute(S, D, AL); break; + case ParsedAttr::AT_FastPacPtrTag: + handleSimpleAttribute(S, D, AL); + break; } // OHOS_LOCAL end } diff --git a/clang/test/Misc/pragma-attribute-supported-attributes-list.test b/clang/test/Misc/pragma-attribute-supported-attributes-list.test index ae909cbebb9e..18b909fcd594 100644 --- a/clang/test/Misc/pragma-attribute-supported-attributes-list.test +++ b/clang/test/Misc/pragma-attribute-supported-attributes-list.test @@ -67,6 +67,9 @@ // CHECK-NEXT: Error (SubjectMatchRule_function) // CHECK-NEXT: ExcludeFromExplicitInstantiation (SubjectMatchRule_variable, SubjectMatchRule_function, SubjectMatchRule_record) // CHECK-NEXT: ExternalSourceSymbol ((SubjectMatchRule_record, SubjectMatchRule_enum, SubjectMatchRule_enum_constant, SubjectMatchRule_field, SubjectMatchRule_function, SubjectMatchRule_namespace, SubjectMatchRule_objc_category, SubjectMatchRule_objc_implementation, SubjectMatchRule_objc_interface, SubjectMatchRule_objc_method, SubjectMatchRule_objc_property, SubjectMatchRule_objc_protocol, SubjectMatchRule_record, SubjectMatchRule_type_alias, SubjectMatchRule_variable)) +// OHOS_LOCAL begin +// CHECK-NEXT: FastPacPtrTag (SubjectMatchRule_field) +// OHOS_LOCAL end // CHECK-NEXT: FlagEnum (SubjectMatchRule_enum) // CHECK-NEXT: Flatten (SubjectMatchRule_function) // CHECK-NEXT: FunctionReturnThunks (SubjectMatchRule_function) -- Gitee