Skip to content

Commit e441b88

Browse files
author
git apple-llvm automerger
committed
Merge commit '6bac20b391ed' from llvm.org/main into next
2 parents 9a7714b + 6bac20b commit e441b88

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
@@ -2423,6 +2423,12 @@ void AsmPrinter::emitRemarksSection(remarks::RemarkStreamer &RS) {
24232423
OutStreamer->emitBinaryData(Buf);
24242424
}
24252425

2426+
static uint64_t globalSize(const llvm::GlobalVariable &G) {
2427+
const Constant *Initializer = G.getInitializer();
2428+
return G.getParent()->getDataLayout().getTypeAllocSize(
2429+
Initializer->getType());
2430+
}
2431+
24262432
static bool shouldTagGlobal(const llvm::GlobalVariable &G) {
24272433
// We used to do this in clang, but there are optimization passes that turn
24282434
// non-constant globals into constants. So now, clang only tells us whether
@@ -2455,19 +2461,18 @@ static bool shouldTagGlobal(const llvm::GlobalVariable &G) {
24552461
if (G.hasSection())
24562462
return false;
24572463

2458-
return true;
2464+
return globalSize(G) > 0;
24592465
}
24602466

24612467
static void tagGlobalDefinition(Module &M, GlobalVariable *G) {
2462-
Constant *Initializer = G->getInitializer();
2463-
uint64_t SizeInBytes =
2464-
M.getDataLayout().getTypeAllocSize(Initializer->getType());
2468+
uint64_t SizeInBytes = globalSize(*G);
24652469

24662470
uint64_t NewSize = alignTo(SizeInBytes, 16);
24672471
if (SizeInBytes != NewSize) {
24682472
// Pad the initializer out to the next multiple of 16 bytes.
24692473
llvm::SmallVector<uint8_t> Init(NewSize - SizeInBytes, 0);
24702474
Constant *Padding = ConstantDataArray::get(M.getContext(), Init);
2475+
Constant *Initializer = G->getInitializer();
24712476
Initializer = ConstantStruct::getAnon({Initializer, Padding});
24722477
auto *NewGV = new GlobalVariable(
24732478
M, Initializer->getType(), G->isConstant(), G->getLinkage(),

0 commit comments

Comments
 (0)