55 lines · cpp
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-exception6//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 *16AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA) {17 return static_cast<char *>(GPA.allocate(1));18}19__attribute__((optnone)) void20DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {21 GPA.deallocate(Ptr);22}23__attribute__((optnone)) void24DeallocateMemory2(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 31void 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__)43INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests,44 BacktraceGuardedPoolAllocatorDeathTest,45 /* Recoverable */ testing::Values(false));46#else47INSTANTIATE_TEST_SUITE_P(RecoverableTests, BacktraceGuardedPoolAllocator,48 /* Recoverable */ testing::Values(true));49GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocator);50INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests,51 BacktraceGuardedPoolAllocatorDeathTest,52 /* Recoverable */ testing::Bool());53GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocatorDeathTest);54#endif55