5#include "core/fxcrt/fx_memory.h"
10#include "build/build_config.h"
11#include "testing/gtest/include/gtest/gtest.h"
13#if defined(PDF_USE_PARTITION_ALLOC)
14#include "partition_alloc/partition_address_space.h"
19constexpr size_t kMaxByteAlloc = std::numeric_limits<size_t>::max();
20constexpr size_t kMaxIntAlloc = kMaxByteAlloc /
sizeof(
int);
21constexpr size_t kOverflowIntAlloc = kMaxIntAlloc + 100;
22constexpr size_t kWidth = 640;
23constexpr size_t kOverflowIntAlloc2D = kMaxIntAlloc / kWidth + 10;
24constexpr size_t kCloseToMaxIntAlloc = kMaxIntAlloc - 100;
25constexpr size_t kCloseToMaxByteAlloc = kMaxByteAlloc - 100;
30 uint8_t* ptr = FX_Alloc(uint8_t, 0);
31 uint8_t* ptr2 = FX_Alloc(uint8_t, 0);
39 EXPECT_DEATH_IF_SUPPORTED((
void)FX_Alloc(
int, kCloseToMaxIntAlloc),
"");
41 int* ptr = FX_Alloc(
int, 1);
43 EXPECT_DEATH_IF_SUPPORTED((
void)FX_Realloc(
int, ptr, kCloseToMaxIntAlloc),
52 EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc(
int, kOverflowIntAlloc),
"") << ptr;
54 ptr = FX_Alloc(
int, 1);
56 EXPECT_DEATH_IF_SUPPORTED((
void)FX_Realloc(
int, ptr, kOverflowIntAlloc),
"");
64 EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc2D(
int, kWidth, kOverflowIntAlloc2D),
70 EXPECT_FALSE(FX_TryAlloc(
int, kCloseToMaxIntAlloc));
72 int* ptr = FX_Alloc(
int, 1);
74 EXPECT_FALSE(FX_TryRealloc(
int, ptr, kCloseToMaxIntAlloc));
78#if !defined(COMPILER_GCC)
82 int* ptr = (
int*)calloc(
sizeof(
int), kOverflowIntAlloc);
83 EXPECT_FALSE(ptr) << ptr;
85 ptr = FX_Alloc(
int, 1);
88 EXPECT_FALSE(FX_TryRealloc(
int, ptr, kOverflowIntAlloc));
89 EXPECT_EQ(1492, *ptr);
95 EXPECT_FALSE(FXMEM_DefaultAlloc(kCloseToMaxByteAlloc));
97 void* ptr = FXMEM_DefaultAlloc(1);
99 EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kCloseToMaxByteAlloc));
104 uint8_t* ptr = FX_Alloc(uint8_t, 32);
106 for (size_t i = 0; i < 32; ++i)
107 EXPECT_EQ(0, ptr[i]);
112 static_assert(std::numeric_limits<size_t>::max() % 2 == 1,
113 "numeric limit must be odd for this test");
118 size_t sbig = std::numeric_limits<size_t>::max() - 2;
119 EXPECT_EQ(0u, FxAlignToBoundary<2>(s0));
120 EXPECT_EQ(2u, FxAlignToBoundary<2>(s1));
121 EXPECT_EQ(2u, FxAlignToBoundary<2>(s2));
122 EXPECT_EQ(std::numeric_limits<size_t>::max() - 1, FxAlignToBoundary<2>(sbig));
128 EXPECT_EQ(0, FxAlignToBoundary<512>(i0));
129 EXPECT_EQ(512, FxAlignToBoundary<512>(i511));
130 EXPECT_EQ(512, FxAlignToBoundary<512>(i512));
131 EXPECT_EQ(-512, FxAlignToBoundary<512>(ineg));
134#if defined(PDF_USE_PARTITION_ALLOC)
135#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) && BUILDFLAG(HAS_64_BIT_POINTERS)
136TEST(FxMemory, NewOperatorResultIsPA) {
137 auto obj = std::make_unique<
double>(4.0);
138 EXPECT_TRUE(partition_alloc::IsManagedByPartitionAlloc(
139 reinterpret_cast<uintptr_t>(obj.get())));
140#if BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
141 EXPECT_TRUE(partition_alloc::IsManagedByPartitionAllocBRPPool(
142 reinterpret_cast<uintptr_t>(obj.get())));
146TEST(FxMemory, MallocResultIsPA) {
147 void* obj = malloc(16);
148 EXPECT_TRUE(partition_alloc::IsManagedByPartitionAlloc(
149 reinterpret_cast<uintptr_t>(obj)));
150#if BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
151 EXPECT_TRUE(partition_alloc::IsManagedByPartitionAllocBRPPool(
152 reinterpret_cast<uintptr_t>(obj)));
157TEST(FxMemory, StackObjectIsNotPA) {
159 EXPECT_FALSE(partition_alloc::IsManagedByPartitionAlloc(
160 reinterpret_cast<uintptr_t>(&x)));
161#if BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
162 EXPECT_FALSE(partition_alloc::IsManagedByPartitionAllocBRPPool(
163 reinterpret_cast<uintptr_t>(&x)));
TEST(FXCRYPT, MD5GenerateEmtpyData)
void FXMEM_DefaultFree(void *pointer)