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