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_x86_unittest.cc: Unit tests for StackwalkerX86 class. michael@0: 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_x86.h" michael@0: #include "processor/windows_frame_info.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::StackFrameX86; michael@0: using google_breakpad::StackwalkerX86; michael@0: using google_breakpad::SystemInfo; michael@0: using google_breakpad::WindowsFrameInfo; 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 StackwalkerX86Fixture { michael@0: public: michael@0: StackwalkerX86Fixture() 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(0x40000000, 0x10000, "module1", "version1"), michael@0: module2(0x50000000, 0x10000, "module2", "version2"), michael@0: module3(0x771d0000, 0x180000, "module3", "version3"), michael@0: module4(0x75f90000, 0x46000, "module4", "version4"), michael@0: module5(0x75730000, 0x110000, "module5", "version5"), michael@0: module6(0x647f0000, 0x1ba8000, "module6", "version6") { 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 = "Salacious Skink"; 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: modules.Add(&module3); michael@0: modules.Add(&module4); michael@0: modules.Add(&module5); michael@0: modules.Add(&module6); 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(MDRawContextX86 *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: MDRawContextX86 raw_context; michael@0: Section stack_section; michael@0: MockMemoryRegion stack_region; michael@0: MockCodeModule module1; michael@0: MockCodeModule module2; michael@0: MockCodeModule module3; michael@0: MockCodeModule module4; michael@0: MockCodeModule module5; michael@0: MockCodeModule module6; 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 SanityCheck: public StackwalkerX86Fixture, public Test { }; michael@0: michael@0: TEST_F(SanityCheck, NoResolver) { michael@0: stack_section.start() = 0x80000000; michael@0: stack_section.D32(0).D32(0); // end-of-stack marker michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x40000200; michael@0: raw_context.ebp = 0x80000000; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(NULL, NULL); michael@0: StackwalkerX86 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: StackFrameX86 *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 GetContextFrame: public StackwalkerX86Fixture, public Test { }; michael@0: michael@0: TEST_F(GetContextFrame, Simple) { michael@0: stack_section.start() = 0x80000000; michael@0: stack_section.D32(0).D32(0); // end-of-stack marker michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x40000200; michael@0: raw_context.ebp = 0x80000000; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: StackFrameX86 *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.eip = 0x40000200; michael@0: raw_context.ebp = 0x80000000; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: StackFrameX86 *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 StackwalkerX86Fixture, public Test { }; michael@0: michael@0: // Walk a traditional frame. A traditional frame saves the caller's michael@0: // %ebp just below the return address, and has its own %ebp pointing michael@0: // at the saved %ebp. michael@0: TEST_F(GetCallerFrame, Traditional) { michael@0: stack_section.start() = 0x80000000; michael@0: Label frame0_ebp, frame1_ebp; michael@0: stack_section michael@0: .Append(12, 0) // frame 0: space michael@0: .Mark(&frame0_ebp) // frame 0 %ebp points here michael@0: .D32(frame1_ebp) // frame 0: saved %ebp michael@0: .D32(0x40008679) // frame 0: return address michael@0: .Append(8, 0) // frame 1: space michael@0: .Mark(&frame1_ebp) // frame 1 %ebp points here michael@0: .D32(0) // frame 1: saved %ebp (stack end) michael@0: .D32(0); // frame 1: return address (stack end) michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x4000c7a5; michael@0: raw_context.esp = stack_section.start().Value(); michael@0: raw_context.ebp = frame0_ebp.Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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_EQ(2U, frames->size()); michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: EXPECT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x4000c7a5U, frame0->instruction); michael@0: EXPECT_EQ(0x4000c7a5U, frame0->context.eip); michael@0: EXPECT_EQ(frame0_ebp.Value(), frame0->context.ebp); michael@0: EXPECT_EQ(NULL, frame0->windows_frame_info); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_FP, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x40008679U, frame1->instruction + 1); michael@0: EXPECT_EQ(0x40008679U, frame1->context.eip); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_EQ(NULL, frame1->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // Walk a traditional frame, but use a bogus %ebp value, forcing a scan michael@0: // of the stack for something that looks like a return address. michael@0: TEST_F(GetCallerFrame, TraditionalScan) { michael@0: stack_section.start() = 0x80000000; michael@0: Label frame1_ebp; michael@0: stack_section michael@0: // frame 0 michael@0: .D32(0xf065dc76) // locals area: michael@0: .D32(0x46ee2167) // garbage that doesn't look like michael@0: .D32(0xbab023ec) // a return address michael@0: .D32(frame1_ebp) // saved %ebp (%ebp fails to point here, forcing scan) michael@0: .D32(0x4000129d) // return address michael@0: // frame 1 michael@0: .Append(8, 0) // space michael@0: .Mark(&frame1_ebp) // %ebp points here michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // return address (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x4000f49d; michael@0: raw_context.esp = stack_section.start().Value(); michael@0: // Make the frame pointer bogus, to make the stackwalker scan the stack michael@0: // for something that looks like a return address. michael@0: raw_context.ebp = 0xd43eed6e; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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_EQ(2U, frames->size()); michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x4000f49dU, frame0->instruction); michael@0: EXPECT_EQ(0x4000f49dU, frame0->context.eip); michael@0: EXPECT_EQ(stack_section.start().Value(), frame0->context.esp); michael@0: EXPECT_EQ(0xd43eed6eU, frame0->context.ebp); michael@0: EXPECT_EQ(NULL, frame0->windows_frame_info); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); michael@0: // I'd argue that CONTEXT_VALID_EBP shouldn't be here, since the michael@0: // walker does not actually fetch the EBP after a scan (forcing the michael@0: // next frame to be scanned as well). But let's grandfather the existing michael@0: // behavior in for now. michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x4000129dU, frame1->instruction + 1); michael@0: EXPECT_EQ(0x4000129dU, frame1->context.eip); michael@0: EXPECT_EQ(0x80000014U, frame1->context.esp); michael@0: EXPECT_EQ(0xd43eed6eU, frame1->context.ebp); michael@0: EXPECT_EQ(NULL, frame1->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // Force scanning for a return address a long way down the stack michael@0: TEST_F(GetCallerFrame, TraditionalScanLongWay) { michael@0: stack_section.start() = 0x80000000; michael@0: Label frame1_ebp; michael@0: stack_section michael@0: // frame 0 michael@0: .D32(0xf065dc76) // locals area: michael@0: .D32(0x46ee2167) // garbage that doesn't look like michael@0: .D32(0xbab023ec) // a return address michael@0: .Append(20 * 4, 0) // a bunch of space michael@0: .D32(frame1_ebp) // saved %ebp (%ebp fails to point here, forcing scan) michael@0: .D32(0x4000129d) // return address michael@0: // frame 1 michael@0: .Append(8, 0) // space michael@0: .Mark(&frame1_ebp) // %ebp points here michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // return address (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x4000f49d; michael@0: raw_context.esp = stack_section.start().Value(); michael@0: // Make the frame pointer bogus, to make the stackwalker scan the stack michael@0: // for something that looks like a return address. michael@0: raw_context.ebp = 0xd43eed6e; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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_EQ(2U, frames->size()); michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x4000f49dU, frame0->instruction); michael@0: EXPECT_EQ(0x4000f49dU, frame0->context.eip); michael@0: EXPECT_EQ(stack_section.start().Value(), frame0->context.esp); michael@0: EXPECT_EQ(0xd43eed6eU, frame0->context.ebp); michael@0: EXPECT_EQ(NULL, frame0->windows_frame_info); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); michael@0: // I'd argue that CONTEXT_VALID_EBP shouldn't be here, since the michael@0: // walker does not actually fetch the EBP after a scan (forcing the michael@0: // next frame to be scanned as well). But let's grandfather the existing michael@0: // behavior in for now. michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x4000129dU, frame1->instruction + 1); michael@0: EXPECT_EQ(0x4000129dU, frame1->context.eip); michael@0: EXPECT_EQ(0x80000064U, frame1->context.esp); michael@0: EXPECT_EQ(0xd43eed6eU, frame1->context.ebp); michael@0: EXPECT_EQ(NULL, frame1->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // Use Windows frame data (a "STACK WIN 4" record, from a michael@0: // FrameTypeFrameData DIA record) to walk a stack frame. michael@0: TEST_F(GetCallerFrame, WindowsFrameData) { michael@0: SetModuleSymbols(&module1, michael@0: "STACK WIN 4 aa85 176 0 0 4 10 4 0 1" michael@0: " $T2 $esp .cbSavedRegs + =" michael@0: " $T0 .raSearchStart =" michael@0: " $eip $T0 ^ =" michael@0: " $esp $T0 4 + =" michael@0: " $ebx $T2 4 - ^ =" michael@0: " $edi $T2 8 - ^ =" michael@0: " $esi $T2 12 - ^ =" michael@0: " $ebp $T2 16 - ^ =\n"); michael@0: Label frame1_esp, frame1_ebp; michael@0: stack_section.start() = 0x80000000; michael@0: stack_section michael@0: // frame 0 michael@0: .D32(frame1_ebp) // saved regs: %ebp michael@0: .D32(0xa7120d1a) // %esi michael@0: .D32(0x630891be) // %edi michael@0: .D32(0x9068a878) // %ebx michael@0: .D32(0xa08ea45f) // locals: unused michael@0: .D32(0x40001350) // return address michael@0: // frame 1 michael@0: .Mark(&frame1_esp) michael@0: .Append(12, 0) // empty space michael@0: .Mark(&frame1_ebp) michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // saved %eip (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x4000aa85; michael@0: raw_context.esp = stack_section.start().Value(); michael@0: raw_context.ebp = 0xf052c1de; // should not be needed to walk frame michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x4000aa85U, frame0->instruction); michael@0: EXPECT_EQ(0x4000aa85U, frame0->context.eip); michael@0: EXPECT_EQ(stack_section.start().Value(), frame0->context.esp); michael@0: EXPECT_EQ(0xf052c1deU, frame0->context.ebp); michael@0: EXPECT_TRUE(frame0->windows_frame_info != NULL); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP michael@0: | StackFrameX86::CONTEXT_VALID_EBX michael@0: | StackFrameX86::CONTEXT_VALID_ESI michael@0: | StackFrameX86::CONTEXT_VALID_EDI), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x40001350U, frame1->instruction + 1); michael@0: EXPECT_EQ(0x40001350U, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_EQ(0x9068a878U, frame1->context.ebx); michael@0: EXPECT_EQ(0xa7120d1aU, frame1->context.esi); michael@0: EXPECT_EQ(0x630891beU, frame1->context.edi); michael@0: EXPECT_EQ(NULL, frame1->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // Use Windows frame data (a "STACK WIN 4" record, from a michael@0: // FrameTypeFrameData DIA record) to walk a stack frame where the stack michael@0: // is aligned and we must search michael@0: TEST_F(GetCallerFrame, WindowsFrameDataAligned) { michael@0: SetModuleSymbols(&module1, michael@0: "STACK WIN 4 aa85 176 0 0 4 4 8 0 1" michael@0: " $T1 .raSearch =" michael@0: " $T0 $T1 4 - 8 @ =" michael@0: " $ebp $T1 4 - ^ =" michael@0: " $eip $T1 ^ =" michael@0: " $esp $T1 4 + ="); michael@0: Label frame1_esp, frame1_ebp; michael@0: stack_section.start() = 0x80000000; michael@0: stack_section michael@0: // frame 0 michael@0: .D32(0x0ffa0ffa) // unused saved register michael@0: .D32(0xdeaddead) // locals michael@0: .D32(0xbeefbeef) michael@0: .D32(0) // 8-byte alignment michael@0: .D32(frame1_ebp) michael@0: .D32(0x5000129d) // return address michael@0: // frame 1 michael@0: .Mark(&frame1_esp) michael@0: .D32(0x1) // parameter michael@0: .Mark(&frame1_ebp) michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // saved %eip (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x4000aa85; michael@0: raw_context.esp = stack_section.start().Value(); michael@0: raw_context.ebp = 0xf052c1de; // should not be needed to walk frame michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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("module2", modules_without_symbols[0]->debug_file()); michael@0: frames = call_stack.frames(); michael@0: ASSERT_EQ(2U, frames->size()); michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x4000aa85U, frame0->instruction); michael@0: EXPECT_EQ(0x4000aa85U, frame0->context.eip); michael@0: EXPECT_EQ(stack_section.start().Value(), frame0->context.esp); michael@0: EXPECT_EQ(0xf052c1deU, frame0->context.ebp); michael@0: EXPECT_TRUE(frame0->windows_frame_info != NULL); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x5000129dU, frame1->instruction + 1); michael@0: EXPECT_EQ(0x5000129dU, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_EQ(NULL, frame1->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // Use Windows frame data (a "STACK WIN 4" record, from a michael@0: // FrameTypeFrameData DIA record) to walk a frame, and depend on the michael@0: // parameter size from the callee as well. michael@0: TEST_F(GetCallerFrame, WindowsFrameDataParameterSize) { michael@0: SetModuleSymbols(&module1, "FUNC 1000 100 c module1::wheedle\n"); michael@0: SetModuleSymbols(&module2, michael@0: // Note bogus parameter size in FUNC record; the stack walker michael@0: // should prefer the STACK WIN record, and see '4' below. michael@0: "FUNC aa85 176 beef module2::whine\n" michael@0: "STACK WIN 4 aa85 176 0 0 4 10 4 0 1" michael@0: " $T2 $esp .cbLocals + .cbSavedRegs + =" michael@0: " $T0 .raSearchStart =" michael@0: " $eip $T0 ^ =" michael@0: " $esp $T0 4 + =" michael@0: " $ebp $T0 20 - ^ =" michael@0: " $ebx $T0 8 - ^ =\n"); michael@0: Label frame0_esp, frame0_ebp; michael@0: Label frame1_esp; michael@0: Label frame2_esp, frame2_ebp; michael@0: stack_section.start() = 0x80000000; michael@0: stack_section michael@0: // frame 0, in module1::wheedle. Traditional frame. michael@0: .Mark(&frame0_esp) michael@0: .Append(16, 0) // frame space michael@0: .Mark(&frame0_ebp) michael@0: .D32(0x6fa902e0) // saved %ebp. Not a frame pointer. michael@0: .D32(0x5000aa95) // return address, in module2::whine michael@0: // frame 1, in module2::whine. FrameData frame. michael@0: .Mark(&frame1_esp) michael@0: .D32(0xbaa0cb7a) // argument 3 passed to module1::wheedle michael@0: .D32(0xbdc92f9f) // argument 2 michael@0: .D32(0x0b1d8442) // argument 1 michael@0: .D32(frame2_ebp) // saved %ebp michael@0: .D32(0xb1b90a15) // unused michael@0: .D32(0xf18e072d) // unused michael@0: .D32(0x2558c7f3) // saved %ebx michael@0: .D32(0x0365e25e) // unused michael@0: .D32(0x2a179e38) // return address; $T0 points here michael@0: // frame 2, in no module michael@0: .Mark(&frame2_esp) michael@0: .Append(12, 0) // empty space michael@0: .Mark(&frame2_ebp) michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // saved %eip (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x40001004; // in module1::wheedle michael@0: raw_context.esp = stack_section.start().Value(); michael@0: raw_context.ebp = frame0_ebp.Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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(3U, frames->size()); michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x40001004U, frame0->instruction); michael@0: EXPECT_EQ(0x40001004U, frame0->context.eip); michael@0: EXPECT_EQ(frame0_esp.Value(), frame0->context.esp); michael@0: EXPECT_EQ(frame0_ebp.Value(), frame0->context.ebp); michael@0: EXPECT_EQ(&module1, frame0->module); michael@0: EXPECT_EQ("module1::wheedle", frame0->function_name); michael@0: EXPECT_EQ(0x40001000U, frame0->function_base); michael@0: // The FUNC record for module1::wheedle should have produced a michael@0: // WindowsFrameInfo structure with only the parameter size valid. michael@0: ASSERT_TRUE(frame0->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_PARAMETER_SIZE, michael@0: frame0->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_UNKNOWN, michael@0: frame0->windows_frame_info->type_); michael@0: EXPECT_EQ(12U, frame0->windows_frame_info->parameter_size); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_FP, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x5000aa95U, frame1->instruction + 1); michael@0: EXPECT_EQ(0x5000aa95U, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_EQ(0x6fa902e0U, frame1->context.ebp); michael@0: EXPECT_EQ(&module2, frame1->module); michael@0: EXPECT_EQ("module2::whine", frame1->function_name); michael@0: EXPECT_EQ(0x5000aa85U, frame1->function_base); michael@0: ASSERT_TRUE(frame1->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame1->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FRAME_DATA, michael@0: frame1->windows_frame_info->type_); michael@0: // This should not see the 0xbeef parameter size from the FUNC michael@0: // record, but should instead see the STACK WIN record. michael@0: EXPECT_EQ(4U, frame1->windows_frame_info->parameter_size); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame2 = static_cast(frames->at(2)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame2->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP michael@0: | StackFrameX86::CONTEXT_VALID_EBX), michael@0: frame2->context_validity); michael@0: EXPECT_EQ(0x2a179e38U, frame2->instruction + 1); michael@0: EXPECT_EQ(0x2a179e38U, frame2->context.eip); michael@0: EXPECT_EQ(frame2_esp.Value(), frame2->context.esp); michael@0: EXPECT_EQ(frame2_ebp.Value(), frame2->context.ebp); michael@0: EXPECT_EQ(0x2558c7f3U, frame2->context.ebx); michael@0: EXPECT_EQ(NULL, frame2->module); michael@0: EXPECT_EQ(NULL, frame2->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // Use Windows frame data (a "STACK WIN 4" record, from a michael@0: // FrameTypeFrameData DIA record) to walk a stack frame, where the michael@0: // expression fails to yield both an $eip and an $ebp value, and the stack michael@0: // walker must scan. michael@0: TEST_F(GetCallerFrame, WindowsFrameDataScan) { michael@0: SetModuleSymbols(&module1, michael@0: "STACK WIN 4 c8c 111 0 0 4 10 4 0 1 bad program string\n"); michael@0: // Mark frame 1's PC as the end of the stack. michael@0: SetModuleSymbols(&module2, michael@0: "FUNC 7c38 accf 0 module2::function\n" michael@0: "STACK WIN 4 7c38 accf 0 0 4 10 4 0 1 $eip 0 = $ebp 0 =\n"); michael@0: Label frame1_esp; michael@0: stack_section.start() = 0x80000000; michael@0: stack_section michael@0: // frame 0 michael@0: .Append(16, 0x2a) // unused, garbage michael@0: .D32(0x50007ce9) // return address michael@0: // frame 1 michael@0: .Mark(&frame1_esp) michael@0: .Append(8, 0); // empty space michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x40000c9c; michael@0: raw_context.esp = stack_section.start().Value(); michael@0: raw_context.ebp = 0x2ae314cd; // should not be needed to walk frame michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x40000c9cU, frame0->instruction); michael@0: EXPECT_EQ(0x40000c9cU, frame0->context.eip); michael@0: EXPECT_EQ(stack_section.start().Value(), frame0->context.esp); michael@0: EXPECT_EQ(0x2ae314cdU, frame0->context.ebp); michael@0: EXPECT_TRUE(frame0->windows_frame_info != NULL); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); michael@0: // I'd argue that CONTEXT_VALID_EBP shouldn't be here, since the walker michael@0: // does not actually fetch the EBP after a scan (forcing the next frame michael@0: // to be scanned as well). But let's grandfather the existing behavior in michael@0: // for now. michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x50007ce9U, frame1->instruction + 1); michael@0: EXPECT_EQ(0x50007ce9U, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_TRUE(frame1->windows_frame_info != NULL); michael@0: } michael@0: } michael@0: michael@0: // Use Windows frame data (a "STACK WIN 4" record, from a michael@0: // FrameTypeFrameData DIA record) to walk a stack frame, where the michael@0: // expression yields an $eip that falls outside of any module, and the michael@0: // stack walker must scan. michael@0: TEST_F(GetCallerFrame, WindowsFrameDataBadEIPScan) { michael@0: SetModuleSymbols(&module1, michael@0: "STACK WIN 4 6e6 e7 0 0 0 8 4 0 1" michael@0: // A traditional frame, actually. michael@0: " $eip $ebp 4 + ^ = $esp $ebp 8 + = $ebp $ebp ^ =\n"); michael@0: // Mark frame 1's PC as the end of the stack. michael@0: SetModuleSymbols(&module2, michael@0: "FUNC cfdb 8406 0 module2::function\n" michael@0: "STACK WIN 4 cfdb 8406 0 0 0 0 0 0 1 $eip 0 = $ebp 0 =\n"); michael@0: stack_section.start() = 0x80000000; michael@0: michael@0: // In this stack, the context's %ebp is pointing at the wrong place, so michael@0: // the stack walker needs to scan to find the return address, and then michael@0: // scan again to find the caller's saved %ebp. michael@0: Label frame0_ebp, frame1_ebp, frame1_esp; michael@0: stack_section michael@0: // frame 0 michael@0: .Append(8, 0x2a) // garbage michael@0: .Mark(&frame0_ebp) // frame 0 %ebp points here, but should point michael@0: // at *** below michael@0: // The STACK WIN record says that the following two values are michael@0: // frame 1's saved %ebp and return address, but the %ebp is wrong; michael@0: // they're garbage. The stack walker will scan for the right values. michael@0: .D32(0x3d937b2b) // alleged to be frame 1's saved %ebp michael@0: .D32(0x17847f5b) // alleged to be frame 1's return address michael@0: .D32(frame1_ebp) // frame 1's real saved %ebp; scan will find michael@0: .D32(0x2b2b2b2b) // first word of realigned register save area michael@0: // *** frame 0 %ebp ought to be pointing here michael@0: .D32(0x2c2c2c2c) // realigned locals area michael@0: .D32(0x5000d000) // frame 1's real saved %eip; scan will find michael@0: // Frame 1, in module2::function. The STACK WIN record describes michael@0: // this as the oldest frame, without referring to its contents, so michael@0: // we needn't to provide any actual data here. michael@0: .Mark(&frame1_esp) michael@0: .Mark(&frame1_ebp) // frame 1 %ebp points here michael@0: // A dummy value for frame 1's %ebp to point at. The scan recognizes the michael@0: // saved %ebp because it points to a valid word in the stack memory region. michael@0: .D32(0x2d2d2d2d); michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x40000700; michael@0: raw_context.esp = stack_section.start().Value(); michael@0: raw_context.ebp = frame0_ebp.Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x40000700U, frame0->instruction); michael@0: EXPECT_EQ(0x40000700U, frame0->context.eip); michael@0: EXPECT_EQ(stack_section.start().Value(), frame0->context.esp); michael@0: EXPECT_EQ(frame0_ebp.Value(), frame0->context.ebp); michael@0: EXPECT_TRUE(frame0->windows_frame_info != NULL); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI_SCAN, frame1->trust); michael@0: // I'd argue that CONTEXT_VALID_EBP shouldn't be here, since the michael@0: // walker does not actually fetch the EBP after a scan (forcing the michael@0: // next frame to be scanned as well). But let's grandfather the existing michael@0: // behavior in for now. michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x5000d000U, frame1->instruction + 1); michael@0: EXPECT_EQ(0x5000d000U, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_TRUE(frame1->windows_frame_info != NULL); michael@0: } michael@0: } michael@0: michael@0: // Use Windows FrameTypeFPO data to walk a stack frame for a function that michael@0: // does not modify %ebp from the value it had in the caller. michael@0: TEST_F(GetCallerFrame, WindowsFPOUnchangedEBP) { michael@0: SetModuleSymbols(&module1, michael@0: // Note bogus parameter size in FUNC record; the walker michael@0: // should prefer the STACK WIN record, and see the '8' below. michael@0: "FUNC e8a8 100 feeb module1::discombobulated\n" michael@0: "STACK WIN 0 e8a8 100 0 0 8 4 10 0 0 0\n"); michael@0: Label frame0_esp; michael@0: Label frame1_esp, frame1_ebp; michael@0: stack_section.start() = 0x80000000; michael@0: stack_section michael@0: // frame 0, in module1::wheedle. FrameTypeFPO (STACK WIN 0) frame. michael@0: .Mark(&frame0_esp) michael@0: // no outgoing parameters; this is the youngest frame. michael@0: .D32(0x7c521352) // four bytes of saved registers michael@0: .Append(0x10, 0x42) // local area michael@0: .D32(0x40009b5b) // return address, in module1, no function michael@0: // frame 1, in module1, no function. michael@0: .Mark(&frame1_esp) michael@0: .D32(0xf60ea7fc) // junk michael@0: .Mark(&frame1_ebp) michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // saved %eip (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x4000e8b8; // in module1::whine michael@0: raw_context.esp = stack_section.start().Value(); michael@0: // Frame pointer unchanged from caller. michael@0: raw_context.ebp = frame1_ebp.Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x4000e8b8U, frame0->instruction); michael@0: EXPECT_EQ(0x4000e8b8U, frame0->context.eip); michael@0: EXPECT_EQ(frame0_esp.Value(), frame0->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame0->context.ebp); // unchanged from caller michael@0: EXPECT_EQ(&module1, frame0->module); michael@0: EXPECT_EQ("module1::discombobulated", frame0->function_name); michael@0: EXPECT_EQ(0x4000e8a8U, frame0->function_base); michael@0: // The STACK WIN record for module1::discombobulated should have michael@0: // produced a fully populated WindowsFrameInfo structure. michael@0: ASSERT_TRUE(frame0->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame0->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FPO, michael@0: frame0->windows_frame_info->type_); michael@0: EXPECT_EQ(0x10U, frame0->windows_frame_info->local_size); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x40009b5bU, frame1->instruction + 1); michael@0: EXPECT_EQ(0x40009b5bU, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_EQ(&module1, frame1->module); michael@0: EXPECT_EQ("", frame1->function_name); michael@0: EXPECT_EQ(NULL, frame1->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // Use Windows FrameTypeFPO data to walk a stack frame for a function michael@0: // that uses %ebp for its own purposes, saving the value it had in the michael@0: // caller in the standard place in the saved register area. michael@0: TEST_F(GetCallerFrame, WindowsFPOUsedEBP) { michael@0: SetModuleSymbols(&module1, michael@0: // Note bogus parameter size in FUNC record; the walker michael@0: // should prefer the STACK WIN record, and see the '8' below. michael@0: "FUNC 9aa8 e6 abbe module1::RaisedByTheAliens\n" michael@0: "STACK WIN 0 9aa8 e6 a 0 10 8 4 0 0 1\n"); michael@0: Label frame0_esp; michael@0: Label frame1_esp, frame1_ebp; michael@0: stack_section.start() = 0x80000000; michael@0: stack_section michael@0: // frame 0, in module1::wheedle. FrameTypeFPO (STACK WIN 0) frame. michael@0: .Mark(&frame0_esp) michael@0: // no outgoing parameters; this is the youngest frame. michael@0: .D32(frame1_ebp) // saved register area: saved %ebp michael@0: .D32(0xb68bd5f9) // saved register area: something else michael@0: .D32(0xd25d05fc) // local area michael@0: .D32(0x4000debe) // return address, in module1, no function michael@0: // frame 1, in module1, no function. michael@0: .Mark(&frame1_esp) michael@0: .D32(0xf0c9a974) // junk michael@0: .Mark(&frame1_ebp) michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // saved %eip (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x40009ab8; // in module1::RaisedByTheAliens michael@0: raw_context.esp = stack_section.start().Value(); michael@0: // RaisedByTheAliens uses %ebp for its own mysterious purposes. michael@0: raw_context.ebp = 0xecbdd1a5; michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x40009ab8U, frame0->instruction); michael@0: EXPECT_EQ(0x40009ab8U, frame0->context.eip); michael@0: EXPECT_EQ(frame0_esp.Value(), frame0->context.esp); michael@0: EXPECT_EQ(0xecbdd1a5, frame0->context.ebp); michael@0: EXPECT_EQ(&module1, frame0->module); michael@0: EXPECT_EQ("module1::RaisedByTheAliens", frame0->function_name); michael@0: EXPECT_EQ(0x40009aa8U, frame0->function_base); michael@0: // The STACK WIN record for module1::RaisedByTheAliens should have michael@0: // produced a fully populated WindowsFrameInfo structure. michael@0: ASSERT_TRUE(frame0->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame0->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FPO, michael@0: frame0->windows_frame_info->type_); michael@0: EXPECT_EQ("", frame0->windows_frame_info->program_string); michael@0: EXPECT_TRUE(frame0->windows_frame_info->allocates_base_pointer); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x4000debeU, frame1->instruction + 1); michael@0: EXPECT_EQ(0x4000debeU, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_EQ(&module1, frame1->module); michael@0: EXPECT_EQ("", frame1->function_name); michael@0: EXPECT_EQ(NULL, frame1->windows_frame_info); michael@0: } michael@0: } michael@0: michael@0: // This is a regression unit test which covers a bug which has to do with michael@0: // FPO-optimized Windows system call stubs in the context frame. There is michael@0: // a more recent Windows system call dispatch mechanism which differs from michael@0: // the one which is being tested here. The newer system call dispatch michael@0: // mechanism creates an extra context frame (KiFastSystemCallRet). michael@0: TEST_F(GetCallerFrame, WindowsFPOSystemCall) { michael@0: SetModuleSymbols(&module3, // ntdll.dll michael@0: "PUBLIC 1f8ac c ZwWaitForSingleObject\n" michael@0: "STACK WIN 0 1f8ac 1b 0 0 c 0 0 0 0 0\n"); michael@0: SetModuleSymbols(&module4, // kernelbase.dll michael@0: "PUBLIC 109f9 c WaitForSingleObjectEx\n" michael@0: "PUBLIC 36590 0 _except_handler4\n" michael@0: "STACK WIN 4 109f9 df c 0 c c 48 0 1 $T0 $ebp = $eip " michael@0: "$T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L " michael@0: "$T0 .cbSavedRegs - = $P $T0 8 + .cbParams + =\n" michael@0: "STACK WIN 4 36590 154 17 0 10 0 14 0 1 $T0 $ebp = $eip " michael@0: "$T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L $T0 " michael@0: ".cbSavedRegs - = $P $T0 8 + .cbParams + =\n"); michael@0: SetModuleSymbols(&module5, // kernel32.dll michael@0: "PUBLIC 11136 8 WaitForSingleObject\n" michael@0: "PUBLIC 11151 c WaitForSingleObjectExImplementation\n" michael@0: "STACK WIN 4 11136 16 5 0 8 0 0 0 1 $T0 $ebp = $eip " michael@0: "$T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L " michael@0: "$T0 .cbSavedRegs - = $P $T0 8 + .cbParams + =\n" michael@0: "STACK WIN 4 11151 7a 5 0 c 0 0 0 1 $T0 $ebp = $eip " michael@0: "$T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L " michael@0: "$T0 .cbSavedRegs - = $P $T0 8 + .cbParams + =\n"); michael@0: SetModuleSymbols(&module6, // chrome.dll michael@0: "FILE 7038 some_file_name.h\n" michael@0: "FILE 839776 some_file_name.cc\n" michael@0: "FUNC 217fda 17 4 function_217fda\n" michael@0: "217fda 4 102 839776\n" michael@0: "FUNC 217ff1 a 4 function_217ff1\n" michael@0: "217ff1 0 594 7038\n" michael@0: "217ff1 a 596 7038\n" michael@0: "STACK WIN 0 217ff1 a 0 0 4 0 0 0 0 0\n"); michael@0: michael@0: Label frame0_esp, frame1_esp; michael@0: Label frame1_ebp, frame2_ebp, frame3_ebp; michael@0: stack_section.start() = 0x002ff290; michael@0: stack_section michael@0: .Mark(&frame0_esp) michael@0: .D32(0x771ef8c1) // EIP in frame 0 (system call) michael@0: .D32(0x75fa0a91) // return address of frame 0 michael@0: .Mark(&frame1_esp) michael@0: .D32(0x000017b0) // args to child michael@0: .D32(0x00000000) michael@0: .D32(0x002ff2d8) michael@0: .D32(0x88014a2e) michael@0: .D32(0x002ff364) michael@0: .D32(0x000017b0) michael@0: .D32(0x00000000) michael@0: .D32(0x00000024) michael@0: .D32(0x00000001) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x9e3b9800) michael@0: .D32(0xfffffff7) michael@0: .D32(0x00000000) michael@0: .D32(0x002ff2a4) michael@0: .D32(0x64a07ff1) // random value to be confused with a return address michael@0: .D32(0x002ff8dc) michael@0: .D32(0x75fc6590) // random value to be confused with a return address michael@0: .D32(0xfdd2c6ea) michael@0: .D32(0x00000000) michael@0: .Mark(&frame1_ebp) michael@0: .D32(frame2_ebp) // Child EBP michael@0: .D32(0x75741194) // return address of frame 1 michael@0: .D32(0x000017b0) // args to child michael@0: .D32(0x0036ee80) michael@0: .D32(0x00000000) michael@0: .D32(0x65bc7d14) michael@0: .Mark(&frame2_ebp) michael@0: .D32(frame3_ebp) // Child EBP michael@0: .D32(0x75741148) // return address of frame 2 michael@0: .D32(0x000017b0) // args to child michael@0: .D32(0x0036ee80) michael@0: .D32(0x00000000) michael@0: .Mark(&frame3_ebp) michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // saved %eip (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x771ef8c1; // in ntdll::ZwWaitForSingleObject michael@0: raw_context.esp = stack_section.start().Value(); michael@0: ASSERT_TRUE(raw_context.esp == frame0_esp.Value()); michael@0: raw_context.ebp = frame1_ebp.Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: michael@0: ASSERT_EQ(4U, frames->size()); michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x771ef8c1U, frame0->instruction); michael@0: EXPECT_EQ(0x771ef8c1U, frame0->context.eip); michael@0: EXPECT_EQ(frame0_esp.Value(), frame0->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame0->context.ebp); michael@0: EXPECT_EQ(&module3, frame0->module); michael@0: EXPECT_EQ("ZwWaitForSingleObject", frame0->function_name); michael@0: // The STACK WIN record for module3!ZwWaitForSingleObject should have michael@0: // produced a fully populated WindowsFrameInfo structure. michael@0: ASSERT_TRUE(frame0->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame0->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FPO, michael@0: frame0->windows_frame_info->type_); michael@0: EXPECT_EQ("", frame0->windows_frame_info->program_string); michael@0: EXPECT_FALSE(frame0->windows_frame_info->allocates_base_pointer); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP michael@0: | StackFrameX86::CONTEXT_VALID_ESP michael@0: | StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x75fa0a91U, frame1->instruction + 1); michael@0: EXPECT_EQ(0x75fa0a91U, frame1->context.eip); michael@0: EXPECT_EQ(frame1_esp.Value(), frame1->context.esp); michael@0: EXPECT_EQ(frame1_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_EQ(&module4, frame1->module); michael@0: EXPECT_EQ("WaitForSingleObjectEx", frame1->function_name); michael@0: // The STACK WIN record for module4!WaitForSingleObjectEx should have michael@0: // produced a fully populated WindowsFrameInfo structure. michael@0: ASSERT_TRUE(frame1->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame1->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FRAME_DATA, michael@0: frame1->windows_frame_info->type_); michael@0: EXPECT_EQ("$T0 $ebp = $eip $T0 4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L " michael@0: "$T0 .cbSavedRegs - = $P $T0 8 + .cbParams + =", michael@0: frame1->windows_frame_info->program_string); michael@0: EXPECT_FALSE(frame1->windows_frame_info->allocates_base_pointer); michael@0: } michael@0: } michael@0: michael@0: // Scan the stack for a better return address and potentially skip frames michael@0: // when the calculated return address is not in a known module. michael@0: // Note, that the span of this scan is somewhat arbitrarily limited to 30 michael@0: // search words (pointers): michael@0: // const int kRASearchWords = 30; michael@0: // This means that frames can be skipped only when their size is relatively michael@0: // small: smaller than kRASearchWords * sizeof(InstructionType) michael@0: TEST_F(GetCallerFrame, ReturnAddressIsNotInKnownModule) { michael@0: MockCodeModule msvcrt_dll(0x77be0000, 0x58000, "msvcrt.dll", "version1"); michael@0: SetModuleSymbols(&msvcrt_dll, // msvcrt.dll michael@0: "PUBLIC 38180 0 wcsstr\n" michael@0: "STACK WIN 4 38180 61 10 0 8 0 0 0 1 $T0 $ebp = $eip $T0 " michael@0: "4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L $T0 .cbSavedRegs " michael@0: "- = $P $T0 4 + .cbParams + =\n"); michael@0: michael@0: MockCodeModule kernel32_dll(0x7c800000, 0x103000, "kernel32.dll", "version1"); michael@0: SetModuleSymbols(&kernel32_dll, // kernel32.dll michael@0: "PUBLIC efda 8 FindNextFileW\n" michael@0: "STACK WIN 4 efda 1bb c 0 8 8 3c 0 1 $T0 $ebp = $eip $T0 " michael@0: "4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L $T0 .cbSavedRegs " michael@0: "- = $P $T0 4 + .cbParams + =\n"); michael@0: michael@0: MockCodeModule chrome_dll(0x1c30000, 0x28C8000, "chrome.dll", "version1"); michael@0: SetModuleSymbols(&chrome_dll, // chrome.dll michael@0: "FUNC e3cff 4af 0 file_util::FileEnumerator::Next()\n" michael@0: "e3cff 1a 711 2505\n" michael@0: "STACK WIN 4 e3cff 4af 20 0 4 c 94 0 1 $T1 .raSearch = " michael@0: "$T0 $T1 4 - 8 @ = $ebp $T1 4 - ^ = $eip $T1 ^ = $esp " michael@0: "$T1 4 + = $20 $T0 152 - ^ = $23 $T0 156 - ^ = $24 " michael@0: "$T0 160 - ^ =\n"); michael@0: michael@0: // Create some modules with some stock debugging information. michael@0: MockCodeModules local_modules; michael@0: local_modules.Add(&msvcrt_dll); michael@0: local_modules.Add(&kernel32_dll); michael@0: local_modules.Add(&chrome_dll); michael@0: michael@0: Label frame0_esp; michael@0: Label frame0_ebp; michael@0: Label frame1_ebp; michael@0: Label frame2_ebp; michael@0: Label frame3_ebp; michael@0: michael@0: stack_section.start() = 0x0932f2d0; michael@0: stack_section michael@0: .Mark(&frame0_esp) michael@0: .D32(0x0764e000) michael@0: .D32(0x0764e068) michael@0: .Mark(&frame0_ebp) michael@0: .D32(frame1_ebp) // Child EBP michael@0: .D32(0x001767a0) // return address of frame 0 michael@0: // Not in known module michael@0: .D32(0x0764e0c6) michael@0: .D32(0x001bb1b8) michael@0: .D32(0x0764e068) michael@0: .D32(0x00000003) michael@0: .D32(0x0764e068) michael@0: .D32(0x00000003) michael@0: .D32(0x07578828) michael@0: .D32(0x0764e000) michael@0: .D32(0x00000000) michael@0: .D32(0x001c0010) michael@0: .D32(0x0764e0c6) michael@0: .Mark(&frame1_ebp) michael@0: .D32(frame2_ebp) // Child EBP michael@0: .D32(0x7c80f10f) // return address of frame 1 michael@0: // inside kernel32!FindNextFileW michael@0: .D32(0x000008f8) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x0932f34c) michael@0: .D32(0x0764e000) michael@0: .D32(0x00001000) michael@0: .D32(0x00000000) michael@0: .D32(0x00000001) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x0932f6a8) michael@0: .D32(0x00000000) michael@0: .D32(0x0932f6d8) michael@0: .D32(0x00000000) michael@0: .D32(0x000000d6) michael@0: .D32(0x0764e000) michael@0: .D32(0x7ff9a000) michael@0: .D32(0x0932f3fc) michael@0: .D32(0x00000001) michael@0: .D32(0x00000001) michael@0: .D32(0x07578828) michael@0: .D32(0x0000002e) michael@0: .D32(0x0932f340) michael@0: .D32(0x0932eef4) michael@0: .D32(0x0932ffdc) michael@0: .D32(0x7c839ad8) michael@0: .D32(0x7c80f0d8) michael@0: .D32(0x00000000) michael@0: .Mark(&frame2_ebp) michael@0: .D32(frame3_ebp) // Child EBP michael@0: .D32(0x01d13f91) // return address of frame 2 michael@0: // inside chrome_dll!file_util::FileEnumerator::Next michael@0: .D32(0x07578828) michael@0: .D32(0x0932f6ac) michael@0: .D32(0x0932f9c4) michael@0: .D32(0x0932f9b4) michael@0: .D32(0x00000000) michael@0: .D32(0x00000003) michael@0: .D32(0x0932f978) michael@0: .D32(0x01094330) michael@0: .D32(0x00000000) michael@0: .D32(0x00000001) michael@0: .D32(0x01094330) michael@0: .D32(0x00000000) michael@0: .D32(0x00000000) michael@0: .D32(0x07f30000) michael@0: .D32(0x01c3ba17) michael@0: .D32(0x08bab840) michael@0: .D32(0x07f31580) michael@0: .D32(0x00000000) michael@0: .D32(0x00000007) michael@0: .D32(0x0932f940) michael@0: .D32(0x0000002e) michael@0: .D32(0x0932f40c) michael@0: .D32(0x01d13b53) michael@0: .D32(0x0932f958) michael@0: .D32(0x00000001) michael@0: .D32(0x00000007) michael@0: .D32(0x0932f940) michael@0: .D32(0x0000002e) michael@0: .D32(0x00000000) michael@0: .D32(0x0932f6ac) michael@0: .D32(0x01e13ef0) michael@0: .D32(0x00000001) michael@0: .D32(0x00000007) michael@0: .D32(0x0932f958) michael@0: .D32(0x08bab840) michael@0: .D32(0x0932f9b4) michael@0: .D32(0x00000000) michael@0: .D32(0x0932f9b4) michael@0: .D32(0x000000a7) michael@0: .D32(0x000000a7) michael@0: .D32(0x0932f998) michael@0: .D32(0x579627a2) michael@0: .Mark(&frame3_ebp) michael@0: .D32(0) // saved %ebp (stack end) michael@0: .D32(0); // saved %eip (stack end) michael@0: michael@0: RegionFromSection(); michael@0: raw_context.eip = 0x77c181cd; // inside msvcrt!wcsstr michael@0: raw_context.esp = frame0_esp.Value(); michael@0: raw_context.ebp = frame0_ebp.Value(); michael@0: // sanity michael@0: ASSERT_TRUE(raw_context.esp == stack_section.start().Value()); michael@0: ASSERT_TRUE(raw_context.ebp == stack_section.start().Value() + 8); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 walker(&system_info, &raw_context, &stack_region, michael@0: &local_modules, &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: michael@0: ASSERT_EQ(3U, frames->size()); michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ(0x77c181cdU, frame0->instruction); michael@0: EXPECT_EQ(0x77c181cdU, frame0->context.eip); michael@0: EXPECT_EQ(frame0_esp.Value(), frame0->context.esp); michael@0: EXPECT_EQ(frame0_ebp.Value(), frame0->context.ebp); michael@0: EXPECT_EQ(&msvcrt_dll, frame0->module); michael@0: EXPECT_EQ("wcsstr", frame0->function_name); michael@0: ASSERT_TRUE(frame0->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame0->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FRAME_DATA, michael@0: frame0->windows_frame_info->type_); michael@0: EXPECT_EQ("$T0 $ebp = $eip $T0 " michael@0: "4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L $T0 .cbSavedRegs " michael@0: "- = $P $T0 4 + .cbParams + =", michael@0: frame0->windows_frame_info->program_string); michael@0: // It has program string, so allocates_base_pointer is not expected michael@0: EXPECT_FALSE(frame0->windows_frame_info->allocates_base_pointer); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI_SCAN, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP | michael@0: StackFrameX86::CONTEXT_VALID_ESP | michael@0: StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(0x7c80f10fU, frame1->instruction + 1); michael@0: EXPECT_EQ(0x7c80f10fU, frame1->context.eip); michael@0: // frame 1 was skipped, so intead of frame1_ebp compare with frame2_ebp. michael@0: EXPECT_EQ(frame2_ebp.Value(), frame1->context.ebp); michael@0: EXPECT_EQ(&kernel32_dll, frame1->module); michael@0: EXPECT_EQ("FindNextFileW", frame1->function_name); michael@0: ASSERT_TRUE(frame1->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame1->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FRAME_DATA, michael@0: frame1->windows_frame_info->type_); michael@0: EXPECT_EQ("$T0 $ebp = $eip $T0 " michael@0: "4 + ^ = $ebp $T0 ^ = $esp $T0 8 + = $L $T0 .cbSavedRegs " michael@0: "- = $P $T0 4 + .cbParams + =", michael@0: frame1->windows_frame_info->program_string); michael@0: EXPECT_FALSE(frame1->windows_frame_info->allocates_base_pointer); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame2 = static_cast(frames->at(2)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame2->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP | michael@0: StackFrameX86::CONTEXT_VALID_ESP | michael@0: StackFrameX86::CONTEXT_VALID_EBP), michael@0: frame2->context_validity); michael@0: EXPECT_EQ(0x01d13f91U, frame2->instruction + 1); michael@0: EXPECT_EQ(0x01d13f91U, frame2->context.eip); michael@0: // frame 1 was skipped, so intead of frame2_ebp compare with frame3_ebp. michael@0: EXPECT_EQ(frame3_ebp.Value(), frame2->context.ebp); michael@0: EXPECT_EQ(&chrome_dll, frame2->module); michael@0: EXPECT_EQ("file_util::FileEnumerator::Next()", frame2->function_name); michael@0: ASSERT_TRUE(frame2->windows_frame_info != NULL); michael@0: EXPECT_EQ(WindowsFrameInfo::VALID_ALL, frame2->windows_frame_info->valid); michael@0: EXPECT_EQ(WindowsFrameInfo::STACK_INFO_FRAME_DATA, michael@0: frame2->windows_frame_info->type_); michael@0: EXPECT_EQ("$T1 .raSearch = " michael@0: "$T0 $T1 4 - 8 @ = $ebp $T1 4 - ^ = $eip $T1 ^ = $esp " michael@0: "$T1 4 + = $20 $T0 152 - ^ = $23 $T0 156 - ^ = $24 " michael@0: "$T0 160 - ^ =", michael@0: frame2->windows_frame_info->program_string); michael@0: EXPECT_FALSE(frame2->windows_frame_info->allocates_base_pointer); michael@0: } michael@0: } michael@0: michael@0: struct CFIFixture: public StackwalkerX86Fixture { michael@0: CFIFixture() { michael@0: // Provide a bunch of STACK CFI records; individual tests walk to the michael@0: // caller from every point in this series, expecting to find the same michael@0: // set 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: $esp 4 + .ra: .cfa 4 - ^\n" michael@0: // Push %ebx. michael@0: "STACK CFI 4001 .cfa: $esp 8 + $ebx: .cfa 8 - ^\n" michael@0: // Move %esi into %ebx. Weird, but permitted. michael@0: "STACK CFI 4002 $esi: $ebx\n" michael@0: // Allocate frame space, and save %edi. michael@0: "STACK CFI 4003 .cfa: $esp 20 + $edi: .cfa 16 - ^\n" michael@0: // Put the return address in %edi. michael@0: "STACK CFI 4005 .ra: $edi\n" michael@0: // Save %ebp, and use it as a frame pointer. michael@0: "STACK CFI 4006 .cfa: $ebp 8 + $ebp: .cfa 12 - ^\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: $esp .ra 0\n"); michael@0: michael@0: // Provide some distinctive values for the caller's registers. michael@0: expected.esp = 0x80000000; michael@0: expected.eip = 0x40005510; michael@0: expected.ebp = 0xc0d4aab9; michael@0: expected.ebx = 0x60f20ce6; michael@0: expected.esi = 0x53d1379d; michael@0: expected.edi = 0xafbae234; 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.esp 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.esp = stack_section.start().Value(); michael@0: michael@0: StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); michael@0: StackwalkerX86 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: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame0 = static_cast(frames->at(0)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); michael@0: ASSERT_EQ(StackFrameX86::CONTEXT_VALID_ALL, frame0->context_validity); michael@0: EXPECT_EQ("enchiridion", frame0->function_name); michael@0: EXPECT_EQ(0x40004000U, frame0->function_base); michael@0: ASSERT_TRUE(frame0->windows_frame_info != NULL); michael@0: ASSERT_EQ(WindowsFrameInfo::VALID_PARAMETER_SIZE, michael@0: frame0->windows_frame_info->valid); michael@0: ASSERT_TRUE(frame0->cfi_frame_info != NULL); michael@0: } michael@0: michael@0: { // To avoid reusing locals by mistake michael@0: StackFrameX86 *frame1 = static_cast(frames->at(1)); michael@0: EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); michael@0: ASSERT_EQ((StackFrameX86::CONTEXT_VALID_EIP | michael@0: StackFrameX86::CONTEXT_VALID_ESP | michael@0: StackFrameX86::CONTEXT_VALID_EBP | michael@0: StackFrameX86::CONTEXT_VALID_EBX | michael@0: StackFrameX86::CONTEXT_VALID_ESI | michael@0: StackFrameX86::CONTEXT_VALID_EDI), michael@0: frame1->context_validity); michael@0: EXPECT_EQ(expected.eip, frame1->context.eip); michael@0: EXPECT_EQ(expected.esp, frame1->context.esp); michael@0: EXPECT_EQ(expected.ebp, frame1->context.ebp); michael@0: EXPECT_EQ(expected.ebx, frame1->context.ebx); michael@0: EXPECT_EQ(expected.esi, frame1->context.esi); michael@0: EXPECT_EQ(expected.edi, frame1->context.edi); michael@0: EXPECT_EQ("epictetus", frame1->function_name); michael@0: } michael@0: } michael@0: michael@0: // The values the stack walker should find for the caller's registers. michael@0: MDRawContextX86 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_esp = expected.esp; michael@0: stack_section michael@0: .D32(0x40005510) // return address michael@0: .Mark(&frame1_esp); // This effectively sets stack_section.start(). michael@0: raw_context.eip = 0x40004000; michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4001) { michael@0: Label frame1_esp = expected.esp; michael@0: stack_section michael@0: .D32(0x60f20ce6) // saved %ebx michael@0: .D32(0x40005510) // return address michael@0: .Mark(&frame1_esp); // This effectively sets stack_section.start(). michael@0: raw_context.eip = 0x40004001; michael@0: raw_context.ebx = 0x91aa9a8b; // callee's %ebx value michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4002) { michael@0: Label frame1_esp = expected.esp; michael@0: stack_section michael@0: .D32(0x60f20ce6) // saved %ebx michael@0: .D32(0x40005510) // return address michael@0: .Mark(&frame1_esp); // This effectively sets stack_section.start(). michael@0: raw_context.eip = 0x40004002; michael@0: raw_context.ebx = 0x53d1379d; // saved %esi michael@0: raw_context.esi = 0xa5c790ed; // callee's %esi value michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4003) { michael@0: Label frame1_esp = expected.esp; michael@0: stack_section michael@0: .D32(0x56ec3db7) // garbage michael@0: .D32(0xafbae234) // saved %edi michael@0: .D32(0x53d67131) // garbage michael@0: .D32(0x60f20ce6) // saved %ebx michael@0: .D32(0x40005510) // return address michael@0: .Mark(&frame1_esp); // This effectively sets stack_section.start(). michael@0: raw_context.eip = 0x40004003; michael@0: raw_context.ebx = 0x53d1379d; // saved %esi michael@0: raw_context.esi = 0xa97f229d; // callee's %esi michael@0: raw_context.edi = 0xb05cc997; // callee's %edi michael@0: CheckWalk(); michael@0: } michael@0: michael@0: // The results here should be the same as those at module offset michael@0: // 0x4003. michael@0: TEST_F(CFI, At4004) { michael@0: Label frame1_esp = expected.esp; michael@0: stack_section michael@0: .D32(0xe29782c2) // garbage michael@0: .D32(0xafbae234) // saved %edi michael@0: .D32(0x5ba29ce9) // garbage michael@0: .D32(0x60f20ce6) // saved %ebx michael@0: .D32(0x40005510) // return address michael@0: .Mark(&frame1_esp); // This effectively sets stack_section.start(). michael@0: raw_context.eip = 0x40004004; michael@0: raw_context.ebx = 0x53d1379d; // saved %esi michael@0: raw_context.esi = 0x0fb7dc4e; // callee's %esi michael@0: raw_context.edi = 0x993b4280; // callee's %edi michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4005) { michael@0: Label frame1_esp = expected.esp; michael@0: stack_section michael@0: .D32(0xe29782c2) // garbage michael@0: .D32(0xafbae234) // saved %edi michael@0: .D32(0x5ba29ce9) // garbage michael@0: .D32(0x60f20ce6) // saved %ebx michael@0: .D32(0x8036cc02) // garbage michael@0: .Mark(&frame1_esp); // This effectively sets stack_section.start(). michael@0: raw_context.eip = 0x40004005; michael@0: raw_context.ebx = 0x53d1379d; // saved %esi michael@0: raw_context.esi = 0x0fb7dc4e; // callee's %esi michael@0: raw_context.edi = 0x40005510; // return address michael@0: CheckWalk(); michael@0: } michael@0: michael@0: TEST_F(CFI, At4006) { michael@0: Label frame0_ebp; michael@0: Label frame1_esp = expected.esp; michael@0: stack_section michael@0: .D32(0xdcdd25cd) // garbage michael@0: .D32(0xafbae234) // saved %edi michael@0: .D32(0xc0d4aab9) // saved %ebp michael@0: .Mark(&frame0_ebp) // frame pointer points here michael@0: .D32(0x60f20ce6) // saved %ebx michael@0: .D32(0x8036cc02) // garbage michael@0: .Mark(&frame1_esp); // This effectively sets stack_section.start(). michael@0: raw_context.eip = 0x40004006; michael@0: raw_context.ebp = frame0_ebp.Value(); michael@0: raw_context.ebx = 0x53d1379d; // saved %esi michael@0: raw_context.esi = 0x743833c9; // callee's %esi michael@0: raw_context.edi = 0x40005510; // return address michael@0: CheckWalk(); michael@0: } michael@0: