xref: /aosp_15_r20/external/scudo/config/custom_scudo_config.h (revision 76559068c068bd27e82aff38fac3bfc865233bca)
1*76559068SAndroid Build Coastguard Worker /*
2*76559068SAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*76559068SAndroid Build Coastguard Worker  * All rights reserved.
4*76559068SAndroid Build Coastguard Worker  *
5*76559068SAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*76559068SAndroid Build Coastguard Worker  * modification, are permitted provided that the following conditions
7*76559068SAndroid Build Coastguard Worker  * are met:
8*76559068SAndroid Build Coastguard Worker  *  * Redistributions of source code must retain the above copyright
9*76559068SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer.
10*76559068SAndroid Build Coastguard Worker  *  * Redistributions in binary form must reproduce the above copyright
11*76559068SAndroid Build Coastguard Worker  *    notice, this list of conditions and the following disclaimer in
12*76559068SAndroid Build Coastguard Worker  *    the documentation and/or other materials provided with the
13*76559068SAndroid Build Coastguard Worker  *    distribution.
14*76559068SAndroid Build Coastguard Worker  *
15*76559068SAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16*76559068SAndroid Build Coastguard Worker  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17*76559068SAndroid Build Coastguard Worker  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18*76559068SAndroid Build Coastguard Worker  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19*76559068SAndroid Build Coastguard Worker  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20*76559068SAndroid Build Coastguard Worker  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21*76559068SAndroid Build Coastguard Worker  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22*76559068SAndroid Build Coastguard Worker  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23*76559068SAndroid Build Coastguard Worker  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24*76559068SAndroid Build Coastguard Worker  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25*76559068SAndroid Build Coastguard Worker  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*76559068SAndroid Build Coastguard Worker  * SUCH DAMAGE.
27*76559068SAndroid Build Coastguard Worker  */
28*76559068SAndroid Build Coastguard Worker 
29*76559068SAndroid Build Coastguard Worker //===-- custom_scudo-config.h -----------------------------------*- C++ -*-===//
30*76559068SAndroid Build Coastguard Worker //
31*76559068SAndroid Build Coastguard Worker // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
32*76559068SAndroid Build Coastguard Worker // See https://llvm.org/LICENSE.txt for license information.
33*76559068SAndroid Build Coastguard Worker // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
34*76559068SAndroid Build Coastguard Worker //
35*76559068SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
36*76559068SAndroid Build Coastguard Worker 
37*76559068SAndroid Build Coastguard Worker #pragma once
38*76559068SAndroid Build Coastguard Worker 
39*76559068SAndroid Build Coastguard Worker // Use a custom config instead of the config found in allocator_config.h
40*76559068SAndroid Build Coastguard Worker namespace scudo {
41*76559068SAndroid Build Coastguard Worker 
42*76559068SAndroid Build Coastguard Worker struct AndroidNormalSizeClassConfig {
43*76559068SAndroid Build Coastguard Worker #if SCUDO_WORDSIZE == 64U
44*76559068SAndroid Build Coastguard Worker   static const uptr NumBits = 7;
45*76559068SAndroid Build Coastguard Worker   static const uptr MinSizeLog = 4;
46*76559068SAndroid Build Coastguard Worker   static const uptr MidSizeLog = 6;
47*76559068SAndroid Build Coastguard Worker   static const uptr MaxSizeLog = 16;
48*76559068SAndroid Build Coastguard Worker   static const u16 MaxNumCachedHint = 13;
49*76559068SAndroid Build Coastguard Worker   static const uptr MaxBytesCachedLog = 13;
50*76559068SAndroid Build Coastguard Worker 
51*76559068SAndroid Build Coastguard Worker   static constexpr uptr Classes[] = {
52*76559068SAndroid Build Coastguard Worker       0x00020, 0x00030, 0x00040, 0x00050, 0x00060, 0x00070, 0x00090, 0x000b0,
53*76559068SAndroid Build Coastguard Worker       0x000c0, 0x000e0, 0x00120, 0x00160, 0x001c0, 0x00250, 0x00320, 0x00450,
54*76559068SAndroid Build Coastguard Worker       0x00670, 0x00830, 0x00a10, 0x00c30, 0x01010, 0x01210, 0x01bd0, 0x02210,
55*76559068SAndroid Build Coastguard Worker       0x02d90, 0x03790, 0x04010, 0x04810, 0x05a10, 0x07310, 0x08210, 0x10010,
56*76559068SAndroid Build Coastguard Worker   };
57*76559068SAndroid Build Coastguard Worker   static const uptr SizeDelta = 16;
58*76559068SAndroid Build Coastguard Worker #else
59*76559068SAndroid Build Coastguard Worker   static const uptr NumBits = 8;
60*76559068SAndroid Build Coastguard Worker   static const uptr MinSizeLog = 4;
61*76559068SAndroid Build Coastguard Worker   static const uptr MidSizeLog = 7;
62*76559068SAndroid Build Coastguard Worker   static const uptr MaxSizeLog = 16;
63*76559068SAndroid Build Coastguard Worker   static const u16 MaxNumCachedHint = 14;
64*76559068SAndroid Build Coastguard Worker   static const uptr MaxBytesCachedLog = 13;
65*76559068SAndroid Build Coastguard Worker 
66*76559068SAndroid Build Coastguard Worker   static constexpr uptr Classes[] = {
67*76559068SAndroid Build Coastguard Worker       0x00020, 0x00030, 0x00040, 0x00050, 0x00060, 0x00070, 0x00080, 0x00090,
68*76559068SAndroid Build Coastguard Worker       0x000a0, 0x000b0, 0x000c0, 0x000e0, 0x000f0, 0x00110, 0x00120, 0x00130,
69*76559068SAndroid Build Coastguard Worker       0x00150, 0x00160, 0x00170, 0x00190, 0x001d0, 0x00210, 0x00240, 0x002a0,
70*76559068SAndroid Build Coastguard Worker       0x00330, 0x00370, 0x003a0, 0x00400, 0x00430, 0x004a0, 0x00530, 0x00610,
71*76559068SAndroid Build Coastguard Worker       0x00730, 0x00840, 0x00910, 0x009c0, 0x00a60, 0x00b10, 0x00ca0, 0x00e00,
72*76559068SAndroid Build Coastguard Worker       0x00fb0, 0x01030, 0x01130, 0x011f0, 0x01490, 0x01650, 0x01930, 0x02010,
73*76559068SAndroid Build Coastguard Worker       0x02190, 0x02490, 0x02850, 0x02d50, 0x03010, 0x03210, 0x03c90, 0x04090,
74*76559068SAndroid Build Coastguard Worker       0x04510, 0x04810, 0x05c10, 0x06f10, 0x07310, 0x08010, 0x0c010, 0x10010,
75*76559068SAndroid Build Coastguard Worker   };
76*76559068SAndroid Build Coastguard Worker   static const uptr SizeDelta = 16;
77*76559068SAndroid Build Coastguard Worker #endif
78*76559068SAndroid Build Coastguard Worker };
79*76559068SAndroid Build Coastguard Worker 
80*76559068SAndroid Build Coastguard Worker typedef TableSizeClassMap<AndroidNormalSizeClassConfig>
81*76559068SAndroid Build Coastguard Worker     AndroidNormalSizeClassMap;
82*76559068SAndroid Build Coastguard Worker 
83*76559068SAndroid Build Coastguard Worker #if defined(__LP64__)
84*76559068SAndroid Build Coastguard Worker static_assert(AndroidNormalSizeClassMap::usesCompressedLSBFormat(), "");
85*76559068SAndroid Build Coastguard Worker #endif
86*76559068SAndroid Build Coastguard Worker 
87*76559068SAndroid Build Coastguard Worker struct HostConfig {
88*76559068SAndroid Build Coastguard Worker   static const bool MaySupportMemoryTagging = false;
89*76559068SAndroid Build Coastguard Worker 
90*76559068SAndroid Build Coastguard Worker   template <class A> using TSDRegistryT = TSDRegistryExT<A>; // Exclusive TSD
91*76559068SAndroid Build Coastguard Worker 
92*76559068SAndroid Build Coastguard Worker   struct Primary {
93*76559068SAndroid Build Coastguard Worker     using SizeClassMap = AndroidNormalSizeClassMap;
94*76559068SAndroid Build Coastguard Worker #if SCUDO_CAN_USE_PRIMARY64
95*76559068SAndroid Build Coastguard Worker     static const uptr RegionSizeLog = 30U;
96*76559068SAndroid Build Coastguard Worker     typedef u32 CompactPtrT;
97*76559068SAndroid Build Coastguard Worker     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
98*76559068SAndroid Build Coastguard Worker     static const uptr GroupSizeLog = 26U;
99*76559068SAndroid Build Coastguard Worker     static const bool EnableRandomOffset = false;
100*76559068SAndroid Build Coastguard Worker     static const uptr MapSizeIncrement = 1UL << 18;
101*76559068SAndroid Build Coastguard Worker #else
102*76559068SAndroid Build Coastguard Worker     static const uptr RegionSizeLog = 18U;
103*76559068SAndroid Build Coastguard Worker     static const uptr GroupSizeLog = 18U;
104*76559068SAndroid Build Coastguard Worker     typedef uptr CompactPtrT;
105*76559068SAndroid Build Coastguard Worker #endif
106*76559068SAndroid Build Coastguard Worker     static const s32 MinReleaseToOsIntervalMs = -1;
107*76559068SAndroid Build Coastguard Worker     static const s32 MaxReleaseToOsIntervalMs = 10000;
108*76559068SAndroid Build Coastguard Worker     static const s32 DefaultReleaseToOsIntervalMs = 10000;
109*76559068SAndroid Build Coastguard Worker   };
110*76559068SAndroid Build Coastguard Worker #if SCUDO_CAN_USE_PRIMARY64
111*76559068SAndroid Build Coastguard Worker   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
112*76559068SAndroid Build Coastguard Worker #else
113*76559068SAndroid Build Coastguard Worker   template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
114*76559068SAndroid Build Coastguard Worker #endif
115*76559068SAndroid Build Coastguard Worker 
116*76559068SAndroid Build Coastguard Worker   struct Secondary {
117*76559068SAndroid Build Coastguard Worker     struct Cache {
118*76559068SAndroid Build Coastguard Worker       static const u32 EntriesArraySize = 1024U;
119*76559068SAndroid Build Coastguard Worker       static const u32 QuarantineSize = 32U;
120*76559068SAndroid Build Coastguard Worker       static const u32 DefaultMaxEntriesCount = 1024U;
121*76559068SAndroid Build Coastguard Worker       static const uptr DefaultMaxEntrySize = 1UL << 30;
122*76559068SAndroid Build Coastguard Worker       static const s32 MinReleaseToOsIntervalMs = -1;
123*76559068SAndroid Build Coastguard Worker       static const s32 MaxReleaseToOsIntervalMs = 10000;
124*76559068SAndroid Build Coastguard Worker       static const s32 DefaultReleaseToOsIntervalMs = 10000;
125*76559068SAndroid Build Coastguard Worker     };
126*76559068SAndroid Build Coastguard Worker     template <typename Config> using CacheT = MapAllocatorCache<Config>;
127*76559068SAndroid Build Coastguard Worker   };
128*76559068SAndroid Build Coastguard Worker 
129*76559068SAndroid Build Coastguard Worker   template <typename Config> using SecondaryT = MapAllocator<Config>;
130*76559068SAndroid Build Coastguard Worker };
131*76559068SAndroid Build Coastguard Worker 
132*76559068SAndroid Build Coastguard Worker struct AndroidNormalConfig {
133*76559068SAndroid Build Coastguard Worker #if defined(__aarch64__)
134*76559068SAndroid Build Coastguard Worker   static const bool MaySupportMemoryTagging = true;
135*76559068SAndroid Build Coastguard Worker #else
136*76559068SAndroid Build Coastguard Worker   static const bool MaySupportMemoryTagging = false;
137*76559068SAndroid Build Coastguard Worker #endif
138*76559068SAndroid Build Coastguard Worker   template <class A>
139*76559068SAndroid Build Coastguard Worker   using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs.
140*76559068SAndroid Build Coastguard Worker 
141*76559068SAndroid Build Coastguard Worker   struct Primary {
142*76559068SAndroid Build Coastguard Worker     using SizeClassMap = AndroidNormalSizeClassMap;
143*76559068SAndroid Build Coastguard Worker #if SCUDO_CAN_USE_PRIMARY64
144*76559068SAndroid Build Coastguard Worker     static const uptr RegionSizeLog = 28U;
145*76559068SAndroid Build Coastguard Worker     typedef u32 CompactPtrT;
146*76559068SAndroid Build Coastguard Worker     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
147*76559068SAndroid Build Coastguard Worker     static const uptr GroupSizeLog = 20U;
148*76559068SAndroid Build Coastguard Worker     static const bool EnableRandomOffset = true;
149*76559068SAndroid Build Coastguard Worker     static const uptr MapSizeIncrement = 1UL << 18;
150*76559068SAndroid Build Coastguard Worker #else
151*76559068SAndroid Build Coastguard Worker     static const uptr RegionSizeLog = 18U;
152*76559068SAndroid Build Coastguard Worker     static const uptr GroupSizeLog = 18U;
153*76559068SAndroid Build Coastguard Worker     typedef uptr CompactPtrT;
154*76559068SAndroid Build Coastguard Worker #endif
155*76559068SAndroid Build Coastguard Worker     static const s32 MinReleaseToOsIntervalMs = -1;
156*76559068SAndroid Build Coastguard Worker     static const s32 MaxReleaseToOsIntervalMs = 1000;
157*76559068SAndroid Build Coastguard Worker     static const s32 DefaultReleaseToOsIntervalMs = 1000;
158*76559068SAndroid Build Coastguard Worker   };
159*76559068SAndroid Build Coastguard Worker #if SCUDO_CAN_USE_PRIMARY64
160*76559068SAndroid Build Coastguard Worker   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
161*76559068SAndroid Build Coastguard Worker #else
162*76559068SAndroid Build Coastguard Worker   template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
163*76559068SAndroid Build Coastguard Worker #endif
164*76559068SAndroid Build Coastguard Worker 
165*76559068SAndroid Build Coastguard Worker   struct Secondary {
166*76559068SAndroid Build Coastguard Worker     struct Cache {
167*76559068SAndroid Build Coastguard Worker       static const u32 EntriesArraySize = 256U;
168*76559068SAndroid Build Coastguard Worker       static const u32 QuarantineSize = 32U;
169*76559068SAndroid Build Coastguard Worker       static const u32 DefaultMaxEntriesCount = 32U;
170*76559068SAndroid Build Coastguard Worker       static const uptr DefaultMaxEntrySize = 2UL << 20;
171*76559068SAndroid Build Coastguard Worker       static const s32 MinReleaseToOsIntervalMs = -1;
172*76559068SAndroid Build Coastguard Worker       static const s32 MaxReleaseToOsIntervalMs = 1000;
173*76559068SAndroid Build Coastguard Worker       static const s32 DefaultReleaseToOsIntervalMs = 0;
174*76559068SAndroid Build Coastguard Worker     };
175*76559068SAndroid Build Coastguard Worker     template <typename Config> using CacheT = MapAllocatorCache<Config>;
176*76559068SAndroid Build Coastguard Worker   };
177*76559068SAndroid Build Coastguard Worker 
178*76559068SAndroid Build Coastguard Worker   template <typename Config> using SecondaryT = MapAllocator<Config>;
179*76559068SAndroid Build Coastguard Worker };
180*76559068SAndroid Build Coastguard Worker 
181*76559068SAndroid Build Coastguard Worker struct AndroidLowMemoryConfig {
182*76559068SAndroid Build Coastguard Worker #if defined(__aarch64__)
183*76559068SAndroid Build Coastguard Worker   static const bool MaySupportMemoryTagging = true;
184*76559068SAndroid Build Coastguard Worker #else
185*76559068SAndroid Build Coastguard Worker   static const bool MaySupportMemoryTagging = false;
186*76559068SAndroid Build Coastguard Worker #endif
187*76559068SAndroid Build Coastguard Worker   template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>;
188*76559068SAndroid Build Coastguard Worker 
189*76559068SAndroid Build Coastguard Worker   struct Primary {
190*76559068SAndroid Build Coastguard Worker     // Use the same size class map as the normal config.
191*76559068SAndroid Build Coastguard Worker     using SizeClassMap = AndroidNormalSizeClassMap;
192*76559068SAndroid Build Coastguard Worker #if SCUDO_CAN_USE_PRIMARY64
193*76559068SAndroid Build Coastguard Worker     static const uptr RegionSizeLog = 28U;
194*76559068SAndroid Build Coastguard Worker     typedef u32 CompactPtrT;
195*76559068SAndroid Build Coastguard Worker     static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
196*76559068SAndroid Build Coastguard Worker     static const uptr GroupSizeLog = 18U;
197*76559068SAndroid Build Coastguard Worker     static const bool EnableRandomOffset = true;
198*76559068SAndroid Build Coastguard Worker     static const uptr MapSizeIncrement = 1UL << 18;
199*76559068SAndroid Build Coastguard Worker #else
200*76559068SAndroid Build Coastguard Worker     static const uptr RegionSizeLog = 20U;
201*76559068SAndroid Build Coastguard Worker     static const uptr GroupSizeLog = 20U;
202*76559068SAndroid Build Coastguard Worker     typedef uptr CompactPtrT;
203*76559068SAndroid Build Coastguard Worker #endif
204*76559068SAndroid Build Coastguard Worker     static const s32 MinReleaseToOsIntervalMs = 100;
205*76559068SAndroid Build Coastguard Worker     static const s32 MaxReleaseToOsIntervalMs = 1000;
206*76559068SAndroid Build Coastguard Worker   };
207*76559068SAndroid Build Coastguard Worker #if SCUDO_CAN_USE_PRIMARY64
208*76559068SAndroid Build Coastguard Worker   template <typename Config> using PrimaryT = SizeClassAllocator64<Config>;
209*76559068SAndroid Build Coastguard Worker #else
210*76559068SAndroid Build Coastguard Worker   template <typename Config> using PrimaryT = SizeClassAllocator32<Config>;
211*76559068SAndroid Build Coastguard Worker #endif
212*76559068SAndroid Build Coastguard Worker 
213*76559068SAndroid Build Coastguard Worker   struct Secondary {
214*76559068SAndroid Build Coastguard Worker     // TODO(cferris): After secondary caching tuned, re-add a cache config.
215*76559068SAndroid Build Coastguard Worker     template <typename Config> using CacheT = MapAllocatorNoCache<Config>;
216*76559068SAndroid Build Coastguard Worker   };
217*76559068SAndroid Build Coastguard Worker 
218*76559068SAndroid Build Coastguard Worker   template <typename Config> using SecondaryT = MapAllocator<Config>;
219*76559068SAndroid Build Coastguard Worker };
220*76559068SAndroid Build Coastguard Worker 
221*76559068SAndroid Build Coastguard Worker #if defined(__ANDROID__)
222*76559068SAndroid Build Coastguard Worker 
223*76559068SAndroid Build Coastguard Worker #include <unistd.h>
224*76559068SAndroid Build Coastguard Worker 
225*76559068SAndroid Build Coastguard Worker #if defined(PAGE_SIZE)
226*76559068SAndroid Build Coastguard Worker // This is to guarantee that the getPageSizeCached() function is constexpr.
227*76559068SAndroid Build Coastguard Worker static_assert(getPageSizeCached() != 0, "getPageSizeCached() is zero");
228*76559068SAndroid Build Coastguard Worker #endif
229*76559068SAndroid Build Coastguard Worker 
230*76559068SAndroid Build Coastguard Worker #if defined(SCUDO_LOW_MEMORY)
231*76559068SAndroid Build Coastguard Worker typedef AndroidLowMemoryConfig Config;
232*76559068SAndroid Build Coastguard Worker #else
233*76559068SAndroid Build Coastguard Worker typedef AndroidNormalConfig Config;
234*76559068SAndroid Build Coastguard Worker #endif
235*76559068SAndroid Build Coastguard Worker 
236*76559068SAndroid Build Coastguard Worker #else
237*76559068SAndroid Build Coastguard Worker 
238*76559068SAndroid Build Coastguard Worker typedef HostConfig Config;
239*76559068SAndroid Build Coastguard Worker 
240*76559068SAndroid Build Coastguard Worker #endif
241*76559068SAndroid Build Coastguard Worker 
242*76559068SAndroid Build Coastguard Worker typedef Config DefaultConfig;
243*76559068SAndroid Build Coastguard Worker 
244*76559068SAndroid Build Coastguard Worker } // namespace scudo
245