| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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 #ifndef CRASHPAD_UTIL_MACH_EXCEPTION_PORTS_H_ |
| 16 #define CRASHPAD_UTIL_MACH_EXCEPTION_PORTS_H_ |
| 17 |
| 18 #include <mach/mach.h> |
| 19 |
| 20 #include <vector> |
| 21 |
| 22 #include "base/basictypes.h" |
| 23 |
| 24 namespace crashpad { |
| 25 |
| 26 //! \brief A better interface to `*_get_exception_ports()` and |
| 27 //! `*_set_exception_ports()`. |
| 28 //! |
| 29 //! The same generic interface can be used to operate on host, task, and thread |
| 30 //! exception ports. The “get” interface is superior to the system’s native |
| 31 //! interface because it keeps related data about a single exception handler |
| 32 //! together in one struct, rather than separating it into four parallel arrays. |
| 33 class ExceptionPorts { |
| 34 public: |
| 35 //! \brief Various entities which can have their own exception ports set. |
| 36 enum TargetType { |
| 37 //! \brief The host exception target. |
| 38 //! |
| 39 //! `host_get_exception_ports()` and `host_set_exception_ports()` will be |
| 40 //! used. If no target port is explicitly provided, `mach_host_self()` will |
| 41 //! be used as the target port. `mach_host_self()` is the only target port |
| 42 //! for this type that is expected to function properly. |
| 43 //! |
| 44 //! \note Operations on this target type are not expected to succeed as |
| 45 //! non-root, because `mach_host_self()` doesn’t return the privileged |
| 46 //! `host_priv` port to non-root users, and this is the target port |
| 47 //! that’s required for `host_get_exception_ports()` and |
| 48 //! `host_set_exception_ports()`. |
| 49 kTargetTypeHost = 0, |
| 50 |
| 51 //! \brief A task exception target. |
| 52 //! |
| 53 //! `task_get_exception_ports()` and `task_set_exception_ports()` will be |
| 54 //! used. If no target port is explicitly provided, `mach_task_self()` will |
| 55 //! be used as the target port. |
| 56 kTargetTypeTask, |
| 57 |
| 58 //! \brief A thread exception target. |
| 59 //! |
| 60 //! `thread_get_exception_ports()` and `thread_set_exception_ports()` will |
| 61 //! be used. If no target port is explicitly provided, `mach_thread_self()` |
| 62 //! will be used as the target port. |
| 63 kTargetTypeThread, |
| 64 }; |
| 65 |
| 66 //! \brief Information about a registered exception handler. |
| 67 struct ExceptionHandler { |
| 68 //! \brief A mask specifying the exception types to direct to \a port, |
| 69 //! containing `EXC_MASK_*` values. |
| 70 exception_mask_t mask; |
| 71 |
| 72 //! \brief A send right to a Mach port that will handle exceptions of the |
| 73 //! types indicated in \a mask. |
| 74 exception_handler_t port; |
| 75 |
| 76 //! \brief The “behavior” that the exception handler at \a port implements: |
| 77 //! `EXCEPTION_DEFAULT`, `EXCEPTION_STATE`, or |
| 78 //! `EXCEPTION_STATE_IDENTITY`, possibly combined with |
| 79 //! `MACH_EXCEPTION_CODES`. |
| 80 exception_behavior_t behavior; |
| 81 |
| 82 //! \brief The thread state flavor that the exception handler at \a port |
| 83 //! will receive and possibly modify. This member has no effect for \a |
| 84 //! \a behavior values that indicate a “default” behavior. |
| 85 thread_state_flavor_t flavor; |
| 86 }; |
| 87 |
| 88 //! \brief Wraps `std::vector<ExceptionHandler>`, providing proper cleanup of |
| 89 //! the send rights contained in each element’s ExceptionHandler::port. |
| 90 //! |
| 91 //! Upon destruction or clear(), an object of this class will deallocate all |
| 92 //! send rights it contains. Otherwise, it is an interface-compatible drop-in |
| 93 //! replacement for `std::vector<ExceptionHandler>`. Note that non-`const` |
| 94 //! mutators are not provided to avoid accidental Mach right leaks. |
| 95 class ExceptionHandlerVector { |
| 96 public: |
| 97 using VectorType = std::vector<ExceptionHandler>; |
| 98 |
| 99 ExceptionHandlerVector(); |
| 100 ~ExceptionHandlerVector(); |
| 101 |
| 102 VectorType::const_iterator begin() const { return vector_.begin(); } |
| 103 VectorType::const_iterator end() const { return vector_.end(); } |
| 104 VectorType::size_type size() const { return vector_.size(); } |
| 105 bool empty() const { return vector_.empty(); } |
| 106 VectorType::const_reference operator[](VectorType::size_type index) const { |
| 107 return vector_[index]; |
| 108 } |
| 109 void push_back(VectorType::value_type& value) { vector_.push_back(value); } |
| 110 void clear(); |
| 111 |
| 112 private: |
| 113 void Deallocate(); |
| 114 |
| 115 VectorType vector_; |
| 116 |
| 117 DISALLOW_COPY_AND_ASSIGN(ExceptionHandlerVector); |
| 118 }; |
| 119 |
| 120 //! \brief Constructs an interface object to get or set exception ports on a |
| 121 //! host, task, or thread port. |
| 122 //! |
| 123 //! \param[in] target_type The type of target on which the exception ports are |
| 124 //! to be get or set: #kTargetTypeHost, #kTargetTypeTask, or or |
| 125 //! #kTargetTypeThread. The correct functions for |
| 126 //! `*_get_exception_ports()` and `*_set_exception_ports()` will be used. |
| 127 //! \param[in] target_port The target on which to call |
| 128 //! `*_get_exception_ports()` or `*_set_exception_ports()`. The target |
| 129 //! port must be a send right to a port of the type specified in \a |
| 130 //! target_type. In this case, ownership of \a target_port is not given to |
| 131 //! the new ExceptionPorts object. \a target_port may also be |
| 132 //! `HOST_NULL`, `TASK_NULL`, or `THREAD_NULL`, in which case |
| 133 //! `mach_host_self()`, `mach_task_self()`, or `mach_thread_self()` will |
| 134 //! be used as the target port depending on the value of \a target_type. |
| 135 //! In this case, ownership of the target port will be managed |
| 136 //! appropriately for \a target_type. |
| 137 ExceptionPorts(TargetType target_type, mach_port_t target_port); |
| 138 |
| 139 ~ExceptionPorts(); |
| 140 |
| 141 //! \brief Calls `*_get_exception_ports()` on the target. |
| 142 //! |
| 143 //! \param[in] mask The exception mask, containing the `EXC_MASK_*` values to |
| 144 //! be looked up and returned in \a handlers. |
| 145 //! \param[out] handlers The exception handlers registered for \a target_port |
| 146 //! to handle exceptions indicated in \a mask. If no execption port is |
| 147 //! registered for a bit in \a mask, \a handlers will not contain an entry |
| 148 //! corresponding to that bit. This is a departure from the |
| 149 //! `*_get_exception_ports()` functions, which may return a handler whose |
| 150 //! port is set to `EXCEPTION_PORT_NULL` in this case. On failure, this |
| 151 //! argument is untouched. |
| 152 //! |
| 153 //! \return `true` if `*_get_exception_ports()` returned `KERN_SUCCESS`, with |
| 154 //! \a handlers set appropriately. `false` otherwise, with an appropriate |
| 155 //! message logged. |
| 156 bool GetExceptionPorts(exception_mask_t mask, |
| 157 ExceptionHandlerVector* handlers) const; |
| 158 |
| 159 //! \brief Calls `*_set_exception_ports()` on the target. |
| 160 //! |
| 161 //! \param[in] mask A mask specifying the exception types to direct to \a |
| 162 //! port, containing `EXC_MASK_*` values. |
| 163 //! \param[in] port A send right to a Mach port that will handle exceptions |
| 164 //! sustained by \a target_port of the types indicated in \a mask. The |
| 165 //! send right is copied, not consumed, by this call. |
| 166 //! \param[in] behavior The “behavior” that the exception handler at \a port |
| 167 //! implements: `EXCEPTION_DEFAULT`, `EXCEPTION_STATE`, or |
| 168 //! `EXCEPTION_STATE_IDENTITY`, possibly combined with |
| 169 //! `MACH_EXCEPTION_CODES`. |
| 170 //! \param[in] flavor The thread state flavor that the exception handler at \a |
| 171 //! port expects to receive and possibly modify. This argument has no |
| 172 //! effect for \a behavior values that indicate a “default” behavior. |
| 173 //! |
| 174 //! \return `true` if `*_set_exception_ports()` returned `KERN_SUCCESS`. |
| 175 //! `false` otherwise, with an appropriate message logged. |
| 176 bool SetExceptionPort(exception_mask_t mask, |
| 177 exception_handler_t port, |
| 178 exception_behavior_t behavior, |
| 179 thread_state_flavor_t flavor) const; |
| 180 |
| 181 //! \brief Returns a string identifying the target type. |
| 182 //! |
| 183 //! \return `"host"`, `"task"`, or `"thread"`, as appropriate. |
| 184 const char* TargetTypeName() const; |
| 185 |
| 186 private: |
| 187 using GetExceptionPortsType = kern_return_t(*)(mach_port_t, |
| 188 exception_mask_t, |
| 189 exception_mask_array_t, |
| 190 mach_msg_type_number_t*, |
| 191 exception_handler_array_t, |
| 192 exception_behavior_array_t, |
| 193 exception_flavor_array_t); |
| 194 |
| 195 using SetExceptionPortsType = kern_return_t(*)(mach_port_t, |
| 196 exception_mask_t, |
| 197 exception_handler_t, |
| 198 exception_behavior_t, |
| 199 thread_state_flavor_t); |
| 200 |
| 201 GetExceptionPortsType get_exception_ports_; |
| 202 SetExceptionPortsType set_exception_ports_; |
| 203 const char* target_name_; |
| 204 mach_port_t target_port_; |
| 205 |
| 206 // If true, target_port_ will be deallocated in the destructor. This will |
| 207 // always be false when the user provides a non-null target_port to the |
| 208 // constructor. It will also be false when target_type is kTargetTypeTask, |
| 209 // even with a TASK_NULL target_port, because it is incorrect to deallocate |
| 210 // the result of mach_task_self(). |
| 211 bool dealloc_target_port_; |
| 212 |
| 213 DISALLOW_COPY_AND_ASSIGN(ExceptionPorts); |
| 214 }; |
| 215 |
| 216 } // namespace crashpad |
| 217 |
| 218 #endif // CRASHPAD_UTIL_MACH_EXCEPTION_PORTS_H_ |
| OLD | NEW |