xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/scudo/standalone/allocator_config.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- allocator_config.h --------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef SCUDO_ALLOCATOR_CONFIG_H_
100b57cec5SDimitry Andric #define SCUDO_ALLOCATOR_CONFIG_H_
110b57cec5SDimitry Andric 
120b57cec5SDimitry Andric #include "combined.h"
130b57cec5SDimitry Andric #include "common.h"
145f757f3fSDimitry Andric #include "condition_variable.h"
150b57cec5SDimitry Andric #include "flags.h"
160b57cec5SDimitry Andric #include "primary32.h"
170b57cec5SDimitry Andric #include "primary64.h"
18480093f4SDimitry Andric #include "secondary.h"
190b57cec5SDimitry Andric #include "size_class_map.h"
200b57cec5SDimitry Andric #include "tsd_exclusive.h"
210b57cec5SDimitry Andric #include "tsd_shared.h"
220b57cec5SDimitry Andric 
2306c3fb27SDimitry Andric // To import a custom configuration, define `SCUDO_USE_CUSTOM_CONFIG` and
2406c3fb27SDimitry Andric // aliasing the `Config` like:
2506c3fb27SDimitry Andric //
2606c3fb27SDimitry Andric // namespace scudo {
2706c3fb27SDimitry Andric //   // The instance of Scudo will be initiated with `Config`.
2806c3fb27SDimitry Andric //   typedef CustomConfig Config;
2906c3fb27SDimitry Andric //   // Aliasing as default configuration to run the tests with this config.
3006c3fb27SDimitry Andric //   typedef CustomConfig DefaultConfig;
3106c3fb27SDimitry Andric // } // namespace scudo
3206c3fb27SDimitry Andric //
3306c3fb27SDimitry Andric // Put them in the header `custom_scudo_config.h` then you will be using the
3406c3fb27SDimitry Andric // custom configuration and able to run all the tests as well.
3506c3fb27SDimitry Andric #ifdef SCUDO_USE_CUSTOM_CONFIG
3606c3fb27SDimitry Andric #include "custom_scudo_config.h"
3706c3fb27SDimitry Andric #endif
3806c3fb27SDimitry Andric 
390b57cec5SDimitry Andric namespace scudo {
400b57cec5SDimitry Andric 
41*0fca6ea1SDimitry Andric // Scudo uses a structure as a template argument that specifies the
42*0fca6ea1SDimitry Andric // configuration options for the various subcomponents of the allocator. See the
43*0fca6ea1SDimitry Andric // following configs as examples and check `allocator_config.def` for all the
44*0fca6ea1SDimitry Andric // available options.
45fe6060f1SDimitry Andric 
4606c3fb27SDimitry Andric #ifndef SCUDO_USE_CUSTOM_CONFIG
470b57cec5SDimitry Andric 
4806c3fb27SDimitry Andric // Default configurations for various platforms. Note this is only enabled when
4906c3fb27SDimitry Andric // there's no custom configuration in the build system.
500b57cec5SDimitry Andric struct DefaultConfig {
51fe6060f1SDimitry Andric   static const bool MaySupportMemoryTagging = true;
520b57cec5SDimitry Andric   template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive
53e8d8bef9SDimitry Andric 
5406c3fb27SDimitry Andric   struct Primary {
5506c3fb27SDimitry Andric     using SizeClassMap = DefaultSizeClassMap;
560b57cec5SDimitry Andric #if SCUDO_CAN_USE_PRIMARY64
5706c3fb27SDimitry Andric     static const uptr RegionSizeLog = 32U;
5806c3fb27SDimitry Andric     static const uptr GroupSizeLog = 21U;
5906c3fb27SDimitry Andric     typedef uptr CompactPtrT;
6006c3fb27SDimitry Andric     static const uptr CompactPtrScale = 0;
6106c3fb27SDimitry Andric     static const bool EnableRandomOffset = true;
6206c3fb27SDimitry Andric     static const uptr MapSizeIncrement = 1UL << 18;
630b57cec5SDimitry Andric #else
6406c3fb27SDimitry Andric     static const uptr RegionSizeLog = 19U;
6506c3fb27SDimitry Andric     static const uptr GroupSizeLog = 19U;
6606c3fb27SDimitry Andric     typedef uptr CompactPtrT;
670b57cec5SDimitry Andric #endif
6806c3fb27SDimitry Andric     static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
6906c3fb27SDimitry Andric     static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
7006c3fb27SDimitry Andric   };
7106c3fb27SDimitry Andric #if SCUDO_CAN_USE_PRIMARY64
7206c3fb27SDimitry Andric   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
7306c3fb27SDimitry Andric #else
7406c3fb27SDimitry Andric   template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
7506c3fb27SDimitry Andric #endif
76e8d8bef9SDimitry Andric 
7706c3fb27SDimitry Andric   struct Secondary {
7806c3fb27SDimitry Andric     struct Cache {
7906c3fb27SDimitry Andric       static const u32 EntriesArraySize = 32U;
8006c3fb27SDimitry Andric       static const u32 QuarantineSize = 0U;
8106c3fb27SDimitry Andric       static const u32 DefaultMaxEntriesCount = 32U;
8206c3fb27SDimitry Andric       static const uptr DefaultMaxEntrySize = 1UL << 19;
8306c3fb27SDimitry Andric       static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
8406c3fb27SDimitry Andric       static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
8506c3fb27SDimitry Andric     };
8606c3fb27SDimitry Andric     template <typename Config> using CacheT = MapAllocatorCache<Config>;
8706c3fb27SDimitry Andric   };
88e8d8bef9SDimitry Andric 
8906c3fb27SDimitry Andric   template <typename Config> using SecondaryT = MapAllocator<Config>;
9006c3fb27SDimitry Andric };
9106c3fb27SDimitry Andric 
9206c3fb27SDimitry Andric #endif // SCUDO_USE_CUSTOM_CONFIG
9306c3fb27SDimitry Andric 
9406c3fb27SDimitry Andric struct AndroidConfig {
9506c3fb27SDimitry Andric   static const bool MaySupportMemoryTagging = true;
960b57cec5SDimitry Andric   template <class A>
97e8d8bef9SDimitry Andric   using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
9806c3fb27SDimitry Andric 
9906c3fb27SDimitry Andric   struct Primary {
10006c3fb27SDimitry Andric     using SizeClassMap = AndroidSizeClassMap;
10106c3fb27SDimitry Andric #if SCUDO_CAN_USE_PRIMARY64
10206c3fb27SDimitry Andric     static const uptr RegionSizeLog = 28U;
10306c3fb27SDimitry Andric     typedef u32 CompactPtrT;
10406c3fb27SDimitry Andric     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
10506c3fb27SDimitry Andric     static const uptr GroupSizeLog = 20U;
10606c3fb27SDimitry Andric     static const bool EnableRandomOffset = true;
10706c3fb27SDimitry Andric     static const uptr MapSizeIncrement = 1UL << 18;
10806c3fb27SDimitry Andric #else
10906c3fb27SDimitry Andric     static const uptr RegionSizeLog = 18U;
11006c3fb27SDimitry Andric     static const uptr GroupSizeLog = 18U;
11106c3fb27SDimitry Andric     typedef uptr CompactPtrT;
11206c3fb27SDimitry Andric #endif
11306c3fb27SDimitry Andric     static const s32 MinReleaseToOsIntervalMs = 1000;
11406c3fb27SDimitry Andric     static const s32 MaxReleaseToOsIntervalMs = 1000;
11506c3fb27SDimitry Andric   };
11606c3fb27SDimitry Andric #if SCUDO_CAN_USE_PRIMARY64
11706c3fb27SDimitry Andric   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
11806c3fb27SDimitry Andric #else
11906c3fb27SDimitry Andric   template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
12006c3fb27SDimitry Andric #endif
12106c3fb27SDimitry Andric 
12206c3fb27SDimitry Andric   struct Secondary {
12306c3fb27SDimitry Andric     struct Cache {
12406c3fb27SDimitry Andric       static const u32 EntriesArraySize = 256U;
12506c3fb27SDimitry Andric       static const u32 QuarantineSize = 32U;
12606c3fb27SDimitry Andric       static const u32 DefaultMaxEntriesCount = 32U;
12706c3fb27SDimitry Andric       static const uptr DefaultMaxEntrySize = 2UL << 20;
12806c3fb27SDimitry Andric       static const s32 MinReleaseToOsIntervalMs = 0;
12906c3fb27SDimitry Andric       static const s32 MaxReleaseToOsIntervalMs = 1000;
13006c3fb27SDimitry Andric     };
13106c3fb27SDimitry Andric     template <typename Config> using CacheT = MapAllocatorCache<Config>;
13206c3fb27SDimitry Andric   };
13306c3fb27SDimitry Andric 
13406c3fb27SDimitry Andric   template <typename Config> using SecondaryT = MapAllocator<Config>;
1350b57cec5SDimitry Andric };
1360b57cec5SDimitry Andric 
137480093f4SDimitry Andric #if SCUDO_CAN_USE_PRIMARY64
1380b57cec5SDimitry Andric struct FuchsiaConfig {
139e8d8bef9SDimitry Andric   static const bool MaySupportMemoryTagging = false;
1400b57cec5SDimitry Andric   template <class A>
141e8d8bef9SDimitry Andric   using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>; // Shared, max 8 TSDs.
14206c3fb27SDimitry Andric 
14306c3fb27SDimitry Andric   struct Primary {
14406c3fb27SDimitry Andric     using SizeClassMap = FuchsiaSizeClassMap;
14506c3fb27SDimitry Andric #if SCUDO_RISCV64
14606c3fb27SDimitry Andric     // Support 39-bit VMA for riscv-64
14706c3fb27SDimitry Andric     static const uptr RegionSizeLog = 28U;
14806c3fb27SDimitry Andric     static const uptr GroupSizeLog = 19U;
149*0fca6ea1SDimitry Andric     static const bool EnableContiguousRegions = false;
15006c3fb27SDimitry Andric #else
15106c3fb27SDimitry Andric     static const uptr RegionSizeLog = 30U;
15206c3fb27SDimitry Andric     static const uptr GroupSizeLog = 21U;
15306c3fb27SDimitry Andric #endif
15406c3fb27SDimitry Andric     typedef u32 CompactPtrT;
15506c3fb27SDimitry Andric     static const bool EnableRandomOffset = true;
15606c3fb27SDimitry Andric     static const uptr MapSizeIncrement = 1UL << 18;
15706c3fb27SDimitry Andric     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
15806c3fb27SDimitry Andric     static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
15906c3fb27SDimitry Andric     static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
16006c3fb27SDimitry Andric   };
16106c3fb27SDimitry Andric   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
16206c3fb27SDimitry Andric 
16306c3fb27SDimitry Andric   struct Secondary {
16406c3fb27SDimitry Andric     template <typename Config> using CacheT = MapAllocatorNoCache<Config>;
16506c3fb27SDimitry Andric   };
16606c3fb27SDimitry Andric   template <typename Config> using SecondaryT = MapAllocator<Config>;
1670b57cec5SDimitry Andric };
168fe6060f1SDimitry Andric 
169fe6060f1SDimitry Andric struct TrustyConfig {
17006c3fb27SDimitry Andric   static const bool MaySupportMemoryTagging = true;
171fe6060f1SDimitry Andric   template <class A>
172fe6060f1SDimitry Andric   using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; // Shared, max 1 TSD.
17306c3fb27SDimitry Andric 
17406c3fb27SDimitry Andric   struct Primary {
17506c3fb27SDimitry Andric     using SizeClassMap = TrustySizeClassMap;
17606c3fb27SDimitry Andric     static const uptr RegionSizeLog = 28U;
17706c3fb27SDimitry Andric     static const uptr GroupSizeLog = 20U;
17806c3fb27SDimitry Andric     typedef u32 CompactPtrT;
17906c3fb27SDimitry Andric     static const bool EnableRandomOffset = false;
18006c3fb27SDimitry Andric     static const uptr MapSizeIncrement = 1UL << 12;
18106c3fb27SDimitry Andric     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
18206c3fb27SDimitry Andric     static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
18306c3fb27SDimitry Andric     static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
18406c3fb27SDimitry Andric   };
18506c3fb27SDimitry Andric   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
18606c3fb27SDimitry Andric 
18706c3fb27SDimitry Andric   struct Secondary {
18806c3fb27SDimitry Andric     template <typename Config> using CacheT = MapAllocatorNoCache<Config>;
18906c3fb27SDimitry Andric   };
19006c3fb27SDimitry Andric 
19106c3fb27SDimitry Andric   template <typename Config> using SecondaryT = MapAllocator<Config>;
192fe6060f1SDimitry Andric };
193480093f4SDimitry Andric #endif
1940b57cec5SDimitry Andric 
19506c3fb27SDimitry Andric #ifndef SCUDO_USE_CUSTOM_CONFIG
19606c3fb27SDimitry Andric 
1970b57cec5SDimitry Andric #if SCUDO_ANDROID
1980b57cec5SDimitry Andric typedef AndroidConfig Config;
1990b57cec5SDimitry Andric #elif SCUDO_FUCHSIA
2000b57cec5SDimitry Andric typedef FuchsiaConfig Config;
201fe6060f1SDimitry Andric #elif SCUDO_TRUSTY
202fe6060f1SDimitry Andric typedef TrustyConfig Config;
2030b57cec5SDimitry Andric #else
2040b57cec5SDimitry Andric typedef DefaultConfig Config;
2050b57cec5SDimitry Andric #endif
2060b57cec5SDimitry Andric 
20706c3fb27SDimitry Andric #endif // SCUDO_USE_CUSTOM_CONFIG
20806c3fb27SDimitry Andric 
2090b57cec5SDimitry Andric } // namespace scudo
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric #endif // SCUDO_ALLOCATOR_CONFIG_H_
212