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