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