Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 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 | |
Rob Hueber | 336a5ae | 2019-09-10 21:43:05 | [diff] [blame] | 5 | import("//build/config/chromecast_build.gni") |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 6 | import("//build/config/sanitizers/sanitizers.gni") |
| 7 | |
Ali Juma | 50a1938 | 2021-09-20 21:25:23 | [diff] [blame] | 8 | if (is_ios) { |
| 9 | import("//build/config/ios/ios_sdk.gni") |
| 10 | } |
| 11 | |
Benoit Lize | 88016fd | 2021-01-13 16:25:11 | [diff] [blame] | 12 | # Sanitizers replace the allocator, don't use our own. |
| 13 | _is_using_sanitizers = is_asan || is_hwasan || is_lsan || is_tsan || is_msan |
| 14 | |
Benoit Lize | 1ac7ce7 | 2021-07-01 10:59:55 | [diff] [blame] | 15 | # - Component build support is disabled on all platforms. It is known to cause |
| 16 | # issues on some (e.g. Windows with shims, Android with non-universal symbol |
| 17 | # wrapping), and has not been validated on others. |
| 18 | # - Windows: debug CRT is not compatible, see below. |
Benoit Lize | c74f98f | 2021-10-25 13:15:29 | [diff] [blame] | 19 | _disable_partition_alloc = is_component_build || (is_win && is_debug) |
Benoit Lize | ec6b52f | 2021-11-18 19:54:17 | [diff] [blame] | 20 | _is_partition_alloc_platform = |
Kevin Marshall | 1e07824 | 2022-01-20 12:10:12 | [diff] [blame] | 21 | is_android || is_win || is_mac || is_linux || is_chromeos || is_fuchsia |
Benoit Lize | b30fcd4 | 2021-01-15 13:44:12 | [diff] [blame] | 22 | |
Kevin Marshall | 7a835b4 | 2022-01-08 23:17:11 | [diff] [blame] | 23 | # Under Windows Debug the allocator shim is not compatible with CRT. |
| 24 | # NaCl in particular does seem to link some binaries statically |
| 25 | # against the debug CRT with "is_nacl=false". |
| 26 | # Under Fuchsia the allocator shim is only required for PA-E. |
| 27 | # For all other platforms & configurations, the shim is required, to replace |
| 28 | # the default system allocators, e.g. with tcmalloc. |
Kunihiko Sakamoto | 466878a4 | 2021-11-24 07:49:01 | [diff] [blame] | 29 | if ((is_linux || is_chromeos || is_android || is_apple || |
Kevin Marshall | 248fefc3 | 2022-01-10 20:45:30 | [diff] [blame] | 30 | (is_fuchsia && _is_partition_alloc_platform && !is_component_build) || |
Benoit Lize | 88016fd | 2021-01-13 16:25:11 | [diff] [blame] | 31 | (is_win && !is_component_build && !is_debug)) && !_is_using_sanitizers) { |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 32 | _default_use_allocator_shim = true |
| 33 | } else { |
| 34 | _default_use_allocator_shim = false |
| 35 | } |
| 36 | |
Benoit Lize | 060ef06 | 2021-02-01 11:41:46 | [diff] [blame] | 37 | if (_default_use_allocator_shim && _is_partition_alloc_platform && |
Benoit Lize | b30fcd4 | 2021-01-15 13:44:12 | [diff] [blame] | 38 | !_disable_partition_alloc) { |
Yuki Shiino | d476ae303 | 2020-11-18 05:56:23 | [diff] [blame] | 39 | _default_allocator = "partition" |
Lan Wei | db909c37 | 2021-09-10 17:34:09 | [diff] [blame] | 40 | } else { |
Benoit L | d4000aec | 2021-09-15 13:23:26 | [diff] [blame] | 41 | _default_allocator = "none" |
Yuki Shiino | a17905e3 | 2020-10-09 18:04:59 | [diff] [blame] | 42 | } |
| 43 | |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 44 | declare_args() { |
| 45 | # Memory allocator to use. Set to "none" to use default allocator. |
| 46 | use_allocator = _default_allocator |
| 47 | |
| 48 | # Causes all the allocations to be routed via allocator_shim.cc. |
| 49 | use_allocator_shim = _default_use_allocator_shim |
Yuki Shiino | 24622fa | 2020-08-26 12:01:02 | [diff] [blame] | 50 | |
Yuki Shiino | 90d5fe68 | 2020-09-02 11:03:51 | [diff] [blame] | 51 | # Whether PartitionAlloc should be available for use or not. |
| 52 | # true makes PartitionAlloc linked to the executable or shared library and |
| 53 | # makes it available for use, but it doesn't mean that the default allocator |
| 54 | # is PartitionAlloc. PartitionAlloc may or may not be the default allocator. |
| 55 | # |
| 56 | # |use_allocator = "partition"| makes PartitionAlloc the default allocator |
| 57 | # but it's effective only when |use_partition_alloc = true|. |
| 58 | # |
| 59 | # TODO(lizeb, yukishiino): Determine if |use_partition_alloc| is necessary or |
| 60 | # not, and redesign or remove the flag accordingly. We may want to assert a |
| 61 | # possible conflict between |use_allocator = "partition"| and |
| 62 | # |use_partition_alloc = true| rather than prioritizing use_partition_alloc. |
Ali Juma | 50a1938 | 2021-09-20 21:25:23 | [diff] [blame] | 63 | # TODO(crbug.com/1250788): Enable use_partition_alloc on iOS device builds. |
| 64 | use_partition_alloc = !is_ios || target_environment == "simulator" |
Yuki Shiino | 90d5fe68 | 2020-09-02 11:03:51 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (!use_partition_alloc && use_allocator == "partition") { |
| 68 | # If there is a conflict, prioritize |use_partition_alloc| over |
| 69 | # |use_allocator|. |
| 70 | use_allocator = "none" |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 71 | } |
| 72 | |
Benoit Lize | f4e1472 | 2022-01-12 10:56:52 | [diff] [blame] | 73 | # TODO(crbug.com/1257213): Completely remove tcmalloc and related files. |
| 74 | assert(use_allocator == "none" || use_allocator == "partition") |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 75 | |
Rohit Rao | 849b426 | 2020-05-01 19:10:34 | [diff] [blame] | 76 | assert( |
Yuki Shiino | 24622fa | 2020-08-26 12:01:02 | [diff] [blame] | 77 | !use_allocator_shim || is_linux || is_chromeos || is_android || is_win || |
Kevin Marshall | 64a82a9 | 2021-09-08 20:49:45 | [diff] [blame] | 78 | is_fuchsia || is_apple, |
| 79 | "use_allocator_shim works only on Android, iOS, Linux, macOS, Fuchsia, " + |
| 80 | "and Windows.") |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 81 | |
Moe Ahmadi | a36d801a | 2021-08-30 22:30:53 | [diff] [blame] | 82 | if (is_win && use_allocator_shim) { |
Kevin Marshall | 64a82a9 | 2021-09-08 20:49:45 | [diff] [blame] | 83 | # TODO(crbug.com/1245317): Add a comment indicating why the shim doesn't work. |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 84 | assert(!is_component_build, |
Moe Ahmadi | a36d801a | 2021-08-30 22:30:53 | [diff] [blame] | 85 | "The allocator shim doesn't work for the component build on Windows.") |
Nico Weber | b80b085 | 2019-08-01 22:03:01 | [diff] [blame] | 86 | } |
Bartek Nowierski | ceb2fb3 | 2021-05-26 15:09:05 | [diff] [blame] | 87 | |
Keishi Hattori | 122e7a0 | 2021-11-28 11:51:01 | [diff] [blame] | 88 | _is_brp_supported = (is_win || is_android) && use_allocator == "partition" |
Keishi Hattori | 01d5ac8fe | 2021-11-26 23:59:14 | [diff] [blame] | 89 | |
| 90 | declare_args() { |
| 91 | # Set use_backup_ref_ptr true to use BackupRefPtr (BRP) as the implementation |
| 92 | # of raw_ptr<T>, and enable PartitionAlloc support for it. |
| 93 | use_backup_ref_ptr = _is_brp_supported |
| 94 | |
| 95 | # If BRP is enabled, additional options are available: |
| 96 | # - put_ref_count_in_previous_slot: place the ref-count at the end of the |
| 97 | # previous slot (or in metadata if a slot starts on the page boundary), as |
| 98 | # opposed to the beginning of the slot. |
| 99 | # - never_remove_from_brp_pool_blocklist: never remove super-pages from the |
| 100 | # BRP-pool block list |
| 101 | # - enable_backup_ref_ptr_slow_checks: enable additional safety checks that |
| 102 | # are too expensive to have on by default. |
Bartek Nowierski | 0bd8f5d | 2021-12-15 02:19:37 | [diff] [blame] | 103 | put_ref_count_in_previous_slot = _is_brp_supported |
Keishi Hattori | 01d5ac8fe | 2021-11-26 23:59:14 | [diff] [blame] | 104 | never_remove_from_brp_pool_blocklist = _is_brp_supported |
Keishi Hattori | 14dcefd | 2021-11-28 10:46:22 | [diff] [blame] | 105 | enable_backup_ref_ptr_slow_checks = false |
Keishi Hattori | 01d5ac8fe | 2021-11-26 23:59:14 | [diff] [blame] | 106 | |
| 107 | # Registers the binary for a fake binary A/B experiment. The binaries built |
| 108 | # with this flag have no behavior difference, except for setting a synthetic |
| 109 | # Finch. |
| 110 | use_fake_binary_experiment = false |
Sergei Glazunov | 017ec4e8 | 2022-01-26 11:50:49 | [diff] [blame^] | 111 | |
| 112 | use_asan_backup_ref_ptr = false |
Keishi Hattori | 01d5ac8fe | 2021-11-26 23:59:14 | [diff] [blame] | 113 | } |
| 114 | |
Bartek Nowierski | ceb2fb3 | 2021-05-26 15:09:05 | [diff] [blame] | 115 | # Prevent using BackupRefPtr when PartitionAlloc-Everywhere isn't used. |
| 116 | # In theory, such a configuration is possible, but its scope would be limited to |
| 117 | # only Blink partitions, which is currently not tested. Better to trigger an |
| 118 | # error, than have BackupRefPtr silently disabled while believing it is enabled. |
| 119 | if (!is_nacl) { |
| 120 | assert(!use_backup_ref_ptr || use_allocator == "partition", |
| 121 | "Can't use BackupRefPtr without PartitionAlloc-Everywhere") |
| 122 | } |
| 123 | |
Bartek Nowierski | ceb2fb3 | 2021-05-26 15:09:05 | [diff] [blame] | 124 | # put_ref_count_in_previous_slot can only be used if use_backup_ref_ptr |
| 125 | # is true. |
| 126 | assert( |
| 127 | use_backup_ref_ptr || !put_ref_count_in_previous_slot, |
| 128 | "Can't put ref count in the previous slot if BackupRefPtr isn't enabled at all") |
| 129 | |
Bartek Nowierski | 5390439 | 2021-07-14 06:55:13 | [diff] [blame] | 130 | # never_remove_from_brp_pool_blocklist can only be used if use_backup_ref_ptr |
Bartek Nowierski | ceb2fb3 | 2021-05-26 15:09:05 | [diff] [blame] | 131 | # is true. |
| 132 | assert( |
Bartek Nowierski | 5390439 | 2021-07-14 06:55:13 | [diff] [blame] | 133 | use_backup_ref_ptr || !never_remove_from_brp_pool_blocklist, |
| 134 | "never_remove_from_brp_pool_blocklist requires BackupRefPtr to be enabled") |
Bartek Nowierski | ceb2fb3 | 2021-05-26 15:09:05 | [diff] [blame] | 135 | |
| 136 | # enable_backup_ref_ptr_slow_checks can only be used if use_backup_ref_ptr |
| 137 | # is true. |
| 138 | assert(use_backup_ref_ptr || !enable_backup_ref_ptr_slow_checks, |
| 139 | "Can't enable additional BackupRefPtr checks if it isn't enabled at all") |
Sergei Glazunov | 017ec4e8 | 2022-01-26 11:50:49 | [diff] [blame^] | 140 | |
| 141 | # BackupRefPtr and AsanBackupRefPtr are mutually exclusive variants of raw_ptr. |
| 142 | assert( |
| 143 | !use_backup_ref_ptr || !use_asan_backup_ref_ptr, |
| 144 | "Both BackupRefPtr and AsanBackupRefPtr can't be enabled at the same time") |
| 145 | |
| 146 | assert(!use_asan_backup_ref_ptr || is_asan, |
| 147 | "AsanBackupRefPtr requires AddressSanitizer") |