• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "testing/gtest/include/gtest/gtest.h"
10 
11 namespace {
12 
13 const size_t kMaxByteAlloc = std::numeric_limits<size_t>::max();
14 const size_t kMaxIntAlloc = kMaxByteAlloc / sizeof(int);
15 const size_t kOverflowIntAlloc = kMaxIntAlloc + 100;
16 const size_t kWidth = 640;
17 const size_t kOverflowIntAlloc2D = kMaxIntAlloc / kWidth + 10;
18 
19 }  // namespace
20 
21 // TODO(tsepez): re-enable OOM tests if we can find a way to
22 // prevent it from hosing the bots.
TEST(fxcrt,DISABLED_FX_AllocOOM)23 TEST(fxcrt, DISABLED_FX_AllocOOM) {
24   EXPECT_DEATH_IF_SUPPORTED((void)FX_Alloc(int, kMaxIntAlloc), "");
25 
26   int* ptr = FX_Alloc(int, 1);
27   EXPECT_TRUE(ptr);
28   EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kMaxIntAlloc), "");
29   FX_Free(ptr);
30 }
31 
TEST(fxcrt,FX_AllocOverflow)32 TEST(fxcrt, FX_AllocOverflow) {
33   // |ptr| needs to be defined and used to avoid Clang optimizes away the
34   // FX_Alloc() statement overzealously for optimized builds.
35   int* ptr = nullptr;
36   EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc(int, kOverflowIntAlloc), "") << ptr;
37 
38   ptr = FX_Alloc(int, 1);
39   EXPECT_TRUE(ptr);
40   EXPECT_DEATH_IF_SUPPORTED((void)FX_Realloc(int, ptr, kOverflowIntAlloc), "");
41   FX_Free(ptr);
42 }
43 
TEST(fxcrt,FX_AllocOverflow2D)44 TEST(fxcrt, FX_AllocOverflow2D) {
45   // |ptr| needs to be defined and used to avoid Clang optimizes away the
46   // FX_Alloc() statement overzealously for optimized builds.
47   int* ptr = nullptr;
48   EXPECT_DEATH_IF_SUPPORTED(ptr = FX_Alloc2D(int, kWidth, kOverflowIntAlloc2D),
49                             "")
50       << ptr;
51 }
52 
TEST(fxcrt,DISABLED_FX_TryAllocOOM)53 TEST(fxcrt, DISABLED_FX_TryAllocOOM) {
54   EXPECT_FALSE(FX_TryAlloc(int, kMaxIntAlloc));
55 
56   int* ptr = FX_Alloc(int, 1);
57   EXPECT_TRUE(ptr);
58   EXPECT_FALSE(FX_TryRealloc(int, ptr, kMaxIntAlloc));
59   FX_Free(ptr);
60 }
61 
TEST(fxcrt,FX_TryAllocOverflow)62 TEST(fxcrt, FX_TryAllocOverflow) {
63   // |ptr| needs to be defined and used to avoid Clang optimizes away the
64   // calloc() statement overzealously for optimized builds.
65   int* ptr = (int*)calloc(sizeof(int), kOverflowIntAlloc);
66   EXPECT_FALSE(ptr) << ptr;
67 
68   ptr = FX_Alloc(int, 1);
69   EXPECT_TRUE(ptr);
70   EXPECT_FALSE(FX_TryRealloc(int, ptr, kOverflowIntAlloc));
71   FX_Free(ptr);
72 }
73 
TEST(fxcrt,DISABLED_FXMEM_DefaultOOM)74 TEST(fxcrt, DISABLED_FXMEM_DefaultOOM) {
75   EXPECT_FALSE(FXMEM_DefaultAlloc(kMaxByteAlloc));
76 
77   void* ptr = FXMEM_DefaultAlloc(1);
78   EXPECT_TRUE(ptr);
79   EXPECT_FALSE(FXMEM_DefaultRealloc(ptr, kMaxByteAlloc));
80   FXMEM_DefaultFree(ptr);
81 }
82