[llvm] [ASAN] Add "asan_instrumented" llvm ir attribute to identify AddressSanitizer instrumented globals (PR #68865)

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 2 02:04:20 PDT 2023


https://github.com/skc7 updated https://github.com/llvm/llvm-project/pull/68865

>From 5c8a5b3bec10c3c04b6bc039c32dfe4acb6fdf4c Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Thu, 12 Oct 2023 14:05:48 +0530
Subject: [PATCH 1/2] [ASAN] Add AsanInstrumented llvm ir attribute to identify
 asan instrumented globals

---
 llvm/include/llvm/Bitcode/LLVMBitCodes.h  | 1 +
 llvm/include/llvm/IR/Attributes.h         | 3 +++
 llvm/include/llvm/IR/Attributes.td        | 3 +++
 llvm/lib/AsmParser/LLLexer.cpp            | 1 +
 llvm/lib/AsmParser/LLParser.cpp           | 8 ++++++++
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 2 ++
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 2 ++
 llvm/lib/IR/Attributes.cpp                | 7 +++++++
 8 files changed, 27 insertions(+)

diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 2a522c517c7fa2f..1f802a45698c93b 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -718,6 +718,7 @@ enum AttributeKindCodes {
   ATTR_KIND_NOFPCLASS = 87,
   ATTR_KIND_OPTIMIZE_FOR_DEBUGGING = 88,
   ATTR_KIND_WRITABLE = 89,
+  ATTR_KIND_ASAN_INSTRUMENTED = 90,
 };
 
 enum ComdatSelectionKindCodes {
diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index db33b5400471685..eb02158f15a1254 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -251,6 +251,9 @@ class Attribute {
   /// Return the FPClassTest for nofpclass
   FPClassTest getNoFPClass() const;
 
+  /// Return if global variable is instrumented by AddrSanitizer.
+  bool isAsanInstrumented() const;
+
   /// The Attribute is converted to a string of equivalent mnemonic. This
   /// is, presumably, for writing out the mnemonics for the assembly writer.
   std::string getAsString(bool InAttrGrp = false) const;
diff --git a/llvm/include/llvm/IR/Attributes.td b/llvm/include/llvm/IR/Attributes.td
index 693ffbbf32e0e33..29af209a3632a62 100644
--- a/llvm/include/llvm/IR/Attributes.td
+++ b/llvm/include/llvm/IR/Attributes.td
@@ -276,6 +276,9 @@ def SanitizeHWAddress : EnumAttr<"sanitize_hwaddress", [FnAttr]>;
 /// MemTagSanitizer is on.
 def SanitizeMemTag : EnumAttr<"sanitize_memtag", [FnAttr]>;
 
+/// Attribute to identify global variables instrumented by AddrSanitizer.
+def AsanInstrumented : EnumAttr<"asan_instrumented", []>;
+
 /// Speculative Load Hardening is enabled.
 ///
 /// Note that this uses the default compatibility (always compatible during
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index 284a4c64c6793ef..96493a474892801 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -583,6 +583,7 @@ lltok::Kind LLLexer::LexIdentifier() {
   KEYWORD(no_sanitize_address);
   KEYWORD(no_sanitize_hwaddress);
   KEYWORD(sanitize_address_dyninit);
+  KEYWORD(asan_instrumented);
 
   KEYWORD(ccc);
   KEYWORD(fastcc);
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 42f306a99d5eefa..cbd21934d37d6fd 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -1525,6 +1525,14 @@ bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B,
       continue;
     }
 
+    if (Token == lltok::kw_asan_instrumented) {
+      if (parseToken(lltok::kw_asan_instrumented,
+                     "expected 'asan_instrumented'"))
+        return true;
+      B.addAttribute(Attribute::AsanInstrumented);
+      continue;
+    }
+
     SMLoc Loc = Lex.getLoc();
     if (Token == lltok::kw_builtin)
       BuiltinLoc = Loc;
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 747968b51407cd9..8602106c348714c 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1982,6 +1982,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
     return Attribute::NullPointerIsValid;
   case bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING:
     return Attribute::OptimizeForDebugging;
+  case bitc::ATTR_KIND_ASAN_INSTRUMENTED:
+    return Attribute::AsanInstrumented;
   case bitc::ATTR_KIND_OPT_FOR_FUZZING:
     return Attribute::OptForFuzzing;
   case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 5e3341d4de13323..9520bb6b024452e 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -792,6 +792,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
     return bitc::ATTR_KIND_SANITIZE_THREAD;
   case Attribute::SanitizeMemory:
     return bitc::ATTR_KIND_SANITIZE_MEMORY;
+  case Attribute::AsanInstrumented:
+    return bitc::ATTR_KIND_ASAN_INSTRUMENTED;
   case Attribute::SpeculativeLoadHardening:
     return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING;
   case Attribute::SwiftError:
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 10a74ed68a392c6..91080f207df1b3d 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -408,6 +408,10 @@ FPClassTest Attribute::getNoFPClass() const {
   return static_cast<FPClassTest>(pImpl->getValueAsInt());
 }
 
+bool Attribute::isAsanInstrumented() const {
+  return hasAttribute(Attribute::AsanInstrumented);
+}
+
 static const char *getModRefStr(ModRefInfo MR) {
   switch (MR) {
   case ModRefInfo::NoModRef:
@@ -562,6 +566,9 @@ std::string Attribute::getAsString(bool InAttrGrp) const {
     return Result;
   }
 
+  if (hasAttribute(Attribute::AsanInstrumented))
+    return "asan_instrumented";
+
   // Convert target-dependent attributes to strings of the form:
   //
   //   "kind"

>From 22c90ff13136ff3db069ab262a905f015407350f Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Thu, 2 Nov 2023 12:36:11 +0530
Subject: [PATCH 2/2] [ASAN] Add sanitized_padded_global llvm ir attribute to
 identify sanitizer instrumented globals

---
 llvm/include/llvm/Bitcode/LLVMBitCodes.h         | 2 +-
 llvm/include/llvm/IR/Attributes.h                | 2 +-
 llvm/include/llvm/IR/Attributes.td               | 4 ++--
 llvm/lib/AsmParser/LLLexer.cpp                   | 2 +-
 llvm/lib/AsmParser/LLParser.cpp                  | 8 ++++----
 llvm/lib/Bitcode/Reader/BitcodeReader.cpp        | 4 ++--
 llvm/lib/Bitcode/Writer/BitcodeWriter.cpp        | 4 ++--
 llvm/lib/IR/Attributes.cpp                       | 8 ++++----
 llvm/test/Assembler/globalvariable-attributes.ll | 5 ++++-
 9 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index 1f802a45698c93b..a15aa6344aa5608 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -718,7 +718,7 @@ enum AttributeKindCodes {
   ATTR_KIND_NOFPCLASS = 87,
   ATTR_KIND_OPTIMIZE_FOR_DEBUGGING = 88,
   ATTR_KIND_WRITABLE = 89,
-  ATTR_KIND_ASAN_INSTRUMENTED = 90,
+  ATTR_KIND_SANITIZED_PADDED_GLOBAL = 90,
 };
 
 enum ComdatSelectionKindCodes {
diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index eb02158f15a1254..9223577dc7d60f1 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -252,7 +252,7 @@ class Attribute {
   FPClassTest getNoFPClass() const;
 
   /// Return if global variable is instrumented by AddrSanitizer.
-  bool isAsanInstrumented() const;
+  bool isSanitizedPaddedGlobal() const;
 
   /// The Attribute is converted to a string of equivalent mnemonic. This
   /// is, presumably, for writing out the mnemonics for the assembly writer.
diff --git a/llvm/include/llvm/IR/Attributes.td b/llvm/include/llvm/IR/Attributes.td
index 29af209a3632a62..19870fec4795fdc 100644
--- a/llvm/include/llvm/IR/Attributes.td
+++ b/llvm/include/llvm/IR/Attributes.td
@@ -276,8 +276,8 @@ def SanitizeHWAddress : EnumAttr<"sanitize_hwaddress", [FnAttr]>;
 /// MemTagSanitizer is on.
 def SanitizeMemTag : EnumAttr<"sanitize_memtag", [FnAttr]>;
 
-/// Attribute to identify global variables instrumented by AddrSanitizer.
-def AsanInstrumented : EnumAttr<"asan_instrumented", []>;
+/// Attribute to identify global variables instrumented by Sanitizers.
+def SanitizedPaddedGlobal : EnumAttr<"sanitized_padded_global", []>;
 
 /// Speculative Load Hardening is enabled.
 ///
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index 96493a474892801..e516cfa99cfddcb 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -583,7 +583,7 @@ lltok::Kind LLLexer::LexIdentifier() {
   KEYWORD(no_sanitize_address);
   KEYWORD(no_sanitize_hwaddress);
   KEYWORD(sanitize_address_dyninit);
-  KEYWORD(asan_instrumented);
+  KEYWORD(sanitized_padded_global);
 
   KEYWORD(ccc);
   KEYWORD(fastcc);
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index cbd21934d37d6fd..d2858f92b555492 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -1525,11 +1525,11 @@ bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B,
       continue;
     }
 
-    if (Token == lltok::kw_asan_instrumented) {
-      if (parseToken(lltok::kw_asan_instrumented,
-                     "expected 'asan_instrumented'"))
+    if (Token == lltok::kw_sanitized_padded_global) {
+      if (parseToken(lltok::kw_sanitized_padded_global,
+                     "expected 'sanitized_padded_global'"))
         return true;
-      B.addAttribute(Attribute::AsanInstrumented);
+      B.addAttribute(Attribute::SanitizedPaddedGlobal);
       continue;
     }
 
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 8602106c348714c..616cc6e7ab8a0e0 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1982,8 +1982,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
     return Attribute::NullPointerIsValid;
   case bitc::ATTR_KIND_OPTIMIZE_FOR_DEBUGGING:
     return Attribute::OptimizeForDebugging;
-  case bitc::ATTR_KIND_ASAN_INSTRUMENTED:
-    return Attribute::AsanInstrumented;
+  case bitc::ATTR_KIND_SANITIZED_PADDED_GLOBAL:
+    return Attribute::SanitizedPaddedGlobal;
   case bitc::ATTR_KIND_OPT_FOR_FUZZING:
     return Attribute::OptForFuzzing;
   case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 9520bb6b024452e..d907eb5767ff55b 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -792,8 +792,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
     return bitc::ATTR_KIND_SANITIZE_THREAD;
   case Attribute::SanitizeMemory:
     return bitc::ATTR_KIND_SANITIZE_MEMORY;
-  case Attribute::AsanInstrumented:
-    return bitc::ATTR_KIND_ASAN_INSTRUMENTED;
+  case Attribute::SanitizedPaddedGlobal:
+    return bitc::ATTR_KIND_SANITIZED_PADDED_GLOBAL;
   case Attribute::SpeculativeLoadHardening:
     return bitc::ATTR_KIND_SPECULATIVE_LOAD_HARDENING;
   case Attribute::SwiftError:
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 91080f207df1b3d..1f0510c95f13674 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -408,8 +408,8 @@ FPClassTest Attribute::getNoFPClass() const {
   return static_cast<FPClassTest>(pImpl->getValueAsInt());
 }
 
-bool Attribute::isAsanInstrumented() const {
-  return hasAttribute(Attribute::AsanInstrumented);
+bool Attribute::isSanitizedPaddedGlobal() const {
+  return hasAttribute(Attribute::SanitizedPaddedGlobal);
 }
 
 static const char *getModRefStr(ModRefInfo MR) {
@@ -566,8 +566,8 @@ std::string Attribute::getAsString(bool InAttrGrp) const {
     return Result;
   }
 
-  if (hasAttribute(Attribute::AsanInstrumented))
-    return "asan_instrumented";
+  if (hasAttribute(Attribute::SanitizedPaddedGlobal))
+    return "sanitized_padded_global";
 
   // Convert target-dependent attributes to strings of the form:
   //
diff --git a/llvm/test/Assembler/globalvariable-attributes.ll b/llvm/test/Assembler/globalvariable-attributes.ll
index 544f9bdb270e99a..071edd2904f5303 100644
--- a/llvm/test/Assembler/globalvariable-attributes.ll
+++ b/llvm/test/Assembler/globalvariable-attributes.ll
@@ -9,8 +9,10 @@
 @g7 = global i32 2, sanitize_address_dyninit, align 4
 @g8 = global i32 2, sanitize_memtag, align 4
 @g9 = global i32 2, no_sanitize_address, no_sanitize_hwaddress, sanitize_memtag, align 4
+ at g10 = global i32 2 #1
 
 attributes #0 = { "string" = "value" nobuiltin norecurse }
+attributes #1 = { sanitized_padded_global }
 
 ; CHECK: @g1 = global i32 7 #0
 ; CHECK: @g2 = global i32 2, align 4 #1
@@ -21,9 +23,10 @@ attributes #0 = { "string" = "value" nobuiltin norecurse }
 ; CHECK: @g7 = global i32 2, sanitize_address_dyninit, align 4
 ; CHECK: @g8 = global i32 2, sanitize_memtag, align 4
 ; CHECK: @g9 = global i32 2, no_sanitize_address, no_sanitize_hwaddress, sanitize_memtag, align 4
+; CHECK: @g10 = global i32 2 #4
 
 ; CHECK: attributes #0 = { "key"="value" "key2"="value2" }
 ; CHECK: attributes #1 = { "key3"="value3" }
 ; CHECK: attributes #2 = { nobuiltin norecurse "string"="value" }
 ; CHECK: attributes #3 = { nobuiltin norecurse "key5"="value5" "string"="value" }
-
+; CHECK: attributes #4 = { sanitized_padded_global }



More information about the llvm-commits mailing list