Skip to content

Fix globals being wrongly tagged after global optimization step #132764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

tarcisiofischer
Copy link

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.

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.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Mar 24, 2025

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-transforms

Author: Tarcísio Fischer (tarcisiofischer)

Changes

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 --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;

@DanielKristofKiss DanielKristofKiss requested review from vitalybuka, eugenis and davemgreen and removed request for vitalybuka, eugenis and davemgreen March 24, 2025 16:18
@@ -266,6 +266,16 @@ void GlobalValue::setNoSanitizeMetadata() {
setSanitizerMetadata(Meta);
}

void GlobalValue::disableSanitizerMetadataGlobalTagging() {
if (!isTagged()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the braces are not need for 1 liner condition. ( git clang-format should handle it too)

@fmayer
Copy link
Contributor

fmayer commented Mar 25, 2025

I am not opposed to this, but I would prefer a solution that doesn't require cooperation by every callsite.

@vitalybuka vitalybuka removed their request for review March 25, 2025 22:02
@fmayer
Copy link
Contributor

fmayer commented Apr 15, 2025

I am not opposed to this, but I would prefer a solution that doesn't require cooperation by every callsite.

I am working on a change that does this.

@tarcisiofischer
Copy link
Author

I am not opposed to this, but I would prefer a solution that doesn't require cooperation by every callsite.

I am working on a change that does this.

Thanks. Sorry I didn't have the time to get back into this.. My interpretation from what you said was to just move the code to the setter method, instead of calling it every time the set is called, but I didn't come back to make the change and push. I don't mind you tackling it in any way you find more appropriate.

If you do push a new PR, thought, please link it here! :)

@fmayer
Copy link
Contributor

fmayer commented Apr 16, 2025

#135891 is my PR for this

@fmayer
Copy link
Contributor

fmayer commented Apr 17, 2025

Submitted 9ed4c70 that addresses the same problem in a more general way.

@fmayer fmayer closed this Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants