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.getAllocatorState()->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, |
41 | GPA.allocate(GPA.getAllocatorState()->maximumAllocationSize() + 1)); |
42 | EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, 0)); |
43 | EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, 1)); |
44 | EXPECT_EQ(nullptr, GPA.allocate(0, SIZE_MAX / 2)); |
45 | EXPECT_EQ(nullptr, GPA.allocate(1, SIZE_MAX / 2)); |
46 | EXPECT_EQ(nullptr, GPA.allocate(SIZE_MAX, SIZE_MAX / 2)); |
47 | } |
48 | |
49 | TEST_F(DefaultGuardedPoolAllocator, ZeroSizeAndAlignmentAllocations) { |
50 | void *P; |
51 | EXPECT_NE(nullptr, (P = GPA.allocate(0, 0))); |
52 | GPA.deallocate(P); |
53 | EXPECT_NE(nullptr, (P = GPA.allocate(1, 0))); |
54 | GPA.deallocate(P); |
55 | EXPECT_NE(nullptr, (P = GPA.allocate(0, 1))); |
56 | GPA.deallocate(P); |
57 | } |
58 | |
59 | TEST_F(DefaultGuardedPoolAllocator, NonPowerOfTwoAlignment) { |
60 | EXPECT_EQ(nullptr, GPA.allocate(0, 3)); |
61 | EXPECT_EQ(nullptr, GPA.allocate(1, 3)); |
62 | EXPECT_EQ(nullptr, GPA.allocate(0, SIZE_MAX)); |
63 | EXPECT_EQ(nullptr, GPA.allocate(1, SIZE_MAX)); |
64 | } |
65 | |
66 | // Added multi-page slots? You'll need to expand this test. |
67 | TEST_F(DefaultGuardedPoolAllocator, TooBigForSinglePageSlots) { |
68 | EXPECT_EQ(nullptr, GPA.allocate(0x1001, 0)); |
69 | EXPECT_EQ(nullptr, GPA.allocate(0x1001, 1)); |
70 | EXPECT_EQ(nullptr, GPA.allocate(0x1001, 0x1000)); |
71 | EXPECT_EQ(nullptr, GPA.allocate(1, 0x2000)); |
72 | EXPECT_EQ(nullptr, GPA.allocate(0, 0x2000)); |
73 | } |
74 | |
75 | TEST_F(CustomGuardedPoolAllocator, AllocAllSlots) { |
76 | constexpr unsigned kNumSlots = 128; |
77 | InitNumSlots(kNumSlots); |
78 | void *Ptrs[kNumSlots]; |
79 | for (unsigned i = 0; i < kNumSlots; ++i) { |
80 | Ptrs[i] = GPA.allocate(1); |
81 | EXPECT_NE(nullptr, Ptrs[i]); |
82 | EXPECT_TRUE(GPA.pointerIsMine(Ptrs[i])); |
83 | } |
84 | |
85 | // This allocation should fail as all the slots are used. |
86 | void *Ptr = GPA.allocate(1); |
87 | EXPECT_EQ(nullptr, Ptr); |
88 | EXPECT_FALSE(GPA.pointerIsMine(nullptr)); |
89 | |
90 | for (unsigned i = 0; i < kNumSlots; ++i) |
91 | GPA.deallocate(Ptrs[i]); |
92 | } |
93 | |