1 //=-- lsan_allocator.cpp --------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of LeakSanitizer.
10 // See lsan_allocator.h for details.
12 //===----------------------------------------------------------------------===//
14 #include "lsan_allocator.h"
16 #include "sanitizer_common/sanitizer_allocator.h"
17 #include "sanitizer_common/sanitizer_allocator_checks.h"
18 #include "sanitizer_common/sanitizer_allocator_interface.h"
19 #include "sanitizer_common/sanitizer_allocator_report.h"
20 #include "sanitizer_common/sanitizer_errno.h"
21 #include "sanitizer_common/sanitizer_internal_defs.h"
22 #include "sanitizer_common/sanitizer_stackdepot.h"
23 #include "sanitizer_common/sanitizer_stacktrace.h"
24 #include "lsan_common.h"
26 extern "C" void *memset(void *ptr
, int value
, uptr num
);
29 #if defined(__i386__) || defined(__arm__)
30 static const uptr kMaxAllowedMallocSize
= 1UL << 30;
31 #elif defined(__mips64) || defined(__aarch64__)
32 static const uptr kMaxAllowedMallocSize
= 4UL << 30;
34 static const uptr kMaxAllowedMallocSize
= 8UL << 30;
37 static Allocator allocator
;
39 static uptr max_malloc_size
;
41 void InitializeAllocator() {
42 SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null
);
43 allocator
.InitLinkerInitialized(
44 common_flags()->allocator_release_to_os_interval_ms
);
45 if (common_flags()->max_allocation_size_mb
)
46 max_malloc_size
= Min(common_flags()->max_allocation_size_mb
<< 20,
47 kMaxAllowedMallocSize
);
49 max_malloc_size
= kMaxAllowedMallocSize
;
52 void AllocatorThreadFinish() {
53 allocator
.SwallowCache(GetAllocatorCache());
56 static ChunkMetadata
*Metadata(const void *p
) {
57 return reinterpret_cast<ChunkMetadata
*>(allocator
.GetMetaData(p
));
60 static void RegisterAllocation(const StackTrace
&stack
, void *p
, uptr size
) {
62 ChunkMetadata
*m
= Metadata(p
);
64 m
->tag
= DisabledInThisThread() ? kIgnored
: kDirectlyLeaked
;
65 m
->stack_trace_id
= StackDepotPut(stack
);
66 m
->requested_size
= size
;
67 atomic_store(reinterpret_cast<atomic_uint8_t
*>(m
), 1, memory_order_relaxed
);
70 static void RegisterDeallocation(void *p
) {
72 ChunkMetadata
*m
= Metadata(p
);
74 atomic_store(reinterpret_cast<atomic_uint8_t
*>(m
), 0, memory_order_relaxed
);
77 static void *ReportAllocationSizeTooBig(uptr size
, const StackTrace
&stack
) {
78 if (AllocatorMayReturnNull()) {
79 Report("WARNING: LeakSanitizer failed to allocate 0x%zx bytes\n", size
);
82 ReportAllocationSizeTooBig(size
, max_malloc_size
, &stack
);
85 void *Allocate(const StackTrace
&stack
, uptr size
, uptr alignment
,
89 if (size
> max_malloc_size
)
90 return ReportAllocationSizeTooBig(size
, stack
);
91 void *p
= allocator
.Allocate(GetAllocatorCache(), size
, alignment
);
93 SetAllocatorOutOfMemory();
94 if (AllocatorMayReturnNull())
96 ReportOutOfMemory(size
, &stack
);
98 // Do not rely on the allocator to clear the memory (it's slow).
99 if (cleared
&& allocator
.FromPrimary(p
))
101 RegisterAllocation(stack
, p
, size
);
102 if (&__sanitizer_malloc_hook
) __sanitizer_malloc_hook(p
, size
);
103 RunMallocHooks(p
, size
);
107 static void *Calloc(uptr nmemb
, uptr size
, const StackTrace
&stack
) {
108 if (UNLIKELY(CheckForCallocOverflow(size
, nmemb
))) {
109 if (AllocatorMayReturnNull())
111 ReportCallocOverflow(nmemb
, size
, &stack
);
114 return Allocate(stack
, size
, 1, true);
117 void Deallocate(void *p
) {
118 if (&__sanitizer_free_hook
) __sanitizer_free_hook(p
);
120 RegisterDeallocation(p
);
121 allocator
.Deallocate(GetAllocatorCache(), p
);
124 void *Reallocate(const StackTrace
&stack
, void *p
, uptr new_size
,
126 if (new_size
> max_malloc_size
) {
127 ReportAllocationSizeTooBig(new_size
, stack
);
130 RegisterDeallocation(p
);
132 allocator
.Reallocate(GetAllocatorCache(), p
, new_size
, alignment
);
134 RegisterAllocation(stack
, new_p
, new_size
);
135 else if (new_size
!= 0)
136 RegisterAllocation(stack
, p
, new_size
);
140 void GetAllocatorCacheRange(uptr
*begin
, uptr
*end
) {
141 *begin
= (uptr
)GetAllocatorCache();
142 *end
= *begin
+ sizeof(AllocatorCache
);
145 uptr
GetMallocUsableSize(const void *p
) {
146 ChunkMetadata
*m
= Metadata(p
);
148 return m
->requested_size
;
151 int lsan_posix_memalign(void **memptr
, uptr alignment
, uptr size
,
152 const StackTrace
&stack
) {
153 if (UNLIKELY(!CheckPosixMemalignAlignment(alignment
))) {
154 if (AllocatorMayReturnNull())
156 ReportInvalidPosixMemalignAlignment(alignment
, &stack
);
158 void *ptr
= Allocate(stack
, size
, alignment
, kAlwaysClearMemory
);
160 // OOM error is already taken care of by Allocate.
162 CHECK(IsAligned((uptr
)ptr
, alignment
));
167 void *lsan_aligned_alloc(uptr alignment
, uptr size
, const StackTrace
&stack
) {
168 if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment
, size
))) {
169 errno
= errno_EINVAL
;
170 if (AllocatorMayReturnNull())
172 ReportInvalidAlignedAllocAlignment(size
, alignment
, &stack
);
174 return SetErrnoOnNull(Allocate(stack
, size
, alignment
, kAlwaysClearMemory
));
177 void *lsan_memalign(uptr alignment
, uptr size
, const StackTrace
&stack
) {
178 if (UNLIKELY(!IsPowerOfTwo(alignment
))) {
179 errno
= errno_EINVAL
;
180 if (AllocatorMayReturnNull())
182 ReportInvalidAllocationAlignment(alignment
, &stack
);
184 return SetErrnoOnNull(Allocate(stack
, size
, alignment
, kAlwaysClearMemory
));
187 void *lsan_malloc(uptr size
, const StackTrace
&stack
) {
188 return SetErrnoOnNull(Allocate(stack
, size
, 1, kAlwaysClearMemory
));
191 void lsan_free(void *p
) {
195 void *lsan_realloc(void *p
, uptr size
, const StackTrace
&stack
) {
196 return SetErrnoOnNull(Reallocate(stack
, p
, size
, 1));
199 void *lsan_reallocarray(void *ptr
, uptr nmemb
, uptr size
,
200 const StackTrace
&stack
) {
201 if (UNLIKELY(CheckForCallocOverflow(size
, nmemb
))) {
202 errno
= errno_ENOMEM
;
203 if (AllocatorMayReturnNull())
205 ReportReallocArrayOverflow(nmemb
, size
, &stack
);
207 return lsan_realloc(ptr
, nmemb
* size
, stack
);
210 void *lsan_calloc(uptr nmemb
, uptr size
, const StackTrace
&stack
) {
211 return SetErrnoOnNull(Calloc(nmemb
, size
, stack
));
214 void *lsan_valloc(uptr size
, const StackTrace
&stack
) {
215 return SetErrnoOnNull(
216 Allocate(stack
, size
, GetPageSizeCached(), kAlwaysClearMemory
));
219 void *lsan_pvalloc(uptr size
, const StackTrace
&stack
) {
220 uptr PageSize
= GetPageSizeCached();
221 if (UNLIKELY(CheckForPvallocOverflow(size
, PageSize
))) {
222 errno
= errno_ENOMEM
;
223 if (AllocatorMayReturnNull())
225 ReportPvallocOverflow(size
, &stack
);
227 // pvalloc(0) should allocate one page.
228 size
= size
? RoundUpTo(size
, PageSize
) : PageSize
;
229 return SetErrnoOnNull(Allocate(stack
, size
, PageSize
, kAlwaysClearMemory
));
232 uptr
lsan_mz_size(const void *p
) {
233 return GetMallocUsableSize(p
);
236 ///// Interface to the common LSan module. /////
238 void LockAllocator() {
239 allocator
.ForceLock();
242 void UnlockAllocator() {
243 allocator
.ForceUnlock();
246 void GetAllocatorGlobalRange(uptr
*begin
, uptr
*end
) {
247 *begin
= (uptr
)&allocator
;
248 *end
= *begin
+ sizeof(allocator
);
251 uptr
PointsIntoChunk(void* p
) {
252 uptr addr
= reinterpret_cast<uptr
>(p
);
253 uptr chunk
= reinterpret_cast<uptr
>(allocator
.GetBlockBeginFastLocked(p
));
254 if (!chunk
) return 0;
255 // LargeMmapAllocator considers pointers to the meta-region of a chunk to be
256 // valid, but we don't want that.
257 if (addr
< chunk
) return 0;
258 ChunkMetadata
*m
= Metadata(reinterpret_cast<void *>(chunk
));
262 if (addr
< chunk
+ m
->requested_size
)
264 if (IsSpecialCaseOfOperatorNew0(chunk
, m
->requested_size
, addr
))
269 uptr
GetUserBegin(uptr chunk
) {
273 LsanMetadata::LsanMetadata(uptr chunk
) {
274 metadata_
= Metadata(reinterpret_cast<void *>(chunk
));
278 bool LsanMetadata::allocated() const {
279 return reinterpret_cast<ChunkMetadata
*>(metadata_
)->allocated
;
282 ChunkTag
LsanMetadata::tag() const {
283 return reinterpret_cast<ChunkMetadata
*>(metadata_
)->tag
;
286 void LsanMetadata::set_tag(ChunkTag value
) {
287 reinterpret_cast<ChunkMetadata
*>(metadata_
)->tag
= value
;
290 uptr
LsanMetadata::requested_size() const {
291 return reinterpret_cast<ChunkMetadata
*>(metadata_
)->requested_size
;
294 u32
LsanMetadata::stack_trace_id() const {
295 return reinterpret_cast<ChunkMetadata
*>(metadata_
)->stack_trace_id
;
298 void ForEachChunk(ForEachChunkCallback callback
, void *arg
) {
299 allocator
.ForEachChunk(callback
, arg
);
302 IgnoreObjectResult
IgnoreObjectLocked(const void *p
) {
303 void *chunk
= allocator
.GetBlockBegin(p
);
304 if (!chunk
|| p
< chunk
) return kIgnoreObjectInvalid
;
305 ChunkMetadata
*m
= Metadata(chunk
);
307 if (m
->allocated
&& (uptr
)p
< (uptr
)chunk
+ m
->requested_size
) {
308 if (m
->tag
== kIgnored
)
309 return kIgnoreObjectAlreadyIgnored
;
311 return kIgnoreObjectSuccess
;
313 return kIgnoreObjectInvalid
;
317 void GetAdditionalThreadContextPtrs(ThreadContextBase
*tctx
, void *ptrs
) {
318 // This function can be used to treat memory reachable from `tctx` as live.
319 // This is useful for threads that have been created but not yet started.
321 // This is currently a no-op because the LSan `pthread_create()` interceptor
322 // blocks until the child thread starts which keeps the thread's `arg` pointer
326 } // namespace __lsan
328 using namespace __lsan
;
331 SANITIZER_INTERFACE_ATTRIBUTE
332 uptr
__sanitizer_get_current_allocated_bytes() {
333 uptr stats
[AllocatorStatCount
];
334 allocator
.GetStats(stats
);
335 return stats
[AllocatorStatAllocated
];
338 SANITIZER_INTERFACE_ATTRIBUTE
339 uptr
__sanitizer_get_heap_size() {
340 uptr stats
[AllocatorStatCount
];
341 allocator
.GetStats(stats
);
342 return stats
[AllocatorStatMapped
];
345 SANITIZER_INTERFACE_ATTRIBUTE
346 uptr
__sanitizer_get_free_bytes() { return 0; }
348 SANITIZER_INTERFACE_ATTRIBUTE
349 uptr
__sanitizer_get_unmapped_bytes() { return 0; }
351 SANITIZER_INTERFACE_ATTRIBUTE
352 uptr
__sanitizer_get_estimated_allocated_size(uptr size
) { return size
; }
354 SANITIZER_INTERFACE_ATTRIBUTE
355 int __sanitizer_get_ownership(const void *p
) { return Metadata(p
) != nullptr; }
357 SANITIZER_INTERFACE_ATTRIBUTE
358 uptr
__sanitizer_get_allocated_size(const void *p
) {
359 return GetMallocUsableSize(p
);
362 #if !SANITIZER_SUPPORTS_WEAK_HOOKS
363 // Provide default (no-op) implementation of malloc hooks.
364 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
365 void __sanitizer_malloc_hook(void *ptr
, uptr size
) {
369 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
370 void __sanitizer_free_hook(void *ptr
) {