xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/memprof/memprof_allocator.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1e8d8bef9SDimitry Andric //===-- memprof_allocator.cpp --------------------------------------------===//
2e8d8bef9SDimitry Andric //
3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
9e8d8bef9SDimitry Andric // This file is a part of MemProfiler, a memory profiler.
10e8d8bef9SDimitry Andric //
11e8d8bef9SDimitry Andric // Implementation of MemProf's memory allocator, which uses the allocator
12e8d8bef9SDimitry Andric // from sanitizer_common.
13e8d8bef9SDimitry Andric //
14e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
15e8d8bef9SDimitry Andric 
16e8d8bef9SDimitry Andric #include "memprof_allocator.h"
17e8d8bef9SDimitry Andric #include "memprof_mapping.h"
18e8d8bef9SDimitry Andric #include "memprof_stack.h"
19e8d8bef9SDimitry Andric #include "memprof_thread.h"
20e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_allocator_checks.h"
21e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_allocator_interface.h"
22e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_allocator_report.h"
23e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_errno.h"
24e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_file.h"
25e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_flags.h"
26e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h"
27e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_list.h"
28e8d8bef9SDimitry Andric #include "sanitizer_common/sanitizer_stackdepot.h"
29e8d8bef9SDimitry Andric 
30e8d8bef9SDimitry Andric #include <sched.h>
31e8d8bef9SDimitry Andric #include <stdlib.h>
32e8d8bef9SDimitry Andric #include <time.h>
33e8d8bef9SDimitry Andric 
34e8d8bef9SDimitry Andric namespace __memprof {
35e8d8bef9SDimitry Andric 
36e8d8bef9SDimitry Andric static int GetCpuId(void) {
37e8d8bef9SDimitry Andric   // _memprof_preinit is called via the preinit_array, which subsequently calls
38e8d8bef9SDimitry Andric   // malloc. Since this is before _dl_init calls VDSO_SETUP, sched_getcpu
39e8d8bef9SDimitry Andric   // will seg fault as the address of __vdso_getcpu will be null.
40e8d8bef9SDimitry Andric   if (!memprof_init_done)
41e8d8bef9SDimitry Andric     return -1;
42e8d8bef9SDimitry Andric   return sched_getcpu();
43e8d8bef9SDimitry Andric }
44e8d8bef9SDimitry Andric 
45e8d8bef9SDimitry Andric // Compute the timestamp in ms.
46e8d8bef9SDimitry Andric static int GetTimestamp(void) {
47e8d8bef9SDimitry Andric   // timespec_get will segfault if called from dl_init
48e8d8bef9SDimitry Andric   if (!memprof_timestamp_inited) {
49e8d8bef9SDimitry Andric     // By returning 0, this will be effectively treated as being
50e8d8bef9SDimitry Andric     // timestamped at memprof init time (when memprof_init_timestamp_s
51e8d8bef9SDimitry Andric     // is initialized).
52e8d8bef9SDimitry Andric     return 0;
53e8d8bef9SDimitry Andric   }
54e8d8bef9SDimitry Andric   timespec ts;
55e8d8bef9SDimitry Andric   clock_gettime(CLOCK_REALTIME, &ts);
56e8d8bef9SDimitry Andric   return (ts.tv_sec - memprof_init_timestamp_s) * 1000 + ts.tv_nsec / 1000000;
57e8d8bef9SDimitry Andric }
58e8d8bef9SDimitry Andric 
59e8d8bef9SDimitry Andric static MemprofAllocator &get_allocator();
60e8d8bef9SDimitry Andric 
61e8d8bef9SDimitry Andric // The memory chunk allocated from the underlying allocator looks like this:
62e8d8bef9SDimitry Andric // H H U U U U U U
63e8d8bef9SDimitry Andric //   H -- ChunkHeader (32 bytes)
64e8d8bef9SDimitry Andric //   U -- user memory.
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric // If there is left padding before the ChunkHeader (due to use of memalign),
67e8d8bef9SDimitry Andric // we store a magic value in the first uptr word of the memory block and
68e8d8bef9SDimitry Andric // store the address of ChunkHeader in the next uptr.
69e8d8bef9SDimitry Andric // M B L L L L L L L L L  H H U U U U U U
70e8d8bef9SDimitry Andric //   |                    ^
71e8d8bef9SDimitry Andric //   ---------------------|
72e8d8bef9SDimitry Andric //   M -- magic value kAllocBegMagic
73e8d8bef9SDimitry Andric //   B -- address of ChunkHeader pointing to the first 'H'
74e8d8bef9SDimitry Andric 
75e8d8bef9SDimitry Andric constexpr uptr kMaxAllowedMallocBits = 40;
76e8d8bef9SDimitry Andric 
77e8d8bef9SDimitry Andric // Should be no more than 32-bytes
78e8d8bef9SDimitry Andric struct ChunkHeader {
79e8d8bef9SDimitry Andric   // 1-st 4 bytes.
80e8d8bef9SDimitry Andric   u32 alloc_context_id;
81e8d8bef9SDimitry Andric   // 2-nd 4 bytes
82e8d8bef9SDimitry Andric   u32 cpu_id;
83e8d8bef9SDimitry Andric   // 3-rd 4 bytes
84e8d8bef9SDimitry Andric   u32 timestamp_ms;
85e8d8bef9SDimitry Andric   // 4-th 4 bytes
86e8d8bef9SDimitry Andric   // Note only 1 bit is needed for this flag if we need space in the future for
87e8d8bef9SDimitry Andric   // more fields.
88e8d8bef9SDimitry Andric   u32 from_memalign;
89e8d8bef9SDimitry Andric   // 5-th and 6-th 4 bytes
90e8d8bef9SDimitry Andric   // The max size of an allocation is 2^40 (kMaxAllowedMallocSize), so this
91e8d8bef9SDimitry Andric   // could be shrunk to kMaxAllowedMallocBits if we need space in the future for
92e8d8bef9SDimitry Andric   // more fields.
93e8d8bef9SDimitry Andric   atomic_uint64_t user_requested_size;
94e8d8bef9SDimitry Andric   // 23 bits available
95e8d8bef9SDimitry Andric   // 7-th and 8-th 4 bytes
96e8d8bef9SDimitry Andric   u64 data_type_id; // TODO: hash of type name
97e8d8bef9SDimitry Andric };
98e8d8bef9SDimitry Andric 
99e8d8bef9SDimitry Andric static const uptr kChunkHeaderSize = sizeof(ChunkHeader);
100e8d8bef9SDimitry Andric COMPILER_CHECK(kChunkHeaderSize == 32);
101e8d8bef9SDimitry Andric 
102e8d8bef9SDimitry Andric struct MemprofChunk : ChunkHeader {
103e8d8bef9SDimitry Andric   uptr Beg() { return reinterpret_cast<uptr>(this) + kChunkHeaderSize; }
104e8d8bef9SDimitry Andric   uptr UsedSize() {
105e8d8bef9SDimitry Andric     return atomic_load(&user_requested_size, memory_order_relaxed);
106e8d8bef9SDimitry Andric   }
107e8d8bef9SDimitry Andric   void *AllocBeg() {
108e8d8bef9SDimitry Andric     if (from_memalign)
109e8d8bef9SDimitry Andric       return get_allocator().GetBlockBegin(reinterpret_cast<void *>(this));
110e8d8bef9SDimitry Andric     return reinterpret_cast<void *>(this);
111e8d8bef9SDimitry Andric   }
112e8d8bef9SDimitry Andric };
113e8d8bef9SDimitry Andric 
114e8d8bef9SDimitry Andric class LargeChunkHeader {
115e8d8bef9SDimitry Andric   static constexpr uptr kAllocBegMagic =
116e8d8bef9SDimitry Andric       FIRST_32_SECOND_64(0xCC6E96B9, 0xCC6E96B9CC6E96B9ULL);
117e8d8bef9SDimitry Andric   atomic_uintptr_t magic;
118e8d8bef9SDimitry Andric   MemprofChunk *chunk_header;
119e8d8bef9SDimitry Andric 
120e8d8bef9SDimitry Andric public:
121e8d8bef9SDimitry Andric   MemprofChunk *Get() const {
122e8d8bef9SDimitry Andric     return atomic_load(&magic, memory_order_acquire) == kAllocBegMagic
123e8d8bef9SDimitry Andric                ? chunk_header
124e8d8bef9SDimitry Andric                : nullptr;
125e8d8bef9SDimitry Andric   }
126e8d8bef9SDimitry Andric 
127e8d8bef9SDimitry Andric   void Set(MemprofChunk *p) {
128e8d8bef9SDimitry Andric     if (p) {
129e8d8bef9SDimitry Andric       chunk_header = p;
130e8d8bef9SDimitry Andric       atomic_store(&magic, kAllocBegMagic, memory_order_release);
131e8d8bef9SDimitry Andric       return;
132e8d8bef9SDimitry Andric     }
133e8d8bef9SDimitry Andric 
134e8d8bef9SDimitry Andric     uptr old = kAllocBegMagic;
135e8d8bef9SDimitry Andric     if (!atomic_compare_exchange_strong(&magic, &old, 0,
136e8d8bef9SDimitry Andric                                         memory_order_release)) {
137e8d8bef9SDimitry Andric       CHECK_EQ(old, kAllocBegMagic);
138e8d8bef9SDimitry Andric     }
139e8d8bef9SDimitry Andric   }
140e8d8bef9SDimitry Andric };
141e8d8bef9SDimitry Andric 
142e8d8bef9SDimitry Andric void FlushUnneededMemProfShadowMemory(uptr p, uptr size) {
143e8d8bef9SDimitry Andric   // Since memprof's mapping is compacting, the shadow chunk may be
144e8d8bef9SDimitry Andric   // not page-aligned, so we only flush the page-aligned portion.
145e8d8bef9SDimitry Andric   ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
146e8d8bef9SDimitry Andric }
147e8d8bef9SDimitry Andric 
148e8d8bef9SDimitry Andric void MemprofMapUnmapCallback::OnMap(uptr p, uptr size) const {
149e8d8bef9SDimitry Andric   // Statistics.
150e8d8bef9SDimitry Andric   MemprofStats &thread_stats = GetCurrentThreadStats();
151e8d8bef9SDimitry Andric   thread_stats.mmaps++;
152e8d8bef9SDimitry Andric   thread_stats.mmaped += size;
153e8d8bef9SDimitry Andric }
154e8d8bef9SDimitry Andric void MemprofMapUnmapCallback::OnUnmap(uptr p, uptr size) const {
155e8d8bef9SDimitry Andric   // We are about to unmap a chunk of user memory.
156e8d8bef9SDimitry Andric   // Mark the corresponding shadow memory as not needed.
157e8d8bef9SDimitry Andric   FlushUnneededMemProfShadowMemory(p, size);
158e8d8bef9SDimitry Andric   // Statistics.
159e8d8bef9SDimitry Andric   MemprofStats &thread_stats = GetCurrentThreadStats();
160e8d8bef9SDimitry Andric   thread_stats.munmaps++;
161e8d8bef9SDimitry Andric   thread_stats.munmaped += size;
162e8d8bef9SDimitry Andric }
163e8d8bef9SDimitry Andric 
164e8d8bef9SDimitry Andric AllocatorCache *GetAllocatorCache(MemprofThreadLocalMallocStorage *ms) {
165e8d8bef9SDimitry Andric   CHECK(ms);
166e8d8bef9SDimitry Andric   return &ms->allocator_cache;
167e8d8bef9SDimitry Andric }
168e8d8bef9SDimitry Andric 
169e8d8bef9SDimitry Andric struct MemInfoBlock {
170e8d8bef9SDimitry Andric   u32 alloc_count;
171e8d8bef9SDimitry Andric   u64 total_access_count, min_access_count, max_access_count;
172e8d8bef9SDimitry Andric   u64 total_size;
173e8d8bef9SDimitry Andric   u32 min_size, max_size;
174e8d8bef9SDimitry Andric   u32 alloc_timestamp, dealloc_timestamp;
175e8d8bef9SDimitry Andric   u64 total_lifetime;
176e8d8bef9SDimitry Andric   u32 min_lifetime, max_lifetime;
177e8d8bef9SDimitry Andric   u32 alloc_cpu_id, dealloc_cpu_id;
178e8d8bef9SDimitry Andric   u32 num_migrated_cpu;
179e8d8bef9SDimitry Andric 
180e8d8bef9SDimitry Andric   // Only compared to prior deallocated object currently.
181e8d8bef9SDimitry Andric   u32 num_lifetime_overlaps;
182e8d8bef9SDimitry Andric   u32 num_same_alloc_cpu;
183e8d8bef9SDimitry Andric   u32 num_same_dealloc_cpu;
184e8d8bef9SDimitry Andric 
185e8d8bef9SDimitry Andric   u64 data_type_id; // TODO: hash of type name
186e8d8bef9SDimitry Andric 
187e8d8bef9SDimitry Andric   MemInfoBlock() : alloc_count(0) {}
188e8d8bef9SDimitry Andric 
189e8d8bef9SDimitry Andric   MemInfoBlock(u32 size, u64 access_count, u32 alloc_timestamp,
190e8d8bef9SDimitry Andric                u32 dealloc_timestamp, u32 alloc_cpu, u32 dealloc_cpu)
191e8d8bef9SDimitry Andric       : alloc_count(1), total_access_count(access_count),
192e8d8bef9SDimitry Andric         min_access_count(access_count), max_access_count(access_count),
193e8d8bef9SDimitry Andric         total_size(size), min_size(size), max_size(size),
194e8d8bef9SDimitry Andric         alloc_timestamp(alloc_timestamp), dealloc_timestamp(dealloc_timestamp),
195e8d8bef9SDimitry Andric         total_lifetime(dealloc_timestamp - alloc_timestamp),
196e8d8bef9SDimitry Andric         min_lifetime(total_lifetime), max_lifetime(total_lifetime),
197e8d8bef9SDimitry Andric         alloc_cpu_id(alloc_cpu), dealloc_cpu_id(dealloc_cpu),
198e8d8bef9SDimitry Andric         num_lifetime_overlaps(0), num_same_alloc_cpu(0),
199e8d8bef9SDimitry Andric         num_same_dealloc_cpu(0) {
200e8d8bef9SDimitry Andric     num_migrated_cpu = alloc_cpu_id != dealloc_cpu_id;
201e8d8bef9SDimitry Andric   }
202e8d8bef9SDimitry Andric 
203e8d8bef9SDimitry Andric   void Print(u64 id) {
204e8d8bef9SDimitry Andric     u64 p;
205e8d8bef9SDimitry Andric     if (flags()->print_terse) {
206e8d8bef9SDimitry Andric       p = total_size * 100 / alloc_count;
207e8d8bef9SDimitry Andric       Printf("MIB:%llu/%u/%d.%02d/%u/%u/", id, alloc_count, p / 100, p % 100,
208e8d8bef9SDimitry Andric              min_size, max_size);
209e8d8bef9SDimitry Andric       p = total_access_count * 100 / alloc_count;
210e8d8bef9SDimitry Andric       Printf("%d.%02d/%u/%u/", p / 100, p % 100, min_access_count,
211e8d8bef9SDimitry Andric              max_access_count);
212e8d8bef9SDimitry Andric       p = total_lifetime * 100 / alloc_count;
213e8d8bef9SDimitry Andric       Printf("%d.%02d/%u/%u/", p / 100, p % 100, min_lifetime, max_lifetime);
214e8d8bef9SDimitry Andric       Printf("%u/%u/%u/%u\n", num_migrated_cpu, num_lifetime_overlaps,
215e8d8bef9SDimitry Andric              num_same_alloc_cpu, num_same_dealloc_cpu);
216e8d8bef9SDimitry Andric     } else {
217e8d8bef9SDimitry Andric       p = total_size * 100 / alloc_count;
218e8d8bef9SDimitry Andric       Printf("Memory allocation stack id = %llu\n", id);
219e8d8bef9SDimitry Andric       Printf("\talloc_count %u, size (ave/min/max) %d.%02d / %u / %u\n",
220e8d8bef9SDimitry Andric              alloc_count, p / 100, p % 100, min_size, max_size);
221e8d8bef9SDimitry Andric       p = total_access_count * 100 / alloc_count;
222e8d8bef9SDimitry Andric       Printf("\taccess_count (ave/min/max): %d.%02d / %u / %u\n", p / 100,
223e8d8bef9SDimitry Andric              p % 100, min_access_count, max_access_count);
224e8d8bef9SDimitry Andric       p = total_lifetime * 100 / alloc_count;
225e8d8bef9SDimitry Andric       Printf("\tlifetime (ave/min/max): %d.%02d / %u / %u\n", p / 100, p % 100,
226e8d8bef9SDimitry Andric              min_lifetime, max_lifetime);
227e8d8bef9SDimitry Andric       Printf("\tnum migrated: %u, num lifetime overlaps: %u, num same alloc "
228e8d8bef9SDimitry Andric              "cpu: %u, num same dealloc_cpu: %u\n",
229e8d8bef9SDimitry Andric              num_migrated_cpu, num_lifetime_overlaps, num_same_alloc_cpu,
230e8d8bef9SDimitry Andric              num_same_dealloc_cpu);
231e8d8bef9SDimitry Andric     }
232e8d8bef9SDimitry Andric   }
233e8d8bef9SDimitry Andric 
234e8d8bef9SDimitry Andric   static void printHeader() {
235e8d8bef9SDimitry Andric     CHECK(flags()->print_terse);
236e8d8bef9SDimitry Andric     Printf("MIB:StackID/AllocCount/AveSize/MinSize/MaxSize/AveAccessCount/"
237e8d8bef9SDimitry Andric            "MinAccessCount/MaxAccessCount/AveLifetime/MinLifetime/MaxLifetime/"
238e8d8bef9SDimitry Andric            "NumMigratedCpu/NumLifetimeOverlaps/NumSameAllocCpu/"
239e8d8bef9SDimitry Andric            "NumSameDeallocCpu\n");
240e8d8bef9SDimitry Andric   }
241e8d8bef9SDimitry Andric 
242e8d8bef9SDimitry Andric   void Merge(MemInfoBlock &newMIB) {
243e8d8bef9SDimitry Andric     alloc_count += newMIB.alloc_count;
244e8d8bef9SDimitry Andric 
245e8d8bef9SDimitry Andric     total_access_count += newMIB.total_access_count;
246e8d8bef9SDimitry Andric     min_access_count = Min(min_access_count, newMIB.min_access_count);
247e8d8bef9SDimitry Andric     max_access_count = Max(max_access_count, newMIB.max_access_count);
248e8d8bef9SDimitry Andric 
249e8d8bef9SDimitry Andric     total_size += newMIB.total_size;
250e8d8bef9SDimitry Andric     min_size = Min(min_size, newMIB.min_size);
251e8d8bef9SDimitry Andric     max_size = Max(max_size, newMIB.max_size);
252e8d8bef9SDimitry Andric 
253e8d8bef9SDimitry Andric     total_lifetime += newMIB.total_lifetime;
254e8d8bef9SDimitry Andric     min_lifetime = Min(min_lifetime, newMIB.min_lifetime);
255e8d8bef9SDimitry Andric     max_lifetime = Max(max_lifetime, newMIB.max_lifetime);
256e8d8bef9SDimitry Andric 
257e8d8bef9SDimitry Andric     // We know newMIB was deallocated later, so just need to check if it was
258e8d8bef9SDimitry Andric     // allocated before last one deallocated.
259e8d8bef9SDimitry Andric     num_lifetime_overlaps += newMIB.alloc_timestamp < dealloc_timestamp;
260e8d8bef9SDimitry Andric     alloc_timestamp = newMIB.alloc_timestamp;
261e8d8bef9SDimitry Andric     dealloc_timestamp = newMIB.dealloc_timestamp;
262e8d8bef9SDimitry Andric 
263e8d8bef9SDimitry Andric     num_same_alloc_cpu += alloc_cpu_id == newMIB.alloc_cpu_id;
264e8d8bef9SDimitry Andric     num_same_dealloc_cpu += dealloc_cpu_id == newMIB.dealloc_cpu_id;
265e8d8bef9SDimitry Andric     alloc_cpu_id = newMIB.alloc_cpu_id;
266e8d8bef9SDimitry Andric     dealloc_cpu_id = newMIB.dealloc_cpu_id;
267e8d8bef9SDimitry Andric   }
268e8d8bef9SDimitry Andric };
269e8d8bef9SDimitry Andric 
270e8d8bef9SDimitry Andric static u32 AccessCount = 0;
271e8d8bef9SDimitry Andric static u32 MissCount = 0;
272e8d8bef9SDimitry Andric 
273e8d8bef9SDimitry Andric struct SetEntry {
274e8d8bef9SDimitry Andric   SetEntry() : id(0), MIB() {}
275e8d8bef9SDimitry Andric   bool Empty() { return id == 0; }
276e8d8bef9SDimitry Andric   void Print() {
277e8d8bef9SDimitry Andric     CHECK(!Empty());
278e8d8bef9SDimitry Andric     MIB.Print(id);
279e8d8bef9SDimitry Andric   }
280e8d8bef9SDimitry Andric   // The stack id
281e8d8bef9SDimitry Andric   u64 id;
282e8d8bef9SDimitry Andric   MemInfoBlock MIB;
283e8d8bef9SDimitry Andric };
284e8d8bef9SDimitry Andric 
285e8d8bef9SDimitry Andric struct CacheSet {
286e8d8bef9SDimitry Andric   enum { kSetSize = 4 };
287e8d8bef9SDimitry Andric 
288e8d8bef9SDimitry Andric   void PrintAll() {
289e8d8bef9SDimitry Andric     for (int i = 0; i < kSetSize; i++) {
290e8d8bef9SDimitry Andric       if (Entries[i].Empty())
291e8d8bef9SDimitry Andric         continue;
292e8d8bef9SDimitry Andric       Entries[i].Print();
293e8d8bef9SDimitry Andric     }
294e8d8bef9SDimitry Andric   }
295e8d8bef9SDimitry Andric   void insertOrMerge(u64 new_id, MemInfoBlock &newMIB) {
296e8d8bef9SDimitry Andric     AccessCount++;
297e8d8bef9SDimitry Andric     SetAccessCount++;
298e8d8bef9SDimitry Andric 
299e8d8bef9SDimitry Andric     for (int i = 0; i < kSetSize; i++) {
300e8d8bef9SDimitry Andric       auto id = Entries[i].id;
301e8d8bef9SDimitry Andric       // Check if this is a hit or an empty entry. Since we always move any
302e8d8bef9SDimitry Andric       // filled locations to the front of the array (see below), we don't need
303e8d8bef9SDimitry Andric       // to look after finding the first empty entry.
304e8d8bef9SDimitry Andric       if (id == new_id || !id) {
305e8d8bef9SDimitry Andric         if (id == 0) {
306e8d8bef9SDimitry Andric           Entries[i].id = new_id;
307e8d8bef9SDimitry Andric           Entries[i].MIB = newMIB;
308e8d8bef9SDimitry Andric         } else {
309e8d8bef9SDimitry Andric           Entries[i].MIB.Merge(newMIB);
310e8d8bef9SDimitry Andric         }
311e8d8bef9SDimitry Andric         // Assuming some id locality, we try to swap the matching entry
312e8d8bef9SDimitry Andric         // into the first set position.
313e8d8bef9SDimitry Andric         if (i != 0) {
314e8d8bef9SDimitry Andric           auto tmp = Entries[0];
315e8d8bef9SDimitry Andric           Entries[0] = Entries[i];
316e8d8bef9SDimitry Andric           Entries[i] = tmp;
317e8d8bef9SDimitry Andric         }
318e8d8bef9SDimitry Andric         return;
319e8d8bef9SDimitry Andric       }
320e8d8bef9SDimitry Andric     }
321e8d8bef9SDimitry Andric 
322e8d8bef9SDimitry Andric     // Miss
323e8d8bef9SDimitry Andric     MissCount++;
324e8d8bef9SDimitry Andric     SetMissCount++;
325e8d8bef9SDimitry Andric 
326e8d8bef9SDimitry Andric     // We try to find the entries with the lowest alloc count to be evicted:
327e8d8bef9SDimitry Andric     int min_idx = 0;
328e8d8bef9SDimitry Andric     u64 min_count = Entries[0].MIB.alloc_count;
329e8d8bef9SDimitry Andric     for (int i = 1; i < kSetSize; i++) {
330e8d8bef9SDimitry Andric       CHECK(!Entries[i].Empty());
331e8d8bef9SDimitry Andric       if (Entries[i].MIB.alloc_count < min_count) {
332e8d8bef9SDimitry Andric         min_idx = i;
333e8d8bef9SDimitry Andric         min_count = Entries[i].MIB.alloc_count;
334e8d8bef9SDimitry Andric       }
335e8d8bef9SDimitry Andric     }
336e8d8bef9SDimitry Andric 
337e8d8bef9SDimitry Andric     // Print the evicted entry profile information
338e8d8bef9SDimitry Andric     if (!flags()->print_terse)
339e8d8bef9SDimitry Andric       Printf("Evicted:\n");
340e8d8bef9SDimitry Andric     Entries[min_idx].Print();
341e8d8bef9SDimitry Andric 
342e8d8bef9SDimitry Andric     // Similar to the hit case, put new MIB in first set position.
343e8d8bef9SDimitry Andric     if (min_idx != 0)
344e8d8bef9SDimitry Andric       Entries[min_idx] = Entries[0];
345e8d8bef9SDimitry Andric     Entries[0].id = new_id;
346e8d8bef9SDimitry Andric     Entries[0].MIB = newMIB;
347e8d8bef9SDimitry Andric   }
348e8d8bef9SDimitry Andric 
349e8d8bef9SDimitry Andric   void PrintMissRate(int i) {
350e8d8bef9SDimitry Andric     u64 p = SetAccessCount ? SetMissCount * 10000ULL / SetAccessCount : 0;
351e8d8bef9SDimitry Andric     Printf("Set %d miss rate: %d / %d = %5d.%02d%%\n", i, SetMissCount,
352e8d8bef9SDimitry Andric            SetAccessCount, p / 100, p % 100);
353e8d8bef9SDimitry Andric   }
354e8d8bef9SDimitry Andric 
355e8d8bef9SDimitry Andric   SetEntry Entries[kSetSize];
356e8d8bef9SDimitry Andric   u32 SetAccessCount = 0;
357e8d8bef9SDimitry Andric   u32 SetMissCount = 0;
358e8d8bef9SDimitry Andric };
359e8d8bef9SDimitry Andric 
360e8d8bef9SDimitry Andric struct MemInfoBlockCache {
361e8d8bef9SDimitry Andric   MemInfoBlockCache() {
362e8d8bef9SDimitry Andric     if (common_flags()->print_module_map)
363e8d8bef9SDimitry Andric       DumpProcessMap();
364e8d8bef9SDimitry Andric     if (flags()->print_terse)
365e8d8bef9SDimitry Andric       MemInfoBlock::printHeader();
366e8d8bef9SDimitry Andric     Sets =
367e8d8bef9SDimitry Andric         (CacheSet *)malloc(sizeof(CacheSet) * flags()->mem_info_cache_entries);
368e8d8bef9SDimitry Andric     Constructed = true;
369e8d8bef9SDimitry Andric   }
370e8d8bef9SDimitry Andric 
371e8d8bef9SDimitry Andric   ~MemInfoBlockCache() { free(Sets); }
372e8d8bef9SDimitry Andric 
373e8d8bef9SDimitry Andric   void insertOrMerge(u64 new_id, MemInfoBlock &newMIB) {
374e8d8bef9SDimitry Andric     u64 hv = new_id;
375e8d8bef9SDimitry Andric 
376e8d8bef9SDimitry Andric     // Use mod method where number of entries should be a prime close to power
377e8d8bef9SDimitry Andric     // of 2.
378e8d8bef9SDimitry Andric     hv %= flags()->mem_info_cache_entries;
379e8d8bef9SDimitry Andric 
380e8d8bef9SDimitry Andric     return Sets[hv].insertOrMerge(new_id, newMIB);
381e8d8bef9SDimitry Andric   }
382e8d8bef9SDimitry Andric 
383e8d8bef9SDimitry Andric   void PrintAll() {
384e8d8bef9SDimitry Andric     for (int i = 0; i < flags()->mem_info_cache_entries; i++) {
385e8d8bef9SDimitry Andric       Sets[i].PrintAll();
386e8d8bef9SDimitry Andric     }
387e8d8bef9SDimitry Andric   }
388e8d8bef9SDimitry Andric 
389e8d8bef9SDimitry Andric   void PrintMissRate() {
390e8d8bef9SDimitry Andric     if (!flags()->print_mem_info_cache_miss_rate)
391e8d8bef9SDimitry Andric       return;
392e8d8bef9SDimitry Andric     u64 p = AccessCount ? MissCount * 10000ULL / AccessCount : 0;
393e8d8bef9SDimitry Andric     Printf("Overall miss rate: %d / %d = %5d.%02d%%\n", MissCount, AccessCount,
394e8d8bef9SDimitry Andric            p / 100, p % 100);
395e8d8bef9SDimitry Andric     if (flags()->print_mem_info_cache_miss_rate_details)
396e8d8bef9SDimitry Andric       for (int i = 0; i < flags()->mem_info_cache_entries; i++)
397e8d8bef9SDimitry Andric         Sets[i].PrintMissRate(i);
398e8d8bef9SDimitry Andric   }
399e8d8bef9SDimitry Andric 
400e8d8bef9SDimitry Andric   CacheSet *Sets;
401e8d8bef9SDimitry Andric   // Flag when the Sets have been allocated, in case a deallocation is called
402e8d8bef9SDimitry Andric   // very early before the static init of the Allocator and therefore this table
403e8d8bef9SDimitry Andric   // have completed.
404e8d8bef9SDimitry Andric   bool Constructed = false;
405e8d8bef9SDimitry Andric };
406e8d8bef9SDimitry Andric 
407e8d8bef9SDimitry Andric // Accumulates the access count from the shadow for the given pointer and size.
408e8d8bef9SDimitry Andric u64 GetShadowCount(uptr p, u32 size) {
409e8d8bef9SDimitry Andric   u64 *shadow = (u64 *)MEM_TO_SHADOW(p);
410e8d8bef9SDimitry Andric   u64 *shadow_end = (u64 *)MEM_TO_SHADOW(p + size);
411e8d8bef9SDimitry Andric   u64 count = 0;
412e8d8bef9SDimitry Andric   for (; shadow <= shadow_end; shadow++)
413e8d8bef9SDimitry Andric     count += *shadow;
414e8d8bef9SDimitry Andric   return count;
415e8d8bef9SDimitry Andric }
416e8d8bef9SDimitry Andric 
417e8d8bef9SDimitry Andric // Clears the shadow counters (when memory is allocated).
418e8d8bef9SDimitry Andric void ClearShadow(uptr addr, uptr size) {
419e8d8bef9SDimitry Andric   CHECK(AddrIsAlignedByGranularity(addr));
420e8d8bef9SDimitry Andric   CHECK(AddrIsInMem(addr));
421e8d8bef9SDimitry Andric   CHECK(AddrIsAlignedByGranularity(addr + size));
422e8d8bef9SDimitry Andric   CHECK(AddrIsInMem(addr + size - SHADOW_GRANULARITY));
423e8d8bef9SDimitry Andric   CHECK(REAL(memset));
424e8d8bef9SDimitry Andric   uptr shadow_beg = MEM_TO_SHADOW(addr);
425e8d8bef9SDimitry Andric   uptr shadow_end = MEM_TO_SHADOW(addr + size - SHADOW_GRANULARITY) + 1;
426e8d8bef9SDimitry Andric   if (shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
427e8d8bef9SDimitry Andric     REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
428e8d8bef9SDimitry Andric   } else {
429e8d8bef9SDimitry Andric     uptr page_size = GetPageSizeCached();
430e8d8bef9SDimitry Andric     uptr page_beg = RoundUpTo(shadow_beg, page_size);
431e8d8bef9SDimitry Andric     uptr page_end = RoundDownTo(shadow_end, page_size);
432e8d8bef9SDimitry Andric 
433e8d8bef9SDimitry Andric     if (page_beg >= page_end) {
434e8d8bef9SDimitry Andric       REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
435e8d8bef9SDimitry Andric     } else {
436e8d8bef9SDimitry Andric       if (page_beg != shadow_beg) {
437e8d8bef9SDimitry Andric         REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg);
438e8d8bef9SDimitry Andric       }
439e8d8bef9SDimitry Andric       if (page_end != shadow_end) {
440e8d8bef9SDimitry Andric         REAL(memset)((void *)page_end, 0, shadow_end - page_end);
441e8d8bef9SDimitry Andric       }
442e8d8bef9SDimitry Andric       ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr);
443e8d8bef9SDimitry Andric     }
444e8d8bef9SDimitry Andric   }
445e8d8bef9SDimitry Andric }
446e8d8bef9SDimitry Andric 
447e8d8bef9SDimitry Andric struct Allocator {
448e8d8bef9SDimitry Andric   static const uptr kMaxAllowedMallocSize = 1ULL << kMaxAllowedMallocBits;
449e8d8bef9SDimitry Andric 
450e8d8bef9SDimitry Andric   MemprofAllocator allocator;
451e8d8bef9SDimitry Andric   StaticSpinMutex fallback_mutex;
452e8d8bef9SDimitry Andric   AllocatorCache fallback_allocator_cache;
453e8d8bef9SDimitry Andric 
454e8d8bef9SDimitry Andric   uptr max_user_defined_malloc_size;
455e8d8bef9SDimitry Andric   atomic_uint8_t rss_limit_exceeded;
456e8d8bef9SDimitry Andric 
457e8d8bef9SDimitry Andric   MemInfoBlockCache MemInfoBlockTable;
458e8d8bef9SDimitry Andric   bool destructing;
459e8d8bef9SDimitry Andric 
460e8d8bef9SDimitry Andric   // ------------------- Initialization ------------------------
461e8d8bef9SDimitry Andric   explicit Allocator(LinkerInitialized) : destructing(false) {}
462e8d8bef9SDimitry Andric 
463e8d8bef9SDimitry Andric   ~Allocator() { FinishAndPrint(); }
464e8d8bef9SDimitry Andric 
465e8d8bef9SDimitry Andric   void FinishAndPrint() {
466e8d8bef9SDimitry Andric     if (!flags()->print_terse)
467e8d8bef9SDimitry Andric       Printf("Live on exit:\n");
468e8d8bef9SDimitry Andric     allocator.ForceLock();
469e8d8bef9SDimitry Andric     allocator.ForEachChunk(
470e8d8bef9SDimitry Andric         [](uptr chunk, void *alloc) {
471e8d8bef9SDimitry Andric           u64 user_requested_size;
472e8d8bef9SDimitry Andric           MemprofChunk *m =
473e8d8bef9SDimitry Andric               ((Allocator *)alloc)
474e8d8bef9SDimitry Andric                   ->GetMemprofChunk((void *)chunk, user_requested_size);
475e8d8bef9SDimitry Andric           if (!m)
476e8d8bef9SDimitry Andric             return;
477e8d8bef9SDimitry Andric           uptr user_beg = ((uptr)m) + kChunkHeaderSize;
478e8d8bef9SDimitry Andric           u64 c = GetShadowCount(user_beg, user_requested_size);
479e8d8bef9SDimitry Andric           long curtime = GetTimestamp();
480e8d8bef9SDimitry Andric           MemInfoBlock newMIB(user_requested_size, c, m->timestamp_ms, curtime,
481e8d8bef9SDimitry Andric                               m->cpu_id, GetCpuId());
482e8d8bef9SDimitry Andric           ((Allocator *)alloc)
483e8d8bef9SDimitry Andric               ->MemInfoBlockTable.insertOrMerge(m->alloc_context_id, newMIB);
484e8d8bef9SDimitry Andric         },
485e8d8bef9SDimitry Andric         this);
486e8d8bef9SDimitry Andric     allocator.ForceUnlock();
487e8d8bef9SDimitry Andric 
488e8d8bef9SDimitry Andric     destructing = true;
489e8d8bef9SDimitry Andric     MemInfoBlockTable.PrintMissRate();
490e8d8bef9SDimitry Andric     MemInfoBlockTable.PrintAll();
491e8d8bef9SDimitry Andric     StackDepotPrintAll();
492e8d8bef9SDimitry Andric   }
493e8d8bef9SDimitry Andric 
494e8d8bef9SDimitry Andric   void InitLinkerInitialized() {
495e8d8bef9SDimitry Andric     SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
496e8d8bef9SDimitry Andric     allocator.InitLinkerInitialized(
497e8d8bef9SDimitry Andric         common_flags()->allocator_release_to_os_interval_ms);
498e8d8bef9SDimitry Andric     max_user_defined_malloc_size = common_flags()->max_allocation_size_mb
499e8d8bef9SDimitry Andric                                        ? common_flags()->max_allocation_size_mb
500e8d8bef9SDimitry Andric                                              << 20
501e8d8bef9SDimitry Andric                                        : kMaxAllowedMallocSize;
502e8d8bef9SDimitry Andric   }
503e8d8bef9SDimitry Andric 
504e8d8bef9SDimitry Andric   bool RssLimitExceeded() {
505e8d8bef9SDimitry Andric     return atomic_load(&rss_limit_exceeded, memory_order_relaxed);
506e8d8bef9SDimitry Andric   }
507e8d8bef9SDimitry Andric 
508e8d8bef9SDimitry Andric   void SetRssLimitExceeded(bool limit_exceeded) {
509e8d8bef9SDimitry Andric     atomic_store(&rss_limit_exceeded, limit_exceeded, memory_order_relaxed);
510e8d8bef9SDimitry Andric   }
511e8d8bef9SDimitry Andric 
512e8d8bef9SDimitry Andric   // -------------------- Allocation/Deallocation routines ---------------
513e8d8bef9SDimitry Andric   void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack,
514e8d8bef9SDimitry Andric                  AllocType alloc_type) {
515e8d8bef9SDimitry Andric     if (UNLIKELY(!memprof_inited))
516e8d8bef9SDimitry Andric       MemprofInitFromRtl();
517e8d8bef9SDimitry Andric     if (RssLimitExceeded()) {
518e8d8bef9SDimitry Andric       if (AllocatorMayReturnNull())
519e8d8bef9SDimitry Andric         return nullptr;
520e8d8bef9SDimitry Andric       ReportRssLimitExceeded(stack);
521e8d8bef9SDimitry Andric     }
522e8d8bef9SDimitry Andric     CHECK(stack);
523e8d8bef9SDimitry Andric     const uptr min_alignment = MEMPROF_ALIGNMENT;
524e8d8bef9SDimitry Andric     if (alignment < min_alignment)
525e8d8bef9SDimitry Andric       alignment = min_alignment;
526e8d8bef9SDimitry Andric     if (size == 0) {
527e8d8bef9SDimitry Andric       // We'd be happy to avoid allocating memory for zero-size requests, but
528e8d8bef9SDimitry Andric       // some programs/tests depend on this behavior and assume that malloc
529e8d8bef9SDimitry Andric       // would not return NULL even for zero-size allocations. Moreover, it
530e8d8bef9SDimitry Andric       // looks like operator new should never return NULL, and results of
531e8d8bef9SDimitry Andric       // consecutive "new" calls must be different even if the allocated size
532e8d8bef9SDimitry Andric       // is zero.
533e8d8bef9SDimitry Andric       size = 1;
534e8d8bef9SDimitry Andric     }
535e8d8bef9SDimitry Andric     CHECK(IsPowerOfTwo(alignment));
536e8d8bef9SDimitry Andric     uptr rounded_size = RoundUpTo(size, alignment);
537e8d8bef9SDimitry Andric     uptr needed_size = rounded_size + kChunkHeaderSize;
538e8d8bef9SDimitry Andric     if (alignment > min_alignment)
539e8d8bef9SDimitry Andric       needed_size += alignment;
540e8d8bef9SDimitry Andric     CHECK(IsAligned(needed_size, min_alignment));
541e8d8bef9SDimitry Andric     if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize ||
542e8d8bef9SDimitry Andric         size > max_user_defined_malloc_size) {
543e8d8bef9SDimitry Andric       if (AllocatorMayReturnNull()) {
544e8d8bef9SDimitry Andric         Report("WARNING: MemProfiler failed to allocate 0x%zx bytes\n",
545e8d8bef9SDimitry Andric                (void *)size);
546e8d8bef9SDimitry Andric         return nullptr;
547e8d8bef9SDimitry Andric       }
548e8d8bef9SDimitry Andric       uptr malloc_limit =
549e8d8bef9SDimitry Andric           Min(kMaxAllowedMallocSize, max_user_defined_malloc_size);
550e8d8bef9SDimitry Andric       ReportAllocationSizeTooBig(size, malloc_limit, stack);
551e8d8bef9SDimitry Andric     }
552e8d8bef9SDimitry Andric 
553e8d8bef9SDimitry Andric     MemprofThread *t = GetCurrentThread();
554e8d8bef9SDimitry Andric     void *allocated;
555e8d8bef9SDimitry Andric     if (t) {
556e8d8bef9SDimitry Andric       AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
557e8d8bef9SDimitry Andric       allocated = allocator.Allocate(cache, needed_size, 8);
558e8d8bef9SDimitry Andric     } else {
559e8d8bef9SDimitry Andric       SpinMutexLock l(&fallback_mutex);
560e8d8bef9SDimitry Andric       AllocatorCache *cache = &fallback_allocator_cache;
561e8d8bef9SDimitry Andric       allocated = allocator.Allocate(cache, needed_size, 8);
562e8d8bef9SDimitry Andric     }
563e8d8bef9SDimitry Andric     if (UNLIKELY(!allocated)) {
564e8d8bef9SDimitry Andric       SetAllocatorOutOfMemory();
565e8d8bef9SDimitry Andric       if (AllocatorMayReturnNull())
566e8d8bef9SDimitry Andric         return nullptr;
567e8d8bef9SDimitry Andric       ReportOutOfMemory(size, stack);
568e8d8bef9SDimitry Andric     }
569e8d8bef9SDimitry Andric 
570e8d8bef9SDimitry Andric     uptr alloc_beg = reinterpret_cast<uptr>(allocated);
571e8d8bef9SDimitry Andric     uptr alloc_end = alloc_beg + needed_size;
572e8d8bef9SDimitry Andric     uptr beg_plus_header = alloc_beg + kChunkHeaderSize;
573e8d8bef9SDimitry Andric     uptr user_beg = beg_plus_header;
574e8d8bef9SDimitry Andric     if (!IsAligned(user_beg, alignment))
575e8d8bef9SDimitry Andric       user_beg = RoundUpTo(user_beg, alignment);
576e8d8bef9SDimitry Andric     uptr user_end = user_beg + size;
577e8d8bef9SDimitry Andric     CHECK_LE(user_end, alloc_end);
578e8d8bef9SDimitry Andric     uptr chunk_beg = user_beg - kChunkHeaderSize;
579e8d8bef9SDimitry Andric     MemprofChunk *m = reinterpret_cast<MemprofChunk *>(chunk_beg);
580e8d8bef9SDimitry Andric     m->from_memalign = alloc_beg != chunk_beg;
581e8d8bef9SDimitry Andric     CHECK(size);
582e8d8bef9SDimitry Andric 
583e8d8bef9SDimitry Andric     m->cpu_id = GetCpuId();
584e8d8bef9SDimitry Andric     m->timestamp_ms = GetTimestamp();
585e8d8bef9SDimitry Andric     m->alloc_context_id = StackDepotPut(*stack);
586e8d8bef9SDimitry Andric 
587e8d8bef9SDimitry Andric     uptr size_rounded_down_to_granularity =
588e8d8bef9SDimitry Andric         RoundDownTo(size, SHADOW_GRANULARITY);
589e8d8bef9SDimitry Andric     if (size_rounded_down_to_granularity)
590e8d8bef9SDimitry Andric       ClearShadow(user_beg, size_rounded_down_to_granularity);
591e8d8bef9SDimitry Andric 
592e8d8bef9SDimitry Andric     MemprofStats &thread_stats = GetCurrentThreadStats();
593e8d8bef9SDimitry Andric     thread_stats.mallocs++;
594e8d8bef9SDimitry Andric     thread_stats.malloced += size;
595e8d8bef9SDimitry Andric     thread_stats.malloced_overhead += needed_size - size;
596e8d8bef9SDimitry Andric     if (needed_size > SizeClassMap::kMaxSize)
597e8d8bef9SDimitry Andric       thread_stats.malloc_large++;
598e8d8bef9SDimitry Andric     else
599e8d8bef9SDimitry Andric       thread_stats.malloced_by_size[SizeClassMap::ClassID(needed_size)]++;
600e8d8bef9SDimitry Andric 
601e8d8bef9SDimitry Andric     void *res = reinterpret_cast<void *>(user_beg);
602e8d8bef9SDimitry Andric     atomic_store(&m->user_requested_size, size, memory_order_release);
603e8d8bef9SDimitry Andric     if (alloc_beg != chunk_beg) {
604e8d8bef9SDimitry Andric       CHECK_LE(alloc_beg + sizeof(LargeChunkHeader), chunk_beg);
605e8d8bef9SDimitry Andric       reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Set(m);
606e8d8bef9SDimitry Andric     }
607e8d8bef9SDimitry Andric     MEMPROF_MALLOC_HOOK(res, size);
608e8d8bef9SDimitry Andric     return res;
609e8d8bef9SDimitry Andric   }
610e8d8bef9SDimitry Andric 
611e8d8bef9SDimitry Andric   void Deallocate(void *ptr, uptr delete_size, uptr delete_alignment,
612e8d8bef9SDimitry Andric                   BufferedStackTrace *stack, AllocType alloc_type) {
613e8d8bef9SDimitry Andric     uptr p = reinterpret_cast<uptr>(ptr);
614e8d8bef9SDimitry Andric     if (p == 0)
615e8d8bef9SDimitry Andric       return;
616e8d8bef9SDimitry Andric 
617e8d8bef9SDimitry Andric     MEMPROF_FREE_HOOK(ptr);
618e8d8bef9SDimitry Andric 
619e8d8bef9SDimitry Andric     uptr chunk_beg = p - kChunkHeaderSize;
620e8d8bef9SDimitry Andric     MemprofChunk *m = reinterpret_cast<MemprofChunk *>(chunk_beg);
621e8d8bef9SDimitry Andric 
622e8d8bef9SDimitry Andric     u64 user_requested_size =
623e8d8bef9SDimitry Andric         atomic_exchange(&m->user_requested_size, 0, memory_order_acquire);
624e8d8bef9SDimitry Andric     if (memprof_inited && memprof_init_done && !destructing &&
625e8d8bef9SDimitry Andric         MemInfoBlockTable.Constructed) {
626e8d8bef9SDimitry Andric       u64 c = GetShadowCount(p, user_requested_size);
627e8d8bef9SDimitry Andric       long curtime = GetTimestamp();
628e8d8bef9SDimitry Andric 
629e8d8bef9SDimitry Andric       MemInfoBlock newMIB(user_requested_size, c, m->timestamp_ms, curtime,
630e8d8bef9SDimitry Andric                           m->cpu_id, GetCpuId());
631e8d8bef9SDimitry Andric       {
632e8d8bef9SDimitry Andric         SpinMutexLock l(&fallback_mutex);
633e8d8bef9SDimitry Andric         MemInfoBlockTable.insertOrMerge(m->alloc_context_id, newMIB);
634e8d8bef9SDimitry Andric       }
635e8d8bef9SDimitry Andric     }
636e8d8bef9SDimitry Andric 
637e8d8bef9SDimitry Andric     MemprofStats &thread_stats = GetCurrentThreadStats();
638e8d8bef9SDimitry Andric     thread_stats.frees++;
639e8d8bef9SDimitry Andric     thread_stats.freed += user_requested_size;
640e8d8bef9SDimitry Andric 
641e8d8bef9SDimitry Andric     void *alloc_beg = m->AllocBeg();
642e8d8bef9SDimitry Andric     if (alloc_beg != m) {
643e8d8bef9SDimitry Andric       // Clear the magic value, as allocator internals may overwrite the
644e8d8bef9SDimitry Andric       // contents of deallocated chunk, confusing GetMemprofChunk lookup.
645e8d8bef9SDimitry Andric       reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Set(nullptr);
646e8d8bef9SDimitry Andric     }
647e8d8bef9SDimitry Andric 
648e8d8bef9SDimitry Andric     MemprofThread *t = GetCurrentThread();
649e8d8bef9SDimitry Andric     if (t) {
650e8d8bef9SDimitry Andric       AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
651e8d8bef9SDimitry Andric       allocator.Deallocate(cache, alloc_beg);
652e8d8bef9SDimitry Andric     } else {
653e8d8bef9SDimitry Andric       SpinMutexLock l(&fallback_mutex);
654e8d8bef9SDimitry Andric       AllocatorCache *cache = &fallback_allocator_cache;
655e8d8bef9SDimitry Andric       allocator.Deallocate(cache, alloc_beg);
656e8d8bef9SDimitry Andric     }
657e8d8bef9SDimitry Andric   }
658e8d8bef9SDimitry Andric 
659e8d8bef9SDimitry Andric   void *Reallocate(void *old_ptr, uptr new_size, BufferedStackTrace *stack) {
660e8d8bef9SDimitry Andric     CHECK(old_ptr && new_size);
661e8d8bef9SDimitry Andric     uptr p = reinterpret_cast<uptr>(old_ptr);
662e8d8bef9SDimitry Andric     uptr chunk_beg = p - kChunkHeaderSize;
663e8d8bef9SDimitry Andric     MemprofChunk *m = reinterpret_cast<MemprofChunk *>(chunk_beg);
664e8d8bef9SDimitry Andric 
665e8d8bef9SDimitry Andric     MemprofStats &thread_stats = GetCurrentThreadStats();
666e8d8bef9SDimitry Andric     thread_stats.reallocs++;
667e8d8bef9SDimitry Andric     thread_stats.realloced += new_size;
668e8d8bef9SDimitry Andric 
669e8d8bef9SDimitry Andric     void *new_ptr = Allocate(new_size, 8, stack, FROM_MALLOC);
670e8d8bef9SDimitry Andric     if (new_ptr) {
671e8d8bef9SDimitry Andric       CHECK_NE(REAL(memcpy), nullptr);
672e8d8bef9SDimitry Andric       uptr memcpy_size = Min(new_size, m->UsedSize());
673e8d8bef9SDimitry Andric       REAL(memcpy)(new_ptr, old_ptr, memcpy_size);
674e8d8bef9SDimitry Andric       Deallocate(old_ptr, 0, 0, stack, FROM_MALLOC);
675e8d8bef9SDimitry Andric     }
676e8d8bef9SDimitry Andric     return new_ptr;
677e8d8bef9SDimitry Andric   }
678e8d8bef9SDimitry Andric 
679e8d8bef9SDimitry Andric   void *Calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {
680e8d8bef9SDimitry Andric     if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
681e8d8bef9SDimitry Andric       if (AllocatorMayReturnNull())
682e8d8bef9SDimitry Andric         return nullptr;
683e8d8bef9SDimitry Andric       ReportCallocOverflow(nmemb, size, stack);
684e8d8bef9SDimitry Andric     }
685e8d8bef9SDimitry Andric     void *ptr = Allocate(nmemb * size, 8, stack, FROM_MALLOC);
686e8d8bef9SDimitry Andric     // If the memory comes from the secondary allocator no need to clear it
687e8d8bef9SDimitry Andric     // as it comes directly from mmap.
688e8d8bef9SDimitry Andric     if (ptr && allocator.FromPrimary(ptr))
689e8d8bef9SDimitry Andric       REAL(memset)(ptr, 0, nmemb * size);
690e8d8bef9SDimitry Andric     return ptr;
691e8d8bef9SDimitry Andric   }
692e8d8bef9SDimitry Andric 
693e8d8bef9SDimitry Andric   void CommitBack(MemprofThreadLocalMallocStorage *ms,
694e8d8bef9SDimitry Andric                   BufferedStackTrace *stack) {
695e8d8bef9SDimitry Andric     AllocatorCache *ac = GetAllocatorCache(ms);
696e8d8bef9SDimitry Andric     allocator.SwallowCache(ac);
697e8d8bef9SDimitry Andric   }
698e8d8bef9SDimitry Andric 
699e8d8bef9SDimitry Andric   // -------------------------- Chunk lookup ----------------------
700e8d8bef9SDimitry Andric 
701e8d8bef9SDimitry Andric   // Assumes alloc_beg == allocator.GetBlockBegin(alloc_beg).
702e8d8bef9SDimitry Andric   MemprofChunk *GetMemprofChunk(void *alloc_beg, u64 &user_requested_size) {
703e8d8bef9SDimitry Andric     if (!alloc_beg)
704e8d8bef9SDimitry Andric       return nullptr;
705e8d8bef9SDimitry Andric     MemprofChunk *p = reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Get();
706e8d8bef9SDimitry Andric     if (!p) {
707e8d8bef9SDimitry Andric       if (!allocator.FromPrimary(alloc_beg))
708e8d8bef9SDimitry Andric         return nullptr;
709e8d8bef9SDimitry Andric       p = reinterpret_cast<MemprofChunk *>(alloc_beg);
710e8d8bef9SDimitry Andric     }
711e8d8bef9SDimitry Andric     // The size is reset to 0 on deallocation (and a min of 1 on
712e8d8bef9SDimitry Andric     // allocation).
713e8d8bef9SDimitry Andric     user_requested_size =
714e8d8bef9SDimitry Andric         atomic_load(&p->user_requested_size, memory_order_acquire);
715e8d8bef9SDimitry Andric     if (user_requested_size)
716e8d8bef9SDimitry Andric       return p;
717e8d8bef9SDimitry Andric     return nullptr;
718e8d8bef9SDimitry Andric   }
719e8d8bef9SDimitry Andric 
720e8d8bef9SDimitry Andric   MemprofChunk *GetMemprofChunkByAddr(uptr p, u64 &user_requested_size) {
721e8d8bef9SDimitry Andric     void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p));
722e8d8bef9SDimitry Andric     return GetMemprofChunk(alloc_beg, user_requested_size);
723e8d8bef9SDimitry Andric   }
724e8d8bef9SDimitry Andric 
725e8d8bef9SDimitry Andric   uptr AllocationSize(uptr p) {
726e8d8bef9SDimitry Andric     u64 user_requested_size;
727e8d8bef9SDimitry Andric     MemprofChunk *m = GetMemprofChunkByAddr(p, user_requested_size);
728e8d8bef9SDimitry Andric     if (!m)
729e8d8bef9SDimitry Andric       return 0;
730e8d8bef9SDimitry Andric     if (m->Beg() != p)
731e8d8bef9SDimitry Andric       return 0;
732e8d8bef9SDimitry Andric     return user_requested_size;
733e8d8bef9SDimitry Andric   }
734e8d8bef9SDimitry Andric 
735e8d8bef9SDimitry Andric   void Purge(BufferedStackTrace *stack) { allocator.ForceReleaseToOS(); }
736e8d8bef9SDimitry Andric 
737e8d8bef9SDimitry Andric   void PrintStats() { allocator.PrintStats(); }
738e8d8bef9SDimitry Andric 
739*fe6060f1SDimitry Andric   void ForceLock() NO_THREAD_SAFETY_ANALYSIS {
740e8d8bef9SDimitry Andric     allocator.ForceLock();
741e8d8bef9SDimitry Andric     fallback_mutex.Lock();
742e8d8bef9SDimitry Andric   }
743e8d8bef9SDimitry Andric 
744*fe6060f1SDimitry Andric   void ForceUnlock() NO_THREAD_SAFETY_ANALYSIS {
745e8d8bef9SDimitry Andric     fallback_mutex.Unlock();
746e8d8bef9SDimitry Andric     allocator.ForceUnlock();
747e8d8bef9SDimitry Andric   }
748e8d8bef9SDimitry Andric };
749e8d8bef9SDimitry Andric 
750e8d8bef9SDimitry Andric static Allocator instance(LINKER_INITIALIZED);
751e8d8bef9SDimitry Andric 
752e8d8bef9SDimitry Andric static MemprofAllocator &get_allocator() { return instance.allocator; }
753e8d8bef9SDimitry Andric 
754e8d8bef9SDimitry Andric void InitializeAllocator() { instance.InitLinkerInitialized(); }
755e8d8bef9SDimitry Andric 
756e8d8bef9SDimitry Andric void MemprofThreadLocalMallocStorage::CommitBack() {
757e8d8bef9SDimitry Andric   GET_STACK_TRACE_MALLOC;
758e8d8bef9SDimitry Andric   instance.CommitBack(this, &stack);
759e8d8bef9SDimitry Andric }
760e8d8bef9SDimitry Andric 
761e8d8bef9SDimitry Andric void PrintInternalAllocatorStats() { instance.PrintStats(); }
762e8d8bef9SDimitry Andric 
763e8d8bef9SDimitry Andric void memprof_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type) {
764e8d8bef9SDimitry Andric   instance.Deallocate(ptr, 0, 0, stack, alloc_type);
765e8d8bef9SDimitry Andric }
766e8d8bef9SDimitry Andric 
767e8d8bef9SDimitry Andric void memprof_delete(void *ptr, uptr size, uptr alignment,
768e8d8bef9SDimitry Andric                     BufferedStackTrace *stack, AllocType alloc_type) {
769e8d8bef9SDimitry Andric   instance.Deallocate(ptr, size, alignment, stack, alloc_type);
770e8d8bef9SDimitry Andric }
771e8d8bef9SDimitry Andric 
772e8d8bef9SDimitry Andric void *memprof_malloc(uptr size, BufferedStackTrace *stack) {
773e8d8bef9SDimitry Andric   return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC));
774e8d8bef9SDimitry Andric }
775e8d8bef9SDimitry Andric 
776e8d8bef9SDimitry Andric void *memprof_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {
777e8d8bef9SDimitry Andric   return SetErrnoOnNull(instance.Calloc(nmemb, size, stack));
778e8d8bef9SDimitry Andric }
779e8d8bef9SDimitry Andric 
780e8d8bef9SDimitry Andric void *memprof_reallocarray(void *p, uptr nmemb, uptr size,
781e8d8bef9SDimitry Andric                            BufferedStackTrace *stack) {
782e8d8bef9SDimitry Andric   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
783e8d8bef9SDimitry Andric     errno = errno_ENOMEM;
784e8d8bef9SDimitry Andric     if (AllocatorMayReturnNull())
785e8d8bef9SDimitry Andric       return nullptr;
786e8d8bef9SDimitry Andric     ReportReallocArrayOverflow(nmemb, size, stack);
787e8d8bef9SDimitry Andric   }
788e8d8bef9SDimitry Andric   return memprof_realloc(p, nmemb * size, stack);
789e8d8bef9SDimitry Andric }
790e8d8bef9SDimitry Andric 
791e8d8bef9SDimitry Andric void *memprof_realloc(void *p, uptr size, BufferedStackTrace *stack) {
792e8d8bef9SDimitry Andric   if (!p)
793e8d8bef9SDimitry Andric     return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC));
794e8d8bef9SDimitry Andric   if (size == 0) {
795e8d8bef9SDimitry Andric     if (flags()->allocator_frees_and_returns_null_on_realloc_zero) {
796e8d8bef9SDimitry Andric       instance.Deallocate(p, 0, 0, stack, FROM_MALLOC);
797e8d8bef9SDimitry Andric       return nullptr;
798e8d8bef9SDimitry Andric     }
799e8d8bef9SDimitry Andric     // Allocate a size of 1 if we shouldn't free() on Realloc to 0
800e8d8bef9SDimitry Andric     size = 1;
801e8d8bef9SDimitry Andric   }
802e8d8bef9SDimitry Andric   return SetErrnoOnNull(instance.Reallocate(p, size, stack));
803e8d8bef9SDimitry Andric }
804e8d8bef9SDimitry Andric 
805e8d8bef9SDimitry Andric void *memprof_valloc(uptr size, BufferedStackTrace *stack) {
806e8d8bef9SDimitry Andric   return SetErrnoOnNull(
807e8d8bef9SDimitry Andric       instance.Allocate(size, GetPageSizeCached(), stack, FROM_MALLOC));
808e8d8bef9SDimitry Andric }
809e8d8bef9SDimitry Andric 
810e8d8bef9SDimitry Andric void *memprof_pvalloc(uptr size, BufferedStackTrace *stack) {
811e8d8bef9SDimitry Andric   uptr PageSize = GetPageSizeCached();
812e8d8bef9SDimitry Andric   if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
813e8d8bef9SDimitry Andric     errno = errno_ENOMEM;
814e8d8bef9SDimitry Andric     if (AllocatorMayReturnNull())
815e8d8bef9SDimitry Andric       return nullptr;
816e8d8bef9SDimitry Andric     ReportPvallocOverflow(size, stack);
817e8d8bef9SDimitry Andric   }
818e8d8bef9SDimitry Andric   // pvalloc(0) should allocate one page.
819e8d8bef9SDimitry Andric   size = size ? RoundUpTo(size, PageSize) : PageSize;
820e8d8bef9SDimitry Andric   return SetErrnoOnNull(instance.Allocate(size, PageSize, stack, FROM_MALLOC));
821e8d8bef9SDimitry Andric }
822e8d8bef9SDimitry Andric 
823e8d8bef9SDimitry Andric void *memprof_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
824e8d8bef9SDimitry Andric                        AllocType alloc_type) {
825e8d8bef9SDimitry Andric   if (UNLIKELY(!IsPowerOfTwo(alignment))) {
826e8d8bef9SDimitry Andric     errno = errno_EINVAL;
827e8d8bef9SDimitry Andric     if (AllocatorMayReturnNull())
828e8d8bef9SDimitry Andric       return nullptr;
829e8d8bef9SDimitry Andric     ReportInvalidAllocationAlignment(alignment, stack);
830e8d8bef9SDimitry Andric   }
831e8d8bef9SDimitry Andric   return SetErrnoOnNull(instance.Allocate(size, alignment, stack, alloc_type));
832e8d8bef9SDimitry Andric }
833e8d8bef9SDimitry Andric 
834e8d8bef9SDimitry Andric void *memprof_aligned_alloc(uptr alignment, uptr size,
835e8d8bef9SDimitry Andric                             BufferedStackTrace *stack) {
836e8d8bef9SDimitry Andric   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
837e8d8bef9SDimitry Andric     errno = errno_EINVAL;
838e8d8bef9SDimitry Andric     if (AllocatorMayReturnNull())
839e8d8bef9SDimitry Andric       return nullptr;
840e8d8bef9SDimitry Andric     ReportInvalidAlignedAllocAlignment(size, alignment, stack);
841e8d8bef9SDimitry Andric   }
842e8d8bef9SDimitry Andric   return SetErrnoOnNull(instance.Allocate(size, alignment, stack, FROM_MALLOC));
843e8d8bef9SDimitry Andric }
844e8d8bef9SDimitry Andric 
845e8d8bef9SDimitry Andric int memprof_posix_memalign(void **memptr, uptr alignment, uptr size,
846e8d8bef9SDimitry Andric                            BufferedStackTrace *stack) {
847e8d8bef9SDimitry Andric   if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
848e8d8bef9SDimitry Andric     if (AllocatorMayReturnNull())
849e8d8bef9SDimitry Andric       return errno_EINVAL;
850e8d8bef9SDimitry Andric     ReportInvalidPosixMemalignAlignment(alignment, stack);
851e8d8bef9SDimitry Andric   }
852e8d8bef9SDimitry Andric   void *ptr = instance.Allocate(size, alignment, stack, FROM_MALLOC);
853e8d8bef9SDimitry Andric   if (UNLIKELY(!ptr))
854e8d8bef9SDimitry Andric     // OOM error is already taken care of by Allocate.
855e8d8bef9SDimitry Andric     return errno_ENOMEM;
856e8d8bef9SDimitry Andric   CHECK(IsAligned((uptr)ptr, alignment));
857e8d8bef9SDimitry Andric   *memptr = ptr;
858e8d8bef9SDimitry Andric   return 0;
859e8d8bef9SDimitry Andric }
860e8d8bef9SDimitry Andric 
861e8d8bef9SDimitry Andric uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp) {
862e8d8bef9SDimitry Andric   if (!ptr)
863e8d8bef9SDimitry Andric     return 0;
864e8d8bef9SDimitry Andric   uptr usable_size = instance.AllocationSize(reinterpret_cast<uptr>(ptr));
865e8d8bef9SDimitry Andric   return usable_size;
866e8d8bef9SDimitry Andric }
867e8d8bef9SDimitry Andric 
868e8d8bef9SDimitry Andric void MemprofSoftRssLimitExceededCallback(bool limit_exceeded) {
869e8d8bef9SDimitry Andric   instance.SetRssLimitExceeded(limit_exceeded);
870e8d8bef9SDimitry Andric }
871e8d8bef9SDimitry Andric 
872e8d8bef9SDimitry Andric } // namespace __memprof
873e8d8bef9SDimitry Andric 
874e8d8bef9SDimitry Andric // ---------------------- Interface ---------------- {{{1
875e8d8bef9SDimitry Andric using namespace __memprof;
876e8d8bef9SDimitry Andric 
877e8d8bef9SDimitry Andric #if !SANITIZER_SUPPORTS_WEAK_HOOKS
878e8d8bef9SDimitry Andric // Provide default (no-op) implementation of malloc hooks.
879e8d8bef9SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_malloc_hook, void *ptr,
880e8d8bef9SDimitry Andric                              uptr size) {
881e8d8bef9SDimitry Andric   (void)ptr;
882e8d8bef9SDimitry Andric   (void)size;
883e8d8bef9SDimitry Andric }
884e8d8bef9SDimitry Andric 
885e8d8bef9SDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_free_hook, void *ptr) {
886e8d8bef9SDimitry Andric   (void)ptr;
887e8d8bef9SDimitry Andric }
888e8d8bef9SDimitry Andric #endif
889e8d8bef9SDimitry Andric 
890e8d8bef9SDimitry Andric uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
891e8d8bef9SDimitry Andric 
892e8d8bef9SDimitry Andric int __sanitizer_get_ownership(const void *p) {
893e8d8bef9SDimitry Andric   return memprof_malloc_usable_size(p, 0, 0) != 0;
894e8d8bef9SDimitry Andric }
895e8d8bef9SDimitry Andric 
896e8d8bef9SDimitry Andric uptr __sanitizer_get_allocated_size(const void *p) {
897e8d8bef9SDimitry Andric   return memprof_malloc_usable_size(p, 0, 0);
898e8d8bef9SDimitry Andric }
899e8d8bef9SDimitry Andric 
900e8d8bef9SDimitry Andric int __memprof_profile_dump() {
901e8d8bef9SDimitry Andric   instance.FinishAndPrint();
902e8d8bef9SDimitry Andric   // In the future we may want to return non-zero if there are any errors
903e8d8bef9SDimitry Andric   // detected during the dumping process.
904e8d8bef9SDimitry Andric   return 0;
905e8d8bef9SDimitry Andric }
906