1 //===- HWAddressSanitizer.cpp - detector of uninitialized reads -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file is a part of HWAddressSanitizer, an address sanity checker
11 /// based on tagged addressing.
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/IR/Attributes.h"
20 #include "llvm/IR/BasicBlock.h"
21 #include "llvm/IR/Constant.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DebugInfoMetadata.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/IRBuilder.h"
28 #include "llvm/IR/InlineAsm.h"
29 #include "llvm/IR/InstVisitor.h"
30 #include "llvm/IR/Instruction.h"
31 #include "llvm/IR/Instructions.h"
32 #include "llvm/IR/IntrinsicInst.h"
33 #include "llvm/IR/Intrinsics.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/MDBuilder.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/Type.h"
38 #include "llvm/IR/Value.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Support/Casting.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Transforms/Instrumentation.h"
45 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
46 #include "llvm/Transforms/Utils/ModuleUtils.h"
47 #include "llvm/Transforms/Utils/PromoteMemToReg.h"
48 #include <sstream>
49 
50 using namespace llvm;
51 
52 #define DEBUG_TYPE "hwasan"
53 
54 static const char *const kHwasanModuleCtorName = "hwasan.module_ctor";
55 static const char *const kHwasanInitName = "__hwasan_init";
56 
57 static const char *const kHwasanShadowMemoryDynamicAddress =
58     "__hwasan_shadow_memory_dynamic_address";
59 
60 // Accesses sizes are powers of two: 1, 2, 4, 8, 16.
61 static const size_t kNumberOfAccessSizes = 5;
62 
63 static const size_t kDefaultShadowScale = 4;
64 static const uint64_t kDynamicShadowSentinel =
65     std::numeric_limits<uint64_t>::max();
66 static const unsigned kPointerTagShift = 56;
67 
68 static const unsigned kShadowBaseAlignment = 32;
69 
70 static cl::opt<std::string> ClMemoryAccessCallbackPrefix(
71     "hwasan-memory-access-callback-prefix",
72     cl::desc("Prefix for memory access callbacks"), cl::Hidden,
73     cl::init("__hwasan_"));
74 
75 static cl::opt<bool>
76     ClInstrumentWithCalls("hwasan-instrument-with-calls",
77                 cl::desc("instrument reads and writes with callbacks"),
78                 cl::Hidden, cl::init(false));
79 
80 static cl::opt<bool> ClInstrumentReads("hwasan-instrument-reads",
81                                        cl::desc("instrument read instructions"),
82                                        cl::Hidden, cl::init(true));
83 
84 static cl::opt<bool> ClInstrumentWrites(
85     "hwasan-instrument-writes", cl::desc("instrument write instructions"),
86     cl::Hidden, cl::init(true));
87 
88 static cl::opt<bool> ClInstrumentAtomics(
89     "hwasan-instrument-atomics",
90     cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden,
91     cl::init(true));
92 
93 static cl::opt<bool> ClRecover(
94     "hwasan-recover",
95     cl::desc("Enable recovery mode (continue-after-error)."),
96     cl::Hidden, cl::init(false));
97 
98 static cl::opt<bool> ClInstrumentStack("hwasan-instrument-stack",
99                                        cl::desc("instrument stack (allocas)"),
100                                        cl::Hidden, cl::init(true));
101 
102 static cl::opt<bool> ClUARRetagToZero(
103     "hwasan-uar-retag-to-zero",
104     cl::desc("Clear alloca tags before returning from the function to allow "
105              "non-instrumented and instrumented function calls mix. When set "
106              "to false, allocas are retagged before returning from the "
107              "function to detect use after return."),
108     cl::Hidden, cl::init(true));
109 
110 static cl::opt<bool> ClGenerateTagsWithCalls(
111     "hwasan-generate-tags-with-calls",
112     cl::desc("generate new tags with runtime library calls"), cl::Hidden,
113     cl::init(false));
114 
115 static cl::opt<int> ClMatchAllTag(
116     "hwasan-match-all-tag",
117     cl::desc("don't report bad accesses via pointers with this tag"),
118     cl::Hidden, cl::init(-1));
119 
120 static cl::opt<bool> ClEnableKhwasan(
121     "hwasan-kernel",
122     cl::desc("Enable KernelHWAddressSanitizer instrumentation"),
123     cl::Hidden, cl::init(false));
124 
125 // These flags allow to change the shadow mapping and control how shadow memory
126 // is accessed. The shadow mapping looks like:
127 //    Shadow = (Mem >> scale) + offset
128 
129 static cl::opt<uint64_t>
130     ClMappingOffset("hwasan-mapping-offset",
131                     cl::desc("HWASan shadow mapping offset [EXPERIMENTAL]"),
132                     cl::Hidden, cl::init(0));
133 
134 static cl::opt<bool>
135     ClWithIfunc("hwasan-with-ifunc",
136                 cl::desc("Access dynamic shadow through an ifunc global on "
137                          "platforms that support this"),
138                 cl::Hidden, cl::init(false));
139 
140 static cl::opt<bool> ClWithTls(
141     "hwasan-with-tls",
142     cl::desc("Access dynamic shadow through an thread-local pointer on "
143              "platforms that support this"),
144     cl::Hidden, cl::init(true));
145 
146 static cl::opt<bool>
147     ClRecordStackHistory("hwasan-record-stack-history",
148                          cl::desc("Record stack frames with tagged allocations "
149                                   "in a thread-local ring buffer"),
150                          cl::Hidden, cl::init(true));
151 static cl::opt<bool>
152     ClInstrumentMemIntrinsics("hwasan-instrument-mem-intrinsics",
153                               cl::desc("instrument memory intrinsics"),
154                               cl::Hidden, cl::init(true));
155 
156 static cl::opt<bool>
157     ClInstrumentLandingPads("hwasan-instrument-landing-pads",
158                               cl::desc("instrument landing pads"), cl::Hidden,
159                               cl::init(true));
160 
161 static cl::opt<bool> ClInlineAllChecks("hwasan-inline-all-checks",
162                                        cl::desc("inline all checks"),
163                                        cl::Hidden, cl::init(false));
164 
165 namespace {
166 
167 /// An instrumentation pass implementing detection of addressability bugs
168 /// using tagged pointers.
169 class HWAddressSanitizer {
170 public:
171   explicit HWAddressSanitizer(Module &M, bool CompileKernel = false,
172                               bool Recover = false) {
173     this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover;
174     this->CompileKernel = ClEnableKhwasan.getNumOccurrences() > 0 ?
175         ClEnableKhwasan : CompileKernel;
176 
177     initializeModule(M);
178   }
179 
180   bool sanitizeFunction(Function &F);
181   void initializeModule(Module &M);
182 
183   void initializeCallbacks(Module &M);
184 
185   Value *getDynamicShadowIfunc(IRBuilder<> &IRB);
186   Value *getDynamicShadowNonTls(IRBuilder<> &IRB);
187 
188   void untagPointerOperand(Instruction *I, Value *Addr);
189   Value *shadowBase();
190   Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
191   void instrumentMemAccessInline(Value *Ptr, bool IsWrite,
192                                  unsigned AccessSizeIndex,
193                                  Instruction *InsertBefore);
194   void instrumentMemIntrinsic(MemIntrinsic *MI);
195   bool instrumentMemAccess(Instruction *I);
196   Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite,
197                                    uint64_t *TypeSize, unsigned *Alignment,
198                                    Value **MaybeMask);
199 
200   bool isInterestingAlloca(const AllocaInst &AI);
201   bool tagAlloca(IRBuilder<> &IRB, AllocaInst *AI, Value *Tag, size_t Size);
202   Value *tagPointer(IRBuilder<> &IRB, Type *Ty, Value *PtrLong, Value *Tag);
203   Value *untagPointer(IRBuilder<> &IRB, Value *PtrLong);
204   bool instrumentStack(
205       SmallVectorImpl<AllocaInst *> &Allocas,
206       DenseMap<AllocaInst *, std::vector<DbgDeclareInst *>> &AllocaDeclareMap,
207       SmallVectorImpl<Instruction *> &RetVec, Value *StackTag);
208   Value *readRegister(IRBuilder<> &IRB, StringRef Name);
209   bool instrumentLandingPads(SmallVectorImpl<Instruction *> &RetVec);
210   Value *getNextTagWithCall(IRBuilder<> &IRB);
211   Value *getStackBaseTag(IRBuilder<> &IRB);
212   Value *getAllocaTag(IRBuilder<> &IRB, Value *StackTag, AllocaInst *AI,
213                      unsigned AllocaNo);
214   Value *getUARTag(IRBuilder<> &IRB, Value *StackTag);
215 
216   Value *getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty);
217   void emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord);
218 
219 private:
220   LLVMContext *C;
221   std::string CurModuleUniqueId;
222   Triple TargetTriple;
223   FunctionCallee HWAsanMemmove, HWAsanMemcpy, HWAsanMemset;
224   FunctionCallee HWAsanHandleVfork;
225 
226   /// This struct defines the shadow mapping using the rule:
227   ///   shadow = (mem >> Scale) + Offset.
228   /// If InGlobal is true, then
229   ///   extern char __hwasan_shadow[];
230   ///   shadow = (mem >> Scale) + &__hwasan_shadow
231   /// If InTls is true, then
232   ///   extern char *__hwasan_tls;
233   ///   shadow = (mem>>Scale) + align_up(__hwasan_shadow, kShadowBaseAlignment)
234   struct ShadowMapping {
235     int Scale;
236     uint64_t Offset;
237     bool InGlobal;
238     bool InTls;
239 
240     void init(Triple &TargetTriple);
241     unsigned getAllocaAlignment() const { return 1U << Scale; }
242   };
243   ShadowMapping Mapping;
244 
245   Type *IntptrTy;
246   Type *Int8PtrTy;
247   Type *Int8Ty;
248   Type *Int32Ty;
249 
250   bool CompileKernel;
251   bool Recover;
252 
253   Function *HwasanCtorFunction;
254 
255   FunctionCallee HwasanMemoryAccessCallback[2][kNumberOfAccessSizes];
256   FunctionCallee HwasanMemoryAccessCallbackSized[2];
257 
258   FunctionCallee HwasanTagMemoryFunc;
259   FunctionCallee HwasanGenerateTagFunc;
260   FunctionCallee HwasanThreadEnterFunc;
261 
262   Constant *ShadowGlobal;
263 
264   Value *LocalDynamicShadow = nullptr;
265   Value *StackBaseTag = nullptr;
266   GlobalValue *ThreadPtrGlobal = nullptr;
267 };
268 
269 class HWAddressSanitizerLegacyPass : public FunctionPass {
270 public:
271   // Pass identification, replacement for typeid.
272   static char ID;
273 
274   explicit HWAddressSanitizerLegacyPass(bool CompileKernel = false,
275                                         bool Recover = false)
276       : FunctionPass(ID), CompileKernel(CompileKernel), Recover(Recover) {}
277 
278   StringRef getPassName() const override { return "HWAddressSanitizer"; }
279 
280   bool runOnFunction(Function &F) override {
281     HWAddressSanitizer HWASan(*F.getParent(), CompileKernel, Recover);
282     return HWASan.sanitizeFunction(F);
283   }
284 
285 private:
286   bool CompileKernel;
287   bool Recover;
288 };
289 
290 } // end anonymous namespace
291 
292 char HWAddressSanitizerLegacyPass::ID = 0;
293 
294 INITIALIZE_PASS_BEGIN(
295     HWAddressSanitizerLegacyPass, "hwasan",
296     "HWAddressSanitizer: detect memory bugs using tagged addressing.", false,
297     false)
298 INITIALIZE_PASS_END(
299     HWAddressSanitizerLegacyPass, "hwasan",
300     "HWAddressSanitizer: detect memory bugs using tagged addressing.", false,
301     false)
302 
303 FunctionPass *llvm::createHWAddressSanitizerLegacyPassPass(bool CompileKernel,
304                                                            bool Recover) {
305   assert(!CompileKernel || Recover);
306   return new HWAddressSanitizerLegacyPass(CompileKernel, Recover);
307 }
308 
309 HWAddressSanitizerPass::HWAddressSanitizerPass(bool CompileKernel, bool Recover)
310     : CompileKernel(CompileKernel), Recover(Recover) {}
311 
312 PreservedAnalyses HWAddressSanitizerPass::run(Function &F,
313                                               FunctionAnalysisManager &FAM) {
314   HWAddressSanitizer HWASan(*F.getParent(), CompileKernel, Recover);
315   if (HWASan.sanitizeFunction(F))
316     return PreservedAnalyses::none();
317   return PreservedAnalyses::all();
318 }
319 
320 /// Module-level initialization.
321 ///
322 /// inserts a call to __hwasan_init to the module's constructor list.
323 void HWAddressSanitizer::initializeModule(Module &M) {
324   LLVM_DEBUG(dbgs() << "Init " << M.getName() << "\n");
325   auto &DL = M.getDataLayout();
326 
327   TargetTriple = Triple(M.getTargetTriple());
328 
329   Mapping.init(TargetTriple);
330 
331   C = &(M.getContext());
332   CurModuleUniqueId = getUniqueModuleId(&M);
333   IRBuilder<> IRB(*C);
334   IntptrTy = IRB.getIntPtrTy(DL);
335   Int8PtrTy = IRB.getInt8PtrTy();
336   Int8Ty = IRB.getInt8Ty();
337   Int32Ty = IRB.getInt32Ty();
338 
339   HwasanCtorFunction = nullptr;
340   if (!CompileKernel) {
341     std::tie(HwasanCtorFunction, std::ignore) =
342         getOrCreateSanitizerCtorAndInitFunctions(
343             M, kHwasanModuleCtorName, kHwasanInitName,
344             /*InitArgTypes=*/{},
345             /*InitArgs=*/{},
346             // This callback is invoked when the functions are created the first
347             // time. Hook them into the global ctors list in that case:
348             [&](Function *Ctor, FunctionCallee) {
349               Comdat *CtorComdat = M.getOrInsertComdat(kHwasanModuleCtorName);
350               Ctor->setComdat(CtorComdat);
351               appendToGlobalCtors(M, Ctor, 0, Ctor);
352             });
353   }
354 
355   if (!TargetTriple.isAndroid()) {
356     Constant *C = M.getOrInsertGlobal("__hwasan_tls", IntptrTy, [&] {
357       auto *GV = new GlobalVariable(M, IntptrTy, /*isConstantGlobal=*/false,
358                                     GlobalValue::ExternalLinkage, nullptr,
359                                     "__hwasan_tls", nullptr,
360                                     GlobalVariable::InitialExecTLSModel);
361       appendToCompilerUsed(M, GV);
362       return GV;
363     });
364     ThreadPtrGlobal = cast<GlobalVariable>(C);
365   }
366 }
367 
368 void HWAddressSanitizer::initializeCallbacks(Module &M) {
369   IRBuilder<> IRB(*C);
370   for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
371     const std::string TypeStr = AccessIsWrite ? "store" : "load";
372     const std::string EndingStr = Recover ? "_noabort" : "";
373 
374     HwasanMemoryAccessCallbackSized[AccessIsWrite] = M.getOrInsertFunction(
375         ClMemoryAccessCallbackPrefix + TypeStr + "N" + EndingStr,
376         FunctionType::get(IRB.getVoidTy(), {IntptrTy, IntptrTy}, false));
377 
378     for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
379          AccessSizeIndex++) {
380       HwasanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] =
381           M.getOrInsertFunction(
382               ClMemoryAccessCallbackPrefix + TypeStr +
383                   itostr(1ULL << AccessSizeIndex) + EndingStr,
384               FunctionType::get(IRB.getVoidTy(), {IntptrTy}, false));
385     }
386   }
387 
388   HwasanTagMemoryFunc = M.getOrInsertFunction(
389       "__hwasan_tag_memory", IRB.getVoidTy(), Int8PtrTy, Int8Ty, IntptrTy);
390   HwasanGenerateTagFunc =
391       M.getOrInsertFunction("__hwasan_generate_tag", Int8Ty);
392 
393   ShadowGlobal = M.getOrInsertGlobal("__hwasan_shadow",
394                                      ArrayType::get(IRB.getInt8Ty(), 0));
395 
396   const std::string MemIntrinCallbackPrefix =
397       CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
398   HWAsanMemmove = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memmove",
399                                         IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
400                                         IRB.getInt8PtrTy(), IntptrTy);
401   HWAsanMemcpy = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memcpy",
402                                        IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
403                                        IRB.getInt8PtrTy(), IntptrTy);
404   HWAsanMemset = M.getOrInsertFunction(MemIntrinCallbackPrefix + "memset",
405                                        IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
406                                        IRB.getInt32Ty(), IntptrTy);
407 
408   HWAsanHandleVfork =
409       M.getOrInsertFunction("__hwasan_handle_vfork", IRB.getVoidTy(), IntptrTy);
410 
411   HwasanThreadEnterFunc =
412       M.getOrInsertFunction("__hwasan_thread_enter", IRB.getVoidTy());
413 }
414 
415 Value *HWAddressSanitizer::getDynamicShadowIfunc(IRBuilder<> &IRB) {
416   // An empty inline asm with input reg == output reg.
417   // An opaque no-op cast, basically.
418   InlineAsm *Asm = InlineAsm::get(
419       FunctionType::get(Int8PtrTy, {ShadowGlobal->getType()}, false),
420       StringRef(""), StringRef("=r,0"),
421       /*hasSideEffects=*/false);
422   return IRB.CreateCall(Asm, {ShadowGlobal}, ".hwasan.shadow");
423 }
424 
425 Value *HWAddressSanitizer::getDynamicShadowNonTls(IRBuilder<> &IRB) {
426   // Generate code only when dynamic addressing is needed.
427   if (Mapping.Offset != kDynamicShadowSentinel)
428     return nullptr;
429 
430   if (Mapping.InGlobal) {
431     return getDynamicShadowIfunc(IRB);
432   } else {
433     Value *GlobalDynamicAddress =
434         IRB.GetInsertBlock()->getParent()->getParent()->getOrInsertGlobal(
435             kHwasanShadowMemoryDynamicAddress, Int8PtrTy);
436     return IRB.CreateLoad(Int8PtrTy, GlobalDynamicAddress);
437   }
438 }
439 
440 Value *HWAddressSanitizer::isInterestingMemoryAccess(Instruction *I,
441                                                      bool *IsWrite,
442                                                      uint64_t *TypeSize,
443                                                      unsigned *Alignment,
444                                                      Value **MaybeMask) {
445   // Skip memory accesses inserted by another instrumentation.
446   if (I->getMetadata("nosanitize")) return nullptr;
447 
448   // Do not instrument the load fetching the dynamic shadow address.
449   if (LocalDynamicShadow == I)
450     return nullptr;
451 
452   Value *PtrOperand = nullptr;
453   const DataLayout &DL = I->getModule()->getDataLayout();
454   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
455     if (!ClInstrumentReads) return nullptr;
456     *IsWrite = false;
457     *TypeSize = DL.getTypeStoreSizeInBits(LI->getType());
458     *Alignment = LI->getAlignment();
459     PtrOperand = LI->getPointerOperand();
460   } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
461     if (!ClInstrumentWrites) return nullptr;
462     *IsWrite = true;
463     *TypeSize = DL.getTypeStoreSizeInBits(SI->getValueOperand()->getType());
464     *Alignment = SI->getAlignment();
465     PtrOperand = SI->getPointerOperand();
466   } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
467     if (!ClInstrumentAtomics) return nullptr;
468     *IsWrite = true;
469     *TypeSize = DL.getTypeStoreSizeInBits(RMW->getValOperand()->getType());
470     *Alignment = 0;
471     PtrOperand = RMW->getPointerOperand();
472   } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
473     if (!ClInstrumentAtomics) return nullptr;
474     *IsWrite = true;
475     *TypeSize = DL.getTypeStoreSizeInBits(XCHG->getCompareOperand()->getType());
476     *Alignment = 0;
477     PtrOperand = XCHG->getPointerOperand();
478   }
479 
480   if (PtrOperand) {
481     // Do not instrument accesses from different address spaces; we cannot deal
482     // with them.
483     Type *PtrTy = cast<PointerType>(PtrOperand->getType()->getScalarType());
484     if (PtrTy->getPointerAddressSpace() != 0)
485       return nullptr;
486 
487     // Ignore swifterror addresses.
488     // swifterror memory addresses are mem2reg promoted by instruction
489     // selection. As such they cannot have regular uses like an instrumentation
490     // function and it makes no sense to track them as memory.
491     if (PtrOperand->isSwiftError())
492       return nullptr;
493   }
494 
495   return PtrOperand;
496 }
497 
498 static unsigned getPointerOperandIndex(Instruction *I) {
499   if (LoadInst *LI = dyn_cast<LoadInst>(I))
500     return LI->getPointerOperandIndex();
501   if (StoreInst *SI = dyn_cast<StoreInst>(I))
502     return SI->getPointerOperandIndex();
503   if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I))
504     return RMW->getPointerOperandIndex();
505   if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I))
506     return XCHG->getPointerOperandIndex();
507   report_fatal_error("Unexpected instruction");
508   return -1;
509 }
510 
511 static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
512   size_t Res = countTrailingZeros(TypeSize / 8);
513   assert(Res < kNumberOfAccessSizes);
514   return Res;
515 }
516 
517 void HWAddressSanitizer::untagPointerOperand(Instruction *I, Value *Addr) {
518   if (TargetTriple.isAArch64())
519     return;
520 
521   IRBuilder<> IRB(I);
522   Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
523   Value *UntaggedPtr =
524       IRB.CreateIntToPtr(untagPointer(IRB, AddrLong), Addr->getType());
525   I->setOperand(getPointerOperandIndex(I), UntaggedPtr);
526 }
527 
528 Value *HWAddressSanitizer::shadowBase() {
529   if (LocalDynamicShadow)
530     return LocalDynamicShadow;
531   return ConstantExpr::getIntToPtr(ConstantInt::get(IntptrTy, Mapping.Offset),
532                                    Int8PtrTy);
533 }
534 
535 Value *HWAddressSanitizer::memToShadow(Value *Mem, IRBuilder<> &IRB) {
536   // Mem >> Scale
537   Value *Shadow = IRB.CreateLShr(Mem, Mapping.Scale);
538   if (Mapping.Offset == 0)
539     return IRB.CreateIntToPtr(Shadow, Int8PtrTy);
540   // (Mem >> Scale) + Offset
541   return IRB.CreateGEP(Int8Ty, shadowBase(), Shadow);
542 }
543 
544 void HWAddressSanitizer::instrumentMemAccessInline(Value *Ptr, bool IsWrite,
545                                                    unsigned AccessSizeIndex,
546                                                    Instruction *InsertBefore) {
547   const int64_t AccessInfo = Recover * 0x20 + IsWrite * 0x10 + AccessSizeIndex;
548   IRBuilder<> IRB(InsertBefore);
549 
550   if (!ClInlineAllChecks && TargetTriple.isAArch64() &&
551       TargetTriple.isOSBinFormatELF() && !Recover) {
552     Module *M = IRB.GetInsertBlock()->getParent()->getParent();
553     Ptr = IRB.CreateBitCast(Ptr, Int8PtrTy);
554     IRB.CreateCall(
555         Intrinsic::getDeclaration(M, Intrinsic::hwasan_check_memaccess),
556         {shadowBase(), Ptr, ConstantInt::get(Int32Ty, AccessInfo)});
557     return;
558   }
559 
560   Value *PtrLong = IRB.CreatePointerCast(Ptr, IntptrTy);
561   Value *PtrTag = IRB.CreateTrunc(IRB.CreateLShr(PtrLong, kPointerTagShift),
562                                   IRB.getInt8Ty());
563   Value *AddrLong = untagPointer(IRB, PtrLong);
564   Value *Shadow = memToShadow(AddrLong, IRB);
565   Value *MemTag = IRB.CreateLoad(Int8Ty, Shadow);
566   Value *TagMismatch = IRB.CreateICmpNE(PtrTag, MemTag);
567 
568   int matchAllTag = ClMatchAllTag.getNumOccurrences() > 0 ?
569       ClMatchAllTag : (CompileKernel ? 0xFF : -1);
570   if (matchAllTag != -1) {
571     Value *TagNotIgnored = IRB.CreateICmpNE(PtrTag,
572         ConstantInt::get(PtrTag->getType(), matchAllTag));
573     TagMismatch = IRB.CreateAnd(TagMismatch, TagNotIgnored);
574   }
575 
576   Instruction *CheckTerm =
577       SplitBlockAndInsertIfThen(TagMismatch, InsertBefore, false,
578                                 MDBuilder(*C).createBranchWeights(1, 100000));
579 
580   IRB.SetInsertPoint(CheckTerm);
581   Value *OutOfShortGranuleTagRange =
582       IRB.CreateICmpUGT(MemTag, ConstantInt::get(Int8Ty, 15));
583   Instruction *CheckFailTerm =
584       SplitBlockAndInsertIfThen(OutOfShortGranuleTagRange, CheckTerm, !Recover,
585                                 MDBuilder(*C).createBranchWeights(1, 100000));
586 
587   IRB.SetInsertPoint(CheckTerm);
588   Value *PtrLowBits = IRB.CreateTrunc(IRB.CreateAnd(PtrLong, 15), Int8Ty);
589   PtrLowBits = IRB.CreateAdd(
590       PtrLowBits, ConstantInt::get(Int8Ty, (1 << AccessSizeIndex) - 1));
591   Value *PtrLowBitsOOB = IRB.CreateICmpUGE(PtrLowBits, MemTag);
592   SplitBlockAndInsertIfThen(PtrLowBitsOOB, CheckTerm, false,
593                             MDBuilder(*C).createBranchWeights(1, 100000),
594                             nullptr, nullptr, CheckFailTerm->getParent());
595 
596   IRB.SetInsertPoint(CheckTerm);
597   Value *InlineTagAddr = IRB.CreateOr(AddrLong, 15);
598   InlineTagAddr = IRB.CreateIntToPtr(InlineTagAddr, Int8PtrTy);
599   Value *InlineTag = IRB.CreateLoad(Int8Ty, InlineTagAddr);
600   Value *InlineTagMismatch = IRB.CreateICmpNE(PtrTag, InlineTag);
601   SplitBlockAndInsertIfThen(InlineTagMismatch, CheckTerm, false,
602                             MDBuilder(*C).createBranchWeights(1, 100000),
603                             nullptr, nullptr, CheckFailTerm->getParent());
604 
605   IRB.SetInsertPoint(CheckFailTerm);
606   InlineAsm *Asm;
607   switch (TargetTriple.getArch()) {
608     case Triple::x86_64:
609       // The signal handler will find the data address in rdi.
610       Asm = InlineAsm::get(
611           FunctionType::get(IRB.getVoidTy(), {PtrLong->getType()}, false),
612           "int3\nnopl " + itostr(0x40 + AccessInfo) + "(%rax)",
613           "{rdi}",
614           /*hasSideEffects=*/true);
615       break;
616     case Triple::aarch64:
617     case Triple::aarch64_be:
618       // The signal handler will find the data address in x0.
619       Asm = InlineAsm::get(
620           FunctionType::get(IRB.getVoidTy(), {PtrLong->getType()}, false),
621           "brk #" + itostr(0x900 + AccessInfo),
622           "{x0}",
623           /*hasSideEffects=*/true);
624       break;
625     default:
626       report_fatal_error("unsupported architecture");
627   }
628   IRB.CreateCall(Asm, PtrLong);
629   if (Recover)
630     cast<BranchInst>(CheckFailTerm)->setSuccessor(0, CheckTerm->getParent());
631 }
632 
633 void HWAddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
634   IRBuilder<> IRB(MI);
635   if (isa<MemTransferInst>(MI)) {
636     IRB.CreateCall(
637         isa<MemMoveInst>(MI) ? HWAsanMemmove : HWAsanMemcpy,
638         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
639          IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()),
640          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
641   } else if (isa<MemSetInst>(MI)) {
642     IRB.CreateCall(
643         HWAsanMemset,
644         {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()),
645          IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false),
646          IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)});
647   }
648   MI->eraseFromParent();
649 }
650 
651 bool HWAddressSanitizer::instrumentMemAccess(Instruction *I) {
652   LLVM_DEBUG(dbgs() << "Instrumenting: " << *I << "\n");
653   bool IsWrite = false;
654   unsigned Alignment = 0;
655   uint64_t TypeSize = 0;
656   Value *MaybeMask = nullptr;
657 
658   if (ClInstrumentMemIntrinsics && isa<MemIntrinsic>(I)) {
659     instrumentMemIntrinsic(cast<MemIntrinsic>(I));
660     return true;
661   }
662 
663   Value *Addr =
664       isInterestingMemoryAccess(I, &IsWrite, &TypeSize, &Alignment, &MaybeMask);
665 
666   if (!Addr)
667     return false;
668 
669   if (MaybeMask)
670     return false; //FIXME
671 
672   IRBuilder<> IRB(I);
673   if (isPowerOf2_64(TypeSize) &&
674       (TypeSize / 8 <= (1UL << (kNumberOfAccessSizes - 1))) &&
675       (Alignment >= (1UL << Mapping.Scale) || Alignment == 0 ||
676        Alignment >= TypeSize / 8)) {
677     size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
678     if (ClInstrumentWithCalls) {
679       IRB.CreateCall(HwasanMemoryAccessCallback[IsWrite][AccessSizeIndex],
680                      IRB.CreatePointerCast(Addr, IntptrTy));
681     } else {
682       instrumentMemAccessInline(Addr, IsWrite, AccessSizeIndex, I);
683     }
684   } else {
685     IRB.CreateCall(HwasanMemoryAccessCallbackSized[IsWrite],
686                    {IRB.CreatePointerCast(Addr, IntptrTy),
687                     ConstantInt::get(IntptrTy, TypeSize / 8)});
688   }
689   untagPointerOperand(I, Addr);
690 
691   return true;
692 }
693 
694 static uint64_t getAllocaSizeInBytes(const AllocaInst &AI) {
695   uint64_t ArraySize = 1;
696   if (AI.isArrayAllocation()) {
697     const ConstantInt *CI = dyn_cast<ConstantInt>(AI.getArraySize());
698     assert(CI && "non-constant array size");
699     ArraySize = CI->getZExtValue();
700   }
701   Type *Ty = AI.getAllocatedType();
702   uint64_t SizeInBytes = AI.getModule()->getDataLayout().getTypeAllocSize(Ty);
703   return SizeInBytes * ArraySize;
704 }
705 
706 bool HWAddressSanitizer::tagAlloca(IRBuilder<> &IRB, AllocaInst *AI,
707                                    Value *Tag, size_t Size) {
708   size_t AlignedSize = alignTo(Size, Mapping.getAllocaAlignment());
709 
710   Value *JustTag = IRB.CreateTrunc(Tag, IRB.getInt8Ty());
711   if (ClInstrumentWithCalls) {
712     IRB.CreateCall(HwasanTagMemoryFunc,
713                    {IRB.CreatePointerCast(AI, Int8PtrTy), JustTag,
714                     ConstantInt::get(IntptrTy, AlignedSize)});
715   } else {
716     size_t ShadowSize = Size >> Mapping.Scale;
717     Value *ShadowPtr = memToShadow(IRB.CreatePointerCast(AI, IntptrTy), IRB);
718     // If this memset is not inlined, it will be intercepted in the hwasan
719     // runtime library. That's OK, because the interceptor skips the checks if
720     // the address is in the shadow region.
721     // FIXME: the interceptor is not as fast as real memset. Consider lowering
722     // llvm.memset right here into either a sequence of stores, or a call to
723     // hwasan_tag_memory.
724     if (ShadowSize)
725       IRB.CreateMemSet(ShadowPtr, JustTag, ShadowSize, /*Align=*/1);
726     if (Size != AlignedSize) {
727       IRB.CreateStore(
728           ConstantInt::get(Int8Ty, Size % Mapping.getAllocaAlignment()),
729           IRB.CreateConstGEP1_32(Int8Ty, ShadowPtr, ShadowSize));
730       IRB.CreateStore(JustTag, IRB.CreateConstGEP1_32(
731                                    Int8Ty, IRB.CreateBitCast(AI, Int8PtrTy),
732                                    AlignedSize - 1));
733     }
734   }
735   return true;
736 }
737 
738 static unsigned RetagMask(unsigned AllocaNo) {
739   // A list of 8-bit numbers that have at most one run of non-zero bits.
740   // x = x ^ (mask << 56) can be encoded as a single armv8 instruction for these
741   // masks.
742   // The list does not include the value 255, which is used for UAR.
743   //
744   // Because we are more likely to use earlier elements of this list than later
745   // ones, it is sorted in increasing order of probability of collision with a
746   // mask allocated (temporally) nearby. The program that generated this list
747   // can be found at:
748   // https://github.com/google/sanitizers/blob/master/hwaddress-sanitizer/sort_masks.py
749   static unsigned FastMasks[] = {0,  128, 64,  192, 32,  96,  224, 112, 240,
750                                  48, 16,  120, 248, 56,  24,  8,   124, 252,
751                                  60, 28,  12,  4,   126, 254, 62,  30,  14,
752                                  6,  2,   127, 63,  31,  15,  7,   3,   1};
753   return FastMasks[AllocaNo % (sizeof(FastMasks) / sizeof(FastMasks[0]))];
754 }
755 
756 Value *HWAddressSanitizer::getNextTagWithCall(IRBuilder<> &IRB) {
757   return IRB.CreateZExt(IRB.CreateCall(HwasanGenerateTagFunc), IntptrTy);
758 }
759 
760 Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
761   if (ClGenerateTagsWithCalls)
762     return getNextTagWithCall(IRB);
763   if (StackBaseTag)
764     return StackBaseTag;
765   // FIXME: use addressofreturnaddress (but implement it in aarch64 backend
766   // first).
767   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
768   auto GetStackPointerFn =
769       Intrinsic::getDeclaration(M, Intrinsic::frameaddress);
770   Value *StackPointer = IRB.CreateCall(
771       GetStackPointerFn, {Constant::getNullValue(IRB.getInt32Ty())});
772 
773   // Extract some entropy from the stack pointer for the tags.
774   // Take bits 20..28 (ASLR entropy) and xor with bits 0..8 (these differ
775   // between functions).
776   Value *StackPointerLong = IRB.CreatePointerCast(StackPointer, IntptrTy);
777   Value *StackTag =
778       IRB.CreateXor(StackPointerLong, IRB.CreateLShr(StackPointerLong, 20),
779                     "hwasan.stack.base.tag");
780   return StackTag;
781 }
782 
783 Value *HWAddressSanitizer::getAllocaTag(IRBuilder<> &IRB, Value *StackTag,
784                                         AllocaInst *AI, unsigned AllocaNo) {
785   if (ClGenerateTagsWithCalls)
786     return getNextTagWithCall(IRB);
787   return IRB.CreateXor(StackTag,
788                        ConstantInt::get(IntptrTy, RetagMask(AllocaNo)));
789 }
790 
791 Value *HWAddressSanitizer::getUARTag(IRBuilder<> &IRB, Value *StackTag) {
792   if (ClUARRetagToZero)
793     return ConstantInt::get(IntptrTy, 0);
794   if (ClGenerateTagsWithCalls)
795     return getNextTagWithCall(IRB);
796   return IRB.CreateXor(StackTag, ConstantInt::get(IntptrTy, 0xFFU));
797 }
798 
799 // Add a tag to an address.
800 Value *HWAddressSanitizer::tagPointer(IRBuilder<> &IRB, Type *Ty,
801                                       Value *PtrLong, Value *Tag) {
802   Value *TaggedPtrLong;
803   if (CompileKernel) {
804     // Kernel addresses have 0xFF in the most significant byte.
805     Value *ShiftedTag = IRB.CreateOr(
806         IRB.CreateShl(Tag, kPointerTagShift),
807         ConstantInt::get(IntptrTy, (1ULL << kPointerTagShift) - 1));
808     TaggedPtrLong = IRB.CreateAnd(PtrLong, ShiftedTag);
809   } else {
810     // Userspace can simply do OR (tag << 56);
811     Value *ShiftedTag = IRB.CreateShl(Tag, kPointerTagShift);
812     TaggedPtrLong = IRB.CreateOr(PtrLong, ShiftedTag);
813   }
814   return IRB.CreateIntToPtr(TaggedPtrLong, Ty);
815 }
816 
817 // Remove tag from an address.
818 Value *HWAddressSanitizer::untagPointer(IRBuilder<> &IRB, Value *PtrLong) {
819   Value *UntaggedPtrLong;
820   if (CompileKernel) {
821     // Kernel addresses have 0xFF in the most significant byte.
822     UntaggedPtrLong = IRB.CreateOr(PtrLong,
823         ConstantInt::get(PtrLong->getType(), 0xFFULL << kPointerTagShift));
824   } else {
825     // Userspace addresses have 0x00.
826     UntaggedPtrLong = IRB.CreateAnd(PtrLong,
827         ConstantInt::get(PtrLong->getType(), ~(0xFFULL << kPointerTagShift)));
828   }
829   return UntaggedPtrLong;
830 }
831 
832 Value *HWAddressSanitizer::getHwasanThreadSlotPtr(IRBuilder<> &IRB, Type *Ty) {
833   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
834   if (TargetTriple.isAArch64() && TargetTriple.isAndroid()) {
835     // Android provides a fixed TLS slot for sanitizers. See TLS_SLOT_SANITIZER
836     // in Bionic's libc/private/bionic_tls.h.
837     Function *ThreadPointerFunc =
838         Intrinsic::getDeclaration(M, Intrinsic::thread_pointer);
839     Value *SlotPtr = IRB.CreatePointerCast(
840         IRB.CreateConstGEP1_32(IRB.getInt8Ty(),
841                                IRB.CreateCall(ThreadPointerFunc), 0x30),
842         Ty->getPointerTo(0));
843     return SlotPtr;
844   }
845   if (ThreadPtrGlobal)
846     return ThreadPtrGlobal;
847 
848 
849   return nullptr;
850 }
851 
852 void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
853   if (!Mapping.InTls) {
854     LocalDynamicShadow = getDynamicShadowNonTls(IRB);
855     return;
856   }
857 
858   if (!WithFrameRecord && TargetTriple.isAndroid()) {
859     LocalDynamicShadow = getDynamicShadowIfunc(IRB);
860     return;
861   }
862 
863   Value *SlotPtr = getHwasanThreadSlotPtr(IRB, IntptrTy);
864   assert(SlotPtr);
865 
866   Instruction *ThreadLong = IRB.CreateLoad(IntptrTy, SlotPtr);
867 
868   Function *F = IRB.GetInsertBlock()->getParent();
869   if (F->getFnAttribute("hwasan-abi").getValueAsString() == "interceptor") {
870     Value *ThreadLongEqZero =
871         IRB.CreateICmpEQ(ThreadLong, ConstantInt::get(IntptrTy, 0));
872     auto *Br = cast<BranchInst>(SplitBlockAndInsertIfThen(
873         ThreadLongEqZero, cast<Instruction>(ThreadLongEqZero)->getNextNode(),
874         false, MDBuilder(*C).createBranchWeights(1, 100000)));
875 
876     IRB.SetInsertPoint(Br);
877     // FIXME: This should call a new runtime function with a custom calling
878     // convention to avoid needing to spill all arguments here.
879     IRB.CreateCall(HwasanThreadEnterFunc);
880     LoadInst *ReloadThreadLong = IRB.CreateLoad(IntptrTy, SlotPtr);
881 
882     IRB.SetInsertPoint(&*Br->getSuccessor(0)->begin());
883     PHINode *ThreadLongPhi = IRB.CreatePHI(IntptrTy, 2);
884     ThreadLongPhi->addIncoming(ThreadLong, ThreadLong->getParent());
885     ThreadLongPhi->addIncoming(ReloadThreadLong, ReloadThreadLong->getParent());
886     ThreadLong = ThreadLongPhi;
887   }
888 
889   // Extract the address field from ThreadLong. Unnecessary on AArch64 with TBI.
890   Value *ThreadLongMaybeUntagged =
891       TargetTriple.isAArch64() ? ThreadLong : untagPointer(IRB, ThreadLong);
892 
893   if (WithFrameRecord) {
894     StackBaseTag = IRB.CreateAShr(ThreadLong, 3);
895 
896     // Prepare ring buffer data.
897     Value *PC;
898     if (TargetTriple.getArch() == Triple::aarch64)
899       PC = readRegister(IRB, "pc");
900     else
901       PC = IRB.CreatePtrToInt(F, IntptrTy);
902     auto GetStackPointerFn =
903         Intrinsic::getDeclaration(F->getParent(), Intrinsic::frameaddress);
904     Value *SP = IRB.CreatePtrToInt(
905         IRB.CreateCall(GetStackPointerFn,
906                        {Constant::getNullValue(IRB.getInt32Ty())}),
907         IntptrTy);
908     // Mix SP and PC.
909     // Assumptions:
910     // PC is 0x0000PPPPPPPPPPPP  (48 bits are meaningful, others are zero)
911     // SP is 0xsssssssssssSSSS0  (4 lower bits are zero)
912     // We only really need ~20 lower non-zero bits (SSSS), so we mix like this:
913     //       0xSSSSPPPPPPPPPPPP
914     SP = IRB.CreateShl(SP, 44);
915 
916     // Store data to ring buffer.
917     Value *RecordPtr =
918         IRB.CreateIntToPtr(ThreadLongMaybeUntagged, IntptrTy->getPointerTo(0));
919     IRB.CreateStore(IRB.CreateOr(PC, SP), RecordPtr);
920 
921     // Update the ring buffer. Top byte of ThreadLong defines the size of the
922     // buffer in pages, it must be a power of two, and the start of the buffer
923     // must be aligned by twice that much. Therefore wrap around of the ring
924     // buffer is simply Addr &= ~((ThreadLong >> 56) << 12).
925     // The use of AShr instead of LShr is due to
926     //   https://bugs.llvm.org/show_bug.cgi?id=39030
927     // Runtime library makes sure not to use the highest bit.
928     Value *WrapMask = IRB.CreateXor(
929         IRB.CreateShl(IRB.CreateAShr(ThreadLong, 56), 12, "", true, true),
930         ConstantInt::get(IntptrTy, (uint64_t)-1));
931     Value *ThreadLongNew = IRB.CreateAnd(
932         IRB.CreateAdd(ThreadLong, ConstantInt::get(IntptrTy, 8)), WrapMask);
933     IRB.CreateStore(ThreadLongNew, SlotPtr);
934   }
935 
936   // Get shadow base address by aligning RecordPtr up.
937   // Note: this is not correct if the pointer is already aligned.
938   // Runtime library will make sure this never happens.
939   LocalDynamicShadow = IRB.CreateAdd(
940       IRB.CreateOr(
941           ThreadLongMaybeUntagged,
942           ConstantInt::get(IntptrTy, (1ULL << kShadowBaseAlignment) - 1)),
943       ConstantInt::get(IntptrTy, 1), "hwasan.shadow");
944   LocalDynamicShadow = IRB.CreateIntToPtr(LocalDynamicShadow, Int8PtrTy);
945 }
946 
947 Value *HWAddressSanitizer::readRegister(IRBuilder<> &IRB, StringRef Name) {
948   Module *M = IRB.GetInsertBlock()->getParent()->getParent();
949   Function *ReadRegister =
950       Intrinsic::getDeclaration(M, Intrinsic::read_register, IntptrTy);
951   MDNode *MD = MDNode::get(*C, {MDString::get(*C, Name)});
952   Value *Args[] = {MetadataAsValue::get(*C, MD)};
953   return IRB.CreateCall(ReadRegister, Args);
954 }
955 
956 bool HWAddressSanitizer::instrumentLandingPads(
957     SmallVectorImpl<Instruction *> &LandingPadVec) {
958   for (auto *LP : LandingPadVec) {
959     IRBuilder<> IRB(LP->getNextNode());
960     IRB.CreateCall(
961         HWAsanHandleVfork,
962         {readRegister(IRB, (TargetTriple.getArch() == Triple::x86_64) ? "rsp"
963                                                                       : "sp")});
964   }
965   return true;
966 }
967 
968 bool HWAddressSanitizer::instrumentStack(
969     SmallVectorImpl<AllocaInst *> &Allocas,
970     DenseMap<AllocaInst *, std::vector<DbgDeclareInst *>> &AllocaDeclareMap,
971     SmallVectorImpl<Instruction *> &RetVec, Value *StackTag) {
972   // Ideally, we want to calculate tagged stack base pointer, and rewrite all
973   // alloca addresses using that. Unfortunately, offsets are not known yet
974   // (unless we use ASan-style mega-alloca). Instead we keep the base tag in a
975   // temp, shift-OR it into each alloca address and xor with the retag mask.
976   // This generates one extra instruction per alloca use.
977   for (unsigned N = 0; N < Allocas.size(); ++N) {
978     auto *AI = Allocas[N];
979     IRBuilder<> IRB(AI->getNextNode());
980 
981     // Replace uses of the alloca with tagged address.
982     Value *Tag = getAllocaTag(IRB, StackTag, AI, N);
983     Value *AILong = IRB.CreatePointerCast(AI, IntptrTy);
984     Value *Replacement = tagPointer(IRB, AI->getType(), AILong, Tag);
985     std::string Name =
986         AI->hasName() ? AI->getName().str() : "alloca." + itostr(N);
987     Replacement->setName(Name + ".hwasan");
988 
989     for (auto UI = AI->use_begin(), UE = AI->use_end(); UI != UE;) {
990       Use &U = *UI++;
991       if (U.getUser() != AILong)
992         U.set(Replacement);
993     }
994 
995     for (auto *DDI : AllocaDeclareMap.lookup(AI)) {
996       DIExpression *OldExpr = DDI->getExpression();
997       DIExpression *NewExpr = DIExpression::append(
998           OldExpr, {dwarf::DW_OP_LLVM_tag_offset, RetagMask(N)});
999       DDI->setArgOperand(2, MetadataAsValue::get(*C, NewExpr));
1000     }
1001 
1002     size_t Size = getAllocaSizeInBytes(*AI);
1003     tagAlloca(IRB, AI, Tag, Size);
1004 
1005     for (auto RI : RetVec) {
1006       IRB.SetInsertPoint(RI);
1007 
1008       // Re-tag alloca memory with the special UAR tag.
1009       Value *Tag = getUARTag(IRB, StackTag);
1010       tagAlloca(IRB, AI, Tag, alignTo(Size, Mapping.getAllocaAlignment()));
1011     }
1012   }
1013 
1014   return true;
1015 }
1016 
1017 bool HWAddressSanitizer::isInterestingAlloca(const AllocaInst &AI) {
1018   return (AI.getAllocatedType()->isSized() &&
1019           // FIXME: instrument dynamic allocas, too
1020           AI.isStaticAlloca() &&
1021           // alloca() may be called with 0 size, ignore it.
1022           getAllocaSizeInBytes(AI) > 0 &&
1023           // We are only interested in allocas not promotable to registers.
1024           // Promotable allocas are common under -O0.
1025           !isAllocaPromotable(&AI) &&
1026           // inalloca allocas are not treated as static, and we don't want
1027           // dynamic alloca instrumentation for them as well.
1028           !AI.isUsedWithInAlloca() &&
1029           // swifterror allocas are register promoted by ISel
1030           !AI.isSwiftError());
1031 }
1032 
1033 bool HWAddressSanitizer::sanitizeFunction(Function &F) {
1034   if (&F == HwasanCtorFunction)
1035     return false;
1036 
1037   if (!F.hasFnAttribute(Attribute::SanitizeHWAddress))
1038     return false;
1039 
1040   LLVM_DEBUG(dbgs() << "Function: " << F.getName() << "\n");
1041 
1042   SmallVector<Instruction*, 16> ToInstrument;
1043   SmallVector<AllocaInst*, 8> AllocasToInstrument;
1044   SmallVector<Instruction*, 8> RetVec;
1045   SmallVector<Instruction*, 8> LandingPadVec;
1046   DenseMap<AllocaInst *, std::vector<DbgDeclareInst *>> AllocaDeclareMap;
1047   for (auto &BB : F) {
1048     for (auto &Inst : BB) {
1049       if (ClInstrumentStack)
1050         if (AllocaInst *AI = dyn_cast<AllocaInst>(&Inst)) {
1051           if (isInterestingAlloca(*AI))
1052             AllocasToInstrument.push_back(AI);
1053           continue;
1054         }
1055 
1056       if (isa<ReturnInst>(Inst) || isa<ResumeInst>(Inst) ||
1057           isa<CleanupReturnInst>(Inst))
1058         RetVec.push_back(&Inst);
1059 
1060       if (auto *DDI = dyn_cast<DbgDeclareInst>(&Inst))
1061         if (auto *Alloca = dyn_cast_or_null<AllocaInst>(DDI->getAddress()))
1062           AllocaDeclareMap[Alloca].push_back(DDI);
1063 
1064       if (ClInstrumentLandingPads && isa<LandingPadInst>(Inst))
1065         LandingPadVec.push_back(&Inst);
1066 
1067       Value *MaybeMask = nullptr;
1068       bool IsWrite;
1069       unsigned Alignment;
1070       uint64_t TypeSize;
1071       Value *Addr = isInterestingMemoryAccess(&Inst, &IsWrite, &TypeSize,
1072                                               &Alignment, &MaybeMask);
1073       if (Addr || isa<MemIntrinsic>(Inst))
1074         ToInstrument.push_back(&Inst);
1075     }
1076   }
1077 
1078   initializeCallbacks(*F.getParent());
1079 
1080   if (!LandingPadVec.empty())
1081     instrumentLandingPads(LandingPadVec);
1082 
1083   if (AllocasToInstrument.empty() && ToInstrument.empty())
1084     return false;
1085 
1086   assert(!LocalDynamicShadow);
1087 
1088   Instruction *InsertPt = &*F.getEntryBlock().begin();
1089   IRBuilder<> EntryIRB(InsertPt);
1090   emitPrologue(EntryIRB,
1091                /*WithFrameRecord*/ ClRecordStackHistory &&
1092                    !AllocasToInstrument.empty());
1093 
1094   bool Changed = false;
1095   if (!AllocasToInstrument.empty()) {
1096     Value *StackTag =
1097         ClGenerateTagsWithCalls ? nullptr : getStackBaseTag(EntryIRB);
1098     Changed |= instrumentStack(AllocasToInstrument, AllocaDeclareMap, RetVec,
1099                                StackTag);
1100   }
1101 
1102   // Pad and align each of the allocas that we instrumented to stop small
1103   // uninteresting allocas from hiding in instrumented alloca's padding and so
1104   // that we have enough space to store real tags for short granules.
1105   DenseMap<AllocaInst *, AllocaInst *> AllocaToPaddedAllocaMap;
1106   for (AllocaInst *AI : AllocasToInstrument) {
1107     uint64_t Size = getAllocaSizeInBytes(*AI);
1108     uint64_t AlignedSize = alignTo(Size, Mapping.getAllocaAlignment());
1109     AI->setAlignment(std::max(AI->getAlignment(), 16u));
1110     if (Size != AlignedSize) {
1111       Type *TypeWithPadding = StructType::get(
1112           AI->getAllocatedType(), ArrayType::get(Int8Ty, AlignedSize - Size));
1113       auto *NewAI = new AllocaInst(
1114           TypeWithPadding, AI->getType()->getAddressSpace(), nullptr, "", AI);
1115       NewAI->takeName(AI);
1116       NewAI->setAlignment(AI->getAlignment());
1117       NewAI->setUsedWithInAlloca(AI->isUsedWithInAlloca());
1118       NewAI->setSwiftError(AI->isSwiftError());
1119       NewAI->copyMetadata(*AI);
1120       Value *Zero = ConstantInt::get(Int32Ty, 0);
1121       auto *GEP = GetElementPtrInst::Create(TypeWithPadding, NewAI,
1122                                             {Zero, Zero}, "", AI);
1123       AI->replaceAllUsesWith(GEP);
1124       AllocaToPaddedAllocaMap[AI] = NewAI;
1125     }
1126   }
1127 
1128   if (!AllocaToPaddedAllocaMap.empty()) {
1129     for (auto &BB : F)
1130       for (auto &Inst : BB)
1131         if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst))
1132           if (auto *AI =
1133                   dyn_cast_or_null<AllocaInst>(DVI->getVariableLocation()))
1134             if (auto *NewAI = AllocaToPaddedAllocaMap.lookup(AI))
1135               DVI->setArgOperand(
1136                   0, MetadataAsValue::get(*C, LocalAsMetadata::get(NewAI)));
1137     for (auto &P : AllocaToPaddedAllocaMap)
1138       P.first->eraseFromParent();
1139   }
1140 
1141   // If we split the entry block, move any allocas that were originally in the
1142   // entry block back into the entry block so that they aren't treated as
1143   // dynamic allocas.
1144   if (EntryIRB.GetInsertBlock() != &F.getEntryBlock()) {
1145     InsertPt = &*F.getEntryBlock().begin();
1146     for (auto II = EntryIRB.GetInsertBlock()->begin(),
1147               IE = EntryIRB.GetInsertBlock()->end();
1148          II != IE;) {
1149       Instruction *I = &*II++;
1150       if (auto *AI = dyn_cast<AllocaInst>(I))
1151         if (isa<ConstantInt>(AI->getArraySize()))
1152           I->moveBefore(InsertPt);
1153     }
1154   }
1155 
1156   for (auto Inst : ToInstrument)
1157     Changed |= instrumentMemAccess(Inst);
1158 
1159   LocalDynamicShadow = nullptr;
1160   StackBaseTag = nullptr;
1161 
1162   return Changed;
1163 }
1164 
1165 void HWAddressSanitizer::ShadowMapping::init(Triple &TargetTriple) {
1166   Scale = kDefaultShadowScale;
1167   if (ClMappingOffset.getNumOccurrences() > 0) {
1168     InGlobal = false;
1169     InTls = false;
1170     Offset = ClMappingOffset;
1171   } else if (ClEnableKhwasan || ClInstrumentWithCalls) {
1172     InGlobal = false;
1173     InTls = false;
1174     Offset = 0;
1175   } else if (ClWithIfunc) {
1176     InGlobal = true;
1177     InTls = false;
1178     Offset = kDynamicShadowSentinel;
1179   } else if (ClWithTls) {
1180     InGlobal = false;
1181     InTls = true;
1182     Offset = kDynamicShadowSentinel;
1183   } else {
1184     InGlobal = false;
1185     InTls = false;
1186     Offset = kDynamicShadowSentinel;
1187   }
1188 }
1189