1 //===-- allocator_config.h --------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef SCUDO_ALLOCATOR_CONFIG_H_
10 #define SCUDO_ALLOCATOR_CONFIG_H_
14 #include "condition_variable.h"
16 #include "primary32.h"
17 #include "primary64.h"
18 #include "secondary.h"
19 #include "size_class_map.h"
20 #include "tsd_exclusive.h"
21 #include "tsd_shared.h"
23 // To import a custom configuration, define `SCUDO_USE_CUSTOM_CONFIG` and
24 // aliasing the `Config` like:
27 // // The instance of Scudo will be initiated with `Config`.
28 // typedef CustomConfig Config;
29 // // Aliasing as default configuration to run the tests with this config.
30 // typedef CustomConfig DefaultConfig;
31 // } // namespace scudo
33 // Put them in the header `custom_scudo_config.h` then you will be using the
34 // custom configuration and able to run all the tests as well.
35 #ifdef SCUDO_USE_CUSTOM_CONFIG
36 #include "custom_scudo_config.h"
41 // The combined allocator uses a structure as a template argument that
42 // specifies the configuration options for the various subcomponents of the
45 // struct ExampleConfig {
46 // // Indicates possible support for Memory Tagging.
47 // static const bool MaySupportMemoryTagging = false;
49 // // Thread-Specific Data Registry used, shared or exclusive.
50 // template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>;
53 // // SizeClassMap to use with the Primary.
54 // using SizeClassMap = DefaultSizeClassMap;
56 // // Log2 of the size of a size class region, as used by the Primary.
57 // static const uptr RegionSizeLog = 30U;
59 // // Log2 of the size of block group, as used by the Primary. Each group
60 // // contains a range of memory addresses, blocks in the range will belong
61 // // to the same group. In general, single region may have 1 or 2MB group
62 // // size. Multiple regions will have the group size equal to the region
63 // // size because the region size is usually smaller than 1 MB.
64 // // Smaller value gives fine-grained control of memory usage but the
65 // // trade-off is that it may take longer time of deallocation.
66 // static const uptr GroupSizeLog = 20U;
68 // // Defines the type and scale of a compact pointer. A compact pointer can
69 // // be understood as the offset of a pointer within the region it belongs
70 // // to, in increments of a power-of-2 scale.
71 // // eg: Ptr = Base + (CompactPtr << Scale).
72 // typedef u32 CompactPtrT;
73 // static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
75 // // Indicates support for offsetting the start of a region by
76 // // a random number of pages. Only used with primary64.
77 // static const bool EnableRandomOffset = true;
79 // // Call map for user memory with at least this size. Only used with
81 // static const uptr MapSizeIncrement = 1UL << 18;
83 // // Defines the minimal & maximal release interval that can be set.
84 // static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
85 // static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
87 // // Use condition variable to shorten the waiting time of refillment of
88 // // freelist. Note that this depends on the implementation of condition
89 // // variable on each platform and the performance may vary so that it
90 // // doesn't guarantee a performance benefit.
91 // // Note that both variables have to be defined to enable it.
92 // static const bool UseConditionVariable = true;
93 // using ConditionVariableT = ConditionVariableLinux;
95 // // Defines the type of Primary allocator to use.
96 // template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
98 // // Defines the type of cache used by the Secondary. Some additional
99 // // configuration entries can be necessary depending on the Cache.
100 // struct Secondary {
102 // static const u32 EntriesArraySize = 32U;
103 // static const u32 QuarantineSize = 0U;
104 // static const u32 DefaultMaxEntriesCount = 32U;
105 // static const uptr DefaultMaxEntrySize = 1UL << 19;
106 // static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
107 // static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
109 // // Defines the type of Secondary Cache to use.
110 // template <typename Config> using CacheT = MapAllocatorCache<Config>;
112 // // Defines the type of Secondary allocator to use.
113 // template <typename Config> using SecondaryT = MapAllocator<Config>;
116 #ifndef SCUDO_USE_CUSTOM_CONFIG
118 // Default configurations for various platforms. Note this is only enabled when
119 // there's no custom configuration in the build system.
120 struct DefaultConfig
{
121 static const bool MaySupportMemoryTagging
= true;
122 template <class A
> using TSDRegistryT
= TSDRegistryExT
<A
>; // Exclusive
125 using SizeClassMap
= DefaultSizeClassMap
;
126 #if SCUDO_CAN_USE_PRIMARY64
127 static const uptr RegionSizeLog
= 32U;
128 static const uptr GroupSizeLog
= 21U;
129 typedef uptr CompactPtrT
;
130 static const uptr CompactPtrScale
= 0;
131 static const bool EnableRandomOffset
= true;
132 static const uptr MapSizeIncrement
= 1UL << 18;
134 static const uptr RegionSizeLog
= 19U;
135 static const uptr GroupSizeLog
= 19U;
136 typedef uptr CompactPtrT
;
138 static const s32 MinReleaseToOsIntervalMs
= INT32_MIN
;
139 static const s32 MaxReleaseToOsIntervalMs
= INT32_MAX
;
141 #if SCUDO_CAN_USE_PRIMARY64
142 template <typename Config
> using PrimaryT
= SizeClassAllocator64
<Config
>;
144 template <typename Config
> using PrimaryT
= SizeClassAllocator32
<Config
>;
149 static const u32 EntriesArraySize
= 32U;
150 static const u32 QuarantineSize
= 0U;
151 static const u32 DefaultMaxEntriesCount
= 32U;
152 static const uptr DefaultMaxEntrySize
= 1UL << 19;
153 static const s32 MinReleaseToOsIntervalMs
= INT32_MIN
;
154 static const s32 MaxReleaseToOsIntervalMs
= INT32_MAX
;
156 template <typename Config
> using CacheT
= MapAllocatorCache
<Config
>;
159 template <typename Config
> using SecondaryT
= MapAllocator
<Config
>;
162 #endif // SCUDO_USE_CUSTOM_CONFIG
164 struct AndroidConfig
{
165 static const bool MaySupportMemoryTagging
= true;
167 using TSDRegistryT
= TSDRegistrySharedT
<A
, 8U, 2U>; // Shared, max 8 TSDs.
170 using SizeClassMap
= AndroidSizeClassMap
;
171 #if SCUDO_CAN_USE_PRIMARY64
172 static const uptr RegionSizeLog
= 28U;
173 typedef u32 CompactPtrT
;
174 static const uptr CompactPtrScale
= SCUDO_MIN_ALIGNMENT_LOG
;
175 static const uptr GroupSizeLog
= 20U;
176 static const bool EnableRandomOffset
= true;
177 static const uptr MapSizeIncrement
= 1UL << 18;
179 static const uptr RegionSizeLog
= 18U;
180 static const uptr GroupSizeLog
= 18U;
181 typedef uptr CompactPtrT
;
183 static const s32 MinReleaseToOsIntervalMs
= 1000;
184 static const s32 MaxReleaseToOsIntervalMs
= 1000;
186 #if SCUDO_CAN_USE_PRIMARY64
187 template <typename Config
> using PrimaryT
= SizeClassAllocator64
<Config
>;
189 template <typename Config
> using PrimaryT
= SizeClassAllocator32
<Config
>;
194 static const u32 EntriesArraySize
= 256U;
195 static const u32 QuarantineSize
= 32U;
196 static const u32 DefaultMaxEntriesCount
= 32U;
197 static const uptr DefaultMaxEntrySize
= 2UL << 20;
198 static const s32 MinReleaseToOsIntervalMs
= 0;
199 static const s32 MaxReleaseToOsIntervalMs
= 1000;
201 template <typename Config
> using CacheT
= MapAllocatorCache
<Config
>;
204 template <typename Config
> using SecondaryT
= MapAllocator
<Config
>;
207 #if SCUDO_CAN_USE_PRIMARY64
208 struct FuchsiaConfig
{
209 static const bool MaySupportMemoryTagging
= false;
211 using TSDRegistryT
= TSDRegistrySharedT
<A
, 8U, 4U>; // Shared, max 8 TSDs.
214 using SizeClassMap
= FuchsiaSizeClassMap
;
216 // Support 39-bit VMA for riscv-64
217 static const uptr RegionSizeLog
= 28U;
218 static const uptr GroupSizeLog
= 19U;
220 static const uptr RegionSizeLog
= 30U;
221 static const uptr GroupSizeLog
= 21U;
223 typedef u32 CompactPtrT
;
224 static const bool EnableRandomOffset
= true;
225 static const uptr MapSizeIncrement
= 1UL << 18;
226 static const uptr CompactPtrScale
= SCUDO_MIN_ALIGNMENT_LOG
;
227 static const s32 MinReleaseToOsIntervalMs
= INT32_MIN
;
228 static const s32 MaxReleaseToOsIntervalMs
= INT32_MAX
;
230 template <typename Config
> using PrimaryT
= SizeClassAllocator64
<Config
>;
233 template <typename Config
> using CacheT
= MapAllocatorNoCache
<Config
>;
235 template <typename Config
> using SecondaryT
= MapAllocator
<Config
>;
238 struct TrustyConfig
{
239 static const bool MaySupportMemoryTagging
= true;
241 using TSDRegistryT
= TSDRegistrySharedT
<A
, 1U, 1U>; // Shared, max 1 TSD.
244 using SizeClassMap
= TrustySizeClassMap
;
245 static const uptr RegionSizeLog
= 28U;
246 static const uptr GroupSizeLog
= 20U;
247 typedef u32 CompactPtrT
;
248 static const bool EnableRandomOffset
= false;
249 static const uptr MapSizeIncrement
= 1UL << 12;
250 static const uptr CompactPtrScale
= SCUDO_MIN_ALIGNMENT_LOG
;
251 static const s32 MinReleaseToOsIntervalMs
= INT32_MIN
;
252 static const s32 MaxReleaseToOsIntervalMs
= INT32_MAX
;
254 template <typename Config
> using PrimaryT
= SizeClassAllocator64
<Config
>;
257 template <typename Config
> using CacheT
= MapAllocatorNoCache
<Config
>;
260 template <typename Config
> using SecondaryT
= MapAllocator
<Config
>;
264 #ifndef SCUDO_USE_CUSTOM_CONFIG
267 typedef AndroidConfig Config
;
269 typedef FuchsiaConfig Config
;
271 typedef TrustyConfig Config
;
273 typedef DefaultConfig Config
;
276 #endif // SCUDO_USE_CUSTOM_CONFIG
280 #endif // SCUDO_ALLOCATOR_CONFIG_H_