1 // Copyright 2018 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "snapshot/fuchsia/exception_snapshot_fuchsia.h"
16 
17 #include "base/numerics/safe_conversions.h"
18 #include "snapshot/fuchsia/cpu_context_fuchsia.h"
19 #include "snapshot/fuchsia/process_reader_fuchsia.h"
20 
21 namespace crashpad {
22 namespace internal {
23 
24 ExceptionSnapshotFuchsia::ExceptionSnapshotFuchsia() = default;
25 ExceptionSnapshotFuchsia::~ExceptionSnapshotFuchsia() = default;
26 
Initialize(ProcessReaderFuchsia * process_reader,zx_koid_t thread_id,const zx_exception_report_t & exception_report)27 bool ExceptionSnapshotFuchsia::Initialize(
28     ProcessReaderFuchsia* process_reader,
29     zx_koid_t thread_id,
30     const zx_exception_report_t& exception_report) {
31   INITIALIZATION_STATE_SET_INITIALIZING(initialized_);
32 
33   exception_ = exception_report.header.type;
34   thread_id_ = thread_id;
35 
36   // TODO(scottmg): Not sure whether these values for exception_info_ are
37   // helpful or correct. Other values in the structures are stored below into
38   // Codes() in case they are useful.
39 #if defined(ARCH_CPU_X86_64)
40   DCHECK(base::IsValueInRangeForNumericType<uint32_t>(
41       exception_report.context.arch.u.x86_64.err_code));
42   exception_info_ = exception_report.context.arch.u.x86_64.err_code;
43 #elif defined(ARCH_CPU_ARM64)
44   exception_info_ = exception_report.context.arch.u.arm_64.esr;
45 #endif
46 
47   codes_.push_back(exception_);
48   codes_.push_back(exception_info_);
49 
50 #if defined(ARCH_CPU_X86_64)
51   codes_.push_back(exception_report.context.arch.u.x86_64.vector);
52   codes_.push_back(exception_report.context.arch.u.x86_64.cr2);
53 #elif defined(ARCH_CPU_ARM64)
54   codes_.push_back(exception_report.context.arch.u.arm_64.far);
55 #endif
56 
57   const auto threads = process_reader->Threads();
58   const auto& t =
59       std::find_if(threads.begin(),
60                    threads.end(),
61                    [thread_id](const ProcessReaderFuchsia::Thread& thread) {
62                      return thread.id == thread_id;
63                    });
64   if (t == threads.end()) {
65     // If no threads have been read, then context_ can't be initalized, and the
66     // exception snapshot can't be considered initialized_.
67     return false;
68   }
69 
70 #if defined(ARCH_CPU_X86_64)
71   context_.architecture = kCPUArchitectureX86_64;
72   context_.x86_64 = &context_arch_;
73   // TODO(fxbug.dev/5496): Add float context once saved in |t|.
74   InitializeCPUContextX86_64_NoFloatingPoint(t->general_registers,
75                                              context_.x86_64);
76 #elif defined(ARCH_CPU_ARM64)
77   context_.architecture = kCPUArchitectureARM64;
78   context_.arm64 = &context_arch_;
79   InitializeCPUContextARM64(
80       t->general_registers, t->vector_registers, context_.arm64);
81 #else
82 #error Port.
83 #endif
84 
85   if (context_.InstructionPointer() != 0 &&
86       (exception_ == ZX_EXCP_UNDEFINED_INSTRUCTION ||
87        exception_ == ZX_EXCP_SW_BREAKPOINT ||
88        exception_ == ZX_EXCP_HW_BREAKPOINT)) {
89     exception_address_ = context_.InstructionPointer();
90   } else {
91 #if defined(ARCH_CPU_X86_64)
92     exception_address_ = exception_report.context.arch.u.x86_64.cr2;
93 #elif defined(ARCH_CPU_ARM64)
94     exception_address_ = exception_report.context.arch.u.arm_64.far;
95 #else
96 #error Port.
97 #endif
98   }
99 
100   INITIALIZATION_STATE_SET_VALID(initialized_);
101   return true;
102 }
103 
Context() const104 const CPUContext* ExceptionSnapshotFuchsia::Context() const {
105   INITIALIZATION_STATE_DCHECK_VALID(initialized_);
106   return &context_;
107 }
108 
ThreadID() const109 uint64_t ExceptionSnapshotFuchsia::ThreadID() const {
110   INITIALIZATION_STATE_DCHECK_VALID(initialized_);
111   return thread_id_;
112 }
113 
Exception() const114 uint32_t ExceptionSnapshotFuchsia::Exception() const {
115   INITIALIZATION_STATE_DCHECK_VALID(initialized_);
116   return exception_;
117 }
118 
ExceptionInfo() const119 uint32_t ExceptionSnapshotFuchsia::ExceptionInfo() const {
120   INITIALIZATION_STATE_DCHECK_VALID(initialized_);
121   return exception_info_;
122 }
123 
ExceptionAddress() const124 uint64_t ExceptionSnapshotFuchsia::ExceptionAddress() const {
125   INITIALIZATION_STATE_DCHECK_VALID(initialized_);
126   return exception_address_;
127 }
128 
Codes() const129 const std::vector<uint64_t>& ExceptionSnapshotFuchsia::Codes() const {
130   INITIALIZATION_STATE_DCHECK_VALID(initialized_);
131   return codes_;
132 }
133 
ExtraMemory() const134 std::vector<const MemorySnapshot*> ExceptionSnapshotFuchsia::ExtraMemory()
135     const {
136   INITIALIZATION_STATE_DCHECK_VALID(initialized_);
137   return std::vector<const MemorySnapshot*>();
138 }
139 
140 }  // namespace internal
141 }  // namespace crashpad
142