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/InlineAsm.h"
26 #include "llvm/IntrinsicInst.h"
27 #include "llvm/LLVMContext.h"
28 #include "llvm/Module.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/DataTypes.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/IRBuilder.h"
33 #include "llvm/Support/MemoryBuffer.h"
34 #include "llvm/Support/Regex.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Support/system_error.h"
37 #include "llvm/Target/TargetData.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Transforms/Instrumentation.h"
40 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
41 #include "llvm/Transforms/Utils/ModuleUtils.h"
42 #include "llvm/Type.h"
49 static const uint64_t kDefaultShadowScale = 3;
50 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
51 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
53 static const size_t kMaxStackMallocSize = 1 << 16; // 64K
54 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
55 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
57 static const char *kAsanModuleCtorName = "asan.module_ctor";
58 static const char *kAsanModuleDtorName = "asan.module_dtor";
59 static const int kAsanCtorAndCtorPriority = 1;
60 static const char *kAsanReportErrorTemplate = "__asan_report_";
61 static const char *kAsanRegisterGlobalsName = "__asan_register_globals";
62 static const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
63 static const char *kAsanInitName = "__asan_init";
64 static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
65 static const char *kAsanMappingScaleName = "__asan_mapping_scale";
66 static const char *kAsanStackMallocName = "__asan_stack_malloc";
67 static const char *kAsanStackFreeName = "__asan_stack_free";
69 static const int kAsanStackLeftRedzoneMagic = 0xf1;
70 static const int kAsanStackMidRedzoneMagic = 0xf2;
71 static const int kAsanStackRightRedzoneMagic = 0xf3;
72 static const int kAsanStackPartialRedzoneMagic = 0xf4;
74 // Command-line flags.
76 // This flag may need to be replaced with -f[no-]asan-reads.
77 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
78 cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
79 static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
80 cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
81 // This flag may need to be replaced with -f[no]asan-stack.
82 static cl::opt<bool> ClStack("asan-stack",
83 cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
84 // This flag may need to be replaced with -f[no]asan-use-after-return.
85 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
86 cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
87 // This flag may need to be replaced with -f[no]asan-globals.
88 static cl::opt<bool> ClGlobals("asan-globals",
89 cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
90 static cl::opt<bool> ClMemIntrin("asan-memintrin",
91 cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
92 // This flag may need to be replaced with -fasan-blacklist.
93 static cl::opt<std::string> ClBlackListFile("asan-blacklist",
94 cl::desc("File containing the list of functions to ignore "
95 "during instrumentation"), cl::Hidden);
96 static cl::opt<bool> ClUseCall("asan-use-call",
97 cl::desc("Use function call to generate a crash"), cl::Hidden,
100 // These flags allow to change the shadow mapping.
101 // The shadow mapping looks like
102 // Shadow = (Mem >> scale) + (1 << offset_log)
103 static cl::opt<int> ClMappingScale("asan-mapping-scale",
104 cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
105 static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
106 cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
108 // Optimization flags. Not user visible, used mostly for testing
109 // and benchmarking the tool.
110 static cl::opt<bool> ClOpt("asan-opt",
111 cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
112 static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
113 cl::desc("Instrument the same temp just once"), cl::Hidden,
115 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
116 cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
119 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
121 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
122 cl::Hidden, cl::init(0));
123 static cl::opt<std::string> ClDebugFunc("asan-debug-func",
124 cl::Hidden, cl::desc("Debug func"));
125 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
126 cl::Hidden, cl::init(-1));
127 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
128 cl::Hidden, cl::init(-1));
132 // Blacklisted functions are not instrumented.
133 // The blacklist file contains one or more lines like this:
135 // fun:FunctionWildCard
137 // This is similar to the "ignore" feature of ThreadSanitizer.
138 // http://code.google.com/p/data-race-test/wiki/ThreadSanitizerIgnores
141 BlackList(const std::string &Path);
142 bool isIn(const Function &F);
147 /// AddressSanitizer: instrument the code in module to find memory bugs.
148 struct AddressSanitizer : public ModulePass {
150 void instrumentMop(Instruction *I);
151 void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB,
152 Value *Addr, uint32_t TypeSize, bool IsWrite);
153 Instruction *generateCrashCode(IRBuilder<> &IRB, Value *Addr,
154 bool IsWrite, uint32_t TypeSize);
155 bool instrumentMemIntrinsic(MemIntrinsic *MI);
156 void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
158 Instruction *InsertBefore, bool IsWrite);
159 Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
160 bool handleFunction(Module &M, Function &F);
161 bool poisonStackInFunction(Module &M, Function &F);
162 virtual bool runOnModule(Module &M);
163 bool insertGlobalRedzones(Module &M);
164 BranchInst *splitBlockAndInsertIfThen(Instruction *SplitBefore, Value *Cmp);
165 static char ID; // Pass identification, replacement for typeid
169 uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
170 Type *Ty = AI->getAllocatedType();
171 uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
174 uint64_t getAlignedSize(uint64_t SizeInBytes) {
175 return ((SizeInBytes + RedzoneSize - 1)
176 / RedzoneSize) * RedzoneSize;
178 uint64_t getAlignedAllocaSize(AllocaInst *AI) {
179 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
180 return getAlignedSize(SizeInBytes);
183 void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
184 Value *ShadowBase, bool DoPoison);
185 bool LooksLikeCodeInBug11395(Instruction *I);
187 Module *CurrentModule;
190 uint64_t MappingOffset;
196 Function *AsanCtorFunction;
197 Function *AsanInitFunction;
198 Instruction *CtorInsertBefore;
199 OwningPtr<BlackList> BL;
203 char AddressSanitizer::ID = 0;
204 INITIALIZE_PASS(AddressSanitizer, "asan",
205 "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
207 AddressSanitizer::AddressSanitizer() : ModulePass(ID) { }
208 ModulePass *llvm::createAddressSanitizerPass() {
209 return new AddressSanitizer();
212 // Create a constant for Str so that we can pass it to the run-time lib.
213 static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
214 Constant *StrConst = ConstantArray::get(M.getContext(), Str);
215 return new GlobalVariable(M, StrConst->getType(), true,
216 GlobalValue::PrivateLinkage, StrConst, "");
219 // Split the basic block and insert an if-then code.
231 // Returns the NewBasicBlock's terminator.
232 BranchInst *AddressSanitizer::splitBlockAndInsertIfThen(
233 Instruction *SplitBefore, Value *Cmp) {
234 BasicBlock *Head = SplitBefore->getParent();
235 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
236 TerminatorInst *HeadOldTerm = Head->getTerminator();
237 BasicBlock *NewBasicBlock =
238 BasicBlock::Create(*C, "", Head->getParent());
239 BranchInst *HeadNewTerm = BranchInst::Create(/*ifTrue*/NewBasicBlock,
242 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
244 BranchInst *CheckTerm = BranchInst::Create(Tail, NewBasicBlock);
248 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
250 Shadow = IRB.CreateLShr(Shadow, MappingScale);
251 if (MappingOffset == 0)
253 // (Shadow >> scale) | offset
254 return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
258 void AddressSanitizer::instrumentMemIntrinsicParam(Instruction *OrigIns,
259 Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
260 // Check the first byte.
262 IRBuilder<> IRB(InsertBefore);
263 instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
265 // Check the last byte.
267 IRBuilder<> IRB(InsertBefore);
268 Value *SizeMinusOne = IRB.CreateSub(
269 Size, ConstantInt::get(Size->getType(), 1));
270 SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
271 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
272 Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
273 instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
277 // Instrument memset/memmove/memcpy
278 bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
279 Value *Dst = MI->getDest();
280 MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
281 Value *Src = MemTran ? MemTran->getSource() : NULL;
282 Value *Length = MI->getLength();
284 Constant *ConstLength = dyn_cast<Constant>(Length);
285 Instruction *InsertBefore = MI;
287 if (ConstLength->isNullValue()) return false;
289 // The size is not a constant so it could be zero -- check at run-time.
290 IRBuilder<> IRB(InsertBefore);
292 Value *Cmp = IRB.CreateICmpNE(Length,
293 Constant::getNullValue(Length->getType()));
294 InsertBefore = splitBlockAndInsertIfThen(InsertBefore, Cmp);
297 instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
299 instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
303 static Value *getLDSTOperand(Instruction *I) {
304 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
305 return LI->getPointerOperand();
307 return cast<StoreInst>(*I).getPointerOperand();
310 void AddressSanitizer::instrumentMop(Instruction *I) {
311 int IsWrite = isa<StoreInst>(*I);
312 Value *Addr = getLDSTOperand(I);
313 if (ClOpt && ClOptGlobals && isa<GlobalVariable>(Addr)) {
314 // We are accessing a global scalar variable. Nothing to catch here.
317 Type *OrigPtrTy = Addr->getType();
318 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
320 assert(OrigTy->isSized());
321 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
323 if (TypeSize != 8 && TypeSize != 16 &&
324 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
325 // Ignore all unusual sizes.
330 instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
333 Instruction *AddressSanitizer::generateCrashCode(
334 IRBuilder<> &IRB, Value *Addr, bool IsWrite, uint32_t TypeSize) {
337 // Here we use a call instead of arch-specific asm to report an error.
338 // This is almost always slower (because the codegen needs to generate
339 // prologue/epilogue for otherwise leaf functions) and generates more code.
340 // This mode could be useful if we can not use SIGILL for some reason.
342 // IsWrite and TypeSize are encoded in the function name.
343 std::string FunctionName = std::string(kAsanReportErrorTemplate) +
344 (IsWrite ? "store" : "load") + itostr(TypeSize / 8);
345 Value *ReportWarningFunc = CurrentModule->getOrInsertFunction(
346 FunctionName, IRB.getVoidTy(), IntptrTy, NULL);
347 CallInst *Call = IRB.CreateCall(ReportWarningFunc, Addr);
348 Call->setDoesNotReturn();
352 uint32_t LogOfSizeInBytes = CountTrailingZeros_32(TypeSize / 8);
353 assert(8U * (1 << LogOfSizeInBytes) == TypeSize);
354 uint8_t TelltaleValue = IsWrite * 8 + LogOfSizeInBytes;
355 assert(TelltaleValue < 16);
357 // Move the failing address to %rax/%eax
358 FunctionType *Fn1Ty = FunctionType::get(
359 IRB.getVoidTy(), ArrayRef<Type*>(IntptrTy), false);
360 const char *MovStr = LongSize == 32
361 ? "mov $0, %eax" : "mov $0, %rax";
362 Value *AsmMov = InlineAsm::get(
363 Fn1Ty, StringRef(MovStr), StringRef("r"), true);
364 IRB.CreateCall(AsmMov, Addr);
366 // crash with ud2; could use int3, but it is less friendly to gdb.
367 // after ud2 put a 1-byte instruction that encodes the access type and size.
369 const char *TelltaleInsns[16] = {
388 std::string AsmStr = "ud2;";
389 AsmStr += TelltaleInsns[TelltaleValue];
390 Value *MyAsm = InlineAsm::get(FunctionType::get(Type::getVoidTy(*C), false),
391 StringRef(AsmStr), StringRef(""), true);
392 CallInst *AsmCall = IRB.CreateCall(MyAsm);
394 // This saves us one jump, but triggers a bug in RA (or somewhere else):
395 // while building 483.xalancbmk the compiler goes into infinite loop in
396 // llvm::SpillPlacement::iterate() / RAGreedy::growRegion
397 // AsmCall->setDoesNotReturn();
401 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
402 IRBuilder<> &IRB, Value *Addr,
403 uint32_t TypeSize, bool IsWrite) {
404 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
406 Type *ShadowTy = IntegerType::get(
407 *C, std::max(8U, TypeSize >> MappingScale));
408 Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
409 Value *ShadowPtr = memToShadow(AddrLong, IRB);
410 Value *CmpVal = Constant::getNullValue(ShadowTy);
411 Value *ShadowValue = IRB.CreateLoad(
412 IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
414 Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
416 Instruction *CheckTerm = splitBlockAndInsertIfThen(
417 cast<Instruction>(Cmp)->getNextNode(), Cmp);
418 IRBuilder<> IRB2(CheckTerm);
420 size_t Granularity = 1 << MappingScale;
421 if (TypeSize < 8 * Granularity) {
422 // Addr & (Granularity - 1)
423 Value *Lower3Bits = IRB2.CreateAnd(
424 AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
425 // (Addr & (Granularity - 1)) + size - 1
426 Value *LastAccessedByte = IRB2.CreateAdd(
427 Lower3Bits, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
428 // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
429 LastAccessedByte = IRB2.CreateIntCast(
430 LastAccessedByte, IRB.getInt8Ty(), false);
431 // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
432 Value *Cmp2 = IRB2.CreateICmpSGE(LastAccessedByte, ShadowValue);
434 CheckTerm = splitBlockAndInsertIfThen(CheckTerm, Cmp2);
437 IRBuilder<> IRB1(CheckTerm);
438 Instruction *Crash = generateCrashCode(IRB1, AddrLong, IsWrite, TypeSize);
439 Crash->setDebugLoc(OrigIns->getDebugLoc());
440 ReplaceInstWithInst(CheckTerm, new UnreachableInst(*C));
443 // This function replaces all global variables with new variables that have
444 // trailing redzones. It also creates a function that poisons
445 // redzones and inserts this function into llvm.global_ctors.
446 bool AddressSanitizer::insertGlobalRedzones(Module &M) {
447 SmallVector<GlobalVariable *, 16> GlobalsToChange;
449 for (Module::GlobalListType::iterator G = M.getGlobalList().begin(),
450 E = M.getGlobalList().end(); G != E; ++G) {
451 Type *Ty = cast<PointerType>(G->getType())->getElementType();
452 DEBUG(dbgs() << "GLOBAL: " << *G);
454 if (!Ty->isSized()) continue;
455 if (!G->hasInitializer()) continue;
456 // Touch only those globals that will not be defined in other modules.
457 // Don't handle ODR type linkages since other modules may be built w/o asan.
458 if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
459 G->getLinkage() != GlobalVariable::PrivateLinkage &&
460 G->getLinkage() != GlobalVariable::InternalLinkage)
462 // Two problems with thread-locals:
463 // - The address of the main thread's copy can't be computed at link-time.
464 // - Need to poison all copies, not just the main thread's one.
465 if (G->isThreadLocal())
467 // For now, just ignore this Alloca if the alignment is large.
468 if (G->getAlignment() > RedzoneSize) continue;
470 // Ignore all the globals with the names starting with "\01L_OBJC_".
471 // Many of those are put into the .cstring section. The linker compresses
472 // that section by removing the spare \0s after the string terminator, so
473 // our redzones get broken.
474 if ((G->getName().find("\01L_OBJC_") == 0) ||
475 (G->getName().find("\01l_OBJC_") == 0)) {
476 DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
480 // Ignore the globals from the __OBJC section. The ObjC runtime assumes
481 // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
483 if (G->hasSection()) {
484 StringRef Section(G->getSection());
485 if ((Section.find("__OBJC,") == 0) ||
486 (Section.find("__DATA, __objc_") == 0)) {
487 DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
492 GlobalsToChange.push_back(G);
495 size_t n = GlobalsToChange.size();
496 if (n == 0) return false;
498 // A global is described by a structure
501 // size_t size_with_redzone;
503 // We initialize an array of such structures and pass it to a run-time call.
504 StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
505 IntptrTy, IntptrTy, NULL);
506 SmallVector<Constant *, 16> Initializers(n);
508 IRBuilder<> IRB(CtorInsertBefore);
510 for (size_t i = 0; i < n; i++) {
511 GlobalVariable *G = GlobalsToChange[i];
512 PointerType *PtrTy = cast<PointerType>(G->getType());
513 Type *Ty = PtrTy->getElementType();
514 uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
515 uint64_t RightRedzoneSize = RedzoneSize +
516 (RedzoneSize - (SizeInBytes % RedzoneSize));
517 Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
519 StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
520 Constant *NewInitializer = ConstantStruct::get(
521 NewTy, G->getInitializer(),
522 Constant::getNullValue(RightRedZoneTy), NULL);
524 GlobalVariable *Name = createPrivateGlobalForString(M, G->getName());
526 // Create a new global variable with enough space for a redzone.
527 GlobalVariable *NewGlobal = new GlobalVariable(
528 M, NewTy, G->isConstant(), G->getLinkage(),
529 NewInitializer, "", G, G->isThreadLocal());
530 NewGlobal->copyAttributesFrom(G);
531 NewGlobal->setAlignment(RedzoneSize);
534 Indices2[0] = IRB.getInt32(0);
535 Indices2[1] = IRB.getInt32(0);
537 G->replaceAllUsesWith(
538 ConstantExpr::getGetElementPtr(NewGlobal, Indices2, 2));
539 NewGlobal->takeName(G);
540 G->eraseFromParent();
542 Initializers[i] = ConstantStruct::get(
544 ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
545 ConstantInt::get(IntptrTy, SizeInBytes),
546 ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
547 ConstantExpr::getPointerCast(Name, IntptrTy),
549 DEBUG(dbgs() << "NEW GLOBAL:\n" << *NewGlobal);
552 ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
553 GlobalVariable *AllGlobals = new GlobalVariable(
554 M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
555 ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
557 Function *AsanRegisterGlobals = cast<Function>(M.getOrInsertFunction(
558 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
559 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
561 IRB.CreateCall2(AsanRegisterGlobals,
562 IRB.CreatePointerCast(AllGlobals, IntptrTy),
563 ConstantInt::get(IntptrTy, n));
565 // We also need to unregister globals at the end, e.g. when a shared library
567 Function *AsanDtorFunction = Function::Create(
568 FunctionType::get(Type::getVoidTy(*C), false),
569 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
570 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
571 IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
572 Function *AsanUnregisterGlobals = cast<Function>(M.getOrInsertFunction(
573 kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
574 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
576 IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
577 IRB.CreatePointerCast(AllGlobals, IntptrTy),
578 ConstantInt::get(IntptrTy, n));
579 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
586 bool AddressSanitizer::runOnModule(Module &M) {
587 // Initialize the private fields. No one has accessed them before.
588 TD = getAnalysisIfAvailable<TargetData>();
591 BL.reset(new BlackList(ClBlackListFile));
594 C = &(M.getContext());
595 LongSize = TD->getPointerSizeInBits();
596 IntptrTy = Type::getIntNTy(*C, LongSize);
597 IntptrPtrTy = PointerType::get(IntptrTy, 0);
599 AsanCtorFunction = Function::Create(
600 FunctionType::get(Type::getVoidTy(*C), false),
601 GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
602 BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
603 CtorInsertBefore = ReturnInst::Create(*C, AsanCtorBB);
605 // call __asan_init in the module ctor.
606 IRBuilder<> IRB(CtorInsertBefore);
607 AsanInitFunction = cast<Function>(
608 M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
609 AsanInitFunction->setLinkage(Function::ExternalLinkage);
610 IRB.CreateCall(AsanInitFunction);
612 MappingOffset = LongSize == 32
613 ? kDefaultShadowOffset32 : kDefaultShadowOffset64;
614 if (ClMappingOffsetLog >= 0) {
615 if (ClMappingOffsetLog == 0) {
619 MappingOffset = 1ULL << ClMappingOffsetLog;
622 MappingScale = kDefaultShadowScale;
623 if (ClMappingScale) {
624 MappingScale = ClMappingScale;
626 // Redzone used for stack and globals is at least 32 bytes.
627 // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
628 RedzoneSize = std::max(32, (int)(1 << MappingScale));
633 Res |= insertGlobalRedzones(M);
635 // Tell the run-time the current values of mapping offset and scale.
636 GlobalValue *asan_mapping_offset =
637 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
638 ConstantInt::get(IntptrTy, MappingOffset),
639 kAsanMappingOffsetName);
640 GlobalValue *asan_mapping_scale =
641 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
642 ConstantInt::get(IntptrTy, MappingScale),
643 kAsanMappingScaleName);
644 // Read these globals, otherwise they may be optimized away.
645 IRB.CreateLoad(asan_mapping_scale, true);
646 IRB.CreateLoad(asan_mapping_offset, true);
649 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
650 if (F->isDeclaration()) continue;
651 Res |= handleFunction(M, *F);
654 appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
659 bool AddressSanitizer::handleFunction(Module &M, Function &F) {
660 if (BL->isIn(F)) return false;
661 if (&F == AsanCtorFunction) return false;
663 if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
665 // We want to instrument every address only once per basic block
666 // (unless there are calls between uses).
667 SmallSet<Value*, 16> TempsToInstrument;
668 SmallVector<Instruction*, 16> ToInstrument;
670 // Fill the set of memory operations to instrument.
671 for (Function::iterator FI = F.begin(), FE = F.end();
673 TempsToInstrument.clear();
674 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
676 if ((isa<LoadInst>(BI) && ClInstrumentReads) ||
677 (isa<StoreInst>(BI) && ClInstrumentWrites)) {
678 Value *Addr = getLDSTOperand(BI);
679 if (ClOpt && ClOptSameTemp) {
680 if (!TempsToInstrument.insert(Addr))
681 continue; // We've seen this temp in the current BB.
683 } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
686 if (isa<CallInst>(BI)) {
688 TempsToInstrument.clear();
692 ToInstrument.push_back(BI);
697 int NumInstrumented = 0;
698 for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
699 Instruction *Inst = ToInstrument[i];
700 if (ClDebugMin < 0 || ClDebugMax < 0 ||
701 (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
702 if (isa<StoreInst>(Inst) || isa<LoadInst>(Inst))
705 instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
712 bool ChangedStack = poisonStackInFunction(M, F);
714 // For each NSObject descendant having a +load method, this method is invoked
715 // by the ObjC runtime before any of the static constructors is called.
716 // Therefore we need to instrument such methods with a call to __asan_init
717 // at the beginning in order to initialize our runtime before any access to
718 // the shadow memory.
719 // We cannot just ignore these methods, because they may call other
720 // instrumented functions.
721 if (F.getName().find(" load]") != std::string::npos) {
722 IRBuilder<> IRB(F.begin()->begin());
723 IRB.CreateCall(AsanInitFunction);
726 return NumInstrumented > 0 || ChangedStack;
729 static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
730 if (ShadowRedzoneSize == 1) return PoisonByte;
731 if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
732 if (ShadowRedzoneSize == 4)
733 return (PoisonByte << 24) + (PoisonByte << 16) +
734 (PoisonByte << 8) + (PoisonByte);
735 assert(0 && "ShadowRedzoneSize is either 1, 2 or 4");
739 static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
742 size_t ShadowGranularity,
744 for (size_t i = 0; i < RedzoneSize;
745 i+= ShadowGranularity, Shadow++) {
746 if (i + ShadowGranularity <= Size) {
747 *Shadow = 0; // fully addressable
748 } else if (i >= Size) {
749 *Shadow = Magic; // unaddressable
751 *Shadow = Size - i; // first Size-i bytes are addressable
756 void AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
758 Value *ShadowBase, bool DoPoison) {
759 size_t ShadowRZSize = RedzoneSize >> MappingScale;
760 assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
761 Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
762 Type *RZPtrTy = PointerType::get(RZTy, 0);
764 Value *PoisonLeft = ConstantInt::get(RZTy,
765 ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
766 Value *PoisonMid = ConstantInt::get(RZTy,
767 ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
768 Value *PoisonRight = ConstantInt::get(RZTy,
769 ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
771 // poison the first red zone.
772 IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
774 // poison all other red zones.
775 uint64_t Pos = RedzoneSize;
776 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
777 AllocaInst *AI = AllocaVec[i];
778 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
779 uint64_t AlignedSize = getAlignedAllocaSize(AI);
780 assert(AlignedSize - SizeInBytes < RedzoneSize);
785 assert(ShadowBase->getType() == IntptrTy);
786 if (SizeInBytes < AlignedSize) {
787 // Poison the partial redzone at right
789 ShadowBase, ConstantInt::get(IntptrTy,
790 (Pos >> MappingScale) - ShadowRZSize));
791 size_t AddressableBytes = RedzoneSize - (AlignedSize - SizeInBytes);
794 PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
796 1ULL << MappingScale,
797 kAsanStackPartialRedzoneMagic);
799 Value *PartialPoison = ConstantInt::get(RZTy, Poison);
800 IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
803 // Poison the full redzone at right.
804 Ptr = IRB.CreateAdd(ShadowBase,
805 ConstantInt::get(IntptrTy, Pos >> MappingScale));
806 Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
807 IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
813 // Workaround for bug 11395: we don't want to instrument stack in functions
814 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
815 // FIXME: remove once the bug 11395 is fixed.
816 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
817 if (LongSize != 32) return false;
818 CallInst *CI = dyn_cast<CallInst>(I);
819 if (!CI || !CI->isInlineAsm()) return false;
820 if (CI->getNumArgOperands() <= 5) return false;
821 // We have inline assembly with quite a few arguments.
825 // Find all static Alloca instructions and put
826 // poisoned red zones around all of them.
827 // Then unpoison everything back before the function returns.
829 // Stack poisoning does not play well with exception handling.
830 // When an exception is thrown, we essentially bypass the code
831 // that unpoisones the stack. This is why the run-time library has
832 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
833 // stack in the interceptor. This however does not work inside the
834 // actual function which catches the exception. Most likely because the
835 // compiler hoists the load of the shadow value somewhere too high.
836 // This causes asan to report a non-existing bug on 453.povray.
837 // It sounds like an LLVM bug.
838 bool AddressSanitizer::poisonStackInFunction(Module &M, Function &F) {
839 if (!ClStack) return false;
840 SmallVector<AllocaInst*, 16> AllocaVec;
841 SmallVector<Instruction*, 8> RetVec;
842 uint64_t TotalSize = 0;
844 // Filter out Alloca instructions we want (and can) handle.
845 // Collect Ret instructions.
846 for (Function::iterator FI = F.begin(), FE = F.end();
848 BasicBlock &BB = *FI;
849 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
851 if (LooksLikeCodeInBug11395(BI)) return false;
852 if (isa<ReturnInst>(BI)) {
853 RetVec.push_back(BI);
857 AllocaInst *AI = dyn_cast<AllocaInst>(BI);
859 if (AI->isArrayAllocation()) continue;
860 if (!AI->isStaticAlloca()) continue;
861 if (!AI->getAllocatedType()->isSized()) continue;
862 if (AI->getAlignment() > RedzoneSize) continue;
863 AllocaVec.push_back(AI);
864 uint64_t AlignedSize = getAlignedAllocaSize(AI);
865 TotalSize += AlignedSize;
869 if (AllocaVec.empty()) return false;
871 uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize;
873 bool DoStackMalloc = ClUseAfterReturn
874 && LocalStackSize <= kMaxStackMallocSize;
876 Instruction *InsBefore = AllocaVec[0];
877 IRBuilder<> IRB(InsBefore);
880 Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
881 AllocaInst *MyAlloca =
882 new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
883 MyAlloca->setAlignment(RedzoneSize);
884 assert(MyAlloca->isStaticAlloca());
885 Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
886 Value *LocalStackBase = OrigStackBase;
889 Value *AsanStackMallocFunc = M.getOrInsertFunction(
890 kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL);
891 LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
892 ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
895 // This string will be parsed by the run-time (DescribeStackAddress).
896 SmallString<2048> StackDescriptionStorage;
897 raw_svector_ostream StackDescription(StackDescriptionStorage);
898 StackDescription << F.getName() << " " << AllocaVec.size() << " ";
900 uint64_t Pos = RedzoneSize;
901 // Replace Alloca instructions with base+offset.
902 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
903 AllocaInst *AI = AllocaVec[i];
904 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
905 StringRef Name = AI->getName();
906 StackDescription << Pos << " " << SizeInBytes << " "
907 << Name.size() << " " << Name << " ";
908 uint64_t AlignedSize = getAlignedAllocaSize(AI);
909 assert((AlignedSize % RedzoneSize) == 0);
910 AI->replaceAllUsesWith(
912 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
914 Pos += AlignedSize + RedzoneSize;
916 assert(Pos == LocalStackSize);
918 // Write the Magic value and the frame description constant to the redzone.
919 Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
920 IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
922 Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
923 ConstantInt::get(IntptrTy, LongSize/8));
924 BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
925 Value *Description = IRB.CreatePointerCast(
926 createPrivateGlobalForString(M, StackDescription.str()),
928 IRB.CreateStore(Description, BasePlus1);
930 // Poison the stack redzones at the entry.
931 Value *ShadowBase = memToShadow(LocalStackBase, IRB);
932 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
934 Value *AsanStackFreeFunc = NULL;
936 AsanStackFreeFunc = M.getOrInsertFunction(
937 kAsanStackFreeName, IRB.getVoidTy(),
938 IntptrTy, IntptrTy, IntptrTy, NULL);
941 // Unpoison the stack before all ret instructions.
942 for (size_t i = 0, n = RetVec.size(); i < n; i++) {
943 Instruction *Ret = RetVec[i];
944 IRBuilder<> IRBRet(Ret);
946 // Mark the current frame as retired.
947 IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
949 // Unpoison the stack.
950 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
953 IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
954 ConstantInt::get(IntptrTy, LocalStackSize),
966 BlackList::BlackList(const std::string &Path) {
968 const char *kFunPrefix = "fun:";
969 if (!ClBlackListFile.size()) return;
972 OwningPtr<MemoryBuffer> File;
973 if (error_code EC = MemoryBuffer::getFile(ClBlackListFile.c_str(), File)) {
974 report_fatal_error("Can't open blacklist file " + ClBlackListFile + ": " +
977 MemoryBuffer *Buff = File.take();
978 const char *Data = Buff->getBufferStart();
979 size_t DataLen = Buff->getBufferSize();
980 SmallVector<StringRef, 16> Lines;
981 SplitString(StringRef(Data, DataLen), Lines, "\n\r");
982 for (size_t i = 0, numLines = Lines.size(); i < numLines; i++) {
983 if (Lines[i].startswith(kFunPrefix)) {
984 std::string ThisFunc = Lines[i].substr(strlen(kFunPrefix));
985 std::string ThisFuncRE;
986 // add ThisFunc replacing * with .*
987 for (size_t j = 0, n = ThisFunc.size(); j < n; j++) {
988 if (ThisFunc[j] == '*')
990 ThisFuncRE += ThisFunc[j];
992 // Check that the regexp is valid.
993 Regex CheckRE(ThisFuncRE);
995 if (!CheckRE.isValid(Error))
996 report_fatal_error("malformed blacklist regex: " + ThisFunc +
998 // Append to the final regexp.
1005 Functions = new Regex(Fun);
1009 bool BlackList::isIn(const Function &F) {
1011 bool Res = Functions->match(F.getName());