24
24
extern " C" void *memset (void *ptr, int value, uptr num);
25
25
26
26
namespace __lsan {
27
-
28
- struct 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__)
41
27
#if defined(__i386__)
42
28
static const uptr kMaxAllowedMallocSize = 1UL << 30 ;
43
- #else
29
+ #elif defined(__mips64) || defined(__aarch64__)
44
30
static const uptr kMaxAllowedMallocSize = 4UL << 30 ;
45
- #endif
46
- static const uptr kRegionSizeLog = 20 ;
47
- static const uptr kNumRegions = SANITIZER_MMAP_RANGE_SIZE >> kRegionSizeLog ;
48
- typedef TwoLevelByteMap<(kNumRegions >> 12 ), 1 << 12 > ByteMap;
49
- typedef CompactSizeClassMap SizeClassMap;
50
- typedef SizeClassAllocator32<0 , SANITIZER_MMAP_RANGE_SIZE,
51
- sizeof (ChunkMetadata), SizeClassMap, kRegionSizeLog , ByteMap>
52
- PrimaryAllocator;
53
31
#else
54
32
static const uptr kMaxAllowedMallocSize = 8UL << 30 ;
55
-
56
- struct 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
-
65
- typedef SizeClassAllocator64<AP64> PrimaryAllocator;
66
33
#endif
67
- typedef SizeClassAllocatorLocalCache<PrimaryAllocator> AllocatorCache;
68
34
typedef LargeMmapAllocator<> SecondaryAllocator;
69
35
typedef CombinedAllocator<PrimaryAllocator, AllocatorCache,
70
36
SecondaryAllocator> Allocator;
71
37
72
38
static Allocator allocator;
73
- static THREADLOCAL AllocatorCache cache;
74
39
75
40
void InitializeAllocator () {
76
41
allocator.InitLinkerInitialized (
@@ -79,7 +44,7 @@ void InitializeAllocator() {
79
44
}
80
45
81
46
void AllocatorThreadFinish () {
82
- allocator.SwallowCache (&cache );
47
+ allocator.SwallowCache (GetAllocatorCache () );
83
48
}
84
49
85
50
static ChunkMetadata *Metadata (const void *p) {
@@ -111,7 +76,7 @@ void *Allocate(const StackTrace &stack, uptr size, uptr alignment,
111
76
Report (" WARNING: LeakSanitizer failed to allocate %zu bytes\n " , size);
112
77
return nullptr ;
113
78
}
114
- void *p = allocator.Allocate (&cache , size, alignment, false );
79
+ void *p = allocator.Allocate (GetAllocatorCache () , size, alignment, false );
115
80
// Do not rely on the allocator to clear the memory (it's slow).
116
81
if (cleared && allocator.FromPrimary (p))
117
82
memset (p, 0 , size);
@@ -125,25 +90,25 @@ void Deallocate(void *p) {
125
90
if (&__sanitizer_free_hook) __sanitizer_free_hook (p);
126
91
RunFreeHooks (p);
127
92
RegisterDeallocation (p);
128
- allocator.Deallocate (&cache , p);
93
+ allocator.Deallocate (GetAllocatorCache () , p);
129
94
}
130
95
131
96
void *Reallocate (const StackTrace &stack, void *p, uptr new_size,
132
97
uptr alignment) {
133
98
RegisterDeallocation (p);
134
99
if (new_size > kMaxAllowedMallocSize ) {
135
100
Report (" WARNING: LeakSanitizer failed to allocate %zu bytes\n " , new_size);
136
- allocator.Deallocate (&cache , p);
101
+ allocator.Deallocate (GetAllocatorCache () , p);
137
102
return nullptr ;
138
103
}
139
- p = allocator.Reallocate (&cache , p, new_size, alignment);
104
+ p = allocator.Reallocate (GetAllocatorCache () , p, new_size, alignment);
140
105
RegisterAllocation (stack, p, new_size);
141
106
return p;
142
107
}
143
108
144
109
void GetAllocatorCacheRange (uptr *begin, uptr *end) {
145
- *begin = (uptr)&cache ;
146
- *end = *begin + sizeof (cache );
110
+ *begin = (uptr)GetAllocatorCache () ;
111
+ *end = *begin + sizeof (AllocatorCache );
147
112
}
148
113
149
114
uptr GetMallocUsableSize (const void *p) {
0 commit comments