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 | |
| 16 | #define DEBUG_TYPE "asan" |
| 17 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Instrumentation.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ArrayRef.h" |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/DenseMap.h" |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DepthFirstIterator.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/OwningPtr.h" |
| 23 | #include "llvm/ADT/SmallSet.h" |
| 24 | #include "llvm/ADT/SmallString.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Evgeniy Stepanov | 617232f | 2012-05-23 11:52:12 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Triple.h" |
Alexey Samsonov | 3d43b63 | 2012-12-12 14:31:53 +0000 | [diff] [blame] | 29 | #include "llvm/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
| 31 | #include "llvm/IR/Function.h" |
| 32 | #include "llvm/IR/IRBuilder.h" |
| 33 | #include "llvm/IR/InlineAsm.h" |
| 34 | #include "llvm/IR/IntrinsicInst.h" |
| 35 | #include "llvm/IR/LLVMContext.h" |
| 36 | #include "llvm/IR/Module.h" |
| 37 | #include "llvm/IR/Type.h" |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 38 | #include "llvm/InstVisitor.h" |
Kostya Serebryany | 699ac28 | 2013-02-20 12:35:15 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CallSite.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CommandLine.h" |
| 41 | #include "llvm/Support/DataTypes.h" |
| 42 | #include "llvm/Support/Debug.h" |
Kostya Serebryany | 9e62b30 | 2013-06-03 14:46:56 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Endian.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 44 | #include "llvm/Support/system_error.h" |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 45 | #include "llvm/Transforms/Utils/ASanStackFrameLayout.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 46 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 47 | #include "llvm/Transforms/Utils/Cloning.h" |
Alexey Samsonov | 3d43b63 | 2012-12-12 14:31:53 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/Local.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Peter Collingbourne | 015370e | 2013-07-09 22:02:49 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/Utils/SpecialCaseList.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 51 | #include <algorithm> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 52 | #include <string> |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 53 | |
| 54 | using namespace llvm; |
| 55 | |
| 56 | static const uint64_t kDefaultShadowScale = 3; |
| 57 | static const uint64_t kDefaultShadowOffset32 = 1ULL << 29; |
| 58 | static const uint64_t kDefaultShadowOffset64 = 1ULL << 44; |
Kostya Serebryany | c5f44bc | 2013-02-11 14:36:01 +0000 | [diff] [blame] | 59 | static const uint64_t kDefaultShort64bitShadowOffset = 0x7FFF8000; // < 2G. |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 60 | static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41; |
Kostya Serebryany | 9e62b30 | 2013-06-03 14:46:56 +0000 | [diff] [blame] | 61 | static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa8000; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 62 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 63 | static const size_t kMinStackMallocSize = 1 << 6; // 64B |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 64 | static const size_t kMaxStackMallocSize = 1 << 16; // 64K |
| 65 | static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; |
| 66 | static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; |
| 67 | |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 68 | static const char *const kAsanModuleCtorName = "asan.module_ctor"; |
| 69 | static const char *const kAsanModuleDtorName = "asan.module_dtor"; |
| 70 | static const int kAsanCtorAndCtorPriority = 1; |
| 71 | static const char *const kAsanReportErrorTemplate = "__asan_report_"; |
| 72 | static const char *const kAsanReportLoadN = "__asan_report_load_n"; |
| 73 | static const char *const kAsanReportStoreN = "__asan_report_store_n"; |
| 74 | static const char *const kAsanRegisterGlobalsName = "__asan_register_globals"; |
Alexey Samsonov | f52b717 | 2013-08-05 13:19:49 +0000 | [diff] [blame] | 75 | static const char *const kAsanUnregisterGlobalsName = |
| 76 | "__asan_unregister_globals"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 77 | static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init"; |
| 78 | static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init"; |
| 79 | static const char *const kAsanInitName = "__asan_init_v3"; |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 80 | static const char *const kAsanCovName = "__sanitizer_cov"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 81 | static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return"; |
| 82 | static const char *const kAsanMappingOffsetName = "__asan_mapping_offset"; |
| 83 | static const char *const kAsanMappingScaleName = "__asan_mapping_scale"; |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 84 | static const int kMaxAsanStackMallocSizeClass = 10; |
| 85 | static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_"; |
| 86 | static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 87 | static const char *const kAsanGenPrefix = "__asan_gen_"; |
| 88 | static const char *const kAsanPoisonStackMemoryName = |
| 89 | "__asan_poison_stack_memory"; |
| 90 | static const char *const kAsanUnpoisonStackMemoryName = |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 91 | "__asan_unpoison_stack_memory"; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 92 | |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 93 | static const char *const kAsanOptionDetectUAR = |
| 94 | "__asan_option_detect_stack_use_after_return"; |
| 95 | |
David Blaikie | eacc287 | 2013-09-18 00:11:27 +0000 | [diff] [blame] | 96 | #ifndef NDEBUG |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 97 | static const int kAsanStackAfterReturnMagic = 0xf5; |
David Blaikie | eacc287 | 2013-09-18 00:11:27 +0000 | [diff] [blame] | 98 | #endif |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 99 | |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 100 | // Accesses sizes are powers of two: 1, 2, 4, 8, 16. |
| 101 | static const size_t kNumberOfAccessSizes = 5; |
| 102 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 103 | // Command-line flags. |
| 104 | |
| 105 | // This flag may need to be replaced with -f[no-]asan-reads. |
| 106 | static cl::opt<bool> ClInstrumentReads("asan-instrument-reads", |
| 107 | cl::desc("instrument read instructions"), cl::Hidden, cl::init(true)); |
| 108 | static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes", |
| 109 | cl::desc("instrument write instructions"), cl::Hidden, cl::init(true)); |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 110 | static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics", |
| 111 | cl::desc("instrument atomic instructions (rmw, cmpxchg)"), |
| 112 | cl::Hidden, cl::init(true)); |
Kostya Serebryany | 1e575ab | 2012-08-15 08:58:58 +0000 | [diff] [blame] | 113 | static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path", |
| 114 | cl::desc("use instrumentation with slow path for all accesses"), |
| 115 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 116 | // This flag limits the number of instructions to be instrumented |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 117 | // in any given BB. Normally, this should be set to unlimited (INT_MAX), |
| 118 | // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary |
| 119 | // set it to 10000. |
| 120 | static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb", |
| 121 | cl::init(10000), |
| 122 | cl::desc("maximal number of instructions to instrument in any given BB"), |
| 123 | cl::Hidden); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 124 | // This flag may need to be replaced with -f[no]asan-stack. |
| 125 | static cl::opt<bool> ClStack("asan-stack", |
| 126 | cl::desc("Handle stack memory"), cl::Hidden, cl::init(true)); |
| 127 | // This flag may need to be replaced with -f[no]asan-use-after-return. |
| 128 | static cl::opt<bool> ClUseAfterReturn("asan-use-after-return", |
| 129 | cl::desc("Check return-after-free"), cl::Hidden, cl::init(false)); |
| 130 | // This flag may need to be replaced with -f[no]asan-globals. |
| 131 | static cl::opt<bool> ClGlobals("asan-globals", |
| 132 | cl::desc("Handle global objects"), cl::Hidden, cl::init(true)); |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 133 | static cl::opt<bool> ClCoverage("asan-coverage", |
| 134 | cl::desc("ASan coverage"), cl::Hidden, cl::init(false)); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 135 | static cl::opt<bool> ClInitializers("asan-initialization-order", |
| 136 | cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 137 | static cl::opt<bool> ClMemIntrin("asan-memintrin", |
| 138 | cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true)); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 139 | static cl::opt<unsigned> ClRealignStack("asan-realign-stack", |
| 140 | cl::desc("Realign stack to the value of this flag (power of two)"), |
| 141 | cl::Hidden, cl::init(32)); |
Alexey Samsonov | ef51c3f | 2012-12-03 19:09:26 +0000 | [diff] [blame] | 142 | static cl::opt<std::string> ClBlacklistFile("asan-blacklist", |
| 143 | cl::desc("File containing the list of objects to ignore " |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 144 | "during instrumentation"), cl::Hidden); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 145 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 146 | // This is an experimental feature that will allow to choose between |
| 147 | // instrumented and non-instrumented code at link-time. |
| 148 | // If this option is on, just before instrumenting a function we create its |
| 149 | // clone; if the function is not changed by asan the clone is deleted. |
| 150 | // If we end up with a clone, we put the instrumented function into a section |
| 151 | // called "ASAN" and the uninstrumented function into a section called "NOASAN". |
| 152 | // |
| 153 | // This is still a prototype, we need to figure out a way to keep two copies of |
| 154 | // a function so that the linker can easily choose one of them. |
| 155 | static cl::opt<bool> ClKeepUninstrumented("asan-keep-uninstrumented-functions", |
| 156 | cl::desc("Keep uninstrumented copies of functions"), |
| 157 | cl::Hidden, cl::init(false)); |
| 158 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 159 | // These flags allow to change the shadow mapping. |
| 160 | // The shadow mapping looks like |
| 161 | // Shadow = (Mem >> scale) + (1 << offset_log) |
| 162 | static cl::opt<int> ClMappingScale("asan-mapping-scale", |
| 163 | cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0)); |
| 164 | static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log", |
| 165 | cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1)); |
Kostya Serebryany | c5f44bc | 2013-02-11 14:36:01 +0000 | [diff] [blame] | 166 | static cl::opt<bool> ClShort64BitOffset("asan-short-64bit-mapping-offset", |
| 167 | cl::desc("Use short immediate constant as the mapping offset for 64bit"), |
Kostya Serebryany | be73337 | 2013-02-12 11:11:02 +0000 | [diff] [blame] | 168 | cl::Hidden, cl::init(true)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 169 | |
| 170 | // Optimization flags. Not user visible, used mostly for testing |
| 171 | // and benchmarking the tool. |
| 172 | static cl::opt<bool> ClOpt("asan-opt", |
| 173 | cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true)); |
| 174 | static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp", |
| 175 | cl::desc("Instrument the same temp just once"), cl::Hidden, |
| 176 | cl::init(true)); |
| 177 | static cl::opt<bool> ClOptGlobals("asan-opt-globals", |
| 178 | cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true)); |
| 179 | |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 180 | static cl::opt<bool> ClCheckLifetime("asan-check-lifetime", |
| 181 | cl::desc("Use llvm.lifetime intrinsics to insert extra checks"), |
| 182 | cl::Hidden, cl::init(false)); |
| 183 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 184 | // Debug flags. |
| 185 | static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden, |
| 186 | cl::init(0)); |
| 187 | static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"), |
| 188 | cl::Hidden, cl::init(0)); |
| 189 | static cl::opt<std::string> ClDebugFunc("asan-debug-func", |
| 190 | cl::Hidden, cl::desc("Debug func")); |
| 191 | static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"), |
| 192 | cl::Hidden, cl::init(-1)); |
| 193 | static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"), |
| 194 | cl::Hidden, cl::init(-1)); |
| 195 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 196 | STATISTIC(NumInstrumentedReads, "Number of instrumented reads"); |
| 197 | STATISTIC(NumInstrumentedWrites, "Number of instrumented writes"); |
| 198 | STATISTIC(NumOptimizedAccessesToGlobalArray, |
| 199 | "Number of optimized accesses to global arrays"); |
| 200 | STATISTIC(NumOptimizedAccessesToGlobalVar, |
| 201 | "Number of optimized accesses to global vars"); |
| 202 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 203 | namespace { |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 204 | /// A set of dynamically initialized globals extracted from metadata. |
| 205 | class SetOfDynamicallyInitializedGlobals { |
| 206 | public: |
| 207 | void Init(Module& M) { |
| 208 | // Clang generates metadata identifying all dynamically initialized globals. |
| 209 | NamedMDNode *DynamicGlobals = |
| 210 | M.getNamedMetadata("llvm.asan.dynamically_initialized_globals"); |
| 211 | if (!DynamicGlobals) |
| 212 | return; |
| 213 | for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) { |
| 214 | MDNode *MDN = DynamicGlobals->getOperand(i); |
| 215 | assert(MDN->getNumOperands() == 1); |
| 216 | Value *VG = MDN->getOperand(0); |
| 217 | // The optimizer may optimize away a global entirely, in which case we |
| 218 | // cannot instrument access to it. |
| 219 | if (!VG) |
| 220 | continue; |
| 221 | DynInitGlobals.insert(cast<GlobalVariable>(VG)); |
| 222 | } |
| 223 | } |
| 224 | bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; } |
| 225 | private: |
| 226 | SmallSet<GlobalValue*, 32> DynInitGlobals; |
| 227 | }; |
| 228 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 229 | /// This struct defines the shadow mapping using the rule: |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 230 | /// shadow = (mem >> Scale) ADD-or-OR Offset. |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 231 | struct ShadowMapping { |
| 232 | int Scale; |
| 233 | uint64_t Offset; |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 234 | bool OrShadowOffset; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 237 | static ShadowMapping getShadowMapping(const Module &M, int LongSize) { |
Alexey Samsonov | 347bcd3 | 2013-01-17 11:12:32 +0000 | [diff] [blame] | 238 | llvm::Triple TargetTriple(M.getTargetTriple()); |
| 239 | bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android; |
Alexander Potapenko | 259e812 | 2013-02-12 12:41:12 +0000 | [diff] [blame] | 240 | bool IsMacOSX = TargetTriple.getOS() == llvm::Triple::MacOSX; |
Bill Schmidt | 0a9170d | 2013-07-26 01:35:43 +0000 | [diff] [blame] | 241 | bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64 || |
| 242 | TargetTriple.getArch() == llvm::Triple::ppc64le; |
Kostya Serebryany | be73337 | 2013-02-12 11:11:02 +0000 | [diff] [blame] | 243 | bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64; |
Kostya Serebryany | 9e62b30 | 2013-06-03 14:46:56 +0000 | [diff] [blame] | 244 | bool IsMIPS32 = TargetTriple.getArch() == llvm::Triple::mips || |
| 245 | TargetTriple.getArch() == llvm::Triple::mipsel; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 246 | |
| 247 | ShadowMapping Mapping; |
| 248 | |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 249 | // OR-ing shadow offset if more efficient (at least on x86), |
| 250 | // but on ppc64 we have to use add since the shadow offset is not neccesary |
| 251 | // 1/8-th of the address space. |
Kostya Serebryany | c5f44bc | 2013-02-11 14:36:01 +0000 | [diff] [blame] | 252 | Mapping.OrShadowOffset = !IsPPC64 && !ClShort64BitOffset; |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 253 | |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 254 | Mapping.Offset = IsAndroid ? 0 : |
Kostya Serebryany | 9e62b30 | 2013-06-03 14:46:56 +0000 | [diff] [blame] | 255 | (LongSize == 32 ? |
| 256 | (IsMIPS32 ? kMIPS32_ShadowOffset32 : kDefaultShadowOffset32) : |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 257 | IsPPC64 ? kPPC64_ShadowOffset64 : kDefaultShadowOffset64); |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 258 | if (!IsAndroid && ClShort64BitOffset && IsX86_64 && !IsMacOSX) { |
Kostya Serebryany | be73337 | 2013-02-12 11:11:02 +0000 | [diff] [blame] | 259 | assert(LongSize == 64); |
Kostya Serebryany | c5f44bc | 2013-02-11 14:36:01 +0000 | [diff] [blame] | 260 | Mapping.Offset = kDefaultShort64bitShadowOffset; |
Kostya Serebryany | caf11af | 2013-02-13 05:14:12 +0000 | [diff] [blame] | 261 | } |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 262 | if (!IsAndroid && ClMappingOffsetLog >= 0) { |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 263 | // Zero offset log is the special case. |
| 264 | Mapping.Offset = (ClMappingOffsetLog == 0) ? 0 : 1ULL << ClMappingOffsetLog; |
| 265 | } |
| 266 | |
| 267 | Mapping.Scale = kDefaultShadowScale; |
| 268 | if (ClMappingScale) { |
| 269 | Mapping.Scale = ClMappingScale; |
| 270 | } |
| 271 | |
| 272 | return Mapping; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 275 | static size_t RedzoneSizeForScale(int MappingScale) { |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 276 | // Redzone used for stack and globals is at least 32 bytes. |
| 277 | // 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] | 278 | return std::max(32U, 1U << MappingScale); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 279 | } |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 280 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 281 | /// AddressSanitizer: instrument the code in module to find memory bugs. |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 282 | struct AddressSanitizer : public FunctionPass { |
Alexey Samsonov | 819eddc | 2013-03-14 12:38:58 +0000 | [diff] [blame] | 283 | AddressSanitizer(bool CheckInitOrder = true, |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 284 | bool CheckUseAfterReturn = false, |
Alexey Samsonov | ef51c3f | 2012-12-03 19:09:26 +0000 | [diff] [blame] | 285 | bool CheckLifetime = false, |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 286 | StringRef BlacklistFile = StringRef()) |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 287 | : FunctionPass(ID), |
| 288 | CheckInitOrder(CheckInitOrder || ClInitializers), |
| 289 | CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn), |
Alexey Samsonov | ef51c3f | 2012-12-03 19:09:26 +0000 | [diff] [blame] | 290 | CheckLifetime(CheckLifetime || ClCheckLifetime), |
| 291 | BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 292 | : BlacklistFile) {} |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 293 | virtual const char *getPassName() const { |
| 294 | return "AddressSanitizerFunctionPass"; |
| 295 | } |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 296 | void instrumentMop(Instruction *I); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 297 | void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore, |
| 298 | Value *Addr, uint32_t TypeSize, bool IsWrite, |
| 299 | Value *SizeArgument); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 300 | Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, |
| 301 | Value *ShadowValue, uint32_t TypeSize); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 302 | Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr, |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 303 | bool IsWrite, size_t AccessSizeIndex, |
| 304 | Value *SizeArgument); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 305 | bool instrumentMemIntrinsic(MemIntrinsic *MI); |
| 306 | void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr, |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 307 | Value *Size, |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 308 | Instruction *InsertBefore, bool IsWrite); |
| 309 | Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 310 | bool runOnFunction(Function &F); |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 311 | bool maybeInsertAsanInitAtFunctionEntry(Function &F); |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 312 | void emitShadowMapping(Module &M, IRBuilder<> &IRB) const; |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 313 | virtual bool doInitialization(Module &M); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 314 | static char ID; // Pass identification, replacement for typeid |
| 315 | |
| 316 | private: |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 317 | void initializeCallbacks(Module &M); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 318 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 319 | bool ShouldInstrumentGlobal(GlobalVariable *G); |
Kostya Serebryany | 1cdc6e9 | 2011-11-18 01:41:06 +0000 | [diff] [blame] | 320 | bool LooksLikeCodeInBug11395(Instruction *I); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 321 | void FindDynamicInitializers(Module &M); |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 322 | bool GlobalIsLinkerInitialized(GlobalVariable *G); |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 323 | bool InjectCoverage(Function &F); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 324 | |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 325 | bool CheckInitOrder; |
| 326 | bool CheckUseAfterReturn; |
| 327 | bool CheckLifetime; |
Alexey Samsonov | 347bcd3 | 2013-01-17 11:12:32 +0000 | [diff] [blame] | 328 | SmallString<64> BlacklistFile; |
Alexey Samsonov | 347bcd3 | 2013-01-17 11:12:32 +0000 | [diff] [blame] | 329 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 330 | LLVMContext *C; |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 331 | DataLayout *TD; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 332 | int LongSize; |
| 333 | Type *IntptrTy; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 334 | ShadowMapping Mapping; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 335 | Function *AsanCtorFunction; |
| 336 | Function *AsanInitFunction; |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 337 | Function *AsanHandleNoReturnFunc; |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 338 | Function *AsanCovFunction; |
Peter Collingbourne | 015370e | 2013-07-09 22:02:49 +0000 | [diff] [blame] | 339 | OwningPtr<SpecialCaseList> BL; |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 340 | // This array is indexed by AccessIsWrite and log2(AccessSize). |
| 341 | Function *AsanErrorCallback[2][kNumberOfAccessSizes]; |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 342 | // This array is indexed by AccessIsWrite. |
| 343 | Function *AsanErrorCallbackSized[2]; |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 344 | InlineAsm *EmptyAsm; |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 345 | SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 346 | |
| 347 | friend struct FunctionStackPoisoner; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 348 | }; |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 349 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 350 | class AddressSanitizerModule : public ModulePass { |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 351 | public: |
Alexey Samsonov | 819eddc | 2013-03-14 12:38:58 +0000 | [diff] [blame] | 352 | AddressSanitizerModule(bool CheckInitOrder = true, |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 353 | StringRef BlacklistFile = StringRef()) |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 354 | : ModulePass(ID), |
Alexey Samsonov | ef51c3f | 2012-12-03 19:09:26 +0000 | [diff] [blame] | 355 | CheckInitOrder(CheckInitOrder || ClInitializers), |
| 356 | BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 357 | : BlacklistFile) {} |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 358 | bool runOnModule(Module &M); |
| 359 | static char ID; // Pass identification, replacement for typeid |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 360 | virtual const char *getPassName() const { |
| 361 | return "AddressSanitizerModule"; |
| 362 | } |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 363 | |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 364 | private: |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 365 | void initializeCallbacks(Module &M); |
| 366 | |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 367 | bool ShouldInstrumentGlobal(GlobalVariable *G); |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 368 | void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 369 | size_t MinRedzoneSizeForGlobal() const { |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 370 | return RedzoneSizeForScale(Mapping.Scale); |
| 371 | } |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 372 | |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 373 | bool CheckInitOrder; |
Alexey Samsonov | ef51c3f | 2012-12-03 19:09:26 +0000 | [diff] [blame] | 374 | SmallString<64> BlacklistFile; |
Alexey Samsonov | 347bcd3 | 2013-01-17 11:12:32 +0000 | [diff] [blame] | 375 | |
Peter Collingbourne | 015370e | 2013-07-09 22:02:49 +0000 | [diff] [blame] | 376 | OwningPtr<SpecialCaseList> BL; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 377 | SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals; |
| 378 | Type *IntptrTy; |
| 379 | LLVMContext *C; |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 380 | DataLayout *TD; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 381 | ShadowMapping Mapping; |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 382 | Function *AsanPoisonGlobals; |
| 383 | Function *AsanUnpoisonGlobals; |
| 384 | Function *AsanRegisterGlobals; |
| 385 | Function *AsanUnregisterGlobals; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 386 | }; |
| 387 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 388 | // Stack poisoning does not play well with exception handling. |
| 389 | // When an exception is thrown, we essentially bypass the code |
| 390 | // that unpoisones the stack. This is why the run-time library has |
| 391 | // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire |
| 392 | // stack in the interceptor. This however does not work inside the |
| 393 | // actual function which catches the exception. Most likely because the |
| 394 | // compiler hoists the load of the shadow value somewhere too high. |
| 395 | // This causes asan to report a non-existing bug on 453.povray. |
| 396 | // It sounds like an LLVM bug. |
| 397 | struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> { |
| 398 | Function &F; |
| 399 | AddressSanitizer &ASan; |
| 400 | DIBuilder DIB; |
| 401 | LLVMContext *C; |
| 402 | Type *IntptrTy; |
| 403 | Type *IntptrPtrTy; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 404 | ShadowMapping Mapping; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 405 | |
| 406 | SmallVector<AllocaInst*, 16> AllocaVec; |
| 407 | SmallVector<Instruction*, 8> RetVec; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 408 | unsigned StackAlignment; |
| 409 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 410 | Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1], |
| 411 | *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1]; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 412 | Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc; |
| 413 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 414 | // Stores a place and arguments of poisoning/unpoisoning call for alloca. |
| 415 | struct AllocaPoisonCall { |
| 416 | IntrinsicInst *InsBefore; |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 417 | AllocaInst *AI; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 418 | uint64_t Size; |
| 419 | bool DoPoison; |
| 420 | }; |
| 421 | SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec; |
| 422 | |
| 423 | // Maps Value to an AllocaInst from which the Value is originated. |
| 424 | typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy; |
| 425 | AllocaForValueMapTy AllocaForValue; |
| 426 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 427 | FunctionStackPoisoner(Function &F, AddressSanitizer &ASan) |
| 428 | : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C), |
| 429 | IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)), |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 430 | Mapping(ASan.Mapping), |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 431 | StackAlignment(1 << Mapping.Scale) {} |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 432 | |
| 433 | bool runOnFunction() { |
| 434 | if (!ClStack) return false; |
| 435 | // Collect alloca, ret, lifetime instructions etc. |
| 436 | for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), |
| 437 | DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) { |
| 438 | BasicBlock *BB = *DI; |
| 439 | visit(*BB); |
| 440 | } |
| 441 | if (AllocaVec.empty()) return false; |
| 442 | |
| 443 | initializeCallbacks(*F.getParent()); |
| 444 | |
| 445 | poisonStack(); |
| 446 | |
| 447 | if (ClDebugStack) { |
| 448 | DEBUG(dbgs() << F); |
| 449 | } |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | // Finds all static Alloca instructions and puts |
| 454 | // poisoned red zones around all of them. |
| 455 | // Then unpoison everything back before the function returns. |
| 456 | void poisonStack(); |
| 457 | |
| 458 | // ----------------------- Visitors. |
| 459 | /// \brief Collect all Ret instructions. |
| 460 | void visitReturnInst(ReturnInst &RI) { |
| 461 | RetVec.push_back(&RI); |
| 462 | } |
| 463 | |
| 464 | /// \brief Collect Alloca instructions we want (and can) handle. |
| 465 | void visitAllocaInst(AllocaInst &AI) { |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 466 | if (!isInterestingAlloca(AI)) return; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 467 | |
| 468 | StackAlignment = std::max(StackAlignment, AI.getAlignment()); |
| 469 | AllocaVec.push_back(&AI); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 472 | /// \brief Collect lifetime intrinsic calls to check for use-after-scope |
| 473 | /// errors. |
| 474 | void visitIntrinsicInst(IntrinsicInst &II) { |
| 475 | if (!ASan.CheckLifetime) return; |
| 476 | Intrinsic::ID ID = II.getIntrinsicID(); |
| 477 | if (ID != Intrinsic::lifetime_start && |
| 478 | ID != Intrinsic::lifetime_end) |
| 479 | return; |
| 480 | // Found lifetime intrinsic, add ASan instrumentation if necessary. |
| 481 | ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0)); |
| 482 | // If size argument is undefined, don't do anything. |
| 483 | if (Size->isMinusOne()) return; |
| 484 | // Check that size doesn't saturate uint64_t and can |
| 485 | // be stored in IntptrTy. |
| 486 | const uint64_t SizeValue = Size->getValue().getLimitedValue(); |
| 487 | if (SizeValue == ~0ULL || |
| 488 | !ConstantInt::isValueValidForType(IntptrTy, SizeValue)) |
| 489 | return; |
| 490 | // Find alloca instruction that corresponds to llvm.lifetime argument. |
| 491 | AllocaInst *AI = findAllocaForValue(II.getArgOperand(1)); |
| 492 | if (!AI) return; |
| 493 | bool DoPoison = (ID == Intrinsic::lifetime_end); |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 494 | AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison}; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 495 | AllocaPoisonCallVec.push_back(APC); |
| 496 | } |
| 497 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 498 | // ---------------------- Helpers. |
| 499 | void initializeCallbacks(Module &M); |
| 500 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 501 | // Check if we want (and can) handle this alloca. |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 502 | bool isInterestingAlloca(AllocaInst &AI) const { |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 503 | return (!AI.isArrayAllocation() && AI.isStaticAlloca() && |
| 504 | AI.getAllocatedType()->isSized() && |
| 505 | // alloca() may be called with 0 size, ignore it. |
| 506 | getAllocaSizeInBytes(&AI) > 0); |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 509 | uint64_t getAllocaSizeInBytes(AllocaInst *AI) const { |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 510 | Type *Ty = AI->getAllocatedType(); |
| 511 | uint64_t SizeInBytes = ASan.TD->getTypeAllocSize(Ty); |
| 512 | return SizeInBytes; |
| 513 | } |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 514 | /// Finds alloca where the value comes from. |
| 515 | AllocaInst *findAllocaForValue(Value *V); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 516 | void poisonRedZones(const ArrayRef<uint8_t> ShadowBytes, IRBuilder<> &IRB, |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 517 | Value *ShadowBase, bool DoPoison); |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 518 | void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 519 | |
| 520 | void SetShadowToStackAfterReturnInlined(IRBuilder<> &IRB, Value *ShadowBase, |
| 521 | int Size); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 524 | } // namespace |
| 525 | |
| 526 | char AddressSanitizer::ID = 0; |
| 527 | INITIALIZE_PASS(AddressSanitizer, "asan", |
| 528 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", |
| 529 | false, false) |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 530 | FunctionPass *llvm::createAddressSanitizerFunctionPass( |
Alexey Samsonov | ef51c3f | 2012-12-03 19:09:26 +0000 | [diff] [blame] | 531 | bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime, |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 532 | StringRef BlacklistFile) { |
Alexey Samsonov | df62452 | 2012-11-29 18:14:24 +0000 | [diff] [blame] | 533 | return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn, |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 534 | CheckLifetime, BlacklistFile); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 537 | char AddressSanitizerModule::ID = 0; |
| 538 | INITIALIZE_PASS(AddressSanitizerModule, "asan-module", |
| 539 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs." |
| 540 | "ModulePass", false, false) |
Alexey Samsonov | ef51c3f | 2012-12-03 19:09:26 +0000 | [diff] [blame] | 541 | ModulePass *llvm::createAddressSanitizerModulePass( |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 542 | bool CheckInitOrder, StringRef BlacklistFile) { |
| 543 | return new AddressSanitizerModule(CheckInitOrder, BlacklistFile); |
Alexander Potapenko | c94cf8f | 2012-01-23 11:22:43 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 546 | static size_t TypeSizeToSizeIndex(uint32_t TypeSize) { |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 547 | size_t Res = countTrailingZeros(TypeSize / 8); |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 548 | assert(Res < kNumberOfAccessSizes); |
| 549 | return Res; |
| 550 | } |
| 551 | |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 552 | // \brief Create a constant for Str so that we can pass it to the run-time lib. |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 553 | static GlobalVariable *createPrivateGlobalForString( |
| 554 | Module &M, StringRef Str, bool AllowMerging) { |
Chris Lattner | cf9e8f6 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 555 | Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 556 | // For module-local strings that can be merged with another one we set the |
Alexander Potapenko | 4f0335f | 2013-12-25 16:46:27 +0000 | [diff] [blame] | 557 | // private linkage and the unnamed_addr attribute. |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 558 | // Non-mergeable strings are made linker_private to remove them from the |
| 559 | // symbol table. "private" linkage doesn't work for Darwin, where the |
| 560 | // "L"-prefixed globals end up in __TEXT,__const section |
| 561 | // (see http://llvm.org/bugs/show_bug.cgi?id=17976 for more info). |
| 562 | GlobalValue::LinkageTypes linkage = |
Alexander Potapenko | 4f0335f | 2013-12-25 16:46:27 +0000 | [diff] [blame] | 563 | AllowMerging ? GlobalValue::PrivateLinkage |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 564 | : GlobalValue::LinkerPrivateLinkage; |
| 565 | GlobalVariable *GV = |
| 566 | new GlobalVariable(M, StrConst->getType(), true, |
| 567 | linkage, StrConst, kAsanGenPrefix); |
| 568 | if (AllowMerging) GV->setUnnamedAddr(true); |
Kostya Serebryany | 10cc12f | 2013-03-18 09:38:39 +0000 | [diff] [blame] | 569 | GV->setAlignment(1); // Strings may not be merged w/o setting align 1. |
| 570 | return GV; |
Kostya Serebryany | 139a937 | 2012-11-20 14:16:08 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | static bool GlobalWasGeneratedByAsan(GlobalVariable *G) { |
| 574 | return G->getName().find(kAsanGenPrefix) == 0; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 577 | Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) { |
| 578 | // Shadow >> scale |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 579 | Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); |
| 580 | if (Mapping.Offset == 0) |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 581 | return Shadow; |
| 582 | // (Shadow >> scale) | offset |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 583 | if (Mapping.OrShadowOffset) |
| 584 | return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); |
| 585 | else |
| 586 | return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 589 | void AddressSanitizer::instrumentMemIntrinsicParam( |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 590 | Instruction *OrigIns, |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 591 | Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) { |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 592 | IRBuilder<> IRB(InsertBefore); |
| 593 | if (Size->getType() != IntptrTy) |
| 594 | Size = IRB.CreateIntCast(Size, IntptrTy, false); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 595 | // Check the first byte. |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 596 | instrumentAddress(OrigIns, InsertBefore, Addr, 8, IsWrite, Size); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 597 | // Check the last byte. |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 598 | IRB.SetInsertPoint(InsertBefore); |
| 599 | Value *SizeMinusOne = IRB.CreateSub(Size, ConstantInt::get(IntptrTy, 1)); |
| 600 | Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); |
| 601 | Value *AddrLast = IRB.CreateAdd(AddrLong, SizeMinusOne); |
| 602 | instrumentAddress(OrigIns, InsertBefore, AddrLast, 8, IsWrite, Size); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // Instrument memset/memmove/memcpy |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 606 | bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 607 | Value *Dst = MI->getDest(); |
| 608 | MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI); |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 609 | Value *Src = MemTran ? MemTran->getSource() : 0; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 610 | Value *Length = MI->getLength(); |
| 611 | |
| 612 | Constant *ConstLength = dyn_cast<Constant>(Length); |
| 613 | Instruction *InsertBefore = MI; |
| 614 | if (ConstLength) { |
| 615 | if (ConstLength->isNullValue()) return false; |
| 616 | } else { |
| 617 | // The size is not a constant so it could be zero -- check at run-time. |
| 618 | IRBuilder<> IRB(InsertBefore); |
| 619 | |
| 620 | Value *Cmp = IRB.CreateICmpNE(Length, |
Kostya Serebryany | eeaf688 | 2012-07-02 11:42:29 +0000 | [diff] [blame] | 621 | Constant::getNullValue(Length->getType())); |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 622 | InsertBefore = SplitBlockAndInsertIfThen(Cmp, InsertBefore, false); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 625 | instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 626 | if (Src) |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 627 | instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 628 | return true; |
| 629 | } |
| 630 | |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 631 | // If I is an interesting memory access, return the PointerOperand |
| 632 | // and set IsWrite. Otherwise return NULL. |
| 633 | static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 634 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 635 | if (!ClInstrumentReads) return NULL; |
| 636 | *IsWrite = false; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 637 | return LI->getPointerOperand(); |
| 638 | } |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 639 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 640 | if (!ClInstrumentWrites) return NULL; |
| 641 | *IsWrite = true; |
| 642 | return SI->getPointerOperand(); |
| 643 | } |
| 644 | if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { |
| 645 | if (!ClInstrumentAtomics) return NULL; |
| 646 | *IsWrite = true; |
| 647 | return RMW->getPointerOperand(); |
| 648 | } |
| 649 | if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { |
| 650 | if (!ClInstrumentAtomics) return NULL; |
| 651 | *IsWrite = true; |
| 652 | return XCHG->getPointerOperand(); |
| 653 | } |
| 654 | return NULL; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 657 | bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) { |
| 658 | // If a global variable does not have dynamic initialization we don't |
| 659 | // have to instrument it. However, if a global does not have initializer |
| 660 | // at all, we assume it has dynamic initializer (in other TU). |
| 661 | return G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G); |
| 662 | } |
| 663 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 664 | void AddressSanitizer::instrumentMop(Instruction *I) { |
Axel Naumann | 4a12706 | 2012-09-17 14:20:57 +0000 | [diff] [blame] | 665 | bool IsWrite = false; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 666 | Value *Addr = isInterestingMemoryAccess(I, &IsWrite); |
| 667 | assert(Addr); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 668 | if (ClOpt && ClOptGlobals) { |
| 669 | if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) { |
| 670 | // If initialization order checking is disabled, a simple access to a |
| 671 | // dynamically initialized global is always valid. |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 672 | if (!CheckInitOrder || GlobalIsLinkerInitialized(G)) { |
| 673 | NumOptimizedAccessesToGlobalVar++; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 674 | return; |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr); |
| 678 | if (CE && CE->isGEPWithNoNotionalOverIndexing()) { |
| 679 | if (GlobalVariable *G = dyn_cast<GlobalVariable>(CE->getOperand(0))) { |
| 680 | if (CE->getOperand(1)->isNullValue() && GlobalIsLinkerInitialized(G)) { |
| 681 | NumOptimizedAccessesToGlobalArray++; |
| 682 | return; |
| 683 | } |
| 684 | } |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 685 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 686 | } |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 687 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 688 | Type *OrigPtrTy = Addr->getType(); |
| 689 | Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType(); |
| 690 | |
| 691 | assert(OrigTy->isSized()); |
Kostya Serebryany | 7ca384b | 2013-02-18 13:47:02 +0000 | [diff] [blame] | 692 | uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 693 | |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 694 | assert((TypeSize % 8) == 0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 695 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 696 | if (IsWrite) |
| 697 | NumInstrumentedWrites++; |
| 698 | else |
| 699 | NumInstrumentedReads++; |
| 700 | |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 701 | // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check. |
| 702 | if (TypeSize == 8 || TypeSize == 16 || |
| 703 | TypeSize == 32 || TypeSize == 64 || TypeSize == 128) |
| 704 | return instrumentAddress(I, I, Addr, TypeSize, IsWrite, 0); |
| 705 | // Instrument unusual size (but still multiple of 8). |
| 706 | // We can not do it with a single check, so we do 1-byte check for the first |
| 707 | // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able |
| 708 | // to report the actual access size. |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 709 | IRBuilder<> IRB(I); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 710 | Value *LastByte = IRB.CreateIntToPtr( |
| 711 | IRB.CreateAdd(IRB.CreatePointerCast(Addr, IntptrTy), |
| 712 | ConstantInt::get(IntptrTy, TypeSize / 8 - 1)), |
| 713 | OrigPtrTy); |
| 714 | Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8); |
| 715 | instrumentAddress(I, I, Addr, 8, IsWrite, Size); |
| 716 | instrumentAddress(I, I, LastByte, 8, IsWrite, Size); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Alexander Potapenko | 056e27e | 2012-04-23 10:47:31 +0000 | [diff] [blame] | 719 | // Validate the result of Module::getOrInsertFunction called for an interface |
| 720 | // function of AddressSanitizer. If the instrumented module defines a function |
| 721 | // with the same name, their prototypes must match, otherwise |
| 722 | // getOrInsertFunction returns a bitcast. |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 723 | static Function *checkInterfaceFunction(Constant *FuncOrBitcast) { |
Alexander Potapenko | 056e27e | 2012-04-23 10:47:31 +0000 | [diff] [blame] | 724 | if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast); |
| 725 | FuncOrBitcast->dump(); |
| 726 | report_fatal_error("trying to redefine an AddressSanitizer " |
| 727 | "interface function"); |
| 728 | } |
| 729 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 730 | Instruction *AddressSanitizer::generateCrashCode( |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 731 | Instruction *InsertBefore, Value *Addr, |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 732 | bool IsWrite, size_t AccessSizeIndex, Value *SizeArgument) { |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 733 | IRBuilder<> IRB(InsertBefore); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 734 | CallInst *Call = SizeArgument |
| 735 | ? IRB.CreateCall2(AsanErrorCallbackSized[IsWrite], Addr, SizeArgument) |
| 736 | : IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex], Addr); |
| 737 | |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 738 | // We don't do Call->setDoesNotReturn() because the BB already has |
| 739 | // UnreachableInst at the end. |
| 740 | // This EmptyAsm is required to avoid callback merge. |
| 741 | IRB.CreateCall(EmptyAsm); |
Kostya Serebryany | 3411f2e | 2012-01-06 18:09:21 +0000 | [diff] [blame] | 742 | return Call; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 745 | Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 746 | Value *ShadowValue, |
| 747 | uint32_t TypeSize) { |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 748 | size_t Granularity = 1 << Mapping.Scale; |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 749 | // Addr & (Granularity - 1) |
| 750 | Value *LastAccessedByte = IRB.CreateAnd( |
| 751 | AddrLong, ConstantInt::get(IntptrTy, Granularity - 1)); |
| 752 | // (Addr & (Granularity - 1)) + size - 1 |
| 753 | if (TypeSize / 8 > 1) |
| 754 | LastAccessedByte = IRB.CreateAdd( |
| 755 | LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)); |
| 756 | // (uint8_t) ((Addr & (Granularity-1)) + size - 1) |
| 757 | LastAccessedByte = IRB.CreateIntCast( |
Kostya Serebryany | 1e575ab | 2012-08-15 08:58:58 +0000 | [diff] [blame] | 758 | LastAccessedByte, ShadowValue->getType(), false); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 759 | // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue |
| 760 | return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue); |
| 761 | } |
| 762 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 763 | void AddressSanitizer::instrumentAddress(Instruction *OrigIns, |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 764 | Instruction *InsertBefore, |
| 765 | Value *Addr, uint32_t TypeSize, |
| 766 | bool IsWrite, Value *SizeArgument) { |
| 767 | IRBuilder<> IRB(InsertBefore); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 768 | Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); |
| 769 | |
| 770 | Type *ShadowTy = IntegerType::get( |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 771 | *C, std::max(8U, TypeSize >> Mapping.Scale)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 772 | Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); |
| 773 | Value *ShadowPtr = memToShadow(AddrLong, IRB); |
| 774 | Value *CmpVal = Constant::getNullValue(ShadowTy); |
| 775 | Value *ShadowValue = IRB.CreateLoad( |
| 776 | IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy)); |
| 777 | |
| 778 | Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal); |
Kostya Serebryany | 0f7a80d | 2012-08-13 14:08:46 +0000 | [diff] [blame] | 779 | size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize); |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 780 | size_t Granularity = 1 << Mapping.Scale; |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 781 | TerminatorInst *CrashTerm = 0; |
| 782 | |
Kostya Serebryany | 1e575ab | 2012-08-15 08:58:58 +0000 | [diff] [blame] | 783 | if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) { |
Evgeniy Stepanov | 8eb77d8 | 2012-10-19 10:48:31 +0000 | [diff] [blame] | 784 | TerminatorInst *CheckTerm = |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 785 | SplitBlockAndInsertIfThen(Cmp, InsertBefore, false); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 786 | assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional()); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 787 | BasicBlock *NextBB = CheckTerm->getSuccessor(0); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 788 | IRB.SetInsertPoint(CheckTerm); |
| 789 | Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 790 | BasicBlock *CrashBlock = |
| 791 | BasicBlock::Create(*C, "", NextBB->getParent(), NextBB); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 792 | CrashTerm = new UnreachableInst(*C, CrashBlock); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 793 | BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2); |
| 794 | ReplaceInstWithInst(CheckTerm, NewTerm); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 795 | } else { |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 796 | CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, true); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 797 | } |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 798 | |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 799 | Instruction *Crash = generateCrashCode( |
| 800 | CrashTerm, AddrLong, IsWrite, AccessSizeIndex, SizeArgument); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 801 | Crash->setDebugLoc(OrigIns->getDebugLoc()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 804 | void AddressSanitizerModule::createInitializerPoisonCalls( |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 805 | Module &M, GlobalValue *ModuleName) { |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 806 | // We do all of our poisoning and unpoisoning within _GLOBAL__I_a. |
| 807 | Function *GlobalInit = M.getFunction("_GLOBAL__I_a"); |
| 808 | // If that function is not present, this TU contains no globals, or they have |
| 809 | // all been optimized away |
| 810 | if (!GlobalInit) |
| 811 | return; |
| 812 | |
| 813 | // Set up the arguments to our poison/unpoison functions. |
| 814 | IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt()); |
| 815 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 816 | // 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] | 817 | Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy); |
| 818 | IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 819 | |
| 820 | // Add calls to unpoison all globals before each return instruction. |
| 821 | for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end(); |
| 822 | I != E; ++I) { |
| 823 | if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) { |
| 824 | CallInst::Create(AsanUnpoisonGlobals, "", RI); |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 829 | bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) { |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 830 | Type *Ty = cast<PointerType>(G->getType())->getElementType(); |
Kostya Serebryany | 2034335 | 2012-10-17 13:40:06 +0000 | [diff] [blame] | 831 | DEBUG(dbgs() << "GLOBAL: " << *G << "\n"); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 832 | |
Kostya Serebryany | 2fa38f8 | 2012-09-05 07:29:56 +0000 | [diff] [blame] | 833 | if (BL->isIn(*G)) return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 834 | if (!Ty->isSized()) return false; |
| 835 | if (!G->hasInitializer()) return false; |
Kostya Serebryany | 139a937 | 2012-11-20 14:16:08 +0000 | [diff] [blame] | 836 | if (GlobalWasGeneratedByAsan(G)) return false; // Our own global. |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 837 | // Touch only those globals that will not be defined in other modules. |
| 838 | // Don't handle ODR type linkages since other modules may be built w/o asan. |
| 839 | if (G->getLinkage() != GlobalVariable::ExternalLinkage && |
| 840 | G->getLinkage() != GlobalVariable::PrivateLinkage && |
| 841 | G->getLinkage() != GlobalVariable::InternalLinkage) |
| 842 | return false; |
| 843 | // Two problems with thread-locals: |
| 844 | // - The address of the main thread's copy can't be computed at link-time. |
| 845 | // - Need to poison all copies, not just the main thread's one. |
| 846 | if (G->isThreadLocal()) |
| 847 | return false; |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 848 | // For now, just ignore this Global if the alignment is large. |
| 849 | if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 850 | |
| 851 | // Ignore all the globals with the names starting with "\01L_OBJC_". |
| 852 | // Many of those are put into the .cstring section. The linker compresses |
| 853 | // that section by removing the spare \0s after the string terminator, so |
| 854 | // our redzones get broken. |
| 855 | if ((G->getName().find("\01L_OBJC_") == 0) || |
| 856 | (G->getName().find("\01l_OBJC_") == 0)) { |
| 857 | DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G); |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | if (G->hasSection()) { |
| 862 | StringRef Section(G->getSection()); |
| 863 | // Ignore the globals from the __OBJC section. The ObjC runtime assumes |
| 864 | // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to |
| 865 | // them. |
| 866 | if ((Section.find("__OBJC,") == 0) || |
| 867 | (Section.find("__DATA, __objc_") == 0)) { |
| 868 | DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G); |
| 869 | return false; |
| 870 | } |
| 871 | // See http://code.google.com/p/address-sanitizer/issues/detail?id=32 |
| 872 | // Constant CFString instances are compiled in the following way: |
| 873 | // -- the string buffer is emitted into |
| 874 | // __TEXT,__cstring,cstring_literals |
| 875 | // -- the constant NSConstantString structure referencing that buffer |
| 876 | // is placed into __DATA,__cfstring |
| 877 | // Therefore there's no point in placing redzones into __DATA,__cfstring. |
| 878 | // Moreover, it causes the linker to crash on OS X 10.7 |
| 879 | if (Section.find("__DATA,__cfstring") == 0) { |
| 880 | DEBUG(dbgs() << "Ignoring CFString: " << *G); |
| 881 | return false; |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | return true; |
| 886 | } |
| 887 | |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 888 | void AddressSanitizerModule::initializeCallbacks(Module &M) { |
| 889 | IRBuilder<> IRB(*C); |
| 890 | // Declare our poisoning and unpoisoning functions. |
| 891 | AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 892 | kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, NULL)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 893 | AsanPoisonGlobals->setLinkage(Function::ExternalLinkage); |
| 894 | AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
| 895 | kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL)); |
| 896 | AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage); |
| 897 | // Declare functions that register/unregister globals. |
| 898 | AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
| 899 | kAsanRegisterGlobalsName, IRB.getVoidTy(), |
| 900 | IntptrTy, IntptrTy, NULL)); |
| 901 | AsanRegisterGlobals->setLinkage(Function::ExternalLinkage); |
| 902 | AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( |
| 903 | kAsanUnregisterGlobalsName, |
| 904 | IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); |
| 905 | AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage); |
| 906 | } |
| 907 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 908 | // This function replaces all global variables with new variables that have |
| 909 | // trailing redzones. It also creates a function that poisons |
| 910 | // redzones and inserts this function into llvm.global_ctors. |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 911 | bool AddressSanitizerModule::runOnModule(Module &M) { |
| 912 | if (!ClGlobals) return false; |
| 913 | TD = getAnalysisIfAvailable<DataLayout>(); |
| 914 | if (!TD) |
| 915 | return false; |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 916 | BL.reset(SpecialCaseList::createOrDie(BlacklistFile)); |
Alexey Samsonov | 9a956e8 | 2012-11-29 18:27:01 +0000 | [diff] [blame] | 917 | if (BL->isIn(M)) return false; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 918 | C = &(M.getContext()); |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 919 | int LongSize = TD->getPointerSizeInBits(); |
| 920 | IntptrTy = Type::getIntNTy(*C, LongSize); |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 921 | Mapping = getShadowMapping(M, LongSize); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 922 | initializeCallbacks(M); |
| 923 | DynamicallyInitializedGlobals.Init(M); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 924 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 925 | SmallVector<GlobalVariable *, 16> GlobalsToChange; |
| 926 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 927 | for (Module::GlobalListType::iterator G = M.global_begin(), |
| 928 | E = M.global_end(); G != E; ++G) { |
| 929 | if (ShouldInstrumentGlobal(G)) |
| 930 | GlobalsToChange.push_back(G); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | size_t n = GlobalsToChange.size(); |
| 934 | if (n == 0) return false; |
| 935 | |
| 936 | // A global is described by a structure |
| 937 | // size_t beg; |
| 938 | // size_t size; |
| 939 | // size_t size_with_redzone; |
| 940 | // const char *name; |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 941 | // const char *module_name; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 942 | // size_t has_dynamic_init; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 943 | // We initialize an array of such structures and pass it to a run-time call. |
| 944 | StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy, |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 945 | IntptrTy, IntptrTy, |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 946 | IntptrTy, IntptrTy, NULL); |
Rafael Espindola | 44fee4e | 2013-10-01 13:32:03 +0000 | [diff] [blame] | 947 | SmallVector<Constant *, 16> Initializers(n); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 948 | |
| 949 | Function *CtorFunc = M.getFunction(kAsanModuleCtorName); |
| 950 | assert(CtorFunc); |
| 951 | IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 952 | |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 953 | bool HasDynamicallyInitializedGlobals = false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 954 | |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 955 | // We shouldn't merge same module names, as this string serves as unique |
| 956 | // module ID in runtime. |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 957 | GlobalVariable *ModuleName = createPrivateGlobalForString( |
| 958 | M, M.getModuleIdentifier(), /*AllowMerging*/false); |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 959 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 960 | for (size_t i = 0; i < n; i++) { |
Kostya Serebryany | e35d59a | 2013-01-24 10:43:50 +0000 | [diff] [blame] | 961 | static const uint64_t kMaxGlobalRedzone = 1 << 18; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 962 | GlobalVariable *G = GlobalsToChange[i]; |
| 963 | PointerType *PtrTy = cast<PointerType>(G->getType()); |
| 964 | Type *Ty = PtrTy->getElementType(); |
Kostya Serebryany | 84a7f2e8 | 2012-03-21 15:28:50 +0000 | [diff] [blame] | 965 | uint64_t SizeInBytes = TD->getTypeAllocSize(Ty); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 966 | uint64_t MinRZ = MinRedzoneSizeForGlobal(); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 967 | // MinRZ <= RZ <= kMaxGlobalRedzone |
| 968 | // and trying to make RZ to be ~ 1/4 of SizeInBytes. |
Kostya Serebryany | e35d59a | 2013-01-24 10:43:50 +0000 | [diff] [blame] | 969 | uint64_t RZ = std::max(MinRZ, |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 970 | std::min(kMaxGlobalRedzone, |
| 971 | (SizeInBytes / MinRZ / 4) * MinRZ)); |
| 972 | uint64_t RightRedzoneSize = RZ; |
| 973 | // Round up to MinRZ |
| 974 | if (SizeInBytes % MinRZ) |
| 975 | RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ); |
| 976 | assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 977 | Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 978 | // Determine whether this global should be poisoned in initialization. |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 979 | bool GlobalHasDynamicInitializer = |
| 980 | DynamicallyInitializedGlobals.Contains(G); |
Kostya Serebryany | 2fa38f8 | 2012-09-05 07:29:56 +0000 | [diff] [blame] | 981 | // Don't check initialization order if this global is blacklisted. |
Peter Collingbourne | 49062a9 | 2013-07-09 22:03:17 +0000 | [diff] [blame] | 982 | GlobalHasDynamicInitializer &= !BL->isIn(*G, "init"); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 983 | |
| 984 | StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL); |
| 985 | Constant *NewInitializer = ConstantStruct::get( |
| 986 | NewTy, G->getInitializer(), |
| 987 | Constant::getNullValue(RightRedZoneTy), NULL); |
| 988 | |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 989 | GlobalVariable *Name = |
| 990 | createPrivateGlobalForString(M, G->getName(), /*AllowMerging*/true); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 991 | |
| 992 | // Create a new global variable with enough space for a redzone. |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 993 | GlobalValue::LinkageTypes Linkage = G->getLinkage(); |
| 994 | if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage) |
| 995 | Linkage = GlobalValue::InternalLinkage; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 996 | GlobalVariable *NewGlobal = new GlobalVariable( |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 997 | M, NewTy, G->isConstant(), Linkage, |
Hans Wennborg | cbe34b4 | 2012-06-23 11:37:03 +0000 | [diff] [blame] | 998 | NewInitializer, "", G, G->getThreadLocalMode()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 999 | NewGlobal->copyAttributesFrom(G); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 1000 | NewGlobal->setAlignment(MinRZ); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1001 | |
| 1002 | Value *Indices2[2]; |
| 1003 | Indices2[0] = IRB.getInt32(0); |
| 1004 | Indices2[1] = IRB.getInt32(0); |
| 1005 | |
| 1006 | G->replaceAllUsesWith( |
Kostya Serebryany | 7471d13 | 2012-01-28 04:27:16 +0000 | [diff] [blame] | 1007 | ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1008 | NewGlobal->takeName(G); |
| 1009 | G->eraseFromParent(); |
| 1010 | |
| 1011 | Initializers[i] = ConstantStruct::get( |
| 1012 | GlobalStructTy, |
| 1013 | ConstantExpr::getPointerCast(NewGlobal, IntptrTy), |
| 1014 | ConstantInt::get(IntptrTy, SizeInBytes), |
| 1015 | ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize), |
| 1016 | ConstantExpr::getPointerCast(Name, IntptrTy), |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 1017 | ConstantExpr::getPointerCast(ModuleName, IntptrTy), |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1018 | ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer), |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1019 | NULL); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1020 | |
| 1021 | // Populate the first and last globals declared in this TU. |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 1022 | if (CheckInitOrder && GlobalHasDynamicInitializer) |
| 1023 | HasDynamicallyInitializedGlobals = true; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1024 | |
Kostya Serebryany | 2034335 | 2012-10-17 13:40:06 +0000 | [diff] [blame] | 1025 | DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n"); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n); |
| 1029 | GlobalVariable *AllGlobals = new GlobalVariable( |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 1030 | M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage, |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1031 | ConstantArray::get(ArrayOfGlobalStructTy, Initializers), ""); |
| 1032 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1033 | // Create calls for poisoning before initializers run and unpoisoning after. |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 1034 | if (CheckInitOrder && HasDynamicallyInitializedGlobals) |
| 1035 | createInitializerPoisonCalls(M, ModuleName); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1036 | IRB.CreateCall2(AsanRegisterGlobals, |
| 1037 | IRB.CreatePointerCast(AllGlobals, IntptrTy), |
| 1038 | ConstantInt::get(IntptrTy, n)); |
| 1039 | |
Kostya Serebryany | cd1aba8 | 2011-12-15 21:59:03 +0000 | [diff] [blame] | 1040 | // We also need to unregister globals at the end, e.g. when a shared library |
| 1041 | // gets closed. |
| 1042 | Function *AsanDtorFunction = Function::Create( |
| 1043 | FunctionType::get(Type::getVoidTy(*C), false), |
| 1044 | GlobalValue::InternalLinkage, kAsanModuleDtorName, &M); |
| 1045 | BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); |
| 1046 | IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB)); |
Kostya Serebryany | cd1aba8 | 2011-12-15 21:59:03 +0000 | [diff] [blame] | 1047 | IRB_Dtor.CreateCall2(AsanUnregisterGlobals, |
| 1048 | IRB.CreatePointerCast(AllGlobals, IntptrTy), |
| 1049 | ConstantInt::get(IntptrTy, n)); |
| 1050 | appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority); |
| 1051 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1052 | DEBUG(dbgs() << M); |
| 1053 | return true; |
| 1054 | } |
| 1055 | |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1056 | void AddressSanitizer::initializeCallbacks(Module &M) { |
| 1057 | IRBuilder<> IRB(*C); |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 1058 | // Create __asan_report* callbacks. |
| 1059 | for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) { |
| 1060 | for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; |
| 1061 | AccessSizeIndex++) { |
| 1062 | // IsWrite and TypeSize are encoded in the function name. |
| 1063 | std::string FunctionName = std::string(kAsanReportErrorTemplate) + |
| 1064 | (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex); |
Kostya Serebryany | 986b8da | 2012-07-17 11:04:12 +0000 | [diff] [blame] | 1065 | // If we are merging crash callbacks, they have two parameters. |
Kostya Serebryany | 157a515 | 2012-11-07 12:42:18 +0000 | [diff] [blame] | 1066 | AsanErrorCallback[AccessIsWrite][AccessSizeIndex] = |
| 1067 | checkInterfaceFunction(M.getOrInsertFunction( |
| 1068 | FunctionName, IRB.getVoidTy(), IntptrTy, NULL)); |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 1069 | } |
| 1070 | } |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 1071 | AsanErrorCallbackSized[0] = checkInterfaceFunction(M.getOrInsertFunction( |
| 1072 | kAsanReportLoadN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); |
| 1073 | AsanErrorCallbackSized[1] = checkInterfaceFunction(M.getOrInsertFunction( |
| 1074 | kAsanReportStoreN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1075 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1076 | AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction( |
| 1077 | kAsanHandleNoReturnName, IRB.getVoidTy(), NULL)); |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 1078 | AsanCovFunction = checkInterfaceFunction(M.getOrInsertFunction( |
| 1079 | kAsanCovName, IRB.getVoidTy(), IntptrTy, NULL)); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 1080 | // We insert an empty inline asm after __asan_report* to avoid callback merge. |
| 1081 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 1082 | StringRef(""), StringRef(""), |
| 1083 | /*hasSideEffects=*/true); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 1086 | void AddressSanitizer::emitShadowMapping(Module &M, IRBuilder<> &IRB) const { |
Alexey Samsonov | 347bcd3 | 2013-01-17 11:12:32 +0000 | [diff] [blame] | 1087 | // Tell the values of mapping offset and scale to the run-time. |
| 1088 | GlobalValue *asan_mapping_offset = |
| 1089 | new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage, |
| 1090 | ConstantInt::get(IntptrTy, Mapping.Offset), |
| 1091 | kAsanMappingOffsetName); |
| 1092 | // Read the global, otherwise it may be optimized away. |
| 1093 | IRB.CreateLoad(asan_mapping_offset, true); |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 1094 | |
Alexey Samsonov | 347bcd3 | 2013-01-17 11:12:32 +0000 | [diff] [blame] | 1095 | GlobalValue *asan_mapping_scale = |
| 1096 | new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage, |
| 1097 | ConstantInt::get(IntptrTy, Mapping.Scale), |
| 1098 | kAsanMappingScaleName); |
| 1099 | // Read the global, otherwise it may be optimized away. |
| 1100 | IRB.CreateLoad(asan_mapping_scale, true); |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1103 | // virtual |
| 1104 | bool AddressSanitizer::doInitialization(Module &M) { |
| 1105 | // Initialize the private fields. No one has accessed them before. |
| 1106 | TD = getAnalysisIfAvailable<DataLayout>(); |
| 1107 | |
| 1108 | if (!TD) |
| 1109 | return false; |
Alexey Samsonov | e4b5fb8 | 2013-08-12 11:46:09 +0000 | [diff] [blame] | 1110 | BL.reset(SpecialCaseList::createOrDie(BlacklistFile)); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1111 | DynamicallyInitializedGlobals.Init(M); |
| 1112 | |
| 1113 | C = &(M.getContext()); |
| 1114 | LongSize = TD->getPointerSizeInBits(); |
| 1115 | IntptrTy = Type::getIntNTy(*C, LongSize); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1116 | |
| 1117 | AsanCtorFunction = Function::Create( |
| 1118 | FunctionType::get(Type::getVoidTy(*C), false), |
| 1119 | GlobalValue::InternalLinkage, kAsanModuleCtorName, &M); |
| 1120 | BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction); |
| 1121 | // call __asan_init in the module ctor. |
| 1122 | IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB)); |
| 1123 | AsanInitFunction = checkInterfaceFunction( |
| 1124 | M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL)); |
| 1125 | AsanInitFunction->setLinkage(Function::ExternalLinkage); |
| 1126 | IRB.CreateCall(AsanInitFunction); |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 1127 | |
Evgeniy Stepanov | 1366536 | 2014-01-16 10:19:12 +0000 | [diff] [blame^] | 1128 | Mapping = getShadowMapping(M, LongSize); |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 1129 | emitShadowMapping(M, IRB); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1130 | |
Kostya Serebryany | cd1aba8 | 2011-12-15 21:59:03 +0000 | [diff] [blame] | 1131 | appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1132 | return true; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 1135 | bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) { |
| 1136 | // For each NSObject descendant having a +load method, this method is invoked |
| 1137 | // by the ObjC runtime before any of the static constructors is called. |
| 1138 | // Therefore we need to instrument such methods with a call to __asan_init |
| 1139 | // at the beginning in order to initialize our runtime before any access to |
| 1140 | // the shadow memory. |
| 1141 | // We cannot just ignore these methods, because they may call other |
| 1142 | // instrumented functions. |
| 1143 | if (F.getName().find(" load]") != std::string::npos) { |
| 1144 | IRBuilder<> IRB(F.begin()->begin()); |
| 1145 | IRB.CreateCall(AsanInitFunction); |
| 1146 | return true; |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 1151 | // Poor man's coverage that works with ASan. |
| 1152 | // We create a Guard boolean variable with the same linkage |
| 1153 | // as the function and inject this code into the entry block: |
| 1154 | // if (*Guard) { |
| 1155 | // __sanitizer_cov(&F); |
| 1156 | // *Guard = 1; |
| 1157 | // } |
| 1158 | // The accesses to Guard are atomic. The rest of the logic is |
| 1159 | // in __sanitizer_cov (it's fine to call it more than once). |
| 1160 | // |
| 1161 | // This coverage implementation provides very limited data: |
| 1162 | // it only tells if a given function was ever executed. |
| 1163 | // No counters, no per-basic-block or per-edge data. |
| 1164 | // But for many use cases this is what we need and the added slowdown |
| 1165 | // is negligible. This simple implementation will probably be obsoleted |
| 1166 | // by the upcoming Clang-based coverage implementation. |
| 1167 | // By having it here and now we hope to |
| 1168 | // a) get the functionality to users earlier and |
| 1169 | // b) collect usage statistics to help improve Clang coverage design. |
| 1170 | bool AddressSanitizer::InjectCoverage(Function &F) { |
| 1171 | if (!ClCoverage) return false; |
Reid Kleckner | 30b2a9a | 2013-12-10 21:49:28 +0000 | [diff] [blame] | 1172 | |
| 1173 | // Skip static allocas at the top of the entry block so they don't become |
| 1174 | // dynamic when we split the block. If we used our optimized stack layout, |
| 1175 | // then there will only be one alloca and it will come first. |
| 1176 | BasicBlock &Entry = F.getEntryBlock(); |
| 1177 | BasicBlock::iterator IP = Entry.getFirstInsertionPt(), BE = Entry.end(); |
| 1178 | for (; IP != BE; ++IP) { |
| 1179 | AllocaInst *AI = dyn_cast<AllocaInst>(IP); |
| 1180 | if (!AI || !AI->isStaticAlloca()) |
| 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | IRBuilder<> IRB(IP); |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 1185 | Type *Int8Ty = IRB.getInt8Ty(); |
| 1186 | GlobalVariable *Guard = new GlobalVariable( |
Kostya Serebryany | 0604c62 | 2013-11-15 09:52:05 +0000 | [diff] [blame] | 1187 | *F.getParent(), Int8Ty, false, GlobalValue::PrivateLinkage, |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 1188 | Constant::getNullValue(Int8Ty), "__asan_gen_cov_" + F.getName()); |
| 1189 | LoadInst *Load = IRB.CreateLoad(Guard); |
| 1190 | Load->setAtomic(Monotonic); |
| 1191 | Load->setAlignment(1); |
| 1192 | Value *Cmp = IRB.CreateICmpEQ(Constant::getNullValue(Int8Ty), Load); |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 1193 | Instruction *Ins = SplitBlockAndInsertIfThen(Cmp, IP, false); |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 1194 | IRB.SetInsertPoint(Ins); |
| 1195 | // We pass &F to __sanitizer_cov. We could avoid this and rely on |
| 1196 | // GET_CALLER_PC, but having the PC of the first instruction is just nice. |
| 1197 | IRB.CreateCall(AsanCovFunction, IRB.CreatePointerCast(&F, IntptrTy)); |
| 1198 | StoreInst *Store = IRB.CreateStore(ConstantInt::get(Int8Ty, 1), Guard); |
| 1199 | Store->setAtomic(Monotonic); |
| 1200 | Store->setAlignment(1); |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1204 | bool AddressSanitizer::runOnFunction(Function &F) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1205 | if (BL->isIn(F)) return false; |
| 1206 | if (&F == AsanCtorFunction) return false; |
Kostya Serebryany | 6b5b58d | 2013-03-18 07:33:49 +0000 | [diff] [blame] | 1207 | if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false; |
Kostya Serebryany | 2034335 | 2012-10-17 13:40:06 +0000 | [diff] [blame] | 1208 | DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n"); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 1209 | initializeCallbacks(*F.getParent()); |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 1210 | |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 1211 | // If needed, insert __asan_init before checking for SanitizeAddress attr. |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 1212 | maybeInsertAsanInitAtFunctionEntry(F); |
| 1213 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1214 | if (!F.hasFnAttribute(Attribute::SanitizeAddress)) |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 1215 | return false; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1216 | |
| 1217 | if (!ClDebugFunc.empty() && ClDebugFunc != F.getName()) |
| 1218 | return false; |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 1219 | |
| 1220 | // We want to instrument every address only once per basic block (unless there |
| 1221 | // are calls between uses). |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1222 | SmallSet<Value*, 16> TempsToInstrument; |
| 1223 | SmallVector<Instruction*, 16> ToInstrument; |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 1224 | SmallVector<Instruction*, 8> NoReturnCalls; |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1225 | int NumAllocas = 0; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1226 | bool IsWrite; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Fill the set of memory operations to instrument. |
| 1229 | for (Function::iterator FI = F.begin(), FE = F.end(); |
| 1230 | FI != FE; ++FI) { |
| 1231 | TempsToInstrument.clear(); |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 1232 | int NumInsnsPerBB = 0; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1233 | for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); |
| 1234 | BI != BE; ++BI) { |
Kostya Serebryany | 687d078 | 2012-01-11 18:15:23 +0000 | [diff] [blame] | 1235 | if (LooksLikeCodeInBug11395(BI)) return false; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1236 | if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1237 | if (ClOpt && ClOptSameTemp) { |
| 1238 | if (!TempsToInstrument.insert(Addr)) |
| 1239 | continue; // We've seen this temp in the current BB. |
| 1240 | } |
| 1241 | } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) { |
| 1242 | // ok, take it. |
| 1243 | } else { |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1244 | if (isa<AllocaInst>(BI)) |
| 1245 | NumAllocas++; |
Kostya Serebryany | 699ac28 | 2013-02-20 12:35:15 +0000 | [diff] [blame] | 1246 | CallSite CS(BI); |
| 1247 | if (CS) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1248 | // A call inside BB. |
| 1249 | TempsToInstrument.clear(); |
Kostya Serebryany | 699ac28 | 2013-02-20 12:35:15 +0000 | [diff] [blame] | 1250 | if (CS.doesNotReturn()) |
| 1251 | NoReturnCalls.push_back(CS.getInstruction()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1252 | } |
| 1253 | continue; |
| 1254 | } |
| 1255 | ToInstrument.push_back(BI); |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 1256 | NumInsnsPerBB++; |
| 1257 | if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) |
| 1258 | break; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1262 | Function *UninstrumentedDuplicate = 0; |
| 1263 | bool LikelyToInstrument = |
| 1264 | !NoReturnCalls.empty() || !ToInstrument.empty() || (NumAllocas > 0); |
| 1265 | if (ClKeepUninstrumented && LikelyToInstrument) { |
| 1266 | ValueToValueMapTy VMap; |
| 1267 | UninstrumentedDuplicate = CloneFunction(&F, VMap, false); |
| 1268 | UninstrumentedDuplicate->removeFnAttr(Attribute::SanitizeAddress); |
| 1269 | UninstrumentedDuplicate->setName("NOASAN_" + F.getName()); |
| 1270 | F.getParent()->getFunctionList().push_back(UninstrumentedDuplicate); |
| 1271 | } |
| 1272 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1273 | // Instrument. |
| 1274 | int NumInstrumented = 0; |
| 1275 | for (size_t i = 0, n = ToInstrument.size(); i != n; i++) { |
| 1276 | Instruction *Inst = ToInstrument[i]; |
| 1277 | if (ClDebugMin < 0 || ClDebugMax < 0 || |
| 1278 | (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) { |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1279 | if (isInterestingMemoryAccess(Inst, &IsWrite)) |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1280 | instrumentMop(Inst); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1281 | else |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1282 | instrumentMemIntrinsic(cast<MemIntrinsic>(Inst)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1283 | } |
| 1284 | NumInstrumented++; |
| 1285 | } |
| 1286 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1287 | FunctionStackPoisoner FSP(F, *this); |
| 1288 | bool ChangedStack = FSP.runOnFunction(); |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 1289 | |
| 1290 | // We must unpoison the stack before every NoReturn call (throw, _exit, etc). |
| 1291 | // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37 |
| 1292 | for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) { |
| 1293 | Instruction *CI = NoReturnCalls[i]; |
| 1294 | IRBuilder<> IRB(CI); |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1295 | IRB.CreateCall(AsanHandleNoReturnFunc); |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1298 | bool res = NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty(); |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 1299 | |
| 1300 | if (InjectCoverage(F)) |
| 1301 | res = true; |
| 1302 | |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 1303 | DEBUG(dbgs() << "ASAN done instrumenting: " << res << " " << F << "\n"); |
| 1304 | |
| 1305 | if (ClKeepUninstrumented) { |
| 1306 | if (!res) { |
| 1307 | // No instrumentation is done, no need for the duplicate. |
| 1308 | if (UninstrumentedDuplicate) |
| 1309 | UninstrumentedDuplicate->eraseFromParent(); |
| 1310 | } else { |
| 1311 | // The function was instrumented. We must have the duplicate. |
| 1312 | assert(UninstrumentedDuplicate); |
| 1313 | UninstrumentedDuplicate->setSection("NOASAN"); |
| 1314 | assert(!F.hasSection()); |
| 1315 | F.setSection("ASAN"); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | return res; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1322 | // Workaround for bug 11395: we don't want to instrument stack in functions |
| 1323 | // with large assembly blobs (32-bit only), otherwise reg alloc may crash. |
| 1324 | // FIXME: remove once the bug 11395 is fixed. |
| 1325 | bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) { |
| 1326 | if (LongSize != 32) return false; |
| 1327 | CallInst *CI = dyn_cast<CallInst>(I); |
| 1328 | if (!CI || !CI->isInlineAsm()) return false; |
| 1329 | if (CI->getNumArgOperands() <= 5) return false; |
| 1330 | // We have inline assembly with quite a few arguments. |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
| 1334 | void FunctionStackPoisoner::initializeCallbacks(Module &M) { |
| 1335 | IRBuilder<> IRB(*C); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1336 | for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) { |
| 1337 | std::string Suffix = itostr(i); |
| 1338 | AsanStackMallocFunc[i] = checkInterfaceFunction( |
| 1339 | M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy, |
| 1340 | IntptrTy, IntptrTy, NULL)); |
| 1341 | AsanStackFreeFunc[i] = checkInterfaceFunction(M.getOrInsertFunction( |
| 1342 | kAsanStackFreeNameTemplate + Suffix, IRB.getVoidTy(), IntptrTy, |
| 1343 | IntptrTy, IntptrTy, NULL)); |
| 1344 | } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1345 | AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction( |
| 1346 | kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); |
| 1347 | AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction( |
| 1348 | kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); |
| 1349 | } |
| 1350 | |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1351 | void |
| 1352 | FunctionStackPoisoner::poisonRedZones(const ArrayRef<uint8_t> ShadowBytes, |
| 1353 | IRBuilder<> &IRB, Value *ShadowBase, |
| 1354 | bool DoPoison) { |
| 1355 | size_t n = ShadowBytes.size(); |
Kostya Serebryany | ff7bde1 | 2013-12-23 09:24:36 +0000 | [diff] [blame] | 1356 | size_t i = 0; |
| 1357 | // We need to (un)poison n bytes of stack shadow. Poison as many as we can |
| 1358 | // using 64-bit stores (if we are on 64-bit arch), then poison the rest |
| 1359 | // with 32-bit stores, then with 16-byte stores, then with 8-byte stores. |
| 1360 | for (size_t LargeStoreSizeInBytes = ASan.LongSize / 8; |
| 1361 | LargeStoreSizeInBytes != 0; LargeStoreSizeInBytes /= 2) { |
| 1362 | for (; i + LargeStoreSizeInBytes - 1 < n; i += LargeStoreSizeInBytes) { |
| 1363 | uint64_t Val = 0; |
| 1364 | for (size_t j = 0; j < LargeStoreSizeInBytes; j++) { |
| 1365 | if (ASan.TD->isLittleEndian()) |
| 1366 | Val |= (uint64_t)ShadowBytes[i + j] << (8 * j); |
| 1367 | else |
| 1368 | Val = (Val << 8) | ShadowBytes[i + j]; |
| 1369 | } |
| 1370 | if (!Val) continue; |
| 1371 | Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)); |
| 1372 | Type *StoreTy = Type::getIntNTy(*C, LargeStoreSizeInBytes * 8); |
| 1373 | Value *Poison = ConstantInt::get(StoreTy, DoPoison ? Val : 0); |
| 1374 | IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, StoreTy->getPointerTo())); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1375 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1379 | // Fake stack allocator (asan_fake_stack.h) has 11 size classes |
| 1380 | // for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass |
| 1381 | static int StackMallocSizeClass(uint64_t LocalStackSize) { |
| 1382 | assert(LocalStackSize <= kMaxStackMallocSize); |
| 1383 | uint64_t MaxSize = kMinStackMallocSize; |
| 1384 | for (int i = 0; ; i++, MaxSize *= 2) |
| 1385 | if (LocalStackSize <= MaxSize) |
| 1386 | return i; |
| 1387 | llvm_unreachable("impossible LocalStackSize"); |
| 1388 | } |
| 1389 | |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1390 | // Set Size bytes starting from ShadowBase to kAsanStackAfterReturnMagic. |
| 1391 | // We can not use MemSet intrinsic because it may end up calling the actual |
| 1392 | // memset. Size is a multiple of 8. |
| 1393 | // Currently this generates 8-byte stores on x86_64; it may be better to |
| 1394 | // generate wider stores. |
| 1395 | void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined( |
| 1396 | IRBuilder<> &IRB, Value *ShadowBase, int Size) { |
| 1397 | assert(!(Size % 8)); |
| 1398 | assert(kAsanStackAfterReturnMagic == 0xf5); |
| 1399 | for (int i = 0; i < Size; i += 8) { |
| 1400 | Value *p = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)); |
| 1401 | IRB.CreateStore(ConstantInt::get(IRB.getInt64Ty(), 0xf5f5f5f5f5f5f5f5ULL), |
| 1402 | IRB.CreateIntToPtr(p, IRB.getInt64Ty()->getPointerTo())); |
| 1403 | } |
| 1404 | } |
| 1405 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1406 | void FunctionStackPoisoner::poisonStack() { |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1407 | int StackMallocIdx = -1; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1408 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1409 | assert(AllocaVec.size() > 0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1410 | Instruction *InsBefore = AllocaVec[0]; |
| 1411 | IRBuilder<> IRB(InsBefore); |
| 1412 | |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1413 | SmallVector<ASanStackVariableDescription, 16> SVD; |
| 1414 | SVD.reserve(AllocaVec.size()); |
| 1415 | for (size_t i = 0, n = AllocaVec.size(); i < n; i++) { |
| 1416 | AllocaInst *AI = AllocaVec[i]; |
| 1417 | ASanStackVariableDescription D = { AI->getName().data(), |
| 1418 | getAllocaSizeInBytes(AI), |
| 1419 | AI->getAlignment(), AI, 0}; |
| 1420 | SVD.push_back(D); |
| 1421 | } |
| 1422 | // Minimal header size (left redzone) is 4 pointers, |
| 1423 | // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms. |
| 1424 | size_t MinHeaderSize = ASan.LongSize / 2; |
| 1425 | ASanStackFrameLayout L; |
| 1426 | ComputeASanStackFrameLayout(SVD, 1UL << Mapping.Scale, MinHeaderSize, &L); |
| 1427 | DEBUG(dbgs() << L.DescriptionString << " --- " << L.FrameSize << "\n"); |
| 1428 | uint64_t LocalStackSize = L.FrameSize; |
| 1429 | bool DoStackMalloc = |
| 1430 | ASan.CheckUseAfterReturn && LocalStackSize <= kMaxStackMallocSize; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1431 | |
| 1432 | Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize); |
| 1433 | AllocaInst *MyAlloca = |
| 1434 | new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1435 | assert((ClRealignStack & (ClRealignStack - 1)) == 0); |
| 1436 | size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack); |
| 1437 | MyAlloca->setAlignment(FrameAlignment); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1438 | assert(MyAlloca->isStaticAlloca()); |
| 1439 | Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy); |
| 1440 | Value *LocalStackBase = OrigStackBase; |
| 1441 | |
| 1442 | if (DoStackMalloc) { |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1443 | // LocalStackBase = OrigStackBase |
| 1444 | // if (__asan_option_detect_stack_use_after_return) |
| 1445 | // LocalStackBase = __asan_stack_malloc_N(LocalStackBase, OrigStackBase); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1446 | StackMallocIdx = StackMallocSizeClass(LocalStackSize); |
| 1447 | assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1448 | Constant *OptionDetectUAR = F.getParent()->getOrInsertGlobal( |
| 1449 | kAsanOptionDetectUAR, IRB.getInt32Ty()); |
| 1450 | Value *Cmp = IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUAR), |
| 1451 | Constant::getNullValue(IRB.getInt32Ty())); |
Evgeniy Stepanov | a9164e9 | 2013-12-19 13:29:56 +0000 | [diff] [blame] | 1452 | Instruction *Term = SplitBlockAndInsertIfThen(Cmp, InsBefore, false); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1453 | BasicBlock *CmpBlock = cast<Instruction>(Cmp)->getParent(); |
| 1454 | IRBuilder<> IRBIf(Term); |
| 1455 | LocalStackBase = IRBIf.CreateCall2( |
| 1456 | AsanStackMallocFunc[StackMallocIdx], |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1457 | ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 1458 | BasicBlock *SetBlock = cast<Instruction>(LocalStackBase)->getParent(); |
| 1459 | IRB.SetInsertPoint(InsBefore); |
| 1460 | PHINode *Phi = IRB.CreatePHI(IntptrTy, 2); |
| 1461 | Phi->addIncoming(OrigStackBase, CmpBlock); |
| 1462 | Phi->addIncoming(LocalStackBase, SetBlock); |
| 1463 | LocalStackBase = Phi; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1466 | // Insert poison calls for lifetime intrinsics for alloca. |
| 1467 | bool HavePoisonedAllocas = false; |
| 1468 | for (size_t i = 0, n = AllocaPoisonCallVec.size(); i < n; i++) { |
| 1469 | const AllocaPoisonCall &APC = AllocaPoisonCallVec[i]; |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 1470 | assert(APC.InsBefore); |
| 1471 | assert(APC.AI); |
| 1472 | IRBuilder<> IRB(APC.InsBefore); |
| 1473 | poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison); |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1474 | HavePoisonedAllocas |= APC.DoPoison; |
| 1475 | } |
| 1476 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1477 | // Replace Alloca instructions with base+offset. |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1478 | for (size_t i = 0, n = SVD.size(); i < n; i++) { |
| 1479 | AllocaInst *AI = SVD[i].AI; |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1480 | Value *NewAllocaPtr = IRB.CreateIntToPtr( |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1481 | IRB.CreateAdd(LocalStackBase, |
| 1482 | ConstantInt::get(IntptrTy, SVD[i].Offset)), |
| 1483 | AI->getType()); |
Alexey Samsonov | 3d43b63 | 2012-12-12 14:31:53 +0000 | [diff] [blame] | 1484 | replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB); |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1485 | AI->replaceAllUsesWith(NewAllocaPtr); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1486 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1487 | |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 1488 | // The left-most redzone has enough space for at least 4 pointers. |
| 1489 | // Write the Magic value to redzone[0]. |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1490 | Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy); |
| 1491 | IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic), |
| 1492 | BasePlus0); |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 1493 | // Write the frame description constant to redzone[1]. |
| 1494 | Value *BasePlus1 = IRB.CreateIntToPtr( |
| 1495 | IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, ASan.LongSize/8)), |
| 1496 | IntptrPtrTy); |
Alexey Samsonov | 9bdb63a | 2012-11-02 12:20:34 +0000 | [diff] [blame] | 1497 | GlobalVariable *StackDescriptionGlobal = |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 1498 | createPrivateGlobalForString(*F.getParent(), L.DescriptionString, |
| 1499 | /*AllowMerging*/true); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1500 | Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, |
| 1501 | IntptrTy); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1502 | IRB.CreateStore(Description, BasePlus1); |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 1503 | // Write the PC to redzone[2]. |
| 1504 | Value *BasePlus2 = IRB.CreateIntToPtr( |
| 1505 | IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, |
| 1506 | 2 * ASan.LongSize/8)), |
| 1507 | IntptrPtrTy); |
| 1508 | IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1509 | |
| 1510 | // Poison the stack redzones at the entry. |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1511 | Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1512 | poisonRedZones(L.ShadowBytes, IRB, ShadowBase, true); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1513 | |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1514 | // (Un)poison the stack before all ret instructions. |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1515 | for (size_t i = 0, n = RetVec.size(); i < n; i++) { |
| 1516 | Instruction *Ret = RetVec[i]; |
| 1517 | IRBuilder<> IRBRet(Ret); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1518 | // Mark the current frame as retired. |
| 1519 | IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic), |
| 1520 | BasePlus0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1521 | if (DoStackMalloc) { |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 1522 | assert(StackMallocIdx >= 0); |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1523 | // if LocalStackBase != OrigStackBase: |
| 1524 | // // In use-after-return mode, poison the whole stack frame. |
| 1525 | // if StackMallocIdx <= 4 |
| 1526 | // // For small sizes inline the whole thing: |
| 1527 | // memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize); |
| 1528 | // **SavedFlagPtr(LocalStackBase) = 0 |
| 1529 | // else |
| 1530 | // __asan_stack_free_N(LocalStackBase, OrigStackBase) |
| 1531 | // else |
| 1532 | // <This is not a fake stack; unpoison the redzones> |
| 1533 | Value *Cmp = IRBRet.CreateICmpNE(LocalStackBase, OrigStackBase); |
| 1534 | TerminatorInst *ThenTerm, *ElseTerm; |
| 1535 | SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm); |
| 1536 | |
| 1537 | IRBuilder<> IRBPoison(ThenTerm); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1538 | if (StackMallocIdx <= 4) { |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1539 | int ClassSize = kMinStackMallocSize << StackMallocIdx; |
| 1540 | SetShadowToStackAfterReturnInlined(IRBPoison, ShadowBase, |
| 1541 | ClassSize >> Mapping.Scale); |
| 1542 | Value *SavedFlagPtrPtr = IRBPoison.CreateAdd( |
| 1543 | LocalStackBase, |
| 1544 | ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8)); |
| 1545 | Value *SavedFlagPtr = IRBPoison.CreateLoad( |
| 1546 | IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy)); |
| 1547 | IRBPoison.CreateStore( |
| 1548 | Constant::getNullValue(IRBPoison.getInt8Ty()), |
| 1549 | IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy())); |
| 1550 | } else { |
| 1551 | // For larger frames call __asan_stack_free_*. |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1552 | IRBPoison.CreateCall3(AsanStackFreeFunc[StackMallocIdx], LocalStackBase, |
| 1553 | ConstantInt::get(IntptrTy, LocalStackSize), |
| 1554 | OrigStackBase); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1555 | } |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1556 | |
| 1557 | IRBuilder<> IRBElse(ElseTerm); |
| 1558 | poisonRedZones(L.ShadowBytes, IRBElse, ShadowBase, false); |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1559 | } else if (HavePoisonedAllocas) { |
| 1560 | // If we poisoned some allocas in llvm.lifetime analysis, |
| 1561 | // unpoison whole stack frame now. |
| 1562 | assert(LocalStackBase == OrigStackBase); |
| 1563 | poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false); |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 1564 | } else { |
| 1565 | poisonRedZones(L.ShadowBytes, IRBRet, ShadowBase, false); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1566 | } |
| 1567 | } |
| 1568 | |
Kostya Serebryany | 0995994 | 2012-10-19 06:20:53 +0000 | [diff] [blame] | 1569 | // We are done. Remove the old unused alloca instructions. |
| 1570 | for (size_t i = 0, n = AllocaVec.size(); i < n; i++) |
| 1571 | AllocaVec[i]->eraseFromParent(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1572 | } |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1573 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1574 | void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size, |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 1575 | IRBuilder<> &IRB, bool DoPoison) { |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 1576 | // For now just insert the call to ASan runtime. |
| 1577 | Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy); |
| 1578 | Value *SizeArg = ConstantInt::get(IntptrTy, Size); |
| 1579 | IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc |
| 1580 | : AsanUnpoisonStackMemoryFunc, |
| 1581 | AddrArg, SizeArg); |
| 1582 | } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1583 | |
| 1584 | // Handling llvm.lifetime intrinsics for a given %alloca: |
| 1585 | // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca. |
| 1586 | // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect |
| 1587 | // invalid accesses) and unpoison it for llvm.lifetime.start (the memory |
| 1588 | // could be poisoned by previous llvm.lifetime.end instruction, as the |
| 1589 | // variable may go in and out of scope several times, e.g. in loops). |
| 1590 | // (3) if we poisoned at least one %alloca in a function, |
| 1591 | // unpoison the whole stack frame at function exit. |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1592 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1593 | AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) { |
| 1594 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
| 1595 | // We're intested only in allocas we can handle. |
| 1596 | return isInterestingAlloca(*AI) ? AI : 0; |
| 1597 | // See if we've already calculated (or started to calculate) alloca for a |
| 1598 | // given value. |
| 1599 | AllocaForValueMapTy::iterator I = AllocaForValue.find(V); |
| 1600 | if (I != AllocaForValue.end()) |
| 1601 | return I->second; |
| 1602 | // Store 0 while we're calculating alloca for value V to avoid |
| 1603 | // infinite recursion if the value references itself. |
| 1604 | AllocaForValue[V] = 0; |
| 1605 | AllocaInst *Res = 0; |
| 1606 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 1607 | Res = findAllocaForValue(CI->getOperand(0)); |
| 1608 | else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 1609 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1610 | Value *IncValue = PN->getIncomingValue(i); |
| 1611 | // Allow self-referencing phi-nodes. |
| 1612 | if (IncValue == PN) continue; |
| 1613 | AllocaInst *IncValueAI = findAllocaForValue(IncValue); |
| 1614 | // AI for incoming values should exist and should all be equal. |
| 1615 | if (IncValueAI == 0 || (Res != 0 && IncValueAI != Res)) |
| 1616 | return 0; |
| 1617 | Res = IncValueAI; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1618 | } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1619 | } |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1620 | if (Res != 0) |
| 1621 | AllocaForValue[V] = Res; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1622 | return Res; |
| 1623 | } |