xref: /openbsd-src/gnu/llvm/compiler-rt/lib/gwp_asan/tests/harness.h (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
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/options.h"
19 
20 namespace gwp_asan {
21 namespace test {
22 // This printf-function getter allows other platforms (e.g. Android) to define
23 // their own signal-safe Printf function. In LLVM, we use
24 // `optional/printf_sanitizer_common.cpp` which supplies the __sanitizer::Printf
25 // for this purpose.
26 options::Printf_t getPrintfFunction();
27 }; // namespace test
28 }; // namespace gwp_asan
29 
30 class DefaultGuardedPoolAllocator : public ::testing::Test {
31 public:
32   DefaultGuardedPoolAllocator() {
33     gwp_asan::options::Options Opts;
34     Opts.setDefaults();
35     MaxSimultaneousAllocations = Opts.MaxSimultaneousAllocations;
36 
37     Opts.Printf = gwp_asan::test::getPrintfFunction();
38     GPA.init(Opts);
39   }
40 
41 protected:
42   gwp_asan::GuardedPoolAllocator GPA;
43   decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
44       MaxSimultaneousAllocations;
45 };
46 
47 class CustomGuardedPoolAllocator : public ::testing::Test {
48 public:
49   void
50   InitNumSlots(decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
51                    MaxSimultaneousAllocationsArg) {
52     gwp_asan::options::Options Opts;
53     Opts.setDefaults();
54 
55     Opts.MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
56     MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg;
57 
58     Opts.Printf = gwp_asan::test::getPrintfFunction();
59     GPA.init(Opts);
60   }
61 
62 protected:
63   gwp_asan::GuardedPoolAllocator GPA;
64   decltype(gwp_asan::options::Options::MaxSimultaneousAllocations)
65       MaxSimultaneousAllocations;
66 };
67 
68 class BacktraceGuardedPoolAllocator : public ::testing::Test {
69 public:
70   BacktraceGuardedPoolAllocator() {
71     gwp_asan::options::Options Opts;
72     Opts.setDefaults();
73 
74     Opts.Printf = gwp_asan::test::getPrintfFunction();
75     Opts.Backtrace = gwp_asan::options::getBacktraceFunction();
76     Opts.PrintBacktrace = gwp_asan::options::getPrintBacktraceFunction();
77     GPA.init(Opts);
78   }
79 
80 protected:
81   gwp_asan::GuardedPoolAllocator GPA;
82 };
83 
84 #endif // GWP_ASAN_TESTS_HARNESS_H_
85