1 //===-- slot_reuse.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"
13 void singleByteGoodAllocDealloc(gwp_asan::GuardedPoolAllocator
*GPA
) {
14 void *Ptr
= GPA
->allocate(1);
15 EXPECT_NE(nullptr, Ptr
);
16 EXPECT_TRUE(GPA
->pointerIsMine(Ptr
));
17 EXPECT_EQ(1u, GPA
->getSize(Ptr
));
21 TEST_F(CustomGuardedPoolAllocator
, EnsureReuseOfQuarantine1
) {
23 for (unsigned i
= 0; i
< 128; ++i
)
24 singleByteGoodAllocDealloc(&GPA
);
27 TEST_F(CustomGuardedPoolAllocator
, EnsureReuseOfQuarantine2
) {
29 for (unsigned i
= 0; i
< 128; ++i
)
30 singleByteGoodAllocDealloc(&GPA
);
33 TEST_F(CustomGuardedPoolAllocator
, EnsureReuseOfQuarantine127
) {
35 for (unsigned i
= 0; i
< 128; ++i
)
36 singleByteGoodAllocDealloc(&GPA
);
39 // This test ensures that our slots are not reused ahead of time. We increase
40 // the use-after-free detection by not reusing slots until all of them have been
41 // allocated. This is done by always using the slots from left-to-right in the
42 // pool before we used each slot once, at which point random selection takes
44 void runNoReuseBeforeNecessary(gwp_asan::GuardedPoolAllocator
*GPA
,
46 std::set
<void *> Ptrs
;
47 for (unsigned i
= 0; i
< PoolSize
; ++i
) {
48 void *Ptr
= GPA
->allocate(1);
50 EXPECT_TRUE(GPA
->pointerIsMine(Ptr
));
51 EXPECT_EQ(0u, Ptrs
.count(Ptr
));
58 TEST_F(CustomGuardedPoolAllocator
, NoReuseBeforeNecessary2
) {
59 constexpr unsigned kPoolSize
= 2;
60 InitNumSlots(kPoolSize
);
61 runNoReuseBeforeNecessary(&GPA
, kPoolSize
);
64 TEST_F(CustomGuardedPoolAllocator
, NoReuseBeforeNecessary128
) {
65 constexpr unsigned kPoolSize
= 128;
66 InitNumSlots(kPoolSize
);
67 runNoReuseBeforeNecessary(&GPA
, kPoolSize
);
70 TEST_F(CustomGuardedPoolAllocator
, NoReuseBeforeNecessary129
) {
71 constexpr unsigned kPoolSize
= 129;
72 InitNumSlots(kPoolSize
);
73 runNoReuseBeforeNecessary(&GPA
, kPoolSize
);