CwlMachBadInstructionHandler.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // CwlMachBadExceptionHandler.m
  3. // CwlPreconditionTesting
  4. //
  5. // Created by Matt Gallagher on 2016/01/10.
  6. // Copyright © 2016 Matt Gallagher ( https://www.cocoawithlove.com ). All rights reserved.
  7. //
  8. // Permission to use, copy, modify, and/or distribute this software for any
  9. // purpose with or without fee is hereby granted, provided that the above
  10. // copyright notice and this permission notice appear in all copies.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  15. // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  18. // IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. //
  20. #ifdef __APPLE__
  21. #import "TargetConditionals.h"
  22. #if TARGET_OS_OSX || TARGET_OS_IOS
  23. #import "mach_excServer.h"
  24. #import "CwlMachBadInstructionHandler.h"
  25. @protocol BadInstructionReply <NSObject>
  26. +(NSNumber *)receiveReply:(NSValue *)value;
  27. @end
  28. /// A basic function that receives callbacks from mach_exc_server and relays them to the Swift implemented BadInstructionException.catch_mach_exception_raise_state.
  29. kern_return_t catch_mach_exception_raise_state(mach_port_t exception_port, exception_type_t exception, const mach_exception_data_t code, mach_msg_type_number_t codeCnt, int *flavor, const thread_state_t old_state, mach_msg_type_number_t old_stateCnt, thread_state_t new_state, mach_msg_type_number_t *new_stateCnt) {
  30. bad_instruction_exception_reply_t reply = { exception_port, exception, code, codeCnt, flavor, old_state, old_stateCnt, new_state, new_stateCnt };
  31. Class badInstructionClass = NSClassFromString(@"BadInstructionException");
  32. NSValue *value = [NSValue valueWithBytes: &reply objCType: @encode(bad_instruction_exception_reply_t)];
  33. return [[badInstructionClass performSelector: @selector(receiveReply:) withObject: value] intValue];
  34. }
  35. // The mach port should be configured so that this function is never used.
  36. kern_return_t catch_mach_exception_raise(mach_port_t exception_port, mach_port_t thread, mach_port_t task, exception_type_t exception, mach_exception_data_t code, mach_msg_type_number_t codeCnt) {
  37. assert(false);
  38. return KERN_FAILURE;
  39. }
  40. // The mach port should be configured so that this function is never used.
  41. kern_return_t catch_mach_exception_raise_state_identity(mach_port_t exception_port, mach_port_t thread, mach_port_t task, exception_type_t exception, mach_exception_data_t code, mach_msg_type_number_t codeCnt, int *flavor, thread_state_t old_state, mach_msg_type_number_t old_stateCnt, thread_state_t new_state, mach_msg_type_number_t *new_stateCnt) {
  42. assert(false);
  43. return KERN_FAILURE;
  44. }
  45. #endif /* TARGET_OS_OSX || TARGET_OS_IOS */
  46. #endif /* __APPLE__ */