blob: 4284857962feb534b36690d59f468a7fd7bc04ff [file] [log] [blame]
Sergey Matveev866abfb2013-05-20 10:54:00 +00001//=-- lsan_allocator.cc ---------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of LeakSanitizer.
11// See lsan_allocator.h for details.
12//
13//===----------------------------------------------------------------------===//
14
15#include "lsan_allocator.h"
16
17#include "sanitizer_common/sanitizer_allocator.h"
Sergey Matveev08347ca2014-08-26 14:28:28 +000018#include "sanitizer_common/sanitizer_allocator_interface.h"
Sergey Matveev866abfb2013-05-20 10:54:00 +000019#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"
23
Sergey Matveev10548682013-11-24 14:28:18 +000024extern "C" void *memset(void *ptr, int value, uptr num);
25
Sergey Matveev866abfb2013-05-20 10:54:00 +000026namespace __lsan {
Francis Riccid7b08a62017-03-20 13:45:29 +000027
28struct ChunkMetadata {
29 u8 allocated : 8; // Must be first.
30 ChunkTag tag : 2;
31#if SANITIZER_WORDSIZE == 64
32 uptr requested_size : 54;
33#else
34 uptr requested_size : 32;
35 uptr padding : 22;
36#endif
37 u32 stack_trace_id;
38};
39
40#if defined(__mips64) || defined(__aarch64__) || defined(__i386__)
Maxim Ostapenko651cfe32017-01-31 07:15:37 +000041#if defined(__i386__)
42static const uptr kMaxAllowedMallocSize = 1UL << 30;
Francis Riccid7b08a62017-03-20 13:45:29 +000043#else
Mohit K. Bhakkad36f974d2015-02-19 07:30:39 +000044static const uptr kMaxAllowedMallocSize = 4UL << 30;
Francis Riccid7b08a62017-03-20 13:45:29 +000045#endif
46static const uptr kRegionSizeLog = 20;
47static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
48typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
49typedef CompactSizeClassMap SizeClassMap;
50typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE,
51 sizeof(ChunkMetadata), SizeClassMap, kRegionSizeLog, ByteMap>
52 PrimaryAllocator;
Mohit K. Bhakkad36f974d2015-02-19 07:30:39 +000053#else
54static const uptr kMaxAllowedMallocSize = 8UL << 30;
Francis Riccid7b08a62017-03-20 13:45:29 +000055
56struct AP64 { // Allocator64 parameters. Deliberately using a short name.
57 static const uptr kSpaceBeg = 0x600000000000ULL;
58 static const uptr kSpaceSize = 0x40000000000ULL; // 4T.
59 static const uptr kMetadataSize = sizeof(ChunkMetadata);
60 typedef DefaultSizeClassMap SizeClassMap;
61 typedef NoOpMapUnmapCallback MapUnmapCallback;
62 static const uptr kFlags = 0;
63};
64
65typedef SizeClassAllocator64<AP64> PrimaryAllocator;
Mohit K. Bhakkad36f974d2015-02-19 07:30:39 +000066#endif
Francis Riccid7b08a62017-03-20 13:45:29 +000067typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
Sergey Matveev866abfb2013-05-20 10:54:00 +000068typedef LargeMmapAllocator<> SecondaryAllocator;
69typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
70 SecondaryAllocator> Allocator;
71
72static Allocator allocator;
Francis Riccid7b08a62017-03-20 13:45:29 +000073static THREADLOCAL AllocatorCache cache;
Sergey Matveev866abfb2013-05-20 10:54:00 +000074
75void InitializeAllocator() {
Evgeniy Stepanovd3305af2016-11-29 00:22:50 +000076 allocator.InitLinkerInitialized(
77 common_flags()->allocator_may_return_null,
78 common_flags()->allocator_release_to_os_interval_ms);
Sergey Matveev866abfb2013-05-20 10:54:00 +000079}
80
81void AllocatorThreadFinish() {
Francis Riccid7b08a62017-03-20 13:45:29 +000082 allocator.SwallowCache(&cache);
Sergey Matveev866abfb2013-05-20 10:54:00 +000083}
84
Sergey Matveev08347ca2014-08-26 14:28:28 +000085static ChunkMetadata *Metadata(const void *p) {
Sergey Matveev4e0215a2013-06-24 08:34:50 +000086 return reinterpret_cast<ChunkMetadata *>(allocator.GetMetaData(p));
Sergey Matveev866abfb2013-05-20 10:54:00 +000087}
88
89static void RegisterAllocation(const StackTrace &stack, void *p, uptr size) {
90 if (!p) return;
91 ChunkMetadata *m = Metadata(p);
92 CHECK(m);
Sergey Matveevb94d5e22013-06-21 14:51:52 +000093 m->tag = DisabledInThisThread() ? kIgnored : kDirectlyLeaked;
Alexey Samsonov3741ab82014-10-26 06:23:07 +000094 m->stack_trace_id = StackDepotPut(stack);
Sergey Matveev866abfb2013-05-20 10:54:00 +000095 m->requested_size = size;
Sergey Matveev4e0215a2013-06-24 08:34:50 +000096 atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 1, memory_order_relaxed);
Sergey Matveev866abfb2013-05-20 10:54:00 +000097}
98
99static void RegisterDeallocation(void *p) {
100 if (!p) return;
101 ChunkMetadata *m = Metadata(p);
102 CHECK(m);
Sergey Matveev4e0215a2013-06-24 08:34:50 +0000103 atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 0, memory_order_relaxed);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000104}
105
106void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
107 bool cleared) {
108 if (size == 0)
109 size = 1;
110 if (size > kMaxAllowedMallocSize) {
Sergey Matveevd28c03c2013-06-21 15:10:20 +0000111 Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", size);
Vedant Kumar59ba7b82015-10-01 00:22:21 +0000112 return nullptr;
Sergey Matveev866abfb2013-05-20 10:54:00 +0000113 }
Francis Riccid7b08a62017-03-20 13:45:29 +0000114 void *p = allocator.Allocate(&cache, size, alignment, false);
Sergey Matveev10548682013-11-24 14:28:18 +0000115 // Do not rely on the allocator to clear the memory (it's slow).
116 if (cleared && allocator.FromPrimary(p))
117 memset(p, 0, size);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000118 RegisterAllocation(stack, p, size);
Sergey Matveev08347ca2014-08-26 14:28:28 +0000119 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(p, size);
Kostya Serebryanybf6a04f2016-06-16 20:06:06 +0000120 RunMallocHooks(p, size);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000121 return p;
122}
123
124void Deallocate(void *p) {
Sergey Matveev08347ca2014-08-26 14:28:28 +0000125 if (&__sanitizer_free_hook) __sanitizer_free_hook(p);
Kostya Serebryanybf6a04f2016-06-16 20:06:06 +0000126 RunFreeHooks(p);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000127 RegisterDeallocation(p);
Francis Riccid7b08a62017-03-20 13:45:29 +0000128 allocator.Deallocate(&cache, p);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000129}
130
131void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
132 uptr alignment) {
133 RegisterDeallocation(p);
134 if (new_size > kMaxAllowedMallocSize) {
Sergey Matveevd28c03c2013-06-21 15:10:20 +0000135 Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", new_size);
Francis Riccid7b08a62017-03-20 13:45:29 +0000136 allocator.Deallocate(&cache, p);
Vedant Kumar59ba7b82015-10-01 00:22:21 +0000137 return nullptr;
Sergey Matveev866abfb2013-05-20 10:54:00 +0000138 }
Francis Riccid7b08a62017-03-20 13:45:29 +0000139 p = allocator.Reallocate(&cache, p, new_size, alignment);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000140 RegisterAllocation(stack, p, new_size);
141 return p;
142}
143
144void GetAllocatorCacheRange(uptr *begin, uptr *end) {
Francis Riccid7b08a62017-03-20 13:45:29 +0000145 *begin = (uptr)&cache;
146 *end = *begin + sizeof(cache);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000147}
148
Sergey Matveev08347ca2014-08-26 14:28:28 +0000149uptr GetMallocUsableSize(const void *p) {
Sergey Matveev866abfb2013-05-20 10:54:00 +0000150 ChunkMetadata *m = Metadata(p);
151 if (!m) return 0;
152 return m->requested_size;
153}
154
155///// Interface to the common LSan module. /////
156
157void LockAllocator() {
158 allocator.ForceLock();
159}
160
161void UnlockAllocator() {
162 allocator.ForceUnlock();
163}
164
165void GetAllocatorGlobalRange(uptr *begin, uptr *end) {
166 *begin = (uptr)&allocator;
167 *end = *begin + sizeof(allocator);
168}
169
Sergey Matveev4e0215a2013-06-24 08:34:50 +0000170uptr PointsIntoChunk(void* p) {
171 uptr addr = reinterpret_cast<uptr>(p);
172 uptr chunk = reinterpret_cast<uptr>(allocator.GetBlockBeginFastLocked(p));
Sergey Matveev866abfb2013-05-20 10:54:00 +0000173 if (!chunk) return 0;
174 // LargeMmapAllocator considers pointers to the meta-region of a chunk to be
175 // valid, but we don't want that.
Sergey Matveev4e0215a2013-06-24 08:34:50 +0000176 if (addr < chunk) return 0;
177 ChunkMetadata *m = Metadata(reinterpret_cast<void *>(chunk));
Sergey Matveev866abfb2013-05-20 10:54:00 +0000178 CHECK(m);
Kostya Serebryany2b762782014-01-10 10:48:01 +0000179 if (!m->allocated)
180 return 0;
181 if (addr < chunk + m->requested_size)
182 return chunk;
183 if (IsSpecialCaseOfOperatorNew0(chunk, m->requested_size, addr))
Sergey Matveev866abfb2013-05-20 10:54:00 +0000184 return chunk;
185 return 0;
186}
187
Sergey Matveev4e0215a2013-06-24 08:34:50 +0000188uptr GetUserBegin(uptr chunk) {
189 return chunk;
Sergey Matveevbcfd8382013-05-20 13:08:23 +0000190}
191
Sergey Matveev4e0215a2013-06-24 08:34:50 +0000192LsanMetadata::LsanMetadata(uptr chunk) {
193 metadata_ = Metadata(reinterpret_cast<void *>(chunk));
Sergey Matveev866abfb2013-05-20 10:54:00 +0000194 CHECK(metadata_);
195}
196
197bool LsanMetadata::allocated() const {
198 return reinterpret_cast<ChunkMetadata *>(metadata_)->allocated;
199}
200
201ChunkTag LsanMetadata::tag() const {
202 return reinterpret_cast<ChunkMetadata *>(metadata_)->tag;
203}
204
205void LsanMetadata::set_tag(ChunkTag value) {
206 reinterpret_cast<ChunkMetadata *>(metadata_)->tag = value;
207}
208
209uptr LsanMetadata::requested_size() const {
210 return reinterpret_cast<ChunkMetadata *>(metadata_)->requested_size;
211}
212
213u32 LsanMetadata::stack_trace_id() const {
214 return reinterpret_cast<ChunkMetadata *>(metadata_)->stack_trace_id;
215}
216
Sergey Matveev4e0215a2013-06-24 08:34:50 +0000217void ForEachChunk(ForEachChunkCallback callback, void *arg) {
218 allocator.ForEachChunk(callback, arg);
Sergey Matveev866abfb2013-05-20 10:54:00 +0000219}
220
Sergey Matveevecc4f5b2013-06-06 14:17:56 +0000221IgnoreObjectResult IgnoreObjectLocked(const void *p) {
222 void *chunk = allocator.GetBlockBegin(p);
223 if (!chunk || p < chunk) return kIgnoreObjectInvalid;
224 ChunkMetadata *m = Metadata(chunk);
225 CHECK(m);
226 if (m->allocated && (uptr)p < (uptr)chunk + m->requested_size) {
Sergey Matveev978460c2013-06-11 15:26:20 +0000227 if (m->tag == kIgnored)
Sergey Matveevecc4f5b2013-06-06 14:17:56 +0000228 return kIgnoreObjectAlreadyIgnored;
Sergey Matveev978460c2013-06-11 15:26:20 +0000229 m->tag = kIgnored;
Sergey Matveevecc4f5b2013-06-06 14:17:56 +0000230 return kIgnoreObjectSuccess;
231 } else {
232 return kIgnoreObjectInvalid;
233 }
234}
Vedant Kumar59ba7b82015-10-01 00:22:21 +0000235} // namespace __lsan
Sergey Matveev08347ca2014-08-26 14:28:28 +0000236
237using namespace __lsan;
238
239extern "C" {
240SANITIZER_INTERFACE_ATTRIBUTE
241uptr __sanitizer_get_current_allocated_bytes() {
242 uptr stats[AllocatorStatCount];
243 allocator.GetStats(stats);
244 return stats[AllocatorStatAllocated];
245}
246
247SANITIZER_INTERFACE_ATTRIBUTE
248uptr __sanitizer_get_heap_size() {
249 uptr stats[AllocatorStatCount];
250 allocator.GetStats(stats);
251 return stats[AllocatorStatMapped];
252}
253
254SANITIZER_INTERFACE_ATTRIBUTE
255uptr __sanitizer_get_free_bytes() { return 0; }
256
257SANITIZER_INTERFACE_ATTRIBUTE
258uptr __sanitizer_get_unmapped_bytes() { return 0; }
259
260SANITIZER_INTERFACE_ATTRIBUTE
261uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
262
263SANITIZER_INTERFACE_ATTRIBUTE
Vedant Kumar59ba7b82015-10-01 00:22:21 +0000264int __sanitizer_get_ownership(const void *p) { return Metadata(p) != nullptr; }
Sergey Matveev08347ca2014-08-26 14:28:28 +0000265
266SANITIZER_INTERFACE_ATTRIBUTE
267uptr __sanitizer_get_allocated_size(const void *p) {
268 return GetMallocUsableSize(p);
269}
Francis Ricci9a2a9922017-01-07 00:31:20 +0000270
271#if !SANITIZER_SUPPORTS_WEAK_HOOKS
272// Provide default (no-op) implementation of malloc hooks.
273SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
274void __sanitizer_malloc_hook(void *ptr, uptr size) {
275 (void)ptr;
276 (void)size;
277}
278SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
279void __sanitizer_free_hook(void *ptr) {
280 (void)ptr;
281}
282#endif
Vedant Kumar59ba7b82015-10-01 00:22:21 +0000283} // extern "C"