#undef HAVE__SETJMP
#endif
+/* The following is needed for recovery from C stack overflows. */
+#include <setjmp.h>
+typedef jmp_buf sigjmp_buf;
+#ifdef MINGW_W64
+/* Evidently, MinGW64's longjmp crashes when invoked from an exception
+ handler, see https://sourceforge.net/p/mingw-w64/mailman/message/32421953/.
+ This seems to be an unsolved problem in the MinGW64 runtime. So we
+ use the GCC intrinsics instead. FIXME. */
+#define sigsetjmp(j,m) __builtin_setjmp(j)
+#else
+#define sigsetjmp(j,m) setjmp(j)
+#endif
+extern void w32_reset_stack_overflow_guard (void);
+
#ifdef _MSC_VER
#include <sys/timeb.h>
#include <sys/stat.h>
xsignal0 (Qarith_error);
}
-#ifdef HAVE_STACK_OVERFLOW_HANDLING
+#if defined HAVE_STACK_OVERFLOW_HANDLING && !defined WINDOWSNT
/* Alternate stack used by SIGSEGV handler below. */
return sigaction (SIGSEGV, &sa, NULL) < 0 ? 0 : 1;
}
-#else /* not HAVE_STACK_OVERFLOW_HANDLING */
+#else /* not HAVE_STACK_OVERFLOW_HANDLING or WINDOWSNT */
static bool
init_sigsegv (void)
return 0;
}
-#endif /* HAVE_STACK_OVERFLOW_HANDLING */
+#endif /* HAVE_STACK_OVERFLOW_HANDLING && !WINDOWSNT */
static void
deliver_arith_signal (int sig)
static PVOID except_addr;
#ifndef CYGWIN
+
+/* Stack overflow recovery. */
+
+/* Re-establish the guard page at stack limit. This is needed because
+ when a stack overflow is detected, Windows removes the guard bit
+ from the guard page, so if we don't re-establish that protection,
+ the next stack overflow will cause a crash. */
+void
+w32_reset_stack_overflow_guard (void)
+{
+ /* MinGW headers don't declare this (should be in malloc.h). */
+ _CRTIMP int __cdecl _resetstkoflw (void);
+
+ /* We ignore the return value. If _resetstkoflw fails, the next
+ stack overflow will crash the program. */
+ (void)_resetstkoflw ();
+}
+
+static void
+stack_overflow_handler (void)
+{
+ /* Hard GC error may lead to stack overflow caused by
+ too nested calls to mark_object. No way to survive. */
+ if (gc_in_progress)
+ terminate_due_to_signal (SIGSEGV, 40);
+#ifdef _WIN64
+ /* See ms-w32.h: MinGW64's longjmp crashes if invoked in this context. */
+ __builtin_longjmp (return_to_command_loop, 1);
+#else
+ sys_longjmp (return_to_command_loop, 1);
+#endif
+}
+
/* This handler records the exception code and the address where it
was triggered so that this info could be included in the backtrace.
Without that, the backtrace in some cases has no information
whatsoever about the offending code, and looks as if the top-level
- exception handler in the MinGW startup code di the one that
- crashed. */
+ exception handler in the MinGW startup code was the one that
+ crashed. We also recover from stack overflow, by calling our stack
+ overflow handler that jumps back to top level. */
static LONG CALLBACK
my_exception_handler (EXCEPTION_POINTERS * exception_data)
{
except_code = exception_data->ExceptionRecord->ExceptionCode;
except_addr = exception_data->ExceptionRecord->ExceptionAddress;
+ /* If this is a stack overflow exception, attempt to recover. */
+ if (exception_data->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW
+ && exception_data->ExceptionRecord->NumberParameters == 2
+ /* We can only longjmp to top level from the main thread. */
+ && GetCurrentThreadId () == dwMainThreadId)
+ {
+ /* Call stack_overflow_handler (). */
+#ifdef _WIN64
+ exception_data->ContextRecord->Rip = (DWORD_PTR) &stack_overflow_handler;
+#else
+ exception_data->ContextRecord->Eip = (DWORD_PTR) &stack_overflow_handler;
+#endif
+ /* Zero this out, so the stale address of the stack overflow
+ exception we handled is not displayed in some future
+ unrelated crash. */
+ except_addr = 0;
+ return EXCEPTION_CONTINUE_EXECUTION;
+ }
+
if (prev_exception_handler)
return prev_exception_handler (exception_data);
return EXCEPTION_EXECUTE_HANDLER;
InitCommonControls ();
syms_of_w32uniscribe ();
+
+ /* Needed for recovery from C stack overflows in batch mode. */
+ if (noninteractive)
+ dwMainThreadId = GetCurrentThreadId ();
}
#ifdef NTGUI_UNICODE