summaryrefslogtreecommitdiff
path: root/gwp_asan/tests/alignment.cpp
blob: 9f150467c79af3435ed9ceb65cdec55788eb657c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//===-- alignment.cpp -------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "gwp_asan/guarded_pool_allocator.h"
#include "gwp_asan/tests/harness.h"

class AlignmentTestGPA : public gwp_asan::GuardedPoolAllocator {
public:
  static size_t getRequiredBackingSize(size_t Size, size_t Alignment,
                                       size_t PageSize) {
    return GuardedPoolAllocator::getRequiredBackingSize(Size, Alignment,
                                                        PageSize);
  }
  static uintptr_t alignUp(uintptr_t Ptr, size_t Alignment) {
    return GuardedPoolAllocator::alignUp(Ptr, Alignment);
  }
  static uintptr_t alignDown(uintptr_t Ptr, size_t Alignment) {
    return GuardedPoolAllocator::alignDown(Ptr, Alignment);
  }
};

// Global assumptions for these tests:
//   1. Page size is 0x1000.
//   2. All tests assume a slot is multipage, between 0x4000 - 0x8000. While we
//      don't use multipage slots right now, this tests more boundary conditions
//      and allows us to add this feature at a later date without rewriting the
//      alignment functionality.
// These aren't actual requirements of the allocator - but just simplifies the
// numerics of the testing.
TEST(AlignmentTest, LeftAlignedAllocs) {
  // Alignment < Page Size.
  EXPECT_EQ(0x4000u, AlignmentTestGPA::alignUp(
                         /* Ptr */ 0x4000, /* Alignment */ 0x1));
  // Alignment == Page Size.
  EXPECT_EQ(0x4000u, AlignmentTestGPA::alignUp(
                         /* Ptr */ 0x4000, /* Alignment */ 0x1000));
  // Alignment > Page Size.
  EXPECT_EQ(0x4000u, AlignmentTestGPA::alignUp(
                         /* Ptr */ 0x4000, /* Alignment */ 0x4000));
}

TEST(AlignmentTest, SingleByteAllocs) {
  // Alignment < Page Size.
  EXPECT_EQ(0x1u,
            AlignmentTestGPA::getRequiredBackingSize(
                /* Size */ 0x1, /* Alignment */ 0x1, /* PageSize */ 0x1000));
  EXPECT_EQ(0x7fffu, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1));

  // Alignment == Page Size.
  EXPECT_EQ(0x1u,
            AlignmentTestGPA::getRequiredBackingSize(
                /* Size */ 0x1, /* Alignment */ 0x1000, /* PageSize */ 0x1000));
  EXPECT_EQ(0x7000u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x1000));

  // Alignment > Page Size.
  EXPECT_EQ(0x3001u,
            AlignmentTestGPA::getRequiredBackingSize(
                /* Size */ 0x1, /* Alignment */ 0x4000, /* PageSize */ 0x1000));
  EXPECT_EQ(0x4000u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x1, /* Alignment */ 0x4000));
}

TEST(AlignmentTest, PageSizedAllocs) {
  // Alignment < Page Size.
  EXPECT_EQ(0x1000u,
            AlignmentTestGPA::getRequiredBackingSize(
                /* Size */ 0x1000, /* Alignment */ 0x1, /* PageSize */ 0x1000));
  EXPECT_EQ(0x7000u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1));

  // Alignment == Page Size.
  EXPECT_EQ(0x1000u, AlignmentTestGPA::getRequiredBackingSize(
                         /* Size */ 0x1000, /* Alignment */ 0x1000,
                         /* PageSize */ 0x1000));
  EXPECT_EQ(0x7000u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x1000));

  // Alignment > Page Size.
  EXPECT_EQ(0x4000u, AlignmentTestGPA::getRequiredBackingSize(
                         /* Size */ 0x1000, /* Alignment */ 0x4000,
                         /* PageSize */ 0x1000));
  EXPECT_EQ(0x4000u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x1000, /* Alignment */ 0x4000));
}

TEST(AlignmentTest, MoreThanPageAllocs) {
  // Alignment < Page Size.
  EXPECT_EQ(0x2fffu,
            AlignmentTestGPA::getRequiredBackingSize(
                /* Size */ 0x2fff, /* Alignment */ 0x1, /* PageSize */ 0x1000));
  EXPECT_EQ(0x5001u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1));

  // Alignment == Page Size.
  EXPECT_EQ(0x2fffu, AlignmentTestGPA::getRequiredBackingSize(
                         /* Size */ 0x2fff, /* Alignment */ 0x1000,
                         /* PageSize */ 0x1000));
  EXPECT_EQ(0x5000u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x1000));

  // Alignment > Page Size.
  EXPECT_EQ(0x5fffu, AlignmentTestGPA::getRequiredBackingSize(
                         /* Size */ 0x2fff, /* Alignment */ 0x4000,
                         /* PageSize */ 0x1000));
  EXPECT_EQ(0x4000u, AlignmentTestGPA::alignDown(
                         /* Ptr */ 0x8000 - 0x2fff, /* Alignment */ 0x4000));
}