OLD | NEW |
1 // Copyright 2015 Google Inc. All Rights Reserved. | 1 // Copyright 2015 Google Inc. All Rights Reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "syzygy/agent/common/stack_walker_x86.h" | 15 #include "syzygy/agent/common/stack_walker.h" |
16 | 16 |
17 #include <windows.h> | 17 #include <windows.h> |
18 | 18 |
19 #include "gtest/gtest.h" | 19 #include "gtest/gtest.h" |
20 #include "syzygy/testing/metrics.h" | 20 #include "syzygy/testing/metrics.h" |
21 | 21 |
22 namespace agent { | 22 namespace agent { |
23 namespace common { | 23 namespace common { |
24 | 24 |
| 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 class StackWalkerX86Test : public testing::Test { | 28 class StackWalkerTest : public testing::Test { |
28 public: | 29 public: |
29 StackWalkerX86Test() | 30 StackWalkerTest() |
30 : dummy_ebp_(nullptr), dummy_esp_(nullptr), dummy_ret_(0u) { | 31 : dummy_ebp_(nullptr), dummy_esp_(nullptr), dummy_ret_(0u) { |
31 ::memset(frames_, 0, sizeof(frames_)); | 32 ::memset(frames_, 0, sizeof(frames_)); |
32 ::memset(frames2_, 0, sizeof(frames2_)); | 33 ::memset(frames2_, 0, sizeof(frames2_)); |
33 ::memset(dummy_stack_, 0, sizeof(dummy_stack_)); | 34 ::memset(dummy_stack_, 0, sizeof(dummy_stack_)); |
34 } | 35 } |
35 | 36 #ifndef _WIN64 |
36 static const uintptr_t kBaseRet = 0x1000000u; | 37 static const uintptr_t kBaseRet = 0x1000000u; |
37 | 38 |
38 void Push(uintptr_t value) { | 39 void Push(uintptr_t value) { |
39 --dummy_esp_; | 40 --dummy_esp_; |
40 ASSERT_LE(dummy_stack_, dummy_esp_); | 41 ASSERT_LE(dummy_stack_, dummy_esp_); |
41 *dummy_esp_ = value; | 42 *dummy_esp_ = value; |
42 } | 43 } |
43 | 44 |
44 void PushEbp() { | 45 void PushEbp() { |
45 Push(reinterpret_cast<uintptr_t>(dummy_ebp_)); | 46 Push(reinterpret_cast<uintptr_t>(dummy_ebp_)); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 dummy_stack_ + arraysize(dummy_stack_), | 128 dummy_stack_ + arraysize(dummy_stack_), |
128 frames_to_skip, kMaxFrames, frames_, &stack_id)); | 129 frames_to_skip, kMaxFrames, frames_, &stack_id)); |
129 for (size_t i = 0; i < num_frames; ++i) { | 130 for (size_t i = 0; i < num_frames; ++i) { |
130 EXPECT_EQ(reinterpret_cast<void*>(dummy_ret_ - i - 1 - frames_to_skip), | 131 EXPECT_EQ(reinterpret_cast<void*>(dummy_ret_ - i - 1 - frames_to_skip), |
131 frames_[i]); | 132 frames_[i]); |
132 } | 133 } |
133 | 134 |
134 PopEbp(); | 135 PopEbp(); |
135 } | 136 } |
136 | 137 |
| 138 #endif // !defined _WIN64 |
| 139 |
137 static const size_t kMaxFrames = 100; | 140 static const size_t kMaxFrames = 100; |
138 void* frames_[kMaxFrames]; | 141 void* frames_[kMaxFrames]; |
139 void* frames2_[kMaxFrames]; | 142 void* frames2_[kMaxFrames]; |
140 | 143 |
141 uintptr_t dummy_stack_[1024]; | 144 uintptr_t dummy_stack_[1024]; |
142 uintptr_t* dummy_ebp_; | 145 uintptr_t* dummy_ebp_; |
143 uintptr_t* dummy_esp_; | 146 uintptr_t* dummy_esp_; |
144 uintptr_t dummy_ret_; | 147 uintptr_t dummy_ret_; |
145 }; | 148 }; |
146 | 149 |
147 } // namespace | 150 } // namespace |
148 | 151 |
149 TEST_F(StackWalkerX86Test, ValidWalk) { | 152 #ifndef _WIN64 |
| 153 |
| 154 TEST_F(StackWalkerTest, ValidWalk) { |
150 BuildValidFrame(0); | 155 BuildValidFrame(0); |
151 ExpectSuccessfulWalk(2, 0); | 156 ExpectSuccessfulWalk(2, 0); |
152 BuildValidFrame(2); | 157 BuildValidFrame(2); |
153 ExpectSuccessfulWalk(3, 0); | 158 ExpectSuccessfulWalk(3, 0); |
154 BuildValidFrame(1); | 159 BuildValidFrame(1); |
155 ExpectSuccessfulWalk(4, 0); | 160 ExpectSuccessfulWalk(4, 0); |
156 ExpectSuccessfulWalk(3, 1); | 161 ExpectSuccessfulWalk(3, 1); |
157 ExpectSuccessfulWalk(2, 2); | 162 ExpectSuccessfulWalk(2, 2); |
158 } | 163 } |
159 | 164 |
160 TEST_F(StackWalkerX86Test, WalkStopsWhenFrameTooSmall) { | 165 TEST_F(StackWalkerTest, WalkStopsWhenFrameTooSmall) { |
161 BuildValidFrame(0); | 166 BuildValidFrame(0); |
162 ExpectSuccessfulWalk(2, 0); | 167 ExpectSuccessfulWalk(2, 0); |
163 | 168 |
164 BuildInvalidFrameTooSmall(); | 169 BuildInvalidFrameTooSmall(); |
165 BuildValidFrame(1); | 170 BuildValidFrame(1); |
166 ExpectSuccessfulWalk(1, 0); | 171 ExpectSuccessfulWalk(1, 0); |
167 | 172 |
168 BuildValidFrame(2); | 173 BuildValidFrame(2); |
169 ExpectSuccessfulWalk(2, 0); | 174 ExpectSuccessfulWalk(2, 0); |
170 | 175 |
171 BuildValidFrame(1); | 176 BuildValidFrame(1); |
172 ExpectSuccessfulWalk(3, 0); | 177 ExpectSuccessfulWalk(3, 0); |
173 ExpectSuccessfulWalk(2, 1); | 178 ExpectSuccessfulWalk(2, 1); |
174 } | 179 } |
175 | 180 |
176 TEST_F(StackWalkerX86Test, WalkStopsAtNonIncreasingBasePointer) { | 181 TEST_F(StackWalkerTest, WalkStopsAtNonIncreasingBasePointer) { |
177 BuildValidFrame(0); | 182 BuildValidFrame(0); |
178 ExpectSuccessfulWalk(2, 0); | 183 ExpectSuccessfulWalk(2, 0); |
179 | 184 |
180 BuildInvalidFrameNonIncreasingBasePointer(); | 185 BuildInvalidFrameNonIncreasingBasePointer(); |
181 ExpectSuccessfulWalk(2, 0); | 186 ExpectSuccessfulWalk(2, 0); |
182 | 187 |
183 BuildValidFrame(2); | 188 BuildValidFrame(2); |
184 ExpectSuccessfulWalk(3, 0); | 189 ExpectSuccessfulWalk(3, 0); |
185 | 190 |
186 BuildValidFrame(1); | 191 BuildValidFrame(1); |
187 ExpectSuccessfulWalk(4, 0); | 192 ExpectSuccessfulWalk(4, 0); |
188 ExpectSuccessfulWalk(3, 1); | 193 ExpectSuccessfulWalk(3, 1); |
189 } | 194 } |
190 | 195 |
191 TEST_F(StackWalkerX86Test, WalkStopsAtUnalignedBasePointer) { | 196 TEST_F(StackWalkerTest, WalkStopsAtUnalignedBasePointer) { |
192 BuildValidFrame(0); | 197 BuildValidFrame(0); |
193 ExpectSuccessfulWalk(2, 0); | 198 ExpectSuccessfulWalk(2, 0); |
194 | 199 |
195 BuildInvalidFrameUnalignedBasePointer(); | 200 BuildInvalidFrameUnalignedBasePointer(); |
196 ExpectSuccessfulWalk(2, 0); | 201 ExpectSuccessfulWalk(2, 0); |
197 | 202 |
198 BuildValidFrame(2); | 203 BuildValidFrame(2); |
199 ExpectSuccessfulWalk(3, 0); | 204 ExpectSuccessfulWalk(3, 0); |
200 | 205 |
201 BuildValidFrame(1); | 206 BuildValidFrame(1); |
202 ExpectSuccessfulWalk(4, 0); | 207 ExpectSuccessfulWalk(4, 0); |
203 ExpectSuccessfulWalk(3, 1); | 208 ExpectSuccessfulWalk(3, 1); |
204 } | 209 } |
205 | 210 |
206 TEST_F(StackWalkerX86Test, WalkStopsAtInvalidReturnAddress) { | 211 TEST_F(StackWalkerTest, WalkStopsAtInvalidReturnAddress) { |
207 BuildValidFrame(0); | 212 BuildValidFrame(0); |
208 ExpectSuccessfulWalk(2, 0); | 213 ExpectSuccessfulWalk(2, 0); |
209 | 214 |
210 BuildInvalidFrameInvalidReturnAddress(); | 215 BuildInvalidFrameInvalidReturnAddress(); |
211 ExpectSuccessfulWalk(0, 0); | 216 ExpectSuccessfulWalk(0, 0); |
212 | 217 |
213 BuildValidFrame(2); | 218 BuildValidFrame(2); |
214 ExpectSuccessfulWalk(1, 0); | 219 ExpectSuccessfulWalk(1, 0); |
215 | 220 |
216 BuildValidFrame(1); | 221 BuildValidFrame(1); |
217 ExpectSuccessfulWalk(2, 0); | 222 ExpectSuccessfulWalk(2, 0); |
218 } | 223 } |
219 | 224 |
220 TEST_F(StackWalkerX86Test, WalkStopsAtInvalidBasePointer) { | 225 TEST_F(StackWalkerTest, WalkStopsAtInvalidBasePointer) { |
221 BuildValidFrame(0); | 226 BuildValidFrame(0); |
222 ExpectSuccessfulWalk(2, 0); | 227 ExpectSuccessfulWalk(2, 0); |
223 | 228 |
224 BuildInvalidFrameInvalidBasePointer(); | 229 BuildInvalidFrameInvalidBasePointer(); |
225 ExpectSuccessfulWalk(2, 0); | 230 ExpectSuccessfulWalk(2, 0); |
226 | 231 |
227 BuildValidFrame(2); | 232 BuildValidFrame(2); |
228 ExpectSuccessfulWalk(3, 0); | 233 ExpectSuccessfulWalk(3, 0); |
229 | 234 |
230 BuildValidFrame(1); | 235 BuildValidFrame(1); |
231 ExpectSuccessfulWalk(4, 0); | 236 ExpectSuccessfulWalk(4, 0); |
232 ExpectSuccessfulWalk(3, 1); | 237 ExpectSuccessfulWalk(3, 1); |
233 } | 238 } |
234 | 239 |
235 TEST_F(StackWalkerX86Test, WalkStopAtOverflowingBasePointer) { | 240 TEST_F(StackWalkerTest, WalkStopAtOverflowingBasePointer) { |
236 BuildValidFrame(0); | 241 BuildValidFrame(0); |
237 ExpectSuccessfulWalk(2, 0); | 242 ExpectSuccessfulWalk(2, 0); |
238 | 243 |
239 BuildInvalidFrameOverflowingBasePointer(); | 244 BuildInvalidFrameOverflowingBasePointer(); |
240 ExpectSuccessfulWalk(2, 0); | 245 ExpectSuccessfulWalk(2, 0); |
241 | 246 |
242 BuildValidFrame(2); | 247 BuildValidFrame(2); |
243 ExpectSuccessfulWalk(3, 0); | 248 ExpectSuccessfulWalk(3, 0); |
244 | 249 |
245 BuildValidFrame(1); | 250 BuildValidFrame(1); |
246 ExpectSuccessfulWalk(4, 0); | 251 ExpectSuccessfulWalk(4, 0); |
247 ExpectSuccessfulWalk(3, 1); | 252 ExpectSuccessfulWalk(3, 1); |
248 } | 253 } |
249 | 254 |
250 TEST_F(StackWalkerX86Test, CompareToCaptureStackBackTrace) { | 255 #endif // !defined _WIN64 |
| 256 |
| 257 TEST_F(StackWalkerTest, CompareToCaptureStackBackTrace) { |
251 // Use the OS stack walker to get the number of frames. Skip the top frame | 258 // Use the OS stack walker to get the number of frames. Skip the top frame |
252 // (in this function) as WalkStack and CaptureStackBackTrace won't have the | 259 // (in this function) as WalkStack and CaptureStackBackTrace won't have the |
253 // same return address. | 260 // same return address. |
254 size_t num_frames = | 261 uint32_t num_frames = |
255 ::CaptureStackBackTrace(1, kMaxFrames, frames_, nullptr); | 262 ::CaptureStackBackTrace(1, kMaxFrames, frames_, nullptr); |
256 | 263 |
257 while (num_frames > 0) { | 264 while (num_frames > 0) { |
258 StackId stack_id; | 265 StackId stack_id; |
259 size_t num_frames2 = WalkStack(1, num_frames, frames_, &stack_id); | 266 size_t num_frames2 = WalkStack(1, num_frames, frames_, &stack_id); |
260 size_t exp_frames2 = | 267 size_t exp_frames2 = |
261 ::CaptureStackBackTrace(1, num_frames, frames2_, nullptr); | 268 ::CaptureStackBackTrace(1, num_frames, frames2_, nullptr); |
262 EXPECT_EQ(num_frames, num_frames2); | 269 EXPECT_EQ(num_frames, num_frames2); |
263 EXPECT_EQ(exp_frames2, num_frames2); | 270 EXPECT_EQ(exp_frames2, num_frames2); |
264 EXPECT_EQ(0, ::memcmp(frames_, frames2_, num_frames * sizeof(*frames_))); | 271 EXPECT_EQ(0, ::memcmp(frames_, frames2_, num_frames * sizeof(*frames_))); |
265 | 272 |
266 --num_frames; | 273 --num_frames; |
267 } | 274 } |
268 } | 275 } |
269 | 276 |
270 } // namespace common | 277 } // namespace common |
271 } // namespace agent | 278 } // namespace agent |
OLD | NEW |