Fix build on sparc64-linux-gnu.
[official-gcc.git] / libsanitizer / asan / asan_allocator.h
blobc49bd811181833450eb5fbd17d810fe4fcc976c1
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 u32 thread_local_quarantine_size_kb;
35 u16 min_redzone;
36 u16 max_redzone;
37 u8 may_return_null;
38 u8 alloc_dealloc_mismatch;
39 s32 release_to_os_interval_ms;
41 void SetFrom(const Flags *f, const CommonFlags *cf);
42 void CopyTo(Flags *f, CommonFlags *cf);
45 void InitializeAllocator(const AllocatorOptions &options);
46 void ReInitializeAllocator(const AllocatorOptions &options);
47 void GetAllocatorOptions(AllocatorOptions *options);
49 class AsanChunkView {
50 public:
51 explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
52 bool IsValid() const; // Checks if AsanChunkView points to a valid
53 // allocated or quarantined chunk.
54 bool IsAllocated() const; // Checks if the memory is currently allocated.
55 bool IsQuarantined() const; // Checks if the memory is currently quarantined.
56 uptr Beg() const; // First byte of user memory.
57 uptr End() const; // Last byte of user memory.
58 uptr UsedSize() const; // Size requested by the user.
59 u32 UserRequestedAlignment() const; // Originally requested alignment.
60 uptr AllocTid() const;
61 uptr FreeTid() const;
62 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
63 u32 GetAllocStackId() const;
64 u32 GetFreeStackId() const;
65 StackTrace GetAllocStack() const;
66 StackTrace GetFreeStack() const;
67 AllocType GetAllocType() const;
68 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) const {
69 if (addr >= Beg() && (addr + access_size) <= End()) {
70 *offset = addr - Beg();
71 return true;
73 return false;
75 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) const {
76 (void)access_size;
77 if (addr < Beg()) {
78 *offset = Beg() - addr;
79 return true;
81 return false;
83 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const {
84 if (addr + access_size > End()) {
85 *offset = addr - End();
86 return true;
88 return false;
91 private:
92 AsanChunk *const chunk_;
95 AsanChunkView FindHeapChunkByAddress(uptr address);
96 AsanChunkView FindHeapChunkByAllocBeg(uptr address);
98 // List of AsanChunks with total size.
99 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
100 public:
101 explicit AsanChunkFifoList(LinkerInitialized) { }
102 AsanChunkFifoList() { clear(); }
103 void Push(AsanChunk *n);
104 void PushList(AsanChunkFifoList *q);
105 AsanChunk *Pop();
106 uptr size() { return size_; }
107 void clear() {
108 IntrusiveList<AsanChunk>::clear();
109 size_ = 0;
111 private:
112 uptr size_;
115 struct AsanMapUnmapCallback {
116 void OnMap(uptr p, uptr size) const;
117 void OnUnmap(uptr p, uptr size) const;
120 #if SANITIZER_CAN_USE_ALLOCATOR64
121 # if SANITIZER_FUCHSIA
122 const uptr kAllocatorSpace = ~(uptr)0;
123 const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
124 typedef DefaultSizeClassMap SizeClassMap;
125 # elif defined(__powerpc64__)
126 const uptr kAllocatorSpace = ~(uptr)0;
127 const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
128 typedef DefaultSizeClassMap SizeClassMap;
129 # elif defined(__aarch64__) && SANITIZER_ANDROID
130 // Android needs to support 39, 42 and 48 bit VMA.
131 const uptr kAllocatorSpace = ~(uptr)0;
132 const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
133 typedef VeryCompactSizeClassMap SizeClassMap;
134 # elif defined(__aarch64__)
135 // AArch64/SANITIZER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
136 // so no need to different values for different VMA.
137 const uptr kAllocatorSpace = 0x10000000000ULL;
138 const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
139 typedef DefaultSizeClassMap SizeClassMap;
140 # elif SANITIZER_WINDOWS
141 const uptr kAllocatorSpace = ~(uptr)0;
142 const uptr kAllocatorSize = 0x8000000000ULL; // 500G
143 typedef DefaultSizeClassMap SizeClassMap;
144 # else
145 const uptr kAllocatorSpace = 0x600000000000ULL;
146 const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
147 typedef DefaultSizeClassMap SizeClassMap;
148 # endif
149 struct AP64 { // Allocator64 parameters. Deliberately using a short name.
150 static const uptr kSpaceBeg = kAllocatorSpace;
151 static const uptr kSpaceSize = kAllocatorSize;
152 static const uptr kMetadataSize = 0;
153 typedef __asan::SizeClassMap SizeClassMap;
154 typedef AsanMapUnmapCallback MapUnmapCallback;
155 static const uptr kFlags = 0;
158 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
159 #else // Fallback to SizeClassAllocator32.
160 static const uptr kRegionSizeLog = 20;
161 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
162 # if SANITIZER_WORDSIZE == 32
163 typedef FlatByteMap<kNumRegions> ByteMap;
164 # elif SANITIZER_WORDSIZE == 64
165 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
166 # endif
167 typedef CompactSizeClassMap SizeClassMap;
168 struct AP32 {
169 static const uptr kSpaceBeg = 0;
170 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
171 static const uptr kMetadataSize = 16;
172 typedef __asan::SizeClassMap SizeClassMap;
173 static const uptr kRegionSizeLog = __asan::kRegionSizeLog;
174 typedef __asan::ByteMap ByteMap;
175 typedef AsanMapUnmapCallback MapUnmapCallback;
176 static const uptr kFlags = 0;
178 typedef SizeClassAllocator32<AP32> PrimaryAllocator;
179 #endif // SANITIZER_CAN_USE_ALLOCATOR64
181 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
182 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
183 typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
184 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
185 SecondaryAllocator> AsanAllocator;
188 struct AsanThreadLocalMallocStorage {
189 uptr quarantine_cache[16];
190 AllocatorCache allocator_cache;
191 void CommitBack();
192 private:
193 // These objects are allocated via mmap() and are zero-initialized.
194 AsanThreadLocalMallocStorage() {}
197 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
198 AllocType alloc_type);
199 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
200 void asan_delete(void *ptr, uptr size, uptr alignment,
201 BufferedStackTrace *stack, AllocType alloc_type);
203 void *asan_malloc(uptr size, BufferedStackTrace *stack);
204 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
205 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
206 void *asan_valloc(uptr size, BufferedStackTrace *stack);
207 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
209 void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack);
210 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
211 BufferedStackTrace *stack);
212 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
214 uptr asan_mz_size(const void *ptr);
215 void asan_mz_force_lock();
216 void asan_mz_force_unlock();
218 void PrintInternalAllocatorStats();
219 void AsanSoftRssLimitExceededCallback(bool exceeded);
221 } // namespace __asan
222 #endif // ASAN_ALLOCATOR_H