blob: 4025cfe7b9cc40d875bd6f42dd1f64eb5cd3d320 [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:02 +00001//===-- asan_allocator.h ----------------------------------------*- 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//
Kostya Serebryanyeef8bd42013-04-04 11:32:49 +000012// ASan-private header for asan_allocator2.cc.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000013//===----------------------------------------------------------------------===//
14
15#ifndef ASAN_ALLOCATOR_H
16#define ASAN_ALLOCATOR_H
17
18#include "asan_internal.h"
19#include "asan_interceptors.h"
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000020#include "sanitizer_common/sanitizer_list.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000021
22namespace __asan {
23
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000024enum AllocType {
25 FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
26 FROM_NEW = 2, // Memory block came from operator new.
27 FROM_NEW_BR = 3 // Memory block came from operator new [ ]
28};
29
Kostya Serebryany8d032042012-05-31 14:35:53 +000030static const uptr kNumberOfSizeClasses = 255;
Kostya Serebryany9d1eee92011-11-30 17:33:13 +000031struct AsanChunk;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000032
Kostya Serebryany61761f12013-01-28 08:05:47 +000033void InitializeAllocator();
34
Alexey Samsonov86614652012-09-18 07:38:10 +000035class AsanChunkView {
36 public:
37 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
38 bool IsValid() { return chunk_ != 0; }
39 uptr Beg(); // first byte of user memory.
40 uptr End(); // last byte of user memory.
41 uptr UsedSize(); // size requested by the user.
42 uptr AllocTid();
43 uptr FreeTid();
44 void GetAllocStack(StackTrace *stack);
45 void GetFreeStack(StackTrace *stack);
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000046 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000047 if (addr >= Beg() && (addr + access_size) <= End()) {
48 *offset = addr - Beg();
49 return true;
50 }
51 return false;
52 }
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000053 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
Alexander Potapenko602a09f2012-12-12 12:32:57 +000054 (void)access_size;
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000055 if (addr < Beg()) {
56 *offset = Beg() - addr;
57 return true;
58 }
59 return false;
60 }
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000061 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
Evgeniy Stepanov0b805cc2013-02-08 12:59:42 +000062 if (addr + access_size > End()) {
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000063 *offset = addr - End();
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000064 return true;
65 }
66 return false;
67 }
68
Alexey Samsonov86614652012-09-18 07:38:10 +000069 private:
70 AsanChunk *const chunk_;
71};
72
73AsanChunkView FindHeapChunkByAddress(uptr address);
74
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000075// List of AsanChunks with total size.
76class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
Kostya Serebryany019b76f2011-11-30 01:07:02 +000077 public:
78 explicit AsanChunkFifoList(LinkerInitialized) { }
79 AsanChunkFifoList() { clear(); }
80 void Push(AsanChunk *n);
81 void PushList(AsanChunkFifoList *q);
82 AsanChunk *Pop();
Kostya Serebryany8d032042012-05-31 14:35:53 +000083 uptr size() { return size_; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000084 void clear() {
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000085 IntrusiveList<AsanChunk>::clear();
Kostya Serebryany019b76f2011-11-30 01:07:02 +000086 size_ = 0;
87 }
88 private:
Kostya Serebryany8d032042012-05-31 14:35:53 +000089 uptr size_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000090};
91
92struct AsanThreadLocalMallocStorage {
93 explicit AsanThreadLocalMallocStorage(LinkerInitialized x)
Dmitry Vyukovdb0cf872013-01-11 08:07:43 +000094 { }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000095 AsanThreadLocalMallocStorage() {
Alexey Samsonove7254782012-02-08 13:45:31 +000096 CHECK(REAL(memset));
97 REAL(memset)(this, 0, sizeof(AsanThreadLocalMallocStorage));
Kostya Serebryany019b76f2011-11-30 01:07:02 +000098 }
99
Dmitry Vyukovdb0cf872013-01-11 08:07:43 +0000100 uptr quarantine_cache[16];
Dmitry Vyukov0d46b2b2013-01-15 10:45:18 +0000101 uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque.
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000102 void CommitBack();
103};
104
105// Fake stack frame contains local variables of one function.
106// This struct should fit into a stack redzone (32 bytes).
107struct FakeFrame {
Kostya Serebryany8d032042012-05-31 14:35:53 +0000108 uptr magic; // Modified by the instrumented code.
109 uptr descr; // Modified by the instrumented code.
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000110 FakeFrame *next;
Kostya Serebryany1d35d152012-05-31 15:02:07 +0000111 u64 real_stack : 48;
112 u64 size_minus_one : 16;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000113};
114
115struct FakeFrameFifo {
116 public:
117 void FifoPush(FakeFrame *node);
118 FakeFrame *FifoPop();
119 private:
120 FakeFrame *first_, *last_;
121};
122
123class FakeFrameLifo {
124 public:
125 void LifoPush(FakeFrame *node) {
126 node->next = top_;
127 top_ = node;
128 }
129 void LifoPop() {
130 CHECK(top_);
131 top_ = top_->next;
132 }
133 FakeFrame *top() { return top_; }
134 private:
135 FakeFrame *top_;
136};
137
138// For each thread we create a fake stack and place stack objects on this fake
139// stack instead of the real stack. The fake stack is not really a stack but
140// a fast malloc-like allocator so that when a function exits the fake stack
141// is not poped but remains there for quite some time until gets used again.
142// So, we poison the objects on the fake stack when function returns.
143// It helps us find use-after-return bugs.
144// We can not rely on __asan_stack_free being called on every function exit,
145// so we maintain a lifo list of all current fake frames and update it on every
146// call to __asan_stack_malloc.
147class FakeStack {
148 public:
149 FakeStack();
150 explicit FakeStack(LinkerInitialized) {}
Kostya Serebryany8d032042012-05-31 14:35:53 +0000151 void Init(uptr stack_size);
Kostya Serebryany72fde372011-12-09 01:49:31 +0000152 void StopUsingFakeStack() { alive_ = false; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000153 void Cleanup();
Kostya Serebryany8d032042012-05-31 14:35:53 +0000154 uptr AllocateStack(uptr size, uptr real_stack);
155 static void OnFree(uptr ptr, uptr size, uptr real_stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000156 // Return the bottom of the maped region.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000157 uptr AddrIsInFakeStack(uptr addr);
Alexander Potapenko0be25d52012-02-21 08:45:41 +0000158 bool StackSize() { return stack_size_; }
Alexey Samsonovc3a81192012-08-30 14:22:21 +0000159
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000160 private:
Kostya Serebryany8d032042012-05-31 14:35:53 +0000161 static const uptr kMinStackFrameSizeLog = 9; // Min frame is 512B.
162 static const uptr kMaxStackFrameSizeLog = 16; // Max stack frame is 64K.
163 static const uptr kMaxStackMallocSize = 1 << kMaxStackFrameSizeLog;
164 static const uptr kNumberOfSizeClasses =
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000165 kMaxStackFrameSizeLog - kMinStackFrameSizeLog + 1;
166
Kostya Serebryany8d032042012-05-31 14:35:53 +0000167 bool AddrIsInSizeClass(uptr addr, uptr size_class);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000168
169 // Each size class should be large enough to hold all frames.
Kostya Serebryany8d032042012-05-31 14:35:53 +0000170 uptr ClassMmapSize(uptr size_class);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000171
Kostya Serebryany8d032042012-05-31 14:35:53 +0000172 uptr ClassSize(uptr size_class) {
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000173 return 1UL << (size_class + kMinStackFrameSizeLog);
174 }
175
176 void DeallocateFrame(FakeFrame *fake_frame);
177
Kostya Serebryany8d032042012-05-31 14:35:53 +0000178 uptr ComputeSizeClass(uptr alloc_size);
179 void AllocateOneSizeClass(uptr size_class);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000180
Kostya Serebryany8d032042012-05-31 14:35:53 +0000181 uptr stack_size_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000182 bool alive_;
183
Kostya Serebryany8d032042012-05-31 14:35:53 +0000184 uptr allocated_size_classes_[kNumberOfSizeClasses];
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000185 FakeFrameFifo size_classes_[kNumberOfSizeClasses];
186 FakeFrameLifo call_stack_;
187};
188
Kostya Serebryany3674c6b2012-12-21 08:53:59 +0000189void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
190 AllocType alloc_type);
191void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000192
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000193void *asan_malloc(uptr size, StackTrace *stack);
194void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
195void *asan_realloc(void *p, uptr size, StackTrace *stack);
196void *asan_valloc(uptr size, StackTrace *stack);
197void *asan_pvalloc(uptr size, StackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000198
Kostya Serebryany8d032042012-05-31 14:35:53 +0000199int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000200 StackTrace *stack);
201uptr asan_malloc_usable_size(void *ptr, StackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000202
Kostya Serebryany8d032042012-05-31 14:35:53 +0000203uptr asan_mz_size(const void *ptr);
Alexey Samsonov209c5142012-01-17 06:39:10 +0000204void asan_mz_force_lock();
205void asan_mz_force_unlock();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000206
Kostya Serebryany4a42cf62012-12-27 14:09:19 +0000207void PrintInternalAllocatorStats();
208
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000209} // namespace __asan
210#endif // ASAN_ALLOCATOR_H