forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
win32_stack_frame_unwinder_unittest.cc
231 lines (192 loc) · 8.14 KB
/
win32_stack_frame_unwinder_unittest.cc
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/profiler/win32_stack_frame_unwinder.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class TestUnwindFunctions : public Win32StackFrameUnwinder::UnwindFunctions {
public:
TestUnwindFunctions();
PRUNTIME_FUNCTION LookupFunctionEntry(DWORD64 program_counter,
PDWORD64 image_base) override;
void VirtualUnwind(DWORD64 image_base,
DWORD64 program_counter,
PRUNTIME_FUNCTION runtime_function,
CONTEXT* context) override;
void SetNoUnwindInfoForNextFrame();
void SetImageBaseForNextFrame(DWORD64 image_base);
private:
enum { kRuntimeFunctionPointerIncrement = 1, kImageBaseIncrement = 1 << 20 };
static const PRUNTIME_FUNCTION kNonNullRuntimeFunctionPointer;
DWORD64 supplied_program_counter_;
DWORD64 custom_image_base_;
DWORD64 next_image_base_;
bool next_lookup_returns_null_;
DISALLOW_COPY_AND_ASSIGN(TestUnwindFunctions);
};
// This value is opaque to Win32StackFrameUnwinder.
const PRUNTIME_FUNCTION TestUnwindFunctions::kNonNullRuntimeFunctionPointer =
reinterpret_cast<PRUNTIME_FUNCTION>(8);
TestUnwindFunctions::TestUnwindFunctions()
: supplied_program_counter_(0),
custom_image_base_(0),
next_image_base_(kImageBaseIncrement),
next_lookup_returns_null_(false) {
}
PRUNTIME_FUNCTION TestUnwindFunctions::LookupFunctionEntry(
DWORD64 program_counter,
PDWORD64 image_base) {
supplied_program_counter_ = program_counter;
if (custom_image_base_) {
*image_base = custom_image_base_;
custom_image_base_ = 0;
} else {
*image_base = next_image_base_;
next_image_base_ += kImageBaseIncrement;
}
if (next_lookup_returns_null_) {
next_lookup_returns_null_ = false;
return nullptr;
}
return kNonNullRuntimeFunctionPointer;
}
void TestUnwindFunctions::VirtualUnwind(DWORD64 image_base,
DWORD64 program_counter,
PRUNTIME_FUNCTION runtime_function,
CONTEXT* context) {
EXPECT_EQ(next_image_base_ - kImageBaseIncrement, image_base);
EXPECT_EQ(supplied_program_counter_, program_counter);
// This function should only be called when LookupFunctionEntry returns a
// non-null value.
EXPECT_EQ(kNonNullRuntimeFunctionPointer, runtime_function);
}
void TestUnwindFunctions::SetNoUnwindInfoForNextFrame() {
next_lookup_returns_null_ = true;
}
void TestUnwindFunctions::SetImageBaseForNextFrame(DWORD64 image_base) {
next_image_base_ = image_base;
}
} // namespace
class Win32StackFrameUnwinderTest : public testing::Test {
protected:
Win32StackFrameUnwinderTest() {}
// This exists so that Win32StackFrameUnwinder's constructor can be private
// with a single friend declaration of this test fixture.
scoped_ptr<Win32StackFrameUnwinder> CreateUnwinder();
TestUnwindFunctions unwind_functions_;
private:
DISALLOW_COPY_AND_ASSIGN(Win32StackFrameUnwinderTest);
};
scoped_ptr<Win32StackFrameUnwinder>
Win32StackFrameUnwinderTest::CreateUnwinder() {
return make_scoped_ptr(new Win32StackFrameUnwinder(&unwind_functions_));
}
// Checks the case where all frames have unwind information.
TEST_F(Win32StackFrameUnwinderTest, FramesWithUnwindInfo) {
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
EXPECT_TRUE(unwinder->TryUnwind(&context));
EXPECT_TRUE(unwinder->TryUnwind(&context));
EXPECT_TRUE(unwinder->TryUnwind(&context));
}
// Checks that the CONTEXT's stack pointer gets popped when the top frame has no
// unwind information.
TEST_F(Win32StackFrameUnwinderTest, FrameAtTopWithoutUnwindInfo) {
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
const DWORD64 original_rsp = 128;
context.Rsp = original_rsp;
unwind_functions_.SetNoUnwindInfoForNextFrame();
EXPECT_TRUE(unwinder->TryUnwind(&context));
EXPECT_EQ(original_rsp, context.Rip);
EXPECT_EQ(original_rsp + 8, context.Rsp);
EXPECT_TRUE(unwinder->TryUnwind(&context));
EXPECT_TRUE(unwinder->TryUnwind(&context));
}
// Checks that a frame below the top of the stack with missing unwind info
// results in blacklisting the module.
TEST_F(Win32StackFrameUnwinderTest, BlacklistedModule) {
const DWORD64 image_base_for_module_with_bad_function = 1024;
{
// First stack, with a bad function below the top of the stack.
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
EXPECT_TRUE(unwinder->TryUnwind(&context));
unwind_functions_.SetNoUnwindInfoForNextFrame();
unwind_functions_.SetImageBaseForNextFrame(
image_base_for_module_with_bad_function);
EXPECT_FALSE(unwinder->TryUnwind(&context));
}
{
// Second stack; check that a function at the top of the stack without
// unwind info from the previously-seen module is blacklisted.
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
unwind_functions_.SetNoUnwindInfoForNextFrame();
unwind_functions_.SetImageBaseForNextFrame(
image_base_for_module_with_bad_function);
EXPECT_FALSE(unwinder->TryUnwind(&context));
}
{
// Third stack; check that a function at the top of the stack *with* unwind
// info from the previously-seen module is not blacklisted. Then check that
// functions below the top of the stack with unwind info are not
// blacklisted, regardless of whether they are in the previously-seen
// module.
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
unwind_functions_.SetImageBaseForNextFrame(
image_base_for_module_with_bad_function);
EXPECT_TRUE(unwinder->TryUnwind(&context));
EXPECT_TRUE(unwinder->TryUnwind(&context));
unwind_functions_.SetImageBaseForNextFrame(
image_base_for_module_with_bad_function);
EXPECT_TRUE(unwinder->TryUnwind(&context));
}
{
// Fourth stack; check that a function at the top of the stack without
// unwind info and not from the previously-seen module is not
// blacklisted. Then check that functions below the top of the stack with
// unwind info are not blacklisted, regardless of whether they are in the
// previously-seen module.
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
unwind_functions_.SetNoUnwindInfoForNextFrame();
EXPECT_TRUE(unwinder->TryUnwind(&context));
EXPECT_TRUE(unwinder->TryUnwind(&context));
unwind_functions_.SetImageBaseForNextFrame(
image_base_for_module_with_bad_function);
EXPECT_TRUE(unwinder->TryUnwind(&context));
}
}
// Checks that a frame below the top of the stack with missing unwind info does
// not result in blacklisting the module if the first frame also was missing
// unwind info. This ensures we don't blacklist an innocent module because the
// first frame was bad but we didn't know it at the time.
TEST_F(Win32StackFrameUnwinderTest, ModuleFromQuestionableFrameNotBlacklisted) {
const DWORD64 image_base_for_questionable_module = 2048;
{
// First stack, with both the first and second frames missing unwind info.
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
unwind_functions_.SetNoUnwindInfoForNextFrame();
EXPECT_TRUE(unwinder->TryUnwind(&context));
unwind_functions_.SetNoUnwindInfoForNextFrame();
unwind_functions_.SetImageBaseForNextFrame(
image_base_for_questionable_module);
EXPECT_FALSE(unwinder->TryUnwind(&context));
}
{
// Second stack; check that the questionable module was not blacklisted.
scoped_ptr<Win32StackFrameUnwinder> unwinder = CreateUnwinder();
CONTEXT context = {0};
unwind_functions_.SetNoUnwindInfoForNextFrame();
unwind_functions_.SetImageBaseForNextFrame(
image_base_for_questionable_module);
EXPECT_TRUE(unwinder->TryUnwind(&context));
}
}
} // namespace base