1 //===-- harness.cpp ---------------------------------------------*- 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 #include "gwp_asan/tests/harness.h"
20 } // namespace gwp_asan
22 // Optnone to ensure that the calls to these functions are not optimized away,
23 // as we're looking for them in the backtraces.
24 __attribute__((optnone
)) char *
25 AllocateMemory(gwp_asan::GuardedPoolAllocator
&GPA
) {
26 return static_cast<char *>(GPA
.allocate(1));
28 __attribute__((optnone
)) void
29 DeallocateMemory(gwp_asan::GuardedPoolAllocator
&GPA
, void *Ptr
) {
32 __attribute__((optnone
)) void
33 DeallocateMemory2(gwp_asan::GuardedPoolAllocator
&GPA
, void *Ptr
) {
36 __attribute__((optnone
)) void TouchMemory(void *Ptr
) {
37 *(reinterpret_cast<volatile char *>(Ptr
)) = 7;
40 void CheckOnlyOneGwpAsanCrash(const std::string
&OutputBuffer
) {
41 const char *kGwpAsanErrorString
= "GWP-ASan detected a memory error";
42 size_t FirstIndex
= OutputBuffer
.find(kGwpAsanErrorString
);
43 ASSERT_NE(FirstIndex
, std::string::npos
) << "Didn't detect a GWP-ASan crash";
44 ASSERT_EQ(OutputBuffer
.find(kGwpAsanErrorString
, FirstIndex
+ 1),
46 << "Detected more than one GWP-ASan crash:\n"
50 // Fuchsia does not support recoverable GWP-ASan.
51 #if defined(__Fuchsia__)
52 INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests
,
53 BacktraceGuardedPoolAllocatorDeathTest
,
54 /* Recoverable */ testing::Values(false));
56 INSTANTIATE_TEST_SUITE_P(RecoverableTests
, BacktraceGuardedPoolAllocator
,
57 /* Recoverable */ testing::Values(true));
58 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocator
);
59 INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests
,
60 BacktraceGuardedPoolAllocatorDeathTest
,
61 /* Recoverable */ testing::Bool());
62 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocatorDeathTest
);