AddressSanitizer.cpp revision d8313be41031e4d768f5b38199904d4debff88cd
1800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===-- AddressSanitizer.cpp - memory error detector ------------*- C++ -*-===//
2800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
3800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//                     The LLVM Compiler Infrastructure
4800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
5800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This file is distributed under the University of Illinois Open Source
6800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// License. See LICENSE.TXT for details.
7800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
8800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===----------------------------------------------------------------------===//
9800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
10800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This file is a part of AddressSanitizer, an address sanity checker.
11800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Details of the algorithm:
12800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//  http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
13800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
14800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===----------------------------------------------------------------------===//
15800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
16800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#define DEBUG_TYPE "asan"
17800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
18800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/ArrayRef.h"
19800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/OwningPtr.h"
20800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/SmallSet.h"
21800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/SmallString.h"
22800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/SmallVector.h"
23800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/StringExtras.h"
24800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Function.h"
25800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/IntrinsicInst.h"
26800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/LLVMContext.h"
27800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Module.h"
28800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/CommandLine.h"
29800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/DataTypes.h"
30800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/Debug.h"
31800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/IRBuilder.h"
32800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/MemoryBuffer.h"
33800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/Regex.h"
34800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/raw_ostream.h"
35800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/system_error.h"
36800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Target/TargetData.h"
37800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Target/TargetMachine.h"
38800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Transforms/Instrumentation.h"
39800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Transforms/Utils/BasicBlockUtils.h"
40800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Transforms/Utils/ModuleUtils.h"
41800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Type.h"
42800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
43800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <string>
44800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <algorithm>
45800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
46800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyusing namespace llvm;
47800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
48800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowScale = 3;
49800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
50800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
51800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
52800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const size_t kMaxStackMallocSize = 1 << 16;  // 64K
53800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
54800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
55800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
56800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanModuleCtorName = "asan.module_ctor";
577bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const char *kAsanModuleDtorName = "asan.module_dtor";
587bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const int   kAsanCtorAndCtorPriority = 1;
59800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanReportErrorTemplate = "__asan_report_";
60800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanRegisterGlobalsName = "__asan_register_globals";
617bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
62800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanInitName = "__asan_init";
6395e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryanystatic const char *kAsanHandleNoReturnName = "__asan_handle_no_return";
64800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanMappingOffsetName = "__asan_mapping_offset";
65800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanMappingScaleName = "__asan_mapping_scale";
66800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanStackMallocName = "__asan_stack_malloc";
67800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanStackFreeName = "__asan_stack_free";
68800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
69800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackLeftRedzoneMagic = 0xf1;
70800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackMidRedzoneMagic = 0xf2;
71800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackRightRedzoneMagic = 0xf3;
72800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackPartialRedzoneMagic = 0xf4;
73800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
74800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Command-line flags.
75800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
76800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no-]asan-reads.
77800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
78800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
79800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
80800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
81800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-stack.
82800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClStack("asan-stack",
83800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
84800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-use-after-return.
85800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
86800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
87800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-globals.
88800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClGlobals("asan-globals",
89800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
90800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClMemIntrin("asan-memintrin",
91800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
92800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -fasan-blacklist.
93800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<std::string>  ClBlackListFile("asan-blacklist",
94800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("File containing the list of functions to ignore "
95800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                "during instrumentation"), cl::Hidden);
96800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
97800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// These flags allow to change the shadow mapping.
98800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// The shadow mapping looks like
99800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//    Shadow = (Mem >> scale) + (1 << offset_log)
100800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClMappingScale("asan-mapping-scale",
101800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
102800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
103800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
104800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
105800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Optimization flags. Not user visible, used mostly for testing
106800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// and benchmarking the tool.
107800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOpt("asan-opt",
108800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
109800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
110800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("Instrument the same temp just once"), cl::Hidden,
111800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::init(true));
112800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOptGlobals("asan-opt-globals",
113800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
114800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
115800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Debug flags.
116800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
117800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                            cl::init(0));
118800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
119800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                 cl::Hidden, cl::init(0));
120800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<std::string> ClDebugFunc("asan-debug-func",
121800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                        cl::Hidden, cl::desc("Debug func"));
122800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
123800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                               cl::Hidden, cl::init(-1));
124800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
125800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                               cl::Hidden, cl::init(-1));
126800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
127800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanynamespace {
128800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
129800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Blacklisted functions are not instrumented.
130800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// The blacklist file contains one or more lines like this:
131800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// ---
132800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// fun:FunctionWildCard
133800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// ---
134800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This is similar to the "ignore" feature of ThreadSanitizer.
135800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// http://code.google.com/p/data-race-test/wiki/ThreadSanitizerIgnores
136800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyclass BlackList {
137800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany public:
138800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BlackList(const std::string &Path);
139800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool isIn(const Function &F);
140800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany private:
141800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Regex *Functions;
142800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany};
143800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
144800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany/// AddressSanitizer: instrument the code in module to find memory bugs.
145800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystruct AddressSanitizer : public ModulePass {
146800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  AddressSanitizer();
14725878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko  virtual const char *getPassName() const;
148800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void instrumentMop(Instruction *I);
149800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB,
150800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                         Value *Addr, uint32_t TypeSize, bool IsWrite);
151800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Instruction *generateCrashCode(IRBuilder<> &IRB, Value *Addr,
152800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                 bool IsWrite, uint32_t TypeSize);
153800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool instrumentMemIntrinsic(MemIntrinsic *MI);
154800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
155800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                  Value *Size,
156800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                   Instruction *InsertBefore, bool IsWrite);
157800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
158800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool handleFunction(Module &M, Function &F);
159a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  bool maybeInsertAsanInitAtFunctionEntry(Function &F);
160800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool poisonStackInFunction(Module &M, Function &F);
161800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  virtual bool runOnModule(Module &M);
162800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool insertGlobalRedzones(Module &M);
163800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BranchInst *splitBlockAndInsertIfThen(Instruction *SplitBefore, Value *Cmp);
164800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  static char ID;  // Pass identification, replacement for typeid
165800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
166800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany private:
167800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
168800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
169800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Type *Ty = AI->getAllocatedType();
170d8313be41031e4d768f5b38199904d4debff88cdEvgeniy Stepanov    uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
171800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return SizeInBytes;
172800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
173800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t getAlignedSize(uint64_t SizeInBytes) {
174800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return ((SizeInBytes + RedzoneSize - 1)
175800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany            / RedzoneSize) * RedzoneSize;
176800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
177800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t getAlignedAllocaSize(AllocaInst *AI) {
178800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
179800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return getAlignedSize(SizeInBytes);
180800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
181800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
182800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
183800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                   Value *ShadowBase, bool DoPoison);
1845a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  bool LooksLikeCodeInBug11395(Instruction *I);
185800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
186800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Module      *CurrentModule;
187800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  LLVMContext *C;
188800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  TargetData *TD;
189800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t MappingOffset;
190800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  int MappingScale;
191800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  size_t RedzoneSize;
192800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  int LongSize;
193800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *IntptrTy;
194800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *IntptrPtrTy;
195800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Function *AsanCtorFunction;
196800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Function *AsanInitFunction;
197800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Instruction *CtorInsertBefore;
198800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  OwningPtr<BlackList> BL;
199800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany};
200800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}  // namespace
201800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
202800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanychar AddressSanitizer::ID = 0;
203800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyINITIALIZE_PASS(AddressSanitizer, "asan",
204800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
205800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    false, false)
206800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyAddressSanitizer::AddressSanitizer() : ModulePass(ID) { }
207800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyModulePass *llvm::createAddressSanitizerPass() {
208800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return new AddressSanitizer();
209800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
210800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
21125878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenkoconst char *AddressSanitizer::getPassName() const {
21225878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko  return "AddressSanitizer";
21325878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko}
21425878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko
215800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Create a constant for Str so that we can pass it to the run-time lib.
216800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
21718c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner  Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
218800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return new GlobalVariable(M, StrConst->getType(), true,
219800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                            GlobalValue::PrivateLinkage, StrConst, "");
220800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
221800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
222800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Split the basic block and insert an if-then code.
223800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Before:
224800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//   Head
225800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//   SplitBefore
226800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//   Tail
227800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// After:
228800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//   Head
229800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//   if (Cmp)
230800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//     NewBasicBlock
231800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//   SplitBefore
232800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//   Tail
233800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
234800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Returns the NewBasicBlock's terminator.
235800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyBranchInst *AddressSanitizer::splitBlockAndInsertIfThen(
236800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Instruction *SplitBefore, Value *Cmp) {
237800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BasicBlock *Head = SplitBefore->getParent();
238800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
239800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  TerminatorInst *HeadOldTerm = Head->getTerminator();
240800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BasicBlock *NewBasicBlock =
241800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      BasicBlock::Create(*C, "", Head->getParent());
242800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BranchInst *HeadNewTerm = BranchInst::Create(/*ifTrue*/NewBasicBlock,
243800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                               /*ifFalse*/Tail,
244800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                               Cmp);
245800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
246800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
247800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BranchInst *CheckTerm = BranchInst::Create(Tail, NewBasicBlock);
248800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return CheckTerm;
249800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
250800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
251800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyValue *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
252800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Shadow >> scale
253800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Shadow = IRB.CreateLShr(Shadow, MappingScale);
254800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (MappingOffset == 0)
255800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return Shadow;
256800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // (Shadow >> scale) | offset
257800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
258800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                               MappingOffset));
259800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
260800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
261800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyvoid AddressSanitizer::instrumentMemIntrinsicParam(Instruction *OrigIns,
262800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
263800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Check the first byte.
264800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  {
265800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    IRBuilder<> IRB(InsertBefore);
266800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
267800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
268800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Check the last byte.
269800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  {
270800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    IRBuilder<> IRB(InsertBefore);
271800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *SizeMinusOne = IRB.CreateSub(
272800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        Size, ConstantInt::get(Size->getType(), 1));
273800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
274800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
275800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
276800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
277800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
278800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
279800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
280800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Instrument memset/memmove/memcpy
281800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanybool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
282800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *Dst = MI->getDest();
283800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
284800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *Src = MemTran ? MemTran->getSource() : NULL;
285800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *Length = MI->getLength();
286800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
287800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Constant *ConstLength = dyn_cast<Constant>(Length);
288800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Instruction *InsertBefore = MI;
289800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ConstLength) {
290800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (ConstLength->isNullValue()) return false;
291800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  } else {
292800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // The size is not a constant so it could be zero -- check at run-time.
293800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    IRBuilder<> IRB(InsertBefore);
294800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
295800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *Cmp = IRB.CreateICmpNE(Length,
296800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                   Constant::getNullValue(Length->getType()));
297800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    InsertBefore = splitBlockAndInsertIfThen(InsertBefore, Cmp);
298800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
299800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
300800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
301800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (Src)
302800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
303800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return true;
304800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
305800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
306800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic Value *getLDSTOperand(Instruction *I) {
307800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
308800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return LI->getPointerOperand();
309800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
310800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return cast<StoreInst>(*I).getPointerOperand();
311800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
312800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
313800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyvoid AddressSanitizer::instrumentMop(Instruction *I) {
314800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  int IsWrite = isa<StoreInst>(*I);
315800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *Addr = getLDSTOperand(I);
316800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ClOpt && ClOptGlobals && isa<GlobalVariable>(Addr)) {
317800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // We are accessing a global scalar variable. Nothing to catch here.
318800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return;
319800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
320800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *OrigPtrTy = Addr->getType();
321800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
322800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
323800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  assert(OrigTy->isSized());
324800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
325800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
326800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (TypeSize != 8  && TypeSize != 16 &&
327800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
328800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Ignore all unusual sizes.
329800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return;
330800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
331800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
332800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRBuilder<> IRB(I);
333800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
334800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
335800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
336800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyInstruction *AddressSanitizer::generateCrashCode(
337800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    IRBuilder<> &IRB, Value *Addr, bool IsWrite, uint32_t TypeSize) {
3383c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany  // IsWrite and TypeSize are encoded in the function name.
3393c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany  std::string FunctionName = std::string(kAsanReportErrorTemplate) +
3403c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany      (IsWrite ? "store" : "load") + itostr(TypeSize / 8);
3413c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany  Value *ReportWarningFunc = CurrentModule->getOrInsertFunction(
3423c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany      FunctionName, IRB.getVoidTy(), IntptrTy, NULL);
3433c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany  CallInst *Call = IRB.CreateCall(ReportWarningFunc, Addr);
3443c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany  Call->setDoesNotReturn();
3453c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany  return Call;
346800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
347800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
348800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyvoid AddressSanitizer::instrumentAddress(Instruction *OrigIns,
349800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                         IRBuilder<> &IRB, Value *Addr,
350800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                         uint32_t TypeSize, bool IsWrite) {
351800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
352800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
353800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *ShadowTy  = IntegerType::get(
354800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      *C, std::max(8U, TypeSize >> MappingScale));
355800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
356800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *ShadowPtr = memToShadow(AddrLong, IRB);
357800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *CmpVal = Constant::getNullValue(ShadowTy);
358800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *ShadowValue = IRB.CreateLoad(
359800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
360800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
361800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
362800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
363800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Instruction *CheckTerm = splitBlockAndInsertIfThen(
364800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      cast<Instruction>(Cmp)->getNextNode(), Cmp);
365800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRBuilder<> IRB2(CheckTerm);
366800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
367800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  size_t Granularity = 1 << MappingScale;
368800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (TypeSize < 8 * Granularity) {
369800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Addr & (Granularity - 1)
370800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *Lower3Bits = IRB2.CreateAnd(
371800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
372800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // (Addr & (Granularity - 1)) + size - 1
373800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *LastAccessedByte = IRB2.CreateAdd(
374800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        Lower3Bits, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
375800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
376800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    LastAccessedByte = IRB2.CreateIntCast(
377800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        LastAccessedByte, IRB.getInt8Ty(), false);
378800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
379800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *Cmp2 = IRB2.CreateICmpSGE(LastAccessedByte, ShadowValue);
380800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
381800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    CheckTerm = splitBlockAndInsertIfThen(CheckTerm, Cmp2);
382800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
383800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
384800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRBuilder<> IRB1(CheckTerm);
385800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Instruction *Crash = generateCrashCode(IRB1, AddrLong, IsWrite, TypeSize);
386800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Crash->setDebugLoc(OrigIns->getDebugLoc());
387cc1d856d8e8e11407b3c6c1d08768f77c3722e38Kostya Serebryany  ReplaceInstWithInst(CheckTerm, new UnreachableInst(*C));
388800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
389800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
390800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This function replaces all global variables with new variables that have
391800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// trailing redzones. It also creates a function that poisons
392800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// redzones and inserts this function into llvm.global_ctors.
393800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanybool AddressSanitizer::insertGlobalRedzones(Module &M) {
394800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallVector<GlobalVariable *, 16> GlobalsToChange;
395800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
396800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (Module::GlobalListType::iterator G = M.getGlobalList().begin(),
397800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       E = M.getGlobalList().end(); G != E; ++G) {
398800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Type *Ty = cast<PointerType>(G->getType())->getElementType();
399800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    DEBUG(dbgs() << "GLOBAL: " << *G);
400800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
401800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (!Ty->isSized()) continue;
402800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (!G->hasInitializer()) continue;
4037cf2a04361e8613264498e50babe52d65c070473Kostya Serebryany    // Touch only those globals that will not be defined in other modules.
4047cf2a04361e8613264498e50babe52d65c070473Kostya Serebryany    // Don't handle ODR type linkages since other modules may be built w/o asan.
4052e7fb2f73641f1f1ca385fe7777075945bf4ca24Kostya Serebryany    if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
4062e7fb2f73641f1f1ca385fe7777075945bf4ca24Kostya Serebryany        G->getLinkage() != GlobalVariable::PrivateLinkage &&
4072e7fb2f73641f1f1ca385fe7777075945bf4ca24Kostya Serebryany        G->getLinkage() != GlobalVariable::InternalLinkage)
408800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      continue;
409d2703dec271d82c8c9d22afb835c07730fd25d47Kostya Serebryany    // Two problems with thread-locals:
410d2703dec271d82c8c9d22afb835c07730fd25d47Kostya Serebryany    //   - The address of the main thread's copy can't be computed at link-time.
411d2703dec271d82c8c9d22afb835c07730fd25d47Kostya Serebryany    //   - Need to poison all copies, not just the main thread's one.
412d2703dec271d82c8c9d22afb835c07730fd25d47Kostya Serebryany    if (G->isThreadLocal())
413d2703dec271d82c8c9d22afb835c07730fd25d47Kostya Serebryany      continue;
414800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // For now, just ignore this Alloca if the alignment is large.
415800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (G->getAlignment() > RedzoneSize) continue;
416800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
417800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Ignore all the globals with the names starting with "\01L_OBJC_".
418800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Many of those are put into the .cstring section. The linker compresses
419800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // that section by removing the spare \0s after the string terminator, so
420800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // our redzones get broken.
421800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if ((G->getName().find("\01L_OBJC_") == 0) ||
422800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        (G->getName().find("\01l_OBJC_") == 0)) {
423800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
424800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      continue;
425800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
426800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
427800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (G->hasSection()) {
428800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      StringRef Section(G->getSection());
4298375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      // Ignore the globals from the __OBJC section. The ObjC runtime assumes
4308375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
4318375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      // them.
432800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if ((Section.find("__OBJC,") == 0) ||
433800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          (Section.find("__DATA, __objc_") == 0)) {
434800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
435800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        continue;
436800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      }
4378375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
4388375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      // Constant CFString instances are compiled in the following way:
4398375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      //  -- the string buffer is emitted into
4408375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      //     __TEXT,__cstring,cstring_literals
4418375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      //  -- the constant NSConstantString structure referencing that buffer
4428375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      //     is placed into __DATA,__cfstring
4438375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      // Therefore there's no point in placing redzones into __DATA,__cfstring.
4448375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      // Moreover, it causes the linker to crash on OS X 10.7
4458375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      if (Section.find("__DATA,__cfstring") == 0) {
4468375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko        DEBUG(dbgs() << "Ignoring CFString: " << *G);
4478375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko        continue;
4488375bc923638299410e75e8f668524fc1aa113bdAlexander Potapenko      }
449800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
450800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
451800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    GlobalsToChange.push_back(G);
452800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
453800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
454800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  size_t n = GlobalsToChange.size();
455800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (n == 0) return false;
456800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
457800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // A global is described by a structure
458800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  //   size_t beg;
459800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  //   size_t size;
460800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  //   size_t size_with_redzone;
461800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  //   const char *name;
462800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // We initialize an array of such structures and pass it to a run-time call.
463800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
464800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                               IntptrTy, IntptrTy, NULL);
465800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallVector<Constant *, 16> Initializers(n);
466800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
467800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRBuilder<> IRB(CtorInsertBefore);
468800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
469800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (size_t i = 0; i < n; i++) {
470800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    GlobalVariable *G = GlobalsToChange[i];
471800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    PointerType *PtrTy = cast<PointerType>(G->getType());
472800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Type *Ty = PtrTy->getElementType();
473800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    uint64_t SizeInBytes = TD->getTypeStoreSizeInBits(Ty) / 8;
474800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    uint64_t RightRedzoneSize = RedzoneSize +
475800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        (RedzoneSize - (SizeInBytes % RedzoneSize));
476800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
477800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
478800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
479800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Constant *NewInitializer = ConstantStruct::get(
480800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        NewTy, G->getInitializer(),
481800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        Constant::getNullValue(RightRedZoneTy), NULL);
482800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
483a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany    SmallString<2048> DescriptionOfGlobal = G->getName();
484a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany    DescriptionOfGlobal += " (";
485a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany    DescriptionOfGlobal += M.getModuleIdentifier();
486a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany    DescriptionOfGlobal += ")";
487a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany    GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
488800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
489800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Create a new global variable with enough space for a redzone.
490800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    GlobalVariable *NewGlobal = new GlobalVariable(
491800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        M, NewTy, G->isConstant(), G->getLinkage(),
492800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        NewInitializer, "", G, G->isThreadLocal());
493800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    NewGlobal->copyAttributesFrom(G);
494800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    NewGlobal->setAlignment(RedzoneSize);
495800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
496800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *Indices2[2];
497800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Indices2[0] = IRB.getInt32(0);
498800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Indices2[1] = IRB.getInt32(0);
499800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
500800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    G->replaceAllUsesWith(
501f1639abf1aaba1448f719f595156cd0f4cd560ccKostya Serebryany        ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
502800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    NewGlobal->takeName(G);
503800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    G->eraseFromParent();
504800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
505800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Initializers[i] = ConstantStruct::get(
506800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        GlobalStructTy,
507800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
508800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        ConstantInt::get(IntptrTy, SizeInBytes),
509800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
510800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        ConstantExpr::getPointerCast(Name, IntptrTy),
511800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        NULL);
512800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    DEBUG(dbgs() << "NEW GLOBAL:\n" << *NewGlobal);
513800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
514800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
515800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
516800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  GlobalVariable *AllGlobals = new GlobalVariable(
517800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
518800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
519800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
520800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Function *AsanRegisterGlobals = cast<Function>(M.getOrInsertFunction(
521800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
522800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
523800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
524800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRB.CreateCall2(AsanRegisterGlobals,
525800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                  IRB.CreatePointerCast(AllGlobals, IntptrTy),
526800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                  ConstantInt::get(IntptrTy, n));
527800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
5287bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  // We also need to unregister globals at the end, e.g. when a shared library
5297bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  // gets closed.
5307bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  Function *AsanDtorFunction = Function::Create(
5317bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany      FunctionType::get(Type::getVoidTy(*C), false),
5327bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany      GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
5337bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
5347bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
5357bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  Function *AsanUnregisterGlobals = cast<Function>(M.getOrInsertFunction(
5367bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany      kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
5377bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
5387bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany
5397bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
5407bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany                       IRB.CreatePointerCast(AllGlobals, IntptrTy),
5417bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany                       ConstantInt::get(IntptrTy, n));
5427bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
5437bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany
544800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  DEBUG(dbgs() << M);
545800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return true;
546800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
547800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
548800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// virtual
549800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanybool AddressSanitizer::runOnModule(Module &M) {
550800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Initialize the private fields. No one has accessed them before.
551800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  TD = getAnalysisIfAvailable<TargetData>();
552800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (!TD)
553800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return false;
554800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BL.reset(new BlackList(ClBlackListFile));
555800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
556800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  CurrentModule = &M;
557800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  C = &(M.getContext());
558800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  LongSize = TD->getPointerSizeInBits();
559800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IntptrTy = Type::getIntNTy(*C, LongSize);
560800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IntptrPtrTy = PointerType::get(IntptrTy, 0);
561800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
562800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  AsanCtorFunction = Function::Create(
563800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      FunctionType::get(Type::getVoidTy(*C), false),
564800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
565800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
566800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  CtorInsertBefore = ReturnInst::Create(*C, AsanCtorBB);
567800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
568800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // call __asan_init in the module ctor.
569800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRBuilder<> IRB(CtorInsertBefore);
570800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  AsanInitFunction = cast<Function>(
571800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
572800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  AsanInitFunction->setLinkage(Function::ExternalLinkage);
573800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRB.CreateCall(AsanInitFunction);
574800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
575800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  MappingOffset = LongSize == 32
576800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      ? kDefaultShadowOffset32 : kDefaultShadowOffset64;
577800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ClMappingOffsetLog >= 0) {
578800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (ClMappingOffsetLog == 0) {
579800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      // special case
580800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      MappingOffset = 0;
581800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    } else {
582800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      MappingOffset = 1ULL << ClMappingOffsetLog;
583800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
584800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
585800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  MappingScale = kDefaultShadowScale;
586800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ClMappingScale) {
587800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    MappingScale = ClMappingScale;
588800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
589800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Redzone used for stack and globals is at least 32 bytes.
590800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
591800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  RedzoneSize = std::max(32, (int)(1 << MappingScale));
592800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
593800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool Res = false;
594800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
595800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ClGlobals)
596800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Res |= insertGlobalRedzones(M);
597800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
598800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Tell the run-time the current values of mapping offset and scale.
599800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  GlobalValue *asan_mapping_offset =
600800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
601800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                     ConstantInt::get(IntptrTy, MappingOffset),
602800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                     kAsanMappingOffsetName);
603800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  GlobalValue *asan_mapping_scale =
604800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
605800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                         ConstantInt::get(IntptrTy, MappingScale),
606800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                         kAsanMappingScaleName);
607800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Read these globals, otherwise they may be optimized away.
608800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRB.CreateLoad(asan_mapping_scale, true);
609800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRB.CreateLoad(asan_mapping_offset, true);
610800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
611800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
612800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
613800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (F->isDeclaration()) continue;
614800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Res |= handleFunction(M, *F);
615800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
616800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
6177bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany  appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
6189b02741d2284d9516a59666c211fe1f57e8803adKostya Serebryany
619800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return Res;
620800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
621800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
622a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryanybool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
623a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // For each NSObject descendant having a +load method, this method is invoked
624a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // by the ObjC runtime before any of the static constructors is called.
625a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // Therefore we need to instrument such methods with a call to __asan_init
626a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // at the beginning in order to initialize our runtime before any access to
627a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // the shadow memory.
628a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // We cannot just ignore these methods, because they may call other
629a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // instrumented functions.
630a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  if (F.getName().find(" load]") != std::string::npos) {
631a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany    IRBuilder<> IRB(F.begin()->begin());
632a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany    IRB.CreateCall(AsanInitFunction);
633a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany    return true;
634a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  }
635a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  return false;
636a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany}
637a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany
638800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanybool AddressSanitizer::handleFunction(Module &M, Function &F) {
639800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (BL->isIn(F)) return false;
640800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (&F == AsanCtorFunction) return false;
641a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany
642a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  // If needed, insert __asan_init before checking for AddressSafety attr.
643a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany  maybeInsertAsanInitAtFunctionEntry(F);
644a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany
6450307b9a88574d7e54459181afaa656ac92971847Kostya Serebryany  if (!F.hasFnAttr(Attribute::AddressSafety)) return false;
646800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
647800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
648800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return false;
649800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // We want to instrument every address only once per basic block
650800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // (unless there are calls between uses).
651800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallSet<Value*, 16> TempsToInstrument;
652800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallVector<Instruction*, 16> ToInstrument;
65395e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany  SmallVector<Instruction*, 8> NoReturnCalls;
654800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
655800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Fill the set of memory operations to instrument.
656800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (Function::iterator FI = F.begin(), FE = F.end();
657800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       FI != FE; ++FI) {
658800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    TempsToInstrument.clear();
659800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
660800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany         BI != BE; ++BI) {
661bcb55ce3862bbbedac4e09d7099c9e0efc434e4bKostya Serebryany      if (LooksLikeCodeInBug11395(BI)) return false;
662800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if ((isa<LoadInst>(BI) && ClInstrumentReads) ||
663800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          (isa<StoreInst>(BI) && ClInstrumentWrites)) {
664800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        Value *Addr = getLDSTOperand(BI);
665800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        if (ClOpt && ClOptSameTemp) {
666800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          if (!TempsToInstrument.insert(Addr))
667800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany            continue;  // We've seen this temp in the current BB.
668800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        }
669800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
670800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        // ok, take it.
671800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      } else {
67295e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany        if (CallInst *CI = dyn_cast<CallInst>(BI)) {
673800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          // A call inside BB.
674800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          TempsToInstrument.clear();
67595e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany          if (CI->doesNotReturn()) {
67695e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany            NoReturnCalls.push_back(CI);
67795e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany          }
678800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        }
679800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        continue;
680800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      }
681800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      ToInstrument.push_back(BI);
682800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
683800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
684800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
685800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Instrument.
686800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  int NumInstrumented = 0;
687800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
688800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Instruction *Inst = ToInstrument[i];
689800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (ClDebugMin < 0 || ClDebugMax < 0 ||
690800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
691800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (isa<StoreInst>(Inst) || isa<LoadInst>(Inst))
692800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        instrumentMop(Inst);
693800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      else
694800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
695800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
696800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    NumInstrumented++;
697800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
698800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
699800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  DEBUG(dbgs() << F);
700800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
701800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool ChangedStack = poisonStackInFunction(M, F);
70295e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany
70395e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany  // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
70495e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany  // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
70595e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany  for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
70695e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany    Instruction *CI = NoReturnCalls[i];
70795e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany    IRBuilder<> IRB(CI);
70895e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany    IRB.CreateCall(M.getOrInsertFunction(kAsanHandleNoReturnName,
70995e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany                                         IRB.getVoidTy(), NULL));
71095e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany  }
71195e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany
71295e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany  return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
713800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
714800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
715800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
716800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ShadowRedzoneSize == 1) return PoisonByte;
717800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
718800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ShadowRedzoneSize == 4)
719800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return (PoisonByte << 24) + (PoisonByte << 16) +
720800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        (PoisonByte << 8) + (PoisonByte);
721858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper  llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
722800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
723800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
724800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
725800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                            size_t Size,
726800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                            size_t RedzoneSize,
727800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                            size_t ShadowGranularity,
728800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                            uint8_t Magic) {
729800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (size_t i = 0; i < RedzoneSize;
730800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       i+= ShadowGranularity, Shadow++) {
731800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (i + ShadowGranularity <= Size) {
732800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      *Shadow = 0;  // fully addressable
733800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    } else if (i >= Size) {
734800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      *Shadow = Magic;  // unaddressable
735800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    } else {
736800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      *Shadow = Size - i;  // first Size-i bytes are addressable
737800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
738800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
739800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
740800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
741800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyvoid AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
742800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                   IRBuilder<> IRB,
743800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                   Value *ShadowBase, bool DoPoison) {
744800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  size_t ShadowRZSize = RedzoneSize >> MappingScale;
745800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
746800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
747800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *RZPtrTy = PointerType::get(RZTy, 0);
748800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
749800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *PoisonLeft  = ConstantInt::get(RZTy,
750800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
751800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *PoisonMid   = ConstantInt::get(RZTy,
752800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
753800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *PoisonRight = ConstantInt::get(RZTy,
754800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
755800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
756800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // poison the first red zone.
757800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
758800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
759800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // poison all other red zones.
760800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t Pos = RedzoneSize;
761800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
762800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    AllocaInst *AI = AllocaVec[i];
763800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
764800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    uint64_t AlignedSize = getAlignedAllocaSize(AI);
765800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(AlignedSize - SizeInBytes < RedzoneSize);
766800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *Ptr = NULL;
767800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
768800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Pos += AlignedSize;
769800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
770800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert(ShadowBase->getType() == IntptrTy);
771800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (SizeInBytes < AlignedSize) {
772800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      // Poison the partial redzone at right
773800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      Ptr = IRB.CreateAdd(
774800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          ShadowBase, ConstantInt::get(IntptrTy,
775800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                       (Pos >> MappingScale) - ShadowRZSize));
776800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      size_t AddressableBytes = RedzoneSize - (AlignedSize - SizeInBytes);
777800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      uint32_t Poison = 0;
778800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (DoPoison) {
779800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
780800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                        RedzoneSize,
781800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                        1ULL << MappingScale,
782800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                        kAsanStackPartialRedzoneMagic);
783800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      }
784800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      Value *PartialPoison = ConstantInt::get(RZTy, Poison);
785800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
786800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
787800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
788800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Poison the full redzone at right.
789800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Ptr = IRB.CreateAdd(ShadowBase,
790800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                        ConstantInt::get(IntptrTy, Pos >> MappingScale));
791800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
792800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
793800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
794800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Pos += RedzoneSize;
795800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
796800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
797800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
7985a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany// Workaround for bug 11395: we don't want to instrument stack in functions
7995a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
800d2703dec271d82c8c9d22afb835c07730fd25d47Kostya Serebryany// FIXME: remove once the bug 11395 is fixed.
8015a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryanybool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
8025a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  if (LongSize != 32) return false;
8035a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  CallInst *CI = dyn_cast<CallInst>(I);
8045a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  if (!CI || !CI->isInlineAsm()) return false;
8055a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  if (CI->getNumArgOperands() <= 5) return false;
8065a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  // We have inline assembly with quite a few arguments.
8075a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany  return true;
8085a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany}
8095a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany
810800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Find all static Alloca instructions and put
811800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// poisoned red zones around all of them.
812800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Then unpoison everything back before the function returns.
813800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//
814800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Stack poisoning does not play well with exception handling.
815800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// When an exception is thrown, we essentially bypass the code
816800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// that unpoisones the stack. This is why the run-time library has
817800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
818800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// stack in the interceptor. This however does not work inside the
819800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// actual function which catches the exception. Most likely because the
820800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// compiler hoists the load of the shadow value somewhere too high.
821800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This causes asan to report a non-existing bug on 453.povray.
822800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// It sounds like an LLVM bug.
823800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanybool AddressSanitizer::poisonStackInFunction(Module &M, Function &F) {
824800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (!ClStack) return false;
825800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallVector<AllocaInst*, 16> AllocaVec;
826800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallVector<Instruction*, 8> RetVec;
827800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t TotalSize = 0;
828800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
829800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Filter out Alloca instructions we want (and can) handle.
830800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Collect Ret instructions.
831800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (Function::iterator FI = F.begin(), FE = F.end();
832800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany       FI != FE; ++FI) {
833800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    BasicBlock &BB = *FI;
834800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
835800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany         BI != BE; ++BI) {
836800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (isa<ReturnInst>(BI)) {
837800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          RetVec.push_back(BI);
838800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany          continue;
839800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      }
840800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
841800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      AllocaInst *AI = dyn_cast<AllocaInst>(BI);
842800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (!AI) continue;
843800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (AI->isArrayAllocation()) continue;
844800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (!AI->isStaticAlloca()) continue;
845800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (!AI->getAllocatedType()->isSized()) continue;
846800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      if (AI->getAlignment() > RedzoneSize) continue;
847800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      AllocaVec.push_back(AI);
848800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      uint64_t AlignedSize =  getAlignedAllocaSize(AI);
849800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      TotalSize += AlignedSize;
850800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
851800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
852800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
853800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (AllocaVec.empty()) return false;
854800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
855800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize;
856800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
857800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  bool DoStackMalloc = ClUseAfterReturn
858800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      && LocalStackSize <= kMaxStackMallocSize;
859800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
860800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Instruction *InsBefore = AllocaVec[0];
861800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRBuilder<> IRB(InsBefore);
862800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
863800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
864800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
865800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  AllocaInst *MyAlloca =
866800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
867800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  MyAlloca->setAlignment(RedzoneSize);
868800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  assert(MyAlloca->isStaticAlloca());
869800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
870800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *LocalStackBase = OrigStackBase;
871800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
872800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (DoStackMalloc) {
873800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Value *AsanStackMallocFunc = M.getOrInsertFunction(
874800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL);
875800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
876800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
877800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
878800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
879800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // This string will be parsed by the run-time (DescribeStackAddress).
880800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallString<2048> StackDescriptionStorage;
881800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  raw_svector_ostream StackDescription(StackDescriptionStorage);
882800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  StackDescription << F.getName() << " " << AllocaVec.size() << " ";
883800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
884800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  uint64_t Pos = RedzoneSize;
885800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Replace Alloca instructions with base+offset.
886800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
887800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    AllocaInst *AI = AllocaVec[i];
888800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
889800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    StringRef Name = AI->getName();
890800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    StackDescription << Pos << " " << SizeInBytes << " "
891800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                     << Name.size() << " " << Name << " ";
892800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    uint64_t AlignedSize = getAlignedAllocaSize(AI);
893800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    assert((AlignedSize % RedzoneSize) == 0);
894800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    AI->replaceAllUsesWith(
895800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        IRB.CreateIntToPtr(
896800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany            IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
897800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany            AI->getType()));
898800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Pos += AlignedSize + RedzoneSize;
899800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
900800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  assert(Pos == LocalStackSize);
901800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
902800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Write the Magic value and the frame description constant to the redzone.
903800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
904800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
905800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                  BasePlus0);
906800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
907800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                                   ConstantInt::get(IntptrTy, LongSize/8));
908800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
909800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *Description = IRB.CreatePointerCast(
910800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      createPrivateGlobalForString(M, StackDescription.str()),
911800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      IntptrTy);
912800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  IRB.CreateStore(Description, BasePlus1);
913800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
914800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Poison the stack redzones at the entry.
915800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *ShadowBase = memToShadow(LocalStackBase, IRB);
916800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
917800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
918800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Value *AsanStackFreeFunc = NULL;
919800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (DoStackMalloc) {
920800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    AsanStackFreeFunc = M.getOrInsertFunction(
921800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        kAsanStackFreeName, IRB.getVoidTy(),
922800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        IntptrTy, IntptrTy, IntptrTy, NULL);
923800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
924800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
925800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  // Unpoison the stack before all ret instructions.
926800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (size_t i = 0, n = RetVec.size(); i < n; i++) {
927800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Instruction *Ret = RetVec[i];
928800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    IRBuilder<> IRBRet(Ret);
929800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
930800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Mark the current frame as retired.
931800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
932800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                       BasePlus0);
933800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    // Unpoison the stack.
934800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
935800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
936800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (DoStackMalloc) {
937800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
938800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                         ConstantInt::get(IntptrTy, LocalStackSize),
939800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany                         OrigStackBase);
940800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
941800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
942800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
943800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (ClDebugStack) {
944800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    DEBUG(dbgs() << F);
945800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
946800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
947800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return true;
948800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
949800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
950800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyBlackList::BlackList(const std::string &Path) {
951800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  Functions = NULL;
952800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  const char *kFunPrefix = "fun:";
953800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (!ClBlackListFile.size()) return;
954800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  std::string Fun;
955800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
956800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  OwningPtr<MemoryBuffer> File;
957800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (error_code EC = MemoryBuffer::getFile(ClBlackListFile.c_str(), File)) {
958cc1d856d8e8e11407b3c6c1d08768f77c3722e38Kostya Serebryany    report_fatal_error("Can't open blacklist file " + ClBlackListFile + ": " +
959cc1d856d8e8e11407b3c6c1d08768f77c3722e38Kostya Serebryany                       EC.message());
960800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
961800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  MemoryBuffer *Buff = File.take();
962800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  const char *Data = Buff->getBufferStart();
963800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  size_t DataLen = Buff->getBufferSize();
964800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SmallVector<StringRef, 16> Lines;
965800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  SplitString(StringRef(Data, DataLen), Lines, "\n\r");
966800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  for (size_t i = 0, numLines = Lines.size(); i < numLines; i++) {
967800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    if (Lines[i].startswith(kFunPrefix)) {
968800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      std::string ThisFunc = Lines[i].substr(strlen(kFunPrefix));
969085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      std::string ThisFuncRE;
970800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      // add ThisFunc replacing * with .*
971800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      for (size_t j = 0, n = ThisFunc.size(); j < n; j++) {
972800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany        if (ThisFunc[j] == '*')
973085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany          ThisFuncRE += '.';
974085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany        ThisFuncRE += ThisFunc[j];
975800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany      }
976085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      // Check that the regexp is valid.
977085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      Regex CheckRE(ThisFuncRE);
978085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      std::string Error;
979085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      if (!CheckRE.isValid(Error))
980085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany        report_fatal_error("malformed blacklist regex: " + ThisFunc +
981085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany                           ": " + Error);
982085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      // Append to the final regexp.
983085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      if (Fun.size())
984085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany        Fun += "|";
985085cb8f0b957841ab667a46be0c49ce4ee8c9c79Kostya Serebryany      Fun += ThisFuncRE;
986800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    }
987800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
988800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (Fun.size()) {
989800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    Functions = new Regex(Fun);
990800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
991800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
992800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany
993800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanybool BlackList::isIn(const Function &F) {
994800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  if (Functions) {
995800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    bool Res = Functions->match(F.getName());
996800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany    return Res;
997800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  }
998800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany  return false;
999800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}
1000