summaryrefslogtreecommitdiffstats
path: root/gnu/llvm/compiler-rt/lib/gwp_asan/tests/alignment.cpp
blob: bf98f1f583386180de4b248ac3ac9f3776d50018 (plain) (blame)
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
//===-- 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/tests/harness.h"
#include "gwp_asan/utilities.h"

TEST(AlignmentTest, PowerOfTwo) {
  std::vector<std::pair<size_t, size_t>> AskedSizeToAlignedSize = {
      {1, 1},   {2, 2},   {3, 4},       {4, 4},       {5, 8},   {7, 8},
      {8, 8},   {9, 16},  {15, 16},     {16, 16},     {17, 32}, {31, 32},
      {32, 32}, {33, 48}, {4095, 4096}, {4096, 4096},
  };

  for (const auto &KV : AskedSizeToAlignedSize) {
    EXPECT_EQ(KV.second,
              gwp_asan::rightAlignedAllocationSize(
                  KV.first, gwp_asan::AlignmentStrategy::POWER_OF_TWO));
  }
}

TEST(AlignmentTest, AlignBionic) {
  std::vector<std::pair<size_t, size_t>> AskedSizeToAlignedSize = {
      {1, 8},   {2, 8},   {3, 8},       {4, 8},       {5, 8},   {7, 8},
      {8, 8},   {9, 16},  {15, 16},     {16, 16},     {17, 24}, {31, 32},
      {32, 32}, {33, 40}, {4095, 4096}, {4096, 4096},
  };

  for (const auto &KV : AskedSizeToAlignedSize) {
    EXPECT_EQ(KV.second, gwp_asan::rightAlignedAllocationSize(
                             KV.first, gwp_asan::AlignmentStrategy::BIONIC));
  }
}

TEST(AlignmentTest, PerfectAlignment) {
  for (size_t i = 1; i <= 4096; ++i) {
    EXPECT_EQ(i, gwp_asan::rightAlignedAllocationSize(
                     i, gwp_asan::AlignmentStrategy::PERFECT));
  }
}