AddressSanitizer.cpp revision 3016056769639878b4f152838f0cf16d2e482339
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" 381479c9bb392325688b72e5829bbb7939c4a079a4Kostya Serebryany#include "llvm/Support/CallSite.h" 39800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/CommandLine.h" 40800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/DataTypes.h" 41800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/Debug.h" 42800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/raw_ostream.h" 43800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Support/system_error.h" 44800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Target/TargetMachine.h" 45800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Transforms/Utils/BasicBlockUtils.h" 4690230c84668269fbd53d163e398cd16486d5d414Chandler Carruth#include "llvm/Transforms/Utils/BlackList.h" 471afbb517965e29b07cb42e2335d5eadd87de6535Alexey Samsonov#include "llvm/Transforms/Utils/Local.h" 48800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include "llvm/Transforms/Utils/ModuleUtils.h" 49800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany#include <algorithm> 50d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include <string> 51800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 52800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanyusing namespace llvm; 53800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 54800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowScale = 3; 55800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowOffset32 = 1ULL << 29; 56800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uint64_t kDefaultShadowOffset64 = 1ULL << 44; 57117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryanystatic const uint64_t kDefaultShort64bitShadowOffset = 0x7FFF8000; // < 2G. 5848a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryanystatic const uint64_t kPPC64_ShadowOffset64 = 1ULL << 41; 59800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 60800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const size_t kMaxStackMallocSize = 1 << 16; // 64K 61800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; 62800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; 63800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 64800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanModuleCtorName = "asan.module_ctor"; 657bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const char *kAsanModuleDtorName = "asan.module_dtor"; 667bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const int kAsanCtorAndCtorPriority = 1; 67800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanReportErrorTemplate = "__asan_report_"; 686ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryanystatic const char *kAsanReportLoadN = "__asan_report_load_n"; 696ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryanystatic const char *kAsanReportStoreN = "__asan_report_store_n"; 70800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanRegisterGlobalsName = "__asan_register_globals"; 717bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryanystatic const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals"; 729b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryanystatic const char *kAsanPoisonGlobalsName = "__asan_before_dynamic_init"; 739b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryanystatic const char *kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init"; 743016056769639878b4f152838f0cf16d2e482339Kostya Serebryanystatic const char *kAsanInitName = "__asan_init_v3"; 7595e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryanystatic const char *kAsanHandleNoReturnName = "__asan_handle_no_return"; 76800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanMappingOffsetName = "__asan_mapping_offset"; 77800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanMappingScaleName = "__asan_mapping_scale"; 78800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanStackMallocName = "__asan_stack_malloc"; 79800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const char *kAsanStackFreeName = "__asan_stack_free"; 8051c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryanystatic const char *kAsanGenPrefix = "__asan_gen_"; 81f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonovstatic const char *kAsanPoisonStackMemoryName = "__asan_poison_stack_memory"; 82f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonovstatic const char *kAsanUnpoisonStackMemoryName = 83f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov "__asan_unpoison_stack_memory"; 84800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 85800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackLeftRedzoneMagic = 0xf1; 86800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackMidRedzoneMagic = 0xf2; 87800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackRightRedzoneMagic = 0xf3; 88800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic const int kAsanStackPartialRedzoneMagic = 0xf4; 89800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 90c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany// Accesses sizes are powers of two: 1, 2, 4, 8, 16. 91c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryanystatic const size_t kNumberOfAccessSizes = 5; 92c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany 93800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Command-line flags. 94800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 95800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no-]asan-reads. 96800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClInstrumentReads("asan-instrument-reads", 97800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("instrument read instructions"), cl::Hidden, cl::init(true)); 98800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClInstrumentWrites("asan-instrument-writes", 99800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("instrument write instructions"), cl::Hidden, cl::init(true)); 100e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryanystatic cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics", 101e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany cl::desc("instrument atomic instructions (rmw, cmpxchg)"), 102e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany cl::Hidden, cl::init(true)); 1036e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryanystatic cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path", 1046e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany cl::desc("use instrumentation with slow path for all accesses"), 1056e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany cl::Hidden, cl::init(false)); 106c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany// This flag limits the number of instructions to be instrumented 107324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany// in any given BB. Normally, this should be set to unlimited (INT_MAX), 108324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary 109324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany// set it to 10000. 110324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryanystatic cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb", 111324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany cl::init(10000), 112324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany cl::desc("maximal number of instructions to instrument in any given BB"), 113324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany cl::Hidden); 114800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-stack. 115800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClStack("asan-stack", 116800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Handle stack memory"), cl::Hidden, cl::init(true)); 117800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-use-after-return. 118800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClUseAfterReturn("asan-use-after-return", 119800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Check return-after-free"), cl::Hidden, cl::init(false)); 120800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This flag may need to be replaced with -f[no]asan-globals. 121800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClGlobals("asan-globals", 122800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Handle global objects"), cl::Hidden, cl::init(true)); 1239b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryanystatic cl::opt<bool> ClInitializers("asan-initialization-order", 1249b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false)); 125800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClMemIntrin("asan-memintrin", 126800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true)); 1276c55412ea4b39103a3a5764d49ddfdf50e066d56Kostya Serebryanystatic cl::opt<bool> ClRealignStack("asan-realign-stack", 1286c55412ea4b39103a3a5764d49ddfdf50e066d56Kostya Serebryany cl::desc("Realign stack to 32"), cl::Hidden, cl::init(true)); 129b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonovstatic cl::opt<std::string> ClBlacklistFile("asan-blacklist", 130b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov cl::desc("File containing the list of objects to ignore " 131800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany "during instrumentation"), cl::Hidden); 132800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 133800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// These flags allow to change the shadow mapping. 134800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// The shadow mapping looks like 135800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Shadow = (Mem >> scale) + (1 << offset_log) 136800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClMappingScale("asan-mapping-scale", 137800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0)); 138800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log", 139800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1)); 140117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryanystatic cl::opt<bool> ClShort64BitOffset("asan-short-64bit-mapping-offset", 141117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryany cl::desc("Use short immediate constant as the mapping offset for 64bit"), 1420bc55d517e8e64f0f441736fba2447781c405ef4Kostya Serebryany cl::Hidden, cl::init(true)); 143800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 144800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Optimization flags. Not user visible, used mostly for testing 145800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// and benchmarking the tool. 146800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOpt("asan-opt", 147800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true)); 148800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOptSameTemp("asan-opt-same-temp", 149800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Instrument the same temp just once"), cl::Hidden, 150800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::init(true)); 151800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<bool> ClOptGlobals("asan-opt-globals", 152800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true)); 153800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 154ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonovstatic cl::opt<bool> ClCheckLifetime("asan-check-lifetime", 155ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov cl::desc("Use llvm.lifetime intrinsics to insert extra checks"), 156ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov cl::Hidden, cl::init(false)); 157ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov 158800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Debug flags. 159800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden, 160800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::init(0)); 161800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"), 162800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::init(0)); 163800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<std::string> ClDebugFunc("asan-debug-func", 164800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::desc("Debug func")); 165800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"), 166800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::init(-1)); 167800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"), 168800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany cl::Hidden, cl::init(-1)); 169800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 170800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanynamespace { 171ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany/// A set of dynamically initialized globals extracted from metadata. 172ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryanyclass SetOfDynamicallyInitializedGlobals { 173ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany public: 174ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany void Init(Module& M) { 175ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany // Clang generates metadata identifying all dynamically initialized globals. 176ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany NamedMDNode *DynamicGlobals = 177ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany M.getNamedMetadata("llvm.asan.dynamically_initialized_globals"); 178ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany if (!DynamicGlobals) 179ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany return; 180ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) { 181ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany MDNode *MDN = DynamicGlobals->getOperand(i); 182ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany assert(MDN->getNumOperands() == 1); 183ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany Value *VG = MDN->getOperand(0); 184ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany // The optimizer may optimize away a global entirely, in which case we 185ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany // cannot instrument access to it. 186ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany if (!VG) 187ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany continue; 188ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany DynInitGlobals.insert(cast<GlobalVariable>(VG)); 189ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany } 190ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany } 191ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; } 192ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany private: 193ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany SmallSet<GlobalValue*, 32> DynInitGlobals; 194ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany}; 195ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany 19619cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov/// This struct defines the shadow mapping using the rule: 19748a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany/// shadow = (mem >> Scale) ADD-or-OR Offset. 19819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonovstruct ShadowMapping { 19919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov int Scale; 20019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov uint64_t Offset; 20148a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany bool OrShadowOffset; 20219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov}; 20319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 20411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonovstatic ShadowMapping getShadowMapping(const Module &M, int LongSize, 20511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow) { 20611af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov llvm::Triple TargetTriple(M.getTargetTriple()); 20711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool IsAndroid = TargetTriple.getEnvironment() == llvm::Triple::Android; 208c8a196ae8fad3cba7a777e2e7916fd36ebf70fe6Alexander Potapenko bool IsMacOSX = TargetTriple.getOS() == llvm::Triple::MacOSX; 20948a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany bool IsPPC64 = TargetTriple.getArch() == llvm::Triple::ppc64; 2100bc55d517e8e64f0f441736fba2447781c405ef4Kostya Serebryany bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64; 21119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 21219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 21319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 21448a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany // OR-ing shadow offset if more efficient (at least on x86), 21548a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany // but on ppc64 we have to use add since the shadow offset is not neccesary 21648a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany // 1/8-th of the address space. 217117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryany Mapping.OrShadowOffset = !IsPPC64 && !ClShort64BitOffset; 21848a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany 21911af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov Mapping.Offset = (IsAndroid || ZeroBaseShadow) ? 0 : 22048a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany (LongSize == 32 ? kDefaultShadowOffset32 : 22148a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany IsPPC64 ? kPPC64_ShadowOffset64 : kDefaultShadowOffset64); 222c8a196ae8fad3cba7a777e2e7916fd36ebf70fe6Alexander Potapenko if (!ZeroBaseShadow && ClShort64BitOffset && IsX86_64 && !IsMacOSX) { 2230bc55d517e8e64f0f441736fba2447781c405ef4Kostya Serebryany assert(LongSize == 64); 224117de489a0d5f4ff280fb173fe45bd5ce8514d93Kostya Serebryany Mapping.Offset = kDefaultShort64bitShadowOffset; 22539f02940ba085af79011f7e9095bf9902cd0fa6fKostya Serebryany } 22639f02940ba085af79011f7e9095bf9902cd0fa6fKostya Serebryany if (!ZeroBaseShadow && ClMappingOffsetLog >= 0) { 22719cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov // Zero offset log is the special case. 22819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping.Offset = (ClMappingOffsetLog == 0) ? 0 : 1ULL << ClMappingOffsetLog; 22919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 23019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 23119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping.Scale = kDefaultShadowScale; 23219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov if (ClMappingScale) { 23319cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping.Scale = ClMappingScale; 23419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 23519cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 23619cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return Mapping; 237b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany} 238b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 23919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonovstatic size_t RedzoneSizeForScale(int MappingScale) { 240b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany // Redzone used for stack and globals is at least 32 bytes. 241b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively. 24219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return std::max(32U, 1U << MappingScale); 243b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany} 244ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany 245800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany/// AddressSanitizer: instrument the code in module to find memory bugs. 246ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanystruct AddressSanitizer : public FunctionPass { 247b4ba5e68e1ac00bfb93572a1f271673deefd7ea9Alexey Samsonov AddressSanitizer(bool CheckInitOrder = true, 248ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckUseAfterReturn = false, 249b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov bool CheckLifetime = false, 25011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov StringRef BlacklistFile = StringRef(), 25111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow = false) 252ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov : FunctionPass(ID), 253ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov CheckInitOrder(CheckInitOrder || ClInitializers), 254ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn), 255b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov CheckLifetime(CheckLifetime || ClCheckLifetime), 256b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile 25711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov : BlacklistFile), 25811af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ZeroBaseShadow(ZeroBaseShadow) {} 2591416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany virtual const char *getPassName() const { 2601416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany return "AddressSanitizerFunctionPass"; 2611416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany } 262ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany void instrumentMop(Instruction *I); 2636ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany void instrumentAddress(Instruction *OrigIns, Instruction *InsertBefore, 2646ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *Addr, uint32_t TypeSize, bool IsWrite, 2656ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *SizeArgument); 266c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, 267c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *ShadowValue, uint32_t TypeSize); 268ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr, 2696ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany bool IsWrite, size_t AccessSizeIndex, 2706ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *SizeArgument); 271ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany bool instrumentMemIntrinsic(MemIntrinsic *MI); 272ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr, 273c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *Size, 274800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *InsertBefore, bool IsWrite); 275800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *memToShadow(Value *Shadow, IRBuilder<> &IRB); 276ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany bool runOnFunction(Function &F); 2779b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany void createInitializerPoisonCalls(Module &M, 2789b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Value *FirstAddr, Value *LastAddr); 279a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany bool maybeInsertAsanInitAtFunctionEntry(Function &F); 28019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov void emitShadowMapping(Module &M, IRBuilder<> &IRB) const; 281ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany virtual bool doInitialization(Module &M); 282800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany static char ID; // Pass identification, replacement for typeid 283800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 284800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany private: 2858b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany void initializeCallbacks(Module &M); 286800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 2879b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany bool ShouldInstrumentGlobal(GlobalVariable *G); 2885a3a9c937198084498a196dae856ac5a5a005bccKostya Serebryany bool LooksLikeCodeInBug11395(Instruction *I); 2899b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany void FindDynamicInitializers(Module &M); 290800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 291ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckInitOrder; 292ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckUseAfterReturn; 293ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckLifetime; 29411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov SmallString<64> BlacklistFile; 29511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow; 29611af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov 297800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany LLVMContext *C; 2983574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow DataLayout *TD; 299800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany int LongSize; 300800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *IntptrTy; 30119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 302800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Function *AsanCtorFunction; 303800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Function *AsanInitFunction; 304ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany Function *AsanHandleNoReturnFunc; 305b5b86d263a651566cb25c0f406f75ceffb771029Kostya Serebryany OwningPtr<BlackList> BL; 3069db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany // This array is indexed by AccessIsWrite and log2(AccessSize). 3079db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany Function *AsanErrorCallback[2][kNumberOfAccessSizes]; 3086ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany // This array is indexed by AccessIsWrite. 3096ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Function *AsanErrorCallbackSized[2]; 310f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany InlineAsm *EmptyAsm; 311ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals; 31259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 31359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov friend struct FunctionStackPoisoner; 314800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany}; 315c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany 3161416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanyclass AddressSanitizerModule : public ModulePass { 317b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany public: 318b4ba5e68e1ac00bfb93572a1f271673deefd7ea9Alexey Samsonov AddressSanitizerModule(bool CheckInitOrder = true, 31911af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov StringRef BlacklistFile = StringRef(), 32011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow = false) 321ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov : ModulePass(ID), 322b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov CheckInitOrder(CheckInitOrder || ClInitializers), 323b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile 32411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov : BlacklistFile), 32511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ZeroBaseShadow(ZeroBaseShadow) {} 3261416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany bool runOnModule(Module &M); 3271416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany static char ID; // Pass identification, replacement for typeid 3281416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany virtual const char *getPassName() const { 3291416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany return "AddressSanitizerModule"; 3301416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany } 331f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov 332b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany private: 3334684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov void initializeCallbacks(Module &M); 3344684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov 335b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany bool ShouldInstrumentGlobal(GlobalVariable *G); 336b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany void createInitializerPoisonCalls(Module &M, Value *FirstAddr, 337b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Value *LastAddr); 33819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t RedzoneSize() const { 33919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return RedzoneSizeForScale(Mapping.Scale); 34019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 341b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 342ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov bool CheckInitOrder; 343b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov SmallString<64> BlacklistFile; 34411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool ZeroBaseShadow; 34511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov 346b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany OwningPtr<BlackList> BL; 347b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals; 348b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Type *IntptrTy; 349b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany LLVMContext *C; 3501416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany DataLayout *TD; 35119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 3524684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanPoisonGlobals; 3534684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanUnpoisonGlobals; 3544684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanRegisterGlobals; 3554684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov Function *AsanUnregisterGlobals; 356b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany}; 357b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 35859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// Stack poisoning does not play well with exception handling. 35959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// When an exception is thrown, we essentially bypass the code 36059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// that unpoisones the stack. This is why the run-time library has 36159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire 36259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// stack in the interceptor. This however does not work inside the 36359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// actual function which catches the exception. Most likely because the 36459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// compiler hoists the load of the shadow value somewhere too high. 36559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// This causes asan to report a non-existing bug on 453.povray. 36659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// It sounds like an LLVM bug. 36759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovstruct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> { 36859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Function &F; 36959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AddressSanitizer &ASan; 37059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov DIBuilder DIB; 37159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov LLVMContext *C; 37259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Type *IntptrTy; 37359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Type *IntptrPtrTy; 37419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ShadowMapping Mapping; 37559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 37659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov SmallVector<AllocaInst*, 16> AllocaVec; 37759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov SmallVector<Instruction*, 8> RetVec; 37859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t TotalStackSize; 37959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov unsigned StackAlignment; 38059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 38159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Function *AsanStackMallocFunc, *AsanStackFreeFunc; 38259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc; 38359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 3841c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Stores a place and arguments of poisoning/unpoisoning call for alloca. 3851c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov struct AllocaPoisonCall { 3861c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov IntrinsicInst *InsBefore; 3871c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov uint64_t Size; 3881c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool DoPoison; 3891c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov }; 3901c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov SmallVector<AllocaPoisonCall, 8> AllocaPoisonCallVec; 3911c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 3921c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Maps Value to an AllocaInst from which the Value is originated. 3931c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov typedef DenseMap<Value*, AllocaInst*> AllocaForValueMapTy; 3941c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValueMapTy AllocaForValue; 3951c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 39659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov FunctionStackPoisoner(Function &F, AddressSanitizer &ASan) 39759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov : F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C), 39859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)), 39919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Mapping(ASan.Mapping), 40019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov TotalStackSize(0), StackAlignment(1 << Mapping.Scale) {} 40159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 40259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool runOnFunction() { 40359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (!ClStack) return false; 40459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // Collect alloca, ret, lifetime instructions etc. 40559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), 40659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) { 40759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov BasicBlock *BB = *DI; 40859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov visit(*BB); 40959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 41059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (AllocaVec.empty()) return false; 41159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 41259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov initializeCallbacks(*F.getParent()); 41359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 41459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov poisonStack(); 41559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 41659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (ClDebugStack) { 41759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov DEBUG(dbgs() << F); 41859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 41959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return true; 42059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 42159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 42259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // Finds all static Alloca instructions and puts 42359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // poisoned red zones around all of them. 42459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // Then unpoison everything back before the function returns. 42559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void poisonStack(); 42659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 42759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // ----------------------- Visitors. 42859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov /// \brief Collect all Ret instructions. 42959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void visitReturnInst(ReturnInst &RI) { 43059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov RetVec.push_back(&RI); 43159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 43259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 43359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov /// \brief Collect Alloca instructions we want (and can) handle. 43459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void visitAllocaInst(AllocaInst &AI) { 4351c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (!isInterestingAlloca(AI)) return; 43659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 43759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov StackAlignment = std::max(StackAlignment, AI.getAlignment()); 43859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AllocaVec.push_back(&AI); 43959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t AlignedSize = getAlignedAllocaSize(&AI); 44059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov TotalStackSize += AlignedSize; 44159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 44259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 4431c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov /// \brief Collect lifetime intrinsic calls to check for use-after-scope 4441c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov /// errors. 4451c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov void visitIntrinsicInst(IntrinsicInst &II) { 4461c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (!ASan.CheckLifetime) return; 4471c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Intrinsic::ID ID = II.getIntrinsicID(); 4481c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (ID != Intrinsic::lifetime_start && 4491c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov ID != Intrinsic::lifetime_end) 4501c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return; 4511c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Found lifetime intrinsic, add ASan instrumentation if necessary. 4521c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov ConstantInt *Size = dyn_cast<ConstantInt>(II.getArgOperand(0)); 4531c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // If size argument is undefined, don't do anything. 4541c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (Size->isMinusOne()) return; 4551c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Check that size doesn't saturate uint64_t and can 4561c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // be stored in IntptrTy. 4571c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov const uint64_t SizeValue = Size->getValue().getLimitedValue(); 4581c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (SizeValue == ~0ULL || 4591c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov !ConstantInt::isValueValidForType(IntptrTy, SizeValue)) 4601c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return; 4611c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Find alloca instruction that corresponds to llvm.lifetime argument. 4621c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *AI = findAllocaForValue(II.getArgOperand(1)); 4631c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (!AI) return; 4641c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool DoPoison = (ID == Intrinsic::lifetime_end); 4651c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaPoisonCall APC = {&II, SizeValue, DoPoison}; 4661c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaPoisonCallVec.push_back(APC); 4671c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov } 4681c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 46959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // ---------------------- Helpers. 47059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void initializeCallbacks(Module &M); 47159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 4721c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Check if we want (and can) handle this alloca. 4731c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool isInterestingAlloca(AllocaInst &AI) { 4741c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return (!AI.isArrayAllocation() && 4751c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AI.isStaticAlloca() && 4761c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AI.getAllocatedType()->isSized()); 4771c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov } 4781c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 47919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t RedzoneSize() const { 48019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov return RedzoneSizeForScale(Mapping.Scale); 48119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov } 48259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t getAllocaSizeInBytes(AllocaInst *AI) { 48359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Type *Ty = AI->getAllocatedType(); 48459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t SizeInBytes = ASan.TD->getTypeAllocSize(Ty); 48559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return SizeInBytes; 48659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 48759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t getAlignedSize(uint64_t SizeInBytes) { 48859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov size_t RZ = RedzoneSize(); 48959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return ((SizeInBytes + RZ - 1) / RZ) * RZ; 49059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 49159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t getAlignedAllocaSize(AllocaInst *AI) { 49259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t SizeInBytes = getAllocaSizeInBytes(AI); 49359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return getAlignedSize(SizeInBytes); 49459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 4951c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov /// Finds alloca where the value comes from. 4961c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *findAllocaForValue(Value *V); 49759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void poisonRedZones(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB, 49859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Value *ShadowBase, bool DoPoison); 49959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> IRB, bool DoPoison); 50059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov}; 50159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 502800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} // namespace 503800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 504800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanychar AddressSanitizer::ID = 0; 505800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyINITIALIZE_PASS(AddressSanitizer, "asan", 506800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany "AddressSanitizer: detects use-after-free and out-of-bounds bugs.", 507800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany false, false) 508ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey SamsonovFunctionPass *llvm::createAddressSanitizerFunctionPass( 509b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime, 51011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov StringRef BlacklistFile, bool ZeroBaseShadow) { 511ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn, 51211af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov CheckLifetime, BlacklistFile, ZeroBaseShadow); 513800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 514800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 5151416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanychar AddressSanitizerModule::ID = 0; 5161416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya SerebryanyINITIALIZE_PASS(AddressSanitizerModule, "asan-module", 5171416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany "AddressSanitizer: detects use-after-free and out-of-bounds bugs." 5181416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany "ModulePass", false, false) 519b0dcf61252e58715a3bea79f4c112572df361c30Alexey SamsonovModulePass *llvm::createAddressSanitizerModulePass( 52011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov bool CheckInitOrder, StringRef BlacklistFile, bool ZeroBaseShadow) { 52111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov return new AddressSanitizerModule(CheckInitOrder, BlacklistFile, 52211af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ZeroBaseShadow); 52325878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko} 52425878042030e85c244b41bfcdfad27c32360e2ecAlexander Potapenko 5252735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryanystatic size_t TypeSizeToSizeIndex(uint32_t TypeSize) { 5262735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany size_t Res = CountTrailingZeros_32(TypeSize / 8); 5272735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany assert(Res < kNumberOfAccessSizes); 5282735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany return Res; 5292735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany} 5302735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany 531800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Create a constant for Str so that we can pass it to the run-time lib. 532800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) { 53318c7f80b3e83ab584bd8572695a3cde8bafd9d3cChris Lattner Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); 5345111627ac1b0ae8a5a9d4dc1be8b22939ba850d0Kostya Serebryany GlobalVariable *GV = new GlobalVariable(M, StrConst->getType(), true, 53551c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany GlobalValue::PrivateLinkage, StrConst, 53651c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany kAsanGenPrefix); 5375111627ac1b0ae8a5a9d4dc1be8b22939ba850d0Kostya Serebryany GV->setUnnamedAddr(true); // Ok to merge these. 5385111627ac1b0ae8a5a9d4dc1be8b22939ba850d0Kostya Serebryany GV->setAlignment(1); // Strings may not be merged w/o setting align 1. 5395111627ac1b0ae8a5a9d4dc1be8b22939ba850d0Kostya Serebryany return GV; 54051c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany} 54151c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany 54251c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryanystatic bool GlobalWasGeneratedByAsan(GlobalVariable *G) { 54351c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany return G->getName().find(kAsanGenPrefix) == 0; 544800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 545800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 546800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyValue *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) { 547800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Shadow >> scale 54819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov Shadow = IRB.CreateLShr(Shadow, Mapping.Scale); 54919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov if (Mapping.Offset == 0) 550800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return Shadow; 551800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // (Shadow >> scale) | offset 55248a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany if (Mapping.OrShadowOffset) 55348a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); 55448a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany else 55548a615fee78c3c262c60147a65dc6fff5fd6bb3bKostya Serebryany return IRB.CreateAdd(Shadow, ConstantInt::get(IntptrTy, Mapping.Offset)); 556800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 557800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 558c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryanyvoid AddressSanitizer::instrumentMemIntrinsicParam( 559ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany Instruction *OrigIns, 560800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) { 5616ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany IRBuilder<> IRB(InsertBefore); 5626ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany if (Size->getType() != IntptrTy) 5636ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Size = IRB.CreateIntCast(Size, IntptrTy, false); 564800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Check the first byte. 5656ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany instrumentAddress(OrigIns, InsertBefore, Addr, 8, IsWrite, Size); 566800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Check the last byte. 5676ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany IRB.SetInsertPoint(InsertBefore); 5686ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *SizeMinusOne = IRB.CreateSub(Size, ConstantInt::get(IntptrTy, 1)); 5696ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); 5706ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *AddrLast = IRB.CreateAdd(AddrLong, SizeMinusOne); 5716ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany instrumentAddress(OrigIns, InsertBefore, AddrLast, 8, IsWrite, Size); 572800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 573800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 574800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// Instrument memset/memmove/memcpy 575ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanybool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) { 576800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Dst = MI->getDest(); 577800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI); 5782735cf4aa52e31b8d2de90f836c3ad991215e04eKostya Serebryany Value *Src = MemTran ? MemTran->getSource() : 0; 579800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Length = MI->getLength(); 580800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 581800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Constant *ConstLength = dyn_cast<Constant>(Length); 582800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *InsertBefore = MI; 583800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ConstLength) { 584800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ConstLength->isNullValue()) return false; 585800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else { 586800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // The size is not a constant so it could be zero -- check at run-time. 587800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(InsertBefore); 588800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 589800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Cmp = IRB.CreateICmpNE(Length, 59056139bc493790612ee6281630678e293be6b2eb2Kostya Serebryany Constant::getNullValue(Length->getType())); 5914a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov InsertBefore = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false); 592800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 593800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 594ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true); 595800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (Src) 596ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false); 597800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return true; 598800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 599800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 600e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany// If I is an interesting memory access, return the PointerOperand 601e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany// and set IsWrite. Otherwise return NULL. 602e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryanystatic Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) { 603800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 604e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentReads) return NULL; 605e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = false; 606800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return LI->getPointerOperand(); 607800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 608e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 609e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentWrites) return NULL; 610e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = true; 611e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return SI->getPointerOperand(); 612e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany } 613e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { 614e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentAtomics) return NULL; 615e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = true; 616e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return RMW->getPointerOperand(); 617e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany } 618e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) { 619e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (!ClInstrumentAtomics) return NULL; 620e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany *IsWrite = true; 621e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return XCHG->getPointerOperand(); 622e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany } 623e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany return NULL; 624800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 625800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 626ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanyvoid AddressSanitizer::instrumentMop(Instruction *I) { 6273780ad8b998d93d7db406919c06137cdb786ef05Axel Naumann bool IsWrite = false; 628e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany Value *Addr = isInterestingMemoryAccess(I, &IsWrite); 629e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany assert(Addr); 6309b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (ClOpt && ClOptGlobals) { 6319b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) { 6329b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // If initialization order checking is disabled, a simple access to a 6339b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // dynamically initialized global is always valid. 634ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov if (!CheckInitOrder) 6359b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return; 6369b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // If a global variable does not have dynamic initialization we don't 637407790604b8f71f7172bbdfb76c27e1799d241c2Kostya Serebryany // have to instrument it. However, if a global does not have initailizer 638407790604b8f71f7172bbdfb76c27e1799d241c2Kostya Serebryany // at all, we assume it has dynamic initializer (in other TU). 639407790604b8f71f7172bbdfb76c27e1799d241c2Kostya Serebryany if (G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G)) 6409b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return; 6419b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 642800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 6439b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 644800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *OrigPtrTy = Addr->getType(); 645800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType(); 646800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 647800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(OrigTy->isSized()); 648605ff6655b31033dde21e61416751847bd0ee201Kostya Serebryany uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy); 649800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 6506ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany assert((TypeSize % 8) == 0); 651800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 6526ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany // Instrument a 1-, 2-, 4-, 8-, or 16- byte access with one check. 6536ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany if (TypeSize == 8 || TypeSize == 16 || 6546ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany TypeSize == 32 || TypeSize == 64 || TypeSize == 128) 6556ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany return instrumentAddress(I, I, Addr, TypeSize, IsWrite, 0); 6566ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany // Instrument unusual size (but still multiple of 8). 6576ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany // We can not do it with a single check, so we do 1-byte check for the first 6586ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany // and the last bytes. We call __asan_report_*_n(addr, real_size) to be able 6596ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany // to report the actual access size. 660800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(I); 6616ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *LastByte = IRB.CreateIntToPtr( 6626ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany IRB.CreateAdd(IRB.CreatePointerCast(Addr, IntptrTy), 6636ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany ConstantInt::get(IntptrTy, TypeSize / 8 - 1)), 6646ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany OrigPtrTy); 6656ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *Size = ConstantInt::get(IntptrTy, TypeSize / 8); 6666ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany instrumentAddress(I, I, Addr, 8, IsWrite, Size); 6676ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany instrumentAddress(I, I, LastByte, 8, IsWrite, Size); 668800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 669800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 67055cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// Validate the result of Module::getOrInsertFunction called for an interface 67155cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// function of AddressSanitizer. If the instrumented module defines a function 67255cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// with the same name, their prototypes must match, otherwise 67355cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko// getOrInsertFunction returns a bitcast. 674b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryanystatic Function *checkInterfaceFunction(Constant *FuncOrBitcast) { 67555cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast); 67655cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko FuncOrBitcast->dump(); 67755cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko report_fatal_error("trying to redefine an AddressSanitizer " 67855cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko "interface function"); 67955cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko} 68055cabae685991ac6bc6d96b14e2139f64a0c9967Alexander Potapenko 681800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya SerebryanyInstruction *AddressSanitizer::generateCrashCode( 682ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Instruction *InsertBefore, Value *Addr, 6836ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany bool IsWrite, size_t AccessSizeIndex, Value *SizeArgument) { 684ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany IRBuilder<> IRB(InsertBefore); 6856ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany CallInst *Call = SizeArgument 6866ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany ? IRB.CreateCall2(AsanErrorCallbackSized[IsWrite], Addr, SizeArgument) 6876ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany : IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex], Addr); 6886ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany 689f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // We don't do Call->setDoesNotReturn() because the BB already has 690f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // UnreachableInst at the end. 691f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // This EmptyAsm is required to avoid callback merge. 692f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany IRB.CreateCall(EmptyAsm); 6933c7faae346f548c55cad86d82a2e242443001f23Kostya Serebryany return Call; 694800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 695800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 6962735cf4aa52e31b8d2de90f836c3ad991215e04eKostya SerebryanyValue *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong, 697c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *ShadowValue, 698c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany uint32_t TypeSize) { 69919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t Granularity = 1 << Mapping.Scale; 700c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // Addr & (Granularity - 1) 701c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *LastAccessedByte = IRB.CreateAnd( 702c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany AddrLong, ConstantInt::get(IntptrTy, Granularity - 1)); 703c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // (Addr & (Granularity - 1)) + size - 1 704c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany if (TypeSize / 8 > 1) 705c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany LastAccessedByte = IRB.CreateAdd( 706c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)); 707c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // (uint8_t) ((Addr & (Granularity-1)) + size - 1) 708c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany LastAccessedByte = IRB.CreateIntCast( 7096e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany LastAccessedByte, ShadowValue->getType(), false); 710c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue 711c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue); 712c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany} 713c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany 714ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanyvoid AddressSanitizer::instrumentAddress(Instruction *OrigIns, 7156ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Instruction *InsertBefore, 7166ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Value *Addr, uint32_t TypeSize, 7176ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany bool IsWrite, Value *SizeArgument) { 7186ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany IRBuilder<> IRB(InsertBefore); 719800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); 720800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 721800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *ShadowTy = IntegerType::get( 72219cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov *C, std::max(8U, TypeSize >> Mapping.Scale)); 723800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); 724800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *ShadowPtr = memToShadow(AddrLong, IRB); 725800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *CmpVal = Constant::getNullValue(ShadowTy); 726800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *ShadowValue = IRB.CreateLoad( 727800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy)); 728800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 729800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal); 73011c2a47af825a0f89d75aaa97ad873ed2acef266Kostya Serebryany size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize); 73119cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t Granularity = 1 << Mapping.Scale; 732ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany TerminatorInst *CrashTerm = 0; 733ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany 7346e2d506dc962873a0e05092bbb034f9a615d1084Kostya Serebryany if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) { 7354a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov TerminatorInst *CheckTerm = 7364a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false); 737ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional()); 738f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany BasicBlock *NextBB = CheckTerm->getSuccessor(0); 739c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany IRB.SetInsertPoint(CheckTerm); 740c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize); 741ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany BasicBlock *CrashBlock = 742ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany BasicBlock::Create(*C, "", NextBB->getParent(), NextBB); 743ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany CrashTerm = new UnreachableInst(*C, CrashBlock); 744f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2); 745f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany ReplaceInstWithInst(CheckTerm, NewTerm); 746c0ed3e548c6f688e22685de04e210c7b59ac3433Kostya Serebryany } else { 7474a2dec05cef5882b745dd248d79e42a42cdbc87bEvgeniy Stepanov CrashTerm = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), true); 748800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 749ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany 7506ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany Instruction *Crash = generateCrashCode( 7516ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany CrashTerm, AddrLong, IsWrite, AccessSizeIndex, SizeArgument); 752ebd6454929f2ba3b92f61c151eccde0b8b0a8dedKostya Serebryany Crash->setDebugLoc(OrigIns->getDebugLoc()); 753800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 754800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 7551416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanyvoid AddressSanitizerModule::createInitializerPoisonCalls( 756b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Module &M, Value *FirstAddr, Value *LastAddr) { 7579b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // We do all of our poisoning and unpoisoning within _GLOBAL__I_a. 7589b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Function *GlobalInit = M.getFunction("_GLOBAL__I_a"); 7599b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // If that function is not present, this TU contains no globals, or they have 7609b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // all been optimized away 7619b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (!GlobalInit) 7629b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return; 7639b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7649b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Set up the arguments to our poison/unpoison functions. 7659b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt()); 7669b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7679b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Add a call to poison all external globals before the given function starts. 7689b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany IRB.CreateCall2(AsanPoisonGlobals, FirstAddr, LastAddr); 7699b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7709b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Add calls to unpoison all globals before each return instruction. 7719b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end(); 7729b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany I != E; ++I) { 7739b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) { 7749b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany CallInst::Create(AsanUnpoisonGlobals, "", RI); 7759b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 7769b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 7779b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany} 7789b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 7791416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanybool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) { 7809b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Type *Ty = cast<PointerType>(G->getType())->getElementType(); 781324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "GLOBAL: " << *G << "\n"); 7829b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 78359a4a47a7bea7cc17877c6a3954ad9f8309ff1cbKostya Serebryany if (BL->isIn(*G)) return false; 7849b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (!Ty->isSized()) return false; 7859b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (!G->hasInitializer()) return false; 78651c7c65e32f76ec5a50cdecfe2b4c287c57da127Kostya Serebryany if (GlobalWasGeneratedByAsan(G)) return false; // Our own global. 7879b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Touch only those globals that will not be defined in other modules. 7889b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Don't handle ODR type linkages since other modules may be built w/o asan. 7899b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (G->getLinkage() != GlobalVariable::ExternalLinkage && 7909b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany G->getLinkage() != GlobalVariable::PrivateLinkage && 7919b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany G->getLinkage() != GlobalVariable::InternalLinkage) 7929b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 7939b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Two problems with thread-locals: 7949b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // - The address of the main thread's copy can't be computed at link-time. 7959b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // - Need to poison all copies, not just the main thread's one. 7969b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (G->isThreadLocal()) 7979b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 7989b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // For now, just ignore this Alloca if the alignment is large. 799b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany if (G->getAlignment() > RedzoneSize()) return false; 8009b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 8019b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Ignore all the globals with the names starting with "\01L_OBJC_". 8029b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Many of those are put into the .cstring section. The linker compresses 8039b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // that section by removing the spare \0s after the string terminator, so 8049b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // our redzones get broken. 8059b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if ((G->getName().find("\01L_OBJC_") == 0) || 8069b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany (G->getName().find("\01l_OBJC_") == 0)) { 8079b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G); 8089b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 8099b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 8109b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 8119b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (G->hasSection()) { 8129b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany StringRef Section(G->getSection()); 8139b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Ignore the globals from the __OBJC section. The ObjC runtime assumes 8149b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to 8159b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // them. 8169b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if ((Section.find("__OBJC,") == 0) || 8179b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany (Section.find("__DATA, __objc_") == 0)) { 8189b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G); 8199b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 8209b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 8219b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // See http://code.google.com/p/address-sanitizer/issues/detail?id=32 8229b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Constant CFString instances are compiled in the following way: 8239b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // -- the string buffer is emitted into 8249b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // __TEXT,__cstring,cstring_literals 8259b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // -- the constant NSConstantString structure referencing that buffer 8269b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // is placed into __DATA,__cfstring 8279b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Therefore there's no point in placing redzones into __DATA,__cfstring. 8289b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Moreover, it causes the linker to crash on OS X 10.7 8299b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (Section.find("__DATA,__cfstring") == 0) { 8309b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany DEBUG(dbgs() << "Ignoring CFString: " << *G); 8319b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return false; 8329b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 8339b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 8349b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 8359b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany return true; 8369b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany} 8379b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 8384684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonovvoid AddressSanitizerModule::initializeCallbacks(Module &M) { 8394684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov IRBuilder<> IRB(*C); 8404684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov // Declare our poisoning and unpoisoning functions. 8414684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8424684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 8434684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanPoisonGlobals->setLinkage(Function::ExternalLinkage); 8444684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8454684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL)); 8464684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage); 8474684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov // Declare functions that register/unregister globals. 8484684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8494684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanRegisterGlobalsName, IRB.getVoidTy(), 8504684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov IntptrTy, IntptrTy, NULL)); 8514684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanRegisterGlobals->setLinkage(Function::ExternalLinkage); 8524684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnregisterGlobals = checkInterfaceFunction(M.getOrInsertFunction( 8534684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov kAsanUnregisterGlobalsName, 8544684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 8554684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage); 8564684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov} 8574684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov 858800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// This function replaces all global variables with new variables that have 859800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// trailing redzones. It also creates a function that poisons 860800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany// redzones and inserts this function into llvm.global_ctors. 8611416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryanybool AddressSanitizerModule::runOnModule(Module &M) { 8621416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany if (!ClGlobals) return false; 8631416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany TD = getAnalysisIfAvailable<DataLayout>(); 8641416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany if (!TD) 8651416edc30adbd53b2be863f7f3de56de4a4c9d0aKostya Serebryany return false; 866b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BL.reset(new BlackList(BlacklistFile)); 867d6f62c8da5aa4f3388cec1542309ffa623cac601Alexey Samsonov if (BL->isIn(M)) return false; 868b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany C = &(M.getContext()); 86919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov int LongSize = TD->getPointerSizeInBits(); 87019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov IntptrTy = Type::getIntNTy(*C, LongSize); 87111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov Mapping = getShadowMapping(M, LongSize, ZeroBaseShadow); 8724684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov initializeCallbacks(M); 8734684858624d7ffe82379783e9b678227d5e0b515Alexey Samsonov DynamicallyInitializedGlobals.Init(M); 874b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 875800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallVector<GlobalVariable *, 16> GlobalsToChange; 876800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 8779b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany for (Module::GlobalListType::iterator G = M.global_begin(), 8789b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany E = M.global_end(); G != E; ++G) { 8799b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (ShouldInstrumentGlobal(G)) 8809b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany GlobalsToChange.push_back(G); 881800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 882800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 883800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany size_t n = GlobalsToChange.size(); 884800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (n == 0) return false; 885800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 886800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // A global is described by a structure 887800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // size_t beg; 888800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // size_t size; 889800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // size_t size_with_redzone; 890800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // const char *name; 891086a472dbed9911f83add781e020cb49c89829d0Kostya Serebryany // const char *module_name; 8929b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // size_t has_dynamic_init; 893800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // We initialize an array of such structures and pass it to a run-time call. 894800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy, 8959b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany IntptrTy, IntptrTy, 896086a472dbed9911f83add781e020cb49c89829d0Kostya Serebryany IntptrTy, IntptrTy, NULL); 8979b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany SmallVector<Constant *, 16> Initializers(n), DynamicInit; 898800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 899b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany 900b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Function *CtorFunc = M.getFunction(kAsanModuleCtorName); 901b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany assert(CtorFunc); 902b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator()); 903800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 9049b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // The addresses of the first and last dynamically initialized globals in 9059b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // this TU. Used in initialization order checking. 9069b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany Value *FirstDynamic = 0, *LastDynamic = 0; 9079b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 908086a472dbed9911f83add781e020cb49c89829d0Kostya Serebryany GlobalVariable *ModuleName = createPrivateGlobalForString( 909086a472dbed9911f83add781e020cb49c89829d0Kostya Serebryany M, M.getModuleIdentifier()); 910086a472dbed9911f83add781e020cb49c89829d0Kostya Serebryany 911800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0; i < n; i++) { 91229f975f8ffda1f5d78cbf2530c2316abef11aa70Kostya Serebryany static const uint64_t kMaxGlobalRedzone = 1 << 18; 913800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalVariable *G = GlobalsToChange[i]; 914800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany PointerType *PtrTy = cast<PointerType>(G->getType()); 915800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *Ty = PtrTy->getElementType(); 916208a4ff2b56f453910bb817540f34b8169f7702aKostya Serebryany uint64_t SizeInBytes = TD->getTypeAllocSize(Ty); 91729f975f8ffda1f5d78cbf2530c2316abef11aa70Kostya Serebryany uint64_t MinRZ = RedzoneSize(); 91863f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany // MinRZ <= RZ <= kMaxGlobalRedzone 91963f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany // and trying to make RZ to be ~ 1/4 of SizeInBytes. 92029f975f8ffda1f5d78cbf2530c2316abef11aa70Kostya Serebryany uint64_t RZ = std::max(MinRZ, 92163f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany std::min(kMaxGlobalRedzone, 92263f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany (SizeInBytes / MinRZ / 4) * MinRZ)); 92363f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany uint64_t RightRedzoneSize = RZ; 92463f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany // Round up to MinRZ 92563f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany if (SizeInBytes % MinRZ) 92663f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany RightRedzoneSize += MinRZ - (SizeInBytes % MinRZ); 92763f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0); 928800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize); 9299b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Determine whether this global should be poisoned in initialization. 930ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany bool GlobalHasDynamicInitializer = 931ca23d43084c45a7d6f4371d62f45be044650ce38Kostya Serebryany DynamicallyInitializedGlobals.Contains(G); 93259a4a47a7bea7cc17877c6a3954ad9f8309ff1cbKostya Serebryany // Don't check initialization order if this global is blacklisted. 9337dadac65d375ff21b7a36bfb7642578d2f467525Kostya Serebryany GlobalHasDynamicInitializer &= !BL->isInInit(*G); 934800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 935800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL); 936800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Constant *NewInitializer = ConstantStruct::get( 937800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NewTy, G->getInitializer(), 938800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Constant::getNullValue(RightRedZoneTy), NULL); 939800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 940086a472dbed9911f83add781e020cb49c89829d0Kostya Serebryany GlobalVariable *Name = createPrivateGlobalForString(M, G->getName()); 941800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 942800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Create a new global variable with enough space for a redzone. 943800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalVariable *NewGlobal = new GlobalVariable( 944800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany M, NewTy, G->isConstant(), G->getLinkage(), 945ce718ff9f42c7da092eaa01dd0242e8d5ba84713Hans Wennborg NewInitializer, "", G, G->getThreadLocalMode()); 946800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NewGlobal->copyAttributesFrom(G); 94763f0846f1eb43332a08811d332b813276b727eb6Kostya Serebryany NewGlobal->setAlignment(MinRZ); 948800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 949800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Indices2[2]; 950800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Indices2[0] = IRB.getInt32(0); 951800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Indices2[1] = IRB.getInt32(0); 952800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 953800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany G->replaceAllUsesWith( 954f1639abf1aaba1448f719f595156cd0f4cd560ccKostya Serebryany ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true)); 955800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NewGlobal->takeName(G); 956800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany G->eraseFromParent(); 957800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 958800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Initializers[i] = ConstantStruct::get( 959800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalStructTy, 960800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantExpr::getPointerCast(NewGlobal, IntptrTy), 961800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, SizeInBytes), 962800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize), 963800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantExpr::getPointerCast(Name, IntptrTy), 964086a472dbed9911f83add781e020cb49c89829d0Kostya Serebryany ConstantExpr::getPointerCast(ModuleName, IntptrTy), 9659b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer), 966800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NULL); 9679b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 9689b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Populate the first and last globals declared in this TU. 969ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov if (CheckInitOrder && GlobalHasDynamicInitializer) { 9709b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany LastDynamic = ConstantExpr::getPointerCast(NewGlobal, IntptrTy); 9719b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany if (FirstDynamic == 0) 9729b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany FirstDynamic = LastDynamic; 9739b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany } 9749b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany 975324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n"); 976800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 977800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 978800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n); 979800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany GlobalVariable *AllGlobals = new GlobalVariable( 980800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage, 981800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantArray::get(ArrayOfGlobalStructTy, Initializers), ""); 982800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 9839b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany // Create calls for poisoning before initializers run and unpoisoning after. 984ee548275c63a1eeffda9d3edd2bea04e1dadcc67Alexey Samsonov if (CheckInitOrder && FirstDynamic && LastDynamic) 9859b9f87a87ac1b373c4c6a70904315bebbd01c50cKostya Serebryany createInitializerPoisonCalls(M, FirstDynamic, LastDynamic); 986800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateCall2(AsanRegisterGlobals, 987800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreatePointerCast(AllGlobals, IntptrTy), 988800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, n)); 989800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 9907bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany // We also need to unregister globals at the end, e.g. when a shared library 9917bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany // gets closed. 9927bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany Function *AsanDtorFunction = Function::Create( 9937bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany FunctionType::get(Type::getVoidTy(*C), false), 9947bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany GlobalValue::InternalLinkage, kAsanModuleDtorName, &M); 9957bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction); 9967bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB)); 9977bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany IRB_Dtor.CreateCall2(AsanUnregisterGlobals, 9987bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany IRB.CreatePointerCast(AllGlobals, IntptrTy), 9997bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany ConstantInt::get(IntptrTy, n)); 10007bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority); 10017bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany 1002800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany DEBUG(dbgs() << M); 1003800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return true; 1004800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1005800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 10068b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryanyvoid AddressSanitizer::initializeCallbacks(Module &M) { 10078b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IRBuilder<> IRB(*C); 10089db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany // Create __asan_report* callbacks. 10099db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) { 10109db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes; 10119db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany AccessSizeIndex++) { 10129db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany // IsWrite and TypeSize are encoded in the function name. 10139db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany std::string FunctionName = std::string(kAsanReportErrorTemplate) + 10149db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex); 10154f0c69623c10a3a49f6926fd53694ee532e06a85Kostya Serebryany // If we are merging crash callbacks, they have two parameters. 10167846c1c851a53a8280f9d8ed57cd98d82c742551Kostya Serebryany AsanErrorCallback[AccessIsWrite][AccessSizeIndex] = 10177846c1c851a53a8280f9d8ed57cd98d82c742551Kostya Serebryany checkInterfaceFunction(M.getOrInsertFunction( 10187846c1c851a53a8280f9d8ed57cd98d82c742551Kostya Serebryany FunctionName, IRB.getVoidTy(), IntptrTy, NULL)); 10199db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany } 10209db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany } 10216ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany AsanErrorCallbackSized[0] = checkInterfaceFunction(M.getOrInsertFunction( 10226ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany kAsanReportLoadN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 10236ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany AsanErrorCallbackSized[1] = checkInterfaceFunction(M.getOrInsertFunction( 10246ecccdbb2bf24a011b9c8ecbdd39be5a02269670Kostya Serebryany kAsanReportStoreN, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 1025ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany 1026ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction( 1027ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany kAsanHandleNoReturnName, IRB.getVoidTy(), NULL)); 1028f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany // We insert an empty inline asm after __asan_report* to avoid callback merge. 1029f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false), 1030f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany StringRef(""), StringRef(""), 1031f7b08226eb44458f6f38cbeaca527028803c725aKostya Serebryany /*hasSideEffects=*/true); 10328b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany} 10338b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 103419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonovvoid AddressSanitizer::emitShadowMapping(Module &M, IRBuilder<> &IRB) const { 103511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov // Tell the values of mapping offset and scale to the run-time. 103611af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov GlobalValue *asan_mapping_offset = 103711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage, 103811af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ConstantInt::get(IntptrTy, Mapping.Offset), 103911af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov kAsanMappingOffsetName); 104011af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov // Read the global, otherwise it may be optimized away. 104111af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov IRB.CreateLoad(asan_mapping_offset, true); 104211af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov 104311af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov GlobalValue *asan_mapping_scale = 104411af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage, 104511af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov ConstantInt::get(IntptrTy, Mapping.Scale), 104611af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov kAsanMappingScaleName); 104711af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov // Read the global, otherwise it may be optimized away. 104811af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov IRB.CreateLoad(asan_mapping_scale, true); 104919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov} 105019cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 10518b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany// virtual 10528b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryanybool AddressSanitizer::doInitialization(Module &M) { 10538b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany // Initialize the private fields. No one has accessed them before. 10548b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany TD = getAnalysisIfAvailable<DataLayout>(); 10558b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 10568b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany if (!TD) 10578b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany return false; 1058b0dcf61252e58715a3bea79f4c112572df361c30Alexey Samsonov BL.reset(new BlackList(BlacklistFile)); 10598b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany DynamicallyInitializedGlobals.Init(M); 10608b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 10618b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany C = &(M.getContext()); 10628b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany LongSize = TD->getPointerSizeInBits(); 10638b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IntptrTy = Type::getIntNTy(*C, LongSize); 10648b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany 10658b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany AsanCtorFunction = Function::Create( 10668b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany FunctionType::get(Type::getVoidTy(*C), false), 10678b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany GlobalValue::InternalLinkage, kAsanModuleCtorName, &M); 10688b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction); 10698b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany // call __asan_init in the module ctor. 10708b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB)); 10718b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany AsanInitFunction = checkInterfaceFunction( 10728b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL)); 10738b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany AsanInitFunction->setLinkage(Function::ExternalLinkage); 10748b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany IRB.CreateCall(AsanInitFunction); 10759db5b5ffa9fccd5c7f1f39a3e9aa66cc4a5eedc1Kostya Serebryany 107611af9a873f9e1409a422ab31e22729368805afafAlexey Samsonov Mapping = getShadowMapping(M, LongSize, ZeroBaseShadow); 107719cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov emitShadowMapping(M, IRB); 1078800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 10797bcfc9950bac0f411f9671e8d6ce483bd219727eKostya Serebryany appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority); 1080ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany return true; 1081ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany} 1082ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany 1083a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryanybool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) { 1084a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // For each NSObject descendant having a +load method, this method is invoked 1085a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // by the ObjC runtime before any of the static constructors is called. 1086a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // Therefore we need to instrument such methods with a call to __asan_init 1087a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // at the beginning in order to initialize our runtime before any access to 1088a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // the shadow memory. 1089a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // We cannot just ignore these methods, because they may call other 1090a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany // instrumented functions. 1091a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany if (F.getName().find(" load]") != std::string::npos) { 1092a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany IRBuilder<> IRB(F.begin()->begin()); 1093a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany IRB.CreateCall(AsanInitFunction); 1094a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany return true; 1095a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany } 1096a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany return false; 1097a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany} 1098a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany 1099ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryanybool AddressSanitizer::runOnFunction(Function &F) { 1100800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (BL->isIn(F)) return false; 1101800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (&F == AsanCtorFunction) return false; 11023797adb94fdc6b747cb0e97a64b15b931f2533b8Kostya Serebryany if (F.getLinkage() == GlobalValue::AvailableExternallyLinkage) return false; 1103324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n"); 11048b390ffbfdd52a23a45a21de99aa1c31f3ce623fKostya Serebryany initializeCallbacks(*F.getParent()); 1105a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany 11068eec41fc778e99d42172a7f6de76faa43a6d8847Kostya Serebryany // If needed, insert __asan_init before checking for SanitizeAddress attr. 1107a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany maybeInsertAsanInitAtFunctionEntry(F); 1108a1a8a323f43a95620fc4c6b5aad8d5ff5e5eb590Kostya Serebryany 1109831737d329a727f53a1fb0572f7b7a8127208881Bill Wendling if (!F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, 11108eec41fc778e99d42172a7f6de76faa43a6d8847Kostya Serebryany Attribute::SanitizeAddress)) 11116765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling return false; 1112800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1113800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (!ClDebugFunc.empty() && ClDebugFunc != F.getName()) 1114800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return false; 11156765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling 11166765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling // We want to instrument every address only once per basic block (unless there 11176765834754cbb3cb0f15b4b15e98c5e73fa50066Bill Wendling // are calls between uses). 1118800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallSet<Value*, 16> TempsToInstrument; 1119800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallVector<Instruction*, 16> ToInstrument; 112095e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany SmallVector<Instruction*, 8> NoReturnCalls; 1121e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany bool IsWrite; 1122800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1123800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Fill the set of memory operations to instrument. 1124800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (Function::iterator FI = F.begin(), FE = F.end(); 1125800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany FI != FE; ++FI) { 1126800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany TempsToInstrument.clear(); 1127324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany int NumInsnsPerBB = 0; 1128800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); 1129800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany BI != BE; ++BI) { 1130bcb55ce3862bbbedac4e09d7099c9e0efc434e4bKostya Serebryany if (LooksLikeCodeInBug11395(BI)) return false; 1131e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) { 1132800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ClOpt && ClOptSameTemp) { 1133800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (!TempsToInstrument.insert(Addr)) 1134800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany continue; // We've seen this temp in the current BB. 1135800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1136800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) { 1137800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // ok, take it. 1138800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else { 11391479c9bb392325688b72e5829bbb7939c4a079a4Kostya Serebryany CallSite CS(BI); 11401479c9bb392325688b72e5829bbb7939c4a079a4Kostya Serebryany if (CS) { 1141800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // A call inside BB. 1142800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany TempsToInstrument.clear(); 11431479c9bb392325688b72e5829bbb7939c4a079a4Kostya Serebryany if (CS.doesNotReturn()) 11441479c9bb392325688b72e5829bbb7939c4a079a4Kostya Serebryany NoReturnCalls.push_back(CS.getInstruction()); 1145800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1146800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany continue; 1147800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1148800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ToInstrument.push_back(BI); 1149324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany NumInsnsPerBB++; 1150324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB) 1151324cbb89f2759fb9ad347bd2af4631e50c39c8f3Kostya Serebryany break; 1152800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1153800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1154800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1155800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Instrument. 1156800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany int NumInstrumented = 0; 1157800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = ToInstrument.size(); i != n; i++) { 1158800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *Inst = ToInstrument[i]; 1159800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ClDebugMin < 0 || ClDebugMax < 0 || 1160800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) { 1161e6cf2e0bd09544eeb69665deb908d264e62a71c2Kostya Serebryany if (isInterestingMemoryAccess(Inst, &IsWrite)) 1162ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMop(Inst); 1163800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany else 1164ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany instrumentMemIntrinsic(cast<MemIntrinsic>(Inst)); 1165800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1166800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany NumInstrumented++; 1167800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1168800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 116959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov FunctionStackPoisoner FSP(F, *this); 117059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool ChangedStack = FSP.runOnFunction(); 117195e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany 117295e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany // We must unpoison the stack before every NoReturn call (throw, _exit, etc). 117395e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37 117495e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) { 117595e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany Instruction *CI = NoReturnCalls[i]; 117695e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany IRBuilder<> IRB(CI); 1177ee4edeccabe1854ee895f52d4ac5588bd5f40c80Kostya Serebryany IRB.CreateCall(AsanHandleNoReturnFunc); 117895e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany } 1179324d96b9e265b0fd8bf63a28340910def64e2164Kostya Serebryany DEBUG(dbgs() << "ASAN done instrumenting:\n" << F << "\n"); 118095e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany 118195e3cf44a9d4672655dee3bd558bfeefa631dd55Kostya Serebryany return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty(); 1182800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1183800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1184800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) { 1185800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ShadowRedzoneSize == 1) return PoisonByte; 1186800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte; 1187800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (ShadowRedzoneSize == 4) 1188800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany return (PoisonByte << 24) + (PoisonByte << 16) + 1189800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany (PoisonByte << 8) + (PoisonByte); 1190858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4"); 1191800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1192800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1193800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryanystatic void PoisonShadowPartialRightRedzone(uint8_t *Shadow, 1194800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany size_t Size, 1195b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany size_t RZSize, 1196800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany size_t ShadowGranularity, 1197800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint8_t Magic) { 1198b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany for (size_t i = 0; i < RZSize; 1199800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany i+= ShadowGranularity, Shadow++) { 1200800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (i + ShadowGranularity <= Size) { 1201800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany *Shadow = 0; // fully addressable 1202800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else if (i >= Size) { 1203800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany *Shadow = Magic; // unaddressable 1204800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } else { 1205800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany *Shadow = Size - i; // first Size-i bytes are addressable 1206800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1207800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1208800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1209800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 121059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// Workaround for bug 11395: we don't want to instrument stack in functions 121159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// with large assembly blobs (32-bit only), otherwise reg alloc may crash. 121259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// FIXME: remove once the bug 11395 is fixed. 121359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovbool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) { 121459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (LongSize != 32) return false; 121559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov CallInst *CI = dyn_cast<CallInst>(I); 121659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (!CI || !CI->isInlineAsm()) return false; 121759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (CI->getNumArgOperands() <= 5) return false; 121859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov // We have inline assembly with quite a few arguments. 121959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return true; 122059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov} 122159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 122259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::initializeCallbacks(Module &M) { 122359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IRBuilder<> IRB(*C); 122459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanStackMallocFunc = checkInterfaceFunction(M.getOrInsertFunction( 122559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL)); 122659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanStackFreeFunc = checkInterfaceFunction(M.getOrInsertFunction( 122759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanStackFreeName, IRB.getVoidTy(), 122859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IntptrTy, IntptrTy, IntptrTy, NULL)); 122959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction( 123059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 123159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction( 123259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL)); 123359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov} 123459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 123559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::poisonRedZones( 123659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB, Value *ShadowBase, 123759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool DoPoison) { 123819cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov size_t ShadowRZSize = RedzoneSize() >> Mapping.Scale; 1239800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(ShadowRZSize >= 1 && ShadowRZSize <= 4); 1240800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8); 1241800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *RZPtrTy = PointerType::get(RZTy, 0); 1242800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1243800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PoisonLeft = ConstantInt::get(RZTy, 1244800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize)); 1245800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PoisonMid = ConstantInt::get(RZTy, 1246800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize)); 1247800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PoisonRight = ConstantInt::get(RZTy, 1248800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize)); 1249800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1250800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // poison the first red zone. 1251800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy)); 1252800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1253800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // poison all other red zones. 1254b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany uint64_t Pos = RedzoneSize(); 1255800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = AllocaVec.size(); i < n; i++) { 1256800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany AllocaInst *AI = AllocaVec[i]; 1257800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t SizeInBytes = getAllocaSizeInBytes(AI); 1258800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t AlignedSize = getAlignedAllocaSize(AI); 1259b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany assert(AlignedSize - SizeInBytes < RedzoneSize()); 1260800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *Ptr = NULL; 1261800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1262800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Pos += AlignedSize; 1263800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1264800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(ShadowBase->getType() == IntptrTy); 1265800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (SizeInBytes < AlignedSize) { 1266800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Poison the partial redzone at right 1267800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Ptr = IRB.CreateAdd( 1268800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ShadowBase, ConstantInt::get(IntptrTy, 126919cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov (Pos >> Mapping.Scale) - ShadowRZSize)); 1270b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany size_t AddressableBytes = RedzoneSize() - (AlignedSize - SizeInBytes); 1271800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint32_t Poison = 0; 1272800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (DoPoison) { 1273800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes, 1274b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany RedzoneSize(), 127519cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov 1ULL << Mapping.Scale, 1276800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany kAsanStackPartialRedzoneMagic); 1277800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1278800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *PartialPoison = ConstantInt::get(RZTy, Poison); 1279800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy)); 1280800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1281800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1282800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Poison the full redzone at right. 1283800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Ptr = IRB.CreateAdd(ShadowBase, 128419cd7e9ce28ed7f3326ebcd386eec215ab3763f9Alexey Samsonov ConstantInt::get(IntptrTy, Pos >> Mapping.Scale)); 12851c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool LastAlloca = (i == AllocaVec.size() - 1); 12861c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Value *Poison = LastAlloca ? PoisonRight : PoisonMid; 1287800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy)); 1288800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1289b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Pos += RedzoneSize(); 1290800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1291800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1292800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 129359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::poisonStack() { 129459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov uint64_t LocalStackSize = TotalStackSize + 129559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov (AllocaVec.size() + 1) * RedzoneSize(); 1296800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 129759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov bool DoStackMalloc = ASan.CheckUseAfterReturn 1298800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany && LocalStackSize <= kMaxStackMallocSize; 1299800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 13001c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov assert(AllocaVec.size() > 0); 1301800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *InsBefore = AllocaVec[0]; 1302800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRB(InsBefore); 1303800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1304800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1305800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize); 1306800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany AllocaInst *MyAlloca = 1307800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore); 130859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov if (ClRealignStack && StackAlignment < RedzoneSize()) 130959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov StackAlignment = RedzoneSize(); 131059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov MyAlloca->setAlignment(StackAlignment); 1311800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(MyAlloca->isStaticAlloca()); 1312800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy); 1313800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *LocalStackBase = OrigStackBase; 1314800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1315800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (DoStackMalloc) { 1316800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc, 1317800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase); 1318800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1319800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 13203016056769639878b4f152838f0cf16d2e482339Kostya Serebryany // This string will be parsed by the run-time (DescribeAddressIfStack). 1321800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany SmallString<2048> StackDescriptionStorage; 1322800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany raw_svector_ostream StackDescription(StackDescriptionStorage); 13233016056769639878b4f152838f0cf16d2e482339Kostya Serebryany StackDescription << AllocaVec.size() << " "; 1324800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 13251c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Insert poison calls for lifetime intrinsics for alloca. 13261c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov bool HavePoisonedAllocas = false; 13271c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov for (size_t i = 0, n = AllocaPoisonCallVec.size(); i < n; i++) { 13281c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov const AllocaPoisonCall &APC = AllocaPoisonCallVec[i]; 13291c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov IntrinsicInst *II = APC.InsBefore; 13301c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *AI = findAllocaForValue(II->getArgOperand(1)); 13311c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov assert(AI); 13321c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov IRBuilder<> IRB(II); 13331c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov poisonAlloca(AI, APC.Size, IRB, APC.DoPoison); 13341c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov HavePoisonedAllocas |= APC.DoPoison; 13351c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov } 13361c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov 1337b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany uint64_t Pos = RedzoneSize(); 1338800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Replace Alloca instructions with base+offset. 1339800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = AllocaVec.size(); i < n; i++) { 1340800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany AllocaInst *AI = AllocaVec[i]; 1341800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t SizeInBytes = getAllocaSizeInBytes(AI); 1342800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StringRef Name = AI->getName(); 1343800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany StackDescription << Pos << " " << SizeInBytes << " " 1344800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany << Name.size() << " " << Name << " "; 1345800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany uint64_t AlignedSize = getAlignedAllocaSize(AI); 1346b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany assert((AlignedSize % RedzoneSize()) == 0); 1347f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov Value *NewAllocaPtr = IRB.CreateIntToPtr( 1348800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)), 1349f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov AI->getType()); 13501afbb517965e29b07cb42e2335d5eadd87de6535Alexey Samsonov replaceDbgDeclareForAlloca(AI, NewAllocaPtr, DIB); 1351f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov AI->replaceAllUsesWith(NewAllocaPtr); 1352b9a12ea0fd92bfdb4c6eb5af333648a618f68686Kostya Serebryany Pos += AlignedSize + RedzoneSize(); 1353800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1354800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany assert(Pos == LocalStackSize); 1355800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 13563016056769639878b4f152838f0cf16d2e482339Kostya Serebryany // The left-most redzone has enough space for at least 4 pointers. 13573016056769639878b4f152838f0cf16d2e482339Kostya Serebryany // Write the Magic value to redzone[0]. 1358800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy); 1359800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic), 1360800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany BasePlus0); 13613016056769639878b4f152838f0cf16d2e482339Kostya Serebryany // Write the frame description constant to redzone[1]. 13623016056769639878b4f152838f0cf16d2e482339Kostya Serebryany Value *BasePlus1 = IRB.CreateIntToPtr( 13633016056769639878b4f152838f0cf16d2e482339Kostya Serebryany IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, ASan.LongSize/8)), 13643016056769639878b4f152838f0cf16d2e482339Kostya Serebryany IntptrPtrTy); 13659ce84c1c95c0153a2f33e188ce0db00770425f9eAlexey Samsonov GlobalVariable *StackDescriptionGlobal = 1366a5f54f14435d881b10d8eb65e19fa42af95757e9Kostya Serebryany createPrivateGlobalForString(*F.getParent(), StackDescription.str()); 136759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, 136859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IntptrTy); 1369800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRB.CreateStore(Description, BasePlus1); 13703016056769639878b4f152838f0cf16d2e482339Kostya Serebryany // Write the PC to redzone[2]. 13713016056769639878b4f152838f0cf16d2e482339Kostya Serebryany Value *BasePlus2 = IRB.CreateIntToPtr( 13723016056769639878b4f152838f0cf16d2e482339Kostya Serebryany IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, 13733016056769639878b4f152838f0cf16d2e482339Kostya Serebryany 2 * ASan.LongSize/8)), 13743016056769639878b4f152838f0cf16d2e482339Kostya Serebryany IntptrPtrTy); 13753016056769639878b4f152838f0cf16d2e482339Kostya Serebryany IRB.CreateStore(IRB.CreatePointerCast(&F, IntptrTy), BasePlus2); 1376800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1377800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Poison the stack redzones at the entry. 137859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov Value *ShadowBase = ASan.memToShadow(LocalStackBase, IRB); 137959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov poisonRedZones(AllocaVec, IRB, ShadowBase, true); 1380800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1381800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Unpoison the stack before all ret instructions. 1382800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany for (size_t i = 0, n = RetVec.size(); i < n; i++) { 1383800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany Instruction *Ret = RetVec[i]; 1384800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBuilder<> IRBRet(Ret); 1385800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Mark the current frame as retired. 1386800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic), 1387800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany BasePlus0); 1388800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany // Unpoison the stack. 138959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov poisonRedZones(AllocaVec, IRBRet, ShadowBase, false); 1390800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany if (DoStackMalloc) { 1391f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // In use-after-return mode, mark the whole stack frame unaddressable. 1392800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase, 1393800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany ConstantInt::get(IntptrTy, LocalStackSize), 1394800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany OrigStackBase); 1395f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov } else if (HavePoisonedAllocas) { 1396f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // If we poisoned some allocas in llvm.lifetime analysis, 1397f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // unpoison whole stack frame now. 1398f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov assert(LocalStackBase == OrigStackBase); 1399f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false); 1400800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1401800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany } 1402800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany 1403bd0052a0f26f04b8fcf59e8f645e5e33751e1f6eKostya Serebryany // We are done. Remove the old unused alloca instructions. 1404bd0052a0f26f04b8fcf59e8f645e5e33751e1f6eKostya Serebryany for (size_t i = 0, n = AllocaVec.size(); i < n; i++) 1405bd0052a0f26f04b8fcf59e8f645e5e33751e1f6eKostya Serebryany AllocaVec[i]->eraseFromParent(); 1406800e03f59896ef4b26d988f1878370bb5aeec0d8Kostya Serebryany} 1407f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov 140859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonovvoid FunctionStackPoisoner::poisonAlloca(Value *V, uint64_t Size, 140959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov IRBuilder<> IRB, bool DoPoison) { 1410f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov // For now just insert the call to ASan runtime. 1411f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy); 1412f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov Value *SizeArg = ConstantInt::get(IntptrTy, Size); 1413f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc 1414f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov : AsanUnpoisonStackMemoryFunc, 1415f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov AddrArg, SizeArg); 1416f985f44b13681071e585acb7a5703a2c1c23b6ceAlexey Samsonov} 141759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 141859cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// Handling llvm.lifetime intrinsics for a given %alloca: 141959cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca. 142059cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// (2) if %size is constant, poison memory for llvm.lifetime.end (to detect 142159cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// invalid accesses) and unpoison it for llvm.lifetime.start (the memory 142259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// could be poisoned by previous llvm.lifetime.end instruction, as the 142359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// variable may go in and out of scope several times, e.g. in loops). 142459cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// (3) if we poisoned at least one %alloca in a function, 142559cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov// unpoison the whole stack frame at function exit. 142659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov 14271c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey SamsonovAllocaInst *FunctionStackPoisoner::findAllocaForValue(Value *V) { 14281c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) 14291c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // We're intested only in allocas we can handle. 14301c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return isInterestingAlloca(*AI) ? AI : 0; 14311c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // See if we've already calculated (or started to calculate) alloca for a 14321c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // given value. 14331c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValueMapTy::iterator I = AllocaForValue.find(V); 14341c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (I != AllocaForValue.end()) 14351c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return I->second; 14361c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Store 0 while we're calculating alloca for value V to avoid 14371c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // infinite recursion if the value references itself. 14381c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValue[V] = 0; 14391c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *Res = 0; 14401c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (CastInst *CI = dyn_cast<CastInst>(V)) 14411c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Res = findAllocaForValue(CI->getOperand(0)); 14421c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov else if (PHINode *PN = dyn_cast<PHINode>(V)) { 14431c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 14441c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Value *IncValue = PN->getIncomingValue(i); 14451c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // Allow self-referencing phi-nodes. 14461c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (IncValue == PN) continue; 14471c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaInst *IncValueAI = findAllocaForValue(IncValue); 14481c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov // AI for incoming values should exist and should all be equal. 14491c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (IncValueAI == 0 || (Res != 0 && IncValueAI != Res)) 14501c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov return 0; 14511c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov Res = IncValueAI; 145259cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 145359cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov } 14541c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov if (Res != 0) 14551c8b825c4396ad9cd38f713d9e9a51adae1d4c4eAlexey Samsonov AllocaForValue[V] = Res; 145659cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov return Res; 145759cca13a80ed78ccbcdaa9bf20381bd48243f2f1Alexey Samsonov} 1458