Skip to content

Commit f459bf1

Browse files
Emit attributes for functions always.
Branch protection, sign return address, guarded control stack flags are only emitted as module flags if not specified per function. The inliner might inline functions with different set of flags as it doesn't see the flags. In case of LTO build the module flags get merged with the `min` rule which means if one of the modules is not build with PAC/BTI then the features will be turned off on all functions due to the functions takes the branch-protection and sign-return-address features from the module flags. The sign-return-address is function level option therefore it is expected functions from files that are compiled with -mbranch-protection=pac-ret to be protected but in LTO case this might not happen. This patch adds the flags to functions in case of an LTO build therefore they don't need to rely on the module flag.
1 parent e33db24 commit f459bf1

File tree

61 files changed

+293
-317
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+293
-317
lines changed

clang/include/clang/Basic/TargetInfo.h

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
#include "llvm/ADT/StringRef.h"
3333
#include "llvm/ADT/StringSet.h"
3434
#include "llvm/Frontend/OpenMP/OMPGridValues.h"
35+
#include "llvm/IR/Attributes.h"
3536
#include "llvm/IR/DerivedTypes.h"
37+
#include "llvm/IR/Function.h"
3638
#include "llvm/Support/DataTypes.h"
3739
#include "llvm/Support/Error.h"
3840
#include "llvm/Support/VersionTuple.h"
@@ -1405,15 +1407,15 @@ class TargetInfo : public TransferrableTargetInfo,
14051407
return StringRef();
14061408
}
14071409

1408-
struct BranchProtectionInfo {
1410+
class BranchProtectionInfo {
1411+
public:
14091412
LangOptions::SignReturnAddressScopeKind SignReturnAddr;
14101413
LangOptions::SignReturnAddressKeyKind SignKey;
14111414
bool BranchTargetEnforcement;
14121415
bool BranchProtectionPAuthLR;
14131416
bool GuardedControlStack;
14141417

1415-
BranchProtectionInfo() = default;
1416-
1418+
protected:
14171419
const char *getSignReturnAddrStr() const {
14181420
switch (SignReturnAddr) {
14191421
case LangOptions::SignReturnAddressScopeKind::None:
@@ -1435,6 +1437,42 @@ class TargetInfo : public TransferrableTargetInfo,
14351437
}
14361438
llvm_unreachable("Unexpected SignReturnAddressKeyKind");
14371439
}
1440+
1441+
public:
1442+
BranchProtectionInfo() = default;
1443+
BranchProtectionInfo(const LangOptions &LangOpts) {
1444+
SignReturnAddr =
1445+
LangOpts.hasSignReturnAddress()
1446+
? (LangOpts.isSignReturnAddressScopeAll()
1447+
? LangOptions::SignReturnAddressScopeKind::All
1448+
: LangOptions::SignReturnAddressScopeKind::NonLeaf)
1449+
: LangOptions::SignReturnAddressScopeKind::None;
1450+
SignKey = LangOpts.isSignReturnAddressWithAKey()
1451+
? LangOptions::SignReturnAddressKeyKind::AKey
1452+
: LangOptions::SignReturnAddressKeyKind::BKey;
1453+
BranchTargetEnforcement = LangOpts.BranchTargetEnforcement;
1454+
BranchProtectionPAuthLR = LangOpts.BranchProtectionPAuthLR;
1455+
GuardedControlStack = LangOpts.GuardedControlStack;
1456+
}
1457+
1458+
void setFnAttributes(llvm::Function &F) {
1459+
llvm::AttrBuilder FuncAttrs(F.getContext());
1460+
setFnAttributes(FuncAttrs);
1461+
F.addFnAttrs(FuncAttrs);
1462+
}
1463+
1464+
void setFnAttributes(llvm::AttrBuilder &FuncAttrs) {
1465+
if (SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
1466+
FuncAttrs.addAttribute("sign-return-address", getSignReturnAddrStr());
1467+
FuncAttrs.addAttribute("sign-return-address-key", getSignKeyStr());
1468+
}
1469+
if (BranchTargetEnforcement)
1470+
FuncAttrs.addAttribute("branch-target-enforcement");
1471+
if (BranchProtectionPAuthLR)
1472+
FuncAttrs.addAttribute("branch-protection-pauth-lr");
1473+
if (GuardedControlStack)
1474+
FuncAttrs.addAttribute("guarded-control-stack");
1475+
}
14381476
};
14391477

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

clang/lib/CodeGen/Targets/AArch64.cpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -119,37 +119,20 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
119119
if (!FD)
120120
return;
121121

122-
const auto *TA = FD->getAttr<TargetAttr>();
123-
if (TA == nullptr)
124-
return;
125-
126-
ParsedTargetAttr Attr =
127-
CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
128-
if (Attr.BranchProtection.empty())
129-
return;
130-
131-
TargetInfo::BranchProtectionInfo BPI;
132-
StringRef Error;
133-
(void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
134-
Attr.CPU, BPI, Error);
135-
assert(Error.empty());
136-
137-
auto *Fn = cast<llvm::Function>(GV);
138-
Fn->addFnAttr("sign-return-address", BPI.getSignReturnAddrStr());
139-
140-
if (BPI.SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
141-
Fn->addFnAttr("sign-return-address-key",
142-
BPI.SignKey == LangOptions::SignReturnAddressKeyKind::AKey
143-
? "a_key"
144-
: "b_key");
122+
TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
123+
124+
if (const auto *TA = FD->getAttr<TargetAttr>()) {
125+
ParsedTargetAttr Attr =
126+
CGM.getTarget().parseTargetAttr(TA->getFeaturesStr());
127+
if (!Attr.BranchProtection.empty()) {
128+
StringRef Error;
129+
(void)CGM.getTarget().validateBranchProtection(Attr.BranchProtection,
130+
Attr.CPU, BPI, Error);
131+
assert(Error.empty());
132+
}
145133
}
146-
147-
Fn->addFnAttr("branch-target-enforcement",
148-
BPI.BranchTargetEnforcement ? "true" : "false");
149-
Fn->addFnAttr("branch-protection-pauth-lr",
150-
BPI.BranchProtectionPAuthLR ? "true" : "false");
151-
Fn->addFnAttr("guarded-control-stack",
152-
BPI.GuardedControlStack ? "true" : "false");
134+
auto *Fn = cast<llvm::Function>(GV);
135+
BPI.setFnAttributes(*Fn);
153136
}
154137

155138
bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,

clang/lib/CodeGen/Targets/ARM.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
152152
diag::warn_target_unsupported_branch_protection_attribute)
153153
<< Arch;
154154
} else {
155-
Fn->addFnAttr("sign-return-address", BPI.getSignReturnAddrStr());
156-
Fn->addFnAttr("branch-target-enforcement",
157-
BPI.BranchTargetEnforcement ? "true" : "false");
155+
BPI.setFnAttributes(*Fn);
158156
}
159157
} else if (CGM.getLangOpts().BranchTargetEnforcement ||
160158
CGM.getLangOpts().hasSignReturnAddress()) {
@@ -167,6 +165,10 @@ class ARMTargetCodeGenInfo : public TargetCodeGenInfo {
167165
diag::warn_target_unsupported_branch_protection_attribute)
168166
<< Attr.CPU;
169167
}
168+
} else if (CGM.getTarget().isBranchProtectionSupportedArch(
169+
CGM.getTarget().getTargetOpts().CPU)) {
170+
TargetInfo::BranchProtectionInfo BPI(CGM.getLangOpts());
171+
BPI.setFnAttributes(*Fn);
170172
}
171173

172174
const ARMInterruptAttr *Attr = FD->getAttr<ARMInterruptAttr>();

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,29 @@ __attribute__ ((target("branch-protection=gcs")))
6767
void gcs() {}
6868
// CHECK: define{{.*}} void @gcs() #[[#GCS:]]
6969

70-
// CHECK-DAG: attributes #[[#NONE]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}} "sign-return-address"="none"
70+
// CHECK-DAG: attributes #[[#NONE]] = { {{.*}}
7171

72-
// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement"="true" "guarded-control-stack"="true" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
72+
// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement" "guarded-control-stack" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
7373

74-
// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"="true" "guarded-control-stack"="false" {{.*}} "sign-return-address"="none"
74+
// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"
7575

76-
// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
76+
// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
7777

78-
// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
78+
// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "sign-return-address"="all" "sign-return-address-key"="a_key"
7979

80-
// CHECK-DAG: attributes #[[#PACBKEY]] = { {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
80+
// CHECK-DAG: attributes #[[#PACBKEY]] = { {{.*}} "sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
8181

82-
// CHECK-DAG: attributes #[[#PACBKEYLEAF]] = { {{.*}} "branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="b_key"
82+
// CHECK-DAG: attributes #[[#PACBKEYLEAF]] = { {{.*}} "sign-return-address"="all" "sign-return-address-key"="b_key"
8383

84-
// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}}"branch-target-enforcement"="true" "guarded-control-stack"="false" {{.*}} "sign-return-address"="all" "sign-return-address-key"="a_key"
84+
// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}} "branch-target-enforcement" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
8585

8686

87-
// CHECK-DAG: attributes #[[#PAUTHLR]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
87+
// CHECK-DAG: attributes #[[#PAUTHLR]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
8888

89-
// CHECK-DAG: attributes #[[#PAUTHLR_BKEY]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
89+
// CHECK-DAG: attributes #[[#PAUTHLR_BKEY]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="b_key"
9090

91-
// CHECK-DAG: attributes #[[#PAUTHLR_LEAF]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="false" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
91+
// CHECK-DAG: attributes #[[#PAUTHLR_LEAF]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"sign-return-address"="all" "sign-return-address-key"="a_key"
9292

93-
// CHECK-DAG: attributes #[[#PAUTHLR_BTI]] = { {{.*}}"branch-protection-pauth-lr"="true" {{.*}}"branch-target-enforcement"="true" "guarded-control-stack"="false" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
93+
// CHECK-DAG: attributes #[[#PAUTHLR_BTI]] = { {{.*}} "branch-protection-pauth-lr" {{.*}}"branch-target-enforcement" {{.*}}"sign-return-address"="non-leaf" "sign-return-address-key"="a_key"
9494

95-
// CHECK-DAG: attributes #[[#GCS]] = { {{.*}}"branch-target-enforcement"="false" "guarded-control-stack"="true" {{.*}} "sign-return-address"="none"
95+
// CHECK-DAG: attributes #[[#GCS]] = { {{.*}} "guarded-control-stack"

clang/test/CodeGen/aarch64-sign-return-address.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313

1414
// CHECK-LABEL: @foo() #[[#ATTR:]]
1515

16-
// CHECK-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
17-
// CHECK-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
18-
// CHECK-NOT: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
16+
// NONE-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
17+
// NONE-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
18+
// NONE-NOT: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
19+
20+
// ALL: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
21+
// PART: attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"="a_key"
22+
// B-KEY: attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"="b_key"
23+
// BTE: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
24+
1925

2026
// Check module attributes
2127

clang/test/CodeGen/aarch64-targetattr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,6 @@ void minusarch() {}
110110
// CHECK: attributes #13 = { {{.*}} "target-features"="+fp-armv8,+fullfp16,+neon,+sve,-sve2" }
111111
// CHECK: attributes #14 = { {{.*}} "target-features"="+fullfp16" }
112112
// CHECK: attributes #15 = { {{.*}} "target-cpu"="neoverse-n1" "target-features"="+aes,+bf16,+complxnum,+crc,+dotprod,+fp-armv8,+fullfp16,+i8mm,+jsconv,+lse,+neon,+pauth,+ras,+rcpc,+rdm,+sha2,+spe,+ssbs,+sve,+sve2,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8.6a,+v8a" "tune-cpu"="cortex-a710" }
113-
// CHECK: attributes #16 = { {{.*}} "branch-target-enforcement"="true" "guarded-control-stack"="true" {{.*}} "target-features"="+aes,+bf16,+complxnum,+crc,+dotprod,+fp-armv8,+fullfp16,+i8mm,+jsconv,+lse,+neon,+pauth,+ras,+rcpc,+rdm,+sha2,+spe,+ssbs,+sve,+sve2,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8.6a,+v8a" "tune-cpu"="cortex-a710" }
113+
// CHECK: attributes #16 = { {{.*}} "branch-target-enforcement" "guarded-control-stack" {{.*}} "target-features"="+aes,+bf16,+complxnum,+crc,+dotprod,+fp-armv8,+fullfp16,+i8mm,+jsconv,+lse,+neon,+pauth,+ras,+rcpc,+rdm,+sha2,+spe,+ssbs,+sve,+sve2,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8.6a,+v8a" "tune-cpu"="cortex-a710" }
114114
// CHECK: attributes #17 = { {{.*}} "target-features"="-neon" }
115115
// CHECK: attributes #18 = { {{.*}} "target-features"="-v9.3a" }

clang/test/CodeGen/arm-branch-protection-attr-1.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ __attribute__((target("branch-protection=pac-ret+leaf"))) void leaf() {}
2929
__attribute__((target("branch-protection=pac-ret+leaf+bti"))) void btileaf() {}
3030
// CHECK: define{{.*}} void @btileaf() #[[#BTIPACLEAF:]]
3131

32-
// CHECK-DAG: attributes #[[#NONE]] = { {{.*}} "branch-target-enforcement"="false" {{.*}} "sign-return-address"="none"
32+
// CHECK-DAG: attributes #[[#NONE]] = { {{.*}}
3333

34-
// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement"="true" {{.*}} "sign-return-address"="non-leaf"
34+
// CHECK-DAG: attributes #[[#STD]] = { {{.*}} "branch-target-enforcement" {{.*}} "sign-return-address"="non-leaf"
3535

36-
// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"="true" {{.*}} "sign-return-address"="none"
36+
// CHECK-DAG: attributes #[[#BTI]] = { {{.*}} "branch-target-enforcement"
3737

38-
// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "branch-target-enforcement"="false" {{.*}} "sign-return-address"="non-leaf"
38+
// CHECK-DAG: attributes #[[#PAC]] = { {{.*}} "sign-return-address"="non-leaf"
3939

40-
// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "branch-target-enforcement"="false" {{.*}}"sign-return-address"="all"
40+
// CHECK-DAG: attributes #[[#PACLEAF]] = { {{.*}} "sign-return-address"="all"
4141

42-
// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}}"branch-target-enforcement"="true" {{.*}} "sign-return-address"="all"
42+
// CHECK-DAG: attributes #[[#BTIPACLEAF]] = { {{.*}} "branch-target-enforcement" {{.*}} "sign-return-address"="all"

clang/test/CodeGen/arm-branch-protection-attr-2.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
// RUN: %clang -target arm-arm-none-eabi -march=armv8.1-m.main -S -emit-llvm -o - -mbranch-protection=pac-ret+b-key %s | FileCheck %s --check-prefix=CHECK --check-prefix=PART
66
// RUN: %clang -target arm-arm-none-eabi -march=armv8.1-m.main -S -emit-llvm -o - -mbranch-protection=bti %s | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
77

8-
// Check there are no branch protection function attributes
8+
// Check there are branch protection function attributes
99

1010
// CHECK-LABEL: @foo() #[[#ATTR:]]
1111

12-
// CHECK-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
13-
// CHECK-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
14-
// CHECK-NOT: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
12+
// NONE-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
13+
// NONE-NOT: attributes #[[#ATTR]] = { {{.*}} "sign-return-address-key"
14+
// NONE-NOT: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
15+
16+
// ALL: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"="all"
17+
// PART: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"="non-leaf"
18+
// BTE: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
19+
1520

1621
// Check module attributes
1722

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// REQUIRES: arm-registered-target
2+
3+
// RUN: %clang_cc1 -triple=thumbv7m-unknown-unknown-eabi -msign-return-address=non-leaf %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=SIGN
4+
// RUN: %clang_cc1 -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
5+
// RUN: %clang_cc1 -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce -msign-return-address=all %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=ALL
6+
7+
// RUN: %clang_cc1 -flto -triple=thumbv7m-unknown-unknown-eabi -msign-return-address=non-leaf %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=SIGN
8+
// RUN: %clang_cc1 -flto -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
9+
// RUN: %clang_cc1 -flto -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce -msign-return-address=all %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=ALL
10+
11+
// RUN: %clang_cc1 -flto=thin -triple=thumbv7m-unknown-unknown-eabi -msign-return-address=non-leaf %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=SIGN
12+
// RUN: %clang_cc1 -flto=thin -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=BTE
13+
// RUN: %clang_cc1 -flto=thin -triple=thumbv7m-unknown-unknown-eabi -mbranch-target-enforce -msign-return-address=all %s -emit-llvm -o - 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=ALL
14+
15+
void foo() {}
16+
17+
// Check there are branch protection function attributes.
18+
// CHECK-LABEL: @foo() #[[#ATTR:]]
19+
20+
// SIGN-NOT: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
21+
// SIGN: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"="non-leaf"
22+
// BTE: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"
23+
// BTE: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
24+
// ALL: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"{{.*}} "sign-return-address"="all"

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11861,17 +11861,7 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
1186111861
// table branch.
1186211862
if (FallthroughUnreachable) {
1186311863
Function &CurFunc = CurMF->getFunction();
11864-
bool HasBranchTargetEnforcement = false;
11865-
if (CurFunc.hasFnAttribute("branch-target-enforcement")) {
11866-
HasBranchTargetEnforcement =
11867-
CurFunc.getFnAttribute("branch-target-enforcement")
11868-
.getValueAsBool();
11869-
} else {
11870-
HasBranchTargetEnforcement =
11871-
CurMF->getMMI().getModule()->getModuleFlag(
11872-
"branch-target-enforcement");
11873-
}
11874-
if (!HasBranchTargetEnforcement)
11864+
if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
1187511865
JTH->FallthroughUnreachable = true;
1187611866
}
1187711867

llvm/lib/IR/Verifier.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2348,15 +2348,33 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
23482348
if (S != "a_key" && S != "b_key")
23492349
CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
23502350
V);
2351+
if (auto AA = Attrs.getFnAttr("sign-return-address"); !AA.isValid()) {
2352+
CheckFailed(
2353+
"'sign-return-address-key' present without `sign-return-address`");
2354+
}
23512355
}
23522356

23532357
if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
23542358
StringRef S = A.getValueAsString();
2355-
if (S != "true" && S != "false")
2359+
if (S != "" && S != "true" && S != "false")
23562360
CheckFailed(
23572361
"invalid value for 'branch-target-enforcement' attribute: " + S, V);
23582362
}
23592363

2364+
if (auto A = Attrs.getFnAttr("branch-protection-pauth-lr"); A.isValid()) {
2365+
StringRef S = A.getValueAsString();
2366+
if (S != "" && S != "true" && S != "false")
2367+
CheckFailed(
2368+
"invalid value for 'branch-protection-pauth-lr' attribute: " + S, V);
2369+
}
2370+
2371+
if (auto A = Attrs.getFnAttr("guarded-control-stack"); A.isValid()) {
2372+
StringRef S = A.getValueAsString();
2373+
if (S != "" && S != "true" && S != "false")
2374+
CheckFailed("invalid value for 'guarded-control-stack' attribute: " + S,
2375+
V);
2376+
}
2377+
23602378
if (auto A = Attrs.getFnAttr("vector-function-abi-variant"); A.isValid()) {
23612379
StringRef S = A.getValueAsString();
23622380
const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(S, FT);

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ void AArch64AsmPrinter::emitStartOfAsmFile(Module &M) {
256256
unsigned Flags = 0;
257257
if (const auto *BTE = mdconst::extract_or_null<ConstantInt>(
258258
M.getModuleFlag("branch-target-enforcement")))
259-
if (BTE->getZExtValue())
259+
if (!BTE->isZero())
260260
Flags |= ELF::GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
261261

262262
if (const auto *GCS = mdconst::extract_or_null<ConstantInt>(
263263
M.getModuleFlag("guarded-control-stack")))
264-
if (GCS->getZExtValue())
264+
if (!GCS->isZero())
265265
Flags |= ELF::GNU_PROPERTY_AARCH64_FEATURE_1_GCS;
266266

267267
if (const auto *Sign = mdconst::extract_or_null<ConstantInt>(
268268
M.getModuleFlag("sign-return-address")))
269-
if (Sign->getZExtValue())
269+
if (!Sign->isZero())
270270
Flags |= ELF::GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
271271

272272
uint64_t PAuthABIPlatform = -1;

0 commit comments

Comments
 (0)