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