blob: 8e103f28b1a7e2a3c3cd2799ddfeb2c1b17b4eb7 [file] [log] [blame]
Dynamic Tools Team517193e2019-09-11 14:48:41 +00001//===-- 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 "flags.h"
15#include "primary32.h"
16#include "primary64.h"
Dynamic Tools Teamd29271f2019-10-31 10:31:49 -070017#include "secondary.h"
Dynamic Tools Team517193e2019-09-11 14:48:41 +000018#include "size_class_map.h"
19#include "tsd_exclusive.h"
20#include "tsd_shared.h"
21
22namespace scudo {
23
Kostya Kortchinskyc9369542021-02-10 10:17:18 -080024// The combined allocator uses a structure as a template argument that
25// specifies the configuration options for the various subcomponents of the
26// allocator.
27//
28// struct ExampleConfig {
29// // SizeClasMmap to use with the Primary.
30// using SizeClassMap = DefaultSizeClassMap;
31// // Indicates possible support for Memory Tagging.
32// static const bool MaySupportMemoryTagging = false;
33// // Defines the Primary allocator to use.
34// typedef SizeClassAllocator64<ExampleConfig> Primary;
35// // Log2 of the size of a size class region, as used by the Primary.
36// static const uptr PrimaryRegionSizeLog = 30U;
37// // Defines the type and scale of a compact pointer. A compact pointer can
38// // be understood as the offset of a pointer within the region it belongs
39// // to, in increments of a power-of-2 scale.
40// // eg: Ptr = Base + (CompactPtr << Scale).
41// typedef u32 PrimaryCompactPtrT;
42// static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
43// // Defines the minimal & maximal release interval that can be set.
44// static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
45// static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
46// // Defines the type of cache used by the Secondary. Some additional
47// // configuration entries can be necessary depending on the Cache.
48// typedef MapAllocatorNoCache SecondaryCache;
49// // Thread-Specific Data Registry used, shared or exclusive.
50// template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>;
51// };
52
Dynamic Tools Team517193e2019-09-11 14:48:41 +000053// Default configurations for various platforms.
54
55struct DefaultConfig {
56 using SizeClassMap = DefaultSizeClassMap;
Peter Collingbourne6be49192020-12-15 14:26:10 -080057 static const bool MaySupportMemoryTagging = false;
58
Dynamic Tools Team517193e2019-09-11 14:48:41 +000059#if SCUDO_CAN_USE_PRIMARY64
Peter Collingbourne6be49192020-12-15 14:26:10 -080060 typedef SizeClassAllocator64<DefaultConfig> Primary;
Kostya Kortchinskyc9369542021-02-10 10:17:18 -080061 static const uptr PrimaryRegionSizeLog = 32U;
62 typedef uptr PrimaryCompactPtrT;
63 static const uptr PrimaryCompactPtrScale = 0;
Dynamic Tools Team517193e2019-09-11 14:48:41 +000064#else
Peter Collingbourne6be49192020-12-15 14:26:10 -080065 typedef SizeClassAllocator32<DefaultConfig> Primary;
66 static const uptr PrimaryRegionSizeLog = 19U;
Kostya Kortchinskyc9369542021-02-10 10:17:18 -080067 typedef uptr PrimaryCompactPtrT;
Dynamic Tools Team517193e2019-09-11 14:48:41 +000068#endif
Peter Collingbourne6be49192020-12-15 14:26:10 -080069 static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
70 static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
71
Peter Collingbourne7488a172020-12-14 13:57:59 -080072 typedef MapAllocatorCache<DefaultConfig> SecondaryCache;
73 static const u32 SecondaryCacheEntriesArraySize = 32U;
Peter Collingbourne3f9de812020-12-21 18:39:03 -080074 static const u32 SecondaryCacheQuarantineSize = 0U;
Peter Collingbourne7488a172020-12-14 13:57:59 -080075 static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
76 static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 19;
77 static const s32 SecondaryCacheMinReleaseToOsIntervalMs = INT32_MIN;
78 static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = INT32_MAX;
79
Dynamic Tools Team517193e2019-09-11 14:48:41 +000080 template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive
81};
82
83struct AndroidConfig {
84 using SizeClassMap = AndroidSizeClassMap;
Peter Collingbourne6be49192020-12-15 14:26:10 -080085 static const bool MaySupportMemoryTagging = true;
86
Dynamic Tools Team517193e2019-09-11 14:48:41 +000087#if SCUDO_CAN_USE_PRIMARY64
Peter Collingbourne6be49192020-12-15 14:26:10 -080088 typedef SizeClassAllocator64<AndroidConfig> Primary;
89 static const uptr PrimaryRegionSizeLog = 28U;
Kostya Kortchinskyc9369542021-02-10 10:17:18 -080090 typedef u32 PrimaryCompactPtrT;
91 static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
Dynamic Tools Team517193e2019-09-11 14:48:41 +000092#else
Peter Collingbourne6be49192020-12-15 14:26:10 -080093 typedef SizeClassAllocator32<AndroidConfig> Primary;
94 static const uptr PrimaryRegionSizeLog = 18U;
Kostya Kortchinskyc9369542021-02-10 10:17:18 -080095 typedef uptr PrimaryCompactPtrT;
Dynamic Tools Team517193e2019-09-11 14:48:41 +000096#endif
Peter Collingbourne6be49192020-12-15 14:26:10 -080097 static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
98 static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;
99
Peter Collingbourne7488a172020-12-14 13:57:59 -0800100 typedef MapAllocatorCache<AndroidConfig> SecondaryCache;
101 static const u32 SecondaryCacheEntriesArraySize = 256U;
Peter Collingbourne3f9de812020-12-21 18:39:03 -0800102 static const u32 SecondaryCacheQuarantineSize = 32U;
Peter Collingbourne7488a172020-12-14 13:57:59 -0800103 static const u32 SecondaryCacheDefaultMaxEntriesCount = 32U;
104 static const uptr SecondaryCacheDefaultMaxEntrySize = 2UL << 20;
105 static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
106 static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 1000;
107
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000108 template <class A>
Kostya Kortchinskyc72ca562020-07-27 09:13:42 -0700109 using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000110};
111
112struct AndroidSvelteConfig {
113 using SizeClassMap = SvelteSizeClassMap;
Peter Collingbourne6be49192020-12-15 14:26:10 -0800114 static const bool MaySupportMemoryTagging = false;
115
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000116#if SCUDO_CAN_USE_PRIMARY64
Peter Collingbourne6be49192020-12-15 14:26:10 -0800117 typedef SizeClassAllocator64<AndroidSvelteConfig> Primary;
118 static const uptr PrimaryRegionSizeLog = 27U;
Kostya Kortchinskyc9369542021-02-10 10:17:18 -0800119 typedef u32 PrimaryCompactPtrT;
120 static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000121#else
Peter Collingbourne6be49192020-12-15 14:26:10 -0800122 typedef SizeClassAllocator32<AndroidSvelteConfig> Primary;
123 static const uptr PrimaryRegionSizeLog = 16U;
Kostya Kortchinskyc9369542021-02-10 10:17:18 -0800124 typedef uptr PrimaryCompactPtrT;
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000125#endif
Peter Collingbourne6be49192020-12-15 14:26:10 -0800126 static const s32 PrimaryMinReleaseToOsIntervalMs = 1000;
127 static const s32 PrimaryMaxReleaseToOsIntervalMs = 1000;
128
Peter Collingbourne7488a172020-12-14 13:57:59 -0800129 typedef MapAllocatorCache<AndroidSvelteConfig> SecondaryCache;
130 static const u32 SecondaryCacheEntriesArraySize = 16U;
Peter Collingbourne3f9de812020-12-21 18:39:03 -0800131 static const u32 SecondaryCacheQuarantineSize = 32U;
Peter Collingbourne7488a172020-12-14 13:57:59 -0800132 static const u32 SecondaryCacheDefaultMaxEntriesCount = 4U;
133 static const uptr SecondaryCacheDefaultMaxEntrySize = 1UL << 18;
134 static const s32 SecondaryCacheMinReleaseToOsIntervalMs = 0;
135 static const s32 SecondaryCacheMaxReleaseToOsIntervalMs = 0;
136
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000137 template <class A>
Kostya Kortchinskyc72ca562020-07-27 09:13:42 -0700138 using TSDRegistryT = TSDRegistrySharedT<A, 2U, 1U>; // Shared, max 2 TSDs.
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000139};
140
Dynamic Tools Teamc9106952019-12-13 09:43:51 -0800141#if SCUDO_CAN_USE_PRIMARY64
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000142struct FuchsiaConfig {
Peter Collingbourne6be49192020-12-15 14:26:10 -0800143 using SizeClassMap = DefaultSizeClassMap;
144 static const bool MaySupportMemoryTagging = false;
145
146 typedef SizeClassAllocator64<FuchsiaConfig> Primary;
147 static const uptr PrimaryRegionSizeLog = 30U;
Kostya Kortchinskyc9369542021-02-10 10:17:18 -0800148 typedef u32 PrimaryCompactPtrT;
149 static const uptr PrimaryCompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
Peter Collingbourne6be49192020-12-15 14:26:10 -0800150 static const s32 PrimaryMinReleaseToOsIntervalMs = INT32_MIN;
151 static const s32 PrimaryMaxReleaseToOsIntervalMs = INT32_MAX;
152
Peter Collingbourne7488a172020-12-14 13:57:59 -0800153 typedef MapAllocatorNoCache SecondaryCache;
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000154 template <class A>
Kostya Kortchinskyc72ca562020-07-27 09:13:42 -0700155 using TSDRegistryT = TSDRegistrySharedT<A, 8U, 4U>; // Shared, max 8 TSDs.
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000156};
Dynamic Tools Teamc9106952019-12-13 09:43:51 -0800157#endif
Dynamic Tools Team517193e2019-09-11 14:48:41 +0000158
159#if SCUDO_ANDROID
160typedef AndroidConfig Config;
161#elif SCUDO_FUCHSIA
162typedef FuchsiaConfig Config;
163#else
164typedef DefaultConfig Config;
165#endif
166
167} // namespace scudo
168
169#endif // SCUDO_ALLOCATOR_CONFIG_H_