1 //===-- harness.h -----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef GWP_ASAN_TESTS_HARNESS_H_
10 #define GWP_ASAN_TESTS_HARNESS_H_
14 #if defined(__Fuchsia__)
15 #define ZXTEST_USE_STREAMABLE_MACROS
16 #include <zxtest/zxtest.h>
17 namespace testing
= zxtest
;
18 // zxtest defines a different ASSERT_DEATH, taking a lambda and an error message
19 // if death didn't occur, versus gtest taking a statement and a string to search
20 // for in the dying process. zxtest doesn't define an EXPECT_DEATH, so we use
21 // that in the tests below (which works as intended for gtest), and we define
22 // EXPECT_DEATH as a wrapper for zxtest's ASSERT_DEATH. Note that zxtest drops
23 // the functionality for checking for a particular message in death.
24 #define EXPECT_DEATH(X, Y) ASSERT_DEATH(([&] { X; }), "")
26 #include "gtest/gtest.h"
29 #include "gwp_asan/guarded_pool_allocator.h"
30 #include "gwp_asan/optional/backtrace.h"
31 #include "gwp_asan/optional/printf.h"
32 #include "gwp_asan/optional/segv_handler.h"
33 #include "gwp_asan/options.h"
37 // This printf-function getter allows other platforms (e.g. Android) to define
38 // their own signal-safe Printf function. In LLVM, we use
39 // `optional/printf_sanitizer_common.cpp` which supplies the __sanitizer::Printf
41 Printf_t
getPrintfFunction();
43 // First call returns true, all the following calls return false.
47 }; // namespace gwp_asan
49 char *AllocateMemory(gwp_asan::GuardedPoolAllocator
&GPA
);
50 void DeallocateMemory(gwp_asan::GuardedPoolAllocator
&GPA
, void *Ptr
);
51 void DeallocateMemory2(gwp_asan::GuardedPoolAllocator
&GPA
, void *Ptr
);
52 void TouchMemory(void *Ptr
);
54 void CheckOnlyOneGwpAsanCrash(const std::string
&OutputBuffer
);
56 class DefaultGuardedPoolAllocator
: public ::testing::Test
{
58 void SetUp() override
{
59 gwp_asan::options::Options Opts
;
61 MaxSimultaneousAllocations
= Opts
.MaxSimultaneousAllocations
;
63 Opts
.InstallForkHandlers
= gwp_asan::test::OnlyOnce();
67 void TearDown() override
{ GPA
.uninitTestOnly(); }
70 gwp_asan::GuardedPoolAllocator GPA
;
71 decltype(gwp_asan::options::Options::MaxSimultaneousAllocations
)
72 MaxSimultaneousAllocations
;
75 class CustomGuardedPoolAllocator
: public ::testing::Test
{
78 InitNumSlots(decltype(gwp_asan::options::Options::MaxSimultaneousAllocations
)
79 MaxSimultaneousAllocationsArg
) {
80 gwp_asan::options::Options Opts
;
83 Opts
.MaxSimultaneousAllocations
= MaxSimultaneousAllocationsArg
;
84 MaxSimultaneousAllocations
= MaxSimultaneousAllocationsArg
;
86 Opts
.InstallForkHandlers
= gwp_asan::test::OnlyOnce();
90 void TearDown() override
{ GPA
.uninitTestOnly(); }
93 gwp_asan::GuardedPoolAllocator GPA
;
94 decltype(gwp_asan::options::Options::MaxSimultaneousAllocations
)
95 MaxSimultaneousAllocations
;
98 class BacktraceGuardedPoolAllocator
99 : public ::testing::TestWithParam
</* Recoverable */ bool> {
101 void SetUp() override
{
102 gwp_asan::options::Options Opts
;
105 Opts
.Backtrace
= gwp_asan::backtrace::getBacktraceFunction();
106 Opts
.InstallForkHandlers
= gwp_asan::test::OnlyOnce();
109 // In recoverable mode, capture GWP-ASan logs to an internal buffer so that
110 // we can search it in unit tests. For non-recoverable tests, the default
111 // buffer is fine, as any tests should be EXPECT_DEATH()'d.
112 Recoverable
= GetParam();
113 gwp_asan::Printf_t PrintfFunction
= PrintfToBuffer
;
114 GetOutputBuffer().clear();
116 PrintfFunction
= gwp_asan::test::getPrintfFunction();
118 gwp_asan::segv_handler::installSignalHandlers(
119 &GPA
, PrintfFunction
, gwp_asan::backtrace::getPrintBacktraceFunction(),
120 gwp_asan::backtrace::getSegvBacktraceFunction(),
121 /* Recoverable */ Recoverable
);
124 void TearDown() override
{
125 GPA
.uninitTestOnly();
126 gwp_asan::segv_handler::uninstallSignalHandlers();
130 static std::string
&GetOutputBuffer() {
131 static std::string Buffer
;
135 __attribute__((format(printf
, 1, 2))) static void
136 PrintfToBuffer(const char *Format
, ...) {
138 va_start(AP
, Format
);
140 vsnprintf(Buffer
, sizeof(Buffer
), Format
, AP
);
141 GetOutputBuffer() += Buffer
;
145 gwp_asan::GuardedPoolAllocator GPA
;
149 // https://github.com/google/googletest/blob/master/docs/advanced.md#death-tests-and-threads
150 using DefaultGuardedPoolAllocatorDeathTest
= DefaultGuardedPoolAllocator
;
151 using CustomGuardedPoolAllocatorDeathTest
= CustomGuardedPoolAllocator
;
152 using BacktraceGuardedPoolAllocatorDeathTest
= BacktraceGuardedPoolAllocator
;
154 #endif // GWP_ASAN_TESTS_HARNESS_H_