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#include "llvm/Transforms/Instrumentation.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/DepthFirstIterator.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringExtras.h"
26#include "llvm/ADT/Triple.h"
27#include "llvm/IR/CallSite.h"
28#include "llvm/IR/DIBuilder.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/IRBuilder.h"
32#include "llvm/IR/InlineAsm.h"
33#include "llvm/IR/InstVisitor.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/MDBuilder.h"
37#include "llvm/IR/Module.h"
38#include "llvm/IR/Type.h"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/DataTypes.h"
41#include "llvm/Support/Debug.h"
42#include "llvm/Support/Endian.h"
43#include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
44#include "llvm/Transforms/Utils/BasicBlockUtils.h"
45#include "llvm/Transforms/Utils/Cloning.h"
46#include "llvm/Transforms/Utils/Local.h"
47#include "llvm/Transforms/Utils/ModuleUtils.h"
48#include <algorithm>
49#include <string>
50#include <system_error>
51
52using namespace llvm;
53
54#define DEBUG_TYPE "asan"
55
56static const uint64_t kDefaultShadowScale = 3;
57static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
58static const uint64_t kIOSShadowOffset32 = 1ULL << 30;
59static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
60static const uint64_t kSmallX86_64ShadowOffset = 0x7FFF8000;  // < 2G.
61static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41;
62static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa8000;
63static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
64static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
65
66static const size_t kMinStackMallocSize = 1 << 6;  // 64B
67static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
68static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
69static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
70
71static const char *const kAsanModuleCtorName = "asan.module_ctor";
72static const char *const kAsanModuleDtorName = "asan.module_dtor";
73static const int         kAsanCtorAndDtorPriority = 1;
74static const char *const kAsanReportErrorTemplate = "__asan_report_";
75static const char *const kAsanReportLoadN = "__asan_report_load_n";
76static const char *const kAsanReportStoreN = "__asan_report_store_n";
77static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
78static const char *const kAsanUnregisterGlobalsName =
79    "__asan_unregister_globals";
80static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
81static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
82static const char *const kAsanInitName = "__asan_init_v4";
83static const char *const kAsanCovModuleInitName = "__sanitizer_cov_module_init";
84static const char *const kAsanCovName = "__sanitizer_cov";
85static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp";
86static const char *const kAsanPtrSub = "__sanitizer_ptr_sub";
87static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return";
88static const int         kMaxAsanStackMallocSizeClass = 10;
89static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_";
90static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_";
91static const char *const kAsanGenPrefix = "__asan_gen_";
92static const char *const kAsanPoisonStackMemoryName =
93    "__asan_poison_stack_memory";
94static const char *const kAsanUnpoisonStackMemoryName =
95    "__asan_unpoison_stack_memory";
96
97static const char *const kAsanOptionDetectUAR =
98    "__asan_option_detect_stack_use_after_return";
99
100#ifndef NDEBUG
101static const int kAsanStackAfterReturnMagic = 0xf5;
102#endif
103
104// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
105static const size_t kNumberOfAccessSizes = 5;
106
107// Command-line flags.
108
109// This flag may need to be replaced with -f[no-]asan-reads.
110static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
111       cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
112static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
113       cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
114static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
115       cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
116       cl::Hidden, cl::init(true));
117static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path",
118       cl::desc("use instrumentation with slow path for all accesses"),
119       cl::Hidden, cl::init(false));
120// This flag limits the number of instructions to be instrumented
121// in any given BB. Normally, this should be set to unlimited (INT_MAX),
122// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
123// set it to 10000.
124static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
125       cl::init(10000),
126       cl::desc("maximal number of instructions to instrument in any given BB"),
127       cl::Hidden);
128// This flag may need to be replaced with -f[no]asan-stack.
129static cl::opt<bool> ClStack("asan-stack",
130       cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
131static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
132       cl::desc("Check return-after-free"), cl::Hidden, cl::init(true));
133// This flag may need to be replaced with -f[no]asan-globals.
134static cl::opt<bool> ClGlobals("asan-globals",
135       cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
136static cl::opt<int> ClCoverage("asan-coverage",
137       cl::desc("ASan coverage. 0: none, 1: entry block, 2: all blocks"),
138       cl::Hidden, cl::init(false));
139static cl::opt<int> ClCoverageBlockThreshold("asan-coverage-block-threshold",
140       cl::desc("Add coverage instrumentation only to the entry block if there "
141                "are more than this number of blocks."),
142       cl::Hidden, cl::init(1500));
143static cl::opt<bool> ClInitializers("asan-initialization-order",
144       cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(true));
145static cl::opt<bool> ClInvalidPointerPairs("asan-detect-invalid-pointer-pair",
146       cl::desc("Instrument <, <=, >, >=, - with pointer operands"),
147       cl::Hidden, cl::init(false));
148static cl::opt<unsigned> ClRealignStack("asan-realign-stack",
149       cl::desc("Realign stack to the value of this flag (power of two)"),
150       cl::Hidden, cl::init(32));
151static cl::opt<int> ClInstrumentationWithCallsThreshold(
152    "asan-instrumentation-with-call-threshold",
153       cl::desc("If the function being instrumented contains more than "
154                "this number of memory accesses, use callbacks instead of "
155                "inline checks (-1 means never use callbacks)."),
156       cl::Hidden, cl::init(7000));
157static cl::opt<std::string> ClMemoryAccessCallbackPrefix(
158       "asan-memory-access-callback-prefix",
159       cl::desc("Prefix for memory access callbacks"), cl::Hidden,
160       cl::init("__asan_"));
161
162// This is an experimental feature that will allow to choose between
163// instrumented and non-instrumented code at link-time.
164// If this option is on, just before instrumenting a function we create its
165// clone; if the function is not changed by asan the clone is deleted.
166// If we end up with a clone, we put the instrumented function into a section
167// called "ASAN" and the uninstrumented function into a section called "NOASAN".
168//
169// This is still a prototype, we need to figure out a way to keep two copies of
170// a function so that the linker can easily choose one of them.
171static cl::opt<bool> ClKeepUninstrumented("asan-keep-uninstrumented-functions",
172       cl::desc("Keep uninstrumented copies of functions"),
173       cl::Hidden, cl::init(false));
174
175// These flags allow to change the shadow mapping.
176// The shadow mapping looks like
177//    Shadow = (Mem >> scale) + (1 << offset_log)
178static cl::opt<int> ClMappingScale("asan-mapping-scale",
179       cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
180
181// Optimization flags. Not user visible, used mostly for testing
182// and benchmarking the tool.
183static cl::opt<bool> ClOpt("asan-opt",
184       cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
185static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
186       cl::desc("Instrument the same temp just once"), cl::Hidden,
187       cl::init(true));
188static cl::opt<bool> ClOptGlobals("asan-opt-globals",
189       cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
190
191static cl::opt<bool> ClCheckLifetime("asan-check-lifetime",
192       cl::desc("Use llvm.lifetime intrinsics to insert extra checks"),
193       cl::Hidden, cl::init(false));
194
195// Debug flags.
196static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
197                            cl::init(0));
198static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
199                                 cl::Hidden, cl::init(0));
200static cl::opt<std::string> ClDebugFunc("asan-debug-func",
201                                        cl::Hidden, cl::desc("Debug func"));
202static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
203                               cl::Hidden, cl::init(-1));
204static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
205                               cl::Hidden, cl::init(-1));
206
207STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
208STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
209STATISTIC(NumOptimizedAccessesToGlobalArray,
210          "Number of optimized accesses to global arrays");
211STATISTIC(NumOptimizedAccessesToGlobalVar,
212          "Number of optimized accesses to global vars");
213
214namespace {
215/// Frontend-provided metadata for global variables.
216class GlobalsMetadata {
217 public:
218  GlobalsMetadata() : inited_(false) {}
219  void init(Module& M) {
220    assert(!inited_);
221    inited_ = true;
222    NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals");
223    if (!Globals)
224      return;
225    for (auto MDN : Globals->operands()) {
226      // Format of the metadata node for the global:
227      // {
228      //   global,
229      //   source_location,
230      //   i1 is_dynamically_initialized,
231      //   i1 is_blacklisted
232      // }
233      assert(MDN->getNumOperands() == 4);
234      Value *V = MDN->getOperand(0);
235      // The optimizer may optimize away a global entirely.
236      if (!V)
237        continue;
238      GlobalVariable *GV = cast<GlobalVariable>(V);
239      if (Value *Loc = MDN->getOperand(1)) {
240        GlobalVariable *GVLoc = cast<GlobalVariable>(Loc);
241        // We may already know the source location for GV, if it was merged
242        // with another global.
243        if (SourceLocation.insert(std::make_pair(GV, GVLoc)).second)
244          addSourceLocationGlobal(GVLoc);
245      }
246      ConstantInt *IsDynInit = cast<ConstantInt>(MDN->getOperand(2));
247      if (IsDynInit->isOne())
248        DynInitGlobals.insert(GV);
249      ConstantInt *IsBlacklisted = cast<ConstantInt>(MDN->getOperand(3));
250      if (IsBlacklisted->isOne())
251        BlacklistedGlobals.insert(GV);
252    }
253  }
254
255  GlobalVariable *getSourceLocation(GlobalVariable *G) const {
256    auto Pos = SourceLocation.find(G);
257    return (Pos != SourceLocation.end()) ? Pos->second : nullptr;
258  }
259
260  /// Check if the global is dynamically initialized.
261  bool isDynInit(GlobalVariable *G) const {
262    return DynInitGlobals.count(G);
263  }
264
265  /// Check if the global was blacklisted.
266  bool isBlacklisted(GlobalVariable *G) const {
267    return BlacklistedGlobals.count(G);
268  }
269
270  /// Check if the global was generated to describe source location of another
271  /// global (we don't want to instrument them).
272  bool isSourceLocationGlobal(GlobalVariable *G) const {
273    return LocationGlobals.count(G);
274  }
275
276 private:
277  bool inited_;
278  DenseMap<GlobalVariable*, GlobalVariable*> SourceLocation;
279  DenseSet<GlobalVariable*> DynInitGlobals;
280  DenseSet<GlobalVariable*> BlacklistedGlobals;
281  DenseSet<GlobalVariable*> LocationGlobals;
282
283  void addSourceLocationGlobal(GlobalVariable *SourceLocGV) {
284    // Source location global is a struct with layout:
285    // {
286    //    filename,
287    //    i32 line_number,
288    //    i32 column_number,
289    // }
290    LocationGlobals.insert(SourceLocGV);
291    ConstantStruct *Contents =
292        cast<ConstantStruct>(SourceLocGV->getInitializer());
293    GlobalVariable *FilenameGV = cast<GlobalVariable>(Contents->getOperand(0));
294    LocationGlobals.insert(FilenameGV);
295  }
296};
297
298/// This struct defines the shadow mapping using the rule:
299///   shadow = (mem >> Scale) ADD-or-OR Offset.
300struct ShadowMapping {
301  int Scale;
302  uint64_t Offset;
303  bool OrShadowOffset;
304};
305
306static ShadowMapping getShadowMapping(const Module &M, int LongSize) {
307  llvm::Triple TargetTriple(M.getTargetTriple());
308  bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android;
309  bool IsIOS = TargetTriple.getOS() == llvm::Triple::IOS;
310  bool IsFreeBSD = TargetTriple.getOS() == llvm::Triple::FreeBSD;
311  bool IsLinux = TargetTriple.getOS() == llvm::Triple::Linux;
312  bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64 ||
313                 TargetTriple.getArch() == llvm::Triple::ppc64le;
314  bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64;
315  bool IsMIPS32 = TargetTriple.getArch() == llvm::Triple::mips ||
316                  TargetTriple.getArch() == llvm::Triple::mipsel;
317
318  ShadowMapping Mapping;
319
320  if (LongSize == 32) {
321    if (IsAndroid)
322      Mapping.Offset = 0;
323    else if (IsMIPS32)
324      Mapping.Offset = kMIPS32_ShadowOffset32;
325    else if (IsFreeBSD)
326      Mapping.Offset = kFreeBSD_ShadowOffset32;
327    else if (IsIOS)
328      Mapping.Offset = kIOSShadowOffset32;
329    else
330      Mapping.Offset = kDefaultShadowOffset32;
331  } else {  // LongSize == 64
332    if (IsPPC64)
333      Mapping.Offset = kPPC64_ShadowOffset64;
334    else if (IsFreeBSD)
335      Mapping.Offset = kFreeBSD_ShadowOffset64;
336    else if (IsLinux && IsX86_64)
337      Mapping.Offset = kSmallX86_64ShadowOffset;
338    else
339      Mapping.Offset = kDefaultShadowOffset64;
340  }
341
342  Mapping.Scale = kDefaultShadowScale;
343  if (ClMappingScale) {
344    Mapping.Scale = ClMappingScale;
345  }
346
347  // OR-ing shadow offset if more efficient (at least on x86) if the offset
348  // is a power of two, but on ppc64 we have to use add since the shadow
349  // offset is not necessary 1/8-th of the address space.
350  Mapping.OrShadowOffset = !IsPPC64 && !(Mapping.Offset & (Mapping.Offset - 1));
351
352  return Mapping;
353}
354
355static size_t RedzoneSizeForScale(int MappingScale) {
356  // Redzone used for stack and globals is at least 32 bytes.
357  // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
358  return std::max(32U, 1U << MappingScale);
359}
360
361/// AddressSanitizer: instrument the code in module to find memory bugs.
362struct AddressSanitizer : public FunctionPass {
363  AddressSanitizer() : FunctionPass(ID) {}
364  const char *getPassName() const override {
365    return "AddressSanitizerFunctionPass";
366  }
367  void instrumentMop(Instruction *I, bool UseCalls);
368  void instrumentPointerComparisonOrSubtraction(Instruction *I);
369  void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
370                         Value *Addr, uint32_t TypeSize, bool IsWrite,
371                         Value *SizeArgument, bool UseCalls);
372  Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
373                           Value *ShadowValue, uint32_t TypeSize);
374  Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
375                                 bool IsWrite, size_t AccessSizeIndex,
376                                 Value *SizeArgument);
377  void instrumentMemIntrinsic(MemIntrinsic *MI);
378  Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
379  bool runOnFunction(Function &F) override;
380  bool maybeInsertAsanInitAtFunctionEntry(Function &F);
381  bool doInitialization(Module &M) override;
382  static char ID;  // Pass identification, replacement for typeid
383
384 private:
385  void initializeCallbacks(Module &M);
386
387  bool LooksLikeCodeInBug11395(Instruction *I);
388  bool GlobalIsLinkerInitialized(GlobalVariable *G);
389  bool InjectCoverage(Function &F, const ArrayRef<BasicBlock*> AllBlocks);
390  void InjectCoverageAtBlock(Function &F, BasicBlock &BB);
391
392  LLVMContext *C;
393  const DataLayout *DL;
394  int LongSize;
395  Type *IntptrTy;
396  ShadowMapping Mapping;
397  Function *AsanCtorFunction;
398  Function *AsanInitFunction;
399  Function *AsanHandleNoReturnFunc;
400  Function *AsanCovFunction;
401  Function *AsanPtrCmpFunction, *AsanPtrSubFunction;
402  // This array is indexed by AccessIsWrite and log2(AccessSize).
403  Function *AsanErrorCallback[2][kNumberOfAccessSizes];
404  Function *AsanMemoryAccessCallback[2][kNumberOfAccessSizes];
405  // This array is indexed by AccessIsWrite.
406  Function *AsanErrorCallbackSized[2],
407           *AsanMemoryAccessCallbackSized[2];
408  Function *AsanMemmove, *AsanMemcpy, *AsanMemset;
409  InlineAsm *EmptyAsm;
410  GlobalsMetadata GlobalsMD;
411
412  friend struct FunctionStackPoisoner;
413};
414
415class AddressSanitizerModule : public ModulePass {
416 public:
417  AddressSanitizerModule() : ModulePass(ID) {}
418  bool runOnModule(Module &M) override;
419  static char ID;  // Pass identification, replacement for typeid
420  const char *getPassName() const override {
421    return "AddressSanitizerModule";
422  }
423
424 private:
425  void initializeCallbacks(Module &M);
426
427  bool InstrumentGlobals(IRBuilder<> &IRB, Module &M);
428  bool ShouldInstrumentGlobal(GlobalVariable *G);
429  void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
430  void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
431  size_t MinRedzoneSizeForGlobal() const {
432    return RedzoneSizeForScale(Mapping.Scale);
433  }
434
435  GlobalsMetadata GlobalsMD;
436  Type *IntptrTy;
437  LLVMContext *C;
438  const DataLayout *DL;
439  ShadowMapping Mapping;
440  Function *AsanPoisonGlobals;
441  Function *AsanUnpoisonGlobals;
442  Function *AsanRegisterGlobals;
443  Function *AsanUnregisterGlobals;
444  Function *AsanCovModuleInit;
445};
446
447// Stack poisoning does not play well with exception handling.
448// When an exception is thrown, we essentially bypass the code
449// that unpoisones the stack. This is why the run-time library has
450// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
451// stack in the interceptor. This however does not work inside the
452// actual function which catches the exception. Most likely because the
453// compiler hoists the load of the shadow value somewhere too high.
454// This causes asan to report a non-existing bug on 453.povray.
455// It sounds like an LLVM bug.
456struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
457  Function &F;
458  AddressSanitizer &ASan;
459  DIBuilder DIB;
460  LLVMContext *C;
461  Type *IntptrTy;
462  Type *IntptrPtrTy;
463  ShadowMapping Mapping;
464
465  SmallVector<AllocaInst*, 16> AllocaVec;
466  SmallVector<Instruction*, 8> RetVec;
467  unsigned StackAlignment;
468
469  Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
470           *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
471  Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
472
473  // Stores a place and arguments of poisoning/unpoisoning call for alloca.
474  struct AllocaPoisonCall {
475    IntrinsicInst *InsBefore;
476    AllocaInst *AI;
477    uint64_t Size;
478    bool DoPoison;
479  };
480  SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec;
481
482  // Maps Value to an AllocaInst from which the Value is originated.
483  typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy;
484  AllocaForValueMapTy AllocaForValue;
485
486  FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
487      : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C),
488        IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)),
489        Mapping(ASan.Mapping),
490        StackAlignment(1 << Mapping.Scale) {}
491
492  bool runOnFunction() {
493    if (!ClStack) return false;
494    // Collect alloca, ret, lifetime instructions etc.
495    for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
496      visit(*BB);
497
498    if (AllocaVec.empty()) return false;
499
500    initializeCallbacks(*F.getParent());
501
502    poisonStack();
503
504    if (ClDebugStack) {
505      DEBUG(dbgs() << F);
506    }
507    return true;
508  }
509
510  // Finds all static Alloca instructions and puts
511  // poisoned red zones around all of them.
512  // Then unpoison everything back before the function returns.
513  void poisonStack();
514
515  // ----------------------- Visitors.
516  /// \brief Collect all Ret instructions.
517  void visitReturnInst(ReturnInst &RI) {
518    RetVec.push_back(&RI);
519  }
520
521  /// \brief Collect Alloca instructions we want (and can) handle.
522  void visitAllocaInst(AllocaInst &AI) {
523    if (!isInterestingAlloca(AI)) return;
524
525    StackAlignment = std::max(StackAlignment, AI.getAlignment());
526    AllocaVec.push_back(&AI);
527  }
528
529  /// \brief Collect lifetime intrinsic calls to check for use-after-scope
530  /// errors.
531  void visitIntrinsicInst(IntrinsicInst &II) {
532    if (!ClCheckLifetime) return;
533    Intrinsic::ID ID = II.getIntrinsicID();
534    if (ID != Intrinsic::lifetime_start &&
535        ID != Intrinsic::lifetime_end)
536      return;
537    // Found lifetime intrinsic, add ASan instrumentation if necessary.
538    ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
539    // If size argument is undefined, don't do anything.
540    if (Size->isMinusOne()) return;
541    // Check that size doesn't saturate uint64_t and can
542    // be stored in IntptrTy.
543    const uint64_t SizeValue = Size->getValue().getLimitedValue();
544    if (SizeValue == ~0ULL ||
545        !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
546      return;
547    // Find alloca instruction that corresponds to llvm.lifetime argument.
548    AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
549    if (!AI) return;
550    bool DoPoison = (ID == Intrinsic::lifetime_end);
551    AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison};
552    AllocaPoisonCallVec.push_back(APC);
553  }
554
555  // ---------------------- Helpers.
556  void initializeCallbacks(Module &M);
557
558  // Check if we want (and can) handle this alloca.
559  bool isInterestingAlloca(AllocaInst &AI) const {
560    return (!AI.isArrayAllocation() && AI.isStaticAlloca() &&
561            AI.getAllocatedType()->isSized() &&
562            // alloca() may be called with 0 size, ignore it.
563            getAllocaSizeInBytes(&AI) > 0);
564  }
565
566  uint64_t getAllocaSizeInBytes(AllocaInst *AI) const {
567    Type *Ty = AI->getAllocatedType();
568    uint64_t SizeInBytes = ASan.DL->getTypeAllocSize(Ty);
569    return SizeInBytes;
570  }
571  /// Finds alloca where the value comes from.
572  AllocaInst *findAllocaForValue(Value *V);
573  void poisonRedZones(const ArrayRef<uint8_t> ShadowBytes, IRBuilder<> &IRB,
574                      Value *ShadowBase, bool DoPoison);
575  void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison);
576
577  void SetShadowToStackAfterReturnInlined(IRBuilder<> &IRB, Value *ShadowBase,
578                                          int Size);
579};
580
581}  // namespace
582
583char AddressSanitizer::ID = 0;
584INITIALIZE_PASS(AddressSanitizer, "asan",
585    "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
586    false, false)
587FunctionPass *llvm::createAddressSanitizerFunctionPass() {
588  return new AddressSanitizer();
589}
590
591char AddressSanitizerModule::ID = 0;
592INITIALIZE_PASS(AddressSanitizerModule, "asan-module",
593    "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
594    "ModulePass", false, false)
595ModulePass *llvm::createAddressSanitizerModulePass() {
596  return new AddressSanitizerModule();
597}
598
599static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
600  size_t Res = countTrailingZeros(TypeSize / 8);
601  assert(Res < kNumberOfAccessSizes);
602  return Res;
603}
604
605// \brief Create a constant for Str so that we can pass it to the run-time lib.
606static GlobalVariable *createPrivateGlobalForString(
607    Module &M, StringRef Str, bool AllowMerging) {
608  Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
609  // We use private linkage for module-local strings. If they can be merged
610  // with another one, we set the unnamed_addr attribute.
611  GlobalVariable *GV =
612      new GlobalVariable(M, StrConst->getType(), true,
613                         GlobalValue::PrivateLinkage, StrConst, kAsanGenPrefix);
614  if (AllowMerging)
615    GV->setUnnamedAddr(true);
616  GV->setAlignment(1);  // Strings may not be merged w/o setting align 1.
617  return GV;
618}
619
620static bool GlobalWasGeneratedByAsan(GlobalVariable *G) {
621  return G->getName().find(kAsanGenPrefix) == 0;
622}
623
624Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
625  // Shadow >> scale
626  Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
627  if (Mapping.Offset == 0)
628    return Shadow;
629  // (Shadow >> scale) | offset
630  if (Mapping.OrShadowOffset)
631    return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
632  else
633    return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
634}
635
636// Instrument memset/memmove/memcpy
637void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
638  IRBuilder<> IRB(MI);
639  if (isa<MemTransferInst>(MI)) {
640    IRB.CreateCall3(
641        isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
642        IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
643        IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
644        IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false));
645  } else if (isa<MemSetInst>(MI)) {
646    IRB.CreateCall3(
647        AsanMemset,
648        IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
649        IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
650        IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false));
651  }
652  MI->eraseFromParent();
653}
654
655// If I is an interesting memory access, return the PointerOperand
656// and set IsWrite/Alignment. Otherwise return NULL.
657static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite,
658                                        unsigned *Alignment) {
659  if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
660    if (!ClInstrumentReads) return nullptr;
661    *IsWrite = false;
662    *Alignment = LI->getAlignment();
663    return LI->getPointerOperand();
664  }
665  if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
666    if (!ClInstrumentWrites) return nullptr;
667    *IsWrite = true;
668    *Alignment = SI->getAlignment();
669    return SI->getPointerOperand();
670  }
671  if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
672    if (!ClInstrumentAtomics) return nullptr;
673    *IsWrite = true;
674    *Alignment = 0;
675    return RMW->getPointerOperand();
676  }
677  if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
678    if (!ClInstrumentAtomics) return nullptr;
679    *IsWrite = true;
680    *Alignment = 0;
681    return XCHG->getPointerOperand();
682  }
683  return nullptr;
684}
685
686static bool isPointerOperand(Value *V) {
687  return V->getType()->isPointerTy() || isa<PtrToIntInst>(V);
688}
689
690// This is a rough heuristic; it may cause both false positives and
691// false negatives. The proper implementation requires cooperation with
692// the frontend.
693static bool isInterestingPointerComparisonOrSubtraction(Instruction *I) {
694  if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) {
695    if (!Cmp->isRelational())
696      return false;
697  } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
698    if (BO->getOpcode() != Instruction::Sub)
699      return false;
700  } else {
701    return false;
702  }
703  if (!isPointerOperand(I->getOperand(0)) ||
704      !isPointerOperand(I->getOperand(1)))
705      return false;
706  return true;
707}
708
709bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) {
710  // If a global variable does not have dynamic initialization we don't
711  // have to instrument it.  However, if a global does not have initializer
712  // at all, we assume it has dynamic initializer (in other TU).
713  return G->hasInitializer() && !GlobalsMD.isDynInit(G);
714}
715
716void
717AddressSanitizer::instrumentPointerComparisonOrSubtraction(Instruction *I) {
718  IRBuilder<> IRB(I);
719  Function *F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
720  Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
721  for (int i = 0; i < 2; i++) {
722    if (Param[i]->getType()->isPointerTy())
723      Param[i] = IRB.CreatePointerCast(Param[i], IntptrTy);
724  }
725  IRB.CreateCall2(F, Param[0], Param[1]);
726}
727
728void AddressSanitizer::instrumentMop(Instruction *I, bool UseCalls) {
729  bool IsWrite = false;
730  unsigned Alignment = 0;
731  Value *Addr = isInterestingMemoryAccess(I, &IsWrite, &Alignment);
732  assert(Addr);
733  if (ClOpt && ClOptGlobals) {
734    if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) {
735      // If initialization order checking is disabled, a simple access to a
736      // dynamically initialized global is always valid.
737      if (!ClInitializers || GlobalIsLinkerInitialized(G)) {
738        NumOptimizedAccessesToGlobalVar++;
739        return;
740      }
741    }
742    ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr);
743    if (CE && CE->isGEPWithNoNotionalOverIndexing()) {
744      if (GlobalVariable *G = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
745        if (CE->getOperand(1)->isNullValue() && GlobalIsLinkerInitialized(G)) {
746          NumOptimizedAccessesToGlobalArray++;
747          return;
748        }
749      }
750    }
751  }
752
753  Type *OrigPtrTy = Addr->getType();
754  Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
755
756  assert(OrigTy->isSized());
757  uint32_t TypeSize = DL->getTypeStoreSizeInBits(OrigTy);
758
759  assert((TypeSize % 8) == 0);
760
761  if (IsWrite)
762    NumInstrumentedWrites++;
763  else
764    NumInstrumentedReads++;
765
766  unsigned Granularity = 1 << Mapping.Scale;
767  // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check
768  // if the data is properly aligned.
769  if ((TypeSize == 8 || TypeSize == 16 || TypeSize == 32 || TypeSize == 64 ||
770       TypeSize == 128) &&
771      (Alignment >= Granularity || Alignment == 0 || Alignment >= TypeSize / 8))
772    return instrumentAddress(I, I, Addr, TypeSize, IsWrite, nullptr, UseCalls);
773  // Instrument unusual size or unusual alignment.
774  // We can not do it with a single check, so we do 1-byte check for the first
775  // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able
776  // to report the actual access size.
777  IRBuilder<> IRB(I);
778  Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8);
779  Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
780  if (UseCalls) {
781    IRB.CreateCall2(AsanMemoryAccessCallbackSized[IsWrite], AddrLong, Size);
782  } else {
783    Value *LastByte = IRB.CreateIntToPtr(
784        IRB.CreateAdd(AddrLong, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)),
785        OrigPtrTy);
786    instrumentAddress(I, I, Addr, 8, IsWrite, Size, false);
787    instrumentAddress(I, I, LastByte, 8, IsWrite, Size, false);
788  }
789}
790
791// Validate the result of Module::getOrInsertFunction called for an interface
792// function of AddressSanitizer. If the instrumented module defines a function
793// with the same name, their prototypes must match, otherwise
794// getOrInsertFunction returns a bitcast.
795static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
796  if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
797  FuncOrBitcast->dump();
798  report_fatal_error("trying to redefine an AddressSanitizer "
799                     "interface function");
800}
801
802Instruction *AddressSanitizer::generateCrashCode(
803    Instruction *InsertBefore, Value *Addr,
804    bool IsWrite, size_t AccessSizeIndex, Value *SizeArgument) {
805  IRBuilder<> IRB(InsertBefore);
806  CallInst *Call = SizeArgument
807    ? IRB.CreateCall2(AsanErrorCallbackSized[IsWrite], Addr, SizeArgument)
808    : IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex], Addr);
809
810  // We don't do Call->setDoesNotReturn() because the BB already has
811  // UnreachableInst at the end.
812  // This EmptyAsm is required to avoid callback merge.
813  IRB.CreateCall(EmptyAsm);
814  return Call;
815}
816
817Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
818                                            Value *ShadowValue,
819                                            uint32_t TypeSize) {
820  size_t Granularity = 1 << Mapping.Scale;
821  // Addr & (Granularity - 1)
822  Value *LastAccessedByte = IRB.CreateAnd(
823      AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
824  // (Addr & (Granularity - 1)) + size - 1
825  if (TypeSize / 8 > 1)
826    LastAccessedByte = IRB.CreateAdd(
827        LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
828  // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
829  LastAccessedByte = IRB.CreateIntCast(
830      LastAccessedByte, ShadowValue->getType(), false);
831  // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
832  return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
833}
834
835void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
836                                         Instruction *InsertBefore, Value *Addr,
837                                         uint32_t TypeSize, bool IsWrite,
838                                         Value *SizeArgument, bool UseCalls) {
839  IRBuilder<> IRB(InsertBefore);
840  Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
841  size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
842
843  if (UseCalls) {
844    IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][AccessSizeIndex],
845                   AddrLong);
846    return;
847  }
848
849  Type *ShadowTy  = IntegerType::get(
850      *C, std::max(8U, TypeSize >> Mapping.Scale));
851  Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
852  Value *ShadowPtr = memToShadow(AddrLong, IRB);
853  Value *CmpVal = Constant::getNullValue(ShadowTy);
854  Value *ShadowValue = IRB.CreateLoad(
855      IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
856
857  Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
858  size_t Granularity = 1 << Mapping.Scale;
859  TerminatorInst *CrashTerm = nullptr;
860
861  if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
862    TerminatorInst *CheckTerm =
863        SplitBlockAndInsertIfThen(Cmp, InsertBefore, false);
864    assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
865    BasicBlock *NextBB = CheckTerm->getSuccessor(0);
866    IRB.SetInsertPoint(CheckTerm);
867    Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
868    BasicBlock *CrashBlock =
869        BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
870    CrashTerm = new UnreachableInst(*C, CrashBlock);
871    BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
872    ReplaceInstWithInst(CheckTerm, NewTerm);
873  } else {
874    CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, true);
875  }
876
877  Instruction *Crash = generateCrashCode(
878      CrashTerm, AddrLong, IsWrite, AccessSizeIndex, SizeArgument);
879  Crash->setDebugLoc(OrigIns->getDebugLoc());
880}
881
882void AddressSanitizerModule::poisonOneInitializer(Function &GlobalInit,
883                                                  GlobalValue *ModuleName) {
884  // Set up the arguments to our poison/unpoison functions.
885  IRBuilder<> IRB(GlobalInit.begin()->getFirstInsertionPt());
886
887  // Add a call to poison all external globals before the given function starts.
888  Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy);
889  IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr);
890
891  // Add calls to unpoison all globals before each return instruction.
892  for (auto &BB : GlobalInit.getBasicBlockList())
893    if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
894      CallInst::Create(AsanUnpoisonGlobals, "", RI);
895}
896
897void AddressSanitizerModule::createInitializerPoisonCalls(
898    Module &M, GlobalValue *ModuleName) {
899  GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
900
901  ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
902  for (Use &OP : CA->operands()) {
903    if (isa<ConstantAggregateZero>(OP))
904      continue;
905    ConstantStruct *CS = cast<ConstantStruct>(OP);
906
907    // Must have a function or null ptr.
908    // (CS->getOperand(0) is the init priority.)
909    if (Function* F = dyn_cast<Function>(CS->getOperand(1))) {
910      if (F->getName() != kAsanModuleCtorName)
911        poisonOneInitializer(*F, ModuleName);
912    }
913  }
914}
915
916bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
917  Type *Ty = cast<PointerType>(G->getType())->getElementType();
918  DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
919
920  if (GlobalsMD.isBlacklisted(G)) return false;
921  if (GlobalsMD.isSourceLocationGlobal(G)) return false;
922  if (!Ty->isSized()) return false;
923  if (!G->hasInitializer()) return false;
924  if (GlobalWasGeneratedByAsan(G)) return false;  // Our own global.
925  // Touch only those globals that will not be defined in other modules.
926  // Don't handle ODR linkage types and COMDATs since other modules may be built
927  // without ASan.
928  if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
929      G->getLinkage() != GlobalVariable::PrivateLinkage &&
930      G->getLinkage() != GlobalVariable::InternalLinkage)
931    return false;
932  if (G->hasComdat())
933    return false;
934  // Two problems with thread-locals:
935  //   - The address of the main thread's copy can't be computed at link-time.
936  //   - Need to poison all copies, not just the main thread's one.
937  if (G->isThreadLocal())
938    return false;
939  // For now, just ignore this Global if the alignment is large.
940  if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false;
941
942  // Ignore all the globals with the names starting with "\01L_OBJC_".
943  // Many of those are put into the .cstring section. The linker compresses
944  // that section by removing the spare \0s after the string terminator, so
945  // our redzones get broken.
946  if ((G->getName().find("\01L_OBJC_") == 0) ||
947      (G->getName().find("\01l_OBJC_") == 0)) {
948    DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G << "\n");
949    return false;
950  }
951
952  if (G->hasSection()) {
953    StringRef Section(G->getSection());
954    // Ignore the globals from the __OBJC section. The ObjC runtime assumes
955    // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
956    // them.
957    if (Section.startswith("__OBJC,") ||
958        Section.startswith("__DATA, __objc_")) {
959      DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n");
960      return false;
961    }
962    // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
963    // Constant CFString instances are compiled in the following way:
964    //  -- the string buffer is emitted into
965    //     __TEXT,__cstring,cstring_literals
966    //  -- the constant NSConstantString structure referencing that buffer
967    //     is placed into __DATA,__cfstring
968    // Therefore there's no point in placing redzones into __DATA,__cfstring.
969    // Moreover, it causes the linker to crash on OS X 10.7
970    if (Section.startswith("__DATA,__cfstring")) {
971      DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n");
972      return false;
973    }
974    // The linker merges the contents of cstring_literals and removes the
975    // trailing zeroes.
976    if (Section.startswith("__TEXT,__cstring,cstring_literals")) {
977      DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n");
978      return false;
979    }
980
981    // Callbacks put into the CRT initializer/terminator sections
982    // should not be instrumented.
983    // See https://code.google.com/p/address-sanitizer/issues/detail?id=305
984    // and http://msdn.microsoft.com/en-US/en-en/library/bb918180(v=vs.120).aspx
985    if (Section.startswith(".CRT")) {
986      DEBUG(dbgs() << "Ignoring a global initializer callback: " << *G << "\n");
987      return false;
988    }
989
990    // Globals from llvm.metadata aren't emitted, do not instrument them.
991    if (Section == "llvm.metadata") return false;
992  }
993
994  return true;
995}
996
997void AddressSanitizerModule::initializeCallbacks(Module &M) {
998  IRBuilder<> IRB(*C);
999  // Declare our poisoning and unpoisoning functions.
1000  AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
1001      kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, NULL));
1002  AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
1003  AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
1004      kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL));
1005  AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
1006  // Declare functions that register/unregister globals.
1007  AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
1008      kAsanRegisterGlobalsName, IRB.getVoidTy(),
1009      IntptrTy, IntptrTy, NULL));
1010  AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
1011  AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
1012      kAsanUnregisterGlobalsName,
1013      IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1014  AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
1015  AsanCovModuleInit = checkInterfaceFunction(M.getOrInsertFunction(
1016      kAsanCovModuleInitName,
1017      IRB.getVoidTy(), IntptrTy, NULL));
1018  AsanCovModuleInit->setLinkage(Function::ExternalLinkage);
1019}
1020
1021// This function replaces all global variables with new variables that have
1022// trailing redzones. It also creates a function that poisons
1023// redzones and inserts this function into llvm.global_ctors.
1024bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
1025  GlobalsMD.init(M);
1026
1027  SmallVector<GlobalVariable *, 16> GlobalsToChange;
1028
1029  for (auto &G : M.globals()) {
1030    if (ShouldInstrumentGlobal(&G))
1031      GlobalsToChange.push_back(&G);
1032  }
1033
1034  size_t n = GlobalsToChange.size();
1035  if (n == 0) return false;
1036
1037  // A global is described by a structure
1038  //   size_t beg;
1039  //   size_t size;
1040  //   size_t size_with_redzone;
1041  //   const char *name;
1042  //   const char *module_name;
1043  //   size_t has_dynamic_init;
1044  //   void *source_location;
1045  // We initialize an array of such structures and pass it to a run-time call.
1046  StructType *GlobalStructTy =
1047      StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy,
1048                      IntptrTy, IntptrTy, NULL);
1049  SmallVector<Constant *, 16> Initializers(n);
1050
1051  bool HasDynamicallyInitializedGlobals = false;
1052
1053  // We shouldn't merge same module names, as this string serves as unique
1054  // module ID in runtime.
1055  GlobalVariable *ModuleName = createPrivateGlobalForString(
1056      M, M.getModuleIdentifier(), /*AllowMerging*/false);
1057
1058  for (size_t i = 0; i < n; i++) {
1059    static const uint64_t kMaxGlobalRedzone = 1 << 18;
1060    GlobalVariable *G = GlobalsToChange[i];
1061    PointerType *PtrTy = cast<PointerType>(G->getType());
1062    Type *Ty = PtrTy->getElementType();
1063    uint64_t SizeInBytes = DL->getTypeAllocSize(Ty);
1064    uint64_t MinRZ = MinRedzoneSizeForGlobal();
1065    // MinRZ <= RZ <= kMaxGlobalRedzone
1066    // and trying to make RZ to be ~ 1/4 of SizeInBytes.
1067    uint64_t RZ = std::max(MinRZ,
1068                         std::min(kMaxGlobalRedzone,
1069                                  (SizeInBytes / MinRZ / 4) * MinRZ));
1070    uint64_t RightRedzoneSize = RZ;
1071    // Round up to MinRZ
1072    if (SizeInBytes % MinRZ)
1073      RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ);
1074    assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0);
1075    Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
1076
1077    StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
1078    Constant *NewInitializer = ConstantStruct::get(
1079        NewTy, G->getInitializer(),
1080        Constant::getNullValue(RightRedZoneTy), NULL);
1081
1082    GlobalVariable *Name =
1083        createPrivateGlobalForString(M, G->getName(), /*AllowMerging*/true);
1084
1085    // Create a new global variable with enough space for a redzone.
1086    GlobalValue::LinkageTypes Linkage = G->getLinkage();
1087    if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage)
1088      Linkage = GlobalValue::InternalLinkage;
1089    GlobalVariable *NewGlobal = new GlobalVariable(
1090        M, NewTy, G->isConstant(), Linkage,
1091        NewInitializer, "", G, G->getThreadLocalMode());
1092    NewGlobal->copyAttributesFrom(G);
1093    NewGlobal->setAlignment(MinRZ);
1094
1095    Value *Indices2[2];
1096    Indices2[0] = IRB.getInt32(0);
1097    Indices2[1] = IRB.getInt32(0);
1098
1099    G->replaceAllUsesWith(
1100        ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
1101    NewGlobal->takeName(G);
1102    G->eraseFromParent();
1103
1104    bool GlobalHasDynamicInitializer = GlobalsMD.isDynInit(G);
1105    GlobalVariable *SourceLoc = GlobalsMD.getSourceLocation(G);
1106
1107    Initializers[i] = ConstantStruct::get(
1108        GlobalStructTy, ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
1109        ConstantInt::get(IntptrTy, SizeInBytes),
1110        ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
1111        ConstantExpr::getPointerCast(Name, IntptrTy),
1112        ConstantExpr::getPointerCast(ModuleName, IntptrTy),
1113        ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer),
1114        SourceLoc ? ConstantExpr::getPointerCast(SourceLoc, IntptrTy)
1115                  : ConstantInt::get(IntptrTy, 0),
1116        NULL);
1117
1118    if (ClInitializers && GlobalHasDynamicInitializer)
1119      HasDynamicallyInitializedGlobals = true;
1120
1121    DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
1122  }
1123
1124  ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
1125  GlobalVariable *AllGlobals = new GlobalVariable(
1126      M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
1127      ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
1128
1129  // Create calls for poisoning before initializers run and unpoisoning after.
1130  if (HasDynamicallyInitializedGlobals)
1131    createInitializerPoisonCalls(M, ModuleName);
1132  IRB.CreateCall2(AsanRegisterGlobals,
1133                  IRB.CreatePointerCast(AllGlobals, IntptrTy),
1134                  ConstantInt::get(IntptrTy, n));
1135
1136  // We also need to unregister globals at the end, e.g. when a shared library
1137  // gets closed.
1138  Function *AsanDtorFunction = Function::Create(
1139      FunctionType::get(Type::getVoidTy(*C), false),
1140      GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
1141  BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
1142  IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
1143  IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
1144                       IRB.CreatePointerCast(AllGlobals, IntptrTy),
1145                       ConstantInt::get(IntptrTy, n));
1146  appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority);
1147
1148  DEBUG(dbgs() << M);
1149  return true;
1150}
1151
1152bool AddressSanitizerModule::runOnModule(Module &M) {
1153  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
1154  if (!DLP)
1155    return false;
1156  DL = &DLP->getDataLayout();
1157  C = &(M.getContext());
1158  int LongSize = DL->getPointerSizeInBits();
1159  IntptrTy = Type::getIntNTy(*C, LongSize);
1160  Mapping = getShadowMapping(M, LongSize);
1161  initializeCallbacks(M);
1162
1163  bool Changed = false;
1164
1165  Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
1166  assert(CtorFunc);
1167  IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
1168
1169  if (ClCoverage > 0) {
1170    Function *CovFunc = M.getFunction(kAsanCovName);
1171    int nCov = CovFunc ? CovFunc->getNumUses() : 0;
1172    IRB.CreateCall(AsanCovModuleInit, ConstantInt::get(IntptrTy, nCov));
1173    Changed = true;
1174  }
1175
1176  if (ClGlobals)
1177    Changed |= InstrumentGlobals(IRB, M);
1178
1179  return Changed;
1180}
1181
1182void AddressSanitizer::initializeCallbacks(Module &M) {
1183  IRBuilder<> IRB(*C);
1184  // Create __asan_report* callbacks.
1185  for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
1186    for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
1187         AccessSizeIndex++) {
1188      // IsWrite and TypeSize are encoded in the function name.
1189      std::string Suffix =
1190          (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
1191      AsanErrorCallback[AccessIsWrite][AccessSizeIndex] =
1192          checkInterfaceFunction(
1193              M.getOrInsertFunction(kAsanReportErrorTemplate + Suffix,
1194                                    IRB.getVoidTy(), IntptrTy, NULL));
1195      AsanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] =
1196          checkInterfaceFunction(
1197              M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + Suffix,
1198                                    IRB.getVoidTy(), IntptrTy, NULL));
1199    }
1200  }
1201  AsanErrorCallbackSized[0] = checkInterfaceFunction(M.getOrInsertFunction(
1202              kAsanReportLoadN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1203  AsanErrorCallbackSized[1] = checkInterfaceFunction(M.getOrInsertFunction(
1204              kAsanReportStoreN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1205
1206  AsanMemoryAccessCallbackSized[0] = checkInterfaceFunction(
1207      M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "loadN",
1208                            IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1209  AsanMemoryAccessCallbackSized[1] = checkInterfaceFunction(
1210      M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "storeN",
1211                            IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1212
1213  AsanMemmove = checkInterfaceFunction(M.getOrInsertFunction(
1214      ClMemoryAccessCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
1215      IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, NULL));
1216  AsanMemcpy = checkInterfaceFunction(M.getOrInsertFunction(
1217      ClMemoryAccessCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
1218      IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, NULL));
1219  AsanMemset = checkInterfaceFunction(M.getOrInsertFunction(
1220      ClMemoryAccessCallbackPrefix + "memset", IRB.getInt8PtrTy(),
1221      IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy, NULL));
1222
1223  AsanHandleNoReturnFunc = checkInterfaceFunction(
1224      M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy(), NULL));
1225  AsanCovFunction = checkInterfaceFunction(M.getOrInsertFunction(
1226      kAsanCovName, IRB.getVoidTy(), NULL));
1227  AsanPtrCmpFunction = checkInterfaceFunction(M.getOrInsertFunction(
1228      kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1229  AsanPtrSubFunction = checkInterfaceFunction(M.getOrInsertFunction(
1230      kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1231  // We insert an empty inline asm after __asan_report* to avoid callback merge.
1232  EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
1233                            StringRef(""), StringRef(""),
1234                            /*hasSideEffects=*/true);
1235}
1236
1237// virtual
1238bool AddressSanitizer::doInitialization(Module &M) {
1239  // Initialize the private fields. No one has accessed them before.
1240  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
1241  if (!DLP)
1242    report_fatal_error("data layout missing");
1243  DL = &DLP->getDataLayout();
1244
1245  GlobalsMD.init(M);
1246
1247  C = &(M.getContext());
1248  LongSize = DL->getPointerSizeInBits();
1249  IntptrTy = Type::getIntNTy(*C, LongSize);
1250
1251  AsanCtorFunction = Function::Create(
1252      FunctionType::get(Type::getVoidTy(*C), false),
1253      GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
1254  BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
1255  // call __asan_init in the module ctor.
1256  IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB));
1257  AsanInitFunction = checkInterfaceFunction(
1258      M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
1259  AsanInitFunction->setLinkage(Function::ExternalLinkage);
1260  IRB.CreateCall(AsanInitFunction);
1261
1262  Mapping = getShadowMapping(M, LongSize);
1263
1264  appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority);
1265  return true;
1266}
1267
1268bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
1269  // For each NSObject descendant having a +load method, this method is invoked
1270  // by the ObjC runtime before any of the static constructors is called.
1271  // Therefore we need to instrument such methods with a call to __asan_init
1272  // at the beginning in order to initialize our runtime before any access to
1273  // the shadow memory.
1274  // We cannot just ignore these methods, because they may call other
1275  // instrumented functions.
1276  if (F.getName().find(" load]") != std::string::npos) {
1277    IRBuilder<> IRB(F.begin()->begin());
1278    IRB.CreateCall(AsanInitFunction);
1279    return true;
1280  }
1281  return false;
1282}
1283
1284void AddressSanitizer::InjectCoverageAtBlock(Function &F, BasicBlock &BB) {
1285  BasicBlock::iterator IP = BB.getFirstInsertionPt(), BE = BB.end();
1286  // Skip static allocas at the top of the entry block so they don't become
1287  // dynamic when we split the block.  If we used our optimized stack layout,
1288  // then there will only be one alloca and it will come first.
1289  for (; IP != BE; ++IP) {
1290    AllocaInst *AI = dyn_cast<AllocaInst>(IP);
1291    if (!AI || !AI->isStaticAlloca())
1292      break;
1293  }
1294
1295  DebugLoc EntryLoc = IP->getDebugLoc().getFnDebugLoc(*C);
1296  IRBuilder<> IRB(IP);
1297  IRB.SetCurrentDebugLocation(EntryLoc);
1298  Type *Int8Ty = IRB.getInt8Ty();
1299  GlobalVariable *Guard = new GlobalVariable(
1300      *F.getParent(), Int8Ty, false, GlobalValue::PrivateLinkage,
1301      Constant::getNullValue(Int8Ty), "__asan_gen_cov_" + F.getName());
1302  LoadInst *Load = IRB.CreateLoad(Guard);
1303  Load->setAtomic(Monotonic);
1304  Load->setAlignment(1);
1305  Value *Cmp = IRB.CreateICmpEQ(Constant::getNullValue(Int8Ty), Load);
1306  Instruction *Ins = SplitBlockAndInsertIfThen(
1307      Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
1308  IRB.SetInsertPoint(Ins);
1309  IRB.SetCurrentDebugLocation(EntryLoc);
1310  // We pass &F to __sanitizer_cov. We could avoid this and rely on
1311  // GET_CALLER_PC, but having the PC of the first instruction is just nice.
1312  IRB.CreateCall(AsanCovFunction);
1313  StoreInst *Store = IRB.CreateStore(ConstantInt::get(Int8Ty, 1), Guard);
1314  Store->setAtomic(Monotonic);
1315  Store->setAlignment(1);
1316}
1317
1318// Poor man's coverage that works with ASan.
1319// We create a Guard boolean variable with the same linkage
1320// as the function and inject this code into the entry block (-asan-coverage=1)
1321// or all blocks (-asan-coverage=2):
1322// if (*Guard) {
1323//    __sanitizer_cov(&F);
1324//    *Guard = 1;
1325// }
1326// The accesses to Guard are atomic. The rest of the logic is
1327// in __sanitizer_cov (it's fine to call it more than once).
1328//
1329// This coverage implementation provides very limited data:
1330// it only tells if a given function (block) was ever executed.
1331// No counters, no per-edge data.
1332// But for many use cases this is what we need and the added slowdown
1333// is negligible. This simple implementation will probably be obsoleted
1334// by the upcoming Clang-based coverage implementation.
1335// By having it here and now we hope to
1336//  a) get the functionality to users earlier and
1337//  b) collect usage statistics to help improve Clang coverage design.
1338bool AddressSanitizer::InjectCoverage(Function &F,
1339                                      const ArrayRef<BasicBlock *> AllBlocks) {
1340  if (!ClCoverage) return false;
1341
1342  if (ClCoverage == 1 ||
1343      (unsigned)ClCoverageBlockThreshold < AllBlocks.size()) {
1344    InjectCoverageAtBlock(F, F.getEntryBlock());
1345  } else {
1346    for (auto BB : AllBlocks)
1347      InjectCoverageAtBlock(F, *BB);
1348  }
1349  return true;
1350}
1351
1352bool AddressSanitizer::runOnFunction(Function &F) {
1353  if (&F == AsanCtorFunction) return false;
1354  if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
1355  DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
1356  initializeCallbacks(*F.getParent());
1357
1358  // If needed, insert __asan_init before checking for SanitizeAddress attr.
1359  maybeInsertAsanInitAtFunctionEntry(F);
1360
1361  if (!F.hasFnAttribute(Attribute::SanitizeAddress))
1362    return false;
1363
1364  if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
1365    return false;
1366
1367  // We want to instrument every address only once per basic block (unless there
1368  // are calls between uses).
1369  SmallSet<Value*, 16> TempsToInstrument;
1370  SmallVector<Instruction*, 16> ToInstrument;
1371  SmallVector<Instruction*, 8> NoReturnCalls;
1372  SmallVector<BasicBlock*, 16> AllBlocks;
1373  SmallVector<Instruction*, 16> PointerComparisonsOrSubtracts;
1374  int NumAllocas = 0;
1375  bool IsWrite;
1376  unsigned Alignment;
1377
1378  // Fill the set of memory operations to instrument.
1379  for (auto &BB : F) {
1380    AllBlocks.push_back(&BB);
1381    TempsToInstrument.clear();
1382    int NumInsnsPerBB = 0;
1383    for (auto &Inst : BB) {
1384      if (LooksLikeCodeInBug11395(&Inst)) return false;
1385      if (Value *Addr =
1386              isInterestingMemoryAccess(&Inst, &IsWrite, &Alignment)) {
1387        if (ClOpt && ClOptSameTemp) {
1388          if (!TempsToInstrument.insert(Addr))
1389            continue;  // We've seen this temp in the current BB.
1390        }
1391      } else if (ClInvalidPointerPairs &&
1392                 isInterestingPointerComparisonOrSubtraction(&Inst)) {
1393        PointerComparisonsOrSubtracts.push_back(&Inst);
1394        continue;
1395      } else if (isa<MemIntrinsic>(Inst)) {
1396        // ok, take it.
1397      } else {
1398        if (isa<AllocaInst>(Inst))
1399          NumAllocas++;
1400        CallSite CS(&Inst);
1401        if (CS) {
1402          // A call inside BB.
1403          TempsToInstrument.clear();
1404          if (CS.doesNotReturn())
1405            NoReturnCalls.push_back(CS.getInstruction());
1406        }
1407        continue;
1408      }
1409      ToInstrument.push_back(&Inst);
1410      NumInsnsPerBB++;
1411      if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
1412        break;
1413    }
1414  }
1415
1416  Function *UninstrumentedDuplicate = nullptr;
1417  bool LikelyToInstrument =
1418      !NoReturnCalls.empty() || !ToInstrument.empty() || (NumAllocas > 0);
1419  if (ClKeepUninstrumented && LikelyToInstrument) {
1420    ValueToValueMapTy VMap;
1421    UninstrumentedDuplicate = CloneFunction(&F, VMap, false);
1422    UninstrumentedDuplicate->removeFnAttr(Attribute::SanitizeAddress);
1423    UninstrumentedDuplicate->setName("NOASAN_" + F.getName());
1424    F.getParent()->getFunctionList().push_back(UninstrumentedDuplicate);
1425  }
1426
1427  bool UseCalls = false;
1428  if (ClInstrumentationWithCallsThreshold >= 0 &&
1429      ToInstrument.size() > (unsigned)ClInstrumentationWithCallsThreshold)
1430    UseCalls = true;
1431
1432  // Instrument.
1433  int NumInstrumented = 0;
1434  for (auto Inst : ToInstrument) {
1435    if (ClDebugMin < 0 || ClDebugMax < 0 ||
1436        (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
1437      if (isInterestingMemoryAccess(Inst, &IsWrite, &Alignment))
1438        instrumentMop(Inst, UseCalls);
1439      else
1440        instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
1441    }
1442    NumInstrumented++;
1443  }
1444
1445  FunctionStackPoisoner FSP(F, *this);
1446  bool ChangedStack = FSP.runOnFunction();
1447
1448  // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
1449  // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
1450  for (auto CI : NoReturnCalls) {
1451    IRBuilder<> IRB(CI);
1452    IRB.CreateCall(AsanHandleNoReturnFunc);
1453  }
1454
1455  for (auto Inst : PointerComparisonsOrSubtracts) {
1456    instrumentPointerComparisonOrSubtraction(Inst);
1457    NumInstrumented++;
1458  }
1459
1460  bool res = NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
1461
1462  if (InjectCoverage(F, AllBlocks))
1463    res = true;
1464
1465  DEBUG(dbgs() << "ASAN done instrumenting: " << res << " " << F << "\n");
1466
1467  if (ClKeepUninstrumented) {
1468    if (!res) {
1469      // No instrumentation is done, no need for the duplicate.
1470      if (UninstrumentedDuplicate)
1471        UninstrumentedDuplicate->eraseFromParent();
1472    } else {
1473      // The function was instrumented. We must have the duplicate.
1474      assert(UninstrumentedDuplicate);
1475      UninstrumentedDuplicate->setSection("NOASAN");
1476      assert(!F.hasSection());
1477      F.setSection("ASAN");
1478    }
1479  }
1480
1481  return res;
1482}
1483
1484// Workaround for bug 11395: we don't want to instrument stack in functions
1485// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
1486// FIXME: remove once the bug 11395 is fixed.
1487bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
1488  if (LongSize != 32) return false;
1489  CallInst *CI = dyn_cast<CallInst>(I);
1490  if (!CI || !CI->isInlineAsm()) return false;
1491  if (CI->getNumArgOperands() <= 5) return false;
1492  // We have inline assembly with quite a few arguments.
1493  return true;
1494}
1495
1496void FunctionStackPoisoner::initializeCallbacks(Module &M) {
1497  IRBuilder<> IRB(*C);
1498  for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
1499    std::string Suffix = itostr(i);
1500    AsanStackMallocFunc[i] = checkInterfaceFunction(
1501        M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
1502                              IntptrTy, IntptrTy, NULL));
1503    AsanStackFreeFunc[i] = checkInterfaceFunction(M.getOrInsertFunction(
1504        kAsanStackFreeNameTemplate + Suffix, IRB.getVoidTy(), IntptrTy,
1505        IntptrTy, IntptrTy, NULL));
1506  }
1507  AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1508      kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1509  AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
1510      kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
1511}
1512
1513void
1514FunctionStackPoisoner::poisonRedZones(const ArrayRef<uint8_t> ShadowBytes,
1515                                      IRBuilder<> &IRB, Value *ShadowBase,
1516                                      bool DoPoison) {
1517  size_t n = ShadowBytes.size();
1518  size_t i = 0;
1519  // We need to (un)poison n bytes of stack shadow. Poison as many as we can
1520  // using 64-bit stores (if we are on 64-bit arch), then poison the rest
1521  // with 32-bit stores, then with 16-byte stores, then with 8-byte stores.
1522  for (size_t LargeStoreSizeInBytes = ASan.LongSize / 8;
1523       LargeStoreSizeInBytes != 0; LargeStoreSizeInBytes /= 2) {
1524    for (; i + LargeStoreSizeInBytes - 1 < n; i += LargeStoreSizeInBytes) {
1525      uint64_t Val = 0;
1526      for (size_t j = 0; j < LargeStoreSizeInBytes; j++) {
1527        if (ASan.DL->isLittleEndian())
1528          Val |= (uint64_t)ShadowBytes[i + j] << (8 * j);
1529        else
1530          Val = (Val << 8) | ShadowBytes[i + j];
1531      }
1532      if (!Val) continue;
1533      Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1534      Type *StoreTy = Type::getIntNTy(*C, LargeStoreSizeInBytes * 8);
1535      Value *Poison = ConstantInt::get(StoreTy, DoPoison ? Val : 0);
1536      IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, StoreTy->getPointerTo()));
1537    }
1538  }
1539}
1540
1541// Fake stack allocator (asan_fake_stack.h) has 11 size classes
1542// for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass
1543static int StackMallocSizeClass(uint64_t LocalStackSize) {
1544  assert(LocalStackSize <= kMaxStackMallocSize);
1545  uint64_t MaxSize = kMinStackMallocSize;
1546  for (int i = 0; ; i++, MaxSize *= 2)
1547    if (LocalStackSize <= MaxSize)
1548      return i;
1549  llvm_unreachable("impossible LocalStackSize");
1550}
1551
1552// Set Size bytes starting from ShadowBase to kAsanStackAfterReturnMagic.
1553// We can not use MemSet intrinsic because it may end up calling the actual
1554// memset. Size is a multiple of 8.
1555// Currently this generates 8-byte stores on x86_64; it may be better to
1556// generate wider stores.
1557void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined(
1558    IRBuilder<> &IRB, Value *ShadowBase, int Size) {
1559  assert(!(Size % 8));
1560  assert(kAsanStackAfterReturnMagic == 0xf5);
1561  for (int i = 0; i < Size; i += 8) {
1562    Value *p = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1563    IRB.CreateStore(ConstantInt::get(IRB.getInt64Ty(), 0xf5f5f5f5f5f5f5f5ULL),
1564                    IRB.CreateIntToPtr(p, IRB.getInt64Ty()->getPointerTo()));
1565  }
1566}
1567
1568static DebugLoc getFunctionEntryDebugLocation(Function &F) {
1569  for (const auto &Inst : F.getEntryBlock())
1570    if (!isa<AllocaInst>(Inst))
1571      return Inst.getDebugLoc();
1572  return DebugLoc();
1573}
1574
1575void FunctionStackPoisoner::poisonStack() {
1576  int StackMallocIdx = -1;
1577  DebugLoc EntryDebugLocation = getFunctionEntryDebugLocation(F);
1578
1579  assert(AllocaVec.size() > 0);
1580  Instruction *InsBefore = AllocaVec[0];
1581  IRBuilder<> IRB(InsBefore);
1582  IRB.SetCurrentDebugLocation(EntryDebugLocation);
1583
1584  SmallVector<ASanStackVariableDescription, 16> SVD;
1585  SVD.reserve(AllocaVec.size());
1586  for (AllocaInst *AI : AllocaVec) {
1587    ASanStackVariableDescription D = { AI->getName().data(),
1588                                   getAllocaSizeInBytes(AI),
1589                                   AI->getAlignment(), AI, 0};
1590    SVD.push_back(D);
1591  }
1592  // Minimal header size (left redzone) is 4 pointers,
1593  // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms.
1594  size_t MinHeaderSize = ASan.LongSize / 2;
1595  ASanStackFrameLayout L;
1596  ComputeASanStackFrameLayout(SVD, 1UL << Mapping.Scale, MinHeaderSize, &L);
1597  DEBUG(dbgs() << L.DescriptionString << " --- " << L.FrameSize << "\n");
1598  uint64_t LocalStackSize = L.FrameSize;
1599  bool DoStackMalloc =
1600      ClUseAfterReturn && LocalStackSize <= kMaxStackMallocSize;
1601
1602  Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
1603  AllocaInst *MyAlloca =
1604      new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
1605  MyAlloca->setDebugLoc(EntryDebugLocation);
1606  assert((ClRealignStack & (ClRealignStack - 1)) == 0);
1607  size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
1608  MyAlloca->setAlignment(FrameAlignment);
1609  assert(MyAlloca->isStaticAlloca());
1610  Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
1611  Value *LocalStackBase = OrigStackBase;
1612
1613  if (DoStackMalloc) {
1614    // LocalStackBase = OrigStackBase
1615    // if (__asan_option_detect_stack_use_after_return)
1616    //   LocalStackBase = __asan_stack_malloc_N(LocalStackBase, OrigStackBase);
1617    StackMallocIdx = StackMallocSizeClass(LocalStackSize);
1618    assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
1619    Constant *OptionDetectUAR = F.getParent()->getOrInsertGlobal(
1620        kAsanOptionDetectUAR, IRB.getInt32Ty());
1621    Value *Cmp = IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUAR),
1622                                  Constant::getNullValue(IRB.getInt32Ty()));
1623    Instruction *Term = SplitBlockAndInsertIfThen(Cmp, InsBefore, false);
1624    BasicBlock *CmpBlock = cast<Instruction>(Cmp)->getParent();
1625    IRBuilder<> IRBIf(Term);
1626    IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
1627    LocalStackBase = IRBIf.CreateCall2(
1628        AsanStackMallocFunc[StackMallocIdx],
1629        ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
1630    BasicBlock *SetBlock = cast<Instruction>(LocalStackBase)->getParent();
1631    IRB.SetInsertPoint(InsBefore);
1632    IRB.SetCurrentDebugLocation(EntryDebugLocation);
1633    PHINode *Phi = IRB.CreatePHI(IntptrTy, 2);
1634    Phi->addIncoming(OrigStackBase, CmpBlock);
1635    Phi->addIncoming(LocalStackBase, SetBlock);
1636    LocalStackBase = Phi;
1637  }
1638
1639  // Insert poison calls for lifetime intrinsics for alloca.
1640  bool HavePoisonedAllocas = false;
1641  for (const auto &APC : AllocaPoisonCallVec) {
1642    assert(APC.InsBefore);
1643    assert(APC.AI);
1644    IRBuilder<> IRB(APC.InsBefore);
1645    poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison);
1646    HavePoisonedAllocas |= APC.DoPoison;
1647  }
1648
1649  // Replace Alloca instructions with base+offset.
1650  for (const auto &Desc : SVD) {
1651    AllocaInst *AI = Desc.AI;
1652    Value *NewAllocaPtr = IRB.CreateIntToPtr(
1653        IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
1654        AI->getType());
1655    replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB);
1656    AI->replaceAllUsesWith(NewAllocaPtr);
1657  }
1658
1659  // The left-most redzone has enough space for at least 4 pointers.
1660  // Write the Magic value to redzone[0].
1661  Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1662  IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1663                  BasePlus0);
1664  // Write the frame description constant to redzone[1].
1665  Value *BasePlus1 = IRB.CreateIntToPtr(
1666    IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, ASan.LongSize/8)),
1667    IntptrPtrTy);
1668  GlobalVariable *StackDescriptionGlobal =
1669      createPrivateGlobalForString(*F.getParent(), L.DescriptionString,
1670                                   /*AllowMerging*/true);
1671  Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal,
1672                                             IntptrTy);
1673  IRB.CreateStore(Description, BasePlus1);
1674  // Write the PC to redzone[2].
1675  Value *BasePlus2 = IRB.CreateIntToPtr(
1676    IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy,
1677                                                   2 * ASan.LongSize/8)),
1678    IntptrPtrTy);
1679  IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2);
1680
1681  // Poison the stack redzones at the entry.
1682  Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
1683  poisonRedZones(L.ShadowBytes, IRB, ShadowBase, true);
1684
1685  // (Un)poison the stack before all ret instructions.
1686  for (auto Ret : RetVec) {
1687    IRBuilder<> IRBRet(Ret);
1688    // Mark the current frame as retired.
1689    IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1690                       BasePlus0);
1691    if (DoStackMalloc) {
1692      assert(StackMallocIdx >= 0);
1693      // if LocalStackBase != OrigStackBase:
1694      //     // In use-after-return mode, poison the whole stack frame.
1695      //     if StackMallocIdx <= 4
1696      //         // For small sizes inline the whole thing:
1697      //         memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize);
1698      //         **SavedFlagPtr(LocalStackBase) = 0
1699      //     else
1700      //         __asan_stack_free_N(LocalStackBase, OrigStackBase)
1701      // else
1702      //     <This is not a fake stack; unpoison the redzones>
1703      Value *Cmp = IRBRet.CreateICmpNE(LocalStackBase, OrigStackBase);
1704      TerminatorInst *ThenTerm, *ElseTerm;
1705      SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
1706
1707      IRBuilder<> IRBPoison(ThenTerm);
1708      if (StackMallocIdx <= 4) {
1709        int ClassSize = kMinStackMallocSize << StackMallocIdx;
1710        SetShadowToStackAfterReturnInlined(IRBPoison, ShadowBase,
1711                                           ClassSize >> Mapping.Scale);
1712        Value *SavedFlagPtrPtr = IRBPoison.CreateAdd(
1713            LocalStackBase,
1714            ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8));
1715        Value *SavedFlagPtr = IRBPoison.CreateLoad(
1716            IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
1717        IRBPoison.CreateStore(
1718            Constant::getNullValue(IRBPoison.getInt8Ty()),
1719            IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy()));
1720      } else {
1721        // For larger frames call __asan_stack_free_*.
1722        IRBPoison.CreateCall3(AsanStackFreeFunc[StackMallocIdx], LocalStackBase,
1723                              ConstantInt::get(IntptrTy, LocalStackSize),
1724                              OrigStackBase);
1725      }
1726
1727      IRBuilder<> IRBElse(ElseTerm);
1728      poisonRedZones(L.ShadowBytes, IRBElse, ShadowBase, false);
1729    } else if (HavePoisonedAllocas) {
1730      // If we poisoned some allocas in llvm.lifetime analysis,
1731      // unpoison whole stack frame now.
1732      assert(LocalStackBase == OrigStackBase);
1733      poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false);
1734    } else {
1735      poisonRedZones(L.ShadowBytes, IRBRet, ShadowBase, false);
1736    }
1737  }
1738
1739  // We are done. Remove the old unused alloca instructions.
1740  for (auto AI : AllocaVec)
1741    AI->eraseFromParent();
1742}
1743
1744void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
1745                                         IRBuilder<> &IRB, bool DoPoison) {
1746  // For now just insert the call to ASan runtime.
1747  Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
1748  Value *SizeArg = ConstantInt::get(IntptrTy, Size);
1749  IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc
1750                           : AsanUnpoisonStackMemoryFunc,
1751                  AddrArg, SizeArg);
1752}
1753
1754// Handling llvm.lifetime intrinsics for a given %alloca:
1755// (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
1756// (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
1757//     invalid accesses) and unpoison it for llvm.lifetime.start (the memory
1758//     could be poisoned by previous llvm.lifetime.end instruction, as the
1759//     variable may go in and out of scope several times, e.g. in loops).
1760// (3) if we poisoned at least one %alloca in a function,
1761//     unpoison the whole stack frame at function exit.
1762
1763AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) {
1764  if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
1765    // We're intested only in allocas we can handle.
1766    return isInterestingAlloca(*AI) ? AI : nullptr;
1767  // See if we've already calculated (or started to calculate) alloca for a
1768  // given value.
1769  AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
1770  if (I != AllocaForValue.end())
1771    return I->second;
1772  // Store 0 while we're calculating alloca for value V to avoid
1773  // infinite recursion if the value references itself.
1774  AllocaForValue[V] = nullptr;
1775  AllocaInst *Res = nullptr;
1776  if (CastInst *CI = dyn_cast<CastInst>(V))
1777    Res = findAllocaForValue(CI->getOperand(0));
1778  else if (PHINode *PN = dyn_cast<PHINode>(V)) {
1779    for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1780      Value *IncValue = PN->getIncomingValue(i);
1781      // Allow self-referencing phi-nodes.
1782      if (IncValue == PN) continue;
1783      AllocaInst *IncValueAI = findAllocaForValue(IncValue);
1784      // AI for incoming values should exist and should all be equal.
1785      if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res))
1786        return nullptr;
1787      Res = IncValueAI;
1788    }
1789  }
1790  if (Res)
1791    AllocaForValue[V] = Res;
1792  return Res;
1793}
1794