diff --git a/compiler-rt/lib/gwp_asan/tests/backtrace.cpp b/compiler-rt/lib/gwp_asan/tests/backtrace.cpp index 6c9a9309ed8b4d..b3d44270bb2a1e 100644 --- a/compiler-rt/lib/gwp_asan/tests/backtrace.cpp +++ b/compiler-rt/lib/gwp_asan/tests/backtrace.cpp @@ -11,34 +11,52 @@ #include "gwp_asan/crash_handler.h" #include "gwp_asan/tests/harness.h" -TEST_F(BacktraceGuardedPoolAllocator, DoubleFree) { - void *Ptr = GPA.allocate(1); +// Optnone to ensure that the calls to these functions are not optimized away, +// as we're looking for them in the backtraces. +__attribute((optnone)) void * +AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA) { + return GPA.allocate(1); +} +__attribute((optnone)) void +DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) { GPA.deallocate(Ptr); +} +__attribute((optnone)) void +DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) { + GPA.deallocate(Ptr); +} +__attribute__((optnone)) void TouchMemory(void *Ptr) { + *(reinterpret_cast(Ptr)) = 7; +} + +TEST_F(BacktraceGuardedPoolAllocator, DoubleFree) { + void *Ptr = AllocateMemory(GPA); + DeallocateMemory(GPA, Ptr); std::string DeathRegex = "Double Free.*"; - DeathRegex.append("backtrace\\.cpp:26.*"); + DeathRegex.append("DeallocateMemory2.*"); DeathRegex.append("was deallocated.*"); - DeathRegex.append("backtrace\\.cpp:16.*"); + DeathRegex.append("DeallocateMemory.*"); DeathRegex.append("was allocated.*"); - DeathRegex.append("backtrace\\.cpp:15.*"); - ASSERT_DEATH(GPA.deallocate(Ptr), DeathRegex); + DeathRegex.append("AllocateMemory.*"); + ASSERT_DEATH(DeallocateMemory2(GPA, Ptr), DeathRegex); } TEST_F(BacktraceGuardedPoolAllocator, UseAfterFree) { - char *Ptr = static_cast(GPA.allocate(1)); - GPA.deallocate(Ptr); + void *Ptr = AllocateMemory(GPA); + DeallocateMemory(GPA, Ptr); std::string DeathRegex = "Use After Free.*"; - DeathRegex.append("backtrace\\.cpp:41.*"); + DeathRegex.append("TouchMemory.*"); DeathRegex.append("was deallocated.*"); - DeathRegex.append("backtrace\\.cpp:31.*"); + DeathRegex.append("DeallocateMemory.*"); DeathRegex.append("was allocated.*"); - DeathRegex.append("backtrace\\.cpp:30.*"); - ASSERT_DEATH({ *Ptr = 7; }, DeathRegex); + DeathRegex.append("AllocateMemory.*"); + ASSERT_DEATH(TouchMemory(Ptr), DeathRegex); } TEST(Backtrace, Short) {