AddressSanitizer.cpp revision 39f02940ba085af79011f7e9095bf9902cd0fa6f
1800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===-- AddressSanitizer.cpp - memory error detector ------------*- C++ -*-===// 2800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// 3800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// The LLVM Compiler Infrastructure 4800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// 5800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This file is distributed under the University of Illinois Open Source 6800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// License. See LICENSE.TXT for details. 7800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// 8800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===----------------------------------------------------------------------===// 9800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// 10800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This file is a part of AddressSanitizer, an address sanity checker. 11800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Details of the algorithm: 12800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm 13800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// 14800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany//===----------------------------------------------------------------------===// 15800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 16800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#define DEBUG_TYPE "asan" 17800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Transforms/Instrumentation.h" 19800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/ArrayRef.h" 201c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov#include "llvm/ADT/DenseMap.h" 2159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov#include "llvm/ADT/DepthFirstIterator.h" 22800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/OwningPtr.h" 23800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/SmallSet.h" 24800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/SmallString.h" 25800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/SmallVector.h" 26800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/ADT/StringExtras.h" 2706fdbaa9145f01a291d4981ca5120b7bdcad44c6Evgeniy Stepanov#include "llvm/ADT/Triple.h" 281afbb517965e29b07cb42e2335d5eadd87de6535Alexey Samsonov#include "llvm/DIBuilder.h" 290b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h" 300b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h" 310b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IRBuilder.h" 320b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/InlineAsm.h" 330b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IntrinsicInst.h" 340b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h" 350b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h" 360b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Type.h" 3759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov#include "llvm/InstVisitor.h" 38800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/CommandLine.h" 39800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/DataTypes.h" 40800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/Debug.h" 41800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/raw_ostream.h" 42800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/system_error.h" 43800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Target/TargetMachine.h" 44800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Transforms/Utils/BasicBlockUtils.h" 4590230c84668269fbd53d163e398cd16486d5d414Chandler Carruth#include "llvm/Transforms/Utils/BlackList.h" 461afbb517965e29b07cb42e2335d5eadd87de6535Alexey Samsonov#include "llvm/Transforms/Utils/Local.h" 47800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Transforms/Utils/ModuleUtils.h" 48800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <algorithm> 49d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include <string> 50800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 51800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyusing namespace llvm; 52800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 53800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowScale = 3; 54800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowOffset32 = 1ULL << 29; 55800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowOffset64 = 1ULL << 44; 56117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryanystatic const uint64_t kDefaultShort64bitShadowOffset = 0x7FFF8000; // < 2G. 5748a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryanystatic const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41; 58800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 59800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const size_t kMaxStackMallocSize = 1 << 16; // 64K 60800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; 61800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; 62800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 63800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanModuleCtorName = "asan.module_ctor"; 647bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const char *kAsanModuleDtorName = "asan.module_dtor"; 657bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const int kAsanCtorAndCtorPriority = 1; 66800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanReportErrorTemplate = "__asan_report_"; 67800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanRegisterGlobalsName = "__asan_register_globals"; 687bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals"; 699b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryanystatic const char *kAsanPoisonGlobalsName = "__asan_before_dynamic_init"; 709b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryanystatic const char *kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init"; 710bc55d517e8e64f0f441736fba2447781c405ef4Kostya Serebryanystatic const char *kAsanInitName = "__asan_init_v1"; 7295e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryanystatic const char *kAsanHandleNoReturnName = "__asan_handle_no_return"; 73800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanMappingOffsetName = "__asan_mapping_offset"; 74800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanMappingScaleName = "__asan_mapping_scale"; 75800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanStackMallocName = "__asan_stack_malloc"; 76800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanStackFreeName = "__asan_stack_free"; 7751c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryanystatic const char *kAsanGenPrefix = "__asan_gen_"; 78f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonovstatic const char *kAsanPoisonStackMemoryName = "__asan_poison_stack_memory"; 79f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonovstatic const char *kAsanUnpoisonStackMemoryName = 80f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov "__asan_unpoison_stack_memory"; 81800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 82800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackLeftRedzoneMagic = 0xf1; 83800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackMidRedzoneMagic = 0xf2; 84800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackRightRedzoneMagic = 0xf3; 85800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackPartialRedzoneMagic = 0xf4; 86800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 87c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany// Accesses sizes are powers of two: 1, 2, 4, 8, 16. 88c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryanystatic const size_t kNumberOfAccessSizes = 5; 89c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany 90800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Command-line flags. 91800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 92800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no-]asan-reads. 93800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClInstrumentReads("asan-instrument-reads", 94800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("instrument read instructions"), cl::Hidden, cl::init(true)); 95800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClInstrumentWrites("asan-instrument-writes", 96800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("instrument write instructions"), cl::Hidden, cl::init(true)); 97e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryanystatic cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics", 98e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany cl::desc("instrument atomic instructions (rmw, cmpxchg)"), 99e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany cl::Hidden, cl::init(true)); 1006e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryanystatic cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path", 1016e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany cl::desc("use instrumentation with slow path for all accesses"), 1026e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany cl::Hidden, cl::init(false)); 103c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany// This flag limits the number of instructions to be instrumented 104324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany// in any given BB. Normally, this should be set to unlimited (INT_MAX), 105324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary 106324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany// set it to 10000. 107324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryanystatic cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb", 108324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany cl::init(10000), 109324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany cl::desc("maximal number of instructions to instrument in any given BB"), 110324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany cl::Hidden); 111800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-stack. 112800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClStack("asan-stack", 113800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Handle stack memory"), cl::Hidden, cl::init(true)); 114800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-use-after-return. 115800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClUseAfterReturn("asan-use-after-return", 116800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Check return-after-free"), cl::Hidden, cl::init(false)); 117800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-globals. 118800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClGlobals("asan-globals", 119800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Handle global objects"), cl::Hidden, cl::init(true)); 1209b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryanystatic cl::opt<bool> ClInitializers("asan-initialization-order", 1219b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false)); 122800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClMemIntrin("asan-memintrin", 123800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true)); 1246c55412ea4b39103a3a5764d49ddfdf50e066d56Kostya Serebryanystatic cl::opt<bool> ClRealignStack("asan-realign-stack", 1256c55412ea4b39103a3a5764d49ddfdf50e066d56Kostya Serebryany cl::desc("Realign stack to 32"), cl::Hidden, cl::init(true)); 126b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonovstatic cl::opt<std::string> ClBlacklistFile("asan-blacklist", 127b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov cl::desc("File containing the list of objects to ignore " 128800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany "during instrumentation"), cl::Hidden); 129800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 130800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// These flags allow to change the shadow mapping. 131800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// The shadow mapping looks like 132800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Shadow = (Mem >> scale) + (1 << offset_log) 133800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClMappingScale("asan-mapping-scale", 134800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0)); 135800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log", 136800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1)); 137117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryanystatic cl::opt<bool> ClShort64BitOffset("asan-short-64bit-mapping-offset", 138117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryany cl::desc("Use short immediate constant as the mapping offset for 64bit"), 1390bc55d517e8e64f0f441736fba2447781c405ef4Kostya Serebryany cl::Hidden, cl::init(true)); 140800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 141800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Optimization flags. Not user visible, used mostly for testing 142800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// and benchmarking the tool. 143800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOpt("asan-opt", 144800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true)); 145800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOptSameTemp("asan-opt-same-temp", 146800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Instrument the same temp just once"), cl::Hidden, 147800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::init(true)); 148800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOptGlobals("asan-opt-globals", 149800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true)); 150800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 151ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonovstatic cl::opt<bool> ClCheckLifetime("asan-check-lifetime", 152ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov cl::desc("Use llvm.lifetime intrinsics to insert extra checks"), 153ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov cl::Hidden, cl::init(false)); 154ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov 155800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Debug flags. 156800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden, 157800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::init(0)); 158800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"), 159800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::init(0)); 160800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<std::string> ClDebugFunc("asan-debug-func", 161800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::desc("Debug func")); 162800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"), 163800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::init(-1)); 164800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"), 165800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::init(-1)); 166800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 167800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanynamespace { 168ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany/// A set of dynamically initialized globals extracted from metadata. 169ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryanyclass SetOfDynamicallyInitializedGlobals { 170ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany public: 171ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany void Init(Module& M) { 172ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany // Clang generates metadata identifying all dynamically initialized globals. 173ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany NamedMDNode *DynamicGlobals = 174ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany M.getNamedMetadata("llvm.asan.dynamically_initialized_globals"); 175ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany if (!DynamicGlobals) 176ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany return; 177ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) { 178ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany MDNode *MDN = DynamicGlobals->getOperand(i); 179ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany assert(MDN->getNumOperands() == 1); 180ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany Value *VG = MDN->getOperand(0); 181ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany // The optimizer may optimize away a global entirely, in which case we 182ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany // cannot instrument access to it. 183ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany if (!VG) 184ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany continue; 185ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany DynInitGlobals.insert(cast<GlobalVariable>(VG)); 186ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany } 187ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany } 188ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; } 189ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany private: 190ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany SmallSet<GlobalValue*, 32> DynInitGlobals; 191ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany}; 192ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany 19319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov/// This struct defines the shadow mapping using the rule: 19448a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany/// shadow = (mem >> Scale) ADD-or-OR Offset. 19519cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonovstruct ShadowMapping { 19619cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov int Scale; 19719cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov uint64_t Offset; 19848a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany bool OrShadowOffset; 19919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov}; 20019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 20111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonovstatic ShadowMapping getShadowMapping(const Module &M, int LongSize, 20211af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow) { 20311af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov llvm::Triple TargetTriple(M.getTargetTriple()); 20411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android; 205c8a196ae8fad3cba7a777e2e7916fd36ebf70fe6Alexander Potapenko bool IsMacOSX = TargetTriple.getOS() == llvm::Triple::MacOSX; 20648a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64; 2070bc55d517e8e64f0f441736fba2447781c405ef4Kostya Serebryany bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64; 20819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 20919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 21019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 21148a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany // OR-ing shadow offset if more efficient (at least on x86), 21248a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany // but on ppc64 we have to use add since the shadow offset is not neccesary 21348a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany // 1/8-th of the address space. 214117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryany Mapping.OrShadowOffset = !IsPPC64 && !ClShort64BitOffset; 21548a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany 21611af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov Mapping.Offset = (IsAndroid || ZeroBaseShadow) ? 0 : 21748a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany (LongSize == 32 ? kDefaultShadowOffset32 : 21848a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany IsPPC64 ? kPPC64_ShadowOffset64 : kDefaultShadowOffset64); 219c8a196ae8fad3cba7a777e2e7916fd36ebf70fe6Alexander Potapenko if (!ZeroBaseShadow && ClShort64BitOffset && IsX86_64 && !IsMacOSX) { 2200bc55d517e8e64f0f441736fba2447781c405ef4Kostya Serebryany assert(LongSize == 64); 221117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryany Mapping.Offset = kDefaultShort64bitShadowOffset; 22239f02940ba085af79011f7e9095bf9902cd0fa6fKostya Serebryany } 22339f02940ba085af79011f7e9095bf9902cd0fa6fKostya Serebryany if (!ZeroBaseShadow && ClMappingOffsetLog >= 0) { 22419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov // Zero offset log is the special case. 22519cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping.Offset = (ClMappingOffsetLog == 0) ? 0 : 1ULL << ClMappingOffsetLog; 22619cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 22719cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 22819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping.Scale = kDefaultShadowScale; 22919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov if (ClMappingScale) { 23019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping.Scale = ClMappingScale; 23119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 23219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 23319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return Mapping; 234b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany} 235b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 23619cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonovstatic size_t RedzoneSizeForScale(int MappingScale) { 237b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany // Redzone used for stack and globals is at least 32 bytes. 238b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively. 23919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return std::max(32U, 1U << MappingScale); 240b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany} 241ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany 242800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany/// AddressSanitizer: instrument the code in module to find memory bugs. 243ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanystruct AddressSanitizer : public FunctionPass { 244ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov AddressSanitizer(bool CheckInitOrder = false, 245ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckUseAfterReturn = false, 246b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov bool CheckLifetime = false, 24711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov StringRef BlacklistFile = StringRef(), 24811af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow = false) 249ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov : FunctionPass(ID), 250ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov CheckInitOrder(CheckInitOrder || ClInitializers), 251ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn), 252b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov CheckLifetime(CheckLifetime || ClCheckLifetime), 253b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile 25411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov : BlacklistFile), 25511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ZeroBaseShadow(ZeroBaseShadow) {} 2561416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany virtual const char *getPassName() const { 2571416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany return "AddressSanitizerFunctionPass"; 2581416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany } 259ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany void instrumentMop(Instruction *I); 260ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB, 261800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Addr, uint32_t TypeSize, bool IsWrite); 262c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, 263c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *ShadowValue, uint32_t TypeSize); 264ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr, 2652735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany bool IsWrite, size_t AccessSizeIndex); 266ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany bool instrumentMemIntrinsic(MemIntrinsic *MI); 267ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr, 268c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *Size, 269800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *InsertBefore, bool IsWrite); 270800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); 271ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany bool runOnFunction(Function &F); 2729b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany void createInitializerPoisonCalls(Module &M, 2739b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Value *FirstAddr, Value *LastAddr); 274a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany bool maybeInsertAsanInitAtFunctionEntry(Function &F); 27519cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov void emitShadowMapping(Module &M, IRBuilder<> &IRB) const; 276ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany virtual bool doInitialization(Module &M); 277800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany static char ID; // Pass identification, replacement for typeid 278800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 279800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany private: 2808b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany void initializeCallbacks(Module &M); 281800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 2829b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany bool ShouldInstrumentGlobal(GlobalVariable *G); 2835a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany bool LooksLikeCodeInBug11395(Instruction *I); 2849b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany void FindDynamicInitializers(Module &M); 285800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 286ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckInitOrder; 287ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckUseAfterReturn; 288ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckLifetime; 28911af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov SmallString<64> BlacklistFile; 29011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow; 29111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov 292800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany LLVMContext *C; 2933574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow DataLayout *TD; 294800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany int LongSize; 295800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *IntptrTy; 29619cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 297800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Function *AsanCtorFunction; 298800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Function *AsanInitFunction; 299ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany Function *AsanHandleNoReturnFunc; 300b5b86d263a651566cb25c0f406f75ceffb771029Kostya Serebryany OwningPtr<BlackList> BL; 3019db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany // This array is indexed by AccessIsWrite and log2(AccessSize). 3029db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany Function *AsanErrorCallback[2][kNumberOfAccessSizes]; 303f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany InlineAsm *EmptyAsm; 304ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals; 30559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 30659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov friend struct FunctionStackPoisoner; 307800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}; 308c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany 3091416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanyclass AddressSanitizerModule : public ModulePass { 310b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany public: 311b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov AddressSanitizerModule(bool CheckInitOrder = false, 31211af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov StringRef BlacklistFile = StringRef(), 31311af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow = false) 314ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov : ModulePass(ID), 315b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov CheckInitOrder(CheckInitOrder || ClInitializers), 316b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile 31711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov : BlacklistFile), 31811af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ZeroBaseShadow(ZeroBaseShadow) {} 3191416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany bool runOnModule(Module &M); 3201416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany static char ID; // Pass identification, replacement for typeid 3211416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany virtual const char *getPassName() const { 3221416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany return "AddressSanitizerModule"; 3231416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany } 324f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov 325b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany private: 3264684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov void initializeCallbacks(Module &M); 3274684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov 328b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany bool ShouldInstrumentGlobal(GlobalVariable *G); 329b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany void createInitializerPoisonCalls(Module &M, Value *FirstAddr, 330b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Value *LastAddr); 33119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t RedzoneSize() const { 33219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return RedzoneSizeForScale(Mapping.Scale); 33319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 334b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 335ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckInitOrder; 336b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov SmallString<64> BlacklistFile; 33711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow; 33811af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov 339b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany OwningPtr<BlackList> BL; 340b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals; 341b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Type *IntptrTy; 342b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany LLVMContext *C; 3431416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany DataLayout *TD; 34419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 3454684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanPoisonGlobals; 3464684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanUnpoisonGlobals; 3474684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanRegisterGlobals; 3484684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanUnregisterGlobals; 349b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany}; 350b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 35159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// Stack poisoning does not play well with exception handling. 35259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// When an exception is thrown, we essentially bypass the code 35359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// that unpoisones the stack. This is why the run-time library has 35459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire 35559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// stack in the interceptor. This however does not work inside the 35659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// actual function which catches the exception. Most likely because the 35759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// compiler hoists the load of the shadow value somewhere too high. 35859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// This causes asan to report a non-existing bug on 453.povray. 35959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// It sounds like an LLVM bug. 36059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovstruct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> { 36159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Function &F; 36259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AddressSanitizer &ASan; 36359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov DIBuilder DIB; 36459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov LLVMContext *C; 36559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Type *IntptrTy; 36659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Type *IntptrPtrTy; 36719cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 36859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 36959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov SmallVector<AllocaInst*, 16> AllocaVec; 37059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov SmallVector<Instruction*, 8> RetVec; 37159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t TotalStackSize; 37259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov unsigned StackAlignment; 37359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 37459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Function *AsanStackMallocFunc, *AsanStackFreeFunc; 37559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc; 37659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 3771c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Stores a place and arguments of poisoning/unpoisoning call for alloca. 3781c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov struct AllocaPoisonCall { 3791c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov IntrinsicInst *InsBefore; 3801c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov uint64_t Size; 3811c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool DoPoison; 3821c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov }; 3831c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec; 3841c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 3851c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Maps Value to an AllocaInst from which the Value is originated. 3861c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy; 3871c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValueMapTy AllocaForValue; 3881c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 38959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov FunctionStackPoisoner(Function &F, AddressSanitizer &ASan) 39059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C), 39159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)), 39219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping(ASan.Mapping), 39319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov TotalStackSize(0), StackAlignment(1 << Mapping.Scale) {} 39459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 39559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool runOnFunction() { 39659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (!ClStack) return false; 39759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // Collect alloca, ret, lifetime instructions etc. 39859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), 39959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) { 40059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov BasicBlock *BB = *DI; 40159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov visit(*BB); 40259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 40359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (AllocaVec.empty()) return false; 40459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 40559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov initializeCallbacks(*F.getParent()); 40659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 40759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov poisonStack(); 40859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 40959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (ClDebugStack) { 41059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov DEBUG(dbgs() << F); 41159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 41259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return true; 41359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 41459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 41559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // Finds all static Alloca instructions and puts 41659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // poisoned red zones around all of them. 41759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // Then unpoison everything back before the function returns. 41859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void poisonStack(); 41959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 42059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // ----------------------- Visitors. 42159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov /// \brief Collect all Ret instructions. 42259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void visitReturnInst(ReturnInst &RI) { 42359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov RetVec.push_back(&RI); 42459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 42559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 42659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov /// \brief Collect Alloca instructions we want (and can) handle. 42759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void visitAllocaInst(AllocaInst &AI) { 4281c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (!isInterestingAlloca(AI)) return; 42959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 43059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov StackAlignment = std::max(StackAlignment, AI.getAlignment()); 43159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AllocaVec.push_back(&AI); 43259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t AlignedSize = getAlignedAllocaSize(&AI); 43359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov TotalStackSize += AlignedSize; 43459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 43559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 4361c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov /// \brief Collect lifetime intrinsic calls to check for use-after-scope 4371c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov /// errors. 4381c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov void visitIntrinsicInst(IntrinsicInst &II) { 4391c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (!ASan.CheckLifetime) return; 4401c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Intrinsic::ID ID = II.getIntrinsicID(); 4411c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (ID != Intrinsic::lifetime_start && 4421c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov ID != Intrinsic::lifetime_end) 4431c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return; 4441c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Found lifetime intrinsic, add ASan instrumentation if necessary. 4451c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0)); 4461c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // If size argument is undefined, don't do anything. 4471c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (Size->isMinusOne()) return; 4481c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Check that size doesn't saturate uint64_t and can 4491c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // be stored in IntptrTy. 4501c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov const uint64_t SizeValue = Size->getValue().getLimitedValue(); 4511c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (SizeValue == ~0ULL || 4521c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov !ConstantInt::isValueValidForType(IntptrTy, SizeValue)) 4531c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return; 4541c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Find alloca instruction that corresponds to llvm.lifetime argument. 4551c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *AI = findAllocaForValue(II.getArgOperand(1)); 4561c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (!AI) return; 4571c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool DoPoison = (ID == Intrinsic::lifetime_end); 4581c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaPoisonCall APC = {&II, SizeValue, DoPoison}; 4591c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaPoisonCallVec.push_back(APC); 4601c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov } 4611c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 46259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // ---------------------- Helpers. 46359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void initializeCallbacks(Module &M); 46459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 4651c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Check if we want (and can) handle this alloca. 4661c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool isInterestingAlloca(AllocaInst &AI) { 4671c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return (!AI.isArrayAllocation() && 4681c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AI.isStaticAlloca() && 4691c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AI.getAllocatedType()->isSized()); 4701c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov } 4711c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 47219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t RedzoneSize() const { 47319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return RedzoneSizeForScale(Mapping.Scale); 47419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 47559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t getAllocaSizeInBytes(AllocaInst *AI) { 47659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Type *Ty = AI->getAllocatedType(); 47759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t SizeInBytes = ASan.TD->getTypeAllocSize(Ty); 47859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return SizeInBytes; 47959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 48059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t getAlignedSize(uint64_t SizeInBytes) { 48159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov size_t RZ = RedzoneSize(); 48259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return ((SizeInBytes + RZ - 1) / RZ) * RZ; 48359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 48459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t getAlignedAllocaSize(AllocaInst *AI) { 48559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t SizeInBytes = getAllocaSizeInBytes(AI); 48659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return getAlignedSize(SizeInBytes); 48759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 4881c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov /// Finds alloca where the value comes from. 4891c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *findAllocaForValue(Value *V); 49059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void poisonRedZones(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB, 49159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Value *ShadowBase, bool DoPoison); 49259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> IRB, bool DoPoison); 49359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov}; 49459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 495800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} // namespace 496800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 497800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanychar AddressSanitizer::ID = 0; 498800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyINITIALIZE_PASS(AddressSanitizer, "asan", 499800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", 500800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany false, false) 501ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey SamsonovFunctionPass *llvm::createAddressSanitizerFunctionPass( 502b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime, 50311af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov StringRef BlacklistFile, bool ZeroBaseShadow) { 504ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn, 50511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov CheckLifetime, BlacklistFile, ZeroBaseShadow); 506800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 507800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 5081416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanychar AddressSanitizerModule::ID = 0; 5091416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya SerebryanyINITIALIZE_PASS(AddressSanitizerModule, "asan-module", 5101416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany "AddressSanitizer: detects use-after-free and out-of-bounds bugs." 5111416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany "ModulePass", false, false) 512b0dcf61252e58715a3bea79f4c112572df361c30Alexey SamsonovModulePass *llvm::createAddressSanitizerModulePass( 51311af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool CheckInitOrder, StringRef BlacklistFile, bool ZeroBaseShadow) { 51411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov return new AddressSanitizerModule(CheckInitOrder, BlacklistFile, 51511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ZeroBaseShadow); 51625878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko} 51725878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko 5182735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryanystatic size_t TypeSizeToSizeIndex(uint32_t TypeSize) { 5192735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany size_t Res = CountTrailingZeros_32(TypeSize / 8); 5202735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany assert(Res < kNumberOfAccessSizes); 5212735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany return Res; 5222735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany} 5232735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany 524800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Create a constant for Str so that we can pass it to the run-time lib. 525800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) { 52618c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); 527800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return new GlobalVariable(M, StrConst->getType(), true, 52851c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany GlobalValue::PrivateLinkage, StrConst, 52951c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany kAsanGenPrefix); 53051c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany} 53151c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany 53251c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryanystatic bool GlobalWasGeneratedByAsan(GlobalVariable *G) { 53351c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany return G->getName().find(kAsanGenPrefix) == 0; 534800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 535800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 536800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyValue *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) { 537800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Shadow >> scale 53819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); 53919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov if (Mapping.Offset == 0) 540800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return Shadow; 541800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // (Shadow >> scale) | offset 54248a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany if (Mapping.OrShadowOffset) 54348a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); 54448a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany else 54548a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); 546800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 547800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 548c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryanyvoid AddressSanitizer::instrumentMemIntrinsicParam( 549ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany Instruction *OrigIns, 550800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) { 551800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Check the first byte. 552800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany { 553800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(InsertBefore); 554ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite); 555800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 556800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Check the last byte. 557800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany { 558800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(InsertBefore); 559800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *SizeMinusOne = IRB.CreateSub( 560800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Size, ConstantInt::get(Size->getType(), 1)); 561800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false); 562800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); 563800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne); 564ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite); 565800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 566800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 567800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 568800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Instrument memset/memmove/memcpy 569ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanybool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) { 570800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Dst = MI->getDest(); 571800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI); 5722735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany Value *Src = MemTran ? MemTran->getSource() : 0; 573800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Length = MI->getLength(); 574800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 575800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Constant *ConstLength = dyn_cast<Constant>(Length); 576800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *InsertBefore = MI; 577800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ConstLength) { 578800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ConstLength->isNullValue()) return false; 579800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else { 580800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // The size is not a constant so it could be zero -- check at run-time. 581800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(InsertBefore); 582800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 583800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Cmp = IRB.CreateICmpNE(Length, 58456139bc493790612ee6281630678e293be6b2eb2Kostya Serebryany Constant::getNullValue(Length->getType())); 5854a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov InsertBefore = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false); 586800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 587800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 588ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true); 589800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (Src) 590ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false); 591800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return true; 592800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 593800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 594e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany// If I is an interesting memory access, return the PointerOperand 595e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany// and set IsWrite. Otherwise return NULL. 596e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryanystatic Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) { 597800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 598e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentReads) return NULL; 599e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = false; 600800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return LI->getPointerOperand(); 601800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 602e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 603e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentWrites) return NULL; 604e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = true; 605e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return SI->getPointerOperand(); 606e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany } 607e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { 608e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentAtomics) return NULL; 609e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = true; 610e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return RMW->getPointerOperand(); 611e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany } 612e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { 613e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentAtomics) return NULL; 614e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = true; 615e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return XCHG->getPointerOperand(); 616e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany } 617e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return NULL; 618800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 619800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 620ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanyvoid AddressSanitizer::instrumentMop(Instruction *I) { 6213780ad8b998d93d7db406919c06137cdb786ef05Axel Naumann bool IsWrite = false; 622e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany Value *Addr = isInterestingMemoryAccess(I, &IsWrite); 623e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany assert(Addr); 6249b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (ClOpt && ClOptGlobals) { 6259b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) { 6269b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // If initialization order checking is disabled, a simple access to a 6279b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // dynamically initialized global is always valid. 628ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov if (!CheckInitOrder) 6299b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return; 6309b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // If a global variable does not have dynamic initialization we don't 631407790604b8f71f7172bbdfb76c27e1799d241c2Kostya Serebryany // have to instrument it. However, if a global does not have initailizer 632407790604b8f71f7172bbdfb76c27e1799d241c2Kostya Serebryany // at all, we assume it has dynamic initializer (in other TU). 633407790604b8f71f7172bbdfb76c27e1799d241c2Kostya Serebryany if (G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G)) 6349b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return; 6359b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 636800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 6379b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 638800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *OrigPtrTy = Addr->getType(); 639800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType(); 640800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 641800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(OrigTy->isSized()); 642800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy); 643800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 644800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (TypeSize != 8 && TypeSize != 16 && 645800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany TypeSize != 32 && TypeSize != 64 && TypeSize != 128) { 646800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Ignore all unusual sizes. 647800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return; 648800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 649800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 650800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(I); 651ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentAddress(I, IRB, Addr, TypeSize, IsWrite); 652800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 653800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 65455cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// Validate the result of Module::getOrInsertFunction called for an interface 65555cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// function of AddressSanitizer. If the instrumented module defines a function 65655cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// with the same name, their prototypes must match, otherwise 65755cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// getOrInsertFunction returns a bitcast. 658b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryanystatic Function *checkInterfaceFunction(Constant *FuncOrBitcast) { 65955cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast); 66055cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko FuncOrBitcast->dump(); 66155cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko report_fatal_error("trying to redefine an AddressSanitizer " 66255cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko "interface function"); 66355cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko} 66455cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko 665800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyInstruction *AddressSanitizer::generateCrashCode( 666ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Instruction *InsertBefore, Value *Addr, 6674f0c69623c10a3a49f6926fd53694ee532e06a85Kostya Serebryany bool IsWrite, size_t AccessSizeIndex) { 668ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany IRBuilder<> IRB(InsertBefore); 669ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany CallInst *Call = IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex], 670ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Addr); 671f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // We don't do Call->setDoesNotReturn() because the BB already has 672f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // UnreachableInst at the end. 673f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // This EmptyAsm is required to avoid callback merge. 674f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany IRB.CreateCall(EmptyAsm); 6753c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany return Call; 676800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 677800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 6782735cf4aa52e31b8d2de90f836c3ad991215e04eKostya SerebryanyValue *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, 679c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *ShadowValue, 680c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany uint32_t TypeSize) { 68119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t Granularity = 1 << Mapping.Scale; 682c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // Addr & (Granularity - 1) 683c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *LastAccessedByte = IRB.CreateAnd( 684c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany AddrLong, ConstantInt::get(IntptrTy, Granularity - 1)); 685c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // (Addr & (Granularity - 1)) + size - 1 686c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany if (TypeSize / 8 > 1) 687c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany LastAccessedByte = IRB.CreateAdd( 688c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)); 689c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // (uint8_t) ((Addr & (Granularity-1)) + size - 1) 690c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany LastAccessedByte = IRB.CreateIntCast( 6916e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany LastAccessedByte, ShadowValue->getType(), false); 692c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue 693c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue); 694c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany} 695c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany 696ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanyvoid AddressSanitizer::instrumentAddress(Instruction *OrigIns, 697800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> &IRB, Value *Addr, 698800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint32_t TypeSize, bool IsWrite) { 699800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); 700800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 701800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *ShadowTy = IntegerType::get( 70219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov *C, std::max(8U, TypeSize >> Mapping.Scale)); 703800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); 704800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *ShadowPtr = memToShadow(AddrLong, IRB); 705800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *CmpVal = Constant::getNullValue(ShadowTy); 706800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *ShadowValue = IRB.CreateLoad( 707800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy)); 708800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 709800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal); 71011c2a47af825a0f89d75aaa97ad873ed2acef266Kostya Serebryany size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize); 71119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t Granularity = 1 << Mapping.Scale; 712ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany TerminatorInst *CrashTerm = 0; 713ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany 7146e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) { 7154a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov TerminatorInst *CheckTerm = 7164a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false); 717ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional()); 718f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany BasicBlock *NextBB = CheckTerm->getSuccessor(0); 719c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany IRB.SetInsertPoint(CheckTerm); 720c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize); 721ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany BasicBlock *CrashBlock = 722ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany BasicBlock::Create(*C, "", NextBB->getParent(), NextBB); 723ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany CrashTerm = new UnreachableInst(*C, CrashBlock); 724f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2); 725f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany ReplaceInstWithInst(CheckTerm, NewTerm); 726c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany } else { 7274a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov CrashTerm = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), true); 728800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 729ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany 730ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Instruction *Crash = 731ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex); 732ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Crash->setDebugLoc(OrigIns->getDebugLoc()); 733800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 734800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 7351416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanyvoid AddressSanitizerModule::createInitializerPoisonCalls( 736b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Module &M, Value *FirstAddr, Value *LastAddr) { 7379b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // We do all of our poisoning and unpoisoning within _GLOBAL__I_a. 7389b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Function *GlobalInit = M.getFunction("_GLOBAL__I_a"); 7399b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // If that function is not present, this TU contains no globals, or they have 7409b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // all been optimized away 7419b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (!GlobalInit) 7429b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return; 7439b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7449b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Set up the arguments to our poison/unpoison functions. 7459b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt()); 7469b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7479b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Add a call to poison all external globals before the given function starts. 7489b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany IRB.CreateCall2(AsanPoisonGlobals, FirstAddr, LastAddr); 7499b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7509b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Add calls to unpoison all globals before each return instruction. 7519b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end(); 7529b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany I != E; ++I) { 7539b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) { 7549b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany CallInst::Create(AsanUnpoisonGlobals, "", RI); 7559b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 7569b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 7579b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany} 7589b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7591416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanybool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) { 7609b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Type *Ty = cast<PointerType>(G->getType())->getElementType(); 761324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "GLOBAL: " << *G << "\n"); 7629b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 76359a4a47a7bea7cc17877c6a3954ad9f8309ff1cbKostya Serebryany if (BL->isIn(*G)) return false; 7649b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (!Ty->isSized()) return false; 7659b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (!G->hasInitializer()) return false; 76651c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany if (GlobalWasGeneratedByAsan(G)) return false; // Our own global. 7679b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Touch only those globals that will not be defined in other modules. 7689b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Don't handle ODR type linkages since other modules may be built w/o asan. 7699b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (G->getLinkage() != GlobalVariable::ExternalLinkage && 7709b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany G->getLinkage() != GlobalVariable::PrivateLinkage && 7719b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany G->getLinkage() != GlobalVariable::InternalLinkage) 7729b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 7739b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Two problems with thread-locals: 7749b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // - The address of the main thread's copy can't be computed at link-time. 7759b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // - Need to poison all copies, not just the main thread's one. 7769b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (G->isThreadLocal()) 7779b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 7789b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // For now, just ignore this Alloca if the alignment is large. 779b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany if (G->getAlignment() > RedzoneSize()) return false; 7809b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7819b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Ignore all the globals with the names starting with "\01L_OBJC_". 7829b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Many of those are put into the .cstring section. The linker compresses 7839b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // that section by removing the spare \0s after the string terminator, so 7849b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // our redzones get broken. 7859b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if ((G->getName().find("\01L_OBJC_") == 0) || 7869b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany (G->getName().find("\01l_OBJC_") == 0)) { 7879b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G); 7889b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 7899b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 7909b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7919b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (G->hasSection()) { 7929b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany StringRef Section(G->getSection()); 7939b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Ignore the globals from the __OBJC section. The ObjC runtime assumes 7949b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to 7959b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // them. 7969b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if ((Section.find("__OBJC,") == 0) || 7979b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany (Section.find("__DATA, __objc_") == 0)) { 7989b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G); 7999b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 8009b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 8019b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // See http://code.google.com/p/address-sanitizer/issues/detail?id=32 8029b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Constant CFString instances are compiled in the following way: 8039b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // -- the string buffer is emitted into 8049b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // __TEXT,__cstring,cstring_literals 8059b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // -- the constant NSConstantString structure referencing that buffer 8069b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // is placed into __DATA,__cfstring 8079b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Therefore there's no point in placing redzones into __DATA,__cfstring. 8089b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Moreover, it causes the linker to crash on OS X 10.7 8099b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (Section.find("__DATA,__cfstring") == 0) { 8109b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany DEBUG(dbgs() << "Ignoring CFString: " << *G); 8119b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 8129b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 8139b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 8149b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 8159b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return true; 8169b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany} 8179b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 8184684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonovvoid AddressSanitizerModule::initializeCallbacks(Module &M) { 8194684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov IRBuilder<> IRB(*C); 8204684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov // Declare our poisoning and unpoisoning functions. 8214684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8224684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 8234684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanPoisonGlobals->setLinkage(Function::ExternalLinkage); 8244684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8254684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL)); 8264684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage); 8274684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov // Declare functions that register/unregister globals. 8284684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8294684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanRegisterGlobalsName, IRB.getVoidTy(), 8304684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov IntptrTy, IntptrTy, NULL)); 8314684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanRegisterGlobals->setLinkage(Function::ExternalLinkage); 8324684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8334684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanUnregisterGlobalsName, 8344684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 8354684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage); 8364684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov} 8374684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov 838800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This function replaces all global variables with new variables that have 839800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// trailing redzones. It also creates a function that poisons 840800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// redzones and inserts this function into llvm.global_ctors. 8411416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanybool AddressSanitizerModule::runOnModule(Module &M) { 8421416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany if (!ClGlobals) return false; 8431416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany TD = getAnalysisIfAvailable<DataLayout>(); 8441416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany if (!TD) 8451416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany return false; 846b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BL.reset(new BlackList(BlacklistFile)); 847d6f62c8da5aa4f3388cec1542309ffa623cac601Alexey Samsonov if (BL->isIn(M)) return false; 848b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany C = &(M.getContext()); 84919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov int LongSize = TD->getPointerSizeInBits(); 85019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov IntptrTy = Type::getIntNTy(*C, LongSize); 85111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov Mapping = getShadowMapping(M, LongSize, ZeroBaseShadow); 8524684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov initializeCallbacks(M); 8534684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov DynamicallyInitializedGlobals.Init(M); 854b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 855800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallVector<GlobalVariable *, 16> GlobalsToChange; 856800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 8579b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany for (Module::GlobalListType::iterator G = M.global_begin(), 8589b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany E = M.global_end(); G != E; ++G) { 8599b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (ShouldInstrumentGlobal(G)) 8609b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany GlobalsToChange.push_back(G); 861800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 862800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 863800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany size_t n = GlobalsToChange.size(); 864800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (n == 0) return false; 865800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 866800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // A global is described by a structure 867800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // size_t beg; 868800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // size_t size; 869800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // size_t size_with_redzone; 870800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // const char *name; 8719b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // size_t has_dynamic_init; 872800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // We initialize an array of such structures and pass it to a run-time call. 873800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy, 8749b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany IntptrTy, IntptrTy, 8759b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany IntptrTy, NULL); 8769b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany SmallVector<Constant *, 16> Initializers(n), DynamicInit; 877800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 878b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 879b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Function *CtorFunc = M.getFunction(kAsanModuleCtorName); 880b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany assert(CtorFunc); 881b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator()); 882800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 8839b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // The addresses of the first and last dynamically initialized globals in 8849b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // this TU. Used in initialization order checking. 8859b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Value *FirstDynamic = 0, *LastDynamic = 0; 8869b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 887800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0; i < n; i++) { 88829f975f8ffda1f5d78cbf2530c2316abef11aa70Kostya Serebryany static const uint64_t kMaxGlobalRedzone = 1 << 18; 889800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalVariable *G = GlobalsToChange[i]; 890800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany PointerType *PtrTy = cast<PointerType>(G->getType()); 891800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *Ty = PtrTy->getElementType(); 892208a4ff2b56f453910bb817540f34b8169f7702aKostya Serebryany uint64_t SizeInBytes = TD->getTypeAllocSize(Ty); 89329f975f8ffda1f5d78cbf2530c2316abef11aa70Kostya Serebryany uint64_t MinRZ = RedzoneSize(); 89463f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany // MinRZ <= RZ <= kMaxGlobalRedzone 89563f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany // and trying to make RZ to be ~ 1/4 of SizeInBytes. 89629f975f8ffda1f5d78cbf2530c2316abef11aa70Kostya Serebryany uint64_t RZ = std::max(MinRZ, 89763f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany std::min(kMaxGlobalRedzone, 89863f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany (SizeInBytes / MinRZ / 4) * MinRZ)); 89963f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany uint64_t RightRedzoneSize = RZ; 90063f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany // Round up to MinRZ 90163f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany if (SizeInBytes % MinRZ) 90263f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ); 90363f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0); 904800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize); 9059b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Determine whether this global should be poisoned in initialization. 906ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany bool GlobalHasDynamicInitializer = 907ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany DynamicallyInitializedGlobals.Contains(G); 90859a4a47a7bea7cc17877c6a3954ad9f8309ff1cbKostya Serebryany // Don't check initialization order if this global is blacklisted. 9097dadac65d375ff21b7a36bfb7642578d2f467525Kostya Serebryany GlobalHasDynamicInitializer &= !BL->isInInit(*G); 910800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 911800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL); 912800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Constant *NewInitializer = ConstantStruct::get( 913800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NewTy, G->getInitializer(), 914800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Constant::getNullValue(RightRedZoneTy), NULL); 915800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 916a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany SmallString<2048> DescriptionOfGlobal = G->getName(); 917a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany DescriptionOfGlobal += " ("; 918a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany DescriptionOfGlobal += M.getModuleIdentifier(); 919a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany DescriptionOfGlobal += ")"; 920a4b2b1d8fbb84b0b6fac5abce21a2a5c5e907282Kostya Serebryany GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal); 921800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 922800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Create a new global variable with enough space for a redzone. 923800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalVariable *NewGlobal = new GlobalVariable( 924800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany M, NewTy, G->isConstant(), G->getLinkage(), 925ce718ff9f42c7da092eaa01dd0242e8d5ba84713Hans Wennborg NewInitializer, "", G, G->getThreadLocalMode()); 926800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NewGlobal->copyAttributesFrom(G); 92763f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany NewGlobal->setAlignment(MinRZ); 928800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 929800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Indices2[2]; 930800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Indices2[0] = IRB.getInt32(0); 931800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Indices2[1] = IRB.getInt32(0); 932800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 933800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany G->replaceAllUsesWith( 934f1639abf1aaba1448f719f595156cd0f4cd560ccKostya Serebryany ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true)); 935800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NewGlobal->takeName(G); 936800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany G->eraseFromParent(); 937800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 938800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Initializers[i] = ConstantStruct::get( 939800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalStructTy, 940800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantExpr::getPointerCast(NewGlobal, IntptrTy), 941800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, SizeInBytes), 942800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize), 943800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantExpr::getPointerCast(Name, IntptrTy), 9449b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer), 945800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NULL); 9469b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 9479b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Populate the first and last globals declared in this TU. 948ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov if (CheckInitOrder && GlobalHasDynamicInitializer) { 9499b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany LastDynamic = ConstantExpr::getPointerCast(NewGlobal, IntptrTy); 9509b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (FirstDynamic == 0) 9519b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany FirstDynamic = LastDynamic; 9529b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 9539b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 954324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n"); 955800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 956800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 957800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n); 958800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalVariable *AllGlobals = new GlobalVariable( 959800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage, 960800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantArray::get(ArrayOfGlobalStructTy, Initializers), ""); 961800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 9629b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Create calls for poisoning before initializers run and unpoisoning after. 963ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov if (CheckInitOrder && FirstDynamic && LastDynamic) 9649b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany createInitializerPoisonCalls(M, FirstDynamic, LastDynamic); 965800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateCall2(AsanRegisterGlobals, 966800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreatePointerCast(AllGlobals, IntptrTy), 967800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, n)); 968800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 9697bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany // We also need to unregister globals at the end, e.g. when a shared library 9707bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany // gets closed. 9717bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany Function *AsanDtorFunction = Function::Create( 9727bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany FunctionType::get(Type::getVoidTy(*C), false), 9737bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany GlobalValue::InternalLinkage, kAsanModuleDtorName, &M); 9747bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); 9757bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB)); 9767bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany IRB_Dtor.CreateCall2(AsanUnregisterGlobals, 9777bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany IRB.CreatePointerCast(AllGlobals, IntptrTy), 9787bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany ConstantInt::get(IntptrTy, n)); 9797bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority); 9807bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany 981800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany DEBUG(dbgs() << M); 982800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return true; 983800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 984800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 9858b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryanyvoid AddressSanitizer::initializeCallbacks(Module &M) { 9868b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IRBuilder<> IRB(*C); 9879db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany // Create __asan_report* callbacks. 9889db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) { 9899db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; 9909db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany AccessSizeIndex++) { 9919db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany // IsWrite and TypeSize are encoded in the function name. 9929db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany std::string FunctionName = std::string(kAsanReportErrorTemplate) + 9939db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex); 9944f0c69623c10a3a49f6926fd53694ee532e06a85Kostya Serebryany // If we are merging crash callbacks, they have two parameters. 9957846c1c851a53a8280f9d8ed57cd98d82c742551Kostya Serebryany AsanErrorCallback[AccessIsWrite][AccessSizeIndex] = 9967846c1c851a53a8280f9d8ed57cd98d82c742551Kostya Serebryany checkInterfaceFunction(M.getOrInsertFunction( 9977846c1c851a53a8280f9d8ed57cd98d82c742551Kostya Serebryany FunctionName, IRB.getVoidTy(), IntptrTy, NULL)); 9989db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany } 9999db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany } 1000ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany 1001ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction( 1002ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany kAsanHandleNoReturnName, IRB.getVoidTy(), NULL)); 1003f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // We insert an empty inline asm after __asan_report* to avoid callback merge. 1004f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), 1005f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany StringRef(""), StringRef(""), 1006f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany /*hasSideEffects=*/true); 10078b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany} 10088b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 100919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonovvoid AddressSanitizer::emitShadowMapping(Module &M, IRBuilder<> &IRB) const { 101011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov // Tell the values of mapping offset and scale to the run-time. 101111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov GlobalValue *asan_mapping_offset = 101211af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage, 101311af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ConstantInt::get(IntptrTy, Mapping.Offset), 101411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov kAsanMappingOffsetName); 101511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov // Read the global, otherwise it may be optimized away. 101611af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov IRB.CreateLoad(asan_mapping_offset, true); 101711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov 101811af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov GlobalValue *asan_mapping_scale = 101911af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage, 102011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ConstantInt::get(IntptrTy, Mapping.Scale), 102111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov kAsanMappingScaleName); 102211af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov // Read the global, otherwise it may be optimized away. 102311af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov IRB.CreateLoad(asan_mapping_scale, true); 102419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov} 102519cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 10268b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany// virtual 10278b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryanybool AddressSanitizer::doInitialization(Module &M) { 10288b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany // Initialize the private fields. No one has accessed them before. 10298b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany TD = getAnalysisIfAvailable<DataLayout>(); 10308b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 10318b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany if (!TD) 10328b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany return false; 1033b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BL.reset(new BlackList(BlacklistFile)); 10348b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany DynamicallyInitializedGlobals.Init(M); 10358b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 10368b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany C = &(M.getContext()); 10378b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany LongSize = TD->getPointerSizeInBits(); 10388b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IntptrTy = Type::getIntNTy(*C, LongSize); 10398b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 10408b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany AsanCtorFunction = Function::Create( 10418b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany FunctionType::get(Type::getVoidTy(*C), false), 10428b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany GlobalValue::InternalLinkage, kAsanModuleCtorName, &M); 10438b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction); 10448b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany // call __asan_init in the module ctor. 10458b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB)); 10468b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany AsanInitFunction = checkInterfaceFunction( 10478b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL)); 10488b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany AsanInitFunction->setLinkage(Function::ExternalLinkage); 10498b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IRB.CreateCall(AsanInitFunction); 10509db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany 105111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov Mapping = getShadowMapping(M, LongSize, ZeroBaseShadow); 105219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov emitShadowMapping(M, IRB); 1053800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 10547bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority); 1055ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany return true; 1056ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany} 1057ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany 1058a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryanybool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) { 1059a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // For each NSObject descendant having a +load method, this method is invoked 1060a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // by the ObjC runtime before any of the static constructors is called. 1061a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // Therefore we need to instrument such methods with a call to __asan_init 1062a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // at the beginning in order to initialize our runtime before any access to 1063a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // the shadow memory. 1064a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // We cannot just ignore these methods, because they may call other 1065a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // instrumented functions. 1066a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany if (F.getName().find(" load]") != std::string::npos) { 1067a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany IRBuilder<> IRB(F.begin()->begin()); 1068a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany IRB.CreateCall(AsanInitFunction); 1069a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany return true; 1070a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany } 1071a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany return false; 1072a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany} 1073a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany 1074ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanybool AddressSanitizer::runOnFunction(Function &F) { 1075800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (BL->isIn(F)) return false; 1076800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (&F == AsanCtorFunction) return false; 1077324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n"); 10788b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany initializeCallbacks(*F.getParent()); 1079a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany 1080a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // If needed, insert __asan_init before checking for AddressSafety attr. 1081a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany maybeInsertAsanInitAtFunctionEntry(F); 1082a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany 1083831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling if (!F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, 1084831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling Attribute::AddressSafety)) 10856765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling return false; 1086800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1087800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (!ClDebugFunc.empty() && ClDebugFunc != F.getName()) 1088800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return false; 10896765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling 10906765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling // We want to instrument every address only once per basic block (unless there 10916765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling // are calls between uses). 1092800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallSet<Value*, 16> TempsToInstrument; 1093800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallVector<Instruction*, 16> ToInstrument; 109495e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany SmallVector<Instruction*, 8> NoReturnCalls; 1095e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany bool IsWrite; 1096800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1097800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Fill the set of memory operations to instrument. 1098800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (Function::iterator FI = F.begin(), FE = F.end(); 1099800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany FI != FE; ++FI) { 1100800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany TempsToInstrument.clear(); 1101324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany int NumInsnsPerBB = 0; 1102800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 1103800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany BI != BE; ++BI) { 1104bcb55ce3862bbbedac4e09d7099c9e0efc434e4bKostya Serebryany if (LooksLikeCodeInBug11395(BI)) return false; 1105e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) { 1106800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ClOpt && ClOptSameTemp) { 1107800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (!TempsToInstrument.insert(Addr)) 1108800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany continue; // We've seen this temp in the current BB. 1109800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1110800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) { 1111800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // ok, take it. 1112800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else { 111395e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany if (CallInst *CI = dyn_cast<CallInst>(BI)) { 1114800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // A call inside BB. 1115800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany TempsToInstrument.clear(); 1116a17babb021aa9dc3441ecce1ac8a62d2b27edecbKostya Serebryany if (CI->doesNotReturn()) { 111795e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany NoReturnCalls.push_back(CI); 111895e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany } 1119800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1120800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany continue; 1121800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1122800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ToInstrument.push_back(BI); 1123324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany NumInsnsPerBB++; 1124324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) 1125324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany break; 1126800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1127800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1128800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1129800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Instrument. 1130800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany int NumInstrumented = 0; 1131800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = ToInstrument.size(); i != n; i++) { 1132800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *Inst = ToInstrument[i]; 1133800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ClDebugMin < 0 || ClDebugMax < 0 || 1134800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) { 1135e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (isInterestingMemoryAccess(Inst, &IsWrite)) 1136ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMop(Inst); 1137800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany else 1138ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMemIntrinsic(cast<MemIntrinsic>(Inst)); 1139800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1140800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NumInstrumented++; 1141800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1142800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 114359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov FunctionStackPoisoner FSP(F, *this); 114459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool ChangedStack = FSP.runOnFunction(); 114595e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany 114695e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany // We must unpoison the stack before every NoReturn call (throw, _exit, etc). 114795e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37 114895e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) { 114995e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany Instruction *CI = NoReturnCalls[i]; 115095e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany IRBuilder<> IRB(CI); 1151ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany IRB.CreateCall(AsanHandleNoReturnFunc); 115295e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany } 1153324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "ASAN done instrumenting:\n" << F << "\n"); 115495e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany 115595e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty(); 1156800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1157800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1158800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) { 1159800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ShadowRedzoneSize == 1) return PoisonByte; 1160800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte; 1161800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ShadowRedzoneSize == 4) 1162800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return (PoisonByte << 24) + (PoisonByte << 16) + 1163800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany (PoisonByte << 8) + (PoisonByte); 1164858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4"); 1165800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1166800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1167800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic void PoisonShadowPartialRightRedzone(uint8_t *Shadow, 1168800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany size_t Size, 1169b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany size_t RZSize, 1170800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany size_t ShadowGranularity, 1171800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint8_t Magic) { 1172b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany for (size_t i = 0; i < RZSize; 1173800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany i+= ShadowGranularity, Shadow++) { 1174800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (i + ShadowGranularity <= Size) { 1175800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany *Shadow = 0; // fully addressable 1176800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else if (i >= Size) { 1177800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany *Shadow = Magic; // unaddressable 1178800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else { 1179800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany *Shadow = Size - i; // first Size-i bytes are addressable 1180800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1181800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1182800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1183800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 118459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// Workaround for bug 11395: we don't want to instrument stack in functions 118559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// with large assembly blobs (32-bit only), otherwise reg alloc may crash. 118659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// FIXME: remove once the bug 11395 is fixed. 118759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovbool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) { 118859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (LongSize != 32) return false; 118959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov CallInst *CI = dyn_cast<CallInst>(I); 119059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (!CI || !CI->isInlineAsm()) return false; 119159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (CI->getNumArgOperands() <= 5) return false; 119259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // We have inline assembly with quite a few arguments. 119359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return true; 119459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov} 119559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 119659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::initializeCallbacks(Module &M) { 119759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IRBuilder<> IRB(*C); 119859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanStackMallocFunc = checkInterfaceFunction(M.getOrInsertFunction( 119959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL)); 120059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanStackFreeFunc = checkInterfaceFunction(M.getOrInsertFunction( 120159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanStackFreeName, IRB.getVoidTy(), 120259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IntptrTy, IntptrTy, IntptrTy, NULL)); 120359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction( 120459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 120559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction( 120659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 120759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov} 120859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 120959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::poisonRedZones( 121059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB, Value *ShadowBase, 121159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool DoPoison) { 121219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t ShadowRZSize = RedzoneSize() >> Mapping.Scale; 1213800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(ShadowRZSize >= 1 && ShadowRZSize <= 4); 1214800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8); 1215800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *RZPtrTy = PointerType::get(RZTy, 0); 1216800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1217800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PoisonLeft = ConstantInt::get(RZTy, 1218800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize)); 1219800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PoisonMid = ConstantInt::get(RZTy, 1220800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize)); 1221800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PoisonRight = ConstantInt::get(RZTy, 1222800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize)); 1223800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1224800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // poison the first red zone. 1225800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy)); 1226800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1227800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // poison all other red zones. 1228b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany uint64_t Pos = RedzoneSize(); 1229800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = AllocaVec.size(); i < n; i++) { 1230800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany AllocaInst *AI = AllocaVec[i]; 1231800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t SizeInBytes = getAllocaSizeInBytes(AI); 1232800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t AlignedSize = getAlignedAllocaSize(AI); 1233b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany assert(AlignedSize - SizeInBytes < RedzoneSize()); 1234800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Ptr = NULL; 1235800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1236800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Pos += AlignedSize; 1237800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1238800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(ShadowBase->getType() == IntptrTy); 1239800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (SizeInBytes < AlignedSize) { 1240800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Poison the partial redzone at right 1241800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Ptr = IRB.CreateAdd( 1242800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ShadowBase, ConstantInt::get(IntptrTy, 124319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov (Pos >> Mapping.Scale) - ShadowRZSize)); 1244b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany size_t AddressableBytes = RedzoneSize() - (AlignedSize - SizeInBytes); 1245800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint32_t Poison = 0; 1246800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (DoPoison) { 1247800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes, 1248b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany RedzoneSize(), 124919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 1ULL << Mapping.Scale, 1250800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany kAsanStackPartialRedzoneMagic); 1251800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1252800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PartialPoison = ConstantInt::get(RZTy, Poison); 1253800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy)); 1254800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1255800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1256800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Poison the full redzone at right. 1257800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Ptr = IRB.CreateAdd(ShadowBase, 125819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ConstantInt::get(IntptrTy, Pos >> Mapping.Scale)); 12591c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool LastAlloca = (i == AllocaVec.size() - 1); 12601c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Value *Poison = LastAlloca ? PoisonRight : PoisonMid; 1261800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy)); 1262800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1263b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Pos += RedzoneSize(); 1264800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1265800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1266800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 126759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::poisonStack() { 126859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t LocalStackSize = TotalStackSize + 126959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov (AllocaVec.size() + 1) * RedzoneSize(); 1270800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 127159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool DoStackMalloc = ASan.CheckUseAfterReturn 1272800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany && LocalStackSize <= kMaxStackMallocSize; 1273800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 12741c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov assert(AllocaVec.size() > 0); 1275800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *InsBefore = AllocaVec[0]; 1276800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(InsBefore); 1277800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1278800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1279800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize); 1280800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany AllocaInst *MyAlloca = 1281800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore); 128259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (ClRealignStack && StackAlignment < RedzoneSize()) 128359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov StackAlignment = RedzoneSize(); 128459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov MyAlloca->setAlignment(StackAlignment); 1285800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(MyAlloca->isStaticAlloca()); 1286800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy); 1287800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *LocalStackBase = OrigStackBase; 1288800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1289800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (DoStackMalloc) { 1290800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc, 1291800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase); 1292800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1293800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1294800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // This string will be parsed by the run-time (DescribeStackAddress). 1295800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallString<2048> StackDescriptionStorage; 1296800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany raw_svector_ostream StackDescription(StackDescriptionStorage); 1297800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StackDescription << F.getName() << " " << AllocaVec.size() << " "; 1298800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 12991c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Insert poison calls for lifetime intrinsics for alloca. 13001c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool HavePoisonedAllocas = false; 13011c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov for (size_t i = 0, n = AllocaPoisonCallVec.size(); i < n; i++) { 13021c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov const AllocaPoisonCall &APC = AllocaPoisonCallVec[i]; 13031c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov IntrinsicInst *II = APC.InsBefore; 13041c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *AI = findAllocaForValue(II->getArgOperand(1)); 13051c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov assert(AI); 13061c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov IRBuilder<> IRB(II); 13071c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov poisonAlloca(AI, APC.Size, IRB, APC.DoPoison); 13081c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov HavePoisonedAllocas |= APC.DoPoison; 13091c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov } 13101c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 1311b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany uint64_t Pos = RedzoneSize(); 1312800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Replace Alloca instructions with base+offset. 1313800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = AllocaVec.size(); i < n; i++) { 1314800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany AllocaInst *AI = AllocaVec[i]; 1315800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t SizeInBytes = getAllocaSizeInBytes(AI); 1316800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StringRef Name = AI->getName(); 1317800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StackDescription << Pos << " " << SizeInBytes << " " 1318800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany << Name.size() << " " << Name << " "; 1319800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t AlignedSize = getAlignedAllocaSize(AI); 1320b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany assert((AlignedSize % RedzoneSize()) == 0); 1321f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov Value *NewAllocaPtr = IRB.CreateIntToPtr( 1322800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)), 1323f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov AI->getType()); 13241afbb517965e29b07cb42e2335d5eadd87de6535Alexey Samsonov replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB); 1325f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov AI->replaceAllUsesWith(NewAllocaPtr); 1326b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Pos += AlignedSize + RedzoneSize(); 1327800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1328800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(Pos == LocalStackSize); 1329800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1330800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Write the Magic value and the frame description constant to the redzone. 1331800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy); 1332800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic), 1333800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany BasePlus0); 1334800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *BasePlus1 = IRB.CreateAdd(LocalStackBase, 133559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov ConstantInt::get(IntptrTy, 133659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov ASan.LongSize/8)); 1337800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy); 13389ce84c1c95c0153a2f33e188ce0db00770425f9eAlexey Samsonov GlobalVariable *StackDescriptionGlobal = 1339a5f54f14435d881b10d8eb65e19fa42af95757e9Kostya Serebryany createPrivateGlobalForString(*F.getParent(), StackDescription.str()); 134059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, 134159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IntptrTy); 1342800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(Description, BasePlus1); 1343800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1344800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Poison the stack redzones at the entry. 134559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB); 134659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov poisonRedZones(AllocaVec, IRB, ShadowBase, true); 1347800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1348800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Unpoison the stack before all ret instructions. 1349800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = RetVec.size(); i < n; i++) { 1350800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *Ret = RetVec[i]; 1351800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRBRet(Ret); 1352800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Mark the current frame as retired. 1353800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic), 1354800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany BasePlus0); 1355800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Unpoison the stack. 135659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov poisonRedZones(AllocaVec, IRBRet, ShadowBase, false); 1357800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (DoStackMalloc) { 1358f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // In use-after-return mode, mark the whole stack frame unaddressable. 1359800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase, 1360800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, LocalStackSize), 1361800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany OrigStackBase); 1362f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov } else if (HavePoisonedAllocas) { 1363f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // If we poisoned some allocas in llvm.lifetime analysis, 1364f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // unpoison whole stack frame now. 1365f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov assert(LocalStackBase == OrigStackBase); 1366f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false); 1367800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1368800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1369800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1370bd0052a0f26f04b8fcf59e8f645e5e33751e1f6eKostya Serebryany // We are done. Remove the old unused alloca instructions. 1371bd0052a0f26f04b8fcf59e8f645e5e33751e1f6eKostya Serebryany for (size_t i = 0, n = AllocaVec.size(); i < n; i++) 1372bd0052a0f26f04b8fcf59e8f645e5e33751e1f6eKostya Serebryany AllocaVec[i]->eraseFromParent(); 1373800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1374f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov 137559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size, 137659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IRBuilder<> IRB, bool DoPoison) { 1377f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // For now just insert the call to ASan runtime. 1378f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy); 1379f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov Value *SizeArg = ConstantInt::get(IntptrTy, Size); 1380f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc 1381f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov : AsanUnpoisonStackMemoryFunc, 1382f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov AddrArg, SizeArg); 1383f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov} 138459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 138559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// Handling llvm.lifetime intrinsics for a given %alloca: 138659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca. 138759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// (2) if %size is constant, poison memory for llvm.lifetime.end (to detect 138859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// invalid accesses) and unpoison it for llvm.lifetime.start (the memory 138959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// could be poisoned by previous llvm.lifetime.end instruction, as the 139059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// variable may go in and out of scope several times, e.g. in loops). 139159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// (3) if we poisoned at least one %alloca in a function, 139259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// unpoison the whole stack frame at function exit. 139359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 13941c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey SamsonovAllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) { 13951c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) 13961c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // We're intested only in allocas we can handle. 13971c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return isInterestingAlloca(*AI) ? AI : 0; 13981c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // See if we've already calculated (or started to calculate) alloca for a 13991c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // given value. 14001c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValueMapTy::iterator I = AllocaForValue.find(V); 14011c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (I != AllocaForValue.end()) 14021c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return I->second; 14031c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Store 0 while we're calculating alloca for value V to avoid 14041c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // infinite recursion if the value references itself. 14051c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValue[V] = 0; 14061c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *Res = 0; 14071c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (CastInst *CI = dyn_cast<CastInst>(V)) 14081c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Res = findAllocaForValue(CI->getOperand(0)); 14091c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov else if (PHINode *PN = dyn_cast<PHINode>(V)) { 14101c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 14111c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Value *IncValue = PN->getIncomingValue(i); 14121c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Allow self-referencing phi-nodes. 14131c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (IncValue == PN) continue; 14141c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *IncValueAI = findAllocaForValue(IncValue); 14151c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // AI for incoming values should exist and should all be equal. 14161c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (IncValueAI == 0 || (Res != 0 && IncValueAI != Res)) 14171c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return 0; 14181c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Res = IncValueAI; 141959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 142059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 14211c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (Res != 0) 14221c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValue[V] = Res; 142359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return Res; 142459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov} 1425