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