From 06f78a83d2781cd4a34eb69877efbb3191cc9e9c Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Sun, 18 Nov 2018 21:01:47 -0800 Subject: [PATCH] Implement handling of traps under macOS. --- src/microcode/uxsig.c | 10 ------ src/microcode/uxtrap.h | 74 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/microcode/uxsig.c b/src/microcode/uxsig.c index 0b899ffb3..2d4be9dfb 100644 --- a/src/microcode/uxsig.c +++ b/src/microcode/uxsig.c @@ -56,10 +56,6 @@ extern void UX_reinitialize_tty (void); # endif #endif -#ifndef __APPLE__ -# define HAVE_SIGFPE -#endif - static Tsignal_handler current_handler (int signo) { @@ -340,9 +336,7 @@ initialize_signal_descriptors (void) defsignal (SIGTRAP, "SIGTRAP", dfl_terminate, CORE_DUMP); defsignal (SIGIOT, "SIGIOT", dfl_terminate, CORE_DUMP); defsignal (SIGEMT, "SIGEMT", dfl_terminate, CORE_DUMP); -#ifdef HAVE_SIGFPE defsignal (SIGFPE, "SIGFPE", dfl_terminate, CORE_DUMP); -#endif defsignal (SIGKILL, "SIGKILL", dfl_terminate, (NOIGNORE | NOBLOCK | NOCATCH)); defsignal (SIGBUS, "SIGBUS", dfl_terminate, CORE_DUMP); defsignal (SIGSEGV, "SIGSEGV", dfl_terminate, CORE_DUMP); @@ -553,7 +547,6 @@ DEFUN_STD_HANDLER (sighnd_terminate, ? (find_signal_name (signo)) : 0))) -#ifdef HAVE_SIGFPE extern void clear_float_exceptions (void); static @@ -562,7 +555,6 @@ DEFUN_STD_HANDLER (sighnd_fpe, clear_float_exceptions (); trap_handler ("floating-point exception", signo, info, scp); }) -#endif static DEFUN_STD_HANDLER (sighnd_hardware_trap, @@ -676,9 +668,7 @@ UX_initialize_signals (void) initialize_signal_descriptors (); initialize_signal_debugging (); bind_handler (SIGINT, sighnd_control_g); -#ifdef HAVE_SIGFPE bind_handler (SIGFPE, sighnd_fpe); -#endif bind_handler (SIGALRM, sighnd_timer); bind_handler (SIGVTALRM, sighnd_timer); bind_handler (SIGTERM, sighnd_control_g); diff --git a/src/microcode/uxtrap.h b/src/microcode/uxtrap.h index 970561912..190a1a074 100644 --- a/src/microcode/uxtrap.h +++ b/src/microcode/uxtrap.h @@ -735,8 +735,78 @@ typedef struct #endif /* __NetBSD_Version__ >= 200000000 */ #endif /* __NetBSD__ */ -#ifdef _POSIX_REALTIME_SIGNALS -# define HAVE_SIGACTION_SIGINFO_SIGNALS +#ifdef __APPLE__ + +# ifdef __IA32__ +# define HAVE_SIGCONTEXT +# define SIGCONTEXT_FIRST_REG(scp) (& ((scp) -> uc_mcontext -> __ss)) +# define SIGCONTEXT_NREGS \ + (((sizeof (_STRUCT_X86_THREAD_STATE32)) + (SIZEOF_UNSIGNED_LONG - 1)) \ + / SIZEOF_UNSIGNED_LONG) +# define SIGCONTEXT_SP(scp) (((scp) -> uc_mcontext -> __ss) . __esp) +# define SIGCONTEXT_PC(scp) (((scp) -> uc_mcontext -> __ss) . __eip) +# define SIGCONTEXT_RFREE(scp) (((scp) -> uc_mcontext -> __ss) . __edi) +# endif /* __IA32__ */ + +# ifdef __x86_64__ +# define HAVE_SIGCONTEXT +# define SIGCONTEXT_FIRST_REG(scp) (& ((scp) -> uc_mcontext -> __ss)) +# define SIGCONTEXT_NREGS \ + (((sizeof (_STRUCT_X86_THREAD_STATE64)) + (SIZEOF_UNSIGNED_LONG - 1)) \ + / SIZEOF_UNSIGNED_LONG) +# define SIGCONTEXT_SP(scp) (((scp) -> uc_mcontext -> __ss) . __rsp) +# define SIGCONTEXT_PC(scp) (((scp) -> uc_mcontext -> __ss) . __rip) +# define SIGCONTEXT_RFREE(scp) (((scp) -> uc_mcontext -> __ss) . __rdi) +# endif /* __x86_64__ */ + +# define INITIALIZE_UX_SIGNAL_CODES() do \ +{ \ + DECLARE_UX_SIGNAL_CODE \ + (SIGILL, (~0L), ILL_ILLOPC, "Illegal opcode"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGILL, (~0L), ILL_ILLTRP, "Illegal trap"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGILL, (~0L), ILL_PRVOPC, "Privileged opcode"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGILL, (~0L), ILL_COPROC, "Coprocessor error"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGILL, (~0L), ILL_BADSTK, "Internal stack error"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_FLTDIV, "Floating-point divide by zero"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_FLTOVF, "Floating-point overflow"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_FLTUND, "Floating-point underflow"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_FLTRES, "Floating-point inexact result"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_FLTINV, "Invalid floating-point operation"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_FLTSUB, "Subscript out of range"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_INTDIV, "Integer divide by zero"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGFPE, (~0L), FPE_INTOVF, "Integer overflow"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGSEGV, (~0L), SEGV_MAPERR, "Address not mapped to object"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGSEGV, (~0L), SEGV_ACCERR, "Invalid permissions for mapped object"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGBUS, (~0L), BUS_ADRALN, "Invalid address alignment"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGBUS, (~0L), BUS_ADRERR, "Nonexistent physical address"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGBUS, (~0L), BUS_OBJERR, "Object-specific hardware error"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGTRAP, (~0L), TRAP_BRKPT, "Process breakpoint"); \ + DECLARE_UX_SIGNAL_CODE \ + (SIGTRAP, (~0L), TRAP_TRACE, "Process trace trap"); \ +} while (0) + +#endif /* __APPLE__ */ + +#if !(defined (HAVE_SIGACTION_SIGINFO_SIGNALS)) && (defined (_POSIX_REALTIME_SIGNALS)) +# define HAVE_SIGACTION_SIGINFO_SIGNALS 1 #endif #ifdef HAVE_SIGACTION_SIGINFO_SIGNALS -- 2.25.1