| 1 |
#include <utility> |
| 2 |
#include <vector> |
| 3 |
|
| 4 |
#include <gtest/gtest.h> |
| 5 |
#include <gtest/gtest-matchers.h> |
| 6 |
#include <gmock/gmock.h> |
| 7 |
#include <gmock/gmock-matchers.h> |
| 8 |
|
| 9 |
#include "common.hpp" |
| 10 |
|
| 11 |
#ifdef TEST_MODULE |
| 12 |
import cpptrace; |
| 13 |
#else |
| 14 |
#include <cpptrace/cpptrace.hpp> |
| 15 |
#endif |
| 16 |
|
| 17 |
|
| 18 |
// Raw trace tests |
| 19 |
|
| 20 |
// This is fickle, however, it's the only way to do it really. I've gotten it reasonably reliable test in practice. |
| 21 |
// Sanitizers do interfere. |
| 22 |
|
| 23 |
#ifndef CPPTRACE_SANITIZER_BUILD |
| 24 |
|
| 25 |
// NOTE: MSVC likes creating trampoline-like entries for non-static functions |
| 26 |
CPPTRACE_FORCE_NO_INLINE static void raw_trace_basic() { |
| 27 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 28 |
auto raw_trace = cpptrace::generate_raw_trace(); |
| 29 |
// look for within 90 bytes of the start of the function |
| 30 |
ASSERT_GE(raw_trace.frames.size(), 1); |
| 31 |
EXPECT_GE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(raw_trace_basic)); |
| 32 |
EXPECT_LE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(raw_trace_basic) + 90); |
| 33 |
} |
| 34 |
|
| 35 |
#ifndef _MSC_VER |
| 36 |
CPPTRACE_FORCE_NO_INLINE void raw_trace_basic_precise() { |
| 37 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 38 |
a: |
| 39 |
auto raw_trace = cpptrace::generate_raw_trace(); |
| 40 |
b: |
| 41 |
// This is stupid, but without it gcc was optimizing both &&a and &&b to point to the start of the function's body |
| 42 |
volatile auto x = 0; |
| 43 |
if(x) { |
| 44 |
goto* &&a; |
| 45 |
} |
| 46 |
if(x) { |
| 47 |
goto* &&b; |
| 48 |
} |
| 49 |
ASSERT_GE(raw_trace.frames.size(), 1); |
| 50 |
EXPECT_GE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&a)); |
| 51 |
EXPECT_LE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&b)); |
| 52 |
} |
| 53 |
#endif |
| 54 |
|
| 55 |
TEST(RawTrace, Basic) { |
| 56 |
raw_trace_basic(); |
| 57 |
#ifndef _MSC_VER |
| 58 |
raw_trace_basic_precise(); |
| 59 |
#endif |
| 60 |
volatile int x = 0; // prevent raw_trace_basic_precise() above being a jmp |
| 61 |
(void)x; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
CPPTRACE_FORCE_NO_INLINE static void raw_trace_multi_2( |
| 67 |
cpptrace::frame_ptr parent_low_bound, |
| 68 |
cpptrace::frame_ptr parent_high_bound |
| 69 |
) { |
| 70 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 71 |
auto raw_trace = cpptrace::generate_raw_trace(); |
| 72 |
ASSERT_GE(raw_trace.frames.size(), 2); |
| 73 |
EXPECT_GE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(raw_trace_multi_2)); |
| 74 |
EXPECT_LE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(raw_trace_multi_2) + 100); |
| 75 |
EXPECT_GE(raw_trace.frames[1], parent_low_bound); |
| 76 |
EXPECT_LE(raw_trace.frames[1], parent_high_bound); |
| 77 |
} |
| 78 |
|
| 79 |
CPPTRACE_FORCE_NO_INLINE static void raw_trace_multi_1() { |
| 80 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 81 |
auto raw_trace = cpptrace::generate_raw_trace(); |
| 82 |
raw_trace_multi_2(reinterpret_cast<uintptr_t>(raw_trace_multi_1), reinterpret_cast<uintptr_t>(raw_trace_multi_1) + 300); |
| 83 |
ASSERT_GE(raw_trace.frames.size(), 1); |
| 84 |
EXPECT_GE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(raw_trace_multi_1)); |
| 85 |
EXPECT_LE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(raw_trace_multi_1) + 90); |
| 86 |
} |
| 87 |
|
| 88 |
std::vector<std::pair<cpptrace::frame_ptr, cpptrace::frame_ptr>> parents; |
| 89 |
|
| 90 |
CPPTRACE_FORCE_NO_INLINE void record_parent(uintptr_t low_bound, uintptr_t high_bound) { |
| 91 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 92 |
parents.insert(parents.begin(), {low_bound, high_bound}); |
| 93 |
} |
| 94 |
|
| 95 |
#ifndef _MSC_VER |
| 96 |
CPPTRACE_FORCE_NO_INLINE void raw_trace_multi_precise_3() { |
| 97 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 98 |
a: |
| 99 |
auto raw_trace = cpptrace::generate_raw_trace(); |
| 100 |
b: |
| 101 |
volatile auto x = 0; |
| 102 |
if(x) { |
| 103 |
goto* &&a; |
| 104 |
} |
| 105 |
if(x) { |
| 106 |
goto* &&b; |
| 107 |
} |
| 108 |
ASSERT_GE(raw_trace.frames.size(), parents.size() + 1); |
| 109 |
EXPECT_GE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&a)); // this frame |
| 110 |
EXPECT_LE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&b)); |
| 111 |
for(size_t i = 0; i < parents.size(); i++) { // parent frames |
| 112 |
EXPECT_GE(raw_trace.frames[i + 1], parents[i].first); |
| 113 |
EXPECT_LE(raw_trace.frames[i + 1], parents[i].second); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
CPPTRACE_FORCE_NO_INLINE void raw_trace_multi_precise_2() { |
| 118 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 119 |
a: |
| 120 |
auto raw_trace = cpptrace::generate_raw_trace(); |
| 121 |
b: |
| 122 |
volatile auto x = 0; |
| 123 |
if(x) { |
| 124 |
goto* &&a; |
| 125 |
} |
| 126 |
if(x) { |
| 127 |
goto* &&b; |
| 128 |
} |
| 129 |
ASSERT_GE(raw_trace.frames.size(), parents.size() + 1); |
| 130 |
EXPECT_GE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&a)); // this frame |
| 131 |
EXPECT_LE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&b)); |
| 132 |
for(size_t i = 0; i < parents.size(); i++) { // parent frames |
| 133 |
EXPECT_GE(raw_trace.frames[i + 1], parents[i].first); |
| 134 |
EXPECT_LE(raw_trace.frames[i + 1], parents[i].second); |
| 135 |
} |
| 136 |
record_parent(reinterpret_cast<uintptr_t>(&&c), reinterpret_cast<uintptr_t>(&&d)); |
| 137 |
c: |
| 138 |
raw_trace_multi_precise_3(); |
| 139 |
d: |
| 140 |
if(x) { |
| 141 |
goto* &&c; |
| 142 |
} |
| 143 |
if(x) { |
| 144 |
goto* &&d; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
CPPTRACE_FORCE_NO_INLINE void raw_trace_multi_precise_1() { |
| 149 |
static volatile int lto_guard; lto_guard = lto_guard + 1; |
| 150 |
a: |
| 151 |
auto raw_trace = cpptrace::generate_raw_trace(); |
| 152 |
b: |
| 153 |
volatile auto x = 0; |
| 154 |
if(x) { |
| 155 |
goto* &&a; |
| 156 |
} |
| 157 |
if(x) { |
| 158 |
goto* &&b; |
| 159 |
} |
| 160 |
ASSERT_GE(raw_trace.frames.size(), 1); |
| 161 |
EXPECT_GE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&a)); |
| 162 |
EXPECT_LE(raw_trace.frames[0], reinterpret_cast<uintptr_t>(&&b)); |
| 163 |
record_parent(reinterpret_cast<uintptr_t>(&&c), reinterpret_cast<uintptr_t>(&&d)); |
| 164 |
c: |
| 165 |
raw_trace_multi_precise_2(); |
| 166 |
d: |
| 167 |
if(x) { |
| 168 |
goto* &&c; |
| 169 |
} |
| 170 |
if(x) { |
| 171 |
goto* &&d; |
| 172 |
} |
| 173 |
} |
| 174 |
#endif |
| 175 |
|
| 176 |
TEST(RawTrace, MultipleCalls) { |
| 177 |
parents.clear(); |
| 178 |
raw_trace_multi_1(); |
| 179 |
#ifndef _MSC_VER |
| 180 |
raw_trace_multi_precise_1(); |
| 181 |
#endif |
| 182 |
} |
| 183 |
|
| 184 |
#endif |