blob: 8600f9183e0c9f32230bba0682875acf0984d086 [file] [log] [blame]
Kostya Serebryany29a18dc2014-11-11 22:14:37 +00001//===-- 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//
Kostya Serebryany9fdeb372014-12-23 22:32:17 +000013// We create a Guard variable with the same linkage
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000014// as the function and inject this code into the entry block (CoverageLevel=1)
15// or all blocks (CoverageLevel>=2):
Kostya Serebryany9fdeb372014-12-23 22:32:17 +000016// if (Guard < 0) {
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +000017// __sanitizer_cov(&Guard);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000018// }
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"
Kostya Serebryany73762942014-12-16 21:24:15 +000040#include "llvm/IR/InlineAsm.h"
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000041#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
52using namespace llvm;
53
54#define DEBUG_TYPE "sancov"
55
56static const char *const kSanCovModuleInitName = "__sanitizer_cov_module_init";
57static const char *const kSanCovName = "__sanitizer_cov";
58static const char *const kSanCovIndirCallName = "__sanitizer_cov_indir_call16";
Kostya Serebryanycb45b122014-11-19 00:22:58 +000059static const char *const kSanCovTraceEnter = "__sanitizer_cov_trace_func_enter";
60static const char *const kSanCovTraceBB = "__sanitizer_cov_trace_basic_block";
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000061static const char *const kSanCovModuleCtorName = "sancov.module_ctor";
62static const uint64_t kSanCtorAndDtorPriority = 1;
63
64static 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
70static 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
Kostya Serebryanycb45b122014-11-19 00:22:58 +000076static 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
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000082namespace {
83
84class SanitizerCoverageModule : public ModulePass {
85 public:
Kostya Serebryanycb45b122014-11-19 00:22:58 +000086 SanitizerCoverageModule(int CoverageLevel = 0)
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000087 : ModulePass(ID),
Kostya Serebryanycb45b122014-11-19 00:22:58 +000088 CoverageLevel(std::max(CoverageLevel, (int)ClCoverageLevel)) {}
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000089 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 {
Kostya Serebryany29a18dc2014-11-11 22:14:37 +000097 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);
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000105 bool InjectTracing(Function &F, ArrayRef<BasicBlock *> AllBlocks);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000106 void InjectCoverageAtBlock(Function &F, BasicBlock &BB);
107 Function *SanCovFunction;
108 Function *SanCovIndirCallFunction;
109 Function *SanCovModuleInit;
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000110 Function *SanCovTraceEnter, *SanCovTraceBB;
Kostya Serebryany73762942014-12-16 21:24:15 +0000111 InlineAsm *EmptyAsm;
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000112 Type *IntptrTy;
113 LLVMContext *C;
114
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000115 SmallVector<Constant *, 16> Guards;
116
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000117 int CoverageLevel;
118};
119
120} // namespace
121
122static 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
132bool 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);
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000138 IRBuilder<> IRB(*C);
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000139 Type *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty());
140 Type *Int32PtrPtrTy = PointerType::getUnqual(Int32PtrTy);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000141
142 Function *CtorFunc =
143 Function::Create(FunctionType::get(VoidTy, false),
144 GlobalValue::InternalLinkage, kSanCovModuleCtorName, &M);
145 ReturnInst::Create(*C, BasicBlock::Create(*C, "", CtorFunc));
146 appendToGlobalCtors(M, CtorFunc, kSanCtorAndDtorPriority);
147
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000148 SanCovFunction = checkInterfaceFunction(
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000149 M.getOrInsertFunction(kSanCovName, VoidTy, Int32PtrTy, nullptr));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000150 SanCovIndirCallFunction = checkInterfaceFunction(M.getOrInsertFunction(
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000151 kSanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000152 SanCovModuleInit = checkInterfaceFunction(
153 M.getOrInsertFunction(kSanCovModuleInitName, Type::getVoidTy(*C),
154 Int32PtrPtrTy, IntptrTy, nullptr));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000155 SanCovModuleInit->setLinkage(Function::ExternalLinkage);
Kostya Serebryany73762942014-12-16 21:24:15 +0000156 // We insert an empty inline asm after cov callbacks to avoid callback merge.
157 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
158 StringRef(""), StringRef(""),
159 /*hasSideEffects=*/true);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000160
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000161 if (ClExperimentalTracing) {
162 SanCovTraceEnter = checkInterfaceFunction(
163 M.getOrInsertFunction(kSanCovTraceEnter, VoidTy, IntptrTy, nullptr));
164 SanCovTraceBB = checkInterfaceFunction(
165 M.getOrInsertFunction(kSanCovTraceBB, VoidTy, IntptrTy, nullptr));
166 }
167
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000168 for (auto &F : M)
169 runOnFunction(F);
170
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000171 ArrayType *ArrayOfInt32PtrTy = ArrayType::get(Int32PtrTy, Guards.size());
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000172 IRB.SetInsertPoint(CtorFunc->getEntryBlock().getTerminator());
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000173 GlobalVariable *AllGuards = new GlobalVariable(
174 M, ArrayOfInt32PtrTy, false, GlobalVariable::InternalLinkage,
175 ConstantArray::get(ArrayOfInt32PtrTy, Guards), "");
176 assert(SanCovFunction->getNumUses() == Guards.size());
177 IRB.CreateCall2(SanCovModuleInit,
178 IRB.CreatePointerCast(AllGuards, Int32PtrPtrTy),
179 ConstantInt::get(IntptrTy, Guards.size()));
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000180 return true;
181}
182
183bool SanitizerCoverageModule::runOnFunction(Function &F) {
184 if (F.empty()) return false;
Kostya Serebryanyfea4fb42014-12-17 21:50:04 +0000185 if (F.getName().find(".module_ctor") != std::string::npos)
186 return false; // Should not instrument sanitizer init functions.
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000187 if (CoverageLevel >= 3)
188 SplitAllCriticalEdges(F, this);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000189 SmallVector<Instruction*, 8> IndirCalls;
190 SmallVector<BasicBlock*, 16> AllBlocks;
191 for (auto &BB : F) {
192 AllBlocks.push_back(&BB);
193 if (CoverageLevel >= 4)
194 for (auto &Inst : BB) {
195 CallSite CS(&Inst);
196 if (CS && !CS.getCalledFunction())
197 IndirCalls.push_back(&Inst);
198 }
199 }
200 InjectCoverage(F, AllBlocks, IndirCalls);
Kostya Serebryanycb45b122014-11-19 00:22:58 +0000201 InjectTracing(F, AllBlocks);
202 return true;
203}
204
205// Experimental support for tracing.
206// Basicaly, insert a callback at the beginning of every basic block.
207// Every callback gets a pointer to a uniqie global for internal storage.
208bool SanitizerCoverageModule::InjectTracing(Function &F,
209 ArrayRef<BasicBlock *> AllBlocks) {
210 if (!ClExperimentalTracing) return false;
211 Type *Ty = ArrayType::get(IntptrTy, 1); // May need to use more words later.
212 for (auto BB : AllBlocks) {
213 IRBuilder<> IRB(BB->getFirstInsertionPt());
214 GlobalVariable *TraceCache = new GlobalVariable(
215 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
216 Constant::getNullValue(Ty), "__sancov_gen_trace_cache");
217 IRB.CreateCall(&F.getEntryBlock() == BB ? SanCovTraceEnter : SanCovTraceBB,
218 IRB.CreatePointerCast(TraceCache, IntptrTy));
219 }
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000220 return true;
221}
222
223bool
224SanitizerCoverageModule::InjectCoverage(Function &F,
225 ArrayRef<BasicBlock *> AllBlocks,
226 ArrayRef<Instruction *> IndirCalls) {
227 if (!CoverageLevel) return false;
228
229 if (CoverageLevel == 1 ||
230 (unsigned)ClCoverageBlockThreshold < AllBlocks.size()) {
231 InjectCoverageAtBlock(F, F.getEntryBlock());
232 } else {
233 for (auto BB : AllBlocks)
234 InjectCoverageAtBlock(F, *BB);
235 }
236 InjectCoverageForIndirectCalls(F, IndirCalls);
237 return true;
238}
239
240// On every indirect call we call a run-time function
241// __sanitizer_cov_indir_call* with two parameters:
242// - callee address,
243// - global cache array that contains kCacheSize pointers (zero-initialized).
244// The cache is used to speed up recording the caller-callee pairs.
245// The address of the caller is passed implicitly via caller PC.
246// kCacheSize is encoded in the name of the run-time function.
247void SanitizerCoverageModule::InjectCoverageForIndirectCalls(
248 Function &F, ArrayRef<Instruction *> IndirCalls) {
249 if (IndirCalls.empty()) return;
250 const int kCacheSize = 16;
251 const int kCacheAlignment = 64; // Align for better performance.
252 Type *Ty = ArrayType::get(IntptrTy, kCacheSize);
253 for (auto I : IndirCalls) {
254 IRBuilder<> IRB(I);
255 CallSite CS(I);
256 Value *Callee = CS.getCalledValue();
257 if (dyn_cast<InlineAsm>(Callee)) continue;
258 GlobalVariable *CalleeCache = new GlobalVariable(
259 *F.getParent(), Ty, false, GlobalValue::PrivateLinkage,
260 Constant::getNullValue(Ty), "__sancov_gen_callee_cache");
261 CalleeCache->setAlignment(kCacheAlignment);
262 IRB.CreateCall2(SanCovIndirCallFunction,
263 IRB.CreatePointerCast(Callee, IntptrTy),
264 IRB.CreatePointerCast(CalleeCache, IntptrTy));
265 }
266}
267
268void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F,
269 BasicBlock &BB) {
270 BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
271 // Skip static allocas at the top of the entry block so they don't become
272 // dynamic when we split the block. If we used our optimized stack layout,
273 // then there will only be one alloca and it will come first.
274 for (; IP != BE; ++IP) {
275 AllocaInst *AI = dyn_cast<AllocaInst>(IP);
276 if (!AI || !AI->isStaticAlloca())
277 break;
278 }
279
280 DebugLoc EntryLoc = &BB == &F.getEntryBlock()
281 ? IP->getDebugLoc().getFnDebugLoc(*C)
282 : IP->getDebugLoc();
283 IRBuilder<> IRB(IP);
284 IRB.SetCurrentDebugLocation(EntryLoc);
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000285 Type *Int32Ty = IRB.getInt32Ty();
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000286 GlobalVariable *Guard = new GlobalVariable(
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000287 *F.getParent(), Int32Ty, false, GlobalValue::PrivateLinkage,
288 Constant::getNullValue(Int32Ty), "__sancov_gen_cov_" + F.getName());
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000289 LoadInst *Load = IRB.CreateLoad(Guard);
290 Load->setAtomic(Monotonic);
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000291 Load->setAlignment(4);
Kostya Serebryany543f3db2014-12-03 23:28:26 +0000292 Load->setMetadata(F.getParent()->getMDKindID("nosanitize"),
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000293 MDNode::get(*C, None));
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000294 Value *Cmp = IRB.CreateICmpSGE(Constant::getNullValue(Int32Ty), Load);
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000295 Instruction *Ins = SplitBlockAndInsertIfThen(
296 Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
297 IRB.SetInsertPoint(Ins);
298 IRB.SetCurrentDebugLocation(EntryLoc);
299 // __sanitizer_cov gets the PC of the instruction using GET_CALLER_PC.
Kostya Serebryany4cadd4a2014-11-24 18:49:53 +0000300 IRB.CreateCall(SanCovFunction, Guard);
Kostya Serebryany73762942014-12-16 21:24:15 +0000301 IRB.CreateCall(EmptyAsm); // Avoids callback merge.
Kostya Serebryany9fdeb372014-12-23 22:32:17 +0000302 Guards.push_back(Guard); // Save the guard for later.
Kostya Serebryany29a18dc2014-11-11 22:14:37 +0000303}
304
305char SanitizerCoverageModule::ID = 0;
306INITIALIZE_PASS(SanitizerCoverageModule, "sancov",
307 "SanitizerCoverage: TODO."
308 "ModulePass", false, false)
309ModulePass *llvm::createSanitizerCoverageModulePass(int CoverageLevel) {
310 return new SanitizerCoverageModule(CoverageLevel);
311}