@@ -578,6 +578,7 @@ namespace PageFaultHandler
578578{
579579#ifdef USE_MACH_EXCEPTION_PORTS
580580 static void SignalHandler (mach_port_t port);
581+ static mach_port_t s_port = 0 ;
581582#else
582583 static void SignalHandler (int sig, siginfo_t * info, void * ctx);
583584#endif
@@ -661,7 +662,6 @@ void PageFaultHandler::SignalHandler(mach_port_t port)
661662 return ;
662663 }
663664
664- s_exception_handler_mutex.lock ();
665665 thread_state64_t * state = (thread_state64_t *)msg_in.old_state ;
666666
667667 HandlerResult result = HandlerResult::ExecuteNextHandler;
@@ -686,15 +686,10 @@ void PageFaultHandler::SignalHandler(mach_port_t port)
686686
687687 if (result != HandlerResult::ContinueExecution) // cooked
688688 {
689+ // Continue to the next exception handler (debugger or crash)
689690 msg_out.RetCode = KERN_FAILURE;
690691 msg_out.flavor = 0 ;
691692 msg_out.new_stateCnt = 0 ;
692-
693- // The crash handler on macOS or Linux doesn't use context passed to it
694- // Stubbing it here is fine
695- CrashHandler::CrashSignalHandler (-1 , nullptr , nullptr );
696-
697- pxFailRel (" CrashSignalHandler returned when it should have terminated us!" );
698693 }
699694 else
700695 {
@@ -709,8 +704,6 @@ void PageFaultHandler::SignalHandler(mach_port_t port)
709704 offsetof (__typeof__ (msg_out), new_state) + msg_out.new_stateCnt * sizeof (natural_t );
710705 send_size = msg_out.Head .msgh_size ;
711706 option |= MACH_SEND_MSG;
712-
713- s_exception_handler_mutex.unlock ();
714707 }
715708}
716709
@@ -751,22 +744,27 @@ bool PageFaultHandler::Install(Error* error)
751744 return false ;
752745 }
753746
754- if ((r = mach_port_mod_refs (mach_task_self (), port, MACH_PORT_RIGHT_SEND, -1 )))
755- {
756- mach_port_deallocate (mach_task_self (), port);
757- pxFailRel (fmt::format (" mach_port_mod_refs: {:x}" , r).c_str ());
758- return false ;
759- }
760-
761747 mach_port_t previous;
762748 if ((r = mach_port_request_notification (mach_task_self (), port, MACH_NOTIFY_NO_SENDERS, 0 , port, MACH_MSG_TYPE_MAKE_SEND_ONCE, &previous)))
763749 {
764750 mach_port_deallocate (mach_task_self (), port);
765- pxFailRel (fmt::format (" mach_port_mod_refs : {:x}" , r).c_str ());
751+ pxFailRel (fmt::format (" mach_port_request_notification : {:x}" , r).c_str ());
766752 return false ;
767753 }
768754
769755 s_installed = true ;
756+ s_port = port;
757+ return true ;
758+ }
759+
760+ bool PageFaultHandler::InstallSecondaryThread ()
761+ {
762+ kern_return_t r = thread_set_exception_ports (mach_thread_self (), EXC_MASK_BAD_ACCESS, s_port, EXCEPTION_STATE | MACH_EXCEPTION_CODES, THREAD_STATE64);
763+ if (r)
764+ {
765+ pxFailRel (fmt::format (" thread_set_exception_ports(secondary): {:x}" , r).c_str ());
766+ return false ;
767+ }
770768 return true ;
771769}
772770
@@ -839,4 +837,6 @@ bool PageFaultHandler::Install(Error* error)
839837 s_installed = true ;
840838 return true ;
841839}
840+
841+ bool PageFaultHandler::InstallSecondaryThread () { return true ; }
842842#endif
0 commit comments