blob: 7eadec5a3bc6d148be7e34895622b6014b3093e7 [file] [log] [blame]
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001//===-- asan_allocator.cc ---------------------------------------*- 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//
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"
35#include "asan_thread.h"
36#include "asan_thread_registry.h"
37
Kostya Serebryany85822082012-01-30 20:55:02 +000038#ifdef _WIN32
39#include <intrin.h>
40#endif
41
Kostya Serebryany1e172b42011-11-30 01:07:02 +000042namespace __asan {
43
44#define REDZONE FLAG_redzone
45static const size_t kMinAllocSize = REDZONE * 2;
46static const size_t kMinMmapSize = 4UL << 20; // 4M
47static const uint64_t kMaxAvailableRam = 128ULL << 30; // 128G
48static const size_t kMaxThreadLocalQuarantine = 1 << 20; // 1M
49static const size_t kMaxSizeForThreadLocalFreeList = 1 << 17;
50
51// Size classes less than kMallocSizeClassStep are powers of two.
52// All other size classes are multiples of kMallocSizeClassStep.
53static const size_t kMallocSizeClassStepLog = 26;
54static const size_t kMallocSizeClassStep = 1UL << kMallocSizeClassStepLog;
55
56#if __WORDSIZE == 32
57static const size_t kMaxAllowedMallocSize = 3UL << 30; // 3G
58#else
59static const size_t kMaxAllowedMallocSize = 8UL << 30; // 8G
60#endif
61
Kostya Serebryany1e172b42011-11-30 01:07:02 +000062static inline bool IsAligned(uintptr_t a, uintptr_t alignment) {
63 return (a & (alignment - 1)) == 0;
64}
65
Kostya Serebryany1e172b42011-11-30 01:07:02 +000066static inline size_t Log2(size_t x) {
67 CHECK(IsPowerOfTwo(x));
Alexander Potapenko6f045292012-01-27 15:15:04 +000068#if defined(_WIN64)
69 unsigned long ret; // NOLINT
70 _BitScanForward64(&ret, x);
71 return ret;
72#elif defined(_WIN32)
73 unsigned long ret; // NOLINT
74 _BitScanForward(&ret, x);
75 return ret;
76#else
Kostya Serebryany1e172b42011-11-30 01:07:02 +000077 return __builtin_ctzl(x);
Alexander Potapenko6f045292012-01-27 15:15:04 +000078#endif
79}
80
Kostya Serebryany1e172b42011-11-30 01:07:02 +000081static inline size_t RoundUpToPowerOfTwo(size_t size) {
82 CHECK(size);
83 if (IsPowerOfTwo(size)) return size;
Alexander Potapenko6f045292012-01-27 15:15:04 +000084
Alexey Samsonovf927ddc2012-02-03 08:50:16 +000085 unsigned long up; // NOLINT
86#if defined(_WIN64)
87 _BitScanReverse64(&up, size);
88#elif defined(_WIN32)
89 _BitScanReverse(&up, size);
90#else
91 up = __WORDSIZE - 1 - __builtin_clzl(size);
92#endif
93 CHECK(size < (1ULL << (up + 1)));
94 CHECK(size > (1ULL << up));
95 return 1UL << (up + 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +000096}
97
98static inline size_t SizeClassToSize(uint8_t size_class) {
99 CHECK(size_class < kNumberOfSizeClasses);
100 if (size_class <= kMallocSizeClassStepLog) {
101 return 1UL << size_class;
102 } else {
103 return (size_class - kMallocSizeClassStepLog) * kMallocSizeClassStep;
104 }
105}
106
107static inline uint8_t SizeToSizeClass(size_t size) {
108 uint8_t res = 0;
109 if (size <= kMallocSizeClassStep) {
110 size_t rounded = RoundUpToPowerOfTwo(size);
111 res = Log2(rounded);
112 } else {
113 res = ((size + kMallocSizeClassStep - 1) / kMallocSizeClassStep)
114 + kMallocSizeClassStepLog;
115 }
116 CHECK(res < kNumberOfSizeClasses);
117 CHECK(size <= SizeClassToSize(res));
118 return res;
119}
120
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000121// Given REDZONE bytes, we need to mark first size bytes
122// as addressable and the rest REDZONE-size bytes as unaddressable.
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000123static void PoisonHeapPartialRightRedzone(uintptr_t mem, size_t size) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000124 CHECK(size <= REDZONE);
125 CHECK(IsAligned(mem, REDZONE));
126 CHECK(IsPowerOfTwo(SHADOW_GRANULARITY));
127 CHECK(IsPowerOfTwo(REDZONE));
128 CHECK(REDZONE >= SHADOW_GRANULARITY);
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000129 PoisonShadowPartialRightRedzone(mem, size, REDZONE,
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000130 kAsanHeapRightRedzoneMagic);
131}
132
133static uint8_t *MmapNewPagesAndPoisonShadow(size_t size) {
134 CHECK(IsAligned(size, kPageSize));
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000135 uint8_t *res = (uint8_t*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000136 PoisonShadow((uintptr_t)res, size, kAsanHeapLeftRedzoneMagic);
137 if (FLAG_debug) {
138 Printf("ASAN_MMAP: [%p, %p)\n", res, res + size);
139 }
140 return res;
141}
142
143// Every chunk of memory allocated by this allocator can be in one of 3 states:
144// CHUNK_AVAILABLE: the chunk is in the free list and ready to be allocated.
145// CHUNK_ALLOCATED: the chunk is allocated and not yet freed.
146// CHUNK_QUARANTINE: the chunk was freed and put into quarantine zone.
147//
148// The pseudo state CHUNK_MEMALIGN is used to mark that the address is not
149// the beginning of a AsanChunk (in which case 'next' contains the address
150// of the AsanChunk).
151//
152// The magic numbers for the enum values are taken randomly.
153enum {
154 CHUNK_AVAILABLE = 0x573B,
155 CHUNK_ALLOCATED = 0x3204,
156 CHUNK_QUARANTINE = 0x1978,
157 CHUNK_MEMALIGN = 0xDC68,
158};
159
160struct ChunkBase {
161 uint16_t chunk_state;
162 uint8_t size_class;
163 uint32_t offset; // User-visible memory starts at this+offset (beg()).
164 int32_t alloc_tid;
165 int32_t free_tid;
166 size_t used_size; // Size requested by the user.
167 AsanChunk *next;
168
169 uintptr_t beg() { return (uintptr_t)this + offset; }
170 size_t Size() { return SizeClassToSize(size_class); }
171 uint8_t SizeClass() { return size_class; }
172};
173
174struct AsanChunk: public ChunkBase {
175 uint32_t *compressed_alloc_stack() {
176 CHECK(REDZONE >= sizeof(ChunkBase));
177 return (uint32_t*)((uintptr_t)this + sizeof(ChunkBase));
178 }
179 uint32_t *compressed_free_stack() {
180 CHECK(REDZONE >= sizeof(ChunkBase));
181 return (uint32_t*)((uintptr_t)this + REDZONE);
182 }
183
184 // The left redzone after the ChunkBase is given to the alloc stack trace.
185 size_t compressed_alloc_stack_size() {
186 return (REDZONE - sizeof(ChunkBase)) / sizeof(uint32_t);
187 }
188 size_t compressed_free_stack_size() {
189 return (REDZONE) / sizeof(uint32_t);
190 }
191
192 bool AddrIsInside(uintptr_t addr, size_t access_size, size_t *offset) {
193 if (addr >= beg() && (addr + access_size) <= (beg() + used_size)) {
194 *offset = addr - beg();
195 return true;
196 }
197 return false;
198 }
199
200 bool AddrIsAtLeft(uintptr_t addr, size_t access_size, size_t *offset) {
201 if (addr < beg()) {
202 *offset = beg() - addr;
203 return true;
204 }
205 return false;
206 }
207
208 bool AddrIsAtRight(uintptr_t addr, size_t access_size, size_t *offset) {
209 if (addr + access_size >= beg() + used_size) {
210 if (addr <= beg() + used_size)
211 *offset = 0;
212 else
213 *offset = addr - (beg() + used_size);
214 return true;
215 }
216 return false;
217 }
218
219 void DescribeAddress(uintptr_t addr, size_t access_size) {
220 size_t offset;
221 Printf("%p is located ", addr);
222 if (AddrIsInside(addr, access_size, &offset)) {
223 Printf("%ld bytes inside of", offset);
224 } else if (AddrIsAtLeft(addr, access_size, &offset)) {
225 Printf("%ld bytes to the left of", offset);
226 } else if (AddrIsAtRight(addr, access_size, &offset)) {
227 Printf("%ld bytes to the right of", offset);
228 } else {
229 Printf(" somewhere around (this is AddressSanitizer bug!)");
230 }
231 Printf(" %lu-byte region [%p,%p)\n",
232 used_size, beg(), beg() + used_size);
233 }
234};
235
236static AsanChunk *PtrToChunk(uintptr_t ptr) {
237 AsanChunk *m = (AsanChunk*)(ptr - REDZONE);
238 if (m->chunk_state == CHUNK_MEMALIGN) {
239 m = m->next;
240 }
241 return m;
242}
243
244
245void AsanChunkFifoList::PushList(AsanChunkFifoList *q) {
246 if (last_) {
247 CHECK(first_);
248 CHECK(!last_->next);
249 last_->next = q->first_;
250 last_ = q->last_;
251 } else {
252 CHECK(!first_);
253 last_ = q->last_;
254 first_ = q->first_;
255 }
256 size_ += q->size();
257 q->clear();
258}
259
260void AsanChunkFifoList::Push(AsanChunk *n) {
261 CHECK(n->next == NULL);
262 if (last_) {
263 CHECK(first_);
264 CHECK(!last_->next);
265 last_->next = n;
266 last_ = n;
267 } else {
268 CHECK(!first_);
269 last_ = first_ = n;
270 }
271 size_ += n->Size();
272}
273
274// Interesting performance observation: this function takes up to 15% of overal
275// allocator time. That's because *first_ has been evicted from cache long time
276// ago. Not sure if we can or want to do anything with this.
277AsanChunk *AsanChunkFifoList::Pop() {
278 CHECK(first_);
279 AsanChunk *res = first_;
280 first_ = first_->next;
281 if (first_ == NULL)
282 last_ = NULL;
283 CHECK(size_ >= res->Size());
284 size_ -= res->Size();
285 if (last_) {
286 CHECK(!last_->next);
287 }
288 return res;
289}
290
291// All pages we ever allocated.
292struct PageGroup {
293 uintptr_t beg;
294 uintptr_t end;
295 size_t size_of_chunk;
296 uintptr_t last_chunk;
297 bool InRange(uintptr_t addr) {
298 return addr >= beg && addr < end;
299 }
300};
301
302class MallocInfo {
303 public:
304
305 explicit MallocInfo(LinkerInitialized x) : mu_(x) { }
306
307 AsanChunk *AllocateChunks(uint8_t size_class, size_t n_chunks) {
308 AsanChunk *m = NULL;
309 AsanChunk **fl = &free_lists_[size_class];
310 {
311 ScopedLock lock(&mu_);
312 for (size_t i = 0; i < n_chunks; i++) {
313 if (!(*fl)) {
314 *fl = GetNewChunks(size_class);
315 }
316 AsanChunk *t = *fl;
317 *fl = t->next;
318 t->next = m;
319 CHECK(t->chunk_state == CHUNK_AVAILABLE);
320 m = t;
321 }
322 }
323 return m;
324 }
325
326 void SwallowThreadLocalMallocStorage(AsanThreadLocalMallocStorage *x,
327 bool eat_free_lists) {
328 CHECK(FLAG_quarantine_size > 0);
329 ScopedLock lock(&mu_);
330 AsanChunkFifoList *q = &x->quarantine_;
331 if (q->size() > 0) {
332 quarantine_.PushList(q);
333 while (quarantine_.size() > FLAG_quarantine_size) {
334 QuarantinePop();
335 }
336 }
337 if (eat_free_lists) {
338 for (size_t size_class = 0; size_class < kNumberOfSizeClasses;
339 size_class++) {
340 AsanChunk *m = x->free_lists_[size_class];
341 while (m) {
342 AsanChunk *t = m->next;
343 m->next = free_lists_[size_class];
344 free_lists_[size_class] = m;
345 m = t;
346 }
347 x->free_lists_[size_class] = 0;
348 }
349 }
350 }
351
352 void BypassThreadLocalQuarantine(AsanChunk *chunk) {
353 ScopedLock lock(&mu_);
354 quarantine_.Push(chunk);
355 }
356
357 AsanChunk *FindMallocedOrFreed(uintptr_t addr, size_t access_size) {
358 ScopedLock lock(&mu_);
359 return FindChunkByAddr(addr);
360 }
361
362 // TODO(glider): AllocationSize() may become very slow if the size of
363 // page_groups_ grows. This can be fixed by increasing kMinMmapSize,
364 // but a better solution is to speed up the search somehow.
365 size_t AllocationSize(uintptr_t ptr) {
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000366 if (!ptr) return 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000367 ScopedLock lock(&mu_);
368
369 // first, check if this is our memory
370 PageGroup *g = FindPageGroupUnlocked(ptr);
371 if (!g) return 0;
372 AsanChunk *m = PtrToChunk(ptr);
373 if (m->chunk_state == CHUNK_ALLOCATED) {
374 return m->used_size;
375 } else {
376 return 0;
377 }
378 }
379
380 void ForceLock() {
381 mu_.Lock();
382 }
383
384 void ForceUnlock() {
385 mu_.Unlock();
386 }
387
388 void PrintStatus() {
389 ScopedLock lock(&mu_);
390 size_t malloced = 0;
391
392 Printf(" MallocInfo: in quarantine: %ld malloced: %ld; ",
393 quarantine_.size() >> 20, malloced >> 20);
394 for (size_t j = 1; j < kNumberOfSizeClasses; j++) {
395 AsanChunk *i = free_lists_[j];
396 if (!i) continue;
397 size_t t = 0;
398 for (; i; i = i->next) {
399 t += i->Size();
400 }
401 Printf("%ld:%ld ", j, t >> 20);
402 }
403 Printf("\n");
404 }
405
406 PageGroup *FindPageGroup(uintptr_t addr) {
407 ScopedLock lock(&mu_);
408 return FindPageGroupUnlocked(addr);
409 }
410
411 private:
412 PageGroup *FindPageGroupUnlocked(uintptr_t addr) {
413 for (int i = 0; i < n_page_groups_; i++) {
414 PageGroup *g = page_groups_[i];
415 if (g->InRange(addr)) {
416 return g;
417 }
418 }
419 return NULL;
420 }
421
422 // We have an address between two chunks, and we want to report just one.
423 AsanChunk *ChooseChunk(uintptr_t addr,
424 AsanChunk *left_chunk, AsanChunk *right_chunk) {
425 // Prefer an allocated chunk or a chunk from quarantine.
426 if (left_chunk->chunk_state == CHUNK_AVAILABLE &&
427 right_chunk->chunk_state != CHUNK_AVAILABLE)
428 return right_chunk;
429 if (right_chunk->chunk_state == CHUNK_AVAILABLE &&
430 left_chunk->chunk_state != CHUNK_AVAILABLE)
431 return left_chunk;
432 // Choose based on offset.
Daniel Dunbar514cebb2011-12-02 01:36:38 +0000433 size_t l_offset = 0, r_offset = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000434 CHECK(left_chunk->AddrIsAtRight(addr, 1, &l_offset));
435 CHECK(right_chunk->AddrIsAtLeft(addr, 1, &r_offset));
436 if (l_offset < r_offset)
437 return left_chunk;
438 return right_chunk;
439 }
440
441 AsanChunk *FindChunkByAddr(uintptr_t addr) {
442 PageGroup *g = FindPageGroupUnlocked(addr);
443 if (!g) return 0;
444 CHECK(g->size_of_chunk);
445 uintptr_t offset_from_beg = addr - g->beg;
446 uintptr_t this_chunk_addr = g->beg +
447 (offset_from_beg / g->size_of_chunk) * g->size_of_chunk;
448 CHECK(g->InRange(this_chunk_addr));
449 AsanChunk *m = (AsanChunk*)this_chunk_addr;
450 CHECK(m->chunk_state == CHUNK_ALLOCATED ||
451 m->chunk_state == CHUNK_AVAILABLE ||
452 m->chunk_state == CHUNK_QUARANTINE);
Daniel Dunbar514cebb2011-12-02 01:36:38 +0000453 size_t offset = 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000454 if (m->AddrIsInside(addr, 1, &offset))
455 return m;
456
457 if (m->AddrIsAtRight(addr, 1, &offset)) {
458 if (this_chunk_addr == g->last_chunk) // rightmost chunk
459 return m;
460 uintptr_t right_chunk_addr = this_chunk_addr + g->size_of_chunk;
461 CHECK(g->InRange(right_chunk_addr));
462 return ChooseChunk(addr, m, (AsanChunk*)right_chunk_addr);
463 } else {
464 CHECK(m->AddrIsAtLeft(addr, 1, &offset));
465 if (this_chunk_addr == g->beg) // leftmost chunk
466 return m;
467 uintptr_t left_chunk_addr = this_chunk_addr - g->size_of_chunk;
468 CHECK(g->InRange(left_chunk_addr));
469 return ChooseChunk(addr, (AsanChunk*)left_chunk_addr, m);
470 }
471 }
472
473 void QuarantinePop() {
474 CHECK(quarantine_.size() > 0);
475 AsanChunk *m = quarantine_.Pop();
476 CHECK(m);
477 // if (F_v >= 2) Printf("MallocInfo::pop %p\n", m);
478
479 CHECK(m->chunk_state == CHUNK_QUARANTINE);
480 m->chunk_state = CHUNK_AVAILABLE;
481 CHECK(m->alloc_tid >= 0);
482 CHECK(m->free_tid >= 0);
483
484 size_t size_class = m->SizeClass();
485 m->next = free_lists_[size_class];
486 free_lists_[size_class] = m;
487
Kostya Serebryany30743142011-12-05 19:17:53 +0000488 // Statistics.
489 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
490 thread_stats.real_frees++;
491 thread_stats.really_freed += m->used_size;
492 thread_stats.really_freed_redzones += m->Size() - m->used_size;
493 thread_stats.really_freed_by_size[m->SizeClass()]++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000494 }
495
496 // Get a list of newly allocated chunks.
497 AsanChunk *GetNewChunks(uint8_t size_class) {
498 size_t size = SizeClassToSize(size_class);
499 CHECK(IsPowerOfTwo(kMinMmapSize));
500 CHECK(size < kMinMmapSize || (size % kMinMmapSize) == 0);
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000501 size_t mmap_size = Max(size, kMinMmapSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000502 size_t n_chunks = mmap_size / size;
503 CHECK(n_chunks * size == mmap_size);
504 if (size < kPageSize) {
505 // Size is small, just poison the last chunk.
506 n_chunks--;
507 } else {
508 // Size is large, allocate an extra page at right and poison it.
509 mmap_size += kPageSize;
510 }
511 CHECK(n_chunks > 0);
512 uint8_t *mem = MmapNewPagesAndPoisonShadow(mmap_size);
Kostya Serebryany30743142011-12-05 19:17:53 +0000513
514 // Statistics.
515 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
516 thread_stats.mmaps++;
517 thread_stats.mmaped += mmap_size;
518 thread_stats.mmaped_by_size[size_class] += n_chunks;
519
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000520 AsanChunk *res = NULL;
521 for (size_t i = 0; i < n_chunks; i++) {
522 AsanChunk *m = (AsanChunk*)(mem + i * size);
523 m->chunk_state = CHUNK_AVAILABLE;
524 m->size_class = size_class;
525 m->next = res;
526 res = m;
527 }
528 PageGroup *pg = (PageGroup*)(mem + n_chunks * size);
529 // This memory is already poisoned, no need to poison it again.
530 pg->beg = (uintptr_t)mem;
531 pg->end = pg->beg + mmap_size;
532 pg->size_of_chunk = size;
533 pg->last_chunk = (uintptr_t)(mem + size * (n_chunks - 1));
534 int page_group_idx = AtomicInc(&n_page_groups_) - 1;
535 CHECK(page_group_idx < (int)ASAN_ARRAY_SIZE(page_groups_));
536 page_groups_[page_group_idx] = pg;
537 return res;
538 }
539
540 AsanChunk *free_lists_[kNumberOfSizeClasses];
541 AsanChunkFifoList quarantine_;
542 AsanLock mu_;
543
544 PageGroup *page_groups_[kMaxAvailableRam / kMinMmapSize];
545 int n_page_groups_; // atomic
546};
547
548static MallocInfo malloc_info(LINKER_INITIALIZED);
549
550void AsanThreadLocalMallocStorage::CommitBack() {
551 malloc_info.SwallowThreadLocalMallocStorage(this, true);
552}
553
554static void Describe(uintptr_t addr, size_t access_size) {
555 AsanChunk *m = malloc_info.FindMallocedOrFreed(addr, access_size);
556 if (!m) return;
557 m->DescribeAddress(addr, access_size);
558 CHECK(m->alloc_tid >= 0);
559 AsanThreadSummary *alloc_thread =
560 asanThreadRegistry().FindByTid(m->alloc_tid);
561 AsanStackTrace alloc_stack;
562 AsanStackTrace::UncompressStack(&alloc_stack, m->compressed_alloc_stack(),
563 m->compressed_alloc_stack_size());
564 AsanThread *t = asanThreadRegistry().GetCurrent();
565 CHECK(t);
566 if (m->free_tid >= 0) {
567 AsanThreadSummary *free_thread =
568 asanThreadRegistry().FindByTid(m->free_tid);
569 Printf("freed by thread T%d here:\n", free_thread->tid());
570 AsanStackTrace free_stack;
571 AsanStackTrace::UncompressStack(&free_stack, m->compressed_free_stack(),
572 m->compressed_free_stack_size());
573 free_stack.PrintStack();
574 Printf("previously allocated by thread T%d here:\n",
575 alloc_thread->tid());
576
577 alloc_stack.PrintStack();
578 t->summary()->Announce();
579 free_thread->Announce();
580 alloc_thread->Announce();
581 } else {
582 Printf("allocated by thread T%d here:\n", alloc_thread->tid());
583 alloc_stack.PrintStack();
584 t->summary()->Announce();
585 alloc_thread->Announce();
586 }
587}
588
589static uint8_t *Allocate(size_t alignment, size_t size, AsanStackTrace *stack) {
590 __asan_init();
591 CHECK(stack);
592 if (size == 0) {
593 size = 1; // TODO(kcc): do something smarter
594 }
595 CHECK(IsPowerOfTwo(alignment));
596 size_t rounded_size = RoundUpTo(size, REDZONE);
597 size_t needed_size = rounded_size + REDZONE;
598 if (alignment > REDZONE) {
599 needed_size += alignment;
600 }
601 CHECK(IsAligned(needed_size, REDZONE));
602 if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize) {
603 Report("WARNING: AddressSanitizer failed to allocate %p bytes\n", size);
604 return 0;
605 }
606
607 uint8_t size_class = SizeToSizeClass(needed_size);
608 size_t size_to_allocate = SizeClassToSize(size_class);
609 CHECK(size_to_allocate >= kMinAllocSize);
610 CHECK(size_to_allocate >= needed_size);
611 CHECK(IsAligned(size_to_allocate, REDZONE));
612
613 if (FLAG_v >= 2) {
614 Printf("Allocate align: %ld size: %ld class: %d real: %ld\n",
615 alignment, size, size_class, size_to_allocate);
616 }
617
618 AsanThread *t = asanThreadRegistry().GetCurrent();
619 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
Kostya Serebryany30743142011-12-05 19:17:53 +0000620 // Statistics
621 thread_stats.mallocs++;
622 thread_stats.malloced += size;
623 thread_stats.malloced_redzones += size_to_allocate - size;
624 thread_stats.malloced_by_size[size_class]++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000625
626 AsanChunk *m = NULL;
627 if (!t || size_to_allocate >= kMaxSizeForThreadLocalFreeList) {
628 // get directly from global storage.
629 m = malloc_info.AllocateChunks(size_class, 1);
Kostya Serebryany30743142011-12-05 19:17:53 +0000630 thread_stats.malloc_large++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000631 } else {
632 // get from the thread-local storage.
633 AsanChunk **fl = &t->malloc_storage().free_lists_[size_class];
634 if (!*fl) {
635 size_t n_new_chunks = kMaxSizeForThreadLocalFreeList / size_to_allocate;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000636 *fl = malloc_info.AllocateChunks(size_class, n_new_chunks);
Kostya Serebryany30743142011-12-05 19:17:53 +0000637 thread_stats.malloc_small_slow++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000638 }
639 m = *fl;
640 *fl = (*fl)->next;
641 }
642 CHECK(m);
643 CHECK(m->chunk_state == CHUNK_AVAILABLE);
644 m->chunk_state = CHUNK_ALLOCATED;
645 m->next = NULL;
646 CHECK(m->Size() == size_to_allocate);
647 uintptr_t addr = (uintptr_t)m + REDZONE;
648 CHECK(addr == (uintptr_t)m->compressed_free_stack());
649
650 if (alignment > REDZONE && (addr & (alignment - 1))) {
651 addr = RoundUpTo(addr, alignment);
652 CHECK((addr & (alignment - 1)) == 0);
653 AsanChunk *p = (AsanChunk*)(addr - REDZONE);
654 p->chunk_state = CHUNK_MEMALIGN;
655 p->next = m;
656 }
657 CHECK(m == PtrToChunk(addr));
658 m->used_size = size;
659 m->offset = addr - (uintptr_t)m;
660 CHECK(m->beg() == addr);
661 m->alloc_tid = t ? t->tid() : 0;
662 m->free_tid = AsanThread::kInvalidTid;
663 AsanStackTrace::CompressStack(stack, m->compressed_alloc_stack(),
664 m->compressed_alloc_stack_size());
665 PoisonShadow(addr, rounded_size, 0);
666 if (size < rounded_size) {
Kostya Serebryany218a9b72011-11-30 18:50:23 +0000667 PoisonHeapPartialRightRedzone(addr + rounded_size - REDZONE,
668 size & (REDZONE - 1));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000669 }
670 if (size <= FLAG_max_malloc_fill_size) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000671 REAL(memset)((void*)addr, 0, rounded_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000672 }
673 return (uint8_t*)addr;
674}
675
676static void Deallocate(uint8_t *ptr, AsanStackTrace *stack) {
677 if (!ptr) return;
678 CHECK(stack);
679
680 if (FLAG_debug) {
681 CHECK(malloc_info.FindPageGroup((uintptr_t)ptr));
682 }
683
684 // Printf("Deallocate %p\n", ptr);
685 AsanChunk *m = PtrToChunk((uintptr_t)ptr);
686 if (m->chunk_state == CHUNK_QUARANTINE) {
Kostya Serebryanyc1620132011-12-13 19:16:36 +0000687 Report("ERROR: AddressSanitizer attempting double-free on %p:\n", ptr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000688 stack->PrintStack();
Kostya Serebryany27f49322012-02-08 00:42:29 +0000689 Describe((uintptr_t)ptr, 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000690 ShowStatsAndAbort();
691 } else if (m->chunk_state != CHUNK_ALLOCATED) {
Timur Iskhodzhanov3e81fe42012-02-09 17:20:14 +0000692 if (ASAN_WINDOWS) {
693 // FIXME: On Windows there are a few extra "unknown free()s"
694 // from __endstdio, need investigating.
695 return;
696 }
Kostya Serebryanyc1620132011-12-13 19:16:36 +0000697 Report("ERROR: AddressSanitizer attempting free on address which was not"
698 " malloc()-ed: %p\n", ptr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000699 stack->PrintStack();
700 ShowStatsAndAbort();
701 }
702 CHECK(m->chunk_state == CHUNK_ALLOCATED);
703 CHECK(m->free_tid == AsanThread::kInvalidTid);
704 CHECK(m->alloc_tid >= 0);
705 AsanThread *t = asanThreadRegistry().GetCurrent();
706 m->free_tid = t ? t->tid() : 0;
707 AsanStackTrace::CompressStack(stack, m->compressed_free_stack(),
708 m->compressed_free_stack_size());
709 size_t rounded_size = RoundUpTo(m->used_size, REDZONE);
710 PoisonShadow((uintptr_t)ptr, rounded_size, kAsanHeapFreeMagic);
711
Kostya Serebryany30743142011-12-05 19:17:53 +0000712 // Statistics.
713 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
714 thread_stats.frees++;
715 thread_stats.freed += m->used_size;
716 thread_stats.freed_by_size[m->SizeClass()]++;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000717
718 m->chunk_state = CHUNK_QUARANTINE;
719 if (t) {
720 AsanThreadLocalMallocStorage *ms = &t->malloc_storage();
721 CHECK(!m->next);
722 ms->quarantine_.Push(m);
723
724 if (ms->quarantine_.size() > kMaxThreadLocalQuarantine) {
725 malloc_info.SwallowThreadLocalMallocStorage(ms, false);
726 }
727 } else {
728 CHECK(!m->next);
729 malloc_info.BypassThreadLocalQuarantine(m);
730 }
731}
732
733static uint8_t *Reallocate(uint8_t *old_ptr, size_t new_size,
734 AsanStackTrace *stack) {
735 CHECK(old_ptr && new_size);
Kostya Serebryany30743142011-12-05 19:17:53 +0000736
737 // Statistics.
738 AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
739 thread_stats.reallocs++;
740 thread_stats.realloced += new_size;
741
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000742 AsanChunk *m = PtrToChunk((uintptr_t)old_ptr);
743 CHECK(m->chunk_state == CHUNK_ALLOCATED);
744 size_t old_size = m->used_size;
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +0000745 size_t memcpy_size = Min(new_size, old_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000746 uint8_t *new_ptr = Allocate(0, new_size, stack);
747 if (new_ptr) {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000748 CHECK(REAL(memcpy) != NULL);
749 REAL(memcpy)(new_ptr, old_ptr, memcpy_size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000750 Deallocate(old_ptr, stack);
751 }
752 return new_ptr;
753}
754
755} // namespace __asan
756
757// Malloc hooks declaration.
758// ASAN_NEW_HOOK(ptr, size) is called immediately after
759// allocation of "size" bytes, which returned "ptr".
760// ASAN_DELETE_HOOK(ptr) is called immediately before
761// deallocation of "ptr".
762// If ASAN_NEW_HOOK or ASAN_DELETE_HOOK is defined, user
763// program must provide implementation of this hook.
764// If macro is undefined, the hook is no-op.
765#ifdef ASAN_NEW_HOOK
766extern "C" void ASAN_NEW_HOOK(void *ptr, size_t size);
767#else
768static inline void ASAN_NEW_HOOK(void *ptr, size_t size) { }
769#endif
770
771#ifdef ASAN_DELETE_HOOK
772extern "C" void ASAN_DELETE_HOOK(void *ptr);
773#else
774static inline void ASAN_DELETE_HOOK(void *ptr) { }
775#endif
776
777namespace __asan {
778
779void *asan_memalign(size_t alignment, size_t size, AsanStackTrace *stack) {
780 void *ptr = (void*)Allocate(alignment, size, stack);
781 ASAN_NEW_HOOK(ptr, size);
782 return ptr;
783}
784
785void asan_free(void *ptr, AsanStackTrace *stack) {
786 ASAN_DELETE_HOOK(ptr);
787 Deallocate((uint8_t*)ptr, stack);
788}
789
790void *asan_malloc(size_t size, AsanStackTrace *stack) {
791 void *ptr = (void*)Allocate(0, size, stack);
792 ASAN_NEW_HOOK(ptr, size);
793 return ptr;
794}
795
796void *asan_calloc(size_t nmemb, size_t size, AsanStackTrace *stack) {
797 void *ptr = (void*)Allocate(0, nmemb * size, stack);
798 if (ptr)
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000799 REAL(memset)(ptr, 0, nmemb * size);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000800 ASAN_NEW_HOOK(ptr, nmemb * size);
801 return ptr;
802}
803
804void *asan_realloc(void *p, size_t size, AsanStackTrace *stack) {
805 if (p == NULL) {
806 void *ptr = (void*)Allocate(0, size, stack);
807 ASAN_NEW_HOOK(ptr, size);
808 return ptr;
809 } else if (size == 0) {
810 ASAN_DELETE_HOOK(p);
811 Deallocate((uint8_t*)p, stack);
812 return NULL;
813 }
814 return Reallocate((uint8_t*)p, size, stack);
815}
816
817void *asan_valloc(size_t size, AsanStackTrace *stack) {
818 void *ptr = (void*)Allocate(kPageSize, size, stack);
819 ASAN_NEW_HOOK(ptr, size);
820 return ptr;
821}
822
823void *asan_pvalloc(size_t size, AsanStackTrace *stack) {
824 size = RoundUpTo(size, kPageSize);
825 if (size == 0) {
826 // pvalloc(0) should allocate one page.
827 size = kPageSize;
828 }
829 void *ptr = (void*)Allocate(kPageSize, size, stack);
830 ASAN_NEW_HOOK(ptr, size);
831 return ptr;
832}
833
834int asan_posix_memalign(void **memptr, size_t alignment, size_t size,
835 AsanStackTrace *stack) {
836 void *ptr = Allocate(alignment, size, stack);
837 CHECK(IsAligned((uintptr_t)ptr, alignment));
838 ASAN_NEW_HOOK(ptr, size);
839 *memptr = ptr;
840 return 0;
841}
842
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000843size_t asan_malloc_usable_size(void *ptr, AsanStackTrace *stack) {
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000844 CHECK(stack);
845 if (ptr == NULL) return 0;
846 size_t usable_size = malloc_info.AllocationSize((uintptr_t)ptr);
847 if (usable_size == 0) {
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000848 Report("ERROR: AddressSanitizer attempting to call malloc_usable_size() "
849 "for pointer which is not owned: %p\n", ptr);
850 stack->PrintStack();
851 Describe((uintptr_t)ptr, 1);
852 ShowStatsAndAbort();
853 }
854 return usable_size;
855}
856
857size_t asan_mz_size(const void *ptr) {
Alexey Samsonovca2278d2012-01-18 15:26:55 +0000858 return malloc_info.AllocationSize((uintptr_t)ptr);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000859}
860
861void DescribeHeapAddress(uintptr_t addr, uintptr_t access_size) {
862 Describe(addr, access_size);
863}
864
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000865void asan_mz_force_lock() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000866 malloc_info.ForceLock();
867}
868
Alexey Samsonov4fd95f12012-01-17 06:39:10 +0000869void asan_mz_force_unlock() {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000870 malloc_info.ForceUnlock();
871}
872
873// ---------------------- Fake stack-------------------- {{{1
874FakeStack::FakeStack() {
Alexey Samsonov09672ca2012-02-08 13:45:31 +0000875 CHECK(REAL(memset) != NULL);
876 REAL(memset)(this, 0, sizeof(*this));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000877}
878
879bool FakeStack::AddrIsInSizeClass(uintptr_t addr, size_t size_class) {
880 uintptr_t mem = allocated_size_classes_[size_class];
881 uintptr_t size = ClassMmapSize(size_class);
882 bool res = mem && addr >= mem && addr < mem + size;
883 return res;
884}
885
886uintptr_t FakeStack::AddrIsInFakeStack(uintptr_t addr) {
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000887 for (size_t i = 0; i < kNumberOfSizeClasses; i++) {
888 if (AddrIsInSizeClass(addr, i)) return allocated_size_classes_[i];
889 }
890 return 0;
891}
892
893// We may want to compute this during compilation.
894inline size_t FakeStack::ComputeSizeClass(size_t alloc_size) {
895 size_t rounded_size = RoundUpToPowerOfTwo(alloc_size);
896 size_t log = Log2(rounded_size);
897 CHECK(alloc_size <= (1UL << log));
898 if (!(alloc_size > (1UL << (log-1)))) {
899 Printf("alloc_size %ld log %ld\n", alloc_size, log);
900 }
901 CHECK(alloc_size > (1UL << (log-1)));
902 size_t res = log < kMinStackFrameSizeLog ? 0 : log - kMinStackFrameSizeLog;
903 CHECK(res < kNumberOfSizeClasses);
904 CHECK(ClassSize(res) >= rounded_size);
905 return res;
906}
907
908void FakeFrameFifo::FifoPush(FakeFrame *node) {
909 CHECK(node);
910 node->next = 0;
911 if (first_ == 0 && last_ == 0) {
912 first_ = last_ = node;
913 } else {
914 CHECK(first_);
915 CHECK(last_);
916 last_->next = node;
917 last_ = node;
918 }
919}
920
921FakeFrame *FakeFrameFifo::FifoPop() {
922 CHECK(first_ && last_ && "Exhausted fake stack");
923 FakeFrame *res = 0;
924 if (first_ == last_) {
925 res = first_;
926 first_ = last_ = 0;
927 } else {
928 res = first_;
929 first_ = first_->next;
930 }
931 return res;
932}
933
934void FakeStack::Init(size_t stack_size) {
935 stack_size_ = stack_size;
936 alive_ = true;
937}
938
939void FakeStack::Cleanup() {
940 alive_ = false;
941 for (size_t i = 0; i < kNumberOfSizeClasses; i++) {
942 uintptr_t mem = allocated_size_classes_[i];
943 if (mem) {
944 PoisonShadow(mem, ClassMmapSize(i), 0);
945 allocated_size_classes_[i] = 0;
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000946 AsanUnmapOrDie((void*)mem, ClassMmapSize(i));
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000947 }
948 }
949}
950
951size_t FakeStack::ClassMmapSize(size_t size_class) {
952 return RoundUpToPowerOfTwo(stack_size_);
953}
954
955void FakeStack::AllocateOneSizeClass(size_t size_class) {
956 CHECK(ClassMmapSize(size_class) >= kPageSize);
Kostya Serebryanyde496f42011-12-28 22:58:01 +0000957 uintptr_t new_mem = (uintptr_t)AsanMmapSomewhereOrDie(
958 ClassMmapSize(size_class), __FUNCTION__);
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000959 // Printf("T%d new_mem[%ld]: %p-%p mmap %ld\n",
960 // asanThreadRegistry().GetCurrent()->tid(),
961 // size_class, new_mem, new_mem + ClassMmapSize(size_class),
962 // ClassMmapSize(size_class));
963 size_t i;
964 for (i = 0; i < ClassMmapSize(size_class);
965 i += ClassSize(size_class)) {
966 size_classes_[size_class].FifoPush((FakeFrame*)(new_mem + i));
967 }
968 CHECK(i == ClassMmapSize(size_class));
969 allocated_size_classes_[size_class] = new_mem;
970}
971
972uintptr_t FakeStack::AllocateStack(size_t size, size_t real_stack) {
Kostya Serebryanyc4b34d92011-12-09 01:49:31 +0000973 if (!alive_) return real_stack;
Kostya Serebryany1e172b42011-11-30 01:07:02 +0000974 CHECK(size <= kMaxStackMallocSize && size > 1);
975 size_t size_class = ComputeSizeClass(size);
976 if (!allocated_size_classes_[size_class]) {
977 AllocateOneSizeClass(size_class);
978 }
979 FakeFrame *fake_frame = size_classes_[size_class].FifoPop();
980 CHECK(fake_frame);
981 fake_frame->size_minus_one = size - 1;
982 fake_frame->real_stack = real_stack;
983 while (FakeFrame *top = call_stack_.top()) {
984 if (top->real_stack > real_stack) break;
985 call_stack_.LifoPop();
986 DeallocateFrame(top);
987 }
988 call_stack_.LifoPush(fake_frame);
989 uintptr_t ptr = (uintptr_t)fake_frame;
990 PoisonShadow(ptr, size, 0);
991 return ptr;
992}
993
994void FakeStack::DeallocateFrame(FakeFrame *fake_frame) {
995 CHECK(alive_);
996 size_t size = fake_frame->size_minus_one + 1;
997 size_t size_class = ComputeSizeClass(size);
998 CHECK(allocated_size_classes_[size_class]);
999 uintptr_t ptr = (uintptr_t)fake_frame;
1000 CHECK(AddrIsInSizeClass(ptr, size_class));
1001 CHECK(AddrIsInSizeClass(ptr + size - 1, size_class));
1002 size_classes_[size_class].FifoPush(fake_frame);
1003}
1004
1005void FakeStack::OnFree(size_t ptr, size_t size, size_t real_stack) {
1006 FakeFrame *fake_frame = (FakeFrame*)ptr;
1007 CHECK(fake_frame->magic = kRetiredStackFrameMagic);
1008 CHECK(fake_frame->descr != 0);
1009 CHECK(fake_frame->size_minus_one == size - 1);
1010 PoisonShadow(ptr, size, kAsanStackAfterReturnMagic);
1011}
1012
1013} // namespace __asan
1014
1015// ---------------------- Interface ---------------- {{{1
1016using namespace __asan; // NOLINT
1017
1018size_t __asan_stack_malloc(size_t size, size_t real_stack) {
1019 if (!FLAG_use_fake_stack) return real_stack;
1020 AsanThread *t = asanThreadRegistry().GetCurrent();
1021 if (!t) {
1022 // TSD is gone, use the real stack.
1023 return real_stack;
1024 }
1025 size_t ptr = t->fake_stack().AllocateStack(size, real_stack);
1026 // Printf("__asan_stack_malloc %p %ld %p\n", ptr, size, real_stack);
1027 return ptr;
1028}
1029
1030void __asan_stack_free(size_t ptr, size_t size, size_t real_stack) {
1031 if (!FLAG_use_fake_stack) return;
1032 if (ptr != real_stack) {
1033 FakeStack::OnFree(ptr, size, real_stack);
1034 }
1035}
1036
1037// ASan allocator doesn't reserve extra bytes, so normally we would
1038// just return "size".
1039size_t __asan_get_estimated_allocated_size(size_t size) {
1040 if (size == 0) return 1;
Kostya Serebryany2d8b3bd2011-12-02 18:42:04 +00001041 return Min(size, kMaxAllowedMallocSize);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001042}
1043
1044bool __asan_get_ownership(const void *p) {
Alexey Samsonovca2278d2012-01-18 15:26:55 +00001045 return malloc_info.AllocationSize((uintptr_t)p) > 0;
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001046}
1047
1048size_t __asan_get_allocated_size(const void *p) {
Alexey Samsonovca2278d2012-01-18 15:26:55 +00001049 if (p == NULL) return 0;
1050 size_t allocated_size = malloc_info.AllocationSize((uintptr_t)p);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001051 // Die if p is not malloced or if it is already freed.
Alexey Samsonovca2278d2012-01-18 15:26:55 +00001052 if (allocated_size == 0) {
Alexey Samsonov4fd95f12012-01-17 06:39:10 +00001053 Report("ERROR: AddressSanitizer attempting to call "
1054 "__asan_get_allocated_size() for pointer which is "
1055 "not owned: %p\n", p);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001056 PRINT_CURRENT_STACK();
Alexey Samsonov4fd95f12012-01-17 06:39:10 +00001057 Describe((uintptr_t)p, 1);
Kostya Serebryany1e172b42011-11-30 01:07:02 +00001058 ShowStatsAndAbort();
1059 }
1060 return allocated_size;
1061}