aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjimblandy <jimblandy@4c0a9323-5329-0410-9bdc-e9ce6186880e>2010-03-16 16:49:53 +0000
committerjimblandy <jimblandy@4c0a9323-5329-0410-9bdc-e9ce6186880e>2010-03-16 16:49:53 +0000
commite7e1e1ebf58a306af1e3199f6e493106e463cf91 (patch)
tree497d422691977d5b5b8866e96132222e92132ee2 /src
parentBreakpad: Support DWARF CFI-driven stack walking on ARM. (diff)
downloadbreakpad-e7e1e1ebf58a306af1e3199f6e493106e463cf91.tar.xz
Breakpad processor: Support AMD64 stack unwinding driven by DWARF CFI.
This adds support for 'STACK CFI' records (DWARF CFI) to the AMD64 stack walker. This is necessary for the stack trace to include any frames other than the youngest. Unit tests are included. a=jimblandy, r=mmentovai git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@554 4c0a9323-5329-0410-9bdc-e9ce6186880e
Diffstat (limited to 'src')
-rw-r--r--src/google_breakpad/processor/stack_frame_cpu.h44
-rw-r--r--src/processor/minidump_stackwalk.cc10
-rw-r--r--src/processor/stackwalker_amd64.cc142
-rw-r--r--src/processor/stackwalker_amd64.h17
-rw-r--r--src/processor/stackwalker_amd64_unittest.cc334
5 files changed, 492 insertions, 55 deletions
diff --git a/src/google_breakpad/processor/stack_frame_cpu.h b/src/google_breakpad/processor/stack_frame_cpu.h
index c5bddffb..0996886f 100644
--- a/src/google_breakpad/processor/stack_frame_cpu.h
+++ b/src/google_breakpad/processor/stack_frame_cpu.h
@@ -139,28 +139,44 @@ struct StackFramePPC : public StackFrame {
};
struct StackFrameAMD64 : public StackFrame {
- // ContextValidity has one entry for each relevant hardware pointer register
- // (%rip and %rsp) and one entry for each nonvolatile (callee-save) register.
- //FIXME: validate this list
+ // ContextValidity has one entry for each register that we might be able
+ // to recover.
enum ContextValidity {
- CONTEXT_VALID_NONE = 0,
- CONTEXT_VALID_RIP = 1 << 0,
- CONTEXT_VALID_RSP = 1 << 1,
- CONTEXT_VALID_RBP = 1 << 2,
+ CONTEXT_VALID_NONE = 0,
+ CONTEXT_VALID_RAX = 1 << 0,
+ CONTEXT_VALID_RDX = 1 << 1,
+ CONTEXT_VALID_RCX = 1 << 2,
+ CONTEXT_VALID_RBX = 1 << 3,
+ CONTEXT_VALID_RSI = 1 << 4,
+ CONTEXT_VALID_RDI = 1 << 5,
+ CONTEXT_VALID_RBP = 1 << 6,
+ CONTEXT_VALID_RSP = 1 << 7,
+ CONTEXT_VALID_R8 = 1 << 8,
+ CONTEXT_VALID_R9 = 1 << 9,
+ CONTEXT_VALID_R10 = 1 << 10,
+ CONTEXT_VALID_R11 = 1 << 11,
+ CONTEXT_VALID_R12 = 1 << 12,
+ CONTEXT_VALID_R13 = 1 << 13,
+ CONTEXT_VALID_R14 = 1 << 14,
+ CONTEXT_VALID_R15 = 1 << 15,
+ CONTEXT_VALID_RIP = 1 << 16,
CONTEXT_VALID_ALL = -1
};
StackFrameAMD64() : context(), context_validity(CONTEXT_VALID_NONE) {}
- // Register state. This is only fully valid for the topmost frame in a
- // stack. In other frames, the values of nonvolatile registers may be
- // present, given sufficient debugging information. Refer to
- // context_validity.
+ // Register state. This is only fully valid for the topmost frame in a
+ // stack. In other frames, which registers are present depends on what
+ // debugging information we had available. Refer to context_validity.
MDRawContextAMD64 context;
- // context_validity is actually ContextValidity, but int is used because
- // the OR operator doesn't work well with enumerated types. This indicates
- // which fields in context are valid.
+ // For each register in context whose value has been recovered, we set
+ // the corresponding CONTEXT_VALID_ bit in context_validity.
+ //
+ // context_validity's type should actually be ContextValidity, but
+ // we use int instead because the bitwise inclusive or operator
+ // yields an int when applied to enum values, and C++ doesn't
+ // silently convert from ints to enums.
int context_validity;
};
diff --git a/src/processor/minidump_stackwalk.cc b/src/processor/minidump_stackwalk.cc
index 9fe3b547..4f040476 100644
--- a/src/processor/minidump_stackwalk.cc
+++ b/src/processor/minidump_stackwalk.cc
@@ -196,6 +196,16 @@ static void PrintStack(const CallStack *stack, const string &cpu) {
const StackFrameAMD64 *frame_amd64 =
reinterpret_cast<const StackFrameAMD64*>(frame);
+ if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RBX)
+ sequence = PrintRegister("rbx", frame_amd64->context.rbx, sequence);
+ if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R12)
+ sequence = PrintRegister("r12", frame_amd64->context.r12, sequence);
+ if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R13)
+ sequence = PrintRegister("r13", frame_amd64->context.r13, sequence);
+ if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R14)
+ sequence = PrintRegister("r14", frame_amd64->context.r14, sequence);
+ if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_R15)
+ sequence = PrintRegister("r15", frame_amd64->context.r15, sequence);
if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RIP)
sequence = PrintRegister("rip", frame_amd64->context.rip, sequence);
if (frame_amd64->context_validity & StackFrameAMD64::CONTEXT_VALID_RSP)
diff --git a/src/processor/stackwalker_amd64.cc b/src/processor/stackwalker_amd64.cc
index 88c87210..38d8fb66 100644
--- a/src/processor/stackwalker_amd64.cc
+++ b/src/processor/stackwalker_amd64.cc
@@ -34,15 +34,61 @@
// Author: Mark Mentovai, Ted Mielczarek
-#include "processor/stackwalker_amd64.h"
#include "google_breakpad/processor/call_stack.h"
#include "google_breakpad/processor/memory_region.h"
+#include "google_breakpad/processor/source_line_resolver_interface.h"
#include "google_breakpad/processor/stack_frame_cpu.h"
+#include "processor/cfi_frame_info.h"
#include "processor/logging.h"
+#include "processor/scoped_ptr.h"
+#include "processor/stackwalker_amd64.h"
namespace google_breakpad {
+const StackwalkerAMD64::CFIWalker::RegisterSet
+StackwalkerAMD64::cfi_register_map_[] = {
+ // It may seem like $rip and $rsp are callee-saves, because the callee is
+ // responsible for having them restored upon return. But the callee_saves
+ // flags here really means that the walker should assume they're
+ // unchanged if the CFI doesn't mention them --- clearly wrong for $rip
+ // and $rsp.
+ { "$rax", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_RAX, &MDRawContextAMD64::rax },
+ { "$rdx", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_RDX, &MDRawContextAMD64::rdx },
+ { "$rcx", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_RCX, &MDRawContextAMD64::rcx },
+ { "$rbx", NULL, true,
+ StackFrameAMD64::CONTEXT_VALID_RBX, &MDRawContextAMD64::rbx },
+ { "$rsi", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_RSI, &MDRawContextAMD64::rsi },
+ { "$rdi", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_RDI, &MDRawContextAMD64::rdi },
+ { "$rbp", NULL, true,
+ StackFrameAMD64::CONTEXT_VALID_RBP, &MDRawContextAMD64::rbp },
+ { "$rsp", ".cfa", false,
+ StackFrameAMD64::CONTEXT_VALID_RSP, &MDRawContextAMD64::rsp },
+ { "$r8", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_R8, &MDRawContextAMD64::r8 },
+ { "$r9", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_R9, &MDRawContextAMD64::r9 },
+ { "$r10", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_R10, &MDRawContextAMD64::r10 },
+ { "$r11", NULL, false,
+ StackFrameAMD64::CONTEXT_VALID_R11, &MDRawContextAMD64::r11 },
+ { "$r12", NULL, true,
+ StackFrameAMD64::CONTEXT_VALID_R12, &MDRawContextAMD64::r12 },
+ { "$r13", NULL, true,
+ StackFrameAMD64::CONTEXT_VALID_R13, &MDRawContextAMD64::r13 },
+ { "$r14", NULL, true,
+ StackFrameAMD64::CONTEXT_VALID_R14, &MDRawContextAMD64::r14 },
+ { "$r15", NULL, true,
+ StackFrameAMD64::CONTEXT_VALID_R15, &MDRawContextAMD64::r15 },
+ { "$rip", ".ra", false,
+ StackFrameAMD64::CONTEXT_VALID_RIP, &MDRawContextAMD64::rip },
+};
+
StackwalkerAMD64::StackwalkerAMD64(const SystemInfo *system_info,
const MDRawContextAMD64 *context,
MemoryRegion *memory,
@@ -50,7 +96,9 @@ StackwalkerAMD64::StackwalkerAMD64(const SystemInfo *system_info,
SymbolSupplier *supplier,
SourceLineResolverInterface *resolver)
: Stackwalker(system_info, memory, modules, supplier, resolver),
- context_(context) {
+ context_(context),
+ cfi_walker_(cfi_register_map_,
+ (sizeof(cfi_register_map_) / sizeof(cfi_register_map_[0]))) {
}
@@ -71,6 +119,26 @@ StackFrame* StackwalkerAMD64::GetContextFrame() {
return frame;
}
+StackFrameAMD64 *StackwalkerAMD64::GetCallerByCFIFrameInfo(
+ const vector<StackFrame *> &frames,
+ CFIFrameInfo *cfi_frame_info) {
+ StackFrameAMD64 *last_frame = static_cast<StackFrameAMD64*>(frames.back());
+
+ scoped_ptr<StackFrameAMD64> frame(new StackFrameAMD64());
+ if (!cfi_walker_
+ .FindCallerRegisters(*memory_, *cfi_frame_info,
+ last_frame->context, last_frame->context_validity,
+ &frame->context, &frame->context_validity))
+ return NULL;
+
+ // Make sure we recovered all the essentials.
+ static const int essentials = (StackFrameAMD64::CONTEXT_VALID_RIP
+ | StackFrameAMD64::CONTEXT_VALID_RSP);
+ if ((frame->context_validity & essentials) != essentials)
+ return NULL;
+
+ return frame.release();
+}
StackFrame* StackwalkerAMD64::GetCallerFrame(const CallStack *stack) {
if (!memory_ || !stack) {
@@ -78,51 +146,43 @@ StackFrame* StackwalkerAMD64::GetCallerFrame(const CallStack *stack) {
return NULL;
}
- StackFrameAMD64 *last_frame = static_cast<StackFrameAMD64*>(
- stack->frames()->back());
-
- //FIXME: this pretty much doesn't work at all due to FPO
- // being enabled by default.
- // Brain-dead stackwalking:
- // %rip_new = *(%rbp_old + 8)
- // %rsp_new = %rbp_old + 16
- // %rbp_new = *(%rbp_old)
-
- // A caller frame must reside higher in memory than its callee frames.
- // Anything else is an error, or an indication that we've reached the
- // end of the stack.
- u_int64_t stack_pointer = last_frame->context.rbp + 16;
- if (stack_pointer <= last_frame->context.rsp) {
- return NULL;
- }
+ const vector<StackFrame *> &frames = *stack->frames();
+ StackFrameAMD64 *last_frame = static_cast<StackFrameAMD64 *>(frames.back());
+ scoped_ptr<StackFrameAMD64> new_frame;
- u_int64_t instruction;
- if (!memory_->GetMemoryAtAddress(last_frame->context.rbp + 8,
- &instruction) ||
- instruction <= 1) {
- return NULL;
+ // If we have DWARF CFI information, use it.
+ if (!new_frame.get()) {
+ scoped_ptr<CFIFrameInfo> cfi_frame_info(resolver_
+ ->FindCFIFrameInfo(last_frame));
+ if (cfi_frame_info.get())
+ new_frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
}
- u_int64_t stack_base;
- if (!memory_->GetMemoryAtAddress(last_frame->context.rbp,
- &stack_base) ||
- stack_base <= 1) {
+ // If nothing worked, tell the caller.
+ if (!new_frame.get())
+ return NULL;
+
+ // Treat an instruction address of 0 as end-of-stack.
+ if (new_frame->context.rip == 0)
return NULL;
- }
-
- StackFrameAMD64 *frame = new StackFrameAMD64();
-
- frame->context = last_frame->context;
- frame->context.rip = instruction;
- frame->context.rsp = stack_pointer;
- frame->context.rbp = stack_base;
- frame->context_validity = StackFrameAMD64::CONTEXT_VALID_RIP |
- StackFrameAMD64::CONTEXT_VALID_RSP |
- StackFrameAMD64::CONTEXT_VALID_RBP;
- frame->instruction = frame->context.rip - 1;
+ // If the new stack pointer is at a lower address than the old, then
+ // that's clearly incorrect. Treat this as end-of-stack to enforce
+ // progress and avoid infinite loops.
+ if (new_frame->context.rsp <= last_frame->context.rsp)
+ return NULL;
- return frame;
+ // new_frame->context.rip is the return address, which is one instruction
+ // past the CALL that caused us to arrive at the callee. Set
+ // new_frame->instruction to one less than that. This won't reference the
+ // beginning of the CALL instruction, but it's guaranteed to be within
+ // the CALL, which is sufficient to get the source line information to
+ // match up with the line that contains a function call. Callers that
+ // require the exact return address value may access the context.rip
+ // field of StackFrameAMD64.
+ new_frame->instruction = new_frame->context.rip - 1;
+
+ return new_frame.release();
}
diff --git a/src/processor/stackwalker_amd64.h b/src/processor/stackwalker_amd64.h
index 32251d44..9e61fcf5 100644
--- a/src/processor/stackwalker_amd64.h
+++ b/src/processor/stackwalker_amd64.h
@@ -42,6 +42,8 @@
#include "google_breakpad/common/breakpad_types.h"
#include "google_breakpad/common/minidump_format.h"
#include "google_breakpad/processor/stackwalker.h"
+#include "google_breakpad/processor/stack_frame_cpu.h"
+#include "processor/cfi_frame_info.h"
namespace google_breakpad {
@@ -61,14 +63,29 @@ class StackwalkerAMD64 : public Stackwalker {
SourceLineResolverInterface *resolver);
private:
+ // A STACK CFI-driven frame walker for the AMD64
+ typedef SimpleCFIWalker<u_int64_t, MDRawContextAMD64> CFIWalker;
+
// Implementation of Stackwalker, using amd64 context (stack pointer in %rsp,
// stack base in %rbp) and stack conventions (saved stack pointer at 0(%rbp))
virtual StackFrame* GetContextFrame();
virtual StackFrame* GetCallerFrame(const CallStack *stack);
+ // Use cfi_frame_info (derived from STACK CFI records) to construct
+ // the frame that called frames.back(). The caller takes ownership
+ // of the returned frame. Return NULL on failure.
+ StackFrameAMD64 *GetCallerByCFIFrameInfo(const vector<StackFrame *> &frames,
+ CFIFrameInfo *cfi_frame_info);
+
// Stores the CPU context corresponding to the innermost stack frame to
// be returned by GetContextFrame.
const MDRawContextAMD64 *context_;
+
+ // Our register map, for cfi_walker_.
+ static const CFIWalker::RegisterSet cfi_register_map_[];
+
+ // Our CFI frame walker.
+ const CFIWalker cfi_walker_;
};
diff --git a/src/processor/stackwalker_amd64_unittest.cc b/src/processor/stackwalker_amd64_unittest.cc
new file mode 100644
index 00000000..7b56fb1c
--- /dev/null
+++ b/src/processor/stackwalker_amd64_unittest.cc
@@ -0,0 +1,334 @@
+// Copyright (c) 2010, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
+
+// stackwalker_amd64_unittest.cc: Unit tests for StackwalkerAMD64 class.
+
+#include <string>
+#include <string.h>
+#include <vector>
+
+#include "breakpad_googletest_includes.h"
+#include "google_breakpad/common/minidump_format.h"
+#include "google_breakpad/processor/basic_source_line_resolver.h"
+#include "google_breakpad/processor/call_stack.h"
+#include "google_breakpad/processor/source_line_resolver_interface.h"
+#include "google_breakpad/processor/stack_frame_cpu.h"
+#include "processor/stackwalker_unittest_utils.h"
+#include "processor/stackwalker_amd64.h"
+#include "processor/test_assembler.h"
+
+using google_breakpad::BasicSourceLineResolver;
+using google_breakpad::CallStack;
+using google_breakpad::StackFrame;
+using google_breakpad::StackFrameAMD64;
+using google_breakpad::StackwalkerAMD64;
+using google_breakpad::SystemInfo;
+using google_breakpad::TestAssembler::kLittleEndian;
+using google_breakpad::TestAssembler::Label;
+using google_breakpad::TestAssembler::Section;
+using std::string;
+using std::vector;
+using testing::_;
+using testing::Return;
+using testing::SetArgumentPointee;
+using testing::Test;
+
+class StackwalkerAMD64Fixture {
+ public:
+ StackwalkerAMD64Fixture()
+ : stack_section(kLittleEndian),
+ // Give the two modules reasonable standard locations and names
+ // for tests to play with.
+ module1(0x40000000c0000000ULL, 0x10000, "module1", "version1"),
+ module2(0x50000000b0000000ULL, 0x10000, "module2", "version2") {
+ // Identify the system as a Linux system.
+ system_info.os = "Linux";
+ system_info.os_short = "linux";
+ system_info.os_version = "Horrendous Hippo";
+ system_info.cpu = "x86";
+ system_info.cpu_info = "";
+
+ // Put distinctive values in the raw CPU context.
+ BrandContext(&raw_context);
+
+ // Create some modules with some stock debugging information.
+ modules.Add(&module1);
+ modules.Add(&module2);
+
+ // By default, none of the modules have symbol info; call
+ // SetModuleSymbols to override this.
+ EXPECT_CALL(supplier, GetSymbolFile(_, _, _, _))
+ .WillRepeatedly(Return(MockSymbolSupplier::NOT_FOUND));
+ }
+
+ // Set the Breakpad symbol information that supplier should return for
+ // MODULE to INFO.
+ void SetModuleSymbols(MockCodeModule *module, const string &info) {
+ EXPECT_CALL(supplier, GetSymbolFile(module, &system_info, _, _))
+ .WillRepeatedly(DoAll(SetArgumentPointee<3>(info),
+ Return(MockSymbolSupplier::FOUND)));
+ }
+
+ // Populate stack_region with the contents of stack_section. Use
+ // stack_section.start() as the region's starting address.
+ void RegionFromSection() {
+ string contents;
+ ASSERT_TRUE(stack_section.GetContents(&contents));
+ stack_region.Init(stack_section.start().Value(), contents);
+ }
+
+ // Fill RAW_CONTEXT with pseudo-random data, for round-trip checking.
+ void BrandContext(MDRawContextAMD64 *raw_context) {
+ u_int8_t x = 173;
+ for (size_t i = 0; i < sizeof(*raw_context); i++)
+ reinterpret_cast<u_int8_t *>(raw_context)[i] = (x += 17);
+ }
+
+ SystemInfo system_info;
+ MDRawContextAMD64 raw_context;
+ Section stack_section;
+ MockMemoryRegion stack_region;
+ MockCodeModule module1;
+ MockCodeModule module2;
+ MockCodeModules modules;
+ MockSymbolSupplier supplier;
+ BasicSourceLineResolver resolver;
+ CallStack call_stack;
+ const vector<StackFrame *> *frames;
+};
+
+class GetContextFrame: public StackwalkerAMD64Fixture, public Test { };
+
+TEST_F(GetContextFrame, Simple) {
+ // There should be no references to the stack in this walk: we don't
+ // provide any call frame information, so trying to reconstruct the
+ // context frame's caller should fail. So there's no need for us to
+ // provide stack contents.
+ raw_context.rip = 0x40000000c0000200ULL;
+ raw_context.rbp = 0x8000000080000000ULL;
+
+ StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules,
+ &supplier, &resolver);
+ ASSERT_TRUE(walker.Walk(&call_stack));
+ frames = call_stack.frames();
+ ASSERT_GE(1U, frames->size());
+ StackFrameAMD64 *frame = static_cast<StackFrameAMD64 *>(frames->at(0));
+ // Check that the values from the original raw context made it
+ // through to the context in the stack frame.
+ EXPECT_TRUE(memcmp(&raw_context, &frame->context, sizeof(raw_context)) == 0);
+}
+
+struct CFIFixture: public StackwalkerAMD64Fixture {
+ CFIFixture() {
+ // Provide a bunch of STACK CFI records; we'll walk to the caller
+ // from every point in this series, expecting to find the same set
+ // of register values.
+ SetModuleSymbols(&module1,
+ // The youngest frame's function.
+ "FUNC 4000 1000 10 enchiridion\n"
+ // Initially, just a return address.
+ "STACK CFI INIT 4000 100 .cfa: $rsp 8 + .ra: .cfa 8 - ^\n"
+ // Push %rbx.
+ "STACK CFI 4001 .cfa: $rsp 16 + $rbx: .cfa 16 - ^\n"
+ // Save %r12 in %rbx. Weird, but permitted.
+ "STACK CFI 4002 $r12: $rbx\n"
+ // Allocate frame space, and save %r13.
+ "STACK CFI 4003 .cfa: $rsp 40 + $r13: .cfa 32 - ^\n"
+ // Put the return address in %r13.
+ "STACK CFI 4005 .ra: $r13\n"
+ // Save %rbp, and use it as a frame pointer.
+ "STACK CFI 4006 .cfa: $rbp 16 + $rbp: .cfa 24 - ^\n"
+
+ // The calling function.
+ "FUNC 5000 1000 10 epictetus\n"
+ // Mark it as end of stack.
+ "STACK CFI INIT 5000 1000 .cfa: $rsp .ra 0\n");
+
+ // Provide some distinctive values for the caller's registers.
+ expected.rsp = 0x8000000080000000ULL;
+ expected.rip = 0x40000000c0005510ULL;
+ expected.rbp = 0x68995b1de4700266ULL;
+ expected.rbx = 0x5a5beeb38de23be8ULL;
+ expected.r12 = 0xed1b02e8cc0fc79cULL;
+ expected.r13 = 0x1d20ad8acacbe930ULL;
+ expected.r14 = 0xe94cffc2f7adaa28ULL;
+ expected.r15 = 0xb638d17d8da413b5ULL;
+
+ // By default, registers are unchanged.
+ raw_context = expected;
+ }
+
+ // Walk the stack, using stack_section as the contents of the stack
+ // and raw_context as the current register values. (Set
+ // raw_context.rsp to the stack's starting address.) Expect two
+ // stack frames; in the older frame, expect the callee-saves
+ // registers to have values matching those in 'expected'.
+ void CheckWalk() {
+ RegionFromSection();
+ raw_context.rsp = stack_section.start().Value();
+
+ StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules,
+ &supplier, &resolver);
+ ASSERT_TRUE(walker.Walk(&call_stack));
+ frames = call_stack.frames();
+ ASSERT_EQ(2U, frames->size());
+
+ StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0));
+ ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity);
+ EXPECT_EQ("enchiridion", frame0->function_name);
+ EXPECT_EQ(0x40000000c0004000ULL, frame0->function_base);
+
+ StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1));
+ ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP |
+ StackFrameAMD64::CONTEXT_VALID_RSP |
+ StackFrameAMD64::CONTEXT_VALID_RBP |
+ StackFrameAMD64::CONTEXT_VALID_RBX |
+ StackFrameAMD64::CONTEXT_VALID_R12 |
+ StackFrameAMD64::CONTEXT_VALID_R13 |
+ StackFrameAMD64::CONTEXT_VALID_R14 |
+ StackFrameAMD64::CONTEXT_VALID_R15),
+ frame1->context_validity);
+ EXPECT_EQ(expected.rip, frame1->context.rip);
+ EXPECT_EQ(expected.rsp, frame1->context.rsp);
+ EXPECT_EQ(expected.rbp, frame1->context.rbp);
+ EXPECT_EQ(expected.rbx, frame1->context.rbx);
+ EXPECT_EQ(expected.r12, frame1->context.r12);
+ EXPECT_EQ(expected.r13, frame1->context.r13);
+ EXPECT_EQ(expected.r14, frame1->context.r14);
+ EXPECT_EQ(expected.r15, frame1->context.r15);
+ EXPECT_EQ("epictetus", frame1->function_name);
+ }
+
+ // The values we expect to find for the caller's registers.
+ MDRawContextAMD64 expected;
+};
+
+class CFI: public CFIFixture, public Test { };
+
+TEST_F(CFI, At4000) {
+ Label frame1_rsp = expected.rsp;
+ stack_section
+ .D64(0x40000000c0005510ULL) // return address
+ .Mark(&frame1_rsp); // This effectively sets stack_section.start().
+ raw_context.rip = 0x40000000c0004000ULL;
+ CheckWalk();
+}
+
+TEST_F(CFI, At4001) {
+ Label frame1_rsp = expected.rsp;
+ stack_section
+ .D64(0x5a5beeb38de23be8ULL) // saved %rbx
+ .D64(0x40000000c0005510ULL) // return address
+ .Mark(&frame1_rsp); // This effectively sets stack_section.start().
+ raw_context.rip = 0x40000000c0004001ULL;
+ raw_context.rbx = 0xbe0487d2f9eafe29ULL; // callee's (distinct) %rbx value
+ CheckWalk();
+}
+
+TEST_F(CFI, At4002) {
+ Label frame1_rsp = expected.rsp;
+ stack_section
+ .D64(0x5a5beeb38de23be8ULL) // saved %rbx
+ .D64(0x40000000c0005510ULL) // return address
+ .Mark(&frame1_rsp); // This effectively sets stack_section.start().
+ raw_context.rip = 0x40000000c0004002ULL;
+ raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12
+ raw_context.r12 = 0xb0118de918a4bceaULL; // callee's (distinct) %r12 value
+ CheckWalk();
+}
+
+TEST_F(CFI, At4003) {
+ Label frame1_rsp = expected.rsp;
+ stack_section
+ .D64(0x0e023828dffd4d81ULL) // garbage
+ .D64(0x1d20ad8acacbe930ULL) // saved %r13
+ .D64(0x319e68b49e3ace0fULL) // garbage
+ .D64(0x5a5beeb38de23be8ULL) // saved %rbx
+ .D64(0x40000000c0005510ULL) // return address
+ .Mark(&frame1_rsp); // This effectively sets stack_section.start().
+ raw_context.rip = 0x40000000c0004003ULL;
+ raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12
+ raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12
+ raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13
+ CheckWalk();
+}
+
+// The results here should be the same as those at module offset 0x4003.
+TEST_F(CFI, At4004) {
+ Label frame1_rsp = expected.rsp;
+ stack_section
+ .D64(0x0e023828dffd4d81ULL) // garbage
+ .D64(0x1d20ad8acacbe930ULL) // saved %r13
+ .D64(0x319e68b49e3ace0fULL) // garbage
+ .D64(0x5a5beeb38de23be8ULL) // saved %rbx
+ .D64(0x40000000c0005510ULL) // return address
+ .Mark(&frame1_rsp); // This effectively sets stack_section.start().
+ raw_context.rip = 0x40000000c0004004ULL;
+ raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12
+ raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12
+ raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13
+ CheckWalk();
+}
+
+TEST_F(CFI, At4005) {
+ Label frame1_rsp = expected.rsp;
+ stack_section
+ .D64(0x4b516dd035745953ULL) // garbage
+ .D64(0x1d20ad8acacbe930ULL) // saved %r13
+ .D64(0xa6d445e16ae3d872ULL) // garbage
+ .D64(0x5a5beeb38de23be8ULL) // saved %rbx
+ .D64(0xaa95fa054aedfbaeULL) // garbage
+ .Mark(&frame1_rsp); // This effectively sets stack_section.start().
+ raw_context.rip = 0x40000000c0004005ULL;
+ raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12
+ raw_context.r12 = 0x46b1b8868891b34aULL; // callee's %r12
+ raw_context.r13 = 0x40000000c0005510ULL; // return address
+ CheckWalk();
+}
+
+TEST_F(CFI, At4006) {
+ Label frame0_rbp;
+ Label frame1_rsp = expected.rsp;
+ stack_section
+ .D64(0x043c6dfceb91aa34ULL) // garbage
+ .D64(0x1d20ad8acacbe930ULL) // saved %r13
+ .D64(0x68995b1de4700266ULL) // saved %rbp
+ .Mark(&frame0_rbp) // frame pointer points here
+ .D64(0x5a5beeb38de23be8ULL) // saved %rbx
+ .D64(0xf015ee516ad89eabULL) // garbage
+ .Mark(&frame1_rsp); // This effectively sets stack_section.start().
+ raw_context.rip = 0x40000000c0004006ULL;
+ raw_context.rbp = frame0_rbp.Value();
+ raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12
+ raw_context.r12 = 0x26e007b341acfebdULL; // callee's %r12
+ raw_context.r13 = 0x40000000c0005510ULL; // return address
+ CheckWalk();
+}