xref: /llvm-project/compiler-rt/lib/gwp_asan/tests/harness.cpp (revision 7adb7aa494247f2492f6207289ad90cb48807517)
15556616bSKostya Kortchinsky //===-- harness.cpp ---------------------------------------------*- C++ -*-===//
25556616bSKostya Kortchinsky //
35556616bSKostya Kortchinsky // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45556616bSKostya Kortchinsky // See https://llvm.org/LICENSE.txt for license information.
55556616bSKostya Kortchinsky // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65556616bSKostya Kortchinsky //
75556616bSKostya Kortchinsky //===----------------------------------------------------------------------===//
85556616bSKostya Kortchinsky 
95556616bSKostya Kortchinsky #include "gwp_asan/tests/harness.h"
10596d0614SEvgenii Stepanov 
11bc949f92SMitch Phillips #include <string>
12bc949f92SMitch Phillips 
1335b5499dSMitch Phillips // Optnone to ensure that the calls to these functions are not optimized away,
1435b5499dSMitch Phillips // as we're looking for them in the backtraces.
1535b5499dSMitch Phillips __attribute__((optnone)) char *
AllocateMemory(gwp_asan::GuardedPoolAllocator & GPA)1635b5499dSMitch Phillips AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA) {
1735b5499dSMitch Phillips   return static_cast<char *>(GPA.allocate(1));
1835b5499dSMitch Phillips }
1935b5499dSMitch Phillips __attribute__((optnone)) void
DeallocateMemory(gwp_asan::GuardedPoolAllocator & GPA,void * Ptr)2035b5499dSMitch Phillips DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
2135b5499dSMitch Phillips   GPA.deallocate(Ptr);
2235b5499dSMitch Phillips }
2335b5499dSMitch Phillips __attribute__((optnone)) void
DeallocateMemory2(gwp_asan::GuardedPoolAllocator & GPA,void * Ptr)2435b5499dSMitch Phillips DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
2535b5499dSMitch Phillips   GPA.deallocate(Ptr);
2635b5499dSMitch Phillips }
TouchMemory(void * Ptr)2735b5499dSMitch Phillips __attribute__((optnone)) void TouchMemory(void *Ptr) {
2835b5499dSMitch Phillips   *(reinterpret_cast<volatile char *>(Ptr)) = 7;
2935b5499dSMitch Phillips }
30bc949f92SMitch Phillips 
CheckOnlyOneGwpAsanCrash(const std::string & OutputBuffer)31bc949f92SMitch Phillips void CheckOnlyOneGwpAsanCrash(const std::string &OutputBuffer) {
32bc949f92SMitch Phillips   const char *kGwpAsanErrorString = "GWP-ASan detected a memory error";
33bc949f92SMitch Phillips   size_t FirstIndex = OutputBuffer.find(kGwpAsanErrorString);
34bc949f92SMitch Phillips   ASSERT_NE(FirstIndex, std::string::npos) << "Didn't detect a GWP-ASan crash";
35bc949f92SMitch Phillips   ASSERT_EQ(OutputBuffer.find(kGwpAsanErrorString, FirstIndex + 1),
36bc949f92SMitch Phillips             std::string::npos)
37bc949f92SMitch Phillips       << "Detected more than one GWP-ASan crash:\n"
38bc949f92SMitch Phillips       << OutputBuffer;
39bc949f92SMitch Phillips }
40bc949f92SMitch Phillips 
4143ba3210SCaslyn Tonelli // Fuchsia does not support recoverable GWP-ASan.
4243ba3210SCaslyn Tonelli #if defined(__Fuchsia__)
4343ba3210SCaslyn Tonelli INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests,
4443ba3210SCaslyn Tonelli                          BacktraceGuardedPoolAllocatorDeathTest,
4543ba3210SCaslyn Tonelli                          /* Recoverable */ testing::Values(false));
4643ba3210SCaslyn Tonelli #else
47bc949f92SMitch Phillips INSTANTIATE_TEST_SUITE_P(RecoverableTests, BacktraceGuardedPoolAllocator,
48bc949f92SMitch Phillips                          /* Recoverable */ testing::Values(true));
49*4f1f171fSBenjamin Kramer GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocator);
50bc949f92SMitch Phillips INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests,
51bc949f92SMitch Phillips                          BacktraceGuardedPoolAllocatorDeathTest,
52bc949f92SMitch Phillips                          /* Recoverable */ testing::Bool());
53*4f1f171fSBenjamin Kramer GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BacktraceGuardedPoolAllocatorDeathTest);
5443ba3210SCaslyn Tonelli #endif
55