Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 1 | //===- AddressSanitizer.cpp - memory error detector -----------------------===// |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 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: |
Hans Wennborg | 08b34a0 | 2017-11-13 23:47:58 +0000 | [diff] [blame] | 12 | // https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/ArrayRef.h" |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseMap.h" |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DepthFirstIterator.h" |
Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallPtrSet.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringRef.h" |
Evgeniy Stepanov | 617232f | 2012-05-23 11:52:12 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Twine.h" |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/MemoryBuiltins.h" |
| 27 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/Local.h" |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 30 | #include "llvm/BinaryFormat/MachO.h" |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Argument.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Attributes.h" |
| 33 | #include "llvm/IR/BasicBlock.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 34 | #include "llvm/IR/CallSite.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 35 | #include "llvm/IR/Comdat.h" |
| 36 | #include "llvm/IR/Constant.h" |
| 37 | #include "llvm/IR/Constants.h" |
Chandler Carruth | 12664a0 | 2014-03-06 00:22:06 +0000 | [diff] [blame] | 38 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 39 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 40 | #include "llvm/IR/DebugInfoMetadata.h" |
| 41 | #include "llvm/IR/DebugLoc.h" |
| 42 | #include "llvm/IR/DerivedTypes.h" |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Function.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 45 | #include "llvm/IR/GlobalAlias.h" |
| 46 | #include "llvm/IR/GlobalValue.h" |
| 47 | #include "llvm/IR/GlobalVariable.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 48 | #include "llvm/IR/IRBuilder.h" |
| 49 | #include "llvm/IR/InlineAsm.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 50 | #include "llvm/IR/InstVisitor.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 51 | #include "llvm/IR/InstrTypes.h" |
| 52 | #include "llvm/IR/Instruction.h" |
| 53 | #include "llvm/IR/Instructions.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 54 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 55 | #include "llvm/IR/Intrinsics.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 56 | #include "llvm/IR/LLVMContext.h" |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 57 | #include "llvm/IR/MDBuilder.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Metadata.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 59 | #include "llvm/IR/Module.h" |
| 60 | #include "llvm/IR/Type.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 61 | #include "llvm/IR/Use.h" |
| 62 | #include "llvm/IR/Value.h" |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 63 | #include "llvm/MC/MCSectionMachO.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 64 | #include "llvm/Pass.h" |
| 65 | #include "llvm/Support/Casting.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 66 | #include "llvm/Support/CommandLine.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 67 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 68 | #include "llvm/Support/ErrorHandling.h" |
| 69 | #include "llvm/Support/MathExtras.h" |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 70 | #include "llvm/Support/ScopedPrinter.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 71 | #include "llvm/Support/raw_ostream.h" |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 72 | #include "llvm/Transforms/Instrumentation.h" |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 73 | #include "llvm/Transforms/Utils/ASanStackFrameLayout.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 74 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 75 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 76 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 77 | #include <algorithm> |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 78 | #include <cassert> |
| 79 | #include <cstddef> |
| 80 | #include <cstdint> |
Vitaly Buka | 3455b9b | 2016-08-20 18:34:39 +0000 | [diff] [blame] | 81 | #include <iomanip> |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 82 | #include <limits> |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 83 | #include <memory> |
Vitaly Buka | 3455b9b | 2016-08-20 18:34:39 +0000 | [diff] [blame] | 84 | #include <sstream> |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 85 | #include <string> |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 86 | #include <tuple> |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 87 | |
| 88 | using namespace llvm; |
| 89 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 90 | #define DEBUG_TYPE "asan" |
| 91 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 92 | static const uint64_t kDefaultShadowScale = 3; |
| 93 | static const uint64_t kDefaultShadowOffset32 = 1ULL << 29; |
| 94 | static const uint64_t kDefaultShadowOffset64 = 1ULL << 44; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 95 | static const uint64_t kDynamicShadowSentinel = |
| 96 | std::numeric_limits<uint64_t>::max(); |
Anna Zaks | 3b50e70 | 2016-02-02 22:05:07 +0000 | [diff] [blame] | 97 | static const uint64_t kIOSShadowOffset32 = 1ULL << 30; |
Anna Zaks | 3b50e70 | 2016-02-02 22:05:07 +0000 | [diff] [blame] | 98 | static const uint64_t kIOSSimShadowOffset32 = 1ULL << 30; |
| 99 | static const uint64_t kIOSSimShadowOffset64 = kDefaultShadowOffset64; |
Walter Lee | 8f1545c | 2017-11-16 17:03:00 +0000 | [diff] [blame] | 100 | static const uint64_t kSmallX86_64ShadowOffsetBase = 0x7FFFFFFF; // < 2G. |
| 101 | static const uint64_t kSmallX86_64ShadowOffsetAlignMask = ~0xFFFULL; |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 102 | static const uint64_t kLinuxKasan_ShadowOffset64 = 0xdffffc0000000000; |
Bill Seurer | 957a076 | 2017-12-07 22:53:33 +0000 | [diff] [blame] | 103 | static const uint64_t kPPC64_ShadowOffset64 = 1ULL << 44; |
Marcin Koscielnicki | 57290f9 | 2016-04-30 09:57:34 +0000 | [diff] [blame] | 104 | static const uint64_t kSystemZ_ShadowOffset64 = 1ULL << 52; |
Kostya Serebryany | c5bd981 | 2014-11-04 19:46:15 +0000 | [diff] [blame] | 105 | static const uint64_t kMIPS32_ShadowOffset32 = 0x0aaa0000; |
Kumar Sukhani | 9559a5c | 2015-01-31 10:43:18 +0000 | [diff] [blame] | 106 | static const uint64_t kMIPS64_ShadowOffset64 = 1ULL << 37; |
Renato Golin | af21372 | 2015-02-03 11:20:45 +0000 | [diff] [blame] | 107 | static const uint64_t kAArch64_ShadowOffset64 = 1ULL << 36; |
Kostya Serebryany | 8baa386 | 2014-02-10 07:37:04 +0000 | [diff] [blame] | 108 | static const uint64_t kFreeBSD_ShadowOffset32 = 1ULL << 30; |
| 109 | static const uint64_t kFreeBSD_ShadowOffset64 = 1ULL << 46; |
Kamil Rytarowski | 02c432a | 2018-05-11 00:58:01 +0000 | [diff] [blame] | 110 | static const uint64_t kNetBSD_ShadowOffset32 = 1ULL << 30; |
Kamil Rytarowski | a9f404f8 | 2017-08-28 22:13:52 +0000 | [diff] [blame] | 111 | static const uint64_t kNetBSD_ShadowOffset64 = 1ULL << 46; |
Filipe Cabecinhas | 33dd486 | 2017-02-23 17:10:28 +0000 | [diff] [blame] | 112 | static const uint64_t kPS4CPU_ShadowOffset64 = 1ULL << 40; |
Timur Iskhodzhanov | b4b6b74 | 2015-01-22 12:24:21 +0000 | [diff] [blame] | 113 | static const uint64_t kWindowsShadowOffset32 = 3ULL << 28; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 114 | |
Walter Lee | cdbb207 | 2018-05-18 04:10:38 +0000 | [diff] [blame] | 115 | static const uint64_t kMyriadShadowScale = 5; |
| 116 | static const uint64_t kMyriadMemoryOffset32 = 0x80000000ULL; |
| 117 | static const uint64_t kMyriadMemorySize32 = 0x20000000ULL; |
| 118 | static const uint64_t kMyriadTagShift = 29; |
| 119 | static const uint64_t kMyriadDDRTag = 4; |
| 120 | static const uint64_t kMyriadCacheBitMask32 = 0x40000000ULL; |
| 121 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 122 | // The shadow memory space is dynamically allocated. |
| 123 | static const uint64_t kWindowsShadowOffset64 = kDynamicShadowSentinel; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 124 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 125 | static const size_t kMinStackMallocSize = 1 << 6; // 64B |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 126 | static const size_t kMaxStackMallocSize = 1 << 16; // 64K |
| 127 | static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; |
| 128 | static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; |
| 129 | |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 130 | static const char *const kAsanModuleCtorName = "asan.module_ctor"; |
| 131 | static const char *const kAsanModuleDtorName = "asan.module_dtor"; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 132 | static const uint64_t kAsanCtorAndDtorPriority = 1; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 133 | static const char *const kAsanReportErrorTemplate = "__asan_report_"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 134 | static const char *const kAsanRegisterGlobalsName = "__asan_register_globals"; |
Alexey Samsonov | f52b717 | 2013-08-05 13:19:49 +0000 | [diff] [blame] | 135 | static const char *const kAsanUnregisterGlobalsName = |
| 136 | "__asan_unregister_globals"; |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 137 | static const char *const kAsanRegisterImageGlobalsName = |
| 138 | "__asan_register_image_globals"; |
| 139 | static const char *const kAsanUnregisterImageGlobalsName = |
| 140 | "__asan_unregister_image_globals"; |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 141 | static const char *const kAsanRegisterElfGlobalsName = |
| 142 | "__asan_register_elf_globals"; |
| 143 | static const char *const kAsanUnregisterElfGlobalsName = |
| 144 | "__asan_unregister_elf_globals"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 145 | static const char *const kAsanPoisonGlobalsName = "__asan_before_dynamic_init"; |
| 146 | static const char *const kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init"; |
Kuba Brecka | 45dbffd | 2015-07-23 10:54:06 +0000 | [diff] [blame] | 147 | static const char *const kAsanInitName = "__asan_init"; |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 148 | static const char *const kAsanVersionCheckNamePrefix = |
| 149 | "__asan_version_mismatch_check_v"; |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 150 | static const char *const kAsanPtrCmp = "__sanitizer_ptr_cmp"; |
| 151 | static const char *const kAsanPtrSub = "__sanitizer_ptr_sub"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 152 | static const char *const kAsanHandleNoReturnName = "__asan_handle_no_return"; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 153 | static const int kMaxAsanStackMallocSizeClass = 10; |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 154 | static const char *const kAsanStackMallocNameTemplate = "__asan_stack_malloc_"; |
| 155 | static const char *const kAsanStackFreeNameTemplate = "__asan_stack_free_"; |
Peter Collingbourne | 4a653fa | 2018-07-18 22:23:14 +0000 | [diff] [blame] | 156 | static const char *const kAsanGenPrefix = "___asan_gen_"; |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 157 | static const char *const kODRGenPrefix = "__odr_asan_gen_"; |
Kostya Serebryany | cb45b12 | 2014-11-19 00:22:58 +0000 | [diff] [blame] | 158 | static const char *const kSanCovGenPrefix = "__sancov_gen_"; |
Vitaly Buka | 3455b9b | 2016-08-20 18:34:39 +0000 | [diff] [blame] | 159 | static const char *const kAsanSetShadowPrefix = "__asan_set_shadow_"; |
Craig Topper | d3a34f8 | 2013-07-16 01:17:10 +0000 | [diff] [blame] | 160 | static const char *const kAsanPoisonStackMemoryName = |
| 161 | "__asan_poison_stack_memory"; |
| 162 | static const char *const kAsanUnpoisonStackMemoryName = |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 163 | "__asan_unpoison_stack_memory"; |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 164 | |
| 165 | // ASan version script has __asan_* wildcard. Triple underscore prevents a |
| 166 | // linker (gold) warning about attempting to export a local symbol. |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 167 | static const char *const kAsanGlobalsRegisteredFlagName = |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 168 | "___asan_globals_registered"; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 169 | |
Vitaly Buka | 7b8ed4f | 2016-06-02 00:06:42 +0000 | [diff] [blame] | 170 | static const char *const kAsanOptionDetectUseAfterReturn = |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 171 | "__asan_option_detect_stack_use_after_return"; |
| 172 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 173 | static const char *const kAsanShadowMemoryDynamicAddress = |
| 174 | "__asan_shadow_memory_dynamic_address"; |
| 175 | |
Alexander Potapenko | f90556e | 2015-06-12 11:27:06 +0000 | [diff] [blame] | 176 | static const char *const kAsanAllocaPoison = "__asan_alloca_poison"; |
| 177 | static const char *const kAsanAllocasUnpoison = "__asan_allocas_unpoison"; |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 178 | |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 179 | // Accesses sizes are powers of two: 1, 2, 4, 8, 16. |
| 180 | static const size_t kNumberOfAccessSizes = 5; |
| 181 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 182 | static const unsigned kAllocaRzSize = 32; |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 183 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 184 | // Command-line flags. |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 185 | |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 186 | static cl::opt<bool> ClEnableKasan( |
| 187 | "asan-kernel", cl::desc("Enable KernelAddressSanitizer instrumentation"), |
| 188 | cl::Hidden, cl::init(false)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 189 | |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 190 | static cl::opt<bool> ClRecover( |
| 191 | "asan-recover", |
| 192 | cl::desc("Enable recovery mode (continue-after-error)."), |
| 193 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 194 | |
| 195 | // This flag may need to be replaced with -f[no-]asan-reads. |
| 196 | static cl::opt<bool> ClInstrumentReads("asan-instrument-reads", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 197 | cl::desc("instrument read instructions"), |
| 198 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 199 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 200 | static cl::opt<bool> ClInstrumentWrites( |
| 201 | "asan-instrument-writes", cl::desc("instrument write instructions"), |
| 202 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 203 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 204 | static cl::opt<bool> ClInstrumentAtomics( |
| 205 | "asan-instrument-atomics", |
| 206 | cl::desc("instrument atomic instructions (rmw, cmpxchg)"), cl::Hidden, |
| 207 | cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 208 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 209 | static cl::opt<bool> ClAlwaysSlowPath( |
| 210 | "asan-always-slow-path", |
| 211 | cl::desc("use instrumentation with slow path for all accesses"), cl::Hidden, |
| 212 | cl::init(false)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 213 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 214 | static cl::opt<bool> ClForceDynamicShadow( |
| 215 | "asan-force-dynamic-shadow", |
| 216 | cl::desc("Load shadow address into a local variable for each function"), |
| 217 | cl::Hidden, cl::init(false)); |
| 218 | |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 219 | static cl::opt<bool> |
| 220 | ClWithIfunc("asan-with-ifunc", |
| 221 | cl::desc("Access dynamic shadow through an ifunc global on " |
| 222 | "platforms that support this"), |
| 223 | cl::Hidden, cl::init(true)); |
| 224 | |
| 225 | static cl::opt<bool> ClWithIfuncSuppressRemat( |
| 226 | "asan-with-ifunc-suppress-remat", |
| 227 | cl::desc("Suppress rematerialization of dynamic shadow address by passing " |
| 228 | "it through inline asm in prologue."), |
| 229 | cl::Hidden, cl::init(true)); |
| 230 | |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 231 | // This flag limits the number of instructions to be instrumented |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 232 | // in any given BB. Normally, this should be set to unlimited (INT_MAX), |
| 233 | // but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary |
| 234 | // set it to 10000. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 235 | static cl::opt<int> ClMaxInsnsToInstrumentPerBB( |
| 236 | "asan-max-ins-per-bb", cl::init(10000), |
| 237 | cl::desc("maximal number of instructions to instrument in any given BB"), |
| 238 | cl::Hidden); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 239 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 240 | // This flag may need to be replaced with -f[no]asan-stack. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 241 | static cl::opt<bool> ClStack("asan-stack", cl::desc("Handle stack memory"), |
| 242 | cl::Hidden, cl::init(true)); |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 243 | static cl::opt<uint32_t> ClMaxInlinePoisoningSize( |
| 244 | "asan-max-inline-poisoning-size", |
| 245 | cl::desc( |
| 246 | "Inline shadow poisoning for blocks up to the given size in bytes."), |
| 247 | cl::Hidden, cl::init(64)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 248 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 249 | static cl::opt<bool> ClUseAfterReturn("asan-use-after-return", |
Mike Aizatsky | 243b71f | 2016-04-21 22:00:13 +0000 | [diff] [blame] | 250 | cl::desc("Check stack-use-after-return"), |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 251 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 252 | |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 253 | static cl::opt<bool> ClRedzoneByvalArgs("asan-redzone-byval-args", |
| 254 | cl::desc("Create redzones for byval " |
| 255 | "arguments (extra copy " |
| 256 | "required)"), cl::Hidden, |
| 257 | cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 258 | |
Kostya Serebryany | a83bfea | 2016-04-20 20:02:58 +0000 | [diff] [blame] | 259 | static cl::opt<bool> ClUseAfterScope("asan-use-after-scope", |
| 260 | cl::desc("Check stack-use-after-scope"), |
| 261 | cl::Hidden, cl::init(false)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 262 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 263 | // This flag may need to be replaced with -f[no]asan-globals. |
| 264 | static cl::opt<bool> ClGlobals("asan-globals", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 265 | cl::desc("Handle global objects"), cl::Hidden, |
| 266 | cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 267 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 268 | static cl::opt<bool> ClInitializers("asan-initialization-order", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 269 | cl::desc("Handle C++ initializer order"), |
| 270 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 271 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 272 | static cl::opt<bool> ClInvalidPointerPairs( |
| 273 | "asan-detect-invalid-pointer-pair", |
| 274 | cl::desc("Instrument <, <=, >, >=, - with pointer operands"), cl::Hidden, |
| 275 | cl::init(false)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 276 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 277 | static cl::opt<unsigned> ClRealignStack( |
| 278 | "asan-realign-stack", |
| 279 | cl::desc("Realign stack to the value of this flag (power of two)"), |
| 280 | cl::Hidden, cl::init(32)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 281 | |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 282 | static cl::opt<int> ClInstrumentationWithCallsThreshold( |
| 283 | "asan-instrumentation-with-call-threshold", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 284 | cl::desc( |
| 285 | "If the function being instrumented contains more than " |
| 286 | "this number of memory accesses, use callbacks instead of " |
| 287 | "inline checks (-1 means never use callbacks)."), |
| 288 | cl::Hidden, cl::init(7000)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 289 | |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 290 | static cl::opt<std::string> ClMemoryAccessCallbackPrefix( |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 291 | "asan-memory-access-callback-prefix", |
| 292 | cl::desc("Prefix for memory access callbacks"), cl::Hidden, |
| 293 | cl::init("__asan_")); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 294 | |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 295 | static cl::opt<bool> |
| 296 | ClInstrumentDynamicAllocas("asan-instrument-dynamic-allocas", |
| 297 | cl::desc("instrument dynamic allocas"), |
| 298 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 299 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 300 | static cl::opt<bool> ClSkipPromotableAllocas( |
| 301 | "asan-skip-promotable-allocas", |
| 302 | cl::desc("Do not instrument promotable allocas"), cl::Hidden, |
| 303 | cl::init(true)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 304 | |
| 305 | // These flags allow to change the shadow mapping. |
| 306 | // The shadow mapping looks like |
Ryan Govostes | 3f37df0 | 2016-05-06 10:25:22 +0000 | [diff] [blame] | 307 | // Shadow = (Mem >> scale) + offset |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 308 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 309 | static cl::opt<int> ClMappingScale("asan-mapping-scale", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 310 | cl::desc("scale of asan shadow mapping"), |
| 311 | cl::Hidden, cl::init(0)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 312 | |
Ryan Govostes | 6194ae6 | 2016-05-06 11:22:11 +0000 | [diff] [blame] | 313 | static cl::opt<unsigned long long> ClMappingOffset( |
| 314 | "asan-mapping-offset", |
| 315 | cl::desc("offset of asan shadow mapping [EXPERIMENTAL]"), cl::Hidden, |
| 316 | cl::init(0)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 317 | |
| 318 | // Optimization flags. Not user visible, used mostly for testing |
| 319 | // and benchmarking the tool. |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 320 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 321 | static cl::opt<bool> ClOpt("asan-opt", cl::desc("Optimize instrumentation"), |
| 322 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 323 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 324 | static cl::opt<bool> ClOptSameTemp( |
| 325 | "asan-opt-same-temp", cl::desc("Instrument the same temp just once"), |
| 326 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 327 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 328 | static cl::opt<bool> ClOptGlobals("asan-opt-globals", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 329 | cl::desc("Don't instrument scalar globals"), |
| 330 | cl::Hidden, cl::init(true)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 331 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 332 | static cl::opt<bool> ClOptStack( |
| 333 | "asan-opt-stack", cl::desc("Don't instrument scalar stack variables"), |
| 334 | cl::Hidden, cl::init(false)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 335 | |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 336 | static cl::opt<bool> ClDynamicAllocaStack( |
| 337 | "asan-stack-dynamic-alloca", |
| 338 | cl::desc("Use dynamic alloca to represent stack variables"), cl::Hidden, |
Alexey Samsonov | 19763c4 | 2015-02-05 19:39:20 +0000 | [diff] [blame] | 339 | cl::init(true)); |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 340 | |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 341 | static cl::opt<uint32_t> ClForceExperiment( |
| 342 | "asan-force-experiment", |
| 343 | cl::desc("Force optimization experiment (for testing)"), cl::Hidden, |
| 344 | cl::init(0)); |
| 345 | |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 346 | static cl::opt<bool> |
| 347 | ClUsePrivateAliasForGlobals("asan-use-private-alias", |
| 348 | cl::desc("Use private aliases for global" |
| 349 | " variables"), |
| 350 | cl::Hidden, cl::init(false)); |
| 351 | |
Ryan Govostes | e51401b | 2016-07-05 21:53:08 +0000 | [diff] [blame] | 352 | static cl::opt<bool> |
Evgeniy Stepanov | 9e53608 | 2017-04-24 19:34:13 +0000 | [diff] [blame] | 353 | ClUseGlobalsGC("asan-globals-live-support", |
| 354 | cl::desc("Use linker features to support dead " |
| 355 | "code stripping of globals"), |
| 356 | cl::Hidden, cl::init(true)); |
Ryan Govostes | e51401b | 2016-07-05 21:53:08 +0000 | [diff] [blame] | 357 | |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 358 | // This is on by default even though there is a bug in gold: |
| 359 | // https://sourceware.org/bugzilla/show_bug.cgi?id=19002 |
| 360 | static cl::opt<bool> |
| 361 | ClWithComdat("asan-with-comdat", |
| 362 | cl::desc("Place ASan constructors in comdat sections"), |
| 363 | cl::Hidden, cl::init(true)); |
| 364 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 365 | // Debug flags. |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 366 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 367 | static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden, |
| 368 | cl::init(0)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 369 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 370 | static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"), |
| 371 | cl::Hidden, cl::init(0)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 372 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 373 | static cl::opt<std::string> ClDebugFunc("asan-debug-func", cl::Hidden, |
| 374 | cl::desc("Debug func")); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 375 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 376 | static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"), |
| 377 | cl::Hidden, cl::init(-1)); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 378 | |
Etienne Bergeron | 7f0e315 | 2016-09-22 14:57:24 +0000 | [diff] [blame] | 379 | static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug max inst"), |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 380 | cl::Hidden, cl::init(-1)); |
| 381 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 382 | STATISTIC(NumInstrumentedReads, "Number of instrumented reads"); |
| 383 | STATISTIC(NumInstrumentedWrites, "Number of instrumented writes"); |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 384 | STATISTIC(NumOptimizedAccessesToGlobalVar, |
| 385 | "Number of optimized accesses to global vars"); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 386 | STATISTIC(NumOptimizedAccessesToStackVar, |
| 387 | "Number of optimized accesses to stack vars"); |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 388 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 389 | namespace { |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 390 | |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 391 | /// Frontend-provided metadata for source location. |
| 392 | struct LocationMetadata { |
| 393 | StringRef Filename; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 394 | int LineNo = 0; |
| 395 | int ColumnNo = 0; |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 396 | |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 397 | LocationMetadata() = default; |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 398 | |
| 399 | bool empty() const { return Filename.empty(); } |
| 400 | |
| 401 | void parse(MDNode *MDN) { |
| 402 | assert(MDN->getNumOperands() == 3); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 403 | MDString *DIFilename = cast<MDString>(MDN->getOperand(0)); |
| 404 | Filename = DIFilename->getString(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 405 | LineNo = |
| 406 | mdconst::extract<ConstantInt>(MDN->getOperand(1))->getLimitedValue(); |
| 407 | ColumnNo = |
| 408 | mdconst::extract<ConstantInt>(MDN->getOperand(2))->getLimitedValue(); |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 409 | } |
| 410 | }; |
| 411 | |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 412 | /// Frontend-provided metadata for global variables. |
| 413 | class GlobalsMetadata { |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 414 | public: |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 415 | struct Entry { |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 416 | LocationMetadata SourceLoc; |
| 417 | StringRef Name; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 418 | bool IsDynInit = false; |
| 419 | bool IsBlacklisted = false; |
| 420 | |
| 421 | Entry() = default; |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 422 | }; |
| 423 | |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 424 | GlobalsMetadata() = default; |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 425 | |
Keno Fischer | e03fae4 | 2015-12-05 14:42:34 +0000 | [diff] [blame] | 426 | void reset() { |
| 427 | inited_ = false; |
| 428 | Entries.clear(); |
| 429 | } |
| 430 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 431 | void init(Module &M) { |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 432 | assert(!inited_); |
| 433 | inited_ = true; |
| 434 | NamedMDNode *Globals = M.getNamedMetadata("llvm.asan.globals"); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 435 | if (!Globals) return; |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 436 | for (auto MDN : Globals->operands()) { |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 437 | // Metadata node contains the global and the fields of "Entry". |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 438 | assert(MDN->getNumOperands() == 5); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 439 | auto *GV = mdconst::extract_or_null<GlobalVariable>(MDN->getOperand(0)); |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 440 | // The optimizer may optimize away a global entirely. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 441 | if (!GV) continue; |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 442 | // We can already have an entry for GV if it was merged with another |
| 443 | // global. |
| 444 | Entry &E = Entries[GV]; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 445 | if (auto *Loc = cast_or_null<MDNode>(MDN->getOperand(1))) |
| 446 | E.SourceLoc.parse(Loc); |
| 447 | if (auto *Name = cast_or_null<MDString>(MDN->getOperand(2))) |
| 448 | E.Name = Name->getString(); |
| 449 | ConstantInt *IsDynInit = |
| 450 | mdconst::extract<ConstantInt>(MDN->getOperand(3)); |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 451 | E.IsDynInit |= IsDynInit->isOne(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 452 | ConstantInt *IsBlacklisted = |
| 453 | mdconst::extract<ConstantInt>(MDN->getOperand(4)); |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 454 | E.IsBlacklisted |= IsBlacklisted->isOne(); |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 457 | |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 458 | /// Returns metadata entry for a given global. |
| 459 | Entry get(GlobalVariable *G) const { |
| 460 | auto Pos = Entries.find(G); |
| 461 | return (Pos != Entries.end()) ? Pos->second : Entry(); |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 464 | private: |
| 465 | bool inited_ = false; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 466 | DenseMap<GlobalVariable *, Entry> Entries; |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 467 | }; |
| 468 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 469 | /// This struct defines the shadow mapping using the rule: |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 470 | /// shadow = (mem >> Scale) ADD-or-OR Offset. |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 471 | /// If InGlobal is true, then |
| 472 | /// extern char __asan_shadow[]; |
| 473 | /// shadow = (mem >> Scale) + &__asan_shadow |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 474 | struct ShadowMapping { |
| 475 | int Scale; |
| 476 | uint64_t Offset; |
Kostya Serebryany | 4766fe6 | 2013-01-23 12:54:55 +0000 | [diff] [blame] | 477 | bool OrShadowOffset; |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 478 | bool InGlobal; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 479 | }; |
| 480 | |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 481 | } // end anonymous namespace |
| 482 | |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 483 | static ShadowMapping getShadowMapping(Triple &TargetTriple, int LongSize, |
| 484 | bool IsKasan) { |
Evgeniy Stepanov | 5fe279e | 2015-10-08 21:21:24 +0000 | [diff] [blame] | 485 | bool IsAndroid = TargetTriple.isAndroid(); |
Anna Zaks | 3b50e70 | 2016-02-02 22:05:07 +0000 | [diff] [blame] | 486 | bool IsIOS = TargetTriple.isiOS() || TargetTriple.isWatchOS(); |
Simon Pilgrim | a279410 | 2014-11-22 19:12:10 +0000 | [diff] [blame] | 487 | bool IsFreeBSD = TargetTriple.isOSFreeBSD(); |
Kamil Rytarowski | a9f404f8 | 2017-08-28 22:13:52 +0000 | [diff] [blame] | 488 | bool IsNetBSD = TargetTriple.isOSNetBSD(); |
Filipe Cabecinhas | 33dd486 | 2017-02-23 17:10:28 +0000 | [diff] [blame] | 489 | bool IsPS4CPU = TargetTriple.isPS4CPU(); |
Simon Pilgrim | a279410 | 2014-11-22 19:12:10 +0000 | [diff] [blame] | 490 | bool IsLinux = TargetTriple.isOSLinux(); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 491 | bool IsPPC64 = TargetTriple.getArch() == Triple::ppc64 || |
| 492 | TargetTriple.getArch() == Triple::ppc64le; |
| 493 | bool IsSystemZ = TargetTriple.getArch() == Triple::systemz; |
| 494 | bool IsX86 = TargetTriple.getArch() == Triple::x86; |
| 495 | bool IsX86_64 = TargetTriple.getArch() == Triple::x86_64; |
Alexander Richardson | 85e200e | 2018-06-25 16:49:20 +0000 | [diff] [blame] | 496 | bool IsMIPS32 = TargetTriple.isMIPS32(); |
| 497 | bool IsMIPS64 = TargetTriple.isMIPS64(); |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 498 | bool IsArmOrThumb = TargetTriple.isARM() || TargetTriple.isThumb(); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 499 | bool IsAArch64 = TargetTriple.getArch() == Triple::aarch64; |
Timur Iskhodzhanov | 00ede84 | 2015-01-12 17:38:58 +0000 | [diff] [blame] | 500 | bool IsWindows = TargetTriple.isOSWindows(); |
Petr Hosek | 6f16857 | 2017-02-27 22:49:37 +0000 | [diff] [blame] | 501 | bool IsFuchsia = TargetTriple.isOSFuchsia(); |
Walter Lee | cdbb207 | 2018-05-18 04:10:38 +0000 | [diff] [blame] | 502 | bool IsMyriad = TargetTriple.getVendor() == llvm::Triple::Myriad; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 503 | |
| 504 | ShadowMapping Mapping; |
| 505 | |
Walter Lee | cdbb207 | 2018-05-18 04:10:38 +0000 | [diff] [blame] | 506 | Mapping.Scale = IsMyriad ? kMyriadShadowScale : kDefaultShadowScale; |
Walter Lee | 8f1545c | 2017-11-16 17:03:00 +0000 | [diff] [blame] | 507 | if (ClMappingScale.getNumOccurrences() > 0) { |
| 508 | Mapping.Scale = ClMappingScale; |
| 509 | } |
| 510 | |
Evgeniy Stepanov | 4d81f86 | 2015-07-29 18:22:25 +0000 | [diff] [blame] | 511 | if (LongSize == 32) { |
Evgeniy Stepanov | 4d81f86 | 2015-07-29 18:22:25 +0000 | [diff] [blame] | 512 | if (IsAndroid) |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 513 | Mapping.Offset = kDynamicShadowSentinel; |
Evgeniy Stepanov | 4d81f86 | 2015-07-29 18:22:25 +0000 | [diff] [blame] | 514 | else if (IsMIPS32) |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 515 | Mapping.Offset = kMIPS32_ShadowOffset32; |
| 516 | else if (IsFreeBSD) |
| 517 | Mapping.Offset = kFreeBSD_ShadowOffset32; |
Kamil Rytarowski | 02c432a | 2018-05-11 00:58:01 +0000 | [diff] [blame] | 518 | else if (IsNetBSD) |
| 519 | Mapping.Offset = kNetBSD_ShadowOffset32; |
Alexander Potapenko | a51e483 | 2014-04-23 17:14:45 +0000 | [diff] [blame] | 520 | else if (IsIOS) |
Anna Zaks | 3b50e70 | 2016-02-02 22:05:07 +0000 | [diff] [blame] | 521 | // If we're targeting iOS and x86, the binary is built for iOS simulator. |
| 522 | Mapping.Offset = IsX86 ? kIOSSimShadowOffset32 : kIOSShadowOffset32; |
Timur Iskhodzhanov | 00ede84 | 2015-01-12 17:38:58 +0000 | [diff] [blame] | 523 | else if (IsWindows) |
| 524 | Mapping.Offset = kWindowsShadowOffset32; |
Walter Lee | cdbb207 | 2018-05-18 04:10:38 +0000 | [diff] [blame] | 525 | else if (IsMyriad) { |
| 526 | uint64_t ShadowOffset = (kMyriadMemoryOffset32 + kMyriadMemorySize32 - |
| 527 | (kMyriadMemorySize32 >> Mapping.Scale)); |
| 528 | Mapping.Offset = ShadowOffset - (kMyriadMemoryOffset32 >> Mapping.Scale); |
| 529 | } |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 530 | else |
| 531 | Mapping.Offset = kDefaultShadowOffset32; |
| 532 | } else { // LongSize == 64 |
Petr Hosek | 6f16857 | 2017-02-27 22:49:37 +0000 | [diff] [blame] | 533 | // Fuchsia is always PIE, which means that the beginning of the address |
| 534 | // space is always available. |
| 535 | if (IsFuchsia) |
| 536 | Mapping.Offset = 0; |
| 537 | else if (IsPPC64) |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 538 | Mapping.Offset = kPPC64_ShadowOffset64; |
Marcin Koscielnicki | 57290f9 | 2016-04-30 09:57:34 +0000 | [diff] [blame] | 539 | else if (IsSystemZ) |
| 540 | Mapping.Offset = kSystemZ_ShadowOffset64; |
John Baldwin | c5d7e04 | 2018-08-01 22:51:13 +0000 | [diff] [blame] | 541 | else if (IsFreeBSD && !IsMIPS64) |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 542 | Mapping.Offset = kFreeBSD_ShadowOffset64; |
Kamil Rytarowski | a9f404f8 | 2017-08-28 22:13:52 +0000 | [diff] [blame] | 543 | else if (IsNetBSD) |
| 544 | Mapping.Offset = kNetBSD_ShadowOffset64; |
Filipe Cabecinhas | 33dd486 | 2017-02-23 17:10:28 +0000 | [diff] [blame] | 545 | else if (IsPS4CPU) |
| 546 | Mapping.Offset = kPS4CPU_ShadowOffset64; |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 547 | else if (IsLinux && IsX86_64) { |
| 548 | if (IsKasan) |
| 549 | Mapping.Offset = kLinuxKasan_ShadowOffset64; |
| 550 | else |
Walter Lee | 8f1545c | 2017-11-16 17:03:00 +0000 | [diff] [blame] | 551 | Mapping.Offset = (kSmallX86_64ShadowOffsetBase & |
| 552 | (kSmallX86_64ShadowOffsetAlignMask << Mapping.Scale)); |
Etienne Bergeron | 70684f9 | 2016-06-21 15:07:29 +0000 | [diff] [blame] | 553 | } else if (IsWindows && IsX86_64) { |
| 554 | Mapping.Offset = kWindowsShadowOffset64; |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 555 | } else if (IsMIPS64) |
Kostya Serebryany | 231bd08 | 2014-11-11 23:02:57 +0000 | [diff] [blame] | 556 | Mapping.Offset = kMIPS64_ShadowOffset64; |
Anna Zaks | 3b50e70 | 2016-02-02 22:05:07 +0000 | [diff] [blame] | 557 | else if (IsIOS) |
| 558 | // If we're targeting iOS and x86, the binary is built for iOS simulator. |
Anna Zaks | 9a6a6ef | 2016-10-05 20:34:13 +0000 | [diff] [blame] | 559 | // We are using dynamic shadow offset on the 64-bit devices. |
| 560 | Mapping.Offset = |
| 561 | IsX86_64 ? kIOSSimShadowOffset64 : kDynamicShadowSentinel; |
Renato Golin | af21372 | 2015-02-03 11:20:45 +0000 | [diff] [blame] | 562 | else if (IsAArch64) |
| 563 | Mapping.Offset = kAArch64_ShadowOffset64; |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 564 | else |
| 565 | Mapping.Offset = kDefaultShadowOffset64; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 568 | if (ClForceDynamicShadow) { |
| 569 | Mapping.Offset = kDynamicShadowSentinel; |
| 570 | } |
| 571 | |
Ryan Govostes | 3f37df0 | 2016-05-06 10:25:22 +0000 | [diff] [blame] | 572 | if (ClMappingOffset.getNumOccurrences() > 0) { |
| 573 | Mapping.Offset = ClMappingOffset; |
| 574 | } |
| 575 | |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 576 | // OR-ing shadow offset if more efficient (at least on x86) if the offset |
| 577 | // is a power of two, but on ppc64 we have to use add since the shadow |
Marcin Koscielnicki | 57290f9 | 2016-04-30 09:57:34 +0000 | [diff] [blame] | 578 | // offset is not necessary 1/8-th of the address space. On SystemZ, |
| 579 | // we could OR the constant in a single instruction, but it's more |
| 580 | // efficient to load it once and use indexed addressing. |
Filipe Cabecinhas | 33dd486 | 2017-02-23 17:10:28 +0000 | [diff] [blame] | 581 | Mapping.OrShadowOffset = !IsAArch64 && !IsPPC64 && !IsSystemZ && !IsPS4CPU && |
| 582 | !(Mapping.Offset & (Mapping.Offset - 1)) && |
| 583 | Mapping.Offset != kDynamicShadowSentinel; |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 584 | bool IsAndroidWithIfuncSupport = |
| 585 | IsAndroid && !TargetTriple.isAndroidVersionLT(21); |
| 586 | Mapping.InGlobal = ClWithIfunc && IsAndroidWithIfuncSupport && IsArmOrThumb; |
Kostya Serebryany | cc92c79 | 2014-02-24 13:40:24 +0000 | [diff] [blame] | 587 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 588 | return Mapping; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 591 | static size_t RedzoneSizeForScale(int MappingScale) { |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 592 | // Redzone used for stack and globals is at least 32 bytes. |
| 593 | // 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] | 594 | return std::max(32U, 1U << MappingScale); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 595 | } |
Kostya Serebryany | b3bd605 | 2012-11-20 13:00:01 +0000 | [diff] [blame] | 596 | |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 597 | namespace { |
| 598 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 599 | /// AddressSanitizer: instrument the code in module to find memory bugs. |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 600 | struct AddressSanitizer : public FunctionPass { |
| 601 | // Pass identification, replacement for typeid |
| 602 | static char ID; |
| 603 | |
| 604 | explicit AddressSanitizer(bool CompileKernel = false, bool Recover = false, |
Vitaly Buka | 1e75fa4 | 2016-05-27 22:55:10 +0000 | [diff] [blame] | 605 | bool UseAfterScope = false) |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 606 | : FunctionPass(ID), UseAfterScope(UseAfterScope || ClUseAfterScope) { |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame] | 607 | this->Recover = ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover; |
| 608 | this->CompileKernel = ClEnableKasan.getNumOccurrences() > 0 ? |
| 609 | ClEnableKasan : CompileKernel; |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 610 | initializeAddressSanitizerPass(*PassRegistry::getPassRegistry()); |
| 611 | } |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 612 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 613 | StringRef getPassName() const override { |
| 614 | return "AddressSanitizerFunctionPass"; |
| 615 | } |
| 616 | |
| 617 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 618 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 619 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 620 | } |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 621 | |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 622 | uint64_t getAllocaSizeInBytes(const AllocaInst &AI) const { |
Kuba Brecka | 7d03ce4 | 2016-06-27 15:57:08 +0000 | [diff] [blame] | 623 | uint64_t ArraySize = 1; |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 624 | if (AI.isArrayAllocation()) { |
| 625 | const ConstantInt *CI = dyn_cast<ConstantInt>(AI.getArraySize()); |
Kuba Brecka | 7d03ce4 | 2016-06-27 15:57:08 +0000 | [diff] [blame] | 626 | assert(CI && "non-constant array size"); |
| 627 | ArraySize = CI->getZExtValue(); |
| 628 | } |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 629 | Type *Ty = AI.getAllocatedType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 630 | uint64_t SizeInBytes = |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 631 | AI.getModule()->getDataLayout().getTypeAllocSize(Ty); |
Kuba Brecka | 7d03ce4 | 2016-06-27 15:57:08 +0000 | [diff] [blame] | 632 | return SizeInBytes * ArraySize; |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 633 | } |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 634 | |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 635 | /// Check if we want (and can) handle this alloca. |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 636 | bool isInterestingAlloca(const AllocaInst &AI); |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 637 | |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 638 | /// If it is an interesting memory access, return the PointerOperand |
| 639 | /// and set IsWrite/Alignment. Otherwise return nullptr. |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 640 | /// MaybeMask is an output parameter for the mask Value, if we're looking at a |
| 641 | /// masked load/store. |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 642 | Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite, |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 643 | uint64_t *TypeSize, unsigned *Alignment, |
| 644 | Value **MaybeMask = nullptr); |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 645 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 646 | void instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis, Instruction *I, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 647 | bool UseCalls, const DataLayout &DL); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 648 | void instrumentPointerComparisonOrSubtraction(Instruction *I); |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 649 | void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore, |
| 650 | Value *Addr, uint32_t TypeSize, bool IsWrite, |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 651 | Value *SizeArgument, bool UseCalls, uint32_t Exp); |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 652 | void instrumentUnusualSizeOrAlignment(Instruction *I, |
| 653 | Instruction *InsertBefore, Value *Addr, |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 654 | uint32_t TypeSize, bool IsWrite, |
| 655 | Value *SizeArgument, bool UseCalls, |
| 656 | uint32_t Exp); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 657 | Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, |
| 658 | Value *ShadowValue, uint32_t TypeSize); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 659 | Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr, |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 660 | bool IsWrite, size_t AccessSizeIndex, |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 661 | Value *SizeArgument, uint32_t Exp); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 662 | void instrumentMemIntrinsic(MemIntrinsic *MI); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 663 | Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 664 | bool runOnFunction(Function &F) override; |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 665 | bool maybeInsertAsanInitAtFunctionEntry(Function &F); |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 666 | void maybeInsertDynamicShadowAtFunctionEntry(Function &F); |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 667 | void markEscapedLocalAllocas(Function &F); |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 668 | bool doInitialization(Module &M) override; |
| 669 | bool doFinalization(Module &M) override; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 670 | |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 671 | DominatorTree &getDominatorTree() const { return *DT; } |
| 672 | |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 673 | private: |
| 674 | friend struct FunctionStackPoisoner; |
| 675 | |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 676 | void initializeCallbacks(Module &M); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 677 | |
Kostya Serebryany | 1cdc6e9 | 2011-11-18 01:41:06 +0000 | [diff] [blame] | 678 | bool LooksLikeCodeInBug11395(Instruction *I); |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 679 | bool GlobalIsLinkerInitialized(GlobalVariable *G); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 680 | bool isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis, Value *Addr, |
| 681 | uint64_t TypeSize) const; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 682 | |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 683 | /// Helper to cleanup per-function state. |
| 684 | struct FunctionStateRAII { |
| 685 | AddressSanitizer *Pass; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 686 | |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 687 | FunctionStateRAII(AddressSanitizer *Pass) : Pass(Pass) { |
| 688 | assert(Pass->ProcessedAllocas.empty() && |
| 689 | "last pass forgot to clear cache"); |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 690 | assert(!Pass->LocalDynamicShadow); |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 691 | } |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 692 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 693 | ~FunctionStateRAII() { |
| 694 | Pass->LocalDynamicShadow = nullptr; |
| 695 | Pass->ProcessedAllocas.clear(); |
| 696 | } |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 699 | LLVMContext *C; |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 700 | Triple TargetTriple; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 701 | int LongSize; |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 702 | bool CompileKernel; |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 703 | bool Recover; |
Vitaly Buka | 1e75fa4 | 2016-05-27 22:55:10 +0000 | [diff] [blame] | 704 | bool UseAfterScope; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 705 | Type *IntptrTy; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 706 | ShadowMapping Mapping; |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 707 | DominatorTree *DT; |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 708 | Function *AsanHandleNoReturnFunc; |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 709 | Function *AsanPtrCmpFunction, *AsanPtrSubFunction; |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 710 | Constant *AsanShadowGlobal; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 711 | |
| 712 | // These arrays is indexed by AccessIsWrite, Experiment and log2(AccessSize). |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 713 | Function *AsanErrorCallback[2][2][kNumberOfAccessSizes]; |
| 714 | Function *AsanMemoryAccessCallback[2][2][kNumberOfAccessSizes]; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 715 | |
| 716 | // These arrays is indexed by AccessIsWrite and Experiment. |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 717 | Function *AsanErrorCallbackSized[2][2]; |
| 718 | Function *AsanMemoryAccessCallbackSized[2][2]; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 719 | |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 720 | Function *AsanMemmove, *AsanMemcpy, *AsanMemset; |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 721 | InlineAsm *EmptyAsm; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 722 | Value *LocalDynamicShadow = nullptr; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 723 | GlobalsMetadata GlobalsMD; |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 724 | DenseMap<const AllocaInst *, bool> ProcessedAllocas; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 725 | }; |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 726 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 727 | class AddressSanitizerModule : public ModulePass { |
Evgeniy Stepanov | 9e53608 | 2017-04-24 19:34:13 +0000 | [diff] [blame] | 728 | public: |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 729 | // Pass identification, replacement for typeid |
| 730 | static char ID; |
| 731 | |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 732 | explicit AddressSanitizerModule(bool CompileKernel = false, |
Evgeniy Stepanov | 9e53608 | 2017-04-24 19:34:13 +0000 | [diff] [blame] | 733 | bool Recover = false, |
| 734 | bool UseGlobalsGC = true) |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 735 | : ModulePass(ID), |
| 736 | UseGlobalsGC(UseGlobalsGC && ClUseGlobalsGC), |
Evgeniy Stepanov | b56012b | 2017-05-15 20:43:42 +0000 | [diff] [blame] | 737 | // Not a typo: ClWithComdat is almost completely pointless without |
| 738 | // ClUseGlobalsGC (because then it only works on modules without |
| 739 | // globals, which are rare); it is a prerequisite for ClUseGlobalsGC; |
| 740 | // and both suffer from gold PR19002 for which UseGlobalsGC constructor |
| 741 | // argument is designed as workaround. Therefore, disable both |
| 742 | // ClWithComdat and ClUseGlobalsGC unless the frontend says it's ok to |
| 743 | // do globals-gc. |
Andrey Konovalov | 1ba9d9c | 2018-04-13 18:05:21 +0000 | [diff] [blame] | 744 | UseCtorComdat(UseGlobalsGC && ClWithComdat) { |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 745 | this->Recover = ClRecover.getNumOccurrences() > 0 ? |
| 746 | ClRecover : Recover; |
| 747 | this->CompileKernel = ClEnableKasan.getNumOccurrences() > 0 ? |
| 748 | ClEnableKasan : CompileKernel; |
| 749 | } |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 750 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 751 | bool runOnModule(Module &M) override; |
| 752 | StringRef getPassName() const override { return "AddressSanitizerModule"; } |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 753 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 754 | private: |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 755 | void initializeCallbacks(Module &M); |
| 756 | |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 757 | bool InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool *CtorComdat); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 758 | void InstrumentGlobalsCOFF(IRBuilder<> &IRB, Module &M, |
| 759 | ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 760 | ArrayRef<Constant *> MetadataInitializers); |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 761 | void InstrumentGlobalsELF(IRBuilder<> &IRB, Module &M, |
| 762 | ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 763 | ArrayRef<Constant *> MetadataInitializers, |
| 764 | const std::string &UniqueModuleId); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 765 | void InstrumentGlobalsMachO(IRBuilder<> &IRB, Module &M, |
| 766 | ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 767 | ArrayRef<Constant *> MetadataInitializers); |
| 768 | void |
| 769 | InstrumentGlobalsWithMetadataArray(IRBuilder<> &IRB, Module &M, |
| 770 | ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 771 | ArrayRef<Constant *> MetadataInitializers); |
| 772 | |
| 773 | GlobalVariable *CreateMetadataGlobal(Module &M, Constant *Initializer, |
| 774 | StringRef OriginalName); |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 775 | void SetComdatForGlobalMetadata(GlobalVariable *G, GlobalVariable *Metadata, |
| 776 | StringRef InternalSuffix); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 777 | IRBuilder<> CreateAsanModuleDtor(Module &M); |
| 778 | |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 779 | bool ShouldInstrumentGlobal(GlobalVariable *G); |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 780 | bool ShouldUseMachOGlobalsSection() const; |
Reid Kleckner | 01660a3 | 2016-11-21 20:40:37 +0000 | [diff] [blame] | 781 | StringRef getGlobalMetadataSection() const; |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 782 | void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName); |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 783 | void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 784 | size_t MinRedzoneSizeForGlobal() const { |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 785 | return RedzoneSizeForScale(Mapping.Scale); |
| 786 | } |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 787 | int GetAsanVersion(const Module &M) const; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 788 | |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 789 | GlobalsMetadata GlobalsMD; |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 790 | bool CompileKernel; |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 791 | bool Recover; |
Evgeniy Stepanov | 9e53608 | 2017-04-24 19:34:13 +0000 | [diff] [blame] | 792 | bool UseGlobalsGC; |
Evgeniy Stepanov | b56012b | 2017-05-15 20:43:42 +0000 | [diff] [blame] | 793 | bool UseCtorComdat; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 794 | Type *IntptrTy; |
| 795 | LLVMContext *C; |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 796 | Triple TargetTriple; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 797 | ShadowMapping Mapping; |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 798 | Function *AsanPoisonGlobals; |
| 799 | Function *AsanUnpoisonGlobals; |
| 800 | Function *AsanRegisterGlobals; |
| 801 | Function *AsanUnregisterGlobals; |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 802 | Function *AsanRegisterImageGlobals; |
| 803 | Function *AsanUnregisterImageGlobals; |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 804 | Function *AsanRegisterElfGlobals; |
| 805 | Function *AsanUnregisterElfGlobals; |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 806 | |
| 807 | Function *AsanCtorFunction = nullptr; |
| 808 | Function *AsanDtorFunction = nullptr; |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 809 | }; |
| 810 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 811 | // Stack poisoning does not play well with exception handling. |
| 812 | // When an exception is thrown, we essentially bypass the code |
| 813 | // that unpoisones the stack. This is why the run-time library has |
| 814 | // to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire |
| 815 | // stack in the interceptor. This however does not work inside the |
| 816 | // actual function which catches the exception. Most likely because the |
| 817 | // compiler hoists the load of the shadow value somewhere too high. |
| 818 | // This causes asan to report a non-existing bug on 453.povray. |
| 819 | // It sounds like an LLVM bug. |
| 820 | struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> { |
| 821 | Function &F; |
| 822 | AddressSanitizer &ASan; |
| 823 | DIBuilder DIB; |
| 824 | LLVMContext *C; |
| 825 | Type *IntptrTy; |
| 826 | Type *IntptrPtrTy; |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 827 | ShadowMapping Mapping; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 828 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 829 | SmallVector<AllocaInst *, 16> AllocaVec; |
Kuba Brecka | a49dcbb | 2016-11-08 21:30:41 +0000 | [diff] [blame] | 830 | SmallVector<AllocaInst *, 16> StaticAllocasToMoveUp; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 831 | SmallVector<Instruction *, 8> RetVec; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 832 | unsigned StackAlignment; |
| 833 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 834 | Function *AsanStackMallocFunc[kMaxAsanStackMallocSizeClass + 1], |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 835 | *AsanStackFreeFunc[kMaxAsanStackMallocSizeClass + 1]; |
Vitaly Buka | 3455b9b | 2016-08-20 18:34:39 +0000 | [diff] [blame] | 836 | Function *AsanSetShadowFunc[0x100] = {}; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 837 | Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc; |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 838 | Function *AsanAllocaPoisonFunc, *AsanAllocasUnpoisonFunc; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 839 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 840 | // Stores a place and arguments of poisoning/unpoisoning call for alloca. |
| 841 | struct AllocaPoisonCall { |
| 842 | IntrinsicInst *InsBefore; |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 843 | AllocaInst *AI; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 844 | uint64_t Size; |
| 845 | bool DoPoison; |
| 846 | }; |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 847 | SmallVector<AllocaPoisonCall, 8> DynamicAllocaPoisonCallVec; |
| 848 | SmallVector<AllocaPoisonCall, 8> StaticAllocaPoisonCallVec; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 849 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 850 | SmallVector<AllocaInst *, 1> DynamicAllocaVec; |
| 851 | SmallVector<IntrinsicInst *, 1> StackRestoreVec; |
| 852 | AllocaInst *DynamicAllocaLayout = nullptr; |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 853 | IntrinsicInst *LocalEscapeCall = nullptr; |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 854 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 855 | // Maps Value to an AllocaInst from which the Value is originated. |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 856 | using AllocaForValueMapTy = DenseMap<Value *, AllocaInst *>; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 857 | AllocaForValueMapTy AllocaForValue; |
| 858 | |
Alexey Samsonov | 869a5ff | 2015-07-29 19:36:08 +0000 | [diff] [blame] | 859 | bool HasNonEmptyInlineAsm = false; |
| 860 | bool HasReturnsTwiceCall = false; |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 861 | std::unique_ptr<CallInst> EmptyInlineAsm; |
| 862 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 863 | FunctionStackPoisoner(Function &F, AddressSanitizer &ASan) |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 864 | : F(F), |
| 865 | ASan(ASan), |
| 866 | DIB(*F.getParent(), /*AllowUnresolved*/ false), |
| 867 | C(ASan.C), |
| 868 | IntptrTy(ASan.IntptrTy), |
| 869 | IntptrPtrTy(PointerType::get(IntptrTy, 0)), |
| 870 | Mapping(ASan.Mapping), |
| 871 | StackAlignment(1 << Mapping.Scale), |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 872 | EmptyInlineAsm(CallInst::Create(ASan.EmptyAsm)) {} |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 873 | |
| 874 | bool runOnFunction() { |
| 875 | if (!ClStack) return false; |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 876 | |
Matt Morehouse | 49e5aca | 2017-08-09 17:59:43 +0000 | [diff] [blame] | 877 | if (ClRedzoneByvalArgs) |
Vitaly Buka | 629047de | 2017-08-07 07:12:34 +0000 | [diff] [blame] | 878 | copyArgsPassedByValToAllocas(); |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 879 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 880 | // Collect alloca, ret, lifetime instructions etc. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 881 | for (BasicBlock *BB : depth_first(&F.getEntryBlock())) visit(*BB); |
David Blaikie | ceec2bd | 2014-04-11 01:50:01 +0000 | [diff] [blame] | 882 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 883 | if (AllocaVec.empty() && DynamicAllocaVec.empty()) return false; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 884 | |
| 885 | initializeCallbacks(*F.getParent()); |
| 886 | |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 887 | processDynamicAllocas(); |
| 888 | processStaticAllocas(); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 889 | |
| 890 | if (ClDebugStack) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 891 | LLVM_DEBUG(dbgs() << F); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 892 | } |
| 893 | return true; |
| 894 | } |
| 895 | |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 896 | // Arguments marked with the "byval" attribute are implicitly copied without |
| 897 | // using an alloca instruction. To produce redzones for those arguments, we |
| 898 | // copy them a second time into memory allocated with an alloca instruction. |
| 899 | void copyArgsPassedByValToAllocas(); |
| 900 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 901 | // Finds all Alloca instructions and puts |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 902 | // poisoned red zones around all of them. |
| 903 | // Then unpoison everything back before the function returns. |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 904 | void processStaticAllocas(); |
| 905 | void processDynamicAllocas(); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 906 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 907 | void createDynamicAllocasInitStorage(); |
| 908 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 909 | // ----------------------- Visitors. |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 910 | /// Collect all Ret instructions. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 911 | void visitReturnInst(ReturnInst &RI) { RetVec.push_back(&RI); } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 912 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 913 | /// Collect all Resume instructions. |
Vitaly Buka | e3a032a | 2016-07-22 22:04:38 +0000 | [diff] [blame] | 914 | void visitResumeInst(ResumeInst &RI) { RetVec.push_back(&RI); } |
| 915 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 916 | /// Collect all CatchReturnInst instructions. |
Vitaly Buka | e3a032a | 2016-07-22 22:04:38 +0000 | [diff] [blame] | 917 | void visitCleanupReturnInst(CleanupReturnInst &CRI) { RetVec.push_back(&CRI); } |
| 918 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 919 | void unpoisonDynamicAllocasBeforeInst(Instruction *InstBefore, |
| 920 | Value *SavedStack) { |
| 921 | IRBuilder<> IRB(InstBefore); |
Yury Gribov | 6ff0a66 | 2015-12-04 09:19:14 +0000 | [diff] [blame] | 922 | Value *DynamicAreaPtr = IRB.CreatePtrToInt(SavedStack, IntptrTy); |
| 923 | // When we insert _asan_allocas_unpoison before @llvm.stackrestore, we |
| 924 | // need to adjust extracted SP to compute the address of the most recent |
| 925 | // alloca. We have a special @llvm.get.dynamic.area.offset intrinsic for |
| 926 | // this purpose. |
| 927 | if (!isa<ReturnInst>(InstBefore)) { |
| 928 | Function *DynamicAreaOffsetFunc = Intrinsic::getDeclaration( |
| 929 | InstBefore->getModule(), Intrinsic::get_dynamic_area_offset, |
| 930 | {IntptrTy}); |
| 931 | |
| 932 | Value *DynamicAreaOffset = IRB.CreateCall(DynamicAreaOffsetFunc, {}); |
| 933 | |
| 934 | DynamicAreaPtr = IRB.CreateAdd(IRB.CreatePtrToInt(SavedStack, IntptrTy), |
| 935 | DynamicAreaOffset); |
| 936 | } |
| 937 | |
Yury Gribov | 781bce2 | 2015-05-28 08:03:28 +0000 | [diff] [blame] | 938 | IRB.CreateCall(AsanAllocasUnpoisonFunc, |
Yury Gribov | 6ff0a66 | 2015-12-04 09:19:14 +0000 | [diff] [blame] | 939 | {IRB.CreateLoad(DynamicAllocaLayout), DynamicAreaPtr}); |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 942 | // Unpoison dynamic allocas redzones. |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 943 | void unpoisonDynamicAllocas() { |
| 944 | for (auto &Ret : RetVec) |
| 945 | unpoisonDynamicAllocasBeforeInst(Ret, DynamicAllocaLayout); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 946 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 947 | for (auto &StackRestoreInst : StackRestoreVec) |
| 948 | unpoisonDynamicAllocasBeforeInst(StackRestoreInst, |
| 949 | StackRestoreInst->getOperand(0)); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 952 | // Deploy and poison redzones around dynamic alloca call. To do this, we |
| 953 | // should replace this call with another one with changed parameters and |
| 954 | // replace all its uses with new address, so |
| 955 | // addr = alloca type, old_size, align |
| 956 | // is replaced by |
| 957 | // new_size = (old_size + additional_size) * sizeof(type) |
| 958 | // tmp = alloca i8, new_size, max(align, 32) |
| 959 | // addr = tmp + 32 (first 32 bytes are for the left redzone). |
| 960 | // Additional_size is added to make new memory allocation contain not only |
| 961 | // requested memory, but also left, partial and right redzones. |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 962 | void handleDynamicAllocaCall(AllocaInst *AI); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 963 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 964 | /// Collect Alloca instructions we want (and can) handle. |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 965 | void visitAllocaInst(AllocaInst &AI) { |
Kuba Brecka | 37a5ffa | 2015-07-17 06:29:57 +0000 | [diff] [blame] | 966 | if (!ASan.isInterestingAlloca(AI)) { |
Kuba Brecka | a49dcbb | 2016-11-08 21:30:41 +0000 | [diff] [blame] | 967 | if (AI.isStaticAlloca()) { |
| 968 | // Skip over allocas that are present *before* the first instrumented |
| 969 | // alloca, we don't want to move those around. |
| 970 | if (AllocaVec.empty()) |
| 971 | return; |
| 972 | |
| 973 | StaticAllocasToMoveUp.push_back(&AI); |
| 974 | } |
Kuba Brecka | 37a5ffa | 2015-07-17 06:29:57 +0000 | [diff] [blame] | 975 | return; |
| 976 | } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 977 | |
| 978 | StackAlignment = std::max(StackAlignment, AI.getAlignment()); |
Kuba Brecka | 7d03ce4 | 2016-06-27 15:57:08 +0000 | [diff] [blame] | 979 | if (!AI.isStaticAlloca()) |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 980 | DynamicAllocaVec.push_back(&AI); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 981 | else |
| 982 | AllocaVec.push_back(&AI); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 985 | /// Collect lifetime intrinsic calls to check for use-after-scope |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 986 | /// errors. |
| 987 | void visitIntrinsicInst(IntrinsicInst &II) { |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 988 | Intrinsic::ID ID = II.getIntrinsicID(); |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 989 | if (ID == Intrinsic::stackrestore) StackRestoreVec.push_back(&II); |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 990 | if (ID == Intrinsic::localescape) LocalEscapeCall = &II; |
Vitaly Buka | 1e75fa4 | 2016-05-27 22:55:10 +0000 | [diff] [blame] | 991 | if (!ASan.UseAfterScope) |
| 992 | return; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 993 | if (ID != Intrinsic::lifetime_start && ID != Intrinsic::lifetime_end) |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 994 | return; |
| 995 | // Found lifetime intrinsic, add ASan instrumentation if necessary. |
| 996 | ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0)); |
| 997 | // If size argument is undefined, don't do anything. |
| 998 | if (Size->isMinusOne()) return; |
| 999 | // Check that size doesn't saturate uint64_t and can |
| 1000 | // be stored in IntptrTy. |
| 1001 | const uint64_t SizeValue = Size->getValue().getLimitedValue(); |
| 1002 | if (SizeValue == ~0ULL || |
| 1003 | !ConstantInt::isValueValidForType(IntptrTy, SizeValue)) |
| 1004 | return; |
| 1005 | // Find alloca instruction that corresponds to llvm.lifetime argument. |
| 1006 | AllocaInst *AI = findAllocaForValue(II.getArgOperand(1)); |
Vitaly Buka | b451f1b | 2016-06-09 23:31:59 +0000 | [diff] [blame] | 1007 | if (!AI || !ASan.isInterestingAlloca(*AI)) |
| 1008 | return; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1009 | bool DoPoison = (ID == Intrinsic::lifetime_end); |
Alexey Samsonov | a788b94 | 2013-11-18 14:53:55 +0000 | [diff] [blame] | 1010 | AllocaPoisonCall APC = {&II, AI, SizeValue, DoPoison}; |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 1011 | if (AI->isStaticAlloca()) |
| 1012 | StaticAllocaPoisonCallVec.push_back(APC); |
| 1013 | else if (ClInstrumentDynamicAllocas) |
| 1014 | DynamicAllocaPoisonCallVec.push_back(APC); |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Alexey Samsonov | 869a5ff | 2015-07-29 19:36:08 +0000 | [diff] [blame] | 1017 | void visitCallSite(CallSite CS) { |
| 1018 | Instruction *I = CS.getInstruction(); |
| 1019 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 1020 | HasNonEmptyInlineAsm |= CI->isInlineAsm() && |
| 1021 | !CI->isIdenticalTo(EmptyInlineAsm.get()) && |
| 1022 | I != ASan.LocalDynamicShadow; |
Alexey Samsonov | 869a5ff | 2015-07-29 19:36:08 +0000 | [diff] [blame] | 1023 | HasReturnsTwiceCall |= CI->canReturnTwice(); |
| 1024 | } |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1027 | // ---------------------- Helpers. |
| 1028 | void initializeCallbacks(Module &M); |
| 1029 | |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 1030 | bool doesDominateAllExits(const Instruction *I) const { |
| 1031 | for (auto Ret : RetVec) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1032 | if (!ASan.getDominatorTree().dominates(I, Ret)) return false; |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 1033 | } |
| 1034 | return true; |
| 1035 | } |
| 1036 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 1037 | /// Finds alloca where the value comes from. |
| 1038 | AllocaInst *findAllocaForValue(Value *V); |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 1039 | |
| 1040 | // Copies bytes from ShadowBytes into shadow memory for indexes where |
| 1041 | // ShadowMask is not zero. If ShadowMask[i] is zero, we assume that |
| 1042 | // ShadowBytes[i] is constantly zero and doesn't need to be overwritten. |
| 1043 | void copyToShadow(ArrayRef<uint8_t> ShadowMask, ArrayRef<uint8_t> ShadowBytes, |
| 1044 | IRBuilder<> &IRB, Value *ShadowBase); |
| 1045 | void copyToShadow(ArrayRef<uint8_t> ShadowMask, ArrayRef<uint8_t> ShadowBytes, |
| 1046 | size_t Begin, size_t End, IRBuilder<> &IRB, |
| 1047 | Value *ShadowBase); |
| 1048 | void copyToShadowInline(ArrayRef<uint8_t> ShadowMask, |
| 1049 | ArrayRef<uint8_t> ShadowBytes, size_t Begin, |
| 1050 | size_t End, IRBuilder<> &IRB, Value *ShadowBase); |
| 1051 | |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 1052 | void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> &IRB, bool DoPoison); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 1053 | |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 1054 | Value *createAllocaForLayout(IRBuilder<> &IRB, const ASanStackFrameLayout &L, |
| 1055 | bool Dynamic); |
| 1056 | PHINode *createPHI(IRBuilder<> &IRB, Value *Cond, Value *ValueIfTrue, |
| 1057 | Instruction *ThenTerm, Value *ValueIfFalse); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 1058 | }; |
| 1059 | |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 1060 | } // end anonymous namespace |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1061 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 1062 | char AddressSanitizer::ID = 0; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 1063 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1064 | INITIALIZE_PASS_BEGIN( |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 1065 | AddressSanitizer, "asan", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1066 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false, |
| 1067 | false) |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 1068 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Keno Fischer | a010cfa | 2015-10-20 10:13:55 +0000 | [diff] [blame] | 1069 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1070 | INITIALIZE_PASS_END( |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 1071 | AddressSanitizer, "asan", |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1072 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", false, |
| 1073 | false) |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 1074 | |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 1075 | FunctionPass *llvm::createAddressSanitizerFunctionPass(bool CompileKernel, |
Vitaly Buka | 1e75fa4 | 2016-05-27 22:55:10 +0000 | [diff] [blame] | 1076 | bool Recover, |
| 1077 | bool UseAfterScope) { |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 1078 | assert(!CompileKernel || Recover); |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 1079 | return new AddressSanitizer(CompileKernel, Recover, UseAfterScope); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 1082 | char AddressSanitizerModule::ID = 0; |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 1083 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1084 | INITIALIZE_PASS( |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 1085 | AddressSanitizerModule, "asan-module", |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 1086 | "AddressSanitizer: detects use-after-free and out-of-bounds bugs." |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1087 | "ModulePass", |
| 1088 | false, false) |
Eugene Zelenko | bff0ef0 | 2017-10-19 22:07:16 +0000 | [diff] [blame] | 1089 | |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 1090 | ModulePass *llvm::createAddressSanitizerModulePass(bool CompileKernel, |
Evgeniy Stepanov | 9e53608 | 2017-04-24 19:34:13 +0000 | [diff] [blame] | 1091 | bool Recover, |
| 1092 | bool UseGlobalsGC) { |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 1093 | assert(!CompileKernel || Recover); |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 1094 | return new AddressSanitizerModule(CompileKernel, Recover, UseGlobalsGC); |
Alexander Potapenko | c94cf8f | 2012-01-23 11:22:43 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 1097 | static size_t TypeSizeToSizeIndex(uint32_t TypeSize) { |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 1098 | size_t Res = countTrailingZeros(TypeSize / 8); |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 1099 | assert(Res < kNumberOfAccessSizes); |
| 1100 | return Res; |
| 1101 | } |
| 1102 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1103 | /// Create a global describing a source location. |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 1104 | static GlobalVariable *createPrivateGlobalForSourceLoc(Module &M, |
| 1105 | LocationMetadata MD) { |
| 1106 | Constant *LocData[] = { |
Kostya Serebryany | d891ac9 | 2018-10-11 23:03:27 +0000 | [diff] [blame] | 1107 | createPrivateGlobalForString(M, MD.Filename, true, kAsanGenPrefix), |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 1108 | ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.LineNo), |
| 1109 | ConstantInt::get(Type::getInt32Ty(M.getContext()), MD.ColumnNo), |
| 1110 | }; |
| 1111 | auto LocStruct = ConstantStruct::getAnon(LocData); |
| 1112 | auto GV = new GlobalVariable(M, LocStruct->getType(), true, |
| 1113 | GlobalValue::PrivateLinkage, LocStruct, |
| 1114 | kAsanGenPrefix); |
Peter Collingbourne | 96efdd6 | 2016-06-14 21:01:22 +0000 | [diff] [blame] | 1115 | GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 1116 | return GV; |
| 1117 | } |
| 1118 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 1119 | /// Check if \p G has been created by a trusted compiler pass. |
Vedant Kumar | f5ac6d4 | 2016-06-22 17:30:58 +0000 | [diff] [blame] | 1120 | static bool GlobalWasGeneratedByCompiler(GlobalVariable *G) { |
Reid Kleckner | 85a8c12 | 2018-08-20 23:35:45 +0000 | [diff] [blame] | 1121 | // Do not instrument @llvm.global_ctors, @llvm.used, etc. |
| 1122 | if (G->getName().startswith("llvm.")) |
| 1123 | return true; |
| 1124 | |
Vedant Kumar | f5ac6d4 | 2016-06-22 17:30:58 +0000 | [diff] [blame] | 1125 | // Do not instrument asan globals. |
| 1126 | if (G->getName().startswith(kAsanGenPrefix) || |
| 1127 | G->getName().startswith(kSanCovGenPrefix) || |
| 1128 | G->getName().startswith(kODRGenPrefix)) |
| 1129 | return true; |
| 1130 | |
| 1131 | // Do not instrument gcov counter arrays. |
| 1132 | if (G->getName() == "__llvm_gcov_ctr") |
| 1133 | return true; |
| 1134 | |
| 1135 | return false; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1138 | Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) { |
| 1139 | // Shadow >> scale |
Alexey Samsonov | 1345d35 | 2013-01-16 13:23:28 +0000 | [diff] [blame] | 1140 | Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1141 | if (Mapping.Offset == 0) return Shadow; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1142 | // (Shadow >> scale) | offset |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 1143 | Value *ShadowBase; |
| 1144 | if (LocalDynamicShadow) |
| 1145 | ShadowBase = LocalDynamicShadow; |
Etienne Bergeron | 6ba5176 | 2016-09-19 15:58:38 +0000 | [diff] [blame] | 1146 | else |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 1147 | ShadowBase = ConstantInt::get(IntptrTy, Mapping.Offset); |
| 1148 | if (Mapping.OrShadowOffset) |
| 1149 | return IRB.CreateOr(Shadow, ShadowBase); |
| 1150 | else |
| 1151 | return IRB.CreateAdd(Shadow, ShadowBase); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1154 | // Instrument memset/memmove/memcpy |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1155 | void AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) { |
| 1156 | IRBuilder<> IRB(MI); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1157 | if (isa<MemTransferInst>(MI)) { |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1158 | IRB.CreateCall( |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1159 | isa<MemMoveInst>(MI) ? AsanMemmove : AsanMemcpy, |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1160 | {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()), |
| 1161 | IRB.CreatePointerCast(MI->getOperand(1), IRB.getInt8PtrTy()), |
| 1162 | IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)}); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1163 | } else if (isa<MemSetInst>(MI)) { |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1164 | IRB.CreateCall( |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1165 | AsanMemset, |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1166 | {IRB.CreatePointerCast(MI->getOperand(0), IRB.getInt8PtrTy()), |
| 1167 | IRB.CreateIntCast(MI->getOperand(1), IRB.getInt32Ty(), false), |
| 1168 | IRB.CreateIntCast(MI->getOperand(2), IntptrTy, false)}); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1169 | } |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 1170 | MI->eraseFromParent(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1173 | /// Check if we want (and can) handle this alloca. |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 1174 | bool AddressSanitizer::isInterestingAlloca(const AllocaInst &AI) { |
Anna Zaks | bf28d3a | 2015-03-27 18:52:01 +0000 | [diff] [blame] | 1175 | auto PreviouslySeenAllocaInfo = ProcessedAllocas.find(&AI); |
| 1176 | |
| 1177 | if (PreviouslySeenAllocaInfo != ProcessedAllocas.end()) |
| 1178 | return PreviouslySeenAllocaInfo->getSecond(); |
| 1179 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 1180 | bool IsInteresting = |
| 1181 | (AI.getAllocatedType()->isSized() && |
| 1182 | // alloca() may be called with 0 size, ignore it. |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 1183 | ((!AI.isStaticAlloca()) || getAllocaSizeInBytes(AI) > 0) && |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 1184 | // We are only interested in allocas not promotable to registers. |
| 1185 | // Promotable allocas are common under -O0. |
Alexey Samsonov | 55fda1b | 2015-11-05 21:18:41 +0000 | [diff] [blame] | 1186 | (!ClSkipPromotableAllocas || !isAllocaPromotable(&AI)) && |
| 1187 | // inalloca allocas are not treated as static, and we don't want |
| 1188 | // dynamic alloca instrumentation for them as well. |
Arnold Schwaighofer | 8d61e00 | 2017-02-15 20:43:43 +0000 | [diff] [blame] | 1189 | !AI.isUsedWithInAlloca() && |
| 1190 | // swifterror allocas are register promoted by ISel |
| 1191 | !AI.isSwiftError()); |
Anna Zaks | bf28d3a | 2015-03-27 18:52:01 +0000 | [diff] [blame] | 1192 | |
| 1193 | ProcessedAllocas[&AI] = IsInteresting; |
| 1194 | return IsInteresting; |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1197 | Value *AddressSanitizer::isInterestingMemoryAccess(Instruction *I, |
| 1198 | bool *IsWrite, |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1199 | uint64_t *TypeSize, |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1200 | unsigned *Alignment, |
| 1201 | Value **MaybeMask) { |
Alexey Samsonov | 535b6f9 | 2014-07-17 18:48:12 +0000 | [diff] [blame] | 1202 | // Skip memory accesses inserted by another instrumentation. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1203 | if (I->getMetadata("nosanitize")) return nullptr; |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1204 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 1205 | // Do not instrument the load fetching the dynamic shadow address. |
| 1206 | if (LocalDynamicShadow == I) |
| 1207 | return nullptr; |
| 1208 | |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1209 | Value *PtrOperand = nullptr; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1210 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1211 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1212 | if (!ClInstrumentReads) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1213 | *IsWrite = false; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1214 | *TypeSize = DL.getTypeStoreSizeInBits(LI->getType()); |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1215 | *Alignment = LI->getAlignment(); |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1216 | PtrOperand = LI->getPointerOperand(); |
| 1217 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1218 | if (!ClInstrumentWrites) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1219 | *IsWrite = true; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1220 | *TypeSize = DL.getTypeStoreSizeInBits(SI->getValueOperand()->getType()); |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1221 | *Alignment = SI->getAlignment(); |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1222 | PtrOperand = SI->getPointerOperand(); |
| 1223 | } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1224 | if (!ClInstrumentAtomics) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1225 | *IsWrite = true; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1226 | *TypeSize = DL.getTypeStoreSizeInBits(RMW->getValOperand()->getType()); |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1227 | *Alignment = 0; |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1228 | PtrOperand = RMW->getPointerOperand(); |
| 1229 | } else if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1230 | if (!ClInstrumentAtomics) return nullptr; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1231 | *IsWrite = true; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1232 | *TypeSize = DL.getTypeStoreSizeInBits(XCHG->getCompareOperand()->getType()); |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1233 | *Alignment = 0; |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1234 | PtrOperand = XCHG->getPointerOperand(); |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1235 | } else if (auto CI = dyn_cast<CallInst>(I)) { |
| 1236 | auto *F = dyn_cast<Function>(CI->getCalledValue()); |
| 1237 | if (F && (F->getName().startswith("llvm.masked.load.") || |
| 1238 | F->getName().startswith("llvm.masked.store."))) { |
| 1239 | unsigned OpOffset = 0; |
| 1240 | if (F->getName().startswith("llvm.masked.store.")) { |
Filipe Cabecinhas | 1e69017 | 2016-12-14 21:56:59 +0000 | [diff] [blame] | 1241 | if (!ClInstrumentWrites) |
| 1242 | return nullptr; |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1243 | // Masked store has an initial operand for the value. |
| 1244 | OpOffset = 1; |
| 1245 | *IsWrite = true; |
| 1246 | } else { |
Filipe Cabecinhas | 1e69017 | 2016-12-14 21:56:59 +0000 | [diff] [blame] | 1247 | if (!ClInstrumentReads) |
| 1248 | return nullptr; |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1249 | *IsWrite = false; |
| 1250 | } |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1251 | |
| 1252 | auto BasePtr = CI->getOperand(0 + OpOffset); |
| 1253 | auto Ty = cast<PointerType>(BasePtr->getType())->getElementType(); |
| 1254 | *TypeSize = DL.getTypeStoreSizeInBits(Ty); |
| 1255 | if (auto AlignmentConstant = |
| 1256 | dyn_cast<ConstantInt>(CI->getOperand(1 + OpOffset))) |
| 1257 | *Alignment = (unsigned)AlignmentConstant->getZExtValue(); |
| 1258 | else |
| 1259 | *Alignment = 1; // No alignment guarantees. We probably got Undef |
| 1260 | if (MaybeMask) |
| 1261 | *MaybeMask = CI->getOperand(2 + OpOffset); |
| 1262 | PtrOperand = BasePtr; |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1263 | } |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1264 | } |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1265 | |
Anna Zaks | 644d9d3 | 2016-06-22 00:15:52 +0000 | [diff] [blame] | 1266 | if (PtrOperand) { |
Arnold Schwaighofer | 8d61e00 | 2017-02-15 20:43:43 +0000 | [diff] [blame] | 1267 | // Do not instrument acesses from different address spaces; we cannot deal |
| 1268 | // with them. |
Anna Zaks | 644d9d3 | 2016-06-22 00:15:52 +0000 | [diff] [blame] | 1269 | Type *PtrTy = cast<PointerType>(PtrOperand->getType()->getScalarType()); |
| 1270 | if (PtrTy->getPointerAddressSpace() != 0) |
| 1271 | return nullptr; |
Arnold Schwaighofer | 8d61e00 | 2017-02-15 20:43:43 +0000 | [diff] [blame] | 1272 | |
| 1273 | // Ignore swifterror addresses. |
| 1274 | // swifterror memory addresses are mem2reg promoted by instruction |
| 1275 | // selection. As such they cannot have regular uses like an instrumentation |
| 1276 | // function and it makes no sense to track them as memory. |
| 1277 | if (PtrOperand->isSwiftError()) |
| 1278 | return nullptr; |
Anna Zaks | 644d9d3 | 2016-06-22 00:15:52 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 1281 | // Treat memory accesses to promotable allocas as non-interesting since they |
| 1282 | // will not cause memory violations. This greatly speeds up the instrumented |
| 1283 | // executable at -O0. |
| 1284 | if (ClSkipPromotableAllocas) |
| 1285 | if (auto AI = dyn_cast_or_null<AllocaInst>(PtrOperand)) |
| 1286 | return isInterestingAlloca(*AI) ? AI : nullptr; |
| 1287 | |
| 1288 | return PtrOperand; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1291 | static bool isPointerOperand(Value *V) { |
| 1292 | return V->getType()->isPointerTy() || isa<PtrToIntInst>(V); |
| 1293 | } |
| 1294 | |
| 1295 | // This is a rough heuristic; it may cause both false positives and |
| 1296 | // false negatives. The proper implementation requires cooperation with |
| 1297 | // the frontend. |
| 1298 | static bool isInterestingPointerComparisonOrSubtraction(Instruction *I) { |
| 1299 | if (ICmpInst *Cmp = dyn_cast<ICmpInst>(I)) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1300 | if (!Cmp->isRelational()) return false; |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1301 | } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1302 | if (BO->getOpcode() != Instruction::Sub) return false; |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1303 | } else { |
| 1304 | return false; |
| 1305 | } |
Alexey Samsonov | 145b0fd | 2015-10-26 18:06:40 +0000 | [diff] [blame] | 1306 | return isPointerOperand(I->getOperand(0)) && |
| 1307 | isPointerOperand(I->getOperand(1)); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 1310 | bool AddressSanitizer::GlobalIsLinkerInitialized(GlobalVariable *G) { |
| 1311 | // If a global variable does not have dynamic initialization we don't |
| 1312 | // have to instrument it. However, if a global does not have initializer |
| 1313 | // at all, we assume it has dynamic initializer (in other TU). |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 1314 | return G->hasInitializer() && !GlobalsMD.get(G).IsDynInit; |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1317 | void AddressSanitizer::instrumentPointerComparisonOrSubtraction( |
| 1318 | Instruction *I) { |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1319 | IRBuilder<> IRB(I); |
| 1320 | Function *F = isa<ICmpInst>(I) ? AsanPtrCmpFunction : AsanPtrSubFunction; |
| 1321 | Value *Param[2] = {I->getOperand(0), I->getOperand(1)}; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1322 | for (Value *&i : Param) { |
| 1323 | if (i->getType()->isPointerTy()) |
| 1324 | i = IRB.CreatePointerCast(i, IntptrTy); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1325 | } |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1326 | IRB.CreateCall(F, Param); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1329 | static void doInstrumentAddress(AddressSanitizer *Pass, Instruction *I, |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1330 | Instruction *InsertBefore, Value *Addr, |
| 1331 | unsigned Alignment, unsigned Granularity, |
| 1332 | uint32_t TypeSize, bool IsWrite, |
| 1333 | Value *SizeArgument, bool UseCalls, |
| 1334 | uint32_t Exp) { |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1335 | // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check |
| 1336 | // if the data is properly aligned. |
| 1337 | if ((TypeSize == 8 || TypeSize == 16 || TypeSize == 32 || TypeSize == 64 || |
| 1338 | TypeSize == 128) && |
| 1339 | (Alignment >= Granularity || Alignment == 0 || Alignment >= TypeSize / 8)) |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1340 | return Pass->instrumentAddress(I, InsertBefore, Addr, TypeSize, IsWrite, |
| 1341 | nullptr, UseCalls, Exp); |
| 1342 | Pass->instrumentUnusualSizeOrAlignment(I, InsertBefore, Addr, TypeSize, |
| 1343 | IsWrite, nullptr, UseCalls, Exp); |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | static void instrumentMaskedLoadOrStore(AddressSanitizer *Pass, |
| 1347 | const DataLayout &DL, Type *IntptrTy, |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1348 | Value *Mask, Instruction *I, |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1349 | Value *Addr, unsigned Alignment, |
| 1350 | unsigned Granularity, uint32_t TypeSize, |
| 1351 | bool IsWrite, Value *SizeArgument, |
| 1352 | bool UseCalls, uint32_t Exp) { |
| 1353 | auto *VTy = cast<PointerType>(Addr->getType())->getElementType(); |
| 1354 | uint64_t ElemTypeSize = DL.getTypeStoreSizeInBits(VTy->getScalarType()); |
| 1355 | unsigned Num = VTy->getVectorNumElements(); |
| 1356 | auto Zero = ConstantInt::get(IntptrTy, 0); |
| 1357 | for (unsigned Idx = 0; Idx < Num; ++Idx) { |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1358 | Value *InstrumentedAddress = nullptr; |
| 1359 | Instruction *InsertBefore = I; |
| 1360 | if (auto *Vector = dyn_cast<ConstantVector>(Mask)) { |
| 1361 | // dyn_cast as we might get UndefValue |
| 1362 | if (auto *Masked = dyn_cast<ConstantInt>(Vector->getOperand(Idx))) { |
Craig Topper | 79ab643 | 2017-07-06 18:39:47 +0000 | [diff] [blame] | 1363 | if (Masked->isZero()) |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1364 | // Mask is constant false, so no instrumentation needed. |
| 1365 | continue; |
| 1366 | // If we have a true or undef value, fall through to doInstrumentAddress |
| 1367 | // with InsertBefore == I |
| 1368 | } |
| 1369 | } else { |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1370 | IRBuilder<> IRB(I); |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1371 | Value *MaskElem = IRB.CreateExtractElement(Mask, Idx); |
Chandler Carruth | 4a2d58e | 2018-10-15 09:34:05 +0000 | [diff] [blame] | 1372 | Instruction *ThenTerm = SplitBlockAndInsertIfThen(MaskElem, I, false); |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1373 | InsertBefore = ThenTerm; |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1374 | } |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1375 | |
| 1376 | IRBuilder<> IRB(InsertBefore); |
| 1377 | InstrumentedAddress = |
| 1378 | IRB.CreateGEP(Addr, {Zero, ConstantInt::get(IntptrTy, Idx)}); |
| 1379 | doInstrumentAddress(Pass, I, InsertBefore, InstrumentedAddress, Alignment, |
| 1380 | Granularity, ElemTypeSize, IsWrite, SizeArgument, |
| 1381 | UseCalls, Exp); |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1385 | void AddressSanitizer::instrumentMop(ObjectSizeOffsetVisitor &ObjSizeVis, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1386 | Instruction *I, bool UseCalls, |
| 1387 | const DataLayout &DL) { |
Axel Naumann | 4a12706 | 2012-09-17 14:20:57 +0000 | [diff] [blame] | 1388 | bool IsWrite = false; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1389 | unsigned Alignment = 0; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1390 | uint64_t TypeSize = 0; |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1391 | Value *MaybeMask = nullptr; |
| 1392 | Value *Addr = |
| 1393 | isInterestingMemoryAccess(I, &IsWrite, &TypeSize, &Alignment, &MaybeMask); |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 1394 | assert(Addr); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1395 | |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1396 | // Optimization experiments. |
| 1397 | // The experiments can be used to evaluate potential optimizations that remove |
| 1398 | // instrumentation (assess false negatives). Instead of completely removing |
| 1399 | // some instrumentation, you set Exp to a non-zero value (mask of optimization |
| 1400 | // experiments that want to remove instrumentation of this instruction). |
| 1401 | // If Exp is non-zero, this pass will emit special calls into runtime |
| 1402 | // (e.g. __asan_report_exp_load1 instead of __asan_report_load1). These calls |
| 1403 | // make runtime terminate the program in a special way (with a different |
| 1404 | // exit status). Then you run the new compiler on a buggy corpus, collect |
| 1405 | // the special terminations (ideally, you don't see them at all -- no false |
| 1406 | // negatives) and make the decision on the optimization. |
| 1407 | uint32_t Exp = ClForceExperiment; |
| 1408 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1409 | if (ClOpt && ClOptGlobals) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1410 | // If initialization order checking is disabled, a simple access to a |
| 1411 | // dynamically initialized global is always valid. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1412 | GlobalVariable *G = dyn_cast<GlobalVariable>(GetUnderlyingObject(Addr, DL)); |
Hans Wennborg | 083ca9b | 2015-10-06 23:24:35 +0000 | [diff] [blame] | 1413 | if (G && (!ClInitializers || GlobalIsLinkerInitialized(G)) && |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1414 | isSafeAccess(ObjSizeVis, Addr, TypeSize)) { |
| 1415 | NumOptimizedAccessesToGlobalVar++; |
| 1416 | return; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1417 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1418 | } |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1419 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1420 | if (ClOpt && ClOptStack) { |
| 1421 | // A direct inbounds access to a stack variable is always valid. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1422 | if (isa<AllocaInst>(GetUnderlyingObject(Addr, DL)) && |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1423 | isSafeAccess(ObjSizeVis, Addr, TypeSize)) { |
| 1424 | NumOptimizedAccessesToStackVar++; |
| 1425 | return; |
| 1426 | } |
| 1427 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1428 | |
Kostya Serebryany | d3d23be | 2013-10-16 14:06:14 +0000 | [diff] [blame] | 1429 | if (IsWrite) |
| 1430 | NumInstrumentedWrites++; |
| 1431 | else |
| 1432 | NumInstrumentedReads++; |
| 1433 | |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 1434 | unsigned Granularity = 1 << Mapping.Scale; |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1435 | if (MaybeMask) { |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1436 | instrumentMaskedLoadOrStore(this, DL, IntptrTy, MaybeMask, I, Addr, |
| 1437 | Alignment, Granularity, TypeSize, IsWrite, |
| 1438 | nullptr, UseCalls, Exp); |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1439 | } else { |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1440 | doInstrumentAddress(this, I, I, Addr, Alignment, Granularity, TypeSize, |
Filipe Cabecinhas | ec350b7 | 2016-11-15 22:37:30 +0000 | [diff] [blame] | 1441 | IsWrite, nullptr, UseCalls, Exp); |
| 1442 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1445 | Instruction *AddressSanitizer::generateCrashCode(Instruction *InsertBefore, |
| 1446 | Value *Addr, bool IsWrite, |
| 1447 | size_t AccessSizeIndex, |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1448 | Value *SizeArgument, |
| 1449 | uint32_t Exp) { |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 1450 | IRBuilder<> IRB(InsertBefore); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1451 | Value *ExpVal = Exp == 0 ? nullptr : ConstantInt::get(IRB.getInt32Ty(), Exp); |
| 1452 | CallInst *Call = nullptr; |
| 1453 | if (SizeArgument) { |
| 1454 | if (Exp == 0) |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1455 | Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][0], |
| 1456 | {Addr, SizeArgument}); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1457 | else |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1458 | Call = IRB.CreateCall(AsanErrorCallbackSized[IsWrite][1], |
| 1459 | {Addr, SizeArgument, ExpVal}); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1460 | } else { |
| 1461 | if (Exp == 0) |
| 1462 | Call = |
| 1463 | IRB.CreateCall(AsanErrorCallback[IsWrite][0][AccessSizeIndex], Addr); |
| 1464 | else |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1465 | Call = IRB.CreateCall(AsanErrorCallback[IsWrite][1][AccessSizeIndex], |
| 1466 | {Addr, ExpVal}); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1467 | } |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 1468 | |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 1469 | // We don't do Call->setDoesNotReturn() because the BB already has |
| 1470 | // UnreachableInst at the end. |
| 1471 | // This EmptyAsm is required to avoid callback merge. |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1472 | IRB.CreateCall(EmptyAsm, {}); |
Kostya Serebryany | 3411f2e | 2012-01-06 18:09:21 +0000 | [diff] [blame] | 1473 | return Call; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Kostya Serebryany | c4ce5df | 2012-07-16 17:12:07 +0000 | [diff] [blame] | 1476 | Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1477 | Value *ShadowValue, |
| 1478 | uint32_t TypeSize) { |
Aaron Ballman | ef0fe1e | 2016-03-30 21:30:00 +0000 | [diff] [blame] | 1479 | size_t Granularity = static_cast<size_t>(1) << Mapping.Scale; |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 1480 | // Addr & (Granularity - 1) |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1481 | Value *LastAccessedByte = |
| 1482 | IRB.CreateAnd(AddrLong, ConstantInt::get(IntptrTy, Granularity - 1)); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 1483 | // (Addr & (Granularity - 1)) + size - 1 |
| 1484 | if (TypeSize / 8 > 1) |
| 1485 | LastAccessedByte = IRB.CreateAdd( |
| 1486 | LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)); |
| 1487 | // (uint8_t) ((Addr & (Granularity-1)) + size - 1) |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1488 | LastAccessedByte = |
| 1489 | IRB.CreateIntCast(LastAccessedByte, ShadowValue->getType(), false); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 1490 | // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue |
| 1491 | return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue); |
| 1492 | } |
| 1493 | |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1494 | void AddressSanitizer::instrumentAddress(Instruction *OrigIns, |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1495 | Instruction *InsertBefore, Value *Addr, |
| 1496 | uint32_t TypeSize, bool IsWrite, |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1497 | Value *SizeArgument, bool UseCalls, |
| 1498 | uint32_t Exp) { |
Walter Lee | cdbb207 | 2018-05-18 04:10:38 +0000 | [diff] [blame] | 1499 | bool IsMyriad = TargetTriple.getVendor() == llvm::Triple::Myriad; |
| 1500 | |
Kostya Serebryany | 3ece9bea | 2013-02-19 11:29:21 +0000 | [diff] [blame] | 1501 | IRBuilder<> IRB(InsertBefore); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1502 | Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1503 | size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize); |
| 1504 | |
| 1505 | if (UseCalls) { |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1506 | if (Exp == 0) |
| 1507 | IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][0][AccessSizeIndex], |
| 1508 | AddrLong); |
| 1509 | else |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1510 | IRB.CreateCall(AsanMemoryAccessCallback[IsWrite][1][AccessSizeIndex], |
| 1511 | {AddrLong, ConstantInt::get(IRB.getInt32Ty(), Exp)}); |
Kostya Serebryany | 0c02d26 | 2014-04-16 12:12:19 +0000 | [diff] [blame] | 1512 | return; |
| 1513 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1514 | |
Walter Lee | cdbb207 | 2018-05-18 04:10:38 +0000 | [diff] [blame] | 1515 | if (IsMyriad) { |
| 1516 | // Strip the cache bit and do range check. |
| 1517 | // AddrLong &= ~kMyriadCacheBitMask32 |
| 1518 | AddrLong = IRB.CreateAnd(AddrLong, ~kMyriadCacheBitMask32); |
| 1519 | // Tag = AddrLong >> kMyriadTagShift |
| 1520 | Value *Tag = IRB.CreateLShr(AddrLong, kMyriadTagShift); |
| 1521 | // Tag == kMyriadDDRTag |
| 1522 | Value *TagCheck = |
| 1523 | IRB.CreateICmpEQ(Tag, ConstantInt::get(IntptrTy, kMyriadDDRTag)); |
| 1524 | |
Chandler Carruth | 4a2d58e | 2018-10-15 09:34:05 +0000 | [diff] [blame] | 1525 | Instruction *TagCheckTerm = |
| 1526 | SplitBlockAndInsertIfThen(TagCheck, InsertBefore, false, |
| 1527 | MDBuilder(*C).createBranchWeights(1, 100000)); |
Walter Lee | cdbb207 | 2018-05-18 04:10:38 +0000 | [diff] [blame] | 1528 | assert(cast<BranchInst>(TagCheckTerm)->isUnconditional()); |
| 1529 | IRB.SetInsertPoint(TagCheckTerm); |
| 1530 | InsertBefore = TagCheckTerm; |
| 1531 | } |
| 1532 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1533 | Type *ShadowTy = |
| 1534 | IntegerType::get(*C, std::max(8U, TypeSize >> Mapping.Scale)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1535 | Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); |
| 1536 | Value *ShadowPtr = memToShadow(AddrLong, IRB); |
| 1537 | Value *CmpVal = Constant::getNullValue(ShadowTy); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1538 | Value *ShadowValue = |
| 1539 | IRB.CreateLoad(IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1540 | |
| 1541 | Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal); |
Aaron Ballman | ef0fe1e | 2016-03-30 21:30:00 +0000 | [diff] [blame] | 1542 | size_t Granularity = 1ULL << Mapping.Scale; |
Chandler Carruth | 4a2d58e | 2018-10-15 09:34:05 +0000 | [diff] [blame] | 1543 | Instruction *CrashTerm = nullptr; |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 1544 | |
Kostya Serebryany | 1e575ab | 2012-08-15 08:58:58 +0000 | [diff] [blame] | 1545 | if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) { |
Kostya Serebryany | ad23852 | 2014-09-02 21:46:51 +0000 | [diff] [blame] | 1546 | // We use branch weights for the slow path check, to indicate that the slow |
| 1547 | // path is rarely taken. This seems to be the case for SPEC benchmarks. |
Chandler Carruth | 4a2d58e | 2018-10-15 09:34:05 +0000 | [diff] [blame] | 1548 | Instruction *CheckTerm = SplitBlockAndInsertIfThen( |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1549 | Cmp, InsertBefore, false, MDBuilder(*C).createBranchWeights(1, 100000)); |
Benjamin Kramer | 619c4e5 | 2015-04-10 11:24:51 +0000 | [diff] [blame] | 1550 | assert(cast<BranchInst>(CheckTerm)->isUnconditional()); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 1551 | BasicBlock *NextBB = CheckTerm->getSuccessor(0); |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 1552 | IRB.SetInsertPoint(CheckTerm); |
| 1553 | Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize); |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 1554 | if (Recover) { |
| 1555 | CrashTerm = SplitBlockAndInsertIfThen(Cmp2, CheckTerm, false); |
| 1556 | } else { |
| 1557 | BasicBlock *CrashBlock = |
Kostya Serebryany | b0e2506 | 2012-10-15 14:20:06 +0000 | [diff] [blame] | 1558 | BasicBlock::Create(*C, "", NextBB->getParent(), NextBB); |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 1559 | CrashTerm = new UnreachableInst(*C, CrashBlock); |
| 1560 | BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2); |
| 1561 | ReplaceInstWithInst(CheckTerm, NewTerm); |
| 1562 | } |
Kostya Serebryany | 874dae6 | 2012-07-16 16:15:40 +0000 | [diff] [blame] | 1563 | } else { |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 1564 | CrashTerm = SplitBlockAndInsertIfThen(Cmp, InsertBefore, !Recover); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1565 | } |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 1566 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1567 | Instruction *Crash = generateCrashCode(CrashTerm, AddrLong, IsWrite, |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1568 | AccessSizeIndex, SizeArgument, Exp); |
Kostya Serebryany | fda7a13 | 2012-08-14 14:04:51 +0000 | [diff] [blame] | 1569 | Crash->setDebugLoc(OrigIns->getDebugLoc()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1572 | // Instrument unusual size or unusual alignment. |
| 1573 | // We can not do it with a single check, so we do 1-byte check for the first |
| 1574 | // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able |
| 1575 | // to report the actual access size. |
| 1576 | void AddressSanitizer::instrumentUnusualSizeOrAlignment( |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1577 | Instruction *I, Instruction *InsertBefore, Value *Addr, uint32_t TypeSize, |
| 1578 | bool IsWrite, Value *SizeArgument, bool UseCalls, uint32_t Exp) { |
| 1579 | IRBuilder<> IRB(InsertBefore); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1580 | Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8); |
| 1581 | Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); |
| 1582 | if (UseCalls) { |
| 1583 | if (Exp == 0) |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1584 | IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][0], |
| 1585 | {AddrLong, Size}); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1586 | else |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1587 | IRB.CreateCall(AsanMemoryAccessCallbackSized[IsWrite][1], |
| 1588 | {AddrLong, Size, ConstantInt::get(IRB.getInt32Ty(), Exp)}); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1589 | } else { |
| 1590 | Value *LastByte = IRB.CreateIntToPtr( |
| 1591 | IRB.CreateAdd(AddrLong, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)), |
| 1592 | Addr->getType()); |
Filipe Cabecinhas | 4647b74 | 2017-01-06 15:24:51 +0000 | [diff] [blame] | 1593 | instrumentAddress(I, InsertBefore, Addr, 8, IsWrite, Size, false, Exp); |
| 1594 | instrumentAddress(I, InsertBefore, LastByte, 8, IsWrite, Size, false, Exp); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 1598 | void AddressSanitizerModule::poisonOneInitializer(Function &GlobalInit, |
| 1599 | GlobalValue *ModuleName) { |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1600 | // Set up the arguments to our poison/unpoison functions. |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 1601 | IRBuilder<> IRB(&GlobalInit.front(), |
| 1602 | GlobalInit.front().getFirstInsertionPt()); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1603 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1604 | // 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] | 1605 | Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy); |
| 1606 | IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1607 | |
| 1608 | // Add calls to unpoison all globals before each return instruction. |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 1609 | for (auto &BB : GlobalInit.getBasicBlockList()) |
| 1610 | if (ReturnInst *RI = dyn_cast<ReturnInst>(BB.getTerminator())) |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1611 | CallInst::Create(AsanUnpoisonGlobals, "", RI); |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | void AddressSanitizerModule::createInitializerPoisonCalls( |
| 1615 | Module &M, GlobalValue *ModuleName) { |
| 1616 | GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors"); |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 1617 | if (!GV) |
| 1618 | return; |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 1619 | |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 1620 | ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 1621 | if (!CA) |
| 1622 | return; |
| 1623 | |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 1624 | for (Use &OP : CA->operands()) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1625 | if (isa<ConstantAggregateZero>(OP)) continue; |
Alexey Samsonov | 96e239f | 2014-05-29 00:51:15 +0000 | [diff] [blame] | 1626 | ConstantStruct *CS = cast<ConstantStruct>(OP); |
| 1627 | |
| 1628 | // Must have a function or null ptr. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1629 | if (Function *F = dyn_cast<Function>(CS->getOperand(1))) { |
Kostya Serebryany | 34ddf87 | 2014-09-24 22:41:55 +0000 | [diff] [blame] | 1630 | if (F->getName() == kAsanModuleCtorName) continue; |
| 1631 | ConstantInt *Priority = dyn_cast<ConstantInt>(CS->getOperand(0)); |
| 1632 | // Don't instrument CTORs that will run before asan.module_ctor. |
| 1633 | if (Priority->getLimitedValue() <= kAsanCtorAndDtorPriority) continue; |
| 1634 | poisonOneInitializer(*F, ModuleName); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1635 | } |
| 1636 | } |
| 1637 | } |
| 1638 | |
Kostya Serebryany | dfe9e79 | 2012-11-28 10:31:36 +0000 | [diff] [blame] | 1639 | bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) { |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 1640 | Type *Ty = G->getValueType(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1641 | LLVM_DEBUG(dbgs() << "GLOBAL: " << *G << "\n"); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1642 | |
Alexey Samsonov | 08f022a | 2014-07-11 22:36:02 +0000 | [diff] [blame] | 1643 | if (GlobalsMD.get(G).IsBlacklisted) return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1644 | if (!Ty->isSized()) return false; |
| 1645 | if (!G->hasInitializer()) return false; |
Vedant Kumar | f5ac6d4 | 2016-06-22 17:30:58 +0000 | [diff] [blame] | 1646 | if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals. |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1647 | // Two problems with thread-locals: |
| 1648 | // - The address of the main thread's copy can't be computed at link-time. |
| 1649 | // - Need to poison all copies, not just the main thread's one. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1650 | if (G->isThreadLocal()) return false; |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 1651 | // For now, just ignore this Global if the alignment is large. |
| 1652 | if (G->getAlignment() > MinRedzoneSizeForGlobal()) return false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1653 | |
Reid Kleckner | 85a8c12 | 2018-08-20 23:35:45 +0000 | [diff] [blame] | 1654 | // For non-COFF targets, only instrument globals known to be defined by this |
| 1655 | // TU. |
| 1656 | // FIXME: We can instrument comdat globals on ELF if we are using the |
| 1657 | // GC-friendly metadata scheme. |
| 1658 | if (!TargetTriple.isOSBinFormatCOFF()) { |
| 1659 | if (!G->hasExactDefinition() || G->hasComdat()) |
| 1660 | return false; |
| 1661 | } else { |
| 1662 | // On COFF, don't instrument non-ODR linkages. |
| 1663 | if (G->isInterposable()) |
| 1664 | return false; |
| 1665 | } |
| 1666 | |
| 1667 | // If a comdat is present, it must have a selection kind that implies ODR |
| 1668 | // semantics: no duplicates, any, or exact match. |
| 1669 | if (Comdat *C = G->getComdat()) { |
| 1670 | switch (C->getSelectionKind()) { |
| 1671 | case Comdat::Any: |
| 1672 | case Comdat::ExactMatch: |
| 1673 | case Comdat::NoDuplicates: |
| 1674 | break; |
| 1675 | case Comdat::Largest: |
| 1676 | case Comdat::SameSize: |
| 1677 | return false; |
| 1678 | } |
| 1679 | } |
| 1680 | |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1681 | if (G->hasSection()) { |
Rafael Espindola | 83658d6 | 2016-05-11 18:21:59 +0000 | [diff] [blame] | 1682 | StringRef Section = G->getSection(); |
Kuba Brecka | 086e34b | 2014-12-05 21:32:46 +0000 | [diff] [blame] | 1683 | |
Anna Zaks | 1190460 | 2015-06-09 00:58:08 +0000 | [diff] [blame] | 1684 | // Globals from llvm.metadata aren't emitted, do not instrument them. |
| 1685 | if (Section == "llvm.metadata") return false; |
Anna Zaks | 785c075 | 2015-06-25 23:35:48 +0000 | [diff] [blame] | 1686 | // Do not instrument globals from special LLVM sections. |
Anna Zaks | 40148f1 | 2016-02-24 22:12:18 +0000 | [diff] [blame] | 1687 | if (Section.find("__llvm") != StringRef::npos || Section.find("__LLVM") != StringRef::npos) return false; |
Anna Zaks | 1190460 | 2015-06-09 00:58:08 +0000 | [diff] [blame] | 1688 | |
Alexey Samsonov | c1603b6 | 2015-09-15 23:05:48 +0000 | [diff] [blame] | 1689 | // Do not instrument function pointers to initialization and termination |
| 1690 | // routines: dynamic linker will not properly handle redzones. |
| 1691 | if (Section.startswith(".preinit_array") || |
| 1692 | Section.startswith(".init_array") || |
| 1693 | Section.startswith(".fini_array")) { |
| 1694 | return false; |
| 1695 | } |
| 1696 | |
Reid Kleckner | 12395b7 | 2018-06-13 20:47:21 +0000 | [diff] [blame] | 1697 | // On COFF, if the section name contains '$', it is highly likely that the |
| 1698 | // user is using section sorting to create an array of globals similar to |
| 1699 | // the way initialization callbacks are registered in .init_array and |
| 1700 | // .CRT$XCU. The ATL also registers things in .ATL$__[azm]. Adding redzones |
| 1701 | // to such globals is counterproductive, because the intent is that they |
| 1702 | // will form an array, and out-of-bounds accesses are expected. |
Hans Wennborg | 08b34a0 | 2017-11-13 23:47:58 +0000 | [diff] [blame] | 1703 | // See https://github.com/google/sanitizers/issues/305 |
Anna Zaks | 1190460 | 2015-06-09 00:58:08 +0000 | [diff] [blame] | 1704 | // and http://msdn.microsoft.com/en-US/en-en/library/bb918180(v=vs.120).aspx |
Reid Kleckner | 12395b7 | 2018-06-13 20:47:21 +0000 | [diff] [blame] | 1705 | if (TargetTriple.isOSBinFormatCOFF() && Section.contains('$')) { |
| 1706 | LLVM_DEBUG(dbgs() << "Ignoring global in sorted section (contains '$'): " |
| 1707 | << *G << "\n"); |
Anna Zaks | 1190460 | 2015-06-09 00:58:08 +0000 | [diff] [blame] | 1708 | return false; |
| 1709 | } |
| 1710 | |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 1711 | if (TargetTriple.isOSBinFormatMachO()) { |
| 1712 | StringRef ParsedSegment, ParsedSection; |
| 1713 | unsigned TAA = 0, StubSize = 0; |
| 1714 | bool TAAParsed; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 1715 | std::string ErrorCode = MCSectionMachO::ParseSectionSpecifier( |
| 1716 | Section, ParsedSegment, ParsedSection, TAA, TAAParsed, StubSize); |
Davide Italiano | c807f48 | 2015-11-19 21:50:08 +0000 | [diff] [blame] | 1717 | assert(ErrorCode.empty() && "Invalid section specifier."); |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 1718 | |
| 1719 | // Ignore the globals from the __OBJC section. The ObjC runtime assumes |
| 1720 | // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to |
| 1721 | // them. |
| 1722 | if (ParsedSegment == "__OBJC" || |
| 1723 | (ParsedSegment == "__DATA" && ParsedSection.startswith("__objc_"))) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1724 | LLVM_DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G << "\n"); |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 1725 | return false; |
| 1726 | } |
Hans Wennborg | 08b34a0 | 2017-11-13 23:47:58 +0000 | [diff] [blame] | 1727 | // See https://github.com/google/sanitizers/issues/32 |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 1728 | // Constant CFString instances are compiled in the following way: |
| 1729 | // -- the string buffer is emitted into |
| 1730 | // __TEXT,__cstring,cstring_literals |
| 1731 | // -- the constant NSConstantString structure referencing that buffer |
| 1732 | // is placed into __DATA,__cfstring |
| 1733 | // Therefore there's no point in placing redzones into __DATA,__cfstring. |
| 1734 | // Moreover, it causes the linker to crash on OS X 10.7 |
| 1735 | if (ParsedSegment == "__DATA" && ParsedSection == "__cfstring") { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1736 | LLVM_DEBUG(dbgs() << "Ignoring CFString: " << *G << "\n"); |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 1737 | return false; |
| 1738 | } |
| 1739 | // The linker merges the contents of cstring_literals and removes the |
| 1740 | // trailing zeroes. |
| 1741 | if (ParsedSegment == "__TEXT" && (TAA & MachO::S_CSTRING_LITERALS)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1742 | LLVM_DEBUG(dbgs() << "Ignoring a cstring literal: " << *G << "\n"); |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 1743 | return false; |
| 1744 | } |
| 1745 | } |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | return true; |
| 1749 | } |
| 1750 | |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 1751 | // On Mach-O platforms, we emit global metadata in a separate section of the |
| 1752 | // binary in order to allow the linker to properly dead strip. This is only |
| 1753 | // supported on recent versions of ld64. |
| 1754 | bool AddressSanitizerModule::ShouldUseMachOGlobalsSection() const { |
| 1755 | if (!TargetTriple.isOSBinFormatMachO()) |
| 1756 | return false; |
| 1757 | |
| 1758 | if (TargetTriple.isMacOSX() && !TargetTriple.isMacOSXVersionLT(10, 11)) |
| 1759 | return true; |
| 1760 | if (TargetTriple.isiOS() /* or tvOS */ && !TargetTriple.isOSVersionLT(9)) |
Mike Aizatsky | 243b71f | 2016-04-21 22:00:13 +0000 | [diff] [blame] | 1761 | return true; |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 1762 | if (TargetTriple.isWatchOS() && !TargetTriple.isOSVersionLT(2)) |
| 1763 | return true; |
| 1764 | |
| 1765 | return false; |
| 1766 | } |
| 1767 | |
Reid Kleckner | 01660a3 | 2016-11-21 20:40:37 +0000 | [diff] [blame] | 1768 | StringRef AddressSanitizerModule::getGlobalMetadataSection() const { |
| 1769 | switch (TargetTriple.getObjectFormat()) { |
| 1770 | case Triple::COFF: return ".ASAN$GL"; |
| 1771 | case Triple::ELF: return "asan_globals"; |
| 1772 | case Triple::MachO: return "__DATA,__asan_globals,regular"; |
| 1773 | default: break; |
| 1774 | } |
| 1775 | llvm_unreachable("unsupported object format"); |
| 1776 | } |
| 1777 | |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1778 | void AddressSanitizerModule::initializeCallbacks(Module &M) { |
| 1779 | IRBuilder<> IRB(*C); |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 1780 | |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1781 | // Declare our poisoning and unpoisoning functions. |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 1782 | AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1783 | kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1784 | AsanPoisonGlobals->setLinkage(Function::ExternalLinkage); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 1785 | AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1786 | kAsanUnpoisonGlobalsName, IRB.getVoidTy())); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1787 | AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage); |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 1788 | |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1789 | // Declare functions that register/unregister globals. |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 1790 | AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1791 | kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1792 | AsanRegisterGlobals->setLinkage(Function::ExternalLinkage); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 1793 | AsanUnregisterGlobals = checkSanitizerInterfaceFunction( |
| 1794 | M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1795 | IntptrTy, IntptrTy)); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1796 | AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage); |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 1797 | |
| 1798 | // Declare the functions that find globals in a shared object and then invoke |
| 1799 | // the (un)register function on them. |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1800 | AsanRegisterImageGlobals = |
| 1801 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1802 | kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy)); |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 1803 | AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage); |
Mike Aizatsky | 243b71f | 2016-04-21 22:00:13 +0000 | [diff] [blame] | 1804 | |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1805 | AsanUnregisterImageGlobals = |
| 1806 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 1807 | kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy)); |
Ryan Govostes | 653f9d0 | 2016-03-28 20:28:57 +0000 | [diff] [blame] | 1808 | AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage); |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 1809 | |
| 1810 | AsanRegisterElfGlobals = checkSanitizerInterfaceFunction( |
| 1811 | M.getOrInsertFunction(kAsanRegisterElfGlobalsName, IRB.getVoidTy(), |
| 1812 | IntptrTy, IntptrTy, IntptrTy)); |
| 1813 | AsanRegisterElfGlobals->setLinkage(Function::ExternalLinkage); |
| 1814 | |
| 1815 | AsanUnregisterElfGlobals = checkSanitizerInterfaceFunction( |
| 1816 | M.getOrInsertFunction(kAsanUnregisterElfGlobalsName, IRB.getVoidTy(), |
| 1817 | IntptrTy, IntptrTy, IntptrTy)); |
| 1818 | AsanUnregisterElfGlobals->setLinkage(Function::ExternalLinkage); |
Alexey Samsonov | 788381b | 2012-12-25 12:28:20 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1821 | // Put the metadata and the instrumented global in the same group. This ensures |
| 1822 | // that the metadata is discarded if the instrumented global is discarded. |
| 1823 | void AddressSanitizerModule::SetComdatForGlobalMetadata( |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 1824 | GlobalVariable *G, GlobalVariable *Metadata, StringRef InternalSuffix) { |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1825 | Module &M = *G->getParent(); |
| 1826 | Comdat *C = G->getComdat(); |
| 1827 | if (!C) { |
| 1828 | if (!G->hasName()) { |
| 1829 | // If G is unnamed, it must be internal. Give it an artificial name |
| 1830 | // so we can put it in a comdat. |
| 1831 | assert(G->hasLocalLinkage()); |
| 1832 | G->setName(Twine(kAsanGenPrefix) + "_anon_global"); |
| 1833 | } |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 1834 | |
| 1835 | if (!InternalSuffix.empty() && G->hasLocalLinkage()) { |
| 1836 | std::string Name = G->getName(); |
| 1837 | Name += InternalSuffix; |
| 1838 | C = M.getOrInsertComdat(Name); |
| 1839 | } else { |
| 1840 | C = M.getOrInsertComdat(G->getName()); |
| 1841 | } |
| 1842 | |
Reid Kleckner | c212cc8 | 2017-10-31 16:16:08 +0000 | [diff] [blame] | 1843 | // Make this IMAGE_COMDAT_SELECT_NODUPLICATES on COFF. Also upgrade private |
| 1844 | // linkage to internal linkage so that a symbol table entry is emitted. This |
| 1845 | // is necessary in order to create the comdat group. |
| 1846 | if (TargetTriple.isOSBinFormatCOFF()) { |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1847 | C->setSelectionKind(Comdat::NoDuplicates); |
Reid Kleckner | c212cc8 | 2017-10-31 16:16:08 +0000 | [diff] [blame] | 1848 | if (G->hasPrivateLinkage()) |
| 1849 | G->setLinkage(GlobalValue::InternalLinkage); |
| 1850 | } |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1851 | G->setComdat(C); |
| 1852 | } |
| 1853 | |
| 1854 | assert(G->hasComdat()); |
| 1855 | Metadata->setComdat(G->getComdat()); |
| 1856 | } |
| 1857 | |
| 1858 | // Create a separate metadata global and put it in the appropriate ASan |
| 1859 | // global registration section. |
| 1860 | GlobalVariable * |
| 1861 | AddressSanitizerModule::CreateMetadataGlobal(Module &M, Constant *Initializer, |
| 1862 | StringRef OriginalName) { |
Evgeniy Stepanov | 90fd873 | 2017-04-11 22:28:13 +0000 | [diff] [blame] | 1863 | auto Linkage = TargetTriple.isOSBinFormatMachO() |
| 1864 | ? GlobalVariable::InternalLinkage |
| 1865 | : GlobalVariable::PrivateLinkage; |
| 1866 | GlobalVariable *Metadata = new GlobalVariable( |
| 1867 | M, Initializer->getType(), false, Linkage, Initializer, |
Peter Collingbourne | 6f0ecca | 2017-05-16 00:39:01 +0000 | [diff] [blame] | 1868 | Twine("__asan_global_") + GlobalValue::dropLLVMManglingEscape(OriginalName)); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1869 | Metadata->setSection(getGlobalMetadataSection()); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1870 | return Metadata; |
| 1871 | } |
| 1872 | |
| 1873 | IRBuilder<> AddressSanitizerModule::CreateAsanModuleDtor(Module &M) { |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 1874 | AsanDtorFunction = |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1875 | Function::Create(FunctionType::get(Type::getVoidTy(*C), false), |
| 1876 | GlobalValue::InternalLinkage, kAsanModuleDtorName, &M); |
| 1877 | BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1878 | |
| 1879 | return IRBuilder<>(ReturnInst::Create(*C, AsanDtorBB)); |
| 1880 | } |
| 1881 | |
| 1882 | void AddressSanitizerModule::InstrumentGlobalsCOFF( |
| 1883 | IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 1884 | ArrayRef<Constant *> MetadataInitializers) { |
| 1885 | assert(ExtendedGlobals.size() == MetadataInitializers.size()); |
Evgeniy Stepanov | f01c70f | 2017-01-12 23:26:20 +0000 | [diff] [blame] | 1886 | auto &DL = M.getDataLayout(); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1887 | |
| 1888 | for (size_t i = 0; i < ExtendedGlobals.size(); i++) { |
Evgeniy Stepanov | f01c70f | 2017-01-12 23:26:20 +0000 | [diff] [blame] | 1889 | Constant *Initializer = MetadataInitializers[i]; |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1890 | GlobalVariable *G = ExtendedGlobals[i]; |
| 1891 | GlobalVariable *Metadata = |
Evgeniy Stepanov | f01c70f | 2017-01-12 23:26:20 +0000 | [diff] [blame] | 1892 | CreateMetadataGlobal(M, Initializer, G->getName()); |
| 1893 | |
| 1894 | // The MSVC linker always inserts padding when linking incrementally. We |
| 1895 | // cope with that by aligning each struct to its size, which must be a power |
| 1896 | // of two. |
| 1897 | unsigned SizeOfGlobalStruct = DL.getTypeAllocSize(Initializer->getType()); |
| 1898 | assert(isPowerOf2_32(SizeOfGlobalStruct) && |
| 1899 | "global metadata will not be padded appropriately"); |
| 1900 | Metadata->setAlignment(SizeOfGlobalStruct); |
| 1901 | |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 1902 | SetComdatForGlobalMetadata(G, Metadata, ""); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 1906 | void AddressSanitizerModule::InstrumentGlobalsELF( |
| 1907 | IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 1908 | ArrayRef<Constant *> MetadataInitializers, |
| 1909 | const std::string &UniqueModuleId) { |
| 1910 | assert(ExtendedGlobals.size() == MetadataInitializers.size()); |
| 1911 | |
| 1912 | SmallVector<GlobalValue *, 16> MetadataGlobals(ExtendedGlobals.size()); |
| 1913 | for (size_t i = 0; i < ExtendedGlobals.size(); i++) { |
| 1914 | GlobalVariable *G = ExtendedGlobals[i]; |
| 1915 | GlobalVariable *Metadata = |
| 1916 | CreateMetadataGlobal(M, MetadataInitializers[i], G->getName()); |
| 1917 | MDNode *MD = MDNode::get(M.getContext(), ValueAsMetadata::get(G)); |
| 1918 | Metadata->setMetadata(LLVMContext::MD_associated, MD); |
| 1919 | MetadataGlobals[i] = Metadata; |
| 1920 | |
| 1921 | SetComdatForGlobalMetadata(G, Metadata, UniqueModuleId); |
| 1922 | } |
| 1923 | |
| 1924 | // Update llvm.compiler.used, adding the new metadata globals. This is |
| 1925 | // needed so that during LTO these variables stay alive. |
| 1926 | if (!MetadataGlobals.empty()) |
| 1927 | appendToCompilerUsed(M, MetadataGlobals); |
| 1928 | |
| 1929 | // RegisteredFlag serves two purposes. First, we can pass it to dladdr() |
| 1930 | // to look up the loaded image that contains it. Second, we can store in it |
| 1931 | // whether registration has already occurred, to prevent duplicate |
| 1932 | // registration. |
| 1933 | // |
| 1934 | // Common linkage ensures that there is only one global per shared library. |
| 1935 | GlobalVariable *RegisteredFlag = new GlobalVariable( |
| 1936 | M, IntptrTy, false, GlobalVariable::CommonLinkage, |
| 1937 | ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName); |
| 1938 | RegisteredFlag->setVisibility(GlobalVariable::HiddenVisibility); |
| 1939 | |
| 1940 | // Create start and stop symbols. |
| 1941 | GlobalVariable *StartELFMetadata = new GlobalVariable( |
| 1942 | M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr, |
| 1943 | "__start_" + getGlobalMetadataSection()); |
| 1944 | StartELFMetadata->setVisibility(GlobalVariable::HiddenVisibility); |
| 1945 | GlobalVariable *StopELFMetadata = new GlobalVariable( |
| 1946 | M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr, |
| 1947 | "__stop_" + getGlobalMetadataSection()); |
| 1948 | StopELFMetadata->setVisibility(GlobalVariable::HiddenVisibility); |
| 1949 | |
| 1950 | // Create a call to register the globals with the runtime. |
| 1951 | IRB.CreateCall(AsanRegisterElfGlobals, |
| 1952 | {IRB.CreatePointerCast(RegisteredFlag, IntptrTy), |
| 1953 | IRB.CreatePointerCast(StartELFMetadata, IntptrTy), |
| 1954 | IRB.CreatePointerCast(StopELFMetadata, IntptrTy)}); |
| 1955 | |
| 1956 | // We also need to unregister globals at the end, e.g., when a shared library |
| 1957 | // gets closed. |
| 1958 | IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M); |
| 1959 | IRB_Dtor.CreateCall(AsanUnregisterElfGlobals, |
| 1960 | {IRB.CreatePointerCast(RegisteredFlag, IntptrTy), |
| 1961 | IRB.CreatePointerCast(StartELFMetadata, IntptrTy), |
| 1962 | IRB.CreatePointerCast(StopELFMetadata, IntptrTy)}); |
| 1963 | } |
| 1964 | |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1965 | void AddressSanitizerModule::InstrumentGlobalsMachO( |
| 1966 | IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 1967 | ArrayRef<Constant *> MetadataInitializers) { |
| 1968 | assert(ExtendedGlobals.size() == MetadataInitializers.size()); |
| 1969 | |
| 1970 | // On recent Mach-O platforms, use a structure which binds the liveness of |
| 1971 | // the global variable to the metadata struct. Keep the list of "Liveness" GV |
| 1972 | // created to be added to llvm.compiler.used |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1973 | StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1974 | SmallVector<GlobalValue *, 16> LivenessGlobals(ExtendedGlobals.size()); |
| 1975 | |
| 1976 | for (size_t i = 0; i < ExtendedGlobals.size(); i++) { |
| 1977 | Constant *Initializer = MetadataInitializers[i]; |
| 1978 | GlobalVariable *G = ExtendedGlobals[i]; |
| 1979 | GlobalVariable *Metadata = |
| 1980 | CreateMetadataGlobal(M, Initializer, G->getName()); |
| 1981 | |
| 1982 | // On recent Mach-O platforms, we emit the global metadata in a way that |
| 1983 | // allows the linker to properly strip dead globals. |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 1984 | auto LivenessBinder = |
| 1985 | ConstantStruct::get(LivenessTy, Initializer->getAggregateElement(0u), |
| 1986 | ConstantExpr::getPointerCast(Metadata, IntptrTy)); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 1987 | GlobalVariable *Liveness = new GlobalVariable( |
| 1988 | M, LivenessTy, false, GlobalVariable::InternalLinkage, LivenessBinder, |
| 1989 | Twine("__asan_binder_") + G->getName()); |
| 1990 | Liveness->setSection("__DATA,__asan_liveness,regular,live_support"); |
| 1991 | LivenessGlobals[i] = Liveness; |
| 1992 | } |
| 1993 | |
| 1994 | // Update llvm.compiler.used, adding the new liveness globals. This is |
| 1995 | // needed so that during LTO these variables stay alive. The alternative |
| 1996 | // would be to have the linker handling the LTO symbols, but libLTO |
| 1997 | // current API does not expose access to the section for each symbol. |
| 1998 | if (!LivenessGlobals.empty()) |
| 1999 | appendToCompilerUsed(M, LivenessGlobals); |
| 2000 | |
| 2001 | // RegisteredFlag serves two purposes. First, we can pass it to dladdr() |
| 2002 | // to look up the loaded image that contains it. Second, we can store in it |
| 2003 | // whether registration has already occurred, to prevent duplicate |
| 2004 | // registration. |
| 2005 | // |
| 2006 | // common linkage ensures that there is only one global per shared library. |
| 2007 | GlobalVariable *RegisteredFlag = new GlobalVariable( |
| 2008 | M, IntptrTy, false, GlobalVariable::CommonLinkage, |
| 2009 | ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName); |
| 2010 | RegisteredFlag->setVisibility(GlobalVariable::HiddenVisibility); |
| 2011 | |
| 2012 | IRB.CreateCall(AsanRegisterImageGlobals, |
| 2013 | {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)}); |
| 2014 | |
| 2015 | // We also need to unregister globals at the end, e.g., when a shared library |
| 2016 | // gets closed. |
| 2017 | IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M); |
| 2018 | IRB_Dtor.CreateCall(AsanUnregisterImageGlobals, |
| 2019 | {IRB.CreatePointerCast(RegisteredFlag, IntptrTy)}); |
| 2020 | } |
| 2021 | |
| 2022 | void AddressSanitizerModule::InstrumentGlobalsWithMetadataArray( |
| 2023 | IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals, |
| 2024 | ArrayRef<Constant *> MetadataInitializers) { |
| 2025 | assert(ExtendedGlobals.size() == MetadataInitializers.size()); |
| 2026 | unsigned N = ExtendedGlobals.size(); |
| 2027 | assert(N > 0); |
| 2028 | |
| 2029 | // On platforms that don't have a custom metadata section, we emit an array |
| 2030 | // of global metadata structures. |
| 2031 | ArrayType *ArrayOfGlobalStructTy = |
| 2032 | ArrayType::get(MetadataInitializers[0]->getType(), N); |
| 2033 | auto AllGlobals = new GlobalVariable( |
| 2034 | M, ArrayOfGlobalStructTy, false, GlobalVariable::InternalLinkage, |
| 2035 | ConstantArray::get(ArrayOfGlobalStructTy, MetadataInitializers), ""); |
Walter Lee | 2a2b69e | 2017-11-16 12:57:19 +0000 | [diff] [blame] | 2036 | if (Mapping.Scale > 3) |
| 2037 | AllGlobals->setAlignment(1ULL << Mapping.Scale); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 2038 | |
| 2039 | IRB.CreateCall(AsanRegisterGlobals, |
| 2040 | {IRB.CreatePointerCast(AllGlobals, IntptrTy), |
| 2041 | ConstantInt::get(IntptrTy, N)}); |
| 2042 | |
| 2043 | // We also need to unregister globals at the end, e.g., when a shared library |
| 2044 | // gets closed. |
| 2045 | IRBuilder<> IRB_Dtor = CreateAsanModuleDtor(M); |
| 2046 | IRB_Dtor.CreateCall(AsanUnregisterGlobals, |
| 2047 | {IRB.CreatePointerCast(AllGlobals, IntptrTy), |
| 2048 | ConstantInt::get(IntptrTy, N)}); |
| 2049 | } |
| 2050 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2051 | // This function replaces all global variables with new variables that have |
| 2052 | // trailing redzones. It also creates a function that poisons |
| 2053 | // redzones and inserts this function into llvm.global_ctors. |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 2054 | // Sets *CtorComdat to true if the global registration code emitted into the |
| 2055 | // asan constructor is comdat-compatible. |
| 2056 | bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool *CtorComdat) { |
| 2057 | *CtorComdat = false; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 2058 | GlobalsMD.init(M); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 2059 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2060 | SmallVector<GlobalVariable *, 16> GlobalsToChange; |
| 2061 | |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2062 | for (auto &G : M.globals()) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2063 | if (ShouldInstrumentGlobal(&G)) GlobalsToChange.push_back(&G); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | size_t n = GlobalsToChange.size(); |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 2067 | if (n == 0) { |
| 2068 | *CtorComdat = true; |
| 2069 | return false; |
| 2070 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2071 | |
Reid Kleckner | 7856583 | 2016-11-29 01:32:21 +0000 | [diff] [blame] | 2072 | auto &DL = M.getDataLayout(); |
Reid Kleckner | 01660a3 | 2016-11-21 20:40:37 +0000 | [diff] [blame] | 2073 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2074 | // A global is described by a structure |
| 2075 | // size_t beg; |
| 2076 | // size_t size; |
| 2077 | // size_t size_with_redzone; |
| 2078 | // const char *name; |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 2079 | // const char *module_name; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 2080 | // size_t has_dynamic_init; |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 2081 | // void *source_location; |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 2082 | // size_t odr_indicator; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2083 | // We initialize an array of such structures and pass it to a run-time call. |
Alexey Samsonov | 4f319cc | 2014-07-02 16:54:41 +0000 | [diff] [blame] | 2084 | StructType *GlobalStructTy = |
| 2085 | StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy, |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 2086 | IntptrTy, IntptrTy, IntptrTy); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 2087 | SmallVector<GlobalVariable *, 16> NewGlobals(n); |
| 2088 | SmallVector<Constant *, 16> Initializers(n); |
Kostya Serebryany | 20a7997 | 2012-11-22 03:18:50 +0000 | [diff] [blame] | 2089 | |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 2090 | bool HasDynamicallyInitializedGlobals = false; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 2091 | |
Alexey Samsonov | e1e26bf | 2013-03-26 13:05:41 +0000 | [diff] [blame] | 2092 | // We shouldn't merge same module names, as this string serves as unique |
| 2093 | // module ID in runtime. |
Alexander Potapenko | daf96ae | 2013-12-25 14:22:15 +0000 | [diff] [blame] | 2094 | GlobalVariable *ModuleName = createPrivateGlobalForString( |
Kostya Serebryany | d891ac9 | 2018-10-11 23:03:27 +0000 | [diff] [blame] | 2095 | M, M.getModuleIdentifier(), /*AllowMerging*/ false, kAsanGenPrefix); |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 2096 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2097 | for (size_t i = 0; i < n; i++) { |
Kostya Serebryany | e35d59a | 2013-01-24 10:43:50 +0000 | [diff] [blame] | 2098 | static const uint64_t kMaxGlobalRedzone = 1 << 18; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2099 | GlobalVariable *G = GlobalsToChange[i]; |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 2100 | |
| 2101 | auto MD = GlobalsMD.get(G); |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 2102 | StringRef NameForGlobal = G->getName(); |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 2103 | // Create string holding the global name (use global name from metadata |
| 2104 | // if it's available, otherwise just write the name of global variable). |
| 2105 | GlobalVariable *Name = createPrivateGlobalForString( |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 2106 | M, MD.Name.empty() ? NameForGlobal : MD.Name, |
Kostya Serebryany | d891ac9 | 2018-10-11 23:03:27 +0000 | [diff] [blame] | 2107 | /*AllowMerging*/ true, kAsanGenPrefix); |
Alexey Samsonov | 15c9669 | 2014-07-12 00:42:52 +0000 | [diff] [blame] | 2108 | |
Manuel Jacob | 5f6eaac | 2016-01-16 20:30:46 +0000 | [diff] [blame] | 2109 | Type *Ty = G->getValueType(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2110 | uint64_t SizeInBytes = DL.getTypeAllocSize(Ty); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 2111 | uint64_t MinRZ = MinRedzoneSizeForGlobal(); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 2112 | // MinRZ <= RZ <= kMaxGlobalRedzone |
| 2113 | // and trying to make RZ to be ~ 1/4 of SizeInBytes. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2114 | uint64_t RZ = std::max( |
| 2115 | MinRZ, std::min(kMaxGlobalRedzone, (SizeInBytes / MinRZ / 4) * MinRZ)); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 2116 | uint64_t RightRedzoneSize = RZ; |
| 2117 | // Round up to MinRZ |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2118 | if (SizeInBytes % MinRZ) RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 2119 | assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2120 | Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize); |
| 2121 | |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 2122 | StructType *NewTy = StructType::get(Ty, RightRedZoneTy); |
| 2123 | Constant *NewInitializer = ConstantStruct::get( |
| 2124 | NewTy, G->getInitializer(), Constant::getNullValue(RightRedZoneTy)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2125 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2126 | // Create a new global variable with enough space for a redzone. |
Bill Wendling | 58f8cef | 2013-08-06 22:52:42 +0000 | [diff] [blame] | 2127 | GlobalValue::LinkageTypes Linkage = G->getLinkage(); |
| 2128 | if (G->isConstant() && Linkage == GlobalValue::PrivateLinkage) |
| 2129 | Linkage = GlobalValue::InternalLinkage; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2130 | GlobalVariable *NewGlobal = |
| 2131 | new GlobalVariable(M, NewTy, G->isConstant(), Linkage, NewInitializer, |
| 2132 | "", G, G->getThreadLocalMode()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2133 | NewGlobal->copyAttributesFrom(G); |
Reid Kleckner | 85a8c12 | 2018-08-20 23:35:45 +0000 | [diff] [blame] | 2134 | NewGlobal->setComdat(G->getComdat()); |
Kostya Serebryany | 87191f6 | 2013-01-24 10:35:40 +0000 | [diff] [blame] | 2135 | NewGlobal->setAlignment(MinRZ); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2136 | |
Kuba Brecka | a28c9e8 | 2016-10-31 18:51:58 +0000 | [diff] [blame] | 2137 | // Move null-terminated C strings to "__asan_cstring" section on Darwin. |
| 2138 | if (TargetTriple.isOSBinFormatMachO() && !G->hasSection() && |
| 2139 | G->isConstant()) { |
| 2140 | auto Seq = dyn_cast<ConstantDataSequential>(G->getInitializer()); |
| 2141 | if (Seq && Seq->isCString()) |
| 2142 | NewGlobal->setSection("__TEXT,__asan_cstring,regular"); |
| 2143 | } |
| 2144 | |
Adrian Prantl | 12fa3b3 | 2016-09-20 18:28:42 +0000 | [diff] [blame] | 2145 | // Transfer the debug info. The payload starts at offset zero so we can |
| 2146 | // copy the debug info over as is. |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 2147 | SmallVector<DIGlobalVariableExpression *, 1> GVs; |
Adrian Prantl | 12fa3b3 | 2016-09-20 18:28:42 +0000 | [diff] [blame] | 2148 | G->getDebugInfo(GVs); |
| 2149 | for (auto *GV : GVs) |
| 2150 | NewGlobal->addDebugInfo(GV); |
| 2151 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2152 | Value *Indices2[2]; |
| 2153 | Indices2[0] = IRB.getInt32(0); |
| 2154 | Indices2[1] = IRB.getInt32(0); |
| 2155 | |
| 2156 | G->replaceAllUsesWith( |
David Blaikie | 4a2e73b | 2015-04-02 18:55:32 +0000 | [diff] [blame] | 2157 | ConstantExpr::getGetElementPtr(NewTy, NewGlobal, Indices2, true)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2158 | NewGlobal->takeName(G); |
| 2159 | G->eraseFromParent(); |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 2160 | NewGlobals[i] = NewGlobal; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2161 | |
Alexey Samsonov | d9ad5ce | 2014-08-02 00:35:50 +0000 | [diff] [blame] | 2162 | Constant *SourceLoc; |
| 2163 | if (!MD.SourceLoc.empty()) { |
| 2164 | auto SourceLocGlobal = createPrivateGlobalForSourceLoc(M, MD.SourceLoc); |
| 2165 | SourceLoc = ConstantExpr::getPointerCast(SourceLocGlobal, IntptrTy); |
| 2166 | } else { |
| 2167 | SourceLoc = ConstantInt::get(IntptrTy, 0); |
| 2168 | } |
| 2169 | |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 2170 | Constant *ODRIndicator = ConstantExpr::getNullValue(IRB.getInt8PtrTy()); |
| 2171 | GlobalValue *InstrumentedGlobal = NewGlobal; |
| 2172 | |
Kuba Brecka | a1ea64a | 2016-09-14 14:06:33 +0000 | [diff] [blame] | 2173 | bool CanUsePrivateAliases = |
Dan Gohman | 1209c7a | 2017-01-17 20:34:09 +0000 | [diff] [blame] | 2174 | TargetTriple.isOSBinFormatELF() || TargetTriple.isOSBinFormatMachO() || |
| 2175 | TargetTriple.isOSBinFormatWasm(); |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 2176 | if (CanUsePrivateAliases && ClUsePrivateAliasForGlobals) { |
| 2177 | // Create local alias for NewGlobal to avoid crash on ODR between |
| 2178 | // instrumented and non-instrumented libraries. |
| 2179 | auto *GA = GlobalAlias::create(GlobalValue::InternalLinkage, |
| 2180 | NameForGlobal + M.getName(), NewGlobal); |
| 2181 | |
| 2182 | // With local aliases, we need to provide another externally visible |
| 2183 | // symbol __odr_asan_XXX to detect ODR violation. |
| 2184 | auto *ODRIndicatorSym = |
| 2185 | new GlobalVariable(M, IRB.getInt8Ty(), false, Linkage, |
| 2186 | Constant::getNullValue(IRB.getInt8Ty()), |
| 2187 | kODRGenPrefix + NameForGlobal, nullptr, |
| 2188 | NewGlobal->getThreadLocalMode()); |
| 2189 | |
| 2190 | // Set meaningful attributes for indicator symbol. |
| 2191 | ODRIndicatorSym->setVisibility(NewGlobal->getVisibility()); |
| 2192 | ODRIndicatorSym->setDLLStorageClass(NewGlobal->getDLLStorageClass()); |
| 2193 | ODRIndicatorSym->setAlignment(1); |
| 2194 | ODRIndicator = ODRIndicatorSym; |
| 2195 | InstrumentedGlobal = GA; |
| 2196 | } |
| 2197 | |
Reid Kleckner | 01660a3 | 2016-11-21 20:40:37 +0000 | [diff] [blame] | 2198 | Constant *Initializer = ConstantStruct::get( |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 2199 | GlobalStructTy, |
| 2200 | ConstantExpr::getPointerCast(InstrumentedGlobal, IntptrTy), |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2201 | ConstantInt::get(IntptrTy, SizeInBytes), |
| 2202 | ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize), |
| 2203 | ConstantExpr::getPointerCast(Name, IntptrTy), |
Kostya Serebryany | bd016bb | 2013-03-18 08:05:29 +0000 | [diff] [blame] | 2204 | ConstantExpr::getPointerCast(ModuleName, IntptrTy), |
Maxim Ostapenko | b1e3f60 | 2016-02-08 08:30:57 +0000 | [diff] [blame] | 2205 | ConstantInt::get(IntptrTy, MD.IsDynInit), SourceLoc, |
Serge Guelton | e38003f | 2017-05-09 19:31:13 +0000 | [diff] [blame] | 2206 | ConstantExpr::getPointerCast(ODRIndicator, IntptrTy)); |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 2207 | |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2208 | if (ClInitializers && MD.IsDynInit) HasDynamicallyInitializedGlobals = true; |
Kostya Serebryany | f4be019 | 2012-08-21 08:24:25 +0000 | [diff] [blame] | 2209 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2210 | LLVM_DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n"); |
Reid Kleckner | 01660a3 | 2016-11-21 20:40:37 +0000 | [diff] [blame] | 2211 | |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 2212 | Initializers[i] = Initializer; |
| 2213 | } |
Reid Kleckner | 01660a3 | 2016-11-21 20:40:37 +0000 | [diff] [blame] | 2214 | |
Kuba Mracek | 8842da8 | 2018-03-08 21:02:18 +0000 | [diff] [blame] | 2215 | // Add instrumented globals to llvm.compiler.used list to avoid LTO from |
| 2216 | // ConstantMerge'ing them. |
| 2217 | SmallVector<GlobalValue *, 16> GlobalsToAddToUsedList; |
| 2218 | for (size_t i = 0; i < n; i++) { |
| 2219 | GlobalVariable *G = NewGlobals[i]; |
| 2220 | if (G->getName().empty()) continue; |
| 2221 | GlobalsToAddToUsedList.push_back(G); |
| 2222 | } |
| 2223 | appendToCompilerUsed(M, ArrayRef<GlobalValue *>(GlobalsToAddToUsedList)); |
| 2224 | |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 2225 | std::string ELFUniqueModuleId = |
| 2226 | (UseGlobalsGC && TargetTriple.isOSBinFormatELF()) ? getUniqueModuleId(&M) |
| 2227 | : ""; |
| 2228 | |
| 2229 | if (!ELFUniqueModuleId.empty()) { |
| 2230 | InstrumentGlobalsELF(IRB, M, NewGlobals, Initializers, ELFUniqueModuleId); |
| 2231 | *CtorComdat = true; |
| 2232 | } else if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF()) { |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 2233 | InstrumentGlobalsCOFF(IRB, M, NewGlobals, Initializers); |
Evgeniy Stepanov | 9e53608 | 2017-04-24 19:34:13 +0000 | [diff] [blame] | 2234 | } else if (UseGlobalsGC && ShouldUseMachOGlobalsSection()) { |
Evgeniy Stepanov | 5d31d08 | 2017-01-12 23:03:03 +0000 | [diff] [blame] | 2235 | InstrumentGlobalsMachO(IRB, M, NewGlobals, Initializers); |
| 2236 | } else { |
| 2237 | InstrumentGlobalsWithMetadataArray(IRB, M, NewGlobals, Initializers); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
Reid Kleckner | 01660a3 | 2016-11-21 20:40:37 +0000 | [diff] [blame] | 2240 | // Create calls for poisoning before initializers run and unpoisoning after. |
| 2241 | if (HasDynamicallyInitializedGlobals) |
| 2242 | createInitializerPoisonCalls(M, ModuleName); |
| 2243 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2244 | LLVM_DEBUG(dbgs() << M); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2245 | return true; |
| 2246 | } |
| 2247 | |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 2248 | int AddressSanitizerModule::GetAsanVersion(const Module &M) const { |
| 2249 | int LongSize = M.getDataLayout().getPointerSizeInBits(); |
| 2250 | bool isAndroid = Triple(M.getTargetTriple()).isAndroid(); |
| 2251 | int Version = 8; |
| 2252 | // 32-bit Android is one version ahead because of the switch to dynamic |
| 2253 | // shadow. |
| 2254 | Version += (LongSize == 32 && isAndroid); |
| 2255 | return Version; |
| 2256 | } |
| 2257 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 2258 | bool AddressSanitizerModule::runOnModule(Module &M) { |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 2259 | C = &(M.getContext()); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2260 | int LongSize = M.getDataLayout().getPointerSizeInBits(); |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 2261 | IntptrTy = Type::getIntNTy(*C, LongSize); |
Kuba Brecka | 1001bb5 | 2014-12-05 22:19:18 +0000 | [diff] [blame] | 2262 | TargetTriple = Triple(M.getTargetTriple()); |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2263 | Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel); |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 2264 | initializeCallbacks(M); |
| 2265 | |
Evgeniy Stepanov | 039af60 | 2017-04-06 19:55:09 +0000 | [diff] [blame] | 2266 | if (CompileKernel) |
| 2267 | return false; |
Alex Shlyapnikov | bbd5cc6 | 2017-03-27 23:11:50 +0000 | [diff] [blame] | 2268 | |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 2269 | // Create a module constructor. A destructor is created lazily because not all |
| 2270 | // platforms, and not all modules need it. |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 2271 | std::string VersionCheckName = |
| 2272 | kAsanVersionCheckNamePrefix + std::to_string(GetAsanVersion(M)); |
Evgeniy Stepanov | 039af60 | 2017-04-06 19:55:09 +0000 | [diff] [blame] | 2273 | std::tie(AsanCtorFunction, std::ignore) = createSanitizerCtorAndInitFunctions( |
| 2274 | M, kAsanModuleCtorName, kAsanInitName, /*InitArgTypes=*/{}, |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 2275 | /*InitArgs=*/{}, VersionCheckName); |
Evgeniy Stepanov | 039af60 | 2017-04-06 19:55:09 +0000 | [diff] [blame] | 2276 | |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 2277 | bool CtorComdat = true; |
Evgeniy Stepanov | 039af60 | 2017-04-06 19:55:09 +0000 | [diff] [blame] | 2278 | bool Changed = false; |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2279 | // TODO(glider): temporarily disabled globals instrumentation for KASan. |
Evgeniy Stepanov | 039af60 | 2017-04-06 19:55:09 +0000 | [diff] [blame] | 2280 | if (ClGlobals) { |
| 2281 | IRBuilder<> IRB(AsanCtorFunction->getEntryBlock().getTerminator()); |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 2282 | Changed |= InstrumentGlobals(IRB, M, &CtorComdat); |
| 2283 | } |
| 2284 | |
| 2285 | // Put the constructor and destructor in comdat if both |
| 2286 | // (1) global instrumentation is not TU-specific |
| 2287 | // (2) target is ELF. |
Evgeniy Stepanov | b56012b | 2017-05-15 20:43:42 +0000 | [diff] [blame] | 2288 | if (UseCtorComdat && TargetTriple.isOSBinFormatELF() && CtorComdat) { |
Evgeniy Stepanov | 716f0ff22 | 2017-04-27 20:27:23 +0000 | [diff] [blame] | 2289 | AsanCtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleCtorName)); |
| 2290 | appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority, |
| 2291 | AsanCtorFunction); |
| 2292 | if (AsanDtorFunction) { |
| 2293 | AsanDtorFunction->setComdat(M.getOrInsertComdat(kAsanModuleDtorName)); |
| 2294 | appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority, |
| 2295 | AsanDtorFunction); |
| 2296 | } |
| 2297 | } else { |
| 2298 | appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndDtorPriority); |
| 2299 | if (AsanDtorFunction) |
| 2300 | appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndDtorPriority); |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2301 | } |
Evgeniy Stepanov | 19f75fc | 2014-06-03 14:16:00 +0000 | [diff] [blame] | 2302 | |
| 2303 | return Changed; |
| 2304 | } |
| 2305 | |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 2306 | void AddressSanitizer::initializeCallbacks(Module &M) { |
| 2307 | IRBuilder<> IRB(*C); |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 2308 | // Create __asan_report* callbacks. |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 2309 | // IsWrite, TypeSize and Exp are encoded in the function name. |
| 2310 | for (int Exp = 0; Exp < 2; Exp++) { |
| 2311 | for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) { |
| 2312 | const std::string TypeStr = AccessIsWrite ? "store" : "load"; |
| 2313 | const std::string ExpStr = Exp ? "exp_" : ""; |
Yury Gribov | d773198 | 2015-11-11 10:36:49 +0000 | [diff] [blame] | 2314 | const std::string EndingStr = Recover ? "_noabort" : ""; |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2315 | |
| 2316 | SmallVector<Type *, 3> Args2 = {IntptrTy, IntptrTy}; |
| 2317 | SmallVector<Type *, 2> Args1{1, IntptrTy}; |
| 2318 | if (Exp) { |
| 2319 | Type *ExpType = Type::getInt32Ty(*C); |
| 2320 | Args2.push_back(ExpType); |
| 2321 | Args1.push_back(ExpType); |
Dmitry Vyukov | 618d580 | 2015-03-17 16:59:19 +0000 | [diff] [blame] | 2322 | } |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 2323 | AsanErrorCallbackSized[AccessIsWrite][Exp] = |
| 2324 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Evgeniy Stepanov | 31475a0 | 2018-01-25 21:28:51 +0000 | [diff] [blame] | 2325 | kAsanReportErrorTemplate + ExpStr + TypeStr + "_n" + EndingStr, |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 2326 | FunctionType::get(IRB.getVoidTy(), Args2, false))); |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2327 | |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 2328 | AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] = |
| 2329 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 2330 | ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr, |
| 2331 | FunctionType::get(IRB.getVoidTy(), Args2, false))); |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2332 | |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 2333 | for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; |
| 2334 | AccessSizeIndex++) { |
| 2335 | const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex); |
| 2336 | AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] = |
| 2337 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 2338 | kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr, |
| 2339 | FunctionType::get(IRB.getVoidTy(), Args1, false))); |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2340 | |
NAKAMURA Takumi | a1e97a7 | 2017-08-28 06:47:47 +0000 | [diff] [blame] | 2341 | AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] = |
| 2342 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
| 2343 | ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr, |
| 2344 | FunctionType::get(IRB.getVoidTy(), Args1, false))); |
| 2345 | } |
| 2346 | } |
Kostya Serebryany | 4273bb0 | 2012-07-16 14:09:42 +0000 | [diff] [blame] | 2347 | } |
Kostya Serebryany | 86332c0 | 2014-04-21 07:10:43 +0000 | [diff] [blame] | 2348 | |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2349 | const std::string MemIntrinCallbackPrefix = |
| 2350 | CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix; |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 2351 | AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2352 | MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2353 | IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy)); |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 2354 | AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2355 | MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2356 | IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy)); |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 2357 | AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2358 | MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2359 | IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy)); |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 2360 | |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 2361 | AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2362 | M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy())); |
Kostya Serebryany | 4f8f0c5 | 2014-10-27 18:13:56 +0000 | [diff] [blame] | 2363 | |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 2364 | AsanPtrCmpFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2365 | kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy)); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 2366 | AsanPtrSubFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2367 | kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy)); |
Kostya Serebryany | f02c606 | 2012-07-20 09:54:50 +0000 | [diff] [blame] | 2368 | // We insert an empty inline asm after __asan_report* to avoid callback merge. |
| 2369 | EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), |
| 2370 | StringRef(""), StringRef(""), |
| 2371 | /*hasSideEffects=*/true); |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 2372 | if (Mapping.InGlobal) |
| 2373 | AsanShadowGlobal = M.getOrInsertGlobal("__asan_shadow", |
| 2374 | ArrayType::get(IRB.getInt8Ty(), 0)); |
Kostya Serebryany | 4b929da | 2012-11-29 09:54:21 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 2377 | // virtual |
| 2378 | bool AddressSanitizer::doInitialization(Module &M) { |
| 2379 | // Initialize the private fields. No one has accessed them before. |
| 2380 | GlobalsMD.init(M); |
| 2381 | |
| 2382 | C = &(M.getContext()); |
| 2383 | LongSize = M.getDataLayout().getPointerSizeInBits(); |
| 2384 | IntptrTy = Type::getIntNTy(*C, LongSize); |
| 2385 | TargetTriple = Triple(M.getTargetTriple()); |
| 2386 | |
| 2387 | Mapping = getShadowMapping(TargetTriple, LongSize, CompileKernel); |
| 2388 | return true; |
| 2389 | } |
| 2390 | |
| 2391 | bool AddressSanitizer::doFinalization(Module &M) { |
| 2392 | GlobalsMD.reset(); |
| 2393 | return false; |
| 2394 | } |
| 2395 | |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 2396 | bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) { |
| 2397 | // For each NSObject descendant having a +load method, this method is invoked |
| 2398 | // by the ObjC runtime before any of the static constructors is called. |
| 2399 | // Therefore we need to instrument such methods with a call to __asan_init |
| 2400 | // at the beginning in order to initialize our runtime before any access to |
| 2401 | // the shadow memory. |
| 2402 | // We cannot just ignore these methods, because they may call other |
| 2403 | // instrumented functions. |
| 2404 | if (F.getName().find(" load]") != std::string::npos) { |
Evgeniy Stepanov | 039af60 | 2017-04-06 19:55:09 +0000 | [diff] [blame] | 2405 | Function *AsanInitFunction = |
| 2406 | declareSanitizerInitFunction(*F.getParent(), kAsanInitName, {}); |
Duncan P. N. Exon Smith | e82c286 | 2015-10-13 17:39:10 +0000 | [diff] [blame] | 2407 | IRBuilder<> IRB(&F.front(), F.front().begin()); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 2408 | IRB.CreateCall(AsanInitFunction, {}); |
Kostya Serebryany | 22ddcfd | 2012-01-30 23:50:10 +0000 | [diff] [blame] | 2409 | return true; |
| 2410 | } |
| 2411 | return false; |
| 2412 | } |
| 2413 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 2414 | void AddressSanitizer::maybeInsertDynamicShadowAtFunctionEntry(Function &F) { |
| 2415 | // Generate code only when dynamic addressing is needed. |
Evgeniy Stepanov | cff19ee | 2017-11-15 00:11:51 +0000 | [diff] [blame] | 2416 | if (Mapping.Offset != kDynamicShadowSentinel) |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 2417 | return; |
| 2418 | |
| 2419 | IRBuilder<> IRB(&F.front().front()); |
Evgeniy Stepanov | 8e7018d | 2017-11-20 17:41:57 +0000 | [diff] [blame] | 2420 | if (Mapping.InGlobal) { |
| 2421 | if (ClWithIfuncSuppressRemat) { |
| 2422 | // An empty inline asm with input reg == output reg. |
| 2423 | // An opaque pointer-to-int cast, basically. |
| 2424 | InlineAsm *Asm = InlineAsm::get( |
| 2425 | FunctionType::get(IntptrTy, {AsanShadowGlobal->getType()}, false), |
| 2426 | StringRef(""), StringRef("=r,0"), |
| 2427 | /*hasSideEffects=*/false); |
| 2428 | LocalDynamicShadow = |
| 2429 | IRB.CreateCall(Asm, {AsanShadowGlobal}, ".asan.shadow"); |
| 2430 | } else { |
| 2431 | LocalDynamicShadow = |
| 2432 | IRB.CreatePointerCast(AsanShadowGlobal, IntptrTy, ".asan.shadow"); |
| 2433 | } |
| 2434 | } else { |
| 2435 | Value *GlobalDynamicAddress = F.getParent()->getOrInsertGlobal( |
| 2436 | kAsanShadowMemoryDynamicAddress, IntptrTy); |
| 2437 | LocalDynamicShadow = IRB.CreateLoad(GlobalDynamicAddress); |
| 2438 | } |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 2441 | void AddressSanitizer::markEscapedLocalAllocas(Function &F) { |
| 2442 | // Find the one possible call to llvm.localescape and pre-mark allocas passed |
| 2443 | // to it as uninteresting. This assumes we haven't started processing allocas |
| 2444 | // yet. This check is done up front because iterating the use list in |
| 2445 | // isInterestingAlloca would be algorithmically slower. |
| 2446 | assert(ProcessedAllocas.empty() && "must process localescape before allocas"); |
| 2447 | |
| 2448 | // Try to get the declaration of llvm.localescape. If it's not in the module, |
| 2449 | // we can exit early. |
| 2450 | if (!F.getParent()->getFunction("llvm.localescape")) return; |
| 2451 | |
| 2452 | // Look for a call to llvm.localescape call in the entry block. It can't be in |
| 2453 | // any other block. |
| 2454 | for (Instruction &I : F.getEntryBlock()) { |
| 2455 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I); |
| 2456 | if (II && II->getIntrinsicID() == Intrinsic::localescape) { |
| 2457 | // We found a call. Mark all the allocas passed in as uninteresting. |
| 2458 | for (Value *Arg : II->arg_operands()) { |
| 2459 | AllocaInst *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts()); |
| 2460 | assert(AI && AI->isStaticAlloca() && |
| 2461 | "non-static alloca arg to localescape"); |
| 2462 | ProcessedAllocas[AI] = false; |
| 2463 | } |
| 2464 | break; |
| 2465 | } |
| 2466 | } |
| 2467 | } |
| 2468 | |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 2469 | bool AddressSanitizer::runOnFunction(Function &F) { |
Kostya Serebryany | 6b5b58d | 2013-03-18 07:33:49 +0000 | [diff] [blame] | 2470 | if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false; |
Etienne Bergeron | 752f883 | 2016-09-14 17:18:37 +0000 | [diff] [blame] | 2471 | if (!ClDebugFunc.empty() && ClDebugFunc == F.getName()) return false; |
Etienne Bergeron | 52e4743 | 2016-09-15 15:35:59 +0000 | [diff] [blame] | 2472 | if (F.getName().startswith("__asan_")) return false; |
Yury Gribov | 3ae427d | 2014-12-01 08:47:58 +0000 | [diff] [blame] | 2473 | |
Etienne Bergeron | 78582b2 | 2016-09-15 15:45:05 +0000 | [diff] [blame] | 2474 | bool FunctionModified = false; |
| 2475 | |
Kostya Serebryany | cf880b9 | 2013-02-26 06:58:09 +0000 | [diff] [blame] | 2476 | // If needed, insert __asan_init before checking for SanitizeAddress attr. |
Etienne Bergeron | 752f883 | 2016-09-14 17:18:37 +0000 | [diff] [blame] | 2477 | // This function needs to be called even if the function body is not |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2478 | // instrumented. |
Etienne Bergeron | 78582b2 | 2016-09-15 15:45:05 +0000 | [diff] [blame] | 2479 | if (maybeInsertAsanInitAtFunctionEntry(F)) |
| 2480 | FunctionModified = true; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 2481 | |
Etienne Bergeron | 752f883 | 2016-09-14 17:18:37 +0000 | [diff] [blame] | 2482 | // Leave if the function doesn't need instrumentation. |
Etienne Bergeron | 78582b2 | 2016-09-15 15:45:05 +0000 | [diff] [blame] | 2483 | if (!F.hasFnAttribute(Attribute::SanitizeAddress)) return FunctionModified; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2484 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2485 | LLVM_DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n"); |
Etienne Bergeron | 752f883 | 2016-09-14 17:18:37 +0000 | [diff] [blame] | 2486 | |
| 2487 | initializeCallbacks(*F.getParent()); |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 2488 | DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 2489 | |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 2490 | FunctionStateRAII CleanupObj(this); |
| 2491 | |
Etienne Bergeron | 0ca0568 | 2016-09-30 17:46:32 +0000 | [diff] [blame] | 2492 | maybeInsertDynamicShadowAtFunctionEntry(F); |
| 2493 | |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 2494 | // We can't instrument allocas used with llvm.localescape. Only static allocas |
| 2495 | // can be passed to that intrinsic. |
| 2496 | markEscapedLocalAllocas(F); |
| 2497 | |
Bill Wendling | c9b22d7 | 2012-10-09 07:45:08 +0000 | [diff] [blame] | 2498 | // We want to instrument every address only once per basic block (unless there |
| 2499 | // are calls between uses). |
Florian Hahn | a1cc848 | 2018-06-12 11:16:56 +0000 | [diff] [blame] | 2500 | SmallPtrSet<Value *, 16> TempsToInstrument; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2501 | SmallVector<Instruction *, 16> ToInstrument; |
| 2502 | SmallVector<Instruction *, 8> NoReturnCalls; |
| 2503 | SmallVector<BasicBlock *, 16> AllBlocks; |
| 2504 | SmallVector<Instruction *, 16> PointerComparisonsOrSubtracts; |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 2505 | int NumAllocas = 0; |
Kostya Serebryany | 9024160 | 2012-05-30 09:04:06 +0000 | [diff] [blame] | 2506 | bool IsWrite; |
Kostya Serebryany | c7895a8 | 2014-05-23 11:52:07 +0000 | [diff] [blame] | 2507 | unsigned Alignment; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2508 | uint64_t TypeSize; |
Leonard Chan | eebecb3 | 2018-10-26 22:51:51 +0000 | [diff] [blame] | 2509 | const TargetLibraryInfo *TLI = |
| 2510 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2511 | |
| 2512 | // Fill the set of memory operations to instrument. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2513 | for (auto &BB : F) { |
| 2514 | AllBlocks.push_back(&BB); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2515 | TempsToInstrument.clear(); |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 2516 | int NumInsnsPerBB = 0; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2517 | for (auto &Inst : BB) { |
| 2518 | if (LooksLikeCodeInBug11395(&Inst)) return false; |
Filipe Cabecinhas | dd96887 | 2016-12-14 21:57:04 +0000 | [diff] [blame] | 2519 | Value *MaybeMask = nullptr; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2520 | if (Value *Addr = isInterestingMemoryAccess(&Inst, &IsWrite, &TypeSize, |
Filipe Cabecinhas | dd96887 | 2016-12-14 21:57:04 +0000 | [diff] [blame] | 2521 | &Alignment, &MaybeMask)) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2522 | if (ClOpt && ClOptSameTemp) { |
Filipe Cabecinhas | dd96887 | 2016-12-14 21:57:04 +0000 | [diff] [blame] | 2523 | // If we have a mask, skip instrumentation if we've already |
| 2524 | // instrumented the full object. But don't add to TempsToInstrument |
| 2525 | // because we might get another load/store with a different mask. |
| 2526 | if (MaybeMask) { |
| 2527 | if (TempsToInstrument.count(Addr)) |
| 2528 | continue; // We've seen this (whole) temp in the current BB. |
| 2529 | } else { |
| 2530 | if (!TempsToInstrument.insert(Addr).second) |
| 2531 | continue; // We've seen this temp in the current BB. |
| 2532 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2533 | } |
Kostya Serebryany | cb3f6e1 | 2014-02-27 13:13:59 +0000 | [diff] [blame] | 2534 | } else if (ClInvalidPointerPairs && |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2535 | isInterestingPointerComparisonOrSubtraction(&Inst)) { |
| 2536 | PointerComparisonsOrSubtracts.push_back(&Inst); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 2537 | continue; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2538 | } else if (isa<MemIntrinsic>(Inst)) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2539 | // ok, take it. |
| 2540 | } else { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2541 | if (isa<AllocaInst>(Inst)) NumAllocas++; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2542 | CallSite CS(&Inst); |
Kostya Serebryany | 699ac28 | 2013-02-20 12:35:15 +0000 | [diff] [blame] | 2543 | if (CS) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2544 | // A call inside BB. |
| 2545 | TempsToInstrument.clear(); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2546 | if (CS.doesNotReturn()) NoReturnCalls.push_back(CS.getInstruction()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2547 | } |
Marcin Koscielnicki | 3feda22 | 2016-06-18 10:10:37 +0000 | [diff] [blame] | 2548 | if (CallInst *CI = dyn_cast<CallInst>(&Inst)) |
| 2549 | maybeMarkSanitizerLibraryCallNoBuiltin(CI, TLI); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2550 | continue; |
| 2551 | } |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2552 | ToInstrument.push_back(&Inst); |
Kostya Serebryany | c387ca7 | 2012-06-28 09:34:41 +0000 | [diff] [blame] | 2553 | NumInsnsPerBB++; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2554 | if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) break; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2558 | bool UseCalls = |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2559 | (ClInstrumentationWithCallsThreshold >= 0 && |
| 2560 | ToInstrument.size() > (unsigned)ClInstrumentationWithCallsThreshold); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2561 | const DataLayout &DL = F.getParent()->getDataLayout(); |
George Burgess IV | 56c7e88 | 2017-03-21 20:08:59 +0000 | [diff] [blame] | 2562 | ObjectSizeOpts ObjSizeOpts; |
| 2563 | ObjSizeOpts.RoundToAlign = true; |
| 2564 | ObjectSizeOffsetVisitor ObjSizeVis(DL, TLI, F.getContext(), ObjSizeOpts); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2565 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2566 | // Instrument. |
| 2567 | int NumInstrumented = 0; |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2568 | for (auto Inst : ToInstrument) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2569 | if (ClDebugMin < 0 || ClDebugMax < 0 || |
| 2570 | (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2571 | if (isInterestingMemoryAccess(Inst, &IsWrite, &TypeSize, &Alignment)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2572 | instrumentMop(ObjSizeVis, Inst, UseCalls, |
| 2573 | F.getParent()->getDataLayout()); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2574 | else |
Kostya Serebryany | 94c81ca | 2014-04-21 11:50:42 +0000 | [diff] [blame] | 2575 | instrumentMemIntrinsic(cast<MemIntrinsic>(Inst)); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2576 | } |
| 2577 | NumInstrumented++; |
| 2578 | } |
| 2579 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 2580 | FunctionStackPoisoner FSP(F, *this); |
| 2581 | bool ChangedStack = FSP.runOnFunction(); |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 2582 | |
| 2583 | // We must unpoison the stack before every NoReturn call (throw, _exit, etc). |
Hans Wennborg | 08b34a0 | 2017-11-13 23:47:58 +0000 | [diff] [blame] | 2584 | // See e.g. https://github.com/google/sanitizers/issues/37 |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2585 | for (auto CI : NoReturnCalls) { |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 2586 | IRBuilder<> IRB(CI); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 2587 | IRB.CreateCall(AsanHandleNoReturnFunc, {}); |
Kostya Serebryany | 154a54d | 2012-02-08 21:36:17 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2590 | for (auto Inst : PointerComparisonsOrSubtracts) { |
| 2591 | instrumentPointerComparisonOrSubtraction(Inst); |
Kostya Serebryany | 796f655 | 2014-02-27 12:45:36 +0000 | [diff] [blame] | 2592 | NumInstrumented++; |
| 2593 | } |
| 2594 | |
Etienne Bergeron | 78582b2 | 2016-09-15 15:45:05 +0000 | [diff] [blame] | 2595 | if (NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty()) |
| 2596 | FunctionModified = true; |
Bob Wilson | da4147c | 2013-11-15 07:16:09 +0000 | [diff] [blame] | 2597 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2598 | LLVM_DEBUG(dbgs() << "ASAN done instrumenting: " << FunctionModified << " " |
| 2599 | << F << "\n"); |
Kostya Serebryany | 9f5213f | 2013-06-26 09:18:17 +0000 | [diff] [blame] | 2600 | |
Etienne Bergeron | 78582b2 | 2016-09-15 15:45:05 +0000 | [diff] [blame] | 2601 | return FunctionModified; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 2604 | // Workaround for bug 11395: we don't want to instrument stack in functions |
| 2605 | // with large assembly blobs (32-bit only), otherwise reg alloc may crash. |
| 2606 | // FIXME: remove once the bug 11395 is fixed. |
| 2607 | bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) { |
| 2608 | if (LongSize != 32) return false; |
| 2609 | CallInst *CI = dyn_cast<CallInst>(I); |
| 2610 | if (!CI || !CI->isInlineAsm()) return false; |
| 2611 | if (CI->getNumArgOperands() <= 5) return false; |
| 2612 | // We have inline assembly with quite a few arguments. |
| 2613 | return true; |
| 2614 | } |
| 2615 | |
| 2616 | void FunctionStackPoisoner::initializeCallbacks(Module &M) { |
| 2617 | IRBuilder<> IRB(*C); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 2618 | for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) { |
| 2619 | std::string Suffix = itostr(i); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 2620 | AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction( |
| 2621 | M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy, |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2622 | IntptrTy)); |
Ismail Pazarbasi | 198d6d5 | 2015-04-06 21:09:08 +0000 | [diff] [blame] | 2623 | AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction( |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2624 | M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix, |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2625 | IRB.getVoidTy(), IntptrTy, IntptrTy)); |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 2626 | } |
Vitaly Buka | 79b75d3 | 2016-06-09 23:05:35 +0000 | [diff] [blame] | 2627 | if (ASan.UseAfterScope) { |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 2628 | AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction( |
| 2629 | M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2630 | IntptrTy, IntptrTy)); |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 2631 | AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction( |
| 2632 | M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2633 | IntptrTy, IntptrTy)); |
Vitaly Buka | 79b75d3 | 2016-06-09 23:05:35 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Vitaly Buka | 8e1906e | 2016-10-18 18:04:59 +0000 | [diff] [blame] | 2636 | for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) { |
| 2637 | std::ostringstream Name; |
| 2638 | Name << kAsanSetShadowPrefix; |
| 2639 | Name << std::setw(2) << std::setfill('0') << std::hex << Val; |
Mehdi Amini | db11fdf | 2017-04-06 20:23:57 +0000 | [diff] [blame] | 2640 | AsanSetShadowFunc[Val] = |
| 2641 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2642 | Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy)); |
Vitaly Buka | 3455b9b | 2016-08-20 18:34:39 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 2645 | AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2646 | kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy)); |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 2647 | AsanAllocasUnpoisonFunc = |
| 2648 | checkSanitizerInterfaceFunction(M.getOrInsertFunction( |
Serge Guelton | 59a2d7b | 2017-04-11 15:01:18 +0000 | [diff] [blame] | 2649 | kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy)); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2652 | void FunctionStackPoisoner::copyToShadowInline(ArrayRef<uint8_t> ShadowMask, |
| 2653 | ArrayRef<uint8_t> ShadowBytes, |
| 2654 | size_t Begin, size_t End, |
| 2655 | IRBuilder<> &IRB, |
| 2656 | Value *ShadowBase) { |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2657 | if (Begin >= End) |
| 2658 | return; |
Vitaly Buka | 186280d | 2016-08-20 18:34:36 +0000 | [diff] [blame] | 2659 | |
| 2660 | const size_t LargestStoreSizeInBytes = |
| 2661 | std::min<size_t>(sizeof(uint64_t), ASan.LongSize / 8); |
| 2662 | |
| 2663 | const bool IsLittleEndian = F.getParent()->getDataLayout().isLittleEndian(); |
| 2664 | |
| 2665 | // Poison given range in shadow using larges store size with out leading and |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2666 | // trailing zeros in ShadowMask. Zeros never change, so they need neither |
| 2667 | // poisoning nor up-poisoning. Still we don't mind if some of them get into a |
| 2668 | // middle of a store. |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2669 | for (size_t i = Begin; i < End;) { |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2670 | if (!ShadowMask[i]) { |
| 2671 | assert(!ShadowBytes[i]); |
Vitaly Buka | 186280d | 2016-08-20 18:34:36 +0000 | [diff] [blame] | 2672 | ++i; |
| 2673 | continue; |
| 2674 | } |
| 2675 | |
| 2676 | size_t StoreSizeInBytes = LargestStoreSizeInBytes; |
| 2677 | // Fit store size into the range. |
| 2678 | while (StoreSizeInBytes > End - i) |
| 2679 | StoreSizeInBytes /= 2; |
| 2680 | |
| 2681 | // Minimize store size by trimming trailing zeros. |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2682 | for (size_t j = StoreSizeInBytes - 1; j && !ShadowMask[i + j]; --j) { |
Vitaly Buka | 186280d | 2016-08-20 18:34:36 +0000 | [diff] [blame] | 2683 | while (j <= StoreSizeInBytes / 2) |
| 2684 | StoreSizeInBytes /= 2; |
| 2685 | } |
| 2686 | |
| 2687 | uint64_t Val = 0; |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2688 | for (size_t j = 0; j < StoreSizeInBytes; j++) { |
| 2689 | if (IsLittleEndian) |
| 2690 | Val |= (uint64_t)ShadowBytes[i + j] << (8 * j); |
| 2691 | else |
| 2692 | Val = (Val << 8) | ShadowBytes[i + j]; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2693 | } |
Vitaly Buka | 186280d | 2016-08-20 18:34:36 +0000 | [diff] [blame] | 2694 | |
| 2695 | Value *Ptr = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)); |
| 2696 | Value *Poison = IRB.getIntN(StoreSizeInBytes * 8, Val); |
Vitaly Buka | 0672a27 | 2016-08-22 04:16:14 +0000 | [diff] [blame] | 2697 | IRB.CreateAlignedStore( |
| 2698 | Poison, IRB.CreateIntToPtr(Ptr, Poison->getType()->getPointerTo()), 1); |
Vitaly Buka | 186280d | 2016-08-20 18:34:36 +0000 | [diff] [blame] | 2699 | |
| 2700 | i += StoreSizeInBytes; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2704 | void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask, |
| 2705 | ArrayRef<uint8_t> ShadowBytes, |
| 2706 | IRBuilder<> &IRB, Value *ShadowBase) { |
| 2707 | copyToShadow(ShadowMask, ShadowBytes, 0, ShadowMask.size(), IRB, ShadowBase); |
| 2708 | } |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2709 | |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2710 | void FunctionStackPoisoner::copyToShadow(ArrayRef<uint8_t> ShadowMask, |
| 2711 | ArrayRef<uint8_t> ShadowBytes, |
| 2712 | size_t Begin, size_t End, |
| 2713 | IRBuilder<> &IRB, Value *ShadowBase) { |
| 2714 | assert(ShadowMask.size() == ShadowBytes.size()); |
| 2715 | size_t Done = Begin; |
| 2716 | for (size_t i = Begin, j = Begin + 1; i < End; i = j++) { |
| 2717 | if (!ShadowMask[i]) { |
| 2718 | assert(!ShadowBytes[i]); |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2719 | continue; |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2720 | } |
| 2721 | uint8_t Val = ShadowBytes[i]; |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2722 | if (!AsanSetShadowFunc[Val]) |
| 2723 | continue; |
| 2724 | |
| 2725 | // Skip same values. |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2726 | for (; j < End && ShadowMask[j] && Val == ShadowBytes[j]; ++j) { |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | if (j - i >= ClMaxInlinePoisoningSize) { |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2730 | copyToShadowInline(ShadowMask, ShadowBytes, Done, i, IRB, ShadowBase); |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2731 | IRB.CreateCall(AsanSetShadowFunc[Val], |
| 2732 | {IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i)), |
| 2733 | ConstantInt::get(IntptrTy, j - i)}); |
| 2734 | Done = j; |
| 2735 | } |
| 2736 | } |
| 2737 | |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2738 | copyToShadowInline(ShadowMask, ShadowBytes, Done, End, IRB, ShadowBase); |
Vitaly Buka | 1f9e135 | 2016-08-20 20:23:50 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 2741 | // Fake stack allocator (asan_fake_stack.h) has 11 size classes |
| 2742 | // for every power of 2 from kMinStackMallocSize to kMaxAsanStackMallocSizeClass |
| 2743 | static int StackMallocSizeClass(uint64_t LocalStackSize) { |
| 2744 | assert(LocalStackSize <= kMaxStackMallocSize); |
| 2745 | uint64_t MaxSize = kMinStackMallocSize; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2746 | for (int i = 0;; i++, MaxSize *= 2) |
| 2747 | if (LocalStackSize <= MaxSize) return i; |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 2748 | llvm_unreachable("impossible LocalStackSize"); |
| 2749 | } |
| 2750 | |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 2751 | void FunctionStackPoisoner::copyArgsPassedByValToAllocas() { |
Matt Morehouse | 49e5aca | 2017-08-09 17:59:43 +0000 | [diff] [blame] | 2752 | Instruction *CopyInsertPoint = &F.front().front(); |
| 2753 | if (CopyInsertPoint == ASan.LocalDynamicShadow) { |
| 2754 | // Insert after the dynamic shadow location is determined |
| 2755 | CopyInsertPoint = CopyInsertPoint->getNextNode(); |
| 2756 | assert(CopyInsertPoint); |
| 2757 | } |
| 2758 | IRBuilder<> IRB(CopyInsertPoint); |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 2759 | const DataLayout &DL = F.getParent()->getDataLayout(); |
| 2760 | for (Argument &Arg : F.args()) { |
| 2761 | if (Arg.hasByValAttr()) { |
| 2762 | Type *Ty = Arg.getType()->getPointerElementType(); |
| 2763 | unsigned Align = Arg.getParamAlignment(); |
| 2764 | if (Align == 0) Align = DL.getABITypeAlignment(Ty); |
| 2765 | |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 2766 | AllocaInst *AI = IRB.CreateAlloca( |
| 2767 | Ty, nullptr, |
| 2768 | (Arg.hasName() ? Arg.getName() : "Arg" + Twine(Arg.getArgNo())) + |
| 2769 | ".byval"); |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 2770 | AI->setAlignment(Align); |
| 2771 | Arg.replaceAllUsesWith(AI); |
| 2772 | |
| 2773 | uint64_t AllocSize = DL.getTypeAllocSize(Ty); |
Daniel Neilson | a98d9d9 | 2018-02-08 21:26:12 +0000 | [diff] [blame] | 2774 | IRB.CreateMemCpy(AI, Align, &Arg, Align, AllocSize); |
Vitaly Buka | 74443f0 | 2017-07-18 22:28:03 +0000 | [diff] [blame] | 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2779 | PHINode *FunctionStackPoisoner::createPHI(IRBuilder<> &IRB, Value *Cond, |
| 2780 | Value *ValueIfTrue, |
| 2781 | Instruction *ThenTerm, |
| 2782 | Value *ValueIfFalse) { |
| 2783 | PHINode *PHI = IRB.CreatePHI(IntptrTy, 2); |
| 2784 | BasicBlock *CondBlock = cast<Instruction>(Cond)->getParent(); |
| 2785 | PHI->addIncoming(ValueIfFalse, CondBlock); |
| 2786 | BasicBlock *ThenBlock = ThenTerm->getParent(); |
| 2787 | PHI->addIncoming(ValueIfTrue, ThenBlock); |
| 2788 | return PHI; |
| 2789 | } |
| 2790 | |
| 2791 | Value *FunctionStackPoisoner::createAllocaForLayout( |
| 2792 | IRBuilder<> &IRB, const ASanStackFrameLayout &L, bool Dynamic) { |
| 2793 | AllocaInst *Alloca; |
| 2794 | if (Dynamic) { |
| 2795 | Alloca = IRB.CreateAlloca(IRB.getInt8Ty(), |
| 2796 | ConstantInt::get(IRB.getInt64Ty(), L.FrameSize), |
| 2797 | "MyAlloca"); |
| 2798 | } else { |
| 2799 | Alloca = IRB.CreateAlloca(ArrayType::get(IRB.getInt8Ty(), L.FrameSize), |
| 2800 | nullptr, "MyAlloca"); |
| 2801 | assert(Alloca->isStaticAlloca()); |
| 2802 | } |
| 2803 | assert((ClRealignStack & (ClRealignStack - 1)) == 0); |
| 2804 | size_t FrameAlignment = std::max(L.FrameAlignment, (size_t)ClRealignStack); |
| 2805 | Alloca->setAlignment(FrameAlignment); |
| 2806 | return IRB.CreatePointerCast(Alloca, IntptrTy); |
| 2807 | } |
| 2808 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 2809 | void FunctionStackPoisoner::createDynamicAllocasInitStorage() { |
| 2810 | BasicBlock &FirstBB = *F.begin(); |
| 2811 | IRBuilder<> IRB(dyn_cast<Instruction>(FirstBB.begin())); |
| 2812 | DynamicAllocaLayout = IRB.CreateAlloca(IntptrTy, nullptr); |
| 2813 | IRB.CreateStore(Constant::getNullValue(IntptrTy), DynamicAllocaLayout); |
| 2814 | DynamicAllocaLayout->setAlignment(32); |
| 2815 | } |
| 2816 | |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 2817 | void FunctionStackPoisoner::processDynamicAllocas() { |
| 2818 | if (!ClInstrumentDynamicAllocas || DynamicAllocaVec.empty()) { |
| 2819 | assert(DynamicAllocaPoisonCallVec.empty()); |
| 2820 | return; |
| 2821 | } |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 2822 | |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 2823 | // Insert poison calls for lifetime intrinsics for dynamic allocas. |
| 2824 | for (const auto &APC : DynamicAllocaPoisonCallVec) { |
Alexey Samsonov | 8daaf8b | 2015-10-22 19:51:59 +0000 | [diff] [blame] | 2825 | assert(APC.InsBefore); |
| 2826 | assert(APC.AI); |
Vitaly Buka | b451f1b | 2016-06-09 23:31:59 +0000 | [diff] [blame] | 2827 | assert(ASan.isInterestingAlloca(*APC.AI)); |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 2828 | assert(!APC.AI->isStaticAlloca()); |
Vitaly Buka | b451f1b | 2016-06-09 23:31:59 +0000 | [diff] [blame] | 2829 | |
Alexey Samsonov | 8daaf8b | 2015-10-22 19:51:59 +0000 | [diff] [blame] | 2830 | IRBuilder<> IRB(APC.InsBefore); |
| 2831 | poisonAlloca(APC.AI, APC.Size, IRB, APC.DoPoison); |
Vitaly Buka | b451f1b | 2016-06-09 23:31:59 +0000 | [diff] [blame] | 2832 | // Dynamic allocas will be unpoisoned unconditionally below in |
| 2833 | // unpoisonDynamicAllocas. |
| 2834 | // Flag that we need unpoison static allocas. |
Alexey Samsonov | 8daaf8b | 2015-10-22 19:51:59 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 2837 | // Handle dynamic allocas. |
| 2838 | createDynamicAllocasInitStorage(); |
| 2839 | for (auto &AI : DynamicAllocaVec) |
| 2840 | handleDynamicAllocaCall(AI); |
| 2841 | unpoisonDynamicAllocas(); |
| 2842 | } |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 2843 | |
Vitaly Buka | 5b4f121 | 2016-08-20 17:22:27 +0000 | [diff] [blame] | 2844 | void FunctionStackPoisoner::processStaticAllocas() { |
| 2845 | if (AllocaVec.empty()) { |
| 2846 | assert(StaticAllocaPoisonCallVec.empty()); |
| 2847 | return; |
Kuba Brecka | f5875d3 | 2015-02-24 09:47:05 +0000 | [diff] [blame] | 2848 | } |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 2849 | |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 2850 | int StackMallocIdx = -1; |
Alexey Samsonov | 773e8c3 | 2015-06-26 00:00:47 +0000 | [diff] [blame] | 2851 | DebugLoc EntryDebugLocation; |
Pete Cooper | adebb93 | 2016-03-11 02:14:16 +0000 | [diff] [blame] | 2852 | if (auto SP = F.getSubprogram()) |
Alexey Samsonov | 773e8c3 | 2015-06-26 00:00:47 +0000 | [diff] [blame] | 2853 | EntryDebugLocation = DebugLoc::get(SP->getScopeLine(), 0, SP); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2854 | |
| 2855 | Instruction *InsBefore = AllocaVec[0]; |
| 2856 | IRBuilder<> IRB(InsBefore); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 2857 | IRB.SetCurrentDebugLocation(EntryDebugLocation); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2858 | |
Kuba Brecka | 8ec94ea | 2015-07-22 10:25:38 +0000 | [diff] [blame] | 2859 | // Make sure non-instrumented allocas stay in the entry block. Otherwise, |
| 2860 | // debug info is broken, because only entry-block allocas are treated as |
| 2861 | // regular stack slots. |
| 2862 | auto InsBeforeB = InsBefore->getParent(); |
| 2863 | assert(InsBeforeB == &F.getEntryBlock()); |
Kuba Brecka | a49dcbb | 2016-11-08 21:30:41 +0000 | [diff] [blame] | 2864 | for (auto *AI : StaticAllocasToMoveUp) |
| 2865 | if (AI->getParent() == InsBeforeB) |
| 2866 | AI->moveBefore(InsBefore); |
Kuba Brecka | 37a5ffa | 2015-07-17 06:29:57 +0000 | [diff] [blame] | 2867 | |
Reid Kleckner | 2f90755 | 2015-07-21 17:40:14 +0000 | [diff] [blame] | 2868 | // If we have a call to llvm.localescape, keep it in the entry block. |
| 2869 | if (LocalEscapeCall) LocalEscapeCall->moveBefore(InsBefore); |
| 2870 | |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 2871 | SmallVector<ASanStackVariableDescription, 16> SVD; |
| 2872 | SVD.reserve(AllocaVec.size()); |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2873 | for (AllocaInst *AI : AllocaVec) { |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 2874 | ASanStackVariableDescription D = {AI->getName().data(), |
Vitaly Buka | 21a9e57 | 2016-07-28 22:50:50 +0000 | [diff] [blame] | 2875 | ASan.getAllocaSizeInBytes(*AI), |
Vitaly Buka | d88e520 | 2016-10-18 23:29:41 +0000 | [diff] [blame] | 2876 | 0, |
Vitaly Buka | f9fd63a | 2016-08-20 16:48:24 +0000 | [diff] [blame] | 2877 | AI->getAlignment(), |
| 2878 | AI, |
Vitaly Buka | d88e520 | 2016-10-18 23:29:41 +0000 | [diff] [blame] | 2879 | 0, |
Vitaly Buka | f9fd63a | 2016-08-20 16:48:24 +0000 | [diff] [blame] | 2880 | 0}; |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 2881 | SVD.push_back(D); |
| 2882 | } |
Vitaly Buka | 5910a92 | 2016-10-18 23:29:52 +0000 | [diff] [blame] | 2883 | |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 2884 | // Minimal header size (left redzone) is 4 pointers, |
| 2885 | // i.e. 32 bytes on 64-bit platforms and 16 bytes in 32-bit platforms. |
Walter Lee | 2a2b69e | 2017-11-16 12:57:19 +0000 | [diff] [blame] | 2886 | size_t Granularity = 1ULL << Mapping.Scale; |
| 2887 | size_t MinHeaderSize = std::max((size_t)ASan.LongSize / 2, Granularity); |
Vitaly Buka | db331d8 | 2016-08-29 17:41:29 +0000 | [diff] [blame] | 2888 | const ASanStackFrameLayout &L = |
Walter Lee | 2a2b69e | 2017-11-16 12:57:19 +0000 | [diff] [blame] | 2889 | ComputeASanStackFrameLayout(SVD, Granularity, MinHeaderSize); |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 2890 | |
Vitaly Buka | 5910a92 | 2016-10-18 23:29:52 +0000 | [diff] [blame] | 2891 | // Build AllocaToSVDMap for ASanStackVariableDescription lookup. |
| 2892 | DenseMap<const AllocaInst *, ASanStackVariableDescription *> AllocaToSVDMap; |
| 2893 | for (auto &Desc : SVD) |
| 2894 | AllocaToSVDMap[Desc.AI] = &Desc; |
| 2895 | |
| 2896 | // Update SVD with information from lifetime intrinsics. |
| 2897 | for (const auto &APC : StaticAllocaPoisonCallVec) { |
| 2898 | assert(APC.InsBefore); |
| 2899 | assert(APC.AI); |
| 2900 | assert(ASan.isInterestingAlloca(*APC.AI)); |
| 2901 | assert(APC.AI->isStaticAlloca()); |
| 2902 | |
| 2903 | ASanStackVariableDescription &Desc = *AllocaToSVDMap[APC.AI]; |
| 2904 | Desc.LifetimeSize = Desc.Size; |
| 2905 | if (const DILocation *FnLoc = EntryDebugLocation.get()) { |
| 2906 | if (const DILocation *LifetimeLoc = APC.InsBefore->getDebugLoc().get()) { |
| 2907 | if (LifetimeLoc->getFile() == FnLoc->getFile()) |
| 2908 | if (unsigned Line = LifetimeLoc->getLine()) |
| 2909 | Desc.Line = std::min(Desc.Line ? Desc.Line : Line, Line); |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | |
| 2914 | auto DescriptionString = ComputeASanStackFrameDescription(SVD); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2915 | LLVM_DEBUG(dbgs() << DescriptionString << " --- " << L.FrameSize << "\n"); |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 2916 | uint64_t LocalStackSize = L.FrameSize; |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 2917 | bool DoStackMalloc = ClUseAfterReturn && !ASan.CompileKernel && |
| 2918 | LocalStackSize <= kMaxStackMallocSize; |
Alexey Samsonov | 869a5ff | 2015-07-29 19:36:08 +0000 | [diff] [blame] | 2919 | bool DoDynamicAlloca = ClDynamicAllocaStack; |
| 2920 | // Don't do dynamic alloca or stack malloc if: |
| 2921 | // 1) There is inline asm: too often it makes assumptions on which registers |
| 2922 | // are available. |
| 2923 | // 2) There is a returns_twice call (typically setjmp), which is |
| 2924 | // optimization-hostile, and doesn't play well with introduced indirect |
| 2925 | // register-relative calculation of local variable addresses. |
| 2926 | DoDynamicAlloca &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall; |
| 2927 | DoStackMalloc &= !HasNonEmptyInlineAsm && !HasReturnsTwiceCall; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2928 | |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2929 | Value *StaticAlloca = |
| 2930 | DoDynamicAlloca ? nullptr : createAllocaForLayout(IRB, L, false); |
| 2931 | |
| 2932 | Value *FakeStack; |
| 2933 | Value *LocalStackBase; |
Adrian Prantl | 3c6c14d | 2017-12-11 20:43:21 +0000 | [diff] [blame] | 2934 | Value *LocalStackBaseAlloca; |
| 2935 | bool Deref; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2936 | |
| 2937 | if (DoStackMalloc) { |
Adrian Prantl | 3c6c14d | 2017-12-11 20:43:21 +0000 | [diff] [blame] | 2938 | LocalStackBaseAlloca = |
| 2939 | IRB.CreateAlloca(IntptrTy, nullptr, "asan_local_stack_base"); |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2940 | // void *FakeStack = __asan_option_detect_stack_use_after_return |
| 2941 | // ? __asan_stack_malloc_N(LocalStackSize) |
| 2942 | // : nullptr; |
| 2943 | // void *LocalStackBase = (FakeStack) ? FakeStack : alloca(LocalStackSize); |
Vitaly Buka | 7b8ed4f | 2016-06-02 00:06:42 +0000 | [diff] [blame] | 2944 | Constant *OptionDetectUseAfterReturn = F.getParent()->getOrInsertGlobal( |
| 2945 | kAsanOptionDetectUseAfterReturn, IRB.getInt32Ty()); |
| 2946 | Value *UseAfterReturnIsEnabled = |
| 2947 | IRB.CreateICmpNE(IRB.CreateLoad(OptionDetectUseAfterReturn), |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2948 | Constant::getNullValue(IRB.getInt32Ty())); |
| 2949 | Instruction *Term = |
Vitaly Buka | 7b8ed4f | 2016-06-02 00:06:42 +0000 | [diff] [blame] | 2950 | SplitBlockAndInsertIfThen(UseAfterReturnIsEnabled, InsBefore, false); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 2951 | IRBuilder<> IRBIf(Term); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 2952 | IRBIf.SetCurrentDebugLocation(EntryDebugLocation); |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2953 | StackMallocIdx = StackMallocSizeClass(LocalStackSize); |
| 2954 | assert(StackMallocIdx <= kMaxAsanStackMallocSizeClass); |
| 2955 | Value *FakeStackValue = |
| 2956 | IRBIf.CreateCall(AsanStackMallocFunc[StackMallocIdx], |
| 2957 | ConstantInt::get(IntptrTy, LocalStackSize)); |
Kostya Serebryany | f322382 | 2013-09-18 14:07:14 +0000 | [diff] [blame] | 2958 | IRB.SetInsertPoint(InsBefore); |
Evgeniy Stepanov | aaf4bb2 | 2014-05-14 10:30:15 +0000 | [diff] [blame] | 2959 | IRB.SetCurrentDebugLocation(EntryDebugLocation); |
Vitaly Buka | 7b8ed4f | 2016-06-02 00:06:42 +0000 | [diff] [blame] | 2960 | FakeStack = createPHI(IRB, UseAfterReturnIsEnabled, FakeStackValue, Term, |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2961 | ConstantInt::get(IntptrTy, 0)); |
| 2962 | |
| 2963 | Value *NoFakeStack = |
| 2964 | IRB.CreateICmpEQ(FakeStack, Constant::getNullValue(IntptrTy)); |
| 2965 | Term = SplitBlockAndInsertIfThen(NoFakeStack, InsBefore, false); |
| 2966 | IRBIf.SetInsertPoint(Term); |
| 2967 | IRBIf.SetCurrentDebugLocation(EntryDebugLocation); |
| 2968 | Value *AllocaValue = |
| 2969 | DoDynamicAlloca ? createAllocaForLayout(IRBIf, L, true) : StaticAlloca; |
Adrian Prantl | 3c6c14d | 2017-12-11 20:43:21 +0000 | [diff] [blame] | 2970 | |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2971 | IRB.SetInsertPoint(InsBefore); |
| 2972 | IRB.SetCurrentDebugLocation(EntryDebugLocation); |
| 2973 | LocalStackBase = createPHI(IRB, NoFakeStack, AllocaValue, Term, FakeStack); |
Adrian Prantl | 3c6c14d | 2017-12-11 20:43:21 +0000 | [diff] [blame] | 2974 | IRB.SetCurrentDebugLocation(EntryDebugLocation); |
| 2975 | IRB.CreateStore(LocalStackBase, LocalStackBaseAlloca); |
| 2976 | Deref = true; |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 2977 | } else { |
| 2978 | // void *FakeStack = nullptr; |
| 2979 | // void *LocalStackBase = alloca(LocalStackSize); |
| 2980 | FakeStack = ConstantInt::get(IntptrTy, 0); |
| 2981 | LocalStackBase = |
| 2982 | DoDynamicAlloca ? createAllocaForLayout(IRB, L, true) : StaticAlloca; |
Adrian Prantl | 3c6c14d | 2017-12-11 20:43:21 +0000 | [diff] [blame] | 2983 | LocalStackBaseAlloca = LocalStackBase; |
| 2984 | Deref = false; |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2987 | // Replace Alloca instructions with base+offset. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2988 | for (const auto &Desc : SVD) { |
| 2989 | AllocaInst *AI = Desc.AI; |
Adrian Prantl | 3c6c14d | 2017-12-11 20:43:21 +0000 | [diff] [blame] | 2990 | replaceDbgDeclareForAlloca(AI, LocalStackBaseAlloca, DIB, Deref, |
| 2991 | Desc.Offset, DIExpression::NoDeref); |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 2992 | Value *NewAllocaPtr = IRB.CreateIntToPtr( |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 2993 | IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Desc.Offset)), |
Kostya Serebryany | 4fb7801 | 2013-12-06 09:00:17 +0000 | [diff] [blame] | 2994 | AI->getType()); |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 2995 | AI->replaceAllUsesWith(NewAllocaPtr); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2996 | } |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 2997 | |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 2998 | // The left-most redzone has enough space for at least 4 pointers. |
| 2999 | // Write the Magic value to redzone[0]. |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3000 | Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy); |
| 3001 | IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic), |
| 3002 | BasePlus0); |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 3003 | // Write the frame description constant to redzone[1]. |
| 3004 | Value *BasePlus1 = IRB.CreateIntToPtr( |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3005 | IRB.CreateAdd(LocalStackBase, |
| 3006 | ConstantInt::get(IntptrTy, ASan.LongSize / 8)), |
| 3007 | IntptrPtrTy); |
Alexey Samsonov | 9bdb63a | 2012-11-02 12:20:34 +0000 | [diff] [blame] | 3008 | GlobalVariable *StackDescriptionGlobal = |
Vitaly Buka | 5910a92 | 2016-10-18 23:29:52 +0000 | [diff] [blame] | 3009 | createPrivateGlobalForString(*F.getParent(), DescriptionString, |
Kostya Serebryany | d891ac9 | 2018-10-11 23:03:27 +0000 | [diff] [blame] | 3010 | /*AllowMerging*/ true, kAsanGenPrefix); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3011 | Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, IntptrTy); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3012 | IRB.CreateStore(Description, BasePlus1); |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 3013 | // Write the PC to redzone[2]. |
| 3014 | Value *BasePlus2 = IRB.CreateIntToPtr( |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3015 | IRB.CreateAdd(LocalStackBase, |
| 3016 | ConstantInt::get(IntptrTy, 2 * ASan.LongSize / 8)), |
| 3017 | IntptrPtrTy); |
Kostya Serebryany | cdd35a9 | 2013-03-22 10:37:20 +0000 | [diff] [blame] | 3018 | IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3019 | |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 3020 | const auto &ShadowAfterScope = GetShadowBytesAfterScope(SVD, L); |
| 3021 | |
| 3022 | // Poison the stack red zones at the entry. |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 3023 | Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB); |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 3024 | // As mask we must use most poisoned case: red zones and after scope. |
| 3025 | // As bytes we can use either the same or just red zones only. |
| 3026 | copyToShadow(ShadowAfterScope, ShadowAfterScope, IRB, ShadowBase); |
| 3027 | |
Vitaly Buka | 8e1906e | 2016-10-18 18:04:59 +0000 | [diff] [blame] | 3028 | if (!StaticAllocaPoisonCallVec.empty()) { |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 3029 | const auto &ShadowInScope = GetShadowBytes(SVD, L); |
| 3030 | |
| 3031 | // Poison static allocas near lifetime intrinsics. |
| 3032 | for (const auto &APC : StaticAllocaPoisonCallVec) { |
Vitaly Buka | 5910a92 | 2016-10-18 23:29:52 +0000 | [diff] [blame] | 3033 | const ASanStackVariableDescription &Desc = *AllocaToSVDMap[APC.AI]; |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 3034 | assert(Desc.Offset % L.Granularity == 0); |
| 3035 | size_t Begin = Desc.Offset / L.Granularity; |
| 3036 | size_t End = Begin + (APC.Size + L.Granularity - 1) / L.Granularity; |
| 3037 | |
| 3038 | IRBuilder<> IRB(APC.InsBefore); |
| 3039 | copyToShadow(ShadowAfterScope, |
| 3040 | APC.DoPoison ? ShadowAfterScope : ShadowInScope, Begin, End, |
| 3041 | IRB, ShadowBase); |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | SmallVector<uint8_t, 64> ShadowClean(ShadowAfterScope.size(), 0); |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 3046 | SmallVector<uint8_t, 64> ShadowAfterReturn; |
Vitaly Buka | 186280d | 2016-08-20 18:34:36 +0000 | [diff] [blame] | 3047 | |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 3048 | // (Un)poison the stack before all ret instructions. |
Alexey Samsonov | a02e664 | 2014-05-29 18:40:48 +0000 | [diff] [blame] | 3049 | for (auto Ret : RetVec) { |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3050 | IRBuilder<> IRBRet(Ret); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3051 | // Mark the current frame as retired. |
| 3052 | IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic), |
| 3053 | BasePlus0); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3054 | if (DoStackMalloc) { |
Kostya Serebryany | 6805de5 | 2013-09-10 13:16:56 +0000 | [diff] [blame] | 3055 | assert(StackMallocIdx >= 0); |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 3056 | // if FakeStack != 0 // LocalStackBase == FakeStack |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 3057 | // // In use-after-return mode, poison the whole stack frame. |
| 3058 | // if StackMallocIdx <= 4 |
| 3059 | // // For small sizes inline the whole thing: |
| 3060 | // memset(ShadowBase, kAsanStackAfterReturnMagic, ShadowSize); |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 3061 | // **SavedFlagPtr(FakeStack) = 0 |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 3062 | // else |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 3063 | // __asan_stack_free_N(FakeStack, LocalStackSize) |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 3064 | // else |
| 3065 | // <This is not a fake stack; unpoison the redzones> |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 3066 | Value *Cmp = |
| 3067 | IRBRet.CreateICmpNE(FakeStack, Constant::getNullValue(IntptrTy)); |
Chandler Carruth | 4a2d58e | 2018-10-15 09:34:05 +0000 | [diff] [blame] | 3068 | Instruction *ThenTerm, *ElseTerm; |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 3069 | SplitBlockAndInsertIfThenElse(Cmp, Ret, &ThenTerm, &ElseTerm); |
| 3070 | |
| 3071 | IRBuilder<> IRBPoison(ThenTerm); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 3072 | if (StackMallocIdx <= 4) { |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 3073 | int ClassSize = kMinStackMallocSize << StackMallocIdx; |
Vitaly Buka | 793913c | 2016-08-29 18:17:21 +0000 | [diff] [blame] | 3074 | ShadowAfterReturn.resize(ClassSize / L.Granularity, |
| 3075 | kAsanStackUseAfterReturnMagic); |
| 3076 | copyToShadow(ShadowAfterReturn, ShadowAfterReturn, IRBPoison, |
| 3077 | ShadowBase); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 3078 | Value *SavedFlagPtrPtr = IRBPoison.CreateAdd( |
Alexey Samsonov | 4b7f413 | 2014-12-11 21:53:03 +0000 | [diff] [blame] | 3079 | FakeStack, |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 3080 | ConstantInt::get(IntptrTy, ClassSize - ASan.LongSize / 8)); |
| 3081 | Value *SavedFlagPtr = IRBPoison.CreateLoad( |
| 3082 | IRBPoison.CreateIntToPtr(SavedFlagPtrPtr, IntptrPtrTy)); |
| 3083 | IRBPoison.CreateStore( |
| 3084 | Constant::getNullValue(IRBPoison.getInt8Ty()), |
| 3085 | IRBPoison.CreateIntToPtr(SavedFlagPtr, IRBPoison.getInt8PtrTy())); |
| 3086 | } else { |
| 3087 | // For larger frames call __asan_stack_free_*. |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 3088 | IRBPoison.CreateCall( |
| 3089 | AsanStackFreeFunc[StackMallocIdx], |
| 3090 | {FakeStack, ConstantInt::get(IntptrTy, LocalStackSize)}); |
Kostya Serebryany | bc86efb | 2013-09-17 12:14:50 +0000 | [diff] [blame] | 3091 | } |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 3092 | |
| 3093 | IRBuilder<> IRBElse(ElseTerm); |
Vitaly Buka | 8e1906e | 2016-10-18 18:04:59 +0000 | [diff] [blame] | 3094 | copyToShadow(ShadowAfterScope, ShadowClean, IRBElse, ShadowBase); |
Kostya Serebryany | 530e207 | 2013-12-23 14:15:08 +0000 | [diff] [blame] | 3095 | } else { |
Vitaly Buka | 8e1906e | 2016-10-18 18:04:59 +0000 | [diff] [blame] | 3096 | copyToShadow(ShadowAfterScope, ShadowClean, IRBRet, ShadowBase); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3097 | } |
| 3098 | } |
| 3099 | |
Kostya Serebryany | 0995994 | 2012-10-19 06:20:53 +0000 | [diff] [blame] | 3100 | // We are done. Remove the old unused alloca instructions. |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3101 | for (auto AI : AllocaVec) AI->eraseFromParent(); |
Kostya Serebryany | 6e6b03e | 2011-11-16 01:35:23 +0000 | [diff] [blame] | 3102 | } |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 3103 | |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 3104 | void FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size, |
Jakub Staszak | 23ec6a9 | 2013-08-09 20:53:48 +0000 | [diff] [blame] | 3105 | IRBuilder<> &IRB, bool DoPoison) { |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 3106 | // For now just insert the call to ASan runtime. |
| 3107 | Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy); |
| 3108 | Value *SizeArg = ConstantInt::get(IntptrTy, Size); |
Alexander Potapenko | f90556e | 2015-06-12 11:27:06 +0000 | [diff] [blame] | 3109 | IRB.CreateCall( |
| 3110 | DoPoison ? AsanPoisonStackMemoryFunc : AsanUnpoisonStackMemoryFunc, |
| 3111 | {AddrArg, SizeArg}); |
Alexey Samsonov | 261177a | 2012-12-04 01:34:23 +0000 | [diff] [blame] | 3112 | } |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 3113 | |
| 3114 | // Handling llvm.lifetime intrinsics for a given %alloca: |
| 3115 | // (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca. |
| 3116 | // (2) if %size is constant, poison memory for llvm.lifetime.end (to detect |
| 3117 | // invalid accesses) and unpoison it for llvm.lifetime.start (the memory |
| 3118 | // could be poisoned by previous llvm.lifetime.end instruction, as the |
| 3119 | // variable may go in and out of scope several times, e.g. in loops). |
| 3120 | // (3) if we poisoned at least one %alloca in a function, |
| 3121 | // unpoison the whole stack frame at function exit. |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 3122 | |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 3123 | AllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) { |
| 3124 | if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) |
Etienne Bergeron | 9bd4281 | 2016-09-14 15:59:32 +0000 | [diff] [blame] | 3125 | // We're interested only in allocas we can handle. |
Anna Zaks | 8ed1d81 | 2015-02-27 03:12:36 +0000 | [diff] [blame] | 3126 | return ASan.isInterestingAlloca(*AI) ? AI : nullptr; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 3127 | // See if we've already calculated (or started to calculate) alloca for a |
| 3128 | // given value. |
| 3129 | AllocaForValueMapTy::iterator I = AllocaForValue.find(V); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3130 | if (I != AllocaForValue.end()) return I->second; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 3131 | // Store 0 while we're calculating alloca for value V to avoid |
| 3132 | // infinite recursion if the value references itself. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3133 | AllocaForValue[V] = nullptr; |
| 3134 | AllocaInst *Res = nullptr; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 3135 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 3136 | Res = findAllocaForValue(CI->getOperand(0)); |
| 3137 | else if (PHINode *PN = dyn_cast<PHINode>(V)) { |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 3138 | for (Value *IncValue : PN->incoming_values()) { |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 3139 | // Allow self-referencing phi-nodes. |
| 3140 | if (IncValue == PN) continue; |
| 3141 | AllocaInst *IncValueAI = findAllocaForValue(IncValue); |
| 3142 | // AI for incoming values should exist and should all be equal. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 3143 | if (IncValueAI == nullptr || (Res != nullptr && IncValueAI != Res)) |
| 3144 | return nullptr; |
Alexey Samsonov | 29dd7f2 | 2012-12-27 08:50:58 +0000 | [diff] [blame] | 3145 | Res = IncValueAI; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 3146 | } |
Vitaly Buka | 53054a7 | 2016-07-22 00:56:17 +0000 | [diff] [blame] | 3147 | } else if (GetElementPtrInst *EP = dyn_cast<GetElementPtrInst>(V)) { |
| 3148 | Res = findAllocaForValue(EP->getPointerOperand()); |
| 3149 | } else { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3150 | LLVM_DEBUG(dbgs() << "Alloca search canceled on unknown instruction: " << *V |
| 3151 | << "\n"); |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 3152 | } |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3153 | if (Res) AllocaForValue[V] = Res; |
Alexey Samsonov | 1e3f7ba | 2012-12-25 12:04:36 +0000 | [diff] [blame] | 3154 | return Res; |
| 3155 | } |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3156 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 3157 | void FunctionStackPoisoner::handleDynamicAllocaCall(AllocaInst *AI) { |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3158 | IRBuilder<> IRB(AI); |
| 3159 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3160 | const unsigned Align = std::max(kAllocaRzSize, AI->getAlignment()); |
| 3161 | const uint64_t AllocaRedzoneMask = kAllocaRzSize - 1; |
| 3162 | |
| 3163 | Value *Zero = Constant::getNullValue(IntptrTy); |
| 3164 | Value *AllocaRzSize = ConstantInt::get(IntptrTy, kAllocaRzSize); |
| 3165 | Value *AllocaRzMask = ConstantInt::get(IntptrTy, AllocaRedzoneMask); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3166 | |
| 3167 | // Since we need to extend alloca with additional memory to locate |
| 3168 | // redzones, and OldSize is number of allocated blocks with |
| 3169 | // ElementSize size, get allocated memory size in bytes by |
| 3170 | // OldSize * ElementSize. |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 3171 | const unsigned ElementSize = |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 3172 | F.getParent()->getDataLayout().getTypeAllocSize(AI->getAllocatedType()); |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 3173 | Value *OldSize = |
| 3174 | IRB.CreateMul(IRB.CreateIntCast(AI->getArraySize(), IntptrTy, false), |
| 3175 | ConstantInt::get(IntptrTy, ElementSize)); |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3176 | |
| 3177 | // PartialSize = OldSize % 32 |
| 3178 | Value *PartialSize = IRB.CreateAnd(OldSize, AllocaRzMask); |
| 3179 | |
| 3180 | // Misalign = kAllocaRzSize - PartialSize; |
| 3181 | Value *Misalign = IRB.CreateSub(AllocaRzSize, PartialSize); |
| 3182 | |
| 3183 | // PartialPadding = Misalign != kAllocaRzSize ? Misalign : 0; |
| 3184 | Value *Cond = IRB.CreateICmpNE(Misalign, AllocaRzSize); |
| 3185 | Value *PartialPadding = IRB.CreateSelect(Cond, Misalign, Zero); |
| 3186 | |
| 3187 | // AdditionalChunkSize = Align + PartialPadding + kAllocaRzSize |
| 3188 | // Align is added to locate left redzone, PartialPadding for possible |
| 3189 | // partial redzone and kAllocaRzSize for right redzone respectively. |
| 3190 | Value *AdditionalChunkSize = IRB.CreateAdd( |
| 3191 | ConstantInt::get(IntptrTy, Align + kAllocaRzSize), PartialPadding); |
| 3192 | |
| 3193 | Value *NewSize = IRB.CreateAdd(OldSize, AdditionalChunkSize); |
| 3194 | |
| 3195 | // Insert new alloca with new NewSize and Align params. |
| 3196 | AllocaInst *NewAlloca = IRB.CreateAlloca(IRB.getInt8Ty(), NewSize); |
| 3197 | NewAlloca->setAlignment(Align); |
| 3198 | |
| 3199 | // NewAddress = Address + Align |
| 3200 | Value *NewAddress = IRB.CreateAdd(IRB.CreatePtrToInt(NewAlloca, IntptrTy), |
| 3201 | ConstantInt::get(IntptrTy, Align)); |
| 3202 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 3203 | // Insert __asan_alloca_poison call for new created alloca. |
Yury Gribov | 781bce2 | 2015-05-28 08:03:28 +0000 | [diff] [blame] | 3204 | IRB.CreateCall(AsanAllocaPoisonFunc, {NewAddress, OldSize}); |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 3205 | |
| 3206 | // Store the last alloca's address to DynamicAllocaLayout. We'll need this |
| 3207 | // for unpoisoning stuff. |
| 3208 | IRB.CreateStore(IRB.CreatePtrToInt(NewAlloca, IntptrTy), DynamicAllocaLayout); |
| 3209 | |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3210 | Value *NewAddressPtr = IRB.CreateIntToPtr(NewAddress, AI->getType()); |
| 3211 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 3212 | // Replace all uses of AddessReturnedByAlloca with NewAddressPtr. |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3213 | AI->replaceAllUsesWith(NewAddressPtr); |
| 3214 | |
Yury Gribov | 98b1859 | 2015-05-28 07:51:49 +0000 | [diff] [blame] | 3215 | // We are done. Erase old alloca from parent. |
Yury Gribov | 55441bb | 2014-11-21 10:29:50 +0000 | [diff] [blame] | 3216 | AI->eraseFromParent(); |
| 3217 | } |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3218 | |
| 3219 | // isSafeAccess returns true if Addr is always inbounds with respect to its |
| 3220 | // base object. For example, it is a field access or an array access with |
| 3221 | // constant inbounds index. |
| 3222 | bool AddressSanitizer::isSafeAccess(ObjectSizeOffsetVisitor &ObjSizeVis, |
| 3223 | Value *Addr, uint64_t TypeSize) const { |
| 3224 | SizeOffsetType SizeOffset = ObjSizeVis.compute(Addr); |
| 3225 | if (!ObjSizeVis.bothKnown(SizeOffset)) return false; |
Dmitry Vyukov | ee84238 | 2015-03-16 08:04:26 +0000 | [diff] [blame] | 3226 | uint64_t Size = SizeOffset.first.getZExtValue(); |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3227 | int64_t Offset = SizeOffset.second.getSExtValue(); |
| 3228 | // Three checks are required to ensure safety: |
| 3229 | // . Offset >= 0 (since the offset is given from the base ptr) |
| 3230 | // . Size >= Offset (unsigned) |
| 3231 | // . Size - Offset >= NeededSize (unsigned) |
Dmitry Vyukov | ee84238 | 2015-03-16 08:04:26 +0000 | [diff] [blame] | 3232 | return Offset >= 0 && Size >= uint64_t(Offset) && |
| 3233 | Size - uint64_t(Offset) >= TypeSize / 8; |
Dmitry Vyukov | b37b95e | 2015-03-04 13:27:53 +0000 | [diff] [blame] | 3234 | } |