xref: /aosp_15_r20/external/google-breakpad/src/processor/stackwalker_mips.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2013 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 // stackwalker_mips.cc: MIPS-specific stackwalker.
30 //
31 // See stackwalker_mips.h for documentation.
32 //
33 // Author: Tata Elxsi
34 
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>  // Must come first
37 #endif
38 
39 #include "common/scoped_ptr.h"
40 #include "google_breakpad/processor/call_stack.h"
41 #include "google_breakpad/processor/code_modules.h"
42 #include "google_breakpad/processor/memory_region.h"
43 #include "google_breakpad/processor/source_line_resolver_interface.h"
44 #include "google_breakpad/processor/stack_frame_cpu.h"
45 #include "processor/cfi_frame_info.h"
46 #include "processor/logging.h"
47 #include "processor/postfix_evaluator-inl.h"
48 #include "processor/stackwalker_mips.h"
49 #include "processor/windows_frame_info.h"
50 #include "google_breakpad/common/minidump_cpu_mips.h"
51 
52 namespace google_breakpad {
53 
StackwalkerMIPS(const SystemInfo * system_info,const MDRawContextMIPS * context,MemoryRegion * memory,const CodeModules * modules,StackFrameSymbolizer * resolver_helper)54 StackwalkerMIPS::StackwalkerMIPS(const SystemInfo* system_info,
55                                  const MDRawContextMIPS* context,
56                                  MemoryRegion* memory,
57                                  const CodeModules* modules,
58                                  StackFrameSymbolizer* resolver_helper)
59 : Stackwalker(system_info, memory, modules, resolver_helper),
60   context_(context) {
61   if (memory_) {
62     if (context_->context_flags & MD_CONTEXT_MIPS64 ) {
63       if (0xffffffffffffffff - memory_->GetBase() < memory_->GetSize() - 1) {
64         BPLOG(ERROR) << "Memory out of range for stackwalking mips64: "
65             << HexString(memory_->GetBase())
66             << "+"
67             << HexString(memory_->GetSize());
68         memory_ = NULL;
69       }
70     } else {
71       if (0xffffffff - memory_->GetBase() < memory_->GetSize() - 1) {
72         BPLOG(ERROR) << "Memory out of range for stackwalking mips32: "
73             << HexString(memory_->GetBase())
74             << "+"
75             << HexString(memory_->GetSize());
76         memory_ = NULL;
77       }
78     }
79   }
80 }
81 
GetContextFrame()82 StackFrame* StackwalkerMIPS::GetContextFrame() {
83   if (!context_) {
84     BPLOG(ERROR) << "Can't get context frame without context.";
85     return NULL;
86   }
87 
88   StackFrameMIPS* frame = new StackFrameMIPS();
89 
90   // The instruction pointer is stored directly in a register, so pull it
91   // straight out of the CPU context structure.
92   frame->context = *context_;
93   frame->context_validity = StackFrameMIPS::CONTEXT_VALID_ALL;
94   frame->trust = StackFrame::FRAME_TRUST_CONTEXT;
95   frame->instruction = frame->context.epc;
96 
97   return frame;
98 }
99 
100 // Register names for mips.
101 static const char* const kRegisterNames[] = {
102    "$zero", "$at", "$v0", "$v1", "$a0", "$a1", "$a2", "$a3", "$to", "$t1",
103    "$t2",   "$t3", "$t4", "$t5", "$t6", "$t7", "$s0", "$s1", "$s2", "$s3",
104    "$s4",   "$s5", "$s6", "$s7", "$t8", "$t9", "$k0", "$k1", "$gp", "$sp",
105    "$fp",   "$ra", NULL
106   // TODO(gordanac): add float point save registers
107 };
108 
GetCallerByCFIFrameInfo(const vector<StackFrame * > & frames,CFIFrameInfo * cfi_frame_info)109 StackFrameMIPS* StackwalkerMIPS::GetCallerByCFIFrameInfo(
110     const vector<StackFrame*>& frames,
111     CFIFrameInfo* cfi_frame_info) {
112   StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
113 
114   if (context_->context_flags & MD_CONTEXT_MIPS) {
115     uint32_t pc = 0;
116 
117     // Populate a dictionary with the valid register values in last_frame.
118     CFIFrameInfo::RegisterValueMap<uint32_t> callee_registers;
119     // Use the STACK CFI data to recover the caller's register values.
120     CFIFrameInfo::RegisterValueMap<uint32_t> caller_registers;
121 
122     for (int i = 0; kRegisterNames[i]; ++i) {
123       caller_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
124       callee_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
125     }
126 
127     if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
128         &caller_registers))  {
129       return NULL;
130     }
131 
132     CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator entry =
133         caller_registers.find(".cfa");
134 
135     if (entry != caller_registers.end()) {
136       caller_registers["$sp"] = entry->second;
137     }
138 
139     entry = caller_registers.find(".ra");
140     if (entry != caller_registers.end()) {
141       caller_registers["$ra"] = entry->second;
142       pc = entry->second - 2 * sizeof(pc);
143     }
144     caller_registers["$pc"] = pc;
145     // Construct a new stack frame given the values the CFI recovered.
146     scoped_ptr<StackFrameMIPS> frame(new StackFrameMIPS());
147 
148     for (int i = 0; kRegisterNames[i]; ++i) {
149       CFIFrameInfo::RegisterValueMap<uint32_t>::const_iterator caller_entry =
150           caller_registers.find(kRegisterNames[i]);
151 
152       if (caller_entry != caller_registers.end()) {
153         // The value of this register is recovered; fill the context with the
154         // value from caller_registers.
155         frame->context.iregs[i] = caller_entry->second;
156         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
157       } else if (((i >= INDEX_MIPS_REG_S0 && i <= INDEX_MIPS_REG_S7) ||
158           (i > INDEX_MIPS_REG_GP && i < INDEX_MIPS_REG_RA)) &&
159           (last_frame->context_validity &
160               StackFrameMIPS::RegisterValidFlag(i))) {
161         // If the STACK CFI data doesn't mention some callee-save register, and
162         // it is valid in the callee, assume the callee has not yet changed it.
163         // Calee-save registers according to the MIPS o32 ABI specification are:
164         // $s0 to $s7
165         // $sp, $s8
166         frame->context.iregs[i] = last_frame->context.iregs[i];
167         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
168       }
169     }
170 
171     frame->context.epc = caller_registers["$pc"];
172     frame->instruction = caller_registers["$pc"];
173     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
174 
175     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] = caller_registers["$ra"];
176     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
177 
178     frame->trust = StackFrame::FRAME_TRUST_CFI;
179 
180     return frame.release();
181   } else {
182     uint64_t pc = 0;
183 
184     // Populate a dictionary with the valid register values in last_frame.
185     CFIFrameInfo::RegisterValueMap<uint64_t> callee_registers;
186     // Use the STACK CFI data to recover the caller's register values.
187     CFIFrameInfo::RegisterValueMap<uint64_t> caller_registers;
188 
189     for (int i = 0; kRegisterNames[i]; ++i) {
190       caller_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
191       callee_registers[kRegisterNames[i]] = last_frame->context.iregs[i];
192     }
193 
194     if (!cfi_frame_info->FindCallerRegs(callee_registers, *memory_,
195         &caller_registers))  {
196       return NULL;
197     }
198 
199     CFIFrameInfo::RegisterValueMap<uint64_t>::const_iterator entry =
200         caller_registers.find(".cfa");
201 
202     if (entry != caller_registers.end()) {
203       caller_registers["$sp"] = entry->second;
204     }
205 
206     entry = caller_registers.find(".ra");
207     if (entry != caller_registers.end()) {
208       caller_registers["$ra"] = entry->second;
209       pc = entry->second - 2 * sizeof(pc);
210     }
211     caller_registers["$pc"] = pc;
212     // Construct a new stack frame given the values the CFI recovered.
213     scoped_ptr<StackFrameMIPS> frame(new StackFrameMIPS());
214 
215     for (int i = 0; kRegisterNames[i]; ++i) {
216       CFIFrameInfo::RegisterValueMap<uint64_t>::const_iterator caller_entry =
217           caller_registers.find(kRegisterNames[i]);
218 
219       if (caller_entry != caller_registers.end()) {
220         // The value of this register is recovered; fill the context with the
221         // value from caller_registers.
222         frame->context.iregs[i] = caller_entry->second;
223         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
224       } else if (((i >= INDEX_MIPS_REG_S0 && i <= INDEX_MIPS_REG_S7) ||
225           (i >= INDEX_MIPS_REG_GP && i < INDEX_MIPS_REG_RA)) &&
226           (last_frame->context_validity &
227               StackFrameMIPS::RegisterValidFlag(i))) {
228         // If the STACK CFI data doesn't mention some callee-save register, and
229         // it is valid in the callee, assume the callee has not yet changed it.
230         // Calee-save registers according to the MIPS o32 ABI specification are:
231         // $s0 to $s7
232         // $sp, $s8
233         frame->context.iregs[i] = last_frame->context.iregs[i];
234         frame->context_validity |= StackFrameMIPS::RegisterValidFlag(i);
235       }
236     }
237 
238     frame->context.epc = caller_registers["$pc"];
239     frame->instruction = caller_registers["$pc"];
240     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
241 
242     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] = caller_registers["$ra"];
243     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
244 
245     frame->trust = StackFrame::FRAME_TRUST_CFI;
246 
247     return frame.release();
248   }
249 }
250 
GetCallerFrame(const CallStack * stack,bool stack_scan_allowed)251 StackFrame* StackwalkerMIPS::GetCallerFrame(const CallStack* stack,
252                                             bool stack_scan_allowed) {
253   if (!memory_ || !stack) {
254     BPLOG(ERROR) << "Can't get caller frame without memory or stack";
255     return NULL;
256   }
257 
258   const vector<StackFrame*>& frames = *stack->frames();
259   StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
260   scoped_ptr<StackFrameMIPS> new_frame;
261 
262   // See if there is DWARF call frame information covering this address.
263   scoped_ptr<CFIFrameInfo> cfi_frame_info(
264     frame_symbolizer_->FindCFIFrameInfo(last_frame));
265   if (cfi_frame_info.get())
266     new_frame.reset(GetCallerByCFIFrameInfo(frames, cfi_frame_info.get()));
267 
268   // If caller frame is not found in CFI try analyzing the stack.
269   if (stack_scan_allowed && !new_frame.get()) {
270     new_frame.reset(GetCallerByStackScan(frames));
271   }
272 
273   // If nothing worked, tell the caller.
274   if (!new_frame.get()) {
275     return NULL;
276   }
277 
278   // Should we terminate the stack walk? (end-of-stack or broken invariant)
279   if (TerminateWalk(new_frame->context.epc,
280                     new_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP],
281                     last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP],
282                     /*first_unwind=*/last_frame->trust ==
283                         StackFrame::FRAME_TRUST_CONTEXT)) {
284     return NULL;
285   }
286 
287   return new_frame.release();
288 }
289 
GetCallerByStackScan(const vector<StackFrame * > & frames)290 StackFrameMIPS* StackwalkerMIPS::GetCallerByStackScan(
291     const vector<StackFrame*>& frames) {
292   const uint32_t kMaxFrameStackSize = 1024;
293   const uint32_t kMinArgsOnStack = 4;
294 
295   StackFrameMIPS* last_frame = static_cast<StackFrameMIPS*>(frames.back());
296 
297   if (context_->context_flags & MD_CONTEXT_MIPS) {
298     uint32_t last_sp = last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP];
299     uint32_t caller_pc, caller_sp, caller_fp;
300 
301     // Return address cannot be obtained directly.
302     // Force stackwalking.
303 
304     // We cannot use frame pointer to get the return address.
305     // We'll scan the stack for a
306     // return address. This can happen if last_frame is executing code
307     // for a module for which we don't have symbols.
308     int count = kMaxFrameStackSize / sizeof(caller_pc);
309 
310     if (frames.size() > 1) {
311       // In case of mips32 ABI stack frame of a nonleaf function
312       // must have minimum stack frame assigned for 4 arguments (4 words).
313       // Move stack pointer for 4 words to avoid reporting non-existing frames
314       // for all frames except the topmost one.
315       // There is no way of knowing if topmost frame belongs to a leaf or
316       // a nonleaf function.
317       last_sp +=  kMinArgsOnStack * sizeof(caller_pc);
318       // Adjust 'count' so that return address is scanned only in limits
319       // of one stack frame.
320       count -= kMinArgsOnStack;
321     }
322 
323     do {
324       // Scanning for return address from stack pointer of the last frame.
325       if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, count)) {
326         // If we can't find an instruction pointer even with stack scanning,
327         // give up.
328         BPLOG(ERROR) << " ScanForReturnAddress failed ";
329         return NULL;
330       }
331       // Get $fp stored in the stack frame.
332       if (!memory_->GetMemoryAtAddress(caller_sp - sizeof(caller_pc),
333           &caller_fp)) {
334         BPLOG(INFO) << " GetMemoryAtAddress for fp failed " ;
335         return NULL;
336       }
337 
338       count = count - (caller_sp - last_sp) / sizeof(caller_pc);
339       // Now scan the next address in the stack.
340       last_sp = caller_sp + sizeof(caller_pc);
341     } while ((caller_fp - caller_sp >= kMaxFrameStackSize) && count > 0);
342 
343     if (!count) {
344       BPLOG(INFO) << " No frame found " ;
345       return NULL;
346     }
347 
348     // ScanForReturnAddress found a reasonable return address. Advance
349     // $sp to the location above the one where the return address was
350     // found.
351     caller_sp += sizeof(caller_pc);
352     // caller_pc is actually containing $ra value;
353     // $pc is two instructions before $ra,
354     // so the caller_pc needs to be decremented accordingly.
355     caller_pc -= 2 * sizeof(caller_pc);
356 
357     // Create a new stack frame (ownership will be transferred to the caller)
358     // and fill it in.
359     StackFrameMIPS* frame = new StackFrameMIPS();
360     frame->trust = StackFrame::FRAME_TRUST_SCAN;
361     frame->context = last_frame->context;
362     frame->context.epc = caller_pc;
363     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
364     frame->instruction = caller_pc;
365 
366     frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] = caller_sp;
367     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_SP;
368     frame->context.iregs[MD_CONTEXT_MIPS_REG_FP] = caller_fp;
369     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_FP;
370 
371     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] =
372         caller_pc + 2 * sizeof(caller_pc);
373     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
374 
375     return frame;
376   } else {
377     uint64_t last_sp = last_frame->context.iregs[MD_CONTEXT_MIPS_REG_SP];
378     uint64_t caller_pc, caller_sp, caller_fp;
379 
380     // Return address cannot be obtained directly.
381     // Force stackwalking.
382 
383     // We cannot use frame pointer to get the return address.
384     // We'll scan the stack for a
385     // return address. This can happen if last_frame is executing code
386     // for a module for which we don't have symbols.
387     int count = kMaxFrameStackSize / sizeof(caller_pc);
388 
389     do {
390       // Scanning for return address from stack pointer of the last frame.
391       if (!ScanForReturnAddress(last_sp, &caller_sp, &caller_pc, count)) {
392         // If we can't find an instruction pointer even with stack scanning,
393         // give up.
394         BPLOG(ERROR) << " ScanForReturnAddress failed ";
395         return NULL;
396       }
397       // Get $fp stored in the stack frame.
398       if (!memory_->GetMemoryAtAddress(caller_sp - sizeof(caller_pc),
399           &caller_fp)) {
400         BPLOG(INFO) << " GetMemoryAtAddress for fp failed " ;
401         return NULL;
402       }
403 
404       count = count - (caller_sp - last_sp) / sizeof(caller_pc);
405       // Now scan the next address in the stack.
406       last_sp = caller_sp + sizeof(caller_pc);
407     } while ((caller_fp - caller_sp >= kMaxFrameStackSize) && count > 0);
408 
409     if (!count) {
410       BPLOG(INFO) << " No frame found " ;
411       return NULL;
412     }
413 
414     // ScanForReturnAddress found a reasonable return address. Advance
415     // $sp to the location above the one where the return address was
416     // found.
417     caller_sp += sizeof(caller_pc);
418     // caller_pc is actually containing $ra value;
419     // $pc is two instructions before $ra,
420     // so the caller_pc needs to be decremented accordingly.
421     caller_pc -= 2 * sizeof(caller_pc);
422 
423     // Create a new stack frame (ownership will be transferred to the caller)
424     // and fill it in.
425     StackFrameMIPS* frame = new StackFrameMIPS();
426     frame->trust = StackFrame::FRAME_TRUST_SCAN;
427     frame->context = last_frame->context;
428     frame->context.epc = caller_pc;
429     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_PC;
430     frame->instruction = caller_pc;
431 
432     frame->context.iregs[MD_CONTEXT_MIPS_REG_SP] = caller_sp;
433     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_SP;
434     frame->context.iregs[MD_CONTEXT_MIPS_REG_FP] = caller_fp;
435     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_FP;
436 
437     frame->context.iregs[MD_CONTEXT_MIPS_REG_RA] =
438         caller_pc + 2 * sizeof(caller_pc);
439     frame->context_validity |= StackFrameMIPS::CONTEXT_VALID_RA;
440 
441     return frame;
442   }
443 }
444 
445 }  // namespace google_breakpad
446 
447