xref: /openbsd-src/gnu/llvm/compiler-rt/lib/gwp_asan/tests/backtrace.cpp (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 //===-- backtrace.cpp -------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <string>
10 
11 #include "gwp_asan/tests/harness.h"
12 
13 TEST_F(BacktraceGuardedPoolAllocator, DoubleFree) {
14   void *Ptr = GPA.allocate(1);
15   GPA.deallocate(Ptr);
16 
17   std::string DeathRegex = "Double free.*";
18   DeathRegex.append("backtrace\\.cpp:25.*");
19 
20   DeathRegex.append("was deallocated.*");
21   DeathRegex.append("backtrace\\.cpp:15.*");
22 
23   DeathRegex.append("was allocated.*");
24   DeathRegex.append("backtrace\\.cpp:14.*");
25   ASSERT_DEATH(GPA.deallocate(Ptr), DeathRegex);
26 }
27 
28 TEST_F(BacktraceGuardedPoolAllocator, UseAfterFree) {
29   char *Ptr = static_cast<char *>(GPA.allocate(1));
30   GPA.deallocate(Ptr);
31 
32   std::string DeathRegex = "Use after free.*";
33   DeathRegex.append("backtrace\\.cpp:40.*");
34 
35   DeathRegex.append("was deallocated.*");
36   DeathRegex.append("backtrace\\.cpp:30.*");
37 
38   DeathRegex.append("was allocated.*");
39   DeathRegex.append("backtrace\\.cpp:29.*");
40   ASSERT_DEATH({ *Ptr = 7; }, DeathRegex);
41 }
42