1 | //===-- harness.cpp ---------------------------------------------*- 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 | #include "gwp_asan/tests/harness.h" |
10 | |
11 | #include <string> |
12 | |
13 | // Optnone to ensure that the calls to these functions are not optimized away, |
14 | // as we're looking for them in the backtraces. |
15 | __attribute__((optnone)) char * |
16 | AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA) { |
17 | return static_cast<char *>(GPA.allocate(Size: 1)); |
18 | } |
19 | __attribute__((optnone)) void |
20 | DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) { |
21 | GPA.deallocate(Ptr); |
22 | } |
23 | __attribute__((optnone)) void |
24 | DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) { |
25 | GPA.deallocate(Ptr); |
26 | } |
27 | __attribute__((optnone)) void TouchMemory(void *Ptr) { |
28 | *(reinterpret_cast<volatile char *>(Ptr)) = 7; |
29 | } |
30 | |
31 | void CheckOnlyOneGwpAsanCrash(const std::string &OutputBuffer) { |
32 | const char *kGwpAsanErrorString = "GWP-ASan detected a memory error" ; |
33 | size_t FirstIndex = OutputBuffer.find(kGwpAsanErrorString); |
34 | ASSERT_NE(FirstIndex, std::string::npos) << "Didn't detect a GWP-ASan crash" ; |
35 | ASSERT_EQ(OutputBuffer.find(kGwpAsanErrorString, FirstIndex + 1), |
36 | std::string::npos) |
37 | << "Detected more than one GWP-ASan crash:\n" |
38 | << OutputBuffer; |
39 | } |
40 | |
41 | // Fuchsia does not support recoverable GWP-ASan. |
42 | #if defined(__Fuchsia__) |
43 | INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests, |
44 | BacktraceGuardedPoolAllocatorDeathTest, |
45 | /* Recoverable */ testing::Values(false)); |
46 | #else |
47 | INSTANTIATE_TEST_SUITE_P(RecoverableTests, BacktraceGuardedPoolAllocator, |
48 | /* Recoverable */ testing::Values(true)); |
49 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocator); |
50 | INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests, |
51 | BacktraceGuardedPoolAllocatorDeathTest, |
52 | /* Recoverable */ testing::Bool()); |
53 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocatorDeathTest); |
54 | #endif |
55 | |