|
11 | 11 | #include "gwp_asan/crash_handler.h"
|
12 | 12 | #include "gwp_asan/tests/harness.h"
|
13 | 13 |
|
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) { |
16 | 22 | 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); |
17 | 35 |
|
18 | 36 | std::string DeathRegex = "Double Free.*";
|
19 |
| - DeathRegex.append("backtrace\\.cpp:26.*"); |
| 37 | + DeathRegex.append("DeallocateMemory2.*"); |
20 | 38 |
|
21 | 39 | DeathRegex.append("was deallocated.*");
|
22 |
| - DeathRegex.append("backtrace\\.cpp:16.*"); |
| 40 | + DeathRegex.append("DeallocateMemory.*"); |
23 | 41 |
|
24 | 42 | 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); |
27 | 45 | }
|
28 | 46 |
|
29 | 47 | 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); |
32 | 50 |
|
33 | 51 | std::string DeathRegex = "Use After Free.*";
|
34 |
| - DeathRegex.append("backtrace\\.cpp:41.*"); |
| 52 | + DeathRegex.append("TouchMemory.*"); |
35 | 53 |
|
36 | 54 | DeathRegex.append("was deallocated.*");
|
37 |
| - DeathRegex.append("backtrace\\.cpp:31.*"); |
| 55 | + DeathRegex.append("DeallocateMemory.*"); |
38 | 56 |
|
39 | 57 | 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); |
42 | 60 | }
|
43 | 61 |
|
44 | 62 | TEST(Backtrace, Short) {
|
|
0 commit comments