simulate_crash_mac.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "client/simulate_crash_mac.h"
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include "base/logging.h"
  18. #include "base/mac/mach_logging.h"
  19. #include "base/mac/scoped_mach_port.h"
  20. #include "base/macros.h"
  21. #include "base/strings/stringprintf.h"
  22. #include "build/build_config.h"
  23. #include "util/mach/exc_client_variants.h"
  24. #include "util/mach/exception_behaviors.h"
  25. #include "util/mach/exception_ports.h"
  26. #include "util/mach/mach_extensions.h"
  27. #include "util/misc/implicit_cast.h"
  28. namespace crashpad {
  29. namespace {
  30. //! \brief Sends an exception message to an exception port in accordance with
  31. //! the behavior and thread state flavor it’s registered to receive.
  32. //!
  33. //! \param[in] thread, task, exception, code, code_count These parameters will
  34. //! be passed to the exception handler as appropriate.
  35. //! \param[in] cpu_context The value to use for the thread state, if \a behavior
  36. //! indicates that the handler should receive a thread state and if the
  37. //! supplied thread state matches or can be converted to \a flavor. If \a
  38. //! behavior requires a thread state but this argument cannot be converted
  39. //! to match \a flavor, `thread_get_state()` will be called to obtain a
  40. //! suitable thread state value.
  41. //! \param[in] handler The Mach exception handler to deliver the exception to.
  42. //! \param[in] set_state If `true` and \a behavior indicates that the handler
  43. //! should receive and return a thread state, a new thread state will be set
  44. //! by `thread_set_state()` upon successful completion of the exception
  45. //! handler. If `false`, this will be suppressed, even when \a behavior
  46. //! indicates that the handler receives and returns a thread state.
  47. //!
  48. //! \return `true` if the exception was delivered to the handler and the handler
  49. //! indicated success. `false` otherwise, with a warning message logged.
  50. bool DeliverException(thread_t thread,
  51. task_t task,
  52. exception_type_t exception,
  53. const mach_exception_data_t code,
  54. mach_msg_type_number_t code_count,
  55. const NativeCPUContext& cpu_context,
  56. const ExceptionPorts::ExceptionHandler& handler,
  57. bool set_state) {
  58. kern_return_t kr;
  59. bool handler_wants_state = ExceptionBehaviorHasState(handler.behavior);
  60. if (!handler_wants_state) {
  61. // Regardless of the passed-in value of |set_state|, if the handler won’t be
  62. // dealing with any state at all, no state should be set.
  63. set_state = false;
  64. }
  65. // old_state is only used if the context already captured doesn’t match (or
  66. // can’t be converted to) what’s registered for the handler.
  67. thread_state_data_t old_state;
  68. thread_state_flavor_t flavor = handler.flavor;
  69. ConstThreadState state;
  70. mach_msg_type_number_t state_count;
  71. switch (flavor) {
  72. #if defined(ARCH_CPU_X86_FAMILY)
  73. case x86_THREAD_STATE:
  74. state = reinterpret_cast<ConstThreadState>(&cpu_context);
  75. state_count = x86_THREAD_STATE_COUNT;
  76. break;
  77. #if defined(ARCH_CPU_X86)
  78. case x86_THREAD_STATE32:
  79. state = reinterpret_cast<ConstThreadState>(&cpu_context.uts.ts32);
  80. state_count = cpu_context.tsh.count;
  81. break;
  82. #elif defined(ARCH_CPU_X86_64)
  83. case x86_THREAD_STATE64:
  84. state = reinterpret_cast<ConstThreadState>(&cpu_context.uts.ts64);
  85. state_count = cpu_context.tsh.count;
  86. break;
  87. #endif
  88. #else
  89. #error Port to your CPU architecture
  90. #endif
  91. case THREAD_STATE_NONE:
  92. // This is only acceptable if the handler doesn’t have one of the “state”
  93. // behaviors. Otherwise, if the kernel were attempting to send an
  94. // exception message to this port, it would call thread_getstatus() (known
  95. // outside the kernel as thread_get_state()) which would fail because
  96. // THREAD_STATE_NONE is not a valid state to get. See 10.9.5
  97. // xnu-2422.115.4/osfmk/kern/exception.c exception_deliver() and
  98. // xnu-2422.115.4/osfmk/i386/pcb.c machine_thread_get_state().
  99. if (!handler_wants_state) {
  100. state = nullptr;
  101. state_count = 0;
  102. break;
  103. }
  104. LOG(WARNING) << "exception handler has unexpected state flavor" << flavor;
  105. return false;
  106. default:
  107. if (!handler_wants_state) {
  108. // Don’t bother getting any thread state if the handler’s not actually
  109. // going to use it.
  110. state = nullptr;
  111. state_count = 0;
  112. } else {
  113. state = old_state;
  114. state_count = THREAD_STATE_MAX;
  115. kr = thread_get_state(thread, flavor, old_state, &state_count);
  116. if (kr != KERN_SUCCESS) {
  117. MACH_LOG(WARNING, kr) << "thread_get_state";
  118. return false;
  119. }
  120. }
  121. break;
  122. }
  123. // new_state is supposed to be an out parameter only, but in case the handler
  124. // doesn't touch it, make sure it's initialized to a valid thread state.
  125. // Otherwise, the thread_set_state() call below would set a garbage thread
  126. // state.
  127. thread_state_data_t new_state;
  128. size_t state_size =
  129. sizeof(natural_t) *
  130. std::min(state_count, implicit_cast<unsigned int>(THREAD_STATE_MAX));
  131. memcpy(new_state, state, state_size);
  132. mach_msg_type_number_t new_state_count = THREAD_STATE_MAX;
  133. kr = UniversalExceptionRaise(handler.behavior,
  134. handler.port,
  135. thread,
  136. task,
  137. exception,
  138. code,
  139. code_count,
  140. &flavor,
  141. state,
  142. state_count,
  143. new_state,
  144. &new_state_count);
  145. // The kernel treats a return value of MACH_RCV_PORT_DIED as successful,
  146. // although it will not set a new thread state in that case. See 10.9.5
  147. // xnu-2422.115.4/osfmk/kern/exception.c exception_deliver(), and the more
  148. // elaborate comment at util/mach/exc_server_variants.h
  149. // ExcServerSuccessfulReturnValue(). Duplicate that behavior.
  150. bool success = kr == KERN_SUCCESS || kr == MACH_RCV_PORT_DIED;
  151. MACH_LOG_IF(WARNING, !success, kr) << "UniversalExceptionRaise";
  152. if (kr == KERN_SUCCESS && set_state) {
  153. kr = thread_set_state(thread, flavor, new_state, new_state_count);
  154. MACH_LOG_IF(WARNING, kr != KERN_SUCCESS, kr) << "thread_set_state";
  155. }
  156. return success;
  157. }
  158. } // namespace
  159. void SimulateCrash(const NativeCPUContext& cpu_context) {
  160. #if defined(ARCH_CPU_X86)
  161. DCHECK_EQ(implicit_cast<thread_state_flavor_t>(cpu_context.tsh.flavor),
  162. implicit_cast<thread_state_flavor_t>(x86_THREAD_STATE32));
  163. DCHECK_EQ(implicit_cast<mach_msg_type_number_t>(cpu_context.tsh.count),
  164. x86_THREAD_STATE32_COUNT);
  165. #elif defined(ARCH_CPU_X86_64)
  166. DCHECK_EQ(implicit_cast<thread_state_flavor_t>(cpu_context.tsh.flavor),
  167. implicit_cast<thread_state_flavor_t>(x86_THREAD_STATE64));
  168. DCHECK_EQ(implicit_cast<mach_msg_type_number_t>(cpu_context.tsh.count),
  169. x86_THREAD_STATE64_COUNT);
  170. #endif
  171. base::mac::ScopedMachSendRight thread(mach_thread_self());
  172. exception_type_t exception = kMachExceptionSimulated;
  173. mach_exception_data_type_t codes[] = {0, 0};
  174. mach_msg_type_number_t code_count = arraysize(codes);
  175. // Look up the handler for EXC_CRASH exceptions in the same way that the
  176. // kernel would: try a thread handler, then a task handler, and finally a host
  177. // handler. 10.9.5 xnu-2422.115.4/osfmk/kern/exception.c exception_triage().
  178. static constexpr ExceptionPorts::TargetType kTargetTypes[] = {
  179. ExceptionPorts::kTargetTypeThread,
  180. ExceptionPorts::kTargetTypeTask,
  181. // This is not expected to succeed, because mach_host_self() doesn’t
  182. // return the host_priv port to non-root users, and this is the port
  183. // that’s required for host_get_exception_ports().
  184. //
  185. // See 10.9.5 xnu-2422.115.4/bsd/kern/kern_prot.c set_security_token(),
  186. // xnu-2422.115.4/osfmk/kern/task.c host_security_set_task_token(), and
  187. // xnu-2422.115.4/osfmk/kern/ipc_host.c host_get_exception_ports().
  188. ExceptionPorts::kTargetTypeHost,
  189. };
  190. bool success = false;
  191. for (size_t target_type_index = 0;
  192. !success && target_type_index < arraysize(kTargetTypes);
  193. ++target_type_index) {
  194. ExceptionPorts::ExceptionHandlerVector handlers;
  195. ExceptionPorts exception_ports(kTargetTypes[target_type_index],
  196. MACH_PORT_NULL);
  197. if (exception_ports.GetExceptionPorts(EXC_MASK_CRASH, &handlers)) {
  198. DCHECK_LE(handlers.size(), 1u);
  199. if (handlers.size() == 1) {
  200. DCHECK(handlers[0].mask & EXC_MASK_CRASH);
  201. success = DeliverException(thread.get(),
  202. mach_task_self(),
  203. exception,
  204. codes,
  205. code_count,
  206. cpu_context,
  207. handlers[0],
  208. false);
  209. }
  210. }
  211. }
  212. LOG_IF(WARNING, !success)
  213. << "SimulateCrash did not find an appropriate exception handler";
  214. }
  215. } // namespace crashpad