Skip to content

Commit 6bac20b

Browse files
authored
[MTE] do not tag zero sized globals (#136020)
1 parent 9ed4c70 commit 6bac20b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

clang/test/CodeGen/memtag-globals-asm.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P
2222
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q
2323
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R
24+
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S
2425

2526
// RUN: %clang_cc1 -O3 -S -x c++ -std=c++11 -triple aarch64-linux-android31 \
2627
// RUN: -fsanitize=memtag-globals -o %t.out %s
@@ -43,6 +44,7 @@
4344
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-P
4445
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-Q
4546
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-R
47+
// RUN: FileCheck %s --input-file=%t.out --check-prefix=CHECK-S
4648

4749
/// Ensure that emulated TLS also doesn't get sanitized.
4850
// 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];
99101
// CHECK-H: .size global_buffer_global_end, 16
100102
char* global_buffer_global_end = &global_buffer[16];
101103

104+
// CHECK-S-NOT: .memtag zero_sized
105+
struct empty {};
106+
char zero_sized[0];
107+
102108
class MyClass {
103109
public:
104110
virtual ~MyClass() {}

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,12 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
23982398
OutStreamer->emitBinaryData(Buf);
23992399
}
24002400

2401+
static uint64_t globalSize(const llvm::GlobalVariable &G) {
2402+
const Constant *Initializer = G.getInitializer();
2403+
return G.getParent()->getDataLayout().getTypeAllocSize(
2404+
Initializer->getType());
2405+
}
2406+
24012407
static bool shouldTagGlobal(const llvm::GlobalVariable &G) {
24022408
// We used to do this in clang, but there are optimization passes that turn
24032409
// non-constant globals into constants. So now, clang only tells us whether
@@ -2430,19 +2436,18 @@ static bool shouldTagGlobal(const llvm::GlobalVariable &G) {
24302436
if (G.hasSection())
24312437
return false;
24322438

2433-
return true;
2439+
return globalSize(G) > 0;
24342440
}
24352441

24362442
static void tagGlobalDefinition(Module &M, GlobalVariable *G) {
2437-
Constant *Initializer = G->getInitializer();
2438-
uint64_t SizeInBytes =
2439-
M.getDataLayout().getTypeAllocSize(Initializer->getType());
2443+
uint64_t SizeInBytes = globalSize(*G);
24402444

24412445
uint64_t NewSize = alignTo(SizeInBytes, 16);
24422446
if (SizeInBytes != NewSize) {
24432447
// Pad the initializer out to the next multiple of 16 bytes.
24442448
llvm::SmallVector<uint8_t> Init(NewSize - SizeInBytes, 0);
24452449
Constant *Padding = ConstantDataArray::get(M.getContext(), Init);
2450+
Constant *Initializer = G->getInitializer();
24462451
Initializer = ConstantStruct::getAnon({Initializer, Padding});
24472452
auto *NewGV = new GlobalVariable(
24482453
M, Initializer->getType(), G->isConstant(), G->getLinkage(),

0 commit comments

Comments
 (0)