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 }; 128 129 template <typename Config> using SecondaryT = MapAllocator<Config>; 130 }; 131 132 struct AndroidNormalConfig { 133 #if defined(__aarch64__) 134 static const bool MaySupportMemoryTagging = true; 135 #else 136 static const bool MaySupportMemoryTagging = false; 137 #endif 138 template <class A> 139 using TSDRegistryT = TSDRegistrySharedT<A, 8U, 2U>; // Shared, max 8 TSDs. 140 141 struct Primary { 142 using SizeClassMap = AndroidNormalSizeClassMap; 143 #if SCUDO_CAN_USE_PRIMARY64 144 static const uptr RegionSizeLog = 28U; 145 typedef u32 CompactPtrT; 146 static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG; 147 static const uptr GroupSizeLog = 20U; 148 static const bool EnableRandomOffset = true; 149 static const uptr MapSizeIncrement = 1UL << 18; 150 #else 151 static const uptr RegionSizeLog = 18U; 152 static const uptr GroupSizeLog = 18U; 153 typedef uptr CompactPtrT; 154 #endif 155 static const s32 MinReleaseToOsIntervalMs = -1; 156 static const s32 MaxReleaseToOsIntervalMs = 1000; 157 static const s32 DefaultReleaseToOsIntervalMs = 1000; 158 }; 159 #if SCUDO_CAN_USE_PRIMARY64 160 template <typename Config> using PrimaryT = SizeClassAllocator64<Config>; 161 #else 162 template <typename Config> using PrimaryT = SizeClassAllocator32<Config>; 163 #endif 164 165 struct Secondary { 166 struct Cache { 167 static const u32 EntriesArraySize = 256U; 168 static const u32 QuarantineSize = 32U; 169 static const u32 DefaultMaxEntriesCount = 32U; 170 static const uptr DefaultMaxEntrySize = 2UL << 20; 171 static const s32 MinReleaseToOsIntervalMs = -1; 172 static const s32 MaxReleaseToOsIntervalMs = 1000; 173 static const s32 DefaultReleaseToOsIntervalMs = 0; 174 }; 175 template <typename Config> using CacheT = MapAllocatorCache<Config>; 176 }; 177 178 template <typename Config> using SecondaryT = MapAllocator<Config>; 179 }; 180 181 struct AndroidLowMemoryConfig { 182 #if defined(__aarch64__) 183 static const bool MaySupportMemoryTagging = true; 184 #else 185 static const bool MaySupportMemoryTagging = false; 186 #endif 187 template <class A> using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>; 188 189 struct Primary { 190 // Use the same size class map as the normal config. 191 using SizeClassMap = AndroidNormalSizeClassMap; 192 #if SCUDO_CAN_USE_PRIMARY64 193 static const uptr RegionSizeLog = 28U; 194 typedef u32 CompactPtrT; 195 static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG; 196 static const uptr GroupSizeLog = 18U; 197 static const bool EnableRandomOffset = true; 198 static const uptr MapSizeIncrement = 1UL << 18; 199 #else 200 static const uptr RegionSizeLog = 20U; 201 static const uptr GroupSizeLog = 20U; 202 typedef uptr CompactPtrT; 203 #endif 204 static const s32 MinReleaseToOsIntervalMs = 100; 205 static const s32 MaxReleaseToOsIntervalMs = 1000; 206 }; 207 #if SCUDO_CAN_USE_PRIMARY64 208 template <typename Config> using PrimaryT = SizeClassAllocator64<Config>; 209 #else 210 template <typename Config> using PrimaryT = SizeClassAllocator32<Config>; 211 #endif 212 213 struct Secondary { 214 // TODO(cferris): After secondary caching tuned, re-add a cache config. 215 template <typename Config> using CacheT = MapAllocatorNoCache<Config>; 216 }; 217 218 template <typename Config> using SecondaryT = MapAllocator<Config>; 219 }; 220 221 #if defined(__ANDROID__) 222 223 #include <unistd.h> 224 225 #if defined(PAGE_SIZE) 226 // This is to guarantee that the getPageSizeCached() function is constexpr. 227 static_assert(getPageSizeCached() != 0, "getPageSizeCached() is zero"); 228 #endif 229 230 #if defined(SCUDO_LOW_MEMORY) 231 typedef AndroidLowMemoryConfig Config; 232 #else 233 typedef AndroidNormalConfig Config; 234 #endif 235 236 #else 237 238 typedef HostConfig Config; 239 240 #endif 241 242 typedef Config DefaultConfig; 243 244 } // namespace scudo 245