Skip to content

Commit

Permalink
[PA] Namespace migration (34-omake of N)
Browse files Browse the repository at this point in the history
This change supports the PartitionAlloc namespace migration by cutting
some non-PA dependencies on PA internals. In particular, the use of
PA-internal page size is substituted with a `base::` analogue.

I could not find an analogue for `PageAllocationGranularity()`, so the
PA-internal usage is retained.

Bug: 1288247
Change-Id: Ief157fed5b7aaf55801fa19fec27412c52105b21
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3582438
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: danakj <danakj@chromium.org>
Commit-Queue: Kalvin Lee <kdlee@chromium.org>
Reviewed-by: Benoit Lize <lizeb@chromium.org>
Reviewed-by: Yuki Shiino <yukishiino@chromium.org>
Cr-Commit-Position: refs/heads/main@{#998650}
  • Loading branch information
Kalvin Lee authored and Chromium LUCI CQ committed May 3, 2022
1 parent f1e9199 commit 8adb726
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
65 changes: 32 additions & 33 deletions base/mac/scoped_mach_vm_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <mach/mach.h>

#include "base/allocator/partition_allocator/page_allocator_constants.h"
#include "base/memory/page_size.h"
#include "base/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand All @@ -32,7 +32,7 @@ void GetRegionInfo(vm_address_t* region_address, vm_size_t* region_size) {

TEST(ScopedMachVMTest, Basic) {
vm_address_t address;
vm_size_t size = base::SystemPageSize();
vm_size_t size = base::GetPageSize();
kern_return_t kr =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
ASSERT_EQ(KERN_SUCCESS, kr);
Expand All @@ -47,7 +47,7 @@ TEST(ScopedMachVMTest, Basic) {
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(KERN_SUCCESS, kr);
EXPECT_EQ(address, region_address);
EXPECT_EQ(1u * base::SystemPageSize(), region_size);
EXPECT_EQ(1u * base::GetPageSize(), region_size);

{
ScopedMachVM scoper2;
Expand All @@ -72,7 +72,7 @@ TEST(ScopedMachVMTest, Basic) {

TEST(ScopedMachVMTest, Reset) {
vm_address_t address;
vm_size_t size = base::SystemPageSize();
vm_size_t size = base::GetPageSize();
kern_return_t kr =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
ASSERT_EQ(KERN_SUCCESS, kr);
Expand All @@ -85,7 +85,7 @@ TEST(ScopedMachVMTest, Reset) {
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(KERN_SUCCESS, kr);
EXPECT_EQ(address, region_address);
EXPECT_EQ(1u * base::SystemPageSize(), region_size);
EXPECT_EQ(1u * base::GetPageSize(), region_size);

scoper.reset();

Expand All @@ -98,35 +98,35 @@ TEST(ScopedMachVMTest, Reset) {

TEST(ScopedMachVMTest, ResetSmallerAddress) {
vm_address_t address;
vm_size_t size = 2 * base::SystemPageSize();
vm_size_t size = 2 * base::GetPageSize();
kern_return_t kr =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
ASSERT_EQ(KERN_SUCCESS, kr);

ScopedMachVM scoper(address, base::SystemPageSize());
ScopedMachVM scoper(address, base::GetPageSize());

// Test the initial region.
vm_address_t region_address = address;
vm_size_t region_size;
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(KERN_SUCCESS, kr);
EXPECT_EQ(address, region_address);
EXPECT_EQ(2u * base::SystemPageSize(), region_size);
EXPECT_EQ(2u * base::GetPageSize(), region_size);

// This will free address..base::SystemPageSize() that is currently in the
// This will free address..base::GetPageSize() that is currently in the
// scoper.
scoper.reset(address + base::SystemPageSize(), base::SystemPageSize());
scoper.reset(address + base::GetPageSize(), base::GetPageSize());

// Verify that the region is now only one page.
region_address = address;
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(address + base::SystemPageSize(), region_address);
EXPECT_EQ(1u * base::SystemPageSize(), region_size);
EXPECT_EQ(address + base::GetPageSize(), region_address);
EXPECT_EQ(1u * base::GetPageSize(), region_size);
}

TEST(ScopedMachVMTest, ResetLargerAddressAndSize) {
vm_address_t address;
vm_size_t size = 3 * base::SystemPageSize();
vm_size_t size = 3 * base::GetPageSize();
kern_return_t kr =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
ASSERT_EQ(KERN_SUCCESS, kr);
Expand All @@ -137,23 +137,22 @@ TEST(ScopedMachVMTest, ResetLargerAddressAndSize) {
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(KERN_SUCCESS, kr);
EXPECT_EQ(address, region_address);
EXPECT_EQ(3u * base::SystemPageSize(), region_size);
EXPECT_EQ(3u * base::GetPageSize(), region_size);

ScopedMachVM scoper(address + 2 * base::SystemPageSize(),
base::SystemPageSize());
ScopedMachVM scoper(address + 2 * base::GetPageSize(), base::GetPageSize());
// Expand the region to be larger.
scoper.reset(address, size);

// Verify that the region is still three pages.
region_address = address;
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(address, region_address);
EXPECT_EQ(3u * base::SystemPageSize(), region_size);
EXPECT_EQ(3u * base::GetPageSize(), region_size);
}

TEST(ScopedMachVMTest, ResetLargerAddress) {
vm_address_t address;
vm_size_t size = 6 * base::SystemPageSize();
vm_size_t size = 6 * base::GetPageSize();
kern_return_t kr =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
ASSERT_EQ(KERN_SUCCESS, kr);
Expand All @@ -164,25 +163,25 @@ TEST(ScopedMachVMTest, ResetLargerAddress) {
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(KERN_SUCCESS, kr);
EXPECT_EQ(address, region_address);
EXPECT_EQ(6u * base::SystemPageSize(), region_size);
EXPECT_EQ(6u * base::GetPageSize(), region_size);

ScopedMachVM scoper(address + 3 * base::SystemPageSize(),
3 * base::SystemPageSize());
ScopedMachVM scoper(address + 3 * base::GetPageSize(),
3 * base::GetPageSize());

// Shift the region by three pages; the last three pages should be
// deallocated, while keeping the first three.
scoper.reset(address, 3 * base::SystemPageSize());
scoper.reset(address, 3 * base::GetPageSize());

// Verify that the region is just three pages.
region_address = address;
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(address, region_address);
EXPECT_EQ(3u * base::SystemPageSize(), region_size);
EXPECT_EQ(3u * base::GetPageSize(), region_size);
}

TEST(ScopedMachVMTest, ResetUnaligned) {
vm_address_t address;
vm_size_t size = 2 * base::SystemPageSize();
vm_size_t size = 2 * base::GetPageSize();
kern_return_t kr =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
ASSERT_EQ(KERN_SUCCESS, kr);
Expand All @@ -194,36 +193,36 @@ TEST(ScopedMachVMTest, ResetUnaligned) {
vm_size_t region_size;
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(address, region_address);
EXPECT_EQ(2u * base::SystemPageSize(), region_size);
EXPECT_EQ(2u * base::GetPageSize(), region_size);

// Initialize with unaligned size.
scoper.reset_unaligned(address + base::SystemPageSize(),
base::SystemPageSize() - 3);
scoper.reset_unaligned(address + base::GetPageSize(),
base::GetPageSize() - 3);
// Reset with another unaligned size.
scoper.reset_unaligned(address + base::SystemPageSize(),
base::SystemPageSize() - 11);
scoper.reset_unaligned(address + base::GetPageSize(),
base::GetPageSize() - 11);

// The entire unaligned page gets deallocated.
region_address = address;
GetRegionInfo(&region_address, &region_size);
EXPECT_EQ(address, region_address);
EXPECT_EQ(1u * base::SystemPageSize(), region_size);
EXPECT_EQ(1u * base::GetPageSize(), region_size);

// Reset with the remaining page.
scoper.reset_unaligned(address, base::SystemPageSize());
scoper.reset_unaligned(address, base::GetPageSize());
}

#if DCHECK_IS_ON()

TEST(ScopedMachVMTest, ResetMustBeAligned) {
vm_address_t address;
vm_size_t size = 2 * base::SystemPageSize();
vm_size_t size = 2 * base::GetPageSize();
kern_return_t kr =
vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
ASSERT_EQ(KERN_SUCCESS, kr);

ScopedMachVM scoper;
EXPECT_DCHECK_DEATH(scoper.reset(address, base::SystemPageSize() + 1));
EXPECT_DCHECK_DEATH(scoper.reset(address, base::GetPageSize() + 1));
}

#endif // DCHECK_IS_ON()
Expand Down
5 changes: 3 additions & 2 deletions gin/v8_platform_page_allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "base/allocator/partition_allocator/random.h"
#include "base/check_op.h"
#include "base/cpu.h"
#include "base/memory/page_size.h"
#include "build/build_config.h"

namespace {
Expand Down Expand Up @@ -54,11 +55,11 @@ namespace gin {
PageAllocator::~PageAllocator() = default;

size_t PageAllocator::AllocatePageSize() {
return base::PageAllocationGranularity();
return partition_alloc::internal::PageAllocationGranularity();
}

size_t PageAllocator::CommitPageSize() {
return base::SystemPageSize();
return base::GetPageSize();
}

void PageAllocator::SetRandomMmapSeed(int64_t seed) {
Expand Down
12 changes: 8 additions & 4 deletions gin/v8_platform_page_allocator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ using BTITestFunction = int64_t (*)(int64_t);
TEST(V8PlatformPageAllocatorBTITest, VerifyReadExecutePagesAreProtected) {
auto page_allocator = gin::PageAllocator();

auto const memory_size = base::PageAllocationGranularity();
auto const memory_alignment = base::PageAllocationGranularity();
auto const memory_size =
partition_alloc::internal::PageAllocationGranularity();
auto const memory_alignment =
partition_alloc::internal::PageAllocationGranularity();

// Next, map some read-write memory and copy some test helper functions there.
char* const buffer = reinterpret_cast<char*>(page_allocator.AllocatePages(
Expand Down Expand Up @@ -114,8 +116,10 @@ TEST(V8PlatformPageAllocatorBTITest, VerifyReadExecutePagesAreProtected) {
TEST(V8PlatformAllocatorBTITest, VerifyReadWriteExecutePagesAreNotProtected) {
auto page_allocator = gin::PageAllocator();

auto const memory_size = base::PageAllocationGranularity();
auto const memory_alignment = base::PageAllocationGranularity();
auto const memory_size =
partition_alloc::internal::PageAllocationGranularity();
auto const memory_alignment =
partition_alloc::internal::PageAllocationGranularity();

// Next, map some read-write memory and copy some test helper functions there.
char* const buffer = reinterpret_cast<char*>(page_allocator.AllocatePages(
Expand Down

0 comments on commit 8adb726

Please sign in to comment.