Skip to content

[NFC] [MTE] Improve readability of AArch64GlobalsTagging #111580

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

fmayer
Copy link
Contributor

@fmayer fmayer commented Oct 8, 2024

shouldTagGlobal doesn't sound like it should modify anything, so don't
do that. Remove unused code. Use SmallVector over std::vector

Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2024

@llvm/pr-subscribers-backend-aarch64

Author: Florian Mayer (fmayer)

Changes

shouldTagGlobal doesn't sound like it should modify anything, so don't
do that. Remove unused code. Use SmallVector over std::vector


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

1 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp (+16-27)
diff --git a/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp b/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
index 27959489e7dfa4..a49d391d9148c3 100644
--- a/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
+++ b/llvm/lib/Target/AArch64/AArch64GlobalsTagging.cpp
@@ -9,39 +9,25 @@
 //===----------------------------------------------------------------------===//
 
 #include "AArch64.h"
-#include "llvm/BinaryFormat/ELF.h"
-#include "llvm/IR/Attributes.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/IR/GlobalVariable.h"
-#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Pass.h"
-#include "llvm/Support/raw_ostream.h"
 
 #include <algorithm>
-#include <set>
 
 using namespace llvm;
 
 static const Align kTagGranuleSize = Align(16);
 
-static bool shouldTagGlobal(GlobalVariable &G) {
-  if (!G.isTagged())
-    return false;
-
-  assert(G.hasSanitizerMetadata() &&
-         "Missing sanitizer metadata, but symbol is apparently tagged.");
-  GlobalValue::SanitizerMetadata Meta = G.getSanitizerMetadata();
-
+static bool shouldTagGlobal(const GlobalVariable &G) {
   // For now, don't instrument constant data, as it'll be in .rodata anyway. It
   // may be worth instrumenting these in future to stop them from being used as
   // gadgets.
-  if (G.getName().starts_with("llvm.") || G.isThreadLocal() || G.isConstant()) {
-    Meta.Memtag = false;
-    G.setSanitizerMetadata(Meta);
+  if (G.getName().starts_with("llvm.") || G.isThreadLocal() || G.isConstant())
     return false;
-  }
 
   // Globals can be placed implicitly or explicitly in sections. There's two
   // different types of globals that meet this criteria that cause problems:
@@ -54,18 +40,15 @@ static bool shouldTagGlobal(GlobalVariable &G) {
   //     them causes SIGSEGV/MTE[AS]ERR).
   //  2. Global variables put into an explicit section, where the section's name
   //     is a valid C-style identifier. The linker emits a `__start_<name>` and
-  //     `__stop_<na,e>` symbol for the section, so that you can iterate over
+  //     `__stop_<name>` symbol for the section, so that you can iterate over
   //     globals within this section. Unfortunately, again, these globals would
   //     be tagged and so iteration causes SIGSEGV/MTE[AS]ERR.
   //
   // To mitigate both these cases, and because specifying a section is rare
   // outside of these two cases, disable MTE protection for globals in any
   // section.
-  if (G.hasSection()) {
-    Meta.Memtag = false;
-    G.setSanitizerMetadata(Meta);
+  if (G.hasSection())
     return false;
-  }
 
   return true;
 }
@@ -132,9 +115,6 @@ class AArch64GlobalsTagging : public ModulePass {
   bool runOnModule(Module &M) override;
 
   StringRef getPassName() const override { return "AArch64 Globals Tagging"; }
-
-private:
-  std::set<GlobalVariable *> GlobalsToTag;
 };
 } // anonymous namespace
 
@@ -142,10 +122,19 @@ char AArch64GlobalsTagging::ID = 0;
 
 bool AArch64GlobalsTagging::runOnModule(Module &M) {
   // No mutating the globals in-place, or iterator invalidation occurs.
-  std::vector<GlobalVariable *> GlobalsToTag;
+  SmallVector<GlobalVariable *> GlobalsToTag;
   for (GlobalVariable &G : M.globals()) {
-    if (G.isDeclaration() || !shouldTagGlobal(G))
+    if (G.isDeclaration() || !G.isTagged())
       continue;
+
+    assert(G.hasSanitizerMetadata() &&
+           "Missing sanitizer metadata, but symbol is apparently tagged.");
+    if (!shouldTagGlobal(G)) {
+      GlobalValue::SanitizerMetadata Meta = G.getSanitizerMetadata();
+      Meta.Memtag = false;
+      G.setSanitizerMetadata(Meta);
+      assert(!G.isTagged());
+    }
     GlobalsToTag.push_back(&G);
   }
 

@fmayer fmayer requested a review from eugenis October 8, 2024 19:48
@fmayer fmayer merged commit a797144 into main Oct 9, 2024
11 checks passed
@fmayer fmayer deleted the users/fmayer/spr/nfc-mte-improve-readability-of-aarch64globalstagging branch October 9, 2024 22:59
DanielCChen pushed a commit to DanielCChen/llvm-project that referenced this pull request Oct 16, 2024
`shouldTagGlobal` doesn't sound like it should modify anything, so don't
do that. Remove unused code. Use SmallVector over std::vector
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.

3 participants