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