allocator.gni 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright 2019 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. import("//build/config/chromecast_build.gni")
  5. import("//build/config/sanitizers/sanitizers.gni")
  6. if (is_ios) {
  7. import("//build/config/ios/ios_sdk.gni")
  8. }
  9. # Sanitizers replace the allocator, don't use our own.
  10. _is_using_sanitizers = is_asan || is_hwasan || is_lsan || is_tsan || is_msan
  11. # - Component build support is disabled on all platforms. It is known to cause
  12. # issues on some (e.g. Windows with shims, Android with non-universal symbol
  13. # wrapping), and has not been validated on others.
  14. # - Windows: debug CRT is not compatible, see below.
  15. _disable_partition_alloc = is_component_build || (is_win && is_debug)
  16. # - NaCl: No plans to support it.
  17. # - iOS: not done yet.
  18. _is_partition_alloc_platform = !is_nacl && !is_ios
  19. # Under Windows Debug the allocator shim is not compatible with CRT.
  20. # NaCl in particular does seem to link some binaries statically
  21. # against the debug CRT with "is_nacl=false".
  22. # Under Fuchsia the allocator shim is only required for PA-E.
  23. # For all other platforms & configurations, the shim is required, to replace
  24. # the default system allocators, e.g. with Partition Alloc.
  25. if ((is_linux || is_chromeos || is_android || is_apple ||
  26. (is_fuchsia && !_disable_partition_alloc) ||
  27. (is_win && !is_component_build && !is_debug)) && !_is_using_sanitizers) {
  28. _default_use_allocator_shim = true
  29. } else {
  30. _default_use_allocator_shim = false
  31. }
  32. if (_default_use_allocator_shim && _is_partition_alloc_platform &&
  33. !_disable_partition_alloc) {
  34. _default_allocator = "partition"
  35. } else {
  36. _default_allocator = "none"
  37. }
  38. declare_args() {
  39. # Memory allocator to use. Set to "none" to use default allocator.
  40. use_allocator = _default_allocator
  41. # Causes all the allocations to be routed via allocator_shim.cc.
  42. use_allocator_shim = _default_use_allocator_shim
  43. # Whether PartitionAlloc should be available for use or not.
  44. # true makes PartitionAlloc linked to the executable or shared library and
  45. # makes it available for use. It doesn't mean that the default allocator
  46. # is PartitionAlloc, which is governed by |use_allocator|.
  47. #
  48. # This flag is currently set to false only on Cronet bots, because Cronet
  49. # doesn't use PartitionAlloc at all, and doesn't wish to incur the library
  50. # size increase (crbug.com/674570).
  51. use_partition_alloc = true
  52. }
  53. if (!use_partition_alloc && use_allocator == "partition") {
  54. # If there is a conflict, prioritize |use_partition_alloc| over
  55. # |use_allocator|.
  56. use_allocator = "none"
  57. }
  58. assert(use_allocator == "none" || use_allocator == "partition")
  59. assert(
  60. !use_allocator_shim || is_linux || is_chromeos || is_android || is_win ||
  61. is_fuchsia || is_apple,
  62. "use_allocator_shim works only on Android, iOS, Linux, macOS, Fuchsia, " +
  63. "and Windows.")
  64. if (is_win && use_allocator_shim) {
  65. # TODO(crbug.com/1245317): Add a comment indicating why the shim doesn't work.
  66. assert(!is_component_build,
  67. "The allocator shim doesn't work for the component build on Windows.")
  68. }
  69. _is_brp_supported = (is_win || is_android || is_linux || is_mac ||
  70. is_chromeos) && use_allocator == "partition"
  71. _is_mcp_supported = is_win && use_allocator == "partition"
  72. declare_args() {
  73. # We jam MTECheckedPtr off by default, but can set it to
  74. # `_is_mcp_supported` to activate it.
  75. use_mte_checked_ptr = false
  76. }
  77. declare_args() {
  78. # Set use_backup_ref_ptr true to use BackupRefPtr (BRP) as the implementation
  79. # of raw_ptr<T>, and enable PartitionAlloc support for it.
  80. # We also disable BRP in the presence of MTECheckedPtr, which is almost
  81. # never enabled.
  82. use_backup_ref_ptr = _is_brp_supported && !use_mte_checked_ptr
  83. }
  84. assert(!(use_backup_ref_ptr && use_mte_checked_ptr),
  85. "MTECheckedPtr conflicts with BRP.")
  86. declare_args() {
  87. # If BRP is enabled, additional options are available:
  88. # - put_ref_count_in_previous_slot: place the ref-count at the end of the
  89. # previous slot (or in metadata if a slot starts on the page boundary), as
  90. # opposed to the beginning of the slot.
  91. # - enable_backup_ref_ptr_slow_checks: enable additional safety checks that
  92. # are too expensive to have on by default.
  93. # - enable_dangling_raw_ptr_checks: enable checking raw_ptr do not become
  94. # dangling during their lifetime.
  95. put_ref_count_in_previous_slot = use_backup_ref_ptr
  96. enable_backup_ref_ptr_slow_checks = false
  97. enable_dangling_raw_ptr_checks = false
  98. # The supported platforms are supposed to match `_is_brp_supported`, but we
  99. # enable the feature on Linux early because it's most widely used for security
  100. # research
  101. use_asan_backup_ref_ptr = is_asan && (is_win || is_android || is_linux)
  102. }
  103. # Prevent using BackupRefPtr when PartitionAlloc-Everywhere isn't used.
  104. # In theory, such a configuration is possible, but its scope would be limited to
  105. # only Blink partitions, which is currently not tested. Better to trigger an
  106. # error, than have BackupRefPtr silently disabled while believing it is enabled.
  107. if (!is_nacl) {
  108. assert(!use_backup_ref_ptr || use_allocator == "partition",
  109. "Can't use BackupRefPtr without PartitionAlloc-Everywhere")
  110. }
  111. # put_ref_count_in_previous_slot can only be used if use_backup_ref_ptr
  112. # is true.
  113. assert(
  114. use_backup_ref_ptr || !put_ref_count_in_previous_slot,
  115. "Can't put ref count in the previous slot if BackupRefPtr isn't enabled at all")
  116. # enable_backup_ref_ptr_slow_checks can only be used if use_backup_ref_ptr
  117. # is true.
  118. assert(use_backup_ref_ptr || !enable_backup_ref_ptr_slow_checks,
  119. "Can't enable additional BackupRefPtr checks if it isn't enabled at all")
  120. # enable_dangling_raw_ptr_checks can only be used if use_backup_ref_ptr
  121. # is true.
  122. assert(
  123. use_backup_ref_ptr || !enable_dangling_raw_ptr_checks,
  124. "Can't enable dangling raw_ptr checks if BackupRefPtr isn't enabled at all")
  125. # BackupRefPtr and AsanBackupRefPtr are mutually exclusive variants of raw_ptr.
  126. assert(
  127. !use_backup_ref_ptr || !use_asan_backup_ref_ptr,
  128. "Both BackupRefPtr and AsanBackupRefPtr can't be enabled at the same time")
  129. assert(!use_asan_backup_ref_ptr || is_asan,
  130. "AsanBackupRefPtr requires AddressSanitizer")