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