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 #if defined(__Fuchsia__) 15 #include <zxtest/zxtest.h> 16 using Test = ::zxtest::Test; 17 #else 18 #include "gtest/gtest.h" 19 using Test = ::testing::Test; 20 #endif 21 22 #include "gwp_asan/guarded_pool_allocator.h" 23 #include "gwp_asan/optional/backtrace.h" 24 #include "gwp_asan/optional/printf.h" 25 #include "gwp_asan/optional/segv_handler.h" 26 #include "gwp_asan/options.h" 27 28 namespace gwp_asan { 29 namespace test { 30 // This printf-function getter allows other platforms (e.g. Android) to define 31 // their own signal-safe Printf function. In LLVM, we use 32 // `optional/printf_sanitizer_common.cpp` which supplies the __sanitizer::Printf 33 // for this purpose. 34 Printf_t getPrintfFunction(); 35 36 // First call returns true, all the following calls return false. 37 bool OnlyOnce(); 38 39 }; // namespace test 40 }; // namespace gwp_asan 41 42 class DefaultGuardedPoolAllocator : public Test { 43 public: 44 void SetUp() override { 45 gwp_asan::options::Options Opts; 46 Opts.setDefaults(); 47 MaxSimultaneousAllocations = Opts.MaxSimultaneousAllocations; 48 49 Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce(); 50 GPA.init(Opts); 51 } 52 53 void TearDown() override { GPA.uninitTestOnly(); } 54 55 protected: 56 gwp_asan::GuardedPoolAllocator GPA; 57 decltype(gwp_asan::options::Options::MaxSimultaneousAllocations) 58 MaxSimultaneousAllocations; 59 }; 60 61 class CustomGuardedPoolAllocator : public Test { 62 public: 63 void 64 InitNumSlots(decltype(gwp_asan::options::Options::MaxSimultaneousAllocations) 65 MaxSimultaneousAllocationsArg) { 66 gwp_asan::options::Options Opts; 67 Opts.setDefaults(); 68 69 Opts.MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg; 70 MaxSimultaneousAllocations = MaxSimultaneousAllocationsArg; 71 72 Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce(); 73 GPA.init(Opts); 74 } 75 76 void TearDown() override { GPA.uninitTestOnly(); } 77 78 protected: 79 gwp_asan::GuardedPoolAllocator GPA; 80 decltype(gwp_asan::options::Options::MaxSimultaneousAllocations) 81 MaxSimultaneousAllocations; 82 }; 83 84 class BacktraceGuardedPoolAllocator : public Test { 85 public: 86 void SetUp() override { 87 gwp_asan::options::Options Opts; 88 Opts.setDefaults(); 89 90 Opts.Backtrace = gwp_asan::backtrace::getBacktraceFunction(); 91 Opts.InstallForkHandlers = gwp_asan::test::OnlyOnce(); 92 GPA.init(Opts); 93 94 gwp_asan::segv_handler::installSignalHandlers( 95 &GPA, gwp_asan::test::getPrintfFunction(), 96 gwp_asan::backtrace::getPrintBacktraceFunction(), 97 gwp_asan::backtrace::getSegvBacktraceFunction()); 98 } 99 100 void TearDown() override { 101 GPA.uninitTestOnly(); 102 gwp_asan::segv_handler::uninstallSignalHandlers(); 103 } 104 105 protected: 106 gwp_asan::GuardedPoolAllocator GPA; 107 }; 108 109 // https://github.com/google/googletest/blob/master/docs/advanced.md#death-tests-and-threads 110 using DefaultGuardedPoolAllocatorDeathTest = DefaultGuardedPoolAllocator; 111 using CustomGuardedPoolAllocatorDeathTest = CustomGuardedPoolAllocator; 112 using BacktraceGuardedPoolAllocatorDeathTest = BacktraceGuardedPoolAllocator; 113 114 #endif // GWP_ASAN_TESTS_HARNESS_H_ 115