X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fruntime%2Finterrupt.c;h=13ff61df1d81023636fef060d972a2871f7509f8;hb=d25e3478acccec70402ff32554669a982be8e281;hp=8c87054c7d2d559959b71d2d04a2d07cf8c70ef7;hpb=cf507f95509a855a752b6f1771aa06877b8a3b30;p=sbcl.git diff --git a/src/runtime/interrupt.c b/src/runtime/interrupt.c index 8c87054..13ff61d 100644 --- a/src/runtime/interrupt.c +++ b/src/runtime/interrupt.c @@ -68,6 +68,19 @@ #include "genesis/simple-fun.h" #include "genesis/cons.h" +/* When we catch an internal error, should we pass it back to Lisp to + * be handled in a high-level way? (Early in cold init, the answer is + * 'no', because Lisp is still too brain-dead to handle anything. + * After sufficient initialization has been completed, the answer + * becomes 'yes'.) */ +boolean internal_errors_enabled = 0; + +#ifndef LISP_FEATURE_WIN32 +static +void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, os_context_t*); +#endif +union interrupt_handler interrupt_handlers[NSIG]; + /* Under Linux on some architectures, we appear to have to restore the * FPU control word from the context, as after the signal is delivered * we appear to have a null FPU control word. */ @@ -80,6 +93,56 @@ os_context_t *context = arch_os_get_context(&void_context); #endif +/* Foreign code may want to start some threads on its own. + * Non-targetted, truly asynchronous signals can be delivered to + * basically any thread, but invoking Lisp handlers in such foregign + * threads is really bad, so let's resignal it. + * + * This should at least bring attention to the problem, but it cannot + * work for SIGSEGV and similar. It is good enough for timers, and + * maybe all deferrables. */ + +#ifdef LISP_FEATURE_SB_THREAD +static void +add_handled_signals(sigset_t *sigset) +{ + int i; + for(i = 1; i < NSIG; i++) { + if (!(ARE_SAME_HANDLER(interrupt_low_level_handlers[i], SIG_DFL)) || + !(ARE_SAME_HANDLER(interrupt_handlers[i].c, SIG_DFL))) { + sigaddset(sigset, i); + } + } +} + +void block_signals(sigset_t *what, sigset_t *where, sigset_t *old); +#endif + +static boolean +maybe_resignal_to_lisp_thread(int signal, os_context_t *context) +{ +#ifdef LISP_FEATURE_SB_THREAD + if (!pthread_getspecific(lisp_thread)) { + if (!(sigismember(&deferrable_sigset,signal))) { + corruption_warning_and_maybe_lose + ("Received signal %d in non-lisp thread %lu, resignalling to a lisp thread.", + signal, + pthread_self()); + } + { + sigset_t sigset; + sigemptyset(&sigset); + add_handled_signals(&sigset); + block_signals(&sigset, 0, 0); + block_signals(&sigset, os_context_sigmask_addr(context), 0); + kill(getpid(), signal); + } + return 1; + } else +#endif + return 0; +} + /* These are to be used in signal handlers. Currently all handlers are * called from one of: * @@ -98,10 +161,11 @@ * kernel properly, so we fix it up ourselves in the * arch_os_get_context(..) function. -- CSR, 2002-07-23 */ -#define SAVE_ERRNO(context,void_context) \ +#define SAVE_ERRNO(signal,context,void_context) \ { \ int _saved_errno = errno; \ RESTORE_FP_CONTROL_WORD(context,void_context); \ + if (!maybe_resignal_to_lisp_thread(signal, context)) \ { #define RESTORE_ERRNO \ @@ -250,11 +314,13 @@ sigset_t gc_sigset; #endif +#if !defined(LISP_FEATURE_WIN32) boolean deferrables_blocked_p(sigset_t *sigset) { return all_signals_blocked_p(sigset, &deferrable_sigset, "deferrable"); } +#endif void check_deferrables_unblocked_or_lose(sigset_t *sigset) @@ -274,11 +340,13 @@ check_deferrables_blocked_or_lose(sigset_t *sigset) #endif } +#if !defined(LISP_FEATURE_WIN32) boolean blockables_blocked_p(sigset_t *sigset) { return all_signals_blocked_p(sigset, &blockable_sigset, "blockable"); } +#endif void check_blockables_unblocked_or_lose(sigset_t *sigset) @@ -298,11 +366,13 @@ check_blockables_blocked_or_lose(sigset_t *sigset) #endif } +#if !defined(LISP_FEATURE_WIN32) boolean gc_signals_blocked_p(sigset_t *sigset) { return all_signals_blocked_p(sigset, &gc_sigset, "gc"); } +#endif void check_gc_signals_unblocked_or_lose(sigset_t *sigset) @@ -498,7 +568,14 @@ check_interrupt_context_or_lose(os_context_t *context) if (stop_for_gc_pending) if (!(pseudo_atomic_interrupted || gc_inhibit || in_race_p)) lose("STOP_FOR_GC_PENDING, but why?\n"); + if (pseudo_atomic_interrupted) + if (!(gc_pending || stop_for_gc_pending || interrupt_deferred_p)) + lose("pseudo_atomic_interrupted, but why?\n"); } +#else + if (pseudo_atomic_interrupted) + if (!(gc_pending || interrupt_deferred_p)) + lose("pseudo_atomic_interrupted, but why?\n"); #endif #endif if (interrupt_pending && !interrupt_deferred_p) @@ -518,20 +595,6 @@ check_interrupt_context_or_lose(os_context_t *context) } #endif } - -/* When we catch an internal error, should we pass it back to Lisp to - * be handled in a high-level way? (Early in cold init, the answer is - * 'no', because Lisp is still too brain-dead to handle anything. - * After sufficient initialization has been completed, the answer - * becomes 'yes'.) */ -boolean internal_errors_enabled = 0; - -#ifndef LISP_FEATURE_WIN32 -static -void (*interrupt_low_level_handlers[NSIG]) (int, siginfo_t*, os_context_t*); -#endif -union interrupt_handler interrupt_handlers[NSIG]; - /* * utility routines used by various signal handlers @@ -756,7 +819,7 @@ interrupt_handle_pending(os_context_t *context) struct interrupt_data *data = thread->interrupt_data; if (arch_pseudo_atomic_atomic(context)) { - lose("Handling pending interrupt in pseduo atomic."); + lose("Handling pending interrupt in pseudo atomic."); } FSHOW_SIGNAL((stderr, "/entering interrupt_handle_pending\n")); @@ -876,11 +939,15 @@ interrupt_handle_pending(os_context_t *context) sigcopyset(os_context_sigmask_addr(context), &data->pending_mask); run_deferred_handler(data, context); } +#endif +#ifdef LISP_FEATURE_GENCGC + if (get_pseudo_atomic_interrupted(thread)) + lose("pseudo_atomic_interrupted after interrupt_handle_pending\n"); +#endif /* It is possible that the end of this function was reached * without never actually doing anything, the tests in Lisp for * when to call receive-pending-interrupt are not exact. */ FSHOW_SIGNAL((stderr, "/exiting interrupt_handle_pending\n")); -#endif } @@ -1023,12 +1090,12 @@ maybe_defer_handler(void *handler, struct interrupt_data *data, */ if ((SymbolValue(INTERRUPTS_ENABLED,thread) == NIL) || in_leaving_without_gcing_race_p(thread)) { - store_signal_data_for_later(data,handler,signal,info,context); - SetSymbolValue(INTERRUPT_PENDING, T,thread); FSHOW_SIGNAL((stderr, "/maybe_defer_handler(%x,%d): deferred (RACE=%d)\n", (unsigned int)handler,signal, in_leaving_without_gcing_race_p(thread))); + store_signal_data_for_later(data,handler,signal,info,context); + SetSymbolValue(INTERRUPT_PENDING, T,thread); check_interrupt_context_or_lose(context); return 1; } @@ -1036,11 +1103,11 @@ maybe_defer_handler(void *handler, struct interrupt_data *data, * actually use its argument for anything on x86, so this branch * may succeed even when context is null (gencgc alloc()) */ if (arch_pseudo_atomic_atomic(context)) { - store_signal_data_for_later(data,handler,signal,info,context); - arch_set_pseudo_atomic_interrupted(context); FSHOW_SIGNAL((stderr, "/maybe_defer_handler(%x,%d): deferred(PA)\n", (unsigned int)handler,signal)); + store_signal_data_for_later(data,handler,signal,info,context); + arch_set_pseudo_atomic_interrupted(context); check_interrupt_context_or_lose(context); return 1; } @@ -1083,10 +1150,9 @@ store_signal_data_for_later (struct interrupt_data *data, void *handler, static void maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context) { - SAVE_ERRNO(context,void_context); + SAVE_ERRNO(signal,context,void_context); struct thread *thread = arch_os_get_current_thread(); struct interrupt_data *data = thread->interrupt_data; - if(!maybe_defer_handler(interrupt_handle_now,data,signal,info,context)) interrupt_handle_now(signal, info, context); RESTORE_ERRNO; @@ -1106,7 +1172,7 @@ low_level_interrupt_handle_now(int signal, siginfo_t *info, static void low_level_maybe_now_maybe_later(int signal, siginfo_t *info, void *void_context) { - SAVE_ERRNO(context,void_context); + SAVE_ERRNO(signal,context,void_context); struct thread *thread = arch_os_get_current_thread(); struct interrupt_data *data = thread->interrupt_data; @@ -1128,15 +1194,15 @@ sig_stop_for_gc_handler(int signal, siginfo_t *info, os_context_t *context) /* Test for GC_INHIBIT _first_, else we'd trap on every single * pseudo atomic until gc is finally allowed. */ if (SymbolValue(GC_INHIBIT,thread) != NIL) { - SetSymbolValue(STOP_FOR_GC_PENDING,T,thread); FSHOW_SIGNAL((stderr, "sig_stop_for_gc deferred (*GC-INHIBIT*)\n")); + SetSymbolValue(STOP_FOR_GC_PENDING,T,thread); return; } else if (arch_pseudo_atomic_atomic(context)) { + FSHOW_SIGNAL((stderr,"sig_stop_for_gc deferred (PA)\n")); SetSymbolValue(STOP_FOR_GC_PENDING,T,thread); arch_set_pseudo_atomic_interrupted(context); maybe_save_gc_mask_and_block_deferrables (os_context_sigmask_addr(context)); - FSHOW_SIGNAL((stderr,"sig_stop_for_gc deferred (PA)\n")); return; } @@ -1151,6 +1217,23 @@ sig_stop_for_gc_handler(int signal, siginfo_t *info, os_context_t *context) SetSymbolValue(GC_PENDING,NIL,thread); SetSymbolValue(STOP_FOR_GC_PENDING,NIL,thread); + /* Consider this: in a PA section GC is requested: GC_PENDING, + * pseudo_atomic_interrupted and gc_blocked_deferrables are set, + * deferrables are blocked then pseudo_atomic_atomic is cleared, + * but a SIG_STOP_FOR_GC arrives before trapping to + * interrupt_handle_pending. Here, GC_PENDING is cleared but + * pseudo_atomic_interrupted is not and we go on running with + * pseudo_atomic_interrupted but without a pending interrupt or + * GC. GC_BLOCKED_DEFERRABLES is also left at 1. So let's tidy it + * up. */ + if (thread->interrupt_data->gc_blocked_deferrables) { + FSHOW_SIGNAL((stderr,"cleaning up after gc_blocked_deferrables\n")); + clear_pseudo_atomic_interrupted(thread); + sigcopyset(os_context_sigmask_addr(context), + &thread->interrupt_data->pending_mask); + thread->interrupt_data->gc_blocked_deferrables = 0; + } + if(thread_state(thread)!=STATE_RUNNING) { lose("sig_stop_for_gc_handler: wrong thread state: %ld\n", fixnum_value(thread->state)); @@ -1175,7 +1258,7 @@ sig_stop_for_gc_handler(int signal, siginfo_t *info, os_context_t *context) void interrupt_handle_now_handler(int signal, siginfo_t *info, void *void_context) { - SAVE_ERRNO(context,void_context); + SAVE_ERRNO(signal,context,void_context); #ifndef LISP_FEATURE_WIN32 if ((signal == SIGILL) || (signal == SIGBUS) #ifndef LISP_FEATURE_LINUX @@ -1401,17 +1484,19 @@ handle_guard_page_triggered(os_context_t *context,os_vm_address_t addr) { struct thread *th=arch_os_get_current_thread(); - /* note the os_context hackery here. When the signal handler returns, - * it won't go back to what it was doing ... */ - if(addr >= CONTROL_STACK_GUARD_PAGE(th) && - addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) { + if(addr >= CONTROL_STACK_HARD_GUARD_PAGE(th) && + addr < CONTROL_STACK_HARD_GUARD_PAGE(th) + os_vm_page_size) { + lose("Control stack exhausted"); + } + else if(addr >= CONTROL_STACK_GUARD_PAGE(th) && + addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) { /* We hit the end of the control stack: disable guard page * protection so the error handler has some headroom, protect the * previous page so that we can catch returns from the guard page * and restore it. */ - corruption_warning_and_maybe_lose("Control stack exhausted"); protect_control_stack_guard_page(0, NULL); protect_control_stack_return_guard_page(1, NULL); + fprintf(stderr, "INFO: Control stack guard page unprotected\n"); #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK /* For the unfortunate case, when the control stack is @@ -1428,16 +1513,20 @@ handle_guard_page_triggered(os_context_t *context,os_vm_address_t addr) * unprotect this one. This works even if we somehow missed * the return-guard-page, and hit it on our way to new * exhaustion instead. */ - fprintf(stderr, "INFO: Control stack guard page reprotected\n"); protect_control_stack_guard_page(1, NULL); protect_control_stack_return_guard_page(0, NULL); + fprintf(stderr, "INFO: Control stack guard page reprotected\n"); return 1; } + else if(addr >= BINDING_STACK_HARD_GUARD_PAGE(th) && + addr < BINDING_STACK_HARD_GUARD_PAGE(th) + os_vm_page_size) { + lose("Binding stack exhausted"); + } else if(addr >= BINDING_STACK_GUARD_PAGE(th) && addr < BINDING_STACK_GUARD_PAGE(th) + os_vm_page_size) { - corruption_warning_and_maybe_lose("Binding stack exhausted"); protect_binding_stack_guard_page(0, NULL); protect_binding_stack_return_guard_page(1, NULL); + fprintf(stderr, "INFO: Binding stack guard page unprotected\n"); /* For the unfortunate case, when the binding stack is * exhausted in a signal handler. */ @@ -1448,16 +1537,20 @@ handle_guard_page_triggered(os_context_t *context,os_vm_address_t addr) } else if(addr >= BINDING_STACK_RETURN_GUARD_PAGE(th) && addr < BINDING_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) { - fprintf(stderr, "INFO: Binding stack guard page reprotected\n"); protect_binding_stack_guard_page(1, NULL); protect_binding_stack_return_guard_page(0, NULL); + fprintf(stderr, "INFO: Binding stack guard page reprotected\n"); return 1; } + else if(addr >= ALIEN_STACK_HARD_GUARD_PAGE(th) && + addr < ALIEN_STACK_HARD_GUARD_PAGE(th) + os_vm_page_size) { + lose("Alien stack exhausted"); + } else if(addr >= ALIEN_STACK_GUARD_PAGE(th) && addr < ALIEN_STACK_GUARD_PAGE(th) + os_vm_page_size) { - corruption_warning_and_maybe_lose("Alien stack exhausted"); protect_alien_stack_guard_page(0, NULL); protect_alien_stack_return_guard_page(1, NULL); + fprintf(stderr, "INFO: Alien stack guard page unprotected\n"); /* For the unfortunate case, when the alien stack is * exhausted in a signal handler. */ @@ -1468,15 +1561,15 @@ handle_guard_page_triggered(os_context_t *context,os_vm_address_t addr) } else if(addr >= ALIEN_STACK_RETURN_GUARD_PAGE(th) && addr < ALIEN_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) { - fprintf(stderr, "INFO: Alien stack guard page reprotected\n"); protect_alien_stack_guard_page(1, NULL); protect_alien_stack_return_guard_page(0, NULL); + fprintf(stderr, "INFO: Alien stack guard page reprotected\n"); return 1; } else if (addr >= undefined_alien_address && addr < undefined_alien_address + os_vm_page_size) { arrange_return_to_lisp_function - (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR)); + (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR)); return 1; } else return 0; @@ -1554,7 +1647,7 @@ see_if_sigaction_nodefer_works(void) static void unblock_me_trampoline(int signal, siginfo_t *info, void *void_context) { - SAVE_ERRNO(context,void_context); + SAVE_ERRNO(signal,context,void_context); sigset_t unblock; sigemptyset(&unblock); @@ -1567,7 +1660,7 @@ unblock_me_trampoline(int signal, siginfo_t *info, void *void_context) static void low_level_unblock_me_trampoline(int signal, siginfo_t *info, void *void_context) { - SAVE_ERRNO(context,void_context); + SAVE_ERRNO(signal,context,void_context); sigset_t unblock; sigemptyset(&unblock); @@ -1580,7 +1673,7 @@ low_level_unblock_me_trampoline(int signal, siginfo_t *info, void *void_context) static void low_level_handle_now_handler(int signal, siginfo_t *info, void *void_context) { - SAVE_ERRNO(context,void_context); + SAVE_ERRNO(signal,context,void_context); (*interrupt_low_level_handlers[signal])(signal, info, context); RESTORE_ERRNO; }