blob: b7be4625ca8d5d8b35740fd2a3aebae410fd5a50 [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 Serebryanyb5b86d22012-08-24 16:44:47 +000018#include "BlackList.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"
Micah Villmow3574eca2012-10-08 16:38:25 +000038#include "llvm/DataLayout.h"
Kostya Serebryany800e03f2011-11-16 01:35:23 +000039#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 Serebryany9b9f87a2012-08-21 08:24:25 +000064static const char *kAsanPoisonGlobalsName = "__asan_before_dynamic_init";
65static const char *kAsanUnpoisonGlobalsName = "__asan_after_dynamic_init";
Kostya Serebryany800e03f2011-11-16 01:35:23 +000066static const char *kAsanInitName = "__asan_init";
Kostya Serebryany95e3cf42012-02-08 21:36:17 +000067static const char *kAsanHandleNoReturnName = "__asan_handle_no_return";
Kostya Serebryany800e03f2011-11-16 01:35:23 +000068static const char *kAsanMappingOffsetName = "__asan_mapping_offset";
69static const char *kAsanMappingScaleName = "__asan_mapping_scale";
70static const char *kAsanStackMallocName = "__asan_stack_malloc";
71static const char *kAsanStackFreeName = "__asan_stack_free";
72
73static const int kAsanStackLeftRedzoneMagic = 0xf1;
74static const int kAsanStackMidRedzoneMagic = 0xf2;
75static const int kAsanStackRightRedzoneMagic = 0xf3;
76static const int kAsanStackPartialRedzoneMagic = 0xf4;
77
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +000078// Accesses sizes are powers of two: 1, 2, 4, 8, 16.
79static const size_t kNumberOfAccessSizes = 5;
80
Kostya Serebryany800e03f2011-11-16 01:35:23 +000081// Command-line flags.
82
83// This flag may need to be replaced with -f[no-]asan-reads.
84static cl::opt<bool> ClInstrumentReads("asan-instrument-reads",
85 cl::desc("instrument read instructions"), cl::Hidden, cl::init(true));
86static cl::opt<bool> ClInstrumentWrites("asan-instrument-writes",
87 cl::desc("instrument write instructions"), cl::Hidden, cl::init(true));
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +000088static cl::opt<bool> ClInstrumentAtomics("asan-instrument-atomics",
89 cl::desc("instrument atomic instructions (rmw, cmpxchg)"),
90 cl::Hidden, cl::init(true));
Kostya Serebryany6e2d5062012-08-15 08:58:58 +000091static cl::opt<bool> ClAlwaysSlowPath("asan-always-slow-path",
92 cl::desc("use instrumentation with slow path for all accesses"),
93 cl::Hidden, cl::init(false));
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +000094// This flag limits the number of instructions to be instrumented
Kostya Serebryany324cbb82012-06-28 09:34:41 +000095// in any given BB. Normally, this should be set to unlimited (INT_MAX),
96// but due to http://llvm.org/bugs/show_bug.cgi?id=12652 we temporary
97// set it to 10000.
98static cl::opt<int> ClMaxInsnsToInstrumentPerBB("asan-max-ins-per-bb",
99 cl::init(10000),
100 cl::desc("maximal number of instructions to instrument in any given BB"),
101 cl::Hidden);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000102// This flag may need to be replaced with -f[no]asan-stack.
103static cl::opt<bool> ClStack("asan-stack",
104 cl::desc("Handle stack memory"), cl::Hidden, cl::init(true));
105// This flag may need to be replaced with -f[no]asan-use-after-return.
106static cl::opt<bool> ClUseAfterReturn("asan-use-after-return",
107 cl::desc("Check return-after-free"), cl::Hidden, cl::init(false));
108// This flag may need to be replaced with -f[no]asan-globals.
109static cl::opt<bool> ClGlobals("asan-globals",
110 cl::desc("Handle global objects"), cl::Hidden, cl::init(true));
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000111static cl::opt<bool> ClInitializers("asan-initialization-order",
112 cl::desc("Handle C++ initializer order"), cl::Hidden, cl::init(false));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000113static cl::opt<bool> ClMemIntrin("asan-memintrin",
114 cl::desc("Handle memset/memcpy/memmove"), cl::Hidden, cl::init(true));
115// This flag may need to be replaced with -fasan-blacklist.
116static cl::opt<std::string> ClBlackListFile("asan-blacklist",
117 cl::desc("File containing the list of functions to ignore "
118 "during instrumentation"), cl::Hidden);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000119
120// These flags allow to change the shadow mapping.
121// The shadow mapping looks like
122// Shadow = (Mem >> scale) + (1 << offset_log)
123static cl::opt<int> ClMappingScale("asan-mapping-scale",
124 cl::desc("scale of asan shadow mapping"), cl::Hidden, cl::init(0));
125static cl::opt<int> ClMappingOffsetLog("asan-mapping-offset-log",
126 cl::desc("offset of asan shadow mapping"), cl::Hidden, cl::init(-1));
127
128// Optimization flags. Not user visible, used mostly for testing
129// and benchmarking the tool.
130static cl::opt<bool> ClOpt("asan-opt",
131 cl::desc("Optimize instrumentation"), cl::Hidden, cl::init(true));
132static cl::opt<bool> ClOptSameTemp("asan-opt-same-temp",
133 cl::desc("Instrument the same temp just once"), cl::Hidden,
134 cl::init(true));
135static cl::opt<bool> ClOptGlobals("asan-opt-globals",
136 cl::desc("Don't instrument scalar globals"), cl::Hidden, cl::init(true));
137
138// Debug flags.
139static cl::opt<int> ClDebug("asan-debug", cl::desc("debug"), cl::Hidden,
140 cl::init(0));
141static cl::opt<int> ClDebugStack("asan-debug-stack", cl::desc("debug stack"),
142 cl::Hidden, cl::init(0));
143static cl::opt<std::string> ClDebugFunc("asan-debug-func",
144 cl::Hidden, cl::desc("Debug func"));
145static cl::opt<int> ClDebugMin("asan-debug-min", cl::desc("Debug min inst"),
146 cl::Hidden, cl::init(-1));
147static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
148 cl::Hidden, cl::init(-1));
149
150namespace {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000151/// AddressSanitizer: instrument the code in module to find memory bugs.
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000152struct AddressSanitizer : public FunctionPass {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000153 AddressSanitizer();
Alexander Potapenko25878042012-01-23 11:22:43 +0000154 virtual const char *getPassName() const;
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000155 void instrumentMop(Instruction *I);
156 void instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000157 Value *Addr, uint32_t TypeSize, bool IsWrite);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000158 Value *createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
159 Value *ShadowValue, uint32_t TypeSize);
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000160 Instruction *generateCrashCode(Instruction *InsertBefore, Value *Addr,
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000161 bool IsWrite, size_t AccessSizeIndex);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000162 bool instrumentMemIntrinsic(MemIntrinsic *MI);
163 void instrumentMemIntrinsicParam(Instruction *OrigIns, Value *Addr,
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000164 Value *Size,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000165 Instruction *InsertBefore, bool IsWrite);
166 Value *memToShadow(Value *Shadow, IRBuilder<> &IRB);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000167 bool runOnFunction(Function &F);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000168 void createInitializerPoisonCalls(Module &M,
169 Value *FirstAddr, Value *LastAddr);
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000170 bool maybeInsertAsanInitAtFunctionEntry(Function &F);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000171 bool poisonStackInFunction(Function &F);
172 virtual bool doInitialization(Module &M);
173 virtual bool doFinalization(Module &M);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000174 bool insertGlobalRedzones(Module &M);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000175 static char ID; // Pass identification, replacement for typeid
176
177 private:
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000178 uint64_t getAllocaSizeInBytes(AllocaInst *AI) {
179 Type *Ty = AI->getAllocatedType();
Evgeniy Stepanovd8313be2012-03-02 10:41:08 +0000180 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000181 return SizeInBytes;
182 }
183 uint64_t getAlignedSize(uint64_t SizeInBytes) {
184 return ((SizeInBytes + RedzoneSize - 1)
185 / RedzoneSize) * RedzoneSize;
186 }
187 uint64_t getAlignedAllocaSize(AllocaInst *AI) {
188 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
189 return getAlignedSize(SizeInBytes);
190 }
191
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000192 Function *checkInterfaceFunction(Constant *FuncOrBitcast);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000193 bool ShouldInstrumentGlobal(GlobalVariable *G);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000194 void PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec, IRBuilder<> IRB,
195 Value *ShadowBase, bool DoPoison);
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +0000196 bool LooksLikeCodeInBug11395(Instruction *I);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000197 void FindDynamicInitializers(Module &M);
198 bool HasDynamicInitializer(GlobalVariable *G);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000199
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000200 LLVMContext *C;
Micah Villmow3574eca2012-10-08 16:38:25 +0000201 DataLayout *TD;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000202 uint64_t MappingOffset;
203 int MappingScale;
204 size_t RedzoneSize;
205 int LongSize;
206 Type *IntptrTy;
207 Type *IntptrPtrTy;
208 Function *AsanCtorFunction;
209 Function *AsanInitFunction;
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000210 Function *AsanStackMallocFunc, *AsanStackFreeFunc;
211 Function *AsanHandleNoReturnFunc;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000212 Instruction *CtorInsertBefore;
Kostya Serebryanyb5b86d22012-08-24 16:44:47 +0000213 OwningPtr<BlackList> BL;
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000214 // This array is indexed by AccessIsWrite and log2(AccessSize).
215 Function *AsanErrorCallback[2][kNumberOfAccessSizes];
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000216 InlineAsm *EmptyAsm;
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000217 SmallSet<GlobalValue*, 32> DynamicallyInitializedGlobals;
Kostya Serebryanya5f54f12012-11-01 13:42:40 +0000218 SmallSet<GlobalValue*, 32> GlobalsCreatedByAsan;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000219};
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000220
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000221} // namespace
222
223char AddressSanitizer::ID = 0;
224INITIALIZE_PASS(AddressSanitizer, "asan",
225 "AddressSanitizer: detects use-after-free and out-of-bounds bugs.",
226 false, false)
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000227AddressSanitizer::AddressSanitizer() : FunctionPass(ID) { }
228FunctionPass *llvm::createAddressSanitizerPass() {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000229 return new AddressSanitizer();
230}
231
Alexander Potapenko25878042012-01-23 11:22:43 +0000232const char *AddressSanitizer::getPassName() const {
233 return "AddressSanitizer";
234}
235
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000236static size_t TypeSizeToSizeIndex(uint32_t TypeSize) {
237 size_t Res = CountTrailingZeros_32(TypeSize / 8);
238 assert(Res < kNumberOfAccessSizes);
239 return Res;
240}
241
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000242// Create a constant for Str so that we can pass it to the run-time lib.
243static GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str) {
Chris Lattner18c7f802012-02-05 02:29:43 +0000244 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000245 return new GlobalVariable(M, StrConst->getType(), true,
246 GlobalValue::PrivateLinkage, StrConst, "");
247}
248
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000249Value *AddressSanitizer::memToShadow(Value *Shadow, IRBuilder<> &IRB) {
250 // Shadow >> scale
251 Shadow = IRB.CreateLShr(Shadow, MappingScale);
252 if (MappingOffset == 0)
253 return Shadow;
254 // (Shadow >> scale) | offset
255 return IRB.CreateOr(Shadow, ConstantInt::get(IntptrTy,
256 MappingOffset));
257}
258
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000259void AddressSanitizer::instrumentMemIntrinsicParam(
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000260 Instruction *OrigIns,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000261 Value *Addr, Value *Size, Instruction *InsertBefore, bool IsWrite) {
262 // Check the first byte.
263 {
264 IRBuilder<> IRB(InsertBefore);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000265 instrumentAddress(OrigIns, IRB, Addr, 8, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000266 }
267 // Check the last byte.
268 {
269 IRBuilder<> IRB(InsertBefore);
270 Value *SizeMinusOne = IRB.CreateSub(
271 Size, ConstantInt::get(Size->getType(), 1));
272 SizeMinusOne = IRB.CreateIntCast(SizeMinusOne, IntptrTy, false);
273 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
274 Value *AddrPlusSizeMinisOne = IRB.CreateAdd(AddrLong, SizeMinusOne);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000275 instrumentAddress(OrigIns, IRB, AddrPlusSizeMinisOne, 8, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000276 }
277}
278
279// Instrument memset/memmove/memcpy
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000280bool AddressSanitizer::instrumentMemIntrinsic(MemIntrinsic *MI) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000281 Value *Dst = MI->getDest();
282 MemTransferInst *MemTran = dyn_cast<MemTransferInst>(MI);
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000283 Value *Src = MemTran ? MemTran->getSource() : 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000284 Value *Length = MI->getLength();
285
286 Constant *ConstLength = dyn_cast<Constant>(Length);
287 Instruction *InsertBefore = MI;
288 if (ConstLength) {
289 if (ConstLength->isNullValue()) return false;
290 } else {
291 // The size is not a constant so it could be zero -- check at run-time.
292 IRBuilder<> IRB(InsertBefore);
293
294 Value *Cmp = IRB.CreateICmpNE(Length,
Kostya Serebryany56139bc2012-07-02 11:42:29 +0000295 Constant::getNullValue(Length->getType()));
Evgeniy Stepanov4a2dec02012-10-19 10:48:31 +0000296 InsertBefore = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000297 }
298
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000299 instrumentMemIntrinsicParam(MI, Dst, Length, InsertBefore, true);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000300 if (Src)
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000301 instrumentMemIntrinsicParam(MI, Src, Length, InsertBefore, false);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000302 return true;
303}
304
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000305// If I is an interesting memory access, return the PointerOperand
306// and set IsWrite. Otherwise return NULL.
307static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000308 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000309 if (!ClInstrumentReads) return NULL;
310 *IsWrite = false;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000311 return LI->getPointerOperand();
312 }
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000313 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
314 if (!ClInstrumentWrites) return NULL;
315 *IsWrite = true;
316 return SI->getPointerOperand();
317 }
318 if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
319 if (!ClInstrumentAtomics) return NULL;
320 *IsWrite = true;
321 return RMW->getPointerOperand();
322 }
323 if (AtomicCmpXchgInst *XCHG = dyn_cast<AtomicCmpXchgInst>(I)) {
324 if (!ClInstrumentAtomics) return NULL;
325 *IsWrite = true;
326 return XCHG->getPointerOperand();
327 }
328 return NULL;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000329}
330
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000331void AddressSanitizer::FindDynamicInitializers(Module& M) {
332 // Clang generates metadata identifying all dynamically initialized globals.
333 NamedMDNode *DynamicGlobals =
334 M.getNamedMetadata("llvm.asan.dynamically_initialized_globals");
335 if (!DynamicGlobals)
336 return;
337 for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) {
338 MDNode *MDN = DynamicGlobals->getOperand(i);
339 assert(MDN->getNumOperands() == 1);
340 Value *VG = MDN->getOperand(0);
341 // The optimizer may optimize away a global entirely, in which case we
342 // cannot instrument access to it.
343 if (!VG)
344 continue;
345
346 GlobalVariable *G = cast<GlobalVariable>(VG);
347 DynamicallyInitializedGlobals.insert(G);
348 }
349}
350// Returns true if a global variable is initialized dynamically in this TU.
351bool AddressSanitizer::HasDynamicInitializer(GlobalVariable *G) {
352 return DynamicallyInitializedGlobals.count(G);
353}
354
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000355void AddressSanitizer::instrumentMop(Instruction *I) {
Axel Naumann3780ad82012-09-17 14:20:57 +0000356 bool IsWrite = false;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000357 Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
358 assert(Addr);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000359 if (ClOpt && ClOptGlobals) {
360 if (GlobalVariable *G = dyn_cast<GlobalVariable>(Addr)) {
361 // If initialization order checking is disabled, a simple access to a
362 // dynamically initialized global is always valid.
363 if (!ClInitializers)
364 return;
365 // If a global variable does not have dynamic initialization we don't
366 // have to instrument it. However, if a global has external linkage, we
367 // assume it has dynamic initialization, as it may have an initializer
368 // in a different TU.
369 if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
370 !HasDynamicInitializer(G))
371 return;
372 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000373 }
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000374
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000375 Type *OrigPtrTy = Addr->getType();
376 Type *OrigTy = cast<PointerType>(OrigPtrTy)->getElementType();
377
378 assert(OrigTy->isSized());
379 uint32_t TypeSize = TD->getTypeStoreSizeInBits(OrigTy);
380
381 if (TypeSize != 8 && TypeSize != 16 &&
382 TypeSize != 32 && TypeSize != 64 && TypeSize != 128) {
383 // Ignore all unusual sizes.
384 return;
385 }
386
387 IRBuilder<> IRB(I);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000388 instrumentAddress(I, IRB, Addr, TypeSize, IsWrite);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000389}
390
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000391// Validate the result of Module::getOrInsertFunction called for an interface
392// function of AddressSanitizer. If the instrumented module defines a function
393// with the same name, their prototypes must match, otherwise
394// getOrInsertFunction returns a bitcast.
395Function *AddressSanitizer::checkInterfaceFunction(Constant *FuncOrBitcast) {
396 if (isa<Function>(FuncOrBitcast)) return cast<Function>(FuncOrBitcast);
397 FuncOrBitcast->dump();
398 report_fatal_error("trying to redefine an AddressSanitizer "
399 "interface function");
400}
401
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000402Instruction *AddressSanitizer::generateCrashCode(
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000403 Instruction *InsertBefore, Value *Addr,
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000404 bool IsWrite, size_t AccessSizeIndex) {
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000405 IRBuilder<> IRB(InsertBefore);
406 CallInst *Call = IRB.CreateCall(AsanErrorCallback[IsWrite][AccessSizeIndex],
407 Addr);
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000408 // We don't do Call->setDoesNotReturn() because the BB already has
409 // UnreachableInst at the end.
410 // This EmptyAsm is required to avoid callback merge.
411 IRB.CreateCall(EmptyAsm);
Kostya Serebryany3c7faae2012-01-06 18:09:21 +0000412 return Call;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000413}
414
Kostya Serebryany2735cf42012-07-16 17:12:07 +0000415Value *AddressSanitizer::createSlowPathCmp(IRBuilder<> &IRB, Value *AddrLong,
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000416 Value *ShadowValue,
417 uint32_t TypeSize) {
418 size_t Granularity = 1 << MappingScale;
419 // Addr & (Granularity - 1)
420 Value *LastAccessedByte = IRB.CreateAnd(
421 AddrLong, ConstantInt::get(IntptrTy, Granularity - 1));
422 // (Addr & (Granularity - 1)) + size - 1
423 if (TypeSize / 8 > 1)
424 LastAccessedByte = IRB.CreateAdd(
425 LastAccessedByte, ConstantInt::get(IntptrTy, TypeSize / 8 - 1));
426 // (uint8_t) ((Addr & (Granularity-1)) + size - 1)
427 LastAccessedByte = IRB.CreateIntCast(
Kostya Serebryany6e2d5062012-08-15 08:58:58 +0000428 LastAccessedByte, ShadowValue->getType(), false);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000429 // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue
430 return IRB.CreateICmpSGE(LastAccessedByte, ShadowValue);
431}
432
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000433void AddressSanitizer::instrumentAddress(Instruction *OrigIns,
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000434 IRBuilder<> &IRB, Value *Addr,
435 uint32_t TypeSize, bool IsWrite) {
436 Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy);
437
438 Type *ShadowTy = IntegerType::get(
439 *C, std::max(8U, TypeSize >> MappingScale));
440 Type *ShadowPtrTy = PointerType::get(ShadowTy, 0);
441 Value *ShadowPtr = memToShadow(AddrLong, IRB);
442 Value *CmpVal = Constant::getNullValue(ShadowTy);
443 Value *ShadowValue = IRB.CreateLoad(
444 IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy));
445
446 Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal);
Kostya Serebryany11c2a472012-08-13 14:08:46 +0000447 size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000448 size_t Granularity = 1 << MappingScale;
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000449 TerminatorInst *CrashTerm = 0;
450
Kostya Serebryany6e2d5062012-08-15 08:58:58 +0000451 if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) {
Evgeniy Stepanov4a2dec02012-10-19 10:48:31 +0000452 TerminatorInst *CheckTerm =
453 SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), false);
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000454 assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional());
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000455 BasicBlock *NextBB = CheckTerm->getSuccessor(0);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000456 IRB.SetInsertPoint(CheckTerm);
457 Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000458 BasicBlock *CrashBlock =
459 BasicBlock::Create(*C, "", NextBB->getParent(), NextBB);
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000460 CrashTerm = new UnreachableInst(*C, CrashBlock);
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000461 BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2);
462 ReplaceInstWithInst(CheckTerm, NewTerm);
Kostya Serebryanyc0ed3e52012-07-16 16:15:40 +0000463 } else {
Evgeniy Stepanov4a2dec02012-10-19 10:48:31 +0000464 CrashTerm = SplitBlockAndInsertIfThen(cast<Instruction>(Cmp), true);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000465 }
Kostya Serebryanyebd64542012-08-14 14:04:51 +0000466
467 Instruction *Crash =
468 generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex);
469 Crash->setDebugLoc(OrigIns->getDebugLoc());
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000470}
471
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000472void AddressSanitizer::createInitializerPoisonCalls(Module &M,
473 Value *FirstAddr,
474 Value *LastAddr) {
475 // We do all of our poisoning and unpoisoning within _GLOBAL__I_a.
476 Function *GlobalInit = M.getFunction("_GLOBAL__I_a");
477 // If that function is not present, this TU contains no globals, or they have
478 // all been optimized away
479 if (!GlobalInit)
480 return;
481
482 // Set up the arguments to our poison/unpoison functions.
483 IRBuilder<> IRB(GlobalInit->begin()->getFirstInsertionPt());
484
485 // Declare our poisoning and unpoisoning functions.
486 Function *AsanPoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
487 kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
488 AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
489 Function *AsanUnpoisonGlobals = checkInterfaceFunction(M.getOrInsertFunction(
490 kAsanUnpoisonGlobalsName, IRB.getVoidTy(), NULL));
491 AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
492
493 // Add a call to poison all external globals before the given function starts.
494 IRB.CreateCall2(AsanPoisonGlobals, FirstAddr, LastAddr);
495
496 // Add calls to unpoison all globals before each return instruction.
497 for (Function::iterator I = GlobalInit->begin(), E = GlobalInit->end();
498 I != E; ++I) {
499 if (ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator())) {
500 CallInst::Create(AsanUnpoisonGlobals, "", RI);
501 }
502 }
503}
504
505bool AddressSanitizer::ShouldInstrumentGlobal(GlobalVariable *G) {
506 Type *Ty = cast<PointerType>(G->getType())->getElementType();
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000507 DEBUG(dbgs() << "GLOBAL: " << *G << "\n");
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000508
Kostya Serebryany59a4a472012-09-05 07:29:56 +0000509 if (BL->isIn(*G)) return false;
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000510 if (!Ty->isSized()) return false;
511 if (!G->hasInitializer()) return false;
Alexey Samsonov9ce84c12012-11-02 12:20:34 +0000512 if (GlobalsCreatedByAsan.count(G)) return false; // Our own global.
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000513 // Touch only those globals that will not be defined in other modules.
514 // Don't handle ODR type linkages since other modules may be built w/o asan.
515 if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
516 G->getLinkage() != GlobalVariable::PrivateLinkage &&
517 G->getLinkage() != GlobalVariable::InternalLinkage)
518 return false;
519 // Two problems with thread-locals:
520 // - The address of the main thread's copy can't be computed at link-time.
521 // - Need to poison all copies, not just the main thread's one.
522 if (G->isThreadLocal())
523 return false;
524 // For now, just ignore this Alloca if the alignment is large.
525 if (G->getAlignment() > RedzoneSize) return false;
526
527 // Ignore all the globals with the names starting with "\01L_OBJC_".
528 // Many of those are put into the .cstring section. The linker compresses
529 // that section by removing the spare \0s after the string terminator, so
530 // our redzones get broken.
531 if ((G->getName().find("\01L_OBJC_") == 0) ||
532 (G->getName().find("\01l_OBJC_") == 0)) {
533 DEBUG(dbgs() << "Ignoring \\01L_OBJC_* global: " << *G);
534 return false;
535 }
536
537 if (G->hasSection()) {
538 StringRef Section(G->getSection());
539 // Ignore the globals from the __OBJC section. The ObjC runtime assumes
540 // those conform to /usr/lib/objc/runtime.h, so we can't add redzones to
541 // them.
542 if ((Section.find("__OBJC,") == 0) ||
543 (Section.find("__DATA, __objc_") == 0)) {
544 DEBUG(dbgs() << "Ignoring ObjC runtime global: " << *G);
545 return false;
546 }
547 // See http://code.google.com/p/address-sanitizer/issues/detail?id=32
548 // Constant CFString instances are compiled in the following way:
549 // -- the string buffer is emitted into
550 // __TEXT,__cstring,cstring_literals
551 // -- the constant NSConstantString structure referencing that buffer
552 // is placed into __DATA,__cfstring
553 // Therefore there's no point in placing redzones into __DATA,__cfstring.
554 // Moreover, it causes the linker to crash on OS X 10.7
555 if (Section.find("__DATA,__cfstring") == 0) {
556 DEBUG(dbgs() << "Ignoring CFString: " << *G);
557 return false;
558 }
559 }
560
561 return true;
562}
563
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000564// This function replaces all global variables with new variables that have
565// trailing redzones. It also creates a function that poisons
566// redzones and inserts this function into llvm.global_ctors.
567bool AddressSanitizer::insertGlobalRedzones(Module &M) {
568 SmallVector<GlobalVariable *, 16> GlobalsToChange;
569
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000570 for (Module::GlobalListType::iterator G = M.global_begin(),
571 E = M.global_end(); G != E; ++G) {
572 if (ShouldInstrumentGlobal(G))
573 GlobalsToChange.push_back(G);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000574 }
575
576 size_t n = GlobalsToChange.size();
577 if (n == 0) return false;
578
579 // A global is described by a structure
580 // size_t beg;
581 // size_t size;
582 // size_t size_with_redzone;
583 // const char *name;
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000584 // size_t has_dynamic_init;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000585 // We initialize an array of such structures and pass it to a run-time call.
586 StructType *GlobalStructTy = StructType::get(IntptrTy, IntptrTy,
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000587 IntptrTy, IntptrTy,
588 IntptrTy, NULL);
589 SmallVector<Constant *, 16> Initializers(n), DynamicInit;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000590
591 IRBuilder<> IRB(CtorInsertBefore);
592
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000593 if (ClInitializers)
594 FindDynamicInitializers(M);
595
596 // The addresses of the first and last dynamically initialized globals in
597 // this TU. Used in initialization order checking.
598 Value *FirstDynamic = 0, *LastDynamic = 0;
599
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000600 for (size_t i = 0; i < n; i++) {
601 GlobalVariable *G = GlobalsToChange[i];
602 PointerType *PtrTy = cast<PointerType>(G->getType());
603 Type *Ty = PtrTy->getElementType();
Kostya Serebryany208a4ff2012-03-21 15:28:50 +0000604 uint64_t SizeInBytes = TD->getTypeAllocSize(Ty);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000605 uint64_t RightRedzoneSize = RedzoneSize +
606 (RedzoneSize - (SizeInBytes % RedzoneSize));
607 Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000608 // Determine whether this global should be poisoned in initialization.
609 bool GlobalHasDynamicInitializer = HasDynamicInitializer(G);
Kostya Serebryany59a4a472012-09-05 07:29:56 +0000610 // Don't check initialization order if this global is blacklisted.
Kostya Serebryany7dadac62012-09-05 09:00:18 +0000611 GlobalHasDynamicInitializer &= !BL->isInInit(*G);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000612
613 StructType *NewTy = StructType::get(Ty, RightRedZoneTy, NULL);
614 Constant *NewInitializer = ConstantStruct::get(
615 NewTy, G->getInitializer(),
616 Constant::getNullValue(RightRedZoneTy), NULL);
617
Kostya Serebryanya4b2b1d2011-12-15 22:55:55 +0000618 SmallString<2048> DescriptionOfGlobal = G->getName();
619 DescriptionOfGlobal += " (";
620 DescriptionOfGlobal += M.getModuleIdentifier();
621 DescriptionOfGlobal += ")";
622 GlobalVariable *Name = createPrivateGlobalForString(M, DescriptionOfGlobal);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000623
624 // Create a new global variable with enough space for a redzone.
625 GlobalVariable *NewGlobal = new GlobalVariable(
626 M, NewTy, G->isConstant(), G->getLinkage(),
Hans Wennborgce718ff2012-06-23 11:37:03 +0000627 NewInitializer, "", G, G->getThreadLocalMode());
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000628 NewGlobal->copyAttributesFrom(G);
629 NewGlobal->setAlignment(RedzoneSize);
630
631 Value *Indices2[2];
632 Indices2[0] = IRB.getInt32(0);
633 Indices2[1] = IRB.getInt32(0);
634
635 G->replaceAllUsesWith(
Kostya Serebryanyf1639ab2012-01-28 04:27:16 +0000636 ConstantExpr::getGetElementPtr(NewGlobal, Indices2, true));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000637 NewGlobal->takeName(G);
638 G->eraseFromParent();
639
640 Initializers[i] = ConstantStruct::get(
641 GlobalStructTy,
642 ConstantExpr::getPointerCast(NewGlobal, IntptrTy),
643 ConstantInt::get(IntptrTy, SizeInBytes),
644 ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
645 ConstantExpr::getPointerCast(Name, IntptrTy),
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000646 ConstantInt::get(IntptrTy, GlobalHasDynamicInitializer),
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000647 NULL);
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000648
649 // Populate the first and last globals declared in this TU.
650 if (ClInitializers && GlobalHasDynamicInitializer) {
651 LastDynamic = ConstantExpr::getPointerCast(NewGlobal, IntptrTy);
652 if (FirstDynamic == 0)
653 FirstDynamic = LastDynamic;
654 }
655
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000656 DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000657 }
658
659 ArrayType *ArrayOfGlobalStructTy = ArrayType::get(GlobalStructTy, n);
660 GlobalVariable *AllGlobals = new GlobalVariable(
661 M, ArrayOfGlobalStructTy, false, GlobalVariable::PrivateLinkage,
662 ConstantArray::get(ArrayOfGlobalStructTy, Initializers), "");
663
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000664 // Create calls for poisoning before initializers run and unpoisoning after.
665 if (ClInitializers && FirstDynamic && LastDynamic)
666 createInitializerPoisonCalls(M, FirstDynamic, LastDynamic);
667
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000668 Function *AsanRegisterGlobals = checkInterfaceFunction(M.getOrInsertFunction(
Kostya Serebryany9b9f87a2012-08-21 08:24:25 +0000669 kAsanRegisterGlobalsName, IRB.getVoidTy(),
670 IntptrTy, IntptrTy, NULL));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000671 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
672
673 IRB.CreateCall2(AsanRegisterGlobals,
674 IRB.CreatePointerCast(AllGlobals, IntptrTy),
675 ConstantInt::get(IntptrTy, n));
676
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000677 // We also need to unregister globals at the end, e.g. when a shared library
678 // gets closed.
679 Function *AsanDtorFunction = Function::Create(
680 FunctionType::get(Type::getVoidTy(*C), false),
681 GlobalValue::InternalLinkage, kAsanModuleDtorName, &M);
682 BasicBlock *AsanDtorBB = BasicBlock::Create(*C, "", AsanDtorFunction);
683 IRBuilder<> IRB_Dtor(ReturnInst::Create(*C, AsanDtorBB));
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000684 Function *AsanUnregisterGlobals =
685 checkInterfaceFunction(M.getOrInsertFunction(
686 kAsanUnregisterGlobalsName,
687 IRB.getVoidTy(), IntptrTy, IntptrTy, NULL));
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000688 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
689
690 IRB_Dtor.CreateCall2(AsanUnregisterGlobals,
691 IRB.CreatePointerCast(AllGlobals, IntptrTy),
692 ConstantInt::get(IntptrTy, n));
693 appendToGlobalDtors(M, AsanDtorFunction, kAsanCtorAndCtorPriority);
694
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000695 DEBUG(dbgs() << M);
696 return true;
697}
698
699// virtual
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000700bool AddressSanitizer::doInitialization(Module &M) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000701 // Initialize the private fields. No one has accessed them before.
Micah Villmow3574eca2012-10-08 16:38:25 +0000702 TD = getAnalysisIfAvailable<DataLayout>();
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000703
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000704 if (!TD)
705 return false;
Kostya Serebryanyb5b86d22012-08-24 16:44:47 +0000706 BL.reset(new BlackList(ClBlackListFile));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000707
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000708 C = &(M.getContext());
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000709 LongSize = TD->getPointerSizeInBits();
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000710 IntptrTy = Type::getIntNTy(*C, LongSize);
711 IntptrPtrTy = PointerType::get(IntptrTy, 0);
712
713 AsanCtorFunction = Function::Create(
714 FunctionType::get(Type::getVoidTy(*C), false),
715 GlobalValue::InternalLinkage, kAsanModuleCtorName, &M);
716 BasicBlock *AsanCtorBB = BasicBlock::Create(*C, "", AsanCtorFunction);
717 CtorInsertBefore = ReturnInst::Create(*C, AsanCtorBB);
718
719 // call __asan_init in the module ctor.
720 IRBuilder<> IRB(CtorInsertBefore);
Alexander Potapenko55cabae2012-04-23 10:47:31 +0000721 AsanInitFunction = checkInterfaceFunction(
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000722 M.getOrInsertFunction(kAsanInitName, IRB.getVoidTy(), NULL));
723 AsanInitFunction->setLinkage(Function::ExternalLinkage);
724 IRB.CreateCall(AsanInitFunction);
725
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000726 // Create __asan_report* callbacks.
727 for (size_t AccessIsWrite = 0; AccessIsWrite <= 1; AccessIsWrite++) {
728 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
729 AccessSizeIndex++) {
730 // IsWrite and TypeSize are encoded in the function name.
731 std::string FunctionName = std::string(kAsanReportErrorTemplate) +
732 (AccessIsWrite ? "store" : "load") + itostr(1 << AccessSizeIndex);
Kostya Serebryany4f0c6962012-07-17 11:04:12 +0000733 // If we are merging crash callbacks, they have two parameters.
Kostya Serebryany7846c1c2012-11-07 12:42:18 +0000734 AsanErrorCallback[AccessIsWrite][AccessSizeIndex] =
735 checkInterfaceFunction(M.getOrInsertFunction(
736 FunctionName, IRB.getVoidTy(), IntptrTy, NULL));
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000737 }
738 }
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000739
740 AsanStackMallocFunc = checkInterfaceFunction(M.getOrInsertFunction(
741 kAsanStackMallocName, IntptrTy, IntptrTy, IntptrTy, NULL));
742 AsanStackFreeFunc = checkInterfaceFunction(M.getOrInsertFunction(
743 kAsanStackFreeName, IRB.getVoidTy(),
744 IntptrTy, IntptrTy, IntptrTy, NULL));
745 AsanHandleNoReturnFunc = checkInterfaceFunction(M.getOrInsertFunction(
746 kAsanHandleNoReturnName, IRB.getVoidTy(), NULL));
747
Kostya Serebryanyf7b08222012-07-20 09:54:50 +0000748 // We insert an empty inline asm after __asan_report* to avoid callback merge.
749 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
750 StringRef(""), StringRef(""),
751 /*hasSideEffects=*/true);
Kostya Serebryany9db5b5f2012-07-16 14:09:42 +0000752
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +0000753 llvm::Triple targetTriple(M.getTargetTriple());
Logan Chien43bf7092012-09-02 09:29:46 +0000754 bool isAndroid = targetTriple.getEnvironment() == llvm::Triple::Android;
Evgeniy Stepanov06fdbaa2012-05-23 11:52:12 +0000755
756 MappingOffset = isAndroid ? kDefaultShadowOffsetAndroid :
757 (LongSize == 32 ? kDefaultShadowOffset32 : kDefaultShadowOffset64);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000758 if (ClMappingOffsetLog >= 0) {
759 if (ClMappingOffsetLog == 0) {
760 // special case
761 MappingOffset = 0;
762 } else {
763 MappingOffset = 1ULL << ClMappingOffsetLog;
764 }
765 }
766 MappingScale = kDefaultShadowScale;
767 if (ClMappingScale) {
768 MappingScale = ClMappingScale;
769 }
770 // Redzone used for stack and globals is at least 32 bytes.
771 // For scales 6 and 7, the redzone has to be 64 and 128 bytes respectively.
772 RedzoneSize = std::max(32, (int)(1 << MappingScale));
773
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000774
Kostya Serebryany8c0134a2012-03-19 16:40:35 +0000775 if (ClMappingOffsetLog >= 0) {
776 // Tell the run-time the current values of mapping offset and scale.
777 GlobalValue *asan_mapping_offset =
778 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
779 ConstantInt::get(IntptrTy, MappingOffset),
780 kAsanMappingOffsetName);
781 // Read the global, otherwise it may be optimized away.
782 IRB.CreateLoad(asan_mapping_offset, true);
783 }
784 if (ClMappingScale) {
785 GlobalValue *asan_mapping_scale =
786 new GlobalVariable(M, IntptrTy, true, GlobalValue::LinkOnceODRLinkage,
787 ConstantInt::get(IntptrTy, MappingScale),
788 kAsanMappingScaleName);
789 // Read the global, otherwise it may be optimized away.
790 IRB.CreateLoad(asan_mapping_scale, true);
791 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000792
Kostya Serebryany7bcfc992011-12-15 21:59:03 +0000793 appendToGlobalCtors(M, AsanCtorFunction, kAsanCtorAndCtorPriority);
Kostya Serebryany9b027412011-12-12 18:01:46 +0000794
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000795 return true;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000796}
797
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000798bool AddressSanitizer::doFinalization(Module &M) {
799 // We transform the globals at the very end so that the optimization analysis
800 // works on the original globals.
801 if (ClGlobals)
802 return insertGlobalRedzones(M);
803 return false;
804}
805
806
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000807bool AddressSanitizer::maybeInsertAsanInitAtFunctionEntry(Function &F) {
808 // For each NSObject descendant having a +load method, this method is invoked
809 // by the ObjC runtime before any of the static constructors is called.
810 // Therefore we need to instrument such methods with a call to __asan_init
811 // at the beginning in order to initialize our runtime before any access to
812 // the shadow memory.
813 // We cannot just ignore these methods, because they may call other
814 // instrumented functions.
815 if (F.getName().find(" load]") != std::string::npos) {
816 IRBuilder<> IRB(F.begin()->begin());
817 IRB.CreateCall(AsanInitFunction);
818 return true;
819 }
820 return false;
821}
822
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000823bool AddressSanitizer::runOnFunction(Function &F) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000824 if (BL->isIn(F)) return false;
825 if (&F == AsanCtorFunction) return false;
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000826 DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
Kostya Serebryanya1a8a322012-01-30 23:50:10 +0000827
828 // If needed, insert __asan_init before checking for AddressSafety attr.
829 maybeInsertAsanInitAtFunctionEntry(F);
830
Bill Wendling67658342012-10-09 07:45:08 +0000831 if (!F.getFnAttributes().hasAttribute(Attributes::AddressSafety))
832 return false;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000833
834 if (!ClDebugFunc.empty() && ClDebugFunc != F.getName())
835 return false;
Bill Wendling67658342012-10-09 07:45:08 +0000836
837 // We want to instrument every address only once per basic block (unless there
838 // are calls between uses).
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000839 SmallSet<Value*, 16> TempsToInstrument;
840 SmallVector<Instruction*, 16> ToInstrument;
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000841 SmallVector<Instruction*, 8> NoReturnCalls;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000842 bool IsWrite;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000843
844 // Fill the set of memory operations to instrument.
845 for (Function::iterator FI = F.begin(), FE = F.end();
846 FI != FE; ++FI) {
847 TempsToInstrument.clear();
Kostya Serebryany324cbb82012-06-28 09:34:41 +0000848 int NumInsnsPerBB = 0;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000849 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end();
850 BI != BE; ++BI) {
Kostya Serebryanybcb55ce2012-01-11 18:15:23 +0000851 if (LooksLikeCodeInBug11395(BI)) return false;
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000852 if (Value *Addr = isInterestingMemoryAccess(BI, &IsWrite)) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000853 if (ClOpt && ClOptSameTemp) {
854 if (!TempsToInstrument.insert(Addr))
855 continue; // We've seen this temp in the current BB.
856 }
857 } else if (isa<MemIntrinsic>(BI) && ClMemIntrin) {
858 // ok, take it.
859 } else {
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000860 if (CallInst *CI = dyn_cast<CallInst>(BI)) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000861 // A call inside BB.
862 TempsToInstrument.clear();
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000863 if (CI->doesNotReturn()) {
864 NoReturnCalls.push_back(CI);
865 }
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000866 }
867 continue;
868 }
869 ToInstrument.push_back(BI);
Kostya Serebryany324cbb82012-06-28 09:34:41 +0000870 NumInsnsPerBB++;
871 if (NumInsnsPerBB >= ClMaxInsnsToInstrumentPerBB)
872 break;
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000873 }
874 }
875
876 // Instrument.
877 int NumInstrumented = 0;
878 for (size_t i = 0, n = ToInstrument.size(); i != n; i++) {
879 Instruction *Inst = ToInstrument[i];
880 if (ClDebugMin < 0 || ClDebugMax < 0 ||
881 (NumInstrumented >= ClDebugMin && NumInstrumented <= ClDebugMax)) {
Kostya Serebryanye6cf2e02012-05-30 09:04:06 +0000882 if (isInterestingMemoryAccess(Inst, &IsWrite))
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000883 instrumentMop(Inst);
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000884 else
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000885 instrumentMemIntrinsic(cast<MemIntrinsic>(Inst));
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000886 }
887 NumInstrumented++;
888 }
889
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000890 bool ChangedStack = poisonStackInFunction(F);
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000891
892 // We must unpoison the stack before every NoReturn call (throw, _exit, etc).
893 // See e.g. http://code.google.com/p/address-sanitizer/issues/detail?id=37
894 for (size_t i = 0, n = NoReturnCalls.size(); i != n; i++) {
895 Instruction *CI = NoReturnCalls[i];
896 IRBuilder<> IRB(CI);
Kostya Serebryanyee4edec2012-10-15 14:20:06 +0000897 IRB.CreateCall(AsanHandleNoReturnFunc);
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000898 }
Kostya Serebryany324d96b2012-10-17 13:40:06 +0000899 DEBUG(dbgs() << "ASAN done instrumenting:\n" << F << "\n");
Kostya Serebryany95e3cf42012-02-08 21:36:17 +0000900
901 return NumInstrumented > 0 || ChangedStack || !NoReturnCalls.empty();
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000902}
903
904static uint64_t ValueForPoison(uint64_t PoisonByte, size_t ShadowRedzoneSize) {
905 if (ShadowRedzoneSize == 1) return PoisonByte;
906 if (ShadowRedzoneSize == 2) return (PoisonByte << 8) + PoisonByte;
907 if (ShadowRedzoneSize == 4)
908 return (PoisonByte << 24) + (PoisonByte << 16) +
909 (PoisonByte << 8) + (PoisonByte);
Craig Topper85814382012-02-07 05:05:23 +0000910 llvm_unreachable("ShadowRedzoneSize is either 1, 2 or 4");
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000911}
912
913static void PoisonShadowPartialRightRedzone(uint8_t *Shadow,
914 size_t Size,
915 size_t RedzoneSize,
916 size_t ShadowGranularity,
917 uint8_t Magic) {
918 for (size_t i = 0; i < RedzoneSize;
919 i+= ShadowGranularity, Shadow++) {
920 if (i + ShadowGranularity <= Size) {
921 *Shadow = 0; // fully addressable
922 } else if (i >= Size) {
923 *Shadow = Magic; // unaddressable
924 } else {
925 *Shadow = Size - i; // first Size-i bytes are addressable
926 }
927 }
928}
929
930void AddressSanitizer::PoisonStack(const ArrayRef<AllocaInst*> &AllocaVec,
931 IRBuilder<> IRB,
932 Value *ShadowBase, bool DoPoison) {
933 size_t ShadowRZSize = RedzoneSize >> MappingScale;
934 assert(ShadowRZSize >= 1 && ShadowRZSize <= 4);
935 Type *RZTy = Type::getIntNTy(*C, ShadowRZSize * 8);
936 Type *RZPtrTy = PointerType::get(RZTy, 0);
937
938 Value *PoisonLeft = ConstantInt::get(RZTy,
939 ValueForPoison(DoPoison ? kAsanStackLeftRedzoneMagic : 0LL, ShadowRZSize));
940 Value *PoisonMid = ConstantInt::get(RZTy,
941 ValueForPoison(DoPoison ? kAsanStackMidRedzoneMagic : 0LL, ShadowRZSize));
942 Value *PoisonRight = ConstantInt::get(RZTy,
943 ValueForPoison(DoPoison ? kAsanStackRightRedzoneMagic : 0LL, ShadowRZSize));
944
945 // poison the first red zone.
946 IRB.CreateStore(PoisonLeft, IRB.CreateIntToPtr(ShadowBase, RZPtrTy));
947
948 // poison all other red zones.
949 uint64_t Pos = RedzoneSize;
950 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
951 AllocaInst *AI = AllocaVec[i];
952 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
953 uint64_t AlignedSize = getAlignedAllocaSize(AI);
954 assert(AlignedSize - SizeInBytes < RedzoneSize);
955 Value *Ptr = NULL;
956
957 Pos += AlignedSize;
958
959 assert(ShadowBase->getType() == IntptrTy);
960 if (SizeInBytes < AlignedSize) {
961 // Poison the partial redzone at right
962 Ptr = IRB.CreateAdd(
963 ShadowBase, ConstantInt::get(IntptrTy,
964 (Pos >> MappingScale) - ShadowRZSize));
965 size_t AddressableBytes = RedzoneSize - (AlignedSize - SizeInBytes);
966 uint32_t Poison = 0;
967 if (DoPoison) {
968 PoisonShadowPartialRightRedzone((uint8_t*)&Poison, AddressableBytes,
969 RedzoneSize,
970 1ULL << MappingScale,
971 kAsanStackPartialRedzoneMagic);
972 }
973 Value *PartialPoison = ConstantInt::get(RZTy, Poison);
974 IRB.CreateStore(PartialPoison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
975 }
976
977 // Poison the full redzone at right.
978 Ptr = IRB.CreateAdd(ShadowBase,
979 ConstantInt::get(IntptrTy, Pos >> MappingScale));
980 Value *Poison = i == AllocaVec.size() - 1 ? PoisonRight : PoisonMid;
981 IRB.CreateStore(Poison, IRB.CreateIntToPtr(Ptr, RZPtrTy));
982
983 Pos += RedzoneSize;
984 }
985}
986
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +0000987// Workaround for bug 11395: we don't want to instrument stack in functions
988// with large assembly blobs (32-bit only), otherwise reg alloc may crash.
Kostya Serebryanyd2703de2011-11-23 02:10:54 +0000989// FIXME: remove once the bug 11395 is fixed.
Kostya Serebryany5a3a9c92011-11-18 01:41:06 +0000990bool AddressSanitizer::LooksLikeCodeInBug11395(Instruction *I) {
991 if (LongSize != 32) return false;
992 CallInst *CI = dyn_cast<CallInst>(I);
993 if (!CI || !CI->isInlineAsm()) return false;
994 if (CI->getNumArgOperands() <= 5) return false;
995 // We have inline assembly with quite a few arguments.
996 return true;
997}
998
Kostya Serebryany800e03f2011-11-16 01:35:23 +0000999// Find all static Alloca instructions and put
1000// poisoned red zones around all of them.
1001// Then unpoison everything back before the function returns.
1002//
1003// Stack poisoning does not play well with exception handling.
1004// When an exception is thrown, we essentially bypass the code
1005// that unpoisones the stack. This is why the run-time library has
1006// to intercept __cxa_throw (as well as longjmp, etc) and unpoison the entire
1007// stack in the interceptor. This however does not work inside the
1008// actual function which catches the exception. Most likely because the
1009// compiler hoists the load of the shadow value somewhere too high.
1010// This causes asan to report a non-existing bug on 453.povray.
1011// It sounds like an LLVM bug.
Kostya Serebryanyee4edec2012-10-15 14:20:06 +00001012bool AddressSanitizer::poisonStackInFunction(Function &F) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001013 if (!ClStack) return false;
1014 SmallVector<AllocaInst*, 16> AllocaVec;
1015 SmallVector<Instruction*, 8> RetVec;
1016 uint64_t TotalSize = 0;
1017
1018 // Filter out Alloca instructions we want (and can) handle.
1019 // Collect Ret instructions.
1020 for (Function::iterator FI = F.begin(), FE = F.end();
1021 FI != FE; ++FI) {
1022 BasicBlock &BB = *FI;
1023 for (BasicBlock::iterator BI = BB.begin(), BE = BB.end();
1024 BI != BE; ++BI) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001025 if (isa<ReturnInst>(BI)) {
1026 RetVec.push_back(BI);
1027 continue;
1028 }
1029
1030 AllocaInst *AI = dyn_cast<AllocaInst>(BI);
1031 if (!AI) continue;
1032 if (AI->isArrayAllocation()) continue;
1033 if (!AI->isStaticAlloca()) continue;
1034 if (!AI->getAllocatedType()->isSized()) continue;
1035 if (AI->getAlignment() > RedzoneSize) continue;
1036 AllocaVec.push_back(AI);
1037 uint64_t AlignedSize = getAlignedAllocaSize(AI);
1038 TotalSize += AlignedSize;
1039 }
1040 }
1041
1042 if (AllocaVec.empty()) return false;
1043
1044 uint64_t LocalStackSize = TotalSize + (AllocaVec.size() + 1) * RedzoneSize;
1045
1046 bool DoStackMalloc = ClUseAfterReturn
1047 && LocalStackSize <= kMaxStackMallocSize;
1048
1049 Instruction *InsBefore = AllocaVec[0];
1050 IRBuilder<> IRB(InsBefore);
1051
1052
1053 Type *ByteArrayTy = ArrayType::get(IRB.getInt8Ty(), LocalStackSize);
1054 AllocaInst *MyAlloca =
1055 new AllocaInst(ByteArrayTy, "MyAlloca", InsBefore);
1056 MyAlloca->setAlignment(RedzoneSize);
1057 assert(MyAlloca->isStaticAlloca());
1058 Value *OrigStackBase = IRB.CreatePointerCast(MyAlloca, IntptrTy);
1059 Value *LocalStackBase = OrigStackBase;
1060
1061 if (DoStackMalloc) {
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001062 LocalStackBase = IRB.CreateCall2(AsanStackMallocFunc,
1063 ConstantInt::get(IntptrTy, LocalStackSize), OrigStackBase);
1064 }
1065
1066 // This string will be parsed by the run-time (DescribeStackAddress).
1067 SmallString<2048> StackDescriptionStorage;
1068 raw_svector_ostream StackDescription(StackDescriptionStorage);
1069 StackDescription << F.getName() << " " << AllocaVec.size() << " ";
1070
1071 uint64_t Pos = RedzoneSize;
1072 // Replace Alloca instructions with base+offset.
1073 for (size_t i = 0, n = AllocaVec.size(); i < n; i++) {
1074 AllocaInst *AI = AllocaVec[i];
1075 uint64_t SizeInBytes = getAllocaSizeInBytes(AI);
1076 StringRef Name = AI->getName();
1077 StackDescription << Pos << " " << SizeInBytes << " "
1078 << Name.size() << " " << Name << " ";
1079 uint64_t AlignedSize = getAlignedAllocaSize(AI);
1080 assert((AlignedSize % RedzoneSize) == 0);
1081 AI->replaceAllUsesWith(
1082 IRB.CreateIntToPtr(
1083 IRB.CreateAdd(LocalStackBase, ConstantInt::get(IntptrTy, Pos)),
1084 AI->getType()));
1085 Pos += AlignedSize + RedzoneSize;
1086 }
1087 assert(Pos == LocalStackSize);
1088
1089 // Write the Magic value and the frame description constant to the redzone.
1090 Value *BasePlus0 = IRB.CreateIntToPtr(LocalStackBase, IntptrPtrTy);
1091 IRB.CreateStore(ConstantInt::get(IntptrTy, kCurrentStackFrameMagic),
1092 BasePlus0);
1093 Value *BasePlus1 = IRB.CreateAdd(LocalStackBase,
1094 ConstantInt::get(IntptrTy, LongSize/8));
1095 BasePlus1 = IRB.CreateIntToPtr(BasePlus1, IntptrPtrTy);
Alexey Samsonov9ce84c12012-11-02 12:20:34 +00001096 GlobalVariable *StackDescriptionGlobal =
Kostya Serebryanya5f54f12012-11-01 13:42:40 +00001097 createPrivateGlobalForString(*F.getParent(), StackDescription.str());
1098 GlobalsCreatedByAsan.insert(StackDescriptionGlobal);
1099 Value *Description = IRB.CreatePointerCast(StackDescriptionGlobal, IntptrTy);
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001100 IRB.CreateStore(Description, BasePlus1);
1101
1102 // Poison the stack redzones at the entry.
1103 Value *ShadowBase = memToShadow(LocalStackBase, IRB);
1104 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRB, ShadowBase, true);
1105
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001106 // Unpoison the stack before all ret instructions.
1107 for (size_t i = 0, n = RetVec.size(); i < n; i++) {
1108 Instruction *Ret = RetVec[i];
1109 IRBuilder<> IRBRet(Ret);
1110
1111 // Mark the current frame as retired.
1112 IRBRet.CreateStore(ConstantInt::get(IntptrTy, kRetiredStackFrameMagic),
1113 BasePlus0);
1114 // Unpoison the stack.
1115 PoisonStack(ArrayRef<AllocaInst*>(AllocaVec), IRBRet, ShadowBase, false);
1116
1117 if (DoStackMalloc) {
1118 IRBRet.CreateCall3(AsanStackFreeFunc, LocalStackBase,
1119 ConstantInt::get(IntptrTy, LocalStackSize),
1120 OrigStackBase);
1121 }
1122 }
1123
Kostya Serebryanybd0052a2012-10-19 06:20:53 +00001124 // We are done. Remove the old unused alloca instructions.
1125 for (size_t i = 0, n = AllocaVec.size(); i < n; i++)
1126 AllocaVec[i]->eraseFromParent();
1127
Kostya Serebryany800e03f2011-11-16 01:35:23 +00001128 if (ClDebugStack) {
1129 DEBUG(dbgs() << F);
1130 }
1131
1132 return true;
1133}