1 // Copyright (c) 2010 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // stackwalker_amd64.cc: amd64-specific stackwalker.
31 //
32 // See stackwalker_amd64.h for documentation.
33 //
34 // Author: Mark Mentovai, Ted Mielczarek
35 
36 #include <assert.h>
37 
38 #include "common/scoped_ptr.h"
39 #include "google_breakpad/processor/call_stack.h"
40 #include "google_breakpad/processor/memory_region.h"
41 #include "google_breakpad/processor/source_line_resolver_interface.h"
42 #include "google_breakpad/processor/stack_frame_cpu.h"
43 #include "google_breakpad/processor/system_info.h"
44 #include "processor/cfi_frame_info.h"
45 #include "processor/logging.h"
46 #include "processor/stackwalker_amd64.h"
47 
48 namespace google_breakpad {
49 
50 
51 const StackwalkerAMD64::CFIWalker::RegisterSet
52 StackwalkerAMD64::cfi_register_map_[] = {
53   // It may seem like $rip and $rsp are callee-saves, because the callee is
54   // responsible for having them restored upon return. But the callee_saves
55   // flags here really means that the walker should assume they're
56   // unchanged if the CFI doesn't mention them --- clearly wrong for $rip
57   // and $rsp.
58   { "$rax", NULL, false,
59     StackFrameAMD64::CONTEXT_VALID_RAX, &MDRawContextAMD64::rax },
60   { "$rdx", NULL, false,
61     StackFrameAMD64::CONTEXT_VALID_RDX, &MDRawContextAMD64::rdx },
62   { "$rcx", NULL, false,
63     StackFrameAMD64::CONTEXT_VALID_RCX, &MDRawContextAMD64::rcx },
64   { "$rbx", NULL, true,
65     StackFrameAMD64::CONTEXT_VALID_RBX, &MDRawContextAMD64::rbx },
66   { "$rsi", NULL, false,
67     StackFrameAMD64::CONTEXT_VALID_RSI, &MDRawContextAMD64::rsi },
68   { "$rdi", NULL, false,
69     StackFrameAMD64::CONTEXT_VALID_RDI, &MDRawContextAMD64::rdi },
70   { "$rbp", NULL, true,
71     StackFrameAMD64::CONTEXT_VALID_RBP, &MDRawContextAMD64::rbp },
72   { "$rsp", ".cfa", false,
73     StackFrameAMD64::CONTEXT_VALID_RSP, &MDRawContextAMD64::rsp },
74   { "$r8", NULL, false,
75     StackFrameAMD64::CONTEXT_VALID_R8,  &MDRawContextAMD64::r8 },
76   { "$r9", NULL, false,
77     StackFrameAMD64::CONTEXT_VALID_R9,  &MDRawContextAMD64::r9 },
78   { "$r10", NULL, false,
79     StackFrameAMD64::CONTEXT_VALID_R10, &MDRawContextAMD64::r10 },
80   { "$r11", NULL, false,
81     StackFrameAMD64::CONTEXT_VALID_R11, &MDRawContextAMD64::r11 },
82   { "$r12", NULL, true,
83     StackFrameAMD64::CONTEXT_VALID_R12, &MDRawContextAMD64::r12 },
84   { "$r13", NULL, true,
85     StackFrameAMD64::CONTEXT_VALID_R13, &MDRawContextAMD64::r13 },
86   { "$r14", NULL, true,
87     StackFrameAMD64::CONTEXT_VALID_R14, &MDRawContextAMD64::r14 },
88   { "$r15", NULL, true,
89     StackFrameAMD64::CONTEXT_VALID_R15, &MDRawContextAMD64::r15 },
90   { "$rip", ".ra", false,
91     StackFrameAMD64::CONTEXT_VALID_RIP, &MDRawContextAMD64::rip },
92 };
93 
StackwalkerAMD64(const SystemInfo * system_info,const MDRawContextAMD64 * context,MemoryRegion * memory,const CodeModules * modules,StackFrameSymbolizer * resolver_helper)94 StackwalkerAMD64::StackwalkerAMD64(const SystemInfo* system_info,
95                                    const MDRawContextAMD64* context,
96                                    MemoryRegion* memory,
97                                    const CodeModules* modules,
98                                    StackFrameSymbolizer* resolver_helper)
99     : Stackwalker(system_info, memory, modules, resolver_helper),
100       context_(context),
101       cfi_walker_(cfi_register_map_,
102                   (sizeof(cfi_register_map_) / sizeof(cfi_register_map_[0]))) {
103 }
104 
ReturnAddress() const105 uint64_t StackFrameAMD64::ReturnAddress() const {
106   assert(context_validity & StackFrameAMD64::CONTEXT_VALID_RIP);
107   return context.rip;
108 }
109 
GetContextFrame()110 StackFrame* StackwalkerAMD64::GetContextFrame() {
111   if (!context_) {
112     BPLOG(ERROR) << "Can't get context frame without context";
113     return NULL;
114   }
115 
116   StackFrameAMD64* frame = new StackFrameAMD64();
117 
118   // The instruction pointer is stored directly in a register, so pull it
119   // straight out of the CPU context structure.
120   frame->context = *context_;
121   frame->context_validity = StackFrameAMD64::CONTEXT_VALID_ALL;
122   frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
123   frame->instruction = frame->context.rip;
124 
125   return frame;
126 }
127 
GetCallerByCFIFrameInfo(const vector<StackFrame * > & frames,CFIFrameInfo * cfi_frame_info)128 StackFrameAMD64* StackwalkerAMD64::GetCallerByCFIFrameInfo(
129     const vector<StackFrame*> &frames,
130     CFIFrameInfo* cfi_frame_info) {
131   StackFrameAMD64* last_frame = static_cast<StackFrameAMD64*>(frames.back());
132 
133   scoped_ptr<StackFrameAMD64> frame(new StackFrameAMD64());
134   if (!cfi_walker_
135       .FindCallerRegisters(*memory_, *cfi_frame_info,
136                            last_frame->context, last_frame->context_validity,
137                            &frame->context, &frame->context_validity))
138     return NULL;
139 
140   // Make sure we recovered all the essentials.
141   static const int essentials = (StackFrameAMD64::CONTEXT_VALID_RIP
142                                  | StackFrameAMD64::CONTEXT_VALID_RSP);
143   if ((frame->context_validity & essentials) != essentials)
144     return NULL;
145 
146   frame->trust = StackFrame::FRAME_TRUST_CFI;
147   return frame.release();
148 }
149 
150 // Returns true if `ptr` is not in x86-64 canonical form.
151 // https://en.wikipedia.org/wiki/X86-64#Virtual_address_space_details
is_non_canonical(uint64_t ptr)152 static bool is_non_canonical(uint64_t ptr) {
153   return ptr > 0x7FFFFFFFFFFF && ptr < 0xFFFF800000000000;
154 }
155 
GetCallerByFramePointerRecovery(const vector<StackFrame * > & frames)156 StackFrameAMD64* StackwalkerAMD64::GetCallerByFramePointerRecovery(
157     const vector<StackFrame*>& frames) {
158   StackFrameAMD64* last_frame = static_cast<StackFrameAMD64*>(frames.back());
159   uint64_t last_rbp = last_frame->context.rbp;
160 
161   // Assume the presence of a frame pointer. This is not mandated by the
162   // AMD64 ABI, c.f. section 3.2.2 footnote 7, though it is typical for
163   // compilers to still preserve the frame pointer and not treat %rbp as a
164   // general purpose register.
165   //
166   // With this assumption, the CALL instruction pushes the return address
167   // onto the stack and sets %rip to the procedure to enter. The procedure
168   // then establishes the stack frame with a prologue that PUSHes the current
169   // %rbp onto the stack, MOVes the current %rsp to %rbp, and then allocates
170   // space for any local variables. Using this procedure linking information,
171   // it is possible to locate frame information for the callee:
172   //
173   // %caller_rsp = *(%callee_rbp + 16)
174   // %caller_rip = *(%callee_rbp + 8)
175   // %caller_rbp = *(%callee_rbp)
176 
177   // If rbp is not 8-byte aligned it can't be a frame pointer.
178   if (last_rbp % 8 != 0) {
179     return NULL;
180   }
181 
182   uint64_t caller_rip, caller_rbp;
183   if (memory_->GetMemoryAtAddress(last_rbp + 8, &caller_rip) &&
184       memory_->GetMemoryAtAddress(last_rbp, &caller_rbp)) {
185     uint64_t caller_rsp = last_rbp + 16;
186 
187     // If the recovered rip is not a canonical address it can't be
188     // the return address, so rbp must not have been a frame pointer.
189     if (is_non_canonical(caller_rip)) {
190       return NULL;
191     }
192 
193     // Check that rbp is within the right frame
194     if (caller_rsp <= last_rbp || caller_rbp < caller_rsp) {
195       return NULL;
196     }
197 
198     // Sanity check that resulting rbp is still inside stack memory.
199     uint64_t unused;
200     if (!memory_->GetMemoryAtAddress(caller_rbp, &unused)) {
201       return NULL;
202     }
203 
204     StackFrameAMD64* frame = new StackFrameAMD64();
205     frame->trust = StackFrame::FRAME_TRUST_FP;
206     frame->context = last_frame->context;
207     frame->context.rip = caller_rip;
208     frame->context.rsp = caller_rsp;
209     frame->context.rbp = caller_rbp;
210     frame->context_validity = StackFrameAMD64::CONTEXT_VALID_RIP |
211                               StackFrameAMD64::CONTEXT_VALID_RSP |
212                               StackFrameAMD64::CONTEXT_VALID_RBP;
213     return frame;
214   }
215 
216   return NULL;
217 }
218 
GetCallerByStackScan(const vector<StackFrame * > & frames)219 StackFrameAMD64* StackwalkerAMD64::GetCallerByStackScan(
220     const vector<StackFrame*> &frames) {
221   StackFrameAMD64* last_frame = static_cast<StackFrameAMD64*>(frames.back());
222   uint64_t last_rsp = last_frame->context.rsp;
223   uint64_t caller_rip_address, caller_rip;
224 
225   if (!ScanForReturnAddress(last_rsp, &caller_rip_address, &caller_rip,
226                             frames.size() == 1 /* is_context_frame */)) {
227     // No plausible return address was found.
228     return NULL;
229   }
230 
231   // Create a new stack frame (ownership will be transferred to the caller)
232   // and fill it in.
233   StackFrameAMD64* frame = new StackFrameAMD64();
234 
235   frame->trust = StackFrame::FRAME_TRUST_SCAN;
236   frame->context = last_frame->context;
237   frame->context.rip = caller_rip;
238   // The caller's %rsp is directly underneath the return address pushed by
239   // the call.
240   frame->context.rsp = caller_rip_address + 8;
241   frame->context_validity = StackFrameAMD64::CONTEXT_VALID_RIP |
242                             StackFrameAMD64::CONTEXT_VALID_RSP;
243 
244   // Other unwinders give up if they don't have an %rbp value, so see if we
245   // can pass some plausible value on.
246   if (last_frame->context_validity & StackFrameAMD64::CONTEXT_VALID_RBP) {
247     // Functions typically push their caller's %rbp immediately upon entry,
248     // and then set %rbp to point to that. So if the callee's %rbp is
249     // pointing to the first word below the alleged return address, presume
250     // that the caller's %rbp is saved there.
251     if (caller_rip_address - 8 == last_frame->context.rbp) {
252       uint64_t caller_rbp = 0;
253       if (memory_->GetMemoryAtAddress(last_frame->context.rbp, &caller_rbp) &&
254           caller_rbp > caller_rip_address) {
255         frame->context.rbp = caller_rbp;
256         frame->context_validity |= StackFrameAMD64::CONTEXT_VALID_RBP;
257       }
258     } else if (last_frame->context.rbp >= caller_rip_address + 8) {
259       // If the callee's %rbp is plausible as a value for the caller's
260       // %rbp, presume that the callee left it unchanged.
261       frame->context.rbp = last_frame->context.rbp;
262       frame->context_validity |= StackFrameAMD64::CONTEXT_VALID_RBP;
263     }
264   }
265 
266   return frame;
267 }
268 
GetCallerFrame(const CallStack * stack,bool stack_scan_allowed)269 StackFrame* StackwalkerAMD64::GetCallerFrame(const CallStack* stack,
270                                              bool stack_scan_allowed) {
271   if (!memory_ || !stack) {
272     BPLOG(ERROR) << "Can't get caller frame without memory or stack";
273     return NULL;
274   }
275 
276   const vector<StackFrame*> &frames = *stack->frames();
277   StackFrameAMD64* last_frame = static_cast<StackFrameAMD64*>(frames.back());
278   scoped_ptr<StackFrameAMD64> new_frame;
279 
280   // If we have DWARF CFI information, use it.
281   scoped_ptr<CFIFrameInfo> cfi_frame_info(
282       frame_symbolizer_->FindCFIFrameInfo(last_frame));
283   if (cfi_frame_info.get())
284     new_frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
285 
286   // If CFI was not available or failed, try using frame pointer recovery.
287   if (!new_frame.get()) {
288     new_frame.reset(GetCallerByFramePointerRecovery(frames));
289   }
290 
291   // If all else fails, fall back to stack scanning.
292   if (stack_scan_allowed && !new_frame.get()) {
293     new_frame.reset(GetCallerByStackScan(frames));
294   }
295 
296   // If nothing worked, tell the caller.
297   if (!new_frame.get())
298     return NULL;
299 
300   if (system_info_->os_short == "nacl") {
301     // Apply constraints from Native Client's x86-64 sandbox.  These
302     // registers have the 4GB-aligned sandbox base address (from r15)
303     // added to them, and only the bottom 32 bits are relevant for
304     // stack walking.
305     new_frame->context.rip = static_cast<uint32_t>(new_frame->context.rip);
306     new_frame->context.rsp = static_cast<uint32_t>(new_frame->context.rsp);
307     new_frame->context.rbp = static_cast<uint32_t>(new_frame->context.rbp);
308   }
309 
310   // Should we terminate the stack walk? (end-of-stack or broken invariant)
311   if (TerminateWalk(new_frame->context.rip, new_frame->context.rsp,
312                     last_frame->context.rsp, frames.size() == 1)) {
313     return NULL;
314   }
315 
316   // new_frame->context.rip is the return address, which is the instruction
317   // after the CALL that caused us to arrive at the callee. Set
318   // new_frame->instruction to one less than that, so it points within the
319   // CALL instruction. See StackFrame::instruction for details, and
320   // StackFrameAMD64::ReturnAddress.
321   new_frame->instruction = new_frame->context.rip - 1;
322 
323   return new_frame.release();
324 }
325 
326 }  // namespace google_breakpad
327