[llvm] Fix globals being wrongly tagged after global optimization step (PR #132764)

via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 24 08:57:30 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: TarcĂ­sio Fischer (tarcisiofischer)

<details>
<summary>Changes</summary>

Global tagging for MTE is currently only supported for non-RO data. Most cases are already handled, but some global variables are late marked as RO, and the SanitizerMetadata must be updated.

---
Full diff: https://github.com/llvm/llvm-project/pull/132764.diff


3 Files Affected:

- (modified) llvm/include/llvm/IR/GlobalValue.h (+1) 
- (modified) llvm/lib/IR/Globals.cpp (+10) 
- (modified) llvm/lib/Transforms/IPO/GlobalOpt.cpp (+4-1) 


``````````diff
diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h
index 2176e2c2cfbfc..b6dd349dd7889 100644
--- a/llvm/include/llvm/IR/GlobalValue.h
+++ b/llvm/include/llvm/IR/GlobalValue.h
@@ -362,6 +362,7 @@ class GlobalValue : public Constant {
   void setSanitizerMetadata(SanitizerMetadata Meta);
   void removeSanitizerMetadata();
   void setNoSanitizeMetadata();
+  void disableSanitizerMetadataGlobalTagging();
 
   bool isTagged() const {
     return hasSanitizerMetadata() && getSanitizerMetadata().Memtag;
diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 8ca44719a3f94..8ed46c1d99c25 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -266,6 +266,16 @@ void GlobalValue::setNoSanitizeMetadata() {
   setSanitizerMetadata(Meta);
 }
 
+void GlobalValue::disableSanitizerMetadataGlobalTagging() {
+  if (!isTagged()) {
+    return;
+  }
+
+  auto MD = getSanitizerMetadata();
+  MD.Memtag = false;
+  setSanitizerMetadata(MD);
+}
+
 StringRef GlobalObject::getSectionImpl() const {
   assert(hasSection());
   return getContext().pImpl->GlobalObjectSections[this];
diff --git a/llvm/lib/Transforms/IPO/GlobalOpt.cpp b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
index 2d046f09f1b2b..cf2f0f7393531 100644
--- a/llvm/lib/Transforms/IPO/GlobalOpt.cpp
+++ b/llvm/lib/Transforms/IPO/GlobalOpt.cpp
@@ -1525,6 +1525,7 @@ processInternalGlobal(GlobalVariable *GV, const GlobalStatus &GS,
     if (GS.Ordering == AtomicOrdering::NotAtomic) {
       assert(!GV->isConstant() && "Expected a non-constant global");
       GV->setConstant(true);
+      GV->disableSanitizerMetadataGlobalTagging();
       Changed = true;
     }
 
@@ -2257,8 +2258,10 @@ static bool EvaluateStaticConstructor(Function *F, const DataLayout &DL,
                       << " stores.\n");
     for (const auto &Pair : NewInitializers)
       Pair.first->setInitializer(Pair.second);
-    for (GlobalVariable *GV : Eval.getInvariants())
+    for (GlobalVariable *GV : Eval.getInvariants()) {
       GV->setConstant(true);
+      GV->disableSanitizerMetadataGlobalTagging();
+    }
   }
 
   return EvalSuccess;

``````````

</details>


https://github.com/llvm/llvm-project/pull/132764


More information about the llvm-commits mailing list