[llvm] [clang] [Verifier] Check function attributes related to branch protection (NFC) (PR #70565)

2023-12-04 Thread Momchil Velikov via cfe-commits

https://github.com/momchil-velikov updated 
https://github.com/llvm/llvm-project/pull/70565

>From 66a84fffb5d1b5c34eea9ecdb83a88afb0b627ff Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Sat, 28 Oct 2023 15:01:36 +0100
Subject: [PATCH 1/4] [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 5f466581ea980..ab9cc89693bd4 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2241,6 +2241,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 224db85a1dad0416ab403ab526eae413bd0f03fd Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Mon, 30 Oct 2023 09:27:08 +
Subject: [PATCH 2/4] 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 ab9cc89693bd4..c01e52d2671cb 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2242,22 +2242,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 24b5f411e79f1173b8413b8a6ab113169fc9c0b6 Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Mon, 30 Oct 2023 10:34:26 +
Subject: [PATCH 3/4] 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 0..123a8207cfe6b
--- /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" }

>From 

[llvm] [clang] [Verifier] Check function attributes related to branch protection (NFC) (PR #70565)

2023-11-30 Thread Matt Arsenault via cfe-commits

https://github.com/arsenm approved this pull request.


https://github.com/llvm/llvm-project/pull/70565
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang] [Verifier] Check function attributes related to branch protection (NFC) (PR #70565)

2023-11-11 Thread Momchil Velikov via cfe-commits

momchil-velikov wrote:

Ping?

https://github.com/llvm/llvm-project/pull/70565
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[llvm] [clang] [Verifier] Check function attributes related to branch protection (NFC) (PR #70565)

2023-11-11 Thread Momchil Velikov via cfe-commits

https://github.com/momchil-velikov updated 
https://github.com/llvm/llvm-project/pull/70565

>From 0fb3e4e96d9377e65d1c794fe0b648ff835748b9 Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Sat, 28 Oct 2023 15:01:36 +0100
Subject: [PATCH 1/4] [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 b1d1075285c2210..58f7f674a3405a3 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2248,6 +2248,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 a8d2edfc0bdb59f319c0ab11aa0b182750dcdfca Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Mon, 30 Oct 2023 09:27:08 +
Subject: [PATCH 2/4] 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 58f7f674a3405a3..7a259833808aafd 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2249,22 +2249,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 74ad1067e56329bc3aa7c0315f6fefed71ec294c Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Mon, 30 Oct 2023 10:34:26 +
Subject: [PATCH 3/4] 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 000..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" }

>From 

[llvm] [clang] [Verifier] Check function attributes related to branch protection (NFC) (PR #70565)

2023-11-04 Thread Momchil Velikov via cfe-commits

https://github.com/momchil-velikov updated 
https://github.com/llvm/llvm-project/pull/70565

>From eb47903ad47f4a9e833948424a1c88bc2c72090e Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Sat, 28 Oct 2023 15:01:36 +0100
Subject: [PATCH 1/4] [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 d3db7a16dc0d607..a65c5abb823011f 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2244,6 +2244,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 e518faffb9ec822c6809a6b8e731425c3212a7eb Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Mon, 30 Oct 2023 09:27:08 +
Subject: [PATCH 2/4] 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 a65c5abb823011f..453a51b859d1ebe 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -2245,22 +2245,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 a3aeeb8ad28de3c102180d82d86a0d3aed760d00 Mon Sep 17 00:00:00 2001
From: Momchil Velikov 
Date: Mon, 30 Oct 2023 10:34:26 +
Subject: [PATCH 3/4] 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 000..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" }

>From