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