blob: 10c9eee8da5a12db4e5aedca53fddba6ebd0b658 [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:02 +00001//===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of AddressSanitizer, an address sanity checker.
11//
Kostya Serebryanyeef8bd42013-04-04 11:32:49 +000012// ASan-private header for asan_allocator2.cc.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000013//===----------------------------------------------------------------------===//
14
15#ifndef ASAN_ALLOCATOR_H
16#define ASAN_ALLOCATOR_H
17
18#include "asan_internal.h"
19#include "asan_interceptors.h"
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000020#include "sanitizer_common/sanitizer_list.h"
Kostya Serebryany019b76f2011-11-30 01:07:02 +000021
22namespace __asan {
23
Kostya Serebryany3674c6b2012-12-21 08:53:59 +000024enum AllocType {
25 FROM_MALLOC = 1, // Memory block came from malloc, calloc, realloc, etc.
26 FROM_NEW = 2, // Memory block came from operator new.
27 FROM_NEW_BR = 3 // Memory block came from operator new [ ]
28};
29
Kostya Serebryany8d032042012-05-31 14:35:53 +000030static const uptr kNumberOfSizeClasses = 255;
Kostya Serebryany9d1eee92011-11-30 17:33:13 +000031struct AsanChunk;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000032
Kostya Serebryany61761f12013-01-28 08:05:47 +000033void InitializeAllocator();
Evgeniy Stepanov756e1c12014-02-03 14:19:08 +000034void ReInitializeAllocator();
Kostya Serebryany61761f12013-01-28 08:05:47 +000035
Alexey Samsonov86614652012-09-18 07:38:10 +000036class AsanChunkView {
37 public:
38 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
Alexey Samsonovdf6e6562013-10-14 11:13:54 +000039 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
40 // or quarantined chunk.
41 uptr Beg(); // First byte of user memory.
42 uptr End(); // Last byte of user memory.
43 uptr UsedSize(); // Size requested by the user.
Alexey Samsonov86614652012-09-18 07:38:10 +000044 uptr AllocTid();
45 uptr FreeTid();
Kostya Serebryany796f6552014-02-27 12:45:36 +000046 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
Alexey Samsonov86614652012-09-18 07:38:10 +000047 void GetAllocStack(StackTrace *stack);
48 void GetFreeStack(StackTrace *stack);
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000049 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000050 if (addr >= Beg() && (addr + access_size) <= End()) {
51 *offset = addr - Beg();
52 return true;
53 }
54 return false;
55 }
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000056 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
Alexander Potapenko602a09f2012-12-12 12:32:57 +000057 (void)access_size;
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000058 if (addr < Beg()) {
59 *offset = Beg() - addr;
60 return true;
61 }
62 return false;
63 }
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000064 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
Evgeniy Stepanov0b805cc2013-02-08 12:59:42 +000065 if (addr + access_size > End()) {
Evgeniy Stepanov1bc72982013-02-05 14:32:03 +000066 *offset = addr - End();
Kostya Serebryany5e2a7ac2012-12-11 09:02:36 +000067 return true;
68 }
69 return false;
70 }
71
Alexey Samsonov86614652012-09-18 07:38:10 +000072 private:
73 AsanChunk *const chunk_;
74};
75
76AsanChunkView FindHeapChunkByAddress(uptr address);
77
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000078// List of AsanChunks with total size.
79class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
Kostya Serebryany019b76f2011-11-30 01:07:02 +000080 public:
81 explicit AsanChunkFifoList(LinkerInitialized) { }
82 AsanChunkFifoList() { clear(); }
83 void Push(AsanChunk *n);
84 void PushList(AsanChunkFifoList *q);
85 AsanChunk *Pop();
Kostya Serebryany8d032042012-05-31 14:35:53 +000086 uptr size() { return size_; }
Kostya Serebryany019b76f2011-11-30 01:07:02 +000087 void clear() {
Kostya Serebryany41ffe3d2012-12-17 07:54:29 +000088 IntrusiveList<AsanChunk>::clear();
Kostya Serebryany019b76f2011-11-30 01:07:02 +000089 size_ = 0;
90 }
91 private:
Kostya Serebryany8d032042012-05-31 14:35:53 +000092 uptr size_;
Kostya Serebryany019b76f2011-11-30 01:07:02 +000093};
94
95struct AsanThreadLocalMallocStorage {
Dmitry Vyukovdb0cf872013-01-11 08:07:43 +000096 uptr quarantine_cache[16];
Dmitry Vyukovdb1ad122014-01-22 14:13:37 +000097 // Allocator cache contains atomic_uint64_t which must be 8-byte aligned.
98 ALIGNED(8) uptr allocator2_cache[96 * (512 * 8 + 16)]; // Opaque.
Kostya Serebryany019b76f2011-11-30 01:07:02 +000099 void CommitBack();
Alexey Samsonove0e31c42013-11-27 13:22:21 +0000100 private:
101 // These objects are allocated via mmap() and are zero-initialized.
102 AsanThreadLocalMallocStorage() {}
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000103};
104
Kostya Serebryany3674c6b2012-12-21 08:53:59 +0000105void *asan_memalign(uptr alignment, uptr size, StackTrace *stack,
106 AllocType alloc_type);
107void asan_free(void *ptr, StackTrace *stack, AllocType alloc_type);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000108
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000109void *asan_malloc(uptr size, StackTrace *stack);
110void *asan_calloc(uptr nmemb, uptr size, StackTrace *stack);
111void *asan_realloc(void *p, uptr size, StackTrace *stack);
112void *asan_valloc(uptr size, StackTrace *stack);
113void *asan_pvalloc(uptr size, StackTrace *stack);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000114
Kostya Serebryany8d032042012-05-31 14:35:53 +0000115int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
Kostya Serebryany6b0d7752012-08-28 11:54:30 +0000116 StackTrace *stack);
Alexey Samsonov9ff45982013-11-13 14:46:58 +0000117uptr asan_malloc_usable_size(void *ptr, uptr pc, uptr bp);
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000118
Kostya Serebryany8d032042012-05-31 14:35:53 +0000119uptr asan_mz_size(const void *ptr);
Alexey Samsonov209c5142012-01-17 06:39:10 +0000120void asan_mz_force_lock();
121void asan_mz_force_unlock();
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000122
Kostya Serebryany4a42cf62012-12-27 14:09:19 +0000123void PrintInternalAllocatorStats();
124
Kostya Serebryany019b76f2011-11-30 01:07:02 +0000125} // namespace __asan
126#endif // ASAN_ALLOCATOR_H