• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ADT/ArrayRef.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/SetVector.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/Analysis/MemoryBuiltins.h"
26 #include "llvm/Analysis/TargetLibraryInfo.h"
27 #include "llvm/Analysis/ValueTracking.h"
28 #include "llvm/IR/CallSite.h"
29 #include "llvm/IR/DIBuilder.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/Dominators.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/IRBuilder.h"
34 #include "llvm/IR/InlineAsm.h"
35 #include "llvm/IR/InstVisitor.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/LLVMContext.h"
38 #include "llvm/IR/MDBuilder.h"
39 #include "llvm/IR/Module.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/MC/MCSectionMachO.h"
42 #include "llvm/Support/CommandLine.h"
43 #include "llvm/Support/DataTypes.h"
44 #include "llvm/Support/Debug.h"
45 #include "llvm/Support/Endian.h"
46 #include "llvm/Support/SwapByteOrder.h"
47 #include "llvm/Support/raw_ostream.h"
48 #include "llvm/Transforms/Instrumentation.h"
49 #include "llvm/Transforms/Scalar.h"
50 #include "llvm/Transforms/Utils/ASanStackFrameLayout.h"
51 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
52 #include "llvm/Transforms/Utils/Cloning.h"
53 #include "llvm/Transforms/Utils/Local.h"
54 #include "llvm/Transforms/Utils/ModuleUtils.h"
55 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
56 #include <algorithm>
57 #include <string>
58 #include <system_error>
59 
60 using namespace llvm;
61 
62 #define DEBUG_TYPE "asan"
63 
64 static const uint64_t kDefaultShadowScale = 3;
65 static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
66 static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
67 static const uint64_t kIOSShadowOffset32 = 1ULL << 30;
68 static const uint64_t kIOSShadowOffset64 = 0x120200000;
69 static const uint64_t kIOSSimShadowOffset32 = 1ULL << 30;
70 static const uint64_t kIOSSimShadowOffset64 = kDefaultShadowOffset64;
71 static const uint64_t kSmallX86_64ShadowOffset = 0x7FFF8000;  // < 2G.
72 static const uint64_t kLinuxKasan_ShadowOffset64 = 0xdffffc0000000000;
73 static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41;
74 static const uint64_t kSystemZ_ShadowOffset64 = 1ULL << 52;
75 static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000;
76 static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37;
77 static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36;
78 static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30;
79 static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46;
80 static const uint64_t kWindowsShadowOffset32 = 3ULL << 28;
81 // TODO(wwchrome): Experimental for asan Win64, may change.
82 static const uint64_t kWindowsShadowOffset64 = 0x1ULL << 45;  // 32TB.
83 
84 static const size_t kMinStackMallocSize = 1 << 6;   // 64B
85 static const size_t kMaxStackMallocSize = 1 << 16;  // 64K
86 static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
87 static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
88 
89 static const char *const kAsanModuleCtorName = "asan.module_ctor";
90 static const char *const kAsanModuleDtorName = "asan.module_dtor";
91 static const uint64_t kAsanCtorAndDtorPriority = 1;
92 static const char *const kAsanReportErrorTemplate = "__asan_report_";
93 static const char *const kAsanRegisterGlobalsName = "__asan_register_globals";
94 static const char *const kAsanUnregisterGlobalsName =
95     "__asan_unregister_globals";
96 static const char *const kAsanRegisterImageGlobalsName =
97   "__asan_register_image_globals";
98 static const char *const kAsanUnregisterImageGlobalsName =
99   "__asan_unregister_image_globals";
100 static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
101 static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
102 static const char *const kAsanInitName = "__asan_init";
103 static const char *const kAsanVersionCheckName =
104     "__asan_version_mismatch_check_v8";
105 static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp";
106 static const char *const kAsanPtrSub = "__sanitizer_ptr_sub";
107 static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return";
108 static const int kMaxAsanStackMallocSizeClass = 10;
109 static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_";
110 static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_";
111 static const char *const kAsanGenPrefix = "__asan_gen_";
112 static const char *const kODRGenPrefix = "__odr_asan_gen_";
113 static const char *const kSanCovGenPrefix = "__sancov_gen_";
114 static const char *const kAsanPoisonStackMemoryName =
115     "__asan_poison_stack_memory";
116 static const char *const kAsanUnpoisonStackMemoryName =
117     "__asan_unpoison_stack_memory";
118 static const char *const kAsanGlobalsRegisteredFlagName =
119     "__asan_globals_registered";
120 
121 static const char *const kAsanOptionDetectUseAfterReturn =
122     "__asan_option_detect_stack_use_after_return";
123 
124 static const char *const kAsanAllocaPoison = "__asan_alloca_poison";
125 static const char *const kAsanAllocasUnpoison = "__asan_allocas_unpoison";
126 
127 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
128 static const size_t kNumberOfAccessSizes = 5;
129 
130 static const unsigned kAllocaRzSize = 32;
131 
132 // Command-line flags.
133 static cl::opt<bool> ClEnableKasan(
134     "asan-kernel", cl::desc("Enable KernelAddressSanitizer instrumentation"),
135     cl::Hidden, cl::init(false));
136 static cl::opt<bool> ClRecover(
137     "asan-recover",
138     cl::desc("Enable recovery mode (continue-after-error)."),
139     cl::Hidden, cl::init(false));
140 
141 // This flag may need to be replaced with -f[no-]asan-reads.
142 static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
143                                        cl::desc("instrument read instructions"),
144                                        cl::Hidden, cl::init(true));
145 static cl::opt<bool> ClInstrumentWrites(
146     "asan-instrument-writes", cl::desc("instrument write instructions"),
147     cl::Hidden, cl::init(true));
148 static cl::opt<bool> ClInstrumentAtomics(
149     "asan-instrument-atomics",
150     cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
151     cl::init(true));
152 static cl::opt<bool> ClAlwaysSlowPath(
153     "asan-always-slow-path",
154     cl::desc("use instrumentation with slow path for all accesses"), cl::Hidden,
155     cl::init(false));
156 // This flag limits the number of instructions to be instrumented
157 // in any given BB. Normally, this should be set to unlimited (INT_MAX),
158 // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
159 // set it to 10000.
160 static cl::opt<int> ClMaxInsnsToInstrumentPerBB(
161     "asan-max-ins-per-bb", cl::init(10000),
162     cl::desc("maximal number of instructions to instrument in any given BB"),
163     cl::Hidden);
164 // This flag may need to be replaced with -f[no]asan-stack.
165 static cl::opt<bool> ClStack("asan-stack", cl::desc("Handle stack memory"),
166                              cl::Hidden, cl::init(true));
167 static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
168                                       cl::desc("Check stack-use-after-return"),
169                                       cl::Hidden, cl::init(true));
170 static cl::opt<bool> ClUseAfterScope("asan-use-after-scope",
171                                      cl::desc("Check stack-use-after-scope"),
172                                      cl::Hidden, cl::init(false));
173 // This flag may need to be replaced with -f[no]asan-globals.
174 static cl::opt<bool> ClGlobals("asan-globals",
175                                cl::desc("Handle global objects"), cl::Hidden,
176                                cl::init(true));
177 static cl::opt<bool> ClInitializers("asan-initialization-order",
178                                     cl::desc("Handle C++ initializer order"),
179                                     cl::Hidden, cl::init(true));
180 static cl::opt<bool> ClInvalidPointerPairs(
181     "asan-detect-invalid-pointer-pair",
182     cl::desc("Instrument <, <=, >, >=, - with pointer operands"), cl::Hidden,
183     cl::init(false));
184 static cl::opt<unsigned> ClRealignStack(
185     "asan-realign-stack",
186     cl::desc("Realign stack to the value of this flag (power of two)"),
187     cl::Hidden, cl::init(32));
188 static cl::opt<int> ClInstrumentationWithCallsThreshold(
189     "asan-instrumentation-with-call-threshold",
190     cl::desc(
191         "If the function being instrumented contains more than "
192         "this number of memory accesses, use callbacks instead of "
193         "inline checks (-1 means never use callbacks)."),
194     cl::Hidden, cl::init(7000));
195 static cl::opt<std::string> ClMemoryAccessCallbackPrefix(
196     "asan-memory-access-callback-prefix",
197     cl::desc("Prefix for memory access callbacks"), cl::Hidden,
198     cl::init("__asan_"));
199 static cl::opt<bool> ClInstrumentAllocas("asan-instrument-allocas",
200                                          cl::desc("instrument dynamic allocas"),
201                                          cl::Hidden, cl::init(true));
202 static cl::opt<bool> ClSkipPromotableAllocas(
203     "asan-skip-promotable-allocas",
204     cl::desc("Do not instrument promotable allocas"), cl::Hidden,
205     cl::init(true));
206 
207 // These flags allow to change the shadow mapping.
208 // The shadow mapping looks like
209 //    Shadow = (Mem >> scale) + offset
210 static cl::opt<int> ClMappingScale("asan-mapping-scale",
211                                    cl::desc("scale of asan shadow mapping"),
212                                    cl::Hidden, cl::init(0));
213 static cl::opt<unsigned long long> ClMappingOffset(
214     "asan-mapping-offset",
215     cl::desc("offset of asan shadow mapping [EXPERIMENTAL]"), cl::Hidden,
216     cl::init(0));
217 
218 // Optimization flags. Not user visible, used mostly for testing
219 // and benchmarking the tool.
220 static cl::opt<bool> ClOpt("asan-opt", cl::desc("Optimize instrumentation"),
221                            cl::Hidden, cl::init(true));
222 static cl::opt<bool> ClOptSameTemp(
223     "asan-opt-same-temp", cl::desc("Instrument the same temp just once"),
224     cl::Hidden, cl::init(true));
225 static cl::opt<bool> ClOptGlobals("asan-opt-globals",
226                                   cl::desc("Don't instrument scalar globals"),
227                                   cl::Hidden, cl::init(true));
228 static cl::opt<bool> ClOptStack(
229     "asan-opt-stack", cl::desc("Don't instrument scalar stack variables"),
230     cl::Hidden, cl::init(false));
231 
232 static cl::opt<bool> ClDynamicAllocaStack(
233     "asan-stack-dynamic-alloca",
234     cl::desc("Use dynamic alloca to represent stack variables"), cl::Hidden,
235     cl::init(true));
236 
237 static cl::opt<uint32_t> ClForceExperiment(
238     "asan-force-experiment",
239     cl::desc("Force optimization experiment (for testing)"), cl::Hidden,
240     cl::init(0));
241 
242 static cl::opt<bool>
243     ClUsePrivateAliasForGlobals("asan-use-private-alias",
244                                 cl::desc("Use private aliases for global"
245                                          " variables"),
246                                 cl::Hidden, cl::init(false));
247 
248 static cl::opt<bool>
249     ClUseMachOGlobalsSection("asan-globals-live-support",
250                              cl::desc("Use linker features to support dead "
251                                       "code stripping of globals "
252                                       "(Mach-O only)"),
253                              cl::Hidden, cl::init(false));
254 
255 // Debug flags.
256 static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
257                             cl::init(0));
258 static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
259                                  cl::Hidden, cl::init(0));
260 static cl::opt<std::string> ClDebugFunc("asan-debug-func", cl::Hidden,
261                                         cl::desc("Debug func"));
262 static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
263                                cl::Hidden, cl::init(-1));
264 static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
265                                cl::Hidden, cl::init(-1));
266 
267 STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
268 STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
269 STATISTIC(NumOptimizedAccessesToGlobalVar,
270           "Number of optimized accesses to global vars");
271 STATISTIC(NumOptimizedAccessesToStackVar,
272           "Number of optimized accesses to stack vars");
273 
274 namespace {
275 /// Frontend-provided metadata for source location.
276 struct LocationMetadata {
277   StringRef Filename;
278   int LineNo;
279   int ColumnNo;
280 
LocationMetadata__anon234c86510111::LocationMetadata281   LocationMetadata() : Filename(), LineNo(0), ColumnNo(0) {}
282 
empty__anon234c86510111::LocationMetadata283   bool empty() const { return Filename.empty(); }
284 
parse__anon234c86510111::LocationMetadata285   void parse(MDNode *MDN) {
286     assert(MDN->getNumOperands() == 3);
287     MDString *DIFilename = cast<MDString>(MDN->getOperand(0));
288     Filename = DIFilename->getString();
289     LineNo =
290         mdconst::extract<ConstantInt>(MDN->getOperand(1))->getLimitedValue();
291     ColumnNo =
292         mdconst::extract<ConstantInt>(MDN->getOperand(2))->getLimitedValue();
293   }
294 };
295 
296 /// Frontend-provided metadata for global variables.
297 class GlobalsMetadata {
298  public:
299   struct Entry {
Entry__anon234c86510111::GlobalsMetadata::Entry300     Entry() : SourceLoc(), Name(), IsDynInit(false), IsBlacklisted(false) {}
301     LocationMetadata SourceLoc;
302     StringRef Name;
303     bool IsDynInit;
304     bool IsBlacklisted;
305   };
306 
GlobalsMetadata()307   GlobalsMetadata() : inited_(false) {}
308 
reset()309   void reset() {
310     inited_ = false;
311     Entries.clear();
312   }
313 
init(Module & M)314   void init(Module &M) {
315     assert(!inited_);
316     inited_ = true;
317     NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals");
318     if (!Globals) return;
319     for (auto MDN : Globals->operands()) {
320       // Metadata node contains the global and the fields of "Entry".
321       assert(MDN->getNumOperands() == 5);
322       auto *GV = mdconst::extract_or_null<GlobalVariable>(MDN->getOperand(0));
323       // The optimizer may optimize away a global entirely.
324       if (!GV) continue;
325       // We can already have an entry for GV if it was merged with another
326       // global.
327       Entry &E = Entries[GV];
328       if (auto *Loc = cast_or_null<MDNode>(MDN->getOperand(1)))
329         E.SourceLoc.parse(Loc);
330       if (auto *Name = cast_or_null<MDString>(MDN->getOperand(2)))
331         E.Name = Name->getString();
332       ConstantInt *IsDynInit =
333           mdconst::extract<ConstantInt>(MDN->getOperand(3));
334       E.IsDynInit |= IsDynInit->isOne();
335       ConstantInt *IsBlacklisted =
336           mdconst::extract<ConstantInt>(MDN->getOperand(4));
337       E.IsBlacklisted |= IsBlacklisted->isOne();
338     }
339   }
340 
341   /// Returns metadata entry for a given global.
get(GlobalVariable * G) const342   Entry get(GlobalVariable *G) const {
343     auto Pos = Entries.find(G);
344     return (Pos != Entries.end()) ? Pos->second : Entry();
345   }
346 
347  private:
348   bool inited_;
349   DenseMap<GlobalVariable *, Entry> Entries;
350 };
351 
352 /// This struct defines the shadow mapping using the rule:
353 ///   shadow = (mem >> Scale) ADD-or-OR Offset.
354 struct ShadowMapping {
355   int Scale;
356   uint64_t Offset;
357   bool OrShadowOffset;
358 };
359 
getShadowMapping(Triple & TargetTriple,int LongSize,bool IsKasan)360 static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize,
361                                       bool IsKasan) {
362   bool IsAndroid = TargetTriple.isAndroid();
363   bool IsIOS = TargetTriple.isiOS() || TargetTriple.isWatchOS();
364   bool IsFreeBSD = TargetTriple.isOSFreeBSD();
365   bool IsLinux = TargetTriple.isOSLinux();
366   bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64 ||
367                  TargetTriple.getArch() == llvm::Triple::ppc64le;
368   bool IsSystemZ = TargetTriple.getArch() == llvm::Triple::systemz;
369   bool IsX86 = TargetTriple.getArch() == llvm::Triple::x86;
370   bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64;
371   bool IsMIPS32 = TargetTriple.getArch() == llvm::Triple::mips ||
372                   TargetTriple.getArch() == llvm::Triple::mipsel;
373   bool IsMIPS64 = TargetTriple.getArch() == llvm::Triple::mips64 ||
374                   TargetTriple.getArch() == llvm::Triple::mips64el;
375   bool IsAArch64 = TargetTriple.getArch() == llvm::Triple::aarch64;
376   bool IsWindows = TargetTriple.isOSWindows();
377 
378   ShadowMapping Mapping;
379 
380   if (LongSize == 32) {
381     // Android is always PIE, which means that the beginning of the address
382     // space is always available.
383     if (IsAndroid)
384       Mapping.Offset = 0;
385     else if (IsMIPS32)
386       Mapping.Offset = kMIPS32_ShadowOffset32;
387     else if (IsFreeBSD)
388       Mapping.Offset = kFreeBSD_ShadowOffset32;
389     else if (IsIOS)
390       // If we're targeting iOS and x86, the binary is built for iOS simulator.
391       Mapping.Offset = IsX86 ? kIOSSimShadowOffset32 : kIOSShadowOffset32;
392     else if (IsWindows)
393       Mapping.Offset = kWindowsShadowOffset32;
394     else
395       Mapping.Offset = kDefaultShadowOffset32;
396   } else {  // LongSize == 64
397     if (IsPPC64)
398       Mapping.Offset = kPPC64_ShadowOffset64;
399     else if (IsSystemZ)
400       Mapping.Offset = kSystemZ_ShadowOffset64;
401     else if (IsFreeBSD)
402       Mapping.Offset = kFreeBSD_ShadowOffset64;
403     else if (IsLinux && IsX86_64) {
404       if (IsKasan)
405         Mapping.Offset = kLinuxKasan_ShadowOffset64;
406       else
407         Mapping.Offset = kSmallX86_64ShadowOffset;
408     } else if (IsWindows && IsX86_64) {
409       Mapping.Offset = kWindowsShadowOffset64;
410     } else if (IsMIPS64)
411       Mapping.Offset = kMIPS64_ShadowOffset64;
412     else if (IsIOS)
413       // If we're targeting iOS and x86, the binary is built for iOS simulator.
414       Mapping.Offset = IsX86_64 ? kIOSSimShadowOffset64 : kIOSShadowOffset64;
415     else if (IsAArch64)
416       Mapping.Offset = kAArch64_ShadowOffset64;
417     else
418       Mapping.Offset = kDefaultShadowOffset64;
419   }
420 
421   Mapping.Scale = kDefaultShadowScale;
422   if (ClMappingScale.getNumOccurrences() > 0) {
423     Mapping.Scale = ClMappingScale;
424   }
425 
426   if (ClMappingOffset.getNumOccurrences() > 0) {
427     Mapping.Offset = ClMappingOffset;
428   }
429 
430   // OR-ing shadow offset if more efficient (at least on x86) if the offset
431   // is a power of two, but on ppc64 we have to use add since the shadow
432   // offset is not necessary 1/8-th of the address space.  On SystemZ,
433   // we could OR the constant in a single instruction, but it's more
434   // efficient to load it once and use indexed addressing.
435   Mapping.OrShadowOffset = !IsAArch64 && !IsPPC64 && !IsSystemZ
436                            && !(Mapping.Offset & (Mapping.Offset - 1));
437 
438   return Mapping;
439 }
440 
RedzoneSizeForScale(int MappingScale)441 static size_t RedzoneSizeForScale(int MappingScale) {
442   // Redzone used for stack and globals is at least 32 bytes.
443   // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
444   return std::max(32U, 1U << MappingScale);
445 }
446 
447 /// AddressSanitizer: instrument the code in module to find memory bugs.
448 struct AddressSanitizer : public FunctionPass {
AddressSanitizer__anon234c86510111::AddressSanitizer449   explicit AddressSanitizer(bool CompileKernel = false, bool Recover = false,
450                             bool UseAfterScope = false)
451       : FunctionPass(ID), CompileKernel(CompileKernel || ClEnableKasan),
452         Recover(Recover || ClRecover),
453         UseAfterScope(UseAfterScope || ClUseAfterScope) {
454     initializeAddressSanitizerPass(*PassRegistry::getPassRegistry());
455   }
getPassName__anon234c86510111::AddressSanitizer456   const char *getPassName() const override {
457     return "AddressSanitizerFunctionPass";
458   }
getAnalysisUsage__anon234c86510111::AddressSanitizer459   void getAnalysisUsage(AnalysisUsage &AU) const override {
460     AU.addRequired<DominatorTreeWrapperPass>();
461     AU.addRequired<TargetLibraryInfoWrapperPass>();
462   }
getAllocaSizeInBytes__anon234c86510111::AddressSanitizer463   uint64_t getAllocaSizeInBytes(AllocaInst *AI) const {
464     uint64_t ArraySize = 1;
465     if (AI->isArrayAllocation()) {
466       ConstantInt *CI = dyn_cast<ConstantInt>(AI->getArraySize());
467       assert(CI && "non-constant array size");
468       ArraySize = CI->getZExtValue();
469     }
470     Type *Ty = AI->getAllocatedType();
471     uint64_t SizeInBytes =
472         AI->getModule()->getDataLayout().getTypeAllocSize(Ty);
473     return SizeInBytes * ArraySize;
474   }
475   /// Check if we want (and can) handle this alloca.
476   bool isInterestingAlloca(AllocaInst &AI);
477 
478   /// If it is an interesting memory access, return the PointerOperand
479   /// and set IsWrite/Alignment. Otherwise return nullptr.
480   Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite,
481                                    uint64_t *TypeSize, unsigned *Alignment);
482   void instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis, Instruction *I,
483                      bool UseCalls, const DataLayout &DL);
484   void instrumentPointerComparisonOrSubtraction(Instruction *I);
485   void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore,
486                          Value *Addr, uint32_t TypeSize, bool IsWrite,
487                          Value *SizeArgument, bool UseCalls, uint32_t Exp);
488   void instrumentUnusualSizeOrAlignment(Instruction *I, Value *Addr,
489                                         uint32_t TypeSize, bool IsWrite,
490                                         Value *SizeArgument, bool UseCalls,
491                                         uint32_t Exp);
492   Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
493                            Value *ShadowValue, uint32_t TypeSize);
494   Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
495                                  bool IsWrite, size_t AccessSizeIndex,
496                                  Value *SizeArgument, uint32_t Exp);
497   void instrumentMemIntrinsic(MemIntrinsic *MI);
498   Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
499   bool runOnFunction(Function &F) override;
500   bool maybeInsertAsanInitAtFunctionEntry(Function &F);
501   void markEscapedLocalAllocas(Function &F);
502   bool doInitialization(Module &M) override;
503   bool doFinalization(Module &M) override;
504   static char ID;  // Pass identification, replacement for typeid
505 
getDominatorTree__anon234c86510111::AddressSanitizer506   DominatorTree &getDominatorTree() const { return *DT; }
507 
508  private:
509   void initializeCallbacks(Module &M);
510 
511   bool LooksLikeCodeInBug11395(Instruction *I);
512   bool GlobalIsLinkerInitialized(GlobalVariable *G);
513   bool isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis, Value *Addr,
514                     uint64_t TypeSize) const;
515 
516   /// Helper to cleanup per-function state.
517   struct FunctionStateRAII {
518     AddressSanitizer *Pass;
FunctionStateRAII__anon234c86510111::AddressSanitizer::FunctionStateRAII519     FunctionStateRAII(AddressSanitizer *Pass) : Pass(Pass) {
520       assert(Pass->ProcessedAllocas.empty() &&
521              "last pass forgot to clear cache");
522     }
~FunctionStateRAII__anon234c86510111::AddressSanitizer::FunctionStateRAII523     ~FunctionStateRAII() { Pass->ProcessedAllocas.clear(); }
524   };
525 
526   LLVMContext *C;
527   Triple TargetTriple;
528   int LongSize;
529   bool CompileKernel;
530   bool Recover;
531   bool UseAfterScope;
532   Type *IntptrTy;
533   ShadowMapping Mapping;
534   DominatorTree *DT;
535   Function *AsanCtorFunction = nullptr;
536   Function *AsanInitFunction = nullptr;
537   Function *AsanHandleNoReturnFunc;
538   Function *AsanPtrCmpFunction, *AsanPtrSubFunction;
539   // This array is indexed by AccessIsWrite, Experiment and log2(AccessSize).
540   Function *AsanErrorCallback[2][2][kNumberOfAccessSizes];
541   Function *AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes];
542   // This array is indexed by AccessIsWrite and Experiment.
543   Function *AsanErrorCallbackSized[2][2];
544   Function *AsanMemoryAccessCallbackSized[2][2];
545   Function *AsanMemmove, *AsanMemcpy, *AsanMemset;
546   InlineAsm *EmptyAsm;
547   GlobalsMetadata GlobalsMD;
548   DenseMap<AllocaInst *, bool> ProcessedAllocas;
549 
550   friend struct FunctionStackPoisoner;
551 };
552 
553 class AddressSanitizerModule : public ModulePass {
554  public:
AddressSanitizerModule(bool CompileKernel=false,bool Recover=false)555   explicit AddressSanitizerModule(bool CompileKernel = false,
556                                   bool Recover = false)
557       : ModulePass(ID), CompileKernel(CompileKernel || ClEnableKasan),
558         Recover(Recover || ClRecover) {}
559   bool runOnModule(Module &M) override;
560   static char ID;  // Pass identification, replacement for typeid
getPassName() const561   const char *getPassName() const override { return "AddressSanitizerModule"; }
562 
563  private:
564   void initializeCallbacks(Module &M);
565 
566   bool InstrumentGlobals(IRBuilder<> &IRB, Module &M);
567   bool ShouldInstrumentGlobal(GlobalVariable *G);
568   bool ShouldUseMachOGlobalsSection() const;
569   void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
570   void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
MinRedzoneSizeForGlobal() const571   size_t MinRedzoneSizeForGlobal() const {
572     return RedzoneSizeForScale(Mapping.Scale);
573   }
574 
575   GlobalsMetadata GlobalsMD;
576   bool CompileKernel;
577   bool Recover;
578   Type *IntptrTy;
579   LLVMContext *C;
580   Triple TargetTriple;
581   ShadowMapping Mapping;
582   Function *AsanPoisonGlobals;
583   Function *AsanUnpoisonGlobals;
584   Function *AsanRegisterGlobals;
585   Function *AsanUnregisterGlobals;
586   Function *AsanRegisterImageGlobals;
587   Function *AsanUnregisterImageGlobals;
588 };
589 
590 // Stack poisoning does not play well with exception handling.
591 // When an exception is thrown, we essentially bypass the code
592 // that unpoisones the stack. This is why the run-time library has
593 // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
594 // stack in the interceptor. This however does not work inside the
595 // actual function which catches the exception. Most likely because the
596 // compiler hoists the load of the shadow value somewhere too high.
597 // This causes asan to report a non-existing bug on 453.povray.
598 // It sounds like an LLVM bug.
599 struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
600   Function &F;
601   AddressSanitizer &ASan;
602   DIBuilder DIB;
603   LLVMContext *C;
604   Type *IntptrTy;
605   Type *IntptrPtrTy;
606   ShadowMapping Mapping;
607 
608   SmallVector<AllocaInst *, 16> AllocaVec;
609   SmallSetVector<AllocaInst *, 16> NonInstrumentedStaticAllocaVec;
610   SmallVector<Instruction *, 8> RetVec;
611   unsigned StackAlignment;
612 
613   Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1],
614       *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1];
615   Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
616   Function *AsanAllocaPoisonFunc, *AsanAllocasUnpoisonFunc;
617 
618   // Stores a place and arguments of poisoning/unpoisoning call for alloca.
619   struct AllocaPoisonCall {
620     IntrinsicInst *InsBefore;
621     AllocaInst *AI;
622     uint64_t Size;
623     bool DoPoison;
624   };
625   SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec;
626 
627   SmallVector<AllocaInst *, 1> DynamicAllocaVec;
628   SmallVector<IntrinsicInst *, 1> StackRestoreVec;
629   AllocaInst *DynamicAllocaLayout = nullptr;
630   IntrinsicInst *LocalEscapeCall = nullptr;
631 
632   // Maps Value to an AllocaInst from which the Value is originated.
633   typedef DenseMap<Value *, AllocaInst *> AllocaForValueMapTy;
634   AllocaForValueMapTy AllocaForValue;
635 
636   bool HasNonEmptyInlineAsm = false;
637   bool HasReturnsTwiceCall = false;
638   std::unique_ptr<CallInst> EmptyInlineAsm;
639 
FunctionStackPoisoner__anon234c86510111::FunctionStackPoisoner640   FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
641       : F(F),
642         ASan(ASan),
643         DIB(*F.getParent(), /*AllowUnresolved*/ false),
644         C(ASan.C),
645         IntptrTy(ASan.IntptrTy),
646         IntptrPtrTy(PointerType::get(IntptrTy, 0)),
647         Mapping(ASan.Mapping),
648         StackAlignment(1 << Mapping.Scale),
649         EmptyInlineAsm(CallInst::Create(ASan.EmptyAsm)) {}
650 
runOnFunction__anon234c86510111::FunctionStackPoisoner651   bool runOnFunction() {
652     if (!ClStack) return false;
653     // Collect alloca, ret, lifetime instructions etc.
654     for (BasicBlock *BB : depth_first(&F.getEntryBlock())) visit(*BB);
655 
656     if (AllocaVec.empty() && DynamicAllocaVec.empty()) return false;
657 
658     initializeCallbacks(*F.getParent());
659 
660     poisonStack();
661 
662     if (ClDebugStack) {
663       DEBUG(dbgs() << F);
664     }
665     return true;
666   }
667 
668   // Finds all Alloca instructions and puts
669   // poisoned red zones around all of them.
670   // Then unpoison everything back before the function returns.
671   void poisonStack();
672 
673   void createDynamicAllocasInitStorage();
674 
675   // ----------------------- Visitors.
676   /// \brief Collect all Ret instructions.
visitReturnInst__anon234c86510111::FunctionStackPoisoner677   void visitReturnInst(ReturnInst &RI) { RetVec.push_back(&RI); }
678 
unpoisonDynamicAllocasBeforeInst__anon234c86510111::FunctionStackPoisoner679   void unpoisonDynamicAllocasBeforeInst(Instruction *InstBefore,
680                                         Value *SavedStack) {
681     IRBuilder<> IRB(InstBefore);
682     Value *DynamicAreaPtr = IRB.CreatePtrToInt(SavedStack, IntptrTy);
683     // When we insert _asan_allocas_unpoison before @llvm.stackrestore, we
684     // need to adjust extracted SP to compute the address of the most recent
685     // alloca. We have a special @llvm.get.dynamic.area.offset intrinsic for
686     // this purpose.
687     if (!isa<ReturnInst>(InstBefore)) {
688       Function *DynamicAreaOffsetFunc = Intrinsic::getDeclaration(
689           InstBefore->getModule(), Intrinsic::get_dynamic_area_offset,
690           {IntptrTy});
691 
692       Value *DynamicAreaOffset = IRB.CreateCall(DynamicAreaOffsetFunc, {});
693 
694       DynamicAreaPtr = IRB.CreateAdd(IRB.CreatePtrToInt(SavedStack, IntptrTy),
695                                      DynamicAreaOffset);
696     }
697 
698     IRB.CreateCall(AsanAllocasUnpoisonFunc,
699                    {IRB.CreateLoad(DynamicAllocaLayout), DynamicAreaPtr});
700   }
701 
702   // Unpoison dynamic allocas redzones.
unpoisonDynamicAllocas__anon234c86510111::FunctionStackPoisoner703   void unpoisonDynamicAllocas() {
704     for (auto &Ret : RetVec)
705       unpoisonDynamicAllocasBeforeInst(Ret, DynamicAllocaLayout);
706 
707     for (auto &StackRestoreInst : StackRestoreVec)
708       unpoisonDynamicAllocasBeforeInst(StackRestoreInst,
709                                        StackRestoreInst->getOperand(0));
710   }
711 
712   // Deploy and poison redzones around dynamic alloca call. To do this, we
713   // should replace this call with another one with changed parameters and
714   // replace all its uses with new address, so
715   //   addr = alloca type, old_size, align
716   // is replaced by
717   //   new_size = (old_size + additional_size) * sizeof(type)
718   //   tmp = alloca i8, new_size, max(align, 32)
719   //   addr = tmp + 32 (first 32 bytes are for the left redzone).
720   // Additional_size is added to make new memory allocation contain not only
721   // requested memory, but also left, partial and right redzones.
722   void handleDynamicAllocaCall(AllocaInst *AI);
723 
724   /// \brief Collect Alloca instructions we want (and can) handle.
visitAllocaInst__anon234c86510111::FunctionStackPoisoner725   void visitAllocaInst(AllocaInst &AI) {
726     if (!ASan.isInterestingAlloca(AI)) {
727       if (AI.isStaticAlloca()) NonInstrumentedStaticAllocaVec.insert(&AI);
728       return;
729     }
730 
731     StackAlignment = std::max(StackAlignment, AI.getAlignment());
732     if (!AI.isStaticAlloca())
733       DynamicAllocaVec.push_back(&AI);
734     else
735       AllocaVec.push_back(&AI);
736   }
737 
738   /// \brief Collect lifetime intrinsic calls to check for use-after-scope
739   /// errors.
visitIntrinsicInst__anon234c86510111::FunctionStackPoisoner740   void visitIntrinsicInst(IntrinsicInst &II) {
741     Intrinsic::ID ID = II.getIntrinsicID();
742     if (ID == Intrinsic::stackrestore) StackRestoreVec.push_back(&II);
743     if (ID == Intrinsic::localescape) LocalEscapeCall = &II;
744     if (!ASan.UseAfterScope)
745       return;
746     if (ID != Intrinsic::lifetime_start && ID != Intrinsic::lifetime_end)
747       return;
748     // Found lifetime intrinsic, add ASan instrumentation if necessary.
749     ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0));
750     // If size argument is undefined, don't do anything.
751     if (Size->isMinusOne()) return;
752     // Check that size doesn't saturate uint64_t and can
753     // be stored in IntptrTy.
754     const uint64_t SizeValue = Size->getValue().getLimitedValue();
755     if (SizeValue == ~0ULL ||
756         !ConstantInt::isValueValidForType(IntptrTy, SizeValue))
757       return;
758     // Find alloca instruction that corresponds to llvm.lifetime argument.
759     AllocaInst *AI = findAllocaForValue(II.getArgOperand(1));
760     if (!AI || !ASan.isInterestingAlloca(*AI))
761       return;
762     bool DoPoison = (ID == Intrinsic::lifetime_end);
763     AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison};
764     AllocaPoisonCallVec.push_back(APC);
765   }
766 
visitCallSite__anon234c86510111::FunctionStackPoisoner767   void visitCallSite(CallSite CS) {
768     Instruction *I = CS.getInstruction();
769     if (CallInst *CI = dyn_cast<CallInst>(I)) {
770       HasNonEmptyInlineAsm |=
771           CI->isInlineAsm() && !CI->isIdenticalTo(EmptyInlineAsm.get());
772       HasReturnsTwiceCall |= CI->canReturnTwice();
773     }
774   }
775 
776   // ---------------------- Helpers.
777   void initializeCallbacks(Module &M);
778 
doesDominateAllExits__anon234c86510111::FunctionStackPoisoner779   bool doesDominateAllExits(const Instruction *I) const {
780     for (auto Ret : RetVec) {
781       if (!ASan.getDominatorTree().dominates(I, Ret)) return false;
782     }
783     return true;
784   }
785 
786   /// Finds alloca where the value comes from.
787   AllocaInst *findAllocaForValue(Value *V);
788   void poisonRedZones(ArrayRef<uint8_t> ShadowBytes, IRBuilder<> &IRB,
789                       Value *ShadowBase, bool DoPoison);
790   void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison);
791 
792   void SetShadowToStackAfterReturnInlined(IRBuilder<> &IRB, Value *ShadowBase,
793                                           int Size);
794   Value *createAllocaForLayout(IRBuilder<> &IRB, const ASanStackFrameLayout &L,
795                                bool Dynamic);
796   PHINode *createPHI(IRBuilder<> &IRB, Value *Cond, Value *ValueIfTrue,
797                      Instruction *ThenTerm, Value *ValueIfFalse);
798 };
799 
800 } // anonymous namespace
801 
802 char AddressSanitizer::ID = 0;
803 INITIALIZE_PASS_BEGIN(
804     AddressSanitizer, "asan",
805     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false,
806     false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)807 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
808 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
809 INITIALIZE_PASS_END(
810     AddressSanitizer, "asan",
811     "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false,
812     false)
813 FunctionPass *llvm::createAddressSanitizerFunctionPass(bool CompileKernel,
814                                                        bool Recover,
815                                                        bool UseAfterScope) {
816   assert(!CompileKernel || Recover);
817   return new AddressSanitizer(CompileKernel, Recover, UseAfterScope);
818 }
819 
820 char AddressSanitizerModule::ID = 0;
821 INITIALIZE_PASS(
822     AddressSanitizerModule, "asan-module",
823     "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
824     "ModulePass",
825     false, false)
createAddressSanitizerModulePass(bool CompileKernel,bool Recover)826 ModulePass *llvm::createAddressSanitizerModulePass(bool CompileKernel,
827                                                    bool Recover) {
828   assert(!CompileKernel || Recover);
829   return new AddressSanitizerModule(CompileKernel, Recover);
830 }
831 
TypeSizeToSizeIndex(uint32_t TypeSize)832 static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
833   size_t Res = countTrailingZeros(TypeSize / 8);
834   assert(Res < kNumberOfAccessSizes);
835   return Res;
836 }
837 
838 // \brief Create a constant for Str so that we can pass it to the run-time lib.
createPrivateGlobalForString(Module & M,StringRef Str,bool AllowMerging)839 static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
840                                                     bool AllowMerging) {
841   Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
842   // We use private linkage for module-local strings. If they can be merged
843   // with another one, we set the unnamed_addr attribute.
844   GlobalVariable *GV =
845       new GlobalVariable(M, StrConst->getType(), true,
846                          GlobalValue::PrivateLinkage, StrConst, kAsanGenPrefix);
847   if (AllowMerging) GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
848   GV->setAlignment(1);  // Strings may not be merged w/o setting align 1.
849   return GV;
850 }
851 
852 /// \brief Create a global describing a source location.
createPrivateGlobalForSourceLoc(Module & M,LocationMetadata MD)853 static GlobalVariable *createPrivateGlobalForSourceLoc(Module &M,
854                                                        LocationMetadata MD) {
855   Constant *LocData[] = {
856       createPrivateGlobalForString(M, MD.Filename, true),
857       ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.LineNo),
858       ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.ColumnNo),
859   };
860   auto LocStruct = ConstantStruct::getAnon(LocData);
861   auto GV = new GlobalVariable(M, LocStruct->getType(), true,
862                                GlobalValue::PrivateLinkage, LocStruct,
863                                kAsanGenPrefix);
864   GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
865   return GV;
866 }
867 
868 /// \brief Check if \p G has been created by a trusted compiler pass.
GlobalWasGeneratedByCompiler(GlobalVariable * G)869 static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) {
870   // Do not instrument asan globals.
871   if (G->getName().startswith(kAsanGenPrefix) ||
872       G->getName().startswith(kSanCovGenPrefix) ||
873       G->getName().startswith(kODRGenPrefix))
874     return true;
875 
876   // Do not instrument gcov counter arrays.
877   if (G->getName() == "__llvm_gcov_ctr")
878     return true;
879 
880   return false;
881 }
882 
memToShadow(Value * Shadow,IRBuilder<> & IRB)883 Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
884   // Shadow >> scale
885   Shadow = IRB.CreateLShr(Shadow, Mapping.Scale);
886   if (Mapping.Offset == 0) return Shadow;
887   // (Shadow >> scale) | offset
888   if (Mapping.OrShadowOffset)
889     return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
890   else
891     return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset));
892 }
893 
894 // Instrument memset/memmove/memcpy
instrumentMemIntrinsic(MemIntrinsic * MI)895 void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
896   IRBuilder<> IRB(MI);
897   if (isa<MemTransferInst>(MI)) {
898     IRB.CreateCall(
899         isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy,
900         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
901          IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
902          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
903   } else if (isa<MemSetInst>(MI)) {
904     IRB.CreateCall(
905         AsanMemset,
906         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
907          IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
908          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
909   }
910   MI->eraseFromParent();
911 }
912 
913 /// Check if we want (and can) handle this alloca.
isInterestingAlloca(AllocaInst & AI)914 bool AddressSanitizer::isInterestingAlloca(AllocaInst &AI) {
915   auto PreviouslySeenAllocaInfo = ProcessedAllocas.find(&AI);
916 
917   if (PreviouslySeenAllocaInfo != ProcessedAllocas.end())
918     return PreviouslySeenAllocaInfo->getSecond();
919 
920   bool IsInteresting =
921       (AI.getAllocatedType()->isSized() &&
922        // alloca() may be called with 0 size, ignore it.
923        ((!AI.isStaticAlloca()) || getAllocaSizeInBytes(&AI) > 0) &&
924        // We are only interested in allocas not promotable to registers.
925        // Promotable allocas are common under -O0.
926        (!ClSkipPromotableAllocas || !isAllocaPromotable(&AI)) &&
927        // inalloca allocas are not treated as static, and we don't want
928        // dynamic alloca instrumentation for them as well.
929        !AI.isUsedWithInAlloca());
930 
931   ProcessedAllocas[&AI] = IsInteresting;
932   return IsInteresting;
933 }
934 
935 /// If I is an interesting memory access, return the PointerOperand
936 /// and set IsWrite/Alignment. Otherwise return nullptr.
isInterestingMemoryAccess(Instruction * I,bool * IsWrite,uint64_t * TypeSize,unsigned * Alignment)937 Value *AddressSanitizer::isInterestingMemoryAccess(Instruction *I,
938                                                    bool *IsWrite,
939                                                    uint64_t *TypeSize,
940                                                    unsigned *Alignment) {
941   // Skip memory accesses inserted by another instrumentation.
942   if (I->getMetadata("nosanitize")) return nullptr;
943 
944   Value *PtrOperand = nullptr;
945   const DataLayout &DL = I->getModule()->getDataLayout();
946   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
947     if (!ClInstrumentReads) return nullptr;
948     *IsWrite = false;
949     *TypeSize = DL.getTypeStoreSizeInBits(LI->getType());
950     *Alignment = LI->getAlignment();
951     PtrOperand = LI->getPointerOperand();
952   } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
953     if (!ClInstrumentWrites) return nullptr;
954     *IsWrite = true;
955     *TypeSize = DL.getTypeStoreSizeInBits(SI->getValueOperand()->getType());
956     *Alignment = SI->getAlignment();
957     PtrOperand = SI->getPointerOperand();
958   } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
959     if (!ClInstrumentAtomics) return nullptr;
960     *IsWrite = true;
961     *TypeSize = DL.getTypeStoreSizeInBits(RMW->getValOperand()->getType());
962     *Alignment = 0;
963     PtrOperand = RMW->getPointerOperand();
964   } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
965     if (!ClInstrumentAtomics) return nullptr;
966     *IsWrite = true;
967     *TypeSize = DL.getTypeStoreSizeInBits(XCHG->getCompareOperand()->getType());
968     *Alignment = 0;
969     PtrOperand = XCHG->getPointerOperand();
970   }
971 
972   // Do not instrument acesses from different address spaces; we cannot deal
973   // with them.
974   if (PtrOperand) {
975     Type *PtrTy = cast<PointerType>(PtrOperand->getType()->getScalarType());
976     if (PtrTy->getPointerAddressSpace() != 0)
977       return nullptr;
978   }
979 
980   // Treat memory accesses to promotable allocas as non-interesting since they
981   // will not cause memory violations. This greatly speeds up the instrumented
982   // executable at -O0.
983   if (ClSkipPromotableAllocas)
984     if (auto AI = dyn_cast_or_null<AllocaInst>(PtrOperand))
985       return isInterestingAlloca(*AI) ? AI : nullptr;
986 
987   return PtrOperand;
988 }
989 
isPointerOperand(Value * V)990 static bool isPointerOperand(Value *V) {
991   return V->getType()->isPointerTy() || isa<PtrToIntInst>(V);
992 }
993 
994 // This is a rough heuristic; it may cause both false positives and
995 // false negatives. The proper implementation requires cooperation with
996 // the frontend.
isInterestingPointerComparisonOrSubtraction(Instruction * I)997 static bool isInterestingPointerComparisonOrSubtraction(Instruction *I) {
998   if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) {
999     if (!Cmp->isRelational()) return false;
1000   } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
1001     if (BO->getOpcode() != Instruction::Sub) return false;
1002   } else {
1003     return false;
1004   }
1005   return isPointerOperand(I->getOperand(0)) &&
1006          isPointerOperand(I->getOperand(1));
1007 }
1008 
GlobalIsLinkerInitialized(GlobalVariable * G)1009 bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) {
1010   // If a global variable does not have dynamic initialization we don't
1011   // have to instrument it.  However, if a global does not have initializer
1012   // at all, we assume it has dynamic initializer (in other TU).
1013   return G->hasInitializer() && !GlobalsMD.get(G).IsDynInit;
1014 }
1015 
instrumentPointerComparisonOrSubtraction(Instruction * I)1016 void AddressSanitizer::instrumentPointerComparisonOrSubtraction(
1017     Instruction *I) {
1018   IRBuilder<> IRB(I);
1019   Function *F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction;
1020   Value *Param[2] = {I->getOperand(0), I->getOperand(1)};
1021   for (Value *&i : Param) {
1022     if (i->getType()->isPointerTy())
1023       i = IRB.CreatePointerCast(i, IntptrTy);
1024   }
1025   IRB.CreateCall(F, Param);
1026 }
1027 
instrumentMop(ObjectSizeOffsetVisitor & ObjSizeVis,Instruction * I,bool UseCalls,const DataLayout & DL)1028 void AddressSanitizer::instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis,
1029                                      Instruction *I, bool UseCalls,
1030                                      const DataLayout &DL) {
1031   bool IsWrite = false;
1032   unsigned Alignment = 0;
1033   uint64_t TypeSize = 0;
1034   Value *Addr = isInterestingMemoryAccess(I, &IsWrite, &TypeSize, &Alignment);
1035   assert(Addr);
1036 
1037   // Optimization experiments.
1038   // The experiments can be used to evaluate potential optimizations that remove
1039   // instrumentation (assess false negatives). Instead of completely removing
1040   // some instrumentation, you set Exp to a non-zero value (mask of optimization
1041   // experiments that want to remove instrumentation of this instruction).
1042   // If Exp is non-zero, this pass will emit special calls into runtime
1043   // (e.g. __asan_report_exp_load1 instead of __asan_report_load1). These calls
1044   // make runtime terminate the program in a special way (with a different
1045   // exit status). Then you run the new compiler on a buggy corpus, collect
1046   // the special terminations (ideally, you don't see them at all -- no false
1047   // negatives) and make the decision on the optimization.
1048   uint32_t Exp = ClForceExperiment;
1049 
1050   if (ClOpt && ClOptGlobals) {
1051     // If initialization order checking is disabled, a simple access to a
1052     // dynamically initialized global is always valid.
1053     GlobalVariable *G = dyn_cast<GlobalVariable>(GetUnderlyingObject(Addr, DL));
1054     if (G && (!ClInitializers || GlobalIsLinkerInitialized(G)) &&
1055         isSafeAccess(ObjSizeVis, Addr, TypeSize)) {
1056       NumOptimizedAccessesToGlobalVar++;
1057       return;
1058     }
1059   }
1060 
1061   if (ClOpt && ClOptStack) {
1062     // A direct inbounds access to a stack variable is always valid.
1063     if (isa<AllocaInst>(GetUnderlyingObject(Addr, DL)) &&
1064         isSafeAccess(ObjSizeVis, Addr, TypeSize)) {
1065       NumOptimizedAccessesToStackVar++;
1066       return;
1067     }
1068   }
1069 
1070   if (IsWrite)
1071     NumInstrumentedWrites++;
1072   else
1073     NumInstrumentedReads++;
1074 
1075   unsigned Granularity = 1 << Mapping.Scale;
1076   // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check
1077   // if the data is properly aligned.
1078   if ((TypeSize == 8 || TypeSize == 16 || TypeSize == 32 || TypeSize == 64 ||
1079        TypeSize == 128) &&
1080       (Alignment >= Granularity || Alignment == 0 || Alignment >= TypeSize / 8))
1081     return instrumentAddress(I, I, Addr, TypeSize, IsWrite, nullptr, UseCalls,
1082                              Exp);
1083   instrumentUnusualSizeOrAlignment(I, Addr, TypeSize, IsWrite, nullptr,
1084                                    UseCalls, Exp);
1085 }
1086 
generateCrashCode(Instruction * InsertBefore,Value * Addr,bool IsWrite,size_t AccessSizeIndex,Value * SizeArgument,uint32_t Exp)1087 Instruction *AddressSanitizer::generateCrashCode(Instruction *InsertBefore,
1088                                                  Value *Addr, bool IsWrite,
1089                                                  size_t AccessSizeIndex,
1090                                                  Value *SizeArgument,
1091                                                  uint32_t Exp) {
1092   IRBuilder<> IRB(InsertBefore);
1093   Value *ExpVal = Exp == 0 ? nullptr : ConstantInt::get(IRB.getInt32Ty(), Exp);
1094   CallInst *Call = nullptr;
1095   if (SizeArgument) {
1096     if (Exp == 0)
1097       Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][0],
1098                             {Addr, SizeArgument});
1099     else
1100       Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][1],
1101                             {Addr, SizeArgument, ExpVal});
1102   } else {
1103     if (Exp == 0)
1104       Call =
1105           IRB.CreateCall(AsanErrorCallback[IsWrite][0][AccessSizeIndex], Addr);
1106     else
1107       Call = IRB.CreateCall(AsanErrorCallback[IsWrite][1][AccessSizeIndex],
1108                             {Addr, ExpVal});
1109   }
1110 
1111   // We don't do Call->setDoesNotReturn() because the BB already has
1112   // UnreachableInst at the end.
1113   // This EmptyAsm is required to avoid callback merge.
1114   IRB.CreateCall(EmptyAsm, {});
1115   return Call;
1116 }
1117 
createSlowPathCmp(IRBuilder<> & IRB,Value * AddrLong,Value * ShadowValue,uint32_t TypeSize)1118 Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
1119                                            Value *ShadowValue,
1120                                            uint32_t TypeSize) {
1121   size_t Granularity = static_cast<size_t>(1) << Mapping.Scale;
1122   // Addr & (Granularity - 1)
1123   Value *LastAccessedByte =
1124       IRB.CreateAnd(AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
1125   // (Addr & (Granularity - 1)) + size - 1
1126   if (TypeSize / 8 > 1)
1127     LastAccessedByte = IRB.CreateAdd(
1128         LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
1129   // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
1130   LastAccessedByte =
1131       IRB.CreateIntCast(LastAccessedByte, ShadowValue->getType(), false);
1132   // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
1133   return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
1134 }
1135 
instrumentAddress(Instruction * OrigIns,Instruction * InsertBefore,Value * Addr,uint32_t TypeSize,bool IsWrite,Value * SizeArgument,bool UseCalls,uint32_t Exp)1136 void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
1137                                          Instruction *InsertBefore, Value *Addr,
1138                                          uint32_t TypeSize, bool IsWrite,
1139                                          Value *SizeArgument, bool UseCalls,
1140                                          uint32_t Exp) {
1141   IRBuilder<> IRB(InsertBefore);
1142   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1143   size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
1144 
1145   if (UseCalls) {
1146     if (Exp == 0)
1147       IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex],
1148                      AddrLong);
1149     else
1150       IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][1][AccessSizeIndex],
1151                      {AddrLong, ConstantInt::get(IRB.getInt32Ty(), Exp)});
1152     return;
1153   }
1154 
1155   Type *ShadowTy =
1156       IntegerType::get(*C, std::max(8U, TypeSize >> Mapping.Scale));
1157   Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
1158   Value *ShadowPtr = memToShadow(AddrLong, IRB);
1159   Value *CmpVal = Constant::getNullValue(ShadowTy);
1160   Value *ShadowValue =
1161       IRB.CreateLoad(IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
1162 
1163   Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
1164   size_t Granularity = 1ULL << Mapping.Scale;
1165   TerminatorInst *CrashTerm = nullptr;
1166 
1167   if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
1168     // We use branch weights for the slow path check, to indicate that the slow
1169     // path is rarely taken. This seems to be the case for SPEC benchmarks.
1170     TerminatorInst *CheckTerm = SplitBlockAndInsertIfThen(
1171         Cmp, InsertBefore, false, MDBuilder(*C).createBranchWeights(1, 100000));
1172     assert(cast<BranchInst>(CheckTerm)->isUnconditional());
1173     BasicBlock *NextBB = CheckTerm->getSuccessor(0);
1174     IRB.SetInsertPoint(CheckTerm);
1175     Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
1176     if (Recover) {
1177       CrashTerm = SplitBlockAndInsertIfThen(Cmp2, CheckTerm, false);
1178     } else {
1179       BasicBlock *CrashBlock =
1180         BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
1181       CrashTerm = new UnreachableInst(*C, CrashBlock);
1182       BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
1183       ReplaceInstWithInst(CheckTerm, NewTerm);
1184     }
1185   } else {
1186     CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, !Recover);
1187   }
1188 
1189   Instruction *Crash = generateCrashCode(CrashTerm, AddrLong, IsWrite,
1190                                          AccessSizeIndex, SizeArgument, Exp);
1191   Crash->setDebugLoc(OrigIns->getDebugLoc());
1192 }
1193 
1194 // Instrument unusual size or unusual alignment.
1195 // We can not do it with a single check, so we do 1-byte check for the first
1196 // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able
1197 // to report the actual access size.
instrumentUnusualSizeOrAlignment(Instruction * I,Value * Addr,uint32_t TypeSize,bool IsWrite,Value * SizeArgument,bool UseCalls,uint32_t Exp)1198 void AddressSanitizer::instrumentUnusualSizeOrAlignment(
1199     Instruction *I, Value *Addr, uint32_t TypeSize, bool IsWrite,
1200     Value *SizeArgument, bool UseCalls, uint32_t Exp) {
1201   IRBuilder<> IRB(I);
1202   Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8);
1203   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
1204   if (UseCalls) {
1205     if (Exp == 0)
1206       IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][0],
1207                      {AddrLong, Size});
1208     else
1209       IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][1],
1210                      {AddrLong, Size, ConstantInt::get(IRB.getInt32Ty(), Exp)});
1211   } else {
1212     Value *LastByte = IRB.CreateIntToPtr(
1213         IRB.CreateAdd(AddrLong, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)),
1214         Addr->getType());
1215     instrumentAddress(I, I, Addr, 8, IsWrite, Size, false, Exp);
1216     instrumentAddress(I, I, LastByte, 8, IsWrite, Size, false, Exp);
1217   }
1218 }
1219 
poisonOneInitializer(Function & GlobalInit,GlobalValue * ModuleName)1220 void AddressSanitizerModule::poisonOneInitializer(Function &GlobalInit,
1221                                                   GlobalValue *ModuleName) {
1222   // Set up the arguments to our poison/unpoison functions.
1223   IRBuilder<> IRB(&GlobalInit.front(),
1224                   GlobalInit.front().getFirstInsertionPt());
1225 
1226   // Add a call to poison all external globals before the given function starts.
1227   Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy);
1228   IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr);
1229 
1230   // Add calls to unpoison all globals before each return instruction.
1231   for (auto &BB : GlobalInit.getBasicBlockList())
1232     if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
1233       CallInst::Create(AsanUnpoisonGlobals, "", RI);
1234 }
1235 
createInitializerPoisonCalls(Module & M,GlobalValue * ModuleName)1236 void AddressSanitizerModule::createInitializerPoisonCalls(
1237     Module &M, GlobalValue *ModuleName) {
1238   GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
1239 
1240   ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
1241   for (Use &OP : CA->operands()) {
1242     if (isa<ConstantAggregateZero>(OP)) continue;
1243     ConstantStruct *CS = cast<ConstantStruct>(OP);
1244 
1245     // Must have a function or null ptr.
1246     if (Function *F = dyn_cast<Function>(CS->getOperand(1))) {
1247       if (F->getName() == kAsanModuleCtorName) continue;
1248       ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0));
1249       // Don't instrument CTORs that will run before asan.module_ctor.
1250       if (Priority->getLimitedValue() <= kAsanCtorAndDtorPriority) continue;
1251       poisonOneInitializer(*F, ModuleName);
1252     }
1253   }
1254 }
1255 
ShouldInstrumentGlobal(GlobalVariable * G)1256 bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
1257   Type *Ty = G->getValueType();
1258   DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
1259 
1260   if (GlobalsMD.get(G).IsBlacklisted) return false;
1261   if (!Ty->isSized()) return false;
1262   if (!G->hasInitializer()) return false;
1263   if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
1264   // Touch only those globals that will not be defined in other modules.
1265   // Don't handle ODR linkage types and COMDATs since other modules may be built
1266   // without ASan.
1267   if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
1268       G->getLinkage() != GlobalVariable::PrivateLinkage &&
1269       G->getLinkage() != GlobalVariable::InternalLinkage)
1270     return false;
1271   if (G->hasComdat()) return false;
1272   // Two problems with thread-locals:
1273   //   - The address of the main thread's copy can't be computed at link-time.
1274   //   - Need to poison all copies, not just the main thread's one.
1275   if (G->isThreadLocal()) return false;
1276   // For now, just ignore this Global if the alignment is large.
1277   if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false;
1278 
1279   if (G->hasSection()) {
1280     StringRef Section = G->getSection();
1281 
1282     // Globals from llvm.metadata aren't emitted, do not instrument them.
1283     if (Section == "llvm.metadata") return false;
1284     // Do not instrument globals from special LLVM sections.
1285     if (Section.find("__llvm") != StringRef::npos || Section.find("__LLVM") != StringRef::npos) return false;
1286 
1287     // Do not instrument function pointers to initialization and termination
1288     // routines: dynamic linker will not properly handle redzones.
1289     if (Section.startswith(".preinit_array") ||
1290         Section.startswith(".init_array") ||
1291         Section.startswith(".fini_array")) {
1292       return false;
1293     }
1294 
1295     // Callbacks put into the CRT initializer/terminator sections
1296     // should not be instrumented.
1297     // See https://code.google.com/p/address-sanitizer/issues/detail?id=305
1298     // and http://msdn.microsoft.com/en-US/en-en/library/bb918180(v=vs.120).aspx
1299     if (Section.startswith(".CRT")) {
1300       DEBUG(dbgs() << "Ignoring a global initializer callback: " << *G << "\n");
1301       return false;
1302     }
1303 
1304     if (TargetTriple.isOSBinFormatMachO()) {
1305       StringRef ParsedSegment, ParsedSection;
1306       unsigned TAA = 0, StubSize = 0;
1307       bool TAAParsed;
1308       std::string ErrorCode = MCSectionMachO::ParseSectionSpecifier(
1309           Section, ParsedSegment, ParsedSection, TAA, TAAParsed, StubSize);
1310       assert(ErrorCode.empty() && "Invalid section specifier.");
1311 
1312       // Ignore the globals from the __OBJC section. The ObjC runtime assumes
1313       // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
1314       // them.
1315       if (ParsedSegment == "__OBJC" ||
1316           (ParsedSegment == "__DATA" && ParsedSection.startswith("__objc_"))) {
1317         DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n");
1318         return false;
1319       }
1320       // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
1321       // Constant CFString instances are compiled in the following way:
1322       //  -- the string buffer is emitted into
1323       //     __TEXT,__cstring,cstring_literals
1324       //  -- the constant NSConstantString structure referencing that buffer
1325       //     is placed into __DATA,__cfstring
1326       // Therefore there's no point in placing redzones into __DATA,__cfstring.
1327       // Moreover, it causes the linker to crash on OS X 10.7
1328       if (ParsedSegment == "__DATA" && ParsedSection == "__cfstring") {
1329         DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n");
1330         return false;
1331       }
1332       // The linker merges the contents of cstring_literals and removes the
1333       // trailing zeroes.
1334       if (ParsedSegment == "__TEXT" && (TAA & MachO::S_CSTRING_LITERALS)) {
1335         DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n");
1336         return false;
1337       }
1338     }
1339   }
1340 
1341   return true;
1342 }
1343 
1344 // On Mach-O platforms, we emit global metadata in a separate section of the
1345 // binary in order to allow the linker to properly dead strip. This is only
1346 // supported on recent versions of ld64.
ShouldUseMachOGlobalsSection() const1347 bool AddressSanitizerModule::ShouldUseMachOGlobalsSection() const {
1348   if (!ClUseMachOGlobalsSection)
1349     return false;
1350 
1351   if (!TargetTriple.isOSBinFormatMachO())
1352     return false;
1353 
1354   if (TargetTriple.isMacOSX() && !TargetTriple.isMacOSXVersionLT(10, 11))
1355     return true;
1356   if (TargetTriple.isiOS() /* or tvOS */ && !TargetTriple.isOSVersionLT(9))
1357     return true;
1358   if (TargetTriple.isWatchOS() && !TargetTriple.isOSVersionLT(2))
1359     return true;
1360 
1361   return false;
1362 }
1363 
initializeCallbacks(Module & M)1364 void AddressSanitizerModule::initializeCallbacks(Module &M) {
1365   IRBuilder<> IRB(*C);
1366 
1367   // Declare our poisoning and unpoisoning functions.
1368   AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1369       kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1370   AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
1371   AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1372       kAsanUnpoisonGlobalsName, IRB.getVoidTy(), nullptr));
1373   AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
1374 
1375   // Declare functions that register/unregister globals.
1376   AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1377       kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1378   AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
1379   AsanUnregisterGlobals = checkSanitizerInterfaceFunction(
1380       M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(),
1381                             IntptrTy, IntptrTy, nullptr));
1382   AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
1383 
1384   // Declare the functions that find globals in a shared object and then invoke
1385   // the (un)register function on them.
1386   AsanRegisterImageGlobals = checkSanitizerInterfaceFunction(
1387       M.getOrInsertFunction(kAsanRegisterImageGlobalsName,
1388       IRB.getVoidTy(), IntptrTy, nullptr));
1389   AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage);
1390 
1391   AsanUnregisterImageGlobals = checkSanitizerInterfaceFunction(
1392       M.getOrInsertFunction(kAsanUnregisterImageGlobalsName,
1393       IRB.getVoidTy(), IntptrTy, nullptr));
1394   AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
1395 }
1396 
1397 // This function replaces all global variables with new variables that have
1398 // trailing redzones. It also creates a function that poisons
1399 // redzones and inserts this function into llvm.global_ctors.
InstrumentGlobals(IRBuilder<> & IRB,Module & M)1400 bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) {
1401   GlobalsMD.init(M);
1402 
1403   SmallVector<GlobalVariable *, 16> GlobalsToChange;
1404 
1405   for (auto &G : M.globals()) {
1406     if (ShouldInstrumentGlobal(&G)) GlobalsToChange.push_back(&G);
1407   }
1408 
1409   size_t n = GlobalsToChange.size();
1410   if (n == 0) return false;
1411 
1412   // A global is described by a structure
1413   //   size_t beg;
1414   //   size_t size;
1415   //   size_t size_with_redzone;
1416   //   const char *name;
1417   //   const char *module_name;
1418   //   size_t has_dynamic_init;
1419   //   void *source_location;
1420   //   size_t odr_indicator;
1421   // We initialize an array of such structures and pass it to a run-time call.
1422   StructType *GlobalStructTy =
1423       StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy,
1424                       IntptrTy, IntptrTy, IntptrTy, nullptr);
1425   SmallVector<Constant *, 16> Initializers(n);
1426 
1427   bool HasDynamicallyInitializedGlobals = false;
1428 
1429   // We shouldn't merge same module names, as this string serves as unique
1430   // module ID in runtime.
1431   GlobalVariable *ModuleName = createPrivateGlobalForString(
1432       M, M.getModuleIdentifier(), /*AllowMerging*/ false);
1433 
1434   auto &DL = M.getDataLayout();
1435   for (size_t i = 0; i < n; i++) {
1436     static const uint64_t kMaxGlobalRedzone = 1 << 18;
1437     GlobalVariable *G = GlobalsToChange[i];
1438 
1439     auto MD = GlobalsMD.get(G);
1440     StringRef NameForGlobal = G->getName();
1441     // Create string holding the global name (use global name from metadata
1442     // if it's available, otherwise just write the name of global variable).
1443     GlobalVariable *Name = createPrivateGlobalForString(
1444         M, MD.Name.empty() ? NameForGlobal : MD.Name,
1445         /*AllowMerging*/ true);
1446 
1447     Type *Ty = G->getValueType();
1448     uint64_t SizeInBytes = DL.getTypeAllocSize(Ty);
1449     uint64_t MinRZ = MinRedzoneSizeForGlobal();
1450     // MinRZ <= RZ <= kMaxGlobalRedzone
1451     // and trying to make RZ to be ~ 1/4 of SizeInBytes.
1452     uint64_t RZ = std::max(
1453         MinRZ, std::min(kMaxGlobalRedzone, (SizeInBytes / MinRZ / 4) * MinRZ));
1454     uint64_t RightRedzoneSize = RZ;
1455     // Round up to MinRZ
1456     if (SizeInBytes % MinRZ) RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ);
1457     assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0);
1458     Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
1459 
1460     StructType *NewTy = StructType::get(Ty, RightRedZoneTy, nullptr);
1461     Constant *NewInitializer =
1462         ConstantStruct::get(NewTy, G->getInitializer(),
1463                             Constant::getNullValue(RightRedZoneTy), nullptr);
1464 
1465     // Create a new global variable with enough space for a redzone.
1466     GlobalValue::LinkageTypes Linkage = G->getLinkage();
1467     if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage)
1468       Linkage = GlobalValue::InternalLinkage;
1469     GlobalVariable *NewGlobal =
1470         new GlobalVariable(M, NewTy, G->isConstant(), Linkage, NewInitializer,
1471                            "", G, G->getThreadLocalMode());
1472     NewGlobal->copyAttributesFrom(G);
1473     NewGlobal->setAlignment(MinRZ);
1474 
1475     Value *Indices2[2];
1476     Indices2[0] = IRB.getInt32(0);
1477     Indices2[1] = IRB.getInt32(0);
1478 
1479     G->replaceAllUsesWith(
1480         ConstantExpr::getGetElementPtr(NewTy, NewGlobal, Indices2, true));
1481     NewGlobal->takeName(G);
1482     G->eraseFromParent();
1483 
1484     Constant *SourceLoc;
1485     if (!MD.SourceLoc.empty()) {
1486       auto SourceLocGlobal = createPrivateGlobalForSourceLoc(M, MD.SourceLoc);
1487       SourceLoc = ConstantExpr::getPointerCast(SourceLocGlobal, IntptrTy);
1488     } else {
1489       SourceLoc = ConstantInt::get(IntptrTy, 0);
1490     }
1491 
1492     Constant *ODRIndicator = ConstantExpr::getNullValue(IRB.getInt8PtrTy());
1493     GlobalValue *InstrumentedGlobal = NewGlobal;
1494 
1495     bool CanUsePrivateAliases = TargetTriple.isOSBinFormatELF();
1496     if (CanUsePrivateAliases && ClUsePrivateAliasForGlobals) {
1497       // Create local alias for NewGlobal to avoid crash on ODR between
1498       // instrumented and non-instrumented libraries.
1499       auto *GA = GlobalAlias::create(GlobalValue::InternalLinkage,
1500                                      NameForGlobal + M.getName(), NewGlobal);
1501 
1502       // With local aliases, we need to provide another externally visible
1503       // symbol __odr_asan_XXX to detect ODR violation.
1504       auto *ODRIndicatorSym =
1505           new GlobalVariable(M, IRB.getInt8Ty(), false, Linkage,
1506                              Constant::getNullValue(IRB.getInt8Ty()),
1507                              kODRGenPrefix + NameForGlobal, nullptr,
1508                              NewGlobal->getThreadLocalMode());
1509 
1510       // Set meaningful attributes for indicator symbol.
1511       ODRIndicatorSym->setVisibility(NewGlobal->getVisibility());
1512       ODRIndicatorSym->setDLLStorageClass(NewGlobal->getDLLStorageClass());
1513       ODRIndicatorSym->setAlignment(1);
1514       ODRIndicator = ODRIndicatorSym;
1515       InstrumentedGlobal = GA;
1516     }
1517 
1518     Initializers[i] = ConstantStruct::get(
1519         GlobalStructTy,
1520         ConstantExpr::getPointerCast(InstrumentedGlobal, IntptrTy),
1521         ConstantInt::get(IntptrTy, SizeInBytes),
1522         ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
1523         ConstantExpr::getPointerCast(Name, IntptrTy),
1524         ConstantExpr::getPointerCast(ModuleName, IntptrTy),
1525         ConstantInt::get(IntptrTy, MD.IsDynInit), SourceLoc,
1526         ConstantExpr::getPointerCast(ODRIndicator, IntptrTy), nullptr);
1527 
1528     if (ClInitializers && MD.IsDynInit) HasDynamicallyInitializedGlobals = true;
1529 
1530     DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
1531   }
1532 
1533 
1534   GlobalVariable *AllGlobals = nullptr;
1535   GlobalVariable *RegisteredFlag = nullptr;
1536 
1537   // On recent Mach-O platforms, we emit the global metadata in a way that
1538   // allows the linker to properly strip dead globals.
1539   if (ShouldUseMachOGlobalsSection()) {
1540     // RegisteredFlag serves two purposes. First, we can pass it to dladdr()
1541     // to look up the loaded image that contains it. Second, we can store in it
1542     // whether registration has already occurred, to prevent duplicate
1543     // registration.
1544     //
1545     // Common linkage allows us to coalesce needles defined in each object
1546     // file so that there's only one per shared library.
1547     RegisteredFlag = new GlobalVariable(
1548         M, IntptrTy, false, GlobalVariable::CommonLinkage,
1549         ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
1550 
1551     // We also emit a structure which binds the liveness of the global
1552     // variable to the metadata struct.
1553     StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy, nullptr);
1554 
1555     for (size_t i = 0; i < n; i++) {
1556       GlobalVariable *Metadata = new GlobalVariable(
1557           M, GlobalStructTy, false, GlobalVariable::InternalLinkage,
1558           Initializers[i], "");
1559       Metadata->setSection("__DATA,__asan_globals,regular");
1560       Metadata->setAlignment(1); // don't leave padding in between
1561 
1562       auto LivenessBinder = ConstantStruct::get(LivenessTy,
1563           Initializers[i]->getAggregateElement(0u),
1564           ConstantExpr::getPointerCast(Metadata, IntptrTy),
1565           nullptr);
1566       GlobalVariable *Liveness = new GlobalVariable(
1567           M, LivenessTy, false, GlobalVariable::InternalLinkage,
1568           LivenessBinder, "");
1569       Liveness->setSection("__DATA,__asan_liveness,regular,live_support");
1570     }
1571   } else {
1572     // On all other platfoms, we just emit an array of global metadata
1573     // structures.
1574     ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
1575     AllGlobals = new GlobalVariable(
1576         M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage,
1577         ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
1578   }
1579 
1580   // Create calls for poisoning before initializers run and unpoisoning after.
1581   if (HasDynamicallyInitializedGlobals)
1582     createInitializerPoisonCalls(M, ModuleName);
1583 
1584   // Create a call to register the globals with the runtime.
1585   if (ShouldUseMachOGlobalsSection()) {
1586     IRB.CreateCall(AsanRegisterImageGlobals,
1587                    {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1588   } else {
1589     IRB.CreateCall(AsanRegisterGlobals,
1590                    {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1591                     ConstantInt::get(IntptrTy, n)});
1592   }
1593 
1594   // We also need to unregister globals at the end, e.g., when a shared library
1595   // gets closed.
1596   Function *AsanDtorFunction =
1597       Function::Create(FunctionType::get(Type::getVoidTy(*C), false),
1598                        GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
1599   BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
1600   IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
1601 
1602   if (ShouldUseMachOGlobalsSection()) {
1603     IRB_Dtor.CreateCall(AsanUnregisterImageGlobals,
1604                         {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
1605   } else {
1606     IRB_Dtor.CreateCall(AsanUnregisterGlobals,
1607                         {IRB.CreatePointerCast(AllGlobals, IntptrTy),
1608                          ConstantInt::get(IntptrTy, n)});
1609   }
1610 
1611   appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority);
1612 
1613   DEBUG(dbgs() << M);
1614   return true;
1615 }
1616 
runOnModule(Module & M)1617 bool AddressSanitizerModule::runOnModule(Module &M) {
1618   C = &(M.getContext());
1619   int LongSize = M.getDataLayout().getPointerSizeInBits();
1620   IntptrTy = Type::getIntNTy(*C, LongSize);
1621   TargetTriple = Triple(M.getTargetTriple());
1622   Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel);
1623   initializeCallbacks(M);
1624 
1625   bool Changed = false;
1626 
1627   // TODO(glider): temporarily disabled globals instrumentation for KASan.
1628   if (ClGlobals && !CompileKernel) {
1629     Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
1630     assert(CtorFunc);
1631     IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
1632     Changed |= InstrumentGlobals(IRB, M);
1633   }
1634 
1635   return Changed;
1636 }
1637 
initializeCallbacks(Module & M)1638 void AddressSanitizer::initializeCallbacks(Module &M) {
1639   IRBuilder<> IRB(*C);
1640   // Create __asan_report* callbacks.
1641   // IsWrite, TypeSize and Exp are encoded in the function name.
1642   for (int Exp = 0; Exp < 2; Exp++) {
1643     for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
1644       const std::string TypeStr = AccessIsWrite ? "store" : "load";
1645       const std::string ExpStr = Exp ? "exp_" : "";
1646       const std::string SuffixStr = CompileKernel ? "N" : "_n";
1647       const std::string EndingStr = Recover ? "_noabort" : "";
1648       Type *ExpType = Exp ? Type::getInt32Ty(*C) : nullptr;
1649       AsanErrorCallbackSized[AccessIsWrite][Exp] =
1650           checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1651               kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr + EndingStr,
1652               IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
1653       AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] =
1654           checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1655               ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
1656               IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
1657       for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
1658            AccessSizeIndex++) {
1659         const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
1660         AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1661             checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1662                 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
1663                 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
1664         AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1665             checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1666                 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
1667                 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
1668       }
1669     }
1670   }
1671 
1672   const std::string MemIntrinCallbackPrefix =
1673       CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
1674   AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1675       MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
1676       IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
1677   AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1678       MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
1679       IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
1680   AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1681       MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(),
1682       IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy, nullptr));
1683 
1684   AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction(
1685       M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy(), nullptr));
1686 
1687   AsanPtrCmpFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1688       kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1689   AsanPtrSubFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1690       kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1691   // We insert an empty inline asm after __asan_report* to avoid callback merge.
1692   EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
1693                             StringRef(""), StringRef(""),
1694                             /*hasSideEffects=*/true);
1695 }
1696 
1697 // virtual
doInitialization(Module & M)1698 bool AddressSanitizer::doInitialization(Module &M) {
1699   // Initialize the private fields. No one has accessed them before.
1700 
1701   GlobalsMD.init(M);
1702 
1703   C = &(M.getContext());
1704   LongSize = M.getDataLayout().getPointerSizeInBits();
1705   IntptrTy = Type::getIntNTy(*C, LongSize);
1706   TargetTriple = Triple(M.getTargetTriple());
1707 
1708   if (!CompileKernel) {
1709     std::tie(AsanCtorFunction, AsanInitFunction) =
1710         createSanitizerCtorAndInitFunctions(
1711             M, kAsanModuleCtorName, kAsanInitName,
1712             /*InitArgTypes=*/{}, /*InitArgs=*/{}, kAsanVersionCheckName);
1713     appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority);
1714   }
1715   Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel);
1716   return true;
1717 }
1718 
doFinalization(Module & M)1719 bool AddressSanitizer::doFinalization(Module &M) {
1720   GlobalsMD.reset();
1721   return false;
1722 }
1723 
maybeInsertAsanInitAtFunctionEntry(Function & F)1724 bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
1725   // For each NSObject descendant having a +load method, this method is invoked
1726   // by the ObjC runtime before any of the static constructors is called.
1727   // Therefore we need to instrument such methods with a call to __asan_init
1728   // at the beginning in order to initialize our runtime before any access to
1729   // the shadow memory.
1730   // We cannot just ignore these methods, because they may call other
1731   // instrumented functions.
1732   if (F.getName().find(" load]") != std::string::npos) {
1733     IRBuilder<> IRB(&F.front(), F.front().begin());
1734     IRB.CreateCall(AsanInitFunction, {});
1735     return true;
1736   }
1737   return false;
1738 }
1739 
markEscapedLocalAllocas(Function & F)1740 void AddressSanitizer::markEscapedLocalAllocas(Function &F) {
1741   // Find the one possible call to llvm.localescape and pre-mark allocas passed
1742   // to it as uninteresting. This assumes we haven't started processing allocas
1743   // yet. This check is done up front because iterating the use list in
1744   // isInterestingAlloca would be algorithmically slower.
1745   assert(ProcessedAllocas.empty() && "must process localescape before allocas");
1746 
1747   // Try to get the declaration of llvm.localescape. If it's not in the module,
1748   // we can exit early.
1749   if (!F.getParent()->getFunction("llvm.localescape")) return;
1750 
1751   // Look for a call to llvm.localescape call in the entry block. It can't be in
1752   // any other block.
1753   for (Instruction &I : F.getEntryBlock()) {
1754     IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
1755     if (II && II->getIntrinsicID() == Intrinsic::localescape) {
1756       // We found a call. Mark all the allocas passed in as uninteresting.
1757       for (Value *Arg : II->arg_operands()) {
1758         AllocaInst *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts());
1759         assert(AI && AI->isStaticAlloca() &&
1760                "non-static alloca arg to localescape");
1761         ProcessedAllocas[AI] = false;
1762       }
1763       break;
1764     }
1765   }
1766 }
1767 
runOnFunction(Function & F)1768 bool AddressSanitizer::runOnFunction(Function &F) {
1769   if (&F == AsanCtorFunction) return false;
1770   if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false;
1771   DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
1772   initializeCallbacks(*F.getParent());
1773 
1774   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1775 
1776   // If needed, insert __asan_init before checking for SanitizeAddress attr.
1777   maybeInsertAsanInitAtFunctionEntry(F);
1778 
1779   if (!F.hasFnAttribute(Attribute::SanitizeAddress)) return false;
1780 
1781   if (!ClDebugFunc.empty() && ClDebugFunc != F.getName()) return false;
1782 
1783   FunctionStateRAII CleanupObj(this);
1784 
1785   // We can't instrument allocas used with llvm.localescape. Only static allocas
1786   // can be passed to that intrinsic.
1787   markEscapedLocalAllocas(F);
1788 
1789   // We want to instrument every address only once per basic block (unless there
1790   // are calls between uses).
1791   SmallSet<Value *, 16> TempsToInstrument;
1792   SmallVector<Instruction *, 16> ToInstrument;
1793   SmallVector<Instruction *, 8> NoReturnCalls;
1794   SmallVector<BasicBlock *, 16> AllBlocks;
1795   SmallVector<Instruction *, 16> PointerComparisonsOrSubtracts;
1796   int NumAllocas = 0;
1797   bool IsWrite;
1798   unsigned Alignment;
1799   uint64_t TypeSize;
1800   const TargetLibraryInfo *TLI =
1801       &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1802 
1803   // Fill the set of memory operations to instrument.
1804   for (auto &BB : F) {
1805     AllBlocks.push_back(&BB);
1806     TempsToInstrument.clear();
1807     int NumInsnsPerBB = 0;
1808     for (auto &Inst : BB) {
1809       if (LooksLikeCodeInBug11395(&Inst)) return false;
1810       if (Value *Addr = isInterestingMemoryAccess(&Inst, &IsWrite, &TypeSize,
1811                                                   &Alignment)) {
1812         if (ClOpt && ClOptSameTemp) {
1813           if (!TempsToInstrument.insert(Addr).second)
1814             continue;  // We've seen this temp in the current BB.
1815         }
1816       } else if (ClInvalidPointerPairs &&
1817                  isInterestingPointerComparisonOrSubtraction(&Inst)) {
1818         PointerComparisonsOrSubtracts.push_back(&Inst);
1819         continue;
1820       } else if (isa<MemIntrinsic>(Inst)) {
1821         // ok, take it.
1822       } else {
1823         if (isa<AllocaInst>(Inst)) NumAllocas++;
1824         CallSite CS(&Inst);
1825         if (CS) {
1826           // A call inside BB.
1827           TempsToInstrument.clear();
1828           if (CS.doesNotReturn()) NoReturnCalls.push_back(CS.getInstruction());
1829         }
1830         if (CallInst *CI = dyn_cast<CallInst>(&Inst))
1831           maybeMarkSanitizerLibraryCallNoBuiltin(CI, TLI);
1832         continue;
1833       }
1834       ToInstrument.push_back(&Inst);
1835       NumInsnsPerBB++;
1836       if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) break;
1837     }
1838   }
1839 
1840   bool UseCalls =
1841       CompileKernel ||
1842       (ClInstrumentationWithCallsThreshold >= 0 &&
1843        ToInstrument.size() > (unsigned)ClInstrumentationWithCallsThreshold);
1844   const DataLayout &DL = F.getParent()->getDataLayout();
1845   ObjectSizeOffsetVisitor ObjSizeVis(DL, TLI, F.getContext(),
1846                                      /*RoundToAlign=*/true);
1847 
1848   // Instrument.
1849   int NumInstrumented = 0;
1850   for (auto Inst : ToInstrument) {
1851     if (ClDebugMin < 0 || ClDebugMax < 0 ||
1852         (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
1853       if (isInterestingMemoryAccess(Inst, &IsWrite, &TypeSize, &Alignment))
1854         instrumentMop(ObjSizeVis, Inst, UseCalls,
1855                       F.getParent()->getDataLayout());
1856       else
1857         instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
1858     }
1859     NumInstrumented++;
1860   }
1861 
1862   FunctionStackPoisoner FSP(F, *this);
1863   bool ChangedStack = FSP.runOnFunction();
1864 
1865   // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
1866   // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
1867   for (auto CI : NoReturnCalls) {
1868     IRBuilder<> IRB(CI);
1869     IRB.CreateCall(AsanHandleNoReturnFunc, {});
1870   }
1871 
1872   for (auto Inst : PointerComparisonsOrSubtracts) {
1873     instrumentPointerComparisonOrSubtraction(Inst);
1874     NumInstrumented++;
1875   }
1876 
1877   bool res = NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
1878 
1879   DEBUG(dbgs() << "ASAN done instrumenting: " << res << " " << F << "\n");
1880 
1881   return res;
1882 }
1883 
1884 // Workaround for bug 11395: we don't want to instrument stack in functions
1885 // with large assembly blobs (32-bit only), otherwise reg alloc may crash.
1886 // FIXME: remove once the bug 11395 is fixed.
LooksLikeCodeInBug11395(Instruction * I)1887 bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
1888   if (LongSize != 32) return false;
1889   CallInst *CI = dyn_cast<CallInst>(I);
1890   if (!CI || !CI->isInlineAsm()) return false;
1891   if (CI->getNumArgOperands() <= 5) return false;
1892   // We have inline assembly with quite a few arguments.
1893   return true;
1894 }
1895 
initializeCallbacks(Module & M)1896 void FunctionStackPoisoner::initializeCallbacks(Module &M) {
1897   IRBuilder<> IRB(*C);
1898   for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
1899     std::string Suffix = itostr(i);
1900     AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction(
1901         M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
1902                               IntptrTy, nullptr));
1903     AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
1904         M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
1905                               IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1906   }
1907   if (ASan.UseAfterScope) {
1908     AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
1909         M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(),
1910                               IntptrTy, IntptrTy, nullptr));
1911     AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
1912         M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(),
1913                               IntptrTy, IntptrTy, nullptr));
1914   }
1915 
1916   AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1917       kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1918   AsanAllocasUnpoisonFunc =
1919       checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1920           kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1921 }
1922 
poisonRedZones(ArrayRef<uint8_t> ShadowBytes,IRBuilder<> & IRB,Value * ShadowBase,bool DoPoison)1923 void FunctionStackPoisoner::poisonRedZones(ArrayRef<uint8_t> ShadowBytes,
1924                                            IRBuilder<> &IRB, Value *ShadowBase,
1925                                            bool DoPoison) {
1926   size_t n = ShadowBytes.size();
1927   size_t i = 0;
1928   // We need to (un)poison n bytes of stack shadow. Poison as many as we can
1929   // using 64-bit stores (if we are on 64-bit arch), then poison the rest
1930   // with 32-bit stores, then with 16-byte stores, then with 8-byte stores.
1931   for (size_t LargeStoreSizeInBytes = ASan.LongSize / 8;
1932        LargeStoreSizeInBytes != 0; LargeStoreSizeInBytes /= 2) {
1933     for (; i + LargeStoreSizeInBytes - 1 < n; i += LargeStoreSizeInBytes) {
1934       uint64_t Val = 0;
1935       for (size_t j = 0; j < LargeStoreSizeInBytes; j++) {
1936         if (F.getParent()->getDataLayout().isLittleEndian())
1937           Val |= (uint64_t)ShadowBytes[i + j] << (8 * j);
1938         else
1939           Val = (Val << 8) | ShadowBytes[i + j];
1940       }
1941       if (!Val) continue;
1942       Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1943       Type *StoreTy = Type::getIntNTy(*C, LargeStoreSizeInBytes * 8);
1944       Value *Poison = ConstantInt::get(StoreTy, DoPoison ? Val : 0);
1945       IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, StoreTy->getPointerTo()));
1946     }
1947   }
1948 }
1949 
1950 // Fake stack allocator (asan_fake_stack.h) has 11 size classes
1951 // for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass
StackMallocSizeClass(uint64_t LocalStackSize)1952 static int StackMallocSizeClass(uint64_t LocalStackSize) {
1953   assert(LocalStackSize <= kMaxStackMallocSize);
1954   uint64_t MaxSize = kMinStackMallocSize;
1955   for (int i = 0;; i++, MaxSize *= 2)
1956     if (LocalStackSize <= MaxSize) return i;
1957   llvm_unreachable("impossible LocalStackSize");
1958 }
1959 
1960 // Set Size bytes starting from ShadowBase to kAsanStackAfterReturnMagic.
1961 // We can not use MemSet intrinsic because it may end up calling the actual
1962 // memset. Size is a multiple of 8.
1963 // Currently this generates 8-byte stores on x86_64; it may be better to
1964 // generate wider stores.
SetShadowToStackAfterReturnInlined(IRBuilder<> & IRB,Value * ShadowBase,int Size)1965 void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined(
1966     IRBuilder<> &IRB, Value *ShadowBase, int Size) {
1967   assert(!(Size % 8));
1968 
1969   // kAsanStackAfterReturnMagic is 0xf5.
1970   const uint64_t kAsanStackAfterReturnMagic64 = 0xf5f5f5f5f5f5f5f5ULL;
1971 
1972   for (int i = 0; i < Size; i += 8) {
1973     Value *p = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
1974     IRB.CreateStore(
1975         ConstantInt::get(IRB.getInt64Ty(), kAsanStackAfterReturnMagic64),
1976         IRB.CreateIntToPtr(p, IRB.getInt64Ty()->getPointerTo()));
1977   }
1978 }
1979 
createPHI(IRBuilder<> & IRB,Value * Cond,Value * ValueIfTrue,Instruction * ThenTerm,Value * ValueIfFalse)1980 PHINode *FunctionStackPoisoner::createPHI(IRBuilder<> &IRB, Value *Cond,
1981                                           Value *ValueIfTrue,
1982                                           Instruction *ThenTerm,
1983                                           Value *ValueIfFalse) {
1984   PHINode *PHI = IRB.CreatePHI(IntptrTy, 2);
1985   BasicBlock *CondBlock = cast<Instruction>(Cond)->getParent();
1986   PHI->addIncoming(ValueIfFalse, CondBlock);
1987   BasicBlock *ThenBlock = ThenTerm->getParent();
1988   PHI->addIncoming(ValueIfTrue, ThenBlock);
1989   return PHI;
1990 }
1991 
createAllocaForLayout(IRBuilder<> & IRB,const ASanStackFrameLayout & L,bool Dynamic)1992 Value *FunctionStackPoisoner::createAllocaForLayout(
1993     IRBuilder<> &IRB, const ASanStackFrameLayout &L, bool Dynamic) {
1994   AllocaInst *Alloca;
1995   if (Dynamic) {
1996     Alloca = IRB.CreateAlloca(IRB.getInt8Ty(),
1997                               ConstantInt::get(IRB.getInt64Ty(), L.FrameSize),
1998                               "MyAlloca");
1999   } else {
2000     Alloca = IRB.CreateAlloca(ArrayType::get(IRB.getInt8Ty(), L.FrameSize),
2001                               nullptr, "MyAlloca");
2002     assert(Alloca->isStaticAlloca());
2003   }
2004   assert((ClRealignStack & (ClRealignStack - 1)) == 0);
2005   size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack);
2006   Alloca->setAlignment(FrameAlignment);
2007   return IRB.CreatePointerCast(Alloca, IntptrTy);
2008 }
2009 
createDynamicAllocasInitStorage()2010 void FunctionStackPoisoner::createDynamicAllocasInitStorage() {
2011   BasicBlock &FirstBB = *F.begin();
2012   IRBuilder<> IRB(dyn_cast<Instruction>(FirstBB.begin()));
2013   DynamicAllocaLayout = IRB.CreateAlloca(IntptrTy, nullptr);
2014   IRB.CreateStore(Constant::getNullValue(IntptrTy), DynamicAllocaLayout);
2015   DynamicAllocaLayout->setAlignment(32);
2016 }
2017 
poisonStack()2018 void FunctionStackPoisoner::poisonStack() {
2019   assert(AllocaVec.size() > 0 || DynamicAllocaVec.size() > 0);
2020 
2021   // Insert poison calls for lifetime intrinsics for alloca.
2022   bool HavePoisonedStaticAllocas = false;
2023   for (const auto &APC : AllocaPoisonCallVec) {
2024     assert(APC.InsBefore);
2025     assert(APC.AI);
2026     assert(ASan.isInterestingAlloca(*APC.AI));
2027     bool IsDynamicAlloca = !(*APC.AI).isStaticAlloca();
2028     if (!ClInstrumentAllocas && IsDynamicAlloca)
2029       continue;
2030 
2031     IRBuilder<> IRB(APC.InsBefore);
2032     poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison);
2033     // Dynamic allocas will be unpoisoned unconditionally below in
2034     // unpoisonDynamicAllocas.
2035     // Flag that we need unpoison static allocas.
2036     HavePoisonedStaticAllocas |= (APC.DoPoison && !IsDynamicAlloca);
2037   }
2038 
2039   if (ClInstrumentAllocas && DynamicAllocaVec.size() > 0) {
2040     // Handle dynamic allocas.
2041     createDynamicAllocasInitStorage();
2042     for (auto &AI : DynamicAllocaVec) handleDynamicAllocaCall(AI);
2043 
2044     unpoisonDynamicAllocas();
2045   }
2046 
2047   if (AllocaVec.empty()) return;
2048 
2049   int StackMallocIdx = -1;
2050   DebugLoc EntryDebugLocation;
2051   if (auto SP = F.getSubprogram())
2052     EntryDebugLocation = DebugLoc::get(SP->getScopeLine(), 0, SP);
2053 
2054   Instruction *InsBefore = AllocaVec[0];
2055   IRBuilder<> IRB(InsBefore);
2056   IRB.SetCurrentDebugLocation(EntryDebugLocation);
2057 
2058   // Make sure non-instrumented allocas stay in the entry block. Otherwise,
2059   // debug info is broken, because only entry-block allocas are treated as
2060   // regular stack slots.
2061   auto InsBeforeB = InsBefore->getParent();
2062   assert(InsBeforeB == &F.getEntryBlock());
2063   for (BasicBlock::iterator I(InsBefore); I != InsBeforeB->end(); ++I)
2064     if (auto *AI = dyn_cast<AllocaInst>(I))
2065       if (NonInstrumentedStaticAllocaVec.count(AI) > 0)
2066         AI->moveBefore(InsBefore);
2067 
2068   // If we have a call to llvm.localescape, keep it in the entry block.
2069   if (LocalEscapeCall) LocalEscapeCall->moveBefore(InsBefore);
2070 
2071   SmallVector<ASanStackVariableDescription, 16> SVD;
2072   SVD.reserve(AllocaVec.size());
2073   for (AllocaInst *AI : AllocaVec) {
2074     ASanStackVariableDescription D = {AI->getName().data(),
2075                                       ASan.getAllocaSizeInBytes(AI),
2076                                       AI->getAlignment(), AI, 0};
2077     SVD.push_back(D);
2078   }
2079   // Minimal header size (left redzone) is 4 pointers,
2080   // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms.
2081   size_t MinHeaderSize = ASan.LongSize / 2;
2082   ASanStackFrameLayout L;
2083   ComputeASanStackFrameLayout(SVD, 1ULL << Mapping.Scale, MinHeaderSize, &L);
2084   DEBUG(dbgs() << L.DescriptionString << " --- " << L.FrameSize << "\n");
2085   uint64_t LocalStackSize = L.FrameSize;
2086   bool DoStackMalloc = ClUseAfterReturn && !ASan.CompileKernel &&
2087                        LocalStackSize <= kMaxStackMallocSize;
2088   bool DoDynamicAlloca = ClDynamicAllocaStack;
2089   // Don't do dynamic alloca or stack malloc if:
2090   // 1) There is inline asm: too often it makes assumptions on which registers
2091   //    are available.
2092   // 2) There is a returns_twice call (typically setjmp), which is
2093   //    optimization-hostile, and doesn't play well with introduced indirect
2094   //    register-relative calculation of local variable addresses.
2095   DoDynamicAlloca &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall;
2096   DoStackMalloc &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall;
2097 
2098   Value *StaticAlloca =
2099       DoDynamicAlloca ? nullptr : createAllocaForLayout(IRB, L, false);
2100 
2101   Value *FakeStack;
2102   Value *LocalStackBase;
2103 
2104   if (DoStackMalloc) {
2105     // void *FakeStack = __asan_option_detect_stack_use_after_return
2106     //     ? __asan_stack_malloc_N(LocalStackSize)
2107     //     : nullptr;
2108     // void *LocalStackBase = (FakeStack) ? FakeStack : alloca(LocalStackSize);
2109     Constant *OptionDetectUseAfterReturn = F.getParent()->getOrInsertGlobal(
2110         kAsanOptionDetectUseAfterReturn, IRB.getInt32Ty());
2111     Value *UseAfterReturnIsEnabled =
2112         IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUseAfterReturn),
2113                          Constant::getNullValue(IRB.getInt32Ty()));
2114     Instruction *Term =
2115         SplitBlockAndInsertIfThen(UseAfterReturnIsEnabled, InsBefore, false);
2116     IRBuilder<> IRBIf(Term);
2117     IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
2118     StackMallocIdx = StackMallocSizeClass(LocalStackSize);
2119     assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass);
2120     Value *FakeStackValue =
2121         IRBIf.CreateCall(AsanStackMallocFunc[StackMallocIdx],
2122                          ConstantInt::get(IntptrTy, LocalStackSize));
2123     IRB.SetInsertPoint(InsBefore);
2124     IRB.SetCurrentDebugLocation(EntryDebugLocation);
2125     FakeStack = createPHI(IRB, UseAfterReturnIsEnabled, FakeStackValue, Term,
2126                           ConstantInt::get(IntptrTy, 0));
2127 
2128     Value *NoFakeStack =
2129         IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy));
2130     Term = SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false);
2131     IRBIf.SetInsertPoint(Term);
2132     IRBIf.SetCurrentDebugLocation(EntryDebugLocation);
2133     Value *AllocaValue =
2134         DoDynamicAlloca ? createAllocaForLayout(IRBIf, L, true) : StaticAlloca;
2135     IRB.SetInsertPoint(InsBefore);
2136     IRB.SetCurrentDebugLocation(EntryDebugLocation);
2137     LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack);
2138   } else {
2139     // void *FakeStack = nullptr;
2140     // void *LocalStackBase = alloca(LocalStackSize);
2141     FakeStack = ConstantInt::get(IntptrTy, 0);
2142     LocalStackBase =
2143         DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca;
2144   }
2145 
2146   // Replace Alloca instructions with base+offset.
2147   for (const auto &Desc : SVD) {
2148     AllocaInst *AI = Desc.AI;
2149     Value *NewAllocaPtr = IRB.CreateIntToPtr(
2150         IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)),
2151         AI->getType());
2152     replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB, /*Deref=*/true);
2153     AI->replaceAllUsesWith(NewAllocaPtr);
2154   }
2155 
2156   // The left-most redzone has enough space for at least 4 pointers.
2157   // Write the Magic value to redzone[0].
2158   Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
2159   IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
2160                   BasePlus0);
2161   // Write the frame description constant to redzone[1].
2162   Value *BasePlus1 = IRB.CreateIntToPtr(
2163       IRB.CreateAdd(LocalStackBase,
2164                     ConstantInt::get(IntptrTy, ASan.LongSize / 8)),
2165       IntptrPtrTy);
2166   GlobalVariable *StackDescriptionGlobal =
2167       createPrivateGlobalForString(*F.getParent(), L.DescriptionString,
2168                                    /*AllowMerging*/ true);
2169   Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, IntptrTy);
2170   IRB.CreateStore(Description, BasePlus1);
2171   // Write the PC to redzone[2].
2172   Value *BasePlus2 = IRB.CreateIntToPtr(
2173       IRB.CreateAdd(LocalStackBase,
2174                     ConstantInt::get(IntptrTy, 2 * ASan.LongSize / 8)),
2175       IntptrPtrTy);
2176   IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2);
2177 
2178   // Poison the stack redzones at the entry.
2179   Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB);
2180   poisonRedZones(L.ShadowBytes, IRB, ShadowBase, true);
2181 
2182   auto UnpoisonStack = [&](IRBuilder<> &IRB) {
2183     if (HavePoisonedStaticAllocas) {
2184       // If we poisoned some allocas in llvm.lifetime analysis,
2185       // unpoison whole stack frame now.
2186       poisonAlloca(LocalStackBase, LocalStackSize, IRB, false);
2187     } else {
2188       poisonRedZones(L.ShadowBytes, IRB, ShadowBase, false);
2189     }
2190   };
2191 
2192   // (Un)poison the stack before all ret instructions.
2193   for (auto Ret : RetVec) {
2194     IRBuilder<> IRBRet(Ret);
2195     // Mark the current frame as retired.
2196     IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
2197                        BasePlus0);
2198     if (DoStackMalloc) {
2199       assert(StackMallocIdx >= 0);
2200       // if FakeStack != 0  // LocalStackBase == FakeStack
2201       //     // In use-after-return mode, poison the whole stack frame.
2202       //     if StackMallocIdx <= 4
2203       //         // For small sizes inline the whole thing:
2204       //         memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize);
2205       //         **SavedFlagPtr(FakeStack) = 0
2206       //     else
2207       //         __asan_stack_free_N(FakeStack, LocalStackSize)
2208       // else
2209       //     <This is not a fake stack; unpoison the redzones>
2210       Value *Cmp =
2211           IRBRet.CreateICmpNE(FakeStack, Constant::getNullValue(IntptrTy));
2212       TerminatorInst *ThenTerm, *ElseTerm;
2213       SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm);
2214 
2215       IRBuilder<> IRBPoison(ThenTerm);
2216       if (StackMallocIdx <= 4) {
2217         int ClassSize = kMinStackMallocSize << StackMallocIdx;
2218         SetShadowToStackAfterReturnInlined(IRBPoison, ShadowBase,
2219                                            ClassSize >> Mapping.Scale);
2220         Value *SavedFlagPtrPtr = IRBPoison.CreateAdd(
2221             FakeStack,
2222             ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8));
2223         Value *SavedFlagPtr = IRBPoison.CreateLoad(
2224             IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy));
2225         IRBPoison.CreateStore(
2226             Constant::getNullValue(IRBPoison.getInt8Ty()),
2227             IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy()));
2228       } else {
2229         // For larger frames call __asan_stack_free_*.
2230         IRBPoison.CreateCall(
2231             AsanStackFreeFunc[StackMallocIdx],
2232             {FakeStack, ConstantInt::get(IntptrTy, LocalStackSize)});
2233       }
2234 
2235       IRBuilder<> IRBElse(ElseTerm);
2236       UnpoisonStack(IRBElse);
2237     } else {
2238       UnpoisonStack(IRBRet);
2239     }
2240   }
2241 
2242   // We are done. Remove the old unused alloca instructions.
2243   for (auto AI : AllocaVec) AI->eraseFromParent();
2244 }
2245 
poisonAlloca(Value * V,uint64_t Size,IRBuilder<> & IRB,bool DoPoison)2246 void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size,
2247                                          IRBuilder<> &IRB, bool DoPoison) {
2248   // For now just insert the call to ASan runtime.
2249   Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
2250   Value *SizeArg = ConstantInt::get(IntptrTy, Size);
2251   IRB.CreateCall(
2252       DoPoison ? AsanPoisonStackMemoryFunc : AsanUnpoisonStackMemoryFunc,
2253       {AddrArg, SizeArg});
2254 }
2255 
2256 // Handling llvm.lifetime intrinsics for a given %alloca:
2257 // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
2258 // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
2259 //     invalid accesses) and unpoison it for llvm.lifetime.start (the memory
2260 //     could be poisoned by previous llvm.lifetime.end instruction, as the
2261 //     variable may go in and out of scope several times, e.g. in loops).
2262 // (3) if we poisoned at least one %alloca in a function,
2263 //     unpoison the whole stack frame at function exit.
2264 
findAllocaForValue(Value * V)2265 AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) {
2266   if (AllocaInst *AI = dyn_cast<AllocaInst>(V))
2267     // We're intested only in allocas we can handle.
2268     return ASan.isInterestingAlloca(*AI) ? AI : nullptr;
2269   // See if we've already calculated (or started to calculate) alloca for a
2270   // given value.
2271   AllocaForValueMapTy::iterator I = AllocaForValue.find(V);
2272   if (I != AllocaForValue.end()) return I->second;
2273   // Store 0 while we're calculating alloca for value V to avoid
2274   // infinite recursion if the value references itself.
2275   AllocaForValue[V] = nullptr;
2276   AllocaInst *Res = nullptr;
2277   if (CastInst *CI = dyn_cast<CastInst>(V))
2278     Res = findAllocaForValue(CI->getOperand(0));
2279   else if (PHINode *PN = dyn_cast<PHINode>(V)) {
2280     for (Value *IncValue : PN->incoming_values()) {
2281       // Allow self-referencing phi-nodes.
2282       if (IncValue == PN) continue;
2283       AllocaInst *IncValueAI = findAllocaForValue(IncValue);
2284       // AI for incoming values should exist and should all be equal.
2285       if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res))
2286         return nullptr;
2287       Res = IncValueAI;
2288     }
2289   }
2290   if (Res) AllocaForValue[V] = Res;
2291   return Res;
2292 }
2293 
handleDynamicAllocaCall(AllocaInst * AI)2294 void FunctionStackPoisoner::handleDynamicAllocaCall(AllocaInst *AI) {
2295   IRBuilder<> IRB(AI);
2296 
2297   const unsigned Align = std::max(kAllocaRzSize, AI->getAlignment());
2298   const uint64_t AllocaRedzoneMask = kAllocaRzSize - 1;
2299 
2300   Value *Zero = Constant::getNullValue(IntptrTy);
2301   Value *AllocaRzSize = ConstantInt::get(IntptrTy, kAllocaRzSize);
2302   Value *AllocaRzMask = ConstantInt::get(IntptrTy, AllocaRedzoneMask);
2303 
2304   // Since we need to extend alloca with additional memory to locate
2305   // redzones, and OldSize is number of allocated blocks with
2306   // ElementSize size, get allocated memory size in bytes by
2307   // OldSize * ElementSize.
2308   const unsigned ElementSize =
2309       F.getParent()->getDataLayout().getTypeAllocSize(AI->getAllocatedType());
2310   Value *OldSize =
2311       IRB.CreateMul(IRB.CreateIntCast(AI->getArraySize(), IntptrTy, false),
2312                     ConstantInt::get(IntptrTy, ElementSize));
2313 
2314   // PartialSize = OldSize % 32
2315   Value *PartialSize = IRB.CreateAnd(OldSize, AllocaRzMask);
2316 
2317   // Misalign = kAllocaRzSize - PartialSize;
2318   Value *Misalign = IRB.CreateSub(AllocaRzSize, PartialSize);
2319 
2320   // PartialPadding = Misalign != kAllocaRzSize ? Misalign : 0;
2321   Value *Cond = IRB.CreateICmpNE(Misalign, AllocaRzSize);
2322   Value *PartialPadding = IRB.CreateSelect(Cond, Misalign, Zero);
2323 
2324   // AdditionalChunkSize = Align + PartialPadding + kAllocaRzSize
2325   // Align is added to locate left redzone, PartialPadding for possible
2326   // partial redzone and kAllocaRzSize for right redzone respectively.
2327   Value *AdditionalChunkSize = IRB.CreateAdd(
2328       ConstantInt::get(IntptrTy, Align + kAllocaRzSize), PartialPadding);
2329 
2330   Value *NewSize = IRB.CreateAdd(OldSize, AdditionalChunkSize);
2331 
2332   // Insert new alloca with new NewSize and Align params.
2333   AllocaInst *NewAlloca = IRB.CreateAlloca(IRB.getInt8Ty(), NewSize);
2334   NewAlloca->setAlignment(Align);
2335 
2336   // NewAddress = Address + Align
2337   Value *NewAddress = IRB.CreateAdd(IRB.CreatePtrToInt(NewAlloca, IntptrTy),
2338                                     ConstantInt::get(IntptrTy, Align));
2339 
2340   // Insert __asan_alloca_poison call for new created alloca.
2341   IRB.CreateCall(AsanAllocaPoisonFunc, {NewAddress, OldSize});
2342 
2343   // Store the last alloca's address to DynamicAllocaLayout. We'll need this
2344   // for unpoisoning stuff.
2345   IRB.CreateStore(IRB.CreatePtrToInt(NewAlloca, IntptrTy), DynamicAllocaLayout);
2346 
2347   Value *NewAddressPtr = IRB.CreateIntToPtr(NewAddress, AI->getType());
2348 
2349   // Replace all uses of AddessReturnedByAlloca with NewAddressPtr.
2350   AI->replaceAllUsesWith(NewAddressPtr);
2351 
2352   // We are done. Erase old alloca from parent.
2353   AI->eraseFromParent();
2354 }
2355 
2356 // isSafeAccess returns true if Addr is always inbounds with respect to its
2357 // base object. For example, it is a field access or an array access with
2358 // constant inbounds index.
isSafeAccess(ObjectSizeOffsetVisitor & ObjSizeVis,Value * Addr,uint64_t TypeSize) const2359 bool AddressSanitizer::isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis,
2360                                     Value *Addr, uint64_t TypeSize) const {
2361   SizeOffsetType SizeOffset = ObjSizeVis.compute(Addr);
2362   if (!ObjSizeVis.bothKnown(SizeOffset)) return false;
2363   uint64_t Size = SizeOffset.first.getZExtValue();
2364   int64_t Offset = SizeOffset.second.getSExtValue();
2365   // Three checks are required to ensure safety:
2366   // . Offset >= 0  (since the offset is given from the base ptr)
2367   // . Size >= Offset  (unsigned)
2368   // . Size - Offset >= NeededSize  (unsigned)
2369   return Offset >= 0 && Size >= uint64_t(Offset) &&
2370          Size - uint64_t(Offset) >= TypeSize / 8;
2371 }
2372