diff options
author | Reid Kleckner <rnk@google.com> | 2016-11-29 01:32:21 +0000 |
---|---|---|
committer | Reid Kleckner <rnk@google.com> | 2016-11-29 01:32:21 +0000 |
commit | 78565839c6318cb19b87eebad6ddfa26991d32b6 (patch) | |
tree | dc4ca2e4af63d1b6bdc5f5a47cea53bdd47ea3bf /llvm/lib | |
parent | 0bc688116c95d74a1b72a650607eab1f8fdc2a33 (diff) | |
download | bcm5719-llvm-78565839c6318cb19b87eebad6ddfa26991d32b6.tar.gz bcm5719-llvm-78565839c6318cb19b87eebad6ddfa26991d32b6.zip |
[asan/win] Align global registration metadata to its size
This way, when the linker adds padding between globals, we can skip over
the zero padding bytes and reliably find the start of the next metadata
global.
llvm-svn: 288096
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp index db9dd1f8c61..42d3cb68446 100644 --- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -1561,6 +1561,7 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) { size_t n = GlobalsToChange.size(); if (n == 0) return false; + auto &DL = M.getDataLayout(); bool UseComdatMetadata = TargetTriple.isOSBinFormatCOFF(); bool UseMachOGlobalsSection = ShouldUseMachOGlobalsSection(); bool UseMetadataArray = !(UseComdatMetadata || UseMachOGlobalsSection); @@ -1578,6 +1579,10 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) { StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy, nullptr); + unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(GlobalStructTy); + assert((isPowerOf2_32(SizeOfGlobalStruct) || + !TargetTriple.isOSBinFormatCOFF()) && + "global metadata will not be padded appropriately"); SmallVector<Constant *, 16> Initializers(UseMetadataArray ? n : 0); // On recent Mach-O platforms, use a structure which binds the liveness of @@ -1596,7 +1601,6 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) { GlobalVariable *ModuleName = createPrivateGlobalForString( M, M.getModuleIdentifier(), /*AllowMerging*/ false); - auto &DL = M.getDataLayout(); for (size_t i = 0; i < n; i++) { static const uint64_t kMaxGlobalRedzone = 1 << 18; GlobalVariable *G = GlobalsToChange[i]; @@ -1743,7 +1747,14 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) { Initializer, Twine("__asan_global_") + GlobalValue::getRealLinkageName(G->getName())); Metadata->setSection(getGlobalMetadataSection()); - Metadata->setAlignment(1); // Don't leave padding in between. + + // 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. + if (TargetTriple.isOSBinFormatCOFF()) + Metadata->setAlignment(SizeOfGlobalStruct); + else + Metadata->setAlignment(1); // Don't leave padding in between. // On platforms that support comdats, put the metadata and the // instrumented global in the same group. This ensures that the metadata |