Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fx_memory_unittest.cpp
Go to the documentation of this file.
1// Copyright 2015 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "core/fxcrt/fx_memory.h"
6
7#include <limits>
8#include <memory>
9
10#include "build/build_config.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13#if defined(PDF_USE_PARTITION_ALLOC)
14#include "partition_alloc/partition_address_space.h"
15#endif
16
17namespace {
18
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;
26
27} // namespace
28
30 uint8_t* ptr = FX_Alloc(uint8_t, 0);
31 uint8_t* ptr2 = FX_Alloc(uint8_t, 0);
32 EXPECT_TRUE(ptr); // Malloc(0) is distinguishable from OOM.
33 EXPECT_NE(ptr, ptr2); // Each malloc(0) is distinguishable.
34 FX_Free(ptr2);
35 FX_Free(ptr);
36}
37
39 EXPECT_DEATH_IF_SUPPORTED((void)FX_Alloc(int, kCloseToMaxIntAlloc), "");
40
41 int* ptr = FX_Alloc(int, 1);
42 EXPECT_TRUE(ptr);
43 EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kCloseToMaxIntAlloc),
44 "");
45 FX_Free(ptr);
46}
47
49 // |ptr| needs to be defined and used to avoid Clang optimizes away the
50 // FX_Alloc() statement overzealously for optimized builds.
51 int* ptr = nullptr;
52 EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc(int, kOverflowIntAlloc), "") << ptr;
53
54 ptr = FX_Alloc(int, 1);
55 EXPECT_TRUE(ptr);
56 EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kOverflowIntAlloc), "");
57 FX_Free(ptr);
58}
59
61 // |ptr| needs to be defined and used to avoid Clang optimizes away the
62 // FX_Alloc() statement overzealously for optimized builds.
63 int* ptr = nullptr;
64 EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc2D(int, kWidth, kOverflowIntAlloc2D),
65 "")
66 << ptr;
67}
68
70 EXPECT_FALSE(FX_TryAlloc(int, kCloseToMaxIntAlloc));
71
72 int* ptr = FX_Alloc(int, 1);
73 EXPECT_TRUE(ptr);
74 EXPECT_FALSE(FX_TryRealloc(int, ptr, kCloseToMaxIntAlloc));
75 FX_Free(ptr);
76}
77
78#if !defined(COMPILER_GCC)
80 // |ptr| needs to be defined and used to avoid Clang optimizes away the
81 // calloc() statement overzealously for optimized builds.
82 int* ptr = (int*)calloc(sizeof(int), kOverflowIntAlloc);
83 EXPECT_FALSE(ptr) << ptr;
84
85 ptr = FX_Alloc(int, 1);
86 EXPECT_TRUE(ptr);
87 *ptr = 1492; // Arbitrary sentinel.
88 EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc));
89 EXPECT_EQ(1492, *ptr);
90 FX_Free(ptr);
91}
92#endif
93
95 EXPECT_FALSE(FXMEM_DefaultAlloc(kCloseToMaxByteAlloc));
96
97 void* ptr = FXMEM_DefaultAlloc(1);
98 EXPECT_TRUE(ptr);
99 EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kCloseToMaxByteAlloc));
101}
102
104 uint8_t* ptr = FX_Alloc(uint8_t, 32);
105 ASSERT_TRUE(ptr);
106 for (size_t i = 0; i < 32; ++i)
107 EXPECT_EQ(0, ptr[i]);
108 FX_Free(ptr);
109}
110
112 static_assert(std::numeric_limits<size_t>::max() % 2 == 1,
113 "numeric limit must be odd for this test");
114
115 size_t s0 = 0;
116 size_t s1 = 1;
117 size_t s2 = 2;
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));
123
124 int i0 = 0;
125 int i511 = 511;
126 int i512 = 512;
127 int ineg = -513;
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));
132}
133
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())));
143#endif // BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
144}
145
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)));
153#endif // BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
154 free(obj);
155}
156
157TEST(FxMemory, StackObjectIsNotPA) {
158 int x = 3;
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)));
164#endif // BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
165}
166#endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC) &&
167 // BUILDFLAG(HAS_64_BIT_POINTERS)
168#endif // defined(PDF_USE_PARTITION_ALLOC)
TEST(FXCRYPT, MD5GenerateEmtpyData)
void FXMEM_DefaultFree(void *pointer)
Definition fx_memory.cpp:33