From 4f8c0b90bb3273bb044d1c7a12730d65bba8489a Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 12 May 2014 01:35:09 +0000 Subject: [PATCH] signal.c: check the next page too * signal.c (check_stack_overflow): check the next page too. SP in ucontext is not decremented yet when `push` failed, so the fault page can be the next. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45924 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- signal.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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)