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//===----------------------------------------------------------------------===//
12
13#ifndef ASAN_ALLOCATOR_H
14#define ASAN_ALLOCATOR_H
15
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"
21
22namespace __asan {
23
24enum 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 [ ]
28};
29
30struct AsanChunk;
31
32struct 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;
40
41  void SetFrom(const Flags *f, const CommonFlags *cf);
42  void CopyTo(Flags *f, CommonFlags *cf);
43};
44
45void InitializeAllocator(const AllocatorOptions &options);
46void ReInitializeAllocator(const AllocatorOptions &options);
47void GetAllocatorOptions(AllocatorOptions *options);
48
49class 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;
72    }
73    return false;
74  }
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;
80    }
81    return false;
82  }
83  bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) const {
84    if (addr + access_size > End()) {
85      *offset = addr - End();
86      return true;
87    }
88    return false;
89  }
90
91 private:
92  AsanChunk *const chunk_;
93};
94
95AsanChunkView FindHeapChunkByAddress(uptr address);
96AsanChunkView FindHeapChunkByAllocBeg(uptr address);
97
98// List of AsanChunks with total size.
99class 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;
110  }
111 private:
112  uptr size_;
113};
114
115struct AsanMapUnmapCallback {
116  void OnMap(uptr p, uptr size) const;
117  void OnUnmap(uptr p, uptr size) const;
118};
119
120#if SANITIZER_CAN_USE_ALLOCATOR64
121# if SANITIZER_FUCHSIA
122const uptr kAllocatorSpace = ~(uptr)0;
123const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
124typedef DefaultSizeClassMap SizeClassMap;
125# elif defined(__powerpc64__)
126const uptr kAllocatorSpace = ~(uptr)0;
127const uptr kAllocatorSize  =  0x20000000000ULL;  // 2T.
128typedef DefaultSizeClassMap SizeClassMap;
129# elif defined(__aarch64__) && SANITIZER_ANDROID
130// Android needs to support 39, 42 and 48 bit VMA.
131const uptr kAllocatorSpace =  ~(uptr)0;
132const uptr kAllocatorSize  =  0x2000000000ULL;  // 128G.
133typedef VeryCompactSizeClassMap SizeClassMap;
134# elif defined(__aarch64__)
135// AArch64/SANITIZER_CAN_USE_ALLOCATOR64 is only for 42-bit VMA
136// so no need to different values for different VMA.
137const uptr kAllocatorSpace =  0x10000000000ULL;
138const uptr kAllocatorSize  =  0x10000000000ULL;  // 3T.
139typedef DefaultSizeClassMap SizeClassMap;
140# elif defined(__sparc__)
141const uptr kAllocatorSpace = ~(uptr)0;
142const uptr kAllocatorSize  =  0x20000000000ULL;  // 2T.
143typedef DefaultSizeClassMap SizeClassMap;
144# elif SANITIZER_WINDOWS
145const uptr kAllocatorSpace = ~(uptr)0;
146const uptr kAllocatorSize  =  0x8000000000ULL;  // 500G
147typedef DefaultSizeClassMap SizeClassMap;
148# else
149#if _LP64
150const uptr kAllocatorSpace = 0x600000000000ULL;
151const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
152#else
153const uptr kAllocatorSpace = 0x60000000UL;
154const uptr kAllocatorSize  =  0x40000000ULL;     // 2G.
155#endif
156typedef DefaultSizeClassMap SizeClassMap;
157# endif
158struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
159  static const uptr kSpaceBeg = kAllocatorSpace;
160  static const uptr kSpaceSize = kAllocatorSize;
161  static const uptr kMetadataSize = 0;
162  typedef __asan::SizeClassMap SizeClassMap;
163  typedef AsanMapUnmapCallback MapUnmapCallback;
164  static const uptr kFlags = 0;
165};
166
167typedef SizeClassAllocator64<AP64> PrimaryAllocator;
168#else  // Fallback to SizeClassAllocator32.
169static const uptr kRegionSizeLog = 20;
170static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
171# if SANITIZER_WORDSIZE == 32
172typedef FlatByteMap<kNumRegions> ByteMap;
173# elif SANITIZER_WORDSIZE == 64
174typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
175# endif
176typedef CompactSizeClassMap SizeClassMap;
177struct AP32 {
178  static const uptr kSpaceBeg = 0;
179  static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
180  static const uptr kMetadataSize = 16;
181  typedef __asan::SizeClassMap SizeClassMap;
182  static const uptr kRegionSizeLog = __asan::kRegionSizeLog;
183  typedef __asan::ByteMap ByteMap;
184  typedef AsanMapUnmapCallback MapUnmapCallback;
185  static const uptr kFlags = 0;
186};
187typedef SizeClassAllocator32<AP32> PrimaryAllocator;
188#endif  // SANITIZER_CAN_USE_ALLOCATOR64
189
190static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
191typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
192typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
193typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
194    SecondaryAllocator> AsanAllocator;
195
196
197struct AsanThreadLocalMallocStorage {
198  uptr quarantine_cache[16];
199  AllocatorCache allocator_cache;
200  void CommitBack();
201 private:
202  // These objects are allocated via mmap() and are zero-initialized.
203  AsanThreadLocalMallocStorage() {}
204};
205
206void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
207                    AllocType alloc_type);
208void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
209void asan_delete(void *ptr, uptr size, uptr alignment,
210                 BufferedStackTrace *stack, AllocType alloc_type);
211
212void *asan_malloc(uptr size, BufferedStackTrace *stack);
213void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
214void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
215void *asan_valloc(uptr size, BufferedStackTrace *stack);
216void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
217
218void *asan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack);
219int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
220                        BufferedStackTrace *stack);
221uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
222
223uptr asan_mz_size(const void *ptr);
224void asan_mz_force_lock();
225void asan_mz_force_unlock();
226
227void PrintInternalAllocatorStats();
228void AsanSoftRssLimitExceededCallback(bool exceeded);
229
230}  // namespace __asan
231#endif  // ASAN_ALLOCATOR_H
232