xref: /aosp_15_r20/external/cronet/base/allocator/partition_alloc_features.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_ALLOCATOR_PARTITION_ALLOC_FEATURES_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOC_FEATURES_H_
7 
8 #include "base/base_export.h"
9 #include "base/compiler_specific.h"
10 #include "base/feature_list.h"
11 #include "base/metrics/field_trial_params.h"
12 #include "base/strings/string_piece.h"
13 #include "base/time/time.h"
14 #include "build/build_config.h"
15 #include "partition_alloc/partition_alloc_base/time/time.h"
16 #include "partition_alloc/partition_alloc_buildflags.h"
17 #include "partition_alloc/partition_root.h"
18 
19 namespace base {
20 namespace features {
21 
22 extern const BASE_EXPORT Feature kPartitionAllocUnretainedDanglingPtr;
23 enum class UnretainedDanglingPtrMode {
24   kCrash,
25   kDumpWithoutCrashing,
26 };
27 extern const BASE_EXPORT base::FeatureParam<UnretainedDanglingPtrMode>
28     kUnretainedDanglingPtrModeParam;
29 
30 // See /docs/dangling_ptr.md
31 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocDanglingPtr);
32 enum class DanglingPtrMode {
33   // Crash immediately after detecting a dangling raw_ptr.
34   kCrash,  // (default)
35 
36   // Log the signature of every occurrences without crashing. It is used by
37   // bots.
38   // Format "[DanglingSignature]\t<1>\t<2>\t<3>\t<4>"
39   // 1. The function which freed the memory while it was still referenced.
40   // 2. The task in which the memory was freed.
41   // 3. The function which released the raw_ptr reference.
42   // 4. The task in which the raw_ptr was released.
43   kLogOnly,
44 
45   // Note: This will be extended with a single shot DumpWithoutCrashing.
46 };
47 extern const BASE_EXPORT base::FeatureParam<DanglingPtrMode>
48     kDanglingPtrModeParam;
49 enum class DanglingPtrType {
50   // Act on any dangling raw_ptr released after being freed.
51   kAll,  // (default)
52 
53   // Detect when freeing memory and releasing the dangling raw_ptr happens in
54   // a different task. Those are more likely to cause use after free.
55   kCrossTask,
56 
57   // Note: This will be extended with LongLived
58 };
59 extern const BASE_EXPORT base::FeatureParam<DanglingPtrType>
60     kDanglingPtrTypeParam;
61 
62 #if BUILDFLAG(USE_STARSCAN)
63 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPCScan);
64 #endif
65 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
66 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPCScanBrowserOnly);
67 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPCScanRendererOnly);
68 
69 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocLargeThreadCacheSize);
70 BASE_EXPORT int GetPartitionAllocLargeThreadCacheSizeValue();
71 BASE_EXPORT int GetPartitionAllocLargeThreadCacheSizeValueForLowRAMAndroid();
72 
73 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocLargeEmptySlotSpanRing);
74 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocSchedulerLoopQuarantine);
75 // Scheduler Loop Quarantine's capacity in bytes.
76 extern const BASE_EXPORT base::FeatureParam<int>
77     kPartitionAllocSchedulerLoopQuarantineCapacity;
78 
79 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocZappingByFreeFlags);
80 #endif  // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
81 
82 enum class BackupRefPtrEnabledProcesses {
83   // BRP enabled only in the browser process.
84   kBrowserOnly,
85   // BRP enabled only in the browser and renderer processes.
86   kBrowserAndRenderer,
87   // BRP enabled in all processes, except renderer.
88   kNonRenderer,
89   // BRP enabled in all processes.
90   kAllProcesses,
91 };
92 
93 enum class BackupRefPtrMode {
94   // BRP is disabled across all partitions. Equivalent to the Finch flag being
95   // disabled.
96   kDisabled,
97 
98   // BRP is enabled in the main partition, as well as certain Renderer-only
99   // partitions (if enabled in Renderer at all).
100   kEnabled,
101 };
102 
103 enum class MemtagMode {
104   // memtagMode will be SYNC.
105   kSync,
106   // memtagMode will be ASYNC.
107   kAsync,
108 };
109 
110 enum class MemoryTaggingEnabledProcesses {
111   // Memory tagging enabled only in the browser process.
112   kBrowserOnly,
113   // Memory tagging enabled in all processes, except renderer.
114   kNonRenderer,
115   // Memory tagging enabled in all processes.
116   kAllProcesses,
117 };
118 
119 enum class BucketDistributionMode : uint8_t {
120   kDefault,
121   kDenser,
122 };
123 
124 // Parameter for 'kPartitionAllocMakeFreeNoOpOnShutdown' feature which
125 // controls when free() becomes a no-op during Shutdown()
126 enum class WhenFreeBecomesNoOp {
127   // Allocator is inserted either before, in, or after shutdown threads
128   kBeforeShutDownThreads,
129   kInShutDownThreads,
130   kAfterShutDownThreads,
131 };
132 
133 // Inserts a no-op on 'free()' allocator shim at the front of the
134 // dispatch chain if called from the appropriate callsite.
135 BASE_EXPORT void MakeFreeNoOp(WhenFreeBecomesNoOp callsite);
136 
137 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocMakeFreeNoOpOnShutdown);
138 extern const BASE_EXPORT base::FeatureParam<WhenFreeBecomesNoOp>
139     kPartitionAllocMakeFreeNoOpOnShutdownParam;
140 
141 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocBackupRefPtr);
142 extern const BASE_EXPORT base::FeatureParam<BackupRefPtrEnabledProcesses>
143     kBackupRefPtrEnabledProcessesParam;
144 extern const BASE_EXPORT base::FeatureParam<BackupRefPtrMode>
145     kBackupRefPtrModeParam;
146 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocMemoryTagging);
147 extern const BASE_EXPORT base::FeatureParam<MemtagMode> kMemtagModeParam;
148 extern const BASE_EXPORT base::FeatureParam<MemoryTaggingEnabledProcesses>
149     kMemoryTaggingEnabledProcessesParam;
150 // Kill switch for memory tagging. Skips any code related to memory tagging when
151 // enabled.
152 BASE_EXPORT BASE_DECLARE_FEATURE(kKillPartitionAllocMemoryTagging);
153 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPermissiveMte);
154 extern const BASE_EXPORT base::FeatureParam<bool>
155     kBackupRefPtrAsanEnableDereferenceCheckParam;
156 extern const BASE_EXPORT base::FeatureParam<bool>
157     kBackupRefPtrAsanEnableExtractionCheckParam;
158 extern const BASE_EXPORT base::FeatureParam<bool>
159     kBackupRefPtrAsanEnableInstantiationCheckParam;
160 extern const BASE_EXPORT base::FeatureParam<BucketDistributionMode>
161     kPartitionAllocBucketDistributionParam;
162 
163 BASE_EXPORT BASE_DECLARE_FEATURE(kLowerPAMemoryLimitForNonMainRenderers);
164 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPCScanMUAwareScheduler);
165 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPCScanStackScanning);
166 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocDCScan);
167 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPCScanImmediateFreeing);
168 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocPCScanEagerClearing);
169 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocUseDenserDistribution);
170 
171 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocMemoryReclaimer);
172 extern const BASE_EXPORT base::FeatureParam<TimeDelta>
173     kPartitionAllocMemoryReclaimerInterval;
174 BASE_EXPORT BASE_DECLARE_FEATURE(
175     kPartitionAllocStraightenLargerSlotSpanFreeLists);
176 extern const BASE_EXPORT
177     base::FeatureParam<partition_alloc::StraightenLargerSlotSpanFreeListsMode>
178         kPartitionAllocStraightenLargerSlotSpanFreeListsMode;
179 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocSortSmallerSlotSpanFreeLists);
180 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocSortActiveSlotSpans);
181 
182 #if BUILDFLAG(IS_WIN)
183 BASE_EXPORT BASE_DECLARE_FEATURE(kPageAllocatorRetryOnCommitFailure);
184 #endif
185 
186 #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
187 extern const base::FeatureParam<bool>
188     kPartialLowEndModeExcludePartitionAllocSupport;
189 #endif
190 
191 BASE_EXPORT BASE_DECLARE_FEATURE(kEnableConfigurableThreadCacheMultiplier);
192 BASE_EXPORT double GetThreadCacheMultiplier();
193 BASE_EXPORT double GetThreadCacheMultiplierForAndroid();
194 
195 BASE_EXPORT BASE_DECLARE_FEATURE(kEnableConfigurableThreadCachePurgeInterval);
196 extern const partition_alloc::internal::base::TimeDelta
197 GetThreadCacheMinPurgeInterval();
198 extern const partition_alloc::internal::base::TimeDelta
199 GetThreadCacheMaxPurgeInterval();
200 extern const partition_alloc::internal::base::TimeDelta
201 GetThreadCacheDefaultPurgeInterval();
202 
203 BASE_EXPORT BASE_DECLARE_FEATURE(
204     kEnableConfigurableThreadCacheMinCachedMemoryForPurging);
205 BASE_EXPORT int GetThreadCacheMinCachedMemoryForPurgingBytes();
206 
207 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocDisableBRPInBufferPartition);
208 
209 // This feature is additionally gated behind a buildflag because
210 // pool offset freelists cannot be represented when PartitionAlloc uses
211 // 32-bit pointers.
212 #if BUILDFLAG(USE_FREELIST_POOL_OFFSETS)
213 BASE_EXPORT BASE_DECLARE_FEATURE(kUsePoolOffsetFreelists);
214 #endif
215 
216 // When set, partitions use a larger ring buffer and free memory less
217 // aggressively when in the foreground.
218 BASE_EXPORT BASE_DECLARE_FEATURE(kPartitionAllocAdjustSizeWhenInForeground);
219 
220 }  // namespace features
221 }  // namespace base
222 
223 #endif  // BASE_ALLOCATOR_PARTITION_ALLOC_FEATURES_H_
224