1 // Copyright 2016 The Chromium Authors. All rights reserved.
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 <ctype.h>
8 #include <string.h>
9 
10 #include <string>
11 
12 #include "base/strings/string_util.h"
13 
14 namespace base {
15 namespace trace_event {
16 namespace {
17 
18 // The names of dump providers allowed to perform background tracing. Dump
19 // providers can be added here only if the background mode dump has very
20 // little processor and memory overhead.
21 // TODO(ssid): Some dump providers do not create ownership edges on background
22 // dump. So, the effective size will not be correct.
23 const char* const kDumpProviderAllowlist[] = {
24     "android::ResourceManagerImpl",
25     "AutocompleteController",
26     "BlinkGC",
27     "BlinkObjectCounters",
28     "BlobStorageContext",
29     "ClientDiscardableSharedMemoryManager",
30     "DevTools",
31     "DiscardableSharedMemoryManager",
32     "DOMStorage",
33     "DownloadService",
34     "ExtensionFunctions",
35     "gpu::BufferManager",
36     "gpu::RenderbufferManager",
37     "gpu::ServiceDiscardableManager",
38     "gpu::ServiceTransferCache",
39     "gpu::SharedImageStub",
40     "gpu::TextureManager",
41     "GrShaderCache",
42     "FontCaches",
43     "HistoryReport",
44     "IPCChannel",
45     "IndexedDBBackingStore",
46     "IndexedDBFactoryImpl",
47     "InMemoryURLIndex",
48     "JavaHeap",
49     "LevelDB",
50     "LeveldbValueStore",
51     "LocalStorage",
52     "MadvFreeDiscardableMemoryAllocator",
53     "Malloc",
54     "MemoryCache",
55     "MojoHandleTable",
56     "MojoLevelDB",
57     "MojoMessages",
58     "PartitionAlloc",
59     "ProcessMemoryMetrics",
60     "SharedContextState",
61     "SharedMemoryTracker",
62     "Skia",
63     "Sql",
64     "URLRequestContext",
65     "V8Isolate",
66     "WebMediaPlayer_MainThread",
67     "WebMediaPlayer_MediaThread",
68     "SyncDirectory",
69     "TabRestoreServiceHelper",
70     "VizProcessContextProvider",
71     nullptr  // End of list marker.
72 };
73 
74 // A list of string names that are allowed for the memory allocator dumps in
75 // background mode.
76 const char* const kAllocatorDumpNameAllowlist[] = {
77     "blink_gc/main/heap",
78     "blink_gc/workers/heap/worker_0x?",
79     "blink_objects/AdSubframe",
80     "blink_objects/AudioHandler",
81     "blink_objects/ContextLifecycleStateObserver",
82     "blink_objects/DetachedScriptState",
83     "blink_objects/Document",
84     "blink_objects/Frame",
85     "blink_objects/JSEventListener",
86     "blink_objects/LayoutObject",
87     "blink_objects/MediaKeySession",
88     "blink_objects/MediaKeys",
89     "blink_objects/Node",
90     "blink_objects/Resource",
91     "blink_objects/RTCPeerConnection",
92     "blink_objects/ScriptPromise",
93     "blink_objects/V8PerContextData",
94     "blink_objects/WorkerGlobalScope",
95     "blink_objects/UACSSResource",
96     "blink_objects/ResourceFetcher",
97     "components/download/controller_0x?",
98     "devtools/file_watcher_0x?",
99     "discardable",
100     "discardable/madv_free_allocated",
101     "discardable/child_0x?",
102     "extensions/functions",
103     "extensions/value_store/Extensions.Database.Open.Settings/0x?",
104     "extensions/value_store/Extensions.Database.Open.Rules/0x?",
105     "extensions/value_store/Extensions.Database.Open.State/0x?",
106     "extensions/value_store/Extensions.Database.Open/0x?",
107     "extensions/value_store/Extensions.Database.Restore/0x?",
108     "extensions/value_store/Extensions.Database.Value.Restore/0x?",
109     "font_caches/font_platform_data_cache",
110     "font_caches/shape_caches",
111     "gpu/discardable_cache/cache_0x?",
112     "gpu/discardable_cache/cache_0x?/avg_image_size",
113     "gpu/gl/buffers/context_group_0x?",
114     "gpu/gl/renderbuffers/context_group_0x?",
115     "gpu/gl/textures/context_group_0x?",
116     "gpu/gr_shader_cache/cache_0x?",
117     "gpu/shared_images/client_0x?",
118     "gpu/transfer_cache/cache_0x?",
119     "gpu/transfer_cache/cache_0x?/avg_image_size",
120     "history/delta_file_service/leveldb_0x?",
121     "history/usage_reports_buffer/leveldb_0x?",
122     "java_heap",
123     "java_heap/allocated_objects",
124     "leveldatabase",
125     "leveldatabase/block_cache/browser",
126     "leveldatabase/block_cache/in_memory",
127     "leveldatabase/block_cache/unified",
128     "leveldatabase/block_cache/web",
129     "leveldatabase/db_0x?",
130     "leveldatabase/db_0x?/block_cache",
131     "leveldatabase/memenv_0x?",
132     "malloc",
133     "malloc/allocated_objects",
134     "malloc/metadata_fragmentation_caches",
135     "media/webmediaplayer/audio/player_0x?",
136     "media/webmediaplayer/data_source/player_0x?",
137     "media/webmediaplayer/demuxer/player_0x?",
138     "media/webmediaplayer/video/player_0x?",
139     "media/webmediaplayer/player_0x?",
140     "mojo",
141     "mojo/data_pipe_consumer",
142     "mojo/data_pipe_producer",
143     "mojo/invitation",
144     "mojo/messages",
145     "mojo/message_pipe",
146     "mojo/platform_handle",
147     "mojo/queued_ipc_channel_message/0x?",
148     "mojo/shared_buffer",
149     "mojo/unknown",
150     "mojo/watcher",
151     "net/http_network_session_0x?",
152     "net/http_network_session_0x?/quic_stream_factory",
153     "net/http_network_session_0x?/socket_pool",
154     "net/http_network_session_0x?/spdy_session_pool",
155     "net/http_network_session_0x?/ssl_client_session_cache",
156     "net/http_network_session_0x?/stream_factory",
157     "net/url_request_context",
158     "net/url_request_context/app_request",
159     "net/url_request_context/app_request/0x?",
160     "net/url_request_context/app_request/0x?/cookie_monster",
161     "net/url_request_context/app_request/0x?/cookie_monster/cookies",
162     "net/url_request_context/app_request/0x?/cookie_monster/"
163     "tasks_pending_global",
164     "net/url_request_context/app_request/0x?/cookie_monster/"
165     "tasks_pending_for_key",
166     "net/url_request_context/app_request/0x?/http_cache",
167     "net/url_request_context/app_request/0x?/http_cache/memory_backend",
168     "net/url_request_context/app_request/0x?/http_cache/simple_backend",
169     "net/url_request_context/app_request/0x?/http_network_session",
170     "net/url_request_context/extensions",
171     "net/url_request_context/extensions/0x?",
172     "net/url_request_context/extensions/0x?/cookie_monster",
173     "net/url_request_context/extensions/0x?/cookie_monster/cookies",
174     "net/url_request_context/extensions/0x?/cookie_monster/"
175     "tasks_pending_global",
176     "net/url_request_context/extensions/0x?/cookie_monster/"
177     "tasks_pending_for_key",
178     "net/url_request_context/extensions/0x?/http_cache",
179     "net/url_request_context/extensions/0x?/http_cache/memory_backend",
180     "net/url_request_context/extensions/0x?/http_cache/simple_backend",
181     "net/url_request_context/extensions/0x?/http_network_session",
182     "net/url_request_context/isolated_media",
183     "net/url_request_context/isolated_media/0x?",
184     "net/url_request_context/isolated_media/0x?/cookie_monster",
185     "net/url_request_context/isolated_media/0x?/cookie_monster/cookies",
186     "net/url_request_context/isolated_media/0x?/cookie_monster/"
187     "tasks_pending_global",
188     "net/url_request_context/isolated_media/0x?/cookie_monster/"
189     "tasks_pending_for_key",
190     "net/url_request_context/isolated_media/0x?/http_cache",
191     "net/url_request_context/isolated_media/0x?/http_cache/memory_backend",
192     "net/url_request_context/isolated_media/0x?/http_cache/simple_backend",
193     "net/url_request_context/isolated_media/0x?/http_network_session",
194     "net/url_request_context/main",
195     "net/url_request_context/main/0x?",
196     "net/url_request_context/main/0x?/cookie_monster",
197     "net/url_request_context/main/0x?/cookie_monster/cookies",
198     "net/url_request_context/main/0x?/cookie_monster/tasks_pending_global",
199     "net/url_request_context/main/0x?/cookie_monster/tasks_pending_for_key",
200     "net/url_request_context/main/0x?/http_cache",
201     "net/url_request_context/main/0x?/http_cache/memory_backend",
202     "net/url_request_context/main/0x?/http_cache/simple_backend",
203     "net/url_request_context/main/0x?/http_network_session",
204     "net/url_request_context/main_media",
205     "net/url_request_context/main_media/0x?",
206     "net/url_request_context/main_media/0x?/cookie_monster",
207     "net/url_request_context/main_media/0x?/cookie_monster/cookies",
208     "net/url_request_context/main_media/0x?/cookie_monster/"
209     "tasks_pending_global",
210     "net/url_request_context/main_media/0x?/cookie_monster/"
211     "tasks_pending_for_key",
212     "net/url_request_context/main_media/0x?/http_cache",
213     "net/url_request_context/main_media/0x?/http_cache/memory_backend",
214     "net/url_request_context/main_media/0x?/http_cache/simple_backend",
215     "net/url_request_context/main_media/0x?/http_network_session",
216     "net/url_request_context/mirroring",
217     "net/url_request_context/mirroring/0x?",
218     "net/url_request_context/mirroring/0x?/cookie_monster",
219     "net/url_request_context/mirroring/0x?/cookie_monster/cookies",
220     "net/url_request_context/mirroring/0x?/cookie_monster/tasks_pending_global",
221     "net/url_request_context/mirroring/0x?/cookie_monster/"
222     "tasks_pending_for_key",
223     "net/url_request_context/mirroring/0x?/http_cache",
224     "net/url_request_context/mirroring/0x?/http_cache/memory_backend",
225     "net/url_request_context/mirroring/0x?/http_cache/simple_backend",
226     "net/url_request_context/mirroring/0x?/http_network_session",
227     "net/url_request_context/proxy",
228     "net/url_request_context/proxy/0x?",
229     "net/url_request_context/proxy/0x?/cookie_monster",
230     "net/url_request_context/proxy/0x?/cookie_monster/cookies",
231     "net/url_request_context/proxy/0x?/cookie_monster/tasks_pending_global",
232     "net/url_request_context/proxy/0x?/cookie_monster/tasks_pending_for_key",
233     "net/url_request_context/proxy/0x?/http_cache",
234     "net/url_request_context/proxy/0x?/http_cache/memory_backend",
235     "net/url_request_context/proxy/0x?/http_cache/simple_backend",
236     "net/url_request_context/proxy/0x?/http_network_session",
237     "net/url_request_context/safe_browsing",
238     "net/url_request_context/safe_browsing/0x?",
239     "net/url_request_context/safe_browsing/0x?/cookie_monster",
240     "net/url_request_context/safe_browsing/0x?/cookie_monster/cookies",
241     "net/url_request_context/safe_browsing/0x?/cookie_monster/"
242     "tasks_pending_global",
243     "net/url_request_context/safe_browsing/0x?/cookie_monster/"
244     "tasks_pending_for_key",
245     "net/url_request_context/safe_browsing/0x?/http_cache",
246     "net/url_request_context/safe_browsing/0x?/http_cache/memory_backend",
247     "net/url_request_context/safe_browsing/0x?/http_cache/simple_backend",
248     "net/url_request_context/safe_browsing/0x?/http_network_session",
249     "net/url_request_context/system",
250     "net/url_request_context/system/0x?",
251     "net/url_request_context/system/0x?/cookie_monster",
252     "net/url_request_context/system/0x?/cookie_monster/cookies",
253     "net/url_request_context/system/0x?/cookie_monster/tasks_pending_global",
254     "net/url_request_context/system/0x?/cookie_monster/tasks_pending_for_key",
255     "net/url_request_context/system/0x?/http_cache",
256     "net/url_request_context/system/0x?/http_cache/memory_backend",
257     "net/url_request_context/system/0x?/http_cache/simple_backend",
258     "net/url_request_context/system/0x?/http_network_session",
259     "net/url_request_context/unknown",
260     "net/url_request_context/unknown/0x?",
261     "net/url_request_context/unknown/0x?/cookie_monster",
262     "net/url_request_context/unknown/0x?/cookie_monster/cookies",
263     "net/url_request_context/unknown/0x?/cookie_monster/tasks_pending_global",
264     "net/url_request_context/unknown/0x?/cookie_monster/tasks_pending_for_key",
265     "net/url_request_context/unknown/0x?/http_cache",
266     "net/url_request_context/unknown/0x?/http_cache/memory_backend",
267     "net/url_request_context/unknown/0x?/http_cache/simple_backend",
268     "net/url_request_context/unknown/0x?/http_network_session",
269     "omnibox/autocomplete_controller/0x?",
270     "omnibox/in_memory_url_index/0x?",
271     "web_cache/Image_resources",
272     "web_cache/CSS stylesheet_resources",
273     "web_cache/Script_resources",
274     "web_cache/XSL stylesheet_resources",
275     "web_cache/Font_resources",
276     "web_cache/Code_cache",
277     "web_cache/Encoded_size_duplicated_in_data_urls",
278     "web_cache/Other_resources",
279     "partition_alloc/allocated_objects",
280     "partition_alloc/partitions",
281     "partition_alloc/partitions/array_buffer",
282     "partition_alloc/partitions/buffer",
283     "partition_alloc/partitions/fast_malloc",
284     "partition_alloc/partitions/layout",
285     "skia/gpu_resources/context_0x?",
286     "skia/sk_glyph_cache",
287     "skia/sk_resource_cache",
288     "sqlite",
289     "ui/resource_manager_0x?/default_resource/0x?",
290     "ui/resource_manager_0x?/tinted_resource",
291     "site_storage/blob_storage/0x?",
292     "v8/main/code_stats",
293     "v8/main/contexts/detached_context",
294     "v8/main/contexts/native_context",
295     "v8/main/global_handles",
296     "v8/main/heap/code_space",
297     "v8/main/heap/code_stats",
298     "v8/main/heap/code_large_object_space",
299     "v8/main/heap/large_object_space",
300     "v8/main/heap/map_space",
301     "v8/main/heap/new_large_object_space",
302     "v8/main/heap/new_space",
303     "v8/main/heap/old_space",
304     "v8/main/heap/read_only_space",
305     "v8/main/malloc",
306     "v8/main/zapped_for_debug",
307     "v8/utility/code_stats",
308     "v8/utility/contexts/detached_context",
309     "v8/utility/contexts/native_context",
310     "v8/utility/global_handles",
311     "v8/utility/heap/code_space",
312     "v8/utility/heap/code_large_object_space",
313     "v8/utility/heap/large_object_space",
314     "v8/utility/heap/map_space",
315     "v8/utility/heap/new_large_object_space",
316     "v8/utility/heap/new_space",
317     "v8/utility/heap/old_space",
318     "v8/utility/heap/read_only_space",
319     "v8/utility/malloc",
320     "v8/utility/zapped_for_debug",
321     "v8/workers/code_stats/isolate_0x?",
322     "v8/workers/contexts/detached_context/isolate_0x?",
323     "v8/workers/contexts/native_context/isolate_0x?",
324     "v8/workers/global_handles/isolate_0x?",
325     "v8/workers/heap/code_space/isolate_0x?",
326     "v8/workers/heap/code_large_object_space/isolate_0x?",
327     "v8/workers/heap/large_object_space/isolate_0x?",
328     "v8/workers/heap/map_space/isolate_0x?",
329     "v8/workers/heap/new_large_object_space/isolate_0x?",
330     "v8/workers/heap/new_space/isolate_0x?",
331     "v8/workers/heap/old_space/isolate_0x?",
332     "v8/workers/heap/read_only_space/isolate_0x?",
333     "v8/workers/malloc/isolate_0x?",
334     "v8/workers/zapped_for_debug/isolate_0x?",
335     "site_storage/index_db/db_0x?",
336     "site_storage/index_db/memenv_0x?",
337     "site_storage/index_db/in_flight_0x?",
338     "site_storage/local_storage/0x?/cache_size",
339     "site_storage/localstorage/0x?/cache_size",
340     "site_storage/localstorage/0x?/leveldb",
341     "site_storage/session_storage/0x?",
342     "site_storage/session_storage/0x?/cache_size",
343     "sync/0x?/kernel",
344     "sync/0x?/store",
345     "sync/0x?/model_type/APP",
346     "sync/0x?/model_type/APP_LIST",
347     "sync/0x?/model_type/APP_NOTIFICATION",
348     "sync/0x?/model_type/APP_SETTING",
349     "sync/0x?/model_type/ARC_PACKAGE",
350     "sync/0x?/model_type/ARTICLE",
351     "sync/0x?/model_type/AUTOFILL",
352     "sync/0x?/model_type/AUTOFILL_PROFILE",
353     "sync/0x?/model_type/AUTOFILL_WALLET",
354     "sync/0x?/model_type/BOOKMARK",
355     "sync/0x?/model_type/DEVICE_INFO",
356     "sync/0x?/model_type/DICTIONARY",
357     "sync/0x?/model_type/EXPERIMENTS",
358     "sync/0x?/model_type/EXTENSION",
359     "sync/0x?/model_type/EXTENSION_SETTING",
360     "sync/0x?/model_type/FAVICON_IMAGE",
361     "sync/0x?/model_type/FAVICON_TRACKING",
362     "sync/0x?/model_type/HISTORY_DELETE_DIRECTIVE",
363     "sync/0x?/model_type/MANAGED_USER",
364     "sync/0x?/model_type/MANAGED_USER_SETTING",
365     "sync/0x?/model_type/MANAGED_USER_SHARED_SETTING",
366     "sync/0x?/model_type/MANAGED_USER_WHITELIST",
367     "sync/0x?/model_type/NIGORI",
368     "sync/0x?/model_type/OS_PREFERENCE",
369     "sync/0x?/model_type/OS_PRIORITY_PREFERENCE",
370     "sync/0x?/model_type/PASSWORD",
371     "sync/0x?/model_type/PREFERENCE",
372     "sync/0x?/model_type/PRINTER",
373     "sync/0x?/model_type/PRIORITY_PREFERENCE",
374     "sync/0x?/model_type/READING_LIST",
375     "sync/0x?/model_type/SEARCH_ENGINE",
376     "sync/0x?/model_type/SECURITY_EVENT",
377     "sync/0x?/model_type/SEND_TAB_TO_SELF",
378     "sync/0x?/model_type/SESSION",
379     "sync/0x?/model_type/SHARING_MESSAGE",
380     "sync/0x?/model_type/SYNCED_NOTIFICATION",
381     "sync/0x?/model_type/SYNCED_NOTIFICATION_APP_INFO",
382     "sync/0x?/model_type/THEME",
383     "sync/0x?/model_type/TYPED_URL",
384     "sync/0x?/model_type/USER_CONSENT",
385     "sync/0x?/model_type/USER_EVENT",
386     "sync/0x?/model_type/WALLET_METADATA",
387     "sync/0x?/model_type/WEB_APP",
388     "sync/0x?/model_type/WIFI_CONFIGURATION",
389     "sync/0x?/model_type/WIFI_CREDENTIAL",
390     "tab_restore/service_helper_0x?/entries",
391     "tab_restore/service_helper_0x?/entries/tab_0x?",
392     "tab_restore/service_helper_0x?/entries/window_0x?",
393     "tracing/heap_profiler_blink_gc/AllocationRegister",
394     "tracing/heap_profiler_malloc/AllocationRegister",
395     "tracing/heap_profiler_partition_alloc/AllocationRegister",
396     nullptr  // End of list marker.
397 };
398 
399 const char* const* g_dump_provider_allowlist = kDumpProviderAllowlist;
400 const char* const* g_allocator_dump_name_allowlist =
401     kAllocatorDumpNameAllowlist;
402 
IsMemoryDumpProviderInList(const char * mdp_name,const char * const * list)403 bool IsMemoryDumpProviderInList(const char* mdp_name, const char* const* list) {
404   for (size_t i = 0; list[i] != nullptr; ++i) {
405     if (strcmp(mdp_name, list[i]) == 0)
406       return true;
407   }
408   return false;
409 }
410 
411 }  // namespace
412 
IsMemoryDumpProviderInAllowlist(const char * mdp_name)413 bool IsMemoryDumpProviderInAllowlist(const char* mdp_name) {
414   return IsMemoryDumpProviderInList(mdp_name, g_dump_provider_allowlist);
415 }
416 
IsMemoryAllocatorDumpNameInAllowlist(const std::string & name)417 bool IsMemoryAllocatorDumpNameInAllowlist(const std::string& name) {
418   // Global dumps that are of hex digits are all allowed for background use.
419   if (base::StartsWith(name, "global/", CompareCase::SENSITIVE)) {
420     for (size_t i = strlen("global/"); i < name.size(); i++)
421       if (!base::IsHexDigit(name[i]))
422         return false;
423     return true;
424   }
425 
426   if (base::StartsWith(name, "shared_memory/", CompareCase::SENSITIVE)) {
427     for (size_t i = strlen("shared_memory/"); i < name.size(); i++)
428       if (!base::IsHexDigit(name[i]))
429         return false;
430     return true;
431   }
432 
433   // Remove special characters, numbers (including hexadecimal which are marked
434   // by '0x') from the given string.
435   const size_t length = name.size();
436   std::string stripped_str;
437   stripped_str.reserve(length);
438   bool parsing_hex = false;
439   for (size_t i = 0; i < length; ++i) {
440     if (parsing_hex && isxdigit(name[i]))
441       continue;
442     parsing_hex = false;
443     if (i + 1 < length && name[i] == '0' && name[i + 1] == 'x') {
444       parsing_hex = true;
445       stripped_str.append("0x?");
446       ++i;
447     } else {
448       stripped_str.push_back(name[i]);
449     }
450   }
451 
452   for (size_t i = 0; g_allocator_dump_name_allowlist[i] != nullptr; ++i) {
453     if (stripped_str == g_allocator_dump_name_allowlist[i]) {
454       return true;
455     }
456   }
457   return false;
458 }
459 
SetDumpProviderAllowlistForTesting(const char * const * list)460 void SetDumpProviderAllowlistForTesting(const char* const* list) {
461   g_dump_provider_allowlist = list;
462 }
463 
SetAllocatorDumpNameAllowlistForTesting(const char * const * list)464 void SetAllocatorDumpNameAllowlistForTesting(const char* const* list) {
465   g_allocator_dump_name_allowlist = list;
466 }
467 
468 }  // namespace trace_event
469 }  // namespace base
470