[llvm-commits] [llvm] r146284 - /llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Kostya Serebryany kcc at google.com
Fri Dec 9 14:09:33 PST 2011


Author: kcc
Date: Fri Dec  9 16:09:32 2011
New Revision: 146284

URL: http://llvm.org/viewvc/llvm-project?rev=146284&view=rev
Log:
[asan] call __asan_init from .preinit_array. This simplifies __asan_init vs malloc chicken-and-egg situation on Android and probably on other flavours of Linux. Patch by eugenis at google.com.

Modified:
    llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Modified: llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp?rev=146284&r1=146283&r2=146284&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp Fri Dec  9 16:09:32 2011
@@ -163,6 +163,8 @@
 
  private:
 
+  void appendToPreinitArray(Module &M, Function *F);
+
   uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
     Type *Ty = AI->getAllocatedType();
     uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
@@ -563,6 +565,18 @@
   return true;
 }
 
+// .preinit_array is something that hapens before all other inits.
+// On systems where .preinit_array is honored, we will call __asan_init early.
+// On other systems this will make no effect.
+void AddressSanitizer::appendToPreinitArray(Module &M, Function *F) {
+  IRBuilder<> IRB(M.getContext());
+  GlobalVariable *Var =
+      new GlobalVariable(M, PointerType::getUnqual(F->getFunctionType()),
+                         false, GlobalValue::PrivateLinkage,
+                         F, "__asan_preinit_private");
+  Var->setSection(".preinit_array");
+}
+
 // virtual
 bool AddressSanitizer::runOnModule(Module &M) {
   // Initialize the private fields. No one has accessed them before.
@@ -633,6 +647,7 @@
   }
 
   appendToGlobalCtors(M, AsanCtorFunction, 1 /*high priority*/);
+  appendToPreinitArray(M, AsanInitFunction);
 
   return Res;
 }





More information about the llvm-commits mailing list