• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   u16 min_redzone;
37   u16 max_redzone;
38   u8 may_return_null;
39   u8 alloc_dealloc_mismatch;
40 
41   void SetFrom(const Flags *f, const CommonFlags *cf);
42   void CopyTo(Flags *f, CommonFlags *cf);
43 };
44 
45 void InitializeAllocator(const AllocatorOptions &options);
46 void ReInitializeAllocator(const AllocatorOptions &options);
47 void GetAllocatorOptions(AllocatorOptions *options);
48 
49 class AsanChunkView {
50  public:
AsanChunkView(AsanChunk * chunk)51   explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
52   bool IsValid();        // Checks if AsanChunkView points to a valid allocated
53                          // or quarantined chunk.
54   bool IsAllocated();    // Checks if the memory is currently allocated.
55   uptr Beg();            // First byte of user memory.
56   uptr End();            // Last byte of user memory.
57   uptr UsedSize();       // Size requested by the user.
58   uptr AllocTid();
59   uptr FreeTid();
Eq(const AsanChunkView & c)60   bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
61   u32 GetAllocStackId();
62   u32 GetFreeStackId();
63   StackTrace GetAllocStack();
64   StackTrace GetFreeStack();
AddrIsInside(uptr addr,uptr access_size,sptr * offset)65   bool AddrIsInside(uptr addr, uptr access_size, sptr *offset) {
66     if (addr >= Beg() && (addr + access_size) <= End()) {
67       *offset = addr - Beg();
68       return true;
69     }
70     return false;
71   }
AddrIsAtLeft(uptr addr,uptr access_size,sptr * offset)72   bool AddrIsAtLeft(uptr addr, uptr access_size, sptr *offset) {
73     (void)access_size;
74     if (addr < Beg()) {
75       *offset = Beg() - addr;
76       return true;
77     }
78     return false;
79   }
AddrIsAtRight(uptr addr,uptr access_size,sptr * offset)80   bool AddrIsAtRight(uptr addr, uptr access_size, sptr *offset) {
81     if (addr + access_size > End()) {
82       *offset = addr - End();
83       return true;
84     }
85     return false;
86   }
87 
88  private:
89   AsanChunk *const chunk_;
90 };
91 
92 AsanChunkView FindHeapChunkByAddress(uptr address);
93 
94 // List of AsanChunks with total size.
95 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
96  public:
AsanChunkFifoList(LinkerInitialized)97   explicit AsanChunkFifoList(LinkerInitialized) { }
AsanChunkFifoList()98   AsanChunkFifoList() { clear(); }
99   void Push(AsanChunk *n);
100   void PushList(AsanChunkFifoList *q);
101   AsanChunk *Pop();
size()102   uptr size() { return size_; }
clear()103   void clear() {
104     IntrusiveList<AsanChunk>::clear();
105     size_ = 0;
106   }
107  private:
108   uptr size_;
109 };
110 
111 struct AsanMapUnmapCallback {
112   void OnMap(uptr p, uptr size) const;
113   void OnUnmap(uptr p, uptr size) const;
114 };
115 
116 #if SANITIZER_CAN_USE_ALLOCATOR64
117 # if defined(__powerpc64__)
118 const uptr kAllocatorSpace =  0xa0000000000ULL;
119 const uptr kAllocatorSize  =  0x20000000000ULL;  // 2T.
120 # elif defined(__aarch64__)
121 // AArch64/SANITIZIER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
122 // so no need to different values for different VMA.
123 const uptr kAllocatorSpace =  0x10000000000ULL;
124 const uptr kAllocatorSize  =  0x10000000000ULL;  // 3T.
125 # else
126 const uptr kAllocatorSpace = 0x600000000000ULL;
127 const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
128 # endif
129 typedef DefaultSizeClassMap SizeClassMap;
130 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, 0 /*metadata*/,
131     SizeClassMap, AsanMapUnmapCallback> PrimaryAllocator;
132 #else  // Fallback to SizeClassAllocator32.
133 static const uptr kRegionSizeLog = 20;
134 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
135 # if SANITIZER_WORDSIZE == 32
136 typedef FlatByteMap<kNumRegions> ByteMap;
137 # elif SANITIZER_WORDSIZE == 64
138 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
139 # endif
140 typedef CompactSizeClassMap SizeClassMap;
141 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
142   SizeClassMap, kRegionSizeLog,
143   ByteMap,
144   AsanMapUnmapCallback> PrimaryAllocator;
145 #endif  // SANITIZER_CAN_USE_ALLOCATOR64
146 
147 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
148 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
149 typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
150 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
151     SecondaryAllocator> AsanAllocator;
152 
153 
154 struct AsanThreadLocalMallocStorage {
155   uptr quarantine_cache[16];
156   AllocatorCache allocator_cache;
157   void CommitBack();
158  private:
159   // These objects are allocated via mmap() and are zero-initialized.
AsanThreadLocalMallocStorageAsanThreadLocalMallocStorage160   AsanThreadLocalMallocStorage() {}
161 };
162 
163 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
164                     AllocType alloc_type);
165 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
166 void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
167                      AllocType alloc_type);
168 
169 void *asan_malloc(uptr size, BufferedStackTrace *stack);
170 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
171 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
172 void *asan_valloc(uptr size, BufferedStackTrace *stack);
173 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
174 
175 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
176                         BufferedStackTrace *stack);
177 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
178 
179 uptr asan_mz_size(const void *ptr);
180 void asan_mz_force_lock();
181 void asan_mz_force_unlock();
182 
183 void PrintInternalAllocatorStats();
184 void AsanSoftRssLimitExceededCallback(bool exceeded);
185 
186 }  // namespace __asan
187 #endif  // ASAN_ALLOCATOR_H
188