Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2010, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> |
michael@0 | 31 | |
michael@0 | 32 | // stackwalker_amd64_unittest.cc: Unit tests for StackwalkerAMD64 class. |
michael@0 | 33 | |
michael@0 | 34 | #include <string.h> |
michael@0 | 35 | #include <string> |
michael@0 | 36 | #include <vector> |
michael@0 | 37 | |
michael@0 | 38 | #include "breakpad_googletest_includes.h" |
michael@0 | 39 | #include "common/test_assembler.h" |
michael@0 | 40 | #include "common/using_std_string.h" |
michael@0 | 41 | #include "google_breakpad/common/minidump_format.h" |
michael@0 | 42 | #include "google_breakpad/processor/basic_source_line_resolver.h" |
michael@0 | 43 | #include "google_breakpad/processor/call_stack.h" |
michael@0 | 44 | #include "google_breakpad/processor/code_module.h" |
michael@0 | 45 | #include "google_breakpad/processor/source_line_resolver_interface.h" |
michael@0 | 46 | #include "google_breakpad/processor/stack_frame_cpu.h" |
michael@0 | 47 | #include "processor/stackwalker_unittest_utils.h" |
michael@0 | 48 | #include "processor/stackwalker_amd64.h" |
michael@0 | 49 | |
michael@0 | 50 | using google_breakpad::BasicSourceLineResolver; |
michael@0 | 51 | using google_breakpad::CallStack; |
michael@0 | 52 | using google_breakpad::CodeModule; |
michael@0 | 53 | using google_breakpad::StackFrameSymbolizer; |
michael@0 | 54 | using google_breakpad::StackFrame; |
michael@0 | 55 | using google_breakpad::StackFrameAMD64; |
michael@0 | 56 | using google_breakpad::StackwalkerAMD64; |
michael@0 | 57 | using google_breakpad::SystemInfo; |
michael@0 | 58 | using google_breakpad::test_assembler::kLittleEndian; |
michael@0 | 59 | using google_breakpad::test_assembler::Label; |
michael@0 | 60 | using google_breakpad::test_assembler::Section; |
michael@0 | 61 | using std::vector; |
michael@0 | 62 | using testing::_; |
michael@0 | 63 | using testing::Return; |
michael@0 | 64 | using testing::SetArgumentPointee; |
michael@0 | 65 | using testing::Test; |
michael@0 | 66 | |
michael@0 | 67 | class StackwalkerAMD64Fixture { |
michael@0 | 68 | public: |
michael@0 | 69 | StackwalkerAMD64Fixture() |
michael@0 | 70 | : stack_section(kLittleEndian), |
michael@0 | 71 | // Give the two modules reasonable standard locations and names |
michael@0 | 72 | // for tests to play with. |
michael@0 | 73 | module1(0x40000000c0000000ULL, 0x10000, "module1", "version1"), |
michael@0 | 74 | module2(0x50000000b0000000ULL, 0x10000, "module2", "version2") { |
michael@0 | 75 | // Identify the system as a Linux system. |
michael@0 | 76 | system_info.os = "Linux"; |
michael@0 | 77 | system_info.os_short = "linux"; |
michael@0 | 78 | system_info.os_version = "Horrendous Hippo"; |
michael@0 | 79 | system_info.cpu = "x86"; |
michael@0 | 80 | system_info.cpu_info = ""; |
michael@0 | 81 | |
michael@0 | 82 | // Put distinctive values in the raw CPU context. |
michael@0 | 83 | BrandContext(&raw_context); |
michael@0 | 84 | |
michael@0 | 85 | // Create some modules with some stock debugging information. |
michael@0 | 86 | modules.Add(&module1); |
michael@0 | 87 | modules.Add(&module2); |
michael@0 | 88 | |
michael@0 | 89 | // By default, none of the modules have symbol info; call |
michael@0 | 90 | // SetModuleSymbols to override this. |
michael@0 | 91 | EXPECT_CALL(supplier, GetCStringSymbolData(_, _, _, _)) |
michael@0 | 92 | .WillRepeatedly(Return(MockSymbolSupplier::NOT_FOUND)); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Set the Breakpad symbol information that supplier should return for |
michael@0 | 96 | // MODULE to INFO. |
michael@0 | 97 | void SetModuleSymbols(MockCodeModule *module, const string &info) { |
michael@0 | 98 | char *buffer = supplier.CopySymbolDataAndOwnTheCopy(info); |
michael@0 | 99 | EXPECT_CALL(supplier, GetCStringSymbolData(module, &system_info, _, _)) |
michael@0 | 100 | .WillRepeatedly(DoAll(SetArgumentPointee<3>(buffer), |
michael@0 | 101 | Return(MockSymbolSupplier::FOUND))); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | // Populate stack_region with the contents of stack_section. Use |
michael@0 | 105 | // stack_section.start() as the region's starting address. |
michael@0 | 106 | void RegionFromSection() { |
michael@0 | 107 | string contents; |
michael@0 | 108 | ASSERT_TRUE(stack_section.GetContents(&contents)); |
michael@0 | 109 | stack_region.Init(stack_section.start().Value(), contents); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | // Fill RAW_CONTEXT with pseudo-random data, for round-trip checking. |
michael@0 | 113 | void BrandContext(MDRawContextAMD64 *raw_context) { |
michael@0 | 114 | uint8_t x = 173; |
michael@0 | 115 | for (size_t i = 0; i < sizeof(*raw_context); i++) |
michael@0 | 116 | reinterpret_cast<uint8_t *>(raw_context)[i] = (x += 17); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | SystemInfo system_info; |
michael@0 | 120 | MDRawContextAMD64 raw_context; |
michael@0 | 121 | Section stack_section; |
michael@0 | 122 | MockMemoryRegion stack_region; |
michael@0 | 123 | MockCodeModule module1; |
michael@0 | 124 | MockCodeModule module2; |
michael@0 | 125 | MockCodeModules modules; |
michael@0 | 126 | MockSymbolSupplier supplier; |
michael@0 | 127 | BasicSourceLineResolver resolver; |
michael@0 | 128 | CallStack call_stack; |
michael@0 | 129 | const vector<StackFrame *> *frames; |
michael@0 | 130 | }; |
michael@0 | 131 | |
michael@0 | 132 | class GetContextFrame: public StackwalkerAMD64Fixture, public Test { }; |
michael@0 | 133 | |
michael@0 | 134 | class SanityCheck: public StackwalkerAMD64Fixture, public Test { }; |
michael@0 | 135 | |
michael@0 | 136 | TEST_F(SanityCheck, NoResolver) { |
michael@0 | 137 | // There should be no references to the stack in this walk: we don't |
michael@0 | 138 | // provide any call frame information, so trying to reconstruct the |
michael@0 | 139 | // context frame's caller should fail. So there's no need for us to |
michael@0 | 140 | // provide stack contents. |
michael@0 | 141 | raw_context.rip = 0x40000000c0000200ULL; |
michael@0 | 142 | raw_context.rbp = 0x8000000080000000ULL; |
michael@0 | 143 | |
michael@0 | 144 | StackFrameSymbolizer frame_symbolizer(NULL, NULL); |
michael@0 | 145 | StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
michael@0 | 146 | &frame_symbolizer); |
michael@0 | 147 | // This should succeed even without a resolver or supplier. |
michael@0 | 148 | vector<const CodeModule*> modules_without_symbols; |
michael@0 | 149 | ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
michael@0 | 150 | ASSERT_EQ(1U, modules_without_symbols.size()); |
michael@0 | 151 | ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
michael@0 | 152 | frames = call_stack.frames(); |
michael@0 | 153 | ASSERT_GE(1U, frames->size()); |
michael@0 | 154 | StackFrameAMD64 *frame = static_cast<StackFrameAMD64 *>(frames->at(0)); |
michael@0 | 155 | // Check that the values from the original raw context made it |
michael@0 | 156 | // through to the context in the stack frame. |
michael@0 | 157 | EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | TEST_F(GetContextFrame, Simple) { |
michael@0 | 161 | // There should be no references to the stack in this walk: we don't |
michael@0 | 162 | // provide any call frame information, so trying to reconstruct the |
michael@0 | 163 | // context frame's caller should fail. So there's no need for us to |
michael@0 | 164 | // provide stack contents. |
michael@0 | 165 | raw_context.rip = 0x40000000c0000200ULL; |
michael@0 | 166 | raw_context.rbp = 0x8000000080000000ULL; |
michael@0 | 167 | |
michael@0 | 168 | StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
michael@0 | 169 | StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
michael@0 | 170 | &frame_symbolizer); |
michael@0 | 171 | vector<const CodeModule*> modules_without_symbols; |
michael@0 | 172 | ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
michael@0 | 173 | ASSERT_EQ(1U, modules_without_symbols.size()); |
michael@0 | 174 | ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
michael@0 | 175 | frames = call_stack.frames(); |
michael@0 | 176 | ASSERT_GE(1U, frames->size()); |
michael@0 | 177 | StackFrameAMD64 *frame = static_cast<StackFrameAMD64 *>(frames->at(0)); |
michael@0 | 178 | // Check that the values from the original raw context made it |
michael@0 | 179 | // through to the context in the stack frame. |
michael@0 | 180 | EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | // The stackwalker should be able to produce the context frame even |
michael@0 | 184 | // without stack memory present. |
michael@0 | 185 | TEST_F(GetContextFrame, NoStackMemory) { |
michael@0 | 186 | raw_context.rip = 0x40000000c0000200ULL; |
michael@0 | 187 | raw_context.rbp = 0x8000000080000000ULL; |
michael@0 | 188 | |
michael@0 | 189 | StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
michael@0 | 190 | StackwalkerAMD64 walker(&system_info, &raw_context, NULL, &modules, |
michael@0 | 191 | &frame_symbolizer); |
michael@0 | 192 | vector<const CodeModule*> modules_without_symbols; |
michael@0 | 193 | ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
michael@0 | 194 | ASSERT_EQ(1U, modules_without_symbols.size()); |
michael@0 | 195 | ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
michael@0 | 196 | frames = call_stack.frames(); |
michael@0 | 197 | ASSERT_GE(1U, frames->size()); |
michael@0 | 198 | StackFrameAMD64 *frame = static_cast<StackFrameAMD64 *>(frames->at(0)); |
michael@0 | 199 | // Check that the values from the original raw context made it |
michael@0 | 200 | // through to the context in the stack frame. |
michael@0 | 201 | EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | class GetCallerFrame: public StackwalkerAMD64Fixture, public Test { }; |
michael@0 | 205 | |
michael@0 | 206 | TEST_F(GetCallerFrame, ScanWithoutSymbols) { |
michael@0 | 207 | // When the stack walker resorts to scanning the stack, |
michael@0 | 208 | // only addresses located within loaded modules are |
michael@0 | 209 | // considered valid return addresses. |
michael@0 | 210 | // Force scanning through three frames to ensure that the |
michael@0 | 211 | // stack pointer is set properly in scan-recovered frames. |
michael@0 | 212 | stack_section.start() = 0x8000000080000000ULL; |
michael@0 | 213 | uint64_t return_address1 = 0x50000000b0000100ULL; |
michael@0 | 214 | uint64_t return_address2 = 0x50000000b0000900ULL; |
michael@0 | 215 | Label frame1_sp, frame2_sp, frame1_rbp; |
michael@0 | 216 | stack_section |
michael@0 | 217 | // frame 0 |
michael@0 | 218 | .Append(16, 0) // space |
michael@0 | 219 | |
michael@0 | 220 | .D64(0x40000000b0000000ULL) // junk that's not |
michael@0 | 221 | .D64(0x50000000d0000000ULL) // a return address |
michael@0 | 222 | |
michael@0 | 223 | .D64(return_address1) // actual return address |
michael@0 | 224 | // frame 1 |
michael@0 | 225 | .Mark(&frame1_sp) |
michael@0 | 226 | .Append(16, 0) // space |
michael@0 | 227 | |
michael@0 | 228 | .D64(0x40000000b0000000ULL) // more junk |
michael@0 | 229 | .D64(0x50000000d0000000ULL) |
michael@0 | 230 | |
michael@0 | 231 | .Mark(&frame1_rbp) |
michael@0 | 232 | .D64(stack_section.start()) // This is in the right place to be |
michael@0 | 233 | // a saved rbp, but it's bogus, so |
michael@0 | 234 | // we shouldn't report it. |
michael@0 | 235 | |
michael@0 | 236 | .D64(return_address2) // actual return address |
michael@0 | 237 | // frame 2 |
michael@0 | 238 | .Mark(&frame2_sp) |
michael@0 | 239 | .Append(32, 0); // end of stack |
michael@0 | 240 | |
michael@0 | 241 | RegionFromSection(); |
michael@0 | 242 | |
michael@0 | 243 | raw_context.rip = 0x40000000c0000200ULL; |
michael@0 | 244 | raw_context.rbp = frame1_rbp.Value(); |
michael@0 | 245 | raw_context.rsp = stack_section.start().Value(); |
michael@0 | 246 | |
michael@0 | 247 | StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
michael@0 | 248 | StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
michael@0 | 249 | &frame_symbolizer); |
michael@0 | 250 | vector<const CodeModule*> modules_without_symbols; |
michael@0 | 251 | ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
michael@0 | 252 | ASSERT_EQ(2U, modules_without_symbols.size()); |
michael@0 | 253 | ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
michael@0 | 254 | ASSERT_EQ("module2", modules_without_symbols[1]->debug_file()); |
michael@0 | 255 | frames = call_stack.frames(); |
michael@0 | 256 | ASSERT_EQ(3U, frames->size()); |
michael@0 | 257 | |
michael@0 | 258 | StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
michael@0 | 259 | EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
michael@0 | 260 | ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
michael@0 | 261 | EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context))); |
michael@0 | 262 | |
michael@0 | 263 | StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
michael@0 | 264 | EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); |
michael@0 | 265 | ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
michael@0 | 266 | StackFrameAMD64::CONTEXT_VALID_RSP | |
michael@0 | 267 | StackFrameAMD64::CONTEXT_VALID_RBP), |
michael@0 | 268 | frame1->context_validity); |
michael@0 | 269 | EXPECT_EQ(return_address1, frame1->context.rip); |
michael@0 | 270 | EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); |
michael@0 | 271 | EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); |
michael@0 | 272 | |
michael@0 | 273 | StackFrameAMD64 *frame2 = static_cast<StackFrameAMD64 *>(frames->at(2)); |
michael@0 | 274 | EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame2->trust); |
michael@0 | 275 | ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
michael@0 | 276 | StackFrameAMD64::CONTEXT_VALID_RSP), |
michael@0 | 277 | frame2->context_validity); |
michael@0 | 278 | EXPECT_EQ(return_address2, frame2->context.rip); |
michael@0 | 279 | EXPECT_EQ(frame2_sp.Value(), frame2->context.rsp); |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | TEST_F(GetCallerFrame, ScanWithFunctionSymbols) { |
michael@0 | 283 | // During stack scanning, if a potential return address |
michael@0 | 284 | // is located within a loaded module that has symbols, |
michael@0 | 285 | // it is only considered a valid return address if it |
michael@0 | 286 | // lies within a function's bounds. |
michael@0 | 287 | stack_section.start() = 0x8000000080000000ULL; |
michael@0 | 288 | uint64_t return_address = 0x50000000b0000110ULL; |
michael@0 | 289 | Label frame1_sp, frame1_rbp; |
michael@0 | 290 | |
michael@0 | 291 | stack_section |
michael@0 | 292 | // frame 0 |
michael@0 | 293 | .Append(16, 0) // space |
michael@0 | 294 | |
michael@0 | 295 | .D64(0x40000000b0000000ULL) // junk that's not |
michael@0 | 296 | .D64(0x50000000b0000000ULL) // a return address |
michael@0 | 297 | |
michael@0 | 298 | .D64(0x40000000c0001000ULL) // a couple of plausible addresses |
michael@0 | 299 | .D64(0x50000000b000aaaaULL) // that are not within functions |
michael@0 | 300 | |
michael@0 | 301 | .D64(return_address) // actual return address |
michael@0 | 302 | // frame 1 |
michael@0 | 303 | .Mark(&frame1_sp) |
michael@0 | 304 | .Append(32, 0) // end of stack |
michael@0 | 305 | .Mark(&frame1_rbp); |
michael@0 | 306 | RegionFromSection(); |
michael@0 | 307 | |
michael@0 | 308 | raw_context.rip = 0x40000000c0000200ULL; |
michael@0 | 309 | raw_context.rbp = frame1_rbp.Value(); |
michael@0 | 310 | raw_context.rsp = stack_section.start().Value(); |
michael@0 | 311 | |
michael@0 | 312 | SetModuleSymbols(&module1, |
michael@0 | 313 | // The youngest frame's function. |
michael@0 | 314 | "FUNC 100 400 10 platypus\n"); |
michael@0 | 315 | SetModuleSymbols(&module2, |
michael@0 | 316 | // The calling frame's function. |
michael@0 | 317 | "FUNC 100 400 10 echidna\n"); |
michael@0 | 318 | |
michael@0 | 319 | StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
michael@0 | 320 | StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
michael@0 | 321 | &frame_symbolizer); |
michael@0 | 322 | vector<const CodeModule*> modules_without_symbols; |
michael@0 | 323 | ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
michael@0 | 324 | ASSERT_EQ(0U, modules_without_symbols.size()); |
michael@0 | 325 | frames = call_stack.frames(); |
michael@0 | 326 | ASSERT_EQ(2U, frames->size()); |
michael@0 | 327 | |
michael@0 | 328 | StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
michael@0 | 329 | EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
michael@0 | 330 | ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
michael@0 | 331 | EXPECT_EQ("platypus", frame0->function_name); |
michael@0 | 332 | EXPECT_EQ(0x40000000c0000100ULL, frame0->function_base); |
michael@0 | 333 | |
michael@0 | 334 | StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
michael@0 | 335 | EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); |
michael@0 | 336 | ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
michael@0 | 337 | StackFrameAMD64::CONTEXT_VALID_RSP | |
michael@0 | 338 | StackFrameAMD64::CONTEXT_VALID_RBP), |
michael@0 | 339 | frame1->context_validity); |
michael@0 | 340 | EXPECT_EQ(return_address, frame1->context.rip); |
michael@0 | 341 | EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); |
michael@0 | 342 | EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); |
michael@0 | 343 | EXPECT_EQ("echidna", frame1->function_name); |
michael@0 | 344 | EXPECT_EQ(0x50000000b0000100ULL, frame1->function_base); |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | TEST_F(GetCallerFrame, CallerPushedRBP) { |
michael@0 | 348 | // Functions typically push their %rbp upon entry and set %rbp pointing |
michael@0 | 349 | // there. If stackwalking finds a plausible address for the next frame's |
michael@0 | 350 | // %rbp directly below the return address, assume that it is indeed the |
michael@0 | 351 | // next frame's %rbp. |
michael@0 | 352 | stack_section.start() = 0x8000000080000000ULL; |
michael@0 | 353 | uint64_t return_address = 0x50000000b0000110ULL; |
michael@0 | 354 | Label frame0_rbp, frame1_sp, frame1_rbp; |
michael@0 | 355 | |
michael@0 | 356 | stack_section |
michael@0 | 357 | // frame 0 |
michael@0 | 358 | .Append(16, 0) // space |
michael@0 | 359 | |
michael@0 | 360 | .D64(0x40000000b0000000ULL) // junk that's not |
michael@0 | 361 | .D64(0x50000000b0000000ULL) // a return address |
michael@0 | 362 | |
michael@0 | 363 | .D64(0x40000000c0001000ULL) // a couple of plausible addresses |
michael@0 | 364 | .D64(0x50000000b000aaaaULL) // that are not within functions |
michael@0 | 365 | |
michael@0 | 366 | .Mark(&frame0_rbp) |
michael@0 | 367 | .D64(frame1_rbp) // caller-pushed %rbp |
michael@0 | 368 | .D64(return_address) // actual return address |
michael@0 | 369 | // frame 1 |
michael@0 | 370 | .Mark(&frame1_sp) |
michael@0 | 371 | .Append(32, 0) // body of frame1 |
michael@0 | 372 | .Mark(&frame1_rbp); // end of stack |
michael@0 | 373 | RegionFromSection(); |
michael@0 | 374 | |
michael@0 | 375 | raw_context.rip = 0x40000000c0000200ULL; |
michael@0 | 376 | raw_context.rbp = frame0_rbp.Value(); |
michael@0 | 377 | raw_context.rsp = stack_section.start().Value(); |
michael@0 | 378 | |
michael@0 | 379 | SetModuleSymbols(&module1, |
michael@0 | 380 | // The youngest frame's function. |
michael@0 | 381 | "FUNC 100 400 10 sasquatch\n"); |
michael@0 | 382 | SetModuleSymbols(&module2, |
michael@0 | 383 | // The calling frame's function. |
michael@0 | 384 | "FUNC 100 400 10 yeti\n"); |
michael@0 | 385 | |
michael@0 | 386 | StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
michael@0 | 387 | StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
michael@0 | 388 | &frame_symbolizer); |
michael@0 | 389 | vector<const CodeModule*> modules_without_symbols; |
michael@0 | 390 | ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
michael@0 | 391 | ASSERT_EQ(0U, modules_without_symbols.size()); |
michael@0 | 392 | frames = call_stack.frames(); |
michael@0 | 393 | ASSERT_EQ(2U, frames->size()); |
michael@0 | 394 | |
michael@0 | 395 | StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
michael@0 | 396 | EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
michael@0 | 397 | ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
michael@0 | 398 | EXPECT_EQ(frame0_rbp.Value(), frame0->context.rbp); |
michael@0 | 399 | EXPECT_EQ("sasquatch", frame0->function_name); |
michael@0 | 400 | EXPECT_EQ(0x40000000c0000100ULL, frame0->function_base); |
michael@0 | 401 | |
michael@0 | 402 | StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
michael@0 | 403 | EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); |
michael@0 | 404 | ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
michael@0 | 405 | StackFrameAMD64::CONTEXT_VALID_RSP | |
michael@0 | 406 | StackFrameAMD64::CONTEXT_VALID_RBP), |
michael@0 | 407 | frame1->context_validity); |
michael@0 | 408 | EXPECT_EQ(return_address, frame1->context.rip); |
michael@0 | 409 | EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); |
michael@0 | 410 | EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); |
michael@0 | 411 | EXPECT_EQ("yeti", frame1->function_name); |
michael@0 | 412 | EXPECT_EQ(0x50000000b0000100ULL, frame1->function_base); |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | struct CFIFixture: public StackwalkerAMD64Fixture { |
michael@0 | 416 | CFIFixture() { |
michael@0 | 417 | // Provide a bunch of STACK CFI records; we'll walk to the caller |
michael@0 | 418 | // from every point in this series, expecting to find the same set |
michael@0 | 419 | // of register values. |
michael@0 | 420 | SetModuleSymbols(&module1, |
michael@0 | 421 | // The youngest frame's function. |
michael@0 | 422 | "FUNC 4000 1000 10 enchiridion\n" |
michael@0 | 423 | // Initially, just a return address. |
michael@0 | 424 | "STACK CFI INIT 4000 100 .cfa: $rsp 8 + .ra: .cfa 8 - ^\n" |
michael@0 | 425 | // Push %rbx. |
michael@0 | 426 | "STACK CFI 4001 .cfa: $rsp 16 + $rbx: .cfa 16 - ^\n" |
michael@0 | 427 | // Save %r12 in %rbx. Weird, but permitted. |
michael@0 | 428 | "STACK CFI 4002 $r12: $rbx\n" |
michael@0 | 429 | // Allocate frame space, and save %r13. |
michael@0 | 430 | "STACK CFI 4003 .cfa: $rsp 40 + $r13: .cfa 32 - ^\n" |
michael@0 | 431 | // Put the return address in %r13. |
michael@0 | 432 | "STACK CFI 4005 .ra: $r13\n" |
michael@0 | 433 | // Save %rbp, and use it as a frame pointer. |
michael@0 | 434 | "STACK CFI 4006 .cfa: $rbp 16 + $rbp: .cfa 24 - ^\n" |
michael@0 | 435 | |
michael@0 | 436 | // The calling function. |
michael@0 | 437 | "FUNC 5000 1000 10 epictetus\n" |
michael@0 | 438 | // Mark it as end of stack. |
michael@0 | 439 | "STACK CFI INIT 5000 1000 .cfa: $rsp .ra 0\n"); |
michael@0 | 440 | |
michael@0 | 441 | // Provide some distinctive values for the caller's registers. |
michael@0 | 442 | expected.rsp = 0x8000000080000000ULL; |
michael@0 | 443 | expected.rip = 0x40000000c0005510ULL; |
michael@0 | 444 | expected.rbp = 0x68995b1de4700266ULL; |
michael@0 | 445 | expected.rbx = 0x5a5beeb38de23be8ULL; |
michael@0 | 446 | expected.r12 = 0xed1b02e8cc0fc79cULL; |
michael@0 | 447 | expected.r13 = 0x1d20ad8acacbe930ULL; |
michael@0 | 448 | expected.r14 = 0xe94cffc2f7adaa28ULL; |
michael@0 | 449 | expected.r15 = 0xb638d17d8da413b5ULL; |
michael@0 | 450 | |
michael@0 | 451 | // By default, registers are unchanged. |
michael@0 | 452 | raw_context = expected; |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | // Walk the stack, using stack_section as the contents of the stack |
michael@0 | 456 | // and raw_context as the current register values. (Set |
michael@0 | 457 | // raw_context.rsp to the stack's starting address.) Expect two |
michael@0 | 458 | // stack frames; in the older frame, expect the callee-saves |
michael@0 | 459 | // registers to have values matching those in 'expected'. |
michael@0 | 460 | void CheckWalk() { |
michael@0 | 461 | RegionFromSection(); |
michael@0 | 462 | raw_context.rsp = stack_section.start().Value(); |
michael@0 | 463 | |
michael@0 | 464 | StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
michael@0 | 465 | StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
michael@0 | 466 | &frame_symbolizer); |
michael@0 | 467 | vector<const CodeModule*> modules_without_symbols; |
michael@0 | 468 | ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
michael@0 | 469 | ASSERT_EQ(0U, modules_without_symbols.size()); |
michael@0 | 470 | frames = call_stack.frames(); |
michael@0 | 471 | ASSERT_EQ(2U, frames->size()); |
michael@0 | 472 | |
michael@0 | 473 | StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
michael@0 | 474 | EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
michael@0 | 475 | ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
michael@0 | 476 | EXPECT_EQ("enchiridion", frame0->function_name); |
michael@0 | 477 | EXPECT_EQ(0x40000000c0004000ULL, frame0->function_base); |
michael@0 | 478 | |
michael@0 | 479 | StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
michael@0 | 480 | EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); |
michael@0 | 481 | ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
michael@0 | 482 | StackFrameAMD64::CONTEXT_VALID_RSP | |
michael@0 | 483 | StackFrameAMD64::CONTEXT_VALID_RBP | |
michael@0 | 484 | StackFrameAMD64::CONTEXT_VALID_RBX | |
michael@0 | 485 | StackFrameAMD64::CONTEXT_VALID_R12 | |
michael@0 | 486 | StackFrameAMD64::CONTEXT_VALID_R13 | |
michael@0 | 487 | StackFrameAMD64::CONTEXT_VALID_R14 | |
michael@0 | 488 | StackFrameAMD64::CONTEXT_VALID_R15), |
michael@0 | 489 | frame1->context_validity); |
michael@0 | 490 | EXPECT_EQ(expected.rip, frame1->context.rip); |
michael@0 | 491 | EXPECT_EQ(expected.rsp, frame1->context.rsp); |
michael@0 | 492 | EXPECT_EQ(expected.rbp, frame1->context.rbp); |
michael@0 | 493 | EXPECT_EQ(expected.rbx, frame1->context.rbx); |
michael@0 | 494 | EXPECT_EQ(expected.r12, frame1->context.r12); |
michael@0 | 495 | EXPECT_EQ(expected.r13, frame1->context.r13); |
michael@0 | 496 | EXPECT_EQ(expected.r14, frame1->context.r14); |
michael@0 | 497 | EXPECT_EQ(expected.r15, frame1->context.r15); |
michael@0 | 498 | EXPECT_EQ("epictetus", frame1->function_name); |
michael@0 | 499 | } |
michael@0 | 500 | |
michael@0 | 501 | // The values we expect to find for the caller's registers. |
michael@0 | 502 | MDRawContextAMD64 expected; |
michael@0 | 503 | }; |
michael@0 | 504 | |
michael@0 | 505 | class CFI: public CFIFixture, public Test { }; |
michael@0 | 506 | |
michael@0 | 507 | TEST_F(CFI, At4000) { |
michael@0 | 508 | Label frame1_rsp = expected.rsp; |
michael@0 | 509 | stack_section |
michael@0 | 510 | .D64(0x40000000c0005510ULL) // return address |
michael@0 | 511 | .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
michael@0 | 512 | raw_context.rip = 0x40000000c0004000ULL; |
michael@0 | 513 | CheckWalk(); |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | TEST_F(CFI, At4001) { |
michael@0 | 517 | Label frame1_rsp = expected.rsp; |
michael@0 | 518 | stack_section |
michael@0 | 519 | .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
michael@0 | 520 | .D64(0x40000000c0005510ULL) // return address |
michael@0 | 521 | .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
michael@0 | 522 | raw_context.rip = 0x40000000c0004001ULL; |
michael@0 | 523 | raw_context.rbx = 0xbe0487d2f9eafe29ULL; // callee's (distinct) %rbx value |
michael@0 | 524 | CheckWalk(); |
michael@0 | 525 | } |
michael@0 | 526 | |
michael@0 | 527 | TEST_F(CFI, At4002) { |
michael@0 | 528 | Label frame1_rsp = expected.rsp; |
michael@0 | 529 | stack_section |
michael@0 | 530 | .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
michael@0 | 531 | .D64(0x40000000c0005510ULL) // return address |
michael@0 | 532 | .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
michael@0 | 533 | raw_context.rip = 0x40000000c0004002ULL; |
michael@0 | 534 | raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
michael@0 | 535 | raw_context.r12 = 0xb0118de918a4bceaULL; // callee's (distinct) %r12 value |
michael@0 | 536 | CheckWalk(); |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | TEST_F(CFI, At4003) { |
michael@0 | 540 | Label frame1_rsp = expected.rsp; |
michael@0 | 541 | stack_section |
michael@0 | 542 | .D64(0x0e023828dffd4d81ULL) // garbage |
michael@0 | 543 | .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
michael@0 | 544 | .D64(0x319e68b49e3ace0fULL) // garbage |
michael@0 | 545 | .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
michael@0 | 546 | .D64(0x40000000c0005510ULL) // return address |
michael@0 | 547 | .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
michael@0 | 548 | raw_context.rip = 0x40000000c0004003ULL; |
michael@0 | 549 | raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
michael@0 | 550 | raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12 |
michael@0 | 551 | raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13 |
michael@0 | 552 | CheckWalk(); |
michael@0 | 553 | } |
michael@0 | 554 | |
michael@0 | 555 | // The results here should be the same as those at module offset 0x4003. |
michael@0 | 556 | TEST_F(CFI, At4004) { |
michael@0 | 557 | Label frame1_rsp = expected.rsp; |
michael@0 | 558 | stack_section |
michael@0 | 559 | .D64(0x0e023828dffd4d81ULL) // garbage |
michael@0 | 560 | .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
michael@0 | 561 | .D64(0x319e68b49e3ace0fULL) // garbage |
michael@0 | 562 | .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
michael@0 | 563 | .D64(0x40000000c0005510ULL) // return address |
michael@0 | 564 | .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
michael@0 | 565 | raw_context.rip = 0x40000000c0004004ULL; |
michael@0 | 566 | raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
michael@0 | 567 | raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12 |
michael@0 | 568 | raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13 |
michael@0 | 569 | CheckWalk(); |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | TEST_F(CFI, At4005) { |
michael@0 | 573 | Label frame1_rsp = expected.rsp; |
michael@0 | 574 | stack_section |
michael@0 | 575 | .D64(0x4b516dd035745953ULL) // garbage |
michael@0 | 576 | .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
michael@0 | 577 | .D64(0xa6d445e16ae3d872ULL) // garbage |
michael@0 | 578 | .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
michael@0 | 579 | .D64(0xaa95fa054aedfbaeULL) // garbage |
michael@0 | 580 | .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
michael@0 | 581 | raw_context.rip = 0x40000000c0004005ULL; |
michael@0 | 582 | raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
michael@0 | 583 | raw_context.r12 = 0x46b1b8868891b34aULL; // callee's %r12 |
michael@0 | 584 | raw_context.r13 = 0x40000000c0005510ULL; // return address |
michael@0 | 585 | CheckWalk(); |
michael@0 | 586 | } |
michael@0 | 587 | |
michael@0 | 588 | TEST_F(CFI, At4006) { |
michael@0 | 589 | Label frame0_rbp; |
michael@0 | 590 | Label frame1_rsp = expected.rsp; |
michael@0 | 591 | stack_section |
michael@0 | 592 | .D64(0x043c6dfceb91aa34ULL) // garbage |
michael@0 | 593 | .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
michael@0 | 594 | .D64(0x68995b1de4700266ULL) // saved %rbp |
michael@0 | 595 | .Mark(&frame0_rbp) // frame pointer points here |
michael@0 | 596 | .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
michael@0 | 597 | .D64(0xf015ee516ad89eabULL) // garbage |
michael@0 | 598 | .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
michael@0 | 599 | raw_context.rip = 0x40000000c0004006ULL; |
michael@0 | 600 | raw_context.rbp = frame0_rbp.Value(); |
michael@0 | 601 | raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
michael@0 | 602 | raw_context.r12 = 0x26e007b341acfebdULL; // callee's %r12 |
michael@0 | 603 | raw_context.r13 = 0x40000000c0005510ULL; // return address |
michael@0 | 604 | CheckWalk(); |
michael@0 | 605 | } |