1 //===-- alignment.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/guarded_pool_allocator.h"
10 #include "gwp_asan/tests/harness.h"
11 
12 class AlignmentTestGPA : public gwp_asan::GuardedPoolAllocator {
13 public:
getRequiredBackingSize(size_t Size,size_t Alignment,size_t PageSize)14   static size_t getRequiredBackingSize(size_t Size, size_t Alignment,
15                                        size_t PageSize) {
16     return GuardedPoolAllocator::getRequiredBackingSize(Size, Alignment,
17                                                         PageSize);
18   }
alignUp(uintptr_t Ptr,size_t Alignment)19   static uintptr_t alignUp(uintptr_t Ptr, size_t Alignment) {
20     return GuardedPoolAllocator::alignUp(Ptr, Alignment);
21   }
alignDown(uintptr_t Ptr,size_t Alignment)22   static uintptr_t alignDown(uintptr_t Ptr, size_t Alignment) {
23     return GuardedPoolAllocator::alignDown(Ptr, Alignment);
24   }
25 };
26 
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.
TEST(AlignmentTest,LeftAlignedAllocs)35 TEST(AlignmentTest, LeftAlignedAllocs) {
36   // Alignment < Page Size.
37   EXPECT_EQ(0x4000, AlignmentTestGPA::alignUp(
38                         /* Ptr */ 0x4000, /* Alignment */ 0x1));
39   // Alignment == Page Size.
40   EXPECT_EQ(0x4000, AlignmentTestGPA::alignUp(
41                         /* Ptr */ 0x4000, /* Alignment */ 0x1000));
42   // Alignment > Page Size.
43   EXPECT_EQ(0x4000, AlignmentTestGPA::alignUp(
44                         /* Ptr */ 0x4000, /* Alignment */ 0x4000));
45 }
46 
TEST(AlignmentTest,SingleByteAllocs)47 TEST(AlignmentTest, SingleByteAllocs) {
48   // Alignment < Page Size.
49   EXPECT_EQ(0x1,
50             AlignmentTestGPA::getRequiredBackingSize(
51                 /* Size */ 0x1, /* Alignment */ 0x1, /* PageSize */ 0x1000));
52   EXPECT_EQ(0x7fff, AlignmentTestGPA::alignDown(
53                         /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1));
54 
55   // Alignment == Page Size.
56   EXPECT_EQ(0x1,
57             AlignmentTestGPA::getRequiredBackingSize(
58                 /* Size */ 0x1, /* Alignment */ 0x1000, /* PageSize */ 0x1000));
59   EXPECT_EQ(0x7000, AlignmentTestGPA::alignDown(
60                         /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1000));
61 
62   // Alignment > Page Size.
63   EXPECT_EQ(0x3001,
64             AlignmentTestGPA::getRequiredBackingSize(
65                 /* Size */ 0x1, /* Alignment */ 0x4000, /* PageSize */ 0x1000));
66   EXPECT_EQ(0x4000, AlignmentTestGPA::alignDown(
67                         /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x4000));
68 }
69 
TEST(AlignmentTest,PageSizedAllocs)70 TEST(AlignmentTest, PageSizedAllocs) {
71   // Alignment < Page Size.
72   EXPECT_EQ(0x1000,
73             AlignmentTestGPA::getRequiredBackingSize(
74                 /* Size */ 0x1000, /* Alignment */ 0x1, /* PageSize */ 0x1000));
75   EXPECT_EQ(0x7000, AlignmentTestGPA::alignDown(
76                         /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1));
77 
78   // Alignment == Page Size.
79   EXPECT_EQ(0x1000, AlignmentTestGPA::getRequiredBackingSize(
80                         /* Size */ 0x1000, /* Alignment */ 0x1000,
81                         /* PageSize */ 0x1000));
82   EXPECT_EQ(0x7000, AlignmentTestGPA::alignDown(
83                         /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1000));
84 
85   // Alignment > Page Size.
86   EXPECT_EQ(0x4000, AlignmentTestGPA::getRequiredBackingSize(
87                         /* Size */ 0x1000, /* Alignment */ 0x4000,
88                         /* PageSize */ 0x1000));
89   EXPECT_EQ(0x4000, AlignmentTestGPA::alignDown(
90                         /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x4000));
91 }
92 
TEST(AlignmentTest,MoreThanPageAllocs)93 TEST(AlignmentTest, MoreThanPageAllocs) {
94   // Alignment < Page Size.
95   EXPECT_EQ(0x2fff,
96             AlignmentTestGPA::getRequiredBackingSize(
97                 /* Size */ 0x2fff, /* Alignment */ 0x1, /* PageSize */ 0x1000));
98   EXPECT_EQ(0x5001, AlignmentTestGPA::alignDown(
99                         /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1));
100 
101   // Alignment == Page Size.
102   EXPECT_EQ(0x2fff, AlignmentTestGPA::getRequiredBackingSize(
103                         /* Size */ 0x2fff, /* Alignment */ 0x1000,
104                         /* PageSize */ 0x1000));
105   EXPECT_EQ(0x5000, AlignmentTestGPA::alignDown(
106                         /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1000));
107 
108   // Alignment > Page Size.
109   EXPECT_EQ(0x5fff, AlignmentTestGPA::getRequiredBackingSize(
110                         /* Size */ 0x2fff, /* Alignment */ 0x4000,
111                         /* PageSize */ 0x1000));
112   EXPECT_EQ(0x4000, AlignmentTestGPA::alignDown(
113                         /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x4000));
114 }
115