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