aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang14-rt/lib/scudo/standalone/allocator_config.h
blob: e6f46b511dbfacb8de3261e5e577d2e8603d3643 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//===-- allocator_config.h --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef SCUDO_ALLOCATOR_CONFIG_H_
#define SCUDO_ALLOCATOR_CONFIG_H_

#include "combined.h"
#include "common.h"
#include "flags.h"
#include "primary32.h"
#include "primary64.h"
#include "secondary.h"
#include "size_class_map.h"
#include "tsd_exclusive.h"
#include "tsd_shared.h"

namespace scudo {

// The combined allocator uses a structure as a template argument that
// specifies the configuration options for the various subcomponents of the
// allocator.
//
// struct ExampleConfig {
//   // SizeClasMmap to use with the Primary.
//   using SizeClassMap = DefaultSizeClassMap;
//   // Indicates possible support for Memory Tagging.
//   static const bool MaySupportMemoryTagging = false;
//   // Defines the Primary allocator to use.
//   typedef SizeClassAllocator64<ExampleConfig> Primary;
//   // Log2 of the size of a size class region, as used by the Primary.
//   static const uptr PrimaryRegionSizeLog = 30U;
//   // Defines the type and scale of a compact pointer. A compact pointer can
//   // be understood as the offset of a pointer within the region it belongs
//   // to, in increments of a power-of-2 scale.
//   // eg: Ptr = Base + (CompactPtr << Scale).
//   typedef u32 PrimaryCompactPtrT;
//   static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
//   // Indicates support for offsetting the start of a region by
//   // a random number of pages. Only used with primary64.
//   static const bool PrimaryEnableRandomOffset = true;
//   // Call map for user memory with at least this size. Only used with
//   // primary64.
//   static const uptr PrimaryMapSizeIncrement = 1UL << 18;
//   // Defines the minimal & maximal release interval that can be set.
//   static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
//   static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
//   // Defines the type of cache used by the Secondary. Some additional
//   // configuration entries can be necessary depending on the Cache.
//   typedef MapAllocatorNoCache SecondaryCache;
//   // Thread-Specific Data Registry used, shared or exclusive.
//   template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>;
// };

// Default configurations for various platforms.

struct DefaultConfig {
  using SizeClassMap = DefaultSizeClassMap;
  static const bool MaySupportMemoryTagging = true;

#if SCUDO_CAN_USE_PRIMARY64
  typedef SizeClassAllocator64<DefaultConfig> Primary;
  static const uptr PrimaryRegionSizeLog = 32U;
  typedef uptr PrimaryCompactPtrT;
  static const uptr PrimaryCompactPtrScale = 0;
  static const bool PrimaryEnableRandomOffset = true;
  static const uptr PrimaryMapSizeIncrement = 1UL << 18;
#else
  typedef SizeClassAllocator32<DefaultConfig> Primary;
  static const uptr PrimaryRegionSizeLog = 19U;
  typedef uptr PrimaryCompactPtrT;
#endif
  static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;

  typedef MapAllocatorCache<DefaultConfig> SecondaryCache;
  static const u32 SecondaryCacheEntriesArraySize = 32U;
  static const u32 SecondaryCacheQuarantineSize = 0U;
  static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
  static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 19;
  static const s32 SecondaryCacheMinReleaseToOsIntervalMs = INT32_MIN;
  static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = INT32_MAX;

  template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive
};
struct AndroidConfig {
  using SizeClassMap = AndroidSizeClassMap;
  static const bool MaySupportMemoryTagging = true;

#if SCUDO_CAN_USE_PRIMARY64
  typedef SizeClassAllocator64<AndroidConfig> Primary;
  static const uptr PrimaryRegionSizeLog = 28U;
  typedef u32 PrimaryCompactPtrT;
  static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  static const bool PrimaryEnableRandomOffset = true;
  static const uptr PrimaryMapSizeIncrement = 1UL << 18;
#else
  typedef SizeClassAllocator32<AndroidConfig> Primary;
  static const uptr PrimaryRegionSizeLog = 18U;
  typedef uptr PrimaryCompactPtrT;
#endif
  static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
  static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;

  typedef MapAllocatorCache<AndroidConfig> SecondaryCache;
  static const u32 SecondaryCacheEntriesArraySize = 256U;
  static const u32 SecondaryCacheQuarantineSize = 32U;
  static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
  static const uptr SecondaryCacheDefaultMaxEntrySize = 2UL << 20;
  static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
  static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 1000;

  template <class A>
  using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
};

struct AndroidSvelteConfig {
  using SizeClassMap = SvelteSizeClassMap;
  static const bool MaySupportMemoryTagging = false;

#if SCUDO_CAN_USE_PRIMARY64
  typedef SizeClassAllocator64<AndroidSvelteConfig> Primary;
  static const uptr PrimaryRegionSizeLog = 27U;
  typedef u32 PrimaryCompactPtrT;
  static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  static const bool PrimaryEnableRandomOffset = true;
  static const uptr PrimaryMapSizeIncrement = 1UL << 18;
#else
  typedef SizeClassAllocator32<AndroidSvelteConfig> Primary;
  static const uptr PrimaryRegionSizeLog = 16U;
  typedef uptr PrimaryCompactPtrT;
#endif
  static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
  static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;

  typedef MapAllocatorCache<AndroidSvelteConfig> SecondaryCache;
  static const u32 SecondaryCacheEntriesArraySize = 16U;
  static const u32 SecondaryCacheQuarantineSize = 32U;
  static const u32 SecondaryCacheDefaultMaxEntriesCount = 4U;
  static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 18;
  static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
  static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 0;

  template <class A>
  using TSDRegistryT = TSDRegistrySharedT<A, 2U, 1U>; // Shared, max 2 TSDs.
};

#if SCUDO_CAN_USE_PRIMARY64
struct FuchsiaConfig {
  using SizeClassMap = FuchsiaSizeClassMap;
  static const bool MaySupportMemoryTagging = false;

  typedef SizeClassAllocator64<FuchsiaConfig> Primary;
  static const uptr PrimaryRegionSizeLog = 30U;
  typedef u32 PrimaryCompactPtrT;
  static const bool PrimaryEnableRandomOffset = true;
  static const uptr PrimaryMapSizeIncrement = 1UL << 18;
  static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;

  typedef MapAllocatorNoCache SecondaryCache;
  template <class A>
  using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>; // Shared, max 8 TSDs.
};

struct TrustyConfig {
  using SizeClassMap = TrustySizeClassMap;
  static const bool MaySupportMemoryTagging = false;

  typedef SizeClassAllocator64<TrustyConfig> Primary;
  // Some apps have 1 page of heap total so small regions are necessary.
  static const uptr PrimaryRegionSizeLog = 10U;
  typedef u32 PrimaryCompactPtrT;
  static const bool PrimaryEnableRandomOffset = false;
  // Trusty is extremely memory-constrained so minimally round up map calls.
  static const uptr PrimaryMapSizeIncrement = 1UL << 4;
  static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
  static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
  static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;

  typedef MapAllocatorNoCache SecondaryCache;
  template <class A>
  using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; // Shared, max 1 TSD.
};
#endif

#if SCUDO_ANDROID
typedef AndroidConfig Config;
#elif SCUDO_FUCHSIA
typedef FuchsiaConfig Config;
#elif SCUDO_TRUSTY
typedef TrustyConfig Config;
#else
typedef DefaultConfig Config;
#endif

} // namespace scudo

#endif // SCUDO_ALLOCATOR_CONFIG_H_