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
13namespace gwp_asan {
14namespace test {
15bool 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 *
25AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA) {
26 return static_cast<char *>(GPA.allocate(Size: 1));
27}
28__attribute__((optnone)) void
29DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
30 GPA.deallocate(Ptr);
31}
32__attribute__((optnone)) void
33DeallocateMemory2(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
40void 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__)
52INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests,
53 BacktraceGuardedPoolAllocatorDeathTest,
54 /* Recoverable */ testing::Values(false));
55#else
56INSTANTIATE_TEST_SUITE_P(RecoverableTests, BacktraceGuardedPoolAllocator,
57 /* Recoverable */ testing::Values(true));
58GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocator);
59INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests,
60 BacktraceGuardedPoolAllocatorDeathTest,
61 /* Recoverable */ testing::Bool());
62GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocatorDeathTest);
63#endif
64

source code of compiler-rt/lib/gwp_asan/tests/harness.cpp