#include <regex>
#include <string>
#include "gwp_asan/common.h"
#include "gwp_asan/crash_handler.h"
#include "gwp_asan/tests/harness.h"
TEST_P(BacktraceGuardedPoolAllocatorDeathTest, DoubleFree) {
void *Ptr = AllocateMemory(GPA);
DeallocateMemory(GPA, Ptr);
std::string DeathRegex = "Double Free.*DeallocateMemory2.*";
DeathRegex.append("was deallocated.*DeallocateMemory[^2].*");
DeathRegex.append("was allocated.*AllocateMemory");
if (!Recoverable) {
EXPECT_DEATH(DeallocateMemory2(GPA, Ptr), DeathRegex);
return;
}
DeallocateMemory2(GPA, Ptr);
EXPECT_TRUE(std::regex_search(
GetOutputBuffer(),
std::basic_regex(DeathRegex, std::regex_constants::extended)))
<< "Regex \"" << DeathRegex
<< "\" was not found in input:\n============\n"
<< GetOutputBuffer() << "\n============";
}
TEST_P(BacktraceGuardedPoolAllocatorDeathTest, UseAfterFree) {
#if defined(__linux__) && __ARM_ARCH == 7
GTEST_SKIP();
#endif
void *Ptr = AllocateMemory(GPA);
DeallocateMemory(GPA, Ptr);
std::string DeathRegex = "Use After Free.*TouchMemory.*";
DeathRegex.append("was deallocated.*DeallocateMemory[^2].*");
DeathRegex.append("was allocated.*AllocateMemory");
if (!Recoverable) {
EXPECT_DEATH(TouchMemory(Ptr), DeathRegex);
return;
}
TouchMemory(Ptr);
EXPECT_TRUE(std::regex_search(
GetOutputBuffer(),
std::basic_regex(DeathRegex, std::regex_constants::extended)))
<< "Regex \"" << DeathRegex
<< "\" was not found in input:\n============\n"
<< GetOutputBuffer() << "\n============";
;
}
TEST(Backtrace, Short) {
gwp_asan::AllocationMetadata Meta;
Meta.AllocationTrace.RecordBacktrace(
[](uintptr_t *TraceBuffer, size_t ) -> size_t {
TraceBuffer[0] = 123u;
TraceBuffer[1] = 321u;
return 2u;
});
uintptr_t TraceOutput[2] = {};
EXPECT_EQ(2u, __gwp_asan_get_allocation_trace(&Meta, TraceOutput, 2));
EXPECT_EQ(TraceOutput[0], 123u);
EXPECT_EQ(TraceOutput[1], 321u);
}
TEST(Backtrace, ExceedsStorableLength) {
gwp_asan::AllocationMetadata Meta;
Meta.AllocationTrace.RecordBacktrace(
[](uintptr_t *TraceBuffer, size_t Size) -> size_t {
memset(TraceBuffer, 0u, Size * sizeof(*TraceBuffer));
return Size * 2;
});
uintptr_t TraceOutput;
EXPECT_EQ(gwp_asan::AllocationMetadata::kMaxTraceLengthToCollect,
__gwp_asan_get_allocation_trace(&Meta, &TraceOutput, 1));
}
TEST(Backtrace, ExceedsRetrievableAllocLength) {
gwp_asan::AllocationMetadata Meta;
constexpr size_t kNumFramesToStore = 3u;
Meta.AllocationTrace.RecordBacktrace(
[](uintptr_t *TraceBuffer, size_t ) -> size_t {
memset(TraceBuffer, kNumFramesToStore,
kNumFramesToStore * sizeof(*TraceBuffer));
return kNumFramesToStore;
});
uintptr_t TraceOutput;
EXPECT_EQ(kNumFramesToStore,
__gwp_asan_get_allocation_trace(&Meta, &TraceOutput, 1));
}
TEST(Backtrace, ExceedsRetrievableDeallocLength) {
gwp_asan::AllocationMetadata Meta;
constexpr size_t kNumFramesToStore = 3u;
Meta.DeallocationTrace.RecordBacktrace(
[](uintptr_t *TraceBuffer, size_t ) -> size_t {
memset(TraceBuffer, kNumFramesToStore,
kNumFramesToStore * sizeof(*TraceBuffer));
return kNumFramesToStore;
});
uintptr_t TraceOutput;
EXPECT_EQ(kNumFramesToStore,
__gwp_asan_get_deallocation_trace(&Meta, &TraceOutput, 1));
}