Skip to content

[MTE] do not tag zero sized globals #136020

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

Merged
merged 4 commits into from
Apr 17, 2025

Conversation

fmayer
Copy link
Contributor

@fmayer fmayer commented Apr 16, 2025

No description provided.

fmayer added 2 commits April 16, 2025 13:26
Created using spr 1.3.4
@fmayer fmayer requested a review from pcc April 16, 2025 22:11
@fmayer fmayer marked this pull request as ready for review April 16, 2025 22:11
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Apr 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 16, 2025

@llvm/pr-subscribers-clang

Author: Florian Mayer (fmayer)

Changes

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

2 Files Affected:

  • (modified) clang/test/CodeGen/memtag-globals-asm.cpp (+6)
  • (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+9-4)
diff --git a/clang/test/CodeGen/memtag-globals-asm.cpp b/clang/test/CodeGen/memtag-globals-asm.cpp
index 57e3cedc083fd..fb3958dd8bcb6 100644
--- a/clang/test/CodeGen/memtag-globals-asm.cpp
+++ b/clang/test/CodeGen/memtag-globals-asm.cpp
@@ -21,6 +21,7 @@
 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P
 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q
 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R
+// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S
 
 // RUN: %clang_cc1 -O3 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \
 // RUN:   -fsanitize=memtag-globals -o %t.out %s
@@ -43,6 +44,7 @@
 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P
 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q
 // RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R
+// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S
 
 /// Ensure that emulated TLS also doesn't get sanitized.
 // RUN: %clang_cc1 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \
@@ -99,6 +101,10 @@ static char* global_buffer_local_end = &global_buffer[16];
 // CHECK-H: .size global_buffer_global_end, 16
 char* global_buffer_global_end = &global_buffer[16];
 
+// CHECK-S-NOT: .memtag zero_sized
+struct empty {};
+char zero_sized[0];
+
 class MyClass {
  public:
   virtual ~MyClass() {}
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index ad34465e2c606..b565ed66b6051 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -2398,6 +2398,12 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
   OutStreamer->emitBinaryData(Buf);
 }
 
+static uint64_t globalSize(const llvm::GlobalVariable &G) {
+  const Constant *Initializer = G.getInitializer();
+  return G.getParent()->getDataLayout().getTypeAllocSize(
+      Initializer->getType());
+}
+
 static bool shouldTagGlobal(const llvm::GlobalVariable &G) {
   // We used to do this in clang, but there are optimization passes that turn
   // non-constant globals into constants. So now, clang only tells us whether
@@ -2430,19 +2436,18 @@ static bool shouldTagGlobal(const llvm::GlobalVariable &G) {
   if (G.hasSection())
     return false;
 
-  return true;
+  return globalSize(G) > 0;
 }
 
 static void tagGlobalDefinition(Module &M, GlobalVariable *G) {
-  Constant *Initializer = G->getInitializer();
-  uint64_t SizeInBytes =
-      M.getDataLayout().getTypeAllocSize(Initializer->getType());
+  uint64_t SizeInBytes = globalSize(*G);
 
   uint64_t NewSize = alignTo(SizeInBytes, 16);
   if (SizeInBytes != NewSize) {
     // Pad the initializer out to the next multiple of 16 bytes.
     llvm::SmallVector<uint8_t> Init(NewSize - SizeInBytes, 0);
     Constant *Padding = ConstantDataArray::get(M.getContext(), Init);
+    Constant *Initializer = G->getInitializer();
     Initializer = ConstantStruct::getAnon({Initializer, Padding});
     auto *NewGV = new GlobalVariable(
         M, Initializer->getType(), G->isConstant(), G->getLinkage(),

fmayer added 2 commits April 17, 2025 09:54
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
@fmayer fmayer changed the base branch from users/fmayer/spr/main.mte-do-not-tag-zero-sized-globals to main April 17, 2025 16:55
@fmayer fmayer merged commit 6bac20b into main Apr 17, 2025
8 of 15 checks passed
@fmayer fmayer deleted the users/fmayer/spr/mte-do-not-tag-zero-sized-globals branch April 17, 2025 16:55
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants