michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Jim Blandy michael@0: michael@0: // stackwalker_amd64_unittest.cc: Unit tests for StackwalkerAMD64 class. michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "breakpad_googletest_includes.h" michael@0: #include "common/test_assembler.h" michael@0: #include "common/using_std_string.h" michael@0: #include "google_breakpad/common/minidump_format.h" michael@0: #include "google_breakpad/processor/basic_source_line_resolver.h" michael@0: #include "google_breakpad/processor/call_stack.h" michael@0: #include "google_breakpad/processor/code_module.h" michael@0: #include "google_breakpad/processor/source_line_resolver_interface.h" michael@0: #include "google_breakpad/processor/stack_frame_cpu.h" michael@0: #include "processor/stackwalker_unittest_utils.h" michael@0: #include "processor/stackwalker_amd64.h" michael@0: michael@0: using google_breakpad::BasicSourceLineResolver; michael@0: using google_breakpad::CallStack; michael@0: using google_breakpad::CodeModule; michael@0: using google_breakpad::StackFrameSymbolizer; michael@0: using google_breakpad::StackFrame; michael@0: using google_breakpad::StackFrameAMD64; michael@0: using google_breakpad::StackwalkerAMD64; michael@0: using google_breakpad::SystemInfo; michael@0: using google_breakpad::test_assembler::kLittleEndian; michael@0: using google_breakpad::test_assembler::Label; michael@0: using google_breakpad::test_assembler::Section; michael@0: using std::vector; michael@0: using testing::_; michael@0: using testing::Return; michael@0: using testing::SetArgumentPointee; michael@0: using testing::Test; michael@0: michael@0: class StackwalkerAMD64Fixture { michael@0: public: michael@0: StackwalkerAMD64Fixture() michael@0: : stack_section(kLittleEndian), michael@0: // Give the two modules reasonable standard locations and names michael@0: // for tests to play with. michael@0: module1(0x40000000c0000000ULL, 0x10000, "module1", "version1"), michael@0: module2(0x50000000b0000000ULL, 0x10000, "module2", "version2") { michael@0: // Identify the system as a Linux system. michael@0: system_info.os = "Linux"; michael@0: system_info.os_short = "linux"; michael@0: system_info.os_version = "Horrendous Hippo"; michael@0: system_info.cpu = "x86"; michael@0: system_info.cpu_info = ""; michael@0: michael@0: // Put distinctive values in the raw CPU context. michael@0: BrandContext(&raw_context); michael@0: michael@0: // Create some modules with some stock debugging information. michael@0: modules.Add(&module1); michael@0: modules.Add(&module2); michael@0: michael@0: // By default, none of the modules have symbol info; call michael@0: // SetModuleSymbols to override this. michael@0: EXPECT_CALL(supplier, GetCStringSymbolData(_, _, _, _)) michael@0: .WillRepeatedly(Return(MockSymbolSupplier::NOT_FOUND)); michael@0: } michael@0: michael@0: // Set the Breakpad symbol information that supplier should return for michael@0: // MODULE to INFO. michael@0: void SetModuleSymbols(MockCodeModule *module, const string &info) { michael@0: char *buffer = supplier.CopySymbolDataAndOwnTheCopy(info); michael@0: EXPECT_CALL(supplier, GetCStringSymbolData(module, &system_info, _, _)) michael@0: .WillRepeatedly(DoAll(SetArgumentPointee<3>(buffer), michael@0: Return(MockSymbolSupplier::FOUND))); michael@0: } michael@0: michael@0: // Populate stack_region with the contents of stack_section. Use michael@0: // stack_section.start() as the region's starting address. michael@0: void RegionFromSection() { michael@0: string contents; michael@0: ASSERT_TRUE(stack_section.GetContents(&contents)); michael@0: stack_region.Init(stack_section.start().Value(), contents); michael@0: } michael@0: michael@0: // Fill RAW_CONTEXT with pseudo-random data, for round-trip checking. michael@0: void BrandContext(MDRawContextAMD64 *raw_context) { michael@0: uint8_t x = 173; michael@0: for (size_t i = 0; i < sizeof(*raw_context); i++) michael@0: reinterpret_cast(raw_context)[i] = (x += 17); michael@0: } michael@0: michael@0: SystemInfo system_info; michael@0: MDRawContextAMD64 raw_context; michael@0: Section stack_section; michael@0: MockMemoryRegion stack_region; michael@0: MockCodeModule module1; michael@0: MockCodeModule module2; michael@0: MockCodeModules modules; michael@0: MockSymbolSupplier supplier; michael@0: BasicSourceLineResolver resolver; michael@0: CallStack call_stack; michael@0: const vector *frames; michael@0: }; michael@0: michael@0: class GetContextFrame: public StackwalkerAMD64Fixture, public Test { }; michael@0: michael@0: class SanityCheck: public StackwalkerAMD64Fixture, public Test { }; michael@0: michael@0: TEST_F(SanityCheck, NoResolver) { michael@0: // There should be no references to the stack in this walk: we don't michael@0: // provide any call frame information, so trying to reconstruct the michael@0: // context frame's caller should fail. So there's no need for us to michael@0: // provide stack contents. michael@0: raw_context.rip = 0x40000000c0000200ULL; michael@0: raw_context.rbp = 0x8000000080000000ULL; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(NULL, NULL); michael@0: StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, michael@0: &frame_symbolizer); michael@0: // This should succeed even without a resolver or supplier. michael@0: vector modules_without_symbols; michael@0: ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); michael@0: ASSERT_EQ(1U, modules_without_symbols.size()); michael@0: ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_GE(1U, frames->size()); michael@0: StackFrameAMD64 *frame = static_cast(frames->at(0)); michael@0: // Check that the values from the original raw context made it michael@0: // through to the context in the stack frame. michael@0: EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); michael@0: } michael@0: michael@0: TEST_F(GetContextFrame, Simple) { michael@0: // There should be no references to the stack in this walk: we don't michael@0: // provide any call frame information, so trying to reconstruct the michael@0: // context frame's caller should fail. So there's no need for us to michael@0: // provide stack contents. michael@0: raw_context.rip = 0x40000000c0000200ULL; michael@0: raw_context.rbp = 0x8000000080000000ULL; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, michael@0: &frame_symbolizer); michael@0: vector modules_without_symbols; michael@0: ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); michael@0: ASSERT_EQ(1U, modules_without_symbols.size()); michael@0: ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_GE(1U, frames->size()); michael@0: StackFrameAMD64 *frame = static_cast(frames->at(0)); michael@0: // Check that the values from the original raw context made it michael@0: // through to the context in the stack frame. michael@0: EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); michael@0: } michael@0: michael@0: // The stackwalker should be able to produce the context frame even michael@0: // without stack memory present. michael@0: TEST_F(GetContextFrame, NoStackMemory) { michael@0: raw_context.rip = 0x40000000c0000200ULL; michael@0: raw_context.rbp = 0x8000000080000000ULL; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerAMD64 walker(&system_info, &raw_context, NULL, &modules, michael@0: &frame_symbolizer); michael@0: vector modules_without_symbols; michael@0: ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); michael@0: ASSERT_EQ(1U, modules_without_symbols.size()); michael@0: ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_GE(1U, frames->size()); michael@0: StackFrameAMD64 *frame = static_cast(frames->at(0)); michael@0: // Check that the values from the original raw context made it michael@0: // through to the context in the stack frame. michael@0: EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); michael@0: } michael@0: michael@0: class GetCallerFrame: public StackwalkerAMD64Fixture, public Test { }; michael@0: michael@0: TEST_F(GetCallerFrame, ScanWithoutSymbols) { michael@0: // When the stack walker resorts to scanning the stack, michael@0: // only addresses located within loaded modules are michael@0: // considered valid return addresses. michael@0: // Force scanning through three frames to ensure that the michael@0: // stack pointer is set properly in scan-recovered frames. michael@0: stack_section.start() = 0x8000000080000000ULL; michael@0: uint64_t return_address1 = 0x50000000b0000100ULL; michael@0: uint64_t return_address2 = 0x50000000b0000900ULL; michael@0: Label frame1_sp, frame2_sp, frame1_rbp; michael@0: stack_section michael@0: // frame 0 michael@0: .Append(16, 0) // space michael@0: michael@0: .D64(0x40000000b0000000ULL) // junk that's not michael@0: .D64(0x50000000d0000000ULL) // a return address michael@0: michael@0: .D64(return_address1) // actual return address michael@0: // frame 1 michael@0: .Mark(&frame1_sp) michael@0: .Append(16, 0) // space michael@0: michael@0: .D64(0x40000000b0000000ULL) // more junk michael@0: .D64(0x50000000d0000000ULL) michael@0: michael@0: .Mark(&frame1_rbp) michael@0: .D64(stack_section.start()) // This is in the right place to be michael@0: // a saved rbp, but it's bogus, so michael@0: // we shouldn't report it. michael@0: michael@0: .D64(return_address2) // actual return address michael@0: // frame 2 michael@0: .Mark(&frame2_sp) michael@0: .Append(32, 0); // end of stack michael@0: michael@0: RegionFromSection(); michael@0: michael@0: raw_context.rip = 0x40000000c0000200ULL; michael@0: raw_context.rbp = frame1_rbp.Value(); michael@0: raw_context.rsp = stack_section.start().Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, michael@0: &frame_symbolizer); michael@0: vector modules_without_symbols; michael@0: ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); michael@0: ASSERT_EQ(2U, modules_without_symbols.size()); michael@0: ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); michael@0: ASSERT_EQ("module2", modules_without_symbols[1]->debug_file()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_EQ(3U, frames->size()); michael@0: michael@0: StackFrameAMD64 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context))); michael@0: michael@0: StackFrameAMD64 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); michael@0: ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | michael@0: StackFrameAMD64::CONTEXT_VALID_RSP | michael@0: StackFrameAMD64::CONTEXT_VALID_RBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(return_address1, frame1->context.rip); michael@0: EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); michael@0: EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); michael@0: michael@0: StackFrameAMD64 *frame2 = static_cast(frames->at(2)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame2->trust); michael@0: ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | michael@0: StackFrameAMD64::CONTEXT_VALID_RSP), michael@0: frame2->context_validity); michael@0: EXPECT_EQ(return_address2, frame2->context.rip); michael@0: EXPECT_EQ(frame2_sp.Value(), frame2->context.rsp); michael@0: } michael@0: michael@0: TEST_F(GetCallerFrame, ScanWithFunctionSymbols) { michael@0: // During stack scanning, if a potential return address michael@0: // is located within a loaded module that has symbols, michael@0: // it is only considered a valid return address if it michael@0: // lies within a function's bounds. michael@0: stack_section.start() = 0x8000000080000000ULL; michael@0: uint64_t return_address = 0x50000000b0000110ULL; michael@0: Label frame1_sp, frame1_rbp; michael@0: michael@0: stack_section michael@0: // frame 0 michael@0: .Append(16, 0) // space michael@0: michael@0: .D64(0x40000000b0000000ULL) // junk that's not michael@0: .D64(0x50000000b0000000ULL) // a return address michael@0: michael@0: .D64(0x40000000c0001000ULL) // a couple of plausible addresses michael@0: .D64(0x50000000b000aaaaULL) // that are not within functions michael@0: michael@0: .D64(return_address) // actual return address michael@0: // frame 1 michael@0: .Mark(&frame1_sp) michael@0: .Append(32, 0) // end of stack michael@0: .Mark(&frame1_rbp); michael@0: RegionFromSection(); michael@0: michael@0: raw_context.rip = 0x40000000c0000200ULL; michael@0: raw_context.rbp = frame1_rbp.Value(); michael@0: raw_context.rsp = stack_section.start().Value(); michael@0: michael@0: SetModuleSymbols(&module1, michael@0: // The youngest frame's function. michael@0: "FUNC 100 400 10 platypus\n"); michael@0: SetModuleSymbols(&module2, michael@0: // The calling frame's function. michael@0: "FUNC 100 400 10 echidna\n"); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, michael@0: &frame_symbolizer); michael@0: vector modules_without_symbols; michael@0: ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); michael@0: ASSERT_EQ(0U, modules_without_symbols.size()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_EQ(2U, frames->size()); michael@0: michael@0: StackFrameAMD64 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ("platypus", frame0->function_name); michael@0: EXPECT_EQ(0x40000000c0000100ULL, frame0->function_base); michael@0: michael@0: StackFrameAMD64 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); michael@0: ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | michael@0: StackFrameAMD64::CONTEXT_VALID_RSP | michael@0: StackFrameAMD64::CONTEXT_VALID_RBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(return_address, frame1->context.rip); michael@0: EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); michael@0: EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); michael@0: EXPECT_EQ("echidna", frame1->function_name); michael@0: EXPECT_EQ(0x50000000b0000100ULL, frame1->function_base); michael@0: } michael@0: michael@0: TEST_F(GetCallerFrame, CallerPushedRBP) { michael@0: // Functions typically push their %rbp upon entry and set %rbp pointing michael@0: // there. If stackwalking finds a plausible address for the next frame's michael@0: // %rbp directly below the return address, assume that it is indeed the michael@0: // next frame's %rbp. michael@0: stack_section.start() = 0x8000000080000000ULL; michael@0: uint64_t return_address = 0x50000000b0000110ULL; michael@0: Label frame0_rbp, frame1_sp, frame1_rbp; michael@0: michael@0: stack_section michael@0: // frame 0 michael@0: .Append(16, 0) // space michael@0: michael@0: .D64(0x40000000b0000000ULL) // junk that's not michael@0: .D64(0x50000000b0000000ULL) // a return address michael@0: michael@0: .D64(0x40000000c0001000ULL) // a couple of plausible addresses michael@0: .D64(0x50000000b000aaaaULL) // that are not within functions michael@0: michael@0: .Mark(&frame0_rbp) michael@0: .D64(frame1_rbp) // caller-pushed %rbp michael@0: .D64(return_address) // actual return address michael@0: // frame 1 michael@0: .Mark(&frame1_sp) michael@0: .Append(32, 0) // body of frame1 michael@0: .Mark(&frame1_rbp); // end of stack michael@0: RegionFromSection(); michael@0: michael@0: raw_context.rip = 0x40000000c0000200ULL; michael@0: raw_context.rbp = frame0_rbp.Value(); michael@0: raw_context.rsp = stack_section.start().Value(); michael@0: michael@0: SetModuleSymbols(&module1, michael@0: // The youngest frame's function. michael@0: "FUNC 100 400 10 sasquatch\n"); michael@0: SetModuleSymbols(&module2, michael@0: // The calling frame's function. michael@0: "FUNC 100 400 10 yeti\n"); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, michael@0: &frame_symbolizer); michael@0: vector modules_without_symbols; michael@0: ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); michael@0: ASSERT_EQ(0U, modules_without_symbols.size()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_EQ(2U, frames->size()); michael@0: michael@0: StackFrameAMD64 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(frame0_rbp.Value(), frame0->context.rbp); michael@0: EXPECT_EQ("sasquatch", frame0->function_name); michael@0: EXPECT_EQ(0x40000000c0000100ULL, frame0->function_base); michael@0: michael@0: StackFrameAMD64 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); michael@0: ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | michael@0: StackFrameAMD64::CONTEXT_VALID_RSP | michael@0: StackFrameAMD64::CONTEXT_VALID_RBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(return_address, frame1->context.rip); michael@0: EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); michael@0: EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); michael@0: EXPECT_EQ("yeti", frame1->function_name); michael@0: EXPECT_EQ(0x50000000b0000100ULL, frame1->function_base); michael@0: } michael@0: michael@0: struct CFIFixture: public StackwalkerAMD64Fixture { michael@0: CFIFixture() { michael@0: // Provide a bunch of STACK CFI records; we'll walk to the caller michael@0: // from every point in this series, expecting to find the same set michael@0: // of register values. michael@0: SetModuleSymbols(&module1, michael@0: // The youngest frame's function. michael@0: "FUNC 4000 1000 10 enchiridion\n" michael@0: // Initially, just a return address. michael@0: "STACK CFI INIT 4000 100 .cfa: $rsp 8 + .ra: .cfa 8 - ^\n" michael@0: // Push %rbx. michael@0: "STACK CFI 4001 .cfa: $rsp 16 + $rbx: .cfa 16 - ^\n" michael@0: // Save %r12 in %rbx. Weird, but permitted. michael@0: "STACK CFI 4002 $r12: $rbx\n" michael@0: // Allocate frame space, and save %r13. michael@0: "STACK CFI 4003 .cfa: $rsp 40 + $r13: .cfa 32 - ^\n" michael@0: // Put the return address in %r13. michael@0: "STACK CFI 4005 .ra: $r13\n" michael@0: // Save %rbp, and use it as a frame pointer. michael@0: "STACK CFI 4006 .cfa: $rbp 16 + $rbp: .cfa 24 - ^\n" michael@0: michael@0: // The calling function. michael@0: "FUNC 5000 1000 10 epictetus\n" michael@0: // Mark it as end of stack. michael@0: "STACK CFI INIT 5000 1000 .cfa: $rsp .ra 0\n"); michael@0: michael@0: // Provide some distinctive values for the caller's registers. michael@0: expected.rsp = 0x8000000080000000ULL; michael@0: expected.rip = 0x40000000c0005510ULL; michael@0: expected.rbp = 0x68995b1de4700266ULL; michael@0: expected.rbx = 0x5a5beeb38de23be8ULL; michael@0: expected.r12 = 0xed1b02e8cc0fc79cULL; michael@0: expected.r13 = 0x1d20ad8acacbe930ULL; michael@0: expected.r14 = 0xe94cffc2f7adaa28ULL; michael@0: expected.r15 = 0xb638d17d8da413b5ULL; michael@0: michael@0: // By default, registers are unchanged. michael@0: raw_context = expected; michael@0: } michael@0: michael@0: // Walk the stack, using stack_section as the contents of the stack michael@0: // and raw_context as the current register values. (Set michael@0: // raw_context.rsp to the stack's starting address.) Expect two michael@0: // stack frames; in the older frame, expect the callee-saves michael@0: // registers to have values matching those in 'expected'. michael@0: void CheckWalk() { michael@0: RegionFromSection(); michael@0: raw_context.rsp = stack_section.start().Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, michael@0: &frame_symbolizer); michael@0: vector modules_without_symbols; michael@0: ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); michael@0: ASSERT_EQ(0U, modules_without_symbols.size()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_EQ(2U, frames->size()); michael@0: michael@0: StackFrameAMD64 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ("enchiridion", frame0->function_name); michael@0: EXPECT_EQ(0x40000000c0004000ULL, frame0->function_base); michael@0: michael@0: StackFrameAMD64 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); michael@0: ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | michael@0: StackFrameAMD64::CONTEXT_VALID_RSP | michael@0: StackFrameAMD64::CONTEXT_VALID_RBP | michael@0: StackFrameAMD64::CONTEXT_VALID_RBX | michael@0: StackFrameAMD64::CONTEXT_VALID_R12 | michael@0: StackFrameAMD64::CONTEXT_VALID_R13 | michael@0: StackFrameAMD64::CONTEXT_VALID_R14 | michael@0: StackFrameAMD64::CONTEXT_VALID_R15), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(expected.rip, frame1->context.rip); michael@0: EXPECT_EQ(expected.rsp, frame1->context.rsp); michael@0: EXPECT_EQ(expected.rbp, frame1->context.rbp); michael@0: EXPECT_EQ(expected.rbx, frame1->context.rbx); michael@0: EXPECT_EQ(expected.r12, frame1->context.r12); michael@0: EXPECT_EQ(expected.r13, frame1->context.r13); michael@0: EXPECT_EQ(expected.r14, frame1->context.r14); michael@0: EXPECT_EQ(expected.r15, frame1->context.r15); michael@0: EXPECT_EQ("epictetus", frame1->function_name); michael@0: } michael@0: michael@0: // The values we expect to find for the caller's registers. michael@0: MDRawContextAMD64 expected; michael@0: }; michael@0: michael@0: class CFI: public CFIFixture, public Test { }; michael@0: michael@0: TEST_F(CFI, At4000) { michael@0: Label frame1_rsp = expected.rsp; michael@0: stack_section michael@0: .D64(0x40000000c0005510ULL) // return address michael@0: .Mark(&frame1_rsp); // This effectively sets stack_section.start(). michael@0: raw_context.rip = 0x40000000c0004000ULL; michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4001) { michael@0: Label frame1_rsp = expected.rsp; michael@0: stack_section michael@0: .D64(0x5a5beeb38de23be8ULL) // saved %rbx michael@0: .D64(0x40000000c0005510ULL) // return address michael@0: .Mark(&frame1_rsp); // This effectively sets stack_section.start(). michael@0: raw_context.rip = 0x40000000c0004001ULL; michael@0: raw_context.rbx = 0xbe0487d2f9eafe29ULL; // callee's (distinct) %rbx value michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4002) { michael@0: Label frame1_rsp = expected.rsp; michael@0: stack_section michael@0: .D64(0x5a5beeb38de23be8ULL) // saved %rbx michael@0: .D64(0x40000000c0005510ULL) // return address michael@0: .Mark(&frame1_rsp); // This effectively sets stack_section.start(). michael@0: raw_context.rip = 0x40000000c0004002ULL; michael@0: raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 michael@0: raw_context.r12 = 0xb0118de918a4bceaULL; // callee's (distinct) %r12 value michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4003) { michael@0: Label frame1_rsp = expected.rsp; michael@0: stack_section michael@0: .D64(0x0e023828dffd4d81ULL) // garbage michael@0: .D64(0x1d20ad8acacbe930ULL) // saved %r13 michael@0: .D64(0x319e68b49e3ace0fULL) // garbage michael@0: .D64(0x5a5beeb38de23be8ULL) // saved %rbx michael@0: .D64(0x40000000c0005510ULL) // return address michael@0: .Mark(&frame1_rsp); // This effectively sets stack_section.start(). michael@0: raw_context.rip = 0x40000000c0004003ULL; michael@0: raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 michael@0: raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12 michael@0: raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13 michael@0: CheckWalk(); michael@0: } michael@0: michael@0: // The results here should be the same as those at module offset 0x4003. michael@0: TEST_F(CFI, At4004) { michael@0: Label frame1_rsp = expected.rsp; michael@0: stack_section michael@0: .D64(0x0e023828dffd4d81ULL) // garbage michael@0: .D64(0x1d20ad8acacbe930ULL) // saved %r13 michael@0: .D64(0x319e68b49e3ace0fULL) // garbage michael@0: .D64(0x5a5beeb38de23be8ULL) // saved %rbx michael@0: .D64(0x40000000c0005510ULL) // return address michael@0: .Mark(&frame1_rsp); // This effectively sets stack_section.start(). michael@0: raw_context.rip = 0x40000000c0004004ULL; michael@0: raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 michael@0: raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12 michael@0: raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13 michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4005) { michael@0: Label frame1_rsp = expected.rsp; michael@0: stack_section michael@0: .D64(0x4b516dd035745953ULL) // garbage michael@0: .D64(0x1d20ad8acacbe930ULL) // saved %r13 michael@0: .D64(0xa6d445e16ae3d872ULL) // garbage michael@0: .D64(0x5a5beeb38de23be8ULL) // saved %rbx michael@0: .D64(0xaa95fa054aedfbaeULL) // garbage michael@0: .Mark(&frame1_rsp); // This effectively sets stack_section.start(). michael@0: raw_context.rip = 0x40000000c0004005ULL; michael@0: raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 michael@0: raw_context.r12 = 0x46b1b8868891b34aULL; // callee's %r12 michael@0: raw_context.r13 = 0x40000000c0005510ULL; // return address michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4006) { michael@0: Label frame0_rbp; michael@0: Label frame1_rsp = expected.rsp; michael@0: stack_section michael@0: .D64(0x043c6dfceb91aa34ULL) // garbage michael@0: .D64(0x1d20ad8acacbe930ULL) // saved %r13 michael@0: .D64(0x68995b1de4700266ULL) // saved %rbp michael@0: .Mark(&frame0_rbp) // frame pointer points here michael@0: .D64(0x5a5beeb38de23be8ULL) // saved %rbx michael@0: .D64(0xf015ee516ad89eabULL) // garbage michael@0: .Mark(&frame1_rsp); // This effectively sets stack_section.start(). michael@0: raw_context.rip = 0x40000000c0004006ULL; michael@0: raw_context.rbp = frame0_rbp.Value(); michael@0: raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 michael@0: raw_context.r12 = 0x26e007b341acfebdULL; // callee's %r12 michael@0: raw_context.r13 = 0x40000000c0005510ULL; // return address michael@0: CheckWalk(); michael@0: }