1 //===-- basic.cpp -----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "gwp_asan/tests/harness.h"
11 TEST_F(CustomGuardedPoolAllocator
, BasicAllocation
) {
13 void *Ptr
= GPA
.allocate(1);
14 EXPECT_NE(nullptr, Ptr
);
15 EXPECT_TRUE(GPA
.pointerIsMine(Ptr
));
16 EXPECT_EQ(1u, GPA
.getSize(Ptr
));
20 TEST_F(DefaultGuardedPoolAllocator
, NullptrIsNotMine
) {
21 EXPECT_FALSE(GPA
.pointerIsMine(nullptr));
24 TEST_F(CustomGuardedPoolAllocator
, SizedAllocations
) {
27 std::size_t MaxAllocSize
= GPA
.getAllocatorState()->maximumAllocationSize();
28 EXPECT_TRUE(MaxAllocSize
> 0);
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
));
39 TEST_F(DefaultGuardedPoolAllocator
, TooLargeAllocation
) {
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));
49 TEST_F(DefaultGuardedPoolAllocator
, ZeroSizeAndAlignmentAllocations
) {
51 EXPECT_NE(nullptr, (P
= GPA
.allocate(0, 0)));
53 EXPECT_NE(nullptr, (P
= GPA
.allocate(1, 0)));
55 EXPECT_NE(nullptr, (P
= GPA
.allocate(0, 1)));
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
));
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));
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
]));
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));
90 for (unsigned i
= 0; i
< kNumSlots
; ++i
)
91 GPA
.deallocate(Ptrs
[i
]);