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