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