Kostya Serebryany | 6e6b03e | 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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Instrumentation.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseMap.h" |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseSet.h" |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DepthFirstIterator.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallSet.h" |
| 22 | #include "llvm/ADT/SmallString.h" |
| 23 | #include "llvm/ADT/SmallVector.h" |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Evgeniy Stepanov | 617232f | 2012-05-23 11:52:12 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 27 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 12664a0 | 2014-03-06 00:22:06 +0000 | [diff] [blame] | 28 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/DataLayout.h" |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Function.h" |
| 32 | #include "llvm/IR/IRBuilder.h" |
| 33 | #include "llvm/IR/InlineAsm.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 34 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 35 | #include "llvm/IR/IntrinsicInst.h" |
| 36 | #include "llvm/IR/LLVMContext.h" |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 37 | #include "llvm/IR/MDBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Module.h" |
| 39 | #include "llvm/IR/Type.h" |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 40 | #include "llvm/MC/MCSectionMachO.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 41 | #include "llvm/Support/CommandLine.h" |
| 42 | #include "llvm/Support/DataTypes.h" |
| 43 | #include "llvm/Support/Debug.h" |
Kostya Serebryany | 9e62b30 | 2013-06-03 14:46:56 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Endian.h" |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 45 | #include "llvm/Support/SwapByteOrder.h" |
Kostya Serebryany | 351b078 | 2014-09-03 22:37:37 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/Scalar.h" |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/ASanStackFrameLayout.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/Utils/Cloning.h" |
Alexey Samsonov | 3d43b63 | 2012-12-12 14:31:53 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/Utils/Local.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 52 | #include <algorithm> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 53 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 54 | #include <system_error> |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 55 | |
| 56 | using namespace llvm; |
| 57 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 58 | #define DEBUG_TYPE "asan" |
| 59 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 60 | static const uint64_t kDefaultShadowScale = 3; |
| 61 | static const uint64_t kDefaultShadowOffset32 = 1ULL << 29; |
Alexander Potapenko | a51e483 | 2014-04-23 17:14:45 +0000 | [diff] [blame] | 62 | static const uint64_t kIOSShadowOffset32 = 1ULL << 30; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 63 | static const uint64_t kDefaultShadowOffset64 = 1ULL << 44; |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 64 | static const uint64_t kSmallX86_64ShadowOffset = 0x7FFF8000; // < 2G. |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 65 | static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41; |
Kostya Serebryany | c5bd981 | 2014-11-04 19:46:15 +0000 | [diff] [blame] | 66 | static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000; |
Kostya Serebryany | 231bd08 | 2014-11-11 23:02:57 +0000 | [diff] [blame] | 67 | static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 36; |
Kostya Serebryany | 8baa386 | 2014-02-10 07:37:04 +0000 | [diff] [blame] | 68 | static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30; |
| 69 | static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 70 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 71 | static const size_t kMinStackMallocSize = 1 << 6; // 64B |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 72 | static const size_t kMaxStackMallocSize = 1 << 16; // 64K |
| 73 | static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; |
| 74 | static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; |
| 75 | |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 76 | static const char *const kAsanModuleCtorName = "asan.module_ctor"; |
| 77 | static const char *const kAsanModuleDtorName = "asan.module_dtor"; |
Kostya Serebryany | 34ddf87 | 2014-09-24 22:41:55 +0000 | [diff] [blame] | 78 | static const uint64_t kAsanCtorAndDtorPriority = 1; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 79 | static const char *const kAsanReportErrorTemplate = "__asan_report_"; |
| 80 | static const char *const kAsanReportLoadN = "__asan_report_load_n"; |
| 81 | static const char *const kAsanReportStoreN = "__asan_report_store_n"; |
| 82 | static const char *const kAsanRegisterGlobalsName = "__asan_register_globals"; |
Alexey Samsonov | f52b717 | 2013-08-05 13:19:49 +0000 | [diff] [blame] | 83 | static const char *const kAsanUnregisterGlobalsName = |
| 84 | "__asan_unregister_globals"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 85 | static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init"; |
| 86 | static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init"; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 87 | static const char *const kAsanInitName = "__asan_init_v4"; |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 88 | static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp"; |
| 89 | static const char *const kAsanPtrSub = "__sanitizer_ptr_sub"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 90 | static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return"; |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 91 | static const int kMaxAsanStackMallocSizeClass = 10; |
| 92 | static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_"; |
| 93 | static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 94 | static const char *const kAsanGenPrefix = "__asan_gen_"; |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 95 | static const char *const kSanCovGenPrefix = "__sancov_gen_"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 96 | static const char *const kAsanPoisonStackMemoryName = |
| 97 | "__asan_poison_stack_memory"; |
| 98 | static const char *const kAsanUnpoisonStackMemoryName = |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 99 | "__asan_unpoison_stack_memory"; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 100 | |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 101 | static const char *const kAsanOptionDetectUAR = |
| 102 | "__asan_option_detect_stack_use_after_return"; |
| 103 | |
David Blaikie | eacc287 | 2013-09-18 00:11:27 +0000 | [diff] [blame] | 104 | #ifndef NDEBUG |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 105 | static const int kAsanStackAfterReturnMagic = 0xf5; |
David Blaikie | eacc287 | 2013-09-18 00:11:27 +0000 | [diff] [blame] | 106 | #endif |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 107 | |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 108 | // Accesses sizes are powers of two: 1, 2, 4, 8, 16. |
| 109 | static const size_t kNumberOfAccessSizes = 5; |
| 110 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 111 | static const unsigned kAllocaRzSize = 32; |
| 112 | static const unsigned kAsanAllocaLeftMagic = 0xcacacacaU; |
| 113 | static const unsigned kAsanAllocaRightMagic = 0xcbcbcbcbU; |
| 114 | static const unsigned kAsanAllocaPartialVal1 = 0xcbcbcb00U; |
| 115 | static const unsigned kAsanAllocaPartialVal2 = 0x000000cbU; |
| 116 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 117 | // Command-line flags. |
| 118 | |
| 119 | // This flag may need to be replaced with -f[no-]asan-reads. |
| 120 | static cl::opt<bool> ClInstrumentReads("asan-instrument-reads", |
| 121 | cl::desc("instrument read instructions"), cl::Hidden, cl::init(true)); |
| 122 | static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes", |
| 123 | cl::desc("instrument write instructions"), cl::Hidden, cl::init(true)); |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 124 | static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics", |
| 125 | cl::desc("instrument atomic instructions (rmw, cmpxchg)"), |
| 126 | cl::Hidden, cl::init(true)); |
Kostya Serebryany | 1e575ab | 2012-08-15 08:58:58 +0000 | [diff] [blame] | 127 | static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path", |
| 128 | cl::desc("use instrumentation with slow path for all accesses"), |
| 129 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 130 | // This flag limits the number of instructions to be instrumented |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 131 | // in any given BB. Normally, this should be set to unlimited (INT_MAX), |
| 132 | // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary |
| 133 | // set it to 10000. |
| 134 | static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb", |
| 135 | cl::init(10000), |
| 136 | cl::desc("maximal number of instructions to instrument in any given BB"), |
| 137 | cl::Hidden); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 138 | // This flag may need to be replaced with -f[no]asan-stack. |
| 139 | static cl::opt<bool> ClStack("asan-stack", |
| 140 | cl::desc("Handle stack memory"), cl::Hidden, cl::init(true)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 141 | static cl::opt<bool> ClUseAfterReturn("asan-use-after-return", |
Alexey Samsonov | e595e1a | 2014-06-13 17:53:44 +0000 | [diff] [blame] | 142 | cl::desc("Check return-after-free"), cl::Hidden, cl::init(true)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 143 | // This flag may need to be replaced with -f[no]asan-globals. |
| 144 | static cl::opt<bool> ClGlobals("asan-globals", |
| 145 | cl::desc("Handle global objects"), cl::Hidden, cl::init(true)); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 146 | static cl::opt<bool> ClInitializers("asan-initialization-order", |
Alexey Samsonov | e595e1a | 2014-06-13 17:53:44 +0000 | [diff] [blame] | 147 | cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(true)); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 148 | static cl::opt<bool> ClInvalidPointerPairs("asan-detect-invalid-pointer-pair", |
| 149 | cl::desc("Instrument <, <=, >, >=, - with pointer operands"), |
Kostya Serebryany | ec34665 | 2014-02-27 12:56:20 +0000 | [diff] [blame] | 150 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 151 | static cl::opt<unsigned> ClRealignStack("asan-realign-stack", |
| 152 | cl::desc("Realign stack to the value of this flag (power of two)"), |
| 153 | cl::Hidden, cl::init(32)); |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 154 | static cl::opt<int> ClInstrumentationWithCallsThreshold( |
| 155 | "asan-instrumentation-with-call-threshold", |
| 156 | cl::desc("If the function being instrumented contains more than " |
| 157 | "this number of memory accesses, use callbacks instead of " |
| 158 | "inline checks (-1 means never use callbacks)."), |
Kostya Serebryany | 4d237a8 | 2014-05-26 11:57:16 +0000 | [diff] [blame] | 159 | cl::Hidden, cl::init(7000)); |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 160 | static cl::opt<std::string> ClMemoryAccessCallbackPrefix( |
| 161 | "asan-memory-access-callback-prefix", |
| 162 | cl::desc("Prefix for memory access callbacks"), cl::Hidden, |
| 163 | cl::init("__asan_")); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 164 | static cl::opt<bool> ClInstrumentAllocas("asan-instrument-allocas", |
| 165 | cl::desc("instrument dynamic allocas"), cl::Hidden, cl::init(false)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 166 | |
| 167 | // These flags allow to change the shadow mapping. |
| 168 | // The shadow mapping looks like |
| 169 | // Shadow = (Mem >> scale) + (1 << offset_log) |
| 170 | static cl::opt<int> ClMappingScale("asan-mapping-scale", |
| 171 | cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 172 | |
| 173 | // Optimization flags. Not user visible, used mostly for testing |
| 174 | // and benchmarking the tool. |
| 175 | static cl::opt<bool> ClOpt("asan-opt", |
| 176 | cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true)); |
| 177 | static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp", |
| 178 | cl::desc("Instrument the same temp just once"), cl::Hidden, |
| 179 | cl::init(true)); |
| 180 | static cl::opt<bool> ClOptGlobals("asan-opt-globals", |
| 181 | cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true)); |
| 182 | |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 183 | static cl::opt<bool> ClCheckLifetime("asan-check-lifetime", |
| 184 | cl::desc("Use llvm.lifetime intrinsics to insert extra checks"), |
| 185 | cl::Hidden, cl::init(false)); |
| 186 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 187 | // Debug flags. |
| 188 | static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden, |
| 189 | cl::init(0)); |
| 190 | static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"), |
| 191 | cl::Hidden, cl::init(0)); |
| 192 | static cl::opt<std::string> ClDebugFunc("asan-debug-func", |
| 193 | cl::Hidden, cl::desc("Debug func")); |
| 194 | static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"), |
| 195 | cl::Hidden, cl::init(-1)); |
| 196 | static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"), |
| 197 | cl::Hidden, cl::init(-1)); |
| 198 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 199 | STATISTIC(NumInstrumentedReads, "Number of instrumented reads"); |
| 200 | STATISTIC(NumInstrumentedWrites, "Number of instrumented writes"); |
Kostya Serebryany | ea2cb6f | 2014-11-21 21:25:18 +0000 | [diff] [blame] | 201 | STATISTIC(NumInstrumentedDynamicAllocas, |
| 202 | "Number of instrumented dynamic allocas"); |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 203 | STATISTIC(NumOptimizedAccessesToGlobalArray, |
| 204 | "Number of optimized accesses to global arrays"); |
| 205 | STATISTIC(NumOptimizedAccessesToGlobalVar, |
| 206 | "Number of optimized accesses to global vars"); |
| 207 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 208 | namespace { |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 209 | /// Frontend-provided metadata for source location. |
| 210 | struct LocationMetadata { |
| 211 | StringRef Filename; |
| 212 | int LineNo; |
| 213 | int ColumnNo; |
| 214 | |
| 215 | LocationMetadata() : Filename(), LineNo(0), ColumnNo(0) {} |
| 216 | |
| 217 | bool empty() const { return Filename.empty(); } |
| 218 | |
| 219 | void parse(MDNode *MDN) { |
| 220 | assert(MDN->getNumOperands() == 3); |
| 221 | MDString *MDFilename = cast<MDString>(MDN->getOperand(0)); |
| 222 | Filename = MDFilename->getString(); |
| 223 | LineNo = cast<ConstantInt>(MDN->getOperand(1))->getLimitedValue(); |
| 224 | ColumnNo = cast<ConstantInt>(MDN->getOperand(2))->getLimitedValue(); |
| 225 | } |
| 226 | }; |
| 227 | |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 228 | /// Frontend-provided metadata for global variables. |
| 229 | class GlobalsMetadata { |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 230 | public: |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 231 | struct Entry { |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 232 | Entry() |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 233 | : SourceLoc(), Name(), IsDynInit(false), |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 234 | IsBlacklisted(false) {} |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 235 | LocationMetadata SourceLoc; |
| 236 | StringRef Name; |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 237 | bool IsDynInit; |
| 238 | bool IsBlacklisted; |
| 239 | }; |
| 240 | |
Alexey Samsonov | 0c5ecdd | 2014-07-02 20:25:42 +0000 | [diff] [blame] | 241 | GlobalsMetadata() : inited_(false) {} |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 242 | |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 243 | void init(Module& M) { |
| 244 | assert(!inited_); |
| 245 | inited_ = true; |
| 246 | NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals"); |
| 247 | if (!Globals) |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 248 | return; |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 249 | for (auto MDN : Globals->operands()) { |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 250 | // Metadata node contains the global and the fields of "Entry". |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 251 | assert(MDN->getNumOperands() == 5); |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 252 | Value *V = MDN->getOperand(0); |
| 253 | // The optimizer may optimize away a global entirely. |
| 254 | if (!V) |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 255 | continue; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 256 | GlobalVariable *GV = cast<GlobalVariable>(V); |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 257 | // We can already have an entry for GV if it was merged with another |
| 258 | // global. |
| 259 | Entry &E = Entries[GV]; |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 260 | if (Value *Loc = MDN->getOperand(1)) |
| 261 | E.SourceLoc.parse(cast<MDNode>(Loc)); |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 262 | if (Value *Name = MDN->getOperand(2)) { |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 263 | MDString *MDName = cast<MDString>(Name); |
| 264 | E.Name = MDName->getString(); |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 265 | } |
| 266 | ConstantInt *IsDynInit = cast<ConstantInt>(MDN->getOperand(3)); |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 267 | E.IsDynInit |= IsDynInit->isOne(); |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 268 | ConstantInt *IsBlacklisted = cast<ConstantInt>(MDN->getOperand(4)); |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 269 | E.IsBlacklisted |= IsBlacklisted->isOne(); |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 272 | |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 273 | /// Returns metadata entry for a given global. |
| 274 | Entry get(GlobalVariable *G) const { |
| 275 | auto Pos = Entries.find(G); |
| 276 | return (Pos != Entries.end()) ? Pos->second : Entry(); |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 279 | private: |
Alexey Samsonov | 0c5ecdd | 2014-07-02 20:25:42 +0000 | [diff] [blame] | 280 | bool inited_; |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 281 | DenseMap<GlobalVariable*, Entry> Entries; |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 282 | }; |
| 283 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 284 | /// This struct defines the shadow mapping using the rule: |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 285 | /// shadow = (mem >> Scale) ADD-or-OR Offset. |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 286 | struct ShadowMapping { |
| 287 | int Scale; |
| 288 | uint64_t Offset; |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 289 | bool OrShadowOffset; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 290 | }; |
| 291 | |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 292 | static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize) { |
Alexey Samsonov | 347bcd3 | 2013-01-17 11:12:32 +0000 | [diff] [blame] | 293 | bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android; |
Bob Wilson | 9868d71 | 2014-10-09 05:43:30 +0000 | [diff] [blame] | 294 | bool IsIOS = TargetTriple.isiOS(); |
Simon Pilgrim | a279410 | 2014-11-22 19:12:10 +0000 | [diff] [blame] | 295 | bool IsFreeBSD = TargetTriple.isOSFreeBSD(); |
| 296 | bool IsLinux = TargetTriple.isOSLinux(); |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 297 | bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64 || |
| 298 | TargetTriple.getArch() == llvm::Triple::ppc64le; |
Kostya Serebryany | be73337 | 2013-02-12 11:11:02 +0000 | [diff] [blame] | 299 | bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64; |
Kostya Serebryany | 9e62b30 | 2013-06-03 14:46:56 +0000 | [diff] [blame] | 300 | bool IsMIPS32 = TargetTriple.getArch() == llvm::Triple::mips || |
| 301 | TargetTriple.getArch() == llvm::Triple::mipsel; |
Kostya Serebryany | 231bd08 | 2014-11-11 23:02:57 +0000 | [diff] [blame] | 302 | bool IsMIPS64 = TargetTriple.getArch() == llvm::Triple::mips64 || |
| 303 | TargetTriple.getArch() == llvm::Triple::mips64el; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 304 | |
| 305 | ShadowMapping Mapping; |
| 306 | |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 307 | if (LongSize == 32) { |
| 308 | if (IsAndroid) |
| 309 | Mapping.Offset = 0; |
| 310 | else if (IsMIPS32) |
| 311 | Mapping.Offset = kMIPS32_ShadowOffset32; |
| 312 | else if (IsFreeBSD) |
| 313 | Mapping.Offset = kFreeBSD_ShadowOffset32; |
Alexander Potapenko | a51e483 | 2014-04-23 17:14:45 +0000 | [diff] [blame] | 314 | else if (IsIOS) |
| 315 | Mapping.Offset = kIOSShadowOffset32; |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 316 | else |
| 317 | Mapping.Offset = kDefaultShadowOffset32; |
| 318 | } else { // LongSize == 64 |
| 319 | if (IsPPC64) |
| 320 | Mapping.Offset = kPPC64_ShadowOffset64; |
| 321 | else if (IsFreeBSD) |
| 322 | Mapping.Offset = kFreeBSD_ShadowOffset64; |
| 323 | else if (IsLinux && IsX86_64) |
| 324 | Mapping.Offset = kSmallX86_64ShadowOffset; |
Kostya Serebryany | 231bd08 | 2014-11-11 23:02:57 +0000 | [diff] [blame] | 325 | else if (IsMIPS64) |
| 326 | Mapping.Offset = kMIPS64_ShadowOffset64; |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 327 | else |
| 328 | Mapping.Offset = kDefaultShadowOffset64; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | Mapping.Scale = kDefaultShadowScale; |
| 332 | if (ClMappingScale) { |
| 333 | Mapping.Scale = ClMappingScale; |
| 334 | } |
| 335 | |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 336 | // OR-ing shadow offset if more efficient (at least on x86) if the offset |
| 337 | // is a power of two, but on ppc64 we have to use add since the shadow |
| 338 | // offset is not necessary 1/8-th of the address space. |
| 339 | Mapping.OrShadowOffset = !IsPPC64 && !(Mapping.Offset & (Mapping.Offset - 1)); |
| 340 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 341 | return Mapping; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 344 | static size_t RedzoneSizeForScale(int MappingScale) { |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 345 | // Redzone used for stack and globals is at least 32 bytes. |
| 346 | // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively. |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 347 | return std::max(32U, 1U << MappingScale); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 348 | } |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 349 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 350 | /// AddressSanitizer: instrument the code in module to find memory bugs. |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 351 | struct AddressSanitizer : public FunctionPass { |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 352 | AddressSanitizer() : FunctionPass(ID) { |
| 353 | initializeAddressSanitizerPass(*PassRegistry::getPassRegistry()); |
| 354 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 355 | const char *getPassName() const override { |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 356 | return "AddressSanitizerFunctionPass"; |
| 357 | } |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 358 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 359 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 360 | } |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 361 | void instrumentMop(Instruction *I, bool UseCalls); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 362 | void instrumentPointerComparisonOrSubtraction(Instruction *I); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 363 | void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore, |
| 364 | Value *Addr, uint32_t TypeSize, bool IsWrite, |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 365 | Value *SizeArgument, bool UseCalls); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 366 | Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, |
| 367 | Value *ShadowValue, uint32_t TypeSize); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 368 | Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr, |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 369 | bool IsWrite, size_t AccessSizeIndex, |
| 370 | Value *SizeArgument); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 371 | void instrumentMemIntrinsic(MemIntrinsic *MI); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 372 | Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 373 | bool runOnFunction(Function &F) override; |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 374 | bool maybeInsertAsanInitAtFunctionEntry(Function &F); |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 375 | bool doInitialization(Module &M) override; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 376 | static char ID; // Pass identification, replacement for typeid |
| 377 | |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 378 | DominatorTree &getDominatorTree() const { return *DT; } |
| 379 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 380 | private: |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 381 | void initializeCallbacks(Module &M); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 382 | |
Kostya Serebryany | 1cdc6e9 | 2011-11-18 01:41:06 +0000 | [diff] [blame] | 383 | bool LooksLikeCodeInBug11395(Instruction *I); |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 384 | bool GlobalIsLinkerInitialized(GlobalVariable *G); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 385 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 386 | LLVMContext *C; |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 387 | const DataLayout *DL; |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 388 | Triple TargetTriple; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 389 | int LongSize; |
| 390 | Type *IntptrTy; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 391 | ShadowMapping Mapping; |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 392 | DominatorTree *DT; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 393 | Function *AsanCtorFunction; |
| 394 | Function *AsanInitFunction; |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 395 | Function *AsanHandleNoReturnFunc; |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 396 | Function *AsanPtrCmpFunction, *AsanPtrSubFunction; |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 397 | // This array is indexed by AccessIsWrite and log2(AccessSize). |
| 398 | Function *AsanErrorCallback[2][kNumberOfAccessSizes]; |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 399 | Function *AsanMemoryAccessCallback[2][kNumberOfAccessSizes]; |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 400 | // This array is indexed by AccessIsWrite. |
Kostya Serebryany | 86332c0 | 2014-04-21 07:10:43 +0000 | [diff] [blame] | 401 | Function *AsanErrorCallbackSized[2], |
| 402 | *AsanMemoryAccessCallbackSized[2]; |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 403 | Function *AsanMemmove, *AsanMemcpy, *AsanMemset; |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 404 | InlineAsm *EmptyAsm; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 405 | GlobalsMetadata GlobalsMD; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 406 | |
| 407 | friend struct FunctionStackPoisoner; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 408 | }; |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 409 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 410 | class AddressSanitizerModule : public ModulePass { |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 411 | public: |
Alexey Samsonov | c94285a | 2014-07-08 00:50:49 +0000 | [diff] [blame] | 412 | AddressSanitizerModule() : ModulePass(ID) {} |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 413 | bool runOnModule(Module &M) override; |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 414 | static char ID; // Pass identification, replacement for typeid |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 415 | const char *getPassName() const override { |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 416 | return "AddressSanitizerModule"; |
| 417 | } |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 418 | |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 419 | private: |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 420 | void initializeCallbacks(Module &M); |
| 421 | |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 422 | bool InstrumentGlobals(IRBuilder<> &IRB, Module &M); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 423 | bool ShouldInstrumentGlobal(GlobalVariable *G); |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 424 | void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName); |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 425 | void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 426 | size_t MinRedzoneSizeForGlobal() const { |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 427 | return RedzoneSizeForScale(Mapping.Scale); |
| 428 | } |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 429 | |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 430 | GlobalsMetadata GlobalsMD; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 431 | Type *IntptrTy; |
| 432 | LLVMContext *C; |
Rafael Espindola | aeff8a9 | 2014-02-24 23:12:18 +0000 | [diff] [blame] | 433 | const DataLayout *DL; |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 434 | Triple TargetTriple; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 435 | ShadowMapping Mapping; |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 436 | Function *AsanPoisonGlobals; |
| 437 | Function *AsanUnpoisonGlobals; |
| 438 | Function *AsanRegisterGlobals; |
| 439 | Function *AsanUnregisterGlobals; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 442 | // Stack poisoning does not play well with exception handling. |
| 443 | // When an exception is thrown, we essentially bypass the code |
| 444 | // that unpoisones the stack. This is why the run-time library has |
| 445 | // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire |
| 446 | // stack in the interceptor. This however does not work inside the |
| 447 | // actual function which catches the exception. Most likely because the |
| 448 | // compiler hoists the load of the shadow value somewhere too high. |
| 449 | // This causes asan to report a non-existing bug on 453.povray. |
| 450 | // It sounds like an LLVM bug. |
| 451 | struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> { |
| 452 | Function &F; |
| 453 | AddressSanitizer &ASan; |
| 454 | DIBuilder DIB; |
| 455 | LLVMContext *C; |
| 456 | Type *IntptrTy; |
| 457 | Type *IntptrPtrTy; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 458 | ShadowMapping Mapping; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 459 | |
| 460 | SmallVector<AllocaInst*, 16> AllocaVec; |
| 461 | SmallVector<Instruction*, 8> RetVec; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 462 | unsigned StackAlignment; |
| 463 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 464 | Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1], |
| 465 | *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1]; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 466 | Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc; |
| 467 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 468 | // Stores a place and arguments of poisoning/unpoisoning call for alloca. |
| 469 | struct AllocaPoisonCall { |
| 470 | IntrinsicInst *InsBefore; |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 471 | AllocaInst *AI; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 472 | uint64_t Size; |
| 473 | bool DoPoison; |
| 474 | }; |
| 475 | SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec; |
| 476 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 477 | // Stores left and right redzone shadow addresses for dynamic alloca |
| 478 | // and pointer to alloca instruction itself. |
| 479 | // LeftRzAddr is a shadow address for alloca left redzone. |
| 480 | // RightRzAddr is a shadow address for alloca right redzone. |
| 481 | struct DynamicAllocaCall { |
| 482 | AllocaInst *AI; |
| 483 | Value *LeftRzAddr; |
| 484 | Value *RightRzAddr; |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 485 | bool Poison; |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 486 | explicit DynamicAllocaCall(AllocaInst *AI, |
| 487 | Value *LeftRzAddr = nullptr, |
| 488 | Value *RightRzAddr = nullptr) |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 489 | : AI(AI), LeftRzAddr(LeftRzAddr), RightRzAddr(RightRzAddr), Poison(true) |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 490 | {} |
| 491 | }; |
| 492 | SmallVector<DynamicAllocaCall, 1> DynamicAllocaVec; |
| 493 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 494 | // Maps Value to an AllocaInst from which the Value is originated. |
| 495 | typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy; |
| 496 | AllocaForValueMapTy AllocaForValue; |
| 497 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 498 | FunctionStackPoisoner(Function &F, AddressSanitizer &ASan) |
| 499 | : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C), |
| 500 | IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)), |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 501 | Mapping(ASan.Mapping), |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 502 | StackAlignment(1 << Mapping.Scale) {} |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 503 | |
| 504 | bool runOnFunction() { |
| 505 | if (!ClStack) return false; |
| 506 | // Collect alloca, ret, lifetime instructions etc. |
David Blaikie | ceec2bd | 2014-04-11 01:50:01 +0000 | [diff] [blame] | 507 | for (BasicBlock *BB : depth_first(&F.getEntryBlock())) |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 508 | visit(*BB); |
David Blaikie | ceec2bd | 2014-04-11 01:50:01 +0000 | [diff] [blame] | 509 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 510 | if (AllocaVec.empty() && DynamicAllocaVec.empty()) return false; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 511 | |
| 512 | initializeCallbacks(*F.getParent()); |
| 513 | |
| 514 | poisonStack(); |
| 515 | |
| 516 | if (ClDebugStack) { |
| 517 | DEBUG(dbgs() << F); |
| 518 | } |
| 519 | return true; |
| 520 | } |
| 521 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 522 | // Finds all Alloca instructions and puts |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 523 | // poisoned red zones around all of them. |
| 524 | // Then unpoison everything back before the function returns. |
| 525 | void poisonStack(); |
| 526 | |
| 527 | // ----------------------- Visitors. |
| 528 | /// \brief Collect all Ret instructions. |
| 529 | void visitReturnInst(ReturnInst &RI) { |
| 530 | RetVec.push_back(&RI); |
| 531 | } |
| 532 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 533 | // Unpoison dynamic allocas redzones. |
| 534 | void unpoisonDynamicAlloca(DynamicAllocaCall &AllocaCall) { |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 535 | if (!AllocaCall.Poison) |
| 536 | return; |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 537 | for (auto Ret : RetVec) { |
| 538 | IRBuilder<> IRBRet(Ret); |
| 539 | PointerType *Int32PtrTy = PointerType::getUnqual(IRBRet.getInt32Ty()); |
| 540 | Value *Zero = Constant::getNullValue(IRBRet.getInt32Ty()); |
| 541 | Value *PartialRzAddr = IRBRet.CreateSub(AllocaCall.RightRzAddr, |
| 542 | ConstantInt::get(IntptrTy, 4)); |
| 543 | IRBRet.CreateStore(Zero, IRBRet.CreateIntToPtr(AllocaCall.LeftRzAddr, |
| 544 | Int32PtrTy)); |
| 545 | IRBRet.CreateStore(Zero, IRBRet.CreateIntToPtr(PartialRzAddr, |
| 546 | Int32PtrTy)); |
| 547 | IRBRet.CreateStore(Zero, IRBRet.CreateIntToPtr(AllocaCall.RightRzAddr, |
| 548 | Int32PtrTy)); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | // Right shift for BigEndian and left shift for LittleEndian. |
| 553 | Value *shiftAllocaMagic(Value *Val, IRBuilder<> &IRB, Value *Shift) { |
| 554 | return ASan.DL->isLittleEndian() ? IRB.CreateShl(Val, Shift) |
| 555 | : IRB.CreateLShr(Val, Shift); |
| 556 | } |
| 557 | |
| 558 | // Compute PartialRzMagic for dynamic alloca call. Since we don't know the |
| 559 | // size of requested memory until runtime, we should compute it dynamically. |
| 560 | // If PartialSize is 0, PartialRzMagic would contain kAsanAllocaRightMagic, |
| 561 | // otherwise it would contain the value that we will use to poison the |
| 562 | // partial redzone for alloca call. |
| 563 | Value *computePartialRzMagic(Value *PartialSize, IRBuilder<> &IRB); |
| 564 | |
| 565 | // Deploy and poison redzones around dynamic alloca call. To do this, we |
| 566 | // should replace this call with another one with changed parameters and |
| 567 | // replace all its uses with new address, so |
| 568 | // addr = alloca type, old_size, align |
| 569 | // is replaced by |
| 570 | // new_size = (old_size + additional_size) * sizeof(type) |
| 571 | // tmp = alloca i8, new_size, max(align, 32) |
| 572 | // addr = tmp + 32 (first 32 bytes are for the left redzone). |
| 573 | // Additional_size is added to make new memory allocation contain not only |
| 574 | // requested memory, but also left, partial and right redzones. |
| 575 | // After that, we should poison redzones: |
| 576 | // (1) Left redzone with kAsanAllocaLeftMagic. |
| 577 | // (2) Partial redzone with the value, computed in runtime by |
| 578 | // computePartialRzMagic function. |
| 579 | // (3) Right redzone with kAsanAllocaRightMagic. |
| 580 | void handleDynamicAllocaCall(DynamicAllocaCall &AllocaCall); |
| 581 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 582 | /// \brief Collect Alloca instructions we want (and can) handle. |
| 583 | void visitAllocaInst(AllocaInst &AI) { |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 584 | if (!isInterestingAlloca(AI)) return; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 585 | |
| 586 | StackAlignment = std::max(StackAlignment, AI.getAlignment()); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 587 | if (isDynamicAlloca(AI)) |
| 588 | DynamicAllocaVec.push_back(DynamicAllocaCall(&AI)); |
| 589 | else |
| 590 | AllocaVec.push_back(&AI); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 593 | /// \brief Collect lifetime intrinsic calls to check for use-after-scope |
| 594 | /// errors. |
| 595 | void visitIntrinsicInst(IntrinsicInst &II) { |
Alexey Samsonov | e595e1a | 2014-06-13 17:53:44 +0000 | [diff] [blame] | 596 | if (!ClCheckLifetime) return; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 597 | Intrinsic::ID ID = II.getIntrinsicID(); |
| 598 | if (ID != Intrinsic::lifetime_start && |
| 599 | ID != Intrinsic::lifetime_end) |
| 600 | return; |
| 601 | // Found lifetime intrinsic, add ASan instrumentation if necessary. |
| 602 | ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0)); |
| 603 | // If size argument is undefined, don't do anything. |
| 604 | if (Size->isMinusOne()) return; |
| 605 | // Check that size doesn't saturate uint64_t and can |
| 606 | // be stored in IntptrTy. |
| 607 | const uint64_t SizeValue = Size->getValue().getLimitedValue(); |
| 608 | if (SizeValue == ~0ULL || |
| 609 | !ConstantInt::isValueValidForType(IntptrTy, SizeValue)) |
| 610 | return; |
| 611 | // Find alloca instruction that corresponds to llvm.lifetime argument. |
| 612 | AllocaInst *AI = findAllocaForValue(II.getArgOperand(1)); |
| 613 | if (!AI) return; |
| 614 | bool DoPoison = (ID == Intrinsic::lifetime_end); |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 615 | AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison}; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 616 | AllocaPoisonCallVec.push_back(APC); |
| 617 | } |
| 618 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 619 | // ---------------------- Helpers. |
| 620 | void initializeCallbacks(Module &M); |
| 621 | |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 622 | bool doesDominateAllExits(const Instruction *I) const { |
| 623 | for (auto Ret : RetVec) { |
| 624 | if (!ASan.getDominatorTree().dominates(I, Ret)) |
| 625 | return false; |
| 626 | } |
| 627 | return true; |
| 628 | } |
| 629 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 630 | bool isDynamicAlloca(AllocaInst &AI) const { |
| 631 | return AI.isArrayAllocation() || !AI.isStaticAlloca(); |
| 632 | } |
| 633 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 634 | // Check if we want (and can) handle this alloca. |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 635 | bool isInterestingAlloca(AllocaInst &AI) const { |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 636 | return (AI.getAllocatedType()->isSized() && |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 637 | // alloca() may be called with 0 size, ignore it. |
| 638 | getAllocaSizeInBytes(&AI) > 0); |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 641 | uint64_t getAllocaSizeInBytes(AllocaInst *AI) const { |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 642 | Type *Ty = AI->getAllocatedType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 643 | uint64_t SizeInBytes = ASan.DL->getTypeAllocSize(Ty); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 644 | return SizeInBytes; |
| 645 | } |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 646 | /// Finds alloca where the value comes from. |
| 647 | AllocaInst *findAllocaForValue(Value *V); |
Craig Topper | 3af9722 | 2014-08-27 05:25:00 +0000 | [diff] [blame] | 648 | void poisonRedZones(ArrayRef<uint8_t> ShadowBytes, IRBuilder<> &IRB, |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 649 | Value *ShadowBase, bool DoPoison); |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 650 | void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 651 | |
| 652 | void SetShadowToStackAfterReturnInlined(IRBuilder<> &IRB, Value *ShadowBase, |
| 653 | int Size); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 654 | }; |
| 655 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 656 | } // namespace |
| 657 | |
| 658 | char AddressSanitizer::ID = 0; |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 659 | INITIALIZE_PASS_BEGIN(AddressSanitizer, "asan", |
| 660 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", |
| 661 | false, false) |
| 662 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 663 | INITIALIZE_PASS_END(AddressSanitizer, "asan", |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 664 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", |
| 665 | false, false) |
Alexey Samsonov | e595e1a | 2014-06-13 17:53:44 +0000 | [diff] [blame] | 666 | FunctionPass *llvm::createAddressSanitizerFunctionPass() { |
| 667 | return new AddressSanitizer(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 670 | char AddressSanitizerModule::ID = 0; |
| 671 | INITIALIZE_PASS(AddressSanitizerModule, "asan-module", |
| 672 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs." |
| 673 | "ModulePass", false, false) |
Alexey Samsonov | c94285a | 2014-07-08 00:50:49 +0000 | [diff] [blame] | 674 | ModulePass *llvm::createAddressSanitizerModulePass() { |
| 675 | return new AddressSanitizerModule(); |
Alexander Potapenko | c94cf8f | 2012-01-23 11:22:43 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 678 | static size_t TypeSizeToSizeIndex(uint32_t TypeSize) { |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 679 | size_t Res = countTrailingZeros(TypeSize / 8); |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 680 | assert(Res < kNumberOfAccessSizes); |
| 681 | return Res; |
| 682 | } |
| 683 | |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 684 | // \brief Create a constant for Str so that we can pass it to the run-time lib. |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 685 | static GlobalVariable *createPrivateGlobalForString( |
| 686 | Module &M, StringRef Str, bool AllowMerging) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 687 | Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 688 | // We use private linkage for module-local strings. If they can be merged |
| 689 | // with another one, we set the unnamed_addr attribute. |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 690 | GlobalVariable *GV = |
| 691 | new GlobalVariable(M, StrConst->getType(), true, |
Rafael Espindola | daeafb4 | 2014-02-19 17:23:20 +0000 | [diff] [blame] | 692 | GlobalValue::PrivateLinkage, StrConst, kAsanGenPrefix); |
| 693 | if (AllowMerging) |
| 694 | GV->setUnnamedAddr(true); |
Kostya Serebryany | 10cc12f | 2013-03-18 09:38:39 +0000 | [diff] [blame] | 695 | GV->setAlignment(1); // Strings may not be merged w/o setting align 1. |
| 696 | return GV; |
Kostya Serebryany | 139a937 | 2012-11-20 14:16:08 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 699 | /// \brief Create a global describing a source location. |
| 700 | static GlobalVariable *createPrivateGlobalForSourceLoc(Module &M, |
| 701 | LocationMetadata MD) { |
| 702 | Constant *LocData[] = { |
| 703 | createPrivateGlobalForString(M, MD.Filename, true), |
| 704 | ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.LineNo), |
| 705 | ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.ColumnNo), |
| 706 | }; |
| 707 | auto LocStruct = ConstantStruct::getAnon(LocData); |
| 708 | auto GV = new GlobalVariable(M, LocStruct->getType(), true, |
| 709 | GlobalValue::PrivateLinkage, LocStruct, |
| 710 | kAsanGenPrefix); |
| 711 | GV->setUnnamedAddr(true); |
| 712 | return GV; |
| 713 | } |
| 714 | |
Kostya Serebryany | 139a937 | 2012-11-20 14:16:08 +0000 | [diff] [blame] | 715 | static bool GlobalWasGeneratedByAsan(GlobalVariable *G) { |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 716 | return G->getName().find(kAsanGenPrefix) == 0 || |
| 717 | G->getName().find(kSanCovGenPrefix) == 0; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 720 | Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) { |
| 721 | // Shadow >> scale |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 722 | Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); |
| 723 | if (Mapping.Offset == 0) |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 724 | return Shadow; |
| 725 | // (Shadow >> scale) | offset |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 726 | if (Mapping.OrShadowOffset) |
| 727 | return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); |
| 728 | else |
| 729 | return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 732 | // Instrument memset/memmove/memcpy |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 733 | void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) { |
| 734 | IRBuilder<> IRB(MI); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 735 | if (isa<MemTransferInst>(MI)) { |
Evgeniy Stepanov | ed31ca4 | 2014-05-14 10:56:19 +0000 | [diff] [blame] | 736 | IRB.CreateCall3( |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 737 | isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy, |
| 738 | IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()), |
| 739 | IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()), |
| 740 | IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)); |
| 741 | } else if (isa<MemSetInst>(MI)) { |
Evgeniy Stepanov | ed31ca4 | 2014-05-14 10:56:19 +0000 | [diff] [blame] | 742 | IRB.CreateCall3( |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 743 | AsanMemset, |
| 744 | IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()), |
| 745 | IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false), |
| 746 | IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 747 | } |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 748 | MI->eraseFromParent(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 751 | // If I is an interesting memory access, return the PointerOperand |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 752 | // and set IsWrite/Alignment. Otherwise return nullptr. |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 753 | static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite, |
| 754 | unsigned *Alignment) { |
Alexey Samsonov | 535b6f9 | 2014-07-17 18:48:12 +0000 | [diff] [blame] | 755 | // Skip memory accesses inserted by another instrumentation. |
| 756 | if (I->getMetadata("nosanitize")) |
| 757 | return nullptr; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 758 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 759 | if (!ClInstrumentReads) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 760 | *IsWrite = false; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 761 | *Alignment = LI->getAlignment(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 762 | return LI->getPointerOperand(); |
| 763 | } |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 764 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 765 | if (!ClInstrumentWrites) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 766 | *IsWrite = true; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 767 | *Alignment = SI->getAlignment(); |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 768 | return SI->getPointerOperand(); |
| 769 | } |
| 770 | if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 771 | if (!ClInstrumentAtomics) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 772 | *IsWrite = true; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 773 | *Alignment = 0; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 774 | return RMW->getPointerOperand(); |
| 775 | } |
| 776 | if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 777 | if (!ClInstrumentAtomics) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 778 | *IsWrite = true; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 779 | *Alignment = 0; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 780 | return XCHG->getPointerOperand(); |
| 781 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 782 | return nullptr; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 785 | static bool isPointerOperand(Value *V) { |
| 786 | return V->getType()->isPointerTy() || isa<PtrToIntInst>(V); |
| 787 | } |
| 788 | |
| 789 | // This is a rough heuristic; it may cause both false positives and |
| 790 | // false negatives. The proper implementation requires cooperation with |
| 791 | // the frontend. |
| 792 | static bool isInterestingPointerComparisonOrSubtraction(Instruction *I) { |
| 793 | if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) { |
| 794 | if (!Cmp->isRelational()) |
| 795 | return false; |
| 796 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Kostya Serebryany | cb3f6e1 | 2014-02-27 13:13:59 +0000 | [diff] [blame] | 797 | if (BO->getOpcode() != Instruction::Sub) |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 798 | return false; |
| 799 | } else { |
| 800 | return false; |
| 801 | } |
| 802 | if (!isPointerOperand(I->getOperand(0)) || |
| 803 | !isPointerOperand(I->getOperand(1))) |
| 804 | return false; |
| 805 | return true; |
| 806 | } |
| 807 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 808 | bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) { |
| 809 | // If a global variable does not have dynamic initialization we don't |
| 810 | // have to instrument it. However, if a global does not have initializer |
| 811 | // at all, we assume it has dynamic initializer (in other TU). |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 812 | return G->hasInitializer() && !GlobalsMD.get(G).IsDynInit; |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 815 | void |
| 816 | AddressSanitizer::instrumentPointerComparisonOrSubtraction(Instruction *I) { |
| 817 | IRBuilder<> IRB(I); |
| 818 | Function *F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction; |
| 819 | Value *Param[2] = {I->getOperand(0), I->getOperand(1)}; |
| 820 | for (int i = 0; i < 2; i++) { |
| 821 | if (Param[i]->getType()->isPointerTy()) |
| 822 | Param[i] = IRB.CreatePointerCast(Param[i], IntptrTy); |
| 823 | } |
| 824 | IRB.CreateCall2(F, Param[0], Param[1]); |
| 825 | } |
| 826 | |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 827 | void AddressSanitizer::instrumentMop(Instruction *I, bool UseCalls) { |
Axel Naumann | 4a12706 | 2012-09-17 14:20:57 +0000 | [diff] [blame] | 828 | bool IsWrite = false; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 829 | unsigned Alignment = 0; |
| 830 | Value *Addr = isInterestingMemoryAccess(I, &IsWrite, &Alignment); |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 831 | assert(Addr); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 832 | if (ClOpt && ClOptGlobals) { |
| 833 | if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) { |
| 834 | // If initialization order checking is disabled, a simple access to a |
| 835 | // dynamically initialized global is always valid. |
Alexey Samsonov | e595e1a | 2014-06-13 17:53:44 +0000 | [diff] [blame] | 836 | if (!ClInitializers || GlobalIsLinkerInitialized(G)) { |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 837 | NumOptimizedAccessesToGlobalVar++; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 838 | return; |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr); |
| 842 | if (CE && CE->isGEPWithNoNotionalOverIndexing()) { |
| 843 | if (GlobalVariable *G = dyn_cast<GlobalVariable>(CE->getOperand(0))) { |
| 844 | if (CE->getOperand(1)->isNullValue() && GlobalIsLinkerInitialized(G)) { |
| 845 | NumOptimizedAccessesToGlobalArray++; |
| 846 | return; |
| 847 | } |
| 848 | } |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 849 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 850 | } |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 851 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 852 | Type *OrigPtrTy = Addr->getType(); |
| 853 | Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType(); |
| 854 | |
| 855 | assert(OrigTy->isSized()); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 856 | uint32_t TypeSize = DL->getTypeStoreSizeInBits(OrigTy); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 857 | |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 858 | assert((TypeSize % 8) == 0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 859 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 860 | if (IsWrite) |
| 861 | NumInstrumentedWrites++; |
| 862 | else |
| 863 | NumInstrumentedReads++; |
| 864 | |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 865 | unsigned Granularity = 1 << Mapping.Scale; |
| 866 | // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check |
| 867 | // if the data is properly aligned. |
| 868 | if ((TypeSize == 8 || TypeSize == 16 || TypeSize == 32 || TypeSize == 64 || |
| 869 | TypeSize == 128) && |
| 870 | (Alignment >= Granularity || Alignment == 0 || Alignment >= TypeSize / 8)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 871 | return instrumentAddress(I, I, Addr, TypeSize, IsWrite, nullptr, UseCalls); |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 872 | // Instrument unusual size or unusual alignment. |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 873 | // We can not do it with a single check, so we do 1-byte check for the first |
| 874 | // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able |
| 875 | // to report the actual access size. |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 876 | IRBuilder<> IRB(I); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 877 | Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8); |
Kostya Serebryany | c9a2c17 | 2014-04-22 11:19:45 +0000 | [diff] [blame] | 878 | Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); |
| 879 | if (UseCalls) { |
Evgeniy Stepanov | ed31ca4 | 2014-05-14 10:56:19 +0000 | [diff] [blame] | 880 | IRB.CreateCall2(AsanMemoryAccessCallbackSized[IsWrite], AddrLong, Size); |
Kostya Serebryany | c9a2c17 | 2014-04-22 11:19:45 +0000 | [diff] [blame] | 881 | } else { |
| 882 | Value *LastByte = IRB.CreateIntToPtr( |
| 883 | IRB.CreateAdd(AddrLong, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)), |
| 884 | OrigPtrTy); |
| 885 | instrumentAddress(I, I, Addr, 8, IsWrite, Size, false); |
| 886 | instrumentAddress(I, I, LastByte, 8, IsWrite, Size, false); |
| 887 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Alexander Potapenko | 056e27e | 2012-04-23 10:47:31 +0000 | [diff] [blame] | 890 | // Validate the result of Module::getOrInsertFunction called for an interface |
| 891 | // function of AddressSanitizer. If the instrumented module defines a function |
| 892 | // with the same name, their prototypes must match, otherwise |
| 893 | // getOrInsertFunction returns a bitcast. |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 894 | static Function *checkInterfaceFunction(Constant *FuncOrBitcast) { |
Alexander Potapenko | 056e27e | 2012-04-23 10:47:31 +0000 | [diff] [blame] | 895 | if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast); |
| 896 | FuncOrBitcast->dump(); |
| 897 | report_fatal_error("trying to redefine an AddressSanitizer " |
| 898 | "interface function"); |
| 899 | } |
| 900 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 901 | Instruction *AddressSanitizer::generateCrashCode( |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 902 | Instruction *InsertBefore, Value *Addr, |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 903 | bool IsWrite, size_t AccessSizeIndex, Value *SizeArgument) { |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 904 | IRBuilder<> IRB(InsertBefore); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 905 | CallInst *Call = SizeArgument |
| 906 | ? IRB.CreateCall2(AsanErrorCallbackSized[IsWrite], Addr, SizeArgument) |
| 907 | : IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex], Addr); |
| 908 | |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 909 | // We don't do Call->setDoesNotReturn() because the BB already has |
| 910 | // UnreachableInst at the end. |
| 911 | // This EmptyAsm is required to avoid callback merge. |
| 912 | IRB.CreateCall(EmptyAsm); |
Kostya Serebryany | 3411f2e | 2012-01-06 18:09:21 +0000 | [diff] [blame] | 913 | return Call; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 916 | Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 917 | Value *ShadowValue, |
| 918 | uint32_t TypeSize) { |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 919 | size_t Granularity = 1 << Mapping.Scale; |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 920 | // Addr & (Granularity - 1) |
| 921 | Value *LastAccessedByte = IRB.CreateAnd( |
| 922 | AddrLong, ConstantInt::get(IntptrTy, Granularity - 1)); |
| 923 | // (Addr & (Granularity - 1)) + size - 1 |
| 924 | if (TypeSize / 8 > 1) |
| 925 | LastAccessedByte = IRB.CreateAdd( |
| 926 | LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)); |
| 927 | // (uint8_t) ((Addr & (Granularity-1)) + size - 1) |
| 928 | LastAccessedByte = IRB.CreateIntCast( |
Kostya Serebryany | 1e575ab | 2012-08-15 08:58:58 +0000 | [diff] [blame] | 929 | LastAccessedByte, ShadowValue->getType(), false); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 930 | // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue |
| 931 | return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue); |
| 932 | } |
| 933 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 934 | void AddressSanitizer::instrumentAddress(Instruction *OrigIns, |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 935 | Instruction *InsertBefore, Value *Addr, |
| 936 | uint32_t TypeSize, bool IsWrite, |
| 937 | Value *SizeArgument, bool UseCalls) { |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 938 | IRBuilder<> IRB(InsertBefore); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 939 | Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 940 | size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize); |
| 941 | |
| 942 | if (UseCalls) { |
Kostya Serebryany | 94f57d19 | 2014-04-21 10:28:13 +0000 | [diff] [blame] | 943 | IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][AccessSizeIndex], |
| 944 | AddrLong); |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 947 | |
| 948 | Type *ShadowTy = IntegerType::get( |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 949 | *C, std::max(8U, TypeSize >> Mapping.Scale)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 950 | Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); |
| 951 | Value *ShadowPtr = memToShadow(AddrLong, IRB); |
| 952 | Value *CmpVal = Constant::getNullValue(ShadowTy); |
| 953 | Value *ShadowValue = IRB.CreateLoad( |
| 954 | IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy)); |
| 955 | |
| 956 | Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal); |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 957 | size_t Granularity = 1 << Mapping.Scale; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 958 | TerminatorInst *CrashTerm = nullptr; |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 959 | |
Kostya Serebryany | 1e575ab | 2012-08-15 08:58:58 +0000 | [diff] [blame] | 960 | if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) { |
Kostya Serebryany | ad23852 | 2014-09-02 21:46:51 +0000 | [diff] [blame] | 961 | // We use branch weights for the slow path check, to indicate that the slow |
| 962 | // path is rarely taken. This seems to be the case for SPEC benchmarks. |
Evgeniy Stepanov | 8eb77d8 | 2012-10-19 10:48:31 +0000 | [diff] [blame] | 963 | TerminatorInst *CheckTerm = |
Kostya Serebryany | ad23852 | 2014-09-02 21:46:51 +0000 | [diff] [blame] | 964 | SplitBlockAndInsertIfThen(Cmp, InsertBefore, false, |
| 965 | MDBuilder(*C).createBranchWeights(1, 100000)); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 966 | assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional()); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 967 | BasicBlock *NextBB = CheckTerm->getSuccessor(0); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 968 | IRB.SetInsertPoint(CheckTerm); |
| 969 | Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 970 | BasicBlock *CrashBlock = |
| 971 | BasicBlock::Create(*C, "", NextBB->getParent(), NextBB); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 972 | CrashTerm = new UnreachableInst(*C, CrashBlock); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 973 | BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2); |
| 974 | ReplaceInstWithInst(CheckTerm, NewTerm); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 975 | } else { |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 976 | CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, true); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 977 | } |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 978 | |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 979 | Instruction *Crash = generateCrashCode( |
| 980 | CrashTerm, AddrLong, IsWrite, AccessSizeIndex, SizeArgument); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 981 | Crash->setDebugLoc(OrigIns->getDebugLoc()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 982 | } |
| 983 | |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 984 | void AddressSanitizerModule::poisonOneInitializer(Function &GlobalInit, |
| 985 | GlobalValue *ModuleName) { |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 986 | // Set up the arguments to our poison/unpoison functions. |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 987 | IRBuilder<> IRB(GlobalInit.begin()->getFirstInsertionPt()); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 988 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 989 | // Add a call to poison all external globals before the given function starts. |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 990 | Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy); |
| 991 | IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 992 | |
| 993 | // Add calls to unpoison all globals before each return instruction. |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 994 | for (auto &BB : GlobalInit.getBasicBlockList()) |
| 995 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 996 | CallInst::Create(AsanUnpoisonGlobals, "", RI); |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | void AddressSanitizerModule::createInitializerPoisonCalls( |
| 1000 | Module &M, GlobalValue *ModuleName) { |
| 1001 | GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors"); |
| 1002 | |
| 1003 | ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); |
| 1004 | for (Use &OP : CA->operands()) { |
| 1005 | if (isa<ConstantAggregateZero>(OP)) |
| 1006 | continue; |
| 1007 | ConstantStruct *CS = cast<ConstantStruct>(OP); |
| 1008 | |
| 1009 | // Must have a function or null ptr. |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 1010 | if (Function* F = dyn_cast<Function>(CS->getOperand(1))) { |
Kostya Serebryany | 34ddf87 | 2014-09-24 22:41:55 +0000 | [diff] [blame] | 1011 | if (F->getName() == kAsanModuleCtorName) continue; |
| 1012 | ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0)); |
| 1013 | // Don't instrument CTORs that will run before asan.module_ctor. |
| 1014 | if (Priority->getLimitedValue() <= kAsanCtorAndDtorPriority) continue; |
| 1015 | poisonOneInitializer(*F, ModuleName); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 1020 | bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) { |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1021 | Type *Ty = cast<PointerType>(G->getType())->getElementType(); |
Kostya Serebryany | 2034335 | 2012-10-17 13:40:06 +0000 | [diff] [blame] | 1022 | DEBUG(dbgs() << "GLOBAL: " << *G << "\n"); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1023 | |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 1024 | if (GlobalsMD.get(G).IsBlacklisted) return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1025 | if (!Ty->isSized()) return false; |
| 1026 | if (!G->hasInitializer()) return false; |
Kostya Serebryany | 139a937 | 2012-11-20 14:16:08 +0000 | [diff] [blame] | 1027 | if (GlobalWasGeneratedByAsan(G)) return false; // Our own global. |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1028 | // Touch only those globals that will not be defined in other modules. |
Timur Iskhodzhanov | e40fb37 | 2014-07-09 08:35:33 +0000 | [diff] [blame] | 1029 | // Don't handle ODR linkage types and COMDATs since other modules may be built |
| 1030 | // without ASan. |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1031 | if (G->getLinkage() != GlobalVariable::ExternalLinkage && |
| 1032 | G->getLinkage() != GlobalVariable::PrivateLinkage && |
| 1033 | G->getLinkage() != GlobalVariable::InternalLinkage) |
| 1034 | return false; |
Timur Iskhodzhanov | e40fb37 | 2014-07-09 08:35:33 +0000 | [diff] [blame] | 1035 | if (G->hasComdat()) |
| 1036 | return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1037 | // Two problems with thread-locals: |
| 1038 | // - The address of the main thread's copy can't be computed at link-time. |
| 1039 | // - Need to poison all copies, not just the main thread's one. |
| 1040 | if (G->isThreadLocal()) |
| 1041 | return false; |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1042 | // For now, just ignore this Global if the alignment is large. |
| 1043 | if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1044 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1045 | if (G->hasSection()) { |
| 1046 | StringRef Section(G->getSection()); |
Kuba Brecka | 086e34b | 2014-12-05 21:32:46 +0000 | [diff] [blame] | 1047 | |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 1048 | if (TargetTriple.isOSBinFormatMachO()) { |
| 1049 | StringRef ParsedSegment, ParsedSection; |
| 1050 | unsigned TAA = 0, StubSize = 0; |
| 1051 | bool TAAParsed; |
| 1052 | std::string ErrorCode = |
| 1053 | MCSectionMachO::ParseSectionSpecifier(Section, ParsedSegment, |
| 1054 | ParsedSection, TAA, TAAParsed, |
| 1055 | StubSize); |
| 1056 | if (!ErrorCode.empty()) { |
| 1057 | report_fatal_error("Invalid section specifier '" + ParsedSection + |
| 1058 | "': " + ErrorCode + "."); |
| 1059 | } |
| 1060 | |
| 1061 | // Ignore the globals from the __OBJC section. The ObjC runtime assumes |
| 1062 | // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to |
| 1063 | // them. |
| 1064 | if (ParsedSegment == "__OBJC" || |
| 1065 | (ParsedSegment == "__DATA" && ParsedSection.startswith("__objc_"))) { |
| 1066 | DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n"); |
| 1067 | return false; |
| 1068 | } |
| 1069 | // See http://code.google.com/p/address-sanitizer/issues/detail?id=32 |
| 1070 | // Constant CFString instances are compiled in the following way: |
| 1071 | // -- the string buffer is emitted into |
| 1072 | // __TEXT,__cstring,cstring_literals |
| 1073 | // -- the constant NSConstantString structure referencing that buffer |
| 1074 | // is placed into __DATA,__cfstring |
| 1075 | // Therefore there's no point in placing redzones into __DATA,__cfstring. |
| 1076 | // Moreover, it causes the linker to crash on OS X 10.7 |
| 1077 | if (ParsedSegment == "__DATA" && ParsedSection == "__cfstring") { |
| 1078 | DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n"); |
| 1079 | return false; |
| 1080 | } |
| 1081 | // The linker merges the contents of cstring_literals and removes the |
| 1082 | // trailing zeroes. |
| 1083 | if (ParsedSegment == "__TEXT" && (TAA & MachO::S_CSTRING_LITERALS)) { |
| 1084 | DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n"); |
| 1085 | return false; |
| 1086 | } |
| 1087 | } |
Timur Iskhodzhanov | 9dbc206 | 2014-05-05 14:28:38 +0000 | [diff] [blame] | 1088 | |
| 1089 | // Callbacks put into the CRT initializer/terminator sections |
| 1090 | // should not be instrumented. |
| 1091 | // See https://code.google.com/p/address-sanitizer/issues/detail?id=305 |
| 1092 | // and http://msdn.microsoft.com/en-US/en-en/library/bb918180(v=vs.120).aspx |
| 1093 | if (Section.startswith(".CRT")) { |
| 1094 | DEBUG(dbgs() << "Ignoring a global initializer callback: " << *G << "\n"); |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
Alexander Potapenko | 04969e8 | 2014-03-20 10:48:34 +0000 | [diff] [blame] | 1098 | // Globals from llvm.metadata aren't emitted, do not instrument them. |
| 1099 | if (Section == "llvm.metadata") return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | return true; |
| 1103 | } |
| 1104 | |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1105 | void AddressSanitizerModule::initializeCallbacks(Module &M) { |
| 1106 | IRBuilder<> IRB(*C); |
| 1107 | // Declare our poisoning and unpoisoning functions. |
| 1108 | AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1109 | kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1110 | AsanPoisonGlobals->setLinkage(Function::ExternalLinkage); |
| 1111 | AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1112 | kAsanUnpoisonGlobalsName, IRB.getVoidTy(), nullptr)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1113 | AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage); |
| 1114 | // Declare functions that register/unregister globals. |
| 1115 | AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
| 1116 | kAsanRegisterGlobalsName, IRB.getVoidTy(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1117 | IntptrTy, IntptrTy, nullptr)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1118 | AsanRegisterGlobals->setLinkage(Function::ExternalLinkage); |
| 1119 | AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
| 1120 | kAsanUnregisterGlobalsName, |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1121 | IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1122 | AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage); |
| 1123 | } |
| 1124 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1125 | // This function replaces all global variables with new variables that have |
| 1126 | // trailing redzones. It also creates a function that poisons |
| 1127 | // redzones and inserts this function into llvm.global_ctors. |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 1128 | bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M) { |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 1129 | GlobalsMD.init(M); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 1130 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1131 | SmallVector<GlobalVariable *, 16> GlobalsToChange; |
| 1132 | |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1133 | for (auto &G : M.globals()) { |
| 1134 | if (ShouldInstrumentGlobal(&G)) |
| 1135 | GlobalsToChange.push_back(&G); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | size_t n = GlobalsToChange.size(); |
| 1139 | if (n == 0) return false; |
| 1140 | |
| 1141 | // A global is described by a structure |
| 1142 | // size_t beg; |
| 1143 | // size_t size; |
| 1144 | // size_t size_with_redzone; |
| 1145 | // const char *name; |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 1146 | // const char *module_name; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1147 | // size_t has_dynamic_init; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 1148 | // void *source_location; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1149 | // We initialize an array of such structures and pass it to a run-time call. |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 1150 | StructType *GlobalStructTy = |
| 1151 | StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy, |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1152 | IntptrTy, IntptrTy, nullptr); |
Rafael Espindola | 44fee4e | 2013-10-01 13:32:03 +0000 | [diff] [blame] | 1153 | SmallVector<Constant *, 16> Initializers(n); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 1154 | |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 1155 | bool HasDynamicallyInitializedGlobals = false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1156 | |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 1157 | // We shouldn't merge same module names, as this string serves as unique |
| 1158 | // module ID in runtime. |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 1159 | GlobalVariable *ModuleName = createPrivateGlobalForString( |
| 1160 | M, M.getModuleIdentifier(), /*AllowMerging*/false); |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 1161 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1162 | for (size_t i = 0; i < n; i++) { |
Kostya Serebryany | e35d59a | 2013-01-24 10:43:50 +0000 | [diff] [blame] | 1163 | static const uint64_t kMaxGlobalRedzone = 1 << 18; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1164 | GlobalVariable *G = GlobalsToChange[i]; |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 1165 | |
| 1166 | auto MD = GlobalsMD.get(G); |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 1167 | // Create string holding the global name (use global name from metadata |
| 1168 | // if it's available, otherwise just write the name of global variable). |
| 1169 | GlobalVariable *Name = createPrivateGlobalForString( |
| 1170 | M, MD.Name.empty() ? G->getName() : MD.Name, |
| 1171 | /*AllowMerging*/ true); |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 1172 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1173 | PointerType *PtrTy = cast<PointerType>(G->getType()); |
| 1174 | Type *Ty = PtrTy->getElementType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1175 | uint64_t SizeInBytes = DL->getTypeAllocSize(Ty); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1176 | uint64_t MinRZ = MinRedzoneSizeForGlobal(); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 1177 | // MinRZ <= RZ <= kMaxGlobalRedzone |
| 1178 | // and trying to make RZ to be ~ 1/4 of SizeInBytes. |
Kostya Serebryany | e35d59a | 2013-01-24 10:43:50 +0000 | [diff] [blame] | 1179 | uint64_t RZ = std::max(MinRZ, |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 1180 | std::min(kMaxGlobalRedzone, |
| 1181 | (SizeInBytes / MinRZ / 4) * MinRZ)); |
| 1182 | uint64_t RightRedzoneSize = RZ; |
| 1183 | // Round up to MinRZ |
| 1184 | if (SizeInBytes % MinRZ) |
| 1185 | RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ); |
| 1186 | assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1187 | Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize); |
| 1188 | |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1189 | StructType *NewTy = StructType::get(Ty, RightRedZoneTy, nullptr); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1190 | Constant *NewInitializer = ConstantStruct::get( |
| 1191 | NewTy, G->getInitializer(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1192 | Constant::getNullValue(RightRedZoneTy), nullptr); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1193 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1194 | // Create a new global variable with enough space for a redzone. |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 1195 | GlobalValue::LinkageTypes Linkage = G->getLinkage(); |
| 1196 | if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage) |
| 1197 | Linkage = GlobalValue::InternalLinkage; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1198 | GlobalVariable *NewGlobal = new GlobalVariable( |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 1199 | M, NewTy, G->isConstant(), Linkage, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 1200 | NewInitializer, "", G, G->getThreadLocalMode()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1201 | NewGlobal->copyAttributesFrom(G); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 1202 | NewGlobal->setAlignment(MinRZ); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1203 | |
| 1204 | Value *Indices2[2]; |
| 1205 | Indices2[0] = IRB.getInt32(0); |
| 1206 | Indices2[1] = IRB.getInt32(0); |
| 1207 | |
| 1208 | G->replaceAllUsesWith( |
Kostya Serebryany | 7471d13 | 2012-01-28 04:27:16 +0000 | [diff] [blame] | 1209 | ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1210 | NewGlobal->takeName(G); |
| 1211 | G->eraseFromParent(); |
| 1212 | |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 1213 | Constant *SourceLoc; |
| 1214 | if (!MD.SourceLoc.empty()) { |
| 1215 | auto SourceLocGlobal = createPrivateGlobalForSourceLoc(M, MD.SourceLoc); |
| 1216 | SourceLoc = ConstantExpr::getPointerCast(SourceLocGlobal, IntptrTy); |
| 1217 | } else { |
| 1218 | SourceLoc = ConstantInt::get(IntptrTy, 0); |
| 1219 | } |
| 1220 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1221 | Initializers[i] = ConstantStruct::get( |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 1222 | GlobalStructTy, ConstantExpr::getPointerCast(NewGlobal, IntptrTy), |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1223 | ConstantInt::get(IntptrTy, SizeInBytes), |
| 1224 | ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize), |
| 1225 | ConstantExpr::getPointerCast(Name, IntptrTy), |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 1226 | ConstantExpr::getPointerCast(ModuleName, IntptrTy), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1227 | ConstantInt::get(IntptrTy, MD.IsDynInit), SourceLoc, nullptr); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1228 | |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 1229 | if (ClInitializers && MD.IsDynInit) |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 1230 | HasDynamicallyInitializedGlobals = true; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1231 | |
Kostya Serebryany | 2034335 | 2012-10-17 13:40:06 +0000 | [diff] [blame] | 1232 | DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n"); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n); |
| 1236 | GlobalVariable *AllGlobals = new GlobalVariable( |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 1237 | M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage, |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1238 | ConstantArray::get(ArrayOfGlobalStructTy, Initializers), ""); |
| 1239 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1240 | // Create calls for poisoning before initializers run and unpoisoning after. |
Alexey Samsonov | e595e1a | 2014-06-13 17:53:44 +0000 | [diff] [blame] | 1241 | if (HasDynamicallyInitializedGlobals) |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 1242 | createInitializerPoisonCalls(M, ModuleName); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1243 | IRB.CreateCall2(AsanRegisterGlobals, |
| 1244 | IRB.CreatePointerCast(AllGlobals, IntptrTy), |
| 1245 | ConstantInt::get(IntptrTy, n)); |
| 1246 | |
Kostya Serebryany | cd1aba8 | 2011-12-15 21:59:03 +0000 | [diff] [blame] | 1247 | // We also need to unregister globals at the end, e.g. when a shared library |
| 1248 | // gets closed. |
| 1249 | Function *AsanDtorFunction = Function::Create( |
| 1250 | FunctionType::get(Type::getVoidTy(*C), false), |
| 1251 | GlobalValue::InternalLinkage, kAsanModuleDtorName, &M); |
| 1252 | BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); |
| 1253 | IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB)); |
Kostya Serebryany | cd1aba8 | 2011-12-15 21:59:03 +0000 | [diff] [blame] | 1254 | IRB_Dtor.CreateCall2(AsanUnregisterGlobals, |
| 1255 | IRB.CreatePointerCast(AllGlobals, IntptrTy), |
| 1256 | ConstantInt::get(IntptrTy, n)); |
Alexey Samsonov | 1f64750 | 2014-05-29 01:10:14 +0000 | [diff] [blame] | 1257 | appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority); |
Kostya Serebryany | cd1aba8 | 2011-12-15 21:59:03 +0000 | [diff] [blame] | 1258 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1259 | DEBUG(dbgs() << M); |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 1263 | bool AddressSanitizerModule::runOnModule(Module &M) { |
| 1264 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 1265 | if (!DLP) |
| 1266 | return false; |
| 1267 | DL = &DLP->getDataLayout(); |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 1268 | C = &(M.getContext()); |
| 1269 | int LongSize = DL->getPointerSizeInBits(); |
| 1270 | IntptrTy = Type::getIntNTy(*C, LongSize); |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 1271 | TargetTriple = Triple(M.getTargetTriple()); |
| 1272 | Mapping = getShadowMapping(TargetTriple, LongSize); |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 1273 | initializeCallbacks(M); |
| 1274 | |
| 1275 | bool Changed = false; |
| 1276 | |
| 1277 | Function *CtorFunc = M.getFunction(kAsanModuleCtorName); |
| 1278 | assert(CtorFunc); |
| 1279 | IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator()); |
| 1280 | |
Alexey Samsonov | c94285a | 2014-07-08 00:50:49 +0000 | [diff] [blame] | 1281 | if (ClGlobals) |
| 1282 | Changed |= InstrumentGlobals(IRB, M); |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 1283 | |
| 1284 | return Changed; |
| 1285 | } |
| 1286 | |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1287 | void AddressSanitizer::initializeCallbacks(Module &M) { |
| 1288 | IRBuilder<> IRB(*C); |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 1289 | // Create __asan_report* callbacks. |
| 1290 | for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) { |
| 1291 | for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; |
| 1292 | AccessSizeIndex++) { |
| 1293 | // IsWrite and TypeSize are encoded in the function name. |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1294 | std::string Suffix = |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 1295 | (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex); |
Kostya Serebryany | 157a515 | 2012-11-07 12:42:18 +0000 | [diff] [blame] | 1296 | AsanErrorCallback[AccessIsWrite][AccessSizeIndex] = |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1297 | checkInterfaceFunction( |
| 1298 | M.getOrInsertFunction(kAsanReportErrorTemplate + Suffix, |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1299 | IRB.getVoidTy(), IntptrTy, nullptr)); |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1300 | AsanMemoryAccessCallback[AccessIsWrite][AccessSizeIndex] = |
| 1301 | checkInterfaceFunction( |
| 1302 | M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + Suffix, |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1303 | IRB.getVoidTy(), IntptrTy, nullptr)); |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 1306 | AsanErrorCallbackSized[0] = checkInterfaceFunction(M.getOrInsertFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1307 | kAsanReportLoadN, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 1308 | AsanErrorCallbackSized[1] = checkInterfaceFunction(M.getOrInsertFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1309 | kAsanReportStoreN, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1310 | |
Kostya Serebryany | 86332c0 | 2014-04-21 07:10:43 +0000 | [diff] [blame] | 1311 | AsanMemoryAccessCallbackSized[0] = checkInterfaceFunction( |
| 1312 | M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "loadN", |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1313 | IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | 86332c0 | 2014-04-21 07:10:43 +0000 | [diff] [blame] | 1314 | AsanMemoryAccessCallbackSized[1] = checkInterfaceFunction( |
| 1315 | M.getOrInsertFunction(ClMemoryAccessCallbackPrefix + "storeN", |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1316 | IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | 86332c0 | 2014-04-21 07:10:43 +0000 | [diff] [blame] | 1317 | |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1318 | AsanMemmove = checkInterfaceFunction(M.getOrInsertFunction( |
| 1319 | ClMemoryAccessCallbackPrefix + "memmove", IRB.getInt8PtrTy(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1320 | IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr)); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1321 | AsanMemcpy = checkInterfaceFunction(M.getOrInsertFunction( |
| 1322 | ClMemoryAccessCallbackPrefix + "memcpy", IRB.getInt8PtrTy(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1323 | IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr)); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1324 | AsanMemset = checkInterfaceFunction(M.getOrInsertFunction( |
| 1325 | ClMemoryAccessCallbackPrefix + "memset", IRB.getInt8PtrTy(), |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1326 | IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy, nullptr)); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1327 | |
| 1328 | AsanHandleNoReturnFunc = checkInterfaceFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1329 | M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy(), nullptr)); |
Kostya Serebryany | 4f8f0c5 | 2014-10-27 18:13:56 +0000 | [diff] [blame] | 1330 | |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1331 | AsanPtrCmpFunction = checkInterfaceFunction(M.getOrInsertFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1332 | kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1333 | AsanPtrSubFunction = checkInterfaceFunction(M.getOrInsertFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1334 | kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 1335 | // We insert an empty inline asm after __asan_report* to avoid callback merge. |
| 1336 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 1337 | StringRef(""), StringRef(""), |
| 1338 | /*hasSideEffects=*/true); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | // virtual |
| 1342 | bool AddressSanitizer::doInitialization(Module &M) { |
| 1343 | // Initialize the private fields. No one has accessed them before. |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 1344 | DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); |
| 1345 | if (!DLP) |
Evgeniy Stepanov | 119cb2e | 2014-04-23 12:51:32 +0000 | [diff] [blame] | 1346 | report_fatal_error("data layout missing"); |
Rafael Espindola | 9351251 | 2014-02-25 17:30:31 +0000 | [diff] [blame] | 1347 | DL = &DLP->getDataLayout(); |
| 1348 | |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 1349 | GlobalsMD.init(M); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1350 | |
| 1351 | C = &(M.getContext()); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1352 | LongSize = DL->getPointerSizeInBits(); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1353 | IntptrTy = Type::getIntNTy(*C, LongSize); |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 1354 | TargetTriple = Triple(M.getTargetTriple()); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1355 | |
| 1356 | AsanCtorFunction = Function::Create( |
| 1357 | FunctionType::get(Type::getVoidTy(*C), false), |
| 1358 | GlobalValue::InternalLinkage, kAsanModuleCtorName, &M); |
| 1359 | BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction); |
| 1360 | // call __asan_init in the module ctor. |
| 1361 | IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB)); |
| 1362 | AsanInitFunction = checkInterfaceFunction( |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1363 | M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), nullptr)); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1364 | AsanInitFunction->setLinkage(Function::ExternalLinkage); |
| 1365 | IRB.CreateCall(AsanInitFunction); |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 1366 | |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame^] | 1367 | Mapping = getShadowMapping(TargetTriple, LongSize); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1368 | |
Alexey Samsonov | 1f64750 | 2014-05-29 01:10:14 +0000 | [diff] [blame] | 1369 | appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1370 | return true; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 1373 | bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) { |
| 1374 | // For each NSObject descendant having a +load method, this method is invoked |
| 1375 | // by the ObjC runtime before any of the static constructors is called. |
| 1376 | // Therefore we need to instrument such methods with a call to __asan_init |
| 1377 | // at the beginning in order to initialize our runtime before any access to |
| 1378 | // the shadow memory. |
| 1379 | // We cannot just ignore these methods, because they may call other |
| 1380 | // instrumented functions. |
| 1381 | if (F.getName().find(" load]") != std::string::npos) { |
| 1382 | IRBuilder<> IRB(F.begin()->begin()); |
| 1383 | IRB.CreateCall(AsanInitFunction); |
| 1384 | return true; |
| 1385 | } |
| 1386 | return false; |
| 1387 | } |
| 1388 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1389 | bool AddressSanitizer::runOnFunction(Function &F) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1390 | if (&F == AsanCtorFunction) return false; |
Kostya Serebryany | 6b5b58d | 2013-03-18 07:33:49 +0000 | [diff] [blame] | 1391 | if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false; |
Kostya Serebryany | 2034335 | 2012-10-17 13:40:06 +0000 | [diff] [blame] | 1392 | DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n"); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1393 | initializeCallbacks(*F.getParent()); |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 1394 | |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 1395 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 1396 | |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 1397 | // If needed, insert __asan_init before checking for SanitizeAddress attr. |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 1398 | maybeInsertAsanInitAtFunctionEntry(F); |
| 1399 | |
Alexey Samsonov | 6d8bab8 | 2014-06-02 18:08:27 +0000 | [diff] [blame] | 1400 | if (!F.hasFnAttribute(Attribute::SanitizeAddress)) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 1401 | return false; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (!ClDebugFunc.empty() && ClDebugFunc != F.getName()) |
| 1404 | return false; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 1405 | |
| 1406 | // We want to instrument every address only once per basic block (unless there |
| 1407 | // are calls between uses). |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1408 | SmallSet<Value*, 16> TempsToInstrument; |
| 1409 | SmallVector<Instruction*, 16> ToInstrument; |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 1410 | SmallVector<Instruction*, 8> NoReturnCalls; |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 1411 | SmallVector<BasicBlock*, 16> AllBlocks; |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1412 | SmallVector<Instruction*, 16> PointerComparisonsOrSubtracts; |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1413 | int NumAllocas = 0; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1414 | bool IsWrite; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1415 | unsigned Alignment; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1416 | |
| 1417 | // Fill the set of memory operations to instrument. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1418 | for (auto &BB : F) { |
| 1419 | AllBlocks.push_back(&BB); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1420 | TempsToInstrument.clear(); |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 1421 | int NumInsnsPerBB = 0; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1422 | for (auto &Inst : BB) { |
| 1423 | if (LooksLikeCodeInBug11395(&Inst)) return false; |
| 1424 | if (Value *Addr = |
| 1425 | isInterestingMemoryAccess(&Inst, &IsWrite, &Alignment)) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1426 | if (ClOpt && ClOptSameTemp) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1427 | if (!TempsToInstrument.insert(Addr).second) |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1428 | continue; // We've seen this temp in the current BB. |
| 1429 | } |
Kostya Serebryany | cb3f6e1 | 2014-02-27 13:13:59 +0000 | [diff] [blame] | 1430 | } else if (ClInvalidPointerPairs && |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1431 | isInterestingPointerComparisonOrSubtraction(&Inst)) { |
| 1432 | PointerComparisonsOrSubtracts.push_back(&Inst); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1433 | continue; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1434 | } else if (isa<MemIntrinsic>(Inst)) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1435 | // ok, take it. |
| 1436 | } else { |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1437 | if (isa<AllocaInst>(Inst)) |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1438 | NumAllocas++; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1439 | CallSite CS(&Inst); |
Kostya Serebryany | 699ac28 | 2013-02-20 12:35:15 +0000 | [diff] [blame] | 1440 | if (CS) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1441 | // A call inside BB. |
| 1442 | TempsToInstrument.clear(); |
Kostya Serebryany | 699ac28 | 2013-02-20 12:35:15 +0000 | [diff] [blame] | 1443 | if (CS.doesNotReturn()) |
| 1444 | NoReturnCalls.push_back(CS.getInstruction()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1445 | } |
| 1446 | continue; |
| 1447 | } |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1448 | ToInstrument.push_back(&Inst); |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 1449 | NumInsnsPerBB++; |
| 1450 | if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) |
| 1451 | break; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1455 | bool UseCalls = false; |
| 1456 | if (ClInstrumentationWithCallsThreshold >= 0 && |
| 1457 | ToInstrument.size() > (unsigned)ClInstrumentationWithCallsThreshold) |
| 1458 | UseCalls = true; |
| 1459 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1460 | // Instrument. |
| 1461 | int NumInstrumented = 0; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1462 | for (auto Inst : ToInstrument) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1463 | if (ClDebugMin < 0 || ClDebugMax < 0 || |
| 1464 | (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) { |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1465 | if (isInterestingMemoryAccess(Inst, &IsWrite, &Alignment)) |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1466 | instrumentMop(Inst, UseCalls); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1467 | else |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1468 | instrumentMemIntrinsic(cast<MemIntrinsic>(Inst)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1469 | } |
| 1470 | NumInstrumented++; |
| 1471 | } |
| 1472 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1473 | FunctionStackPoisoner FSP(F, *this); |
| 1474 | bool ChangedStack = FSP.runOnFunction(); |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 1475 | |
| 1476 | // We must unpoison the stack before every NoReturn call (throw, _exit, etc). |
| 1477 | // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37 |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1478 | for (auto CI : NoReturnCalls) { |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 1479 | IRBuilder<> IRB(CI); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1480 | IRB.CreateCall(AsanHandleNoReturnFunc); |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1483 | for (auto Inst : PointerComparisonsOrSubtracts) { |
| 1484 | instrumentPointerComparisonOrSubtraction(Inst); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1485 | NumInstrumented++; |
| 1486 | } |
| 1487 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1488 | bool res = NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty(); |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 1489 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1490 | DEBUG(dbgs() << "ASAN done instrumenting: " << res << " " << F << "\n"); |
| 1491 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1492 | return res; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1495 | // Workaround for bug 11395: we don't want to instrument stack in functions |
| 1496 | // with large assembly blobs (32-bit only), otherwise reg alloc may crash. |
| 1497 | // FIXME: remove once the bug 11395 is fixed. |
| 1498 | bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) { |
| 1499 | if (LongSize != 32) return false; |
| 1500 | CallInst *CI = dyn_cast<CallInst>(I); |
| 1501 | if (!CI || !CI->isInlineAsm()) return false; |
| 1502 | if (CI->getNumArgOperands() <= 5) return false; |
| 1503 | // We have inline assembly with quite a few arguments. |
| 1504 | return true; |
| 1505 | } |
| 1506 | |
| 1507 | void FunctionStackPoisoner::initializeCallbacks(Module &M) { |
| 1508 | IRBuilder<> IRB(*C); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1509 | for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) { |
| 1510 | std::string Suffix = itostr(i); |
| 1511 | AsanStackMallocFunc[i] = checkInterfaceFunction( |
| 1512 | M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy, |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1513 | IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1514 | AsanStackFreeFunc[i] = checkInterfaceFunction(M.getOrInsertFunction( |
| 1515 | kAsanStackFreeNameTemplate + Suffix, IRB.getVoidTy(), IntptrTy, |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 1516 | IntptrTy, IntptrTy, nullptr)); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1517 | } |
David Blaikie | a92765c | 2014-11-14 00:41:42 +0000 | [diff] [blame] | 1518 | AsanPoisonStackMemoryFunc = checkInterfaceFunction( |
| 1519 | M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(), |
| 1520 | IntptrTy, IntptrTy, nullptr)); |
| 1521 | AsanUnpoisonStackMemoryFunc = checkInterfaceFunction( |
| 1522 | M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), |
| 1523 | IntptrTy, IntptrTy, nullptr)); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1526 | void |
Craig Topper | 3af9722 | 2014-08-27 05:25:00 +0000 | [diff] [blame] | 1527 | FunctionStackPoisoner::poisonRedZones(ArrayRef<uint8_t> ShadowBytes, |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1528 | IRBuilder<> &IRB, Value *ShadowBase, |
| 1529 | bool DoPoison) { |
| 1530 | size_t n = ShadowBytes.size(); |
Kostya Serebryany | ff7bde1 | 2013-12-23 09:24:36 +0000 | [diff] [blame] | 1531 | size_t i = 0; |
| 1532 | // We need to (un)poison n bytes of stack shadow. Poison as many as we can |
| 1533 | // using 64-bit stores (if we are on 64-bit arch), then poison the rest |
| 1534 | // with 32-bit stores, then with 16-byte stores, then with 8-byte stores. |
| 1535 | for (size_t LargeStoreSizeInBytes = ASan.LongSize / 8; |
| 1536 | LargeStoreSizeInBytes != 0; LargeStoreSizeInBytes /= 2) { |
| 1537 | for (; i + LargeStoreSizeInBytes - 1 < n; i += LargeStoreSizeInBytes) { |
| 1538 | uint64_t Val = 0; |
| 1539 | for (size_t j = 0; j < LargeStoreSizeInBytes; j++) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1540 | if (ASan.DL->isLittleEndian()) |
Kostya Serebryany | ff7bde1 | 2013-12-23 09:24:36 +0000 | [diff] [blame] | 1541 | Val |= (uint64_t)ShadowBytes[i + j] << (8 * j); |
| 1542 | else |
| 1543 | Val = (Val << 8) | ShadowBytes[i + j]; |
| 1544 | } |
| 1545 | if (!Val) continue; |
| 1546 | Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)); |
| 1547 | Type *StoreTy = Type::getIntNTy(*C, LargeStoreSizeInBytes * 8); |
| 1548 | Value *Poison = ConstantInt::get(StoreTy, DoPoison ? Val : 0); |
| 1549 | IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, StoreTy->getPointerTo())); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1550 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1554 | // Fake stack allocator (asan_fake_stack.h) has 11 size classes |
| 1555 | // for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass |
| 1556 | static int StackMallocSizeClass(uint64_t LocalStackSize) { |
| 1557 | assert(LocalStackSize <= kMaxStackMallocSize); |
| 1558 | uint64_t MaxSize = kMinStackMallocSize; |
| 1559 | for (int i = 0; ; i++, MaxSize *= 2) |
| 1560 | if (LocalStackSize <= MaxSize) |
| 1561 | return i; |
| 1562 | llvm_unreachable("impossible LocalStackSize"); |
| 1563 | } |
| 1564 | |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1565 | // Set Size bytes starting from ShadowBase to kAsanStackAfterReturnMagic. |
| 1566 | // We can not use MemSet intrinsic because it may end up calling the actual |
| 1567 | // memset. Size is a multiple of 8. |
| 1568 | // Currently this generates 8-byte stores on x86_64; it may be better to |
| 1569 | // generate wider stores. |
| 1570 | void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined( |
| 1571 | IRBuilder<> &IRB, Value *ShadowBase, int Size) { |
| 1572 | assert(!(Size % 8)); |
| 1573 | assert(kAsanStackAfterReturnMagic == 0xf5); |
| 1574 | for (int i = 0; i < Size; i += 8) { |
| 1575 | Value *p = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)); |
| 1576 | IRB.CreateStore(ConstantInt::get(IRB.getInt64Ty(), 0xf5f5f5f5f5f5f5f5ULL), |
| 1577 | IRB.CreateIntToPtr(p, IRB.getInt64Ty()->getPointerTo())); |
| 1578 | } |
| 1579 | } |
| 1580 | |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 1581 | static DebugLoc getFunctionEntryDebugLocation(Function &F) { |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1582 | for (const auto &Inst : F.getEntryBlock()) |
| 1583 | if (!isa<AllocaInst>(Inst)) |
| 1584 | return Inst.getDebugLoc(); |
| 1585 | return DebugLoc(); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1588 | void FunctionStackPoisoner::poisonStack() { |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 1589 | assert(AllocaVec.size() > 0 || DynamicAllocaVec.size() > 0); |
| 1590 | |
| 1591 | if (ClInstrumentAllocas) |
| 1592 | // Handle dynamic allocas. |
| 1593 | for (auto &AllocaCall : DynamicAllocaVec) |
| 1594 | handleDynamicAllocaCall(AllocaCall); |
| 1595 | |
| 1596 | if (AllocaVec.size() == 0) return; |
| 1597 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1598 | int StackMallocIdx = -1; |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 1599 | DebugLoc EntryDebugLocation = getFunctionEntryDebugLocation(F); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1600 | |
| 1601 | Instruction *InsBefore = AllocaVec[0]; |
| 1602 | IRBuilder<> IRB(InsBefore); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 1603 | IRB.SetCurrentDebugLocation(EntryDebugLocation); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1604 | |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1605 | SmallVector<ASanStackVariableDescription, 16> SVD; |
| 1606 | SVD.reserve(AllocaVec.size()); |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1607 | for (AllocaInst *AI : AllocaVec) { |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1608 | ASanStackVariableDescription D = { AI->getName().data(), |
| 1609 | getAllocaSizeInBytes(AI), |
| 1610 | AI->getAlignment(), AI, 0}; |
| 1611 | SVD.push_back(D); |
| 1612 | } |
| 1613 | // Minimal header size (left redzone) is 4 pointers, |
| 1614 | // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms. |
| 1615 | size_t MinHeaderSize = ASan.LongSize / 2; |
| 1616 | ASanStackFrameLayout L; |
| 1617 | ComputeASanStackFrameLayout(SVD, 1UL << Mapping.Scale, MinHeaderSize, &L); |
| 1618 | DEBUG(dbgs() << L.DescriptionString << " --- " << L.FrameSize << "\n"); |
| 1619 | uint64_t LocalStackSize = L.FrameSize; |
| 1620 | bool DoStackMalloc = |
Alexey Samsonov | e595e1a | 2014-06-13 17:53:44 +0000 | [diff] [blame] | 1621 | ClUseAfterReturn && LocalStackSize <= kMaxStackMallocSize; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1622 | |
| 1623 | Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize); |
| 1624 | AllocaInst *MyAlloca = |
| 1625 | new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 1626 | MyAlloca->setDebugLoc(EntryDebugLocation); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1627 | assert((ClRealignStack & (ClRealignStack - 1)) == 0); |
| 1628 | size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack); |
| 1629 | MyAlloca->setAlignment(FrameAlignment); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1630 | assert(MyAlloca->isStaticAlloca()); |
| 1631 | Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy); |
| 1632 | Value *LocalStackBase = OrigStackBase; |
| 1633 | |
| 1634 | if (DoStackMalloc) { |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1635 | // LocalStackBase = OrigStackBase |
| 1636 | // if (__asan_option_detect_stack_use_after_return) |
| 1637 | // LocalStackBase = __asan_stack_malloc_N(LocalStackBase, OrigStackBase); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1638 | StackMallocIdx = StackMallocSizeClass(LocalStackSize); |
| 1639 | assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1640 | Constant *OptionDetectUAR = F.getParent()->getOrInsertGlobal( |
| 1641 | kAsanOptionDetectUAR, IRB.getInt32Ty()); |
| 1642 | Value *Cmp = IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUAR), |
| 1643 | Constant::getNullValue(IRB.getInt32Ty())); |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 1644 | Instruction *Term = SplitBlockAndInsertIfThen(Cmp, InsBefore, false); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1645 | BasicBlock *CmpBlock = cast<Instruction>(Cmp)->getParent(); |
| 1646 | IRBuilder<> IRBIf(Term); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 1647 | IRBIf.SetCurrentDebugLocation(EntryDebugLocation); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1648 | LocalStackBase = IRBIf.CreateCall2( |
| 1649 | AsanStackMallocFunc[StackMallocIdx], |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1650 | ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1651 | BasicBlock *SetBlock = cast<Instruction>(LocalStackBase)->getParent(); |
| 1652 | IRB.SetInsertPoint(InsBefore); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 1653 | IRB.SetCurrentDebugLocation(EntryDebugLocation); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1654 | PHINode *Phi = IRB.CreatePHI(IntptrTy, 2); |
| 1655 | Phi->addIncoming(OrigStackBase, CmpBlock); |
| 1656 | Phi->addIncoming(LocalStackBase, SetBlock); |
| 1657 | LocalStackBase = Phi; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1660 | // Insert poison calls for lifetime intrinsics for alloca. |
| 1661 | bool HavePoisonedAllocas = false; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1662 | for (const auto &APC : AllocaPoisonCallVec) { |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 1663 | assert(APC.InsBefore); |
| 1664 | assert(APC.AI); |
| 1665 | IRBuilder<> IRB(APC.InsBefore); |
| 1666 | poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison); |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1667 | HavePoisonedAllocas |= APC.DoPoison; |
| 1668 | } |
| 1669 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1670 | // Replace Alloca instructions with base+offset. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1671 | for (const auto &Desc : SVD) { |
| 1672 | AllocaInst *AI = Desc.AI; |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1673 | Value *NewAllocaPtr = IRB.CreateIntToPtr( |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1674 | IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)), |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1675 | AI->getType()); |
Alexey Samsonov | 3d43b63 | 2012-12-12 14:31:53 +0000 | [diff] [blame] | 1676 | replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB); |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1677 | AI->replaceAllUsesWith(NewAllocaPtr); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1678 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1679 | |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 1680 | // The left-most redzone has enough space for at least 4 pointers. |
| 1681 | // Write the Magic value to redzone[0]. |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1682 | Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy); |
| 1683 | IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic), |
| 1684 | BasePlus0); |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 1685 | // Write the frame description constant to redzone[1]. |
| 1686 | Value *BasePlus1 = IRB.CreateIntToPtr( |
| 1687 | IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, ASan.LongSize/8)), |
| 1688 | IntptrPtrTy); |
Alexey Samsonov | 9bdb63a | 2012-11-02 12:20:34 +0000 | [diff] [blame] | 1689 | GlobalVariable *StackDescriptionGlobal = |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 1690 | createPrivateGlobalForString(*F.getParent(), L.DescriptionString, |
| 1691 | /*AllowMerging*/true); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1692 | Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, |
| 1693 | IntptrTy); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1694 | IRB.CreateStore(Description, BasePlus1); |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 1695 | // Write the PC to redzone[2]. |
| 1696 | Value *BasePlus2 = IRB.CreateIntToPtr( |
| 1697 | IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, |
| 1698 | 2 * ASan.LongSize/8)), |
| 1699 | IntptrPtrTy); |
| 1700 | IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1701 | |
| 1702 | // Poison the stack redzones at the entry. |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1703 | Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1704 | poisonRedZones(L.ShadowBytes, IRB, ShadowBase, true); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1705 | |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1706 | // (Un)poison the stack before all ret instructions. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1707 | for (auto Ret : RetVec) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1708 | IRBuilder<> IRBRet(Ret); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1709 | // Mark the current frame as retired. |
| 1710 | IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic), |
| 1711 | BasePlus0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1712 | if (DoStackMalloc) { |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1713 | assert(StackMallocIdx >= 0); |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1714 | // if LocalStackBase != OrigStackBase: |
| 1715 | // // In use-after-return mode, poison the whole stack frame. |
| 1716 | // if StackMallocIdx <= 4 |
| 1717 | // // For small sizes inline the whole thing: |
| 1718 | // memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize); |
| 1719 | // **SavedFlagPtr(LocalStackBase) = 0 |
| 1720 | // else |
| 1721 | // __asan_stack_free_N(LocalStackBase, OrigStackBase) |
| 1722 | // else |
| 1723 | // <This is not a fake stack; unpoison the redzones> |
| 1724 | Value *Cmp = IRBRet.CreateICmpNE(LocalStackBase, OrigStackBase); |
| 1725 | TerminatorInst *ThenTerm, *ElseTerm; |
| 1726 | SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm); |
| 1727 | |
| 1728 | IRBuilder<> IRBPoison(ThenTerm); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1729 | if (StackMallocIdx <= 4) { |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1730 | int ClassSize = kMinStackMallocSize << StackMallocIdx; |
| 1731 | SetShadowToStackAfterReturnInlined(IRBPoison, ShadowBase, |
| 1732 | ClassSize >> Mapping.Scale); |
| 1733 | Value *SavedFlagPtrPtr = IRBPoison.CreateAdd( |
| 1734 | LocalStackBase, |
| 1735 | ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8)); |
| 1736 | Value *SavedFlagPtr = IRBPoison.CreateLoad( |
| 1737 | IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy)); |
| 1738 | IRBPoison.CreateStore( |
| 1739 | Constant::getNullValue(IRBPoison.getInt8Ty()), |
| 1740 | IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy())); |
| 1741 | } else { |
| 1742 | // For larger frames call __asan_stack_free_*. |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1743 | IRBPoison.CreateCall3(AsanStackFreeFunc[StackMallocIdx], LocalStackBase, |
| 1744 | ConstantInt::get(IntptrTy, LocalStackSize), |
| 1745 | OrigStackBase); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1746 | } |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1747 | |
| 1748 | IRBuilder<> IRBElse(ElseTerm); |
| 1749 | poisonRedZones(L.ShadowBytes, IRBElse, ShadowBase, false); |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1750 | } else if (HavePoisonedAllocas) { |
| 1751 | // If we poisoned some allocas in llvm.lifetime analysis, |
| 1752 | // unpoison whole stack frame now. |
| 1753 | assert(LocalStackBase == OrigStackBase); |
| 1754 | poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false); |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1755 | } else { |
| 1756 | poisonRedZones(L.ShadowBytes, IRBRet, ShadowBase, false); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 1760 | if (ClInstrumentAllocas) |
| 1761 | // Unpoison dynamic allocas. |
| 1762 | for (auto &AllocaCall : DynamicAllocaVec) |
| 1763 | unpoisonDynamicAlloca(AllocaCall); |
| 1764 | |
Kostya Serebryany | 0995994 | 2012-10-19 06:20:53 +0000 | [diff] [blame] | 1765 | // We are done. Remove the old unused alloca instructions. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 1766 | for (auto AI : AllocaVec) |
| 1767 | AI->eraseFromParent(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1768 | } |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1769 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1770 | void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size, |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 1771 | IRBuilder<> &IRB, bool DoPoison) { |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1772 | // For now just insert the call to ASan runtime. |
| 1773 | Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy); |
| 1774 | Value *SizeArg = ConstantInt::get(IntptrTy, Size); |
| 1775 | IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc |
| 1776 | : AsanUnpoisonStackMemoryFunc, |
| 1777 | AddrArg, SizeArg); |
| 1778 | } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1779 | |
| 1780 | // Handling llvm.lifetime intrinsics for a given %alloca: |
| 1781 | // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca. |
| 1782 | // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect |
| 1783 | // invalid accesses) and unpoison it for llvm.lifetime.start (the memory |
| 1784 | // could be poisoned by previous llvm.lifetime.end instruction, as the |
| 1785 | // variable may go in and out of scope several times, e.g. in loops). |
| 1786 | // (3) if we poisoned at least one %alloca in a function, |
| 1787 | // unpoison the whole stack frame at function exit. |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1788 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1789 | AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) { |
| 1790 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
| 1791 | // We're intested only in allocas we can handle. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1792 | return isInterestingAlloca(*AI) ? AI : nullptr; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1793 | // See if we've already calculated (or started to calculate) alloca for a |
| 1794 | // given value. |
| 1795 | AllocaForValueMapTy::iterator I = AllocaForValue.find(V); |
| 1796 | if (I != AllocaForValue.end()) |
| 1797 | return I->second; |
| 1798 | // Store 0 while we're calculating alloca for value V to avoid |
| 1799 | // infinite recursion if the value references itself. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1800 | AllocaForValue[V] = nullptr; |
| 1801 | AllocaInst *Res = nullptr; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1802 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 1803 | Res = findAllocaForValue(CI->getOperand(0)); |
| 1804 | else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 1805 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1806 | Value *IncValue = PN->getIncomingValue(i); |
| 1807 | // Allow self-referencing phi-nodes. |
| 1808 | if (IncValue == PN) continue; |
| 1809 | AllocaInst *IncValueAI = findAllocaForValue(IncValue); |
| 1810 | // AI for incoming values should exist and should all be equal. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1811 | if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res)) |
| 1812 | return nullptr; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1813 | Res = IncValueAI; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1814 | } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1815 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1816 | if (Res) |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1817 | AllocaForValue[V] = Res; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1818 | return Res; |
| 1819 | } |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 1820 | |
| 1821 | // Compute PartialRzMagic for dynamic alloca call. PartialRzMagic is |
| 1822 | // constructed from two separate 32-bit numbers: PartialRzMagic = Val1 | Val2. |
| 1823 | // (1) Val1 is resposible for forming base value for PartialRzMagic, containing |
| 1824 | // only 00 for fully addressable and 0xcb for fully poisoned bytes for each |
| 1825 | // 8-byte chunk of user memory respectively. |
| 1826 | // (2) Val2 forms the value for marking first poisoned byte in shadow memory |
| 1827 | // with appropriate value (0x01 - 0x07 or 0xcb if Padding % 8 == 0). |
| 1828 | |
| 1829 | // Shift = Padding & ~7; // the number of bits we need to shift to access first |
| 1830 | // chunk in shadow memory, containing nonzero bytes. |
| 1831 | // Example: |
| 1832 | // Padding = 21 Padding = 16 |
| 1833 | // Shadow: |00|00|05|cb| Shadow: |00|00|cb|cb| |
| 1834 | // ^ ^ |
| 1835 | // | | |
| 1836 | // Shift = 21 & ~7 = 16 Shift = 16 & ~7 = 16 |
| 1837 | // |
| 1838 | // Val1 = 0xcbcbcbcb << Shift; |
| 1839 | // PartialBits = Padding ? Padding & 7 : 0xcb; |
| 1840 | // Val2 = PartialBits << Shift; |
| 1841 | // Result = Val1 | Val2; |
| 1842 | Value *FunctionStackPoisoner::computePartialRzMagic(Value *PartialSize, |
| 1843 | IRBuilder<> &IRB) { |
| 1844 | PartialSize = IRB.CreateIntCast(PartialSize, IRB.getInt32Ty(), false); |
| 1845 | Value *Shift = IRB.CreateAnd(PartialSize, IRB.getInt32(~7)); |
| 1846 | unsigned Val1Int = kAsanAllocaPartialVal1; |
| 1847 | unsigned Val2Int = kAsanAllocaPartialVal2; |
| 1848 | if (!ASan.DL->isLittleEndian()) { |
| 1849 | Val1Int = sys::getSwappedBytes(Val1Int); |
| 1850 | Val2Int = sys::getSwappedBytes(Val2Int); |
| 1851 | } |
| 1852 | Value *Val1 = shiftAllocaMagic(IRB.getInt32(Val1Int), IRB, Shift); |
| 1853 | Value *PartialBits = IRB.CreateAnd(PartialSize, IRB.getInt32(7)); |
| 1854 | // For BigEndian get 0x000000YZ -> 0xYZ000000. |
| 1855 | if (ASan.DL->isBigEndian()) |
| 1856 | PartialBits = IRB.CreateShl(PartialBits, IRB.getInt32(24)); |
| 1857 | Value *Val2 = IRB.getInt32(Val2Int); |
| 1858 | Value *Cond = |
| 1859 | IRB.CreateICmpNE(PartialBits, Constant::getNullValue(IRB.getInt32Ty())); |
| 1860 | Val2 = IRB.CreateSelect(Cond, shiftAllocaMagic(PartialBits, IRB, Shift), |
| 1861 | shiftAllocaMagic(Val2, IRB, Shift)); |
| 1862 | return IRB.CreateOr(Val1, Val2); |
| 1863 | } |
| 1864 | |
| 1865 | void FunctionStackPoisoner::handleDynamicAllocaCall( |
| 1866 | DynamicAllocaCall &AllocaCall) { |
| 1867 | AllocaInst *AI = AllocaCall.AI; |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 1868 | if (!doesDominateAllExits(AI)) { |
| 1869 | // We do not yet handle complex allocas |
| 1870 | AllocaCall.Poison = false; |
| 1871 | return; |
| 1872 | } |
| 1873 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 1874 | IRBuilder<> IRB(AI); |
| 1875 | |
| 1876 | PointerType *Int32PtrTy = PointerType::getUnqual(IRB.getInt32Ty()); |
| 1877 | const unsigned Align = std::max(kAllocaRzSize, AI->getAlignment()); |
| 1878 | const uint64_t AllocaRedzoneMask = kAllocaRzSize - 1; |
| 1879 | |
| 1880 | Value *Zero = Constant::getNullValue(IntptrTy); |
| 1881 | Value *AllocaRzSize = ConstantInt::get(IntptrTy, kAllocaRzSize); |
| 1882 | Value *AllocaRzMask = ConstantInt::get(IntptrTy, AllocaRedzoneMask); |
| 1883 | Value *NotAllocaRzMask = ConstantInt::get(IntptrTy, ~AllocaRedzoneMask); |
| 1884 | |
| 1885 | // Since we need to extend alloca with additional memory to locate |
| 1886 | // redzones, and OldSize is number of allocated blocks with |
| 1887 | // ElementSize size, get allocated memory size in bytes by |
| 1888 | // OldSize * ElementSize. |
| 1889 | unsigned ElementSize = ASan.DL->getTypeAllocSize(AI->getAllocatedType()); |
| 1890 | Value *OldSize = IRB.CreateMul(AI->getArraySize(), |
| 1891 | ConstantInt::get(IntptrTy, ElementSize)); |
| 1892 | |
| 1893 | // PartialSize = OldSize % 32 |
| 1894 | Value *PartialSize = IRB.CreateAnd(OldSize, AllocaRzMask); |
| 1895 | |
| 1896 | // Misalign = kAllocaRzSize - PartialSize; |
| 1897 | Value *Misalign = IRB.CreateSub(AllocaRzSize, PartialSize); |
| 1898 | |
| 1899 | // PartialPadding = Misalign != kAllocaRzSize ? Misalign : 0; |
| 1900 | Value *Cond = IRB.CreateICmpNE(Misalign, AllocaRzSize); |
| 1901 | Value *PartialPadding = IRB.CreateSelect(Cond, Misalign, Zero); |
| 1902 | |
| 1903 | // AdditionalChunkSize = Align + PartialPadding + kAllocaRzSize |
| 1904 | // Align is added to locate left redzone, PartialPadding for possible |
| 1905 | // partial redzone and kAllocaRzSize for right redzone respectively. |
| 1906 | Value *AdditionalChunkSize = IRB.CreateAdd( |
| 1907 | ConstantInt::get(IntptrTy, Align + kAllocaRzSize), PartialPadding); |
| 1908 | |
| 1909 | Value *NewSize = IRB.CreateAdd(OldSize, AdditionalChunkSize); |
| 1910 | |
| 1911 | // Insert new alloca with new NewSize and Align params. |
| 1912 | AllocaInst *NewAlloca = IRB.CreateAlloca(IRB.getInt8Ty(), NewSize); |
| 1913 | NewAlloca->setAlignment(Align); |
| 1914 | |
| 1915 | // NewAddress = Address + Align |
| 1916 | Value *NewAddress = IRB.CreateAdd(IRB.CreatePtrToInt(NewAlloca, IntptrTy), |
| 1917 | ConstantInt::get(IntptrTy, Align)); |
| 1918 | |
| 1919 | Value *NewAddressPtr = IRB.CreateIntToPtr(NewAddress, AI->getType()); |
| 1920 | |
| 1921 | // LeftRzAddress = NewAddress - kAllocaRzSize |
| 1922 | Value *LeftRzAddress = IRB.CreateSub(NewAddress, AllocaRzSize); |
| 1923 | |
| 1924 | // Poisoning left redzone. |
| 1925 | AllocaCall.LeftRzAddr = ASan.memToShadow(LeftRzAddress, IRB); |
| 1926 | IRB.CreateStore(ConstantInt::get(IRB.getInt32Ty(), kAsanAllocaLeftMagic), |
| 1927 | IRB.CreateIntToPtr(AllocaCall.LeftRzAddr, Int32PtrTy)); |
| 1928 | |
| 1929 | // PartialRzAligned = PartialRzAddr & ~AllocaRzMask |
| 1930 | Value *PartialRzAddr = IRB.CreateAdd(NewAddress, OldSize); |
| 1931 | Value *PartialRzAligned = IRB.CreateAnd(PartialRzAddr, NotAllocaRzMask); |
| 1932 | |
| 1933 | // Poisoning partial redzone. |
| 1934 | Value *PartialRzMagic = computePartialRzMagic(PartialSize, IRB); |
| 1935 | Value *PartialRzShadowAddr = ASan.memToShadow(PartialRzAligned, IRB); |
| 1936 | IRB.CreateStore(PartialRzMagic, |
| 1937 | IRB.CreateIntToPtr(PartialRzShadowAddr, Int32PtrTy)); |
| 1938 | |
| 1939 | // RightRzAddress |
| 1940 | // = (PartialRzAddr + AllocaRzMask) & ~AllocaRzMask |
| 1941 | Value *RightRzAddress = IRB.CreateAnd( |
| 1942 | IRB.CreateAdd(PartialRzAddr, AllocaRzMask), NotAllocaRzMask); |
| 1943 | |
| 1944 | // Poisoning right redzone. |
| 1945 | AllocaCall.RightRzAddr = ASan.memToShadow(RightRzAddress, IRB); |
| 1946 | IRB.CreateStore(ConstantInt::get(IRB.getInt32Ty(), kAsanAllocaRightMagic), |
| 1947 | IRB.CreateIntToPtr(AllocaCall.RightRzAddr, Int32PtrTy)); |
| 1948 | |
| 1949 | // Replace all uses of AddessReturnedByAlloca with NewAddress. |
| 1950 | AI->replaceAllUsesWith(NewAddressPtr); |
| 1951 | |
| 1952 | // We are done. Erase old alloca and store left, partial and right redzones |
| 1953 | // shadow addresses for future unpoisoning. |
| 1954 | AI->eraseFromParent(); |
Kostya Serebryany | ea2cb6f | 2014-11-21 21:25:18 +0000 | [diff] [blame] | 1955 | NumInstrumentedDynamicAllocas++; |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 1956 | } |