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