| 6 |
| 7 | #include "base/numerics/checked_math.h" |
| 8 | #include "base/profiler/module_cache.h" |
| 9 | #include "base/profiler/native_unwinder.h" |
| 10 | #include "build/build_config.h" |
| 11 |
| 12 | namespace base { |
| 13 |
| 14 | ChromeUnwinderAndroid::ChromeUnwinderAndroid( |
| 15 | const ArmCFITable* cfi_table, |
| 16 | const ModuleCache::Module* chrome_module) |
| 17 | : cfi_table_(cfi_table), chrome_module_(chrome_module) { |
| 18 | DCHECK(cfi_table_); |
| 19 | DCHECK(chrome_module_); |
| 20 | } |
| 21 |
| 22 | ChromeUnwinderAndroid::~ChromeUnwinderAndroid() = default; |
| 23 |
| 24 | bool ChromeUnwinderAndroid::CanUnwindFrom(const Frame& current_frame) const { |
| 25 | return current_frame.module == chrome_module_; |
| 26 | } |
| 27 |
| 28 | UnwindResult ChromeUnwinderAndroid::TryUnwind(RegisterContext* thread_context, |
| 29 | uintptr_t stack_top, |
| 30 | ModuleCache* module_cache, |
| 31 | std::vector<Frame>* stack) const { |
| 32 | DCHECK(CanUnwindFrom(stack->back())); |
| 33 | do { |
| 34 | const ModuleCache::Module* module = stack->back().module; |
| 35 | uintptr_t pc = RegisterContextInstructionPointer(thread_context); |
| 36 | DCHECK_GE(pc, module->GetBaseAddress()); |
| 37 | uintptr_t func_addr = pc - module->GetBaseAddress(); |
| 38 |
| 39 | auto entry = cfi_table_->FindEntryForAddress(func_addr); |
| 40 | if (!entry) |
| 41 | return UnwindResult::ABORTED; |
| 42 | if (!Step(thread_context, stack_top, *entry)) |
| 43 | return UnwindResult::ABORTED; |
| 44 | stack->emplace_back(RegisterContextInstructionPointer(thread_context), |
| 45 | module_cache->GetModuleForAddress( |
| 46 | RegisterContextInstructionPointer(thread_context))); |
| 47 | } while (CanUnwindFrom(stack->back())); |
| 48 | return UnwindResult::UNRECOGNIZED_FRAME; |
| 49 | } |
| 50 |
| 51 | // static |
| 52 | bool ChromeUnwinderAndroid::Step(RegisterContext* thread_context, |
| 53 | uintptr_t stack_top, |
| 54 | const ArmCFITable::FrameEntry& entry) { |
| 55 | CHECK_NE(RegisterContextStackPointer(thread_context), 0U); |
| 56 | CHECK_LE(RegisterContextStackPointer(thread_context), stack_top); |
| 57 | if (entry.cfa_offset == 0) { |
| 58 | uintptr_t pc = RegisterContextInstructionPointer(thread_context); |
| 59 | uintptr_t return_address = static_cast<uintptr_t>(thread_context->arm_lr); |
| 60 |
| 61 | if (pc == return_address) |
| 62 | return false; |
| 63 |
| 64 | RegisterContextInstructionPointer(thread_context) = return_address; |
| 65 | } else { |
| 66 | // The rules for unwinding using the CFI information are: |
| 67 | // SP_prev = SP_cur + cfa_offset and |
| 68 | // PC_prev = * (SP_prev - ra_offset). |
| 69 | auto new_sp = |
| 70 | CheckedNumeric<uintptr_t>(RegisterContextStackPointer(thread_context)) + |
| 71 | CheckedNumeric<uint16_t>(entry.cfa_offset); |
| 72 | if (!new_sp.AssignIfValid(&RegisterContextStackPointer(thread_context)) || |
| 73 | RegisterContextStackPointer(thread_context) >= stack_top) { |
| 74 | return false; |
| 75 | } |
| 76 |
| 77 | if (entry.ra_offset > entry.cfa_offset) |
| 78 | return false; |
| 79 |
| 80 | // Underflow is prevented because |ra_offset| <= |cfa_offset|. |
| 81 | uintptr_t ip_address = (new_sp - CheckedNumeric<uint16_t>(entry.ra_offset)) |
| 82 | .ValueOrDie<uintptr_t>(); |
| 83 | RegisterContextInstructionPointer(thread_context) = |
| 84 | *reinterpret_cast<uintptr_t*>(ip_address); |
| 85 | } |
| 86 | return true; |
| 87 | } |
| 88 |
| 89 | } // namespace base |
| 90 |