* varasm.c (output_constructor_regular_field): Check TYPE_SIZE_UNIT of
[official-gcc.git] / libsanitizer / asan / asan_allocator.h
blob287ff304fbbadb84d6ab8d52ce99be3299ae1a58
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 uptr AllocTid() const;
60 uptr FreeTid() const;
61 bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
62 u32 GetAllocStackId() const;
63 u32 GetFreeStackId() const;
64 StackTrace GetAllocStack() const;
65 StackTrace GetFreeStack() const;
66 AllocType GetAllocType() const;
67 bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) const {
68 if (addr >= Beg() && (addr + access_size) <= End()) {
69 *offset = addr - Beg();
70 return true;
72 return false;
74 bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) const {
75 (void)access_size;
76 if (addr < Beg()) {
77 *offset = Beg() - addr;
78 return true;
80 return false;
82 bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const {
83 if (addr + access_size > End()) {
84 *offset = addr - End();
85 return true;
87 return false;
90 private:
91 AsanChunk *const chunk_;
94 AsanChunkView FindHeapChunkByAddress(uptr address);
95 AsanChunkView FindHeapChunkByAllocBeg(uptr address);
97 // List of AsanChunks with total size.
98 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
99 public:
100 explicit AsanChunkFifoList(LinkerInitialized) { }
101 AsanChunkFifoList() { clear(); }
102 void Push(AsanChunk *n);
103 void PushList(AsanChunkFifoList *q);
104 AsanChunk *Pop();
105 uptr size() { return size_; }
106 void clear() {
107 IntrusiveList<AsanChunk>::clear();
108 size_ = 0;
110 private:
111 uptr size_;
114 struct AsanMapUnmapCallback {
115 void OnMap(uptr p, uptr size) const;
116 void OnUnmap(uptr p, uptr size) const;
119 #if SANITIZER_CAN_USE_ALLOCATOR64
120 # if SANITIZER_FUCHSIA
121 const uptr kAllocatorSpace = ~(uptr)0;
122 const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
123 typedef DefaultSizeClassMap SizeClassMap;
124 # elif defined(__powerpc64__)
125 const uptr kAllocatorSpace = ~(uptr)0;
126 const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
127 typedef DefaultSizeClassMap SizeClassMap;
128 # elif defined(__aarch64__) && SANITIZER_ANDROID
129 const uptr kAllocatorSpace = 0x3000000000ULL;
130 const uptr kAllocatorSize = 0x2000000000ULL; // 128G.
131 typedef VeryCompactSizeClassMap SizeClassMap;
132 # elif defined(__aarch64__)
133 // AArch64/SANITIZER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
134 // so no need to different values for different VMA.
135 const uptr kAllocatorSpace = 0x10000000000ULL;
136 const uptr kAllocatorSize = 0x10000000000ULL; // 3T.
137 typedef DefaultSizeClassMap SizeClassMap;
138 # elif SANITIZER_WINDOWS
139 const uptr kAllocatorSpace = ~(uptr)0;
140 const uptr kAllocatorSize = 0x8000000000ULL; // 500G
141 typedef DefaultSizeClassMap SizeClassMap;
142 # else
143 const uptr kAllocatorSpace = 0x600000000000ULL;
144 const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
145 typedef DefaultSizeClassMap SizeClassMap;
146 # endif
147 struct AP64 { // Allocator64 parameters. Deliberately using a short name.
148 static const uptr kSpaceBeg = kAllocatorSpace;
149 static const uptr kSpaceSize = kAllocatorSize;
150 static const uptr kMetadataSize = 0;
151 typedef __asan::SizeClassMap SizeClassMap;
152 typedef AsanMapUnmapCallback MapUnmapCallback;
153 static const uptr kFlags = 0;
156 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
157 #else // Fallback to SizeClassAllocator32.
158 static const uptr kRegionSizeLog = 20;
159 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
160 # if SANITIZER_WORDSIZE == 32
161 typedef FlatByteMap<kNumRegions> ByteMap;
162 # elif SANITIZER_WORDSIZE == 64
163 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
164 # endif
165 typedef CompactSizeClassMap SizeClassMap;
166 struct AP32 {
167 static const uptr kSpaceBeg = 0;
168 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
169 static const uptr kMetadataSize = 16;
170 typedef __asan::SizeClassMap SizeClassMap;
171 static const uptr kRegionSizeLog = __asan::kRegionSizeLog;
172 typedef __asan::ByteMap ByteMap;
173 typedef AsanMapUnmapCallback MapUnmapCallback;
174 static const uptr kFlags = 0;
176 typedef SizeClassAllocator32<AP32> PrimaryAllocator;
177 #endif // SANITIZER_CAN_USE_ALLOCATOR64
179 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
180 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
181 typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
182 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
183 SecondaryAllocator> AsanAllocator;
186 struct AsanThreadLocalMallocStorage {
187 uptr quarantine_cache[16];
188 AllocatorCache allocator_cache;
189 void CommitBack();
190 private:
191 // These objects are allocated via mmap() and are zero-initialized.
192 AsanThreadLocalMallocStorage() {}
195 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
196 AllocType alloc_type);
197 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
198 void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
199 AllocType alloc_type);
201 void *asan_malloc(uptr size, BufferedStackTrace *stack);
202 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
203 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
204 void *asan_valloc(uptr size, BufferedStackTrace *stack);
205 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
207 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
208 BufferedStackTrace *stack);
209 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
211 uptr asan_mz_size(const void *ptr);
212 void asan_mz_force_lock();
213 void asan_mz_force_unlock();
215 void PrintInternalAllocatorStats();
216 void AsanSoftRssLimitExceededCallback(bool exceeded);
218 } // namespace __asan
219 #endif // ASAN_ALLOCATOR_H