[llvm] [Verifier] Check function attributes related to branch protection (NFC) (PR #70565)
Momchil Velikov via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 30 03:42:39 PDT 2023
https://github.com/momchil-velikov updated https://github.com/llvm/llvm-project/pull/70565
>From 4c3297ffc83ce337afb644b8be5763b6363fc486 Mon Sep 17 00:00:00 2001
From: Momchil Velikov <momchil.velikov at arm.com>
Date: Sat, 28 Oct 2023 15:01:36 +0100
Subject: [PATCH 1/3] [Verifier] Check function attributes related to branch
protection (NFC)
---
llvm/lib/IR/Verifier.cpp | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 396af600b8dab29..863d77656478fd8 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2231,6 +2231,27 @@ 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 (Attrs.hasFnAttr("sign-return-adress")) {
+ StringRef S = Attrs.getFnAttr("sign-return-adress").getValueAsString();
+ if (S != "none" && S != "all" && S != "non-leaf")
+ CheckFailed("invalid value for 'sign-return-adress' attribute: " + S, V);
+ }
+
+ if (Attrs.hasFnAttr("sign-return-adress-key")) {
+ StringRef S = Attrs.getFnAttr("sign-return-adress-key").getValueAsString();
+ if (!S.equals_insensitive("a_key") && !S.equals_insensitive("b_key"))
+ CheckFailed("invalid value for 'sign-return-adress-key' attribute: " + S,
+ V);
+ }
+
+ if (Attrs.hasFnAttr("branch-target-enforcement")) {
+ StringRef S =
+ Attrs.getFnAttr("branch-target-enforcement").getValueAsString();
+ if (!S.equals_insensitive("true") && !S.equals_insensitive("false"))
+ CheckFailed(
+ "invalid value for 'branch-target-enforcement' attribute: " + S, V);
+ }
}
void Verifier::verifyFunctionMetadata(
>From ded25c35f0138e7bd0c5522eb9a553fa9501f1b1 Mon Sep 17 00:00:00 2001
From: Momchil Velikov <momchil.velikov at arm.com>
Date: Mon, 30 Oct 2023 09:27:08 +0000
Subject: [PATCH 2/3] Fix typos, do attribute lookup once
---
llvm/lib/IR/Verifier.cpp | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 863d77656478fd8..a966e64a0a6d6f3 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2232,22 +2232,21 @@ void Verifier::verifyFunctionAttrs(FunctionType *FT, AttributeList Attrs,
checkUnsignedBaseTenFuncAttr(Attrs, "patchable-function-entry", V);
checkUnsignedBaseTenFuncAttr(Attrs, "warn-stack-size", V);
- if (Attrs.hasFnAttr("sign-return-adress")) {
- StringRef S = Attrs.getFnAttr("sign-return-adress").getValueAsString();
+ 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-adress' attribute: " + S, V);
+ CheckFailed("invalid value for 'sign-return-address' attribute: " + S, V);
}
- if (Attrs.hasFnAttr("sign-return-adress-key")) {
- StringRef S = Attrs.getFnAttr("sign-return-adress-key").getValueAsString();
+ if (auto A = Attrs.getFnAttr("sign-return-address-key"); A.isValid()) {
+ StringRef S = A.getValueAsString();
if (!S.equals_insensitive("a_key") && !S.equals_insensitive("b_key"))
- CheckFailed("invalid value for 'sign-return-adress-key' attribute: " + S,
+ CheckFailed("invalid value for 'sign-return-address-key' attribute: " + S,
V);
}
- if (Attrs.hasFnAttr("branch-target-enforcement")) {
- StringRef S =
- Attrs.getFnAttr("branch-target-enforcement").getValueAsString();
+ if (auto A = Attrs.getFnAttr("branch-target-enforcement"); A.isValid()) {
+ StringRef S = A.getValueAsString();
if (!S.equals_insensitive("true") && !S.equals_insensitive("false"))
CheckFailed(
"invalid value for 'branch-target-enforcement' attribute: " + S, V);
>From ba2fdb44867ab833c500eabfef810d860c7aa099 Mon Sep 17 00:00:00 2001
From: Momchil Velikov <momchil.velikov at arm.com>
Date: Mon, 30 Oct 2023 10:34:26 +0000
Subject: [PATCH 3/3] Add a test
---
llvm/test/Verifier/branch-prot-attrs.ll | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 llvm/test/Verifier/branch-prot-attrs.ll
diff --git a/llvm/test/Verifier/branch-prot-attrs.ll b/llvm/test/Verifier/branch-prot-attrs.ll
new file mode 100644
index 000000000000000..123a8207cfe6b4f
--- /dev/null
+++ b/llvm/test/Verifier/branch-prot-attrs.ll
@@ -0,0 +1,13 @@
+; RUN: not llvm-as %s -o /dev/null 2>&1 | FileCheck %s
+
+define void @f() #0 {
+ ret void
+}
+
+attributes #0 = {
+; CHECK: invalid value for 'sign-return-address' attribute: loaf
+ "sign-return-address"="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" }
More information about the llvm-commits
mailing list