• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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 #include "base/trace_event/memory_infra_background_allowlist.h"
6 
7 #include <string.h>
8 
9 #include <string>
10 
11 #include "base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h"
12 #include "base/containers/fixed_flat_set.h"
13 #include "base/strings/string_util.h"
14 #include "build/build_config.h"
15 #include "third_party/abseil-cpp/absl/strings/ascii.h"
16 
17 #if BUILDFLAG(IS_ANDROID)
18 #include "base/android/meminfo_dump_provider.h"
19 #endif
20 
21 namespace base {
22 namespace trace_event {
23 namespace {
24 
25 // The names of dump providers allowed to perform background tracing. Dump
26 // providers can be added here only if the background mode dump has very
27 // little processor and memory overhead.
28 // TODO(ssid): Some dump providers do not create ownership edges on background
29 // dump. So, the effective size will not be correct.
30 constexpr auto kDumpProviderAllowlist =
31     base::MakeFixedFlatSet<base::StringPiece>({
32 // clang-format off
33 #if BUILDFLAG(IS_ANDROID)
34         base::android::MeminfoDumpProvider::kDumpProviderName,
35         "android::ResourceManagerImpl",
36 #endif
37         "AutocompleteController",
38         "AXPlatformNode",
39         "BlinkGC",
40         "BlinkObjectCounters",
41         "BlobStorageContext",
42         "Canvas",
43         "ClientDiscardableSharedMemoryManager",
44 #if BUILDFLAG(IS_MAC)
45         "CommandBuffer",
46 #endif
47         "DOMStorage",
48         "DevTools",
49         "DiscardableSharedMemoryManager",
50         "DownloadService",
51         "ExtensionFunctions",
52         "FontCaches",
53         "FrameEvictionManager",
54         "GrShaderCache",
55         "HistoryReport",
56         "cc::ResourcePool",
57         "gpu::BufferManager",
58         "gpu::RenderbufferManager",
59         "gpu::ServiceDiscardableManager",
60         "gpu::ServiceTransferCache",
61         "gpu::SharedImageStub",
62         "gpu::TextureManager",
63         "hibernated_canvas",
64         "vulkan",
65         "IPCChannel",
66         "InMemoryURLIndex",
67         "IndexedDBBackingStore",
68         "IndexedDBFactoryImpl",
69         "JavaHeap",
70         "LevelDB",
71         "LeveldbValueStore",
72         "LocalStorage",
73         "MadvFreeDiscardableMemoryAllocator",
74         "Malloc",
75         "ManualFillingCache",
76         "MemoryCache",
77         "MojoHandleTable",
78         "MojoLevelDB",
79         "MojoMessages",
80         "PartitionAlloc",
81         "PartitionAlloc.AddressSpace",
82         "ProcessMemoryMetrics",
83         "SharedContextState",
84         "SharedImageManager",
85         "SharedMemoryTracker",
86         "Skia",
87         "Sql",
88         "TabRestoreServiceHelper",
89         "URLRequestContext",
90         "V8Isolate",
91         "WebMediaPlayer_MainThread",
92         "WebMediaPlayer_MediaThread",
93       // clang-format on
94     });
95 
96 // A list of string names that are allowed for the memory allocator dumps in
97 // background mode.
98 constexpr auto kAllocatorDumpNameAllowlist = base::MakeFixedFlatSet<
99     base::StringPiece>({
100 // clang-format off
101         // Some of the blink values vary based on compile time flags. The
102         // compile time flags are not in base, so all are listed here.
103 #if BUILDFLAG(IS_ANDROID)
104         base::android::MeminfoDumpProvider::kDumpName,
105 #endif
106         "accessibility/ax_platform_node",
107         "blink_gc/main/allocated_objects",
108         "blink_gc/main/heap",
109         "blink_gc/workers/heap/worker_0x?",
110         "blink_gc/workers/worker_0x?/heap",
111         "blink_gc/workers/worker_0x?/allocated_objects",
112         "blink_objects/AdSubframe",
113         "blink_objects/ArrayBufferContents",
114         "blink_objects/AudioHandler",
115         "blink_objects/AudioWorkletProcessor",
116         "blink_objects/ContextLifecycleStateObserver",
117         "blink_objects/DetachedScriptState",
118         "blink_objects/Document",
119         "blink_objects/Frame",
120         "blink_objects/JSEventListener",
121         "blink_objects/LayoutObject",
122         "blink_objects/MediaKeySession",
123         "blink_objects/MediaKeys",
124         "blink_objects/Node",
125         "blink_objects/Resource",
126         "blink_objects/RTCPeerConnection",
127         "blink_objects/ScriptPromise",
128         "blink_objects/V8PerContextData",
129         "blink_objects/WorkerGlobalScope",
130         "blink_objects/UACSSResource",
131         "blink_objects/ResourceFetcher",
132         "canvas/hibernated",
133         "canvas/ResourceProvider/SkSurface",
134         "canvas/ResourceProvider/SkSurface/0x?",
135         "cc/tile_memory/provider_0x?",
136         "components/download/controller_0x?",
137         "devtools/file_watcher_0x?",
138         "discardable",
139         "discardable/madv_free_allocated",
140         "discardable/child_0x?",
141         "extensions/functions",
142         "extensions/value_store/Extensions.Database.Open.OriginManagedConfiguration/0x?",
143         "extensions/value_store/Extensions.Database.Open.Rules/0x?",
144         "extensions/value_store/Extensions.Database.Open.Scripts/0x?",
145         "extensions/value_store/Extensions.Database.Open.Settings/0x?",
146         "extensions/value_store/Extensions.Database.Open.State/0x?",
147         "extensions/value_store/Extensions.Database.Open.WebAppsLockScreen/0x?",
148         "extensions/value_store/Extensions.Database.Open/0x?",
149         "extensions/value_store/Extensions.Database.Restore/0x?",
150         "extensions/value_store/Extensions.Database.Value.Restore/0x?",
151         "font_caches/font_platform_data_cache",
152         "font_caches/shape_caches",
153         "frame_evictor",
154         "gpu/discardable_cache/cache_0x?",
155         "gpu/discardable_cache/cache_0x?/avg_image_size",
156         "gpu/gl/buffers/context_group_0x?",
157         "gpu/gl/renderbuffers/context_group_0x?",
158         "gpu/gl/textures/context_group_0x?",
159         "gpu/gr_shader_cache/cache_0x?",
160         "gpu/shared_images",
161         "gpu/transfer_cache/cache_0x?",
162         "gpu/transfer_cache/cache_0x?/avg_image_size",
163         "gpu/vulkan/vma_allocator_0x?",
164         "history/delta_file_service/leveldb_0x?",
165         "history/usage_reports_buffer/leveldb_0x?",
166 #if BUILDFLAG(IS_MAC)
167         "ioaccelerator",
168         "iosurface",
169 #endif
170         "java_heap",
171         "java_heap/allocated_objects",
172         "leveldatabase",
173         "leveldatabase/block_cache/browser",
174         "leveldatabase/block_cache/in_memory",
175         "leveldatabase/block_cache/unified",
176         "leveldatabase/block_cache/web",
177         "leveldatabase/db_0x?",
178         "leveldatabase/db_0x?/block_cache",
179         "leveldatabase/memenv_0x?",
180         "malloc",
181         "malloc/allocated_objects",
182         "malloc/metadata_fragmentation_caches",
183 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
184         "malloc/partitions",
185         "malloc/partitions/allocator",
186         "malloc/partitions/allocator/thread_cache",
187         "malloc/partitions/allocator/thread_cache/main_thread",
188         "malloc/partitions/aligned",
189         "malloc/partitions/original",
190         "malloc/partitions/nonscannable",
191         "malloc/partitions/nonquarantinable",
192         "malloc/sys_malloc",
193         "malloc/win_heap",
194 #endif
195         "media/webmediaplayer/audio/player_0x?",
196         "media/webmediaplayer/data_source/player_0x?",
197         "media/webmediaplayer/demuxer/player_0x?",
198         "media/webmediaplayer/video/player_0x?",
199         "media/webmediaplayer/player_0x?",
200         "mojo",
201         "mojo/data_pipe_consumer",
202         "mojo/data_pipe_producer",
203         "mojo/invitation",
204         "mojo/messages",
205         "mojo/message_pipe",
206         "mojo/platform_handle",
207         "mojo/queued_ipc_channel_message/0x?",
208         "mojo/shared_buffer",
209         "mojo/unknown",
210         "mojo/watcher",
211         "omnibox/autocomplete_controller/0x?",
212         "omnibox/in_memory_url_index/0x?",
213         "passwords/manual_filling_controller/0x?",
214         "web_cache/Image_resources",
215         "web_cache/CSS stylesheet_resources",
216         "web_cache/Script_resources",
217         "web_cache/XSL stylesheet_resources",
218         "web_cache/Font_resources",
219         "web_cache/Code_cache",
220         "web_cache/Encoded_size_duplicated_in_data_urls",
221         "web_cache/Other_resources",
222         "partition_alloc/allocated_objects",
223         "partition_alloc/address_space",
224         "partition_alloc/partitions",
225         "partition_alloc/partitions/array_buffer",
226         "partition_alloc/partitions/buffer",
227         "partition_alloc/partitions/fast_malloc",
228 #if !BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
229         "partition_alloc/partitions/fast_malloc/thread_cache",
230         "partition_alloc/partitions/fast_malloc/thread_cache/main_thread",
231 #endif
232         "partition_alloc/partitions/layout",
233         "skia/gpu_resources/context_0x?",
234         "skia/sk_glyph_cache",
235         "skia/sk_resource_cache",
236         "sqlite",
237         "ui/resource_manager_0x?/default_resource/0x?",
238         "ui/resource_manager_0x?/tinted_resource",
239         "site_storage/blob_storage/0x?",
240         "v8/main/code_stats",
241         "v8/main/contexts/detached_context",
242         "v8/main/contexts/native_context",
243         "v8/main/global_handles",
244         "v8/main/heap/code_space",
245         "v8/main/heap/code_stats",
246         "v8/main/heap/code_large_object_space",
247         "v8/main/heap/large_object_space",
248         "v8/main/heap/map_space",
249         "v8/main/heap/new_large_object_space",
250         "v8/main/heap/new_space",
251         "v8/main/heap/old_space",
252         "v8/main/heap/read_only_space",
253         "v8/main/heap/shared_large_object_space",
254         "v8/main/heap/shared_space",
255         "v8/main/heap/trusted_space",
256         "v8/main/heap/trusted_large_object_space",
257         "v8/main/malloc",
258         "v8/main/zapped_for_debug",
259         "v8/utility/code_stats",
260         "v8/utility/contexts/detached_context",
261         "v8/utility/contexts/native_context",
262         "v8/utility/global_handles",
263         "v8/utility/heap/code_space",
264         "v8/utility/heap/code_large_object_space",
265         "v8/utility/heap/large_object_space",
266         "v8/utility/heap/map_space",
267         "v8/utility/heap/new_large_object_space",
268         "v8/utility/heap/new_space",
269         "v8/utility/heap/old_space",
270         "v8/utility/heap/read_only_space",
271         "v8/utility/heap/shared_large_object_space",
272         "v8/utility/heap/shared_space",
273         "v8/utility/heap/trusted_space",
274         "v8/utility/heap/trusted_large_object_space",
275         "v8/utility/malloc",
276         "v8/utility/zapped_for_debug",
277         "v8/workers/code_stats/isolate_0x?",
278         "v8/workers/contexts/detached_context/isolate_0x?",
279         "v8/workers/contexts/native_context/isolate_0x?",
280         "v8/workers/global_handles/isolate_0x?",
281         "v8/workers/heap/code_space/isolate_0x?",
282         "v8/workers/heap/code_large_object_space/isolate_0x?",
283         "v8/workers/heap/large_object_space/isolate_0x?",
284         "v8/workers/heap/map_space/isolate_0x?",
285         "v8/workers/heap/new_large_object_space/isolate_0x?",
286         "v8/workers/heap/new_space/isolate_0x?",
287         "v8/workers/heap/old_space/isolate_0x?",
288         "v8/workers/heap/read_only_space/isolate_0x?",
289         "v8/workers/heap/shared_large_object_space/isolate_0x?",
290         "v8/workers/heap/shared_space/isolate_0x?",
291         "v8/workers/heap/trusted_space/isolate_0x?",
292         "v8/workers/heap/trusted_large_object_space/isolate_0x?",
293         "v8/workers/malloc/isolate_0x?",
294         "v8/workers/zapped_for_debug/isolate_0x?",
295         "site_storage/index_db/db_0x?",
296         "site_storage/index_db/memenv_0x?",
297         "site_storage/index_db/in_flight_0x?",
298         "site_storage/local_storage/0x?/cache_size",
299         "site_storage/localstorage/0x?/cache_size",
300         "site_storage/localstorage/0x?/leveldb",
301         "site_storage/session_storage/0x?",
302         "site_storage/session_storage/0x?/cache_size",
303         "tab_restore/service_helper_0x?/entries",
304         "tab_restore/service_helper_0x?/entries/group_0x?",
305         "tab_restore/service_helper_0x?/entries/tab_0x?",
306         "tab_restore/service_helper_0x?/entries/window_0x?",
307         "tracing/heap_profiler_blink_gc/AllocationRegister",
308         "tracing/heap_profiler_malloc/AllocationRegister",
309         "tracing/heap_profiler_partition_alloc/AllocationRegister",
310   // clang-format on
311 });
312 
313 const char* const* g_dump_provider_allowlist_for_testing = nullptr;
314 const char* const* g_allocator_dump_name_allowlist_for_testing = nullptr;
315 
IsNameInList(const char * name,const char * const * list)316 bool IsNameInList(const char* name, const char* const* list) {
317   for (size_t i = 0; list[i] != nullptr; ++i) {
318     if (strcmp(name, list[i]) == 0)
319       return true;
320   }
321   return false;
322 }
323 
324 }  // namespace
325 
IsMemoryDumpProviderInAllowlist(const char * mdp_name)326 bool IsMemoryDumpProviderInAllowlist(const char* mdp_name) {
327   if (!g_dump_provider_allowlist_for_testing) {
328     return kDumpProviderAllowlist.contains(mdp_name);
329   } else {
330     return IsNameInList(mdp_name, g_dump_provider_allowlist_for_testing);
331   }
332 }
333 
IsMemoryAllocatorDumpNameInAllowlist(const std::string & name)334 bool IsMemoryAllocatorDumpNameInAllowlist(const std::string& name) {
335   // Global dumps that are of hex digits are all allowed for background use.
336   if (base::StartsWith(name, "global/", CompareCase::SENSITIVE)) {
337     for (size_t i = strlen("global/"); i < name.size(); i++)
338       if (!base::IsHexDigit(name[i]))
339         return false;
340     return true;
341   }
342 
343   if (base::StartsWith(name, "shared_memory/", CompareCase::SENSITIVE)) {
344     for (size_t i = strlen("shared_memory/"); i < name.size(); i++)
345       if (!base::IsHexDigit(name[i]))
346         return false;
347     return true;
348   }
349 
350   // Remove special characters, numbers (including hexadecimal which are marked
351   // by '0x') from the given string.
352   const size_t length = name.size();
353   std::string stripped_str;
354   stripped_str.reserve(length);
355   bool parsing_hex = false;
356   for (size_t i = 0; i < length; ++i) {
357     if (parsing_hex &&
358         absl::ascii_isxdigit(static_cast<unsigned char>(name[i]))) {
359       continue;
360     }
361     parsing_hex = false;
362     if (i + 1 < length && name[i] == '0' && name[i + 1] == 'x') {
363       parsing_hex = true;
364       stripped_str.append("0x?");
365       ++i;
366     } else {
367       stripped_str.push_back(name[i]);
368     }
369   }
370 
371   if (!g_allocator_dump_name_allowlist_for_testing) {
372     return kAllocatorDumpNameAllowlist.contains(stripped_str);
373   } else {
374     return IsNameInList(stripped_str.c_str(),
375                         g_allocator_dump_name_allowlist_for_testing);
376   }
377 }
378 
SetDumpProviderAllowlistForTesting(const char * const * list)379 void SetDumpProviderAllowlistForTesting(const char* const* list) {
380   g_dump_provider_allowlist_for_testing = list;
381 }
382 
SetAllocatorDumpNameAllowlistForTesting(const char * const * list)383 void SetAllocatorDumpNameAllowlistForTesting(const char* const* list) {
384   g_allocator_dump_name_allowlist_for_testing = list;
385 }
386 
387 }  // namespace trace_event
388 }  // namespace base
389