llvm.org GIT mirror llvm / 8d8d014
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@270627 91177308-0d34-0410-b5e6-96231b3b80d8 Derek Bruening 4 years ago
2 changed file(s) with 46 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
5858 STATISTIC(NumAccessesWithIrregularSize,
5959 "Number of accesses with a size outside our targeted callout sizes");
6060
61 static const uint64_t EsanCtorAndDtorPriority = 0;
6162 static const char *const EsanModuleCtorName = "esan.module_ctor";
63 static const char *const EsanModuleDtorName = "esan.module_dtor";
6264 static const char *const EsanInitName = "__esan_init";
65 static const char *const EsanExitName = "__esan_exit";
6366
6467 namespace {
6568
8992 private:
9093 bool initOnModule(Module &M);
9194 void initializeCallbacks(Module &M);
95 GlobalVariable *createEsanInitToolGV(Module &M);
96 void createDestructor(Module &M, GlobalVariable *GV);
9297 bool runOnFunction(Function &F, Module &M);
9398 bool instrumentLoadOrStore(Instruction *I, const DataLayout &DL);
9499 bool instrumentMemIntrinsic(MemIntrinsic *MI);
114119 Function *EsanUnalignedLoadN, *EsanUnalignedStoreN;
115120 Function *MemmoveFn, *MemcpyFn, *MemsetFn;
116121 Function *EsanCtorFunction;
122 Function *EsanDtorFunction;
117123 };
118124 } // namespace
119125
172178 IRB.getInt32Ty(), IntptrTy, nullptr));
173179 }
174180
181 // Create the tool-specific global variable passed to EsanInit and EsanExit.
182 GlobalVariable *EfficiencySanitizer::createEsanInitToolGV(Module &M) {
183 GlobalVariable *GV = nullptr;
184 // FIXME: create the tool specific global variable.
185 if (GV == nullptr) {
186 GV = new GlobalVariable(M, IntptrTy, true, GlobalVariable::InternalLinkage,
187 Constant::getNullValue(IntptrTy));
188 }
189 return GV;
190 }
191
192 void EfficiencySanitizer::createDestructor(Module &M, GlobalVariable *GV) {
193 EsanDtorFunction = Function::Create(FunctionType::get(Type::getVoidTy(*Ctx),
194 false),
195 GlobalValue::InternalLinkage,
196 EsanModuleDtorName, &M);
197 ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
198 IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
199 Function *EsanExit = checkSanitizerInterfaceFunction(
200 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
201 IntptrTy, nullptr));
202 EsanExit->setLinkage(Function::ExternalLinkage);
203 IRB_Dtor.CreateCall(EsanExit,
204 {IRB_Dtor.CreatePointerCast(GV, IntptrTy)});
205 appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority);
206 }
207
175208 bool EfficiencySanitizer::initOnModule(Module &M) {
176209 Ctx = &M.getContext();
177210 const DataLayout &DL = M.getDataLayout();
178211 IRBuilder<> IRB(M.getContext());
179212 IntegerType *OrdTy = IRB.getInt32Ty();
180213 IntptrTy = DL.getIntPtrType(M.getContext());
214 // Create the variable passed to EsanInit and EsanExit.
215 GlobalVariable *GV = createEsanInitToolGV(M);
216 // Constructor
181217 std::tie(EsanCtorFunction, std::ignore) = createSanitizerCtorAndInitFunctions(
182 M, EsanModuleCtorName, EsanInitName, /*InitArgTypes=*/{OrdTy},
218 M, EsanModuleCtorName, EsanInitName, /*InitArgTypes=*/{OrdTy, IntptrTy},
183219 /*InitArgs=*/{
184 ConstantInt::get(OrdTy, static_cast(Options.ToolType))});
185
186 appendToGlobalCtors(M, EsanCtorFunction, 0);
187
220 ConstantInt::get(OrdTy, static_cast(Options.ToolType)),
221 ConstantExpr::getPointerCast(GV, IntptrTy)});
222 appendToGlobalCtors(M, EsanCtorFunction, EsanCtorAndDtorPriority);
223
224 createDestructor(M, GV);
188225 return true;
189226 }
190227
215252 SmallVector LoadsAndStores;
216253 SmallVector MemIntrinCalls;
217254 bool Res = false;
218 const DataLayout &DL = F.getParent()->getDataLayout();
255 const DataLayout &DL = M.getDataLayout();
219256
220257 for (auto &BB : F) {
221258 for (auto &Inst : BB) {
253253 ; Top-level:
254254
255255 ; CHECK: define internal void @esan.module_ctor()
256 ; CHECK: call void @__esan_init(i32 1)
256 ; CHECK: call void @__esan_init(i32 1, i64 ptrtoint (i64* @0 to i64))
257 ; CHECK: define internal void @esan.module_dtor()
258 ; CHECK: call void @__esan_exit(i64 ptrtoint (i64* @0 to i64))