blob: 336802668ca5d4d42c897a8ba3b4734f7377fc87 [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
Kostya Serebryanya1c45042012-03-14 23:22:10 +000018#include "FunctionBlackList.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000019#include "llvm/Function.h"
20#include "llvm/IRBuilder.h"
Kostya Serebryanyf7b08222012-07-20 09:54:50 +000021#include "llvm/InlineAsm.h"
Chandler Carruth06cb8ed2012-06-29 12:38:19 +000022#include "llvm/IntrinsicInst.h"
23#include "llvm/LLVMContext.h"
24#include "llvm/Module.h"
25#include "llvm/Type.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000026#include "llvm/ADT/ArrayRef.h"
27#include "llvm/ADT/OwningPtr.h"
28#include "llvm/ADT/SmallSet.h"
29#include "llvm/ADT/SmallString.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringExtras.h"
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +000032#include "llvm/ADT/Triple.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000033#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/DataTypes.h"
35#include "llvm/Support/Debug.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000036#include "llvm/Support/raw_ostream.h"
37#include "llvm/Support/system_error.h"
38#include "llvm/Target/TargetData.h"
39#include "llvm/Target/TargetMachine.h"
40#include "llvm/Transforms/Instrumentation.h"
41#include "llvm/Transforms/Utils/BasicBlockUtils.h"
42#include "llvm/Transforms/Utils/ModuleUtils.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000043
44#include <string>
45#include <algorithm>
46
47using namespace llvm;
48
49static const uint64_t kDefaultShadowScale = 3;
50static const uint64_t kDefaultShadowOffset32 = 1ULL << 29;
51static const uint64_t kDefaultShadowOffset64 = 1ULL << 44;
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +000052static const uint64_t kDefaultShadowOffsetAndroid = 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +000053
54static const size_t kMaxStackMallocSize = 1 << 16; // 64K
55static const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3;
56static const uintptr_t kRetiredStackFrameMagic = 0x45E0360E;
57
58static const char *kAsanModuleCtorName = "asan.module_ctor";
Kostya Serebryany7bcfc992011-12-15 21:59:03 +000059static const char *kAsanModuleDtorName = "asan.module_dtor";
60static const int kAsanCtorAndCtorPriority = 1;
Kostya Serebryany800e03f2011-11-16 01:35:23 +000061static const char *kAsanReportErrorTemplate = "__asan_report_";
62static const char *kAsanRegisterGlobalsName = "__asan_register_globals";
Kostya Serebryany7bcfc992011-12-15 21:59:03 +000063static const char *kAsanUnregisterGlobalsName = "__asan_unregister_globals";
Kostya Serebryany800e03f2011-11-16 01:35:23 +000064static const char *kAsanInitName = "__asan_init";
Kostya Serebryany95e3cf42012-02-08 21:36:17 +000065static const char *kAsanHandleNoReturnName = "__asan_handle_no_return";
Kostya Serebryany800e03f2011-11-16 01:35:23 +000066static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
67static const char *kAsanMappingScaleName = "__asan_mapping_scale";
68static const char *kAsanStackMallocName = "__asan_stack_malloc";
69static const char *kAsanStackFreeName = "__asan_stack_free";
70
71static const int kAsanStackLeftRedzoneMagic = 0xf1;
72static const int kAsanStackMidRedzoneMagic = 0xf2;
73static const int kAsanStackRightRedzoneMagic = 0xf3;
74static const int kAsanStackPartialRedzoneMagic = 0xf4;
75
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +000076// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
77static const size_t kNumberOfAccessSizes = 5;
78
Kostya Serebryany800e03f2011-11-16 01:35:23 +000079// Command-line flags.
80
81// This flag may need to be replaced with -f[no-]asan-reads.
82static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
83 cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
84static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
85 cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +000086static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
87 cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
88 cl::Hidden, cl::init(true));
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +000089static cl::opt<bool> ClMergeCallbacks("asan-merge-callbacks",
90 cl::desc("merge __asan_report_ callbacks to create fewer BBs"),
91 cl::Hidden, cl::init(false));
92// This flag limits the number of instructions to be instrumented
Kostya Serebryany324cbb82012-06-28 09:34:41 +000093// in any given BB. Normally, this should be set to unlimited (INT_MAX),
94// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
95// set it to 10000.
96static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
97 cl::init(10000),
98 cl::desc("maximal number of instructions to instrument in any given BB"),
99 cl::Hidden);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000100// This flag may need to be replaced with -f[no]asan-stack.
101static cl::opt<bool> ClStack("asan-stack",
102 cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
103// This flag may need to be replaced with -f[no]asan-use-after-return.
104static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
105 cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
106// This flag may need to be replaced with -f[no]asan-globals.
107static cl::opt<bool> ClGlobals("asan-globals",
108 cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
109static cl::opt<bool> ClMemIntrin("asan-memintrin",
110 cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
111// This flag may need to be replaced with -fasan-blacklist.
112static cl::opt<std::string> ClBlackListFile("asan-blacklist",
113 cl::desc("File containing the list of functions to ignore "
114 "during instrumentation"), cl::Hidden);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000115
116// These flags allow to change the shadow mapping.
117// The shadow mapping looks like
118// Shadow = (Mem >> scale) + (1 << offset_log)
119static cl::opt<int> ClMappingScale("asan-mapping-scale",
120 cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
121static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
122 cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
123
124// Optimization flags. Not user visible, used mostly for testing
125// and benchmarking the tool.
126static cl::opt<bool> ClOpt("asan-opt",
127 cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
128static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
129 cl::desc("Instrument the same temp just once"), cl::Hidden,
130 cl::init(true));
131static cl::opt<bool> ClOptGlobals("asan-opt-globals",
132 cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
133
134// Debug flags.
135static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
136 cl::init(0));
137static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
138 cl::Hidden, cl::init(0));
139static cl::opt<std::string> ClDebugFunc("asan-debug-func",
140 cl::Hidden, cl::desc("Debug func"));
141static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
142 cl::Hidden, cl::init(-1));
143static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
144 cl::Hidden, cl::init(-1));
145
146namespace {
147
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000148/// When the crash callbacks are merged, they receive some amount of arguments
149/// that are merged in a PHI node. This struct represents arguments from one
150/// call site.
151struct CrashArg {
152 Value *Arg1;
153 Value *Arg2;
154};
155
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000156/// An object of this type is created while instrumenting every function.
157struct AsanFunctionContext {
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000158 AsanFunctionContext(Function &Function) : F(Function), CrashBlock() { }
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000159
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000160 Function &F;
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000161 // These are initially zero. If we require at least one call to
162 // __asan_report_{read,write}{1,2,4,8,16}, an appropriate BB is created.
163 BasicBlock *CrashBlock[2][kNumberOfAccessSizes];
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000164 typedef SmallVector<CrashArg, 8> CrashArgsVec;
165 CrashArgsVec CrashArgs[2][kNumberOfAccessSizes];
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000166};
167
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000168/// AddressSanitizer: instrument the code in module to find memory bugs.
169struct AddressSanitizer : public ModulePass {
170 AddressSanitizer();
Alexander Potapenko25878042012-01-23 11:22:43 +0000171 virtual const char *getPassName() const;
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000172 void instrumentMop(AsanFunctionContext &AFC, Instruction *I);
173 void instrumentAddress(AsanFunctionContext &AFC,
174 Instruction *OrigIns, IRBuilder<> &IRB,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000175 Value *Addr, uint32_t TypeSize, bool IsWrite);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000176 Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
177 Value *ShadowValue, uint32_t TypeSize);
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000178 Instruction *generateCrashCode(BasicBlock *BB, Value *Addr, Value *PC,
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000179 bool IsWrite, size_t AccessSizeIndex);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000180 bool instrumentMemIntrinsic(AsanFunctionContext &AFC, MemIntrinsic *MI);
181 void instrumentMemIntrinsicParam(AsanFunctionContext &AFC,
182 Instruction *OrigIns, Value *Addr,
183 Value *Size,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000184 Instruction *InsertBefore, bool IsWrite);
185 Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
186 bool handleFunction(Module &M, Function &F);
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000187 bool maybeInsertAsanInitAtFunctionEntry(Function &F);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000188 bool poisonStackInFunction(Module &M, Function &F);
189 virtual bool runOnModule(Module &M);
190 bool insertGlobalRedzones(Module &M);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000191 static char ID; // Pass identification, replacement for typeid
192
193 private:
194
195 uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
196 Type *Ty = AI->getAllocatedType();
Evgeniy Stepanovd8313be2012-03-02 10:41:08 +0000197 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000198 return SizeInBytes;
199 }
200 uint64_t getAlignedSize(uint64_t SizeInBytes) {
201 return ((SizeInBytes + RedzoneSize - 1)
202 / RedzoneSize) * RedzoneSize;
203 }
204 uint64_t getAlignedAllocaSize(AllocaInst *AI) {
205 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
206 return getAlignedSize(SizeInBytes);
207 }
208
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000209 Function *checkInterfaceFunction(Constant *FuncOrBitcast);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000210 void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
211 Value *ShadowBase, bool DoPoison);
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +0000212 bool LooksLikeCodeInBug11395(Instruction *I);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000213
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000214 LLVMContext *C;
215 TargetData *TD;
216 uint64_t MappingOffset;
217 int MappingScale;
218 size_t RedzoneSize;
219 int LongSize;
220 Type *IntptrTy;
221 Type *IntptrPtrTy;
222 Function *AsanCtorFunction;
223 Function *AsanInitFunction;
224 Instruction *CtorInsertBefore;
Kostya Serebryanya1c45042012-03-14 23:22:10 +0000225 OwningPtr<FunctionBlackList> BL;
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000226 // This array is indexed by AccessIsWrite and log2(AccessSize).
227 Function *AsanErrorCallback[2][kNumberOfAccessSizes];
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000228 InlineAsm *EmptyAsm;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000229};
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000230
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000231} // namespace
232
233char AddressSanitizer::ID = 0;
234INITIALIZE_PASS(AddressSanitizer, "asan",
235 "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
236 false, false)
237AddressSanitizer::AddressSanitizer() : ModulePass(ID) { }
238ModulePass *llvm::createAddressSanitizerPass() {
239 return new AddressSanitizer();
240}
241
Alexander Potapenko25878042012-01-23 11:22:43 +0000242const char *AddressSanitizer::getPassName() const {
243 return "AddressSanitizer";
244}
245
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000246static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
247 size_t Res = CountTrailingZeros_32(TypeSize / 8);
248 assert(Res < kNumberOfAccessSizes);
249 return Res;
250}
251
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000252// Create a constant for Str so that we can pass it to the run-time lib.
253static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
Chris Lattner18c7f802012-02-05 02:29:43 +0000254 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000255 return new GlobalVariable(M, StrConst->getType(), true,
256 GlobalValue::PrivateLinkage, StrConst, "");
257}
258
259// Split the basic block and insert an if-then code.
260// Before:
261// Head
Kostya Serebryany56139bc2012-07-02 11:42:29 +0000262// Cmp
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000263// Tail
264// After:
265// Head
266// if (Cmp)
Kostya Serebryany56139bc2012-07-02 11:42:29 +0000267// ThenBlock
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000268// Tail
269//
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000270// If ThenBlock is zero, a new block is created and its terminator is returned.
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000271// Otherwize 0 is returned.
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000272static BranchInst *splitBlockAndInsertIfThen(Value *Cmp,
273 BasicBlock *ThenBlock = 0) {
Kostya Serebryany56139bc2012-07-02 11:42:29 +0000274 Instruction *SplitBefore = cast<Instruction>(Cmp)->getNextNode();
Chandler Carruthc3c8db92012-07-16 08:58:53 +0000275 BasicBlock *Head = SplitBefore->getParent();
Chandler Carruth349f14c2012-07-16 10:01:02 +0000276 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
Chandler Carruthc3c8db92012-07-16 08:58:53 +0000277 TerminatorInst *HeadOldTerm = Head->getTerminator();
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000278 BranchInst *CheckTerm = 0;
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000279 if (!ThenBlock) {
280 LLVMContext &C = Head->getParent()->getParent()->getContext();
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000281 ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000282 CheckTerm = BranchInst::Create(Tail, ThenBlock);
283 }
Chandler Carruth349f14c2012-07-16 10:01:02 +0000284 BranchInst *HeadNewTerm =
285 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cmp);
286 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
Chandler Carruthc3c8db92012-07-16 08:58:53 +0000287
Chandler Carruth349f14c2012-07-16 10:01:02 +0000288 return CheckTerm;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000289}
290
291Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
292 // Shadow >> scale
293 Shadow = IRB.CreateLShr(Shadow, MappingScale);
294 if (MappingOffset == 0)
295 return Shadow;
296 // (Shadow >> scale) | offset
297 return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
298 MappingOffset));
299}
300
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000301void AddressSanitizer::instrumentMemIntrinsicParam(
302 AsanFunctionContext &AFC, Instruction *OrigIns,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000303 Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
304 // Check the first byte.
305 {
306 IRBuilder<> IRB(InsertBefore);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000307 instrumentAddress(AFC, OrigIns, IRB, Addr, 8, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000308 }
309 // Check the last byte.
310 {
311 IRBuilder<> IRB(InsertBefore);
312 Value *SizeMinusOne = IRB.CreateSub(
313 Size, ConstantInt::get(Size->getType(), 1));
314 SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
315 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
316 Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000317 instrumentAddress(AFC, OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000318 }
319}
320
321// Instrument memset/memmove/memcpy
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000322bool AddressSanitizer::instrumentMemIntrinsic(AsanFunctionContext &AFC,
323 MemIntrinsic *MI) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000324 Value *Dst = MI->getDest();
325 MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000326 Value *Src = MemTran ? MemTran->getSource() : 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000327 Value *Length = MI->getLength();
328
329 Constant *ConstLength = dyn_cast<Constant>(Length);
330 Instruction *InsertBefore = MI;
331 if (ConstLength) {
332 if (ConstLength->isNullValue()) return false;
333 } else {
334 // The size is not a constant so it could be zero -- check at run-time.
335 IRBuilder<> IRB(InsertBefore);
336
337 Value *Cmp = IRB.CreateICmpNE(Length,
Kostya Serebryany56139bc2012-07-02 11:42:29 +0000338 Constant::getNullValue(Length->getType()));
339 InsertBefore = splitBlockAndInsertIfThen(Cmp);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000340 }
341
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000342 instrumentMemIntrinsicParam(AFC, MI, Dst, Length, InsertBefore, true);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000343 if (Src)
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000344 instrumentMemIntrinsicParam(AFC, MI, Src, Length, InsertBefore, false);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000345 return true;
346}
347
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000348// If I is an interesting memory access, return the PointerOperand
349// and set IsWrite. Otherwise return NULL.
350static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000351 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000352 if (!ClInstrumentReads) return NULL;
353 *IsWrite = false;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000354 return LI->getPointerOperand();
355 }
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000356 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
357 if (!ClInstrumentWrites) return NULL;
358 *IsWrite = true;
359 return SI->getPointerOperand();
360 }
361 if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
362 if (!ClInstrumentAtomics) return NULL;
363 *IsWrite = true;
364 return RMW->getPointerOperand();
365 }
366 if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
367 if (!ClInstrumentAtomics) return NULL;
368 *IsWrite = true;
369 return XCHG->getPointerOperand();
370 }
371 return NULL;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000372}
373
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000374void AddressSanitizer::instrumentMop(AsanFunctionContext &AFC, Instruction *I) {
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000375 bool IsWrite;
376 Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
377 assert(Addr);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000378 if (ClOpt && ClOptGlobals && isa<GlobalVariable>(Addr)) {
379 // We are accessing a global scalar variable. Nothing to catch here.
380 return;
381 }
382 Type *OrigPtrTy = Addr->getType();
383 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
384
385 assert(OrigTy->isSized());
386 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
387
388 if (TypeSize != 8 && TypeSize != 16 &&
389 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
390 // Ignore all unusual sizes.
391 return;
392 }
393
394 IRBuilder<> IRB(I);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000395 instrumentAddress(AFC, I, IRB, Addr, TypeSize, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000396}
397
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000398// Validate the result of Module::getOrInsertFunction called for an interface
399// function of AddressSanitizer. If the instrumented module defines a function
400// with the same name, their prototypes must match, otherwise
401// getOrInsertFunction returns a bitcast.
402Function *AddressSanitizer::checkInterfaceFunction(Constant *FuncOrBitcast) {
403 if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
404 FuncOrBitcast->dump();
405 report_fatal_error("trying to redefine an AddressSanitizer "
406 "interface function");
407}
408
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000409Instruction *AddressSanitizer::generateCrashCode(
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000410 BasicBlock *BB, Value *Addr, Value *PC,
411 bool IsWrite, size_t AccessSizeIndex) {
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000412 IRBuilder<> IRB(BB->getFirstNonPHI());
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000413 CallInst *Call;
414 if (PC)
415 Call = IRB.CreateCall2(AsanErrorCallback[IsWrite][AccessSizeIndex],
416 Addr, PC);
417 else
418 Call = IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex], Addr);
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000419 // We don't do Call->setDoesNotReturn() because the BB already has
420 // UnreachableInst at the end.
421 // This EmptyAsm is required to avoid callback merge.
422 IRB.CreateCall(EmptyAsm);
Kostya Serebryany3c7faae2012-01-06 18:09:21 +0000423 return Call;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000424}
425
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000426Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000427 Value *ShadowValue,
428 uint32_t TypeSize) {
429 size_t Granularity = 1 << MappingScale;
430 // Addr & (Granularity - 1)
431 Value *LastAccessedByte = IRB.CreateAnd(
432 AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
433 // (Addr & (Granularity - 1)) + size - 1
434 if (TypeSize / 8 > 1)
435 LastAccessedByte = IRB.CreateAdd(
436 LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
437 // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
438 LastAccessedByte = IRB.CreateIntCast(
439 LastAccessedByte, IRB.getInt8Ty(), false);
440 // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
441 return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
442}
443
444void AddressSanitizer::instrumentAddress(AsanFunctionContext &AFC,
445 Instruction *OrigIns,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000446 IRBuilder<> &IRB, Value *Addr,
447 uint32_t TypeSize, bool IsWrite) {
448 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
449
450 Type *ShadowTy = IntegerType::get(
451 *C, std::max(8U, TypeSize >> MappingScale));
452 Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
453 Value *ShadowPtr = memToShadow(AddrLong, IRB);
454 Value *CmpVal = Constant::getNullValue(ShadowTy);
455 Value *ShadowValue = IRB.CreateLoad(
456 IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
457
458 Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
459
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000460 BasicBlock *CrashBlock = 0;
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000461 if (ClMergeCallbacks) {
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000462 size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
463 BasicBlock **Cached = &AFC.CrashBlock[IsWrite][AccessSizeIndex];
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000464 if (!*Cached) {
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000465 std::string BBName("crash_bb-");
466 BBName += (IsWrite ? "w-" : "r-") + itostr(1 << AccessSizeIndex);
467 BasicBlock *BB = BasicBlock::Create(*C, BBName, &AFC.F);
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000468 new UnreachableInst(*C, BB);
469 *Cached = BB;
470 }
471 CrashBlock = *Cached;
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000472 // We need to pass the PC as the second parameter to __asan_report_*.
473 // There are few problems:
474 // - Some architectures (e.g. x86_32) don't have a cheap way to get the PC.
475 // - LLVM doesn't have the appropriate intrinsic.
476 // For now, put a random number into the PC, just to allow experiments.
477 Value *PC = ConstantInt::get(IntptrTy, rand());
478 CrashArg Arg = {AddrLong, PC};
479 AFC.CrashArgs[IsWrite][AccessSizeIndex].push_back(Arg);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000480 } else {
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000481 CrashBlock = BasicBlock::Create(*C, "crash_bb", &AFC.F);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000482 new UnreachableInst(*C, CrashBlock);
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000483 size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000484 Instruction *Crash =
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000485 generateCrashCode(CrashBlock, AddrLong, 0, IsWrite, AccessSizeIndex);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000486 Crash->setDebugLoc(OrigIns->getDebugLoc());
487 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000488
489 size_t Granularity = 1 << MappingScale;
490 if (TypeSize < 8 * Granularity) {
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000491 BranchInst *CheckTerm = splitBlockAndInsertIfThen(Cmp);
492 assert(CheckTerm->isUnconditional());
493 BasicBlock *NextBB = CheckTerm->getSuccessor(0);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000494 IRB.SetInsertPoint(CheckTerm);
495 Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000496 BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
497 ReplaceInstWithInst(CheckTerm, NewTerm);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000498 } else {
499 splitBlockAndInsertIfThen(Cmp, CrashBlock);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000500 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000501}
502
503// This function replaces all global variables with new variables that have
504// trailing redzones. It also creates a function that poisons
505// redzones and inserts this function into llvm.global_ctors.
506bool AddressSanitizer::insertGlobalRedzones(Module &M) {
507 SmallVector<GlobalVariable *, 16> GlobalsToChange;
508
509 for (Module::GlobalListType::iterator G = M.getGlobalList().begin(),
510 E = M.getGlobalList().end(); G != E; ++G) {
511 Type *Ty = cast<PointerType>(G->getType())->getElementType();
512 DEBUG(dbgs() << "GLOBAL: " << *G);
513
514 if (!Ty->isSized()) continue;
515 if (!G->hasInitializer()) continue;
Kostya Serebryany7cf2a042011-11-17 23:14:59 +0000516 // Touch only those globals that will not be defined in other modules.
517 // Don't handle ODR type linkages since other modules may be built w/o asan.
Kostya Serebryany2e7fb2f2011-11-17 23:37:53 +0000518 if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
519 G->getLinkage() != GlobalVariable::PrivateLinkage &&
520 G->getLinkage() != GlobalVariable::InternalLinkage)
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000521 continue;
Kostya Serebryanyd2703de2011-11-23 02:10:54 +0000522 // Two problems with thread-locals:
523 // - The address of the main thread's copy can't be computed at link-time.
524 // - Need to poison all copies, not just the main thread's one.
525 if (G->isThreadLocal())
526 continue;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000527 // For now, just ignore this Alloca if the alignment is large.
528 if (G->getAlignment() > RedzoneSize) continue;
529
530 // Ignore all the globals with the names starting with "\01L_OBJC_".
531 // Many of those are put into the .cstring section. The linker compresses
532 // that section by removing the spare \0s after the string terminator, so
533 // our redzones get broken.
534 if ((G->getName().find("\01L_OBJC_") == 0) ||
535 (G->getName().find("\01l_OBJC_") == 0)) {
536 DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
537 continue;
538 }
539
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000540 if (G->hasSection()) {
541 StringRef Section(G->getSection());
Alexander Potapenko8375bc92012-01-30 10:40:22 +0000542 // Ignore the globals from the __OBJC section. The ObjC runtime assumes
543 // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
544 // them.
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000545 if ((Section.find("__OBJC,") == 0) ||
546 (Section.find("__DATA, __objc_") == 0)) {
547 DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
548 continue;
549 }
Alexander Potapenko8375bc92012-01-30 10:40:22 +0000550 // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
551 // Constant CFString instances are compiled in the following way:
552 // -- the string buffer is emitted into
553 // __TEXT,__cstring,cstring_literals
554 // -- the constant NSConstantString structure referencing that buffer
555 // is placed into __DATA,__cfstring
556 // Therefore there's no point in placing redzones into __DATA,__cfstring.
557 // Moreover, it causes the linker to crash on OS X 10.7
558 if (Section.find("__DATA,__cfstring") == 0) {
559 DEBUG(dbgs() << "Ignoring CFString: " << *G);
560 continue;
561 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000562 }
563
564 GlobalsToChange.push_back(G);
565 }
566
567 size_t n = GlobalsToChange.size();
568 if (n == 0) return false;
569
570 // A global is described by a structure
571 // size_t beg;
572 // size_t size;
573 // size_t size_with_redzone;
574 // const char *name;
575 // We initialize an array of such structures and pass it to a run-time call.
576 StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
577 IntptrTy, IntptrTy, NULL);
578 SmallVector<Constant *, 16> Initializers(n);
579
580 IRBuilder<> IRB(CtorInsertBefore);
581
582 for (size_t i = 0; i < n; i++) {
583 GlobalVariable *G = GlobalsToChange[i];
584 PointerType *PtrTy = cast<PointerType>(G->getType());
585 Type *Ty = PtrTy->getElementType();
Kostya Serebryany208a4ff2012-03-21 15:28:50 +0000586 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000587 uint64_t RightRedzoneSize = RedzoneSize +
588 (RedzoneSize - (SizeInBytes % RedzoneSize));
589 Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
590
591 StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
592 Constant *NewInitializer = ConstantStruct::get(
593 NewTy, G->getInitializer(),
594 Constant::getNullValue(RightRedZoneTy), NULL);
595
Kostya Serebryanya4b2b1d2011-12-15 22:55:55 +0000596 SmallString<2048> DescriptionOfGlobal = G->getName();
597 DescriptionOfGlobal += " (";
598 DescriptionOfGlobal += M.getModuleIdentifier();
599 DescriptionOfGlobal += ")";
600 GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000601
602 // Create a new global variable with enough space for a redzone.
603 GlobalVariable *NewGlobal = new GlobalVariable(
604 M, NewTy, G->isConstant(), G->getLinkage(),
Hans Wennborgce718ff2012-06-23 11:37:03 +0000605 NewInitializer, "", G, G->getThreadLocalMode());
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000606 NewGlobal->copyAttributesFrom(G);
607 NewGlobal->setAlignment(RedzoneSize);
608
609 Value *Indices2[2];
610 Indices2[0] = IRB.getInt32(0);
611 Indices2[1] = IRB.getInt32(0);
612
613 G->replaceAllUsesWith(
Kostya Serebryanyf1639ab2012-01-28 04:27:16 +0000614 ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000615 NewGlobal->takeName(G);
616 G->eraseFromParent();
617
618 Initializers[i] = ConstantStruct::get(
619 GlobalStructTy,
620 ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
621 ConstantInt::get(IntptrTy, SizeInBytes),
622 ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
623 ConstantExpr::getPointerCast(Name, IntptrTy),
624 NULL);
625 DEBUG(dbgs() << "NEW GLOBAL:\n" << *NewGlobal);
626 }
627
628 ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
629 GlobalVariable *AllGlobals = new GlobalVariable(
630 M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
631 ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
632
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000633 Function *AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000634 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
635 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
636
637 IRB.CreateCall2(AsanRegisterGlobals,
638 IRB.CreatePointerCast(AllGlobals, IntptrTy),
639 ConstantInt::get(IntptrTy, n));
640
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000641 // We also need to unregister globals at the end, e.g. when a shared library
642 // gets closed.
643 Function *AsanDtorFunction = Function::Create(
644 FunctionType::get(Type::getVoidTy(*C), false),
645 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
646 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
647 IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000648 Function *AsanUnregisterGlobals =
649 checkInterfaceFunction(M.getOrInsertFunction(
650 kAsanUnregisterGlobalsName,
651 IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000652 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
653
654 IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
655 IRB.CreatePointerCast(AllGlobals, IntptrTy),
656 ConstantInt::get(IntptrTy, n));
657 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
658
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000659 DEBUG(dbgs() << M);
660 return true;
661}
662
663// virtual
664bool AddressSanitizer::runOnModule(Module &M) {
665 // Initialize the private fields. No one has accessed them before.
666 TD = getAnalysisIfAvailable<TargetData>();
667 if (!TD)
668 return false;
Kostya Serebryanya1c45042012-03-14 23:22:10 +0000669 BL.reset(new FunctionBlackList(ClBlackListFile));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000670
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000671 C = &(M.getContext());
672 LongSize = TD->getPointerSizeInBits();
673 IntptrTy = Type::getIntNTy(*C, LongSize);
674 IntptrPtrTy = PointerType::get(IntptrTy, 0);
675
676 AsanCtorFunction = Function::Create(
677 FunctionType::get(Type::getVoidTy(*C), false),
678 GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
679 BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
680 CtorInsertBefore = ReturnInst::Create(*C, AsanCtorBB);
681
682 // call __asan_init in the module ctor.
683 IRBuilder<> IRB(CtorInsertBefore);
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000684 AsanInitFunction = checkInterfaceFunction(
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000685 M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
686 AsanInitFunction->setLinkage(Function::ExternalLinkage);
687 IRB.CreateCall(AsanInitFunction);
688
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000689 // Create __asan_report* callbacks.
690 for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
691 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
692 AccessSizeIndex++) {
693 // IsWrite and TypeSize are encoded in the function name.
694 std::string FunctionName = std::string(kAsanReportErrorTemplate) +
695 (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000696 // If we are merging crash callbacks, they have two parameters.
697 if (ClMergeCallbacks)
698 AsanErrorCallback[AccessIsWrite][AccessSizeIndex] = cast<Function>(
699 M.getOrInsertFunction(FunctionName, IRB.getVoidTy(), IntptrTy,
700 IntptrTy, NULL));
701 else
702 AsanErrorCallback[AccessIsWrite][AccessSizeIndex] = cast<Function>(
703 M.getOrInsertFunction(FunctionName, IRB.getVoidTy(), IntptrTy, NULL));
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000704 }
705 }
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000706 // We insert an empty inline asm after __asan_report* to avoid callback merge.
707 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
708 StringRef(""), StringRef(""),
709 /*hasSideEffects=*/true);
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000710
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +0000711 llvm::Triple targetTriple(M.getTargetTriple());
712 bool isAndroid = targetTriple.getEnvironment() == llvm::Triple::ANDROIDEABI;
713
714 MappingOffset = isAndroid ? kDefaultShadowOffsetAndroid :
715 (LongSize == 32 ? kDefaultShadowOffset32 : kDefaultShadowOffset64);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000716 if (ClMappingOffsetLog >= 0) {
717 if (ClMappingOffsetLog == 0) {
718 // special case
719 MappingOffset = 0;
720 } else {
721 MappingOffset = 1ULL << ClMappingOffsetLog;
722 }
723 }
724 MappingScale = kDefaultShadowScale;
725 if (ClMappingScale) {
726 MappingScale = ClMappingScale;
727 }
728 // Redzone used for stack and globals is at least 32 bytes.
729 // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
730 RedzoneSize = std::max(32, (int)(1 << MappingScale));
731
732 bool Res = false;
733
734 if (ClGlobals)
735 Res |= insertGlobalRedzones(M);
736
Kostya Serebryany8c0134a2012-03-19 16:40:35 +0000737 if (ClMappingOffsetLog >= 0) {
738 // Tell the run-time the current values of mapping offset and scale.
739 GlobalValue *asan_mapping_offset =
740 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
741 ConstantInt::get(IntptrTy, MappingOffset),
742 kAsanMappingOffsetName);
743 // Read the global, otherwise it may be optimized away.
744 IRB.CreateLoad(asan_mapping_offset, true);
745 }
746 if (ClMappingScale) {
747 GlobalValue *asan_mapping_scale =
748 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
749 ConstantInt::get(IntptrTy, MappingScale),
750 kAsanMappingScaleName);
751 // Read the global, otherwise it may be optimized away.
752 IRB.CreateLoad(asan_mapping_scale, true);
753 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000754
755
756 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
757 if (F->isDeclaration()) continue;
758 Res |= handleFunction(M, *F);
759 }
760
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000761 appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
Kostya Serebryany9b027412011-12-12 18:01:46 +0000762
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000763 return Res;
764}
765
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000766bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
767 // For each NSObject descendant having a +load method, this method is invoked
768 // by the ObjC runtime before any of the static constructors is called.
769 // Therefore we need to instrument such methods with a call to __asan_init
770 // at the beginning in order to initialize our runtime before any access to
771 // the shadow memory.
772 // We cannot just ignore these methods, because they may call other
773 // instrumented functions.
774 if (F.getName().find(" load]") != std::string::npos) {
775 IRBuilder<> IRB(F.begin()->begin());
776 IRB.CreateCall(AsanInitFunction);
777 return true;
778 }
779 return false;
780}
781
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000782bool AddressSanitizer::handleFunction(Module &M, Function &F) {
783 if (BL->isIn(F)) return false;
784 if (&F == AsanCtorFunction) return false;
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000785
786 // If needed, insert __asan_init before checking for AddressSafety attr.
787 maybeInsertAsanInitAtFunctionEntry(F);
788
Kostya Serebryany0307b9a2012-01-24 19:34:43 +0000789 if (!F.hasFnAttr(Attribute::AddressSafety)) return false;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000790
791 if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
792 return false;
793 // We want to instrument every address only once per basic block
794 // (unless there are calls between uses).
795 SmallSet<Value*, 16> TempsToInstrument;
796 SmallVector<Instruction*, 16> ToInstrument;
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000797 SmallVector<Instruction*, 8> NoReturnCalls;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000798 bool IsWrite;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000799
800 // Fill the set of memory operations to instrument.
801 for (Function::iterator FI = F.begin(), FE = F.end();
802 FI != FE; ++FI) {
803 TempsToInstrument.clear();
Kostya Serebryany324cbb82012-06-28 09:34:41 +0000804 int NumInsnsPerBB = 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000805 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
806 BI != BE; ++BI) {
Kostya Serebryanybcb55ce2012-01-11 18:15:23 +0000807 if (LooksLikeCodeInBug11395(BI)) return false;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000808 if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000809 if (ClOpt && ClOptSameTemp) {
810 if (!TempsToInstrument.insert(Addr))
811 continue; // We've seen this temp in the current BB.
812 }
813 } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
814 // ok, take it.
815 } else {
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000816 if (CallInst *CI = dyn_cast<CallInst>(BI)) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000817 // A call inside BB.
818 TempsToInstrument.clear();
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000819 if (CI->doesNotReturn()) {
820 NoReturnCalls.push_back(CI);
821 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000822 }
823 continue;
824 }
825 ToInstrument.push_back(BI);
Kostya Serebryany324cbb82012-06-28 09:34:41 +0000826 NumInsnsPerBB++;
827 if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
828 break;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000829 }
830 }
831
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000832 AsanFunctionContext AFC(F);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000833
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000834 // Instrument.
835 int NumInstrumented = 0;
836 for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
837 Instruction *Inst = ToInstrument[i];
838 if (ClDebugMin < 0 || ClDebugMax < 0 ||
839 (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000840 if (isInterestingMemoryAccess(Inst, &IsWrite))
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000841 instrumentMop(AFC, Inst);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000842 else
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000843 instrumentMemIntrinsic(AFC, cast<MemIntrinsic>(Inst));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000844 }
845 NumInstrumented++;
846 }
847
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000848 // Create PHI nodes and crash callbacks if we are merging crash callbacks.
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000849 if (NumInstrumented) {
850 for (size_t IsWrite = 0; IsWrite <= 1; IsWrite++) {
851 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
852 AccessSizeIndex++) {
853 BasicBlock *BB = AFC.CrashBlock[IsWrite][AccessSizeIndex];
854 if (!BB) continue;
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000855 assert(ClMergeCallbacks);
856 AsanFunctionContext::CrashArgsVec &Args =
857 AFC.CrashArgs[IsWrite][AccessSizeIndex];
858 IRBuilder<> IRB(BB->getFirstNonPHI());
859 size_t n = Args.size();
860 PHINode *PN1 = IRB.CreatePHI(IntptrTy, n);
861 PHINode *PN2 = IRB.CreatePHI(IntptrTy, n);
862 // We need to match crash parameters and the predecessors.
863 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
864 PI != PE; ++PI) {
865 n--;
866 PN1->addIncoming(Args[n].Arg1, *PI);
867 PN2->addIncoming(Args[n].Arg2, *PI);
868 }
869 assert(n == 0);
870 generateCrashCode(BB, PN1, PN2, IsWrite, AccessSizeIndex);
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000871 }
872 }
873 }
874
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000875 DEBUG(dbgs() << F);
876
877 bool ChangedStack = poisonStackInFunction(M, F);
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000878
879 // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
880 // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
881 for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
882 Instruction *CI = NoReturnCalls[i];
883 IRBuilder<> IRB(CI);
884 IRB.CreateCall(M.getOrInsertFunction(kAsanHandleNoReturnName,
885 IRB.getVoidTy(), NULL));
886 }
887
888 return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000889}
890
891static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
892 if (ShadowRedzoneSize == 1) return PoisonByte;
893 if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
894 if (ShadowRedzoneSize == 4)
895 return (PoisonByte << 24) + (PoisonByte << 16) +
896 (PoisonByte << 8) + (PoisonByte);
Craig Topper85814382012-02-07 05:05:23 +0000897 llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000898}
899
900static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
901 size_t Size,
902 size_t RedzoneSize,
903 size_t ShadowGranularity,
904 uint8_t Magic) {
905 for (size_t i = 0; i < RedzoneSize;
906 i+= ShadowGranularity, Shadow++) {
907 if (i + ShadowGranularity <= Size) {
908 *Shadow = 0; // fully addressable
909 } else if (i >= Size) {
910 *Shadow = Magic; // unaddressable
911 } else {
912 *Shadow = Size - i; // first Size-i bytes are addressable
913 }
914 }
915}
916
917void AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
918 IRBuilder<> IRB,
919 Value *ShadowBase, bool DoPoison) {
920 size_t ShadowRZSize = RedzoneSize >> MappingScale;
921 assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
922 Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
923 Type *RZPtrTy = PointerType::get(RZTy, 0);
924
925 Value *PoisonLeft = ConstantInt::get(RZTy,
926 ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
927 Value *PoisonMid = ConstantInt::get(RZTy,
928 ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
929 Value *PoisonRight = ConstantInt::get(RZTy,
930 ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
931
932 // poison the first red zone.
933 IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
934
935 // poison all other red zones.
936 uint64_t Pos = RedzoneSize;
937 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
938 AllocaInst *AI = AllocaVec[i];
939 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
940 uint64_t AlignedSize = getAlignedAllocaSize(AI);
941 assert(AlignedSize - SizeInBytes < RedzoneSize);
942 Value *Ptr = NULL;
943
944 Pos += AlignedSize;
945
946 assert(ShadowBase->getType() == IntptrTy);
947 if (SizeInBytes < AlignedSize) {
948 // Poison the partial redzone at right
949 Ptr = IRB.CreateAdd(
950 ShadowBase, ConstantInt::get(IntptrTy,
951 (Pos >> MappingScale) - ShadowRZSize));
952 size_t AddressableBytes = RedzoneSize - (AlignedSize - SizeInBytes);
953 uint32_t Poison = 0;
954 if (DoPoison) {
955 PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
956 RedzoneSize,
957 1ULL << MappingScale,
958 kAsanStackPartialRedzoneMagic);
959 }
960 Value *PartialPoison = ConstantInt::get(RZTy, Poison);
961 IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
962 }
963
964 // Poison the full redzone at right.
965 Ptr = IRB.CreateAdd(ShadowBase,
966 ConstantInt::get(IntptrTy, Pos >> MappingScale));
967 Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
968 IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
969
970 Pos += RedzoneSize;
971 }
972}
973
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +0000974// Workaround for bug 11395: we don't want to instrument stack in functions
975// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
Kostya Serebryanyd2703de2011-11-23 02:10:54 +0000976// FIXME: remove once the bug 11395 is fixed.
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +0000977bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
978 if (LongSize != 32) return false;
979 CallInst *CI = dyn_cast<CallInst>(I);
980 if (!CI || !CI->isInlineAsm()) return false;
981 if (CI->getNumArgOperands() <= 5) return false;
982 // We have inline assembly with quite a few arguments.
983 return true;
984}
985
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000986// Find all static Alloca instructions and put
987// poisoned red zones around all of them.
988// Then unpoison everything back before the function returns.
989//
990// Stack poisoning does not play well with exception handling.
991// When an exception is thrown, we essentially bypass the code
992// that unpoisones the stack. This is why the run-time library has
993// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
994// stack in the interceptor. This however does not work inside the
995// actual function which catches the exception. Most likely because the
996// compiler hoists the load of the shadow value somewhere too high.
997// This causes asan to report a non-existing bug on 453.povray.
998// It sounds like an LLVM bug.
999bool AddressSanitizer::poisonStackInFunction(Module &M, Function &F) {
1000 if (!ClStack) return false;
1001 SmallVector<AllocaInst*, 16> AllocaVec;
1002 SmallVector<Instruction*, 8> RetVec;
1003 uint64_t TotalSize = 0;
1004
1005 // Filter out Alloca instructions we want (and can) handle.
1006 // Collect Ret instructions.
1007 for (Function::iterator FI = F.begin(), FE = F.end();
1008 FI != FE; ++FI) {
1009 BasicBlock &BB = *FI;
1010 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
1011 BI != BE; ++BI) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001012 if (isa<ReturnInst>(BI)) {
1013 RetVec.push_back(BI);
1014 continue;
1015 }
1016
1017 AllocaInst *AI = dyn_cast<AllocaInst>(BI);
1018 if (!AI) continue;
1019 if (AI->isArrayAllocation()) continue;
1020 if (!AI->isStaticAlloca()) continue;
1021 if (!AI->getAllocatedType()->isSized()) continue;
1022 if (AI->getAlignment() > RedzoneSize) continue;
1023 AllocaVec.push_back(AI);
1024 uint64_t AlignedSize = getAlignedAllocaSize(AI);
1025 TotalSize += AlignedSize;
1026 }
1027 }
1028
1029 if (AllocaVec.empty()) return false;
1030
1031 uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize;
1032
1033 bool DoStackMalloc = ClUseAfterReturn
1034 && LocalStackSize <= kMaxStackMallocSize;
1035
1036 Instruction *InsBefore = AllocaVec[0];
1037 IRBuilder<> IRB(InsBefore);
1038
1039
1040 Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
1041 AllocaInst *MyAlloca =
1042 new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
1043 MyAlloca->setAlignment(RedzoneSize);
1044 assert(MyAlloca->isStaticAlloca());
1045 Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
1046 Value *LocalStackBase = OrigStackBase;
1047
1048 if (DoStackMalloc) {
1049 Value *AsanStackMallocFunc = M.getOrInsertFunction(
1050 kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL);
1051 LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
1052 ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
1053 }
1054
1055 // This string will be parsed by the run-time (DescribeStackAddress).
1056 SmallString<2048> StackDescriptionStorage;
1057 raw_svector_ostream StackDescription(StackDescriptionStorage);
1058 StackDescription << F.getName() << " " << AllocaVec.size() << " ";
1059
1060 uint64_t Pos = RedzoneSize;
1061 // Replace Alloca instructions with base+offset.
1062 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1063 AllocaInst *AI = AllocaVec[i];
1064 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1065 StringRef Name = AI->getName();
1066 StackDescription << Pos << " " << SizeInBytes << " "
1067 << Name.size() << " " << Name << " ";
1068 uint64_t AlignedSize = getAlignedAllocaSize(AI);
1069 assert((AlignedSize % RedzoneSize) == 0);
1070 AI->replaceAllUsesWith(
1071 IRB.CreateIntToPtr(
1072 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
1073 AI->getType()));
1074 Pos += AlignedSize + RedzoneSize;
1075 }
1076 assert(Pos == LocalStackSize);
1077
1078 // Write the Magic value and the frame description constant to the redzone.
1079 Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1080 IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1081 BasePlus0);
1082 Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
1083 ConstantInt::get(IntptrTy, LongSize/8));
1084 BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
1085 Value *Description = IRB.CreatePointerCast(
1086 createPrivateGlobalForString(M, StackDescription.str()),
1087 IntptrTy);
1088 IRB.CreateStore(Description, BasePlus1);
1089
1090 // Poison the stack redzones at the entry.
1091 Value *ShadowBase = memToShadow(LocalStackBase, IRB);
1092 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
1093
1094 Value *AsanStackFreeFunc = NULL;
1095 if (DoStackMalloc) {
1096 AsanStackFreeFunc = M.getOrInsertFunction(
1097 kAsanStackFreeName, IRB.getVoidTy(),
1098 IntptrTy, IntptrTy, IntptrTy, NULL);
1099 }
1100
1101 // Unpoison the stack before all ret instructions.
1102 for (size_t i = 0, n = RetVec.size(); i < n; i++) {
1103 Instruction *Ret = RetVec[i];
1104 IRBuilder<> IRBRet(Ret);
1105
1106 // Mark the current frame as retired.
1107 IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1108 BasePlus0);
1109 // Unpoison the stack.
1110 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
1111
1112 if (DoStackMalloc) {
1113 IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
1114 ConstantInt::get(IntptrTy, LocalStackSize),
1115 OrigStackBase);
1116 }
1117 }
1118
1119 if (ClDebugStack) {
1120 DEBUG(dbgs() << F);
1121 }
1122
1123 return true;
1124}