1 //===-- alignment.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/guarded_pool_allocator.h"
10 #include "gwp_asan/tests/harness.h"
12 class AlignmentTestGPA
: public gwp_asan::GuardedPoolAllocator
{
14 static size_t getRequiredBackingSize(size_t Size
, size_t Alignment
,
16 return GuardedPoolAllocator::getRequiredBackingSize(Size
, Alignment
,
19 static uintptr_t alignUp(uintptr_t Ptr
, size_t Alignment
) {
20 return GuardedPoolAllocator::alignUp(Ptr
, Alignment
);
22 static uintptr_t alignDown(uintptr_t Ptr
, size_t Alignment
) {
23 return GuardedPoolAllocator::alignDown(Ptr
, Alignment
);
27 // Global assumptions for these tests:
28 // 1. Page size is 0x1000.
29 // 2. All tests assume a slot is multipage, between 0x4000 - 0x8000. While we
30 // don't use multipage slots right now, this tests more boundary conditions
31 // and allows us to add this feature at a later date without rewriting the
32 // alignment functionality.
33 // These aren't actual requirements of the allocator - but just simplifies the
34 // numerics of the testing.
35 TEST(AlignmentTest
, LeftAlignedAllocs
) {
36 // Alignment < Page Size.
37 EXPECT_EQ(0x4000u
, AlignmentTestGPA::alignUp(
38 /* Ptr */ 0x4000, /* Alignment */ 0x1));
39 // Alignment == Page Size.
40 EXPECT_EQ(0x4000u
, AlignmentTestGPA::alignUp(
41 /* Ptr */ 0x4000, /* Alignment */ 0x1000));
42 // Alignment > Page Size.
43 EXPECT_EQ(0x4000u
, AlignmentTestGPA::alignUp(
44 /* Ptr */ 0x4000, /* Alignment */ 0x4000));
47 TEST(AlignmentTest
, SingleByteAllocs
) {
48 // Alignment < Page Size.
50 AlignmentTestGPA::getRequiredBackingSize(
51 /* Size */ 0x1, /* Alignment */ 0x1, /* PageSize */ 0x1000));
52 EXPECT_EQ(0x7fffu
, AlignmentTestGPA::alignDown(
53 /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1));
55 // Alignment == Page Size.
57 AlignmentTestGPA::getRequiredBackingSize(
58 /* Size */ 0x1, /* Alignment */ 0x1000, /* PageSize */ 0x1000));
59 EXPECT_EQ(0x7000u
, AlignmentTestGPA::alignDown(
60 /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1000));
62 // Alignment > Page Size.
64 AlignmentTestGPA::getRequiredBackingSize(
65 /* Size */ 0x1, /* Alignment */ 0x4000, /* PageSize */ 0x1000));
66 EXPECT_EQ(0x4000u
, AlignmentTestGPA::alignDown(
67 /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x4000));
70 TEST(AlignmentTest
, PageSizedAllocs
) {
71 // Alignment < Page Size.
73 AlignmentTestGPA::getRequiredBackingSize(
74 /* Size */ 0x1000, /* Alignment */ 0x1, /* PageSize */ 0x1000));
75 EXPECT_EQ(0x7000u
, AlignmentTestGPA::alignDown(
76 /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1));
78 // Alignment == Page Size.
79 EXPECT_EQ(0x1000u
, AlignmentTestGPA::getRequiredBackingSize(
80 /* Size */ 0x1000, /* Alignment */ 0x1000,
81 /* PageSize */ 0x1000));
82 EXPECT_EQ(0x7000u
, AlignmentTestGPA::alignDown(
83 /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1000));
85 // Alignment > Page Size.
86 EXPECT_EQ(0x4000u
, AlignmentTestGPA::getRequiredBackingSize(
87 /* Size */ 0x1000, /* Alignment */ 0x4000,
88 /* PageSize */ 0x1000));
89 EXPECT_EQ(0x4000u
, AlignmentTestGPA::alignDown(
90 /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x4000));
93 TEST(AlignmentTest
, MoreThanPageAllocs
) {
94 // Alignment < Page Size.
96 AlignmentTestGPA::getRequiredBackingSize(
97 /* Size */ 0x2fff, /* Alignment */ 0x1, /* PageSize */ 0x1000));
98 EXPECT_EQ(0x5001u
, AlignmentTestGPA::alignDown(
99 /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1));
101 // Alignment == Page Size.
102 EXPECT_EQ(0x2fffu
, AlignmentTestGPA::getRequiredBackingSize(
103 /* Size */ 0x2fff, /* Alignment */ 0x1000,
104 /* PageSize */ 0x1000));
105 EXPECT_EQ(0x5000u
, AlignmentTestGPA::alignDown(
106 /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1000));
108 // Alignment > Page Size.
109 EXPECT_EQ(0x5fffu
, AlignmentTestGPA::getRequiredBackingSize(
110 /* Size */ 0x2fff, /* Alignment */ 0x4000,
111 /* PageSize */ 0x1000));
112 EXPECT_EQ(0x4000u
, AlignmentTestGPA::alignDown(
113 /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x4000));