From 1216f4c0ea0c232d545ce3ab8f858c696e085d47 Mon Sep 17 00:00:00 2001 From: Mitch Phillips <31459023+hctim@users.noreply.github.com> Date: Thu, 26 Mar 2020 10:09:57 -0700 Subject: [PATCH] [GWP-ASan] Use functions in backtrace test, not line numbers. Summary: There's no unwinding functionality on Android that allows for line numbers to be retrieved in-process. As a result, we can't have this backtrace test run on Android. Cleanup the test to use optnone functions instead, which is more stable than line numbers anyway. Reviewers: eugenis Reviewed By: eugenis Subscribers: #sanitizers, morehouse, cferris Tags: #sanitizers Differential Revision: https://reviews.llvm.org/D76807 --- compiler-rt/lib/gwp_asan/tests/backtrace.cpp | 42 ++++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/compiler-rt/lib/gwp_asan/tests/backtrace.cpp b/compiler-rt/lib/gwp_asan/tests/backtrace.cpp index 6c9a9309ed8b..b3d44270bb2a 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) {