[llvm] 6bac20b - [MTE] do not tag zero sized globals (#136020)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 17 09:55:45 PDT 2025
Author: Florian Mayer
Date: 2025-04-17T09:55:41-07:00
New Revision: 6bac20b391edce2bde348e59f5be2143157304b5
URL: https://github.com/llvm/llvm-project/commit/6bac20b391edce2bde348e59f5be2143157304b5
DIFF: https://github.com/llvm/llvm-project/commit/6bac20b391edce2bde348e59f5be2143157304b5.diff
LOG: [MTE] do not tag zero sized globals (#136020)
Added:
Modified:
clang/test/CodeGen/memtag-globals-asm.cpp
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Removed:
################################################################################
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 6b1f3df12ee44..bdcd54a135da9 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(),
More information about the llvm-commits
mailing list