#include <mono/metadata/exception.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/mono-debug.h>
-#include <mono/metadata/mono-debug-debugger.h>
#include "mini.h"
#include "mini-x86.h"
-#if defined(__FreeBSD__)
-#include <ucontext.h>
-#endif
-
#ifdef PLATFORM_WIN32
+static void (*restore_stack) (void *);
+
static MonoW32ExceptionHandler fpe_handler;
static MonoW32ExceptionHandler ill_handler;
static MonoW32ExceptionHandler segv_handler;
#define W32_SEH_HANDLE_EX(_ex) \
if (_ex##_handler) _ex##_handler((int)sctx)
+/*
+ * mono_win32_get_handle_stackoverflow (void):
+ *
+ * Returns a pointer to a method which restores the current context stack
+ * and calls handle_exceptions, when done restores the original stack.
+ */
+static gpointer
+mono_win32_get_handle_stackoverflow (void)
+{
+ static guint8 *start = NULL;
+ guint8 *code;
+
+ if (start)
+ return start;
+
+ /* restore_contect (void *sigctx) */
+ start = code = mono_global_codeman_reserve (128);
+
+ /* load context into ebx */
+ x86_mov_reg_membase (code, X86_EBX, X86_ESP, 4, 4);
+
+ /* move current stack into edi for later restore */
+ x86_mov_reg_reg (code, X86_EDI, X86_ESP, 4);
+
+ /* use the new freed stack from sigcontext */
+ x86_mov_reg_membase (code, X86_ESP, X86_EBX, G_STRUCT_OFFSET (struct sigcontext, esp), 4);
+
+ /* get the current domain */
+ x86_call_code (code, mono_domain_get);
+
+ /* get stack overflow exception from domain object */
+ x86_mov_reg_membase (code, X86_EAX, X86_EAX, G_STRUCT_OFFSET (MonoDomain, stack_overflow_ex), 4);
+
+ /* call mono_arch_handle_exception (sctx, stack_overflow_exception_obj, FALSE) */
+ x86_push_imm (code, 0);
+ x86_push_reg (code, X86_EAX);
+ x86_push_reg (code, X86_EBX);
+ x86_call_code (code, mono_arch_handle_exception);
+
+ /* restore the SEH handler stack */
+ x86_mov_reg_reg (code, X86_ESP, X86_EDI, 4);
+
+ /* return */
+ x86_ret (code);
+
+ return start;
+}
+
+/* Special hack to workaround the fact that the
+ * when the SEH handler is called the stack is
+ * to small to recover.
+ *
+ * Stack walking part of this method is from mono_handle_exception
+ *
+ * The idea is simple;
+ * - walk the stack to free some space (64k)
+ * - set esp to new stack location
+ * - call mono_arch_handle_exception with stack overflow exception
+ * - set esp to SEH handlers stack
+ * - done
+ */
+static void
+win32_handle_stack_overflow (EXCEPTION_POINTERS* ep, struct sigcontext *sctx)
+{
+ SYSTEM_INFO si;
+ DWORD page_size;
+ MonoDomain *domain = mono_domain_get ();
+ MonoJitInfo *ji, rji;
+ MonoJitTlsData *jit_tls = TlsGetValue (mono_jit_tls_id);
+ MonoLMF *lmf = jit_tls->lmf;
+ MonoContext initial_ctx;
+ MonoContext ctx;
+ guint32 free_stack = 0;
+
+ /* convert sigcontext to MonoContext (due to reuse of stack walking helpers */
+ mono_arch_sigctx_to_monoctx (sctx, &ctx);
+
+ /* get our os page size */
+ GetSystemInfo(&si);
+ page_size = si.dwPageSize;
+
+ /* Let's walk the stack to recover
+ * the needed stack space (if possible)
+ */
+ memset (&rji, 0, sizeof (rji));
+
+ initial_ctx = ctx;
+ free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
+
+ /* try to free 64kb from our stack */
+ do {
+ MonoContext new_ctx;
+
+ ji = mono_arch_find_jit_info (domain, jit_tls, &rji, &rji, &ctx, &new_ctx, NULL, &lmf, NULL, NULL);
+ if (!ji) {
+ g_warning ("Exception inside function without unwind info");
+ g_assert_not_reached ();
+ }
+
+ if (ji != (gpointer)-1) {
+ free_stack = (guint8*)(MONO_CONTEXT_GET_BP (&ctx)) - (guint8*)(MONO_CONTEXT_GET_BP (&initial_ctx));
+ }
+
+ /* todo: we should call abort if ji is -1 */
+ ctx = new_ctx;
+ } while (free_stack < 64 * 1024 && ji != (gpointer) -1);
+
+ /* convert into sigcontext to be used in mono_arch_handle_exception */
+ mono_arch_monoctx_to_sigctx (&ctx, sctx);
+
+ /* todo: install new stack-guard page */
+
+ /* use the new stack and call mono_arch_handle_exception () */
+ restore_stack (sctx);
+}
+
/*
* Unhandled Exception Filter
* Top-level per-process exception handler.
sctx->eip = ctx->Eip;
switch (er->ExceptionCode) {
+ case EXCEPTION_STACK_OVERFLOW:
+ win32_handle_stack_overflow (ep, sctx);
+ break;
case EXCEPTION_ACCESS_VIOLATION:
W32_SEH_HANDLE_EX(segv);
break;
void win32_seh_init()
{
+ /* install restore stack helper */
+ if (!restore_stack)
+ restore_stack = mono_win32_get_handle_stackoverflow ();
+
old_handler = SetUnhandledExceptionFilter(seh_handler);
}
return NULL;
}
+#ifdef __sun
+#define REG_EAX EAX
+#define REG_EBX EBX
+#define REG_ECX ECX
+#define REG_EDX EDX
+#define REG_EBP EBP
+#define REG_ESP ESP
+#define REG_ESI ESI
+#define REG_EDI EDI
+#define REG_EIP EIP
+#endif
+
void
mono_arch_sigctx_to_monoctx (void *sigctx, MonoContext *mctx)
{
#ifdef MONO_ARCH_USE_SIGACTION
ucontext_t *ctx = (ucontext_t*)sigctx;
-#if defined(__FreeBSD__)
- mctx->eax = ctx->uc_mcontext.mc_eax;
- mctx->ebx = ctx->uc_mcontext.mc_ebx;
- mctx->ecx = ctx->uc_mcontext.mc_ecx;
- mctx->edx = ctx->uc_mcontext.mc_edx;
- mctx->ebp = ctx->uc_mcontext.mc_ebp;
- mctx->esp = ctx->uc_mcontext.mc_esp;
- mctx->esi = ctx->uc_mcontext.mc_esi;
- mctx->edi = ctx->uc_mcontext.mc_edi;
- mctx->eip = ctx->uc_mcontext.mc_eip;
-#else
- mctx->eax = ctx->uc_mcontext.gregs [REG_EAX];
- mctx->ebx = ctx->uc_mcontext.gregs [REG_EBX];
- mctx->ecx = ctx->uc_mcontext.gregs [REG_ECX];
- mctx->edx = ctx->uc_mcontext.gregs [REG_EDX];
- mctx->ebp = ctx->uc_mcontext.gregs [REG_EBP];
- mctx->esp = ctx->uc_mcontext.gregs [REG_ESP];
- mctx->esi = ctx->uc_mcontext.gregs [REG_ESI];
- mctx->edi = ctx->uc_mcontext.gregs [REG_EDI];
- mctx->eip = ctx->uc_mcontext.gregs [REG_EIP];
-#endif
+ mctx->eax = UCONTEXT_REG_EAX (ctx);
+ mctx->ebx = UCONTEXT_REG_EBX (ctx);
+ mctx->ecx = UCONTEXT_REG_ECX (ctx);
+ mctx->edx = UCONTEXT_REG_EDX (ctx);
+ mctx->ebp = UCONTEXT_REG_EBP (ctx);
+ mctx->esp = UCONTEXT_REG_ESP (ctx);
+ mctx->esi = UCONTEXT_REG_ESI (ctx);
+ mctx->edi = UCONTEXT_REG_EDI (ctx);
+ mctx->eip = UCONTEXT_REG_EIP (ctx);
#else
struct sigcontext *ctx = (struct sigcontext *)sigctx;
#ifdef MONO_ARCH_USE_SIGACTION
ucontext_t *ctx = (ucontext_t*)sigctx;
-#if defined(__FreeBSD__)
- ctx->uc_mcontext.mc_eax = mctx->eax;
- ctx->uc_mcontext.mc_ebx = mctx->ebx;
- ctx->uc_mcontext.mc_ecx = mctx->ecx;
- ctx->uc_mcontext.mc_edx = mctx->edx;
- ctx->uc_mcontext.mc_ebp = mctx->ebp;
- ctx->uc_mcontext.mc_esp = mctx->esp;
- ctx->uc_mcontext.mc_esi = mctx->esi;
- ctx->uc_mcontext.mc_edi = mctx->edi;
- ctx->uc_mcontext.mc_eip = mctx->eip;
-
-#else
- ctx->uc_mcontext.gregs [REG_EAX] = mctx->eax;
- ctx->uc_mcontext.gregs [REG_EBX] = mctx->ebx;
- ctx->uc_mcontext.gregs [REG_ECX] = mctx->ecx;
- ctx->uc_mcontext.gregs [REG_EDX] = mctx->edx;
- ctx->uc_mcontext.gregs [REG_EBP] = mctx->ebp;
- ctx->uc_mcontext.gregs [REG_ESP] = mctx->esp;
- ctx->uc_mcontext.gregs [REG_ESI] = mctx->esi;
- ctx->uc_mcontext.gregs [REG_EDI] = mctx->edi;
- ctx->uc_mcontext.gregs [REG_EIP] = mctx->eip;
-#endif
+ UCONTEXT_REG_EAX (ctx) = mctx->eax;
+ UCONTEXT_REG_EBX (ctx) = mctx->ebx;
+ UCONTEXT_REG_ECX (ctx) = mctx->ecx;
+ UCONTEXT_REG_EDX (ctx) = mctx->edx;
+ UCONTEXT_REG_EBP (ctx) = mctx->ebp;
+ UCONTEXT_REG_ESP (ctx) = mctx->esp;
+ UCONTEXT_REG_ESI (ctx) = mctx->esi;
+ UCONTEXT_REG_EDI (ctx) = mctx->edi;
+ UCONTEXT_REG_EIP (ctx) = mctx->eip;
#else
struct sigcontext *ctx = (struct sigcontext *)sigctx;
{
#ifdef MONO_ARCH_USE_SIGACTION
ucontext_t *ctx = (ucontext_t*)sigctx;
-#if defined(__FreeBSD__)
- return (gpointer)ctx->uc_mcontext.mc_eip;
-#else
- return (gpointer)ctx->uc_mcontext.gregs [REG_EIP];
-#endif
+ return (gpointer)UCONTEXT_REG_EIP (ctx);
#else
struct sigcontext *ctx = sigctx;
return (gpointer)ctx->SC_EIP;