1*6777b538SAndroid Build Coastguard Worker // Copyright 2019 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #include "base/profiler/native_unwinder_win.h"
6*6777b538SAndroid Build Coastguard Worker
7*6777b538SAndroid Build Coastguard Worker #include <winnt.h>
8*6777b538SAndroid Build Coastguard Worker
9*6777b538SAndroid Build Coastguard Worker #include "base/check_op.h"
10*6777b538SAndroid Build Coastguard Worker #include "base/notreached.h"
11*6777b538SAndroid Build Coastguard Worker #include "base/profiler/win32_stack_frame_unwinder.h"
12*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h"
13*6777b538SAndroid Build Coastguard Worker
14*6777b538SAndroid Build Coastguard Worker namespace base {
15*6777b538SAndroid Build Coastguard Worker
CanUnwindFrom(const Frame & current_frame) const16*6777b538SAndroid Build Coastguard Worker bool NativeUnwinderWin::CanUnwindFrom(const Frame& current_frame) const {
17*6777b538SAndroid Build Coastguard Worker return current_frame.module && current_frame.module->IsNative();
18*6777b538SAndroid Build Coastguard Worker }
19*6777b538SAndroid Build Coastguard Worker
20*6777b538SAndroid Build Coastguard Worker // Attempts to unwind the frame represented by the context values. If
21*6777b538SAndroid Build Coastguard Worker // successful appends frames onto the stack and returns true. Otherwise
22*6777b538SAndroid Build Coastguard Worker // returns false.
TryUnwind(RegisterContext * thread_context,uintptr_t stack_top,std::vector<Frame> * stack)23*6777b538SAndroid Build Coastguard Worker UnwindResult NativeUnwinderWin::TryUnwind(RegisterContext* thread_context,
24*6777b538SAndroid Build Coastguard Worker uintptr_t stack_top,
25*6777b538SAndroid Build Coastguard Worker std::vector<Frame>* stack) {
26*6777b538SAndroid Build Coastguard Worker // We expect the frame corresponding to the |thread_context| register state to
27*6777b538SAndroid Build Coastguard Worker // exist within |stack|.
28*6777b538SAndroid Build Coastguard Worker DCHECK_GT(stack->size(), 0u);
29*6777b538SAndroid Build Coastguard Worker
30*6777b538SAndroid Build Coastguard Worker Win32StackFrameUnwinder frame_unwinder;
31*6777b538SAndroid Build Coastguard Worker for (;;) {
32*6777b538SAndroid Build Coastguard Worker if (!stack->back().module) {
33*6777b538SAndroid Build Coastguard Worker // There's no loaded module corresponding to the current frame. This can
34*6777b538SAndroid Build Coastguard Worker // be due to executing code not in a module (e.g. runtime-generated code
35*6777b538SAndroid Build Coastguard Worker // associated with third-party injected DLLs) or the module having been
36*6777b538SAndroid Build Coastguard Worker // unloaded since we recorded the stack. In the latter case the function
37*6777b538SAndroid Build Coastguard Worker // unwind information was part of the unloaded module, so it's not
38*6777b538SAndroid Build Coastguard Worker // possible to unwind further.
39*6777b538SAndroid Build Coastguard Worker //
40*6777b538SAndroid Build Coastguard Worker // NB: if a module was found it's still theoretically possible for the
41*6777b538SAndroid Build Coastguard Worker // detected module module to be different than the one that was loaded
42*6777b538SAndroid Build Coastguard Worker // when the stack was copied, if the module was unloaded and a different
43*6777b538SAndroid Build Coastguard Worker // module loaded in overlapping memory. This likely would cause a crash
44*6777b538SAndroid Build Coastguard Worker // but has not been observed in practice.
45*6777b538SAndroid Build Coastguard Worker return UnwindResult::kAborted;
46*6777b538SAndroid Build Coastguard Worker }
47*6777b538SAndroid Build Coastguard Worker
48*6777b538SAndroid Build Coastguard Worker if (!stack->back().module->IsNative()) {
49*6777b538SAndroid Build Coastguard Worker // This is a non-native module associated with the auxiliary unwinder
50*6777b538SAndroid Build Coastguard Worker // (e.g. corresponding to a frame in V8 generated code). Report as
51*6777b538SAndroid Build Coastguard Worker // UNRECOGNIZED_FRAME to allow that unwinder to unwind the frame.
52*6777b538SAndroid Build Coastguard Worker return UnwindResult::kUnrecognizedFrame;
53*6777b538SAndroid Build Coastguard Worker }
54*6777b538SAndroid Build Coastguard Worker
55*6777b538SAndroid Build Coastguard Worker uintptr_t prev_stack_pointer = RegisterContextStackPointer(thread_context);
56*6777b538SAndroid Build Coastguard Worker if (!frame_unwinder.TryUnwind(stack->size() == 1u, thread_context,
57*6777b538SAndroid Build Coastguard Worker stack->back().module)) {
58*6777b538SAndroid Build Coastguard Worker return UnwindResult::kAborted;
59*6777b538SAndroid Build Coastguard Worker }
60*6777b538SAndroid Build Coastguard Worker
61*6777b538SAndroid Build Coastguard Worker if (RegisterContextInstructionPointer(thread_context) == 0)
62*6777b538SAndroid Build Coastguard Worker return UnwindResult::kCompleted;
63*6777b538SAndroid Build Coastguard Worker
64*6777b538SAndroid Build Coastguard Worker // Exclusive range of expected stack pointer values after the unwind.
65*6777b538SAndroid Build Coastguard Worker struct {
66*6777b538SAndroid Build Coastguard Worker uintptr_t start;
67*6777b538SAndroid Build Coastguard Worker uintptr_t end;
68*6777b538SAndroid Build Coastguard Worker } expected_stack_pointer_range = {prev_stack_pointer, stack_top};
69*6777b538SAndroid Build Coastguard Worker
70*6777b538SAndroid Build Coastguard Worker // Abort if the unwind produced an invalid stack pointer.
71*6777b538SAndroid Build Coastguard Worker #if defined(ARCH_CPU_ARM64)
72*6777b538SAndroid Build Coastguard Worker // Leaf frames on Arm can re-use the stack pointer, so they can validly have
73*6777b538SAndroid Build Coastguard Worker // the same stack pointer as the previous frame.
74*6777b538SAndroid Build Coastguard Worker if (stack->size() == 1u) {
75*6777b538SAndroid Build Coastguard Worker expected_stack_pointer_range.start--;
76*6777b538SAndroid Build Coastguard Worker }
77*6777b538SAndroid Build Coastguard Worker #endif
78*6777b538SAndroid Build Coastguard Worker if (RegisterContextStackPointer(thread_context) <=
79*6777b538SAndroid Build Coastguard Worker expected_stack_pointer_range.start ||
80*6777b538SAndroid Build Coastguard Worker RegisterContextStackPointer(thread_context) >=
81*6777b538SAndroid Build Coastguard Worker expected_stack_pointer_range.end) {
82*6777b538SAndroid Build Coastguard Worker return UnwindResult::kAborted;
83*6777b538SAndroid Build Coastguard Worker }
84*6777b538SAndroid Build Coastguard Worker
85*6777b538SAndroid Build Coastguard Worker // Record the frame to which we just unwound.
86*6777b538SAndroid Build Coastguard Worker stack->emplace_back(RegisterContextInstructionPointer(thread_context),
87*6777b538SAndroid Build Coastguard Worker module_cache()->GetModuleForAddress(
88*6777b538SAndroid Build Coastguard Worker RegisterContextInstructionPointer(thread_context)));
89*6777b538SAndroid Build Coastguard Worker }
90*6777b538SAndroid Build Coastguard Worker
91*6777b538SAndroid Build Coastguard Worker NOTREACHED();
92*6777b538SAndroid Build Coastguard Worker return UnwindResult::kCompleted;
93*6777b538SAndroid Build Coastguard Worker }
94*6777b538SAndroid Build Coastguard Worker
95*6777b538SAndroid Build Coastguard Worker } // namespace base
96