From 4bcd4e3cca3a4ff2398b2f327a4c21c1ed678b27 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Fri, 23 Oct 2026 09:02:47 -0400 Subject: [PATCH] perf: droiddeck-ntsync without signal-mask syscalls, waits that skip the queue, and per-thread device references --- README.md | 2 +- tools/linuxfs/preload/ntsync.c | 283 +++++++++++++----- tools/tests/test_ntsync_preload.py | 456 +++++++++++++++++++++++++++++ 3 files changed, 671 insertions(+), 70 deletions(-) create mode 100644 tools/tests/test_ntsync_preload.py diff --git a/README.md b/README.md index fb86fa653..7fd8cfbdb 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Compatibility and performance vary by device; hardware validation is limited. De ## Credits and licence -GPL-3.0. Runtime, shim, input, and controller work build on WinNative and Bannerlator (maxjivi05). Epic Games and GOG support build on [Phobos’s](https://github.com/phobos665) adaptation of [Legendary](https://github.com/legendary-gl/legendary) and [gogdl](https://github.com/Heroic-Games-Launcher/heroic-gogdl), respectively. The registry entries a GOG game's install script (`goggame-.script`) sets are read the way [Heroic Games Launcher](https://github.com/Heroic-Games-Launcher/HeroicGamesLauncher)'s GOG setup (GPL-3.0) reads them. LSFG frame generation is from the work of Camille LaVey and the [Eden](https://eden-emu.dev) emulator project, following [lsfg-vk](https://github.com/PancakeTAS/lsfg-vk), ported to WinNative and DroidDeck by [@maxjivi05](https://github.com/maxjivi05); it needs your own copy of [Lossless Scaling](https://store.steampowered.com/app/993090/) and ships none of its shaders. x86 AppImages, and any whose own runtime cannot unpack them, are unpacked with [uruntime](https://github.com/VHSgunzo/uruntime) by VHSgunzo (MIT), shipped unmodified with its licence, which also starts the arm64 emulator AppImages whose own runtime cannot run its extractor on Android. The Steam client's audio goes through [@The412Banner](https://github.com/The412Banner)'s [DirectAudio](https://github.com/The412Banner/directaudio) PulseAudio sink (LGPL-2.1-or-later), built from `tools/directaudio/sink` with its request size kept to its ring so clients are not asked to queue 100 ms ahead. mmap ring-buffer controller input was developed by ribbit384 ([WinNative #345](https://github.com/WinNative-Emu/WinNative/pull/345)). Why the x86-64 Steam client refused the arm64 client's sign-in was checked by reading Steam's saved token with the `ConnectCache` format from [deadlock-api-ingest](https://github.com/deadlock-api/deadlock-api-ingest). x86 programs that link OpenSSL 3 under FEX use the copy in the [Steam Linux Runtime 4.0](https://gitlab.steamos.cloud/steamrt/steamrt) platform. Hooks written with `WriteProcessMemory` into code [FEX](https://github.com/FEX-Emu/FEX) has translated go through `/proc//mem` again when the kernel refuses `process_vm_writev`, as [Wine](https://gitlab.winehq.org/wine/wine)'s `server/ptrace.c` wrote them before Wine 11. x86 Linux games under FEX get the same `/etc/asound.conf` that [pressure-vessel](https://gitlab.steamos.cloud/steamrt/steam-runtime-tools) writes into the Steam Linux Runtime, so ALSA plays through PulseAudio. Games render with [Mesa](https://gitlab.freedesktop.org/mesa/mesa) Turnip's own per-render-pass choice between tile memory and system memory, while the Steam client keeps `TU_DEBUG=sysmem`; [FEX](https://github.com/FEX-Emu/FEX)'s code disk cache was checked in its source and is already on through Proton. The proot fast path picks the guest name of a host file bound under several names in the order [PRoot](https://github.com/proot-me/proot)'s `path/binding.c` (GPL-2.0-or-later) sorts its bindings, so links and `/proc/self/fd` read back as they do through proot. The session's [PipeWire](https://gitlab.freedesktop.org/pipewire/pipewire) (MIT) opens its sockets with the per-socket access settings documented in its `module-access.c`, and [WirePlumber](https://gitlab.freedesktop.org/pipewire/wireplumber) (MIT) keeps those clients at full permissions through its `access.rules`. Wine write watches use the userfaultfd support in [Proton](https://github.com/ValveSoftware/wine)'s `dlls/ntdll/unix/virtual.c` (LGPL-2.1-or-later): libblsession gives it the arm64 syscall number and answers its `PAGEMAP_SCAN` from `/proc/self/pagemap` where Android refuses the ioctl. Wine on arm64 sets a thread's full context by raising `SIGUSR2` from `syscall_dispatcher_return_slowpath` in [Wine](https://gitlab.winehq.org/wine/wine)'s `dlls/ntdll/unix/signal_arm64.c` (LGPL-2.1-or-later); libblsession runs that handler in place on a frame laid out as the [Linux](https://github.com/torvalds/linux) kernel's `arch/arm64/kernel/signal.c` builds it and returns through `rt_sigreturn`, so proot does not stop the thread for it. Wine's process list reads each process's memory counters from `/proc//status` in `fill_vm_counters` ([Wine](https://gitlab.winehq.org/wine/wine) `dlls/ntdll/unix/process.c`, LGPL-2.1-or-later), so libblsession gives those reads the kernel's file instead of a copy with `TracerPid` cleared. See [LICENSE](LICENSE). Steam and Proton belong to Valve Corporation; this project is not affiliated with Valve. +GPL-3.0. Runtime, shim, input, and controller work build on WinNative and Bannerlator (maxjivi05). Epic Games and GOG support build on [Phobos’s](https://github.com/phobos665) adaptation of [Legendary](https://github.com/legendary-gl/legendary) and [gogdl](https://github.com/Heroic-Games-Launcher/heroic-gogdl), respectively. The registry entries a GOG game's install script (`goggame-.script`) sets are read the way [Heroic Games Launcher](https://github.com/Heroic-Games-Launcher/HeroicGamesLauncher)'s GOG setup (GPL-3.0) reads them. LSFG frame generation is from the work of Camille LaVey and the [Eden](https://eden-emu.dev) emulator project, following [lsfg-vk](https://github.com/PancakeTAS/lsfg-vk), ported to WinNative and DroidDeck by [@maxjivi05](https://github.com/maxjivi05); it needs your own copy of [Lossless Scaling](https://store.steampowered.com/app/993090/) and ships none of its shaders. x86 AppImages, and any whose own runtime cannot unpack them, are unpacked with [uruntime](https://github.com/VHSgunzo/uruntime) by VHSgunzo (MIT), shipped unmodified with its licence, which also starts the arm64 emulator AppImages whose own runtime cannot run its extractor on Android. The Steam client's audio goes through [@The412Banner](https://github.com/The412Banner)'s [DirectAudio](https://github.com/The412Banner/directaudio) PulseAudio sink (LGPL-2.1-or-later), built from `tools/directaudio/sink` with its request size kept to its ring so clients are not asked to queue 100 ms ahead. mmap ring-buffer controller input was developed by ribbit384 ([WinNative #345](https://github.com/WinNative-Emu/WinNative/pull/345)). Why the x86-64 Steam client refused the arm64 client's sign-in was checked by reading Steam's saved token with the `ConnectCache` format from [deadlock-api-ingest](https://github.com/deadlock-api/deadlock-api-ingest). x86 programs that link OpenSSL 3 under FEX use the copy in the [Steam Linux Runtime 4.0](https://gitlab.steamos.cloud/steamrt/steamrt) platform. Hooks written with `WriteProcessMemory` into code [FEX](https://github.com/FEX-Emu/FEX) has translated go through `/proc//mem` again when the kernel refuses `process_vm_writev`, as [Wine](https://gitlab.winehq.org/wine/wine)'s `server/ptrace.c` wrote them before Wine 11. x86 Linux games under FEX get the same `/etc/asound.conf` that [pressure-vessel](https://gitlab.steamos.cloud/steamrt/steam-runtime-tools) writes into the Steam Linux Runtime, so ALSA plays through PulseAudio. Games render with [Mesa](https://gitlab.freedesktop.org/mesa/mesa) Turnip's own per-render-pass choice between tile memory and system memory, while the Steam client keeps `TU_DEBUG=sysmem`; [FEX](https://github.com/FEX-Emu/FEX)'s code disk cache was checked in its source and is already on through Proton. The proot fast path picks the guest name of a host file bound under several names in the order [PRoot](https://github.com/proot-me/proot)'s `path/binding.c` (GPL-2.0-or-later) sorts its bindings, so links and `/proc/self/fd` read back as they do through proot. The session's [PipeWire](https://gitlab.freedesktop.org/pipewire/pipewire) (MIT) opens its sockets with the per-socket access settings documented in its `module-access.c`, and [WirePlumber](https://gitlab.freedesktop.org/pipewire/wireplumber) (MIT) keeps those clients at full permissions through its `access.rules`. Wine write watches use the userfaultfd support in [Proton](https://github.com/ValveSoftware/wine)'s `dlls/ntdll/unix/virtual.c` (LGPL-2.1-or-later): libblsession gives it the arm64 syscall number and answers its `PAGEMAP_SCAN` from `/proc/self/pagemap` where Android refuses the ioctl. Wine on arm64 sets a thread's full context by raising `SIGUSR2` from `syscall_dispatcher_return_slowpath` in [Wine](https://gitlab.winehq.org/wine/wine)'s `dlls/ntdll/unix/signal_arm64.c` (LGPL-2.1-or-later); libblsession runs that handler in place on a frame laid out as the [Linux](https://github.com/torvalds/linux) kernel's `arch/arm64/kernel/signal.c` builds it and returns through `rt_sigreturn`, so proot does not stop the thread for it. Wine's process list reads each process's memory counters from `/proc//status` in `fill_vm_counters` ([Wine](https://gitlab.winehq.org/wine/wine) `dlls/ntdll/unix/process.c`, LGPL-2.1-or-later), so libblsession gives those reads the kernel's file instead of a copy with `TracerPid` cleared. droiddeck-ntsync is checked against the object, wait and error semantics documented for the [Linux](https://github.com/torvalds/linux) ntsync driver in `Documentation/userspace-api/ntsync.rst` (GPL-2.0). See [LICENSE](LICENSE). Steam and Proton belong to Valve Corporation; this project is not affiliated with Valve. XR glasses support builds on WinNative's VITURE glasses work. VITURE protocol details were checked against [XRLinuxDriver](https://github.com/wheaney/XRLinuxDriver) by [@wheaney](https://github.com/wheaney), including the VITURE protocol header it ships, [ar-drivers-rs](https://github.com/badicsalex/ar-drivers-rs) by [@badicsalex](https://github.com/badicsalex) and the [go-macos/iokit VITURE package](https://pkg.go.dev/github.com/go-macos/iokit/viture); the optional VITURE SDK belongs to VITURE and is not included. RayNeo support adapts [@informalTechCode](https://github.com/informalTechCode)'s [RayNeo-Air-Series-3DoF-Scaffold](https://github.com/informalTechCode/RayNeo-Air-Series-3DoF-Scaffold) (Apache-2.0) and references [@verncat](https://github.com/verncat)'s [RayNeo-Air-3S-Pro-OpenVR](https://github.com/verncat/RayNeo-Air-3S-Pro-OpenVR) (MIT) and [Monado](https://gitlab.freedesktop.org/monado/monado) (BSL-1.0); see [docs/third-party/rayneo](docs/third-party/rayneo/README.md). XREAL 1S support references [XRLinuxDriver](https://github.com/wheaney/XRLinuxDriver) and [xrealOneDeviceKit](https://github.com/wheaney/xrealOneDeviceKit) by @wheaney, [xreal_one_driver](https://github.com/rohitsangwan01/xreal_one_driver) by [@rohitsangwan01](https://github.com/rohitsangwan01) (MIT) and [xr-tools](https://github.com/0xcaff/xr-tools) by [@0xcaff](https://github.com/0xcaff). Rokid support follows the Rokid protocol notes in [ar-drivers-rs](https://github.com/badicsalex/ar-drivers-rs) and @badicsalex's [Void Computing write-ups](https://voidcomputing.hu/blog/worse-better-prettier/), and [Monado](https://gitlab.freedesktop.org/monado/monado)'s Rokid driver. The REDMAGIC glasses-display findings draw on [@mekhontsev](https://github.com/mekhontsev)'s [MagicDesk investigation](https://github.com/mekhontsev/magicdesk/blob/main/docs/xr-resolution-fix.md) and [@Coding-BR](https://github.com/Coding-BR)'s [decompiled Nubia kernel](https://github.com/Coding-BR/android_kernel_nubia_sm8850_qwjujube). VITURE, RayNeo, XREAL, Rokid and REDMAGIC are trademarks of their owners; this project is not affiliated with them. diff --git a/tools/linuxfs/preload/ntsync.c b/tools/linuxfs/preload/ntsync.c index b920b49f9..2b1c79045 100644 --- a/tools/linuxfs/preload/ntsync.c +++ b/tools/linuxfs/preload/ntsync.c @@ -68,12 +68,13 @@ struct ns_wait_args { #define NS_PROCS 256u #define NS_FDS 65536 #define NS_DEVS 8 -#define NS_VERSION 1u +#define NS_VERSION 2u #define NS_HEAD_MAGIC 0x31434e5953544e44ull #define NS_DEV_MAGIC 0x4e54445600000000ull #define NS_OBJ_MAGIC 0x27ull #define NS_WAITERS 0x80000000u #define NS_SWEEP_NS 2000000000ull +#define NS_SPIN_NS 4000ull #define NS_DEAD 0xffffffffu #ifndef CLOSE_RANGE_CLOEXEC @@ -144,6 +145,7 @@ struct ns_q { uint32_t waiting; uint32_t inuse; uint32_t next_free; + uint32_t sleeping; struct ns_ent ent[NS_ENTS]; }; @@ -255,19 +257,104 @@ static void ns_unlock(uint32_t *w) { } } -static void ns_block(sigset_t *old) { - sigset_t s; - sigfillset(&s); - sigdelset(&s, SIGSEGV); - sigdelset(&s, SIGBUS); - sigdelset(&s, SIGILL); - sigdelset(&s, SIGFPE); - sigdelset(&s, SIGTRAP); - pthread_sigmask(SIG_BLOCK, &s, old); +#define NS_DEFER 4 + +struct ns_cs { + int depth; + int count; + int sigs[NS_DEFER]; + siginfo_t info[NS_DEFER]; +}; + +static __thread struct ns_cs ns_cs; +static uintptr_t ns_handlers[_NSIG]; +static uint8_t ns_siginfo[_NSIG]; + +int bl_from_ntdll(const void *caller) __attribute__((visibility("hidden"))); + +static int ns_deferrable(int sig) { + return sig > 0 && sig < _NSIG && sig != SIGKILL && sig != SIGSTOP && sig != SIGSEGV && sig != SIGBUS && sig != SIGILL + && sig != SIGFPE && sig != SIGTRAP && sig != SIGSYS; } -static void ns_restore(const sigset_t *old) { - pthread_sigmask(SIG_SETMASK, old, NULL); +static int ns_defer(int sig, const siginfo_t *si) { + struct ns_cs *cs = &ns_cs; + if (sig < SIGRTMIN) + for (int i = 0; i < cs->count; i++) + if (cs->sigs[i] == sig) return 1; + if (cs->count >= NS_DEFER) return 0; + cs->sigs[cs->count] = sig; + if (si) memcpy(&cs->info[cs->count], si, sizeof(*si)); + else memset(&cs->info[cs->count], 0, sizeof(*si)); + cs->count++; + return 1; +} + +static void ns_trampoline(int sig, siginfo_t *si, void *uc) { + if (__atomic_load_n(&ns_cs.depth, __ATOMIC_RELAXED) > 0 && ns_defer(sig, si)) return; + uintptr_t h = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE); + if (!h) return; + if (__atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED)) ((void (*)(int, siginfo_t *, void *))h)(sig, si, uc); + else ((void (*)(int))h)(sig); +} + +static void ns_flush(void) { + struct ns_cs *cs = &ns_cs; + int n = cs->count, sigs[NS_DEFER]; + siginfo_t info[NS_DEFER]; + memcpy(sigs, cs->sigs, sizeof(sigs)); + memcpy(info, cs->info, sizeof(info)); + cs->count = 0; + int saved = errno; + pid_t pid = getpid(), tid = (pid_t)ns_self(); + for (int i = 0; i < n; i++) + if (syscall(SYS_rt_tgsigqueueinfo, pid, tid, sigs[i], &info[i]) != 0) syscall(SYS_tgkill, pid, tid, sigs[i]); + errno = saved; +} + +static void ns_block(void) { + ns_cs.depth++; + __atomic_signal_fence(__ATOMIC_SEQ_CST); +} + +static void ns_restore(void) { + __atomic_signal_fence(__ATOMIC_SEQ_CST); + if (--ns_cs.depth == 0) { + __atomic_signal_fence(__ATOMIC_SEQ_CST); + if (ns_cs.count) ns_flush(); + } +} + +int sigaction(int sig, const struct sigaction *act, struct sigaction *old) { + static int (*real)(int, const struct sigaction *, struct sigaction *); + if (!real) real = (int (*)(int, const struct sigaction *, struct sigaction *))dlsym(RTLD_NEXT, "sigaction"); + if (!real) { + errno = ENOSYS; + return -1; + } + if (!ns_deferrable(sig) || !ns_on()) return real(sig, act, old); + uintptr_t prev = __atomic_load_n(&ns_handlers[sig], __ATOMIC_ACQUIRE); + uint8_t prev_info = __atomic_load_n(&ns_siginfo[sig], __ATOMIC_RELAXED); + struct sigaction mine; + const struct sigaction *use = act; + if (act && act->sa_handler != SIG_DFL && act->sa_handler != SIG_IGN && bl_from_ntdll(__builtin_return_address(0))) { + mine = *act; + mine.sa_sigaction = ns_trampoline; + mine.sa_flags |= SA_SIGINFO; + __atomic_store_n(&ns_siginfo[sig], (act->sa_flags & SA_SIGINFO) ? 1 : 0, __ATOMIC_RELAXED); + __atomic_store_n(&ns_handlers[sig], (uintptr_t)act->sa_sigaction, __ATOMIC_RELEASE); + use = &mine; + } + int r = real(sig, use, old); + if (r != 0 && use == &mine) { + __atomic_store_n(&ns_siginfo[sig], prev_info, __ATOMIC_RELAXED); + __atomic_store_n(&ns_handlers[sig], prev, __ATOMIC_RELEASE); + } + if (r == 0 && old && old->sa_sigaction == ns_trampoline) { + old->sa_sigaction = (void (*)(int, siginfo_t *, void *))prev; + if (!prev_info) old->sa_flags &= ~SA_SIGINFO; + } + return r; } static inline struct ns_obj *ns_obj(struct ns_head *h, uint32_t s) { @@ -378,10 +465,11 @@ static int ns_claim(struct ns_q *q, int32_t index) { if (dead) return 0; } int32_t want = -1; - return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_ACQ_REL, __ATOMIC_RELAXED); + return __atomic_compare_exchange_n(&q->signaled, &want, index, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED); } static void ns_wake(struct ns_q *q) { + if (!__atomic_load_n(&q->sleeping, __ATOMIC_SEQ_CST)) return; int saved = errno; ns_futex(&q->signaled, FUTEX_WAKE, 1, NULL, 0); errno = saved; @@ -618,14 +706,13 @@ static void ns_dev_put_locked(struct ns_dev *d) { static void ns_dev_put(struct ns_dev *d) { if (__atomic_sub_fetch(&d->refs, 1, __ATOMIC_ACQ_REL)) return; - sigset_t old; int saved = errno; - ns_block(&old); + ns_block(); pthread_mutex_lock(&ns_mx); uint32_t zero = 0; if (d->h && __atomic_compare_exchange_n(&d->refs, &zero, NS_DEAD, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) ns_release(d); pthread_mutex_unlock(&ns_mx); - ns_restore(&old); + ns_restore(); errno = saved; } @@ -682,20 +769,18 @@ static void ns_forget_locked(int fd) { } static void ns_forget(int fd) { - sigset_t old; int saved = errno; - ns_block(&old); + ns_block(); pthread_mutex_lock(&ns_mx); ns_forget_locked(fd); pthread_mutex_unlock(&ns_mx); - ns_restore(&old); + ns_restore(); errno = saved; } static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, uint32_t s) { uint64_t e = 0; - sigset_t old; - ns_block(&old); + ns_block(); pthread_mutex_lock(&ns_mx); if (!d->h || d->tag != tag || !ns_table() || ns_attach(d) < 0 || s >= NS_SLOTS) goto out; if ((e = __atomic_load_n(&ns_fdt[fd], __ATOMIC_ACQUIRE))) goto out; @@ -710,7 +795,7 @@ static uint64_t ns_adopt(struct ns_dev *d, uint32_t tag, int fd, uint32_t gen, u ns_unlock(&o->lock); out: pthread_mutex_unlock(&ns_mx); - ns_restore(&old); + ns_restore(); return e; } @@ -719,8 +804,7 @@ static uint64_t ns_note_device(int fd) { uint64_t e = 0; if (syscall(SYS_fstat, fd, &st) != 0 || !S_ISREG(st.st_mode)) return 0; if (((uint64_t)st.st_size & ~0xffffull) != NS_DEV_MAGIC) return 0; - sigset_t old; - ns_block(&old); + ns_block(); pthread_mutex_lock(&ns_mx); struct ns_dev *d = ns_table() ? ns_map(fd, (uint32_t)st.st_size & 0xffff, (uint64_t)st.st_ino) : NULL; if (d) { @@ -732,7 +816,7 @@ static uint64_t ns_note_device(int fd) { } } pthread_mutex_unlock(&ns_mx); - ns_restore(&old); + ns_restore(); return e; } @@ -773,15 +857,14 @@ static int ns_ready(struct ns_dev *d) { static void ns_thread_gone(void *p) { struct ns_tls *t = p; - if (!t || !t->d || !t->q) return; - sigset_t old; + if (!t || !t->d) return; struct ns_dev *d = t->d; - ns_block(&old); - ns_free_q(d->h, t->q - 1); + ns_block(); + if (t->q) ns_free_q(d->h, t->q - 1); t->q = 0; t->waiting = 0; t->d = NULL; - ns_restore(&old); + ns_restore(); ns_dev_put(d); } @@ -789,24 +872,36 @@ static void ns_key_init(void) { pthread_key_create(&ns_key, ns_thread_gone); } -static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) { - struct ns_head *h = d->h; - if (ns_tq.d && ns_tq.q && ns_tq.waiting) { - ns_drop_q(ns_tq.d->h, ns_tq.q - 1); - ns_tq.waiting = 0; - } - if (ns_tq.d == d && ns_tq.q) { - *qi = ns_tq.q - 1; - return ns_q(h, *qi); - } - if (ns_tq.d && ns_tq.q) { - struct ns_dev *old = ns_tq.d; - ns_free_q(old->h, ns_tq.q - 1); +static int ns_hold(struct ns_dev *d) { + if (ns_tq.d == d) return 1; + if (!ns_dev_get(d)) return 0; + struct ns_dev *old = ns_tq.d; + if (old) { + ns_block(); + if (ns_tq.q) ns_free_q(old->h, ns_tq.q - 1); ns_tq.q = 0; + ns_tq.waiting = 0; ns_tq.d = NULL; + ns_restore(); ns_dev_put(old); } pthread_once(&ns_once, ns_key_init); + ns_tq.d = d; + pthread_setspecific(ns_key, &ns_tq); + return 1; +} + +static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) { + struct ns_head *h = d->h; + if (ns_tq.d != d) return NULL; + if (ns_tq.q && ns_tq.waiting) { + ns_drop_q(h, ns_tq.q - 1); + ns_tq.waiting = 0; + } + if (ns_tq.q) { + *qi = ns_tq.q - 1; + return ns_q(h, *qi); + } uint32_t i; if (!ns_pop(h, &h->q_free, 0, &i) && !ns_grow(&h->q_hwm, NS_QS, &i)) { ns_sweep_dead(h); @@ -815,22 +910,19 @@ static struct ns_q *ns_thread_q(struct ns_dev *d, uint32_t *qi) { struct ns_q *q = ns_q(h, i); q->proc = (uint32_t)d->proc; q->waiting = 0; + q->sleeping = 0; q->inuse = 1; - __atomic_add_fetch(&d->refs, 1, __ATOMIC_ACQ_REL); - ns_tq.d = d; ns_tq.q = i + 1; ns_tq.waiting = 0; - pthread_setspecific(ns_key, &ns_tq); *qi = i; return q; } static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) { struct ns_head *h = d->h; - sigset_t old; int fd = -1, err = 0; uint32_t s = 0; - ns_block(&old); + ns_block(); pthread_mutex_lock(&ns_mx); if (!ns_table() || ns_attach(d) < 0) { err = ENOMEM; @@ -879,7 +971,7 @@ static int ns_create(struct ns_dev *d, int type, uint32_t a, uint32_t b) { ns_unlock(&o->lock); out: pthread_mutex_unlock(&ns_mx); - ns_restore(&old); + ns_restore(); if (fd < 0) errno = err; return fd; } @@ -890,6 +982,20 @@ static int ns_expired(const struct ns_wait_args *args) { return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec >= args->timeout; } +static uint64_t ns_now(void) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + return (uint64_t)now.tv_sec * 1000000000ull + (uint64_t)now.tv_nsec; +} + +static void ns_spin(struct ns_q *q) { + uint64_t end = ns_now() + NS_SPIN_NS; + for (unsigned i = 1; __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1; i++) { + ns_relax(); + if (!(i & 63) && ns_now() >= end) return; + } +} + static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) { struct timespec ts, *tsp = NULL; int op = FUTEX_WAIT_BITSET | ((args->flags & NS_WAIT_REALTIME) ? FUTEX_CLOCK_REALTIME : 0); @@ -898,14 +1004,45 @@ static int ns_sleep(struct ns_q *q, const struct ns_wait_args *args) { ts.tv_nsec = (long)(args->timeout % 1000000000ull); tsp = &ts; } - while (__atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) { - if (tsp && ns_expired(args)) return ETIMEDOUT; - if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1) { - if (errno == ETIMEDOUT) return ETIMEDOUT; - if (errno == EINTR) return EINTR; - } + ns_spin(q); + int ret = 0; + __atomic_store_n(&q->sleeping, 1, __ATOMIC_SEQ_CST); + while (!ret && __atomic_load_n(&q->signaled, __ATOMIC_SEQ_CST) == -1) { + if (tsp && ns_expired(args)) ret = ETIMEDOUT; + else if (ns_futex(&q->signaled, op, (uint32_t)-1, tsp, FUTEX_BITSET_MATCH_ANY) == -1 && (errno == ETIMEDOUT || errno == EINTR)) + ret = errno; } - return 0; + __atomic_store_n(&q->sleeping, 0, __ATOMIC_RELAXED); + return ret; +} + +static int ns_try_any(struct ns_head *h, const uint32_t *slots, const uint32_t *gens, uint32_t total, uint32_t owner, int *dead) { + for (uint32_t i = 0; i < total; i++) { + struct ns_obj *o = ns_obj(h, slots[i]); + ns_lock(&o->lock); + int busy = o->type == NS_FREE || o->gen != gens[i] || o->any_head || o->all_hint; + if (!busy && ns_signaled(o, owner)) { + switch (o->type) { + case NS_SEM: + o->a--; + break; + case NS_MUTEX: + *dead = (int)o->ownerdead; + o->ownerdead = 0; + o->a++; + o->b = owner; + break; + case NS_EVENT: + if (!o->a) o->b = 0; + break; + } + ns_unlock(&o->lock); + return (int)i; + } + ns_unlock(&o->lock); + if (busy) return -1; + } + return -2; } static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) { @@ -927,12 +1064,23 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) { for (uint32_t j = 0; j < i; j++) if (slots[j] == slots[i]) return EINVAL; } - sigset_t old; - ns_block(&old); + ns_block(); + if (!all && !ns_tq.waiting) { + int dead = 0, got = ns_try_any(h, slots, gens, total, args.owner, &dead); + if (got >= 0) { + ns_restore(); + ua->index = (uint32_t)got; + return dead ? EOWNERDEAD : 0; + } + if (got == -2 && args.timeout != UINT64_MAX && ns_expired(&args)) { + ns_restore(); + return ETIMEDOUT; + } + } uint32_t qi; struct ns_q *q = ns_ready(d) ? ns_thread_q(d, &qi) : NULL; if (!q) { - ns_restore(&old); + ns_restore(); return ENOMEM; } ns_maybe_sweep(h); @@ -1004,16 +1152,15 @@ static int ns_wait(struct ns_dev *d, struct ns_wait_args *ua, int all) { } int ret = 0; if (!bad && __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE) == -1) { - sigset_t held; - ns_restore(&old); + ns_restore(); ret = ns_sleep(q, &args); - ns_block(&held); + ns_block(); } ns_drop_q(h, qi); ns_tq.waiting = 0; int32_t got = __atomic_load_n(&q->signaled, __ATOMIC_ACQUIRE); int dead = (int)q->ownerdead; - ns_restore(&old); + ns_restore(); if (bad) return EINVAL; if (got != -1) { ua->index = (uint32_t)got; @@ -1061,12 +1208,11 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a return ENOTTY; } if (type != (uint32_t)want) return EINVAL; - sigset_t old; - ns_block(&old); + ns_block(); int all = ns_lock_obj(h, o); if (o->type != (uint32_t)want || o->gen != gen) { ns_unlock_obj(h, o, all); - ns_restore(&old); + ns_restore(); return EINVAL; } switch (req) { @@ -1133,7 +1279,7 @@ static int ns_obj_ioctl(struct ns_dev *d, uint64_t e, unsigned long req, void *a } } ns_unlock_obj(h, o, all); - ns_restore(&old); + ns_restore(); if (ret) return ret; if (req == NS_SEM_RELEASE || req == NS_EVENT_SET || req == NS_EVENT_RESET || req == NS_EVENT_PULSE) memcpy(arg, &prev, sizeof(prev)); @@ -1176,9 +1322,8 @@ int bl_ntsync_ioctl(int fd, unsigned long req, void *arg, int *rc) { uint64_t e = ns_lookup(fd); if (!e) return 0; struct ns_dev *d = &ns_devs[(e >> 56) & 7]; - if (!ns_dev_get(d)) return 0; + if (!ns_hold(d)) return 0; int out = 0, err = (e & NS_E_DEVICE) ? ns_dev_ioctl(d, req, arg, &out) : ns_obj_ioctl(d, e, req, arg); - ns_dev_put(d); if (err) { errno = err; *rc = -1; diff --git a/tools/tests/test_ntsync_preload.py b/tools/tests/test_ntsync_preload.py new file mode 100644 index 000000000..e05178c8d --- /dev/null +++ b/tools/tests/test_ntsync_preload.py @@ -0,0 +1,456 @@ +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" + +NTDLL = r""" +#define _GNU_SOURCE +#include +#include + +int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen) { + struct sigaction sa, old; + memset(&sa, 0, sizeof(sa)); + sa.sa_sigaction = fn; + sa.sa_flags = SA_SIGINFO | SA_RESTART; + sigemptyset(&sa.sa_mask); + if (sigaction(sig, &sa, NULL) != 0) return -1; + if (sigaction(sig, NULL, &old) != 0) return -1; + *seen = (void *)old.sa_sigaction; + return 0; +} +""" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct sem_args { uint32_t count, max; }; +struct mutex_args { uint32_t owner, count; }; +struct event_args { uint32_t manual, signaled; }; +struct wait_args { uint64_t timeout, objs; uint32_t count, index, flags, owner, alert, pad; }; +#define NT_CREATE_SEM _IOW('N', 0x80, struct sem_args) +#define NT_SEM_RELEASE _IOWR('N', 0x81, uint32_t) +#define NT_WAIT_ANY _IOWR('N', 0x82, struct wait_args) +#define NT_WAIT_ALL _IOWR('N', 0x83, struct wait_args) +#define NT_CREATE_MUTEX _IOW('N', 0x84, struct mutex_args) +#define NT_MUTEX_UNLOCK _IOWR('N', 0x85, struct mutex_args) +#define NT_MUTEX_KILL _IOW('N', 0x86, uint32_t) +#define NT_CREATE_EVENT _IOW('N', 0x87, struct event_args) +#define NT_EVENT_SET _IOR('N', 0x88, uint32_t) +#define NT_EVENT_RESET _IOR('N', 0x89, uint32_t) +#define NT_EVENT_PULSE _IOR('N', 0x8a, uint32_t) +#define NT_SEM_READ _IOR('N', 0x8b, struct sem_args) +#define NT_MUTEX_READ _IOR('N', 0x8c, struct mutex_args) +#define NT_EVENT_READ _IOR('N', 0x8d, struct event_args) + +int install_handler(int sig, void (*fn)(int, siginfo_t *, void *), void **seen); + +static int dev, fails; +#define CHECK(name, cond) do { if (!(cond)) { printf("FAIL %s line %d errno %d\n", name, __LINE__, errno); fails++; } } while (0) + +static uint64_t mono_ns(int64_t delta_ms) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec + (uint64_t)(delta_ms * 1000000); +} + +static int wait_on(unsigned long req, const int *objs, uint32_t count, uint32_t owner, int alert, uint64_t timeout, uint32_t *index) { + struct wait_args w = {timeout, (uintptr_t)objs, count, 0xdead, 0, owner, (uint32_t)alert, 0}; + int r = ioctl(dev, req, &w); + if (index) *index = w.index; + return r < 0 ? errno : 0; +} + +static int event(int manual, int signaled) { + struct event_args a = {(uint32_t)manual, (uint32_t)signaled}; + return ioctl(dev, NT_CREATE_EVENT, &a); +} + +static int sem(uint32_t count, uint32_t max) { + struct sem_args a = {count, max}; + return ioctl(dev, NT_CREATE_SEM, &a); +} + +static int mutex(uint32_t owner, uint32_t count) { + struct mutex_args a = {owner, count}; + return ioctl(dev, NT_CREATE_MUTEX, &a); +} + +static uint32_t prev_of(int fd, unsigned long req, uint32_t in) { + uint32_t v = in; + if (ioctl(fd, req, &v) < 0) return 0xffffffff; + return v; +} + +static void semaphores(void) { + struct sem_args bad = {3, 2}, r; + CHECK("sem_create_invalid", ioctl(dev, NT_CREATE_SEM, &bad) < 0 && errno == EINVAL); + int s = sem(2, 2); + CHECK("sem_create", s >= 0); + uint32_t idx; + CHECK("sem_wait1", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0 && idx == 0); + CHECK("sem_wait2", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, &idx) == 0); + CHECK("sem_empty", wait_on(NT_WAIT_ANY, &s, 1, 1, 0, 0, NULL) == ETIMEDOUT); + CHECK("sem_release_prev", prev_of(s, NT_SEM_RELEASE, 1) == 0); + CHECK("sem_overflow", prev_of(s, NT_SEM_RELEASE, 2) == 0xffffffff && errno == EOVERFLOW); + CHECK("sem_read", ioctl(s, NT_SEM_READ, &r) == 0 && r.count == 1 && r.max == 2); + close(s); +} + +static void mutexes(void) { + struct mutex_args bad = {0, 1}, r, u; + CHECK("mutex_create_invalid", ioctl(dev, NT_CREATE_MUTEX, &bad) < 0 && errno == EINVAL); + int m = mutex(0, 0); + uint32_t idx; + CHECK("mutex_owner_zero", wait_on(NT_WAIT_ANY, &m, 1, 0, 0, 0, NULL) == EINVAL); + CHECK("mutex_take", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, &idx) == 0 && idx == 0); + CHECK("mutex_recurse", wait_on(NT_WAIT_ANY, &m, 1, 7, 0, 0, NULL) == 0); + CHECK("mutex_other", wait_on(NT_WAIT_ANY, &m, 1, 8, 0, 0, NULL) == ETIMEDOUT); + CHECK("mutex_read", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 7 && r.count == 2); + u = (struct mutex_args){8, 0}; + CHECK("mutex_unlock_eperm", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EPERM); + u = (struct mutex_args){0, 0}; + CHECK("mutex_unlock_einval", ioctl(m, NT_MUTEX_UNLOCK, &u) < 0 && errno == EINVAL); + u = (struct mutex_args){7, 0}; + CHECK("mutex_unlock_prev", ioctl(m, NT_MUTEX_UNLOCK, &u) == 0 && u.count == 2); + uint32_t who = 8; + CHECK("mutex_kill_eperm", ioctl(m, NT_MUTEX_KILL, &who) < 0 && errno == EPERM); + who = 7; + CHECK("mutex_kill", ioctl(m, NT_MUTEX_KILL, &who) == 0); + CHECK("mutex_read_dead", ioctl(m, NT_MUTEX_READ, &r) < 0 && errno == EOWNERDEAD && r.owner == 0 && r.count == 0); + CHECK("mutex_abandoned", wait_on(NT_WAIT_ANY, &m, 1, 9, 0, 0, &idx) == EOWNERDEAD && idx == 0); + CHECK("mutex_after_abandon", ioctl(m, NT_MUTEX_READ, &r) == 0 && r.owner == 9 && r.count == 1); + close(m); +} + +static void events(void) { + int a = event(0, 1), m = event(1, 0); + struct event_args r; + CHECK("auto_take", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == 0); + CHECK("auto_consumed", wait_on(NT_WAIT_ANY, &a, 1, 1, 0, 0, NULL) == ETIMEDOUT); + CHECK("set_prev0", prev_of(m, NT_EVENT_SET, 0) == 0); + CHECK("set_prev1", prev_of(m, NT_EVENT_SET, 0) == 1); + CHECK("manual_kept", wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0 && wait_on(NT_WAIT_ANY, &m, 1, 1, 0, 0, NULL) == 0); + CHECK("reset_prev", prev_of(m, NT_EVENT_RESET, 0) == 1); + CHECK("pulse_prev", prev_of(m, NT_EVENT_PULSE, 0) == 0); + CHECK("pulse_leaves_unsignaled", ioctl(m, NT_EVENT_READ, &r) == 0 && r.manual == 1 && r.signaled == 0); + close(a); + close(m); +} + +static void any_order(void) { + int o[4] = {event(1, 0), sem(1, 5), event(1, 1), sem(0, 5)}; + int alert = event(0, 1); + uint32_t idx; + CHECK("any_lowest", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 1); + CHECK("any_next", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 2); + prev_of(o[2], NT_EVENT_RESET, 0); + CHECK("any_alert", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, &idx) == 0 && idx == 4); + CHECK("any_alert_consumed", wait_on(NT_WAIT_ANY, o, 4, 1, alert, 0, NULL) == ETIMEDOUT); + int dup[3] = {o[3], o[0], o[3]}; + prev_of(o[3], NT_SEM_RELEASE, 1); + CHECK("any_duplicate_lowest", wait_on(NT_WAIT_ANY, dup, 3, 1, 0, 0, &idx) == 0 && idx == 0); + int many[65]; + for (int i = 0; i < 65; i++) many[i] = o[0]; + CHECK("any_too_many", wait_on(NT_WAIT_ANY, many, 65, 1, 0, 0, NULL) == EINVAL); + int badfd = 0; + CHECK("any_invalid_fd", wait_on(NT_WAIT_ANY, &badfd, 1, 1, 0, 0, NULL) == EINVAL); + CHECK("any_timeout", wait_on(NT_WAIT_ANY, o, 1, 1, 0, mono_ns(30), NULL) == ETIMEDOUT); + for (int i = 0; i < 4; i++) close(o[i]); + close(alert); +} + +static void all_semantics(void) { + int s = sem(1, 5), e = event(0, 0), m = mutex(0, 0); + int o[3] = {s, e, m}; + struct sem_args sr; + uint32_t idx; + CHECK("all_not_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, NULL) == ETIMEDOUT); + CHECK("all_untouched", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 1); + prev_of(e, NT_EVENT_SET, 0); + CHECK("all_ready", wait_on(NT_WAIT_ALL, o, 3, 4, 0, 0, &idx) == 0 && idx == 0); + CHECK("all_took_sem", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0); + int dup[2] = {s, s}; + CHECK("all_duplicate", wait_on(NT_WAIT_ALL, dup, 2, 4, 0, 0, NULL) == EINVAL); + int alert = event(1, 1); + CHECK("all_alert", wait_on(NT_WAIT_ALL, o, 3, 4, alert, 0, &idx) == 0 && idx == 3); + close(s); + close(e); + close(m); + close(alert); +} + +static int wake_obj; +static uint32_t wake_index; +static int wake_ret; +static void *waiter(void *p) { + int all = (int)(intptr_t)p; + int objs[2] = {wake_obj, wake_obj + 0}; + wake_ret = wait_on(all ? NT_WAIT_ALL : NT_WAIT_ANY, objs, 1, 3, 0, UINT64_MAX, &wake_index); + return NULL; +} + +static void cross_thread(void) { + wake_obj = event(0, 0); + pthread_t t; + pthread_create(&t, NULL, waiter, NULL); + usleep(30000); + prev_of(wake_obj, NT_EVENT_SET, 0); + pthread_join(t, NULL); + CHECK("thread_wake", wake_ret == 0 && wake_index == 0); + struct event_args r; + CHECK("thread_auto_reset", ioctl(wake_obj, NT_EVENT_READ, &r) == 0 && r.signaled == 0); + close(wake_obj); + int s = sem(0, 10); + wake_obj = s; + pthread_t ts[3]; + for (int i = 0; i < 3; i++) pthread_create(&ts[i], NULL, waiter, NULL); + usleep(30000); + CHECK("sem_release_three", prev_of(s, NT_SEM_RELEASE, 3) == 0); + for (int i = 0; i < 3; i++) pthread_join(ts[i], NULL); + struct sem_args sr; + CHECK("sem_all_taken", ioctl(s, NT_SEM_READ, &sr) == 0 && sr.count == 0); + close(s); +} + +static void cross_process(void) { + int sv[2]; + CHECK("socketpair", socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); + int inherited = event(0, 0); + pid_t pid = fork(); + if (pid == 0) { + uint32_t idx; + int r1 = wait_on(NT_WAIT_ANY, &inherited, 1, 5, 0, mono_ns(5000), &idx); + char c; + struct iovec io = {&c, 1}; + char cbuf[CMSG_SPACE(sizeof(int))]; + struct msghdr msg = {0}; + msg.msg_iov = &io; + msg.msg_iovlen = 1; + msg.msg_control = cbuf; + msg.msg_controllen = sizeof(cbuf); + int got = -1; + if (recvmsg(sv[1], &msg, 0) == 1) memcpy(&got, CMSG_DATA(CMSG_FIRSTHDR(&msg)), sizeof(int)); + int r2 = wait_on(NT_WAIT_ANY, &got, 1, 5, 0, mono_ns(5000), NULL); + _exit(r1 == 0 && r2 == 0 ? 0 : 1); + } + usleep(30000); + prev_of(inherited, NT_EVENT_SET, 0); + int sent = sem(0, 1); + char c = 'x'; + struct iovec io = {&c, 1}; + char cbuf[CMSG_SPACE(sizeof(int))]; + struct msghdr msg = {0}; + msg.msg_iov = &io; + msg.msg_iovlen = 1; + msg.msg_control = cbuf; + msg.msg_controllen = sizeof(cbuf); + struct cmsghdr *cm = CMSG_FIRSTHDR(&msg); + cm->cmsg_level = SOL_SOCKET; + cm->cmsg_type = SCM_RIGHTS; + cm->cmsg_len = CMSG_LEN(sizeof(int)); + memcpy(CMSG_DATA(cm), &sent, sizeof(int)); + sendmsg(sv[0], &msg, 0); + usleep(30000); + prev_of(sent, NT_SEM_RELEASE, 1); + int status = 0; + waitpid(pid, &status, 0); + CHECK("process_wake", WIFEXITED(status) && WEXITSTATUS(status) == 0); + close(inherited); + close(sent); + close(sv[0]); + close(sv[1]); +} + +static int ping_obj, pong_obj, stress_sem; +static void *stress_ponger(void *p) { + long n = (long)p; + uint32_t v; + for (long i = 0; i < n; i++) { + if (wait_on(NT_WAIT_ANY, &ping_obj, 1, 21, 0, UINT64_MAX, NULL) != 0) return (void *)1; + ioctl(pong_obj, NT_EVENT_SET, &v); + } + return NULL; +} +static void *stress_hammer(void *p) { + long n = (long)p; + for (long i = 0; i < n; i++) { + if (wait_on(NT_WAIT_ANY, &stress_sem, 1, 22, 0, UINT64_MAX, NULL) != 0) return (void *)1; + uint32_t c = 1; + ioctl(stress_sem, NT_SEM_RELEASE, &c); + } + return NULL; +} + +static void stress(void) { + long rounds = 20000; + ping_obj = event(0, 0); + pong_obj = event(0, 0); + pthread_t t; + void *bad = NULL; + pthread_create(&t, NULL, stress_ponger, (void *)rounds); + uint32_t v; + int ok = 1; + for (long i = 0; i < rounds && ok; i++) { + ioctl(ping_obj, NT_EVENT_SET, &v); + ok = wait_on(NT_WAIT_ANY, &pong_obj, 1, 20, 0, UINT64_MAX, NULL) == 0; + } + pthread_join(t, &bad); + CHECK("stress_pingpong", ok && bad == NULL); + stress_sem = sem(1, 1); + pthread_t th[4]; + for (int i = 0; i < 4; i++) pthread_create(&th[i], NULL, stress_hammer, (void *)5000L); + int hammer_ok = 1; + for (int i = 0; i < 4; i++) { + pthread_join(th[i], &bad); + hammer_ok &= bad == NULL; + } + struct sem_args sr; + CHECK("stress_contended", hammer_ok && ioctl(stress_sem, NT_SEM_READ, &sr) == 0 && sr.count == 1); + close(ping_obj); + close(pong_obj); + close(stress_sem); +} + +static int busy_obj; +static volatile int handled, stop_worker; +static sem_t parked, unpark; +static void reentrant(int sig, siginfo_t *si, void *uc) { + (void)sig; (void)si; (void)uc; + uint32_t v = 0; + ioctl(busy_obj, NT_EVENT_SET, &v); + handled++; +} +static void suspending(int sig, siginfo_t *si, void *uc) { + (void)sig; (void)si; (void)uc; + sem_post(&parked); + while (sem_wait(&unpark) != 0) {} + handled++; +} +static void *worker(void *p) { + (void)p; + uint32_t v; + while (!stop_worker) { + ioctl(busy_obj, NT_EVENT_SET, &v); + wait_on(NT_WAIT_ANY, &busy_obj, 1, 11, 0, 0, NULL); + } + return NULL; +} + +static void signals(void) { + void *seen = NULL; + busy_obj = event(0, 0); + CHECK("install_reentrant", install_handler(SIGUSR1, reentrant, &seen) == 0); + CHECK("query_original", seen == (void *)reentrant); + pthread_t w; + stop_worker = 0; + handled = 0; + pthread_create(&w, NULL, worker, NULL); + for (int i = 0; i < 2000; i++) { + pthread_kill(w, SIGUSR1); + if (i % 64 == 0) usleep(100); + } + usleep(20000); + stop_worker = 1; + pthread_join(w, NULL); + CHECK("reentrant_ran", handled > 0); + sem_init(&parked, 0, 0); + sem_init(&unpark, 0, 0); + CHECK("install_suspending", install_handler(SIGUSR1, suspending, &seen) == 0); + stop_worker = 0; + handled = 0; + pthread_create(&w, NULL, worker, NULL); + uint32_t v; + for (int i = 0; i < 300; i++) { + pthread_kill(w, SIGUSR1); + while (sem_wait(&parked) != 0) {} + ioctl(busy_obj, NT_EVENT_SET, &v); + wait_on(NT_WAIT_ANY, &busy_obj, 1, 12, 0, 0, NULL); + sem_post(&unpark); + } + stop_worker = 1; + pthread_join(w, NULL); + CHECK("suspend_ran", handled == 300); + close(busy_obj); +} + +int main(void) { + alarm(60); + dev = open("/dev/ntsync", O_RDWR | O_CLOEXEC); + if (dev < 0) { + printf("no device %d\n", errno); + return 1; + } + semaphores(); + mutexes(); + events(); + any_order(); + all_semantics(); + cross_thread(); + cross_process(); + stress(); + signals(); + printf("done fails=%d\n", fails); + return fails != 0; +} +""" + + +class NtsyncPreloadTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + if not shutil.which("cc"): + raise unittest.SkipTest("no compiler") + cls.tmp = tempfile.TemporaryDirectory() + tmp = Path(cls.tmp.name) + (tmp / "ntdll.c").write_text(NTDLL) + (tmp / "prog.c").write_text(PROGRAM) + sources = sorted(str(p) for p in PRELOAD.glob("*.c")) + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(tmp / "libblsession.so"), *sources, "-ldl"], + check=True) + subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(tmp / "ntdll.so"), str(tmp / "ntdll.c")], check=True) + subprocess.run(["cc", "-O1", "-pthread", "-o", str(tmp / "prog"), str(tmp / "prog.c"), str(tmp / "ntdll.so"), + "-Wl,-rpath," + str(tmp)], check=True) + cls.program = tmp / "prog" + cls.preload = tmp / "libblsession.so" + + @classmethod + def tearDownClass(cls): + cls.tmp.cleanup() + + def run_program(self): + env = dict(os.environ, LD_PRELOAD=str(self.preload), BL_SYNC="1") + return subprocess.run([str(self.program)], env=env, capture_output=True, text=True, timeout=90) + + def test_emulation_matches_the_ntsync_driver(self): + out = self.run_program() + self.assertEqual(out.returncode, 0, out.stdout + out.stderr) + self.assertIn("done fails=0", out.stdout) + + def test_repeated_runs_stay_consistent(self): + for _ in range(3): + out = self.run_program() + self.assertEqual(out.returncode, 0, out.stdout + out.stderr) + + +if __name__ == "__main__": + unittest.main()