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