AddressSanitizer.cpp revision 1c8b825c4396ad9cd38f713d9e9a51adae1d4c4e
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/Transforms/Instrumentation.h"
19#include "BlackList.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/DepthFirstIterator.h"
23#include "llvm/ADT/OwningPtr.h"
24#include "llvm/ADT/SmallSet.h"
25#include "llvm/ADT/SmallString.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringExtras.h"
28#include "llvm/ADT/Triple.h"
29#include "llvm/DataLayout.h"
30#include "llvm/DIBuilder.h"
31#include "llvm/Function.h"
32#include "llvm/IRBuilder.h"
33#include "llvm/InlineAsm.h"
34#include "llvm/InstVisitor.h"
35#include "llvm/IntrinsicInst.h"
36#include "llvm/LLVMContext.h"
37#include "llvm/Module.h"
38#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/DataTypes.h"
40#include "llvm/Support/Debug.h"
41#include "llvm/Support/raw_ostream.h"
42#include "llvm/Support/system_error.h"
43#include "llvm/Target/TargetMachine.h"
44#include "llvm/Transforms/Utils/BasicBlockUtils.h"
45#include "llvm/Transforms/Utils/Local.h"
46#include "llvm/Transforms/Utils/ModuleUtils.h"
47#include "llvm/Type.h"
48#include <algorithm>
49#include <string>
50
51using namespace llvm;
52
53static const uint64_t kDefaultShadowScale = 3;
54static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
55static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
56static const uint64_t kDefaultShadowOffsetAndroid = 0;
57
58static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
59static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
60static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
61
62static const char *kAsanModuleCtorName = "asan.module_ctor";
63static const char *kAsanModuleDtorName = "asan.module_dtor";
64static const int   kAsanCtorAndCtorPriority = 1;
65static const char *kAsanReportErrorTemplate = "__asan_report_";
66static const char *kAsanRegisterGlobalsName = "__asan_register_globals";
67static const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
68static const char *kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
69static const char *kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
70static const char *kAsanInitName = "__asan_init";
71static const char *kAsanHandleNoReturnName = "__asan_handle_no_return";
72static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
73static const char *kAsanMappingScaleName = "__asan_mapping_scale";
74static const char *kAsanStackMallocName = "__asan_stack_malloc";
75static const char *kAsanStackFreeName = "__asan_stack_free";
76static const char *kAsanGenPrefix = "__asan_gen_";
77static const char *kAsanPoisonStackMemoryName = "__asan_poison_stack_memory";
78static const char *kAsanUnpoisonStackMemoryName =
79    "__asan_unpoison_stack_memory";
80
81static const int kAsanStackLeftRedzoneMagic = 0xf1;
82static const int kAsanStackMidRedzoneMagic = 0xf2;
83static const int kAsanStackRightRedzoneMagic = 0xf3;
84static const int kAsanStackPartialRedzoneMagic = 0xf4;
85
86// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
87static const size_t kNumberOfAccessSizes = 5;
88
89// Command-line flags.
90
91// This flag may need to be replaced with -f[no-]asan-reads.
92static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
93       cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
94static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
95       cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
96static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
97       cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
98       cl::Hidden, cl::init(true));
99static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path",
100       cl::desc("use instrumentation with slow path for all accesses"),
101       cl::Hidden, cl::init(false));
102// This flag limits the number of instructions to be instrumented
103// in any given BB. Normally, this should be set to unlimited (INT_MAX),
104// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
105// set it to 10000.
106static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
107       cl::init(10000),
108       cl::desc("maximal number of instructions to instrument in any given BB"),
109       cl::Hidden);
110// This flag may need to be replaced with -f[no]asan-stack.
111static cl::opt<bool> ClStack("asan-stack",
112       cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
113// This flag may need to be replaced with -f[no]asan-use-after-return.
114static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
115       cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
116// This flag may need to be replaced with -f[no]asan-globals.
117static cl::opt<bool> ClGlobals("asan-globals",
118       cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
119static cl::opt<bool> ClInitializers("asan-initialization-order",
120       cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
121static cl::opt<bool> ClMemIntrin("asan-memintrin",
122       cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
123static cl::opt<bool> ClRealignStack("asan-realign-stack",
124       cl::desc("Realign stack to 32"), cl::Hidden, cl::init(true));
125static cl::opt<std::string> ClBlacklistFile("asan-blacklist",
126       cl::desc("File containing the list of objects to ignore "
127                "during instrumentation"), cl::Hidden);
128
129// These flags allow to change the shadow mapping.
130// The shadow mapping looks like
131//    Shadow = (Mem >> scale) + (1 << offset_log)
132static cl::opt<int> ClMappingScale("asan-mapping-scale",
133       cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
134static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
135       cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
136
137// Optimization flags. Not user visible, used mostly for testing
138// and benchmarking the tool.
139static cl::opt<bool> ClOpt("asan-opt",
140       cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
141static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
142       cl::desc("Instrument the same temp just once"), cl::Hidden,
143       cl::init(true));
144static cl::opt<bool> ClOptGlobals("asan-opt-globals",
145       cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
146
147static cl::opt<bool> ClCheckLifetime("asan-check-lifetime",
148       cl::desc("Use llvm.lifetime intrinsics to insert extra checks"),
149       cl::Hidden, cl::init(false));
150
151// Debug flags.
152static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
153                            cl::init(0));
154static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
155                                 cl::Hidden, cl::init(0));
156static cl::opt<std::string> ClDebugFunc("asan-debug-func",
157                                        cl::Hidden, cl::desc("Debug func"));
158static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
159                               cl::Hidden, cl::init(-1));
160static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
161                               cl::Hidden, cl::init(-1));
162
163namespace {
164/// A set of dynamically initialized globals extracted from metadata.
165class SetOfDynamicallyInitializedGlobals {
166 public:
167  void Init(Module& M) {
168    // Clang generates metadata identifying all dynamically initialized globals.
169    NamedMDNode *DynamicGlobals =
170        M.getNamedMetadata("llvm.asan.dynamically_initialized_globals");
171    if (!DynamicGlobals)
172      return;
173    for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) {
174      MDNode *MDN = DynamicGlobals->getOperand(i);
175      assert(MDN->getNumOperands() == 1);
176      Value *VG = MDN->getOperand(0);
177      // The optimizer may optimize away a global entirely, in which case we
178      // cannot instrument access to it.
179      if (!VG)
180        continue;
181      DynInitGlobals.insert(cast<GlobalVariable>(VG));
182    }
183  }
184  bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; }
185 private:
186  SmallSet<GlobalValue*, 32> DynInitGlobals;
187};
188
189static int MappingScale() {
190  return ClMappingScale ? ClMappingScale : kDefaultShadowScale;
191}
192
193static size_t RedzoneSize() {
194  // Redzone used for stack and globals is at least 32 bytes.
195  // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
196  return std::max(32U, 1U << MappingScale());
197}
198
199/// AddressSanitizer: instrument the code in module to find memory bugs.
200struct AddressSanitizer : public FunctionPass {
201  AddressSanitizer(bool CheckInitOrder = false,
202                   bool CheckUseAfterReturn = false,
203                   bool CheckLifetime = false,
204                   StringRef BlacklistFile = StringRef())
205      : FunctionPass(ID),
206        CheckInitOrder(CheckInitOrder || ClInitializers),
207        CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn),
208        CheckLifetime(CheckLifetime || ClCheckLifetime),
209        BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
210                                            : BlacklistFile) {}
211  virtual const char *getPassName() const {
212    return "AddressSanitizerFunctionPass";
213  }
214  void instrumentMop(Instruction *I);
215  void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB,
216                         Value *Addr, uint32_t TypeSize, bool IsWrite);
217  Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
218                           Value *ShadowValue, uint32_t TypeSize);
219  Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
220                                 bool IsWrite, size_t AccessSizeIndex);
221  bool instrumentMemIntrinsic(MemIntrinsic *MI);
222  void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
223                                   Value *Size,
224                                   Instruction *InsertBefore, bool IsWrite);
225  Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
226  bool runOnFunction(Function &F);
227  void createInitializerPoisonCalls(Module &M,
228                                    Value *FirstAddr, Value *LastAddr);
229  bool maybeInsertAsanInitAtFunctionEntry(Function &F);
230  virtual bool doInitialization(Module &M);
231  static char ID;  // Pass identification, replacement for typeid
232
233 private:
234  void initializeCallbacks(Module &M);
235
236  bool ShouldInstrumentGlobal(GlobalVariable *G);
237  bool LooksLikeCodeInBug11395(Instruction *I);
238  void FindDynamicInitializers(Module &M);
239
240  bool CheckInitOrder;
241  bool CheckUseAfterReturn;
242  bool CheckLifetime;
243  LLVMContext *C;
244  DataLayout *TD;
245  uint64_t MappingOffset;
246  int LongSize;
247  Type *IntptrTy;
248  Function *AsanCtorFunction;
249  Function *AsanInitFunction;
250  Function *AsanHandleNoReturnFunc;
251  SmallString<64> BlacklistFile;
252  OwningPtr<BlackList> BL;
253  // This array is indexed by AccessIsWrite and log2(AccessSize).
254  Function *AsanErrorCallback[2][kNumberOfAccessSizes];
255  InlineAsm *EmptyAsm;
256  SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
257
258  friend struct FunctionStackPoisoner;
259};
260
261class AddressSanitizerModule : public ModulePass {
262 public:
263  AddressSanitizerModule(bool CheckInitOrder = false,
264                         StringRef BlacklistFile = StringRef())
265      : ModulePass(ID),
266        CheckInitOrder(CheckInitOrder || ClInitializers),
267        BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
268                                            : BlacklistFile) {}
269  bool runOnModule(Module &M);
270  static char ID;  // Pass identification, replacement for typeid
271  virtual const char *getPassName() const {
272    return "AddressSanitizerModule";
273  }
274
275 private:
276  void initializeCallbacks(Module &M);
277
278  bool ShouldInstrumentGlobal(GlobalVariable *G);
279  void createInitializerPoisonCalls(Module &M, Value *FirstAddr,
280                                    Value *LastAddr);
281
282  bool CheckInitOrder;
283  SmallString<64> BlacklistFile;
284  OwningPtr<BlackList> BL;
285  SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
286  Type *IntptrTy;
287  LLVMContext *C;
288  DataLayout *TD;
289  Function *AsanPoisonGlobals;
290  Function *AsanUnpoisonGlobals;
291  Function *AsanRegisterGlobals;
292  Function *AsanUnregisterGlobals;
293};
294
295// Stack poisoning does not play well with exception handling.
296// When an exception is thrown, we essentially bypass the code
297// that unpoisones the stack. This is why the run-time library has
298// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
299// stack in the interceptor. This however does not work inside the
300// actual function which catches the exception. Most likely because the
301// compiler hoists the load of the shadow value somewhere too high.
302// This causes asan to report a non-existing bug on 453.povray.
303// It sounds like an LLVM bug.
304struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
305  Function &F;
306  AddressSanitizer &ASan;
307  DIBuilder DIB;
308  LLVMContext *C;
309  Type *IntptrTy;
310  Type *IntptrPtrTy;
311
312  SmallVector<AllocaInst*, 16> AllocaVec;
313  SmallVector<Instruction*, 8> RetVec;
314  uint64_t TotalStackSize;
315  unsigned StackAlignment;
316
317  Function *AsanStackMallocFunc, *AsanStackFreeFunc;
318  Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
319
320  // Stores a place and arguments of poisoning/unpoisoning call for alloca.
321  struct AllocaPoisonCall {
322    IntrinsicInst *InsBefore;
323    uint64_t Size;
324    bool DoPoison;
325  };
326  SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec;
327
328  // Maps Value to an AllocaInst from which the Value is originated.
329  typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy;
330  AllocaForValueMapTy AllocaForValue;
331
332  FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
333      : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C),
334        IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)),
335        TotalStackSize(0), StackAlignment(1 << MappingScale()) {}
336
337  bool runOnFunction() {
338    if (!ClStack) return false;
339    // Collect alloca, ret, lifetime instructions etc.
340    for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
341         DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
342      BasicBlock *BB = *DI;
343      visit(*BB);
344    }
345    if (AllocaVec.empty()) return false;
346
347    initializeCallbacks(*F.getParent());
348
349    poisonStack();
350
351    if (ClDebugStack) {
352      DEBUG(dbgs() << F);
353    }
354    return true;
355  }
356
357  // Finds all static Alloca instructions and puts
358  // poisoned red zones around all of them.
359  // Then unpoison everything back before the function returns.
360  void poisonStack();
361
362  // ----------------------- Visitors.
363  /// \brief Collect all Ret instructions.
364  void visitReturnInst(ReturnInst &RI) {
365    RetVec.push_back(&RI);
366  }
367
368  /// \brief Collect Alloca instructions we want (and can) handle.
369  void visitAllocaInst(AllocaInst &AI) {
370    if (!isInterestingAlloca(AI)) return;
371
372    StackAlignment = std::max(StackAlignment, AI.getAlignment());
373    AllocaVec.push_back(&AI);
374    uint64_t AlignedSize =  getAlignedAllocaSize(&AI);
375    TotalStackSize += AlignedSize;
376  }
377
378  /// \brief Collect lifetime intrinsic calls to check for use-after-scope
379  /// errors.
380  void visitIntrinsicInst(IntrinsicInst &II) {
381    if (!ASan.CheckLifetime) return;
382    Intrinsic::ID ID = II.getIntrinsicID();
383    if (ID != Intrinsic::lifetime_start &&
384        ID != Intrinsic::lifetime_end)
385      return;
386    // Found lifetime intrinsic, add ASan instrumentation if necessary.
387    ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
388    // If size argument is undefined, don't do anything.
389    if (Size->isMinusOne()) return;
390    // Check that size doesn't saturate uint64_t and can
391    // be stored in IntptrTy.
392    const uint64_t SizeValue = Size->getValue().getLimitedValue();
393    if (SizeValue == ~0ULL ||
394        !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
395      return;
396    // Find alloca instruction that corresponds to llvm.lifetime argument.
397    AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
398    if (!AI) return;
399    bool DoPoison = (ID == Intrinsic::lifetime_end);
400    AllocaPoisonCall APC = {&II, SizeValue, DoPoison};
401    AllocaPoisonCallVec.push_back(APC);
402  }
403
404  // ---------------------- Helpers.
405  void initializeCallbacks(Module &M);
406
407  // Check if we want (and can) handle this alloca.
408  bool isInterestingAlloca(AllocaInst &AI) {
409    return (!AI.isArrayAllocation() &&
410            AI.isStaticAlloca() &&
411            AI.getAllocatedType()->isSized());
412  }
413
414  uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
415    Type *Ty = AI->getAllocatedType();
416    uint64_t SizeInBytes = ASan.TD->getTypeAllocSize(Ty);
417    return SizeInBytes;
418  }
419  uint64_t getAlignedSize(uint64_t SizeInBytes) {
420    size_t RZ = RedzoneSize();
421    return ((SizeInBytes + RZ - 1) / RZ) * RZ;
422  }
423  uint64_t getAlignedAllocaSize(AllocaInst *AI) {
424    uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
425    return getAlignedSize(SizeInBytes);
426  }
427  /// Finds alloca where the value comes from.
428  AllocaInst *findAllocaForValue(Value *V);
429  void poisonRedZones(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
430                      Value *ShadowBase, bool DoPoison);
431  void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> IRB, bool DoPoison);
432};
433
434}  // namespace
435
436char AddressSanitizer::ID = 0;
437INITIALIZE_PASS(AddressSanitizer, "asan",
438    "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
439    false, false)
440FunctionPass *llvm::createAddressSanitizerFunctionPass(
441    bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime,
442    StringRef BlacklistFile) {
443  return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn,
444                              CheckLifetime, BlacklistFile);
445}
446
447char AddressSanitizerModule::ID = 0;
448INITIALIZE_PASS(AddressSanitizerModule, "asan-module",
449    "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
450    "ModulePass", false, false)
451ModulePass *llvm::createAddressSanitizerModulePass(
452    bool CheckInitOrder, StringRef BlacklistFile) {
453  return new AddressSanitizerModule(CheckInitOrder, BlacklistFile);
454}
455
456static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
457  size_t Res = CountTrailingZeros_32(TypeSize / 8);
458  assert(Res < kNumberOfAccessSizes);
459  return Res;
460}
461
462// Create a constant for Str so that we can pass it to the run-time lib.
463static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
464  Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
465  return new GlobalVariable(M, StrConst->getType(), true,
466                            GlobalValue::PrivateLinkage, StrConst,
467                            kAsanGenPrefix);
468}
469
470static bool GlobalWasGeneratedByAsan(GlobalVariable *G) {
471  return G->getName().find(kAsanGenPrefix) == 0;
472}
473
474Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
475  // Shadow >> scale
476  Shadow = IRB.CreateLShr(Shadow, MappingScale());
477  if (MappingOffset == 0)
478    return Shadow;
479  // (Shadow >> scale) | offset
480  return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
481                                               MappingOffset));
482}
483
484void AddressSanitizer::instrumentMemIntrinsicParam(
485    Instruction *OrigIns,
486    Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
487  // Check the first byte.
488  {
489    IRBuilder<> IRB(InsertBefore);
490    instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
491  }
492  // Check the last byte.
493  {
494    IRBuilder<> IRB(InsertBefore);
495    Value *SizeMinusOne = IRB.CreateSub(
496        Size, ConstantInt::get(Size->getType(), 1));
497    SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
498    Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
499    Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
500    instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
501  }
502}
503
504// Instrument memset/memmove/memcpy
505bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
506  Value *Dst = MI->getDest();
507  MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
508  Value *Src = MemTran ? MemTran->getSource() : 0;
509  Value *Length = MI->getLength();
510
511  Constant *ConstLength = dyn_cast<Constant>(Length);
512  Instruction *InsertBefore = MI;
513  if (ConstLength) {
514    if (ConstLength->isNullValue()) return false;
515  } else {
516    // The size is not a constant so it could be zero -- check at run-time.
517    IRBuilder<> IRB(InsertBefore);
518
519    Value *Cmp = IRB.CreateICmpNE(Length,
520                                  Constant::getNullValue(Length->getType()));
521    InsertBefore = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
522  }
523
524  instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
525  if (Src)
526    instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
527  return true;
528}
529
530// If I is an interesting memory access, return the PointerOperand
531// and set IsWrite. Otherwise return NULL.
532static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
533  if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
534    if (!ClInstrumentReads) return NULL;
535    *IsWrite = false;
536    return LI->getPointerOperand();
537  }
538  if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
539    if (!ClInstrumentWrites) return NULL;
540    *IsWrite = true;
541    return SI->getPointerOperand();
542  }
543  if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
544    if (!ClInstrumentAtomics) return NULL;
545    *IsWrite = true;
546    return RMW->getPointerOperand();
547  }
548  if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
549    if (!ClInstrumentAtomics) return NULL;
550    *IsWrite = true;
551    return XCHG->getPointerOperand();
552  }
553  return NULL;
554}
555
556void AddressSanitizer::instrumentMop(Instruction *I) {
557  bool IsWrite = false;
558  Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
559  assert(Addr);
560  if (ClOpt && ClOptGlobals) {
561    if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) {
562      // If initialization order checking is disabled, a simple access to a
563      // dynamically initialized global is always valid.
564      if (!CheckInitOrder)
565        return;
566      // If a global variable does not have dynamic initialization we don't
567      // have to instrument it.  However, if a global does not have initailizer
568      // at all, we assume it has dynamic initializer (in other TU).
569      if (G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G))
570        return;
571    }
572  }
573
574  Type *OrigPtrTy = Addr->getType();
575  Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
576
577  assert(OrigTy->isSized());
578  uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
579
580  if (TypeSize != 8  && TypeSize != 16 &&
581      TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
582    // Ignore all unusual sizes.
583    return;
584  }
585
586  IRBuilder<> IRB(I);
587  instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
588}
589
590// Validate the result of Module::getOrInsertFunction called for an interface
591// function of AddressSanitizer. If the instrumented module defines a function
592// with the same name, their prototypes must match, otherwise
593// getOrInsertFunction returns a bitcast.
594static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
595  if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
596  FuncOrBitcast->dump();
597  report_fatal_error("trying to redefine an AddressSanitizer "
598                     "interface function");
599}
600
601Instruction *AddressSanitizer::generateCrashCode(
602    Instruction *InsertBefore, Value *Addr,
603    bool IsWrite, size_t AccessSizeIndex) {
604  IRBuilder<> IRB(InsertBefore);
605  CallInst *Call = IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex],
606                                  Addr);
607  // We don't do Call->setDoesNotReturn() because the BB already has
608  // UnreachableInst at the end.
609  // This EmptyAsm is required to avoid callback merge.
610  IRB.CreateCall(EmptyAsm);
611  return Call;
612}
613
614Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
615                                            Value *ShadowValue,
616                                            uint32_t TypeSize) {
617  size_t Granularity = 1 << MappingScale();
618  // Addr & (Granularity - 1)
619  Value *LastAccessedByte = IRB.CreateAnd(
620      AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
621  // (Addr & (Granularity - 1)) + size - 1
622  if (TypeSize / 8 > 1)
623    LastAccessedByte = IRB.CreateAdd(
624        LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
625  // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
626  LastAccessedByte = IRB.CreateIntCast(
627      LastAccessedByte, ShadowValue->getType(), false);
628  // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
629  return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
630}
631
632void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
633                                         IRBuilder<> &IRB, Value *Addr,
634                                         uint32_t TypeSize, bool IsWrite) {
635  Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
636
637  Type *ShadowTy  = IntegerType::get(
638      *C, std::max(8U, TypeSize >> MappingScale()));
639  Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
640  Value *ShadowPtr = memToShadow(AddrLong, IRB);
641  Value *CmpVal = Constant::getNullValue(ShadowTy);
642  Value *ShadowValue = IRB.CreateLoad(
643      IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
644
645  Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
646  size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
647  size_t Granularity = 1 << MappingScale();
648  TerminatorInst *CrashTerm = 0;
649
650  if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
651    TerminatorInst *CheckTerm =
652        SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
653    assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
654    BasicBlock *NextBB = CheckTerm->getSuccessor(0);
655    IRB.SetInsertPoint(CheckTerm);
656    Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
657    BasicBlock *CrashBlock =
658        BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
659    CrashTerm = new UnreachableInst(*C, CrashBlock);
660    BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
661    ReplaceInstWithInst(CheckTerm, NewTerm);
662  } else {
663    CrashTerm = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), true);
664  }
665
666  Instruction *Crash =
667      generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex);
668  Crash->setDebugLoc(OrigIns->getDebugLoc());
669}
670
671void AddressSanitizerModule::createInitializerPoisonCalls(
672    Module &M, Value *FirstAddr, Value *LastAddr) {
673  // We do all of our poisoning and unpoisoning within _GLOBAL__I_a.
674  Function *GlobalInit = M.getFunction("_GLOBAL__I_a");
675  // If that function is not present, this TU contains no globals, or they have
676  // all been optimized away
677  if (!GlobalInit)
678    return;
679
680  // Set up the arguments to our poison/unpoison functions.
681  IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt());
682
683  // Add a call to poison all external globals before the given function starts.
684  IRB.CreateCall2(AsanPoisonGlobals, FirstAddr, LastAddr);
685
686  // Add calls to unpoison all globals before each return instruction.
687  for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end();
688      I != E; ++I) {
689    if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) {
690      CallInst::Create(AsanUnpoisonGlobals, "", RI);
691    }
692  }
693}
694
695bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
696  Type *Ty = cast<PointerType>(G->getType())->getElementType();
697  DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
698
699  if (BL->isIn(*G)) return false;
700  if (!Ty->isSized()) return false;
701  if (!G->hasInitializer()) return false;
702  if (GlobalWasGeneratedByAsan(G)) return false;  // Our own global.
703  // Touch only those globals that will not be defined in other modules.
704  // Don't handle ODR type linkages since other modules may be built w/o asan.
705  if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
706      G->getLinkage() != GlobalVariable::PrivateLinkage &&
707      G->getLinkage() != GlobalVariable::InternalLinkage)
708    return false;
709  // Two problems with thread-locals:
710  //   - The address of the main thread's copy can't be computed at link-time.
711  //   - Need to poison all copies, not just the main thread's one.
712  if (G->isThreadLocal())
713    return false;
714  // For now, just ignore this Alloca if the alignment is large.
715  if (G->getAlignment() > RedzoneSize()) return false;
716
717  // Ignore all the globals with the names starting with "\01L_OBJC_".
718  // Many of those are put into the .cstring section. The linker compresses
719  // that section by removing the spare \0s after the string terminator, so
720  // our redzones get broken.
721  if ((G->getName().find("\01L_OBJC_") == 0) ||
722      (G->getName().find("\01l_OBJC_") == 0)) {
723    DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
724    return false;
725  }
726
727  if (G->hasSection()) {
728    StringRef Section(G->getSection());
729    // Ignore the globals from the __OBJC section. The ObjC runtime assumes
730    // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
731    // them.
732    if ((Section.find("__OBJC,") == 0) ||
733        (Section.find("__DATA, __objc_") == 0)) {
734      DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
735      return false;
736    }
737    // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
738    // Constant CFString instances are compiled in the following way:
739    //  -- the string buffer is emitted into
740    //     __TEXT,__cstring,cstring_literals
741    //  -- the constant NSConstantString structure referencing that buffer
742    //     is placed into __DATA,__cfstring
743    // Therefore there's no point in placing redzones into __DATA,__cfstring.
744    // Moreover, it causes the linker to crash on OS X 10.7
745    if (Section.find("__DATA,__cfstring") == 0) {
746      DEBUG(dbgs() << "Ignoring CFString: " << *G);
747      return false;
748    }
749  }
750
751  return true;
752}
753
754void AddressSanitizerModule::initializeCallbacks(Module &M) {
755  IRBuilder<> IRB(*C);
756  // Declare our poisoning and unpoisoning functions.
757  AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
758      kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
759  AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
760  AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
761      kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL));
762  AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
763  // Declare functions that register/unregister globals.
764  AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
765      kAsanRegisterGlobalsName, IRB.getVoidTy(),
766      IntptrTy, IntptrTy, NULL));
767  AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
768  AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
769      kAsanUnregisterGlobalsName,
770      IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
771  AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
772}
773
774// This function replaces all global variables with new variables that have
775// trailing redzones. It also creates a function that poisons
776// redzones and inserts this function into llvm.global_ctors.
777bool AddressSanitizerModule::runOnModule(Module &M) {
778  if (!ClGlobals) return false;
779  TD = getAnalysisIfAvailable<DataLayout>();
780  if (!TD)
781    return false;
782  BL.reset(new BlackList(BlacklistFile));
783  if (BL->isIn(M)) return false;
784  C = &(M.getContext());
785  IntptrTy = Type::getIntNTy(*C, TD->getPointerSizeInBits());
786  initializeCallbacks(M);
787  DynamicallyInitializedGlobals.Init(M);
788
789  SmallVector<GlobalVariable *, 16> GlobalsToChange;
790
791  for (Module::GlobalListType::iterator G = M.global_begin(),
792       E = M.global_end(); G != E; ++G) {
793    if (ShouldInstrumentGlobal(G))
794      GlobalsToChange.push_back(G);
795  }
796
797  size_t n = GlobalsToChange.size();
798  if (n == 0) return false;
799
800  // A global is described by a structure
801  //   size_t beg;
802  //   size_t size;
803  //   size_t size_with_redzone;
804  //   const char *name;
805  //   size_t has_dynamic_init;
806  // We initialize an array of such structures and pass it to a run-time call.
807  StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
808                                               IntptrTy, IntptrTy,
809                                               IntptrTy, NULL);
810  SmallVector<Constant *, 16> Initializers(n), DynamicInit;
811
812
813  Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
814  assert(CtorFunc);
815  IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
816
817  // The addresses of the first and last dynamically initialized globals in
818  // this TU.  Used in initialization order checking.
819  Value *FirstDynamic = 0, *LastDynamic = 0;
820
821  for (size_t i = 0; i < n; i++) {
822    GlobalVariable *G = GlobalsToChange[i];
823    PointerType *PtrTy = cast<PointerType>(G->getType());
824    Type *Ty = PtrTy->getElementType();
825    uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
826    size_t RZ = RedzoneSize();
827    uint64_t RightRedzoneSize = RZ + (RZ - (SizeInBytes % RZ));
828    Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
829    // Determine whether this global should be poisoned in initialization.
830    bool GlobalHasDynamicInitializer =
831        DynamicallyInitializedGlobals.Contains(G);
832    // Don't check initialization order if this global is blacklisted.
833    GlobalHasDynamicInitializer &= !BL->isInInit(*G);
834
835    StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
836    Constant *NewInitializer = ConstantStruct::get(
837        NewTy, G->getInitializer(),
838        Constant::getNullValue(RightRedZoneTy), NULL);
839
840    SmallString<2048> DescriptionOfGlobal = G->getName();
841    DescriptionOfGlobal += " (";
842    DescriptionOfGlobal += M.getModuleIdentifier();
843    DescriptionOfGlobal += ")";
844    GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
845
846    // Create a new global variable with enough space for a redzone.
847    GlobalVariable *NewGlobal = new GlobalVariable(
848        M, NewTy, G->isConstant(), G->getLinkage(),
849        NewInitializer, "", G, G->getThreadLocalMode());
850    NewGlobal->copyAttributesFrom(G);
851    NewGlobal->setAlignment(RZ);
852
853    Value *Indices2[2];
854    Indices2[0] = IRB.getInt32(0);
855    Indices2[1] = IRB.getInt32(0);
856
857    G->replaceAllUsesWith(
858        ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
859    NewGlobal->takeName(G);
860    G->eraseFromParent();
861
862    Initializers[i] = ConstantStruct::get(
863        GlobalStructTy,
864        ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
865        ConstantInt::get(IntptrTy, SizeInBytes),
866        ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
867        ConstantExpr::getPointerCast(Name, IntptrTy),
868        ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer),
869        NULL);
870
871    // Populate the first and last globals declared in this TU.
872    if (CheckInitOrder && GlobalHasDynamicInitializer) {
873      LastDynamic = ConstantExpr::getPointerCast(NewGlobal, IntptrTy);
874      if (FirstDynamic == 0)
875        FirstDynamic = LastDynamic;
876    }
877
878    DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
879  }
880
881  ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
882  GlobalVariable *AllGlobals = new GlobalVariable(
883      M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
884      ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
885
886  // Create calls for poisoning before initializers run and unpoisoning after.
887  if (CheckInitOrder && FirstDynamic && LastDynamic)
888    createInitializerPoisonCalls(M, FirstDynamic, LastDynamic);
889  IRB.CreateCall2(AsanRegisterGlobals,
890                  IRB.CreatePointerCast(AllGlobals, IntptrTy),
891                  ConstantInt::get(IntptrTy, n));
892
893  // We also need to unregister globals at the end, e.g. when a shared library
894  // gets closed.
895  Function *AsanDtorFunction = Function::Create(
896      FunctionType::get(Type::getVoidTy(*C), false),
897      GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
898  BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
899  IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
900  IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
901                       IRB.CreatePointerCast(AllGlobals, IntptrTy),
902                       ConstantInt::get(IntptrTy, n));
903  appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
904
905  DEBUG(dbgs() << M);
906  return true;
907}
908
909void AddressSanitizer::initializeCallbacks(Module &M) {
910  IRBuilder<> IRB(*C);
911  // Create __asan_report* callbacks.
912  for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
913    for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
914         AccessSizeIndex++) {
915      // IsWrite and TypeSize are encoded in the function name.
916      std::string FunctionName = std::string(kAsanReportErrorTemplate) +
917          (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
918      // If we are merging crash callbacks, they have two parameters.
919      AsanErrorCallback[AccessIsWrite][AccessSizeIndex] =
920          checkInterfaceFunction(M.getOrInsertFunction(
921              FunctionName, IRB.getVoidTy(), IntptrTy, NULL));
922    }
923  }
924
925  AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction(
926      kAsanHandleNoReturnName, IRB.getVoidTy(), NULL));
927  // We insert an empty inline asm after __asan_report* to avoid callback merge.
928  EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
929                            StringRef(""), StringRef(""),
930                            /*hasSideEffects=*/true);
931}
932
933// virtual
934bool AddressSanitizer::doInitialization(Module &M) {
935  // Initialize the private fields. No one has accessed them before.
936  TD = getAnalysisIfAvailable<DataLayout>();
937
938  if (!TD)
939    return false;
940  BL.reset(new BlackList(BlacklistFile));
941  DynamicallyInitializedGlobals.Init(M);
942
943  C = &(M.getContext());
944  LongSize = TD->getPointerSizeInBits();
945  IntptrTy = Type::getIntNTy(*C, LongSize);
946
947  AsanCtorFunction = Function::Create(
948      FunctionType::get(Type::getVoidTy(*C), false),
949      GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
950  BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
951  // call __asan_init in the module ctor.
952  IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB));
953  AsanInitFunction = checkInterfaceFunction(
954      M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
955  AsanInitFunction->setLinkage(Function::ExternalLinkage);
956  IRB.CreateCall(AsanInitFunction);
957
958  llvm::Triple targetTriple(M.getTargetTriple());
959  bool isAndroid = targetTriple.getEnvironment() == llvm::Triple::Android;
960
961  MappingOffset = isAndroid ? kDefaultShadowOffsetAndroid :
962    (LongSize == 32 ? kDefaultShadowOffset32 : kDefaultShadowOffset64);
963  if (ClMappingOffsetLog >= 0) {
964    if (ClMappingOffsetLog == 0) {
965      // special case
966      MappingOffset = 0;
967    } else {
968      MappingOffset = 1ULL << ClMappingOffsetLog;
969    }
970  }
971
972
973  if (ClMappingOffsetLog >= 0) {
974    // Tell the run-time the current values of mapping offset and scale.
975    GlobalValue *asan_mapping_offset =
976        new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
977                       ConstantInt::get(IntptrTy, MappingOffset),
978                       kAsanMappingOffsetName);
979    // Read the global, otherwise it may be optimized away.
980    IRB.CreateLoad(asan_mapping_offset, true);
981  }
982  if (ClMappingScale) {
983    GlobalValue *asan_mapping_scale =
984        new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
985                           ConstantInt::get(IntptrTy, MappingScale()),
986                           kAsanMappingScaleName);
987    // Read the global, otherwise it may be optimized away.
988    IRB.CreateLoad(asan_mapping_scale, true);
989  }
990
991  appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
992
993  return true;
994}
995
996bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
997  // For each NSObject descendant having a +load method, this method is invoked
998  // by the ObjC runtime before any of the static constructors is called.
999  // Therefore we need to instrument such methods with a call to __asan_init
1000  // at the beginning in order to initialize our runtime before any access to
1001  // the shadow memory.
1002  // We cannot just ignore these methods, because they may call other
1003  // instrumented functions.
1004  if (F.getName().find(" load]") != std::string::npos) {
1005    IRBuilder<> IRB(F.begin()->begin());
1006    IRB.CreateCall(AsanInitFunction);
1007    return true;
1008  }
1009  return false;
1010}
1011
1012bool AddressSanitizer::runOnFunction(Function &F) {
1013  if (BL->isIn(F)) return false;
1014  if (&F == AsanCtorFunction) return false;
1015  DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
1016  initializeCallbacks(*F.getParent());
1017
1018  // If needed, insert __asan_init before checking for AddressSafety attr.
1019  maybeInsertAsanInitAtFunctionEntry(F);
1020
1021  if (!F.getFnAttributes().hasAttribute(Attribute::AddressSafety))
1022    return false;
1023
1024  if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
1025    return false;
1026
1027  // We want to instrument every address only once per basic block (unless there
1028  // are calls between uses).
1029  SmallSet<Value*, 16> TempsToInstrument;
1030  SmallVector<Instruction*, 16> ToInstrument;
1031  SmallVector<Instruction*, 8> NoReturnCalls;
1032  bool IsWrite;
1033
1034  // Fill the set of memory operations to instrument.
1035  for (Function::iterator FI = F.begin(), FE = F.end();
1036       FI != FE; ++FI) {
1037    TempsToInstrument.clear();
1038    int NumInsnsPerBB = 0;
1039    for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
1040         BI != BE; ++BI) {
1041      if (LooksLikeCodeInBug11395(BI)) return false;
1042      if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) {
1043        if (ClOpt && ClOptSameTemp) {
1044          if (!TempsToInstrument.insert(Addr))
1045            continue;  // We've seen this temp in the current BB.
1046        }
1047      } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
1048        // ok, take it.
1049      } else {
1050        if (CallInst *CI = dyn_cast<CallInst>(BI)) {
1051          // A call inside BB.
1052          TempsToInstrument.clear();
1053          if (CI->doesNotReturn()) {
1054            NoReturnCalls.push_back(CI);
1055          }
1056        }
1057        continue;
1058      }
1059      ToInstrument.push_back(BI);
1060      NumInsnsPerBB++;
1061      if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
1062        break;
1063    }
1064  }
1065
1066  // Instrument.
1067  int NumInstrumented = 0;
1068  for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
1069    Instruction *Inst = ToInstrument[i];
1070    if (ClDebugMin < 0 || ClDebugMax < 0 ||
1071        (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
1072      if (isInterestingMemoryAccess(Inst, &IsWrite))
1073        instrumentMop(Inst);
1074      else
1075        instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
1076    }
1077    NumInstrumented++;
1078  }
1079
1080  FunctionStackPoisoner FSP(F, *this);
1081  bool ChangedStack = FSP.runOnFunction();
1082
1083  // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
1084  // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
1085  for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
1086    Instruction *CI = NoReturnCalls[i];
1087    IRBuilder<> IRB(CI);
1088    IRB.CreateCall(AsanHandleNoReturnFunc);
1089  }
1090  DEBUG(dbgs() << "ASAN done instrumenting:\n" << F << "\n");
1091
1092  return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
1093}
1094
1095static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
1096  if (ShadowRedzoneSize == 1) return PoisonByte;
1097  if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
1098  if (ShadowRedzoneSize == 4)
1099    return (PoisonByte << 24) + (PoisonByte << 16) +
1100        (PoisonByte << 8) + (PoisonByte);
1101  llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
1102}
1103
1104static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
1105                                            size_t Size,
1106                                            size_t RZSize,
1107                                            size_t ShadowGranularity,
1108                                            uint8_t Magic) {
1109  for (size_t i = 0; i < RZSize;
1110       i+= ShadowGranularity, Shadow++) {
1111    if (i + ShadowGranularity <= Size) {
1112      *Shadow = 0;  // fully addressable
1113    } else if (i >= Size) {
1114      *Shadow = Magic;  // unaddressable
1115    } else {
1116      *Shadow = Size - i;  // first Size-i bytes are addressable
1117    }
1118  }
1119}
1120
1121// Workaround for bug 11395: we don't want to instrument stack in functions
1122// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
1123// FIXME: remove once the bug 11395 is fixed.
1124bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
1125  if (LongSize != 32) return false;
1126  CallInst *CI = dyn_cast<CallInst>(I);
1127  if (!CI || !CI->isInlineAsm()) return false;
1128  if (CI->getNumArgOperands() <= 5) return false;
1129  // We have inline assembly with quite a few arguments.
1130  return true;
1131}
1132
1133void FunctionStackPoisoner::initializeCallbacks(Module &M) {
1134  IRBuilder<> IRB(*C);
1135  AsanStackMallocFunc = checkInterfaceFunction(M.getOrInsertFunction(
1136      kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL));
1137  AsanStackFreeFunc = checkInterfaceFunction(M.getOrInsertFunction(
1138      kAsanStackFreeName, IRB.getVoidTy(),
1139      IntptrTy, IntptrTy, IntptrTy, NULL));
1140  AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1141      kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1142  AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1143      kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1144}
1145
1146void FunctionStackPoisoner::poisonRedZones(
1147  const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB, Value *ShadowBase,
1148  bool DoPoison) {
1149  size_t ShadowRZSize = RedzoneSize() >> MappingScale();
1150  assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
1151  Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
1152  Type *RZPtrTy = PointerType::get(RZTy, 0);
1153
1154  Value *PoisonLeft  = ConstantInt::get(RZTy,
1155    ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
1156  Value *PoisonMid   = ConstantInt::get(RZTy,
1157    ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
1158  Value *PoisonRight = ConstantInt::get(RZTy,
1159    ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
1160
1161  // poison the first red zone.
1162  IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
1163
1164  // poison all other red zones.
1165  uint64_t Pos = RedzoneSize();
1166  for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1167    AllocaInst *AI = AllocaVec[i];
1168    uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1169    uint64_t AlignedSize = getAlignedAllocaSize(AI);
1170    assert(AlignedSize - SizeInBytes < RedzoneSize());
1171    Value *Ptr = NULL;
1172
1173    Pos += AlignedSize;
1174
1175    assert(ShadowBase->getType() == IntptrTy);
1176    if (SizeInBytes < AlignedSize) {
1177      // Poison the partial redzone at right
1178      Ptr = IRB.CreateAdd(
1179          ShadowBase, ConstantInt::get(IntptrTy,
1180                                       (Pos >> MappingScale()) - ShadowRZSize));
1181      size_t AddressableBytes = RedzoneSize() - (AlignedSize - SizeInBytes);
1182      uint32_t Poison = 0;
1183      if (DoPoison) {
1184        PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
1185                                        RedzoneSize(),
1186                                        1ULL << MappingScale(),
1187                                        kAsanStackPartialRedzoneMagic);
1188      }
1189      Value *PartialPoison = ConstantInt::get(RZTy, Poison);
1190      IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
1191    }
1192
1193    // Poison the full redzone at right.
1194    Ptr = IRB.CreateAdd(ShadowBase,
1195                        ConstantInt::get(IntptrTy, Pos >> MappingScale()));
1196    bool LastAlloca = (i == AllocaVec.size() - 1);
1197    Value *Poison = LastAlloca ? PoisonRight : PoisonMid;
1198    IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
1199
1200    Pos += RedzoneSize();
1201  }
1202}
1203
1204void FunctionStackPoisoner::poisonStack() {
1205  uint64_t LocalStackSize = TotalStackSize +
1206                            (AllocaVec.size() + 1) * RedzoneSize();
1207
1208  bool DoStackMalloc = ASan.CheckUseAfterReturn
1209      && LocalStackSize <= kMaxStackMallocSize;
1210
1211  assert(AllocaVec.size() > 0);
1212  Instruction *InsBefore = AllocaVec[0];
1213  IRBuilder<> IRB(InsBefore);
1214
1215
1216  Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
1217  AllocaInst *MyAlloca =
1218      new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
1219  if (ClRealignStack && StackAlignment < RedzoneSize())
1220    StackAlignment = RedzoneSize();
1221  MyAlloca->setAlignment(StackAlignment);
1222  assert(MyAlloca->isStaticAlloca());
1223  Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
1224  Value *LocalStackBase = OrigStackBase;
1225
1226  if (DoStackMalloc) {
1227    LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
1228        ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
1229  }
1230
1231  // This string will be parsed by the run-time (DescribeStackAddress).
1232  SmallString<2048> StackDescriptionStorage;
1233  raw_svector_ostream StackDescription(StackDescriptionStorage);
1234  StackDescription << F.getName() << " " << AllocaVec.size() << " ";
1235
1236  // Insert poison calls for lifetime intrinsics for alloca.
1237  bool HavePoisonedAllocas = false;
1238  for (size_t i = 0, n = AllocaPoisonCallVec.size(); i < n; i++) {
1239    const AllocaPoisonCall &APC = AllocaPoisonCallVec[i];
1240    IntrinsicInst *II = APC.InsBefore;
1241    AllocaInst *AI = findAllocaForValue(II->getArgOperand(1));
1242    assert(AI);
1243    IRBuilder<> IRB(II);
1244    poisonAlloca(AI, APC.Size, IRB, APC.DoPoison);
1245    HavePoisonedAllocas |= APC.DoPoison;
1246  }
1247
1248  uint64_t Pos = RedzoneSize();
1249  // Replace Alloca instructions with base+offset.
1250  for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1251    AllocaInst *AI = AllocaVec[i];
1252    uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1253    StringRef Name = AI->getName();
1254    StackDescription << Pos << " " << SizeInBytes << " "
1255                     << Name.size() << " " << Name << " ";
1256    uint64_t AlignedSize = getAlignedAllocaSize(AI);
1257    assert((AlignedSize % RedzoneSize()) == 0);
1258    Value *NewAllocaPtr = IRB.CreateIntToPtr(
1259            IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
1260            AI->getType());
1261    replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB);
1262    AI->replaceAllUsesWith(NewAllocaPtr);
1263    Pos += AlignedSize + RedzoneSize();
1264  }
1265  assert(Pos == LocalStackSize);
1266
1267  // Write the Magic value and the frame description constant to the redzone.
1268  Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1269  IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1270                  BasePlus0);
1271  Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
1272                                   ConstantInt::get(IntptrTy,
1273                                                    ASan.LongSize/8));
1274  BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
1275  GlobalVariable *StackDescriptionGlobal =
1276      createPrivateGlobalForString(*F.getParent(), StackDescription.str());
1277  Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal,
1278                                             IntptrTy);
1279  IRB.CreateStore(Description, BasePlus1);
1280
1281  // Poison the stack redzones at the entry.
1282  Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
1283  poisonRedZones(AllocaVec, IRB, ShadowBase, true);
1284
1285  // Unpoison the stack before all ret instructions.
1286  for (size_t i = 0, n = RetVec.size(); i < n; i++) {
1287    Instruction *Ret = RetVec[i];
1288    IRBuilder<> IRBRet(Ret);
1289    // Mark the current frame as retired.
1290    IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1291                       BasePlus0);
1292    // Unpoison the stack.
1293    poisonRedZones(AllocaVec, IRBRet, ShadowBase, false);
1294    if (DoStackMalloc) {
1295      // In use-after-return mode, mark the whole stack frame unaddressable.
1296      IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
1297                         ConstantInt::get(IntptrTy, LocalStackSize),
1298                         OrigStackBase);
1299    } else if (HavePoisonedAllocas) {
1300      // If we poisoned some allocas in llvm.lifetime analysis,
1301      // unpoison whole stack frame now.
1302      assert(LocalStackBase == OrigStackBase);
1303      poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false);
1304    }
1305  }
1306
1307  // We are done. Remove the old unused alloca instructions.
1308  for (size_t i = 0, n = AllocaVec.size(); i < n; i++)
1309    AllocaVec[i]->eraseFromParent();
1310}
1311
1312void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
1313                                         IRBuilder<> IRB, bool DoPoison) {
1314  // For now just insert the call to ASan runtime.
1315  Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
1316  Value *SizeArg = ConstantInt::get(IntptrTy, Size);
1317  IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc
1318                           : AsanUnpoisonStackMemoryFunc,
1319                  AddrArg, SizeArg);
1320}
1321
1322// Handling llvm.lifetime intrinsics for a given %alloca:
1323// (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
1324// (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
1325//     invalid accesses) and unpoison it for llvm.lifetime.start (the memory
1326//     could be poisoned by previous llvm.lifetime.end instruction, as the
1327//     variable may go in and out of scope several times, e.g. in loops).
1328// (3) if we poisoned at least one %alloca in a function,
1329//     unpoison the whole stack frame at function exit.
1330
1331AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) {
1332  if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
1333    // We're intested only in allocas we can handle.
1334    return isInterestingAlloca(*AI) ? AI : 0;
1335  // See if we've already calculated (or started to calculate) alloca for a
1336  // given value.
1337  AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
1338  if (I != AllocaForValue.end())
1339    return I->second;
1340  // Store 0 while we're calculating alloca for value V to avoid
1341  // infinite recursion if the value references itself.
1342  AllocaForValue[V] = 0;
1343  AllocaInst *Res = 0;
1344  if (CastInst *CI = dyn_cast<CastInst>(V))
1345    Res = findAllocaForValue(CI->getOperand(0));
1346  else if (PHINode *PN = dyn_cast<PHINode>(V)) {
1347    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1348      Value *IncValue = PN->getIncomingValue(i);
1349      // Allow self-referencing phi-nodes.
1350      if (IncValue == PN) continue;
1351      AllocaInst *IncValueAI = findAllocaForValue(IncValue);
1352      // AI for incoming values should exist and should all be equal.
1353      if (IncValueAI == 0 || (Res != 0 && IncValueAI != Res))
1354        return 0;
1355      Res = IncValueAI;
1356    }
1357  }
1358  if (Res != 0)
1359    AllocaForValue[V] = Res;
1360  return Res;
1361}
1362