memory_infra_background_allowlist.cc 13 KB

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