1//===-- never_allocated.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 <string>
10
11#include "gwp_asan/common.h"
12#include "gwp_asan/crash_handler.h"
13#include "gwp_asan/tests/harness.h"
14
15TEST_P(BacktraceGuardedPoolAllocatorDeathTest, NeverAllocated) {
16 size_t PageSize = sysconf(_SC_PAGESIZE);
17
18 SCOPED_TRACE("");
19 void *Ptr = GPA.allocate(PageSize);
20 GPA.deallocate(Ptr);
21
22 std::string DeathNeedle =
23 "GWP-ASan cannot provide any more information about this error";
24
25 // Trigger a guard page in a completely different slot that's never allocated.
26 // Previously, there was a bug that this would result in nullptr-dereference
27 // in the posix crash handler.
28 char *volatile NeverAllocatedPtr = static_cast<char *>(Ptr) + 3 * PageSize;
29 if (!Recoverable) {
30 EXPECT_DEATH(*NeverAllocatedPtr = 0, DeathNeedle);
31 return;
32 }
33
34 *NeverAllocatedPtr = 0;
35 CheckOnlyOneGwpAsanCrash(GetOutputBuffer());
36 ASSERT_NE(std::string::npos, GetOutputBuffer().find(DeathNeedle));
37
38 // Check that subsequent invalid touches of the pool don't print a report.
39 GetOutputBuffer().clear();
40 for (size_t i = 0; i < 100; ++i) {
41 *NeverAllocatedPtr = 0;
42 *(NeverAllocatedPtr + 2 * PageSize) = 0;
43 *(NeverAllocatedPtr + 3 * PageSize) = 0;
44 ASSERT_TRUE(GetOutputBuffer().empty());
45 }
46
47 // Check that reports on the other slots still report a double-free, but only
48 // once.
49 GetOutputBuffer().clear();
50 GPA.deallocate(Ptr);
51 ASSERT_NE(std::string::npos, GetOutputBuffer().find("Double Free"));
52 GetOutputBuffer().clear();
53 for (size_t i = 0; i < 100; ++i) {
54 DeallocateMemory(GPA, Ptr);
55 ASSERT_TRUE(GetOutputBuffer().empty());
56 }
57}
58

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