Sergey Matveev | c99de51 | 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" |
| 18 | #include "sanitizer_common/sanitizer_internal_defs.h" |
| 19 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| 20 | #include "sanitizer_common/sanitizer_stacktrace.h" |
| 21 | #include "lsan_common.h" |
| 22 | |
| 23 | namespace __lsan { |
| 24 | |
| 25 | static const uptr kMaxAllowedMallocSize = |
| 26 | FIRST_32_SECOND_64(3UL << 30, 8UL << 30); |
| 27 | |
| 28 | static const uptr kAllocatorSpace = 0x600000000000ULL; |
| 29 | static const uptr kAllocatorSize = 0x10000000000ULL; // 1T. |
| 30 | |
| 31 | struct ChunkMetadata { |
| 32 | bool allocated : 8; // Must be first. |
| 33 | ChunkTag tag : 2; |
| 34 | uptr requested_size : 54; |
| 35 | u32 stack_trace_id; |
| 36 | }; |
| 37 | |
| 38 | typedef SizeClassAllocator64<kAllocatorSpace, kAllocatorSize, |
| 39 | sizeof(ChunkMetadata), CompactSizeClassMap> PrimaryAllocator; |
| 40 | typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache; |
| 41 | typedef LargeMmapAllocator<> SecondaryAllocator; |
| 42 | typedef CombinedAllocator<PrimaryAllocator, AllocatorCache, |
| 43 | SecondaryAllocator> Allocator; |
| 44 | |
| 45 | static Allocator allocator; |
| 46 | static THREADLOCAL AllocatorCache cache; |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 47 | // All allocations made while this is > 0 will be treated as non-leaks. |
| 48 | static THREADLOCAL uptr lsan_disabled; |
Sergey Matveev | c99de51 | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 49 | |
| 50 | void InitializeAllocator() { |
| 51 | allocator.Init(); |
| 52 | } |
| 53 | |
| 54 | void AllocatorThreadFinish() { |
| 55 | allocator.SwallowCache(&cache); |
| 56 | } |
| 57 | |
| 58 | static ChunkMetadata *Metadata(void *p) { |
| 59 | return (ChunkMetadata *)allocator.GetMetaData(p); |
| 60 | } |
| 61 | |
| 62 | static void RegisterAllocation(const StackTrace &stack, void *p, uptr size) { |
| 63 | if (!p) return; |
| 64 | ChunkMetadata *m = Metadata(p); |
| 65 | CHECK(m); |
Sergey Matveev | b3b46da | 2013-06-11 15:26:20 +0000 | [diff] [blame] | 66 | m->tag = lsan_disabled ? kIgnored : kDirectlyLeaked; |
Sergey Matveev | c99de51 | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 67 | m->stack_trace_id = StackDepotPut(stack.trace, stack.size); |
| 68 | m->requested_size = size; |
| 69 | atomic_store((atomic_uint8_t*)m, 1, memory_order_relaxed); |
| 70 | } |
| 71 | |
| 72 | static void RegisterDeallocation(void *p) { |
| 73 | if (!p) return; |
| 74 | ChunkMetadata *m = Metadata(p); |
| 75 | CHECK(m); |
| 76 | atomic_store((atomic_uint8_t*)m, 0, memory_order_relaxed); |
| 77 | } |
| 78 | |
| 79 | void *Allocate(const StackTrace &stack, uptr size, uptr alignment, |
| 80 | bool cleared) { |
| 81 | if (size == 0) |
| 82 | size = 1; |
| 83 | if (size > kMaxAllowedMallocSize) { |
| 84 | Report("WARNING: LeakSanitizer failed to allocate %p bytes\n", |
| 85 | (void*)size); |
| 86 | return 0; |
| 87 | } |
| 88 | void *p = allocator.Allocate(&cache, size, alignment, cleared); |
| 89 | RegisterAllocation(stack, p, size); |
| 90 | return p; |
| 91 | } |
| 92 | |
| 93 | void Deallocate(void *p) { |
| 94 | RegisterDeallocation(p); |
| 95 | allocator.Deallocate(&cache, p); |
| 96 | } |
| 97 | |
| 98 | void *Reallocate(const StackTrace &stack, void *p, uptr new_size, |
| 99 | uptr alignment) { |
| 100 | RegisterDeallocation(p); |
| 101 | if (new_size > kMaxAllowedMallocSize) { |
| 102 | Report("WARNING: LeakSanitizer failed to allocate %p bytes\n", |
| 103 | (void*)new_size); |
| 104 | allocator.Deallocate(&cache, p); |
| 105 | return 0; |
| 106 | } |
| 107 | p = allocator.Reallocate(&cache, p, new_size, alignment); |
| 108 | RegisterAllocation(stack, p, new_size); |
| 109 | return p; |
| 110 | } |
| 111 | |
| 112 | void GetAllocatorCacheRange(uptr *begin, uptr *end) { |
| 113 | *begin = (uptr)&cache; |
| 114 | *end = *begin + sizeof(cache); |
| 115 | } |
| 116 | |
| 117 | uptr GetMallocUsableSize(void *p) { |
| 118 | ChunkMetadata *m = Metadata(p); |
| 119 | if (!m) return 0; |
| 120 | return m->requested_size; |
| 121 | } |
| 122 | |
| 123 | ///// Interface to the common LSan module. ///// |
| 124 | |
| 125 | void LockAllocator() { |
| 126 | allocator.ForceLock(); |
| 127 | } |
| 128 | |
| 129 | void UnlockAllocator() { |
| 130 | allocator.ForceUnlock(); |
| 131 | } |
| 132 | |
| 133 | void GetAllocatorGlobalRange(uptr *begin, uptr *end) { |
| 134 | *begin = (uptr)&allocator; |
| 135 | *end = *begin + sizeof(allocator); |
| 136 | } |
| 137 | |
| 138 | void *PointsIntoChunk(void* p) { |
Sergey Matveev | ba2169a | 2013-05-31 11:13:45 +0000 | [diff] [blame] | 139 | void *chunk = allocator.GetBlockBeginFastLocked(p); |
Sergey Matveev | c99de51 | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 140 | if (!chunk) return 0; |
| 141 | // LargeMmapAllocator considers pointers to the meta-region of a chunk to be |
| 142 | // valid, but we don't want that. |
| 143 | if (p < chunk) return 0; |
| 144 | ChunkMetadata *m = Metadata(chunk); |
| 145 | CHECK(m); |
| 146 | if (m->allocated && (uptr)p < (uptr)chunk + m->requested_size) |
| 147 | return chunk; |
| 148 | return 0; |
| 149 | } |
| 150 | |
Sergey Matveev | 29b7568 | 2013-05-20 13:08:23 +0000 | [diff] [blame] | 151 | void *GetUserBegin(void *p) { |
| 152 | return p; |
| 153 | } |
| 154 | |
Sergey Matveev | c99de51 | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 155 | LsanMetadata::LsanMetadata(void *chunk) { |
| 156 | metadata_ = Metadata(chunk); |
| 157 | CHECK(metadata_); |
| 158 | } |
| 159 | |
| 160 | bool LsanMetadata::allocated() const { |
| 161 | return reinterpret_cast<ChunkMetadata *>(metadata_)->allocated; |
| 162 | } |
| 163 | |
| 164 | ChunkTag LsanMetadata::tag() const { |
| 165 | return reinterpret_cast<ChunkMetadata *>(metadata_)->tag; |
| 166 | } |
| 167 | |
| 168 | void LsanMetadata::set_tag(ChunkTag value) { |
| 169 | reinterpret_cast<ChunkMetadata *>(metadata_)->tag = value; |
| 170 | } |
| 171 | |
| 172 | uptr LsanMetadata::requested_size() const { |
| 173 | return reinterpret_cast<ChunkMetadata *>(metadata_)->requested_size; |
| 174 | } |
| 175 | |
| 176 | u32 LsanMetadata::stack_trace_id() const { |
| 177 | return reinterpret_cast<ChunkMetadata *>(metadata_)->stack_trace_id; |
| 178 | } |
| 179 | |
| 180 | template<typename Callable> |
| 181 | void ForEachChunk(Callable const &callback) { |
| 182 | allocator.ForEachChunk(callback); |
| 183 | } |
| 184 | |
| 185 | template void ForEachChunk<ProcessPlatformSpecificAllocationsCb>( |
| 186 | ProcessPlatformSpecificAllocationsCb const &callback); |
| 187 | template void ForEachChunk<PrintLeakedCb>(PrintLeakedCb const &callback); |
| 188 | template void ForEachChunk<CollectLeaksCb>(CollectLeaksCb const &callback); |
| 189 | template void ForEachChunk<MarkIndirectlyLeakedCb>( |
| 190 | MarkIndirectlyLeakedCb const &callback); |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 191 | template void ForEachChunk<CollectSuppressedCb>( |
| 192 | CollectSuppressedCb const &callback); |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 193 | |
| 194 | IgnoreObjectResult IgnoreObjectLocked(const void *p) { |
| 195 | void *chunk = allocator.GetBlockBegin(p); |
| 196 | if (!chunk || p < chunk) return kIgnoreObjectInvalid; |
| 197 | ChunkMetadata *m = Metadata(chunk); |
| 198 | CHECK(m); |
| 199 | if (m->allocated && (uptr)p < (uptr)chunk + m->requested_size) { |
Sergey Matveev | b3b46da | 2013-06-11 15:26:20 +0000 | [diff] [blame] | 200 | if (m->tag == kIgnored) |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 201 | return kIgnoreObjectAlreadyIgnored; |
Sergey Matveev | b3b46da | 2013-06-11 15:26:20 +0000 | [diff] [blame] | 202 | m->tag = kIgnored; |
Sergey Matveev | cd571e0 | 2013-06-06 14:17:56 +0000 | [diff] [blame] | 203 | return kIgnoreObjectSuccess; |
| 204 | } else { |
| 205 | return kIgnoreObjectInvalid; |
| 206 | } |
| 207 | } |
Sergey Matveev | c99de51 | 2013-05-20 10:54:00 +0000 | [diff] [blame] | 208 | } // namespace __lsan |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 209 | |
| 210 | extern "C" { |
Sergey Matveev | 46ed75f | 2013-06-06 18:40:55 +0000 | [diff] [blame] | 211 | SANITIZER_INTERFACE_ATTRIBUTE |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 212 | void __lsan_disable() { |
| 213 | __lsan::lsan_disabled++; |
| 214 | } |
| 215 | |
Sergey Matveev | 46ed75f | 2013-06-06 18:40:55 +0000 | [diff] [blame] | 216 | SANITIZER_INTERFACE_ATTRIBUTE |
Sergey Matveev | 5e719a7 | 2013-06-03 11:21:34 +0000 | [diff] [blame] | 217 | void __lsan_enable() { |
| 218 | if (!__lsan::lsan_disabled) { |
| 219 | Report("Unmatched call to __lsan_enable().\n"); |
| 220 | Die(); |
| 221 | } |
| 222 | __lsan::lsan_disabled--; |
| 223 | } |
| 224 | } // extern "C" |
| 225 | |