From 4ac3097ae79440eb66c70b9ca8b28ad9f54963f5 Mon Sep 17 00:00:00 2001 From: Juan Wajnerman Date: Tue, 4 Feb 2020 10:44:18 -0300 Subject: [PATCH] Handle exceptions raised at `__crystal_sigfault_handler` --- src/signal.cr | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/signal.cr b/src/signal.cr index c17e67153585..e2d817baa223 100644 --- a/src/signal.cr +++ b/src/signal.cr @@ -328,9 +328,17 @@ fun __crystal_sigfault_handler(sig : LibC::Int, addr : Void*) # Determine if the SEGV was inside or 'near' the top of the stack # to check for potential stack overflow. 'Near' is a small # amount larger than a typical stack frame, 4096 bytes here. - stack_top = Pointer(Void).new(Fiber.current.@stack.address - 4096) + is_stack_overflow = + begin + stack_top = Pointer(Void).new(Fiber.current.@stack.address - 4096) + stack_bottom = Fiber.current.@stack_bottom + stack_top <= addr < stack_bottom + rescue e + LibC.dprintf 2, "Error while trying to determine if a stack overflow has occurred. Probable memory corrpution\n" + false + end - if stack_top <= addr < Fiber.current.@stack_bottom + if is_stack_overflow LibC.dprintf 2, "Stack overflow (e.g., infinite or very deep recursion)\n" else LibC.dprintf 2, "Invalid memory access (signal %d) at address 0x%lx\n", sig, addr