Implemented AddressSanitizer::getPassName()
[oota-llvm.git] / lib / Transforms / Instrumentation / AddressSanitizer.cpp
1 //===-- AddressSanitizer.cpp - memory error detector ------------*- C++ -*-===//
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 // This file is a part of AddressSanitizer, an address sanity checker.
11 // Details of the algorithm:
12 //  http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DEBUG_TYPE "asan"
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/OwningPtr.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Function.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/LLVMContext.h"
27 #include "llvm/Module.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/DataTypes.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/IRBuilder.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/Regex.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Support/system_error.h"
36 #include "llvm/Target/TargetData.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Transforms/Instrumentation.h"
39 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
40 #include "llvm/Transforms/Utils/ModuleUtils.h"
41 #include "llvm/Type.h"
42
43 #include <string>
44 #include <algorithm>
45
46 using namespace llvm;
47
48 static const uint64_t kDefaultShadowScale = 3;
49 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
50 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
51
52 static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
53 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
54 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
55
56 static const char *kAsanModuleCtorName = "asan.module_ctor";
57 static const char *kAsanModuleDtorName = "asan.module_dtor";
58 static const int   kAsanCtorAndCtorPriority = 1;
59 static const char *kAsanReportErrorTemplate = "__asan_report_";
60 static const char *kAsanRegisterGlobalsName = "__asan_register_globals";
61 static const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
62 static const char *kAsanInitName = "__asan_init";
63 static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
64 static const char *kAsanMappingScaleName = "__asan_mapping_scale";
65 static const char *kAsanStackMallocName = "__asan_stack_malloc";
66 static const char *kAsanStackFreeName = "__asan_stack_free";
67
68 static const int kAsanStackLeftRedzoneMagic = 0xf1;
69 static const int kAsanStackMidRedzoneMagic = 0xf2;
70 static const int kAsanStackRightRedzoneMagic = 0xf3;
71 static const int kAsanStackPartialRedzoneMagic = 0xf4;
72
73 // Command-line flags.
74
75 // This flag may need to be replaced with -f[no-]asan-reads.
76 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
77        cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
78 static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
79        cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
80 // This flag may need to be replaced with -f[no]asan-stack.
81 static cl::opt<bool> ClStack("asan-stack",
82        cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
83 // This flag may need to be replaced with -f[no]asan-use-after-return.
84 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
85        cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
86 // This flag may need to be replaced with -f[no]asan-globals.
87 static cl::opt<bool> ClGlobals("asan-globals",
88        cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
89 static cl::opt<bool> ClMemIntrin("asan-memintrin",
90        cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
91 // This flag may need to be replaced with -fasan-blacklist.
92 static cl::opt<std::string>  ClBlackListFile("asan-blacklist",
93        cl::desc("File containing the list of functions to ignore "
94                 "during instrumentation"), cl::Hidden);
95
96 // These flags allow to change the shadow mapping.
97 // The shadow mapping looks like
98 //    Shadow = (Mem >> scale) + (1 << offset_log)
99 static cl::opt<int> ClMappingScale("asan-mapping-scale",
100        cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
101 static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
102        cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
103
104 // Optimization flags. Not user visible, used mostly for testing
105 // and benchmarking the tool.
106 static cl::opt<bool> ClOpt("asan-opt",
107        cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
108 static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
109        cl::desc("Instrument the same temp just once"), cl::Hidden,
110        cl::init(true));
111 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
112        cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
113
114 // Debug flags.
115 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
116                             cl::init(0));
117 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
118                                  cl::Hidden, cl::init(0));
119 static cl::opt<std::string> ClDebugFunc("asan-debug-func",
120                                         cl::Hidden, cl::desc("Debug func"));
121 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
122                                cl::Hidden, cl::init(-1));
123 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
124                                cl::Hidden, cl::init(-1));
125
126 namespace {
127
128 // Blacklisted functions are not instrumented.
129 // The blacklist file contains one or more lines like this:
130 // ---
131 // fun:FunctionWildCard
132 // ---
133 // This is similar to the "ignore" feature of ThreadSanitizer.
134 // http://code.google.com/p/data-race-test/wiki/ThreadSanitizerIgnores
135 class BlackList {
136  public:
137   BlackList(const std::string &Path);
138   bool isIn(const Function &F);
139  private:
140   Regex *Functions;
141 };
142
143 /// AddressSanitizer: instrument the code in module to find memory bugs.
144 struct AddressSanitizer : public ModulePass {
145   AddressSanitizer();
146   virtual const char *getPassName() const;
147   void instrumentMop(Instruction *I);
148   void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB,
149                          Value *Addr, uint32_t TypeSize, bool IsWrite);
150   Instruction *generateCrashCode(IRBuilder<> &IRB, Value *Addr,
151                                  bool IsWrite, uint32_t TypeSize);
152   bool instrumentMemIntrinsic(MemIntrinsic *MI);
153   void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
154                                   Value *Size,
155                                    Instruction *InsertBefore, bool IsWrite);
156   Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
157   bool handleFunction(Module &M, Function &F);
158   bool poisonStackInFunction(Module &M, Function &F);
159   virtual bool runOnModule(Module &M);
160   bool insertGlobalRedzones(Module &M);
161   BranchInst *splitBlockAndInsertIfThen(Instruction *SplitBefore, Value *Cmp);
162   static char ID;  // Pass identification, replacement for typeid
163
164  private:
165
166   uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
167     Type *Ty = AI->getAllocatedType();
168     uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
169     return SizeInBytes;
170   }
171   uint64_t getAlignedSize(uint64_t SizeInBytes) {
172     return ((SizeInBytes + RedzoneSize - 1)
173             / RedzoneSize) * RedzoneSize;
174   }
175   uint64_t getAlignedAllocaSize(AllocaInst *AI) {
176     uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
177     return getAlignedSize(SizeInBytes);
178   }
179
180   void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
181                    Value *ShadowBase, bool DoPoison);
182   bool LooksLikeCodeInBug11395(Instruction *I);
183
184   Module      *CurrentModule;
185   LLVMContext *C;
186   TargetData *TD;
187   uint64_t MappingOffset;
188   int MappingScale;
189   size_t RedzoneSize;
190   int LongSize;
191   Type *IntptrTy;
192   Type *IntptrPtrTy;
193   Function *AsanCtorFunction;
194   Function *AsanInitFunction;
195   Instruction *CtorInsertBefore;
196   OwningPtr<BlackList> BL;
197 };
198 }  // namespace
199
200 char AddressSanitizer::ID = 0;
201 INITIALIZE_PASS(AddressSanitizer, "asan",
202     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
203     false, false)
204 AddressSanitizer::AddressSanitizer() : ModulePass(ID) { }
205 ModulePass *llvm::createAddressSanitizerPass() {
206   return new AddressSanitizer();
207 }
208
209 const char *AddressSanitizer::getPassName() const {
210   return "AddressSanitizer";
211 }
212
213 // Create a constant for Str so that we can pass it to the run-time lib.
214 static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
215   Constant *StrConst = ConstantArray::get(M.getContext(), Str);
216   return new GlobalVariable(M, StrConst->getType(), true,
217                             GlobalValue::PrivateLinkage, StrConst, "");
218 }
219
220 // Split the basic block and insert an if-then code.
221 // Before:
222 //   Head
223 //   SplitBefore
224 //   Tail
225 // After:
226 //   Head
227 //   if (Cmp)
228 //     NewBasicBlock
229 //   SplitBefore
230 //   Tail
231 //
232 // Returns the NewBasicBlock's terminator.
233 BranchInst *AddressSanitizer::splitBlockAndInsertIfThen(
234     Instruction *SplitBefore, Value *Cmp) {
235   BasicBlock *Head = SplitBefore->getParent();
236   BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
237   TerminatorInst *HeadOldTerm = Head->getTerminator();
238   BasicBlock *NewBasicBlock =
239       BasicBlock::Create(*C, "", Head->getParent());
240   BranchInst *HeadNewTerm = BranchInst::Create(/*ifTrue*/NewBasicBlock,
241                                                /*ifFalse*/Tail,
242                                                Cmp);
243   ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
244
245   BranchInst *CheckTerm = BranchInst::Create(Tail, NewBasicBlock);
246   return CheckTerm;
247 }
248
249 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
250   // Shadow >> scale
251   Shadow = IRB.CreateLShr(Shadow, MappingScale);
252   if (MappingOffset == 0)
253     return Shadow;
254   // (Shadow >> scale) | offset
255   return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
256                                                MappingOffset));
257 }
258
259 void AddressSanitizer::instrumentMemIntrinsicParam(Instruction *OrigIns,
260     Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
261   // Check the first byte.
262   {
263     IRBuilder<> IRB(InsertBefore);
264     instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
265   }
266   // Check the last byte.
267   {
268     IRBuilder<> IRB(InsertBefore);
269     Value *SizeMinusOne = IRB.CreateSub(
270         Size, ConstantInt::get(Size->getType(), 1));
271     SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
272     Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
273     Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
274     instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
275   }
276 }
277
278 // Instrument memset/memmove/memcpy
279 bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
280   Value *Dst = MI->getDest();
281   MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
282   Value *Src = MemTran ? MemTran->getSource() : NULL;
283   Value *Length = MI->getLength();
284
285   Constant *ConstLength = dyn_cast<Constant>(Length);
286   Instruction *InsertBefore = MI;
287   if (ConstLength) {
288     if (ConstLength->isNullValue()) return false;
289   } else {
290     // The size is not a constant so it could be zero -- check at run-time.
291     IRBuilder<> IRB(InsertBefore);
292
293     Value *Cmp = IRB.CreateICmpNE(Length,
294                                    Constant::getNullValue(Length->getType()));
295     InsertBefore = splitBlockAndInsertIfThen(InsertBefore, Cmp);
296   }
297
298   instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
299   if (Src)
300     instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
301   return true;
302 }
303
304 static Value *getLDSTOperand(Instruction *I) {
305   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
306     return LI->getPointerOperand();
307   }
308   return cast<StoreInst>(*I).getPointerOperand();
309 }
310
311 void AddressSanitizer::instrumentMop(Instruction *I) {
312   int IsWrite = isa<StoreInst>(*I);
313   Value *Addr = getLDSTOperand(I);
314   if (ClOpt && ClOptGlobals && isa<GlobalVariable>(Addr)) {
315     // We are accessing a global scalar variable. Nothing to catch here.
316     return;
317   }
318   Type *OrigPtrTy = Addr->getType();
319   Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
320
321   assert(OrigTy->isSized());
322   uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
323
324   if (TypeSize != 8  && TypeSize != 16 &&
325       TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
326     // Ignore all unusual sizes.
327     return;
328   }
329
330   IRBuilder<> IRB(I);
331   instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
332 }
333
334 Instruction *AddressSanitizer::generateCrashCode(
335     IRBuilder<> &IRB, Value *Addr, bool IsWrite, uint32_t TypeSize) {
336   // IsWrite and TypeSize are encoded in the function name.
337   std::string FunctionName = std::string(kAsanReportErrorTemplate) +
338       (IsWrite ? "store" : "load") + itostr(TypeSize / 8);
339   Value *ReportWarningFunc = CurrentModule->getOrInsertFunction(
340       FunctionName, IRB.getVoidTy(), IntptrTy, NULL);
341   CallInst *Call = IRB.CreateCall(ReportWarningFunc, Addr);
342   Call->setDoesNotReturn();
343   return Call;
344 }
345
346 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
347                                          IRBuilder<> &IRB, Value *Addr,
348                                          uint32_t TypeSize, bool IsWrite) {
349   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
350
351   Type *ShadowTy  = IntegerType::get(
352       *C, std::max(8U, TypeSize >> MappingScale));
353   Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
354   Value *ShadowPtr = memToShadow(AddrLong, IRB);
355   Value *CmpVal = Constant::getNullValue(ShadowTy);
356   Value *ShadowValue = IRB.CreateLoad(
357       IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
358
359   Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
360
361   Instruction *CheckTerm = splitBlockAndInsertIfThen(
362       cast<Instruction>(Cmp)->getNextNode(), Cmp);
363   IRBuilder<> IRB2(CheckTerm);
364
365   size_t Granularity = 1 << MappingScale;
366   if (TypeSize < 8 * Granularity) {
367     // Addr & (Granularity - 1)
368     Value *Lower3Bits = IRB2.CreateAnd(
369         AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
370     // (Addr & (Granularity - 1)) + size - 1
371     Value *LastAccessedByte = IRB2.CreateAdd(
372         Lower3Bits, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
373     // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
374     LastAccessedByte = IRB2.CreateIntCast(
375         LastAccessedByte, IRB.getInt8Ty(), false);
376     // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
377     Value *Cmp2 = IRB2.CreateICmpSGE(LastAccessedByte, ShadowValue);
378
379     CheckTerm = splitBlockAndInsertIfThen(CheckTerm, Cmp2);
380   }
381
382   IRBuilder<> IRB1(CheckTerm);
383   Instruction *Crash = generateCrashCode(IRB1, AddrLong, IsWrite, TypeSize);
384   Crash->setDebugLoc(OrigIns->getDebugLoc());
385   ReplaceInstWithInst(CheckTerm, new UnreachableInst(*C));
386 }
387
388 // This function replaces all global variables with new variables that have
389 // trailing redzones. It also creates a function that poisons
390 // redzones and inserts this function into llvm.global_ctors.
391 bool AddressSanitizer::insertGlobalRedzones(Module &M) {
392   SmallVector<GlobalVariable *, 16> GlobalsToChange;
393
394   for (Module::GlobalListType::iterator G = M.getGlobalList().begin(),
395        E = M.getGlobalList().end(); G != E; ++G) {
396     Type *Ty = cast<PointerType>(G->getType())->getElementType();
397     DEBUG(dbgs() << "GLOBAL: " << *G);
398
399     if (!Ty->isSized()) continue;
400     if (!G->hasInitializer()) continue;
401     // Touch only those globals that will not be defined in other modules.
402     // Don't handle ODR type linkages since other modules may be built w/o asan.
403     if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
404         G->getLinkage() != GlobalVariable::PrivateLinkage &&
405         G->getLinkage() != GlobalVariable::InternalLinkage)
406       continue;
407     // Two problems with thread-locals:
408     //   - The address of the main thread's copy can't be computed at link-time.
409     //   - Need to poison all copies, not just the main thread's one.
410     if (G->isThreadLocal())
411       continue;
412     // For now, just ignore this Alloca if the alignment is large.
413     if (G->getAlignment() > RedzoneSize) continue;
414
415     // Ignore all the globals with the names starting with "\01L_OBJC_".
416     // Many of those are put into the .cstring section. The linker compresses
417     // that section by removing the spare \0s after the string terminator, so
418     // our redzones get broken.
419     if ((G->getName().find("\01L_OBJC_") == 0) ||
420         (G->getName().find("\01l_OBJC_") == 0)) {
421       DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
422       continue;
423     }
424
425     // Ignore the globals from the __OBJC section. The ObjC runtime assumes
426     // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
427     // them.
428     if (G->hasSection()) {
429       StringRef Section(G->getSection());
430       if ((Section.find("__OBJC,") == 0) ||
431           (Section.find("__DATA, __objc_") == 0)) {
432         DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
433         continue;
434       }
435     }
436
437     GlobalsToChange.push_back(G);
438   }
439
440   size_t n = GlobalsToChange.size();
441   if (n == 0) return false;
442
443   // A global is described by a structure
444   //   size_t beg;
445   //   size_t size;
446   //   size_t size_with_redzone;
447   //   const char *name;
448   // We initialize an array of such structures and pass it to a run-time call.
449   StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
450                                                IntptrTy, IntptrTy, NULL);
451   SmallVector<Constant *, 16> Initializers(n);
452
453   IRBuilder<> IRB(CtorInsertBefore);
454
455   for (size_t i = 0; i < n; i++) {
456     GlobalVariable *G = GlobalsToChange[i];
457     PointerType *PtrTy = cast<PointerType>(G->getType());
458     Type *Ty = PtrTy->getElementType();
459     uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
460     uint64_t RightRedzoneSize = RedzoneSize +
461         (RedzoneSize - (SizeInBytes % RedzoneSize));
462     Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
463
464     StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
465     Constant *NewInitializer = ConstantStruct::get(
466         NewTy, G->getInitializer(),
467         Constant::getNullValue(RightRedZoneTy), NULL);
468
469     SmallString<2048> DescriptionOfGlobal = G->getName();
470     DescriptionOfGlobal += " (";
471     DescriptionOfGlobal += M.getModuleIdentifier();
472     DescriptionOfGlobal += ")";
473     GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
474
475     // Create a new global variable with enough space for a redzone.
476     GlobalVariable *NewGlobal = new GlobalVariable(
477         M, NewTy, G->isConstant(), G->getLinkage(),
478         NewInitializer, "", G, G->isThreadLocal());
479     NewGlobal->copyAttributesFrom(G);
480     NewGlobal->setAlignment(RedzoneSize);
481
482     Value *Indices2[2];
483     Indices2[0] = IRB.getInt32(0);
484     Indices2[1] = IRB.getInt32(0);
485
486     G->replaceAllUsesWith(
487         ConstantExpr::getGetElementPtr(NewGlobal, Indices2, 2));
488     NewGlobal->takeName(G);
489     G->eraseFromParent();
490
491     Initializers[i] = ConstantStruct::get(
492         GlobalStructTy,
493         ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
494         ConstantInt::get(IntptrTy, SizeInBytes),
495         ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
496         ConstantExpr::getPointerCast(Name, IntptrTy),
497         NULL);
498     DEBUG(dbgs() << "NEW GLOBAL:\n" << *NewGlobal);
499   }
500
501   ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
502   GlobalVariable *AllGlobals = new GlobalVariable(
503       M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
504       ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
505
506   Function *AsanRegisterGlobals = cast<Function>(M.getOrInsertFunction(
507       kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
508   AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
509
510   IRB.CreateCall2(AsanRegisterGlobals,
511                   IRB.CreatePointerCast(AllGlobals, IntptrTy),
512                   ConstantInt::get(IntptrTy, n));
513
514   // We also need to unregister globals at the end, e.g. when a shared library
515   // gets closed.
516   Function *AsanDtorFunction = Function::Create(
517       FunctionType::get(Type::getVoidTy(*C), false),
518       GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
519   BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
520   IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
521   Function *AsanUnregisterGlobals = cast<Function>(M.getOrInsertFunction(
522       kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
523   AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
524
525   IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
526                        IRB.CreatePointerCast(AllGlobals, IntptrTy),
527                        ConstantInt::get(IntptrTy, n));
528   appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
529
530   DEBUG(dbgs() << M);
531   return true;
532 }
533
534 // virtual
535 bool AddressSanitizer::runOnModule(Module &M) {
536   // Initialize the private fields. No one has accessed them before.
537   TD = getAnalysisIfAvailable<TargetData>();
538   if (!TD)
539     return false;
540   BL.reset(new BlackList(ClBlackListFile));
541
542   CurrentModule = &M;
543   C = &(M.getContext());
544   LongSize = TD->getPointerSizeInBits();
545   IntptrTy = Type::getIntNTy(*C, LongSize);
546   IntptrPtrTy = PointerType::get(IntptrTy, 0);
547
548   AsanCtorFunction = Function::Create(
549       FunctionType::get(Type::getVoidTy(*C), false),
550       GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
551   BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
552   CtorInsertBefore = ReturnInst::Create(*C, AsanCtorBB);
553
554   // call __asan_init in the module ctor.
555   IRBuilder<> IRB(CtorInsertBefore);
556   AsanInitFunction = cast<Function>(
557       M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
558   AsanInitFunction->setLinkage(Function::ExternalLinkage);
559   IRB.CreateCall(AsanInitFunction);
560
561   MappingOffset = LongSize == 32
562       ? kDefaultShadowOffset32 : kDefaultShadowOffset64;
563   if (ClMappingOffsetLog >= 0) {
564     if (ClMappingOffsetLog == 0) {
565       // special case
566       MappingOffset = 0;
567     } else {
568       MappingOffset = 1ULL << ClMappingOffsetLog;
569     }
570   }
571   MappingScale = kDefaultShadowScale;
572   if (ClMappingScale) {
573     MappingScale = ClMappingScale;
574   }
575   // Redzone used for stack and globals is at least 32 bytes.
576   // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
577   RedzoneSize = std::max(32, (int)(1 << MappingScale));
578
579   bool Res = false;
580
581   if (ClGlobals)
582     Res |= insertGlobalRedzones(M);
583
584   // Tell the run-time the current values of mapping offset and scale.
585   GlobalValue *asan_mapping_offset =
586       new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
587                      ConstantInt::get(IntptrTy, MappingOffset),
588                      kAsanMappingOffsetName);
589   GlobalValue *asan_mapping_scale =
590       new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
591                          ConstantInt::get(IntptrTy, MappingScale),
592                          kAsanMappingScaleName);
593   // Read these globals, otherwise they may be optimized away.
594   IRB.CreateLoad(asan_mapping_scale, true);
595   IRB.CreateLoad(asan_mapping_offset, true);
596
597
598   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
599     if (F->isDeclaration()) continue;
600     Res |= handleFunction(M, *F);
601   }
602
603   appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
604
605   return Res;
606 }
607
608 bool AddressSanitizer::handleFunction(Module &M, Function &F) {
609   if (BL->isIn(F)) return false;
610   if (&F == AsanCtorFunction) return false;
611
612   if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
613     return false;
614   // We want to instrument every address only once per basic block
615   // (unless there are calls between uses).
616   SmallSet<Value*, 16> TempsToInstrument;
617   SmallVector<Instruction*, 16> ToInstrument;
618
619   // Fill the set of memory operations to instrument.
620   for (Function::iterator FI = F.begin(), FE = F.end();
621        FI != FE; ++FI) {
622     TempsToInstrument.clear();
623     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
624          BI != BE; ++BI) {
625       if (LooksLikeCodeInBug11395(BI)) return false;
626       if ((isa<LoadInst>(BI) && ClInstrumentReads) ||
627           (isa<StoreInst>(BI) && ClInstrumentWrites)) {
628         Value *Addr = getLDSTOperand(BI);
629         if (ClOpt && ClOptSameTemp) {
630           if (!TempsToInstrument.insert(Addr))
631             continue;  // We've seen this temp in the current BB.
632         }
633       } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
634         // ok, take it.
635       } else {
636         if (isa<CallInst>(BI)) {
637           // A call inside BB.
638           TempsToInstrument.clear();
639         }
640         continue;
641       }
642       ToInstrument.push_back(BI);
643     }
644   }
645
646   // Instrument.
647   int NumInstrumented = 0;
648   for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
649     Instruction *Inst = ToInstrument[i];
650     if (ClDebugMin < 0 || ClDebugMax < 0 ||
651         (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
652       if (isa<StoreInst>(Inst) || isa<LoadInst>(Inst))
653         instrumentMop(Inst);
654       else
655         instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
656     }
657     NumInstrumented++;
658   }
659
660   DEBUG(dbgs() << F);
661
662   bool ChangedStack = poisonStackInFunction(M, F);
663
664   // For each NSObject descendant having a +load method, this method is invoked
665   // by the ObjC runtime before any of the static constructors is called.
666   // Therefore we need to instrument such methods with a call to __asan_init
667   // at the beginning in order to initialize our runtime before any access to
668   // the shadow memory.
669   // We cannot just ignore these methods, because they may call other
670   // instrumented functions.
671   if (F.getName().find(" load]") != std::string::npos) {
672     IRBuilder<> IRB(F.begin()->begin());
673     IRB.CreateCall(AsanInitFunction);
674   }
675
676   return NumInstrumented > 0 || ChangedStack;
677 }
678
679 static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
680   if (ShadowRedzoneSize == 1) return PoisonByte;
681   if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
682   if (ShadowRedzoneSize == 4)
683     return (PoisonByte << 24) + (PoisonByte << 16) +
684         (PoisonByte << 8) + (PoisonByte);
685   assert(0 && "ShadowRedzoneSize is either 1, 2 or 4");
686   return 0;
687 }
688
689 static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
690                                             size_t Size,
691                                             size_t RedzoneSize,
692                                             size_t ShadowGranularity,
693                                             uint8_t Magic) {
694   for (size_t i = 0; i < RedzoneSize;
695        i+= ShadowGranularity, Shadow++) {
696     if (i + ShadowGranularity <= Size) {
697       *Shadow = 0;  // fully addressable
698     } else if (i >= Size) {
699       *Shadow = Magic;  // unaddressable
700     } else {
701       *Shadow = Size - i;  // first Size-i bytes are addressable
702     }
703   }
704 }
705
706 void AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
707                                    IRBuilder<> IRB,
708                                    Value *ShadowBase, bool DoPoison) {
709   size_t ShadowRZSize = RedzoneSize >> MappingScale;
710   assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
711   Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
712   Type *RZPtrTy = PointerType::get(RZTy, 0);
713
714   Value *PoisonLeft  = ConstantInt::get(RZTy,
715     ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
716   Value *PoisonMid   = ConstantInt::get(RZTy,
717     ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
718   Value *PoisonRight = ConstantInt::get(RZTy,
719     ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
720
721   // poison the first red zone.
722   IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
723
724   // poison all other red zones.
725   uint64_t Pos = RedzoneSize;
726   for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
727     AllocaInst *AI = AllocaVec[i];
728     uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
729     uint64_t AlignedSize = getAlignedAllocaSize(AI);
730     assert(AlignedSize - SizeInBytes < RedzoneSize);
731     Value *Ptr = NULL;
732
733     Pos += AlignedSize;
734
735     assert(ShadowBase->getType() == IntptrTy);
736     if (SizeInBytes < AlignedSize) {
737       // Poison the partial redzone at right
738       Ptr = IRB.CreateAdd(
739           ShadowBase, ConstantInt::get(IntptrTy,
740                                        (Pos >> MappingScale) - ShadowRZSize));
741       size_t AddressableBytes = RedzoneSize - (AlignedSize - SizeInBytes);
742       uint32_t Poison = 0;
743       if (DoPoison) {
744         PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
745                                         RedzoneSize,
746                                         1ULL << MappingScale,
747                                         kAsanStackPartialRedzoneMagic);
748       }
749       Value *PartialPoison = ConstantInt::get(RZTy, Poison);
750       IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
751     }
752
753     // Poison the full redzone at right.
754     Ptr = IRB.CreateAdd(ShadowBase,
755                         ConstantInt::get(IntptrTy, Pos >> MappingScale));
756     Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
757     IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
758
759     Pos += RedzoneSize;
760   }
761 }
762
763 // Workaround for bug 11395: we don't want to instrument stack in functions
764 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
765 // FIXME: remove once the bug 11395 is fixed.
766 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
767   if (LongSize != 32) return false;
768   CallInst *CI = dyn_cast<CallInst>(I);
769   if (!CI || !CI->isInlineAsm()) return false;
770   if (CI->getNumArgOperands() <= 5) return false;
771   // We have inline assembly with quite a few arguments.
772   return true;
773 }
774
775 // Find all static Alloca instructions and put
776 // poisoned red zones around all of them.
777 // Then unpoison everything back before the function returns.
778 //
779 // Stack poisoning does not play well with exception handling.
780 // When an exception is thrown, we essentially bypass the code
781 // that unpoisones the stack. This is why the run-time library has
782 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
783 // stack in the interceptor. This however does not work inside the
784 // actual function which catches the exception. Most likely because the
785 // compiler hoists the load of the shadow value somewhere too high.
786 // This causes asan to report a non-existing bug on 453.povray.
787 // It sounds like an LLVM bug.
788 bool AddressSanitizer::poisonStackInFunction(Module &M, Function &F) {
789   if (!ClStack) return false;
790   SmallVector<AllocaInst*, 16> AllocaVec;
791   SmallVector<Instruction*, 8> RetVec;
792   uint64_t TotalSize = 0;
793
794   // Filter out Alloca instructions we want (and can) handle.
795   // Collect Ret instructions.
796   for (Function::iterator FI = F.begin(), FE = F.end();
797        FI != FE; ++FI) {
798     BasicBlock &BB = *FI;
799     for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
800          BI != BE; ++BI) {
801       if (isa<ReturnInst>(BI)) {
802           RetVec.push_back(BI);
803           continue;
804       }
805
806       AllocaInst *AI = dyn_cast<AllocaInst>(BI);
807       if (!AI) continue;
808       if (AI->isArrayAllocation()) continue;
809       if (!AI->isStaticAlloca()) continue;
810       if (!AI->getAllocatedType()->isSized()) continue;
811       if (AI->getAlignment() > RedzoneSize) continue;
812       AllocaVec.push_back(AI);
813       uint64_t AlignedSize =  getAlignedAllocaSize(AI);
814       TotalSize += AlignedSize;
815     }
816   }
817
818   if (AllocaVec.empty()) return false;
819
820   uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize;
821
822   bool DoStackMalloc = ClUseAfterReturn
823       && LocalStackSize <= kMaxStackMallocSize;
824
825   Instruction *InsBefore = AllocaVec[0];
826   IRBuilder<> IRB(InsBefore);
827
828
829   Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
830   AllocaInst *MyAlloca =
831       new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
832   MyAlloca->setAlignment(RedzoneSize);
833   assert(MyAlloca->isStaticAlloca());
834   Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
835   Value *LocalStackBase = OrigStackBase;
836
837   if (DoStackMalloc) {
838     Value *AsanStackMallocFunc = M.getOrInsertFunction(
839         kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL);
840     LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
841         ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
842   }
843
844   // This string will be parsed by the run-time (DescribeStackAddress).
845   SmallString<2048> StackDescriptionStorage;
846   raw_svector_ostream StackDescription(StackDescriptionStorage);
847   StackDescription << F.getName() << " " << AllocaVec.size() << " ";
848
849   uint64_t Pos = RedzoneSize;
850   // Replace Alloca instructions with base+offset.
851   for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
852     AllocaInst *AI = AllocaVec[i];
853     uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
854     StringRef Name = AI->getName();
855     StackDescription << Pos << " " << SizeInBytes << " "
856                      << Name.size() << " " << Name << " ";
857     uint64_t AlignedSize = getAlignedAllocaSize(AI);
858     assert((AlignedSize % RedzoneSize) == 0);
859     AI->replaceAllUsesWith(
860         IRB.CreateIntToPtr(
861             IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
862             AI->getType()));
863     Pos += AlignedSize + RedzoneSize;
864   }
865   assert(Pos == LocalStackSize);
866
867   // Write the Magic value and the frame description constant to the redzone.
868   Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
869   IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
870                   BasePlus0);
871   Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
872                                    ConstantInt::get(IntptrTy, LongSize/8));
873   BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
874   Value *Description = IRB.CreatePointerCast(
875       createPrivateGlobalForString(M, StackDescription.str()),
876       IntptrTy);
877   IRB.CreateStore(Description, BasePlus1);
878
879   // Poison the stack redzones at the entry.
880   Value *ShadowBase = memToShadow(LocalStackBase, IRB);
881   PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
882
883   Value *AsanStackFreeFunc = NULL;
884   if (DoStackMalloc) {
885     AsanStackFreeFunc = M.getOrInsertFunction(
886         kAsanStackFreeName, IRB.getVoidTy(),
887         IntptrTy, IntptrTy, IntptrTy, NULL);
888   }
889
890   // Unpoison the stack before all ret instructions.
891   for (size_t i = 0, n = RetVec.size(); i < n; i++) {
892     Instruction *Ret = RetVec[i];
893     IRBuilder<> IRBRet(Ret);
894
895     // Mark the current frame as retired.
896     IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
897                        BasePlus0);
898     // Unpoison the stack.
899     PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
900
901     if (DoStackMalloc) {
902       IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
903                          ConstantInt::get(IntptrTy, LocalStackSize),
904                          OrigStackBase);
905     }
906   }
907
908   if (ClDebugStack) {
909     DEBUG(dbgs() << F);
910   }
911
912   return true;
913 }
914
915 BlackList::BlackList(const std::string &Path) {
916   Functions = NULL;
917   const char *kFunPrefix = "fun:";
918   if (!ClBlackListFile.size()) return;
919   std::string Fun;
920
921   OwningPtr<MemoryBuffer> File;
922   if (error_code EC = MemoryBuffer::getFile(ClBlackListFile.c_str(), File)) {
923     report_fatal_error("Can't open blacklist file " + ClBlackListFile + ": " +
924                        EC.message());
925   }
926   MemoryBuffer *Buff = File.take();
927   const char *Data = Buff->getBufferStart();
928   size_t DataLen = Buff->getBufferSize();
929   SmallVector<StringRef, 16> Lines;
930   SplitString(StringRef(Data, DataLen), Lines, "\n\r");
931   for (size_t i = 0, numLines = Lines.size(); i < numLines; i++) {
932     if (Lines[i].startswith(kFunPrefix)) {
933       std::string ThisFunc = Lines[i].substr(strlen(kFunPrefix));
934       std::string ThisFuncRE;
935       // add ThisFunc replacing * with .*
936       for (size_t j = 0, n = ThisFunc.size(); j < n; j++) {
937         if (ThisFunc[j] == '*')
938           ThisFuncRE += '.';
939         ThisFuncRE += ThisFunc[j];
940       }
941       // Check that the regexp is valid.
942       Regex CheckRE(ThisFuncRE);
943       std::string Error;
944       if (!CheckRE.isValid(Error))
945         report_fatal_error("malformed blacklist regex: " + ThisFunc +
946                            ": " + Error);
947       // Append to the final regexp.
948       if (Fun.size())
949         Fun += "|";
950       Fun += ThisFuncRE;
951     }
952   }
953   if (Fun.size()) {
954     Functions = new Regex(Fun);
955   }
956 }
957
958 bool BlackList::isIn(const Function &F) {
959   if (Functions) {
960     bool Res = Functions->match(F.getName());
961     return Res;
962   }
963   return false;
964 }