Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 1 | //=-- 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" |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_allocator_checks.h" |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_allocator_interface.h" |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_allocator_report.h" |
Alex Shlyapnikov | d08c32b | 2017-07-14 22:23:46 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_errno.h" |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_internal_defs.h" |
| 23 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| 24 | #include "sanitizer_common/sanitizer_stacktrace.h" |
| 25 | #include "lsan_common.h" |
| 26 | |
Sergey Matveev | 1054868 | 2013-11-24 14:28:18 +0000 | [diff] [blame] | 27 | extern "C" void *memset(void *ptr, int value, uptr num); |
| 28 | |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 29 | namespace __lsan { |
Maxim Ostapenko | de3b9a2 | 2017-04-11 14:58:26 +0000 | [diff] [blame] | 30 | #if defined(__i386__) || defined(__arm__) |
Maxim Ostapenko | 651cfe3 | 2017-01-31 07:15:37 +0000 | [diff] [blame] | 31 | static const uptr kMaxAllowedMallocSize = 1UL << 30; |
Francis Ricci | d668a01 | 2017-03-27 14:07:50 +0000 | [diff] [blame] | 32 | #elif defined(__mips64) || defined(__aarch64__) |
Mohit K. Bhakkad | 36f974d | 2015-02-19 07:30:39 +0000 | [diff] [blame] | 33 | static const uptr kMaxAllowedMallocSize = 4UL << 30; |
Mohit K. Bhakkad | 36f974d | 2015-02-19 07:30:39 +0000 | [diff] [blame] | 34 | #else |
| 35 | static const uptr kMaxAllowedMallocSize = 8UL << 30; |
Mohit K. Bhakkad | 36f974d | 2015-02-19 07:30:39 +0000 | [diff] [blame] | 36 | #endif |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 37 | typedef LargeMmapAllocator<> SecondaryAllocator; |
| 38 | typedef CombinedAllocator<PrimaryAllocator, AllocatorCache, |
| 39 | SecondaryAllocator> Allocator; |
| 40 | |
| 41 | static Allocator allocator; |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 42 | |
| 43 | void InitializeAllocator() { |
Alex Shlyapnikov | ccab11b | 2017-06-20 21:23:02 +0000 | [diff] [blame] | 44 | SetAllocatorMayReturnNull(common_flags()->allocator_may_return_null); |
Evgeniy Stepanov | d3305af | 2016-11-29 00:22:50 +0000 | [diff] [blame] | 45 | allocator.InitLinkerInitialized( |
Evgeniy Stepanov | d3305af | 2016-11-29 00:22:50 +0000 | [diff] [blame] | 46 | common_flags()->allocator_release_to_os_interval_ms); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void AllocatorThreadFinish() { |
Francis Ricci | dc13921 | 2017-03-22 18:42:43 +0000 | [diff] [blame] | 50 | allocator.SwallowCache(GetAllocatorCache()); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 53 | static ChunkMetadata *Metadata(const void *p) { |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 54 | return reinterpret_cast<ChunkMetadata *>(allocator.GetMetaData(p)); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static void RegisterAllocation(const StackTrace &stack, void *p, uptr size) { |
| 58 | if (!p) return; |
| 59 | ChunkMetadata *m = Metadata(p); |
| 60 | CHECK(m); |
Sergey Matveev | b94d5e2 | 2013-06-21 14:51:52 +0000 | [diff] [blame] | 61 | m->tag = DisabledInThisThread() ? kIgnored : kDirectlyLeaked; |
Alexey Samsonov | 3741ab8 | 2014-10-26 06:23:07 +0000 | [diff] [blame] | 62 | m->stack_trace_id = StackDepotPut(stack); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 63 | m->requested_size = size; |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 64 | atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 1, memory_order_relaxed); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static void RegisterDeallocation(void *p) { |
| 68 | if (!p) return; |
| 69 | ChunkMetadata *m = Metadata(p); |
| 70 | CHECK(m); |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 71 | atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 0, memory_order_relaxed); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 74 | static void *ReportAllocationSizeTooBig(uptr size, const StackTrace &stack) { |
| 75 | if (AllocatorMayReturnNull()) { |
| 76 | Report("WARNING: LeakSanitizer failed to allocate 0x%zx bytes\n", size); |
| 77 | return nullptr; |
| 78 | } |
| 79 | ReportAllocationSizeTooBig(size, kMaxAllowedMallocSize, &stack); |
| 80 | } |
| 81 | |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 82 | void *Allocate(const StackTrace &stack, uptr size, uptr alignment, |
| 83 | bool cleared) { |
| 84 | if (size == 0) |
| 85 | size = 1; |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 86 | if (size > kMaxAllowedMallocSize) |
| 87 | return ReportAllocationSizeTooBig(size, stack); |
Alex Shlyapnikov | 5a308f2 | 2017-06-16 21:00:03 +0000 | [diff] [blame] | 88 | void *p = allocator.Allocate(GetAllocatorCache(), size, alignment); |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 89 | if (UNLIKELY(!p)) { |
| 90 | SetAllocatorOutOfMemory(); |
| 91 | if (AllocatorMayReturnNull()) |
| 92 | return nullptr; |
| 93 | ReportOutOfMemory(size, &stack); |
| 94 | } |
Sergey Matveev | 1054868 | 2013-11-24 14:28:18 +0000 | [diff] [blame] | 95 | // Do not rely on the allocator to clear the memory (it's slow). |
| 96 | if (cleared && allocator.FromPrimary(p)) |
| 97 | memset(p, 0, size); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 98 | RegisterAllocation(stack, p, size); |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 99 | if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(p, size); |
Kostya Serebryany | bf6a04f | 2016-06-16 20:06:06 +0000 | [diff] [blame] | 100 | RunMallocHooks(p, size); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 101 | return p; |
| 102 | } |
| 103 | |
Alex Shlyapnikov | d08c32b | 2017-07-14 22:23:46 +0000 | [diff] [blame] | 104 | static void *Calloc(uptr nmemb, uptr size, const StackTrace &stack) { |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 105 | if (UNLIKELY(CheckForCallocOverflow(size, nmemb))) { |
| 106 | if (AllocatorMayReturnNull()) |
| 107 | return nullptr; |
| 108 | ReportCallocOverflow(nmemb, size, &stack); |
| 109 | } |
Alex Shlyapnikov | d08c32b | 2017-07-14 22:23:46 +0000 | [diff] [blame] | 110 | size *= nmemb; |
| 111 | return Allocate(stack, size, 1, true); |
| 112 | } |
| 113 | |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 114 | void Deallocate(void *p) { |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 115 | if (&__sanitizer_free_hook) __sanitizer_free_hook(p); |
Kostya Serebryany | bf6a04f | 2016-06-16 20:06:06 +0000 | [diff] [blame] | 116 | RunFreeHooks(p); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 117 | RegisterDeallocation(p); |
Francis Ricci | dc13921 | 2017-03-22 18:42:43 +0000 | [diff] [blame] | 118 | allocator.Deallocate(GetAllocatorCache(), p); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void *Reallocate(const StackTrace &stack, void *p, uptr new_size, |
| 122 | uptr alignment) { |
| 123 | RegisterDeallocation(p); |
| 124 | if (new_size > kMaxAllowedMallocSize) { |
Francis Ricci | dc13921 | 2017-03-22 18:42:43 +0000 | [diff] [blame] | 125 | allocator.Deallocate(GetAllocatorCache(), p); |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 126 | return ReportAllocationSizeTooBig(new_size, stack); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 127 | } |
Francis Ricci | dc13921 | 2017-03-22 18:42:43 +0000 | [diff] [blame] | 128 | p = allocator.Reallocate(GetAllocatorCache(), p, new_size, alignment); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 129 | RegisterAllocation(stack, p, new_size); |
| 130 | return p; |
| 131 | } |
| 132 | |
| 133 | void GetAllocatorCacheRange(uptr *begin, uptr *end) { |
Francis Ricci | dc13921 | 2017-03-22 18:42:43 +0000 | [diff] [blame] | 134 | *begin = (uptr)GetAllocatorCache(); |
| 135 | *end = *begin + sizeof(AllocatorCache); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 138 | uptr GetMallocUsableSize(const void *p) { |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 139 | ChunkMetadata *m = Metadata(p); |
| 140 | if (!m) return 0; |
| 141 | return m->requested_size; |
| 142 | } |
| 143 | |
Alex Shlyapnikov | 79a7c4f | 2018-03-12 21:59:06 +0000 | [diff] [blame] | 144 | int lsan_posix_memalign(void **memptr, uptr alignment, uptr size, |
| 145 | const StackTrace &stack) { |
| 146 | if (UNLIKELY(!CheckPosixMemalignAlignment(alignment))) { |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 147 | if (AllocatorMayReturnNull()) |
| 148 | return errno_EINVAL; |
| 149 | ReportInvalidPosixMemalignAlignment(alignment, &stack); |
Alex Shlyapnikov | 79a7c4f | 2018-03-12 21:59:06 +0000 | [diff] [blame] | 150 | } |
| 151 | void *ptr = Allocate(stack, size, alignment, kAlwaysClearMemory); |
| 152 | if (UNLIKELY(!ptr)) |
| 153 | // OOM error is already taken care of by Allocate. |
| 154 | return errno_ENOMEM; |
| 155 | CHECK(IsAligned((uptr)ptr, alignment)); |
| 156 | *memptr = ptr; |
| 157 | return 0; |
| 158 | } |
| 159 | |
Alex Shlyapnikov | dcf0097 | 2018-06-08 20:40:35 +0000 | [diff] [blame] | 160 | void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack) { |
| 161 | if (UNLIKELY(!CheckAlignedAllocAlignmentAndSize(alignment, size))) { |
| 162 | errno = errno_EINVAL; |
| 163 | if (AllocatorMayReturnNull()) |
| 164 | return nullptr; |
| 165 | ReportInvalidAlignedAllocAlignment(size, alignment, &stack); |
| 166 | } |
| 167 | return SetErrnoOnNull(Allocate(stack, size, alignment, kAlwaysClearMemory)); |
| 168 | } |
| 169 | |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 170 | void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack) { |
Alex Shlyapnikov | d08c32b | 2017-07-14 22:23:46 +0000 | [diff] [blame] | 171 | if (UNLIKELY(!IsPowerOfTwo(alignment))) { |
| 172 | errno = errno_EINVAL; |
Alex Shlyapnikov | 236c3f9 | 2018-06-05 18:02:09 +0000 | [diff] [blame] | 173 | if (AllocatorMayReturnNull()) |
| 174 | return nullptr; |
| 175 | ReportInvalidAllocationAlignment(alignment, &stack); |
Alex Shlyapnikov | d08c32b | 2017-07-14 22:23:46 +0000 | [diff] [blame] | 176 | } |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame] | 177 | return SetErrnoOnNull(Allocate(stack, size, alignment, kAlwaysClearMemory)); |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void *lsan_malloc(uptr size, const StackTrace &stack) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame] | 181 | return SetErrnoOnNull(Allocate(stack, size, 1, kAlwaysClearMemory)); |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void lsan_free(void *p) { |
| 185 | Deallocate(p); |
| 186 | } |
| 187 | |
| 188 | void *lsan_realloc(void *p, uptr size, const StackTrace &stack) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame] | 189 | return SetErrnoOnNull(Reallocate(stack, p, size, 1)); |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame] | 193 | return SetErrnoOnNull(Calloc(nmemb, size, stack)); |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void *lsan_valloc(uptr size, const StackTrace &stack) { |
Alex Shlyapnikov | 42bea01 | 2017-07-18 19:11:04 +0000 | [diff] [blame] | 197 | return SetErrnoOnNull( |
Alex Shlyapnikov | d08c32b | 2017-07-14 22:23:46 +0000 | [diff] [blame] | 198 | Allocate(stack, size, GetPageSizeCached(), kAlwaysClearMemory)); |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Alex Shlyapnikov | 7894940 | 2018-06-11 17:33:53 +0000 | [diff] [blame] | 201 | void *lsan_pvalloc(uptr size, const StackTrace &stack) { |
| 202 | uptr PageSize = GetPageSizeCached(); |
| 203 | if (UNLIKELY(CheckForPvallocOverflow(size, PageSize))) { |
| 204 | errno = errno_ENOMEM; |
| 205 | if (AllocatorMayReturnNull()) |
| 206 | return nullptr; |
| 207 | ReportPvallocOverflow(size, &stack); |
| 208 | } |
| 209 | // pvalloc(0) should allocate one page. |
| 210 | size = size ? RoundUpTo(size, PageSize) : PageSize; |
| 211 | return SetErrnoOnNull(Allocate(stack, size, PageSize, kAlwaysClearMemory)); |
| 212 | } |
| 213 | |
Francis Ricci | 03b2a8e | 2017-04-11 20:05:02 +0000 | [diff] [blame] | 214 | uptr lsan_mz_size(const void *p) { |
| 215 | return GetMallocUsableSize(p); |
| 216 | } |
| 217 | |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 218 | ///// Interface to the common LSan module. ///// |
| 219 | |
| 220 | void LockAllocator() { |
| 221 | allocator.ForceLock(); |
| 222 | } |
| 223 | |
| 224 | void UnlockAllocator() { |
| 225 | allocator.ForceUnlock(); |
| 226 | } |
| 227 | |
| 228 | void GetAllocatorGlobalRange(uptr *begin, uptr *end) { |
| 229 | *begin = (uptr)&allocator; |
| 230 | *end = *begin + sizeof(allocator); |
| 231 | } |
| 232 | |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 233 | uptr PointsIntoChunk(void* p) { |
| 234 | uptr addr = reinterpret_cast<uptr>(p); |
| 235 | uptr chunk = reinterpret_cast<uptr>(allocator.GetBlockBeginFastLocked(p)); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 236 | if (!chunk) return 0; |
| 237 | // LargeMmapAllocator considers pointers to the meta-region of a chunk to be |
| 238 | // valid, but we don't want that. |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 239 | if (addr < chunk) return 0; |
| 240 | ChunkMetadata *m = Metadata(reinterpret_cast<void *>(chunk)); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 241 | CHECK(m); |
Kostya Serebryany | 2b76278 | 2014-01-10 10:48:01 +0000 | [diff] [blame] | 242 | if (!m->allocated) |
| 243 | return 0; |
| 244 | if (addr < chunk + m->requested_size) |
| 245 | return chunk; |
| 246 | if (IsSpecialCaseOfOperatorNew0(chunk, m->requested_size, addr)) |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 247 | return chunk; |
| 248 | return 0; |
| 249 | } |
| 250 | |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 251 | uptr GetUserBegin(uptr chunk) { |
| 252 | return chunk; |
Sergey Matveev | bcfd838 | 2013-05-20 13:08:23 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 255 | LsanMetadata::LsanMetadata(uptr chunk) { |
| 256 | metadata_ = Metadata(reinterpret_cast<void *>(chunk)); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 257 | CHECK(metadata_); |
| 258 | } |
| 259 | |
| 260 | bool LsanMetadata::allocated() const { |
| 261 | return reinterpret_cast<ChunkMetadata *>(metadata_)->allocated; |
| 262 | } |
| 263 | |
| 264 | ChunkTag LsanMetadata::tag() const { |
| 265 | return reinterpret_cast<ChunkMetadata *>(metadata_)->tag; |
| 266 | } |
| 267 | |
| 268 | void LsanMetadata::set_tag(ChunkTag value) { |
| 269 | reinterpret_cast<ChunkMetadata *>(metadata_)->tag = value; |
| 270 | } |
| 271 | |
| 272 | uptr LsanMetadata::requested_size() const { |
| 273 | return reinterpret_cast<ChunkMetadata *>(metadata_)->requested_size; |
| 274 | } |
| 275 | |
| 276 | u32 LsanMetadata::stack_trace_id() const { |
| 277 | return reinterpret_cast<ChunkMetadata *>(metadata_)->stack_trace_id; |
| 278 | } |
| 279 | |
Sergey Matveev | 4e0215a | 2013-06-24 08:34:50 +0000 | [diff] [blame] | 280 | void ForEachChunk(ForEachChunkCallback callback, void *arg) { |
| 281 | allocator.ForEachChunk(callback, arg); |
Sergey Matveev | 866abfb | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Sergey Matveev | ecc4f5b | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 284 | IgnoreObjectResult IgnoreObjectLocked(const void *p) { |
| 285 | void *chunk = allocator.GetBlockBegin(p); |
| 286 | if (!chunk || p < chunk) return kIgnoreObjectInvalid; |
| 287 | ChunkMetadata *m = Metadata(chunk); |
| 288 | CHECK(m); |
| 289 | if (m->allocated && (uptr)p < (uptr)chunk + m->requested_size) { |
Sergey Matveev | 978460c | 2013-06-11 15:26:20 +0000 | [diff] [blame] | 290 | if (m->tag == kIgnored) |
Sergey Matveev | ecc4f5b | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 291 | return kIgnoreObjectAlreadyIgnored; |
Sergey Matveev | 978460c | 2013-06-11 15:26:20 +0000 | [diff] [blame] | 292 | m->tag = kIgnored; |
Sergey Matveev | ecc4f5b | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 293 | return kIgnoreObjectSuccess; |
| 294 | } else { |
| 295 | return kIgnoreObjectInvalid; |
| 296 | } |
| 297 | } |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 298 | } // namespace __lsan |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 299 | |
| 300 | using namespace __lsan; |
| 301 | |
| 302 | extern "C" { |
| 303 | SANITIZER_INTERFACE_ATTRIBUTE |
| 304 | uptr __sanitizer_get_current_allocated_bytes() { |
| 305 | uptr stats[AllocatorStatCount]; |
| 306 | allocator.GetStats(stats); |
| 307 | return stats[AllocatorStatAllocated]; |
| 308 | } |
| 309 | |
| 310 | SANITIZER_INTERFACE_ATTRIBUTE |
| 311 | uptr __sanitizer_get_heap_size() { |
| 312 | uptr stats[AllocatorStatCount]; |
| 313 | allocator.GetStats(stats); |
| 314 | return stats[AllocatorStatMapped]; |
| 315 | } |
| 316 | |
| 317 | SANITIZER_INTERFACE_ATTRIBUTE |
| 318 | uptr __sanitizer_get_free_bytes() { return 0; } |
| 319 | |
| 320 | SANITIZER_INTERFACE_ATTRIBUTE |
| 321 | uptr __sanitizer_get_unmapped_bytes() { return 0; } |
| 322 | |
| 323 | SANITIZER_INTERFACE_ATTRIBUTE |
| 324 | uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; } |
| 325 | |
| 326 | SANITIZER_INTERFACE_ATTRIBUTE |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 327 | int __sanitizer_get_ownership(const void *p) { return Metadata(p) != nullptr; } |
Sergey Matveev | 08347ca | 2014-08-26 14:28:28 +0000 | [diff] [blame] | 328 | |
| 329 | SANITIZER_INTERFACE_ATTRIBUTE |
| 330 | uptr __sanitizer_get_allocated_size(const void *p) { |
| 331 | return GetMallocUsableSize(p); |
| 332 | } |
Francis Ricci | 9a2a992 | 2017-01-07 00:31:20 +0000 | [diff] [blame] | 333 | |
| 334 | #if !SANITIZER_SUPPORTS_WEAK_HOOKS |
| 335 | // Provide default (no-op) implementation of malloc hooks. |
| 336 | SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE |
| 337 | void __sanitizer_malloc_hook(void *ptr, uptr size) { |
| 338 | (void)ptr; |
| 339 | (void)size; |
| 340 | } |
| 341 | SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE |
| 342 | void __sanitizer_free_hook(void *ptr) { |
| 343 | (void)ptr; |
| 344 | } |
| 345 | #endif |
Vedant Kumar | 59ba7b8 | 2015-10-01 00:22:21 +0000 | [diff] [blame] | 346 | } // extern "C" |