1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // ASan-private header for asan_allocator2.cc.
11 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_ALLOCATOR_H
14 #define ASAN_ALLOCATOR_H
16 #include "asan_internal.h"
17 #include "asan_interceptors.h"
18 #include "sanitizer_common/sanitizer_list.h"
23 FROM_MALLOC
= 1, // Memory block came from malloc, calloc, realloc, etc.
24 FROM_NEW
= 2, // Memory block came from operator new.
25 FROM_NEW_BR
= 3 // Memory block came from operator new [ ]
28 static const uptr kNumberOfSizeClasses
= 255;
31 void InitializeAllocator();
35 explicit AsanChunkView(AsanChunk
*chunk
) : chunk_(chunk
) {}
36 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
37 // or quarantined chunk.
38 uptr
Beg(); // First byte of user memory.
39 uptr
End(); // Last byte of user memory.
40 uptr
UsedSize(); // Size requested by the user.
43 void GetAllocStack(StackTrace
*stack
);
44 void GetFreeStack(StackTrace
*stack
);
45 bool AddrIsInside(uptr addr
, uptr access_size
, sptr
*offset
) {
46 if (addr
>= Beg() && (addr
+ access_size
) <= End()) {
47 *offset
= addr
- Beg();
52 bool AddrIsAtLeft(uptr addr
, uptr access_size
, sptr
*offset
) {
55 *offset
= Beg() - addr
;
60 bool AddrIsAtRight(uptr addr
, uptr access_size
, sptr
*offset
) {
61 if (addr
+ access_size
> End()) {
62 *offset
= addr
- End();
69 AsanChunk
*const chunk_
;
72 AsanChunkView
FindHeapChunkByAddress(uptr address
);
74 // List of AsanChunks with total size.
75 class AsanChunkFifoList
: public IntrusiveList
<AsanChunk
> {
77 explicit AsanChunkFifoList(LinkerInitialized
) { }
78 AsanChunkFifoList() { clear(); }
79 void Push(AsanChunk
*n
);
80 void PushList(AsanChunkFifoList
*q
);
82 uptr
size() { return size_
; }
84 IntrusiveList
<AsanChunk
>::clear();
91 struct AsanThreadLocalMallocStorage
{
92 uptr quarantine_cache
[16];
93 uptr allocator2_cache
[96 * (512 * 8 + 16)]; // Opaque.
96 // These objects are allocated via mmap() and are zero-initialized.
97 AsanThreadLocalMallocStorage() {}
100 void *asan_memalign(uptr alignment
, uptr size
, StackTrace
*stack
,
101 AllocType alloc_type
);
102 void asan_free(void *ptr
, StackTrace
*stack
, AllocType alloc_type
);
104 void *asan_malloc(uptr size
, StackTrace
*stack
);
105 void *asan_calloc(uptr nmemb
, uptr size
, StackTrace
*stack
);
106 void *asan_realloc(void *p
, uptr size
, StackTrace
*stack
);
107 void *asan_valloc(uptr size
, StackTrace
*stack
);
108 void *asan_pvalloc(uptr size
, StackTrace
*stack
);
110 int asan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
112 uptr
asan_malloc_usable_size(void *ptr
, uptr pc
, uptr bp
);
114 uptr
asan_mz_size(const void *ptr
);
115 void asan_mz_force_lock();
116 void asan_mz_force_unlock();
118 void PrintInternalAllocatorStats();
120 } // namespace __asan
121 #endif // ASAN_ALLOCATOR_H