Skip to content

build: fix windows ctrl-c interrupt#913

Merged
pkova merged 2 commits intodevelopfrom
pkova/windows-ctrlc
Nov 12, 2025
Merged

build: fix windows ctrl-c interrupt#913
pkova merged 2 commits intodevelopfrom
pkova/windows-ctrlc

Conversation

@pkova
Copy link
Copy Markdown
Collaborator

@pkova pkova commented Nov 11, 2025

A few days ago I decided to fix the ctrl-c interrupts that had been broken on windows since I brought it back in #867. How hard could it be?

It turns out @locpyl-tidnyd was really cooking with gas here. The fundamental mismatch between unix and windows here is the following: ctrl-c on unix delivers a synchronous SIGINT signal whereas on windows it starts a new thread to handle the interrupt. A new thread! We can't deal with that!

HANDLE hthread = OpenThread(THREAD_ALL_ACCESS, FALSE, _tid);
if (!hthread) {
fprintf(stderr, "\r\nrsignal_raise: OpenThread(%lu): %lu\r\n", _tid, GetLastError());
return;
}
if (SuspendThread(hthread) < 0) {
fprintf(stderr, "\r\nrsignal_raise: SuspendThread(%lu): %lu\r\n", _tid, GetLastError());
goto cleanup;
}
oldfn(sig);

Ok looks like we suspend the main thread that's cool, what's oldfn?

void rsignal_post_longjmp(DWORD tid, intptr_t* builtin_jb)
{
HANDLE hthread = OpenThread(THREAD_ALL_ACCESS, FALSE, tid);
if (!hthread) {
fprintf(stderr, "\r\nrsignal: OpenThread(%lu): %lu\r\n", tid, GetLastError());
return;
}
CONTEXT context;
context.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
if (!GetThreadContext(hthread, &context)) {
fprintf(stderr, "\r\nrsignal: GetThreadContext(%lu): %lu\r\n", tid, GetLastError());
goto cleanup;
}
// see if the thread is currently handling a structured exception
// if so, let the handler (usually the libsigsegv handler) finish
// and set up the the signal to run at the exception resume point
// otherwise, passing a parameter to fn is completely unreliable
//
DWORD64 kibase;
PRUNTIME_FUNCTION ki = RtlLookupFunctionEntry(__imp_KiUserExceptionDispatcher, &kibase, NULL);
CONTEXT c = context;
while (1)
{
DWORD64 base, frame;
PRUNTIME_FUNCTION f = RtlLookupFunctionEntry(c.Rip, &base, NULL);
if (!f) break;
if (f == ki)
{
// KiUserExceptionDispatcher has a "bare" frame
// with $rsp pointing to the CONTEXT structure
//
((PCONTEXT)c.Rsp)->Rip = (DWORD64)_rsignal_longjmp;
((PCONTEXT)c.Rsp)->Rcx = (DWORD64)builtin_jb;
goto cleanup;
}
PVOID handler_data;
RtlVirtualUnwind(0, base, c.Rip, f, &c, &handler_data, &frame, NULL);
}
context.Rip = (DWORD64)_rsignal_longjmp;
context.Rcx = (DWORD64)builtin_jb;
if (!SetThreadContext(hthread, &context)) {
fprintf(stderr, "\r\nrsignal: SetThreadContext(%lu): %lu\r\n", tid, GetLastError());
goto cleanup;
}
cleanup:
CloseHandle(hthread);
}

We replace the instruction pointer on the main thread with our longjmp and pass it the first argument in rcx as per the windows x64 calling convention. If we are in the middle of an exception handler already we unwind the stack and hijack the CONTEXT structure (on the stack) that windows uses to restore execution state after the handler has run.

It turns out we have to use __builtin_longjmp when we do this crazy shit because the native longjmp on windows also tries to unwind the stack and gets really confused.

All in all I hope that this writeup will be useful to the next poor soul who looks at this stuff.

@pkova pkova requested a review from a team as a code owner November 11, 2025 22:05
@pkova pkova merged commit 2dabb54 into develop Nov 12, 2025
2 checks passed
@pkova pkova deleted the pkova/windows-ctrlc branch November 12, 2025 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant