blob: f095cff33c9b5f9f26fd3b89cafb8720333d675f [file] [log] [blame]
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001//===-- AddressSanitizer.cpp - memory error detector ------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11// Details of the algorithm:
12// http://code.google.com/p/address-sanitizer/wiki/AddressSanitizerAlgorithm
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "asan"
17
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/Transforms/Instrumentation.h"
Kostya Serebryanyb5b86d22012-08-24 16:44:47 +000019#include "BlackList.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000020#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/OwningPtr.h"
22#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/SmallString.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringExtras.h"
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +000026#include "llvm/ADT/Triple.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/DataLayout.h"
28#include "llvm/Function.h"
29#include "llvm/IRBuilder.h"
30#include "llvm/InlineAsm.h"
31#include "llvm/IntrinsicInst.h"
32#include "llvm/LLVMContext.h"
33#include "llvm/Module.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/DataTypes.h"
36#include "llvm/Support/Debug.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000037#include "llvm/Support/raw_ostream.h"
38#include "llvm/Support/system_error.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000039#include "llvm/Target/TargetMachine.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000040#include "llvm/Transforms/Utils/BasicBlockUtils.h"
41#include "llvm/Transforms/Utils/ModuleUtils.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000042#include "llvm/Type.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000043#include <algorithm>
Chandler Carruthd04a8d42012-12-03 16:50:05 +000044#include <string>
Kostya Serebryany800e03f2011-11-16 01:35:23 +000045
46using namespace llvm;
47
48static const uint64_t kDefaultShadowScale = 3;
49static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
50static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +000051static const uint64_t kDefaultShadowOffsetAndroid = 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +000052
53static const size_t kMaxStackMallocSize = 1 << 16; // 64K
54static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
55static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
56
57static const char *kAsanModuleCtorName = "asan.module_ctor";
Kostya Serebryany7bcfc992011-12-15 21:59:03 +000058static const char *kAsanModuleDtorName = "asan.module_dtor";
59static const int kAsanCtorAndCtorPriority = 1;
Kostya Serebryany800e03f2011-11-16 01:35:23 +000060static const char *kAsanReportErrorTemplate = "__asan_report_";
61static const char *kAsanRegisterGlobalsName = "__asan_register_globals";
Kostya Serebryany7bcfc992011-12-15 21:59:03 +000062static const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +000063static const char *kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
64static const char *kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
Kostya Serebryany800e03f2011-11-16 01:35:23 +000065static const char *kAsanInitName = "__asan_init";
Kostya Serebryany95e3cf42012-02-08 21:36:17 +000066static const char *kAsanHandleNoReturnName = "__asan_handle_no_return";
Kostya Serebryany800e03f2011-11-16 01:35:23 +000067static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
68static const char *kAsanMappingScaleName = "__asan_mapping_scale";
69static const char *kAsanStackMallocName = "__asan_stack_malloc";
70static const char *kAsanStackFreeName = "__asan_stack_free";
Kostya Serebryany51c7c652012-11-20 14:16:08 +000071static const char *kAsanGenPrefix = "__asan_gen_";
Alexey Samsonovf985f442012-12-04 01:34:23 +000072static const char *kAsanPoisonStackMemoryName = "__asan_poison_stack_memory";
73static const char *kAsanUnpoisonStackMemoryName =
74 "__asan_unpoison_stack_memory";
Kostya Serebryany800e03f2011-11-16 01:35:23 +000075
76static const int kAsanStackLeftRedzoneMagic = 0xf1;
77static const int kAsanStackMidRedzoneMagic = 0xf2;
78static const int kAsanStackRightRedzoneMagic = 0xf3;
79static const int kAsanStackPartialRedzoneMagic = 0xf4;
80
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +000081// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
82static const size_t kNumberOfAccessSizes = 5;
83
Kostya Serebryany800e03f2011-11-16 01:35:23 +000084// Command-line flags.
85
86// This flag may need to be replaced with -f[no-]asan-reads.
87static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
88 cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
89static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
90 cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +000091static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
92 cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
93 cl::Hidden, cl::init(true));
Kostya Serebryany6e2d5062012-08-15 08:58:58 +000094static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path",
95 cl::desc("use instrumentation with slow path for all accesses"),
96 cl::Hidden, cl::init(false));
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +000097// This flag limits the number of instructions to be instrumented
Kostya Serebryany324cbb82012-06-28 09:34:41 +000098// in any given BB. Normally, this should be set to unlimited (INT_MAX),
99// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
100// set it to 10000.
101static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
102 cl::init(10000),
103 cl::desc("maximal number of instructions to instrument in any given BB"),
104 cl::Hidden);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000105// This flag may need to be replaced with -f[no]asan-stack.
106static cl::opt<bool> ClStack("asan-stack",
107 cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
108// This flag may need to be replaced with -f[no]asan-use-after-return.
109static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
110 cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
111// This flag may need to be replaced with -f[no]asan-globals.
112static cl::opt<bool> ClGlobals("asan-globals",
113 cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000114static cl::opt<bool> ClInitializers("asan-initialization-order",
115 cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000116static cl::opt<bool> ClMemIntrin("asan-memintrin",
117 cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
Kostya Serebryany6c554122012-12-04 06:14:01 +0000118static cl::opt<bool> ClRealignStack("asan-realign-stack",
119 cl::desc("Realign stack to 32"), cl::Hidden, cl::init(true));
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000120static cl::opt<std::string> ClBlacklistFile("asan-blacklist",
121 cl::desc("File containing the list of objects to ignore "
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000122 "during instrumentation"), cl::Hidden);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000123
124// These flags allow to change the shadow mapping.
125// The shadow mapping looks like
126// Shadow = (Mem >> scale) + (1 << offset_log)
127static cl::opt<int> ClMappingScale("asan-mapping-scale",
128 cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
129static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
130 cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
131
132// Optimization flags. Not user visible, used mostly for testing
133// and benchmarking the tool.
134static cl::opt<bool> ClOpt("asan-opt",
135 cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
136static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
137 cl::desc("Instrument the same temp just once"), cl::Hidden,
138 cl::init(true));
139static cl::opt<bool> ClOptGlobals("asan-opt-globals",
140 cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
141
Alexey Samsonovee548272012-11-29 18:14:24 +0000142static cl::opt<bool> ClCheckLifetime("asan-check-lifetime",
143 cl::desc("Use llvm.lifetime intrinsics to insert extra checks"),
144 cl::Hidden, cl::init(false));
145
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000146// Debug flags.
147static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
148 cl::init(0));
149static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
150 cl::Hidden, cl::init(0));
151static cl::opt<std::string> ClDebugFunc("asan-debug-func",
152 cl::Hidden, cl::desc("Debug func"));
153static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
154 cl::Hidden, cl::init(-1));
155static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
156 cl::Hidden, cl::init(-1));
157
158namespace {
Kostya Serebryanyca23d432012-11-20 13:00:01 +0000159/// A set of dynamically initialized globals extracted from metadata.
160class SetOfDynamicallyInitializedGlobals {
161 public:
162 void Init(Module& M) {
163 // Clang generates metadata identifying all dynamically initialized globals.
164 NamedMDNode *DynamicGlobals =
165 M.getNamedMetadata("llvm.asan.dynamically_initialized_globals");
166 if (!DynamicGlobals)
167 return;
168 for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) {
169 MDNode *MDN = DynamicGlobals->getOperand(i);
170 assert(MDN->getNumOperands() == 1);
171 Value *VG = MDN->getOperand(0);
172 // The optimizer may optimize away a global entirely, in which case we
173 // cannot instrument access to it.
174 if (!VG)
175 continue;
176 DynInitGlobals.insert(cast<GlobalVariable>(VG));
177 }
178 }
179 bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; }
180 private:
181 SmallSet<GlobalValue*, 32> DynInitGlobals;
182};
183
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000184static int MappingScale() {
185 return ClMappingScale ? ClMappingScale : kDefaultShadowScale;
186}
187
188static size_t RedzoneSize() {
189 // Redzone used for stack and globals is at least 32 bytes.
190 // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
191 return std::max(32U, 1U << MappingScale());
192}
Kostya Serebryanyca23d432012-11-20 13:00:01 +0000193
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000194/// AddressSanitizer: instrument the code in module to find memory bugs.
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000195struct AddressSanitizer : public FunctionPass {
Alexey Samsonovee548272012-11-29 18:14:24 +0000196 AddressSanitizer(bool CheckInitOrder = false,
197 bool CheckUseAfterReturn = false,
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000198 bool CheckLifetime = false,
199 StringRef BlacklistFile = StringRef())
Alexey Samsonovee548272012-11-29 18:14:24 +0000200 : FunctionPass(ID),
201 CheckInitOrder(CheckInitOrder || ClInitializers),
202 CheckUseAfterReturn(CheckUseAfterReturn || ClUseAfterReturn),
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000203 CheckLifetime(CheckLifetime || ClCheckLifetime),
204 BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
205 : BlacklistFile) {}
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000206 virtual const char *getPassName() const {
207 return "AddressSanitizerFunctionPass";
208 }
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000209 void instrumentMop(Instruction *I);
210 void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000211 Value *Addr, uint32_t TypeSize, bool IsWrite);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000212 Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
213 Value *ShadowValue, uint32_t TypeSize);
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000214 Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000215 bool IsWrite, size_t AccessSizeIndex);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000216 bool instrumentMemIntrinsic(MemIntrinsic *MI);
217 void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000218 Value *Size,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000219 Instruction *InsertBefore, bool IsWrite);
220 Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000221 bool runOnFunction(Function &F);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000222 void createInitializerPoisonCalls(Module &M,
223 Value *FirstAddr, Value *LastAddr);
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000224 bool maybeInsertAsanInitAtFunctionEntry(Function &F);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000225 bool poisonStackInFunction(Function &F);
226 virtual bool doInitialization(Module &M);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000227 static char ID; // Pass identification, replacement for typeid
228
229 private:
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000230 void initializeCallbacks(Module &M);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000231 uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
232 Type *Ty = AI->getAllocatedType();
Evgeniy Stepanovd8313be2012-03-02 10:41:08 +0000233 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000234 return SizeInBytes;
235 }
236 uint64_t getAlignedSize(uint64_t SizeInBytes) {
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000237 size_t RZ = RedzoneSize();
238 return ((SizeInBytes + RZ - 1) / RZ) * RZ;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000239 }
240 uint64_t getAlignedAllocaSize(AllocaInst *AI) {
241 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
242 return getAlignedSize(SizeInBytes);
243 }
244
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000245 bool ShouldInstrumentGlobal(GlobalVariable *G);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000246 void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
247 Value *ShadowBase, bool DoPoison);
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +0000248 bool LooksLikeCodeInBug11395(Instruction *I);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000249 void FindDynamicInitializers(Module &M);
Alexey Samsonovf985f442012-12-04 01:34:23 +0000250 /// Analyze lifetime intrinsics for given alloca. Use Value* instead of
251 /// AllocaInst* here, as we call this method after we merge all allocas into a
252 /// single one. Returns true if ASan added some instrumentation.
253 bool handleAllocaLifetime(Value *Alloca);
254 /// Analyze lifetime intrinsics for a specific value, casted from alloca.
255 /// Returns true if if ASan added some instrumentation.
256 bool handleValueLifetime(Value *V);
257 void poisonAlloca(Value *V, uint64_t Size, IRBuilder<> IRB, bool DoPoison);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000258
Alexey Samsonovee548272012-11-29 18:14:24 +0000259 bool CheckInitOrder;
260 bool CheckUseAfterReturn;
261 bool CheckLifetime;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000262 LLVMContext *C;
Micah Villmow3574eca2012-10-08 16:38:25 +0000263 DataLayout *TD;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000264 uint64_t MappingOffset;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000265 int LongSize;
266 Type *IntptrTy;
267 Type *IntptrPtrTy;
268 Function *AsanCtorFunction;
269 Function *AsanInitFunction;
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000270 Function *AsanStackMallocFunc, *AsanStackFreeFunc;
Alexey Samsonovf985f442012-12-04 01:34:23 +0000271 Function *AsanPoisonStackMemoryFunc, *AsanUnpoisonStackMemoryFunc;
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000272 Function *AsanHandleNoReturnFunc;
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000273 SmallString<64> BlacklistFile;
Kostya Serebryanyb5b86d22012-08-24 16:44:47 +0000274 OwningPtr<BlackList> BL;
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000275 // This array is indexed by AccessIsWrite and log2(AccessSize).
276 Function *AsanErrorCallback[2][kNumberOfAccessSizes];
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000277 InlineAsm *EmptyAsm;
Kostya Serebryanyca23d432012-11-20 13:00:01 +0000278 SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000279};
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000280
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000281class AddressSanitizerModule : public ModulePass {
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000282 public:
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000283 AddressSanitizerModule(bool CheckInitOrder = false,
284 StringRef BlacklistFile = StringRef())
Alexey Samsonovee548272012-11-29 18:14:24 +0000285 : ModulePass(ID),
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000286 CheckInitOrder(CheckInitOrder || ClInitializers),
287 BlacklistFile(BlacklistFile.empty() ? ClBlacklistFile
288 : BlacklistFile) {}
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000289 bool runOnModule(Module &M);
290 static char ID; // Pass identification, replacement for typeid
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000291 virtual const char *getPassName() const {
292 return "AddressSanitizerModule";
293 }
Alexey Samsonovf985f442012-12-04 01:34:23 +0000294
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000295 private:
296 bool ShouldInstrumentGlobal(GlobalVariable *G);
297 void createInitializerPoisonCalls(Module &M, Value *FirstAddr,
298 Value *LastAddr);
299
Alexey Samsonovee548272012-11-29 18:14:24 +0000300 bool CheckInitOrder;
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000301 SmallString<64> BlacklistFile;
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000302 OwningPtr<BlackList> BL;
303 SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
304 Type *IntptrTy;
305 LLVMContext *C;
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000306 DataLayout *TD;
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000307};
308
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000309} // namespace
310
311char AddressSanitizer::ID = 0;
312INITIALIZE_PASS(AddressSanitizer, "asan",
313 "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
314 false, false)
Alexey Samsonovee548272012-11-29 18:14:24 +0000315FunctionPass *llvm::createAddressSanitizerFunctionPass(
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000316 bool CheckInitOrder, bool CheckUseAfterReturn, bool CheckLifetime,
317 StringRef BlacklistFile) {
Alexey Samsonovee548272012-11-29 18:14:24 +0000318 return new AddressSanitizer(CheckInitOrder, CheckUseAfterReturn,
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000319 CheckLifetime, BlacklistFile);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000320}
321
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000322char AddressSanitizerModule::ID = 0;
323INITIALIZE_PASS(AddressSanitizerModule, "asan-module",
324 "AddressSanitizer: detects use-after-free and out-of-bounds bugs."
325 "ModulePass", false, false)
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000326ModulePass *llvm::createAddressSanitizerModulePass(
327 bool CheckInitOrder, StringRef BlacklistFile) {
328 return new AddressSanitizerModule(CheckInitOrder, BlacklistFile);
Alexander Potapenko25878042012-01-23 11:22:43 +0000329}
330
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000331static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
332 size_t Res = CountTrailingZeros_32(TypeSize / 8);
333 assert(Res < kNumberOfAccessSizes);
334 return Res;
335}
336
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000337// Create a constant for Str so that we can pass it to the run-time lib.
338static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
Chris Lattner18c7f802012-02-05 02:29:43 +0000339 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000340 return new GlobalVariable(M, StrConst->getType(), true,
Kostya Serebryany51c7c652012-11-20 14:16:08 +0000341 GlobalValue::PrivateLinkage, StrConst,
342 kAsanGenPrefix);
343}
344
345static bool GlobalWasGeneratedByAsan(GlobalVariable *G) {
346 return G->getName().find(kAsanGenPrefix) == 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000347}
348
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000349Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
350 // Shadow >> scale
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000351 Shadow = IRB.CreateLShr(Shadow, MappingScale());
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000352 if (MappingOffset == 0)
353 return Shadow;
354 // (Shadow >> scale) | offset
355 return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
356 MappingOffset));
357}
358
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000359void AddressSanitizer::instrumentMemIntrinsicParam(
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000360 Instruction *OrigIns,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000361 Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
362 // Check the first byte.
363 {
364 IRBuilder<> IRB(InsertBefore);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000365 instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000366 }
367 // Check the last byte.
368 {
369 IRBuilder<> IRB(InsertBefore);
370 Value *SizeMinusOne = IRB.CreateSub(
371 Size, ConstantInt::get(Size->getType(), 1));
372 SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
373 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
374 Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000375 instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000376 }
377}
378
379// Instrument memset/memmove/memcpy
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000380bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000381 Value *Dst = MI->getDest();
382 MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000383 Value *Src = MemTran ? MemTran->getSource() : 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000384 Value *Length = MI->getLength();
385
386 Constant *ConstLength = dyn_cast<Constant>(Length);
387 Instruction *InsertBefore = MI;
388 if (ConstLength) {
389 if (ConstLength->isNullValue()) return false;
390 } else {
391 // The size is not a constant so it could be zero -- check at run-time.
392 IRBuilder<> IRB(InsertBefore);
393
394 Value *Cmp = IRB.CreateICmpNE(Length,
Kostya Serebryany56139bc2012-07-02 11:42:29 +0000395 Constant::getNullValue(Length->getType()));
Evgeniy Stepanov4a2dec02012-10-19 10:48:31 +0000396 InsertBefore = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000397 }
398
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000399 instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000400 if (Src)
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000401 instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000402 return true;
403}
404
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000405// If I is an interesting memory access, return the PointerOperand
406// and set IsWrite. Otherwise return NULL.
407static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000408 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000409 if (!ClInstrumentReads) return NULL;
410 *IsWrite = false;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000411 return LI->getPointerOperand();
412 }
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000413 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
414 if (!ClInstrumentWrites) return NULL;
415 *IsWrite = true;
416 return SI->getPointerOperand();
417 }
418 if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
419 if (!ClInstrumentAtomics) return NULL;
420 *IsWrite = true;
421 return RMW->getPointerOperand();
422 }
423 if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
424 if (!ClInstrumentAtomics) return NULL;
425 *IsWrite = true;
426 return XCHG->getPointerOperand();
427 }
428 return NULL;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000429}
430
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000431void AddressSanitizer::instrumentMop(Instruction *I) {
Axel Naumann3780ad82012-09-17 14:20:57 +0000432 bool IsWrite = false;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000433 Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
434 assert(Addr);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000435 if (ClOpt && ClOptGlobals) {
436 if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) {
437 // If initialization order checking is disabled, a simple access to a
438 // dynamically initialized global is always valid.
Alexey Samsonovee548272012-11-29 18:14:24 +0000439 if (!CheckInitOrder)
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000440 return;
441 // If a global variable does not have dynamic initialization we don't
Kostya Serebryany40779062012-11-20 13:11:32 +0000442 // have to instrument it. However, if a global does not have initailizer
443 // at all, we assume it has dynamic initializer (in other TU).
444 if (G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G))
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000445 return;
446 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000447 }
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000448
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000449 Type *OrigPtrTy = Addr->getType();
450 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
451
452 assert(OrigTy->isSized());
453 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
454
455 if (TypeSize != 8 && TypeSize != 16 &&
456 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
457 // Ignore all unusual sizes.
458 return;
459 }
460
461 IRBuilder<> IRB(I);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000462 instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000463}
464
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000465// Validate the result of Module::getOrInsertFunction called for an interface
466// function of AddressSanitizer. If the instrumented module defines a function
467// with the same name, their prototypes must match, otherwise
468// getOrInsertFunction returns a bitcast.
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000469static Function *checkInterfaceFunction(Constant *FuncOrBitcast) {
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000470 if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
471 FuncOrBitcast->dump();
472 report_fatal_error("trying to redefine an AddressSanitizer "
473 "interface function");
474}
475
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000476Instruction *AddressSanitizer::generateCrashCode(
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000477 Instruction *InsertBefore, Value *Addr,
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000478 bool IsWrite, size_t AccessSizeIndex) {
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000479 IRBuilder<> IRB(InsertBefore);
480 CallInst *Call = IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex],
481 Addr);
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000482 // We don't do Call->setDoesNotReturn() because the BB already has
483 // UnreachableInst at the end.
484 // This EmptyAsm is required to avoid callback merge.
485 IRB.CreateCall(EmptyAsm);
Kostya Serebryany3c7faae2012-01-06 18:09:21 +0000486 return Call;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000487}
488
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000489Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000490 Value *ShadowValue,
491 uint32_t TypeSize) {
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000492 size_t Granularity = 1 << MappingScale();
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000493 // Addr & (Granularity - 1)
494 Value *LastAccessedByte = IRB.CreateAnd(
495 AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
496 // (Addr & (Granularity - 1)) + size - 1
497 if (TypeSize / 8 > 1)
498 LastAccessedByte = IRB.CreateAdd(
499 LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
500 // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
501 LastAccessedByte = IRB.CreateIntCast(
Kostya Serebryany6e2d5062012-08-15 08:58:58 +0000502 LastAccessedByte, ShadowValue->getType(), false);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000503 // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
504 return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
505}
506
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000507void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000508 IRBuilder<> &IRB, Value *Addr,
509 uint32_t TypeSize, bool IsWrite) {
510 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
511
512 Type *ShadowTy = IntegerType::get(
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000513 *C, std::max(8U, TypeSize >> MappingScale()));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000514 Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
515 Value *ShadowPtr = memToShadow(AddrLong, IRB);
516 Value *CmpVal = Constant::getNullValue(ShadowTy);
517 Value *ShadowValue = IRB.CreateLoad(
518 IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
519
520 Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
Kostya Serebryany11c2a472012-08-13 14:08:46 +0000521 size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000522 size_t Granularity = 1 << MappingScale();
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000523 TerminatorInst *CrashTerm = 0;
524
Kostya Serebryany6e2d5062012-08-15 08:58:58 +0000525 if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
Evgeniy Stepanov4a2dec02012-10-19 10:48:31 +0000526 TerminatorInst *CheckTerm =
527 SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000528 assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000529 BasicBlock *NextBB = CheckTerm->getSuccessor(0);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000530 IRB.SetInsertPoint(CheckTerm);
531 Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000532 BasicBlock *CrashBlock =
533 BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000534 CrashTerm = new UnreachableInst(*C, CrashBlock);
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000535 BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
536 ReplaceInstWithInst(CheckTerm, NewTerm);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000537 } else {
Evgeniy Stepanov4a2dec02012-10-19 10:48:31 +0000538 CrashTerm = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), true);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000539 }
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000540
541 Instruction *Crash =
542 generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex);
543 Crash->setDebugLoc(OrigIns->getDebugLoc());
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000544}
545
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000546void AddressSanitizerModule::createInitializerPoisonCalls(
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000547 Module &M, Value *FirstAddr, Value *LastAddr) {
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000548 // We do all of our poisoning and unpoisoning within _GLOBAL__I_a.
549 Function *GlobalInit = M.getFunction("_GLOBAL__I_a");
550 // If that function is not present, this TU contains no globals, or they have
551 // all been optimized away
552 if (!GlobalInit)
553 return;
554
555 // Set up the arguments to our poison/unpoison functions.
556 IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt());
557
558 // Declare our poisoning and unpoisoning functions.
559 Function *AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
560 kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
561 AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
562 Function *AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
563 kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL));
564 AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
565
566 // Add a call to poison all external globals before the given function starts.
567 IRB.CreateCall2(AsanPoisonGlobals, FirstAddr, LastAddr);
568
569 // Add calls to unpoison all globals before each return instruction.
570 for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end();
571 I != E; ++I) {
572 if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) {
573 CallInst::Create(AsanUnpoisonGlobals, "", RI);
574 }
575 }
576}
577
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000578bool AddressSanitizerModule::ShouldInstrumentGlobal(GlobalVariable *G) {
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000579 Type *Ty = cast<PointerType>(G->getType())->getElementType();
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000580 DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000581
Kostya Serebryany59a4a472012-09-05 07:29:56 +0000582 if (BL->isIn(*G)) return false;
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000583 if (!Ty->isSized()) return false;
584 if (!G->hasInitializer()) return false;
Kostya Serebryany51c7c652012-11-20 14:16:08 +0000585 if (GlobalWasGeneratedByAsan(G)) return false; // Our own global.
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000586 // Touch only those globals that will not be defined in other modules.
587 // Don't handle ODR type linkages since other modules may be built w/o asan.
588 if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
589 G->getLinkage() != GlobalVariable::PrivateLinkage &&
590 G->getLinkage() != GlobalVariable::InternalLinkage)
591 return false;
592 // Two problems with thread-locals:
593 // - The address of the main thread's copy can't be computed at link-time.
594 // - Need to poison all copies, not just the main thread's one.
595 if (G->isThreadLocal())
596 return false;
597 // For now, just ignore this Alloca if the alignment is large.
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000598 if (G->getAlignment() > RedzoneSize()) return false;
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000599
600 // Ignore all the globals with the names starting with "\01L_OBJC_".
601 // Many of those are put into the .cstring section. The linker compresses
602 // that section by removing the spare \0s after the string terminator, so
603 // our redzones get broken.
604 if ((G->getName().find("\01L_OBJC_") == 0) ||
605 (G->getName().find("\01l_OBJC_") == 0)) {
606 DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
607 return false;
608 }
609
610 if (G->hasSection()) {
611 StringRef Section(G->getSection());
612 // Ignore the globals from the __OBJC section. The ObjC runtime assumes
613 // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
614 // them.
615 if ((Section.find("__OBJC,") == 0) ||
616 (Section.find("__DATA, __objc_") == 0)) {
617 DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
618 return false;
619 }
620 // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
621 // Constant CFString instances are compiled in the following way:
622 // -- the string buffer is emitted into
623 // __TEXT,__cstring,cstring_literals
624 // -- the constant NSConstantString structure referencing that buffer
625 // is placed into __DATA,__cfstring
626 // Therefore there's no point in placing redzones into __DATA,__cfstring.
627 // Moreover, it causes the linker to crash on OS X 10.7
628 if (Section.find("__DATA,__cfstring") == 0) {
629 DEBUG(dbgs() << "Ignoring CFString: " << *G);
630 return false;
631 }
632 }
633
634 return true;
635}
636
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000637// This function replaces all global variables with new variables that have
638// trailing redzones. It also creates a function that poisons
639// redzones and inserts this function into llvm.global_ctors.
Kostya Serebryany1416edc2012-11-28 10:31:36 +0000640bool AddressSanitizerModule::runOnModule(Module &M) {
641 if (!ClGlobals) return false;
642 TD = getAnalysisIfAvailable<DataLayout>();
643 if (!TD)
644 return false;
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000645 BL.reset(new BlackList(BlacklistFile));
Alexey Samsonovd6f62c82012-11-29 18:27:01 +0000646 if (BL->isIn(M)) return false;
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000647 DynamicallyInitializedGlobals.Init(M);
648 C = &(M.getContext());
649 IntptrTy = Type::getIntNTy(*C, TD->getPointerSizeInBits());
650
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000651 SmallVector<GlobalVariable *, 16> GlobalsToChange;
652
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000653 for (Module::GlobalListType::iterator G = M.global_begin(),
654 E = M.global_end(); G != E; ++G) {
655 if (ShouldInstrumentGlobal(G))
656 GlobalsToChange.push_back(G);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000657 }
658
659 size_t n = GlobalsToChange.size();
660 if (n == 0) return false;
661
662 // A global is described by a structure
663 // size_t beg;
664 // size_t size;
665 // size_t size_with_redzone;
666 // const char *name;
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000667 // size_t has_dynamic_init;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000668 // We initialize an array of such structures and pass it to a run-time call.
669 StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000670 IntptrTy, IntptrTy,
671 IntptrTy, NULL);
672 SmallVector<Constant *, 16> Initializers(n), DynamicInit;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000673
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000674
675 Function *CtorFunc = M.getFunction(kAsanModuleCtorName);
676 assert(CtorFunc);
677 IRBuilder<> IRB(CtorFunc->getEntryBlock().getTerminator());
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000678
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000679 // The addresses of the first and last dynamically initialized globals in
680 // this TU. Used in initialization order checking.
681 Value *FirstDynamic = 0, *LastDynamic = 0;
682
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000683 for (size_t i = 0; i < n; i++) {
684 GlobalVariable *G = GlobalsToChange[i];
685 PointerType *PtrTy = cast<PointerType>(G->getType());
686 Type *Ty = PtrTy->getElementType();
Kostya Serebryany208a4ff2012-03-21 15:28:50 +0000687 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000688 size_t RZ = RedzoneSize();
689 uint64_t RightRedzoneSize = RZ + (RZ - (SizeInBytes % RZ));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000690 Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000691 // Determine whether this global should be poisoned in initialization.
Kostya Serebryanyca23d432012-11-20 13:00:01 +0000692 bool GlobalHasDynamicInitializer =
693 DynamicallyInitializedGlobals.Contains(G);
Kostya Serebryany59a4a472012-09-05 07:29:56 +0000694 // Don't check initialization order if this global is blacklisted.
Kostya Serebryany7dadac62012-09-05 09:00:18 +0000695 GlobalHasDynamicInitializer &= !BL->isInInit(*G);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000696
697 StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
698 Constant *NewInitializer = ConstantStruct::get(
699 NewTy, G->getInitializer(),
700 Constant::getNullValue(RightRedZoneTy), NULL);
701
Kostya Serebryanya4b2b1d2011-12-15 22:55:55 +0000702 SmallString<2048> DescriptionOfGlobal = G->getName();
703 DescriptionOfGlobal += " (";
704 DescriptionOfGlobal += M.getModuleIdentifier();
705 DescriptionOfGlobal += ")";
706 GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000707
708 // Create a new global variable with enough space for a redzone.
709 GlobalVariable *NewGlobal = new GlobalVariable(
710 M, NewTy, G->isConstant(), G->getLinkage(),
Hans Wennborgce718ff2012-06-23 11:37:03 +0000711 NewInitializer, "", G, G->getThreadLocalMode());
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000712 NewGlobal->copyAttributesFrom(G);
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000713 NewGlobal->setAlignment(RZ);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000714
715 Value *Indices2[2];
716 Indices2[0] = IRB.getInt32(0);
717 Indices2[1] = IRB.getInt32(0);
718
719 G->replaceAllUsesWith(
Kostya Serebryanyf1639ab2012-01-28 04:27:16 +0000720 ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000721 NewGlobal->takeName(G);
722 G->eraseFromParent();
723
724 Initializers[i] = ConstantStruct::get(
725 GlobalStructTy,
726 ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
727 ConstantInt::get(IntptrTy, SizeInBytes),
728 ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
729 ConstantExpr::getPointerCast(Name, IntptrTy),
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000730 ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer),
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000731 NULL);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000732
733 // Populate the first and last globals declared in this TU.
Alexey Samsonovee548272012-11-29 18:14:24 +0000734 if (CheckInitOrder && GlobalHasDynamicInitializer) {
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000735 LastDynamic = ConstantExpr::getPointerCast(NewGlobal, IntptrTy);
736 if (FirstDynamic == 0)
737 FirstDynamic = LastDynamic;
738 }
739
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000740 DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000741 }
742
743 ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
744 GlobalVariable *AllGlobals = new GlobalVariable(
745 M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
746 ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
747
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000748 // Create calls for poisoning before initializers run and unpoisoning after.
Alexey Samsonovee548272012-11-29 18:14:24 +0000749 if (CheckInitOrder && FirstDynamic && LastDynamic)
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000750 createInitializerPoisonCalls(M, FirstDynamic, LastDynamic);
751
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000752 Function *AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000753 kAsanRegisterGlobalsName, IRB.getVoidTy(),
754 IntptrTy, IntptrTy, NULL));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000755 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
756
757 IRB.CreateCall2(AsanRegisterGlobals,
758 IRB.CreatePointerCast(AllGlobals, IntptrTy),
759 ConstantInt::get(IntptrTy, n));
760
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000761 // We also need to unregister globals at the end, e.g. when a shared library
762 // gets closed.
763 Function *AsanDtorFunction = Function::Create(
764 FunctionType::get(Type::getVoidTy(*C), false),
765 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
766 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
767 IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000768 Function *AsanUnregisterGlobals =
769 checkInterfaceFunction(M.getOrInsertFunction(
770 kAsanUnregisterGlobalsName,
771 IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000772 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
773
774 IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
775 IRB.CreatePointerCast(AllGlobals, IntptrTy),
776 ConstantInt::get(IntptrTy, n));
777 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
778
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000779 DEBUG(dbgs() << M);
780 return true;
781}
782
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000783void AddressSanitizer::initializeCallbacks(Module &M) {
784 IRBuilder<> IRB(*C);
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000785 // Create __asan_report* callbacks.
786 for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
787 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
788 AccessSizeIndex++) {
789 // IsWrite and TypeSize are encoded in the function name.
790 std::string FunctionName = std::string(kAsanReportErrorTemplate) +
791 (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000792 // If we are merging crash callbacks, they have two parameters.
Kostya Serebryany7846c1c2012-11-07 12:42:18 +0000793 AsanErrorCallback[AccessIsWrite][AccessSizeIndex] =
794 checkInterfaceFunction(M.getOrInsertFunction(
795 FunctionName, IRB.getVoidTy(), IntptrTy, NULL));
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000796 }
797 }
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000798
799 AsanStackMallocFunc = checkInterfaceFunction(M.getOrInsertFunction(
800 kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL));
801 AsanStackFreeFunc = checkInterfaceFunction(M.getOrInsertFunction(
802 kAsanStackFreeName, IRB.getVoidTy(),
803 IntptrTy, IntptrTy, IntptrTy, NULL));
804 AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction(
805 kAsanHandleNoReturnName, IRB.getVoidTy(), NULL));
Alexey Samsonovf985f442012-12-04 01:34:23 +0000806 AsanPoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
807 kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
808 AsanUnpoisonStackMemoryFunc = checkInterfaceFunction(M.getOrInsertFunction(
809 kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000810
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000811 // We insert an empty inline asm after __asan_report* to avoid callback merge.
812 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
813 StringRef(""), StringRef(""),
814 /*hasSideEffects=*/true);
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000815}
816
817// virtual
818bool AddressSanitizer::doInitialization(Module &M) {
819 // Initialize the private fields. No one has accessed them before.
820 TD = getAnalysisIfAvailable<DataLayout>();
821
822 if (!TD)
823 return false;
Alexey Samsonovb0dcf612012-12-03 19:09:26 +0000824 BL.reset(new BlackList(BlacklistFile));
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000825 DynamicallyInitializedGlobals.Init(M);
826
827 C = &(M.getContext());
828 LongSize = TD->getPointerSizeInBits();
829 IntptrTy = Type::getIntNTy(*C, LongSize);
830 IntptrPtrTy = PointerType::get(IntptrTy, 0);
831
832 AsanCtorFunction = Function::Create(
833 FunctionType::get(Type::getVoidTy(*C), false),
834 GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
835 BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
836 // call __asan_init in the module ctor.
837 IRBuilder<> IRB(ReturnInst::Create(*C, AsanCtorBB));
838 AsanInitFunction = checkInterfaceFunction(
839 M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
840 AsanInitFunction->setLinkage(Function::ExternalLinkage);
841 IRB.CreateCall(AsanInitFunction);
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000842
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +0000843 llvm::Triple targetTriple(M.getTargetTriple());
Logan Chien43bf7092012-09-02 09:29:46 +0000844 bool isAndroid = targetTriple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +0000845
846 MappingOffset = isAndroid ? kDefaultShadowOffsetAndroid :
847 (LongSize == 32 ? kDefaultShadowOffset32 : kDefaultShadowOffset64);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000848 if (ClMappingOffsetLog >= 0) {
849 if (ClMappingOffsetLog == 0) {
850 // special case
851 MappingOffset = 0;
852 } else {
853 MappingOffset = 1ULL << ClMappingOffsetLog;
854 }
855 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000856
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000857
Kostya Serebryany8c0134a2012-03-19 16:40:35 +0000858 if (ClMappingOffsetLog >= 0) {
859 // Tell the run-time the current values of mapping offset and scale.
860 GlobalValue *asan_mapping_offset =
861 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
862 ConstantInt::get(IntptrTy, MappingOffset),
863 kAsanMappingOffsetName);
864 // Read the global, otherwise it may be optimized away.
865 IRB.CreateLoad(asan_mapping_offset, true);
866 }
867 if (ClMappingScale) {
868 GlobalValue *asan_mapping_scale =
869 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000870 ConstantInt::get(IntptrTy, MappingScale()),
Kostya Serebryany8c0134a2012-03-19 16:40:35 +0000871 kAsanMappingScaleName);
872 // Read the global, otherwise it may be optimized away.
873 IRB.CreateLoad(asan_mapping_scale, true);
874 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000875
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000876 appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
Kostya Serebryany9b027412011-12-12 18:01:46 +0000877
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000878 return true;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000879}
880
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000881bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
882 // For each NSObject descendant having a +load method, this method is invoked
883 // by the ObjC runtime before any of the static constructors is called.
884 // Therefore we need to instrument such methods with a call to __asan_init
885 // at the beginning in order to initialize our runtime before any access to
886 // the shadow memory.
887 // We cannot just ignore these methods, because they may call other
888 // instrumented functions.
889 if (F.getName().find(" load]") != std::string::npos) {
890 IRBuilder<> IRB(F.begin()->begin());
891 IRB.CreateCall(AsanInitFunction);
892 return true;
893 }
894 return false;
895}
896
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000897bool AddressSanitizer::runOnFunction(Function &F) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000898 if (BL->isIn(F)) return false;
899 if (&F == AsanCtorFunction) return false;
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000900 DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
Kostya Serebryany8b390ff2012-11-29 09:54:21 +0000901 initializeCallbacks(*F.getParent());
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000902
903 // If needed, insert __asan_init before checking for AddressSafety attr.
904 maybeInsertAsanInitAtFunctionEntry(F);
905
Bill Wendling67658342012-10-09 07:45:08 +0000906 if (!F.getFnAttributes().hasAttribute(Attributes::AddressSafety))
907 return false;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000908
909 if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
910 return false;
Bill Wendling67658342012-10-09 07:45:08 +0000911
912 // We want to instrument every address only once per basic block (unless there
913 // are calls between uses).
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000914 SmallSet<Value*, 16> TempsToInstrument;
915 SmallVector<Instruction*, 16> ToInstrument;
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000916 SmallVector<Instruction*, 8> NoReturnCalls;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000917 bool IsWrite;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000918
919 // Fill the set of memory operations to instrument.
920 for (Function::iterator FI = F.begin(), FE = F.end();
921 FI != FE; ++FI) {
922 TempsToInstrument.clear();
Kostya Serebryany324cbb82012-06-28 09:34:41 +0000923 int NumInsnsPerBB = 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000924 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
925 BI != BE; ++BI) {
Kostya Serebryanybcb55ce2012-01-11 18:15:23 +0000926 if (LooksLikeCodeInBug11395(BI)) return false;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000927 if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000928 if (ClOpt && ClOptSameTemp) {
929 if (!TempsToInstrument.insert(Addr))
930 continue; // We've seen this temp in the current BB.
931 }
932 } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
933 // ok, take it.
934 } else {
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000935 if (CallInst *CI = dyn_cast<CallInst>(BI)) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000936 // A call inside BB.
937 TempsToInstrument.clear();
Kostya Serebryanya17babb2012-11-30 11:08:59 +0000938 if (CI->doesNotReturn()) {
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000939 NoReturnCalls.push_back(CI);
940 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000941 }
942 continue;
943 }
944 ToInstrument.push_back(BI);
Kostya Serebryany324cbb82012-06-28 09:34:41 +0000945 NumInsnsPerBB++;
946 if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
947 break;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000948 }
949 }
950
951 // Instrument.
952 int NumInstrumented = 0;
953 for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
954 Instruction *Inst = ToInstrument[i];
955 if (ClDebugMin < 0 || ClDebugMax < 0 ||
956 (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000957 if (isInterestingMemoryAccess(Inst, &IsWrite))
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000958 instrumentMop(Inst);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000959 else
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000960 instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000961 }
962 NumInstrumented++;
963 }
964
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000965 bool ChangedStack = poisonStackInFunction(F);
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000966
967 // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
968 // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
969 for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
970 Instruction *CI = NoReturnCalls[i];
971 IRBuilder<> IRB(CI);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000972 IRB.CreateCall(AsanHandleNoReturnFunc);
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000973 }
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000974 DEBUG(dbgs() << "ASAN done instrumenting:\n" << F << "\n");
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000975
976 return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000977}
978
979static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
980 if (ShadowRedzoneSize == 1) return PoisonByte;
981 if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
982 if (ShadowRedzoneSize == 4)
983 return (PoisonByte << 24) + (PoisonByte << 16) +
984 (PoisonByte << 8) + (PoisonByte);
Craig Topper85814382012-02-07 05:05:23 +0000985 llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000986}
987
988static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
989 size_t Size,
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000990 size_t RZSize,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000991 size_t ShadowGranularity,
992 uint8_t Magic) {
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +0000993 for (size_t i = 0; i < RZSize;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000994 i+= ShadowGranularity, Shadow++) {
995 if (i + ShadowGranularity <= Size) {
996 *Shadow = 0; // fully addressable
997 } else if (i >= Size) {
998 *Shadow = Magic; // unaddressable
999 } else {
1000 *Shadow = Size - i; // first Size-i bytes are addressable
1001 }
1002 }
1003}
1004
1005void AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
1006 IRBuilder<> IRB,
1007 Value *ShadowBase, bool DoPoison) {
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001008 size_t ShadowRZSize = RedzoneSize() >> MappingScale();
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001009 assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
1010 Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
1011 Type *RZPtrTy = PointerType::get(RZTy, 0);
1012
1013 Value *PoisonLeft = ConstantInt::get(RZTy,
1014 ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
1015 Value *PoisonMid = ConstantInt::get(RZTy,
1016 ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
1017 Value *PoisonRight = ConstantInt::get(RZTy,
1018 ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
1019
1020 // poison the first red zone.
1021 IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
1022
1023 // poison all other red zones.
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001024 uint64_t Pos = RedzoneSize();
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001025 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1026 AllocaInst *AI = AllocaVec[i];
1027 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1028 uint64_t AlignedSize = getAlignedAllocaSize(AI);
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001029 assert(AlignedSize - SizeInBytes < RedzoneSize());
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001030 Value *Ptr = NULL;
1031
1032 Pos += AlignedSize;
1033
1034 assert(ShadowBase->getType() == IntptrTy);
1035 if (SizeInBytes < AlignedSize) {
1036 // Poison the partial redzone at right
1037 Ptr = IRB.CreateAdd(
1038 ShadowBase, ConstantInt::get(IntptrTy,
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001039 (Pos >> MappingScale()) - ShadowRZSize));
1040 size_t AddressableBytes = RedzoneSize() - (AlignedSize - SizeInBytes);
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001041 uint32_t Poison = 0;
1042 if (DoPoison) {
1043 PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001044 RedzoneSize(),
1045 1ULL << MappingScale(),
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001046 kAsanStackPartialRedzoneMagic);
1047 }
1048 Value *PartialPoison = ConstantInt::get(RZTy, Poison);
1049 IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
1050 }
1051
1052 // Poison the full redzone at right.
1053 Ptr = IRB.CreateAdd(ShadowBase,
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001054 ConstantInt::get(IntptrTy, Pos >> MappingScale()));
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001055 Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
1056 IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
1057
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001058 Pos += RedzoneSize();
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001059 }
1060}
1061
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +00001062// Workaround for bug 11395: we don't want to instrument stack in functions
1063// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
Kostya Serebryanyd2703de2011-11-23 02:10:54 +00001064// FIXME: remove once the bug 11395 is fixed.
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +00001065bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
1066 if (LongSize != 32) return false;
1067 CallInst *CI = dyn_cast<CallInst>(I);
1068 if (!CI || !CI->isInlineAsm()) return false;
1069 if (CI->getNumArgOperands() <= 5) return false;
1070 // We have inline assembly with quite a few arguments.
1071 return true;
1072}
1073
Alexey Samsonovf985f442012-12-04 01:34:23 +00001074// Handling llvm.lifetime intrinsics for a given %alloca:
1075// (1) collect all llvm.lifetime.xxx(%size, %value) describing the alloca.
1076// (2) if %size is constant, poison memory for llvm.lifetime.end (to detect
1077// invalid accesses) and unpoison it for llvm.lifetime.start (the memory
1078// could be poisoned by previous llvm.lifetime.end instruction, as the
1079// variable may go in and out of scope several times, e.g. in loops).
1080// (3) if we poisoned at least one %alloca in a function,
1081// unpoison the whole stack frame at function exit.
1082bool AddressSanitizer::handleAllocaLifetime(Value *Alloca) {
1083 assert(CheckLifetime);
1084 Type *AllocaType = Alloca->getType();
1085 Type *Int8PtrTy = Type::getInt8PtrTy(AllocaType->getContext());
1086
1087 bool Res = false;
1088 // Typical code looks like this:
1089 // %alloca = alloca <type>, <alignment>
1090 // ... some code ...
1091 // %val1 = bitcast <type>* %alloca to i8*
1092 // call void @llvm.lifetime.start(i64 <size>, i8* %val1)
1093 // ... more code ...
1094 // %val2 = bitcast <type>* %alloca to i8*
1095 // call void @llvm.lifetime.start(i64 <size>, i8* %val2)
1096 // That is, to handle %alloca we must find all its casts to
1097 // i8* values, and find lifetime instructions for these values.
1098 if (AllocaType == Int8PtrTy)
1099 Res |= handleValueLifetime(Alloca);
1100 for (Value::use_iterator UI = Alloca->use_begin(), UE = Alloca->use_end();
1101 UI != UE; ++UI) {
1102 if (UI->getType() != Int8PtrTy) continue;
1103 if (UI->stripPointerCasts() != Alloca) continue;
1104 Res |= handleValueLifetime(*UI);
1105 }
1106 return Res;
1107}
1108
1109bool AddressSanitizer::handleValueLifetime(Value *V) {
1110 assert(CheckLifetime);
1111 bool Res = false;
1112 for (Value::use_iterator UI = V->use_begin(), UE = V->use_end(); UI != UE;
1113 ++UI) {
1114 IntrinsicInst *II = dyn_cast<IntrinsicInst>(*UI);
1115 if (!II) continue;
1116 Intrinsic::ID ID = II->getIntrinsicID();
1117 if (ID != Intrinsic::lifetime_start &&
1118 ID != Intrinsic::lifetime_end)
1119 continue;
1120 if (V != II->getArgOperand(1))
1121 continue;
1122 // Found lifetime intrinsic, add ASan instrumentation if necessary.
1123 ConstantInt *Size = dyn_cast<ConstantInt>(II->getArgOperand(0));
1124 // If size argument is undefined, don't do anything.
1125 if (Size->isMinusOne())
1126 continue;
1127 // Check that size doesn't saturate uint64_t and can
1128 // be stored in IntptrTy.
1129 const uint64_t SizeValue = Size->getValue().getLimitedValue();
1130 if (SizeValue == ~0ULL ||
1131 !ConstantInt::isValueValidForType(IntptrTy, SizeValue)) {
1132 continue;
1133 }
1134 IRBuilder<> IRB(II);
1135 bool DoPoison = (ID == Intrinsic::lifetime_end);
1136 poisonAlloca(V, SizeValue, IRB, DoPoison);
1137 Res = true;
1138 }
1139 return Res;
1140}
1141
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001142// Find all static Alloca instructions and put
1143// poisoned red zones around all of them.
1144// Then unpoison everything back before the function returns.
1145//
1146// Stack poisoning does not play well with exception handling.
1147// When an exception is thrown, we essentially bypass the code
1148// that unpoisones the stack. This is why the run-time library has
1149// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
1150// stack in the interceptor. This however does not work inside the
1151// actual function which catches the exception. Most likely because the
1152// compiler hoists the load of the shadow value somewhere too high.
1153// This causes asan to report a non-existing bug on 453.povray.
1154// It sounds like an LLVM bug.
Kostya Serebryanyee4edec2012-10-15 14:20:06 +00001155bool AddressSanitizer::poisonStackInFunction(Function &F) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001156 if (!ClStack) return false;
1157 SmallVector<AllocaInst*, 16> AllocaVec;
1158 SmallVector<Instruction*, 8> RetVec;
1159 uint64_t TotalSize = 0;
Alexey Samsonovf985f442012-12-04 01:34:23 +00001160 bool HavePoisonedAllocas = false;
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001161
1162 // Filter out Alloca instructions we want (and can) handle.
1163 // Collect Ret instructions.
Kostya Serebryany6c554122012-12-04 06:14:01 +00001164 unsigned ResultAlignment = 1 << MappingScale();
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001165 for (Function::iterator FI = F.begin(), FE = F.end();
1166 FI != FE; ++FI) {
1167 BasicBlock &BB = *FI;
1168 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
1169 BI != BE; ++BI) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001170 if (isa<ReturnInst>(BI)) {
1171 RetVec.push_back(BI);
1172 continue;
1173 }
1174
1175 AllocaInst *AI = dyn_cast<AllocaInst>(BI);
1176 if (!AI) continue;
1177 if (AI->isArrayAllocation()) continue;
1178 if (!AI->isStaticAlloca()) continue;
1179 if (!AI->getAllocatedType()->isSized()) continue;
Kostya Serebryany6c554122012-12-04 06:14:01 +00001180 ResultAlignment = std::max(ResultAlignment, AI->getAlignment());
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001181 AllocaVec.push_back(AI);
1182 uint64_t AlignedSize = getAlignedAllocaSize(AI);
1183 TotalSize += AlignedSize;
1184 }
1185 }
1186
1187 if (AllocaVec.empty()) return false;
1188
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001189 uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize();
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001190
Alexey Samsonovee548272012-11-29 18:14:24 +00001191 bool DoStackMalloc = CheckUseAfterReturn
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001192 && LocalStackSize <= kMaxStackMallocSize;
1193
1194 Instruction *InsBefore = AllocaVec[0];
1195 IRBuilder<> IRB(InsBefore);
1196
1197
1198 Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
1199 AllocaInst *MyAlloca =
1200 new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
Kostya Serebryany6c554122012-12-04 06:14:01 +00001201 if (ClRealignStack && ResultAlignment < RedzoneSize())
1202 ResultAlignment = RedzoneSize();
1203 MyAlloca->setAlignment(ResultAlignment);
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001204 assert(MyAlloca->isStaticAlloca());
1205 Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
1206 Value *LocalStackBase = OrigStackBase;
1207
1208 if (DoStackMalloc) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001209 LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
1210 ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
1211 }
1212
1213 // This string will be parsed by the run-time (DescribeStackAddress).
1214 SmallString<2048> StackDescriptionStorage;
1215 raw_svector_ostream StackDescription(StackDescriptionStorage);
1216 StackDescription << F.getName() << " " << AllocaVec.size() << " ";
1217
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001218 uint64_t Pos = RedzoneSize();
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001219 // Replace Alloca instructions with base+offset.
1220 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1221 AllocaInst *AI = AllocaVec[i];
1222 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1223 StringRef Name = AI->getName();
1224 StackDescription << Pos << " " << SizeInBytes << " "
1225 << Name.size() << " " << Name << " ";
1226 uint64_t AlignedSize = getAlignedAllocaSize(AI);
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001227 assert((AlignedSize % RedzoneSize()) == 0);
Alexey Samsonovf985f442012-12-04 01:34:23 +00001228 Value *NewAllocaPtr = IRB.CreateIntToPtr(
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001229 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
Alexey Samsonovf985f442012-12-04 01:34:23 +00001230 AI->getType());
1231 AI->replaceAllUsesWith(NewAllocaPtr);
1232 // Analyze lifetime intrinsics only for static allocas we handle.
1233 if (CheckLifetime)
1234 HavePoisonedAllocas |= handleAllocaLifetime(NewAllocaPtr);
Kostya Serebryanyb9a12ea2012-11-22 03:18:50 +00001235 Pos += AlignedSize + RedzoneSize();
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001236 }
1237 assert(Pos == LocalStackSize);
1238
1239 // Write the Magic value and the frame description constant to the redzone.
1240 Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1241 IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1242 BasePlus0);
1243 Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
1244 ConstantInt::get(IntptrTy, LongSize/8));
1245 BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
Alexey Samsonov9ce84c12012-11-02 12:20:34 +00001246 GlobalVariable *StackDescriptionGlobal =
Kostya Serebryanya5f54f12012-11-01 13:42:40 +00001247 createPrivateGlobalForString(*F.getParent(), StackDescription.str());
Kostya Serebryanya5f54f12012-11-01 13:42:40 +00001248 Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, IntptrTy);
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001249 IRB.CreateStore(Description, BasePlus1);
1250
1251 // Poison the stack redzones at the entry.
1252 Value *ShadowBase = memToShadow(LocalStackBase, IRB);
1253 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
1254
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001255 // Unpoison the stack before all ret instructions.
1256 for (size_t i = 0, n = RetVec.size(); i < n; i++) {
1257 Instruction *Ret = RetVec[i];
1258 IRBuilder<> IRBRet(Ret);
1259
1260 // Mark the current frame as retired.
1261 IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1262 BasePlus0);
1263 // Unpoison the stack.
1264 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
1265
1266 if (DoStackMalloc) {
Alexey Samsonovf985f442012-12-04 01:34:23 +00001267 // In use-after-return mode, mark the whole stack frame unaddressable.
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001268 IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
1269 ConstantInt::get(IntptrTy, LocalStackSize),
1270 OrigStackBase);
Alexey Samsonovf985f442012-12-04 01:34:23 +00001271 } else if (HavePoisonedAllocas) {
1272 // If we poisoned some allocas in llvm.lifetime analysis,
1273 // unpoison whole stack frame now.
1274 assert(LocalStackBase == OrigStackBase);
1275 poisonAlloca(LocalStackBase, LocalStackSize, IRBRet, false);
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001276 }
1277 }
1278
Kostya Serebryanybd0052a2012-10-19 06:20:53 +00001279 // We are done. Remove the old unused alloca instructions.
1280 for (size_t i = 0, n = AllocaVec.size(); i < n; i++)
1281 AllocaVec[i]->eraseFromParent();
1282
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001283 if (ClDebugStack) {
1284 DEBUG(dbgs() << F);
1285 }
1286
1287 return true;
1288}
Alexey Samsonovf985f442012-12-04 01:34:23 +00001289
1290void AddressSanitizer::poisonAlloca(Value *V, uint64_t Size, IRBuilder<> IRB,
1291 bool DoPoison) {
1292 // For now just insert the call to ASan runtime.
1293 Value *AddrArg = IRB.CreatePointerCast(V, IntptrTy);
1294 Value *SizeArg = ConstantInt::get(IntptrTy, Size);
1295 IRB.CreateCall2(DoPoison ? AsanPoisonStackMemoryFunc
1296 : AsanUnpoisonStackMemoryFunc,
1297 AddrArg, SizeArg);
1298}