Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / gwp_asan / tests / harness.cpp
blob7ed408bedc2c3c331ba4e6861a2450bc4abcee6b
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 //===----------------------------------------------------------------------===//
9 #include "gwp_asan/tests/harness.h"
11 #include <string>
13 namespace gwp_asan {
14 namespace test {
15 bool OnlyOnce() {
16 static int x = 0;
17 return !x++;
19 } // namespace test
20 } // namespace gwp_asan
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(1));
28 __attribute__((optnone)) void
29 DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
30 GPA.deallocate(Ptr);
32 __attribute__((optnone)) void
33 DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
34 GPA.deallocate(Ptr);
36 __attribute__((optnone)) void TouchMemory(void *Ptr) {
37 *(reinterpret_cast<volatile char *>(Ptr)) = 7;
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;
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