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_whitelist.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 whitelisted for 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 kDumpProviderWhitelist[] = {
24     "android::ResourceManagerImpl",
25     "AutocompleteController",
26     "BlinkGC",
27     "BlinkObjectCounters",
28     "BlobStorageContext",
29     "ClientDiscardableSharedMemoryManager",
30     "DOMStorage",
31     "DownloadService",
32     "DiscardableSharedMemoryManager",
33     "gpu::BufferManager",
34     "gpu::RenderbufferManager",
35     "gpu::TextureManager",
36     "FontCaches",
37     "HistoryReport",
38     "IPCChannel",
39     "IndexedDBBackingStore",
40     "InMemoryURLIndex",
41     "JavaHeap",
42     "LevelDB",
43     "LeveldbValueStore",
44     "LocalStorage",
45     "Malloc",
46     "MemoryCache",
47     "MojoHandleTable",
48     "MojoLevelDB",
49     "MojoMessages",
50     "PartitionAlloc",
51     "ProcessMemoryMetrics",
52     "RenderProcessHost",
53     "SharedMemoryTracker",
54     "Skia",
55     "Sql",
56     "URLRequestContext",
57     "V8Isolate",
58     "SyncDirectory",
59     "TabRestoreServiceHelper",
60     nullptr  // End of list marker.
61 };
62 
63 // A list of string names that are allowed for the memory allocator dumps in
64 // background mode.
65 const char* const kAllocatorDumpNameWhitelist[] = {
66     "blink_gc",
67     "blink_gc/allocated_objects",
68     "blink_objects/AdSubframe",
69     "blink_objects/AudioHandler",
70     "blink_objects/DetachedScriptState",
71     "blink_objects/Document",
72     "blink_objects/Frame",
73     "blink_objects/JSEventListener",
74     "blink_objects/LayoutObject",
75     "blink_objects/MediaKeySession",
76     "blink_objects/MediaKeys",
77     "blink_objects/Node",
78     "blink_objects/Resource",
79     "blink_objects/RTCPeerConnection",
80     "blink_objects/ScriptPromise",
81     "blink_objects/PausableObject",
82     "blink_objects/V8PerContextData",
83     "blink_objects/WorkerGlobalScope",
84     "blink_objects/UACSSResource",
85     "blink_objects/ResourceFetcher",
86     "components/download/controller_0x?",
87     "discardable",
88     "discardable/child_0x?",
89     "extensions/value_store/Extensions.Database.Open.Settings/0x?",
90     "extensions/value_store/Extensions.Database.Open.Rules/0x?",
91     "extensions/value_store/Extensions.Database.Open.State/0x?",
92     "extensions/value_store/Extensions.Database.Open/0x?",
93     "extensions/value_store/Extensions.Database.Restore/0x?",
94     "extensions/value_store/Extensions.Database.Value.Restore/0x?",
95     "font_caches/font_platform_data_cache",
96     "font_caches/shape_caches",
97     "gpu/gl/buffers/share_group_0x?",
98     "gpu/gl/renderbuffers/share_group_0x?",
99     "gpu/gl/textures/share_group_0x?",
100     "history/delta_file_service/leveldb_0x?",
101     "history/usage_reports_buffer/leveldb_0x?",
102     "java_heap",
103     "java_heap/allocated_objects",
104     "leveldatabase",
105     "leveldatabase/block_cache/browser",
106     "leveldatabase/block_cache/in_memory",
107     "leveldatabase/block_cache/unified",
108     "leveldatabase/block_cache/web",
109     "leveldatabase/db_0x?",
110     "leveldatabase/db_0x?/block_cache",
111     "leveldatabase/memenv_0x?",
112     "malloc",
113     "malloc/allocated_objects",
114     "malloc/metadata_fragmentation_caches",
115     "mojo",
116     "mojo/data_pipe_consumer",
117     "mojo/data_pipe_producer",
118     "mojo/invitation",
119     "mojo/messages",
120     "mojo/message_pipe",
121     "mojo/platform_handle",
122     "mojo/queued_ipc_channel_message/0x?",
123     "mojo/render_process_host/0x?",
124     "mojo/shared_buffer",
125     "mojo/unknown",
126     "mojo/watcher",
127     "net/http_network_session_0x?",
128     "net/http_network_session_0x?/quic_stream_factory",
129     "net/http_network_session_0x?/socket_pool",
130     "net/http_network_session_0x?/spdy_session_pool",
131     "net/http_network_session_0x?/stream_factory",
132     "net/ssl_session_cache",
133     "net/url_request_context",
134     "net/url_request_context/app_request",
135     "net/url_request_context/app_request/0x?",
136     "net/url_request_context/app_request/0x?/cookie_monster",
137     "net/url_request_context/app_request/0x?/cookie_monster/cookies",
138     "net/url_request_context/app_request/0x?/cookie_monster/"
139     "tasks_pending_global",
140     "net/url_request_context/app_request/0x?/cookie_monster/"
141     "tasks_pending_for_key",
142     "net/url_request_context/app_request/0x?/http_cache",
143     "net/url_request_context/app_request/0x?/http_cache/memory_backend",
144     "net/url_request_context/app_request/0x?/http_cache/simple_backend",
145     "net/url_request_context/app_request/0x?/http_network_session",
146     "net/url_request_context/extensions",
147     "net/url_request_context/extensions/0x?",
148     "net/url_request_context/extensions/0x?/cookie_monster",
149     "net/url_request_context/extensions/0x?/cookie_monster/cookies",
150     "net/url_request_context/extensions/0x?/cookie_monster/"
151     "tasks_pending_global",
152     "net/url_request_context/extensions/0x?/cookie_monster/"
153     "tasks_pending_for_key",
154     "net/url_request_context/extensions/0x?/http_cache",
155     "net/url_request_context/extensions/0x?/http_cache/memory_backend",
156     "net/url_request_context/extensions/0x?/http_cache/simple_backend",
157     "net/url_request_context/extensions/0x?/http_network_session",
158     "net/url_request_context/isolated_media",
159     "net/url_request_context/isolated_media/0x?",
160     "net/url_request_context/isolated_media/0x?/cookie_monster",
161     "net/url_request_context/isolated_media/0x?/cookie_monster/cookies",
162     "net/url_request_context/isolated_media/0x?/cookie_monster/"
163     "tasks_pending_global",
164     "net/url_request_context/isolated_media/0x?/cookie_monster/"
165     "tasks_pending_for_key",
166     "net/url_request_context/isolated_media/0x?/http_cache",
167     "net/url_request_context/isolated_media/0x?/http_cache/memory_backend",
168     "net/url_request_context/isolated_media/0x?/http_cache/simple_backend",
169     "net/url_request_context/isolated_media/0x?/http_network_session",
170     "net/url_request_context/main",
171     "net/url_request_context/main/0x?",
172     "net/url_request_context/main/0x?/cookie_monster",
173     "net/url_request_context/main/0x?/cookie_monster/cookies",
174     "net/url_request_context/main/0x?/cookie_monster/tasks_pending_global",
175     "net/url_request_context/main/0x?/cookie_monster/tasks_pending_for_key",
176     "net/url_request_context/main/0x?/http_cache",
177     "net/url_request_context/main/0x?/http_cache/memory_backend",
178     "net/url_request_context/main/0x?/http_cache/simple_backend",
179     "net/url_request_context/main/0x?/http_network_session",
180     "net/url_request_context/main_media",
181     "net/url_request_context/main_media/0x?",
182     "net/url_request_context/main_media/0x?/cookie_monster",
183     "net/url_request_context/main_media/0x?/cookie_monster/cookies",
184     "net/url_request_context/main_media/0x?/cookie_monster/"
185     "tasks_pending_global",
186     "net/url_request_context/main_media/0x?/cookie_monster/"
187     "tasks_pending_for_key",
188     "net/url_request_context/main_media/0x?/http_cache",
189     "net/url_request_context/main_media/0x?/http_cache/memory_backend",
190     "net/url_request_context/main_media/0x?/http_cache/simple_backend",
191     "net/url_request_context/main_media/0x?/http_network_session",
192     "net/url_request_context/proxy",
193     "net/url_request_context/proxy/0x?",
194     "net/url_request_context/proxy/0x?/cookie_monster",
195     "net/url_request_context/proxy/0x?/cookie_monster/cookies",
196     "net/url_request_context/proxy/0x?/cookie_monster/tasks_pending_global",
197     "net/url_request_context/proxy/0x?/cookie_monster/tasks_pending_for_key",
198     "net/url_request_context/proxy/0x?/http_cache",
199     "net/url_request_context/proxy/0x?/http_cache/memory_backend",
200     "net/url_request_context/proxy/0x?/http_cache/simple_backend",
201     "net/url_request_context/proxy/0x?/http_network_session",
202     "net/url_request_context/safe_browsing",
203     "net/url_request_context/safe_browsing/0x?",
204     "net/url_request_context/safe_browsing/0x?/cookie_monster",
205     "net/url_request_context/safe_browsing/0x?/cookie_monster/cookies",
206     "net/url_request_context/safe_browsing/0x?/cookie_monster/"
207     "tasks_pending_global",
208     "net/url_request_context/safe_browsing/0x?/cookie_monster/"
209     "tasks_pending_for_key",
210     "net/url_request_context/safe_browsing/0x?/http_cache",
211     "net/url_request_context/safe_browsing/0x?/http_cache/memory_backend",
212     "net/url_request_context/safe_browsing/0x?/http_cache/simple_backend",
213     "net/url_request_context/safe_browsing/0x?/http_network_session",
214     "net/url_request_context/system",
215     "net/url_request_context/system/0x?",
216     "net/url_request_context/system/0x?/cookie_monster",
217     "net/url_request_context/system/0x?/cookie_monster/cookies",
218     "net/url_request_context/system/0x?/cookie_monster/tasks_pending_global",
219     "net/url_request_context/system/0x?/cookie_monster/tasks_pending_for_key",
220     "net/url_request_context/system/0x?/http_cache",
221     "net/url_request_context/system/0x?/http_cache/memory_backend",
222     "net/url_request_context/system/0x?/http_cache/simple_backend",
223     "net/url_request_context/system/0x?/http_network_session",
224     "net/url_request_context/unknown",
225     "net/url_request_context/unknown/0x?",
226     "net/url_request_context/unknown/0x?/cookie_monster",
227     "net/url_request_context/unknown/0x?/cookie_monster/cookies",
228     "net/url_request_context/unknown/0x?/cookie_monster/tasks_pending_global",
229     "net/url_request_context/unknown/0x?/cookie_monster/tasks_pending_for_key",
230     "net/url_request_context/unknown/0x?/http_cache",
231     "net/url_request_context/unknown/0x?/http_cache/memory_backend",
232     "net/url_request_context/unknown/0x?/http_cache/simple_backend",
233     "net/url_request_context/unknown/0x?/http_network_session",
234     "omnibox/autocomplete_controller/0x?",
235     "omnibox/in_memory_url_index/0x?",
236     "web_cache/Image_resources",
237     "web_cache/CSS stylesheet_resources",
238     "web_cache/Script_resources",
239     "web_cache/XSL stylesheet_resources",
240     "web_cache/Font_resources",
241     "web_cache/Other_resources",
242     "partition_alloc/allocated_objects",
243     "partition_alloc/partitions",
244     "partition_alloc/partitions/array_buffer",
245     "partition_alloc/partitions/buffer",
246     "partition_alloc/partitions/fast_malloc",
247     "partition_alloc/partitions/layout",
248     "skia/sk_glyph_cache",
249     "skia/sk_resource_cache",
250     "sqlite",
251     "ui/resource_manager_0x?/default_resource/0x?",
252     "ui/resource_manager_0x?/tinted_resource",
253     "v8/isolate_0x?/contexts/detached_context",
254     "v8/isolate_0x?/contexts/native_context",
255     "v8/isolate_0x?/heap_spaces",
256     "v8/isolate_0x?/heap_spaces/code_space",
257     "v8/isolate_0x?/heap_spaces/large_object_space",
258     "v8/isolate_0x?/heap_spaces/map_space",
259     "v8/isolate_0x?/heap_spaces/new_space",
260     "v8/isolate_0x?/heap_spaces/new_large_object_space",
261     "v8/isolate_0x?/heap_spaces/old_space",
262     "v8/isolate_0x?/heap_spaces/read_only_space",
263     "v8/isolate_0x?/malloc",
264     "v8/isolate_0x?/zapped_for_debug",
265     "site_storage/blob_storage/0x?",
266     "site_storage/index_db/db_0x?",
267     "site_storage/index_db/memenv_0x?",
268     "site_storage/localstorage/0x?/cache_size",
269     "site_storage/localstorage/0x?/leveldb",
270     "site_storage/session_storage/0x?",
271     "site_storage/session_storage/0x?/cache_size",
272     "sync/0x?/kernel",
273     "sync/0x?/store",
274     "sync/0x?/model_type/APP",
275     "sync/0x?/model_type/APP_LIST",
276     "sync/0x?/model_type/APP_NOTIFICATION",
277     "sync/0x?/model_type/APP_SETTING",
278     "sync/0x?/model_type/ARC_PACKAGE",
279     "sync/0x?/model_type/ARTICLE",
280     "sync/0x?/model_type/AUTOFILL",
281     "sync/0x?/model_type/AUTOFILL_PROFILE",
282     "sync/0x?/model_type/AUTOFILL_WALLET",
283     "sync/0x?/model_type/BOOKMARK",
284     "sync/0x?/model_type/DEVICE_INFO",
285     "sync/0x?/model_type/DICTIONARY",
286     "sync/0x?/model_type/EXPERIMENTS",
287     "sync/0x?/model_type/EXTENSION",
288     "sync/0x?/model_type/EXTENSION_SETTING",
289     "sync/0x?/model_type/FAVICON_IMAGE",
290     "sync/0x?/model_type/FAVICON_TRACKING",
291     "sync/0x?/model_type/HISTORY_DELETE_DIRECTIVE",
292     "sync/0x?/model_type/MANAGED_USER",
293     "sync/0x?/model_type/MANAGED_USER_SETTING",
294     "sync/0x?/model_type/MANAGED_USER_SHARED_SETTING",
295     "sync/0x?/model_type/MANAGED_USER_WHITELIST",
296     "sync/0x?/model_type/MOUNTAIN_SHARE",
297     "sync/0x?/model_type/NIGORI",
298     "sync/0x?/model_type/PASSWORD",
299     "sync/0x?/model_type/PREFERENCE",
300     "sync/0x?/model_type/PRINTER",
301     "sync/0x?/model_type/PRIORITY_PREFERENCE",
302     "sync/0x?/model_type/READING_LIST",
303     "sync/0x?/model_type/SEARCH_ENGINE",
304     "sync/0x?/model_type/SESSION",
305     "sync/0x?/model_type/SYNCED_NOTIFICATION",
306     "sync/0x?/model_type/SYNCED_NOTIFICATION_APP_INFO",
307     "sync/0x?/model_type/THEME",
308     "sync/0x?/model_type/TYPED_URL",
309     "sync/0x?/model_type/USER_CONSENT",
310     "sync/0x?/model_type/USER_EVENT",
311     "sync/0x?/model_type/WALLET_METADATA",
312     "sync/0x?/model_type/WIFI_CREDENTIAL",
313     "tab_restore/service_helper_0x?/entries",
314     "tab_restore/service_helper_0x?/entries/tab_0x?",
315     "tab_restore/service_helper_0x?/entries/window_0x?",
316     "tracing/heap_profiler_blink_gc/AllocationRegister",
317     "tracing/heap_profiler_malloc/AllocationRegister",
318     "tracing/heap_profiler_partition_alloc/AllocationRegister",
319     nullptr  // End of list marker.
320 };
321 
322 const char* const* g_dump_provider_whitelist = kDumpProviderWhitelist;
323 const char* const* g_allocator_dump_name_whitelist =
324     kAllocatorDumpNameWhitelist;
325 
IsMemoryDumpProviderInList(const char * mdp_name,const char * const * list)326 bool IsMemoryDumpProviderInList(const char* mdp_name, const char* const* list) {
327   for (size_t i = 0; list[i] != nullptr; ++i) {
328     if (strcmp(mdp_name, list[i]) == 0)
329       return true;
330   }
331   return false;
332 }
333 
334 }  // namespace
335 
IsMemoryDumpProviderWhitelisted(const char * mdp_name)336 bool IsMemoryDumpProviderWhitelisted(const char* mdp_name) {
337   return IsMemoryDumpProviderInList(mdp_name, g_dump_provider_whitelist);
338 }
339 
IsMemoryAllocatorDumpNameWhitelisted(const std::string & name)340 bool IsMemoryAllocatorDumpNameWhitelisted(const std::string& name) {
341   // Global dumps are explicitly whitelisted for background use.
342   if (base::StartsWith(name, "global/", CompareCase::SENSITIVE)) {
343     for (size_t i = strlen("global/"); i < name.size(); i++)
344       if (!base::IsHexDigit(name[i]))
345         return false;
346     return true;
347   }
348 
349   if (base::StartsWith(name, "shared_memory/", CompareCase::SENSITIVE)) {
350     for (size_t i = strlen("shared_memory/"); i < name.size(); i++)
351       if (!base::IsHexDigit(name[i]))
352         return false;
353     return true;
354   }
355 
356   // Remove special characters, numbers (including hexadecimal which are marked
357   // by '0x') from the given string.
358   const size_t length = name.size();
359   std::string stripped_str;
360   stripped_str.reserve(length);
361   bool parsing_hex = false;
362   for (size_t i = 0; i < length; ++i) {
363     if (parsing_hex && isxdigit(name[i]))
364       continue;
365     parsing_hex = false;
366     if (i + 1 < length && name[i] == '0' && name[i + 1] == 'x') {
367       parsing_hex = true;
368       stripped_str.append("0x?");
369       ++i;
370     } else {
371       stripped_str.push_back(name[i]);
372     }
373   }
374 
375   for (size_t i = 0; g_allocator_dump_name_whitelist[i] != nullptr; ++i) {
376     if (stripped_str == g_allocator_dump_name_whitelist[i]) {
377       return true;
378     }
379   }
380   return false;
381 }
382 
SetDumpProviderWhitelistForTesting(const char * const * list)383 void SetDumpProviderWhitelistForTesting(const char* const* list) {
384   g_dump_provider_whitelist = list;
385 }
386 
SetAllocatorDumpNameWhitelistForTesting(const char * const * list)387 void SetAllocatorDumpNameWhitelistForTesting(const char* const* list) {
388   g_allocator_dump_name_whitelist = list;
389 }
390 
391 }  // namespace trace_event
392 }  // namespace base
393