Skip to content

Commit

Permalink
[Verifier] Check function attributes related to branch protection (NF…
Browse files Browse the repository at this point in the history
…C) (#70565)
  • Loading branch information
momchil-velikov committed Dec 4, 2023
1 parent ec000a6 commit e3a97df
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
4 changes: 2 additions & 2 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4159,10 +4159,10 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args,
if (Arg *A = Args.getLastArg(OPT_msign_return_address_key_EQ)) {
StringRef SignKey = A->getValue();
if (!SignScope.empty() && !SignKey.empty()) {
if (SignKey.equals_insensitive("a_key"))
if (SignKey == "a_key")
Opts.setSignReturnAddressKey(
LangOptions::SignReturnAddressKeyKind::AKey);
else if (SignKey.equals_insensitive("b_key"))
else if (SignKey == "b_key")
Opts.setSignReturnAddressKey(
LangOptions::SignReturnAddressKeyKind::BKey);
else
Expand Down
20 changes: 20 additions & 0 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2241,6 +2241,26 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-prefix", V);
checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);

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

if (auto A = Attrs.getFnAttr("sign-return-address-key"); A.isValid()) {
StringRef S = A.getValueAsString();
if (S != "a_key" && S != "b_key")
CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
V);
}

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

void Verifier::verifyFunctionMetadata(
Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/Target/AArch64/AArch64MachineFunctionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ static bool ShouldSignWithBKey(const Function &F, const AArch64Subtarget &STI) {

const StringRef Key =
F.getFnAttribute("sign-return-address-key").getValueAsString();
assert(Key.equals_insensitive("a_key") || Key.equals_insensitive("b_key"));
return Key.equals_insensitive("b_key");
assert(Key == "a_key" || Key == "b_key");
return Key == "b_key";
}

AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
Expand All @@ -100,9 +100,8 @@ AArch64FunctionInfo::AArch64FunctionInfo(const Function &F,
} else {
const StringRef BTIEnable =
F.getFnAttribute("branch-target-enforcement").getValueAsString();
assert(BTIEnable.equals_insensitive("true") ||
BTIEnable.equals_insensitive("false"));
BranchTargetEnforcement = BTIEnable.equals_insensitive("true");
assert(BTIEnable == "true" || BTIEnable == "false");
BranchTargetEnforcement = BTIEnable == "true";
}

// The default stack probe size is 4096 if the function has no
Expand Down
5 changes: 2 additions & 3 deletions llvm/lib/Target/ARM/ARMMachineFunctionInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ static bool GetBranchTargetEnforcement(const Function &F,

const StringRef BTIEnable =
F.getFnAttribute("branch-target-enforcement").getValueAsString();
assert(BTIEnable.equals_insensitive("true") ||
BTIEnable.equals_insensitive("false"));
return BTIEnable.equals_insensitive("true");
assert(BTIEnable == "true" || BTIEnable == "false");
return BTIEnable == "true";
}

// The pair returns values for the ARMFunctionInfo members
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/Verifier/branch-prot-attrs.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s

define void @f() #0 {
ret void
}

define void @g() #1 {
ret void
}

attributes #0 = {
; CHECK: invalid value for 'sign-return-address' attribute: non-loaf
"sign-return-address"="non-loaf"
; CHECK: invalid value for 'sign-return-address-key' attribute: bad-mkey
"sign-return-address-key"="bad-mkey"
; CHECK: invalid value for 'branch-target-enforcement' attribute: yes-please
"branch-target-enforcement"="yes-please" }

attributes #1 = {
; CHECK: invalid value for 'sign-return-address' attribute: All
"sign-return-address"="All"
; CHECK: invalid value for 'sign-return-address-key' attribute: B_Key
"sign-return-address-key"="B_Key"
; CHECK: invalid value for 'branch-target-enforcement' attribute: True
"branch-target-enforcement"="True" }

0 comments on commit e3a97df

Please sign in to comment.