blob: 1c74fc145644fabc9a2d482919e03141310fe4d1 [file] [log] [blame]
Alexey Samsonove5f58952012-06-04 13:50:10 +00001//===-- asan_allocator.cc -------------------------------------------------===//
Kostya Serebryany1e172b42011-11-30 01:07:02 +00002//
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//
12// Implementation of ASan's memory allocator.
13// Evey piece of memory (AsanChunk) allocated by the allocator
14// has a left redzone of REDZONE bytes and
15// a right redzone such that the end of the chunk is aligned by REDZONE
16// (i.e. the right redzone is between 0 and REDZONE-1).
17// The left redzone is always poisoned.
18// The right redzone is poisoned on malloc, the body is poisoned on free.
19// Once freed, a chunk is moved to a quarantine (fifo list).
20// After quarantine, a chunk is returned to freelists.
21//
22// The left redzone contains ASan's internal data and the stack trace of
23// the malloc call.
24// Once freed, the body of the chunk contains the stack trace of the free call.
25//
26//===----------------------------------------------------------------------===//
27
28#include "asan_allocator.h"
29#include "asan_interceptors.h"
30#include "asan_interface.h"
31#include "asan_internal.h"
32#include "asan_lock.h"
33#include "asan_mapping.h"
34#include "asan_stats.h"
Alexey Samsonovf7c1d182012-08-09 08:15:46 +000035#include "asan_report.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000036#include "asan_thread.h"
37#include "asan_thread_registry.h"
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +000038#include "sanitizer_common/sanitizer_atomic.h"
Kostya Serebryany1e172b42011-11-30 01:07:02 +000039
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +000040#if defined(_WIN32) && !defined(__clang__)
Kostya Serebryany85822082012-01-30 20:55:02 +000041#include <intrin.h>
42#endif
43
Kostya Serebryany1e172b42011-11-30 01:07:02 +000044namespace __asan {
45
Alexey Samsonov63201b12012-07-23 09:11:58 +000046#define REDZONE ((uptr)(flags()->redzone))
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000047static const uptr kMinAllocSize = REDZONE * 2;
Kostya Serebryanyee392552012-05-31 15:02:07 +000048static const u64 kMaxAvailableRam = 128ULL << 30; // 128G
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000049static const uptr kMaxThreadLocalQuarantine = 1 << 20; // 1M
Evgeniy Stepanov788e1d72012-02-16 13:35:11 +000050
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000051static const uptr kMinMmapSize = (ASAN_LOW_MEMORY) ? 4UL << 17 : 4UL << 20;
52static const uptr kMaxSizeForThreadLocalFreeList =
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000053 (ASAN_LOW_MEMORY) ? 1 << 15 : 1 << 17;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000054
55// Size classes less than kMallocSizeClassStep are powers of two.
56// All other size classes are multiples of kMallocSizeClassStep.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000057static const uptr kMallocSizeClassStepLog = 26;
58static const uptr kMallocSizeClassStep = 1UL << kMallocSizeClassStepLog;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000059
Kostya Serebryany9aead372012-05-31 14:11:07 +000060static const uptr kMaxAllowedMallocSize =
Evgeniy Stepanov8ae44ac2012-02-27 13:07:29 +000061 (__WORDSIZE == 32) ? 3UL << 30 : 8UL << 30;
Kostya Serebryany1e172b42011-11-30 01:07:02 +000062
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000063static inline bool IsAligned(uptr a, uptr alignment) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000064 return (a & (alignment - 1)) == 0;
65}
66
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000067static inline uptr Log2(uptr x) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000068 CHECK(IsPowerOfTwo(x));
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +000069#if !defined(_WIN32) || defined(__clang__)
70 return __builtin_ctzl(x);
71#elif defined(_WIN64)
Alexander Potapenko6f045292012-01-27 15:15:04 +000072 unsigned long ret; // NOLINT
73 _BitScanForward64(&ret, x);
74 return ret;
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +000075#else
Alexander Potapenko6f045292012-01-27 15:15:04 +000076 unsigned long ret; // NOLINT
77 _BitScanForward(&ret, x);
78 return ret;
Alexander Potapenko6f045292012-01-27 15:15:04 +000079#endif
80}
81
Kostya Serebryany3f4c3872012-05-31 14:35:53 +000082static inline uptr RoundUpToPowerOfTwo(uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +000083 CHECK(size);
84 if (IsPowerOfTwo(size)) return size;
Alexander Potapenko6f045292012-01-27 15:15:04 +000085
Alexey Samsonovf927ddc2012-02-03 08:50:16 +000086 unsigned long up; // NOLINT
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +000087#if !defined(_WIN32) || defined(__clang__)
Alexey Samsonovf927ddc2012-02-03 08:50:16 +000088 up = __WORDSIZE - 1 - __builtin_clzl(size);
Timur Iskhodzhanov8c505ef2012-05-21 14:25:36 +000089#elif defined(_WIN64)
90 _BitScanReverse64(&up, size);
91#else
92 _BitScanReverse(&up, size);
Alexey Samsonovf927ddc2012-02-03 08:50:16 +000093#endif
94 CHECK(size < (1ULL << (up + 1)));
95 CHECK(size > (1ULL << up));
96 return 1UL << (up + 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000097}
98
Kostya Serebryanyee392552012-05-31 15:02:07 +000099static inline uptr SizeClassToSize(u8 size_class) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000100 CHECK(size_class < kNumberOfSizeClasses);
101 if (size_class <= kMallocSizeClassStepLog) {
102 return 1UL << size_class;
103 } else {
104 return (size_class - kMallocSizeClassStepLog) * kMallocSizeClassStep;
105 }
106}
107
Kostya Serebryanyee392552012-05-31 15:02:07 +0000108static inline u8 SizeToSizeClass(uptr size) {
109 u8 res = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000110 if (size <= kMallocSizeClassStep) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000111 uptr rounded = RoundUpToPowerOfTwo(size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000112 res = Log2(rounded);
113 } else {
114 res = ((size + kMallocSizeClassStep - 1) / kMallocSizeClassStep)
115 + kMallocSizeClassStepLog;
116 }
117 CHECK(res < kNumberOfSizeClasses);
118 CHECK(size <= SizeClassToSize(res));
119 return res;
120}
121
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000122// Given REDZONE bytes, we need to mark first size bytes
123// as addressable and the rest REDZONE-size bytes as unaddressable.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000124static void PoisonHeapPartialRightRedzone(uptr mem, uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000125 CHECK(size <= REDZONE);
126 CHECK(IsAligned(mem, REDZONE));
127 CHECK(IsPowerOfTwo(SHADOW_GRANULARITY));
128 CHECK(IsPowerOfTwo(REDZONE));
129 CHECK(REDZONE >= SHADOW_GRANULARITY);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000130 PoisonShadowPartialRightRedzone(mem, size, REDZONE,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000131 kAsanHeapRightRedzoneMagic);
132}
133
Kostya Serebryanyee392552012-05-31 15:02:07 +0000134static u8 *MmapNewPagesAndPoisonShadow(uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000135 CHECK(IsAligned(size, kPageSize));
Alexey Samsonova25b3462012-06-06 16:15:07 +0000136 u8 *res = (u8*)MmapOrDie(size, __FUNCTION__);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000137 PoisonShadow((uptr)res, size, kAsanHeapLeftRedzoneMagic);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000138 if (flags()->debug) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000139 Printf("ASAN_MMAP: [%p, %p)\n", res, res + size);
140 }
141 return res;
142}
143
144// Every chunk of memory allocated by this allocator can be in one of 3 states:
145// CHUNK_AVAILABLE: the chunk is in the free list and ready to be allocated.
146// CHUNK_ALLOCATED: the chunk is allocated and not yet freed.
147// CHUNK_QUARANTINE: the chunk was freed and put into quarantine zone.
148//
149// The pseudo state CHUNK_MEMALIGN is used to mark that the address is not
Kostya Serebryany16071602012-06-06 16:58:21 +0000150// the beginning of a AsanChunk (in which the actual chunk resides at
151// this - this->used_size).
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000152//
153// The magic numbers for the enum values are taken randomly.
154enum {
Kostya Serebryany7ebac952012-06-06 14:46:38 +0000155 CHUNK_AVAILABLE = 0x57,
156 CHUNK_ALLOCATED = 0x32,
157 CHUNK_QUARANTINE = 0x19,
Alexey Samsonovb4fefa72012-06-28 08:27:24 +0000158 CHUNK_MEMALIGN = 0xDC
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000159};
160
161struct ChunkBase {
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000162 // First 8 bytes.
Kostya Serebryanyf4a4d5a2012-06-06 15:30:55 +0000163 uptr chunk_state : 8;
Kostya Serebryanyf4a4d5a2012-06-06 15:30:55 +0000164 uptr alloc_tid : 24;
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000165 uptr size_class : 8;
Kostya Serebryanyf4a4d5a2012-06-06 15:30:55 +0000166 uptr free_tid : 24;
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000167
168 // Second 8 bytes.
169 uptr alignment_log : 8;
170 uptr used_size : FIRST_32_SECOND_64(32, 56); // Size requested by the user.
171
Kostya Serebryany16071602012-06-06 16:58:21 +0000172 // This field may overlap with the user area and thus should not
173 // be used while the chunk is in CHUNK_ALLOCATED state.
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000174 AsanChunk *next;
175
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000176 // Typically the beginning of the user-accessible memory is 'this'+REDZONE
177 // and is also aligned by REDZONE. However, if the memory is allocated
178 // by memalign, the alignment might be higher and the user-accessible memory
Kostya Serebryany0334fc82012-06-07 09:15:48 +0000179 // starts at the first properly aligned address after 'this'.
180 uptr Beg() { return RoundUpTo((uptr)this + 1, 1 << alignment_log); }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000181 uptr Size() { return SizeClassToSize(size_class); }
Kostya Serebryanyee392552012-05-31 15:02:07 +0000182 u8 SizeClass() { return size_class; }
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000183};
184
185struct AsanChunk: public ChunkBase {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000186 u32 *compressed_alloc_stack() {
Kostya Serebryanyee392552012-05-31 15:02:07 +0000187 return (u32*)((uptr)this + sizeof(ChunkBase));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000188 }
Kostya Serebryanyee392552012-05-31 15:02:07 +0000189 u32 *compressed_free_stack() {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000190 return (u32*)((uptr)this + Max((uptr)REDZONE, (uptr)sizeof(ChunkBase)));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000191 }
192
193 // The left redzone after the ChunkBase is given to the alloc stack trace.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000194 uptr compressed_alloc_stack_size() {
Kostya Serebryany0334fc82012-06-07 09:15:48 +0000195 if (REDZONE < sizeof(ChunkBase)) return 0;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000196 return (REDZONE - sizeof(ChunkBase)) / sizeof(u32);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000197 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000198 uptr compressed_free_stack_size() {
Kostya Serebryany0334fc82012-06-07 09:15:48 +0000199 if (REDZONE < sizeof(ChunkBase)) return 0;
Kostya Serebryanyee392552012-05-31 15:02:07 +0000200 return (REDZONE) / sizeof(u32);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000201 }
202
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000203 bool AddrIsInside(uptr addr, uptr access_size, uptr *offset) {
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000204 if (addr >= Beg() && (addr + access_size) <= (Beg() + used_size)) {
205 *offset = addr - Beg();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000206 return true;
207 }
208 return false;
209 }
210
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000211 bool AddrIsAtLeft(uptr addr, uptr access_size, uptr *offset) {
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000212 if (addr < Beg()) {
213 *offset = Beg() - addr;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000214 return true;
215 }
216 return false;
217 }
218
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000219 bool AddrIsAtRight(uptr addr, uptr access_size, uptr *offset) {
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000220 if (addr + access_size >= Beg() + used_size) {
221 if (addr <= Beg() + used_size)
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000222 *offset = 0;
223 else
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000224 *offset = addr - (Beg() + used_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000225 return true;
226 }
227 return false;
228 }
229
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000230 void DescribeAddress(uptr addr, uptr access_size) {
231 uptr offset;
Alexey Samsonove9541012012-06-06 13:11:29 +0000232 AsanPrintf("%p is located ", (void*)addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000233 if (AddrIsInside(addr, access_size, &offset)) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000234 AsanPrintf("%zu bytes inside of", offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000235 } else if (AddrIsAtLeft(addr, access_size, &offset)) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000236 AsanPrintf("%zu bytes to the left of", offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000237 } else if (AddrIsAtRight(addr, access_size, &offset)) {
Alexey Samsonove9541012012-06-06 13:11:29 +0000238 AsanPrintf("%zu bytes to the right of", offset);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000239 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +0000240 AsanPrintf(" somewhere around (this is AddressSanitizer bug!)");
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000241 }
Alexey Samsonove9541012012-06-06 13:11:29 +0000242 AsanPrintf(" %zu-byte region [%p,%p)\n",
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000243 used_size, (void*)Beg(), (void*)(Beg() + used_size));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000244 }
245};
246
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000247static AsanChunk *PtrToChunk(uptr ptr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000248 AsanChunk *m = (AsanChunk*)(ptr - REDZONE);
249 if (m->chunk_state == CHUNK_MEMALIGN) {
Kostya Serebryany16071602012-06-06 16:58:21 +0000250 m = (AsanChunk*)((uptr)m - m->used_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000251 }
252 return m;
253}
254
255
256void AsanChunkFifoList::PushList(AsanChunkFifoList *q) {
Alexey Samsonove0460662012-02-27 09:06:10 +0000257 CHECK(q->size() > 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000258 if (last_) {
259 CHECK(first_);
260 CHECK(!last_->next);
261 last_->next = q->first_;
262 last_ = q->last_;
263 } else {
264 CHECK(!first_);
265 last_ = q->last_;
266 first_ = q->first_;
Alexey Samsonove0460662012-02-27 09:06:10 +0000267 CHECK(first_);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000268 }
Alexey Samsonove0460662012-02-27 09:06:10 +0000269 CHECK(last_);
270 CHECK(!last_->next);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000271 size_ += q->size();
272 q->clear();
273}
274
275void AsanChunkFifoList::Push(AsanChunk *n) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000276 CHECK(n->next == 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000277 if (last_) {
278 CHECK(first_);
279 CHECK(!last_->next);
280 last_->next = n;
281 last_ = n;
282 } else {
283 CHECK(!first_);
284 last_ = first_ = n;
285 }
286 size_ += n->Size();
287}
288
289// Interesting performance observation: this function takes up to 15% of overal
290// allocator time. That's because *first_ has been evicted from cache long time
291// ago. Not sure if we can or want to do anything with this.
292AsanChunk *AsanChunkFifoList::Pop() {
293 CHECK(first_);
294 AsanChunk *res = first_;
295 first_ = first_->next;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000296 if (first_ == 0)
297 last_ = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000298 CHECK(size_ >= res->Size());
299 size_ -= res->Size();
300 if (last_) {
301 CHECK(!last_->next);
302 }
303 return res;
304}
305
306// All pages we ever allocated.
307struct PageGroup {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000308 uptr beg;
309 uptr end;
310 uptr size_of_chunk;
311 uptr last_chunk;
312 bool InRange(uptr addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000313 return addr >= beg && addr < end;
314 }
315};
316
317class MallocInfo {
318 public:
319
320 explicit MallocInfo(LinkerInitialized x) : mu_(x) { }
321
Kostya Serebryanyee392552012-05-31 15:02:07 +0000322 AsanChunk *AllocateChunks(u8 size_class, uptr n_chunks) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000323 AsanChunk *m = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000324 AsanChunk **fl = &free_lists_[size_class];
325 {
326 ScopedLock lock(&mu_);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000327 for (uptr i = 0; i < n_chunks; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000328 if (!(*fl)) {
329 *fl = GetNewChunks(size_class);
330 }
331 AsanChunk *t = *fl;
332 *fl = t->next;
333 t->next = m;
334 CHECK(t->chunk_state == CHUNK_AVAILABLE);
335 m = t;
336 }
337 }
338 return m;
339 }
340
341 void SwallowThreadLocalMallocStorage(AsanThreadLocalMallocStorage *x,
342 bool eat_free_lists) {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000343 CHECK(flags()->quarantine_size > 0);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000344 ScopedLock lock(&mu_);
345 AsanChunkFifoList *q = &x->quarantine_;
346 if (q->size() > 0) {
347 quarantine_.PushList(q);
Alexey Samsonov63201b12012-07-23 09:11:58 +0000348 while (quarantine_.size() > (uptr)flags()->quarantine_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000349 QuarantinePop();
350 }
351 }
352 if (eat_free_lists) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000353 for (uptr size_class = 0; size_class < kNumberOfSizeClasses;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000354 size_class++) {
355 AsanChunk *m = x->free_lists_[size_class];
356 while (m) {
357 AsanChunk *t = m->next;
358 m->next = free_lists_[size_class];
359 free_lists_[size_class] = m;
360 m = t;
361 }
362 x->free_lists_[size_class] = 0;
363 }
364 }
365 }
366
367 void BypassThreadLocalQuarantine(AsanChunk *chunk) {
368 ScopedLock lock(&mu_);
369 quarantine_.Push(chunk);
370 }
371
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000372 AsanChunk *FindMallocedOrFreed(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000373 ScopedLock lock(&mu_);
374 return FindChunkByAddr(addr);
375 }
376
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000377 uptr AllocationSize(uptr ptr) {
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000378 if (!ptr) return 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000379 ScopedLock lock(&mu_);
380
Alexander Potapenko531c7d92012-08-06 12:24:39 +0000381 // Make sure this is our chunk and |ptr| actually points to the beginning
382 // of the allocated memory.
383 AsanChunk *m = FindChunkByAddr(ptr);
384 if (!m || m->Beg() != ptr) return 0;
385
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000386 if (m->chunk_state == CHUNK_ALLOCATED) {
387 return m->used_size;
388 } else {
389 return 0;
390 }
391 }
392
393 void ForceLock() {
394 mu_.Lock();
395 }
396
397 void ForceUnlock() {
398 mu_.Unlock();
399 }
400
401 void PrintStatus() {
402 ScopedLock lock(&mu_);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000403 uptr malloced = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000404
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000405 Printf(" MallocInfo: in quarantine: %zu malloced: %zu; ",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000406 quarantine_.size() >> 20, malloced >> 20);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000407 for (uptr j = 1; j < kNumberOfSizeClasses; j++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000408 AsanChunk *i = free_lists_[j];
409 if (!i) continue;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000410 uptr t = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000411 for (; i; i = i->next) {
412 t += i->Size();
413 }
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000414 Printf("%zu:%zu ", j, t >> 20);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000415 }
416 Printf("\n");
417 }
418
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000419 PageGroup *FindPageGroup(uptr addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000420 ScopedLock lock(&mu_);
421 return FindPageGroupUnlocked(addr);
422 }
423
424 private:
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000425 PageGroup *FindPageGroupUnlocked(uptr addr) {
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000426 int n = atomic_load(&n_page_groups_, memory_order_relaxed);
Kostya Serebryany25c71782012-03-10 01:30:01 +0000427 // If the page groups are not sorted yet, sort them.
Alexey Samsonov9bdf0652012-03-13 06:46:32 +0000428 if (n_sorted_page_groups_ < n) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000429 SortArray((uptr*)page_groups_, n);
Alexey Samsonov9bdf0652012-03-13 06:46:32 +0000430 n_sorted_page_groups_ = n;
Kostya Serebryany25c71782012-03-10 01:30:01 +0000431 }
Alexey Samsonov9bdf0652012-03-13 06:46:32 +0000432 // Binary search over the page groups.
Kostya Serebryany25c71782012-03-10 01:30:01 +0000433 int beg = 0, end = n;
434 while (beg < end) {
435 int med = (beg + end) / 2;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000436 uptr g = (uptr)page_groups_[med];
Kostya Serebryany25c71782012-03-10 01:30:01 +0000437 if (addr > g) {
438 // 'g' points to the end of the group, so 'addr'
439 // may not belong to page_groups_[med] or any previous group.
440 beg = med + 1;
441 } else {
442 // 'addr' may belong to page_groups_[med] or a previous group.
443 end = med;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000444 }
445 }
Kostya Serebryany25c71782012-03-10 01:30:01 +0000446 if (beg >= n)
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000447 return 0;
Kostya Serebryany25c71782012-03-10 01:30:01 +0000448 PageGroup *g = page_groups_[beg];
449 CHECK(g);
450 if (g->InRange(addr))
451 return g;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000452 return 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000453 }
454
455 // We have an address between two chunks, and we want to report just one.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000456 AsanChunk *ChooseChunk(uptr addr,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000457 AsanChunk *left_chunk, AsanChunk *right_chunk) {
458 // Prefer an allocated chunk or a chunk from quarantine.
459 if (left_chunk->chunk_state == CHUNK_AVAILABLE &&
460 right_chunk->chunk_state != CHUNK_AVAILABLE)
461 return right_chunk;
462 if (right_chunk->chunk_state == CHUNK_AVAILABLE &&
463 left_chunk->chunk_state != CHUNK_AVAILABLE)
464 return left_chunk;
465 // Choose based on offset.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000466 uptr l_offset = 0, r_offset = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000467 CHECK(left_chunk->AddrIsAtRight(addr, 1, &l_offset));
468 CHECK(right_chunk->AddrIsAtLeft(addr, 1, &r_offset));
469 if (l_offset < r_offset)
470 return left_chunk;
471 return right_chunk;
472 }
473
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000474 AsanChunk *FindChunkByAddr(uptr addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000475 PageGroup *g = FindPageGroupUnlocked(addr);
476 if (!g) return 0;
477 CHECK(g->size_of_chunk);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000478 uptr offset_from_beg = addr - g->beg;
479 uptr this_chunk_addr = g->beg +
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000480 (offset_from_beg / g->size_of_chunk) * g->size_of_chunk;
481 CHECK(g->InRange(this_chunk_addr));
482 AsanChunk *m = (AsanChunk*)this_chunk_addr;
483 CHECK(m->chunk_state == CHUNK_ALLOCATED ||
484 m->chunk_state == CHUNK_AVAILABLE ||
485 m->chunk_state == CHUNK_QUARANTINE);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000486 uptr offset = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000487 if (m->AddrIsInside(addr, 1, &offset))
488 return m;
489
490 if (m->AddrIsAtRight(addr, 1, &offset)) {
491 if (this_chunk_addr == g->last_chunk) // rightmost chunk
492 return m;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000493 uptr right_chunk_addr = this_chunk_addr + g->size_of_chunk;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000494 CHECK(g->InRange(right_chunk_addr));
495 return ChooseChunk(addr, m, (AsanChunk*)right_chunk_addr);
496 } else {
497 CHECK(m->AddrIsAtLeft(addr, 1, &offset));
498 if (this_chunk_addr == g->beg) // leftmost chunk
499 return m;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000500 uptr left_chunk_addr = this_chunk_addr - g->size_of_chunk;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000501 CHECK(g->InRange(left_chunk_addr));
502 return ChooseChunk(addr, (AsanChunk*)left_chunk_addr, m);
503 }
504 }
505
506 void QuarantinePop() {
507 CHECK(quarantine_.size() > 0);
508 AsanChunk *m = quarantine_.Pop();
509 CHECK(m);
510 // if (F_v >= 2) Printf("MallocInfo::pop %p\n", m);
511
512 CHECK(m->chunk_state == CHUNK_QUARANTINE);
513 m->chunk_state = CHUNK_AVAILABLE;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000514 PoisonShadow((uptr)m, m->Size(), kAsanHeapLeftRedzoneMagic);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000515 CHECK(m->alloc_tid >= 0);
516 CHECK(m->free_tid >= 0);
517
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000518 uptr size_class = m->SizeClass();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000519 m->next = free_lists_[size_class];
520 free_lists_[size_class] = m;
521
Kostya Serebryany30743142011-12-05 19:17:53 +0000522 // Statistics.
523 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
524 thread_stats.real_frees++;
525 thread_stats.really_freed += m->used_size;
526 thread_stats.really_freed_redzones += m->Size() - m->used_size;
527 thread_stats.really_freed_by_size[m->SizeClass()]++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000528 }
529
530 // Get a list of newly allocated chunks.
Kostya Serebryanyee392552012-05-31 15:02:07 +0000531 AsanChunk *GetNewChunks(u8 size_class) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000532 uptr size = SizeClassToSize(size_class);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000533 CHECK(IsPowerOfTwo(kMinMmapSize));
534 CHECK(size < kMinMmapSize || (size % kMinMmapSize) == 0);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000535 uptr mmap_size = Max(size, kMinMmapSize);
536 uptr n_chunks = mmap_size / size;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000537 CHECK(n_chunks * size == mmap_size);
538 if (size < kPageSize) {
539 // Size is small, just poison the last chunk.
540 n_chunks--;
541 } else {
542 // Size is large, allocate an extra page at right and poison it.
543 mmap_size += kPageSize;
544 }
545 CHECK(n_chunks > 0);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000546 u8 *mem = MmapNewPagesAndPoisonShadow(mmap_size);
Kostya Serebryany30743142011-12-05 19:17:53 +0000547
548 // Statistics.
549 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
550 thread_stats.mmaps++;
551 thread_stats.mmaped += mmap_size;
552 thread_stats.mmaped_by_size[size_class] += n_chunks;
553
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000554 AsanChunk *res = 0;
555 for (uptr i = 0; i < n_chunks; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000556 AsanChunk *m = (AsanChunk*)(mem + i * size);
557 m->chunk_state = CHUNK_AVAILABLE;
558 m->size_class = size_class;
559 m->next = res;
560 res = m;
561 }
562 PageGroup *pg = (PageGroup*)(mem + n_chunks * size);
563 // This memory is already poisoned, no need to poison it again.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000564 pg->beg = (uptr)mem;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000565 pg->end = pg->beg + mmap_size;
566 pg->size_of_chunk = size;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000567 pg->last_chunk = (uptr)(mem + size * (n_chunks - 1));
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000568 int idx = atomic_fetch_add(&n_page_groups_, 1, memory_order_relaxed);
569 CHECK(idx < (int)ASAN_ARRAY_SIZE(page_groups_));
570 page_groups_[idx] = pg;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000571 return res;
572 }
573
574 AsanChunk *free_lists_[kNumberOfSizeClasses];
575 AsanChunkFifoList quarantine_;
576 AsanLock mu_;
577
578 PageGroup *page_groups_[kMaxAvailableRam / kMinMmapSize];
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000579 atomic_uint32_t n_page_groups_;
Alexey Samsonov9bdf0652012-03-13 06:46:32 +0000580 int n_sorted_page_groups_;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000581};
582
583static MallocInfo malloc_info(LINKER_INITIALIZED);
584
585void AsanThreadLocalMallocStorage::CommitBack() {
586 malloc_info.SwallowThreadLocalMallocStorage(this, true);
587}
588
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000589void DescribeHeapAddress(uptr addr, uptr access_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000590 AsanChunk *m = malloc_info.FindMallocedOrFreed(addr, access_size);
591 if (!m) return;
592 m->DescribeAddress(addr, access_size);
593 CHECK(m->alloc_tid >= 0);
594 AsanThreadSummary *alloc_thread =
595 asanThreadRegistry().FindByTid(m->alloc_tid);
596 AsanStackTrace alloc_stack;
597 AsanStackTrace::UncompressStack(&alloc_stack, m->compressed_alloc_stack(),
598 m->compressed_alloc_stack_size());
599 AsanThread *t = asanThreadRegistry().GetCurrent();
600 CHECK(t);
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +0000601 if (m->free_tid != kInvalidTid) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000602 AsanThreadSummary *free_thread =
603 asanThreadRegistry().FindByTid(m->free_tid);
Alexey Samsonove9541012012-06-06 13:11:29 +0000604 AsanPrintf("freed by thread T%d here:\n", free_thread->tid());
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000605 AsanStackTrace free_stack;
606 AsanStackTrace::UncompressStack(&free_stack, m->compressed_free_stack(),
607 m->compressed_free_stack_size());
608 free_stack.PrintStack();
Alexey Samsonove9541012012-06-06 13:11:29 +0000609 AsanPrintf("previously allocated by thread T%d here:\n",
610 alloc_thread->tid());
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000611
612 alloc_stack.PrintStack();
613 t->summary()->Announce();
614 free_thread->Announce();
615 alloc_thread->Announce();
616 } else {
Alexey Samsonove9541012012-06-06 13:11:29 +0000617 AsanPrintf("allocated by thread T%d here:\n", alloc_thread->tid());
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000618 alloc_stack.PrintStack();
619 t->summary()->Announce();
620 alloc_thread->Announce();
621 }
622}
623
Kostya Serebryanyee392552012-05-31 15:02:07 +0000624static u8 *Allocate(uptr alignment, uptr size, AsanStackTrace *stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000625 __asan_init();
626 CHECK(stack);
627 if (size == 0) {
628 size = 1; // TODO(kcc): do something smarter
629 }
630 CHECK(IsPowerOfTwo(alignment));
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000631 uptr rounded_size = RoundUpTo(size, REDZONE);
632 uptr needed_size = rounded_size + REDZONE;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000633 if (alignment > REDZONE) {
634 needed_size += alignment;
635 }
636 CHECK(IsAligned(needed_size, REDZONE));
637 if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize) {
Alexey Samsonov5bcca4e2012-06-06 10:46:00 +0000638 Report("WARNING: AddressSanitizer failed to allocate %p bytes\n",
639 (void*)size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000640 return 0;
641 }
642
Kostya Serebryanyee392552012-05-31 15:02:07 +0000643 u8 size_class = SizeToSizeClass(needed_size);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000644 uptr size_to_allocate = SizeClassToSize(size_class);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000645 CHECK(size_to_allocate >= kMinAllocSize);
646 CHECK(size_to_allocate >= needed_size);
647 CHECK(IsAligned(size_to_allocate, REDZONE));
648
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000649 if (flags()->verbosity >= 3) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000650 Printf("Allocate align: %zu size: %zu class: %u real: %zu\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000651 alignment, size, size_class, size_to_allocate);
652 }
653
654 AsanThread *t = asanThreadRegistry().GetCurrent();
655 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
Kostya Serebryany30743142011-12-05 19:17:53 +0000656 // Statistics
657 thread_stats.mallocs++;
658 thread_stats.malloced += size;
659 thread_stats.malloced_redzones += size_to_allocate - size;
660 thread_stats.malloced_by_size[size_class]++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000661
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000662 AsanChunk *m = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000663 if (!t || size_to_allocate >= kMaxSizeForThreadLocalFreeList) {
664 // get directly from global storage.
665 m = malloc_info.AllocateChunks(size_class, 1);
Kostya Serebryany30743142011-12-05 19:17:53 +0000666 thread_stats.malloc_large++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000667 } else {
668 // get from the thread-local storage.
669 AsanChunk **fl = &t->malloc_storage().free_lists_[size_class];
670 if (!*fl) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000671 uptr n_new_chunks = kMaxSizeForThreadLocalFreeList / size_to_allocate;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000672 *fl = malloc_info.AllocateChunks(size_class, n_new_chunks);
Kostya Serebryany30743142011-12-05 19:17:53 +0000673 thread_stats.malloc_small_slow++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000674 }
675 m = *fl;
676 *fl = (*fl)->next;
677 }
678 CHECK(m);
679 CHECK(m->chunk_state == CHUNK_AVAILABLE);
680 m->chunk_state = CHUNK_ALLOCATED;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000681 m->next = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000682 CHECK(m->Size() == size_to_allocate);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000683 uptr addr = (uptr)m + REDZONE;
Kostya Serebryany0334fc82012-06-07 09:15:48 +0000684 CHECK(addr <= (uptr)m->compressed_free_stack());
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000685
686 if (alignment > REDZONE && (addr & (alignment - 1))) {
687 addr = RoundUpTo(addr, alignment);
688 CHECK((addr & (alignment - 1)) == 0);
689 AsanChunk *p = (AsanChunk*)(addr - REDZONE);
690 p->chunk_state = CHUNK_MEMALIGN;
Kostya Serebryany16071602012-06-06 16:58:21 +0000691 p->used_size = (uptr)p - (uptr)m;
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000692 m->alignment_log = Log2(alignment);
693 CHECK(m->Beg() == addr);
694 } else {
695 m->alignment_log = Log2(REDZONE);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000696 }
697 CHECK(m == PtrToChunk(addr));
698 m->used_size = size;
Kostya Serebryany6dc48dd2012-06-06 16:33:46 +0000699 CHECK(m->Beg() == addr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000700 m->alloc_tid = t ? t->tid() : 0;
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +0000701 m->free_tid = kInvalidTid;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000702 AsanStackTrace::CompressStack(stack, m->compressed_alloc_stack(),
703 m->compressed_alloc_stack_size());
704 PoisonShadow(addr, rounded_size, 0);
705 if (size < rounded_size) {
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000706 PoisonHeapPartialRightRedzone(addr + rounded_size - REDZONE,
707 size & (REDZONE - 1));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000708 }
Alexey Samsonov63201b12012-07-23 09:11:58 +0000709 if (size <= (uptr)(flags()->max_malloc_fill_size)) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000710 REAL(memset)((void*)addr, 0, rounded_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000711 }
Kostya Serebryanyee392552012-05-31 15:02:07 +0000712 return (u8*)addr;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000713}
714
Kostya Serebryanyee392552012-05-31 15:02:07 +0000715static void Deallocate(u8 *ptr, AsanStackTrace *stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000716 if (!ptr) return;
717 CHECK(stack);
718
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000719 if (flags()->debug) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000720 CHECK(malloc_info.FindPageGroup((uptr)ptr));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000721 }
722
723 // Printf("Deallocate %p\n", ptr);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000724 AsanChunk *m = PtrToChunk((uptr)ptr);
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000725
Kostya Serebryanyf4a4d5a2012-06-06 15:30:55 +0000726 // Flip the chunk_state atomically to avoid race on double-free.
Dmitry Vyukovfce5bd42012-06-29 16:58:33 +0000727 u8 old_chunk_state = atomic_exchange((atomic_uint8_t*)m, CHUNK_QUARANTINE,
728 memory_order_acq_rel);
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000729
730 if (old_chunk_state == CHUNK_QUARANTINE) {
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000731 ReportDoubleFree((uptr)ptr, stack);
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000732 } else if (old_chunk_state != CHUNK_ALLOCATED) {
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000733 ReportFreeNotMalloced((uptr)ptr, stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000734 }
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000735 CHECK(old_chunk_state == CHUNK_ALLOCATED);
Kostya Serebryany16071602012-06-06 16:58:21 +0000736 // With REDZONE==16 m->next is in the user area, otherwise it should be 0.
737 CHECK(REDZONE <= 16 || !m->next);
Kostya Serebryanye0cff0b2012-06-06 15:06:58 +0000738 CHECK(m->free_tid == kInvalidTid);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000739 CHECK(m->alloc_tid >= 0);
740 AsanThread *t = asanThreadRegistry().GetCurrent();
741 m->free_tid = t ? t->tid() : 0;
742 AsanStackTrace::CompressStack(stack, m->compressed_free_stack(),
743 m->compressed_free_stack_size());
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000744 uptr rounded_size = RoundUpTo(m->used_size, REDZONE);
745 PoisonShadow((uptr)ptr, rounded_size, kAsanHeapFreeMagic);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000746
Kostya Serebryany30743142011-12-05 19:17:53 +0000747 // Statistics.
748 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
749 thread_stats.frees++;
750 thread_stats.freed += m->used_size;
751 thread_stats.freed_by_size[m->SizeClass()]++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000752
Kostya Serebryanyf1e82b82012-04-05 15:55:09 +0000753 CHECK(m->chunk_state == CHUNK_QUARANTINE);
Kostya Serebryany16071602012-06-06 16:58:21 +0000754
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000755 if (t) {
756 AsanThreadLocalMallocStorage *ms = &t->malloc_storage();
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000757 ms->quarantine_.Push(m);
758
759 if (ms->quarantine_.size() > kMaxThreadLocalQuarantine) {
760 malloc_info.SwallowThreadLocalMallocStorage(ms, false);
761 }
762 } else {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000763 malloc_info.BypassThreadLocalQuarantine(m);
764 }
765}
766
Kostya Serebryanyee392552012-05-31 15:02:07 +0000767static u8 *Reallocate(u8 *old_ptr, uptr new_size,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000768 AsanStackTrace *stack) {
769 CHECK(old_ptr && new_size);
Kostya Serebryany30743142011-12-05 19:17:53 +0000770
771 // Statistics.
772 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
773 thread_stats.reallocs++;
774 thread_stats.realloced += new_size;
775
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000776 AsanChunk *m = PtrToChunk((uptr)old_ptr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000777 CHECK(m->chunk_state == CHUNK_ALLOCATED);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000778 uptr old_size = m->used_size;
779 uptr memcpy_size = Min(new_size, old_size);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000780 u8 *new_ptr = Allocate(0, new_size, stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000781 if (new_ptr) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000782 CHECK(REAL(memcpy) != 0);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000783 REAL(memcpy)(new_ptr, old_ptr, memcpy_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000784 Deallocate(old_ptr, stack);
785 }
786 return new_ptr;
787}
788
789} // namespace __asan
790
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000791// Default (no-op) implementation of malloc hooks.
792extern "C" {
793SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
794void __asan_malloc_hook(void *ptr, uptr size) {
795 (void)ptr;
796 (void)size;
797}
798SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
799void __asan_free_hook(void *ptr) {
800 (void)ptr;
801}
802} // extern "C"
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000803
804namespace __asan {
805
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000806SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000807void *asan_memalign(uptr alignment, uptr size, AsanStackTrace *stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000808 void *ptr = (void*)Allocate(alignment, size, stack);
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000809 __asan_malloc_hook(ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000810 return ptr;
811}
812
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000813SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000814void asan_free(void *ptr, AsanStackTrace *stack) {
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000815 __asan_free_hook(ptr);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000816 Deallocate((u8*)ptr, stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000817}
818
Alexander Potapenkoec3b0732012-08-15 11:57:52 +0000819SANITIZER_INTERFACE_ATTRIBUTE
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000820void *asan_malloc(uptr size, AsanStackTrace *stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000821 void *ptr = (void*)Allocate(0, size, stack);
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000822 __asan_malloc_hook(ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000823 return ptr;
824}
825
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000826void *asan_calloc(uptr nmemb, uptr size, AsanStackTrace *stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000827 void *ptr = (void*)Allocate(0, nmemb * size, stack);
828 if (ptr)
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000829 REAL(memset)(ptr, 0, nmemb * size);
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000830 __asan_malloc_hook(ptr, nmemb * size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000831 return ptr;
832}
833
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000834void *asan_realloc(void *p, uptr size, AsanStackTrace *stack) {
835 if (p == 0) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000836 void *ptr = (void*)Allocate(0, size, stack);
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000837 __asan_malloc_hook(ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000838 return ptr;
839 } else if (size == 0) {
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000840 __asan_free_hook(p);
Kostya Serebryanyee392552012-05-31 15:02:07 +0000841 Deallocate((u8*)p, stack);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000842 return 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000843 }
Kostya Serebryanyee392552012-05-31 15:02:07 +0000844 return Reallocate((u8*)p, size, stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000845}
846
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000847void *asan_valloc(uptr size, AsanStackTrace *stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000848 void *ptr = (void*)Allocate(kPageSize, size, stack);
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000849 __asan_malloc_hook(ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000850 return ptr;
851}
852
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000853void *asan_pvalloc(uptr size, AsanStackTrace *stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000854 size = RoundUpTo(size, kPageSize);
855 if (size == 0) {
856 // pvalloc(0) should allocate one page.
857 size = kPageSize;
858 }
859 void *ptr = (void*)Allocate(kPageSize, size, stack);
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000860 __asan_malloc_hook(ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000861 return ptr;
862}
863
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000864int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000865 AsanStackTrace *stack) {
866 void *ptr = Allocate(alignment, size, stack);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000867 CHECK(IsAligned((uptr)ptr, alignment));
Alexey Samsonovb21de9e2012-08-22 10:12:47 +0000868 __asan_malloc_hook(ptr, size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000869 *memptr = ptr;
870 return 0;
871}
872
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000873uptr asan_malloc_usable_size(void *ptr, AsanStackTrace *stack) {
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000874 CHECK(stack);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000875 if (ptr == 0) return 0;
876 uptr usable_size = malloc_info.AllocationSize((uptr)ptr);
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +0000877 if (flags()->check_malloc_usable_size && (usable_size == 0)) {
Alexey Samsonovf7c1d182012-08-09 08:15:46 +0000878 ReportMallocUsableSizeNotOwned((uptr)ptr, stack);
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000879 }
880 return usable_size;
881}
882
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000883uptr asan_mz_size(const void *ptr) {
884 return malloc_info.AllocationSize((uptr)ptr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000885}
886
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000887void asan_mz_force_lock() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000888 malloc_info.ForceLock();
889}
890
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000891void asan_mz_force_unlock() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000892 malloc_info.ForceUnlock();
893}
894
895// ---------------------- Fake stack-------------------- {{{1
896FakeStack::FakeStack() {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000897 CHECK(REAL(memset) != 0);
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000898 REAL(memset)(this, 0, sizeof(*this));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000899}
900
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000901bool FakeStack::AddrIsInSizeClass(uptr addr, uptr size_class) {
902 uptr mem = allocated_size_classes_[size_class];
903 uptr size = ClassMmapSize(size_class);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000904 bool res = mem && addr >= mem && addr < mem + size;
905 return res;
906}
907
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000908uptr FakeStack::AddrIsInFakeStack(uptr addr) {
909 for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000910 if (AddrIsInSizeClass(addr, i)) return allocated_size_classes_[i];
911 }
912 return 0;
913}
914
915// We may want to compute this during compilation.
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000916inline uptr FakeStack::ComputeSizeClass(uptr alloc_size) {
917 uptr rounded_size = RoundUpToPowerOfTwo(alloc_size);
918 uptr log = Log2(rounded_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000919 CHECK(alloc_size <= (1UL << log));
920 if (!(alloc_size > (1UL << (log-1)))) {
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000921 Printf("alloc_size %zu log %zu\n", alloc_size, log);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000922 }
923 CHECK(alloc_size > (1UL << (log-1)));
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000924 uptr res = log < kMinStackFrameSizeLog ? 0 : log - kMinStackFrameSizeLog;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000925 CHECK(res < kNumberOfSizeClasses);
926 CHECK(ClassSize(res) >= rounded_size);
927 return res;
928}
929
930void FakeFrameFifo::FifoPush(FakeFrame *node) {
931 CHECK(node);
932 node->next = 0;
933 if (first_ == 0 && last_ == 0) {
934 first_ = last_ = node;
935 } else {
936 CHECK(first_);
937 CHECK(last_);
938 last_->next = node;
939 last_ = node;
940 }
941}
942
943FakeFrame *FakeFrameFifo::FifoPop() {
944 CHECK(first_ && last_ && "Exhausted fake stack");
945 FakeFrame *res = 0;
946 if (first_ == last_) {
947 res = first_;
948 first_ = last_ = 0;
949 } else {
950 res = first_;
951 first_ = first_->next;
952 }
953 return res;
954}
955
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000956void FakeStack::Init(uptr stack_size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000957 stack_size_ = stack_size;
958 alive_ = true;
959}
960
961void FakeStack::Cleanup() {
962 alive_ = false;
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000963 for (uptr i = 0; i < kNumberOfSizeClasses; i++) {
964 uptr mem = allocated_size_classes_[i];
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000965 if (mem) {
966 PoisonShadow(mem, ClassMmapSize(i), 0);
967 allocated_size_classes_[i] = 0;
Alexey Samsonova25b3462012-06-06 16:15:07 +0000968 UnmapOrDie((void*)mem, ClassMmapSize(i));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000969 }
970 }
971}
972
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000973uptr FakeStack::ClassMmapSize(uptr size_class) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000974 return RoundUpToPowerOfTwo(stack_size_);
975}
976
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000977void FakeStack::AllocateOneSizeClass(uptr size_class) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000978 CHECK(ClassMmapSize(size_class) >= kPageSize);
Alexey Samsonova25b3462012-06-06 16:15:07 +0000979 uptr new_mem = (uptr)MmapOrDie(
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000980 ClassMmapSize(size_class), __FUNCTION__);
Evgeniy Stepanov739eb792012-03-21 11:32:46 +0000981 // Printf("T%d new_mem[%zu]: %p-%p mmap %zu\n",
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000982 // asanThreadRegistry().GetCurrent()->tid(),
983 // size_class, new_mem, new_mem + ClassMmapSize(size_class),
984 // ClassMmapSize(size_class));
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000985 uptr i;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000986 for (i = 0; i < ClassMmapSize(size_class);
987 i += ClassSize(size_class)) {
988 size_classes_[size_class].FifoPush((FakeFrame*)(new_mem + i));
989 }
990 CHECK(i == ClassMmapSize(size_class));
991 allocated_size_classes_[size_class] = new_mem;
992}
993
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000994uptr FakeStack::AllocateStack(uptr size, uptr real_stack) {
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000995 if (!alive_) return real_stack;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000996 CHECK(size <= kMaxStackMallocSize && size > 1);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +0000997 uptr size_class = ComputeSizeClass(size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000998 if (!allocated_size_classes_[size_class]) {
999 AllocateOneSizeClass(size_class);
1000 }
1001 FakeFrame *fake_frame = size_classes_[size_class].FifoPop();
1002 CHECK(fake_frame);
1003 fake_frame->size_minus_one = size - 1;
1004 fake_frame->real_stack = real_stack;
1005 while (FakeFrame *top = call_stack_.top()) {
1006 if (top->real_stack > real_stack) break;
1007 call_stack_.LifoPop();
1008 DeallocateFrame(top);
1009 }
1010 call_stack_.LifoPush(fake_frame);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001011 uptr ptr = (uptr)fake_frame;
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001012 PoisonShadow(ptr, size, 0);
1013 return ptr;
1014}
1015
1016void FakeStack::DeallocateFrame(FakeFrame *fake_frame) {
1017 CHECK(alive_);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001018 uptr size = fake_frame->size_minus_one + 1;
1019 uptr size_class = ComputeSizeClass(size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001020 CHECK(allocated_size_classes_[size_class]);
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001021 uptr ptr = (uptr)fake_frame;
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001022 CHECK(AddrIsInSizeClass(ptr, size_class));
1023 CHECK(AddrIsInSizeClass(ptr + size - 1, size_class));
1024 size_classes_[size_class].FifoPush(fake_frame);
1025}
1026
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001027void FakeStack::OnFree(uptr ptr, uptr size, uptr real_stack) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001028 FakeFrame *fake_frame = (FakeFrame*)ptr;
1029 CHECK(fake_frame->magic = kRetiredStackFrameMagic);
1030 CHECK(fake_frame->descr != 0);
1031 CHECK(fake_frame->size_minus_one == size - 1);
1032 PoisonShadow(ptr, size, kAsanStackAfterReturnMagic);
1033}
1034
1035} // namespace __asan
1036
1037// ---------------------- Interface ---------------- {{{1
1038using namespace __asan; // NOLINT
1039
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001040uptr __asan_stack_malloc(uptr size, uptr real_stack) {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +00001041 if (!flags()->use_fake_stack) return real_stack;
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001042 AsanThread *t = asanThreadRegistry().GetCurrent();
1043 if (!t) {
1044 // TSD is gone, use the real stack.
1045 return real_stack;
1046 }
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001047 uptr ptr = t->fake_stack().AllocateStack(size, real_stack);
Evgeniy Stepanov739eb792012-03-21 11:32:46 +00001048 // Printf("__asan_stack_malloc %p %zu %p\n", ptr, size, real_stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001049 return ptr;
1050}
1051
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001052void __asan_stack_free(uptr ptr, uptr size, uptr real_stack) {
Alexey Samsonovcb8c4dc2012-07-09 14:36:04 +00001053 if (!flags()->use_fake_stack) return;
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001054 if (ptr != real_stack) {
1055 FakeStack::OnFree(ptr, size, real_stack);
1056 }
1057}
1058
1059// ASan allocator doesn't reserve extra bytes, so normally we would
1060// just return "size".
Kostya Serebryany9aead372012-05-31 14:11:07 +00001061uptr __asan_get_estimated_allocated_size(uptr size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001062 if (size == 0) return 1;
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +00001063 return Min(size, kMaxAllowedMallocSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001064}
1065
1066bool __asan_get_ownership(const void *p) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001067 return malloc_info.AllocationSize((uptr)p) > 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001068}
1069
Kostya Serebryany9aead372012-05-31 14:11:07 +00001070uptr __asan_get_allocated_size(const void *p) {
Kostya Serebryany3f4c3872012-05-31 14:35:53 +00001071 if (p == 0) return 0;
1072 uptr allocated_size = malloc_info.AllocationSize((uptr)p);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001073 // Die if p is not malloced or if it is already freed.
Alexey Samsonovca2278d2012-01-18 15:26:55 +00001074 if (allocated_size == 0) {
Alexey Samsonovf7c1d182012-08-09 08:15:46 +00001075 GET_STACK_TRACE_HERE(kStackTraceMax);
1076 ReportAsanGetAllocatedSizeNotOwned((uptr)p, &stack);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001077 }
1078 return allocated_size;
1079}