Skip to content

Commit

Permalink
add unconditional warning before handling StackOverflow
Browse files Browse the repository at this point in the history
Since this is not modeled by the exception logic, and it can interrupt
arbitrary program state or corrupt locks (leading to hangs and other
issues), as well as just frequently segfaulting afterwards, give a
printed message as soon as we notice things are going badly before
attempting to recover.
  • Loading branch information
vtjnash committed Jun 4, 2024
1 parent f613ee3 commit 21b1e15
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/signal-handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ static void jl_check_profile_autostop(void)
}
}

static void stack_overflow_warning(void)
{
jl_safe_printf("Warning: detected a stack overflow; program state may be corrupted, so further execution might be unreliable.\n");
}

#if defined(_WIN32)
#include "signals-win.c"
#else
Expand Down
3 changes: 2 additions & 1 deletion src/signals-mach.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ kern_return_t catch_mach_exception_raise(
// XXX: jl_throw_in_thread or segv_handler will eventually check this, but
// we would like to avoid some of this work if we could detect this earlier
// if (jl_has_safe_restore(ptls2)) {
// jl_throw_in_thread(ptls2, thread, jl_stackovf_exception);
// jl_throw_in_thread(ptls2, thread, NULL);
// return KERN_SUCCESS;
// }
if (jl_atomic_load_acquire(&ptls2->gc_state) == JL_GC_STATE_WAITING)
Expand Down Expand Up @@ -385,6 +385,7 @@ kern_return_t catch_mach_exception_raise(
return KERN_FAILURE;
jl_value_t *excpt;
if (is_addr_on_stack(jl_atomic_load_relaxed(&ptls2->current_task), (void*)fault_addr)) {
stack_overflow_warning();
excpt = jl_stackovf_exception;
}
else if (is_write_fault(exc_state)) // false for alignment errors
Expand Down
1 change: 1 addition & 0 deletions src/signals-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ JL_NO_ASAN static void segv_handler(int sig, siginfo_t *info, void *context)
if (ct->eh == NULL)
sigdie_handler(sig, info, context);
if ((sig != SIGBUS || info->si_code == BUS_ADRERR) && is_addr_on_stack(ct, info->si_addr)) { // stack overflow and not a BUS_ADRALN (alignment error)
stack_overflow_warning();
jl_throw_in_ctx(ct, jl_stackovf_exception, sig, context);
}
else if (jl_is_on_sigstack(ct->ptls, info->si_addr, context)) {
Expand Down
1 change: 1 addition & 0 deletions src/signals-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ LONG WINAPI jl_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
case EXCEPTION_STACK_OVERFLOW:
if (ct->eh != NULL) {
ptls->needs_resetstkoflw = 1;
stack_overflow_warning();
jl_throw_in_ctx(ct, jl_stackovf_exception, ExceptionInfo->ContextRecord);
return EXCEPTION_CONTINUE_EXECUTION;
}
Expand Down

0 comments on commit 21b1e15

Please sign in to comment.