Skip to content

Commit 5973486

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 d4fb50d commit 5973486

File tree

60 files changed

+277
-312
lines changed

Some content is hidden

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

60 files changed

+277
-312
lines changed

clang/include/clang/Basic/TargetInfo.h

+41-3
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"
@@ -1369,15 +1371,15 @@ class TargetInfo : public TransferrableTargetInfo,
13691371
return StringRef();
13701372
}
13711373

1372-
struct BranchProtectionInfo {
1374+
class BranchProtectionInfo {
1375+
public:
13731376
LangOptions::SignReturnAddressScopeKind SignReturnAddr;
13741377
LangOptions::SignReturnAddressKeyKind SignKey;
13751378
bool BranchTargetEnforcement;
13761379
bool BranchProtectionPAuthLR;
13771380
bool GuardedControlStack;
13781381

1379-
BranchProtectionInfo() = default;
1380-
1382+
protected:
13811383
const char *getSignReturnAddrStr() const {
13821384
switch (SignReturnAddr) {
13831385
case LangOptions::SignReturnAddressScopeKind::None:
@@ -1399,6 +1401,42 @@ class TargetInfo : public TransferrableTargetInfo,
13991401
}
14001402
llvm_unreachable("Unexpected SignReturnAddressKeyKind");
14011403
}
1404+
1405+
public:
1406+
BranchProtectionInfo() = default;
1407+
BranchProtectionInfo(const LangOptions &LangOpts) {
1408+
SignReturnAddr =
1409+
LangOpts.hasSignReturnAddress()
1410+
? (LangOpts.isSignReturnAddressScopeAll()
1411+
? LangOptions::SignReturnAddressScopeKind::All
1412+
: LangOptions::SignReturnAddressScopeKind::NonLeaf)
1413+
: LangOptions::SignReturnAddressScopeKind::None;
1414+
SignKey = LangOpts.isSignReturnAddressWithAKey()
1415+
? LangOptions::SignReturnAddressKeyKind::AKey
1416+
: LangOptions::SignReturnAddressKeyKind::BKey;
1417+
BranchTargetEnforcement = LangOpts.BranchTargetEnforcement;
1418+
BranchProtectionPAuthLR = LangOpts.BranchProtectionPAuthLR;
1419+
GuardedControlStack = LangOpts.GuardedControlStack;
1420+
}
1421+
1422+
void setFnAttributes(llvm::Function &F) {
1423+
llvm::AttrBuilder FuncAttrs(F.getContext());
1424+
setFnAttributes(FuncAttrs);
1425+
F.addFnAttrs(FuncAttrs);
1426+
}
1427+
1428+
void setFnAttributes(llvm::AttrBuilder &FuncAttrs) {
1429+
if (SignReturnAddr != LangOptions::SignReturnAddressScopeKind::None) {
1430+
FuncAttrs.addAttribute("sign-return-address", getSignReturnAddrStr());
1431+
FuncAttrs.addAttribute("sign-return-address-key", getSignKeyStr());
1432+
}
1433+
if (BranchTargetEnforcement)
1434+
FuncAttrs.addAttribute("branch-target-enforcement");
1435+
if (BranchProtectionPAuthLR)
1436+
FuncAttrs.addAttribute("branch-protection-pauth-lr");
1437+
if (GuardedControlStack)
1438+
FuncAttrs.addAttribute("guarded-control-stack");
1439+
}
14021440
};
14031441

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

clang/lib/CodeGen/Targets/AArch64.cpp

+13-30
Original file line numberDiff line numberDiff line change
@@ -116,37 +116,20 @@ class AArch64TargetCodeGenInfo : public TargetCodeGenInfo {
116116
if (!FD)
117117
return;
118118

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

152135
bool isScalarizableAsmOperand(CodeGen::CodeGenFunction &CGF,

clang/lib/CodeGen/Targets/ARM.cpp

+5-3
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

+13-13
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

+9-3
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

+1-1
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

+6-6
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

+9-4
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 is 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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// REQUIRES: arm-registered-target
2+
3+
// RUN: %clang_cc1 -triple=thumbv7m-unknown-unknown-eabi -msign-return-address=non-leaf %s -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 -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 -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 -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 -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 -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 -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 -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 -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: attributes #[[#ATTR]] = { {{.*}} "sign-return-address"="non-leaf"
21+
// BTE: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"
22+
// ALL: attributes #[[#ATTR]] = { {{.*}} "branch-target-enforcement"{{.*}} "sign-return-address"="all"

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

+1-11
Original file line numberDiff line numberDiff line change
@@ -11773,17 +11773,7 @@ void SelectionDAGBuilder::lowerWorkItem(SwitchWorkListItem W, Value *Cond,
1177311773
// table branch.
1177411774
if (FallthroughUnreachable) {
1177511775
Function &CurFunc = CurMF->getFunction();
11776-
bool HasBranchTargetEnforcement = false;
11777-
if (CurFunc.hasFnAttribute("branch-target-enforcement")) {
11778-
HasBranchTargetEnforcement =
11779-
CurFunc.getFnAttribute("branch-target-enforcement")
11780-
.getValueAsBool();
11781-
} else {
11782-
HasBranchTargetEnforcement =
11783-
CurMF->getMMI().getModule()->getModuleFlag(
11784-
"branch-target-enforcement");
11785-
}
11786-
if (!HasBranchTargetEnforcement)
11776+
if (!CurFunc.hasFnAttribute("branch-target-enforcement"))
1178711777
JTH->FallthroughUnreachable = true;
1178811778
}
1178911779

llvm/lib/IR/Verifier.cpp

+19-2
Original file line numberDiff line numberDiff line change
@@ -2314,7 +2314,7 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
23142314

23152315
if (auto A = Attrs.getFnAttr("sign-return-address"); A.isValid()) {
23162316
StringRef S = A.getValueAsString();
2317-
if (S != "none" && S != "all" && S != "non-leaf")
2317+
if (S != "all" && S != "non-leaf")
23182318
CheckFailed("invalid value for 'sign-return-address' attribute: " + S, V);
23192319
}
23202320

@@ -2323,15 +2323,32 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
23232323
if (S != "a_key" && S != "b_key")
23242324
CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
23252325
V);
2326+
if (auto AA = Attrs.getFnAttr("sign-return-address"); !AA.isValid()) {
2327+
CheckFailed("invalid value for 'sign-return-address-key' present without sign-return-address ");
2328+
}
23262329
}
23272330

23282331
if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
23292332
StringRef S = A.getValueAsString();
2330-
if (S != "true" && S != "false")
2333+
if (S != "")
23312334
CheckFailed(
23322335
"invalid value for 'branch-target-enforcement' attribute: " + S, V);
23332336
}
23342337

2338+
if (auto A = Attrs.getFnAttr("branch-protection-pauth-lr"); A.isValid()) {
2339+
StringRef S = A.getValueAsString();
2340+
if (S != "")
2341+
CheckFailed(
2342+
"invalid value for 'branch-protection-pauth-lr' attribute: " + S, V);
2343+
}
2344+
2345+
if (auto A = Attrs.getFnAttr("guarded-control-stack"); A.isValid()) {
2346+
StringRef S = A.getValueAsString();
2347+
if (S != "")
2348+
CheckFailed(
2349+
"invalid value for 'guarded-control-stack' attribute: " + S, V);
2350+
}
2351+
23352352
if (auto A = Attrs.getFnAttr("vector-function-abi-variant"); A.isValid()) {
23362353
StringRef S = A.getValueAsString();
23372354
const std::optional<VFInfo> Info = VFABI::tryDemangleForVFABI(S, FT);

llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp

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

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

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

271271
if (Flags == 0)

0 commit comments

Comments
 (0)