xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/allocator_config.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 //===-- allocator_config.h --------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SCUDO_ALLOCATOR_CONFIG_H_
10 #define SCUDO_ALLOCATOR_CONFIG_H_
11 
12 #include "combined.h"
13 #include "common.h"
14 #include "condition_variable.h"
15 #include "flags.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"
22 
23 // To import a custom configuration, define `SCUDO_USE_CUSTOM_CONFIG` and
24 // aliasing the `Config` like:
25 //
26 // namespace scudo {
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
32 //
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"
37 #endif
38 
39 namespace scudo {
40 
41 // Scudo uses a structure as a template argument that specifies the
42 // configuration options for the various subcomponents of the allocator. See the
43 // following configs as examples and check `allocator_config.def` for all the
44 // available options.
45 
46 #ifndef SCUDO_USE_CUSTOM_CONFIG
47 
48 // Default configurations for various platforms. Note this is only enabled when
49 // there's no custom configuration in the build system.
50 struct DefaultConfig {
51   static const bool MaySupportMemoryTagging = true;
52   template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive
53 
54   struct Primary {
55     using SizeClassMap = DefaultSizeClassMap;
56 #if SCUDO_CAN_USE_PRIMARY64
57     static const uptr RegionSizeLog = 32U;
58     static const uptr GroupSizeLog = 21U;
59     typedef uptr CompactPtrT;
60     static const uptr CompactPtrScale = 0;
61     static const bool EnableRandomOffset = true;
62     static const uptr MapSizeIncrement = 1UL << 18;
63 #else
64     static const uptr RegionSizeLog = 19U;
65     static const uptr GroupSizeLog = 19U;
66     typedef uptr CompactPtrT;
67 #endif
68     static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
69     static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
70   };
71 #if SCUDO_CAN_USE_PRIMARY64
72   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
73 #else
74   template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
75 #endif
76 
77   struct Secondary {
78     struct Cache {
79       static const u32 EntriesArraySize = 32U;
80       static const u32 QuarantineSize = 0U;
81       static const u32 DefaultMaxEntriesCount = 32U;
82       static const uptr DefaultMaxEntrySize = 1UL << 19;
83       static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
84       static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
85     };
86     template <typename Config> using CacheT = MapAllocatorCache<Config>;
87   };
88 
89   template <typename Config> using SecondaryT = MapAllocator<Config>;
90 };
91 
92 #endif // SCUDO_USE_CUSTOM_CONFIG
93 
94 struct AndroidConfig {
95   static const bool MaySupportMemoryTagging = true;
96   template <class A>
97   using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
98 
99   struct Primary {
100     using SizeClassMap = AndroidSizeClassMap;
101 #if SCUDO_CAN_USE_PRIMARY64
102     static const uptr RegionSizeLog = 28U;
103     typedef u32 CompactPtrT;
104     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
105     static const uptr GroupSizeLog = 20U;
106     static const bool EnableRandomOffset = true;
107     static const uptr MapSizeIncrement = 1UL << 18;
108 #else
109     static const uptr RegionSizeLog = 18U;
110     static const uptr GroupSizeLog = 18U;
111     typedef uptr CompactPtrT;
112 #endif
113     static const s32 MinReleaseToOsIntervalMs = 1000;
114     static const s32 MaxReleaseToOsIntervalMs = 1000;
115   };
116 #if SCUDO_CAN_USE_PRIMARY64
117   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
118 #else
119   template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
120 #endif
121 
122   struct Secondary {
123     struct Cache {
124       static const u32 EntriesArraySize = 256U;
125       static const u32 QuarantineSize = 32U;
126       static const u32 DefaultMaxEntriesCount = 32U;
127       static const uptr DefaultMaxEntrySize = 2UL << 20;
128       static const s32 MinReleaseToOsIntervalMs = 0;
129       static const s32 MaxReleaseToOsIntervalMs = 1000;
130     };
131     template <typename Config> using CacheT = MapAllocatorCache<Config>;
132   };
133 
134   template <typename Config> using SecondaryT = MapAllocator<Config>;
135 };
136 
137 #if SCUDO_CAN_USE_PRIMARY64
138 struct FuchsiaConfig {
139   static const bool MaySupportMemoryTagging = false;
140   template <class A>
141   using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>; // Shared, max 8 TSDs.
142 
143   struct Primary {
144     using SizeClassMap = FuchsiaSizeClassMap;
145 #if SCUDO_RISCV64
146     // Support 39-bit VMA for riscv-64
147     static const uptr RegionSizeLog = 28U;
148     static const uptr GroupSizeLog = 19U;
149     static const bool EnableContiguousRegions = false;
150 #else
151     static const uptr RegionSizeLog = 30U;
152     static const uptr GroupSizeLog = 21U;
153 #endif
154     typedef u32 CompactPtrT;
155     static const bool EnableRandomOffset = true;
156     static const uptr MapSizeIncrement = 1UL << 18;
157     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
158     static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
159     static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
160   };
161   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
162 
163   struct Secondary {
164     template <typename Config> using CacheT = MapAllocatorNoCache<Config>;
165   };
166   template <typename Config> using SecondaryT = MapAllocator<Config>;
167 };
168 
169 struct TrustyConfig {
170   static const bool MaySupportMemoryTagging = true;
171   template <class A>
172   using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; // Shared, max 1 TSD.
173 
174   struct Primary {
175     using SizeClassMap = TrustySizeClassMap;
176     static const uptr RegionSizeLog = 28U;
177     static const uptr GroupSizeLog = 20U;
178     typedef u32 CompactPtrT;
179     static const bool EnableRandomOffset = false;
180     static const uptr MapSizeIncrement = 1UL << 12;
181     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
182     static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
183     static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
184   };
185   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
186 
187   struct Secondary {
188     template <typename Config> using CacheT = MapAllocatorNoCache<Config>;
189   };
190 
191   template <typename Config> using SecondaryT = MapAllocator<Config>;
192 };
193 #endif
194 
195 #ifndef SCUDO_USE_CUSTOM_CONFIG
196 
197 #if SCUDO_ANDROID
198 typedef AndroidConfig Config;
199 #elif SCUDO_FUCHSIA
200 typedef FuchsiaConfig Config;
201 #elif SCUDO_TRUSTY
202 typedef TrustyConfig Config;
203 #else
204 typedef DefaultConfig Config;
205 #endif
206 
207 #endif // SCUDO_USE_CUSTOM_CONFIG
208 
209 } // namespace scudo
210 
211 #endif // SCUDO_ALLOCATOR_CONFIG_H_
212