Skip to content

Commit 1216f4c

Browse files
committed
[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
1 parent 17e4c38 commit 1216f4c

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

compiler-rt/lib/gwp_asan/tests/backtrace.cpp

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,52 @@
1111
#include "gwp_asan/crash_handler.h"
1212
#include "gwp_asan/tests/harness.h"
1313

14-
TEST_F(BacktraceGuardedPoolAllocator, DoubleFree) {
15-
void *Ptr = GPA.allocate(1);
14+
// Optnone to ensure that the calls to these functions are not optimized away,
15+
// as we're looking for them in the backtraces.
16+
__attribute((optnone)) void *
17+
AllocateMemory(gwp_asan::GuardedPoolAllocator &GPA) {
18+
return GPA.allocate(1);
19+
}
20+
__attribute((optnone)) void
21+
DeallocateMemory(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
1622
GPA.deallocate(Ptr);
23+
}
24+
__attribute((optnone)) void
25+
DeallocateMemory2(gwp_asan::GuardedPoolAllocator &GPA, void *Ptr) {
26+
GPA.deallocate(Ptr);
27+
}
28+
__attribute__((optnone)) void TouchMemory(void *Ptr) {
29+
*(reinterpret_cast<volatile char *>(Ptr)) = 7;
30+
}
31+
32+
TEST_F(BacktraceGuardedPoolAllocator, DoubleFree) {
33+
void *Ptr = AllocateMemory(GPA);
34+
DeallocateMemory(GPA, Ptr);
1735

1836
std::string DeathRegex = "Double Free.*";
19-
DeathRegex.append("backtrace\\.cpp:26.*");
37+
DeathRegex.append("DeallocateMemory2.*");
2038

2139
DeathRegex.append("was deallocated.*");
22-
DeathRegex.append("backtrace\\.cpp:16.*");
40+
DeathRegex.append("DeallocateMemory.*");
2341

2442
DeathRegex.append("was allocated.*");
25-
DeathRegex.append("backtrace\\.cpp:15.*");
26-
ASSERT_DEATH(GPA.deallocate(Ptr), DeathRegex);
43+
DeathRegex.append("AllocateMemory.*");
44+
ASSERT_DEATH(DeallocateMemory2(GPA, Ptr), DeathRegex);
2745
}
2846

2947
TEST_F(BacktraceGuardedPoolAllocator, UseAfterFree) {
30-
char *Ptr = static_cast<char *>(GPA.allocate(1));
31-
GPA.deallocate(Ptr);
48+
void *Ptr = AllocateMemory(GPA);
49+
DeallocateMemory(GPA, Ptr);
3250

3351
std::string DeathRegex = "Use After Free.*";
34-
DeathRegex.append("backtrace\\.cpp:41.*");
52+
DeathRegex.append("TouchMemory.*");
3553

3654
DeathRegex.append("was deallocated.*");
37-
DeathRegex.append("backtrace\\.cpp:31.*");
55+
DeathRegex.append("DeallocateMemory.*");
3856

3957
DeathRegex.append("was allocated.*");
40-
DeathRegex.append("backtrace\\.cpp:30.*");
41-
ASSERT_DEATH({ *Ptr = 7; }, DeathRegex);
58+
DeathRegex.append("AllocateMemory.*");
59+
ASSERT_DEATH(TouchMemory(Ptr), DeathRegex);
4260
}
4361

4462
TEST(Backtrace, Short) {

0 commit comments

Comments
 (0)