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 
49 namespace google_breakpad {
50 
StackwalkerMIPS(const SystemInfo * system_info,const MDRawContextMIPS * context,MemoryRegion * memory,const CodeModules * modules,StackFrameSymbolizer * resolver_helper)51 StackwalkerMIPS::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 (context_->context_flags & MD_CONTEXT_MIPS64 ) {
59     if ((memory_ && memory_->GetBase() + memory_->GetSize() - 1)
60         > 0xffffffffffffffff) {
61       BPLOG(ERROR) << "Memory out of range for stackwalking mips64: "
62           << HexString(memory_->GetBase())
63           << "+"
64           << HexString(memory_->GetSize());
65       memory_ = NULL;
66     }
67   } else {
68     if ((memory_ && memory_->GetBase() + memory_->GetSize() - 1) > 0xffffffff) {
69       BPLOG(ERROR) << "Memory out of range for stackwalking mips32: "
70           << HexString(memory_->GetBase())
71           << "+"
72           << HexString(memory_->GetSize());
73       memory_ = NULL;
74     }
75   }
76 }
77 
GetContextFrame()78 StackFrame* StackwalkerMIPS::GetContextFrame() {
79   if (!context_) {
80     BPLOG(ERROR) << "Can't get context frame without context.";
81     return NULL;
82   }
83 
84   StackFrameMIPS* frame = new StackFrameMIPS();
85 
86   // The instruction pointer is stored directly in a register, so pull it
87   // straight out of the CPU context structure.
88   frame->context = *context_;
89   frame->context_validity = StackFrameMIPS::CONTEXT_VALID_ALL;
90   frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
91   frame->instruction = frame->context.epc;
92 
93   return frame;
94 }
95 
96 // Register names for mips.
97 static const char* const kRegisterNames[] = {
98    "$zero", "$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3", "$to", "$t1",
99    "$t2",   "$t3", "$t4", "$t5", "$t6", "$t7", "$s0", "$s1", "$s2", "$s3",
100    "$s4",   "$s5", "$s6", "$s7", "$t8", "$t9", "$k0", "$k1", "$gp", "$sp",
101    "$fp",   "$ra", NULL
102   // TODO(gordanac): add float point save registers
103 };
104 
GetCallerByCFIFrameInfo(const vector<StackFrame * > & frames,CFIFrameInfo * cfi_frame_info)105 StackFrameMIPS* StackwalkerMIPS::GetCallerByCFIFrameInfo(
106     const vector<StackFrame*>& frames,
107     CFIFrameInfo* cfi_frame_info) {
108   StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
109 
110   if (context_->context_flags & MD_CONTEXT_MIPS) {
111     uint32_t sp = 0, pc = 0;
112 
113     // Populate a dictionary with the valid register values in last_frame.
114     CFIFrameInfo::RegisterValueMap<uint32_t> callee_registers;
115     // Use the STACK CFI data to recover the caller's register values.
116     CFIFrameInfo::RegisterValueMap<uint32_t> caller_registers;
117 
118     for (int i = 0; kRegisterNames[i]; ++i) {
119       caller_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
120       callee_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
121     }
122 
123     if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
124         &caller_registers))  {
125       return NULL;
126     }
127 
128     CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator entry =
129         caller_registers.find(".cfa");
130 
131     if (entry != caller_registers.end()) {
132       sp = entry->second;
133       caller_registers["$sp"] = entry->second;
134     }
135 
136     entry = caller_registers.find(".ra");
137     if (entry != caller_registers.end()) {
138       caller_registers["$ra"] = entry->second;
139       pc = entry->second - 2 * sizeof(pc);
140     }
141     caller_registers["$pc"] = pc;
142     // Construct a new stack frame given the values the CFI recovered.
143     scoped_ptr<StackFrameMIPS> frame(new StackFrameMIPS());
144 
145     for (int i = 0; kRegisterNames[i]; ++i) {
146       CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator caller_entry =
147           caller_registers.find(kRegisterNames[i]);
148 
149       if (caller_entry != caller_registers.end()) {
150         // The value of this register is recovered; fill the context with the
151         // value from caller_registers.
152         frame->context.iregs[i] = caller_entry->second;
153         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
154       } else if (((i >= INDEX_MIPS_REG_S0 && i <= INDEX_MIPS_REG_S7) ||
155           (i > INDEX_MIPS_REG_GP && i < INDEX_MIPS_REG_RA)) &&
156           (last_frame->context_validity &
157               StackFrameMIPS::RegisterValidFlag(i))) {
158         // If the STACK CFI data doesn't mention some callee-save register, and
159         // it is valid in the callee, assume the callee has not yet changed it.
160         // Calee-save registers according to the MIPS o32 ABI specification are:
161         // $s0 to $s7
162         // $sp, $s8
163         frame->context.iregs[i] = last_frame->context.iregs[i];
164         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
165       }
166     }
167 
168     frame->context.epc = caller_registers["$pc"];
169     frame->instruction = caller_registers["$pc"];
170     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
171 
172     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] = caller_registers["$ra"];
173     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
174 
175     frame->trust = StackFrame::FRAME_TRUST_CFI;
176 
177     return frame.release();
178   } else {
179     uint64_t sp = 0, pc = 0;
180 
181     // Populate a dictionary with the valid register values in last_frame.
182     CFIFrameInfo::RegisterValueMap<uint64_t> callee_registers;
183     // Use the STACK CFI data to recover the caller's register values.
184     CFIFrameInfo::RegisterValueMap<uint64_t> caller_registers;
185 
186     for (int i = 0; kRegisterNames[i]; ++i) {
187       caller_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
188       callee_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
189     }
190 
191     if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
192         &caller_registers))  {
193       return NULL;
194     }
195 
196     CFIFrameInfo::RegisterValueMap<uint64_t>::const_iterator entry =
197         caller_registers.find(".cfa");
198 
199     if (entry != caller_registers.end()) {
200       sp = entry->second;
201       caller_registers["$sp"] = entry->second;
202     }
203 
204     entry = caller_registers.find(".ra");
205     if (entry != caller_registers.end()) {
206       caller_registers["$ra"] = entry->second;
207       pc = entry->second - 2 * sizeof(pc);
208     }
209     caller_registers["$pc"] = pc;
210     // Construct a new stack frame given the values the CFI recovered.
211     scoped_ptr<StackFrameMIPS> frame(new StackFrameMIPS());
212 
213     for (int i = 0; kRegisterNames[i]; ++i) {
214       CFIFrameInfo::RegisterValueMap<uint64_t>::const_iterator caller_entry =
215           caller_registers.find(kRegisterNames[i]);
216 
217       if (caller_entry != caller_registers.end()) {
218         // The value of this register is recovered; fill the context with the
219         // value from caller_registers.
220         frame->context.iregs[i] = caller_entry->second;
221         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
222       } else if (((i >= INDEX_MIPS_REG_S0 && i <= INDEX_MIPS_REG_S7) ||
223           (i >= INDEX_MIPS_REG_GP && i < INDEX_MIPS_REG_RA)) &&
224           (last_frame->context_validity &
225               StackFrameMIPS::RegisterValidFlag(i))) {
226         // If the STACK CFI data doesn't mention some callee-save register, and
227         // it is valid in the callee, assume the callee has not yet changed it.
228         // Calee-save registers according to the MIPS o32 ABI specification are:
229         // $s0 to $s7
230         // $sp, $s8
231         frame->context.iregs[i] = last_frame->context.iregs[i];
232         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
233       }
234     }
235 
236     frame->context.epc = caller_registers["$pc"];
237     frame->instruction = caller_registers["$pc"];
238     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
239 
240     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] = caller_registers["$ra"];
241     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
242 
243     frame->trust = StackFrame::FRAME_TRUST_CFI;
244 
245     return frame.release();
246   }
247 }
248 
GetCallerFrame(const CallStack * stack,bool stack_scan_allowed)249 StackFrame* StackwalkerMIPS::GetCallerFrame(const CallStack* stack,
250                                             bool stack_scan_allowed) {
251   if (!memory_ || !stack) {
252     BPLOG(ERROR) << "Can't get caller frame without memory or stack";
253     return NULL;
254   }
255 
256   const vector<StackFrame*>& frames = *stack->frames();
257   StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
258   scoped_ptr<StackFrameMIPS> new_frame;
259 
260   // See if there is DWARF call frame information covering this address.
261   scoped_ptr<CFIFrameInfo> cfi_frame_info(
262     frame_symbolizer_->FindCFIFrameInfo(last_frame));
263   if (cfi_frame_info.get())
264     new_frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
265 
266   // If caller frame is not found in CFI try analyzing the stack.
267   if (stack_scan_allowed && !new_frame.get()) {
268     new_frame.reset(GetCallerByStackScan(frames));
269   }
270 
271   // If nothing worked, tell the caller.
272   if (!new_frame.get()) {
273     return NULL;
274   }
275 
276   // Treat an instruction address of 0 as end-of-stack.
277   if (new_frame->context.epc == 0) {
278     return NULL;
279   }
280 
281   // If the new stack pointer is at a lower address than the old, then
282   // that's clearly incorrect. Treat this as end-of-stack to enforce
283   // progress and avoid infinite loops.
284   if (new_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] <=
285       last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP]) {
286     return NULL;
287   }
288 
289   return new_frame.release();
290 }
291 
GetCallerByStackScan(const vector<StackFrame * > & frames)292 StackFrameMIPS* StackwalkerMIPS::GetCallerByStackScan(
293     const vector<StackFrame*>& frames) {
294   const uint32_t kMaxFrameStackSize = 1024;
295   const uint32_t kMinArgsOnStack = 4;
296 
297   StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
298 
299   if (context_->context_flags & MD_CONTEXT_MIPS) {
300     uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP];
301     uint32_t caller_pc, caller_sp, caller_fp;
302 
303     // Return address cannot be obtained directly.
304     // Force stackwalking.
305 
306     // We cannot use frame pointer to get the return address.
307     // We'll scan the stack for a
308     // return address. This can happen if last_frame is executing code
309     // for a module for which we don't have symbols.
310     int count = kMaxFrameStackSize / sizeof(caller_pc);
311 
312     if (frames.size() > 1) {
313       // In case of mips32 ABI stack frame of a nonleaf function
314       // must have minimum stack frame assigned for 4 arguments (4 words).
315       // Move stack pointer for 4 words to avoid reporting non-existing frames
316       // for all frames except the topmost one.
317       // There is no way of knowing if topmost frame belongs to a leaf or
318       // a nonleaf function.
319       last_sp +=  kMinArgsOnStack * sizeof(caller_pc);
320       // Adjust 'count' so that return address is scanned only in limits
321       // of one stack frame.
322       count -= kMinArgsOnStack;
323     }
324 
325     do {
326       // Scanning for return address from stack pointer of the last frame.
327       if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, count)) {
328         // If we can't find an instruction pointer even with stack scanning,
329         // give up.
330         BPLOG(ERROR) << " ScanForReturnAddress failed ";
331         return NULL;
332       }
333       // Get $fp stored in the stack frame.
334       if (!memory_->GetMemoryAtAddress(caller_sp - sizeof(caller_pc),
335           &caller_fp)) {
336         BPLOG(INFO) << " GetMemoryAtAddress for fp failed " ;
337         return NULL;
338       }
339 
340       count = count - (caller_sp - last_sp) / sizeof(caller_pc);
341       // Now scan the next address in the stack.
342       last_sp = caller_sp + sizeof(caller_pc);
343     } while ((caller_fp - caller_sp >= kMaxFrameStackSize) && count > 0);
344 
345     if (!count) {
346       BPLOG(INFO) << " No frame found " ;
347       return NULL;
348     }
349 
350     // ScanForReturnAddress found a reasonable return address. Advance
351     // $sp to the location above the one where the return address was
352     // found.
353     caller_sp += sizeof(caller_pc);
354     // caller_pc is actually containing $ra value;
355     // $pc is two instructions before $ra,
356     // so the caller_pc needs to be decremented accordingly.
357     caller_pc -= 2 * sizeof(caller_pc);
358 
359     // Create a new stack frame (ownership will be transferred to the caller)
360     // and fill it in.
361     StackFrameMIPS* frame = new StackFrameMIPS();
362     frame->trust = StackFrame::FRAME_TRUST_SCAN;
363     frame->context = last_frame->context;
364     frame->context.epc = caller_pc;
365     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
366     frame->instruction = caller_pc;
367 
368     frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] = caller_sp;
369     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_SP;
370     frame->context.iregs[MD_CONTEXT_MIPS_REG_FP] = caller_fp;
371     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_FP;
372 
373     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] =
374         caller_pc + 2 * sizeof(caller_pc);
375     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
376 
377     return frame;
378   } else {
379     uint64_t last_sp = last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP];
380     uint64_t caller_pc, caller_sp, caller_fp;
381 
382     // Return address cannot be obtained directly.
383     // Force stackwalking.
384 
385     // We cannot use frame pointer to get the return address.
386     // We'll scan the stack for a
387     // return address. This can happen if last_frame is executing code
388     // for a module for which we don't have symbols.
389     int count = kMaxFrameStackSize / sizeof(caller_pc);
390 
391     do {
392       // Scanning for return address from stack pointer of the last frame.
393       if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, count)) {
394         // If we can't find an instruction pointer even with stack scanning,
395         // give up.
396         BPLOG(ERROR) << " ScanForReturnAddress failed ";
397         return NULL;
398       }
399       // Get $fp stored in the stack frame.
400       if (!memory_->GetMemoryAtAddress(caller_sp - sizeof(caller_pc),
401           &caller_fp)) {
402         BPLOG(INFO) << " GetMemoryAtAddress for fp failed " ;
403         return NULL;
404       }
405 
406       count = count - (caller_sp - last_sp) / sizeof(caller_pc);
407       // Now scan the next address in the stack.
408       last_sp = caller_sp + sizeof(caller_pc);
409     } while ((caller_fp - caller_sp >= kMaxFrameStackSize) && count > 0);
410 
411     if (!count) {
412       BPLOG(INFO) << " No frame found " ;
413       return NULL;
414     }
415 
416     // ScanForReturnAddress found a reasonable return address. Advance
417     // $sp to the location above the one where the return address was
418     // found.
419     caller_sp += sizeof(caller_pc);
420     // caller_pc is actually containing $ra value;
421     // $pc is two instructions before $ra,
422     // so the caller_pc needs to be decremented accordingly.
423     caller_pc -= 2 * sizeof(caller_pc);
424 
425     // Create a new stack frame (ownership will be transferred to the caller)
426     // and fill it in.
427     StackFrameMIPS* frame = new StackFrameMIPS();
428     frame->trust = StackFrame::FRAME_TRUST_SCAN;
429     frame->context = last_frame->context;
430     frame->context.epc = caller_pc;
431     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
432     frame->instruction = caller_pc;
433 
434     frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] = caller_sp;
435     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_SP;
436     frame->context.iregs[MD_CONTEXT_MIPS_REG_FP] = caller_fp;
437     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_FP;
438 
439     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] =
440         caller_pc + 2 * sizeof(caller_pc);
441     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
442 
443     return frame;
444   }
445 }
446 
447 }  // namespace google_breakpad
448 
449