]> granicus.if.org Git - llvm/commitdiff
[asan] Don't overalign global metadata.
authorEvgeniy Stepanov <eugeni.stepanov@gmail.com>
Thu, 12 Jan 2017 23:26:20 +0000 (23:26 +0000)
committerEvgeniy Stepanov <eugeni.stepanov@gmail.com>
Thu, 12 Jan 2017 23:26:20 +0000 (23:26 +0000)
Other than on COFF with incremental linking, global metadata should
not need any extra alignment.

Differential Revision: https://reviews.llvm.org/D28628

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291859 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Instrumentation/AddressSanitizer.cpp
test/Instrumentation/AddressSanitizer/global_metadata_darwin.ll

index 5f7e67033d43e9a9720688af5c88fe485770be8f..ffd518e52968a46a8fbd9e034814183be6e33db7 100644 (file)
@@ -1609,22 +1609,12 @@ void AddressSanitizerModule::SetComdatForGlobalMetadata(
 GlobalVariable *
 AddressSanitizerModule::CreateMetadataGlobal(Module &M, Constant *Initializer,
                                              StringRef OriginalName) {
-  auto &DL = M.getDataLayout();
   GlobalVariable *Metadata =
       new GlobalVariable(M, Initializer->getType(), false,
                          GlobalVariable::InternalLinkage, Initializer,
                          Twine("__asan_global_") +
                              GlobalValue::getRealLinkageName(OriginalName));
   Metadata->setSection(getGlobalMetadataSection());
-
-  // We don't want any padding, but we also need a reasonable alignment.
-  // The MSVC linker always inserts padding when linking incrementally. We
-  // cope with that by aligning each struct to its size, which must be a power
-  // of two.
-  unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(Initializer->getType());
-  assert(isPowerOf2_32(SizeOfGlobalStruct) &&
-         "global metadata will not be padded appropriately");
-  Metadata->setAlignment(SizeOfGlobalStruct);
   return Metadata;
 }
 
@@ -1642,11 +1632,22 @@ void AddressSanitizerModule::InstrumentGlobalsCOFF(
     IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
     ArrayRef<Constant *> MetadataInitializers) {
   assert(ExtendedGlobals.size() == MetadataInitializers.size());
+  auto &DL = M.getDataLayout();
 
   for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
+    Constant *Initializer = MetadataInitializers[i];
     GlobalVariable *G = ExtendedGlobals[i];
     GlobalVariable *Metadata =
-        CreateMetadataGlobal(M, MetadataInitializers[i], G->getName());
+        CreateMetadataGlobal(M, Initializer, G->getName());
+
+    // The MSVC linker always inserts padding when linking incrementally. We
+    // cope with that by aligning each struct to its size, which must be a power
+    // of two.
+    unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(Initializer->getType());
+    assert(isPowerOf2_32(SizeOfGlobalStruct) &&
+           "global metadata will not be padded appropriately");
+    Metadata->setAlignment(SizeOfGlobalStruct);
+
     SetComdatForGlobalMetadata(G, Metadata);
   }
 }
index f7b1a071721cd7cc1bc2ad5511f2b85057aa1be4..a8fe6a9f625629ffed27244331f1ab50d771d0f0 100644 (file)
@@ -16,7 +16,7 @@ target triple = "x86_64-apple-macosx10.11.0"
 
 
 ; Find the metadata for @global:
-; CHECK: [[METADATA:@.+]] = internal global {{.*}} @global {{.*}} section "__DATA,__asan_globals,regular", align 64
+; CHECK: [[METADATA:@.+]] = internal global {{.*}} @global {{.*}} section "__DATA,__asan_globals,regular"
 
 ; Find the liveness binder for @global and its metadata:
 ; CHECK: @__asan_binder_global = internal global {{.*}} @global {{.*}} [[METADATA]] {{.*}} section "__DATA,__asan_liveness,regular,live_support"