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