1// Copyright (c) 2013 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_mips.cc: MIPS-specific stackwalker.
31//
32// See stackwalker_mips.h for documentation.
33//
34// Author: Tata Elxsi
35
36#include "common/scoped_ptr.h"
37#include "google_breakpad/processor/call_stack.h"
38#include "google_breakpad/processor/code_modules.h"
39#include "google_breakpad/processor/memory_region.h"
40#include "google_breakpad/processor/source_line_resolver_interface.h"
41#include "google_breakpad/processor/stack_frame_cpu.h"
42#include "processor/cfi_frame_info.h"
43#include "processor/logging.h"
44#include "processor/postfix_evaluator-inl.h"
45#include "processor/stackwalker_mips.h"
46#include "processor/windows_frame_info.h"
47#include "google_breakpad/common/minidump_cpu_mips.h"
48
49namespace google_breakpad {
50
51StackwalkerMIPS::StackwalkerMIPS(const SystemInfo* system_info,
52                                 const MDRawContextMIPS* context,
53                                 MemoryRegion* memory,
54                                 const CodeModules* modules,
55                                 StackFrameSymbolizer* resolver_helper)
56    : Stackwalker(system_info, memory, modules, resolver_helper),
57      context_(context) {
58  if (memory_ && memory_->GetBase() + memory_->GetSize() - 1 > 0xffffffff) {
59    BPLOG(ERROR) << "Memory out of range for stackwalking: "
60                 << HexString(memory_->GetBase())
61                 << "+"
62                 << HexString(memory_->GetSize());
63    memory_ = NULL;
64  }
65}
66
67StackFrame* StackwalkerMIPS::GetContextFrame() {
68  if (!context_) {
69    BPLOG(ERROR) << "Can't get context frame without context.";
70    return NULL;
71  }
72
73  StackFrameMIPS* frame = new StackFrameMIPS();
74
75  // The instruction pointer is stored directly in a register, so pull it
76  // straight out of the CPU context structure.
77  frame->context = *context_;
78  frame->context_validity = StackFrameMIPS::CONTEXT_VALID_ALL;
79  frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
80  frame->instruction = frame->context.epc;
81
82  return frame;
83}
84
85// Register names for mips.
86static const char* const kRegisterNames[] = {
87   "$zero", "$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3", "$to", "$t1",
88   "$t2",   "$t3", "$t4", "$t5", "$t6", "$t7", "$s0", "$s1", "$s2", "$s3",
89   "$s4",   "$s5", "$s6", "$s7", "$t8", "$t9", "$k0", "$k1", "$gp", "$sp",
90   "$fp",   "$ra", NULL
91  // TODO(gordanac): add float point save registers
92};
93
94StackFrameMIPS* StackwalkerMIPS::GetCallerByCFIFrameInfo(
95    const vector<StackFrame*>& frames,
96    CFIFrameInfo* cfi_frame_info) {
97  StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
98
99  uint32_t sp = 0, pc = 0;
100
101  // Populate a dictionary with the valid register values in last_frame.
102  CFIFrameInfo::RegisterValueMap<uint32_t> callee_registers;
103  // Use the STACK CFI data to recover the caller's register values.
104  CFIFrameInfo::RegisterValueMap<uint32_t> caller_registers;
105
106  for (int i = 0; kRegisterNames[i]; ++i) {
107    caller_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
108    callee_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
109  }
110
111  if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
112                                      &caller_registers))  {
113    return NULL;
114  }
115
116  CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator entry =
117      caller_registers.find(".cfa");
118
119  if (entry != caller_registers.end()) {
120    sp = entry->second;
121    caller_registers["$sp"] = entry->second;
122  }
123
124  entry = caller_registers.find(".ra");
125  if (entry != caller_registers.end()) {
126    caller_registers["$ra"] = entry->second;
127    pc = entry->second - 2 * sizeof(pc);
128  }
129  caller_registers["$pc"] = pc;
130  // Construct a new stack frame given the values the CFI recovered.
131  scoped_ptr<StackFrameMIPS> frame(new StackFrameMIPS());
132
133  for (int i = 0; kRegisterNames[i]; ++i) {
134    CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator caller_entry =
135        caller_registers.find(kRegisterNames[i]);
136
137    if (caller_entry != caller_registers.end()) {
138      // The value of this register is recovered; fill the context with the
139      // value from caller_registers.
140      frame->context.iregs[i] = caller_entry->second;
141      frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
142    } else if (((i >= INDEX_MIPS_REG_S0 && i <= INDEX_MIPS_REG_S7) ||
143                (i > INDEX_MIPS_REG_GP && i < INDEX_MIPS_REG_RA)) &&
144               (last_frame->context_validity &
145                StackFrameMIPS::RegisterValidFlag(i))) {
146      // If the STACK CFI data doesn't mention some callee-save register, and
147      // it is valid in the callee, assume the callee has not yet changed it.
148      // Calee-save registers according to the MIPS o32 ABI specification are:
149      // $s0 to $s7
150      // $sp, $s8
151      frame->context.iregs[i] = last_frame->context.iregs[i];
152      frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
153    }
154  }
155
156  frame->context.epc = caller_registers["$pc"];
157  frame->instruction = caller_registers["$pc"];
158  frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
159
160  frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] = caller_registers["$ra"];
161  frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
162
163  frame->trust = StackFrame::FRAME_TRUST_CFI;
164
165  return frame.release();
166}
167
168StackFrame* StackwalkerMIPS::GetCallerFrame(const CallStack* stack,
169                                            bool stack_scan_allowed) {
170  if (!memory_ || !stack) {
171    BPLOG(ERROR) << "Can't get caller frame without memory or stack";
172    return NULL;
173  }
174
175  const vector<StackFrame*>& frames = *stack->frames();
176  StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
177  scoped_ptr<StackFrameMIPS> new_frame;
178
179  // See if there is DWARF call frame information covering this address.
180  scoped_ptr<CFIFrameInfo> cfi_frame_info(
181    frame_symbolizer_->FindCFIFrameInfo(last_frame));
182  if (cfi_frame_info.get())
183    new_frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
184
185  // If caller frame is not found in CFI try analyzing the stack.
186  if (stack_scan_allowed && !new_frame.get()) {
187    new_frame.reset(GetCallerByStackScan(frames));
188  }
189
190  // If nothing worked, tell the caller.
191  if (!new_frame.get()) {
192    return NULL;
193  }
194
195  // Treat an instruction address of 0 as end-of-stack.
196  if (new_frame->context.epc == 0) {
197    return NULL;
198  }
199
200  // If the new stack pointer is at a lower address than the old, then
201  // that's clearly incorrect. Treat this as end-of-stack to enforce
202  // progress and avoid infinite loops.
203  if (new_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] <=
204      last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP]) {
205    return NULL;
206  }
207
208  return new_frame.release();
209}
210
211StackFrameMIPS* StackwalkerMIPS::GetCallerByStackScan(
212    const vector<StackFrame*>& frames) {
213  const uint32_t kMaxFrameStackSize = 1024;
214  const uint32_t kMinArgsOnStack = 4;
215
216  StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
217
218  uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP];
219  uint32_t caller_pc, caller_sp, caller_fp;
220
221  // Return address cannot be obtained directly.
222  // Force stackwalking.
223
224  // We cannot use frame pointer to get the return address.
225  // We'll scan the stack for a
226  // return address. This can happen if last_frame is executing code
227  // for a module for which we don't have symbols.
228  int count = kMaxFrameStackSize / sizeof(caller_pc);
229
230  if (frames.size() > 1) {
231      // In case of mips32 ABI stack frame of a nonleaf function
232      // must have minimum stack frame assigned for 4 arguments (4 words).
233      // Move stack pointer for 4 words to avoid reporting non-existing frames
234      // for all frames except the topmost one.
235      // There is no way of knowing if topmost frame belongs to a leaf or
236      // a nonleaf function.
237      last_sp +=  kMinArgsOnStack * sizeof(caller_pc);
238      // Adjust 'count' so that return address is scanned only in limits
239      // of one stack frame.
240      count -= kMinArgsOnStack;
241  }
242
243  do {
244    // Scanning for return address from stack pointer of the last frame.
245    if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, count)) {
246      // If we can't find an instruction pointer even with stack scanning,
247      // give up.
248      BPLOG(ERROR) << " ScanForReturnAddress failed ";
249      return NULL;
250    }
251    // Get $fp stored in the stack frame.
252    if (!memory_->GetMemoryAtAddress(caller_sp - sizeof(caller_pc),
253                                     &caller_fp)) {
254      BPLOG(INFO) << " GetMemoryAtAddress for fp failed " ;
255      return NULL;
256    }
257
258    count = count - (caller_sp - last_sp) / sizeof(caller_pc);
259    // Now scan the next address in the stack.
260    last_sp = caller_sp + sizeof(caller_pc);
261  } while ((caller_fp - caller_sp >= kMaxFrameStackSize) && count > 0);
262
263  if (!count) {
264    BPLOG(INFO) << " No frame found " ;
265    return NULL;
266  }
267
268  // ScanForReturnAddress found a reasonable return address. Advance
269  // $sp to the location above the one where the return address was
270  // found.
271  caller_sp += sizeof(caller_pc);
272  // caller_pc is actually containing $ra value;
273  // $pc is two instructions before $ra,
274  // so the caller_pc needs to be decremented accordingly.
275  caller_pc -= 2 * sizeof(caller_pc);
276
277
278  // Create a new stack frame (ownership will be transferred to the caller)
279  // and fill it in.
280  StackFrameMIPS* frame = new StackFrameMIPS();
281  frame->trust = StackFrame::FRAME_TRUST_SCAN;
282  frame->context = last_frame->context;
283  frame->context.epc = caller_pc;
284  frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
285  frame->instruction = caller_pc;
286
287  frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] = caller_sp;
288  frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_SP;
289  frame->context.iregs[MD_CONTEXT_MIPS_REG_FP] = caller_fp;
290  frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_FP;
291
292  frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] =
293      caller_pc + 2 * sizeof(caller_pc);
294  frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
295
296  return frame;
297}
298
299}  // namespace google_breakpad
300
301