96 lines · cpp
1//===-- basic.cpp -----------------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "gwp_asan/tests/harness.h"10 11#include <unistd.h>12 13TEST_F(CustomGuardedPoolAllocator, BasicAllocation) {14 InitNumSlots(1);15 void *Ptr = GPA.allocate(1);16 EXPECT_NE(nullptr, Ptr);17 EXPECT_TRUE(GPA.pointerIsMine(Ptr));18 EXPECT_EQ(1u, GPA.getSize(Ptr));19 GPA.deallocate(Ptr);20}21 22TEST_F(DefaultGuardedPoolAllocator, NullptrIsNotMine) {23 EXPECT_FALSE(GPA.pointerIsMine(nullptr));24}25 26TEST_F(CustomGuardedPoolAllocator, SizedAllocations) {27 InitNumSlots(1);28 29 std::size_t MaxAllocSize = GPA.getAllocatorState()->maximumAllocationSize();30 EXPECT_TRUE(MaxAllocSize > 0);31 32 for (unsigned AllocSize = 1; AllocSize <= MaxAllocSize; AllocSize <<= 1) {33 void *Ptr = GPA.allocate(AllocSize);34 EXPECT_NE(nullptr, Ptr);35 EXPECT_TRUE(GPA.pointerIsMine(Ptr));36 EXPECT_EQ(AllocSize, GPA.getSize(Ptr));37 GPA.deallocate(Ptr);38 }39}40 41TEST_F(DefaultGuardedPoolAllocator, TooLargeAllocation) {42 EXPECT_EQ(nullptr,43 GPA.allocate(GPA.getAllocatorState()->maximumAllocationSize() + 1));44 EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, 0));45 EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, 1));46 EXPECT_EQ(nullptr, GPA.allocate(0, SIZE_MAX / 2));47 EXPECT_EQ(nullptr, GPA.allocate(1, SIZE_MAX / 2));48 EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, SIZE_MAX / 2));49}50 51TEST_F(DefaultGuardedPoolAllocator, ZeroSizeAndAlignmentAllocations) {52 void *P;53 EXPECT_NE(nullptr, (P = GPA.allocate(0, 0)));54 GPA.deallocate(P);55 EXPECT_NE(nullptr, (P = GPA.allocate(1, 0)));56 GPA.deallocate(P);57 EXPECT_NE(nullptr, (P = GPA.allocate(0, 1)));58 GPA.deallocate(P);59}60 61TEST_F(DefaultGuardedPoolAllocator, NonPowerOfTwoAlignment) {62 EXPECT_EQ(nullptr, GPA.allocate(0, 3));63 EXPECT_EQ(nullptr, GPA.allocate(1, 3));64 EXPECT_EQ(nullptr, GPA.allocate(0, SIZE_MAX));65 EXPECT_EQ(nullptr, GPA.allocate(1, SIZE_MAX));66}67 68// Added multi-page slots? You'll need to expand this test.69TEST_F(DefaultGuardedPoolAllocator, TooBigForSinglePageSlots) {70 size_t PageSize = sysconf(_SC_PAGESIZE);71 EXPECT_EQ(nullptr, GPA.allocate(PageSize + 1, 0));72 EXPECT_EQ(nullptr, GPA.allocate(PageSize + 1, 1));73 EXPECT_EQ(nullptr, GPA.allocate(PageSize + 1, PageSize));74 EXPECT_EQ(nullptr, GPA.allocate(1, 2 * PageSize));75 EXPECT_EQ(nullptr, GPA.allocate(0, 2 * PageSize));76}77 78TEST_F(CustomGuardedPoolAllocator, AllocAllSlots) {79 constexpr unsigned kNumSlots = 128;80 InitNumSlots(kNumSlots);81 void *Ptrs[kNumSlots];82 for (unsigned i = 0; i < kNumSlots; ++i) {83 Ptrs[i] = GPA.allocate(1);84 EXPECT_NE(nullptr, Ptrs[i]);85 EXPECT_TRUE(GPA.pointerIsMine(Ptrs[i]));86 }87 88 // This allocation should fail as all the slots are used.89 void *Ptr = GPA.allocate(1);90 EXPECT_EQ(nullptr, Ptr);91 EXPECT_FALSE(GPA.pointerIsMine(nullptr));92 93 for (unsigned i = 0; i < kNumSlots; ++i)94 GPA.deallocate(Ptrs[i]);95}96