chromium/base/allocator/partition_allocator/src/partition_alloc/page_allocator_unittest.cc

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "partition_alloc/page_allocator.h"

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>

#include "partition_alloc/address_space_randomization.h"
#include "partition_alloc/build_config.h"
#include "partition_alloc/buildflags.h"
#include "partition_alloc/page_allocator_constants.h"
#include "partition_alloc/partition_alloc_base/cpu.h"
#include "partition_alloc/partition_alloc_base/logging.h"
#include "partition_alloc/partition_alloc_base/notreached.h"
#include "partition_alloc/partition_alloc_config.h"
#include "partition_alloc/tagging.h"

#if defined(LINUX_NAME_REGION)
#include "partition_alloc/partition_alloc_base/debug/proc_maps_linux.h"
#endif

#include "testing/gtest/include/gtest/gtest.h"

#if PA_BUILDFLAG(IS_POSIX)
#include <sys/mman.h>
#include <sys/time.h>

#include <csetjmp>
#include <csignal>
#endif  // PA_BUILDFLAG(IS_POSIX)

#include "partition_alloc/arm_bti_test_functions.h"

#if PA_BUILDFLAG(HAS_MEMORY_TAGGING)
#include <arm_acle.h>
#if PA_BUILDFLAG(IS_ANDROID) || PA_BUILDFLAG(IS_LINUX)
#define MTE_KILLED_BY_SIGNAL_AVAILABLE
#endif
#endif  // PA_BUILDFLAG(HAS_MEMORY_TAGGING)

#if !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)

namespace partition_alloc::internal {

namespace {

// Any number of bytes that can be allocated with no trouble.
size_t EasyAllocSize() {}

// A huge amount of memory, greater than or equal to the ASLR space.
size_t HugeMemoryAmount() {}

}  // namespace

TEST(PartitionAllocPageAllocatorTest, Rounding) {}

TEST(PartitionAllocPageAllocatorTest, NextAlignedWithOffset) {}

// Test that failed page allocations invoke base::ReleaseReservation().
// We detect this by making a reservation and ensuring that after failure, we
// can make a new reservation.
TEST(PartitionAllocPageAllocatorTest, AllocFailure) {}

// TODO(crbug.com/41344946): Test failed on chromium.win/Win10 Tests x64.
#if PA_BUILDFLAG(IS_WIN) && PA_BUILDFLAG(PA_ARCH_CPU_64_BITS)
#define MAYBE_ReserveAddressSpace
#else
#define MAYBE_ReserveAddressSpace
#endif  // PA_BUILDFLAG(IS_WIN) && PA_BUILDFLAG(PA_ARCH_CPU_64_BITS)

// Test that reserving address space can fail.
TEST(PartitionAllocPageAllocatorTest, MAYBE_ReserveAddressSpace) {}

TEST(PartitionAllocPageAllocatorTest, AllocAndFreePages) {}

TEST(PartitionAllocPageAllocatorTest, AllocPagesAligned) {}

TEST(PartitionAllocPageAllocatorTest,
     AllocAndFreePagesWithPageReadWriteTagged) {}

TEST(PartitionAllocPageAllocatorTest,
     AllocAndFreePagesWithPageReadExecuteConfirmCFI) {}

TEST(PartitionAllocPageAllocatorTest,
     AllocAndFreePagesWithPageReadWriteTaggedSynchronous) {}

TEST(PartitionAllocPageAllocatorTest,
     AllocAndFreePagesWithPageReadWriteTaggedAsynchronous) {}

// Test permission setting on POSIX, where we can set a trap handler.
#if PA_BUILDFLAG(IS_POSIX)

namespace {
sigjmp_buf g_continuation;

void SignalHandler(int signal, siginfo_t* info, void*) {}
}  // namespace

// On Mac, sometimes we get SIGBUS instead of SIGSEGV, so handle that too.
#if PA_BUILDFLAG(IS_APPLE)
#define EXTRA_FAULT_BEGIN_ACTION
#define EXTRA_FAULT_END_ACTION
#else
#define EXTRA_FAULT_BEGIN_ACTION()
#define EXTRA_FAULT_END_ACTION()
#endif

// Install a signal handler so we can catch the fault we're about to trigger.
#define FAULT_TEST_BEGIN()
// Fault generating code goes here...

// Handle when sigsetjmp returns nonzero (we are returning from our handler).
#define FAULT_TEST_END()

TEST(PartitionAllocPageAllocatorTest, InaccessiblePages) {}

// TODO(crbug.com/40212918): Understand why we can't read from Read-Execute
// pages on iOS.
#if PA_BUILDFLAG(IS_IOS)
#define MAYBE_ReadExecutePages
#else
#define MAYBE_ReadExecutePages
#endif  // PA_BUILDFLAG(IS_IOS)
TEST(PartitionAllocPageAllocatorTest, MAYBE_ReadExecutePages) {}

#endif  // PA_BUILDFLAG(IS_POSIX)

#if defined(LINUX_NAME_REGION)
TEST(PartitionAllocPageAllocatorTest, PageTagging) {}
#endif  // defined(LINUX_NAME_REGION)

TEST(PartitionAllocPageAllocatorTest, DecommitErasesMemory) {}

TEST(PartitionAllocPageAllocatorTest, DecommitAndZero) {}

TEST(PartitionAllocPageAllocatorTest, MappedPagesAccounting) {}

TEST(PartitionAllocPageAllocatorTest, AllocInaccessibleWillJitLater) {}

#if PA_BUILDFLAG(IS_IOS) || PA_BUILDFLAG(IS_MAC)
// TODO(crbug.com/40916148): Fix test to GTEST_SKIP() if MAP_JIT is in-use,
// or to be run otherwise, since kReadWriteExecute is used in some other
// configurations.
#define MAYBE_AllocReadWriteExecute
#else
#define MAYBE_AllocReadWriteExecute
#endif  // PA_BUILDFLAG(IS_IOS) || PA_BUILDFLAG(IS_MAC)
TEST(PartitionAllocPageAllocatorTest, MAYBE_AllocReadWriteExecute) {}

}  // namespace partition_alloc::internal

#endif  // !defined(MEMORY_TOOL_REPLACES_ALLOCATOR)