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 | namespace gwp_asan { |
14 | namespace test { |
15 | bool OnlyOnce() { |
16 | static int x = 0; |
17 | return !x++; |
18 | } |
19 | } // namespace test |
20 | } // namespace gwp_asan |
21 | |
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(Size: 1)); |
27 | } |
28 | __attribute__((optnone)) void |
29 | DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) { |
30 | GPA.deallocate(Ptr); |
31 | } |
32 | __attribute__((optnone)) void |
33 | DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) { |
34 | GPA.deallocate(Ptr); |
35 | } |
36 | __attribute__((optnone)) void TouchMemory(void *Ptr) { |
37 | *(reinterpret_cast<volatile char *>(Ptr)) = 7; |
38 | } |
39 | |
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), |
45 | std::string::npos) |
46 | << "Detected more than one GWP-ASan crash:\n" |
47 | << OutputBuffer; |
48 | } |
49 | |
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)); |
55 | #else |
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); |
63 | #endif |
64 | |