[asan] change _sanitizer_cov_module_init to accept int* instead of int**
[oota-llvm.git] / lib / Transforms / Instrumentation / SanitizerCoverage.cpp
1 //===-- SanitizerCoverage.cpp - coverage instrumentation for sanitizers ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Coverage instrumentation that works with AddressSanitizer
11 // and potentially with other Sanitizers.
12 //
13 // We create a Guard variable with the same linkage
14 // as the function and inject this code into the entry block (CoverageLevel=1)
15 // or all blocks (CoverageLevel>=2):
16 // if (Guard < 0) {
17 //    __sanitizer_cov(&Guard);
18 // }
19 // The accesses to Guard are atomic. The rest of the logic is
20 // in __sanitizer_cov (it's fine to call it more than once).
21 //
22 // With CoverageLevel>=3 we also split critical edges this effectively
23 // instrumenting all edges.
24 //
25 // CoverageLevel>=4 add indirect call profiling implented as a function call.
26 //
27 // This coverage implementation provides very limited data:
28 // it only tells if a given function (block) was ever executed. No counters.
29 // But for many use cases this is what we need and the added slowdown small.
30 //
31 //===----------------------------------------------------------------------===//
32
33 #include "llvm/Transforms/Instrumentation.h"
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/IR/CallSite.h"
37 #include "llvm/IR/DataLayout.h"
38 #include "llvm/IR/Function.h"
39 #include "llvm/IR/IRBuilder.h"
40 #include "llvm/IR/InlineAsm.h"
41 #include "llvm/IR/LLVMContext.h"
42 #include "llvm/IR/MDBuilder.h"
43 #include "llvm/IR/Module.h"
44 #include "llvm/IR/Type.h"
45 #include "llvm/Support/CommandLine.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include "llvm/Transforms/Scalar.h"
49 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
50 #include "llvm/Transforms/Utils/ModuleUtils.h"
51
52 using namespace llvm;
53
54 #define DEBUG_TYPE "sancov"
55
56 static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init";
57 static const char *const kSanCovName = "__sanitizer_cov";
58 static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16";
59 static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter";
60 static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block";
61 static const char *const kSanCovModuleCtorName = "sancov.module_ctor";
62 static const uint64_t    kSanCtorAndDtorPriority = 1;
63
64 static cl::opt<int> ClCoverageLevel("sanitizer-coverage-level",
65        cl::desc("Sanitizer Coverage. 0: none, 1: entry block, 2: all blocks, "
66                 "3: all blocks and critical edges, "
67                 "4: above plus indirect calls"),
68        cl::Hidden, cl::init(0));
69
70 static cl::opt<int> ClCoverageBlockThreshold(
71     "sanitizer-coverage-block-threshold",
72     cl::desc("Add coverage instrumentation only to the entry block if there "
73              "are more than this number of blocks."),
74     cl::Hidden, cl::init(1500));
75
76 static cl::opt<bool>
77     ClExperimentalTracing("sanitizer-coverage-experimental-tracing",
78                           cl::desc("Experimental basic-block tracing: insert "
79                                    "callbacks at every basic block"),
80                           cl::Hidden, cl::init(false));
81
82 namespace {
83
84 class SanitizerCoverageModule : public ModulePass {
85  public:
86    SanitizerCoverageModule(int CoverageLevel = 0)
87        : ModulePass(ID),
88          CoverageLevel(std::max(CoverageLevel, (int)ClCoverageLevel)) {}
89   bool runOnModule(Module &M) override;
90   bool runOnFunction(Function &F);
91   static char ID;  // Pass identification, replacement for typeid
92   const char *getPassName() const override {
93     return "SanitizerCoverageModule";
94   }
95
96   void getAnalysisUsage(AnalysisUsage &AU) const override {
97     AU.addRequired<DataLayoutPass>();
98   }
99
100  private:
101   void InjectCoverageForIndirectCalls(Function &F,
102                                       ArrayRef<Instruction *> IndirCalls);
103   bool InjectCoverage(Function &F, ArrayRef<BasicBlock *> AllBlocks,
104                       ArrayRef<Instruction *> IndirCalls);
105   bool InjectTracing(Function &F, ArrayRef<BasicBlock *> AllBlocks);
106   void InjectCoverageAtBlock(Function &F, BasicBlock &BB);
107   Function *SanCovFunction;
108   Function *SanCovIndirCallFunction;
109   Function *SanCovModuleInit;
110   Function *SanCovTraceEnter, *SanCovTraceBB;
111   InlineAsm *EmptyAsm;
112   Type *IntptrTy;
113   LLVMContext *C;
114
115   GlobalVariable *GuardArray;
116
117   int CoverageLevel;
118 };
119
120 }  // namespace
121
122 static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
123   if (Function *F = dyn_cast<Function>(FuncOrBitcast))
124      return F;
125   std::string Err;
126   raw_string_ostream Stream(Err);
127   Stream << "SanitizerCoverage interface function redefined: "
128          << *FuncOrBitcast;
129   report_fatal_error(Err);
130 }
131
132 bool SanitizerCoverageModule::runOnModule(Module &M) {
133   if (!CoverageLevel) return false;
134   C = &(M.getContext());
135   DataLayoutPass *DLP = &getAnalysis<DataLayoutPass>();
136   IntptrTy = Type::getIntNTy(*C, DLP->getDataLayout().getPointerSizeInBits());
137   Type *VoidTy = Type::getVoidTy(*C);
138   IRBuilder<> IRB(*C);
139   Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
140
141   Function *CtorFunc =
142       Function::Create(FunctionType::get(VoidTy, false),
143                        GlobalValue::InternalLinkage, kSanCovModuleCtorName, &M);
144   ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc));
145   appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
146
147   SanCovFunction = checkInterfaceFunction(
148       M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
149   SanCovIndirCallFunction = checkInterfaceFunction(M.getOrInsertFunction(
150       kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
151   SanCovModuleInit = checkInterfaceFunction(
152       M.getOrInsertFunction(kSanCovModuleInitName, Type::getVoidTy(*C),
153                             Int32PtrTy, IntptrTy, nullptr));
154   SanCovModuleInit->setLinkage(Function::ExternalLinkage);
155   // We insert an empty inline asm after cov callbacks to avoid callback merge.
156   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
157                             StringRef(""), StringRef(""),
158                             /*hasSideEffects=*/true);
159
160   if (ClExperimentalTracing) {
161     SanCovTraceEnter = checkInterfaceFunction(
162         M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, IntptrTy, nullptr));
163     SanCovTraceBB = checkInterfaceFunction(
164         M.getOrInsertFunction(kSanCovTraceBB, VoidTy, IntptrTy, nullptr));
165   }
166
167   // At this point we create a dummy array of guards because we don't
168   // know how many elements we will need.
169   Type *Int32Ty = IRB.getInt32Ty();
170   GuardArray =
171       new GlobalVariable(M, Int32Ty, false, GlobalValue::ExternalLinkage,
172                          nullptr, "__sancov_gen_cov_tmp");
173
174   for (auto &F : M)
175     runOnFunction(F);
176
177   // Now we know how many elements we need. Create an array of guards
178   // with one extra element at the beginning for the size.
179   Type *Int32ArrayNTy =
180       ArrayType::get(Int32Ty, SanCovFunction->getNumUses() + 1);
181   GlobalVariable *RealGuardArray = new GlobalVariable(
182       M, Int32ArrayNTy, false, GlobalValue::PrivateLinkage,
183       Constant::getNullValue(Int32ArrayNTy), "__sancov_gen_cov");
184
185   // Replace the dummy array with the real one.
186   GuardArray->replaceAllUsesWith(
187       IRB.CreatePointerCast(RealGuardArray, Int32PtrTy));
188   GuardArray->eraseFromParent();
189
190   // Call __sanitizer_cov_module_init
191   IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator());
192   IRB.CreateCall2(SanCovModuleInit,
193                   IRB.CreatePointerCast(RealGuardArray, Int32PtrTy),
194                   ConstantInt::get(IntptrTy, SanCovFunction->getNumUses()));
195   return true;
196 }
197
198 bool SanitizerCoverageModule::runOnFunction(Function &F) {
199   if (F.empty()) return false;
200   if (F.getName().find(".module_ctor") != std::string::npos)
201     return false;  // Should not instrument sanitizer init functions.
202   if (CoverageLevel >= 3)
203     SplitAllCriticalEdges(F, this);
204   SmallVector<Instruction*, 8> IndirCalls;
205   SmallVector<BasicBlock*, 16> AllBlocks;
206   for (auto &BB : F) {
207     AllBlocks.push_back(&BB);
208     if (CoverageLevel >= 4)
209       for (auto &Inst : BB) {
210         CallSite CS(&Inst);
211         if (CS && !CS.getCalledFunction())
212           IndirCalls.push_back(&Inst);
213       }
214   }
215   InjectCoverage(F, AllBlocks, IndirCalls);
216   InjectTracing(F, AllBlocks);
217   return true;
218 }
219
220 // Experimental support for tracing.
221 // Basicaly, insert a callback at the beginning of every basic block.
222 // Every callback gets a pointer to a uniqie global for internal storage.
223 bool SanitizerCoverageModule::InjectTracing(Function &F,
224                                             ArrayRef<BasicBlock *> AllBlocks) {
225   if (!ClExperimentalTracing) return false;
226   Type *Ty = ArrayType::get(IntptrTy, 1);  // May need to use more words later.
227   for (auto BB : AllBlocks) {
228     IRBuilder<> IRB(BB->getFirstInsertionPt());
229     GlobalVariable *TraceCache = new GlobalVariable(
230         *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
231         Constant::getNullValue(Ty), "__sancov_gen_trace_cache");
232     IRB.CreateCall(&F.getEntryBlock() == BB ? SanCovTraceEnter : SanCovTraceBB,
233                    IRB.CreatePointerCast(TraceCache, IntptrTy));
234   }
235   return true;
236 }
237
238 bool
239 SanitizerCoverageModule::InjectCoverage(Function &F,
240                                         ArrayRef<BasicBlock *> AllBlocks,
241                                         ArrayRef<Instruction *> IndirCalls) {
242   if (!CoverageLevel) return false;
243
244   if (CoverageLevel == 1 ||
245       (unsigned)ClCoverageBlockThreshold < AllBlocks.size()) {
246     InjectCoverageAtBlock(F, F.getEntryBlock());
247   } else {
248     for (auto BB : AllBlocks)
249       InjectCoverageAtBlock(F, *BB);
250   }
251   InjectCoverageForIndirectCalls(F, IndirCalls);
252   return true;
253 }
254
255 // On every indirect call we call a run-time function
256 // __sanitizer_cov_indir_call* with two parameters:
257 //   - callee address,
258 //   - global cache array that contains kCacheSize pointers (zero-initialized).
259 //     The cache is used to speed up recording the caller-callee pairs.
260 // The address of the caller is passed implicitly via caller PC.
261 // kCacheSize is encoded in the name of the run-time function.
262 void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
263     Function &F, ArrayRef<Instruction *> IndirCalls) {
264   if (IndirCalls.empty()) return;
265   const int kCacheSize = 16;
266   const int kCacheAlignment = 64;  // Align for better performance.
267   Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
268   for (auto I : IndirCalls) {
269     IRBuilder<> IRB(I);
270     CallSite CS(I);
271     Value *Callee = CS.getCalledValue();
272     if (dyn_cast<InlineAsm>(Callee)) continue;
273     GlobalVariable *CalleeCache = new GlobalVariable(
274         *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
275         Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
276     CalleeCache->setAlignment(kCacheAlignment);
277     IRB.CreateCall2(SanCovIndirCallFunction,
278                     IRB.CreatePointerCast(Callee, IntptrTy),
279                     IRB.CreatePointerCast(CalleeCache, IntptrTy));
280   }
281 }
282
283 void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F,
284                                                     BasicBlock &BB) {
285   BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
286   // Skip static allocas at the top of the entry block so they don't become
287   // dynamic when we split the block.  If we used our optimized stack layout,
288   // then there will only be one alloca and it will come first.
289   for (; IP != BE; ++IP) {
290     AllocaInst *AI = dyn_cast<AllocaInst>(IP);
291     if (!AI || !AI->isStaticAlloca())
292       break;
293   }
294
295   DebugLoc EntryLoc = &BB == &F.getEntryBlock()
296                           ? IP->getDebugLoc().getFnDebugLoc(*C)
297                           : IP->getDebugLoc();
298   IRBuilder<> IRB(IP);
299   IRB.SetCurrentDebugLocation(EntryLoc);
300   SmallVector<Value *, 1> Indices;
301   Value *GuardP = IRB.CreateAdd(
302       IRB.CreatePointerCast(GuardArray, IntptrTy),
303       ConstantInt::get(IntptrTy, (1 + SanCovFunction->getNumUses()) * 4));
304   Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
305   GuardP = IRB.CreateIntToPtr(GuardP, Int32PtrTy);
306   LoadInst *Load = IRB.CreateLoad(GuardP);
307   Load->setAtomic(Monotonic);
308   Load->setAlignment(4);
309   Load->setMetadata(F.getParent()->getMDKindID("nosanitize"),
310                     MDNode::get(*C, None));
311   Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Load->getType()), Load);
312   Instruction *Ins = SplitBlockAndInsertIfThen(
313       Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
314   IRB.SetInsertPoint(Ins);
315   IRB.SetCurrentDebugLocation(EntryLoc);
316   // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
317   IRB.CreateCall(SanCovFunction, GuardP);
318   IRB.CreateCall(EmptyAsm);  // Avoids callback merge.
319 }
320
321 char SanitizerCoverageModule::ID = 0;
322 INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
323     "SanitizerCoverage: TODO."
324     "ModulePass", false, false)
325 ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
326   return new SanitizerCoverageModule(CoverageLevel);
327 }