2013-05-20 18:54:00 +08:00
|
|
|
//=-- lsan_allocator.cc ---------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of LeakSanitizer.
|
|
|
|
// See lsan_allocator.h for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lsan_allocator.h"
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_allocator.h"
|
2014-08-26 22:28:28 +08:00
|
|
|
#include "sanitizer_common/sanitizer_allocator_interface.h"
|
2013-05-20 18:54:00 +08:00
|
|
|
#include "sanitizer_common/sanitizer_internal_defs.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
|
|
|
#include "lsan_common.h"
|
|
|
|
|
2013-11-24 22:28:18 +08:00
|
|
|
extern "C" void *memset(void *ptr, int value, uptr num);
|
|
|
|
|
2013-05-20 18:54:00 +08:00
|
|
|
namespace __lsan {
|
2017-03-21 23:31:15 +08:00
|
|
|
|
|
|
|
struct ChunkMetadata {
|
|
|
|
u8 allocated : 8; // Must be first.
|
|
|
|
ChunkTag tag : 2;
|
|
|
|
#if SANITIZER_WORDSIZE == 64
|
|
|
|
uptr requested_size : 54;
|
|
|
|
#else
|
|
|
|
uptr requested_size : 32;
|
|
|
|
uptr padding : 22;
|
|
|
|
#endif
|
|
|
|
u32 stack_trace_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if defined(__mips64) || defined(__aarch64__) || defined(__i386__)
|
2017-01-31 15:15:37 +08:00
|
|
|
#if defined(__i386__)
|
|
|
|
static const uptr kMaxAllowedMallocSize = 1UL << 30;
|
2017-03-21 23:31:15 +08:00
|
|
|
#else
|
2015-02-19 15:30:39 +08:00
|
|
|
static const uptr kMaxAllowedMallocSize = 4UL << 30;
|
2017-03-21 23:31:15 +08:00
|
|
|
#endif
|
|
|
|
static const uptr kRegionSizeLog = 20;
|
|
|
|
static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
|
|
|
|
typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
|
|
|
|
typedef CompactSizeClassMap SizeClassMap;
|
|
|
|
typedef SizeClassAllocator32<0, SANITIZER_MMAP_RANGE_SIZE,
|
|
|
|
sizeof(ChunkMetadata), SizeClassMap, kRegionSizeLog, ByteMap>
|
|
|
|
PrimaryAllocator;
|
2015-02-19 15:30:39 +08:00
|
|
|
#else
|
|
|
|
static const uptr kMaxAllowedMallocSize = 8UL << 30;
|
2017-03-21 23:31:15 +08:00
|
|
|
|
|
|
|
struct AP64 { // Allocator64 parameters. Deliberately using a short name.
|
|
|
|
static const uptr kSpaceBeg = 0x600000000000ULL;
|
|
|
|
static const uptr kSpaceSize = 0x40000000000ULL; // 4T.
|
|
|
|
static const uptr kMetadataSize = sizeof(ChunkMetadata);
|
|
|
|
typedef DefaultSizeClassMap SizeClassMap;
|
|
|
|
typedef NoOpMapUnmapCallback MapUnmapCallback;
|
|
|
|
static const uptr kFlags = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SizeClassAllocator64<AP64> PrimaryAllocator;
|
2015-02-19 15:30:39 +08:00
|
|
|
#endif
|
2017-03-21 23:31:15 +08:00
|
|
|
typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
|
2013-05-20 18:54:00 +08:00
|
|
|
typedef LargeMmapAllocator<> SecondaryAllocator;
|
|
|
|
typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
|
|
|
|
SecondaryAllocator> Allocator;
|
|
|
|
|
|
|
|
static Allocator allocator;
|
2017-03-21 23:31:15 +08:00
|
|
|
static THREADLOCAL AllocatorCache cache;
|
2013-05-20 18:54:00 +08:00
|
|
|
|
|
|
|
void InitializeAllocator() {
|
2016-11-29 08:22:50 +08:00
|
|
|
allocator.InitLinkerInitialized(
|
|
|
|
common_flags()->allocator_may_return_null,
|
|
|
|
common_flags()->allocator_release_to_os_interval_ms);
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AllocatorThreadFinish() {
|
2017-03-21 23:31:15 +08:00
|
|
|
allocator.SwallowCache(&cache);
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 22:28:28 +08:00
|
|
|
static ChunkMetadata *Metadata(const void *p) {
|
2013-06-24 16:34:50 +08:00
|
|
|
return reinterpret_cast<ChunkMetadata *>(allocator.GetMetaData(p));
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RegisterAllocation(const StackTrace &stack, void *p, uptr size) {
|
|
|
|
if (!p) return;
|
|
|
|
ChunkMetadata *m = Metadata(p);
|
|
|
|
CHECK(m);
|
2013-06-21 22:51:52 +08:00
|
|
|
m->tag = DisabledInThisThread() ? kIgnored : kDirectlyLeaked;
|
2014-10-26 14:23:07 +08:00
|
|
|
m->stack_trace_id = StackDepotPut(stack);
|
2013-05-20 18:54:00 +08:00
|
|
|
m->requested_size = size;
|
2013-06-24 16:34:50 +08:00
|
|
|
atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 1, memory_order_relaxed);
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void RegisterDeallocation(void *p) {
|
|
|
|
if (!p) return;
|
|
|
|
ChunkMetadata *m = Metadata(p);
|
|
|
|
CHECK(m);
|
2013-06-24 16:34:50 +08:00
|
|
|
atomic_store(reinterpret_cast<atomic_uint8_t *>(m), 0, memory_order_relaxed);
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
|
|
|
|
bool cleared) {
|
|
|
|
if (size == 0)
|
|
|
|
size = 1;
|
|
|
|
if (size > kMaxAllowedMallocSize) {
|
2013-06-21 23:10:20 +08:00
|
|
|
Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", size);
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr;
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
2017-03-21 23:31:15 +08:00
|
|
|
void *p = allocator.Allocate(&cache, size, alignment, false);
|
2013-11-24 22:28:18 +08:00
|
|
|
// Do not rely on the allocator to clear the memory (it's slow).
|
|
|
|
if (cleared && allocator.FromPrimary(p))
|
|
|
|
memset(p, 0, size);
|
2013-05-20 18:54:00 +08:00
|
|
|
RegisterAllocation(stack, p, size);
|
2014-08-26 22:28:28 +08:00
|
|
|
if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(p, size);
|
2016-06-17 04:06:06 +08:00
|
|
|
RunMallocHooks(p, size);
|
2013-05-20 18:54:00 +08:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Deallocate(void *p) {
|
2014-08-26 22:28:28 +08:00
|
|
|
if (&__sanitizer_free_hook) __sanitizer_free_hook(p);
|
2016-06-17 04:06:06 +08:00
|
|
|
RunFreeHooks(p);
|
2013-05-20 18:54:00 +08:00
|
|
|
RegisterDeallocation(p);
|
2017-03-21 23:31:15 +08:00
|
|
|
allocator.Deallocate(&cache, p);
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
|
|
|
|
uptr alignment) {
|
|
|
|
RegisterDeallocation(p);
|
|
|
|
if (new_size > kMaxAllowedMallocSize) {
|
2013-06-21 23:10:20 +08:00
|
|
|
Report("WARNING: LeakSanitizer failed to allocate %zu bytes\n", new_size);
|
2017-03-21 23:31:15 +08:00
|
|
|
allocator.Deallocate(&cache, p);
|
2015-10-01 08:22:21 +08:00
|
|
|
return nullptr;
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
2017-03-21 23:31:15 +08:00
|
|
|
p = allocator.Reallocate(&cache, p, new_size, alignment);
|
2013-05-20 18:54:00 +08:00
|
|
|
RegisterAllocation(stack, p, new_size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllocatorCacheRange(uptr *begin, uptr *end) {
|
2017-03-21 23:31:15 +08:00
|
|
|
*begin = (uptr)&cache;
|
|
|
|
*end = *begin + sizeof(cache);
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
2014-08-26 22:28:28 +08:00
|
|
|
uptr GetMallocUsableSize(const void *p) {
|
2013-05-20 18:54:00 +08:00
|
|
|
ChunkMetadata *m = Metadata(p);
|
|
|
|
if (!m) return 0;
|
|
|
|
return m->requested_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
///// Interface to the common LSan module. /////
|
|
|
|
|
|
|
|
void LockAllocator() {
|
|
|
|
allocator.ForceLock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnlockAllocator() {
|
|
|
|
allocator.ForceUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetAllocatorGlobalRange(uptr *begin, uptr *end) {
|
|
|
|
*begin = (uptr)&allocator;
|
|
|
|
*end = *begin + sizeof(allocator);
|
|
|
|
}
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
uptr PointsIntoChunk(void* p) {
|
|
|
|
uptr addr = reinterpret_cast<uptr>(p);
|
|
|
|
uptr chunk = reinterpret_cast<uptr>(allocator.GetBlockBeginFastLocked(p));
|
2013-05-20 18:54:00 +08:00
|
|
|
if (!chunk) return 0;
|
|
|
|
// LargeMmapAllocator considers pointers to the meta-region of a chunk to be
|
|
|
|
// valid, but we don't want that.
|
2013-06-24 16:34:50 +08:00
|
|
|
if (addr < chunk) return 0;
|
|
|
|
ChunkMetadata *m = Metadata(reinterpret_cast<void *>(chunk));
|
2013-05-20 18:54:00 +08:00
|
|
|
CHECK(m);
|
2014-01-10 18:48:01 +08:00
|
|
|
if (!m->allocated)
|
|
|
|
return 0;
|
|
|
|
if (addr < chunk + m->requested_size)
|
|
|
|
return chunk;
|
|
|
|
if (IsSpecialCaseOfOperatorNew0(chunk, m->requested_size, addr))
|
2013-05-20 18:54:00 +08:00
|
|
|
return chunk;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
uptr GetUserBegin(uptr chunk) {
|
|
|
|
return chunk;
|
2013-05-20 21:08:23 +08:00
|
|
|
}
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
LsanMetadata::LsanMetadata(uptr chunk) {
|
|
|
|
metadata_ = Metadata(reinterpret_cast<void *>(chunk));
|
2013-05-20 18:54:00 +08:00
|
|
|
CHECK(metadata_);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LsanMetadata::allocated() const {
|
|
|
|
return reinterpret_cast<ChunkMetadata *>(metadata_)->allocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChunkTag LsanMetadata::tag() const {
|
|
|
|
return reinterpret_cast<ChunkMetadata *>(metadata_)->tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LsanMetadata::set_tag(ChunkTag value) {
|
|
|
|
reinterpret_cast<ChunkMetadata *>(metadata_)->tag = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr LsanMetadata::requested_size() const {
|
|
|
|
return reinterpret_cast<ChunkMetadata *>(metadata_)->requested_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 LsanMetadata::stack_trace_id() const {
|
|
|
|
return reinterpret_cast<ChunkMetadata *>(metadata_)->stack_trace_id;
|
|
|
|
}
|
|
|
|
|
2013-06-24 16:34:50 +08:00
|
|
|
void ForEachChunk(ForEachChunkCallback callback, void *arg) {
|
|
|
|
allocator.ForEachChunk(callback, arg);
|
2013-05-20 18:54:00 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 22:17:56 +08:00
|
|
|
IgnoreObjectResult IgnoreObjectLocked(const void *p) {
|
|
|
|
void *chunk = allocator.GetBlockBegin(p);
|
|
|
|
if (!chunk || p < chunk) return kIgnoreObjectInvalid;
|
|
|
|
ChunkMetadata *m = Metadata(chunk);
|
|
|
|
CHECK(m);
|
|
|
|
if (m->allocated && (uptr)p < (uptr)chunk + m->requested_size) {
|
2013-06-11 23:26:20 +08:00
|
|
|
if (m->tag == kIgnored)
|
2013-06-06 22:17:56 +08:00
|
|
|
return kIgnoreObjectAlreadyIgnored;
|
2013-06-11 23:26:20 +08:00
|
|
|
m->tag = kIgnored;
|
2013-06-06 22:17:56 +08:00
|
|
|
return kIgnoreObjectSuccess;
|
|
|
|
} else {
|
|
|
|
return kIgnoreObjectInvalid;
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 08:22:21 +08:00
|
|
|
} // namespace __lsan
|
2014-08-26 22:28:28 +08:00
|
|
|
|
|
|
|
using namespace __lsan;
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __sanitizer_get_current_allocated_bytes() {
|
|
|
|
uptr stats[AllocatorStatCount];
|
|
|
|
allocator.GetStats(stats);
|
|
|
|
return stats[AllocatorStatAllocated];
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __sanitizer_get_heap_size() {
|
|
|
|
uptr stats[AllocatorStatCount];
|
|
|
|
allocator.GetStats(stats);
|
|
|
|
return stats[AllocatorStatMapped];
|
|
|
|
}
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __sanitizer_get_free_bytes() { return 0; }
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __sanitizer_get_unmapped_bytes() { return 0; }
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __sanitizer_get_estimated_allocated_size(uptr size) { return size; }
|
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
2015-10-01 08:22:21 +08:00
|
|
|
int __sanitizer_get_ownership(const void *p) { return Metadata(p) != nullptr; }
|
2014-08-26 22:28:28 +08:00
|
|
|
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
uptr __sanitizer_get_allocated_size(const void *p) {
|
|
|
|
return GetMallocUsableSize(p);
|
|
|
|
}
|
2017-01-07 08:31:20 +08:00
|
|
|
|
|
|
|
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
|
|
|
|
// Provide default (no-op) implementation of malloc hooks.
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
void __sanitizer_malloc_hook(void *ptr, uptr size) {
|
|
|
|
(void)ptr;
|
|
|
|
(void)size;
|
|
|
|
}
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
void __sanitizer_free_hook(void *ptr) {
|
|
|
|
(void)ptr;
|
|
|
|
}
|
|
|
|
#endif
|
2015-10-01 08:22:21 +08:00
|
|
|
} // extern "C"
|