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