File: debugaligntest.cc

package info (click to toggle)
dune-common 2.11.0-1~exp2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 6,056 kB
  • sloc: cpp: 54,404; python: 4,136; sh: 1,657; makefile: 17
file content (100 lines) | stat: -rw-r--r-- 3,239 bytes parent folder | download
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
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#include <cstdint>
#include <new>
#include <tuple>
#include <utility>

#include <dune/common/debugalign.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/common/parallel/mpihelper.hh>
#include <dune/common/test/arithmetictestsuite.hh>
#include <dune/common/test/testsuite.hh>

//! Exception thrown when an object is allocated with a misaligned address
class MisalignedAddress : public Dune::RangeError {};

class WithViolatedAlignmentHandler {
  Dune::ViolatedAlignmentHandler oldhandler;
public:
  template<class H>
  WithViolatedAlignmentHandler(H &&newhandler) :
    oldhandler(Dune::violatedAlignmentHandler())
  {
    Dune::violatedAlignmentHandler() = std::forward<H>(newhandler);
  }

  WithViolatedAlignmentHandler(const WithViolatedAlignmentHandler &) = delete;
  WithViolatedAlignmentHandler(WithViolatedAlignmentHandler &&) = delete;

  WithViolatedAlignmentHandler&
  operator=(const WithViolatedAlignmentHandler &) = delete;
  WithViolatedAlignmentHandler&
  operator=(WithViolatedAlignmentHandler &&) = delete;

  ~WithViolatedAlignmentHandler()
  {
    Dune::violatedAlignmentHandler() = oldhandler;
  }
};

// intentionally violate alignment and check that that is detected
template<class T>
void checkAlignmentViolation(Dune::TestSuite &test)
{
  WithViolatedAlignmentHandler
    guard([&](auto&&...){ throw MisalignedAddress{}; });

  static_assert(alignof(T) <= sizeof(T));
  char buffer[alignof(T)+sizeof(T)];

  void* misalignedAddr = std::addressof(buffer);
  {
    // take any address, align it to the correct value...
    std::size_t space = alignof(T)+sizeof(T);
    misalignedAddr = std::align(alignof(T), sizeof(T), misalignedAddr, space);
    // and move one past its alignmed address to make sure it's misalingned
    misalignedAddr = static_cast<char*>(misalignedAddr) + 1;
    test.check(not Dune::isAligned(misalignedAddr, alignof(T)), "We could not misalign an address");
  }

  test.checkThrow<MisalignedAddress>([&]{
    std::ignore = new(misalignedAddr) T;
  }, "default construct") << "misaligned address was not caught" << Dune::className<T>();

  test.checkThrow<MisalignedAddress>([&]{
    std::ignore = new(misalignedAddr) T(T(0));
  }, "move construct") << "misaligned address was not caught";

  T t(0);
  test.checkThrow<MisalignedAddress>([&]{
    std::ignore = new(misalignedAddr) T(t);
  }, "copy construct") << "misaligned address was not caught";
}

int main(int argc, char **argv)
{
  Dune::MPIHelper::instance(argc, argv);

  Dune::ArithmeticTestSuite test;

  using ArithmeticTypes = std::tuple<
    bool,
    char, signed char, unsigned char,
    short, unsigned short,
    int, unsigned,
    long, long unsigned,
    long long, long long unsigned,
    wchar_t, char16_t, char32_t,
    float, double, long double>;

  Dune::Hybrid::forEach(ArithmeticTypes(), [&](auto val) {
      using T = decltype(val);
      using Aligned = Dune::AlignedNumber<T>;
      test.checkArithmetic<Aligned, T>();
      if (alignof(T) > 1)
        checkAlignmentViolation<Aligned>(test);
    });

  return test.exit();
}