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