[llvm] r270627 - [esan] Add calls from the ctor/dtor to the runtime library

Derek Bruening via llvm-commits llvm-commits at lists.llvm.org
Tue May 24 15:48:26 PDT 2016


Author: bruening
Date: Tue May 24 17:48:24 2016
New Revision: 270627

URL: http://llvm.org/viewvc/llvm-project?rev=270627&view=rev
Log:
[esan] Add calls from the ctor/dtor to the runtime library

Summary:
Adds createEsanInitToolGV for creating a tool-specific variable passed
to the runtime library.

Adds dtor "esan.module_dtor" and inserts calls from the dtor to
"__esan_exit" in the runtime library.

Updates the EfficiencySanitizer test.

Patch by Qin Zhao.

Reviewers: aizatsky

Subscribers: bruening, kcc, vitalybuka, eugenis, llvm-commits

Differential Revision: http://reviews.llvm.org/D20488

Modified:
    llvm/trunk/lib/Transforms/Instrumentation/EfficiencySanitizer.cpp
    llvm/trunk/test/Instrumentation/EfficiencySanitizer/cache_frag_basic.ll

Modified: llvm/trunk/lib/Transforms/Instrumentation/EfficiencySanitizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/EfficiencySanitizer.cpp?rev=270627&r1=270626&r2=270627&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/EfficiencySanitizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/EfficiencySanitizer.cpp Tue May 24 17:48:24 2016
@@ -59,8 +59,11 @@ STATISTIC(NumFastpaths, "Number of instr
 STATISTIC(NumAccessesWithIrregularSize,
           "Number of accesses with a size outside our targeted callout sizes");
 
+static const uint64_t EsanCtorAndDtorPriority = 0;
 static const char *const EsanModuleCtorName = "esan.module_ctor";
+static const char *const EsanModuleDtorName = "esan.module_dtor";
 static const char *const EsanInitName = "__esan_init";
+static const char *const EsanExitName = "__esan_exit";
 
 namespace {
 
@@ -90,6 +93,8 @@ public:
 private:
   bool initOnModule(Module &M);
   void initializeCallbacks(Module &M);
+  GlobalVariable *createEsanInitToolGV(Module &M);
+  void createDestructor(Module &M, GlobalVariable *GV);
   bool runOnFunction(Function &F, Module &M);
   bool instrumentLoadOrStore(Instruction *I, const DataLayout &DL);
   bool instrumentMemIntrinsic(MemIntrinsic *MI);
@@ -115,6 +120,7 @@ private:
   Function *EsanUnalignedLoadN, *EsanUnalignedStoreN;
   Function *MemmoveFn, *MemcpyFn, *MemsetFn;
   Function *EsanCtorFunction;
+  Function *EsanDtorFunction;
 };
 } // namespace
 
@@ -173,19 +179,50 @@ void EfficiencySanitizer::initializeCall
                             IRB.getInt32Ty(), IntptrTy, nullptr));
 }
 
+// Create the tool-specific global variable passed to EsanInit and EsanExit.
+GlobalVariable *EfficiencySanitizer::createEsanInitToolGV(Module &M) {
+  GlobalVariable *GV = nullptr;
+  // FIXME: create the tool specific global variable.
+  if (GV == nullptr) {
+    GV = new GlobalVariable(M, IntptrTy, true, GlobalVariable::InternalLinkage,
+                            Constant::getNullValue(IntptrTy));
+  }
+  return GV;
+}
+
+void EfficiencySanitizer::createDestructor(Module &M, GlobalVariable *GV) {
+  EsanDtorFunction = Function::Create(FunctionType::get(Type::getVoidTy(*Ctx),
+                                                        false),
+                                      GlobalValue::InternalLinkage,
+                                      EsanModuleDtorName, &M);
+  ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
+  IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
+  Function *EsanExit = checkSanitizerInterfaceFunction(
+      M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
+                            IntptrTy, nullptr));
+  EsanExit->setLinkage(Function::ExternalLinkage);
+  IRB_Dtor.CreateCall(EsanExit,
+                      {IRB_Dtor.CreatePointerCast(GV, IntptrTy)});
+  appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority);
+}
+
 bool EfficiencySanitizer::initOnModule(Module &M) {
   Ctx = &M.getContext();
   const DataLayout &DL = M.getDataLayout();
   IRBuilder<> IRB(M.getContext());
   IntegerType *OrdTy = IRB.getInt32Ty();
   IntptrTy = DL.getIntPtrType(M.getContext());
+  // Create the variable passed to EsanInit and EsanExit.
+  GlobalVariable *GV = createEsanInitToolGV(M);
+  // Constructor
   std::tie(EsanCtorFunction, std::ignore) = createSanitizerCtorAndInitFunctions(
-      M, EsanModuleCtorName, EsanInitName, /*InitArgTypes=*/{OrdTy},
+      M, EsanModuleCtorName, EsanInitName, /*InitArgTypes=*/{OrdTy, IntptrTy},
       /*InitArgs=*/{
-          ConstantInt::get(OrdTy, static_cast<int>(Options.ToolType))});
-
-  appendToGlobalCtors(M, EsanCtorFunction, 0);
+        ConstantInt::get(OrdTy, static_cast<int>(Options.ToolType)),
+        ConstantExpr::getPointerCast(GV, IntptrTy)});
+  appendToGlobalCtors(M, EsanCtorFunction, EsanCtorAndDtorPriority);
 
+  createDestructor(M, GV);
   return true;
 }
 
@@ -216,7 +253,7 @@ bool EfficiencySanitizer::runOnFunction(
   SmallVector<Instruction *, 8> LoadsAndStores;
   SmallVector<Instruction *, 8> MemIntrinCalls;
   bool Res = false;
-  const DataLayout &DL = F.getParent()->getDataLayout();
+  const DataLayout &DL = M.getDataLayout();
 
   for (auto &BB : F) {
     for (auto &Inst : BB) {

Modified: llvm/trunk/test/Instrumentation/EfficiencySanitizer/cache_frag_basic.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/EfficiencySanitizer/cache_frag_basic.ll?rev=270627&r1=270626&r2=270627&view=diff
==============================================================================
--- llvm/trunk/test/Instrumentation/EfficiencySanitizer/cache_frag_basic.ll (original)
+++ llvm/trunk/test/Instrumentation/EfficiencySanitizer/cache_frag_basic.ll Tue May 24 17:48:24 2016
@@ -254,4 +254,6 @@ entry:
 ; Top-level:
 
 ; CHECK: define internal void @esan.module_ctor()
-; CHECK: call void @__esan_init(i32 1)
+; CHECK: call void @__esan_init(i32 1, i64 ptrtoint (i64* @0 to i64))
+; CHECK: define internal void @esan.module_dtor()
+; CHECK: call void @__esan_exit(i64 ptrtoint (i64* @0 to i64))




More information about the llvm-commits mailing list