• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
OnlyOnce()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 *
AllocateMemory(gwp_asan::GuardedPoolAllocator & GPA)25 AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA) {
26   return static_cast<char *>(GPA.allocate(1));
27 }
28 __attribute__((optnone)) void
DeallocateMemory(gwp_asan::GuardedPoolAllocator & GPA,void * Ptr)29 DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
30   GPA.deallocate(Ptr);
31 }
32 __attribute__((optnone)) void
DeallocateMemory2(gwp_asan::GuardedPoolAllocator & GPA,void * Ptr)33 DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
34   GPA.deallocate(Ptr);
35 }
TouchMemory(void * Ptr)36 __attribute__((optnone)) void TouchMemory(void *Ptr) {
37   *(reinterpret_cast<volatile char *>(Ptr)) = 7;
38 }
39 
CheckOnlyOneGwpAsanCrash(const std::string & OutputBuffer)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 INSTANTIATE_TEST_SUITE_P(RecoverableTests, BacktraceGuardedPoolAllocator,
51                          /* Recoverable */ testing::Values(true));
52 INSTANTIATE_TEST_SUITE_P(RecoverableAndNonRecoverableTests,
53                          BacktraceGuardedPoolAllocatorDeathTest,
54                          /* Recoverable */ testing::Bool());
55