1 //===-- AddressSanitizer.cpp - memory error detector ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "asan"
18 #include "FunctionBlackList.h"
19 #include "llvm/Function.h"
20 #include "llvm/IRBuilder.h"
21 #include "llvm/InlineAsm.h"
22 #include "llvm/IntrinsicInst.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/Module.h"
25 #include "llvm/Type.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/OwningPtr.h"
28 #include "llvm/ADT/SmallSet.h"
29 #include "llvm/ADT/SmallString.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringExtras.h"
32 #include "llvm/ADT/Triple.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/DataTypes.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Support/system_error.h"
38 #include "llvm/Target/TargetData.h"
39 #include "llvm/Target/TargetMachine.h"
40 #include "llvm/Transforms/Instrumentation.h"
41 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
42 #include "llvm/Transforms/Utils/ModuleUtils.h"
49 static const uint64_t kDefaultShadowScale = 3;
50 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
51 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
52 static const uint64_t kDefaultShadowOffsetAndroid = 0;
54 static const size_t kMaxStackMallocSize = 1 << 16; // 64K
55 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
56 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
58 static const char *kAsanModuleCtorName = "asan.module_ctor";
59 static const char *kAsanModuleDtorName = "asan.module_dtor";
60 static const int kAsanCtorAndCtorPriority = 1;
61 static const char *kAsanReportErrorTemplate = "__asan_report_";
62 static const char *kAsanRegisterGlobalsName = "__asan_register_globals";
63 static const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
64 static const char *kAsanInitName = "__asan_init";
65 static const char *kAsanHandleNoReturnName = "__asan_handle_no_return";
66 static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
67 static const char *kAsanMappingScaleName = "__asan_mapping_scale";
68 static const char *kAsanStackMallocName = "__asan_stack_malloc";
69 static const char *kAsanStackFreeName = "__asan_stack_free";
71 static const int kAsanStackLeftRedzoneMagic = 0xf1;
72 static const int kAsanStackMidRedzoneMagic = 0xf2;
73 static const int kAsanStackRightRedzoneMagic = 0xf3;
74 static const int kAsanStackPartialRedzoneMagic = 0xf4;
76 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
77 static const size_t kNumberOfAccessSizes = 5;
79 // Command-line flags.
81 // This flag may need to be replaced with -f[no-]asan-reads.
82 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
83 cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
84 static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
85 cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
86 static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
87 cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
88 cl::Hidden, cl::init(true));
89 static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path",
90 cl::desc("use instrumentation with slow path for all accesses"),
91 cl::Hidden, cl::init(false));
92 // This flag limits the number of instructions to be instrumented
93 // in any given BB. Normally, this should be set to unlimited (INT_MAX),
94 // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
96 static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
98 cl::desc("maximal number of instructions to instrument in any given BB"),
100 // This flag may need to be replaced with -f[no]asan-stack.
101 static cl::opt<bool> ClStack("asan-stack",
102 cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
103 // This flag may need to be replaced with -f[no]asan-use-after-return.
104 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
105 cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
106 // This flag may need to be replaced with -f[no]asan-globals.
107 static cl::opt<bool> ClGlobals("asan-globals",
108 cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
109 static cl::opt<bool> ClMemIntrin("asan-memintrin",
110 cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
111 // This flag may need to be replaced with -fasan-blacklist.
112 static cl::opt<std::string> ClBlackListFile("asan-blacklist",
113 cl::desc("File containing the list of functions to ignore "
114 "during instrumentation"), cl::Hidden);
116 // These flags allow to change the shadow mapping.
117 // The shadow mapping looks like
118 // Shadow = (Mem >> scale) + (1 << offset_log)
119 static cl::opt<int> ClMappingScale("asan-mapping-scale",
120 cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
121 static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
122 cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
124 // Optimization flags. Not user visible, used mostly for testing
125 // and benchmarking the tool.
126 static cl::opt<bool> ClOpt("asan-opt",
127 cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
128 static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
129 cl::desc("Instrument the same temp just once"), cl::Hidden,
131 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
132 cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
135 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
137 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
138 cl::Hidden, cl::init(0));
139 static cl::opt<std::string> ClDebugFunc("asan-debug-func",
140 cl::Hidden, cl::desc("Debug func"));
141 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
142 cl::Hidden, cl::init(-1));
143 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
144 cl::Hidden, cl::init(-1));
148 /// An object of this type is created while instrumenting every function.
149 struct AsanFunctionContext {
150 AsanFunctionContext(Function &Function) : F(Function) { }
155 /// AddressSanitizer: instrument the code in module to find memory bugs.
156 struct AddressSanitizer : public ModulePass {
158 virtual const char *getPassName() const;
159 void instrumentMop(AsanFunctionContext &AFC, Instruction *I);
160 void instrumentAddress(AsanFunctionContext &AFC,
161 Instruction *OrigIns, IRBuilder<> &IRB,
162 Value *Addr, uint32_t TypeSize, bool IsWrite);
163 Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
164 Value *ShadowValue, uint32_t TypeSize);
165 Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
166 bool IsWrite, size_t AccessSizeIndex);
167 bool instrumentMemIntrinsic(AsanFunctionContext &AFC, MemIntrinsic *MI);
168 void instrumentMemIntrinsicParam(AsanFunctionContext &AFC,
169 Instruction *OrigIns, Value *Addr,
171 Instruction *InsertBefore, bool IsWrite);
172 Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
173 bool handleFunction(Module &M, Function &F);
174 bool maybeInsertAsanInitAtFunctionEntry(Function &F);
175 bool poisonStackInFunction(Module &M, Function &F);
176 virtual bool runOnModule(Module &M);
177 bool insertGlobalRedzones(Module &M);
178 static char ID; // Pass identification, replacement for typeid
182 uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
183 Type *Ty = AI->getAllocatedType();
184 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
187 uint64_t getAlignedSize(uint64_t SizeInBytes) {
188 return ((SizeInBytes + RedzoneSize - 1)
189 / RedzoneSize) * RedzoneSize;
191 uint64_t getAlignedAllocaSize(AllocaInst *AI) {
192 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
193 return getAlignedSize(SizeInBytes);
196 Function *checkInterfaceFunction(Constant *FuncOrBitcast);
197 void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
198 Value *ShadowBase, bool DoPoison);
199 bool LooksLikeCodeInBug11395(Instruction *I);
203 uint64_t MappingOffset;
209 Function *AsanCtorFunction;
210 Function *AsanInitFunction;
211 Instruction *CtorInsertBefore;
212 OwningPtr<FunctionBlackList> BL;
213 // This array is indexed by AccessIsWrite and log2(AccessSize).
214 Function *AsanErrorCallback[2][kNumberOfAccessSizes];
220 char AddressSanitizer::ID = 0;
221 INITIALIZE_PASS(AddressSanitizer, "asan",
222 "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
224 AddressSanitizer::AddressSanitizer() : ModulePass(ID) { }
225 ModulePass *llvm::createAddressSanitizerPass() {
226 return new AddressSanitizer();
229 const char *AddressSanitizer::getPassName() const {
230 return "AddressSanitizer";
233 static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
234 size_t Res = CountTrailingZeros_32(TypeSize / 8);
235 assert(Res < kNumberOfAccessSizes);
239 // Create a constant for Str so that we can pass it to the run-time lib.
240 static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
241 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
242 return new GlobalVariable(M, StrConst->getType(), true,
243 GlobalValue::PrivateLinkage, StrConst, "");
246 // Split the basic block and insert an if-then code.
257 // ThenBlock block is created and its terminator is returned.
258 // If Unreachable, ThenBlock is terminated with UnreachableInst, otherwise
259 // it is terminated with BranchInst to Tail.
260 static TerminatorInst *splitBlockAndInsertIfThen(Value *Cmp, bool Unreachable) {
261 Instruction *SplitBefore = cast<Instruction>(Cmp)->getNextNode();
262 BasicBlock *Head = SplitBefore->getParent();
263 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
264 TerminatorInst *HeadOldTerm = Head->getTerminator();
265 LLVMContext &C = Head->getParent()->getParent()->getContext();
266 BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
267 TerminatorInst *CheckTerm;
269 CheckTerm = new UnreachableInst(C, ThenBlock);
271 CheckTerm = BranchInst::Create(Tail, ThenBlock);
272 BranchInst *HeadNewTerm =
273 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cmp);
274 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
278 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
280 Shadow = IRB.CreateLShr(Shadow, MappingScale);
281 if (MappingOffset == 0)
283 // (Shadow >> scale) | offset
284 return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
288 void AddressSanitizer::instrumentMemIntrinsicParam(
289 AsanFunctionContext &AFC, Instruction *OrigIns,
290 Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
291 // Check the first byte.
293 IRBuilder<> IRB(InsertBefore);
294 instrumentAddress(AFC, OrigIns, IRB, Addr, 8, IsWrite);
296 // Check the last byte.
298 IRBuilder<> IRB(InsertBefore);
299 Value *SizeMinusOne = IRB.CreateSub(
300 Size, ConstantInt::get(Size->getType(), 1));
301 SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
302 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
303 Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
304 instrumentAddress(AFC, OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
308 // Instrument memset/memmove/memcpy
309 bool AddressSanitizer::instrumentMemIntrinsic(AsanFunctionContext &AFC,
311 Value *Dst = MI->getDest();
312 MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
313 Value *Src = MemTran ? MemTran->getSource() : 0;
314 Value *Length = MI->getLength();
316 Constant *ConstLength = dyn_cast<Constant>(Length);
317 Instruction *InsertBefore = MI;
319 if (ConstLength->isNullValue()) return false;
321 // The size is not a constant so it could be zero -- check at run-time.
322 IRBuilder<> IRB(InsertBefore);
324 Value *Cmp = IRB.CreateICmpNE(Length,
325 Constant::getNullValue(Length->getType()));
326 InsertBefore = splitBlockAndInsertIfThen(Cmp, false);
329 instrumentMemIntrinsicParam(AFC, MI, Dst, Length, InsertBefore, true);
331 instrumentMemIntrinsicParam(AFC, MI, Src, Length, InsertBefore, false);
335 // If I is an interesting memory access, return the PointerOperand
336 // and set IsWrite. Otherwise return NULL.
337 static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
338 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
339 if (!ClInstrumentReads) return NULL;
341 return LI->getPointerOperand();
343 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
344 if (!ClInstrumentWrites) return NULL;
346 return SI->getPointerOperand();
348 if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
349 if (!ClInstrumentAtomics) return NULL;
351 return RMW->getPointerOperand();
353 if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
354 if (!ClInstrumentAtomics) return NULL;
356 return XCHG->getPointerOperand();
361 void AddressSanitizer::instrumentMop(AsanFunctionContext &AFC, Instruction *I) {
363 Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
365 if (ClOpt && ClOptGlobals && isa<GlobalVariable>(Addr)) {
366 // We are accessing a global scalar variable. Nothing to catch here.
369 Type *OrigPtrTy = Addr->getType();
370 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
372 assert(OrigTy->isSized());
373 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
375 if (TypeSize != 8 && TypeSize != 16 &&
376 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
377 // Ignore all unusual sizes.
382 instrumentAddress(AFC, I, IRB, Addr, TypeSize, IsWrite);
385 // Validate the result of Module::getOrInsertFunction called for an interface
386 // function of AddressSanitizer. If the instrumented module defines a function
387 // with the same name, their prototypes must match, otherwise
388 // getOrInsertFunction returns a bitcast.
389 Function *AddressSanitizer::checkInterfaceFunction(Constant *FuncOrBitcast) {
390 if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
391 FuncOrBitcast->dump();
392 report_fatal_error("trying to redefine an AddressSanitizer "
393 "interface function");
396 Instruction *AddressSanitizer::generateCrashCode(
397 Instruction *InsertBefore, Value *Addr,
398 bool IsWrite, size_t AccessSizeIndex) {
399 IRBuilder<> IRB(InsertBefore);
400 CallInst *Call = IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex],
402 // We don't do Call->setDoesNotReturn() because the BB already has
403 // UnreachableInst at the end.
404 // This EmptyAsm is required to avoid callback merge.
405 IRB.CreateCall(EmptyAsm);
409 Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
412 size_t Granularity = 1 << MappingScale;
413 // Addr & (Granularity - 1)
414 Value *LastAccessedByte = IRB.CreateAnd(
415 AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
416 // (Addr & (Granularity - 1)) + size - 1
417 if (TypeSize / 8 > 1)
418 LastAccessedByte = IRB.CreateAdd(
419 LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
420 // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
421 LastAccessedByte = IRB.CreateIntCast(
422 LastAccessedByte, ShadowValue->getType(), false);
423 // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
424 return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
427 void AddressSanitizer::instrumentAddress(AsanFunctionContext &AFC,
428 Instruction *OrigIns,
429 IRBuilder<> &IRB, Value *Addr,
430 uint32_t TypeSize, bool IsWrite) {
431 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
433 Type *ShadowTy = IntegerType::get(
434 *C, std::max(8U, TypeSize >> MappingScale));
435 Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
436 Value *ShadowPtr = memToShadow(AddrLong, IRB);
437 Value *CmpVal = Constant::getNullValue(ShadowTy);
438 Value *ShadowValue = IRB.CreateLoad(
439 IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
441 Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
442 size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
443 size_t Granularity = 1 << MappingScale;
444 TerminatorInst *CrashTerm = 0;
446 if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
447 TerminatorInst *CheckTerm = splitBlockAndInsertIfThen(Cmp, false);
448 assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
449 BasicBlock *NextBB = CheckTerm->getSuccessor(0);
450 IRB.SetInsertPoint(CheckTerm);
451 Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
452 BasicBlock *CrashBlock = BasicBlock::Create(*C, "", &AFC.F, NextBB);
453 CrashTerm = new UnreachableInst(*C, CrashBlock);
454 BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
455 ReplaceInstWithInst(CheckTerm, NewTerm);
457 CrashTerm = splitBlockAndInsertIfThen(Cmp, true);
461 generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex);
462 Crash->setDebugLoc(OrigIns->getDebugLoc());
465 // This function replaces all global variables with new variables that have
466 // trailing redzones. It also creates a function that poisons
467 // redzones and inserts this function into llvm.global_ctors.
468 bool AddressSanitizer::insertGlobalRedzones(Module &M) {
469 SmallVector<GlobalVariable *, 16> GlobalsToChange;
471 for (Module::GlobalListType::iterator G = M.getGlobalList().begin(),
472 E = M.getGlobalList().end(); G != E; ++G) {
473 Type *Ty = cast<PointerType>(G->getType())->getElementType();
474 DEBUG(dbgs() << "GLOBAL: " << *G);
476 if (!Ty->isSized()) continue;
477 if (!G->hasInitializer()) continue;
478 // Touch only those globals that will not be defined in other modules.
479 // Don't handle ODR type linkages since other modules may be built w/o asan.
480 if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
481 G->getLinkage() != GlobalVariable::PrivateLinkage &&
482 G->getLinkage() != GlobalVariable::InternalLinkage)
484 // Two problems with thread-locals:
485 // - The address of the main thread's copy can't be computed at link-time.
486 // - Need to poison all copies, not just the main thread's one.
487 if (G->isThreadLocal())
489 // For now, just ignore this Alloca if the alignment is large.
490 if (G->getAlignment() > RedzoneSize) continue;
492 // Ignore all the globals with the names starting with "\01L_OBJC_".
493 // Many of those are put into the .cstring section. The linker compresses
494 // that section by removing the spare \0s after the string terminator, so
495 // our redzones get broken.
496 if ((G->getName().find("\01L_OBJC_") == 0) ||
497 (G->getName().find("\01l_OBJC_") == 0)) {
498 DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
502 if (G->hasSection()) {
503 StringRef Section(G->getSection());
504 // Ignore the globals from the __OBJC section. The ObjC runtime assumes
505 // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
507 if ((Section.find("__OBJC,") == 0) ||
508 (Section.find("__DATA, __objc_") == 0)) {
509 DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
512 // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
513 // Constant CFString instances are compiled in the following way:
514 // -- the string buffer is emitted into
515 // __TEXT,__cstring,cstring_literals
516 // -- the constant NSConstantString structure referencing that buffer
517 // is placed into __DATA,__cfstring
518 // Therefore there's no point in placing redzones into __DATA,__cfstring.
519 // Moreover, it causes the linker to crash on OS X 10.7
520 if (Section.find("__DATA,__cfstring") == 0) {
521 DEBUG(dbgs() << "Ignoring CFString: " << *G);
526 GlobalsToChange.push_back(G);
529 size_t n = GlobalsToChange.size();
530 if (n == 0) return false;
532 // A global is described by a structure
535 // size_t size_with_redzone;
537 // We initialize an array of such structures and pass it to a run-time call.
538 StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
539 IntptrTy, IntptrTy, NULL);
540 SmallVector<Constant *, 16> Initializers(n);
542 IRBuilder<> IRB(CtorInsertBefore);
544 for (size_t i = 0; i < n; i++) {
545 GlobalVariable *G = GlobalsToChange[i];
546 PointerType *PtrTy = cast<PointerType>(G->getType());
547 Type *Ty = PtrTy->getElementType();
548 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
549 uint64_t RightRedzoneSize = RedzoneSize +
550 (RedzoneSize - (SizeInBytes % RedzoneSize));
551 Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
553 StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
554 Constant *NewInitializer = ConstantStruct::get(
555 NewTy, G->getInitializer(),
556 Constant::getNullValue(RightRedZoneTy), NULL);
558 SmallString<2048> DescriptionOfGlobal = G->getName();
559 DescriptionOfGlobal += " (";
560 DescriptionOfGlobal += M.getModuleIdentifier();
561 DescriptionOfGlobal += ")";
562 GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
564 // Create a new global variable with enough space for a redzone.
565 GlobalVariable *NewGlobal = new GlobalVariable(
566 M, NewTy, G->isConstant(), G->getLinkage(),
567 NewInitializer, "", G, G->getThreadLocalMode());
568 NewGlobal->copyAttributesFrom(G);
569 NewGlobal->setAlignment(RedzoneSize);
572 Indices2[0] = IRB.getInt32(0);
573 Indices2[1] = IRB.getInt32(0);
575 G->replaceAllUsesWith(
576 ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
577 NewGlobal->takeName(G);
578 G->eraseFromParent();
580 Initializers[i] = ConstantStruct::get(
582 ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
583 ConstantInt::get(IntptrTy, SizeInBytes),
584 ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
585 ConstantExpr::getPointerCast(Name, IntptrTy),
587 DEBUG(dbgs() << "NEW GLOBAL:\n" << *NewGlobal);
590 ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
591 GlobalVariable *AllGlobals = new GlobalVariable(
592 M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
593 ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
595 Function *AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
596 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
597 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
599 IRB.CreateCall2(AsanRegisterGlobals,
600 IRB.CreatePointerCast(AllGlobals, IntptrTy),
601 ConstantInt::get(IntptrTy, n));
603 // We also need to unregister globals at the end, e.g. when a shared library
605 Function *AsanDtorFunction = Function::Create(
606 FunctionType::get(Type::getVoidTy(*C), false),
607 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
608 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
609 IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
610 Function *AsanUnregisterGlobals =
611 checkInterfaceFunction(M.getOrInsertFunction(
612 kAsanUnregisterGlobalsName,
613 IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
614 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
616 IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
617 IRB.CreatePointerCast(AllGlobals, IntptrTy),
618 ConstantInt::get(IntptrTy, n));
619 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
626 bool AddressSanitizer::runOnModule(Module &M) {
627 // Initialize the private fields. No one has accessed them before.
628 TD = getAnalysisIfAvailable<TargetData>();
631 BL.reset(new FunctionBlackList(ClBlackListFile));
633 C = &(M.getContext());
634 LongSize = TD->getPointerSizeInBits();
635 IntptrTy = Type::getIntNTy(*C, LongSize);
636 IntptrPtrTy = PointerType::get(IntptrTy, 0);
638 AsanCtorFunction = Function::Create(
639 FunctionType::get(Type::getVoidTy(*C), false),
640 GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
641 BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
642 CtorInsertBefore = ReturnInst::Create(*C, AsanCtorBB);
644 // call __asan_init in the module ctor.
645 IRBuilder<> IRB(CtorInsertBefore);
646 AsanInitFunction = checkInterfaceFunction(
647 M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
648 AsanInitFunction->setLinkage(Function::ExternalLinkage);
649 IRB.CreateCall(AsanInitFunction);
651 // Create __asan_report* callbacks.
652 for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
653 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
655 // IsWrite and TypeSize are encoded in the function name.
656 std::string FunctionName = std::string(kAsanReportErrorTemplate) +
657 (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
658 // If we are merging crash callbacks, they have two parameters.
659 AsanErrorCallback[AccessIsWrite][AccessSizeIndex] = cast<Function>(
660 M.getOrInsertFunction(FunctionName, IRB.getVoidTy(), IntptrTy, NULL));
663 // We insert an empty inline asm after __asan_report* to avoid callback merge.
664 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
665 StringRef(""), StringRef(""),
666 /*hasSideEffects=*/true);
668 llvm::Triple targetTriple(M.getTargetTriple());
669 bool isAndroid = targetTriple.getEnvironment() == llvm::Triple::ANDROIDEABI;
671 MappingOffset = isAndroid ? kDefaultShadowOffsetAndroid :
672 (LongSize == 32 ? kDefaultShadowOffset32 : kDefaultShadowOffset64);
673 if (ClMappingOffsetLog >= 0) {
674 if (ClMappingOffsetLog == 0) {
678 MappingOffset = 1ULL << ClMappingOffsetLog;
681 MappingScale = kDefaultShadowScale;
682 if (ClMappingScale) {
683 MappingScale = ClMappingScale;
685 // Redzone used for stack and globals is at least 32 bytes.
686 // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
687 RedzoneSize = std::max(32, (int)(1 << MappingScale));
692 Res |= insertGlobalRedzones(M);
694 if (ClMappingOffsetLog >= 0) {
695 // Tell the run-time the current values of mapping offset and scale.
696 GlobalValue *asan_mapping_offset =
697 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
698 ConstantInt::get(IntptrTy, MappingOffset),
699 kAsanMappingOffsetName);
700 // Read the global, otherwise it may be optimized away.
701 IRB.CreateLoad(asan_mapping_offset, true);
703 if (ClMappingScale) {
704 GlobalValue *asan_mapping_scale =
705 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
706 ConstantInt::get(IntptrTy, MappingScale),
707 kAsanMappingScaleName);
708 // Read the global, otherwise it may be optimized away.
709 IRB.CreateLoad(asan_mapping_scale, true);
713 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
714 if (F->isDeclaration()) continue;
715 Res |= handleFunction(M, *F);
718 appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
723 bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
724 // For each NSObject descendant having a +load method, this method is invoked
725 // by the ObjC runtime before any of the static constructors is called.
726 // Therefore we need to instrument such methods with a call to __asan_init
727 // at the beginning in order to initialize our runtime before any access to
728 // the shadow memory.
729 // We cannot just ignore these methods, because they may call other
730 // instrumented functions.
731 if (F.getName().find(" load]") != std::string::npos) {
732 IRBuilder<> IRB(F.begin()->begin());
733 IRB.CreateCall(AsanInitFunction);
739 bool AddressSanitizer::handleFunction(Module &M, Function &F) {
740 if (BL->isIn(F)) return false;
741 if (&F == AsanCtorFunction) return false;
743 // If needed, insert __asan_init before checking for AddressSafety attr.
744 maybeInsertAsanInitAtFunctionEntry(F);
746 if (!F.hasFnAttr(Attribute::AddressSafety)) return false;
748 if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
750 // We want to instrument every address only once per basic block
751 // (unless there are calls between uses).
752 SmallSet<Value*, 16> TempsToInstrument;
753 SmallVector<Instruction*, 16> ToInstrument;
754 SmallVector<Instruction*, 8> NoReturnCalls;
757 // Fill the set of memory operations to instrument.
758 for (Function::iterator FI = F.begin(), FE = F.end();
760 TempsToInstrument.clear();
761 int NumInsnsPerBB = 0;
762 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
764 if (LooksLikeCodeInBug11395(BI)) return false;
765 if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) {
766 if (ClOpt && ClOptSameTemp) {
767 if (!TempsToInstrument.insert(Addr))
768 continue; // We've seen this temp in the current BB.
770 } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
773 if (CallInst *CI = dyn_cast<CallInst>(BI)) {
775 TempsToInstrument.clear();
776 if (CI->doesNotReturn()) {
777 NoReturnCalls.push_back(CI);
782 ToInstrument.push_back(BI);
784 if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
789 AsanFunctionContext AFC(F);
792 int NumInstrumented = 0;
793 for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
794 Instruction *Inst = ToInstrument[i];
795 if (ClDebugMin < 0 || ClDebugMax < 0 ||
796 (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
797 if (isInterestingMemoryAccess(Inst, &IsWrite))
798 instrumentMop(AFC, Inst);
800 instrumentMemIntrinsic(AFC, cast<MemIntrinsic>(Inst));
807 bool ChangedStack = poisonStackInFunction(M, F);
809 // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
810 // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
811 for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
812 Instruction *CI = NoReturnCalls[i];
814 IRB.CreateCall(M.getOrInsertFunction(kAsanHandleNoReturnName,
815 IRB.getVoidTy(), NULL));
818 return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
821 static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
822 if (ShadowRedzoneSize == 1) return PoisonByte;
823 if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
824 if (ShadowRedzoneSize == 4)
825 return (PoisonByte << 24) + (PoisonByte << 16) +
826 (PoisonByte << 8) + (PoisonByte);
827 llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
830 static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
833 size_t ShadowGranularity,
835 for (size_t i = 0; i < RedzoneSize;
836 i+= ShadowGranularity, Shadow++) {
837 if (i + ShadowGranularity <= Size) {
838 *Shadow = 0; // fully addressable
839 } else if (i >= Size) {
840 *Shadow = Magic; // unaddressable
842 *Shadow = Size - i; // first Size-i bytes are addressable
847 void AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
849 Value *ShadowBase, bool DoPoison) {
850 size_t ShadowRZSize = RedzoneSize >> MappingScale;
851 assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
852 Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
853 Type *RZPtrTy = PointerType::get(RZTy, 0);
855 Value *PoisonLeft = ConstantInt::get(RZTy,
856 ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
857 Value *PoisonMid = ConstantInt::get(RZTy,
858 ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
859 Value *PoisonRight = ConstantInt::get(RZTy,
860 ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
862 // poison the first red zone.
863 IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
865 // poison all other red zones.
866 uint64_t Pos = RedzoneSize;
867 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
868 AllocaInst *AI = AllocaVec[i];
869 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
870 uint64_t AlignedSize = getAlignedAllocaSize(AI);
871 assert(AlignedSize - SizeInBytes < RedzoneSize);
876 assert(ShadowBase->getType() == IntptrTy);
877 if (SizeInBytes < AlignedSize) {
878 // Poison the partial redzone at right
880 ShadowBase, ConstantInt::get(IntptrTy,
881 (Pos >> MappingScale) - ShadowRZSize));
882 size_t AddressableBytes = RedzoneSize - (AlignedSize - SizeInBytes);
885 PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
887 1ULL << MappingScale,
888 kAsanStackPartialRedzoneMagic);
890 Value *PartialPoison = ConstantInt::get(RZTy, Poison);
891 IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
894 // Poison the full redzone at right.
895 Ptr = IRB.CreateAdd(ShadowBase,
896 ConstantInt::get(IntptrTy, Pos >> MappingScale));
897 Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
898 IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
904 // Workaround for bug 11395: we don't want to instrument stack in functions
905 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
906 // FIXME: remove once the bug 11395 is fixed.
907 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
908 if (LongSize != 32) return false;
909 CallInst *CI = dyn_cast<CallInst>(I);
910 if (!CI || !CI->isInlineAsm()) return false;
911 if (CI->getNumArgOperands() <= 5) return false;
912 // We have inline assembly with quite a few arguments.
916 // Find all static Alloca instructions and put
917 // poisoned red zones around all of them.
918 // Then unpoison everything back before the function returns.
920 // Stack poisoning does not play well with exception handling.
921 // When an exception is thrown, we essentially bypass the code
922 // that unpoisones the stack. This is why the run-time library has
923 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
924 // stack in the interceptor. This however does not work inside the
925 // actual function which catches the exception. Most likely because the
926 // compiler hoists the load of the shadow value somewhere too high.
927 // This causes asan to report a non-existing bug on 453.povray.
928 // It sounds like an LLVM bug.
929 bool AddressSanitizer::poisonStackInFunction(Module &M, Function &F) {
930 if (!ClStack) return false;
931 SmallVector<AllocaInst*, 16> AllocaVec;
932 SmallVector<Instruction*, 8> RetVec;
933 uint64_t TotalSize = 0;
935 // Filter out Alloca instructions we want (and can) handle.
936 // Collect Ret instructions.
937 for (Function::iterator FI = F.begin(), FE = F.end();
939 BasicBlock &BB = *FI;
940 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
942 if (isa<ReturnInst>(BI)) {
943 RetVec.push_back(BI);
947 AllocaInst *AI = dyn_cast<AllocaInst>(BI);
949 if (AI->isArrayAllocation()) continue;
950 if (!AI->isStaticAlloca()) continue;
951 if (!AI->getAllocatedType()->isSized()) continue;
952 if (AI->getAlignment() > RedzoneSize) continue;
953 AllocaVec.push_back(AI);
954 uint64_t AlignedSize = getAlignedAllocaSize(AI);
955 TotalSize += AlignedSize;
959 if (AllocaVec.empty()) return false;
961 uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize;
963 bool DoStackMalloc = ClUseAfterReturn
964 && LocalStackSize <= kMaxStackMallocSize;
966 Instruction *InsBefore = AllocaVec[0];
967 IRBuilder<> IRB(InsBefore);
970 Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
971 AllocaInst *MyAlloca =
972 new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
973 MyAlloca->setAlignment(RedzoneSize);
974 assert(MyAlloca->isStaticAlloca());
975 Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
976 Value *LocalStackBase = OrigStackBase;
979 Value *AsanStackMallocFunc = M.getOrInsertFunction(
980 kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL);
981 LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
982 ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
985 // This string will be parsed by the run-time (DescribeStackAddress).
986 SmallString<2048> StackDescriptionStorage;
987 raw_svector_ostream StackDescription(StackDescriptionStorage);
988 StackDescription << F.getName() << " " << AllocaVec.size() << " ";
990 uint64_t Pos = RedzoneSize;
991 // Replace Alloca instructions with base+offset.
992 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
993 AllocaInst *AI = AllocaVec[i];
994 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
995 StringRef Name = AI->getName();
996 StackDescription << Pos << " " << SizeInBytes << " "
997 << Name.size() << " " << Name << " ";
998 uint64_t AlignedSize = getAlignedAllocaSize(AI);
999 assert((AlignedSize % RedzoneSize) == 0);
1000 AI->replaceAllUsesWith(
1002 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
1004 Pos += AlignedSize + RedzoneSize;
1006 assert(Pos == LocalStackSize);
1008 // Write the Magic value and the frame description constant to the redzone.
1009 Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1010 IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1012 Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
1013 ConstantInt::get(IntptrTy, LongSize/8));
1014 BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
1015 Value *Description = IRB.CreatePointerCast(
1016 createPrivateGlobalForString(M, StackDescription.str()),
1018 IRB.CreateStore(Description, BasePlus1);
1020 // Poison the stack redzones at the entry.
1021 Value *ShadowBase = memToShadow(LocalStackBase, IRB);
1022 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
1024 Value *AsanStackFreeFunc = NULL;
1025 if (DoStackMalloc) {
1026 AsanStackFreeFunc = M.getOrInsertFunction(
1027 kAsanStackFreeName, IRB.getVoidTy(),
1028 IntptrTy, IntptrTy, IntptrTy, NULL);
1031 // Unpoison the stack before all ret instructions.
1032 for (size_t i = 0, n = RetVec.size(); i < n; i++) {
1033 Instruction *Ret = RetVec[i];
1034 IRBuilder<> IRBRet(Ret);
1036 // Mark the current frame as retired.
1037 IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1039 // Unpoison the stack.
1040 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
1042 if (DoStackMalloc) {
1043 IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
1044 ConstantInt::get(IntptrTy, LocalStackSize),