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