xref: /llvm-project/compiler-rt/lib/gwp_asan/tests/basic.cpp (revision a95edb9dc1ddaf70761e8c90be175f144a28f757)
1*a95edb9dSMitch Phillips //===-- basic.cc ------------------------------------------------*- C++ -*-===//
2*a95edb9dSMitch Phillips //
3*a95edb9dSMitch Phillips // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*a95edb9dSMitch Phillips // See https://llvm.org/LICENSE.txt for license information.
5*a95edb9dSMitch Phillips // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*a95edb9dSMitch Phillips //
7*a95edb9dSMitch Phillips //===----------------------------------------------------------------------===//
8*a95edb9dSMitch Phillips 
9*a95edb9dSMitch Phillips #include "gwp_asan/tests/harness.h"
10*a95edb9dSMitch Phillips 
11*a95edb9dSMitch Phillips TEST_F(CustomGuardedPoolAllocator, BasicAllocation) {
12*a95edb9dSMitch Phillips   InitNumSlots(1);
13*a95edb9dSMitch Phillips   void *Ptr = GPA.allocate(1);
14*a95edb9dSMitch Phillips   EXPECT_NE(nullptr, Ptr);
15*a95edb9dSMitch Phillips   EXPECT_TRUE(GPA.pointerIsMine(Ptr));
16*a95edb9dSMitch Phillips   EXPECT_EQ(1u, GPA.getSize(Ptr));
17*a95edb9dSMitch Phillips   GPA.deallocate(Ptr);
18*a95edb9dSMitch Phillips }
19*a95edb9dSMitch Phillips 
20*a95edb9dSMitch Phillips TEST_F(DefaultGuardedPoolAllocator, NullptrIsNotMine) {
21*a95edb9dSMitch Phillips   EXPECT_FALSE(GPA.pointerIsMine(nullptr));
22*a95edb9dSMitch Phillips }
23*a95edb9dSMitch Phillips 
24*a95edb9dSMitch Phillips TEST_F(CustomGuardedPoolAllocator, SizedAllocations) {
25*a95edb9dSMitch Phillips   InitNumSlots(1);
26*a95edb9dSMitch Phillips 
27*a95edb9dSMitch Phillips   std::size_t MaxAllocSize = GPA.maximumAllocationSize();
28*a95edb9dSMitch Phillips   EXPECT_TRUE(MaxAllocSize > 0);
29*a95edb9dSMitch Phillips 
30*a95edb9dSMitch Phillips   for (unsigned AllocSize = 1; AllocSize <= MaxAllocSize; AllocSize <<= 1) {
31*a95edb9dSMitch Phillips     void *Ptr = GPA.allocate(AllocSize);
32*a95edb9dSMitch Phillips     EXPECT_NE(nullptr, Ptr);
33*a95edb9dSMitch Phillips     EXPECT_TRUE(GPA.pointerIsMine(Ptr));
34*a95edb9dSMitch Phillips     EXPECT_EQ(AllocSize, GPA.getSize(Ptr));
35*a95edb9dSMitch Phillips     GPA.deallocate(Ptr);
36*a95edb9dSMitch Phillips   }
37*a95edb9dSMitch Phillips }
38*a95edb9dSMitch Phillips 
39*a95edb9dSMitch Phillips TEST_F(DefaultGuardedPoolAllocator, TooLargeAllocation) {
40*a95edb9dSMitch Phillips   EXPECT_EQ(nullptr, GPA.allocate(GPA.maximumAllocationSize() + 1));
41*a95edb9dSMitch Phillips }
42*a95edb9dSMitch Phillips 
43*a95edb9dSMitch Phillips TEST_F(CustomGuardedPoolAllocator, AllocAllSlots) {
44*a95edb9dSMitch Phillips   constexpr unsigned kNumSlots = 128;
45*a95edb9dSMitch Phillips   InitNumSlots(kNumSlots);
46*a95edb9dSMitch Phillips   void *Ptrs[kNumSlots];
47*a95edb9dSMitch Phillips   for (unsigned i = 0; i < kNumSlots; ++i) {
48*a95edb9dSMitch Phillips     Ptrs[i] = GPA.allocate(1);
49*a95edb9dSMitch Phillips     EXPECT_NE(nullptr, Ptrs[i]);
50*a95edb9dSMitch Phillips     EXPECT_TRUE(GPA.pointerIsMine(Ptrs[i]));
51*a95edb9dSMitch Phillips   }
52*a95edb9dSMitch Phillips 
53*a95edb9dSMitch Phillips   // This allocation should fail as all the slots are used.
54*a95edb9dSMitch Phillips   void *Ptr = GPA.allocate(1);
55*a95edb9dSMitch Phillips   EXPECT_EQ(nullptr, Ptr);
56*a95edb9dSMitch Phillips   EXPECT_FALSE(GPA.pointerIsMine(nullptr));
57*a95edb9dSMitch Phillips 
58*a95edb9dSMitch Phillips   for (unsigned i = 0; i < kNumSlots; ++i)
59*a95edb9dSMitch Phillips     GPA.deallocate(Ptrs[i]);
60*a95edb9dSMitch Phillips }
61