-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[ARM][AArch64] BTI,GCS,PAC Module flag update. #86212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
f2f3356
14eb36d
4cb7a9c
99b6f2b
1e9f118
476dec8
a1fc006
f49192a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7066,6 +7066,8 @@ Error BitcodeReader::materializeModule() { | |
|
|
||
| UpgradeARCRuntime(*TheModule); | ||
|
|
||
| CopyModuleAttrToFunctions(*TheModule); | ||
|
||
|
|
||
| return Error::success(); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5278,6 +5278,106 @@ void llvm::UpgradeFunctionAttributes(Function &F) { | |
| } | ||
| } | ||
|
|
||
| // Check if the module attribute is present and set to one. | ||
| static bool isModuleAttributeOne(Module &M, const StringRef &ModAttr) { | ||
| const auto *Attr = | ||
| mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(ModAttr)); | ||
| return Attr && Attr->isOne(); | ||
| } | ||
|
|
||
| // Check if the module attribute is present and set to two. | ||
| static bool isModuleAttributeTwo(Module &M, const StringRef &ModAttr) { | ||
| const auto *Attr = | ||
| mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(ModAttr)); | ||
| return Attr && Attr->getZExtValue() == 2; | ||
| } | ||
|
|
||
| // Check if the function attribute is not present and set it. | ||
| static void SetFunctionAttrIfNotSet(Function &F, StringRef FnAttrName, | ||
| StringRef Value) { | ||
| if (!F.hasFnAttribute(FnAttrName)) | ||
| F.addFnAttr(FnAttrName, Value); | ||
| } | ||
|
|
||
| // Check if the function attribute is not present and set it if needed. | ||
| // If the attribute is "false" then removes it. | ||
| // If the attribute is "true" resets it to a valueless attribute. | ||
| static void ConvertFunctionAttr(Function &F, bool Set, StringRef FnAttrName) { | ||
| if (!F.hasFnAttribute(FnAttrName)) { | ||
| if (Set) | ||
| F.addFnAttr(FnAttrName); | ||
| } else { | ||
| auto A = F.getFnAttribute(FnAttrName); | ||
| if ("false" == A.getValueAsString()) | ||
| F.removeFnAttr(FnAttrName); | ||
| else if ("true" == A.getValueAsString()) { | ||
| F.removeFnAttr(FnAttrName); | ||
| F.addFnAttr(FnAttrName); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void llvm::CopyModuleAttrToFunctions(Module &M) { | ||
DanielKristofKiss marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Triple T(M.getTargetTriple()); | ||
| if (!T.isThumb() && !T.isARM() && !T.isAArch64()) | ||
| return; | ||
|
|
||
| if (isModuleAttributeTwo(M, "branch-target-enforcement")) | ||
|
||
| return; | ||
| if (isModuleAttributeTwo(M, "branch-protection-pauth-lr")) | ||
| return; | ||
| if (isModuleAttributeTwo(M, "guarded-control-stack")) | ||
| return; | ||
| if (isModuleAttributeTwo(M, "sign-return-address")) | ||
| return; | ||
|
|
||
| bool BTE = isModuleAttributeOne(M, "branch-target-enforcement"); | ||
| bool BPPLR = isModuleAttributeOne(M, "branch-protection-pauth-lr"); | ||
| bool GCS = isModuleAttributeOne(M, "guarded-control-stack"); | ||
| bool SRA = isModuleAttributeOne(M, "sign-return-address"); | ||
|
|
||
| StringRef SignTypeValue = "non-leaf"; | ||
kovdan01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (SRA && isModuleAttributeOne(M, "sign-return-address-all")) | ||
| SignTypeValue = "all"; | ||
|
|
||
| StringRef SignKeyValue = "a_key"; | ||
kovdan01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (SRA && isModuleAttributeOne(M, "sign-return-address-with-bkey")) | ||
| SignKeyValue = "b_key"; | ||
|
|
||
| for (Function &F : M.getFunctionList()) { | ||
| if (F.isDeclaration()) | ||
| continue; | ||
|
|
||
| if (SRA) { | ||
| SetFunctionAttrIfNotSet(F, "sign-return-address", SignTypeValue); | ||
| SetFunctionAttrIfNotSet(F, "sign-return-address-key", SignKeyValue); | ||
| } else { | ||
| if (auto A = F.getFnAttribute("sign-return-address"); | ||
| A.isValid() && "none" == A.getValueAsString()) { | ||
| F.removeFnAttr("sign-return-address"); | ||
| F.removeFnAttr("sign-return-address-key"); | ||
| } | ||
| } | ||
| ConvertFunctionAttr(F, BTE, "branch-target-enforcement"); | ||
| ConvertFunctionAttr(F, BPPLR, "branch-protection-pauth-lr"); | ||
| ConvertFunctionAttr(F, GCS, "guarded-control-stack"); | ||
| } | ||
|
|
||
| if (BTE) | ||
| M.setModuleFlag(llvm::Module::Min, "branch-target-enforcement", 2); | ||
| if (BPPLR) | ||
| M.setModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr", 2); | ||
| if (GCS) | ||
| M.setModuleFlag(llvm::Module::Min, "guarded-control-stack", 2); | ||
| if (SRA) { | ||
| M.setModuleFlag(llvm::Module::Min, "sign-return-address", 2); | ||
| if (isModuleAttributeOne(M, "sign-return-address-all")) | ||
| M.setModuleFlag(llvm::Module::Min, "sign-return-address-all", 2); | ||
| if (isModuleAttributeOne(M, "sign-return-address-with-bkey")) | ||
| M.setModuleFlag(llvm::Module::Min, "sign-return-address-with-bkey", 2); | ||
| } | ||
| } | ||
|
|
||
| static bool isOldLoopArgument(Metadata *MD) { | ||
| auto *T = dyn_cast_or_null<MDTuple>(MD); | ||
| if (!T) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1607,6 +1607,11 @@ Error IRLinker::run() { | |
| // Loop over all of the linked values to compute type mappings. | ||
| computeTypeMapping(); | ||
|
|
||
| // Convert module level attributes to function level attributes because | ||
| // after merging modules the attributes might change and would have different | ||
| // effect on the functions as the original module would have. | ||
| CopyModuleAttrToFunctions(*SrcM); | ||
|
|
||
| std::reverse(Worklist.begin(), Worklist.end()); | ||
| while (!Worklist.empty()) { | ||
| GlobalValue *GV = Worklist.back(); | ||
|
|
@@ -1771,6 +1776,11 @@ IRMover::IRMover(Module &M) : Composite(M) { | |
| for (const auto *MD : StructTypes.getVisitedMetadata()) { | ||
| SharedMDs[MD].reset(const_cast<MDNode *>(MD)); | ||
| } | ||
|
|
||
| // Convert module level attributes to function level attributes because | ||
| // after merging modules the attributes might change and would have different | ||
| // effect on the functions as the original module would have. | ||
| CopyModuleAttrToFunctions(M); | ||
|
||
| } | ||
|
|
||
| Error IRMover::move(std::unique_ptr<Module> Src, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,19 @@ | ||
| ;; Test that module flags "branch-target-enforcement" and "sign-return-address" can be upgraded to | ||
| ;; are upgraded from Error to Min. | ||
| ;; Test that module flags "branch-target-enforcement" and "sign-return-address" | ||
| ;; can be upgraded to are upgraded from Error to Min and the value is changed 2 | ||
| ;; as the module is converted to the semantic. | ||
|
|
||
| ; RUN: llvm-as %s -o - | llvm-dis - | FileCheck %s | ||
|
|
||
| target triple = "aarch64-unknown-linux-gnu" | ||
|
|
||
| !llvm.module.flags = !{!0, !1, !2, !3} | ||
|
|
||
| !0 = !{i32 1, !"branch-target-enforcement", i32 1} | ||
| !1 = !{i32 1, !"sign-return-address", i32 1} | ||
| !2 = !{i32 1, !"sign-return-address-all", i32 1} | ||
| !3 = !{i32 1, !"sign-return-address-with-bkey", i32 1} | ||
|
|
||
| ;CHECK: !0 = !{i32 8, !"branch-target-enforcement", i32 1} | ||
| ;CHECK: !1 = !{i32 8, !"sign-return-address", i32 1} | ||
| ;CHECK: !2 = !{i32 8, !"sign-return-address-all", i32 1} | ||
| ;CHECK: !3 = !{i32 8, !"sign-return-address-with-bkey", i32 1} | ||
| ;CHECK: !0 = !{i32 8, !"branch-target-enforcement", i32 2} | ||
| ;CHECK: !1 = !{i32 8, !"sign-return-address", i32 2} | ||
| ;CHECK: !2 = !{i32 8, !"sign-return-address-all", i32 2} | ||
| ;CHECK: !3 = !{i32 8, !"sign-return-address-with-bkey", i32 2} |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| ; This file contains the new semantic of the branch-target-enforcement, sign-return-address. | ||
kovdan01 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ; Used for test mixing a mixed link case and also verify the import too in llc. | ||
|
|
||
| ; RUN: llc %s -o - | FileCheck %s | ||
kovdan01 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" | ||
| target triple = "aarch64-unknown-linux-gnu" | ||
|
|
||
| define dso_local void @bar() #0 { | ||
| entry: | ||
| ret void | ||
| } | ||
| ; CHECK-LABEL: bar: | ||
kovdan01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ; CHECK-NOT: hint | ||
| ; CHECK-NOT: bti | ||
| ; CHECK: ret | ||
|
|
||
| define dso_local void @baz() #1 { | ||
| entry: | ||
| ret void | ||
| } | ||
|
|
||
| ; CHECK-LABEL: baz: | ||
| ; CHECK: hint | ||
| ; CHECK: ret | ||
|
|
||
| attributes #0 = { noinline nounwind optnone uwtable } | ||
| attributes #1 = { noinline nounwind optnone uwtable "branch-target-enforcement" } | ||
|
|
||
| !llvm.module.flags = !{!0, !1, !2, !3} | ||
|
|
||
| !0 = !{i32 8, !"branch-target-enforcement", i32 2} | ||
kovdan01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| !1 = !{i32 8, !"sign-return-address", i32 2} | ||
| !2 = !{i32 8, !"sign-return-address-all", i32 2} | ||
| !3 = !{i32 8, !"sign-return-address-with-bkey", i32 2} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| ; This file contains the previous semantic of the branch-target-enforcement, sign-return-address. | ||
| ; Used for test mixing a mixed link case and also verify the import too in llc. | ||
|
|
||
| ; RUN: llc %s -o - | FileCheck %s | ||
|
|
||
| target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" | ||
| target triple = "aarch64-unknown-linux-gnu" | ||
|
|
||
| define i32 @foo() #0 { | ||
| entry: | ||
| ret i32 42 | ||
| } | ||
|
|
||
| ; CHECK-LABEL: foo: | ||
| ; CHECK: hint #27 | ||
| ; CHECK: mov | ||
| ; CHECK: hint #31 | ||
| ; CHECK: ret | ||
|
|
||
| define i32 @fiz() #1 { | ||
| entry: | ||
| ret i32 43 | ||
| } | ||
|
|
||
| ; CHECK-LABEL: fiz: | ||
| ; CHECK-NOT: hint | ||
| ; CHECK-NOT: bti | ||
| ; CHECK: ret | ||
|
|
||
| attributes #0 = { noinline nounwind optnone uwtable } | ||
| attributes #1 = { noinline nounwind optnone uwtable "branch-target-enforcement"="false" "sign-return-address"="none" } | ||
|
|
||
| !llvm.module.flags = !{!0, !1, !2, !3} | ||
|
|
||
| !0 = !{i32 8, !"branch-target-enforcement", i32 1} | ||
| !1 = !{i32 8, !"sign-return-address", i32 1} | ||
| !2 = !{i32 8, !"sign-return-address-all", i32 1} | ||
| !3 = !{i32 8, !"sign-return-address-with-bkey", i32 1} |
Uh oh!
There was an error while loading. Please reload this page.