blob: eebfc8b1a2769e8e54f9ce9c5e3360af5f2d1367 [file] [log] [blame]
Sergey Matveev866abfb2013-05-20 10:54:00 +00001//=-- lsan_allocator.h ----------------------------------------------------===//
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// Allocator for standalone LSan.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LSAN_ALLOCATOR_H
16#define LSAN_ALLOCATOR_H
17
Francis Riccid668a012017-03-27 14:07:50 +000018#include "sanitizer_common/sanitizer_allocator.h"
Sergey Matveev866abfb2013-05-20 10:54:00 +000019#include "sanitizer_common/sanitizer_common.h"
20#include "sanitizer_common/sanitizer_internal_defs.h"
Francis Riccid668a012017-03-27 14:07:50 +000021#include "lsan_common.h"
Sergey Matveev866abfb2013-05-20 10:54:00 +000022
23namespace __lsan {
24
25void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
26 bool cleared);
27void Deallocate(void *p);
28void *Reallocate(const StackTrace &stack, void *p, uptr new_size,
29 uptr alignment);
Sergey Matveev08347ca2014-08-26 14:28:28 +000030uptr GetMallocUsableSize(const void *p);
Sergey Matveev866abfb2013-05-20 10:54:00 +000031
32template<typename Callable>
33void ForEachChunk(const Callable &callback);
34
35void GetAllocatorCacheRange(uptr *begin, uptr *end);
36void AllocatorThreadFinish();
37void InitializeAllocator();
38
Francis Ricci03b2a8e2017-04-11 20:05:02 +000039const bool kAlwaysClearMemory = true;
40
Francis Riccid668a012017-03-27 14:07:50 +000041struct ChunkMetadata {
42 u8 allocated : 8; // Must be first.
43 ChunkTag tag : 2;
44#if SANITIZER_WORDSIZE == 64
45 uptr requested_size : 54;
46#else
47 uptr requested_size : 32;
48 uptr padding : 22;
49#endif
50 u32 stack_trace_id;
51};
52
Maxim Ostapenkode3b9a22017-04-11 14:58:26 +000053#if defined(__mips64) || defined(__aarch64__) || defined(__i386__) || \
54 defined(__arm__)
Francis Riccid668a012017-03-27 14:07:50 +000055static const uptr kRegionSizeLog = 20;
56static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog;
57typedef TwoLevelByteMap<(kNumRegions >> 12), 1 << 12> ByteMap;
Kostya Kortchinskydc646a02017-05-15 14:47:19 +000058
59struct AP32 {
60 static const uptr kSpaceBeg = 0;
61 static const u64 kSpaceSize = SANITIZER_MMAP_RANGE_SIZE;
62 static const uptr kMetadataSize = sizeof(ChunkMetadata);
63 typedef __sanitizer::CompactSizeClassMap SizeClassMap;
64 static const uptr kRegionSizeLog = __lsan::kRegionSizeLog;
65 typedef __lsan::ByteMap ByteMap;
66 typedef NoOpMapUnmapCallback MapUnmapCallback;
67 static const uptr kFlags = 0;
68};
69typedef SizeClassAllocator32<AP32> PrimaryAllocator;
Alex Shlyapnikov342586d2017-04-21 21:59:53 +000070#elif defined(__x86_64__) || defined(__powerpc64__)
Alex Shlyapnikov02bda3742017-10-26 01:22:48 +000071# if defined(__powerpc64__)
72const uptr kAllocatorSpace = 0xa0000000000ULL;
73const uptr kAllocatorSize = 0x20000000000ULL; // 2T.
74# else
75const uptr kAllocatorSpace = 0x600000000000ULL;
76const uptr kAllocatorSize = 0x40000000000ULL; // 4T.
77# endif
Francis Riccid668a012017-03-27 14:07:50 +000078struct AP64 { // Allocator64 parameters. Deliberately using a short name.
Alex Shlyapnikov02bda3742017-10-26 01:22:48 +000079 static const uptr kSpaceBeg = kAllocatorSpace;
80 static const uptr kSpaceSize = kAllocatorSize;
Francis Riccid668a012017-03-27 14:07:50 +000081 static const uptr kMetadataSize = sizeof(ChunkMetadata);
82 typedef DefaultSizeClassMap SizeClassMap;
83 typedef NoOpMapUnmapCallback MapUnmapCallback;
84 static const uptr kFlags = 0;
85};
86
87typedef SizeClassAllocator64<AP64> PrimaryAllocator;
88#endif
89typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
90
91AllocatorCache *GetAllocatorCache();
Francis Ricci03b2a8e2017-04-11 20:05:02 +000092
Alex Shlyapnikov79a7c4f2018-03-12 21:59:06 +000093int lsan_posix_memalign(void **memptr, uptr alignment, uptr size,
94 const StackTrace &stack);
Francis Ricci03b2a8e2017-04-11 20:05:02 +000095void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
96void *lsan_malloc(uptr size, const StackTrace &stack);
97void lsan_free(void *p);
98void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
99void *lsan_calloc(uptr nmemb, uptr size, const StackTrace &stack);
100void *lsan_valloc(uptr size, const StackTrace &stack);
101uptr lsan_mz_size(const void *p);
102
Sergey Matveev866abfb2013-05-20 10:54:00 +0000103} // namespace __lsan
104
105#endif // LSAN_ALLOCATOR_H