• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/allocator/partition_allocator/partition_address_space.h"
15 #endif
16 
17 namespace {
18 
19 constexpr size_t kMaxByteAlloc = std::numeric_limits<size_t>::max();
20 constexpr size_t kMaxIntAlloc = kMaxByteAlloc / sizeof(int);
21 constexpr size_t kOverflowIntAlloc = kMaxIntAlloc + 100;
22 constexpr size_t kWidth = 640;
23 constexpr size_t kOverflowIntAlloc2D = kMaxIntAlloc / kWidth + 10;
24 constexpr size_t kCloseToMaxIntAlloc = kMaxIntAlloc - 100;
25 constexpr size_t kCloseToMaxByteAlloc = kMaxByteAlloc - 100;
26 
27 }  // namespace
28 
TEST(fxcrt,FX_AllocZero)29 TEST(fxcrt, FX_AllocZero) {
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 
TEST(fxcrt,FXAllocOOM)38 TEST(fxcrt, FXAllocOOM) {
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 
TEST(fxcrt,FX_AllocOverflow)48 TEST(fxcrt, FX_AllocOverflow) {
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 
TEST(fxcrt,FX_AllocOverflow2D)60 TEST(fxcrt, FX_AllocOverflow2D) {
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 
TEST(fxcrt,FXTryAllocOOM)69 TEST(fxcrt, FXTryAllocOOM) {
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)
TEST(fxcrt,FX_TryAllocOverflow)79 TEST(fxcrt, FX_TryAllocOverflow) {
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 
TEST(fxcrt,FXMEMDefaultOOM)94 TEST(fxcrt, FXMEMDefaultOOM) {
95   EXPECT_FALSE(FXMEM_DefaultAlloc(kCloseToMaxByteAlloc));
96 
97   void* ptr = FXMEM_DefaultAlloc(1);
98   EXPECT_TRUE(ptr);
99   EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kCloseToMaxByteAlloc));
100   FXMEM_DefaultFree(ptr);
101 }
102 
TEST(fxcrt,AllocZeroesMemory)103 TEST(fxcrt, AllocZeroesMemory) {
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 
TEST(fxcrt,FXAlign)111 TEST(fxcrt, FXAlign) {
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)
TEST(FxMemory,NewOperatorResultIsPA)136 TEST(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 
TEST(FxMemory,MallocResultIsPA)146 TEST(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 
TEST(FxMemory,StackObjectIsNotPA)157 TEST(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)
169