1 // Copyright 2015 PDFium Authors. All rights reserved.
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 const size_t kMaxByteAlloc = std::numeric_limits<size_t>::max();
15 const size_t kMaxIntAlloc = kMaxByteAlloc / sizeof(int);
16 const size_t kOverflowIntAlloc = kMaxIntAlloc + 100;
17 const size_t kWidth = 640;
18 const size_t kOverflowIntAlloc2D = kMaxIntAlloc / kWidth + 10;
19
20 } // namespace
21
TEST(fxcrt,FX_AllocZero)22 TEST(fxcrt, FX_AllocZero) {
23 uint8_t* ptr = FX_Alloc(uint8_t, 0);
24 uint8_t* ptr2 = FX_Alloc(uint8_t, 0);
25 EXPECT_TRUE(ptr); // Malloc(0) is distinguishable from OOM.
26 EXPECT_NE(ptr, ptr2); // Each malloc(0) is distinguishable.
27 FX_Free(ptr2);
28 FX_Free(ptr);
29 }
30
31 // TODO(tsepez): re-enable OOM tests if we can find a way to
32 // prevent it from hosing the bots.
TEST(fxcrt,DISABLED_FX_AllocOOM)33 TEST(fxcrt, DISABLED_FX_AllocOOM) {
34 EXPECT_DEATH_IF_SUPPORTED((void)FX_Alloc(int, kMaxIntAlloc), "");
35
36 int* ptr = FX_Alloc(int, 1);
37 EXPECT_TRUE(ptr);
38 EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kMaxIntAlloc), "");
39 FX_Free(ptr);
40 }
41
TEST(fxcrt,FX_AllocOverflow)42 TEST(fxcrt, FX_AllocOverflow) {
43 // |ptr| needs to be defined and used to avoid Clang optimizes away the
44 // FX_Alloc() statement overzealously for optimized builds.
45 int* ptr = nullptr;
46 EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc(int, kOverflowIntAlloc), "") << ptr;
47
48 ptr = FX_Alloc(int, 1);
49 EXPECT_TRUE(ptr);
50 EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kOverflowIntAlloc), "");
51 FX_Free(ptr);
52 }
53
TEST(fxcrt,FX_AllocOverflow2D)54 TEST(fxcrt, FX_AllocOverflow2D) {
55 // |ptr| needs to be defined and used to avoid Clang optimizes away the
56 // FX_Alloc() statement overzealously for optimized builds.
57 int* ptr = nullptr;
58 EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc2D(int, kWidth, kOverflowIntAlloc2D),
59 "")
60 << ptr;
61 }
62
TEST(fxcrt,DISABLED_FX_TryAllocOOM)63 TEST(fxcrt, DISABLED_FX_TryAllocOOM) {
64 EXPECT_FALSE(FX_TryAlloc(int, kMaxIntAlloc));
65
66 int* ptr = FX_Alloc(int, 1);
67 EXPECT_TRUE(ptr);
68 EXPECT_FALSE(FX_TryRealloc(int, ptr, kMaxIntAlloc));
69 FX_Free(ptr);
70 }
71
72 #if !defined(COMPILER_GCC)
TEST(fxcrt,FX_TryAllocOverflow)73 TEST(fxcrt, FX_TryAllocOverflow) {
74 // |ptr| needs to be defined and used to avoid Clang optimizes away the
75 // calloc() statement overzealously for optimized builds.
76 int* ptr = (int*)calloc(sizeof(int), kOverflowIntAlloc);
77 EXPECT_FALSE(ptr) << ptr;
78
79 ptr = FX_Alloc(int, 1);
80 EXPECT_TRUE(ptr);
81 *ptr = 1492; // Arbitrary sentinel.
82 EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc));
83 EXPECT_EQ(1492, *ptr);
84 FX_Free(ptr);
85 }
86 #endif
87
TEST(fxcrt,DISABLED_FXMEM_DefaultOOM)88 TEST(fxcrt, DISABLED_FXMEM_DefaultOOM) {
89 EXPECT_FALSE(FXMEM_DefaultAlloc(kMaxByteAlloc));
90
91 void* ptr = FXMEM_DefaultAlloc(1);
92 EXPECT_TRUE(ptr);
93 EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kMaxByteAlloc));
94 FXMEM_DefaultFree(ptr);
95 }
96
TEST(fxcrt,FXAlign)97 TEST(fxcrt, FXAlign) {
98 static_assert(std::numeric_limits<size_t>::max() % 2 == 1,
99 "numeric limit must be odd for this test");
100
101 size_t s0 = 0;
102 size_t s1 = 1;
103 size_t s2 = 2;
104 size_t sbig = std::numeric_limits<size_t>::max() - 2;
105 EXPECT_EQ(0u, FxAlignToBoundary<2>(s0));
106 EXPECT_EQ(2u, FxAlignToBoundary<2>(s1));
107 EXPECT_EQ(2u, FxAlignToBoundary<2>(s2));
108 EXPECT_EQ(std::numeric_limits<size_t>::max() - 1, FxAlignToBoundary<2>(sbig));
109
110 int i0 = 0;
111 int i511 = 511;
112 int i512 = 512;
113 int ineg = -513;
114 EXPECT_EQ(0, FxAlignToBoundary<512>(i0));
115 EXPECT_EQ(512, FxAlignToBoundary<512>(i511));
116 EXPECT_EQ(512, FxAlignToBoundary<512>(i512));
117 EXPECT_EQ(-512, FxAlignToBoundary<512>(ineg));
118 }
119