1 //=-- lsan_allocator.cc ---------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of LeakSanitizer.
11 // See lsan_allocator.h for details.
13 //===----------------------------------------------------------------------===//
15 #include "lsan_allocator.h"
17 #include "sanitizer_common/sanitizer_allocator.h"
18 #include "sanitizer_common/sanitizer_allocator_interface.h"
19 #include "sanitizer_common/sanitizer_internal_defs.h"
20 #include "sanitizer_common/sanitizer_stackdepot.h"
21 #include "sanitizer_common/sanitizer_stacktrace.h"
22 #include "lsan_common.h"
24 extern "C" void *memset(void *ptr, int value, uptr num);
28 struct ChunkMetadata {
29 u8 allocated : 8; // Must be first.
31 uptr requested_size : 54;
35 #if defined(__mips64) || defined(__aarch64__)
36 static const uptr kMaxAllowedMallocSize = 4UL << 30;
37 static const uptr kRegionSizeLog = 20;
38 static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
39 typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
40 typedef CompactSizeClassMap SizeClassMap;
41 typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE,
42 sizeof(ChunkMetadata), SizeClassMap, kRegionSizeLog, ByteMap>
45 static const uptr kMaxAllowedMallocSize = 8UL << 30;
46 static const uptr kAllocatorSpace = 0x600000000000ULL;
47 static const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
48 typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize,
49 sizeof(ChunkMetadata), DefaultSizeClassMap> PrimaryAllocator;
51 typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
52 typedef LargeMmapAllocator<> SecondaryAllocator;
53 typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
54 SecondaryAllocator> Allocator;
56 static Allocator allocator;
57 static THREADLOCAL AllocatorCache cache;
59 void InitializeAllocator() {
60 allocator.InitLinkerInitialized(common_flags()->allocator_may_return_null);
63 void AllocatorThreadFinish() {
64 allocator.SwallowCache(&cache);
67 static ChunkMetadata *Metadata(const void *p) {
68 return reinterpret_cast<ChunkMetadata *>(allocator.GetMetaData(p));
71 static void RegisterAllocation(const StackTrace &stack, void *p, uptr size) {
73 ChunkMetadata *m = Metadata(p);
75 m->tag = DisabledInThisThread() ? kIgnored : kDirectlyLeaked;
76 m->stack_trace_id = StackDepotPut(stack);
77 m->requested_size = size;
78 atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 1, memory_order_relaxed);
81 static void RegisterDeallocation(void *p) {
83 ChunkMetadata *m = Metadata(p);
85 atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 0, memory_order_relaxed);
88 void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
92 if (size > kMaxAllowedMallocSize) {
93 Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", size);
96 void *p = allocator.Allocate(&cache, size, alignment, false);
97 // Do not rely on the allocator to clear the memory (it's slow).
98 if (cleared && allocator.FromPrimary(p))
100 RegisterAllocation(stack, p, size);
101 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(p, size);
105 void Deallocate(void *p) {
106 if (&__sanitizer_free_hook) __sanitizer_free_hook(p);
107 RegisterDeallocation(p);
108 allocator.Deallocate(&cache, p);
111 void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
113 RegisterDeallocation(p);
114 if (new_size > kMaxAllowedMallocSize) {
115 Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", new_size);
116 allocator.Deallocate(&cache, p);
119 p = allocator.Reallocate(&cache, p, new_size, alignment);
120 RegisterAllocation(stack, p, new_size);
124 void GetAllocatorCacheRange(uptr *begin, uptr *end) {
125 *begin = (uptr)&cache;
126 *end = *begin + sizeof(cache);
129 uptr GetMallocUsableSize(const void *p) {
130 ChunkMetadata *m = Metadata(p);
132 return m->requested_size;
135 ///// Interface to the common LSan module. /////
137 void LockAllocator() {
138 allocator.ForceLock();
141 void UnlockAllocator() {
142 allocator.ForceUnlock();
145 void GetAllocatorGlobalRange(uptr *begin, uptr *end) {
146 *begin = (uptr)&allocator;
147 *end = *begin + sizeof(allocator);
150 uptr PointsIntoChunk(void* p) {
151 uptr addr = reinterpret_cast<uptr>(p);
152 uptr chunk = reinterpret_cast<uptr>(allocator.GetBlockBeginFastLocked(p));
153 if (!chunk) return 0;
154 // LargeMmapAllocator considers pointers to the meta-region of a chunk to be
155 // valid, but we don't want that.
156 if (addr < chunk) return 0;
157 ChunkMetadata *m = Metadata(reinterpret_cast<void *>(chunk));
161 if (addr < chunk + m->requested_size)
163 if (IsSpecialCaseOfOperatorNew0(chunk, m->requested_size, addr))
168 uptr GetUserBegin(uptr chunk) {
172 LsanMetadata::LsanMetadata(uptr chunk) {
173 metadata_ = Metadata(reinterpret_cast<void *>(chunk));
177 bool LsanMetadata::allocated() const {
178 return reinterpret_cast<ChunkMetadata *>(metadata_)->allocated;
181 ChunkTag LsanMetadata::tag() const {
182 return reinterpret_cast<ChunkMetadata *>(metadata_)->tag;
185 void LsanMetadata::set_tag(ChunkTag value) {
186 reinterpret_cast<ChunkMetadata *>(metadata_)->tag = value;
189 uptr LsanMetadata::requested_size() const {
190 return reinterpret_cast<ChunkMetadata *>(metadata_)->requested_size;
193 u32 LsanMetadata::stack_trace_id() const {
194 return reinterpret_cast<ChunkMetadata *>(metadata_)->stack_trace_id;
197 void ForEachChunk(ForEachChunkCallback callback, void *arg) {
198 allocator.ForEachChunk(callback, arg);
201 IgnoreObjectResult IgnoreObjectLocked(const void *p) {
202 void *chunk = allocator.GetBlockBegin(p);
203 if (!chunk || p < chunk) return kIgnoreObjectInvalid;
204 ChunkMetadata *m = Metadata(chunk);
206 if (m->allocated && (uptr)p < (uptr)chunk + m->requested_size) {
207 if (m->tag == kIgnored)
208 return kIgnoreObjectAlreadyIgnored;
210 return kIgnoreObjectSuccess;
212 return kIgnoreObjectInvalid;
215 } // namespace __lsan
217 using namespace __lsan;
220 SANITIZER_INTERFACE_ATTRIBUTE
221 uptr __sanitizer_get_current_allocated_bytes() {
222 uptr stats[AllocatorStatCount];
223 allocator.GetStats(stats);
224 return stats[AllocatorStatAllocated];
227 SANITIZER_INTERFACE_ATTRIBUTE
228 uptr __sanitizer_get_heap_size() {
229 uptr stats[AllocatorStatCount];
230 allocator.GetStats(stats);
231 return stats[AllocatorStatMapped];
234 SANITIZER_INTERFACE_ATTRIBUTE
235 uptr __sanitizer_get_free_bytes() { return 0; }
237 SANITIZER_INTERFACE_ATTRIBUTE
238 uptr __sanitizer_get_unmapped_bytes() { return 0; }
240 SANITIZER_INTERFACE_ATTRIBUTE
241 uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
243 SANITIZER_INTERFACE_ATTRIBUTE
244 int __sanitizer_get_ownership(const void *p) { return Metadata(p) != nullptr; }
246 SANITIZER_INTERFACE_ATTRIBUTE
247 uptr __sanitizer_get_allocated_size(const void *p) {
248 return GetMallocUsableSize(p);