Skip to content

Commit

Permalink
fix force-throw ctrl-C on Windows (#51307)
Browse files Browse the repository at this point in the history
This was getting current-task on the wrong thread, which resulted in the
value being NULL and crashing.

Fixes #50325
  • Loading branch information
vtjnash authored and NHDaly committed Sep 20, 2023
1 parent 6bf51a1 commit 2e05c5e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/signals-win.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,18 @@ void restore_signals(void)
SetConsoleCtrlHandler(NULL, 0);
}

void jl_throw_in_ctx(jl_value_t *excpt, PCONTEXT ctxThread)
void jl_throw_in_ctx(jl_task_t *ct, jl_value_t *excpt, PCONTEXT ctxThread)
{
jl_task_t *ct = jl_current_task;
jl_ptls_t ptls = ct->ptls;
#if defined(_CPU_X86_64_)
DWORD64 Rsp = (ctxThread->Rsp & (DWORD64)-16) - 8;
#elif defined(_CPU_X86_)
DWORD32 Esp = (ctxThread->Esp & (DWORD32)-16) - 4;
#else
#error WIN16 not supported :P
#endif
if (!jl_get_safe_restore()) {
if (ct && !jl_get_safe_restore()) {
assert(excpt != NULL);
jl_ptls_t ptls = ct->ptls;
ptls->bt_size = 0;
if (excpt != jl_stackovf_exception) {
ptls->bt_size = rec_backtrace_ctx(ptls->bt_data, JL_MAX_BT_SIZE, ctxThread,
Expand Down Expand Up @@ -193,7 +192,8 @@ static void jl_try_deliver_sigint(void)
jl_safe_printf("error: GetThreadContext failed\n");
return;
}
jl_throw_in_ctx(jl_interrupt_exception, &ctxThread);
jl_task_t *ct = jl_atomic_load_relaxed(&ptls2->current_task);
jl_throw_in_ctx(ct, jl_interrupt_exception, &ctxThread);
ctxThread.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
if (!SetThreadContext(hMainThread, &ctxThread)) {
jl_safe_printf("error: SetThreadContext failed\n");
Expand Down Expand Up @@ -237,14 +237,14 @@ LONG WINAPI jl_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
case EXCEPTION_INT_DIVIDE_BY_ZERO:
if (ct->eh != NULL) {
fpreset();
jl_throw_in_ctx(jl_diverror_exception, ExceptionInfo->ContextRecord);
jl_throw_in_ctx(ct, jl_diverror_exception, ExceptionInfo->ContextRecord);
return EXCEPTION_CONTINUE_EXECUTION;
}
break;
case EXCEPTION_STACK_OVERFLOW:
if (ct->eh != NULL) {
ptls->needs_resetstkoflw = 1;
jl_throw_in_ctx(jl_stackovf_exception, ExceptionInfo->ContextRecord);
jl_throw_in_ctx(ct, jl_stackovf_exception, ExceptionInfo->ContextRecord);
return EXCEPTION_CONTINUE_EXECUTION;
}
break;
Expand All @@ -259,17 +259,17 @@ LONG WINAPI jl_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
}
else if (jl_safepoint_consume_sigint()) {
jl_clear_force_sigint();
jl_throw_in_ctx(jl_interrupt_exception, ExceptionInfo->ContextRecord);
jl_throw_in_ctx(ct, jl_interrupt_exception, ExceptionInfo->ContextRecord);
}
return EXCEPTION_CONTINUE_EXECUTION;
}
if (jl_get_safe_restore()) {
jl_throw_in_ctx(NULL, ExceptionInfo->ContextRecord);
jl_throw_in_ctx(NULL, NULL, ExceptionInfo->ContextRecord);
return EXCEPTION_CONTINUE_EXECUTION;
}
if (ct->eh != NULL) {
if (ExceptionInfo->ExceptionRecord->ExceptionInformation[0] == 1) { // writing to read-only memory (e.g. mmap)
jl_throw_in_ctx(jl_readonlymemory_exception, ExceptionInfo->ContextRecord);
jl_throw_in_ctx(ct, jl_readonlymemory_exception, ExceptionInfo->ContextRecord);
return EXCEPTION_CONTINUE_EXECUTION;
}
}
Expand Down

0 comments on commit 2e05c5e

Please sign in to comment.