Return memory to OS right after free (not in the async thread).
[platform/upstream/linaro-gcc.git] / libsanitizer / asan / asan_allocator.h
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
22 namespace __asan {
23
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 [ ]
28 };
29
30 struct AsanChunk;
31
32 struct AllocatorOptions {
33   u32 quarantine_size_mb;
34   u16 min_redzone;
35   u16 max_redzone;
36   u8 may_return_null;
37   u8 alloc_dealloc_mismatch;
38   s32 release_to_os_interval_ms;
39
40   void SetFrom(const Flags *f, const CommonFlags *cf);
41   void CopyTo(Flags *f, CommonFlags *cf);
42 };
43
44 void InitializeAllocator(const AllocatorOptions &options);
45 void ReInitializeAllocator(const AllocatorOptions &options);
46 void GetAllocatorOptions(AllocatorOptions *options);
47
48 class AsanChunkView {
49  public:
50   explicit AsanChunkView(AsanChunk *chunk) : chunk_(chunk) {}
51   bool IsValid();        // Checks if AsanChunkView points to a valid allocated
52                          // or quarantined chunk.
53   bool IsAllocated();    // Checks if the memory is currently allocated.
54   uptr Beg();            // First byte of user memory.
55   uptr End();            // Last byte of user memory.
56   uptr UsedSize();       // Size requested by the user.
57   uptr AllocTid();
58   uptr FreeTid();
59   bool Eq(const AsanChunkView &c) const { return chunk_ == c.chunk_; }
60   u32 GetAllocStackId();
61   u32 GetFreeStackId();
62   StackTrace GetAllocStack();
63   StackTrace GetFreeStack();
64   AllocType GetAllocType();
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   }
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   }
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 AsanChunkView FindHeapChunkByAllocBeg(uptr address);
94
95 // List of AsanChunks with total size.
96 class AsanChunkFifoList: public IntrusiveList<AsanChunk> {
97  public:
98   explicit AsanChunkFifoList(LinkerInitialized) { }
99   AsanChunkFifoList() { clear(); }
100   void Push(AsanChunk *n);
101   void PushList(AsanChunkFifoList *q);
102   AsanChunk *Pop();
103   uptr size() { return size_; }
104   void clear() {
105     IntrusiveList<AsanChunk>::clear();
106     size_ = 0;
107   }
108  private:
109   uptr size_;
110 };
111
112 struct AsanMapUnmapCallback {
113   void OnMap(uptr p, uptr size) const;
114   void OnUnmap(uptr p, uptr size) const;
115 };
116
117 #if SANITIZER_CAN_USE_ALLOCATOR64
118 # if defined(__powerpc64__)
119 const uptr kAllocatorSpace =  0xa0000000000ULL;
120 const uptr kAllocatorSize  =  0x20000000000ULL;  // 2T.
121 typedef DefaultSizeClassMap SizeClassMap;
122 # elif defined(__aarch64__) && SANITIZER_ANDROID
123 const uptr kAllocatorSpace =  0x3000000000ULL;
124 const uptr kAllocatorSize  =  0x2000000000ULL;  // 128G.
125 typedef VeryCompactSizeClassMap SizeClassMap;
126 # elif defined(__aarch64__)
127 // AArch64/SANITIZER_CAN_USER_ALLOCATOR64 is only for 42-bit VMA
128 // so no need to different values for different VMA.
129 const uptr kAllocatorSpace =  0x10000000000ULL;
130 const uptr kAllocatorSize  =  0x10000000000ULL;  // 3T.
131 typedef DefaultSizeClassMap SizeClassMap;
132 # elif SANITIZER_WINDOWS
133 const uptr kAllocatorSpace = ~(uptr)0;
134 const uptr kAllocatorSize  =  0x8000000000ULL;  // 500G
135 typedef DefaultSizeClassMap SizeClassMap;
136 # else
137 const uptr kAllocatorSpace = 0x600000000000ULL;
138 const uptr kAllocatorSize  =  0x40000000000ULL;  // 4T.
139 typedef DefaultSizeClassMap SizeClassMap;
140 # endif
141 struct AP64 {  // Allocator64 parameters. Deliberately using a short name.
142   static const uptr kSpaceBeg = kAllocatorSpace;
143   static const uptr kSpaceSize = kAllocatorSize;
144   static const uptr kMetadataSize = 0;
145   typedef __asan::SizeClassMap SizeClassMap;
146   typedef AsanMapUnmapCallback MapUnmapCallback;
147   static const uptr kFlags = 0;
148 };
149
150 typedef SizeClassAllocator64<AP64> PrimaryAllocator;
151 #else  // Fallback to SizeClassAllocator32.
152 static const uptr kRegionSizeLog = 17;
153 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
154 # if SANITIZER_WORDSIZE == 32
155 typedef FlatByteMap<kNumRegions> ByteMap;
156 # elif SANITIZER_WORDSIZE == 64
157 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
158 # endif
159 typedef VeryCompactSizeClassMap SizeClassMap;
160 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE, 16,
161   SizeClassMap, kRegionSizeLog,
162   ByteMap,
163   AsanMapUnmapCallback> PrimaryAllocator;
164 #endif  // SANITIZER_CAN_USE_ALLOCATOR64
165
166 static const uptr kNumberOfSizeClasses = SizeClassMap::kNumClasses;
167 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
168 typedef LargeMmapAllocator<AsanMapUnmapCallback> SecondaryAllocator;
169 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
170     SecondaryAllocator> AsanAllocator;
171
172
173 struct AsanThreadLocalMallocStorage {
174   uptr quarantine_cache[16];
175   AllocatorCache allocator_cache;
176   void CommitBack();
177  private:
178   // These objects are allocated via mmap() and are zero-initialized.
179   AsanThreadLocalMallocStorage() {}
180 };
181
182 void *asan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
183                     AllocType alloc_type);
184 void asan_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type);
185 void asan_sized_free(void *ptr, uptr size, BufferedStackTrace *stack,
186                      AllocType alloc_type);
187
188 void *asan_malloc(uptr size, BufferedStackTrace *stack);
189 void *asan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
190 void *asan_realloc(void *p, uptr size, BufferedStackTrace *stack);
191 void *asan_valloc(uptr size, BufferedStackTrace *stack);
192 void *asan_pvalloc(uptr size, BufferedStackTrace *stack);
193
194 int asan_posix_memalign(void **memptr, uptr alignment, uptr size,
195                         BufferedStackTrace *stack);
196 uptr asan_malloc_usable_size(const void *ptr, uptr pc, uptr bp);
197
198 uptr asan_mz_size(const void *ptr);
199 void asan_mz_force_lock();
200 void asan_mz_force_unlock();
201
202 void PrintInternalAllocatorStats();
203 void AsanSoftRssLimitExceededCallback(bool exceeded);
204
205 }  // namespace __asan
206 #endif  // ASAN_ALLOCATOR_H