xref: /openbsd-src/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.h (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 //===-- harness.h -----------------------------------------------*- 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 #ifndef GWP_ASAN_TESTS_HARNESS_H_
10 #define GWP_ASAN_TESTS_HARNESS_H_
11 
12 #include <stdarg.h>
13 
14 #include "gtest/gtest.h"
15 
16 #include "gwp_asan/guarded_pool_allocator.h"
17 #include "gwp_asan/optional/backtrace.h"
18 #include "gwp_asan/optional/segv_handler.h"
19 #include "gwp_asan/options.h"
20 
21 namespace gwp_asan {
22 namespace test {
23 // This printf-function getter allows other platforms (e.g. Android) to define
24 // their own signal-safe Printf function. In LLVM, we use
25 // `optional/printf_sanitizer_common.cpp` which supplies the __sanitizer::Printf
26 // for this purpose.
27 crash_handler::Printf_t getPrintfFunction();
28 
29 // First call returns true, all the following calls return false.
30 bool OnlyOnce();
31 
32 }; // namespace test
33 }; // namespace gwp_asan
34 
35 class DefaultGuardedPoolAllocator : public ::testing::Test {
36 public:
37   void SetUp() override {
38     gwp_asan::options::Options Opts;
39     Opts.setDefaults();
40     MaxSimultaneousAllocations = Opts.MaxSimultaneousAllocations;
41 
42     Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce();
43     GPA.init(Opts);
44   }
45 
46   void TearDown() override { GPA.uninitTestOnly(); }
47 
48 protected:
49   gwp_asan::GuardedPoolAllocator GPA;
50   decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
51       MaxSimultaneousAllocations;
52 };
53 
54 class CustomGuardedPoolAllocator : public ::testing::Test {
55 public:
56   void
57   InitNumSlots(decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
58                    MaxSimultaneousAllocationsArg) {
59     gwp_asan::options::Options Opts;
60     Opts.setDefaults();
61 
62     Opts.MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
63     MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
64 
65     Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce();
66     GPA.init(Opts);
67   }
68 
69   void TearDown() override { GPA.uninitTestOnly(); }
70 
71 protected:
72   gwp_asan::GuardedPoolAllocator GPA;
73   decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
74       MaxSimultaneousAllocations;
75 };
76 
77 class BacktraceGuardedPoolAllocator : public ::testing::Test {
78 public:
79   void SetUp() override {
80     gwp_asan::options::Options Opts;
81     Opts.setDefaults();
82 
83     Opts.Backtrace = gwp_asan::options::getBacktraceFunction();
84     Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce();
85     GPA.init(Opts);
86 
87     gwp_asan::crash_handler::installSignalHandlers(
88         &GPA, gwp_asan::test::getPrintfFunction(),
89         gwp_asan::options::getPrintBacktraceFunction(), Opts.Backtrace);
90   }
91 
92   void TearDown() override {
93     GPA.uninitTestOnly();
94     gwp_asan::crash_handler::uninstallSignalHandlers();
95   }
96 
97 protected:
98   gwp_asan::GuardedPoolAllocator GPA;
99 };
100 
101 #endif // GWP_ASAN_TESTS_HARNESS_H_
102