1 //===-- never_allocated.cpp -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "gwp_asan/common.h"
12 #include "gwp_asan/crash_handler.h"
13 #include "gwp_asan/tests/harness.h"
15 TEST_P(BacktraceGuardedPoolAllocatorDeathTest
, NeverAllocated
) {
17 void *Ptr
= GPA
.allocate(0x1000);
20 std::string DeathNeedle
=
21 "GWP-ASan cannot provide any more information about this error";
23 // Trigger a guard page in a completely different slot that's never allocated.
24 // Previously, there was a bug that this would result in nullptr-dereference
25 // in the posix crash handler.
26 char *volatile NeverAllocatedPtr
= static_cast<char *>(Ptr
) + 0x3000;
28 EXPECT_DEATH(*NeverAllocatedPtr
= 0, DeathNeedle
);
32 *NeverAllocatedPtr
= 0;
33 CheckOnlyOneGwpAsanCrash(GetOutputBuffer());
34 ASSERT_NE(std::string::npos
, GetOutputBuffer().find(DeathNeedle
));
36 // Check that subsequent invalid touches of the pool don't print a report.
37 GetOutputBuffer().clear();
38 for (size_t i
= 0; i
< 100; ++i
) {
39 *NeverAllocatedPtr
= 0;
40 *(NeverAllocatedPtr
+ 0x2000) = 0;
41 *(NeverAllocatedPtr
+ 0x3000) = 0;
42 ASSERT_TRUE(GetOutputBuffer().empty());
45 // Check that reports on the other slots still report a double-free, but only
47 GetOutputBuffer().clear();
49 ASSERT_NE(std::string::npos
, GetOutputBuffer().find("Double Free"));
50 GetOutputBuffer().clear();
51 for (size_t i
= 0; i
< 100; ++i
) {
52 DeallocateMemory(GPA
, Ptr
);
53 ASSERT_TRUE(GetOutputBuffer().empty());