xref: /freebsd/contrib/llvm-project/compiler-rt/lib/memprof/memprof_allocator.cpp (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 //===-- memprof_allocator.cpp --------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of MemProfiler, a memory profiler.
10 //
11 // Implementation of MemProf's memory allocator, which uses the allocator
12 // from sanitizer_common.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "memprof_allocator.h"
17 #include "memprof_mapping.h"
18 #include "memprof_mibmap.h"
19 #include "memprof_rawprofile.h"
20 #include "memprof_stack.h"
21 #include "memprof_thread.h"
22 #include "profile/MemProfData.inc"
23 #include "sanitizer_common/sanitizer_allocator_checks.h"
24 #include "sanitizer_common/sanitizer_allocator_interface.h"
25 #include "sanitizer_common/sanitizer_allocator_report.h"
26 #include "sanitizer_common/sanitizer_errno.h"
27 #include "sanitizer_common/sanitizer_file.h"
28 #include "sanitizer_common/sanitizer_flags.h"
29 #include "sanitizer_common/sanitizer_internal_defs.h"
30 #include "sanitizer_common/sanitizer_procmaps.h"
31 #include "sanitizer_common/sanitizer_stackdepot.h"
32 
33 #include <sched.h>
34 #include <time.h>
35 
36 namespace __memprof {
37 namespace {
38 using ::llvm::memprof::MemInfoBlock;
39 
40 void Print(const MemInfoBlock &M, const u64 id, bool print_terse) {
41   u64 p;
42 
43   if (print_terse) {
44     p = M.TotalSize * 100 / M.AllocCount;
45     Printf("MIB:%llu/%u/%llu.%02llu/%u/%u/", id, M.AllocCount, p / 100, p % 100,
46            M.MinSize, M.MaxSize);
47     p = M.TotalAccessCount * 100 / M.AllocCount;
48     Printf("%llu.%02llu/%llu/%llu/", p / 100, p % 100, M.MinAccessCount,
49            M.MaxAccessCount);
50     p = M.TotalLifetime * 100 / M.AllocCount;
51     Printf("%llu.%02llu/%u/%u/", p / 100, p % 100, M.MinLifetime,
52            M.MaxLifetime);
53     Printf("%u/%u/%u/%u\n", M.NumMigratedCpu, M.NumLifetimeOverlaps,
54            M.NumSameAllocCpu, M.NumSameDeallocCpu);
55   } else {
56     p = M.TotalSize * 100 / M.AllocCount;
57     Printf("Memory allocation stack id = %llu\n", id);
58     Printf("\talloc_count %u, size (ave/min/max) %llu.%02llu / %u / %u\n",
59            M.AllocCount, p / 100, p % 100, M.MinSize, M.MaxSize);
60     p = M.TotalAccessCount * 100 / M.AllocCount;
61     Printf("\taccess_count (ave/min/max): %llu.%02llu / %llu / %llu\n", p / 100,
62            p % 100, M.MinAccessCount, M.MaxAccessCount);
63     p = M.TotalLifetime * 100 / M.AllocCount;
64     Printf("\tlifetime (ave/min/max): %llu.%02llu / %u / %u\n", p / 100,
65            p % 100, M.MinLifetime, M.MaxLifetime);
66     Printf("\tnum migrated: %u, num lifetime overlaps: %u, num same alloc "
67            "cpu: %u, num same dealloc_cpu: %u\n",
68            M.NumMigratedCpu, M.NumLifetimeOverlaps, M.NumSameAllocCpu,
69            M.NumSameDeallocCpu);
70   }
71 }
72 } // namespace
73 
74 static int GetCpuId(void) {
75   // _memprof_preinit is called via the preinit_array, which subsequently calls
76   // malloc. Since this is before _dl_init calls VDSO_SETUP, sched_getcpu
77   // will seg fault as the address of __vdso_getcpu will be null.
78   if (!memprof_init_done)
79     return -1;
80   return sched_getcpu();
81 }
82 
83 // Compute the timestamp in ms.
84 static int GetTimestamp(void) {
85   // timespec_get will segfault if called from dl_init
86   if (!memprof_timestamp_inited) {
87     // By returning 0, this will be effectively treated as being
88     // timestamped at memprof init time (when memprof_init_timestamp_s
89     // is initialized).
90     return 0;
91   }
92   timespec ts;
93   clock_gettime(CLOCK_REALTIME, &ts);
94   return (ts.tv_sec - memprof_init_timestamp_s) * 1000 + ts.tv_nsec / 1000000;
95 }
96 
97 static MemprofAllocator &get_allocator();
98 
99 // The memory chunk allocated from the underlying allocator looks like this:
100 // H H U U U U U U
101 //   H -- ChunkHeader (32 bytes)
102 //   U -- user memory.
103 
104 // If there is left padding before the ChunkHeader (due to use of memalign),
105 // we store a magic value in the first uptr word of the memory block and
106 // store the address of ChunkHeader in the next uptr.
107 // M B L L L L L L L L L  H H U U U U U U
108 //   |                    ^
109 //   ---------------------|
110 //   M -- magic value kAllocBegMagic
111 //   B -- address of ChunkHeader pointing to the first 'H'
112 
113 constexpr uptr kMaxAllowedMallocBits = 40;
114 
115 // Should be no more than 32-bytes
116 struct ChunkHeader {
117   // 1-st 4 bytes.
118   u32 alloc_context_id;
119   // 2-nd 4 bytes
120   u32 cpu_id;
121   // 3-rd 4 bytes
122   u32 timestamp_ms;
123   // 4-th 4 bytes
124   // Note only 1 bit is needed for this flag if we need space in the future for
125   // more fields.
126   u32 from_memalign;
127   // 5-th and 6-th 4 bytes
128   // The max size of an allocation is 2^40 (kMaxAllowedMallocSize), so this
129   // could be shrunk to kMaxAllowedMallocBits if we need space in the future for
130   // more fields.
131   atomic_uint64_t user_requested_size;
132   // 23 bits available
133   // 7-th and 8-th 4 bytes
134   u64 data_type_id; // TODO: hash of type name
135 };
136 
137 static const uptr kChunkHeaderSize = sizeof(ChunkHeader);
138 COMPILER_CHECK(kChunkHeaderSize == 32);
139 
140 struct MemprofChunk : ChunkHeader {
141   uptr Beg() { return reinterpret_cast<uptr>(this) + kChunkHeaderSize; }
142   uptr UsedSize() {
143     return atomic_load(&user_requested_size, memory_order_relaxed);
144   }
145   void *AllocBeg() {
146     if (from_memalign)
147       return get_allocator().GetBlockBegin(reinterpret_cast<void *>(this));
148     return reinterpret_cast<void *>(this);
149   }
150 };
151 
152 class LargeChunkHeader {
153   static constexpr uptr kAllocBegMagic =
154       FIRST_32_SECOND_64(0xCC6E96B9, 0xCC6E96B9CC6E96B9ULL);
155   atomic_uintptr_t magic;
156   MemprofChunk *chunk_header;
157 
158 public:
159   MemprofChunk *Get() const {
160     return atomic_load(&magic, memory_order_acquire) == kAllocBegMagic
161                ? chunk_header
162                : nullptr;
163   }
164 
165   void Set(MemprofChunk *p) {
166     if (p) {
167       chunk_header = p;
168       atomic_store(&magic, kAllocBegMagic, memory_order_release);
169       return;
170     }
171 
172     uptr old = kAllocBegMagic;
173     if (!atomic_compare_exchange_strong(&magic, &old, 0,
174                                         memory_order_release)) {
175       CHECK_EQ(old, kAllocBegMagic);
176     }
177   }
178 };
179 
180 void FlushUnneededMemProfShadowMemory(uptr p, uptr size) {
181   // Since memprof's mapping is compacting, the shadow chunk may be
182   // not page-aligned, so we only flush the page-aligned portion.
183   ReleaseMemoryPagesToOS(MemToShadow(p), MemToShadow(p + size));
184 }
185 
186 void MemprofMapUnmapCallback::OnMap(uptr p, uptr size) const {
187   // Statistics.
188   MemprofStats &thread_stats = GetCurrentThreadStats();
189   thread_stats.mmaps++;
190   thread_stats.mmaped += size;
191 }
192 void MemprofMapUnmapCallback::OnUnmap(uptr p, uptr size) const {
193   // We are about to unmap a chunk of user memory.
194   // Mark the corresponding shadow memory as not needed.
195   FlushUnneededMemProfShadowMemory(p, size);
196   // Statistics.
197   MemprofStats &thread_stats = GetCurrentThreadStats();
198   thread_stats.munmaps++;
199   thread_stats.munmaped += size;
200 }
201 
202 AllocatorCache *GetAllocatorCache(MemprofThreadLocalMallocStorage *ms) {
203   CHECK(ms);
204   return &ms->allocator_cache;
205 }
206 
207 // Accumulates the access count from the shadow for the given pointer and size.
208 u64 GetShadowCount(uptr p, u32 size) {
209   u64 *shadow = (u64 *)MEM_TO_SHADOW(p);
210   u64 *shadow_end = (u64 *)MEM_TO_SHADOW(p + size);
211   u64 count = 0;
212   for (; shadow <= shadow_end; shadow++)
213     count += *shadow;
214   return count;
215 }
216 
217 // Clears the shadow counters (when memory is allocated).
218 void ClearShadow(uptr addr, uptr size) {
219   CHECK(AddrIsAlignedByGranularity(addr));
220   CHECK(AddrIsInMem(addr));
221   CHECK(AddrIsAlignedByGranularity(addr + size));
222   CHECK(AddrIsInMem(addr + size - SHADOW_GRANULARITY));
223   CHECK(REAL(memset));
224   uptr shadow_beg = MEM_TO_SHADOW(addr);
225   uptr shadow_end = MEM_TO_SHADOW(addr + size - SHADOW_GRANULARITY) + 1;
226   if (shadow_end - shadow_beg < common_flags()->clear_shadow_mmap_threshold) {
227     REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
228   } else {
229     uptr page_size = GetPageSizeCached();
230     uptr page_beg = RoundUpTo(shadow_beg, page_size);
231     uptr page_end = RoundDownTo(shadow_end, page_size);
232 
233     if (page_beg >= page_end) {
234       REAL(memset)((void *)shadow_beg, 0, shadow_end - shadow_beg);
235     } else {
236       if (page_beg != shadow_beg) {
237         REAL(memset)((void *)shadow_beg, 0, page_beg - shadow_beg);
238       }
239       if (page_end != shadow_end) {
240         REAL(memset)((void *)page_end, 0, shadow_end - page_end);
241       }
242       ReserveShadowMemoryRange(page_beg, page_end - 1, nullptr);
243     }
244   }
245 }
246 
247 struct Allocator {
248   static const uptr kMaxAllowedMallocSize = 1ULL << kMaxAllowedMallocBits;
249 
250   MemprofAllocator allocator;
251   StaticSpinMutex fallback_mutex;
252   AllocatorCache fallback_allocator_cache;
253 
254   uptr max_user_defined_malloc_size;
255 
256   // Holds the mapping of stack ids to MemInfoBlocks.
257   MIBMapTy MIBMap;
258 
259   atomic_uint8_t destructing;
260   atomic_uint8_t constructed;
261   bool print_text;
262 
263   // ------------------- Initialization ------------------------
264   explicit Allocator(LinkerInitialized) : print_text(flags()->print_text) {
265     atomic_store_relaxed(&destructing, 0);
266     atomic_store_relaxed(&constructed, 1);
267   }
268 
269   ~Allocator() {
270     atomic_store_relaxed(&destructing, 1);
271     FinishAndWrite();
272   }
273 
274   static void PrintCallback(const uptr Key, LockedMemInfoBlock *const &Value,
275                             void *Arg) {
276     SpinMutexLock l(&Value->mutex);
277     Print(Value->mib, Key, bool(Arg));
278   }
279 
280   void FinishAndWrite() {
281     if (print_text && common_flags()->print_module_map)
282       DumpProcessMap();
283 
284     allocator.ForceLock();
285 
286     InsertLiveBlocks();
287     if (print_text) {
288       if (!flags()->print_terse)
289         Printf("Recorded MIBs (incl. live on exit):\n");
290       MIBMap.ForEach(PrintCallback,
291                      reinterpret_cast<void *>(flags()->print_terse));
292       StackDepotPrintAll();
293     } else {
294       // Serialize the contents to a raw profile. Format documented in
295       // memprof_rawprofile.h.
296       char *Buffer = nullptr;
297 
298       MemoryMappingLayout Layout(/*cache_enabled=*/true);
299       u64 BytesSerialized = SerializeToRawProfile(MIBMap, Layout, Buffer);
300       CHECK(Buffer && BytesSerialized && "could not serialize to buffer");
301       report_file.Write(Buffer, BytesSerialized);
302     }
303 
304     allocator.ForceUnlock();
305   }
306 
307   // Inserts any blocks which have been allocated but not yet deallocated.
308   void InsertLiveBlocks() {
309     allocator.ForEachChunk(
310         [](uptr chunk, void *alloc) {
311           u64 user_requested_size;
312           Allocator *A = (Allocator *)alloc;
313           MemprofChunk *m =
314               A->GetMemprofChunk((void *)chunk, user_requested_size);
315           if (!m)
316             return;
317           uptr user_beg = ((uptr)m) + kChunkHeaderSize;
318           u64 c = GetShadowCount(user_beg, user_requested_size);
319           long curtime = GetTimestamp();
320           MemInfoBlock newMIB(user_requested_size, c, m->timestamp_ms, curtime,
321                               m->cpu_id, GetCpuId());
322           InsertOrMerge(m->alloc_context_id, newMIB, A->MIBMap);
323         },
324         this);
325   }
326 
327   void InitLinkerInitialized() {
328     SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null);
329     allocator.InitLinkerInitialized(
330         common_flags()->allocator_release_to_os_interval_ms);
331     max_user_defined_malloc_size = common_flags()->max_allocation_size_mb
332                                        ? common_flags()->max_allocation_size_mb
333                                              << 20
334                                        : kMaxAllowedMallocSize;
335   }
336 
337   // -------------------- Allocation/Deallocation routines ---------------
338   void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack,
339                  AllocType alloc_type) {
340     if (UNLIKELY(!memprof_inited))
341       MemprofInitFromRtl();
342     if (UNLIKELY(IsRssLimitExceeded())) {
343       if (AllocatorMayReturnNull())
344         return nullptr;
345       ReportRssLimitExceeded(stack);
346     }
347     CHECK(stack);
348     const uptr min_alignment = MEMPROF_ALIGNMENT;
349     if (alignment < min_alignment)
350       alignment = min_alignment;
351     if (size == 0) {
352       // We'd be happy to avoid allocating memory for zero-size requests, but
353       // some programs/tests depend on this behavior and assume that malloc
354       // would not return NULL even for zero-size allocations. Moreover, it
355       // looks like operator new should never return NULL, and results of
356       // consecutive "new" calls must be different even if the allocated size
357       // is zero.
358       size = 1;
359     }
360     CHECK(IsPowerOfTwo(alignment));
361     uptr rounded_size = RoundUpTo(size, alignment);
362     uptr needed_size = rounded_size + kChunkHeaderSize;
363     if (alignment > min_alignment)
364       needed_size += alignment;
365     CHECK(IsAligned(needed_size, min_alignment));
366     if (size > kMaxAllowedMallocSize || needed_size > kMaxAllowedMallocSize ||
367         size > max_user_defined_malloc_size) {
368       if (AllocatorMayReturnNull()) {
369         Report("WARNING: MemProfiler failed to allocate 0x%zx bytes\n", size);
370         return nullptr;
371       }
372       uptr malloc_limit =
373           Min(kMaxAllowedMallocSize, max_user_defined_malloc_size);
374       ReportAllocationSizeTooBig(size, malloc_limit, stack);
375     }
376 
377     MemprofThread *t = GetCurrentThread();
378     void *allocated;
379     if (t) {
380       AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
381       allocated = allocator.Allocate(cache, needed_size, 8);
382     } else {
383       SpinMutexLock l(&fallback_mutex);
384       AllocatorCache *cache = &fallback_allocator_cache;
385       allocated = allocator.Allocate(cache, needed_size, 8);
386     }
387     if (UNLIKELY(!allocated)) {
388       SetAllocatorOutOfMemory();
389       if (AllocatorMayReturnNull())
390         return nullptr;
391       ReportOutOfMemory(size, stack);
392     }
393 
394     uptr alloc_beg = reinterpret_cast<uptr>(allocated);
395     uptr alloc_end = alloc_beg + needed_size;
396     uptr beg_plus_header = alloc_beg + kChunkHeaderSize;
397     uptr user_beg = beg_plus_header;
398     if (!IsAligned(user_beg, alignment))
399       user_beg = RoundUpTo(user_beg, alignment);
400     uptr user_end = user_beg + size;
401     CHECK_LE(user_end, alloc_end);
402     uptr chunk_beg = user_beg - kChunkHeaderSize;
403     MemprofChunk *m = reinterpret_cast<MemprofChunk *>(chunk_beg);
404     m->from_memalign = alloc_beg != chunk_beg;
405     CHECK(size);
406 
407     m->cpu_id = GetCpuId();
408     m->timestamp_ms = GetTimestamp();
409     m->alloc_context_id = StackDepotPut(*stack);
410 
411     uptr size_rounded_down_to_granularity =
412         RoundDownTo(size, SHADOW_GRANULARITY);
413     if (size_rounded_down_to_granularity)
414       ClearShadow(user_beg, size_rounded_down_to_granularity);
415 
416     MemprofStats &thread_stats = GetCurrentThreadStats();
417     thread_stats.mallocs++;
418     thread_stats.malloced += size;
419     thread_stats.malloced_overhead += needed_size - size;
420     if (needed_size > SizeClassMap::kMaxSize)
421       thread_stats.malloc_large++;
422     else
423       thread_stats.malloced_by_size[SizeClassMap::ClassID(needed_size)]++;
424 
425     void *res = reinterpret_cast<void *>(user_beg);
426     atomic_store(&m->user_requested_size, size, memory_order_release);
427     if (alloc_beg != chunk_beg) {
428       CHECK_LE(alloc_beg + sizeof(LargeChunkHeader), chunk_beg);
429       reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Set(m);
430     }
431     RunMallocHooks(res, size);
432     return res;
433   }
434 
435   void Deallocate(void *ptr, uptr delete_size, uptr delete_alignment,
436                   BufferedStackTrace *stack, AllocType alloc_type) {
437     uptr p = reinterpret_cast<uptr>(ptr);
438     if (p == 0)
439       return;
440 
441     RunFreeHooks(ptr);
442 
443     uptr chunk_beg = p - kChunkHeaderSize;
444     MemprofChunk *m = reinterpret_cast<MemprofChunk *>(chunk_beg);
445 
446     u64 user_requested_size =
447         atomic_exchange(&m->user_requested_size, 0, memory_order_acquire);
448     if (memprof_inited && memprof_init_done &&
449         atomic_load_relaxed(&constructed) &&
450         !atomic_load_relaxed(&destructing)) {
451       u64 c = GetShadowCount(p, user_requested_size);
452       long curtime = GetTimestamp();
453 
454       MemInfoBlock newMIB(user_requested_size, c, m->timestamp_ms, curtime,
455                           m->cpu_id, GetCpuId());
456       InsertOrMerge(m->alloc_context_id, newMIB, MIBMap);
457     }
458 
459     MemprofStats &thread_stats = GetCurrentThreadStats();
460     thread_stats.frees++;
461     thread_stats.freed += user_requested_size;
462 
463     void *alloc_beg = m->AllocBeg();
464     if (alloc_beg != m) {
465       // Clear the magic value, as allocator internals may overwrite the
466       // contents of deallocated chunk, confusing GetMemprofChunk lookup.
467       reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Set(nullptr);
468     }
469 
470     MemprofThread *t = GetCurrentThread();
471     if (t) {
472       AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
473       allocator.Deallocate(cache, alloc_beg);
474     } else {
475       SpinMutexLock l(&fallback_mutex);
476       AllocatorCache *cache = &fallback_allocator_cache;
477       allocator.Deallocate(cache, alloc_beg);
478     }
479   }
480 
481   void *Reallocate(void *old_ptr, uptr new_size, BufferedStackTrace *stack) {
482     CHECK(old_ptr && new_size);
483     uptr p = reinterpret_cast<uptr>(old_ptr);
484     uptr chunk_beg = p - kChunkHeaderSize;
485     MemprofChunk *m = reinterpret_cast<MemprofChunk *>(chunk_beg);
486 
487     MemprofStats &thread_stats = GetCurrentThreadStats();
488     thread_stats.reallocs++;
489     thread_stats.realloced += new_size;
490 
491     void *new_ptr = Allocate(new_size, 8, stack, FROM_MALLOC);
492     if (new_ptr) {
493       CHECK_NE(REAL(memcpy), nullptr);
494       uptr memcpy_size = Min(new_size, m->UsedSize());
495       REAL(memcpy)(new_ptr, old_ptr, memcpy_size);
496       Deallocate(old_ptr, 0, 0, stack, FROM_MALLOC);
497     }
498     return new_ptr;
499   }
500 
501   void *Calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {
502     if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
503       if (AllocatorMayReturnNull())
504         return nullptr;
505       ReportCallocOverflow(nmemb, size, stack);
506     }
507     void *ptr = Allocate(nmemb * size, 8, stack, FROM_MALLOC);
508     // If the memory comes from the secondary allocator no need to clear it
509     // as it comes directly from mmap.
510     if (ptr && allocator.FromPrimary(ptr))
511       REAL(memset)(ptr, 0, nmemb * size);
512     return ptr;
513   }
514 
515   void CommitBack(MemprofThreadLocalMallocStorage *ms,
516                   BufferedStackTrace *stack) {
517     AllocatorCache *ac = GetAllocatorCache(ms);
518     allocator.SwallowCache(ac);
519   }
520 
521   // -------------------------- Chunk lookup ----------------------
522 
523   // Assumes alloc_beg == allocator.GetBlockBegin(alloc_beg).
524   MemprofChunk *GetMemprofChunk(void *alloc_beg, u64 &user_requested_size) {
525     if (!alloc_beg)
526       return nullptr;
527     MemprofChunk *p = reinterpret_cast<LargeChunkHeader *>(alloc_beg)->Get();
528     if (!p) {
529       if (!allocator.FromPrimary(alloc_beg))
530         return nullptr;
531       p = reinterpret_cast<MemprofChunk *>(alloc_beg);
532     }
533     // The size is reset to 0 on deallocation (and a min of 1 on
534     // allocation).
535     user_requested_size =
536         atomic_load(&p->user_requested_size, memory_order_acquire);
537     if (user_requested_size)
538       return p;
539     return nullptr;
540   }
541 
542   MemprofChunk *GetMemprofChunkByAddr(uptr p, u64 &user_requested_size) {
543     void *alloc_beg = allocator.GetBlockBegin(reinterpret_cast<void *>(p));
544     return GetMemprofChunk(alloc_beg, user_requested_size);
545   }
546 
547   uptr AllocationSize(uptr p) {
548     u64 user_requested_size;
549     MemprofChunk *m = GetMemprofChunkByAddr(p, user_requested_size);
550     if (!m)
551       return 0;
552     if (m->Beg() != p)
553       return 0;
554     return user_requested_size;
555   }
556 
557   void Purge(BufferedStackTrace *stack) { allocator.ForceReleaseToOS(); }
558 
559   void PrintStats() { allocator.PrintStats(); }
560 
561   void ForceLock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
562     allocator.ForceLock();
563     fallback_mutex.Lock();
564   }
565 
566   void ForceUnlock() SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
567     fallback_mutex.Unlock();
568     allocator.ForceUnlock();
569   }
570 };
571 
572 static Allocator instance(LINKER_INITIALIZED);
573 
574 static MemprofAllocator &get_allocator() { return instance.allocator; }
575 
576 void InitializeAllocator() { instance.InitLinkerInitialized(); }
577 
578 void MemprofThreadLocalMallocStorage::CommitBack() {
579   GET_STACK_TRACE_MALLOC;
580   instance.CommitBack(this, &stack);
581 }
582 
583 void PrintInternalAllocatorStats() { instance.PrintStats(); }
584 
585 void memprof_free(void *ptr, BufferedStackTrace *stack, AllocType alloc_type) {
586   instance.Deallocate(ptr, 0, 0, stack, alloc_type);
587 }
588 
589 void memprof_delete(void *ptr, uptr size, uptr alignment,
590                     BufferedStackTrace *stack, AllocType alloc_type) {
591   instance.Deallocate(ptr, size, alignment, stack, alloc_type);
592 }
593 
594 void *memprof_malloc(uptr size, BufferedStackTrace *stack) {
595   return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC));
596 }
597 
598 void *memprof_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack) {
599   return SetErrnoOnNull(instance.Calloc(nmemb, size, stack));
600 }
601 
602 void *memprof_reallocarray(void *p, uptr nmemb, uptr size,
603                            BufferedStackTrace *stack) {
604   if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) {
605     errno = errno_ENOMEM;
606     if (AllocatorMayReturnNull())
607       return nullptr;
608     ReportReallocArrayOverflow(nmemb, size, stack);
609   }
610   return memprof_realloc(p, nmemb * size, stack);
611 }
612 
613 void *memprof_realloc(void *p, uptr size, BufferedStackTrace *stack) {
614   if (!p)
615     return SetErrnoOnNull(instance.Allocate(size, 8, stack, FROM_MALLOC));
616   if (size == 0) {
617     if (flags()->allocator_frees_and_returns_null_on_realloc_zero) {
618       instance.Deallocate(p, 0, 0, stack, FROM_MALLOC);
619       return nullptr;
620     }
621     // Allocate a size of 1 if we shouldn't free() on Realloc to 0
622     size = 1;
623   }
624   return SetErrnoOnNull(instance.Reallocate(p, size, stack));
625 }
626 
627 void *memprof_valloc(uptr size, BufferedStackTrace *stack) {
628   return SetErrnoOnNull(
629       instance.Allocate(size, GetPageSizeCached(), stack, FROM_MALLOC));
630 }
631 
632 void *memprof_pvalloc(uptr size, BufferedStackTrace *stack) {
633   uptr PageSize = GetPageSizeCached();
634   if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) {
635     errno = errno_ENOMEM;
636     if (AllocatorMayReturnNull())
637       return nullptr;
638     ReportPvallocOverflow(size, stack);
639   }
640   // pvalloc(0) should allocate one page.
641   size = size ? RoundUpTo(size, PageSize) : PageSize;
642   return SetErrnoOnNull(instance.Allocate(size, PageSize, stack, FROM_MALLOC));
643 }
644 
645 void *memprof_memalign(uptr alignment, uptr size, BufferedStackTrace *stack,
646                        AllocType alloc_type) {
647   if (UNLIKELY(!IsPowerOfTwo(alignment))) {
648     errno = errno_EINVAL;
649     if (AllocatorMayReturnNull())
650       return nullptr;
651     ReportInvalidAllocationAlignment(alignment, stack);
652   }
653   return SetErrnoOnNull(instance.Allocate(size, alignment, stack, alloc_type));
654 }
655 
656 void *memprof_aligned_alloc(uptr alignment, uptr size,
657                             BufferedStackTrace *stack) {
658   if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) {
659     errno = errno_EINVAL;
660     if (AllocatorMayReturnNull())
661       return nullptr;
662     ReportInvalidAlignedAllocAlignment(size, alignment, stack);
663   }
664   return SetErrnoOnNull(instance.Allocate(size, alignment, stack, FROM_MALLOC));
665 }
666 
667 int memprof_posix_memalign(void **memptr, uptr alignment, uptr size,
668                            BufferedStackTrace *stack) {
669   if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) {
670     if (AllocatorMayReturnNull())
671       return errno_EINVAL;
672     ReportInvalidPosixMemalignAlignment(alignment, stack);
673   }
674   void *ptr = instance.Allocate(size, alignment, stack, FROM_MALLOC);
675   if (UNLIKELY(!ptr))
676     // OOM error is already taken care of by Allocate.
677     return errno_ENOMEM;
678   CHECK(IsAligned((uptr)ptr, alignment));
679   *memptr = ptr;
680   return 0;
681 }
682 
683 uptr memprof_malloc_usable_size(const void *ptr, uptr pc, uptr bp) {
684   if (!ptr)
685     return 0;
686   uptr usable_size = instance.AllocationSize(reinterpret_cast<uptr>(ptr));
687   return usable_size;
688 }
689 
690 } // namespace __memprof
691 
692 // ---------------------- Interface ---------------- {{{1
693 using namespace __memprof;
694 
695 uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
696 
697 int __sanitizer_get_ownership(const void *p) {
698   return memprof_malloc_usable_size(p, 0, 0) != 0;
699 }
700 
701 uptr __sanitizer_get_allocated_size(const void *p) {
702   return memprof_malloc_usable_size(p, 0, 0);
703 }
704 
705 int __memprof_profile_dump() {
706   instance.FinishAndWrite();
707   // In the future we may want to return non-zero if there are any errors
708   // detected during the dumping process.
709   return 0;
710 }
711