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