|
| 1 | +/* |
| 2 | +* Copyright (C) 2019-2022 The Kraken authors. All rights reserved. |
| 3 | +* Copyright (C) 2022-present The WebF authors. All rights reserved. |
| 4 | +*/ |
| 5 | + |
| 6 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_alloc.h" |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | +#include <cstring> |
| 10 | +#include <memory> |
| 11 | + |
| 12 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/address_pool_manager.h" |
| 13 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/memory_reclaimer.h" |
| 14 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_address_space.h" |
| 15 | +//#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_base/debug/debugging_buildflags.h" |
| 16 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_buildflags.h" |
| 17 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_alloc_hooks.h" |
| 18 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_direct_map_extent.h" |
| 19 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_oom.h" |
| 20 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_page.h" |
| 21 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_root.h" |
| 22 | +#include "bindings/v8/base/allocator/partition_allocator/src/partition_alloc/partition_stats.h" |
| 23 | + |
| 24 | +#if PA_BUILDFLAG(USE_STARSCAN) |
| 25 | +#include "partition_alloc/starscan/pcscan.h" |
| 26 | +#endif |
| 27 | + |
| 28 | +namespace partition_alloc { |
| 29 | + |
| 30 | +void PartitionAllocGlobalInit(OomFunction on_out_of_memory) { |
| 31 | + // This is from page_allocator_constants.h and doesn't really fit here, but |
| 32 | + // there isn't a centralized initialization function in page_allocator.cc, so |
| 33 | + // there's no good place in that file to do a STATIC_ASSERT_OR_PA_CHECK. |
| 34 | + STATIC_ASSERT_OR_PA_CHECK( |
| 35 | + (internal::SystemPageSize() & internal::SystemPageOffsetMask()) == 0, |
| 36 | + "SystemPageSize() must be power of 2"); |
| 37 | + |
| 38 | + // Two partition pages are used as guard / metadata page so make sure the |
| 39 | + // super page size is bigger. |
| 40 | + STATIC_ASSERT_OR_PA_CHECK( |
| 41 | + internal::PartitionPageSize() * 4 <= internal::kSuperPageSize, |
| 42 | + "ok super page size"); |
| 43 | + STATIC_ASSERT_OR_PA_CHECK( |
| 44 | + (internal::kSuperPageSize & internal::SystemPageOffsetMask()) == 0, |
| 45 | + "ok super page multiple"); |
| 46 | + // Four system pages gives us room to hack out a still-guard-paged piece |
| 47 | + // of metadata in the middle of a guard partition page. |
| 48 | + STATIC_ASSERT_OR_PA_CHECK( |
| 49 | + internal::SystemPageSize() * 4 <= internal::PartitionPageSize(), |
| 50 | + "ok partition page size"); |
| 51 | + STATIC_ASSERT_OR_PA_CHECK( |
| 52 | + (internal::PartitionPageSize() & internal::SystemPageOffsetMask()) == 0, |
| 53 | + "ok partition page multiple"); |
| 54 | + static_assert( |
| 55 | + sizeof(internal::PartitionPageMetadata) <= internal::kPageMetadataSize, |
| 56 | + "PartitionPage should not be too big"); |
| 57 | + STATIC_ASSERT_OR_PA_CHECK( |
| 58 | + internal::kPageMetadataSize * internal::NumPartitionPagesPerSuperPage() <= |
| 59 | + internal::SystemPageSize(), |
| 60 | + "page metadata fits in hole"); |
| 61 | + |
| 62 | + // Limit to prevent callers accidentally overflowing an int size. |
| 63 | + STATIC_ASSERT_OR_PA_CHECK( |
| 64 | + internal::MaxDirectMapped() <= |
| 65 | + (1UL << 31) + internal::DirectMapAllocationGranularity(), |
| 66 | + "maximum direct mapped allocation"); |
| 67 | + |
| 68 | + // Check that some of our zanier calculations worked out as expected. |
| 69 | + static_assert(internal::kSmallestBucket == internal::kAlignment, |
| 70 | + "generic smallest bucket"); |
| 71 | + static_assert(internal::kMaxBucketed == 983040, "generic max bucketed"); |
| 72 | + STATIC_ASSERT_OR_PA_CHECK( |
| 73 | + internal::MaxSystemPagesPerRegularSlotSpan() <= 16, |
| 74 | + "System pages per slot span must be no greater than 16."); |
| 75 | + |
| 76 | +#if PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT) |
| 77 | + STATIC_ASSERT_OR_PA_CHECK( |
| 78 | + internal::GetInSlotMetadataIndexMultiplierShift() < |
| 79 | + std::numeric_limits<size_t>::max() / 2, |
| 80 | + "Calculation in GetInSlotMetadataIndexMultiplierShift() must not " |
| 81 | + "underflow."); |
| 82 | + // Check that the GetInSlotMetadataIndexMultiplierShift() calculation is |
| 83 | + // correct. |
| 84 | + STATIC_ASSERT_OR_PA_CHECK( |
| 85 | + (1 << internal::GetInSlotMetadataIndexMultiplierShift()) == |
| 86 | + (internal::SystemPageSize() / |
| 87 | + (sizeof(internal::InSlotMetadata) * |
| 88 | + (internal::kSuperPageSize / internal::SystemPageSize()))), |
| 89 | + "Bitshift must match the intended multiplication."); |
| 90 | + STATIC_ASSERT_OR_PA_CHECK( |
| 91 | + ((sizeof(internal::InSlotMetadata) * |
| 92 | + (internal::kSuperPageSize / internal::SystemPageSize())) |
| 93 | + << internal::GetInSlotMetadataIndexMultiplierShift()) <= |
| 94 | + internal::SystemPageSize(), |
| 95 | + "InSlotMetadata table size must be smaller than or equal to " |
| 96 | + "<= SystemPageSize()."); |
| 97 | +#endif // PA_BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT) |
| 98 | + |
| 99 | + PA_DCHECK(on_out_of_memory); |
| 100 | + internal::g_oom_handling_function = on_out_of_memory; |
| 101 | +} |
| 102 | + |
| 103 | +void PartitionAllocGlobalUninitForTesting() { |
| 104 | +#if PA_BUILDFLAG(ENABLE_THREAD_ISOLATION) |
| 105 | + internal::PartitionAddressSpace::UninitThreadIsolatedPoolForTesting(); |
| 106 | +#endif |
| 107 | + internal::g_oom_handling_function = nullptr; |
| 108 | +} |
| 109 | + |
| 110 | +PartitionAllocator::PartitionAllocator() = default; |
| 111 | + |
| 112 | +PartitionAllocator::~PartitionAllocator() { |
| 113 | + MemoryReclaimer::Instance()->UnregisterPartition(&partition_root_); |
| 114 | +} |
| 115 | + |
| 116 | +void PartitionAllocator::init(PartitionOptions opts) { |
| 117 | +#if PA_BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) |
| 118 | + PA_CHECK(opts.thread_cache == PartitionOptions::kDisabled) |
| 119 | + << "Cannot use a thread cache when PartitionAlloc is malloc()."; |
| 120 | +#endif |
| 121 | + partition_root_.Init(opts); |
| 122 | +#if PA_BUILDFLAG(ENABLE_THREAD_ISOLATION) |
| 123 | + // The MemoryReclaimer won't have write access to the partition, so skip |
| 124 | + // registration. |
| 125 | + const bool use_memory_reclaimer = !opts.thread_isolation.enabled; |
| 126 | +#else |
| 127 | + constexpr bool use_memory_reclaimer = true; |
| 128 | +#endif |
| 129 | + if (use_memory_reclaimer) { |
| 130 | + MemoryReclaimer::Instance()->RegisterPartition(&partition_root_); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace partition_alloc |
| 135 | + |
0 commit comments