]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_allocator_combined.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r305575, and update
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_allocator_combined.h
1 //===-- sanitizer_allocator_combined.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 // Part of the Sanitizer Allocator.
11 //
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_ALLOCATOR_H
14 #error This file must be included inside sanitizer_allocator.h
15 #endif
16
17 // This class implements a complete memory allocator by using two
18 // internal allocators:
19 // PrimaryAllocator is efficient, but may not allocate some sizes (alignments).
20 //  When allocating 2^x bytes it should return 2^x aligned chunk.
21 // PrimaryAllocator is used via a local AllocatorCache.
22 // SecondaryAllocator can allocate anything, but is not efficient.
23 template <class PrimaryAllocator, class AllocatorCache,
24           class SecondaryAllocator>  // NOLINT
25 class CombinedAllocator {
26  public:
27   void InitCommon(bool may_return_null, s32 release_to_os_interval_ms) {
28     primary_.Init(release_to_os_interval_ms);
29     atomic_store(&may_return_null_, may_return_null, memory_order_relaxed);
30   }
31
32   void InitLinkerInitialized(
33       bool may_return_null, s32 release_to_os_interval_ms) {
34     secondary_.InitLinkerInitialized(may_return_null);
35     stats_.InitLinkerInitialized();
36     InitCommon(may_return_null, release_to_os_interval_ms);
37   }
38
39   void Init(bool may_return_null, s32 release_to_os_interval_ms) {
40     secondary_.Init(may_return_null);
41     stats_.Init();
42     InitCommon(may_return_null, release_to_os_interval_ms);
43   }
44
45   void *Allocate(AllocatorCache *cache, uptr size, uptr alignment,
46                  bool cleared = false) {
47     // Returning 0 on malloc(0) may break a lot of code.
48     if (size == 0)
49       size = 1;
50     if (size + alignment < size)
51       return ReturnNullOrDieOnBadRequest();
52     uptr original_size = size;
53     // If alignment requirements are to be fulfilled by the frontend allocator
54     // rather than by the primary or secondary, passing an alignment lower than
55     // or equal to 8 will prevent any further rounding up, as well as the later
56     // alignment check.
57     if (alignment > 8)
58       size = RoundUpTo(size, alignment);
59     void *res;
60     bool from_primary = primary_.CanAllocate(size, alignment);
61     // The primary allocator should return a 2^x aligned allocation when
62     // requested 2^x bytes, hence using the rounded up 'size' when being
63     // serviced by the primary (this is no longer true when the primary is
64     // using a non-fixed base address). The secondary takes care of the
65     // alignment without such requirement, and allocating 'size' would use
66     // extraneous memory, so we employ 'original_size'.
67     if (from_primary)
68       res = cache->Allocate(&primary_, primary_.ClassID(size));
69     else
70       res = secondary_.Allocate(&stats_, original_size, alignment);
71     if (alignment > 8)
72       CHECK_EQ(reinterpret_cast<uptr>(res) & (alignment - 1), 0);
73     // When serviced by the secondary, the chunk comes from a mmap allocation
74     // and will be zero'd out anyway. We only need to clear our the chunk if
75     // it was serviced by the primary, hence using the rounded up 'size'.
76     if (cleared && res && from_primary)
77       internal_bzero_aligned16(res, RoundUpTo(size, 16));
78     return res;
79   }
80
81   bool MayReturnNull() const {
82     return atomic_load(&may_return_null_, memory_order_acquire);
83   }
84
85   void *ReturnNullOrDieOnBadRequest() {
86     if (MayReturnNull())
87       return nullptr;
88     ReportAllocatorCannotReturnNull(false);
89   }
90
91   void *ReturnNullOrDieOnOOM() {
92     if (MayReturnNull())
93       return nullptr;
94     ReportAllocatorCannotReturnNull(true);
95   }
96
97   void SetMayReturnNull(bool may_return_null) {
98     secondary_.SetMayReturnNull(may_return_null);
99     atomic_store(&may_return_null_, may_return_null, memory_order_release);
100   }
101
102   s32 ReleaseToOSIntervalMs() const {
103     return primary_.ReleaseToOSIntervalMs();
104   }
105
106   void SetReleaseToOSIntervalMs(s32 release_to_os_interval_ms) {
107     primary_.SetReleaseToOSIntervalMs(release_to_os_interval_ms);
108   }
109
110   void Deallocate(AllocatorCache *cache, void *p) {
111     if (!p) return;
112     if (primary_.PointerIsMine(p))
113       cache->Deallocate(&primary_, primary_.GetSizeClass(p), p);
114     else
115       secondary_.Deallocate(&stats_, p);
116   }
117
118   void *Reallocate(AllocatorCache *cache, void *p, uptr new_size,
119                    uptr alignment) {
120     if (!p)
121       return Allocate(cache, new_size, alignment);
122     if (!new_size) {
123       Deallocate(cache, p);
124       return nullptr;
125     }
126     CHECK(PointerIsMine(p));
127     uptr old_size = GetActuallyAllocatedSize(p);
128     uptr memcpy_size = Min(new_size, old_size);
129     void *new_p = Allocate(cache, new_size, alignment);
130     if (new_p)
131       internal_memcpy(new_p, p, memcpy_size);
132     Deallocate(cache, p);
133     return new_p;
134   }
135
136   bool PointerIsMine(void *p) {
137     if (primary_.PointerIsMine(p))
138       return true;
139     return secondary_.PointerIsMine(p);
140   }
141
142   bool FromPrimary(void *p) {
143     return primary_.PointerIsMine(p);
144   }
145
146   void *GetMetaData(const void *p) {
147     if (primary_.PointerIsMine(p))
148       return primary_.GetMetaData(p);
149     return secondary_.GetMetaData(p);
150   }
151
152   void *GetBlockBegin(const void *p) {
153     if (primary_.PointerIsMine(p))
154       return primary_.GetBlockBegin(p);
155     return secondary_.GetBlockBegin(p);
156   }
157
158   // This function does the same as GetBlockBegin, but is much faster.
159   // Must be called with the allocator locked.
160   void *GetBlockBeginFastLocked(void *p) {
161     if (primary_.PointerIsMine(p))
162       return primary_.GetBlockBegin(p);
163     return secondary_.GetBlockBeginFastLocked(p);
164   }
165
166   uptr GetActuallyAllocatedSize(void *p) {
167     if (primary_.PointerIsMine(p))
168       return primary_.GetActuallyAllocatedSize(p);
169     return secondary_.GetActuallyAllocatedSize(p);
170   }
171
172   uptr TotalMemoryUsed() {
173     return primary_.TotalMemoryUsed() + secondary_.TotalMemoryUsed();
174   }
175
176   void TestOnlyUnmap() { primary_.TestOnlyUnmap(); }
177
178   void InitCache(AllocatorCache *cache) {
179     cache->Init(&stats_);
180   }
181
182   void DestroyCache(AllocatorCache *cache) {
183     cache->Destroy(&primary_, &stats_);
184   }
185
186   void SwallowCache(AllocatorCache *cache) {
187     cache->Drain(&primary_);
188   }
189
190   void GetStats(AllocatorStatCounters s) const {
191     stats_.Get(s);
192   }
193
194   void PrintStats() {
195     primary_.PrintStats();
196     secondary_.PrintStats();
197   }
198
199   // ForceLock() and ForceUnlock() are needed to implement Darwin malloc zone
200   // introspection API.
201   void ForceLock() {
202     primary_.ForceLock();
203     secondary_.ForceLock();
204   }
205
206   void ForceUnlock() {
207     secondary_.ForceUnlock();
208     primary_.ForceUnlock();
209   }
210
211   // Iterate over all existing chunks.
212   // The allocator must be locked when calling this function.
213   void ForEachChunk(ForEachChunkCallback callback, void *arg) {
214     primary_.ForEachChunk(callback, arg);
215     secondary_.ForEachChunk(callback, arg);
216   }
217
218  private:
219   PrimaryAllocator primary_;
220   SecondaryAllocator secondary_;
221   AllocatorGlobalStats stats_;
222   atomic_uint8_t may_return_null_;
223 };
224