Daily bump.
[official-gcc.git] / libsanitizer / asan / asan_allocator.h
blob7eeddadd5472ee919485d207350bd7c82bc10e6c
1 //===-- asan_allocator.h ----------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan-private header for asan_allocator.cc.
11 //===----------------------------------------------------------------------===//
13 #ifndef ASAN_ALLOCATOR_H
14 #define ASAN_ALLOCATOR_H
16 #include "asan_flags.h"
17 #include "asan_internal.h"
18 #include "asan_interceptors.h"
19 #include "sanitizer_common/sanitizer_allocator.h"
20 #include "sanitizer_common/sanitizer_list.h"
22 namespace __asan {
24 enum 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 [ ]
30 struct AsanChunk;
32 struct AllocatorOptions {
33 u32 quarantine_size_mb;
34 u16 min_redzone;
35 u16 max_redzone;
36 u8 may_return_null;
37 u8 alloc_dealloc_mismatch;
39 void SetFrom(const Flags *f, const CommonFlags *cf);
40 void CopyTo(Flags *f, CommonFlags *cf);
43 void InitializeAllocator(const AllocatorOptions &options);
44 void ReInitializeAllocator(const AllocatorOptions &options);
45 void GetAllocatorOptions(AllocatorOptions *options);
47 class AsanChunkView {
48 public:
49 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
50 bool IsValid(); // Checks if AsanChunkView points to a valid allocated
51 // or quarantined chunk.
52 bool IsAllocated(); // Checks if the memory is currently allocated.
53 uptr Beg(); // First byte of user memory.
54 uptr End(); // Last byte of user memory.
55 uptr UsedSize(); // Size requested by the user.
56 uptr AllocTid();
57 uptr FreeTid();
58 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
59 u32 GetAllocStackId();
60 u32 GetFreeStackId();
61 StackTrace GetAllocStack();
62 StackTrace GetFreeStack();
63 AllocType GetAllocType();
64 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
65 if (addr >= Beg() && (addr + access_size) <= End()) {
66 *offset = addr - Beg();
67 return true;
69 return false;
71 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
72 (void)access_size;
73 if (addr < Beg()) {
74 *offset = Beg() - addr;
75 return true;
77 return false;
79 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
80 if (addr + access_size > End()) {
81 *offset = addr - End();
82 return true;
84 return false;
87 private:
88 AsanChunk *const chunk_;
91 AsanChunkView FindHeapChunkByAddress(uptr address);
92 AsanChunkView FindHeapChunkByAllocBeg(uptr address);
94 // List of AsanChunks with total size.
95 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
96 public:
97 explicit AsanChunkFifoList(LinkerInitialized) { }
98 AsanChunkFifoList() { clear(); }
99 void Push(AsanChunk *n);
100 void PushList(AsanChunkFifoList *q);
101 AsanChunk *Pop();
102 uptr size() { return size_; }
103 void clear() {
104 IntrusiveList<AsanChunk>::clear();
105 size_ = 0;
107 private:
108 uptr size_;
111 struct AsanMapUnmapCallback {
112 void OnMap(uptr p, uptr size) const;
113 void OnUnmap(uptr p, uptr size) const;
116 #if SANITIZER_CAN_USE_ALLOCATOR64
117 # if defined(__powerpc64__)
118 const uptr kAllocatorSpace = 0xa0000000000ULL;
119 const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
120 typedef DefaultSizeClassMap SizeClassMap;
121 # elif defined(__aarch64__) && SANITIZER_ANDROID
122 const uptr kAllocatorSpace = 0x3000000000ULL;
123 const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
124 typedef VeryCompactSizeClassMap SizeClassMap;
125 # elif defined(__aarch64__)
126 // AArch64/SANITIZER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
127 // so no need to different values for different VMA.
128 const uptr kAllocatorSpace = 0x10000000000ULL;
129 const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
130 typedef DefaultSizeClassMap SizeClassMap;
131 # elif SANITIZER_WINDOWS
132 const uptr kAllocatorSpace = ~(uptr)0;
133 const uptr kAllocatorSize = 0x8000000000ULL; // 500G
134 typedef DefaultSizeClassMap SizeClassMap;
135 # else
136 const uptr kAllocatorSpace = 0x600000000000ULL;
137 const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
138 typedef DefaultSizeClassMap SizeClassMap;
139 # endif
140 struct AP64 { // Allocator64 parameters. Deliberately using a short name.
141 static const uptr kSpaceBeg = kAllocatorSpace;
142 static const uptr kSpaceSize = kAllocatorSize;
143 static const uptr kMetadataSize = 0;
144 typedef __asan::SizeClassMap SizeClassMap;
145 typedef AsanMapUnmapCallback MapUnmapCallback;
146 static const uptr kFlags = 0;
149 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
150 #else // Fallback to SizeClassAllocator32.
151 static const uptr kRegionSizeLog = 20;
152 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
153 # if SANITIZER_WORDSIZE == 32
154 typedef FlatByteMap<kNumRegions> ByteMap;
155 # elif SANITIZER_WORDSIZE == 64
156 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
157 # endif
158 typedef CompactSizeClassMap SizeClassMap;
159 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
160 SizeClassMap, kRegionSizeLog,
161 ByteMap,
162 AsanMapUnmapCallback> PrimaryAllocator;
163 #endif // SANITIZER_CAN_USE_ALLOCATOR64
165 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
166 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
167 typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
168 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
169 SecondaryAllocator> AsanAllocator;
172 struct AsanThreadLocalMallocStorage {
173 uptr quarantine_cache[16];
174 AllocatorCache allocator_cache;
175 void CommitBack();
176 private:
177 // These objects are allocated via mmap() and are zero-initialized.
178 AsanThreadLocalMallocStorage() {}
181 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
182 AllocType alloc_type);
183 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
184 void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
185 AllocType alloc_type);
187 void *asan_malloc(uptr size, BufferedStackTrace *stack);
188 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
189 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
190 void *asan_valloc(uptr size, BufferedStackTrace *stack);
191 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
193 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
194 BufferedStackTrace *stack);
195 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
197 uptr asan_mz_size(const void *ptr);
198 void asan_mz_force_lock();
199 void asan_mz_force_unlock();
201 void PrintInternalAllocatorStats();
202 void AsanSoftRssLimitExceededCallback(bool exceeded);
204 } // namespace __asan
205 #endif // ASAN_ALLOCATOR_H