Skip to content

Commit

Permalink
Attempt to fix MODE=dbg Windows execve() flake
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jan 5, 2025
1 parent 7b67b20 commit 035b0e2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 85 deletions.
111 changes: 57 additions & 54 deletions libc/intrin/sig.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ __msabi extern typeof(VirtualProtectEx) *const __imp_VirtualProtectEx;
__msabi extern typeof(VirtualQuery) *const __imp_VirtualQuery;
__msabi extern typeof(WriteFile) *const __imp_WriteFile;

extern pthread_mutex_t __sig_worker_lock;
atomic_int __sig_worker_state;

textwindows static bool __sig_ignored_by_default(int sig) {
return sig == SIGURG || //
Expand Down Expand Up @@ -742,74 +742,77 @@ HAIRY static uint32_t __sig_worker(void *arg) {
STKSZ, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NOFORK);
for (;;) {
_pthread_mutex_lock(&__sig_worker_lock);

// dequeue all pending signals and fire them off. if there's no
// thread that can handle them then __sig_generate will requeue
// those signals back to __sig.process; hence the need for xchg
unsigned long sigs =
atomic_exchange_explicit(__sig.process, 0, memory_order_acq_rel);
while (sigs) {
int sig = bsfl(sigs) + 1;
sigs &= ~(1ull << (sig - 1));
__sig_generate(sig, SI_KERNEL);
}

// unblock stalled i/o signals in threads
_pthread_lock();
for (struct Dll *e = dll_first(_pthread_list); e;
e = dll_next(_pthread_list, e)) {
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
if (atomic_load_explicit(&pt->pt_status, memory_order_acquire) >=
kPosixThreadTerminated)
break;
if (atomic_load_explicit(&pt->pt_blocker, memory_order_acquire) &&
(atomic_load_explicit(&pt->tib->tib_sigpending,
memory_order_acquire) &
~atomic_load_explicit(&pt->pt_blkmask, memory_order_acquire)))
__sig_wake(pt, 0);
}
_pthread_unlock();
// ok sys_execve_nt() might disable this worker
if (~__sig_worker_state & 2) {

// dequeue all pending signals and fire them off. if there's no
// thread that can handle them then __sig_generate will requeue
// those signals back to __sig.process; hence the need for xchg
unsigned long sigs =
atomic_exchange_explicit(__sig.process, 0, memory_order_acq_rel);
while (sigs) {
int sig = bsfl(sigs) + 1;
sigs &= ~(1ull << (sig - 1));
__sig_generate(sig, SI_KERNEL);
}

// unblock stalled asynchronous signals in threads
for (;;) {
sigset_t pending, mask;
struct PosixThread *mark = 0;
// unblock stalled i/o signals in threads
_pthread_lock();
for (struct Dll *e = dll_first(_pthread_list); e;
e = dll_next(_pthread_list, e)) {
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
if (atomic_load_explicit(&pt->pt_status, memory_order_acquire) >=
kPosixThreadTerminated)
break;
pending = atomic_load_explicit(&pt->tib->tib_sigpending,
memory_order_acquire);
mask =
atomic_load_explicit(&pt->tib->tib_sigmask, memory_order_acquire);
if (pending & ~mask) {
_pthread_ref(pt);
mark = pt;
break;
}
if (atomic_load_explicit(&pt->pt_blocker, memory_order_acquire) &&
(atomic_load_explicit(&pt->tib->tib_sigpending,
memory_order_acquire) &
~atomic_load_explicit(&pt->pt_blkmask, memory_order_acquire)))
__sig_wake(pt, 0);
}
_pthread_unlock();
if (!mark)
break;
while (!atomic_compare_exchange_weak_explicit(
&mark->tib->tib_sigpending, &pending, pending & ~mask,
memory_order_acq_rel, memory_order_relaxed)) {
}
while ((pending = pending & ~mask)) {
int sig = bsfl(pending) + 1;
pending &= ~(1ull << (sig - 1));
__sig_killer(mark, sig, SI_KERNEL);

// unblock stalled asynchronous signals in threads
for (;;) {
sigset_t pending, mask;
struct PosixThread *mark = 0;
_pthread_lock();
for (struct Dll *e = dll_first(_pthread_list); e;
e = dll_next(_pthread_list, e)) {
struct PosixThread *pt = POSIXTHREAD_CONTAINER(e);
if (atomic_load_explicit(&pt->pt_status, memory_order_acquire) >=
kPosixThreadTerminated)
break;
pending = atomic_load_explicit(&pt->tib->tib_sigpending,
memory_order_acquire);
mask =
atomic_load_explicit(&pt->tib->tib_sigmask, memory_order_acquire);
if (pending & ~mask) {
_pthread_ref(pt);
mark = pt;
break;
}
}
_pthread_unlock();
if (!mark)
break;
while (!atomic_compare_exchange_weak_explicit(
&mark->tib->tib_sigpending, &pending, pending & ~mask,
memory_order_acq_rel, memory_order_relaxed)) {
}
while ((pending = pending & ~mask)) {
int sig = bsfl(pending) + 1;
pending &= ~(1ull << (sig - 1));
__sig_killer(mark, sig, SI_KERNEL);
}
_pthread_unref(mark);
}
_pthread_unref(mark);
}

// wait until next scheduler quantum
_pthread_mutex_unlock(&__sig_worker_lock);
__sig_worker_state |= 1;
Sleep(POLL_INTERVAL_MS);
__sig_worker_state &= ~1;
}
__builtin_unreachable();
}
Expand Down
22 changes: 0 additions & 22 deletions libc/intrin/siglock.c

This file was deleted.

13 changes: 8 additions & 5 deletions libc/proc/execve-nt.greg.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/assert.h"
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
Expand Down Expand Up @@ -57,11 +58,10 @@
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
__msabi extern typeof(TerminateProcess) *const __imp_TerminateProcess;

extern pthread_mutex_t __sig_worker_lock;
extern atomic_int __sig_worker_state;

static void sys_execve_nt_abort(sigset_t sigmask) {
_pthread_unlock();
_pthread_mutex_unlock(&__sig_worker_lock);
__sig_worker_state &= ~2;
__sig_unblock(sigmask);
}

Expand All @@ -70,8 +70,10 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],

// execve() needs to be @asyncsignalsafe
sigset_t sigmask = __sig_block();
_pthread_mutex_lock(&__sig_worker_lock); // order matters
_pthread_lock(); // order matters
__sig_worker_state |= 2;
for (;;)
if (__sig_worker_state & 1)
break;

// new process should be a child of our parent
int64_t hParentProcess =
Expand Down Expand Up @@ -176,6 +178,7 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
STRACE("warning: execve() lingering due to non-cosmo parent process");

// terminate other threads
_pthread_lock();
struct Dll *e;
struct PosixThread *me = _pthread_self();
for (e = dll_first(_pthread_list); e; e = dll_next(_pthread_list, e)) {
Expand Down
5 changes: 3 additions & 2 deletions libc/proc/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
│ PERFORMANCE OF THIS SOFTWARE. │
╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/sig.internal.h"
Expand Down Expand Up @@ -54,9 +55,9 @@

__msabi extern typeof(GetCurrentProcessId) *const __imp_GetCurrentProcessId;

extern atomic_int __sig_worker_state;
extern pthread_mutex_t __cxa_lock_obj;
extern pthread_mutex_t __pthread_lock_obj;
extern pthread_mutex_t __sig_worker_lock;

void __rand64_lock(void);
void __rand64_unlock(void);
Expand Down Expand Up @@ -191,7 +192,7 @@ static void fork_child(int ppid_win32, int ppid_cosmo) {
sys_read_nt_wipe_keystrokes();
__proc_wipe_and_reset();
__itimer_wipe_and_reset();
_pthread_mutex_wipe_np(&__sig_worker_lock);
atomic_init(&__sig_worker_state, 0);
if (_weaken(__sig_init))
_weaken(__sig_init)();
if (_weaken(sys_getppid_nt_wipe))
Expand Down
3 changes: 1 addition & 2 deletions test/libc/proc/execve_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ TEST(execve, testArgPassing) {
FormatInt32(ibuf, i);
GenBuf(buf, i);
SPAWN(vfork);
execve(prog, (char *const[]){(char *)prog, "-", ibuf, buf, 0},
(char *const[]){0});
execl(prog, prog, "-", ibuf, buf, NULL);
kprintf("execve failed: %m\n");
EXITS(0);
}
Expand Down

0 comments on commit 035b0e2

Please sign in to comment.