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() { return chunk_
!= 0; }
37 uptr
Beg(); // first byte of user memory.
38 uptr
End(); // last byte of user memory.
39 uptr
UsedSize(); // size requested by the user.
42 void GetAllocStack(StackTrace
*stack
);
43 void GetFreeStack(StackTrace
*stack
);
44 bool AddrIsInside(uptr addr
, uptr access_size
, sptr
*offset
) {
45 if (addr
>= Beg() && (addr
+ access_size
) <= End()) {
46 *offset
= addr
- Beg();
51 bool AddrIsAtLeft(uptr addr
, uptr access_size
, sptr
*offset
) {
54 *offset
= Beg() - addr
;
59 bool AddrIsAtRight(uptr addr
, uptr access_size
, sptr
*offset
) {
60 if (addr
+ access_size
> End()) {
61 *offset
= addr
- End();
68 AsanChunk
*const chunk_
;
71 AsanChunkView
FindHeapChunkByAddress(uptr address
);
73 // List of AsanChunks with total size.
74 class AsanChunkFifoList
: public IntrusiveList
<AsanChunk
> {
76 explicit AsanChunkFifoList(LinkerInitialized
) { }
77 AsanChunkFifoList() { clear(); }
78 void Push(AsanChunk
*n
);
79 void PushList(AsanChunkFifoList
*q
);
81 uptr
size() { return size_
; }
83 IntrusiveList
<AsanChunk
>::clear();
90 struct AsanThreadLocalMallocStorage
{
91 explicit AsanThreadLocalMallocStorage(LinkerInitialized x
)
93 AsanThreadLocalMallocStorage() {
95 REAL(memset
)(this, 0, sizeof(AsanThreadLocalMallocStorage
));
98 uptr quarantine_cache
[16];
99 uptr allocator2_cache
[96 * (512 * 8 + 16)]; // Opaque.
103 void *asan_memalign(uptr alignment
, uptr size
, StackTrace
*stack
,
104 AllocType alloc_type
);
105 void asan_free(void *ptr
, StackTrace
*stack
, AllocType alloc_type
);
107 void *asan_malloc(uptr size
, StackTrace
*stack
);
108 void *asan_calloc(uptr nmemb
, uptr size
, StackTrace
*stack
);
109 void *asan_realloc(void *p
, uptr size
, StackTrace
*stack
);
110 void *asan_valloc(uptr size
, StackTrace
*stack
);
111 void *asan_pvalloc(uptr size
, StackTrace
*stack
);
113 int asan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
115 uptr
asan_malloc_usable_size(void *ptr
, StackTrace
*stack
);
117 uptr
asan_mz_size(const void *ptr
);
118 void asan_mz_force_lock();
119 void asan_mz_force_unlock();
121 void PrintInternalAllocatorStats();
123 } // namespace __asan
124 #endif // ASAN_ALLOCATOR_H