From 4e1288550a4ed247c74eae7294fc3ab243146e93 Mon Sep 17 00:00:00 2001 From: Hiroshi Shirosaki Date: Thu, 15 May 2014 11:54:05 +0900 Subject: [PATCH] Handle machine stack overflow on mingw We can use Structured Exception Handling by Addvectoredexceptionhandler() for machine stack overflow on mingw. This would be equivalent to the handling using __try and __exept on mswin introduced by r43748. --- eval_intern.h | 8 ++++++++ thread_win32.c | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/eval_intern.h b/eval_intern.h index f19c252..9ac9f9f 100644 --- a/eval_intern.h +++ b/eval_intern.h @@ -95,6 +95,14 @@ extern int select_large_fdset(int, fd_set *, fd_set *, fd_set *, struct timeval EXCEPTION_CONTINUE_SEARCH) { \ /* never reaches here */ \ } +#elif defined(__MINGW32__) +LONG WINAPI rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *); +#define SAVE_ROOT_JMPBUF_BEFORE_STMT \ + do { \ + PVOID _handler = AddVectoredExceptionHandler(1, rb_w32_stack_overflow_handler); +#define SAVE_ROOT_JMPBUF_AFTER_STMT \ + RemoveVectoredExceptionHandler(_handler); \ + } while (0); #else #define SAVE_ROOT_JMPBUF_BEFORE_STMT #define SAVE_ROOT_JMPBUF_AFTER_STMT diff --git a/thread_win32.c b/thread_win32.c index db00a73..feadede 100644 --- a/thread_win32.c +++ b/thread_win32.c @@ -754,6 +754,18 @@ ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr) return rb_thread_raised_p(th, RAISED_STACKOVERFLOW); } +#if defined(__MINGW32__) +LONG WINAPI +rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *exception) +{ + if (exception->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) { + rb_thread_raised_set(GET_THREAD(), RAISED_STACKOVERFLOW); + raise(SIGSEGV); + } + return EXCEPTION_CONTINUE_SEARCH; +} +#endif + #ifdef RUBY_ALLOCA_CHKSTK void ruby_alloca_chkstk(size_t len, void *sp) -- 1.9.2.msysgit.0