diff --git a/base/BUILD.gn b/base/BUILD.gn index f14e8b5532dd8..83cdc61ae5ac4 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -1181,6 +1181,7 @@ component("base") { "allocator/early_zone_registration_mac.h", "allocator/partition_allocator/shim/allocator_shim_override_mac_default_zone.h", "allocator/partition_allocator/shim/allocator_shim_override_mac_symbols.h", + "allocator/partition_allocator/shim/early_zone_registration_constants.h", ] configs += [ "//base/allocator:mac_no_default_new_delete_symbols" ] } diff --git a/base/allocator/BUILD.gn b/base/allocator/BUILD.gn index 76023f6eac11d..3e5b1da6b86e4 100644 --- a/base/allocator/BUILD.gn +++ b/base/allocator/BUILD.gn @@ -26,6 +26,7 @@ if (is_apple) { sources = [ "early_zone_registration_mac.cc", "early_zone_registration_mac.h", + "partition_allocator/shim/early_zone_registration_constants.h", ] deps = [ diff --git a/base/allocator/early_zone_registration_mac.cc b/base/allocator/early_zone_registration_mac.cc index 5cf4e92ab1813..1c75bc92e7dbc 100644 --- a/base/allocator/early_zone_registration_mac.cc +++ b/base/allocator/early_zone_registration_mac.cc @@ -8,6 +8,7 @@ #include <malloc/malloc.h> #include "base/allocator/partition_allocator/partition_alloc_buildflags.h" +#include "base/allocator/partition_allocator/shim/early_zone_registration_constants.h" // BASE_EXPORT tends to be defined as soon as anything from //base is included. #if defined(BASE_EXPORT) @@ -208,11 +209,11 @@ void EarlyMallocZoneRegistration() { // Could use something lower to support fewer functions, but this is // consistent with the real zone installed by PartitionAlloc. - g_delegating_zone.version = kZoneVersion; + g_delegating_zone.version = allocator_shim::kZoneVersion; g_delegating_zone.introspect = &g_delegating_zone_introspect; // This name is used in PartitionAlloc's initialization to determine whether // it should replace the delegating zone. - g_delegating_zone.zone_name = kDelegatingZoneName; + g_delegating_zone.zone_name = allocator_shim::kDelegatingZoneName; // Register puts the new zone at the end, unregister swaps the new zone with // the last one. @@ -254,7 +255,7 @@ void AllowDoublePartitionAllocZoneRegistration() { for (unsigned int i = 0; i < zone_count; i++) { malloc_zone_t* zone = reinterpret_cast<malloc_zone_t*>(zones[i]); if (zone->zone_name && - strcmp(zone->zone_name, kPartitionAllocZoneName) == 0) { + strcmp(zone->zone_name, allocator_shim::kPartitionAllocZoneName) == 0) { zone->zone_name = "RenamedPartitionAlloc"; break; } diff --git a/base/allocator/early_zone_registration_mac.h b/base/allocator/early_zone_registration_mac.h index c8eeb86814f82..fadcf31e8d3d3 100644 --- a/base/allocator/early_zone_registration_mac.h +++ b/base/allocator/early_zone_registration_mac.h @@ -10,22 +10,6 @@ namespace partition_alloc { -static constexpr char kDelegatingZoneName[] = - "DelegatingDefaultZoneForPartitionAlloc"; -static constexpr char kPartitionAllocZoneName[] = "PartitionAlloc"; - -// Zone version. Determines which callbacks are set in the various malloc_zone_t -// structs. -#if (__MAC_OS_X_VERSION_MAX_ALLOWED >= 130000) || \ - (__IPHONE_OS_VERSION_MAX_ALLOWED >= 160100) -#define PA_TRY_FREE_DEFAULT_IS_AVAILABLE 1 -#endif -#if PA_TRY_FREE_DEFAULT_IS_AVAILABLE -constexpr int kZoneVersion = 13; -#else -constexpr int kZoneVersion = 9; -#endif - // Must be called *once*, *before* the process becomes multi-threaded. void EarlyMallocZoneRegistration(); diff --git a/base/allocator/partition_allocator/shim/DEPS b/base/allocator/partition_allocator/shim/DEPS index d1f3816f7b192..c26088ef977f6 100644 --- a/base/allocator/partition_allocator/shim/DEPS +++ b/base/allocator/partition_allocator/shim/DEPS @@ -7,7 +7,6 @@ # migration gets done. include_rules = [ - "+base/allocator/early_zone_registration_mac.h", "+base/base_export.h", ] diff --git a/base/allocator/partition_allocator/shim/allocator_shim_override_mac_default_zone.h b/base/allocator/partition_allocator/shim/allocator_shim_override_mac_default_zone.h index 144542a34dad3..cfa2742a54d93 100644 --- a/base/allocator/partition_allocator/shim/allocator_shim_override_mac_default_zone.h +++ b/base/allocator/partition_allocator/shim/allocator_shim_override_mac_default_zone.h @@ -18,13 +18,13 @@ #include <atomic> #include <tuple> -#include "base/allocator/early_zone_registration_mac.h" #include "base/allocator/partition_allocator/partition_alloc_base/bits.h" #include "base/allocator/partition_allocator/partition_alloc_base/logging.h" #include "base/allocator/partition_allocator/partition_alloc_base/mac/mach_logging.h" #include "base/allocator/partition_allocator/partition_alloc_buildflags.h" #include "base/allocator/partition_allocator/partition_alloc_check.h" #include "base/allocator/partition_allocator/partition_alloc_constants.h" +#include "base/allocator/partition_allocator/shim/early_zone_registration_constants.h" namespace partition_alloc { @@ -261,8 +261,7 @@ bool IsAlreadyRegistered() { // strcmp() and not a pointer comparison, as the zone was registered from // another library, the pointers don't match. if (zone->zone_name && - (strcmp(zone->zone_name, partition_alloc::kPartitionAllocZoneName) == - 0)) { + (strcmp(zone->zone_name, kPartitionAllocZoneName) == 0)) { // This zone is provided by PartitionAlloc, so this function has been // called from another library (or the main executable), nothing to do. // @@ -309,8 +308,8 @@ void InitializeZone() { // version >= 11: introspect.print_task is supported // version >= 12: introspect.task_statistics is supported // version >= 13: try_free_default is supported - g_mac_malloc_zone.version = partition_alloc::kZoneVersion; - g_mac_malloc_zone.zone_name = partition_alloc::kPartitionAllocZoneName; + g_mac_malloc_zone.version = kZoneVersion; + g_mac_malloc_zone.zone_name = kPartitionAllocZoneName; g_mac_malloc_zone.introspect = &g_mac_malloc_introspection; g_mac_malloc_zone.size = MallocZoneSize; g_mac_malloc_zone.malloc = MallocZoneMalloc; @@ -365,8 +364,7 @@ InitializeDefaultMallocZoneWithPartitionAlloc() { InitializeZone(); malloc_zone_t* system_default_zone = GetDefaultMallocZone(); - if (strcmp(system_default_zone->zone_name, - partition_alloc::kDelegatingZoneName) == 0) { + if (strcmp(system_default_zone->zone_name, kDelegatingZoneName) == 0) { // The first zone is our zone, we can unregister it, replacing it with the // new one. This relies on a precise zone setup, done in // |EarlyMallocZoneRegistration()|. diff --git a/base/allocator/partition_allocator/shim/early_zone_registration_constants.h b/base/allocator/partition_allocator/shim/early_zone_registration_constants.h new file mode 100644 index 0000000000000..25fcfb36191e0 --- /dev/null +++ b/base/allocator/partition_allocator/shim/early_zone_registration_constants.h @@ -0,0 +1,33 @@ +// Copyright 2023 The Chromium Authors +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_EARLY_ZONE_REGISTRATION_CONSTANTS_H_ +#define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_EARLY_ZONE_REGISTRATION_CONSTANTS_H_ + +// This is an Apple-only file, used to register PartitionAlloc's zone *before* +// the process becomes multi-threaded. These constants are shared between the +// allocator shim which installs the PartitionAlloc's malloc zone and the +// application which installs the "early malloc zone" to reserve the zone slot. + +namespace allocator_shim { + +static constexpr char kDelegatingZoneName[] = + "DelegatingDefaultZoneForPartitionAlloc"; +static constexpr char kPartitionAllocZoneName[] = "PartitionAlloc"; + +// Zone version. Determines which callbacks are set in the various malloc_zone_t +// structs. +#if (__MAC_OS_X_VERSION_MAX_ALLOWED >= 130000) || \ + (__IPHONE_OS_VERSION_MAX_ALLOWED >= 160100) +#define PA_TRY_FREE_DEFAULT_IS_AVAILABLE 1 +#endif +#if PA_TRY_FREE_DEFAULT_IS_AVAILABLE +constexpr int kZoneVersion = 13; +#else +constexpr int kZoneVersion = 9; +#endif + +} // namespace allocator_shim + +#endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SHIM_EARLY_ZONE_REGISTRATION_CONSTANTS_H_