1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "chunk.h"
20 #include "common.h"
21 #include "string_utils.h"
22 
23 namespace scudo {
24 
25 /*
26  * generated using the "compute_size_class_config_tool" based on observed
27  * allocations from the storage app, then manually tweaked to remove one
28  * size class that only had one allocation in it, and to lower the cache
29  * settings.
30  */
31 struct TrustyCustomSizeClassConfig {
32     static const uptr NumBits = 6;
33     static const uptr MinSizeLog = 5;
34     static const uptr MidSizeLog = 5;
35     static const uptr MaxSizeLog = 15;
36     static const u16 MaxNumCachedHint = 12;
37     static const uptr MaxBytesCachedLog = 10;
38 
39     static constexpr u32 Classes[] = {
40             0x00040, 0x00080, 0x00090, /*0x000b0,*/ 0x00150, 0x00490, 0x01090,
41     };
42     static const uptr SizeDelta = 16;
43 };
44 typedef TableSizeClassMap<TrustyCustomSizeClassConfig> TrustyCustomSizeClassMap;
45 
46 struct TrustyCustomConfig {
47     static const bool MaySupportMemoryTagging = true;
48     template <class A>
49     using TSDRegistryT = TSDRegistrySharedT<A, 1U, 1U>;  // Shared, max 1 TSD.
50 
51     struct Primary {
52         using SizeClassMap = TrustyCustomSizeClassMap;
53         static const uptr RegionSizeLog = 28U;
54         static const uptr GroupSizeLog = 20U;
55         typedef u32 CompactPtrT;
56         static const bool EnableRandomOffset = false;
57         static const uptr MapSizeIncrement = 1UL << 12;
58         static const uptr CompactPtrScale = SCUDO_MIN_ALIGNMENT_LOG;
59         static const s32 MinReleaseToOsIntervalMs = INT32_MIN;
60         static const s32 MaxReleaseToOsIntervalMs = INT32_MAX;
61     };
62     template <typename Config>
63     using PrimaryT = SizeClassAllocator64<Config>;
64 
65     struct Secondary {
66         template <typename Config>
67         using CacheT = MapAllocatorNoCache<Config>;
68     };
69 
70     template <typename Config>
71     using SecondaryT = MapAllocator<Config>;
72 };
73 
74 typedef TrustyCustomConfig Config;
75 typedef TrustyCustomConfig DefaultConfig;
76 
77 }  // namespace scudo
78