Skip to content

Commit e3a97df

Browse files
[Verifier] Check function attributes related to branch protection (NFC) (#70565)
1 parent ec000a6 commit e3a97df

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

clang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4159,10 +4159,10 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
41594159
if (Arg *A = Args.getLastArg(OPT_msign_return_address_key_EQ)) {
41604160
StringRef SignKey = A->getValue();
41614161
if (!SignScope.empty() && !SignKey.empty()) {
4162-
if (SignKey.equals_insensitive("a_key"))
4162+
if (SignKey == "a_key")
41634163
Opts.setSignReturnAddressKey(
41644164
LangOptions::SignReturnAddressKeyKind::AKey);
4165-
else if (SignKey.equals_insensitive("b_key"))
4165+
else if (SignKey == "b_key")
41664166
Opts.setSignReturnAddressKey(
41674167
LangOptions::SignReturnAddressKeyKind::BKey);
41684168
else

llvm/lib/IR/Verifier.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,26 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
22412241
checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V);
22422242
checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
22432243
checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);
2244+
2245+
if (auto A = Attrs.getFnAttr("sign-return-address"); A.isValid()) {
2246+
StringRef S = A.getValueAsString();
2247+
if (S != "none" && S != "all" && S != "non-leaf")
2248+
CheckFailed("invalid value for 'sign-return-address' attribute: " + S, V);
2249+
}
2250+
2251+
if (auto A = Attrs.getFnAttr("sign-return-address-key"); A.isValid()) {
2252+
StringRef S = A.getValueAsString();
2253+
if (S != "a_key" && S != "b_key")
2254+
CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
2255+
V);
2256+
}
2257+
2258+
if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
2259+
StringRef S = A.getValueAsString();
2260+
if (S != "true" && S != "false")
2261+
CheckFailed(
2262+
"invalid value for 'branch-target-enforcement' attribute: " + S, V);
2263+
}
22442264
}
22452265

22462266
void Verifier::verifyFunctionMetadata(

llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {
7878

7979
const StringRef Key =
8080
F.getFnAttribute("sign-return-address-key").getValueAsString();
81-
assert(Key.equals_insensitive("a_key") || Key.equals_insensitive("b_key"));
82-
return Key.equals_insensitive("b_key");
81+
assert(Key == "a_key" || Key == "b_key");
82+
return Key == "b_key";
8383
}
8484

8585
AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
@@ -100,9 +100,8 @@ AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
100100
} else {
101101
const StringRef BTIEnable =
102102
F.getFnAttribute("branch-target-enforcement").getValueAsString();
103-
assert(BTIEnable.equals_insensitive("true") ||
104-
BTIEnable.equals_insensitive("false"));
105-
BranchTargetEnforcement = BTIEnable.equals_insensitive("true");
103+
assert(BTIEnable == "true" || BTIEnable == "false");
104+
BranchTargetEnforcement = BTIEnable == "true";
106105
}
107106

108107
// The default stack probe size is 4096 if the function has no

llvm/lib/Target/ARM/ARMMachineFunctionInfo.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ static bool GetBranchTargetEnforcement(const Function &F,
2727

2828
const StringRef BTIEnable =
2929
F.getFnAttribute("branch-target-enforcement").getValueAsString();
30-
assert(BTIEnable.equals_insensitive("true") ||
31-
BTIEnable.equals_insensitive("false"));
32-
return BTIEnable.equals_insensitive("true");
30+
assert(BTIEnable == "true" || BTIEnable == "false");
31+
return BTIEnable == "true";
3332
}
3433

3534
// The pair returns values for the ARMFunctionInfo members
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
2+
3+
define void @f() #0 {
4+
ret void
5+
}
6+
7+
define void @g() #1 {
8+
ret void
9+
}
10+
11+
attributes #0 = {
12+
; CHECK: invalid value for 'sign-return-address' attribute: non-loaf
13+
"sign-return-address"="non-loaf"
14+
; CHECK: invalid value for 'sign-return-address-key' attribute: bad-mkey
15+
"sign-return-address-key"="bad-mkey"
16+
; CHECK: invalid value for 'branch-target-enforcement' attribute: yes-please
17+
"branch-target-enforcement"="yes-please" }
18+
19+
attributes #1 = {
20+
; CHECK: invalid value for 'sign-return-address' attribute: All
21+
"sign-return-address"="All"
22+
; CHECK: invalid value for 'sign-return-address-key' attribute: B_Key
23+
"sign-return-address-key"="B_Key"
24+
; CHECK: invalid value for 'branch-target-enforcement' attribute: True
25+
"branch-target-enforcement"="True" }

0 commit comments

Comments
 (0)