Skip to content

Commit 7bd1721

Browse files
committed
Re-land "[AArch64] Codegen support for FEAT_PAuthLR" (#75947)
This reverts commit 9f0f558. Fix expensive checks failure by properly marking register def for ADR.
1 parent c50de57 commit 7bd1721

File tree

21 files changed

+752
-25
lines changed

21 files changed

+752
-25
lines changed

clang/include/clang/Basic/LangOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ ENUM_LANGOPT(SignReturnAddressScope, SignReturnAddressScopeKind, 2, SignReturnAd
456456
ENUM_LANGOPT(SignReturnAddressKey, SignReturnAddressKeyKind, 1, SignReturnAddressKeyKind::AKey,
457457
"Key used for return address signing")
458458
LANGOPT(BranchTargetEnforcement, 1, 0, "Branch-target enforcement enabled")
459+
LANGOPT(BranchProtectionPAuthLR, 1, 0, "Use PC as a diversifier using PAuthLR NOP instructions.")
459460

460461
LANGOPT(SpeculativeLoadHardening, 1, 0, "Speculative load hardening enabled")
461462

clang/include/clang/Basic/TargetInfo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,7 @@ class TargetInfo : public TransferrableTargetInfo,
13721372
LangOptions::SignReturnAddressKeyKind SignKey =
13731373
LangOptions::SignReturnAddressKeyKind::AKey;
13741374
bool BranchTargetEnforcement = false;
1375+
bool BranchProtectionPAuthLR = false;
13751376
};
13761377

13771378
/// Determine if the Architecture in this TargetInfo supports branch

clang/include/clang/Driver/Options.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7000,6 +7000,8 @@ def msign_return_address_key_EQ : Joined<["-"], "msign-return-address-key=">,
70007000
Values<"a_key,b_key">;
70017001
def mbranch_target_enforce : Flag<["-"], "mbranch-target-enforce">,
70027002
MarshallingInfoFlag<LangOpts<"BranchTargetEnforcement">>;
7003+
def mbranch_protection_pauth_lr : Flag<["-"], "mbranch-protection-pauth-lr">,
7004+
MarshallingInfoFlag<LangOpts<"BranchProtectionPAuthLR">>;
70037005
def fno_dllexport_inlines : Flag<["-"], "fno-dllexport-inlines">,
70047006
MarshallingInfoNegativeFlag<LangOpts<"DllExportInlines">>;
70057007
def cfguard_no_checks : Flag<["-"], "cfguard-no-checks">,

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ bool AArch64TargetInfo::validateBranchProtection(StringRef Spec, StringRef,
225225
BPI.SignKey = LangOptions::SignReturnAddressKeyKind::BKey;
226226

227227
BPI.BranchTargetEnforcement = PBP.BranchTargetEnforcement;
228+
BPI.BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
228229
return true;
229230
}
230231

clang/lib/Basic/Targets/ARM.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ bool ARMTargetInfo::validateBranchProtection(StringRef Spec, StringRef Arch,
419419
BPI.SignKey = LangOptions::SignReturnAddressKeyKind::AKey;
420420

421421
BPI.BranchTargetEnforcement = PBP.BranchTargetEnforcement;
422+
BPI.BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
422423
return true;
423424
}
424425

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,9 @@ void CodeGenModule::Release() {
11061106
if (LangOpts.BranchTargetEnforcement)
11071107
getModule().addModuleFlag(llvm::Module::Min, "branch-target-enforcement",
11081108
1);
1109+
if (LangOpts.BranchProtectionPAuthLR)
1110+
getModule().addModuleFlag(llvm::Module::Min, "branch-protection-pauth-lr",
1111+
1);
11091112
if (LangOpts.hasSignReturnAddress())
11101113
getModule().addModuleFlag(llvm::Module::Min, "sign-return-address", 1);
11111114
if (LangOpts.isSignReturnAddressScopeAll())

clang/lib/CodeGen/Targets/AArch64.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
136136

137137
Fn->addFnAttr("branch-target-enforcement",
138138
BPI.BranchTargetEnforcement ? "true" : "false");
139+
Fn->addFnAttr("branch-protection-pauth-lr",
140+
BPI.BranchProtectionPAuthLR ? "true" : "false");
139141
}
140142

141143
bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
14971497
<< Triple.getArchName();
14981498

14991499
StringRef Scope, Key;
1500-
bool IndirectBranches;
1500+
bool IndirectBranches, BranchProtectionPAuthLR;
15011501

15021502
if (A->getOption().matches(options::OPT_msign_return_address_EQ)) {
15031503
Scope = A->getValue();
@@ -1506,6 +1506,7 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
15061506
<< A->getSpelling() << Scope;
15071507
Key = "a_key";
15081508
IndirectBranches = false;
1509+
BranchProtectionPAuthLR = false;
15091510
} else {
15101511
StringRef DiagMsg;
15111512
llvm::ARM::ParsedBranchProtection PBP;
@@ -1517,6 +1518,7 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
15171518
<< "b-key" << A->getAsString(Args);
15181519
Scope = PBP.Scope;
15191520
Key = PBP.Key;
1521+
BranchProtectionPAuthLR = PBP.BranchProtectionPAuthLR;
15201522
IndirectBranches = PBP.BranchTargetEnforcement;
15211523
}
15221524

@@ -1525,6 +1527,9 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
15251527
if (!Scope.equals("none"))
15261528
CmdArgs.push_back(
15271529
Args.MakeArgString(Twine("-msign-return-address-key=") + Key));
1530+
if (BranchProtectionPAuthLR)
1531+
CmdArgs.push_back(
1532+
Args.MakeArgString(Twine("-mbranch-protection-pauth-lr")));
15281533
if (IndirectBranches)
15291534
CmdArgs.push_back("-mbranch-target-enforce");
15301535
}

clang/test/CodeGen/aarch64-branch-protection-attr.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ __attribute__ ((target("branch-protection=pac-ret+leaf+bti")))
4646
void btileaf() {}
4747
// CHECK: define{{.*}} void @btileaf() #[[#BTIPACLEAF:]]
4848

49+
50+
__attribute__ ((target("branch-protection=pac-ret+pc")))
51+
void pauthlr() {}
52+
// CHECK: define{{.*}} void @pauthlr() #[[#PAUTHLR:]]
53+
54+
__attribute__ ((target("branch-protection=pac-ret+pc+b-key")))
55+
void pauthlr_bkey() {}
56+
// CHECK: define{{.*}} void @pauthlr_bkey() #[[#PAUTHLR_BKEY:]]
57+
58+
__attribute__ ((target("branch-protection=pac-ret+pc+leaf")))
59+
void pauthlr_leaf() {}
60+
// CHECK: define{{.*}} void @pauthlr_leaf() #[[#PAUTHLR_LEAF:]]
61+
62+
__attribute__ ((target("branch-protection=pac-ret+pc+bti")))
63+
void pauthlr_bti() {}
64+
// CHECK: define{{.*}} void @pauthlr_bti() #[[#PAUTHLR_BTI:]]
65+
66+
4967
// CHECK-DAG: attributes #[[#NONE]] = { {{.*}} "branch-target-enforcement"="false" {{.*}} "sign-return-address"="none"
5068

5169
// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement"="true" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
@@ -61,3 +79,13 @@ void btileaf() {}
6179
// CHECK-DAG: attributes #[[#PACBKEYLEAF]] = { {{.*}} "branch-target-enforcement"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="b_key"
6280

6381
// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}}"branch-target-enforcement"="true" {{.*}} "sign-return-address"="all" "sign-return-address-key"="a_key"
82+
83+
84+
// CHECK-DAG: attributes #[[#PAUTHLR]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
85+
86+
// CHECK-DAG: attributes #[[#PAUTHLR_BKEY]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
87+
88+
// CHECK-DAG: attributes #[[#PAUTHLR_LEAF]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
89+
90+
// CHECK-DAG: attributes #[[#PAUTHLR_BTI]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="true" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
91+

clang/test/Driver/aarch64-pauth-lr.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Check the -cc1 flags for the various forms of -mbranch-protection=pac-ret+pc.
2+
3+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR
4+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+b-key 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-B-KEY
5+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+leaf 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-LEAF
6+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+bti 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-BTI
7+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+leaf+b-key+bti 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-LEAF-B-KEY-BTI
8+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc -march=armv9.5-a 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR
9+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+b-key -march=armv9.5-a 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-B-KEY
10+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+leaf -march=armv9.5-a 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-LEAF
11+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+bti -march=armv9.5-a 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-BTI
12+
// RUN: %clang -target aarch64-arm-none-eabi -c %s -### -mbranch-protection=pac-ret+pc+leaf+b-key+bti -march=armv9.5-a 2>&1 | FileCheck %s --check-prefixes=PAUTH-LR-LEAF-B-KEY-BTI
13+
14+
// PAUTH-LR: "-msign-return-address=non-leaf" "-msign-return-address-key=a_key" "-mbranch-protection-pauth-lr"
15+
// PAUTH-LR-B-KEY: "-msign-return-address=non-leaf" "-msign-return-address-key=b_key" "-mbranch-protection-pauth-lr"
16+
// PAUTH-LR-LEAF: "-msign-return-address=all" "-msign-return-address-key=a_key" "-mbranch-protection-pauth-lr"
17+
// PAUTH-LR-BTI: "-msign-return-address=non-leaf" "-msign-return-address-key=a_key" "-mbranch-protection-pauth-lr"
18+
// PAUTH-LR-LEAF-B-KEY-BTI: "-msign-return-address=all" "-msign-return-address-key=b_key" "-mbranch-protection-pauth-lr" "-mbranch-target-enforce"
19+
20+
// NOT-PAUTH-LR: "-mbranch-target-enforce"
21+
// NOT-PAUTH-LR-B-KEY: "-mbranch-target-enforce"
22+
// NOT-PAUTH-LR-LEAF: "-mbranch-target-enforce"
23+
// NOT-PAUTH-LR-BTI: "-mbranch-target-enforce"

0 commit comments

Comments
 (0)