• 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 
9 #include "build/build_config.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace {
13 
14 constexpr size_t kMaxByteAlloc = std::numeric_limits<size_t>::max();
15 constexpr size_t kMaxIntAlloc = kMaxByteAlloc / sizeof(int);
16 constexpr size_t kOverflowIntAlloc = kMaxIntAlloc + 100;
17 constexpr size_t kWidth = 640;
18 constexpr size_t kOverflowIntAlloc2D = kMaxIntAlloc / kWidth + 10;
19 constexpr size_t kCloseToMaxIntAlloc = kMaxIntAlloc - 100;
20 constexpr size_t kCloseToMaxByteAlloc = kMaxByteAlloc - 100;
21 
22 }  // namespace
23 
TEST(fxcrt,FX_AllocZero)24 TEST(fxcrt, FX_AllocZero) {
25   uint8_t* ptr = FX_Alloc(uint8_t, 0);
26   uint8_t* ptr2 = FX_Alloc(uint8_t, 0);
27   EXPECT_TRUE(ptr);      // Malloc(0) is distinguishable from OOM.
28   EXPECT_NE(ptr, ptr2);  // Each malloc(0) is distinguishable.
29   FX_Free(ptr2);
30   FX_Free(ptr);
31 }
32 
TEST(fxcrt,FXAllocOOM)33 TEST(fxcrt, FXAllocOOM) {
34   EXPECT_DEATH_IF_SUPPORTED((void)FX_Alloc(int, kCloseToMaxIntAlloc), "");
35 
36   int* ptr = FX_Alloc(int, 1);
37   EXPECT_TRUE(ptr);
38   EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kCloseToMaxIntAlloc),
39                             "");
40   FX_Free(ptr);
41 }
42 
TEST(fxcrt,FX_AllocOverflow)43 TEST(fxcrt, FX_AllocOverflow) {
44   // |ptr| needs to be defined and used to avoid Clang optimizes away the
45   // FX_Alloc() statement overzealously for optimized builds.
46   int* ptr = nullptr;
47   EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc(int, kOverflowIntAlloc), "") << ptr;
48 
49   ptr = FX_Alloc(int, 1);
50   EXPECT_TRUE(ptr);
51   EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kOverflowIntAlloc), "");
52   FX_Free(ptr);
53 }
54 
TEST(fxcrt,FX_AllocOverflow2D)55 TEST(fxcrt, FX_AllocOverflow2D) {
56   // |ptr| needs to be defined and used to avoid Clang optimizes away the
57   // FX_Alloc() statement overzealously for optimized builds.
58   int* ptr = nullptr;
59   EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc2D(int, kWidth, kOverflowIntAlloc2D),
60                             "")
61       << ptr;
62 }
63 
TEST(fxcrt,FXTryAllocOOM)64 TEST(fxcrt, FXTryAllocOOM) {
65   EXPECT_FALSE(FX_TryAlloc(int, kCloseToMaxIntAlloc));
66 
67   int* ptr = FX_Alloc(int, 1);
68   EXPECT_TRUE(ptr);
69   EXPECT_FALSE(FX_TryRealloc(int, ptr, kCloseToMaxIntAlloc));
70   FX_Free(ptr);
71 }
72 
73 #if !defined(COMPILER_GCC)
TEST(fxcrt,FX_TryAllocOverflow)74 TEST(fxcrt, FX_TryAllocOverflow) {
75   // |ptr| needs to be defined and used to avoid Clang optimizes away the
76   // calloc() statement overzealously for optimized builds.
77   int* ptr = (int*)calloc(sizeof(int), kOverflowIntAlloc);
78   EXPECT_FALSE(ptr) << ptr;
79 
80   ptr = FX_Alloc(int, 1);
81   EXPECT_TRUE(ptr);
82   *ptr = 1492;  // Arbitrary sentinel.
83   EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc));
84   EXPECT_EQ(1492, *ptr);
85   FX_Free(ptr);
86 }
87 #endif
88 
TEST(fxcrt,FXMEMDefaultOOM)89 TEST(fxcrt, FXMEMDefaultOOM) {
90   EXPECT_FALSE(FXMEM_DefaultAlloc(kCloseToMaxByteAlloc));
91 
92   void* ptr = FXMEM_DefaultAlloc(1);
93   EXPECT_TRUE(ptr);
94   EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kCloseToMaxByteAlloc));
95   FXMEM_DefaultFree(ptr);
96 }
97 
TEST(fxcrt,AllocZeroesMemory)98 TEST(fxcrt, AllocZeroesMemory) {
99   uint8_t* ptr = FX_Alloc(uint8_t, 32);
100   ASSERT_TRUE(ptr);
101   for (size_t i = 0; i < 32; ++i)
102     EXPECT_EQ(0, ptr[i]);
103   FX_Free(ptr);
104 }
105 
TEST(fxcrt,FXAlign)106 TEST(fxcrt, FXAlign) {
107   static_assert(std::numeric_limits<size_t>::max() % 2 == 1,
108                 "numeric limit must be odd for this test");
109 
110   size_t s0 = 0;
111   size_t s1 = 1;
112   size_t s2 = 2;
113   size_t sbig = std::numeric_limits<size_t>::max() - 2;
114   EXPECT_EQ(0u, FxAlignToBoundary<2>(s0));
115   EXPECT_EQ(2u, FxAlignToBoundary<2>(s1));
116   EXPECT_EQ(2u, FxAlignToBoundary<2>(s2));
117   EXPECT_EQ(std::numeric_limits<size_t>::max() - 1, FxAlignToBoundary<2>(sbig));
118 
119   int i0 = 0;
120   int i511 = 511;
121   int i512 = 512;
122   int ineg = -513;
123   EXPECT_EQ(0, FxAlignToBoundary<512>(i0));
124   EXPECT_EQ(512, FxAlignToBoundary<512>(i511));
125   EXPECT_EQ(512, FxAlignToBoundary<512>(i512));
126   EXPECT_EQ(-512, FxAlignToBoundary<512>(ineg));
127 }
128