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 "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"
48 static const uint64_t kDefaultShadowScale = 3;
49 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
50 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
52 static const size_t kMaxStackMallocSize = 1 << 16; // 64K
53 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
54 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
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";
68 static const int kAsanStackLeftRedzoneMagic = 0xf1;
69 static const int kAsanStackMidRedzoneMagic = 0xf2;
70 static const int kAsanStackRightRedzoneMagic = 0xf3;
71 static const int kAsanStackPartialRedzoneMagic = 0xf4;
73 // Command-line flags.
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);
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));
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,
111 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
112 cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
115 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
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));
128 // Blacklisted functions are not instrumented.
129 // The blacklist file contains one or more lines like this:
131 // fun:FunctionWildCard
133 // This is similar to the "ignore" feature of ThreadSanitizer.
134 // http://code.google.com/p/data-race-test/wiki/ThreadSanitizerIgnores
137 BlackList(const std::string &Path);
138 bool isIn(const Function &F);
143 /// AddressSanitizer: instrument the code in module to find memory bugs.
144 struct AddressSanitizer : public ModulePass {
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,
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
166 uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
167 Type *Ty = AI->getAllocatedType();
168 uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
171 uint64_t getAlignedSize(uint64_t SizeInBytes) {
172 return ((SizeInBytes + RedzoneSize - 1)
173 / RedzoneSize) * RedzoneSize;
175 uint64_t getAlignedAllocaSize(AllocaInst *AI) {
176 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
177 return getAlignedSize(SizeInBytes);
180 void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
181 Value *ShadowBase, bool DoPoison);
182 bool LooksLikeCodeInBug11395(Instruction *I);
184 Module *CurrentModule;
187 uint64_t MappingOffset;
193 Function *AsanCtorFunction;
194 Function *AsanInitFunction;
195 Instruction *CtorInsertBefore;
196 OwningPtr<BlackList> BL;
200 char AddressSanitizer::ID = 0;
201 INITIALIZE_PASS(AddressSanitizer, "asan",
202 "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
204 AddressSanitizer::AddressSanitizer() : ModulePass(ID) { }
205 ModulePass *llvm::createAddressSanitizerPass() {
206 return new AddressSanitizer();
209 const char *AddressSanitizer::getPassName() const {
210 return "AddressSanitizer";
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, "");
220 // Split the basic block and insert an if-then code.
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,
243 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
245 BranchInst *CheckTerm = BranchInst::Create(Tail, NewBasicBlock);
249 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
251 Shadow = IRB.CreateLShr(Shadow, MappingScale);
252 if (MappingOffset == 0)
254 // (Shadow >> scale) | offset
255 return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
259 void AddressSanitizer::instrumentMemIntrinsicParam(Instruction *OrigIns,
260 Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
261 // Check the first byte.
263 IRBuilder<> IRB(InsertBefore);
264 instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
266 // Check the last byte.
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);
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();
285 Constant *ConstLength = dyn_cast<Constant>(Length);
286 Instruction *InsertBefore = MI;
288 if (ConstLength->isNullValue()) return false;
290 // The size is not a constant so it could be zero -- check at run-time.
291 IRBuilder<> IRB(InsertBefore);
293 Value *Cmp = IRB.CreateICmpNE(Length,
294 Constant::getNullValue(Length->getType()));
295 InsertBefore = splitBlockAndInsertIfThen(InsertBefore, Cmp);
298 instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
300 instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
304 static Value *getLDSTOperand(Instruction *I) {
305 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
306 return LI->getPointerOperand();
308 return cast<StoreInst>(*I).getPointerOperand();
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.
318 Type *OrigPtrTy = Addr->getType();
319 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
321 assert(OrigTy->isSized());
322 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
324 if (TypeSize != 8 && TypeSize != 16 &&
325 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
326 // Ignore all unusual sizes.
331 instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
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();
346 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
347 IRBuilder<> &IRB, Value *Addr,
348 uint32_t TypeSize, bool IsWrite) {
349 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
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));
359 Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
361 Instruction *CheckTerm = splitBlockAndInsertIfThen(
362 cast<Instruction>(Cmp)->getNextNode(), Cmp);
363 IRBuilder<> IRB2(CheckTerm);
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);
379 CheckTerm = splitBlockAndInsertIfThen(CheckTerm, Cmp2);
382 IRBuilder<> IRB1(CheckTerm);
383 Instruction *Crash = generateCrashCode(IRB1, AddrLong, IsWrite, TypeSize);
384 Crash->setDebugLoc(OrigIns->getDebugLoc());
385 ReplaceInstWithInst(CheckTerm, new UnreachableInst(*C));
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;
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);
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)
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())
412 // For now, just ignore this Alloca if the alignment is large.
413 if (G->getAlignment() > RedzoneSize) continue;
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);
425 if (G->hasSection()) {
426 StringRef Section(G->getSection());
427 // Ignore the globals from the __OBJC section. The ObjC runtime assumes
428 // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
430 if ((Section.find("__OBJC,") == 0) ||
431 (Section.find("__DATA, __objc_") == 0)) {
432 DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
435 // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
436 // Constant CFString instances are compiled in the following way:
437 // -- the string buffer is emitted into
438 // __TEXT,__cstring,cstring_literals
439 // -- the constant NSConstantString structure referencing that buffer
440 // is placed into __DATA,__cfstring
441 // Therefore there's no point in placing redzones into __DATA,__cfstring.
442 // Moreover, it causes the linker to crash on OS X 10.7
443 if (Section.find("__DATA,__cfstring") == 0) {
444 DEBUG(dbgs() << "Ignoring CFString: " << *G);
449 GlobalsToChange.push_back(G);
452 size_t n = GlobalsToChange.size();
453 if (n == 0) return false;
455 // A global is described by a structure
458 // size_t size_with_redzone;
460 // We initialize an array of such structures and pass it to a run-time call.
461 StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
462 IntptrTy, IntptrTy, NULL);
463 SmallVector<Constant *, 16> Initializers(n);
465 IRBuilder<> IRB(CtorInsertBefore);
467 for (size_t i = 0; i < n; i++) {
468 GlobalVariable *G = GlobalsToChange[i];
469 PointerType *PtrTy = cast<PointerType>(G->getType());
470 Type *Ty = PtrTy->getElementType();
471 uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
472 uint64_t RightRedzoneSize = RedzoneSize +
473 (RedzoneSize - (SizeInBytes % RedzoneSize));
474 Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
476 StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
477 Constant *NewInitializer = ConstantStruct::get(
478 NewTy, G->getInitializer(),
479 Constant::getNullValue(RightRedZoneTy), NULL);
481 SmallString<2048> DescriptionOfGlobal = G->getName();
482 DescriptionOfGlobal += " (";
483 DescriptionOfGlobal += M.getModuleIdentifier();
484 DescriptionOfGlobal += ")";
485 GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
487 // Create a new global variable with enough space for a redzone.
488 GlobalVariable *NewGlobal = new GlobalVariable(
489 M, NewTy, G->isConstant(), G->getLinkage(),
490 NewInitializer, "", G, G->isThreadLocal());
491 NewGlobal->copyAttributesFrom(G);
492 NewGlobal->setAlignment(RedzoneSize);
495 Indices2[0] = IRB.getInt32(0);
496 Indices2[1] = IRB.getInt32(0);
498 G->replaceAllUsesWith(
499 ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
500 NewGlobal->takeName(G);
501 G->eraseFromParent();
503 Initializers[i] = ConstantStruct::get(
505 ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
506 ConstantInt::get(IntptrTy, SizeInBytes),
507 ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
508 ConstantExpr::getPointerCast(Name, IntptrTy),
510 DEBUG(dbgs() << "NEW GLOBAL:\n" << *NewGlobal);
513 ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
514 GlobalVariable *AllGlobals = new GlobalVariable(
515 M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
516 ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
518 Function *AsanRegisterGlobals = cast<Function>(M.getOrInsertFunction(
519 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
520 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
522 IRB.CreateCall2(AsanRegisterGlobals,
523 IRB.CreatePointerCast(AllGlobals, IntptrTy),
524 ConstantInt::get(IntptrTy, n));
526 // We also need to unregister globals at the end, e.g. when a shared library
528 Function *AsanDtorFunction = Function::Create(
529 FunctionType::get(Type::getVoidTy(*C), false),
530 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
531 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
532 IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
533 Function *AsanUnregisterGlobals = cast<Function>(M.getOrInsertFunction(
534 kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
535 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
537 IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
538 IRB.CreatePointerCast(AllGlobals, IntptrTy),
539 ConstantInt::get(IntptrTy, n));
540 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
547 bool AddressSanitizer::runOnModule(Module &M) {
548 // Initialize the private fields. No one has accessed them before.
549 TD = getAnalysisIfAvailable<TargetData>();
552 BL.reset(new BlackList(ClBlackListFile));
555 C = &(M.getContext());
556 LongSize = TD->getPointerSizeInBits();
557 IntptrTy = Type::getIntNTy(*C, LongSize);
558 IntptrPtrTy = PointerType::get(IntptrTy, 0);
560 AsanCtorFunction = Function::Create(
561 FunctionType::get(Type::getVoidTy(*C), false),
562 GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
563 BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
564 CtorInsertBefore = ReturnInst::Create(*C, AsanCtorBB);
566 // call __asan_init in the module ctor.
567 IRBuilder<> IRB(CtorInsertBefore);
568 AsanInitFunction = cast<Function>(
569 M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
570 AsanInitFunction->setLinkage(Function::ExternalLinkage);
571 IRB.CreateCall(AsanInitFunction);
573 MappingOffset = LongSize == 32
574 ? kDefaultShadowOffset32 : kDefaultShadowOffset64;
575 if (ClMappingOffsetLog >= 0) {
576 if (ClMappingOffsetLog == 0) {
580 MappingOffset = 1ULL << ClMappingOffsetLog;
583 MappingScale = kDefaultShadowScale;
584 if (ClMappingScale) {
585 MappingScale = ClMappingScale;
587 // Redzone used for stack and globals is at least 32 bytes.
588 // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
589 RedzoneSize = std::max(32, (int)(1 << MappingScale));
594 Res |= insertGlobalRedzones(M);
596 // Tell the run-time the current values of mapping offset and scale.
597 GlobalValue *asan_mapping_offset =
598 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
599 ConstantInt::get(IntptrTy, MappingOffset),
600 kAsanMappingOffsetName);
601 GlobalValue *asan_mapping_scale =
602 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
603 ConstantInt::get(IntptrTy, MappingScale),
604 kAsanMappingScaleName);
605 // Read these globals, otherwise they may be optimized away.
606 IRB.CreateLoad(asan_mapping_scale, true);
607 IRB.CreateLoad(asan_mapping_offset, true);
610 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
611 if (F->isDeclaration()) continue;
612 Res |= handleFunction(M, *F);
615 appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
620 bool AddressSanitizer::handleFunction(Module &M, Function &F) {
621 if (BL->isIn(F)) return false;
622 if (&F == AsanCtorFunction) return false;
623 if (!F.hasFnAttr(Attribute::AddressSafety)) return false;
625 if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
627 // We want to instrument every address only once per basic block
628 // (unless there are calls between uses).
629 SmallSet<Value*, 16> TempsToInstrument;
630 SmallVector<Instruction*, 16> ToInstrument;
632 // Fill the set of memory operations to instrument.
633 for (Function::iterator FI = F.begin(), FE = F.end();
635 TempsToInstrument.clear();
636 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
638 if (LooksLikeCodeInBug11395(BI)) return false;
639 if ((isa<LoadInst>(BI) && ClInstrumentReads) ||
640 (isa<StoreInst>(BI) && ClInstrumentWrites)) {
641 Value *Addr = getLDSTOperand(BI);
642 if (ClOpt && ClOptSameTemp) {
643 if (!TempsToInstrument.insert(Addr))
644 continue; // We've seen this temp in the current BB.
646 } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
649 if (isa<CallInst>(BI)) {
651 TempsToInstrument.clear();
655 ToInstrument.push_back(BI);
660 int NumInstrumented = 0;
661 for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
662 Instruction *Inst = ToInstrument[i];
663 if (ClDebugMin < 0 || ClDebugMax < 0 ||
664 (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
665 if (isa<StoreInst>(Inst) || isa<LoadInst>(Inst))
668 instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
675 bool ChangedStack = poisonStackInFunction(M, F);
677 // For each NSObject descendant having a +load method, this method is invoked
678 // by the ObjC runtime before any of the static constructors is called.
679 // Therefore we need to instrument such methods with a call to __asan_init
680 // at the beginning in order to initialize our runtime before any access to
681 // the shadow memory.
682 // We cannot just ignore these methods, because they may call other
683 // instrumented functions.
684 if (F.getName().find(" load]") != std::string::npos) {
685 IRBuilder<> IRB(F.begin()->begin());
686 IRB.CreateCall(AsanInitFunction);
689 return NumInstrumented > 0 || ChangedStack;
692 static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
693 if (ShadowRedzoneSize == 1) return PoisonByte;
694 if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
695 if (ShadowRedzoneSize == 4)
696 return (PoisonByte << 24) + (PoisonByte << 16) +
697 (PoisonByte << 8) + (PoisonByte);
698 assert(0 && "ShadowRedzoneSize is either 1, 2 or 4");
702 static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
705 size_t ShadowGranularity,
707 for (size_t i = 0; i < RedzoneSize;
708 i+= ShadowGranularity, Shadow++) {
709 if (i + ShadowGranularity <= Size) {
710 *Shadow = 0; // fully addressable
711 } else if (i >= Size) {
712 *Shadow = Magic; // unaddressable
714 *Shadow = Size - i; // first Size-i bytes are addressable
719 void AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
721 Value *ShadowBase, bool DoPoison) {
722 size_t ShadowRZSize = RedzoneSize >> MappingScale;
723 assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
724 Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
725 Type *RZPtrTy = PointerType::get(RZTy, 0);
727 Value *PoisonLeft = ConstantInt::get(RZTy,
728 ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
729 Value *PoisonMid = ConstantInt::get(RZTy,
730 ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
731 Value *PoisonRight = ConstantInt::get(RZTy,
732 ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
734 // poison the first red zone.
735 IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
737 // poison all other red zones.
738 uint64_t Pos = RedzoneSize;
739 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
740 AllocaInst *AI = AllocaVec[i];
741 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
742 uint64_t AlignedSize = getAlignedAllocaSize(AI);
743 assert(AlignedSize - SizeInBytes < RedzoneSize);
748 assert(ShadowBase->getType() == IntptrTy);
749 if (SizeInBytes < AlignedSize) {
750 // Poison the partial redzone at right
752 ShadowBase, ConstantInt::get(IntptrTy,
753 (Pos >> MappingScale) - ShadowRZSize));
754 size_t AddressableBytes = RedzoneSize - (AlignedSize - SizeInBytes);
757 PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
759 1ULL << MappingScale,
760 kAsanStackPartialRedzoneMagic);
762 Value *PartialPoison = ConstantInt::get(RZTy, Poison);
763 IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
766 // Poison the full redzone at right.
767 Ptr = IRB.CreateAdd(ShadowBase,
768 ConstantInt::get(IntptrTy, Pos >> MappingScale));
769 Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
770 IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
776 // Workaround for bug 11395: we don't want to instrument stack in functions
777 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
778 // FIXME: remove once the bug 11395 is fixed.
779 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
780 if (LongSize != 32) return false;
781 CallInst *CI = dyn_cast<CallInst>(I);
782 if (!CI || !CI->isInlineAsm()) return false;
783 if (CI->getNumArgOperands() <= 5) return false;
784 // We have inline assembly with quite a few arguments.
788 // Find all static Alloca instructions and put
789 // poisoned red zones around all of them.
790 // Then unpoison everything back before the function returns.
792 // Stack poisoning does not play well with exception handling.
793 // When an exception is thrown, we essentially bypass the code
794 // that unpoisones the stack. This is why the run-time library has
795 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
796 // stack in the interceptor. This however does not work inside the
797 // actual function which catches the exception. Most likely because the
798 // compiler hoists the load of the shadow value somewhere too high.
799 // This causes asan to report a non-existing bug on 453.povray.
800 // It sounds like an LLVM bug.
801 bool AddressSanitizer::poisonStackInFunction(Module &M, Function &F) {
802 if (!ClStack) return false;
803 SmallVector<AllocaInst*, 16> AllocaVec;
804 SmallVector<Instruction*, 8> RetVec;
805 uint64_t TotalSize = 0;
807 // Filter out Alloca instructions we want (and can) handle.
808 // Collect Ret instructions.
809 for (Function::iterator FI = F.begin(), FE = F.end();
811 BasicBlock &BB = *FI;
812 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
814 if (isa<ReturnInst>(BI)) {
815 RetVec.push_back(BI);
819 AllocaInst *AI = dyn_cast<AllocaInst>(BI);
821 if (AI->isArrayAllocation()) continue;
822 if (!AI->isStaticAlloca()) continue;
823 if (!AI->getAllocatedType()->isSized()) continue;
824 if (AI->getAlignment() > RedzoneSize) continue;
825 AllocaVec.push_back(AI);
826 uint64_t AlignedSize = getAlignedAllocaSize(AI);
827 TotalSize += AlignedSize;
831 if (AllocaVec.empty()) return false;
833 uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize;
835 bool DoStackMalloc = ClUseAfterReturn
836 && LocalStackSize <= kMaxStackMallocSize;
838 Instruction *InsBefore = AllocaVec[0];
839 IRBuilder<> IRB(InsBefore);
842 Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
843 AllocaInst *MyAlloca =
844 new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
845 MyAlloca->setAlignment(RedzoneSize);
846 assert(MyAlloca->isStaticAlloca());
847 Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
848 Value *LocalStackBase = OrigStackBase;
851 Value *AsanStackMallocFunc = M.getOrInsertFunction(
852 kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL);
853 LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
854 ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
857 // This string will be parsed by the run-time (DescribeStackAddress).
858 SmallString<2048> StackDescriptionStorage;
859 raw_svector_ostream StackDescription(StackDescriptionStorage);
860 StackDescription << F.getName() << " " << AllocaVec.size() << " ";
862 uint64_t Pos = RedzoneSize;
863 // Replace Alloca instructions with base+offset.
864 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
865 AllocaInst *AI = AllocaVec[i];
866 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
867 StringRef Name = AI->getName();
868 StackDescription << Pos << " " << SizeInBytes << " "
869 << Name.size() << " " << Name << " ";
870 uint64_t AlignedSize = getAlignedAllocaSize(AI);
871 assert((AlignedSize % RedzoneSize) == 0);
872 AI->replaceAllUsesWith(
874 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
876 Pos += AlignedSize + RedzoneSize;
878 assert(Pos == LocalStackSize);
880 // Write the Magic value and the frame description constant to the redzone.
881 Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
882 IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
884 Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
885 ConstantInt::get(IntptrTy, LongSize/8));
886 BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
887 Value *Description = IRB.CreatePointerCast(
888 createPrivateGlobalForString(M, StackDescription.str()),
890 IRB.CreateStore(Description, BasePlus1);
892 // Poison the stack redzones at the entry.
893 Value *ShadowBase = memToShadow(LocalStackBase, IRB);
894 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
896 Value *AsanStackFreeFunc = NULL;
898 AsanStackFreeFunc = M.getOrInsertFunction(
899 kAsanStackFreeName, IRB.getVoidTy(),
900 IntptrTy, IntptrTy, IntptrTy, NULL);
903 // Unpoison the stack before all ret instructions.
904 for (size_t i = 0, n = RetVec.size(); i < n; i++) {
905 Instruction *Ret = RetVec[i];
906 IRBuilder<> IRBRet(Ret);
908 // Mark the current frame as retired.
909 IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
911 // Unpoison the stack.
912 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
915 IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
916 ConstantInt::get(IntptrTy, LocalStackSize),
928 BlackList::BlackList(const std::string &Path) {
930 const char *kFunPrefix = "fun:";
931 if (!ClBlackListFile.size()) return;
934 OwningPtr<MemoryBuffer> File;
935 if (error_code EC = MemoryBuffer::getFile(ClBlackListFile.c_str(), File)) {
936 report_fatal_error("Can't open blacklist file " + ClBlackListFile + ": " +
939 MemoryBuffer *Buff = File.take();
940 const char *Data = Buff->getBufferStart();
941 size_t DataLen = Buff->getBufferSize();
942 SmallVector<StringRef, 16> Lines;
943 SplitString(StringRef(Data, DataLen), Lines, "\n\r");
944 for (size_t i = 0, numLines = Lines.size(); i < numLines; i++) {
945 if (Lines[i].startswith(kFunPrefix)) {
946 std::string ThisFunc = Lines[i].substr(strlen(kFunPrefix));
947 std::string ThisFuncRE;
948 // add ThisFunc replacing * with .*
949 for (size_t j = 0, n = ThisFunc.size(); j < n; j++) {
950 if (ThisFunc[j] == '*')
952 ThisFuncRE += ThisFunc[j];
954 // Check that the regexp is valid.
955 Regex CheckRE(ThisFuncRE);
957 if (!CheckRE.isValid(Error))
958 report_fatal_error("malformed blacklist regex: " + ThisFunc +
960 // Append to the final regexp.
967 Functions = new Regex(Fun);
971 bool BlackList::isIn(const Function &F) {
973 bool Res = Functions->match(F.getName());