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