diff --git a/signal.c b/signal.c index 05b21a9ad9..1fad76aad6 100644 --- a/signal.c +++ b/signal.c @@ -701,6 +701,9 @@ rb_get_next_signal(void) #if defined(USE_SIGALTSTACK) || defined(_WIN32) NORETURN(void ruby_thread_stack_overflow(rb_thread_t *th)); #if defined(HAVE_UCONTEXT_H) && defined __linux__ && (defined __i386__ || defined __x86_64__) +# define USE_UCONTEXT_REG 1 +#endif +#ifdef USE_UCONTEXT_REG static void check_stack_overflow(const uintptr_t addr, const ucontext_t *ctx) { @@ -710,7 +713,12 @@ check_stack_overflow(const uintptr_t addr, const ucontext_t *ctx) const greg_t sp = ctx->uc_mcontext.gregs[REG_ESP]; # endif enum {pagesize = 4096}; - if ((uintptr_t)sp / pagesize == addr / pagesize) { + const uintptr_t sp_page = (uintptr_t)sp / pagesize; + const uintptr_t fault_page = addr / pagesize; + + /* SP in ucontext is not decremented yet when `push` failed, so + * the fault page can be the next. */ + if (sp_page == fault_page || sp_page == fault_page + 1) { ruby_thread_stack_overflow(GET_THREAD()); } } @@ -729,7 +737,7 @@ check_stack_overflow(const void *addr) #define CHECK_STACK_OVERFLOW() check_stack_overflow(0) #else #define FAULT_ADDRESS info->si_addr -#if defined(HAVE_UCONTEXT_H) && defined __linux__ && (defined __i386__ || defined __x86_64__) +# ifdef USE_UCONTEXT_REG # define CHECK_STACK_OVERFLOW() check_stack_overflow((uintptr_t)FAULT_ADDRESS, ctx) #else # define CHECK_STACK_OVERFLOW() check_stack_overflow(FAULT_ADDRESS)