1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of AddressSanitizer, an address sanity checker.
12 // ASan-private header for asan_allocator2.cc.
13 //===----------------------------------------------------------------------===//
15 #ifndef ASAN_ALLOCATOR_H
16 #define ASAN_ALLOCATOR_H
18 #include "asan_internal.h"
19 #include "asan_interceptors.h"
20 #include "sanitizer_common/sanitizer_list.h"
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 [ ]
30 static const uptr kNumberOfSizeClasses
= 255;
33 void InitializeAllocator();
34 void ReInitializeAllocator();
38 explicit AsanChunkView(AsanChunk
*chunk
) : chunk_(chunk
) {}
39 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.
46 bool Eq(const AsanChunkView
&c
) const { return chunk_
== c
.chunk_
; }
47 void GetAllocStack(StackTrace
*stack
);
48 void GetFreeStack(StackTrace
*stack
);
49 bool AddrIsInside(uptr addr
, uptr access_size
, sptr
*offset
) {
50 if (addr
>= Beg() && (addr
+ access_size
) <= End()) {
51 *offset
= addr
- Beg();
56 bool AddrIsAtLeft(uptr addr
, uptr access_size
, sptr
*offset
) {
59 *offset
= Beg() - addr
;
64 bool AddrIsAtRight(uptr addr
, uptr access_size
, sptr
*offset
) {
65 if (addr
+ access_size
> End()) {
66 *offset
= addr
- End();
73 AsanChunk
*const chunk_
;
76 AsanChunkView
FindHeapChunkByAddress(uptr address
);
78 // List of AsanChunks with total size.
79 class AsanChunkFifoList
: public IntrusiveList
<AsanChunk
> {
81 explicit AsanChunkFifoList(LinkerInitialized
) { }
82 AsanChunkFifoList() { clear(); }
83 void Push(AsanChunk
*n
);
84 void PushList(AsanChunkFifoList
*q
);
86 uptr
size() { return size_
; }
88 IntrusiveList
<AsanChunk
>::clear();
95 struct AsanThreadLocalMallocStorage
{
96 uptr quarantine_cache
[16];
97 // Allocator cache contains atomic_uint64_t which must be 8-byte aligned.
98 ALIGNED(8) uptr allocator2_cache
[96 * (512 * 8 + 16)]; // Opaque.
101 // These objects are allocated via mmap() and are zero-initialized.
102 AsanThreadLocalMallocStorage() {}
105 void *asan_memalign(uptr alignment
, uptr size
, StackTrace
*stack
,
106 AllocType alloc_type
);
107 void asan_free(void *ptr
, StackTrace
*stack
, AllocType alloc_type
);
109 void *asan_malloc(uptr size
, StackTrace
*stack
);
110 void *asan_calloc(uptr nmemb
, uptr size
, StackTrace
*stack
);
111 void *asan_realloc(void *p
, uptr size
, StackTrace
*stack
);
112 void *asan_valloc(uptr size
, StackTrace
*stack
);
113 void *asan_pvalloc(uptr size
, StackTrace
*stack
);
115 int asan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
117 uptr
asan_malloc_usable_size(void *ptr
, uptr pc
, uptr bp
);
119 uptr
asan_mz_size(const void *ptr
);
120 void asan_mz_force_lock();
121 void asan_mz_force_unlock();
123 void PrintInternalAllocatorStats();
125 } // namespace __asan
126 #endif // ASAN_ALLOCATOR_H