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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "gwp_asan/tests/harness.h" 10 11 TEST_F(CustomGuardedPoolAllocator, BasicAllocation) { 12 InitNumSlots(1); 13 void *Ptr = GPA.allocate(1); 14 EXPECT_NE(nullptr, Ptr); 15 EXPECT_TRUE(GPA.pointerIsMine(Ptr)); 16 EXPECT_EQ(1u, GPA.getSize(Ptr)); 17 GPA.deallocate(Ptr); 18 } 19 20 TEST_F(DefaultGuardedPoolAllocator, NullptrIsNotMine) { 21 EXPECT_FALSE(GPA.pointerIsMine(nullptr)); 22 } 23 24 TEST_F(CustomGuardedPoolAllocator, SizedAllocations) { 25 InitNumSlots(1); 26 27 std::size_t MaxAllocSize = GPA.maximumAllocationSize(); 28 EXPECT_TRUE(MaxAllocSize > 0); 29 30 for (unsigned AllocSize = 1; AllocSize <= MaxAllocSize; AllocSize <<= 1) { 31 void *Ptr = GPA.allocate(AllocSize); 32 EXPECT_NE(nullptr, Ptr); 33 EXPECT_TRUE(GPA.pointerIsMine(Ptr)); 34 EXPECT_EQ(AllocSize, GPA.getSize(Ptr)); 35 GPA.deallocate(Ptr); 36 } 37 } 38 39 TEST_F(DefaultGuardedPoolAllocator, TooLargeAllocation) { 40 EXPECT_EQ(nullptr, GPA.allocate(GPA.maximumAllocationSize() + 1)); 41 } 42 43 TEST_F(CustomGuardedPoolAllocator, AllocAllSlots) { 44 constexpr unsigned kNumSlots = 128; 45 InitNumSlots(kNumSlots); 46 void *Ptrs[kNumSlots]; 47 for (unsigned i = 0; i < kNumSlots; ++i) { 48 Ptrs[i] = GPA.allocate(1); 49 EXPECT_NE(nullptr, Ptrs[i]); 50 EXPECT_TRUE(GPA.pointerIsMine(Ptrs[i])); 51 } 52 53 // This allocation should fail as all the slots are used. 54 void *Ptr = GPA.allocate(1); 55 EXPECT_EQ(nullptr, Ptr); 56 EXPECT_FALSE(GPA.pointerIsMine(nullptr)); 57 58 for (unsigned i = 0; i < kNumSlots; ++i) 59 GPA.deallocate(Ptrs[i]); 60 } 61