From f27d0b006fdb0d4460f22528ff01b83d6884735a Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Thu, 22 Oct 2026 15:53:12 -0400 Subject: [PATCH] perf: Wine's write watches stop costing a signal per page on arm64 (#1333) Wine asks for userfaultfd with the i386 syscall number on arm64, so it never gets one and falls back to mprotect and a SIGSEGV per first write, which under proot is a ptrace stop each time. libblsession hands it the real userfaultfd and, where Android refuses the PAGEMAP_SCAN ioctl, answers it from /proc/self/pagemap and UFFDIO_WRITEPROTECT. --- README.md | 2 +- tools/linuxfs/preload/netif.c | 3 + tools/linuxfs/preload/robust.c | 5 ++ tools/linuxfs/preload/writewatch.c | 113 +++++++++++++++++++++++ tools/tests/test_fsync_preload.py | 2 +- tools/tests/test_noexec_preload.py | 2 +- tools/tests/test_openat2_preload.py | 2 +- tools/tests/test_vrclient_loader.py | 1 + tools/tests/test_writewatch_preload.py | 119 +++++++++++++++++++++++++ 9 files changed, 245 insertions(+), 4 deletions(-) create mode 100644 tools/linuxfs/preload/writewatch.c create mode 100644 tools/tests/test_writewatch_preload.py diff --git a/README.md b/README.md index 13ecff16c3..62e7939526 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`. 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. 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/netif.c b/tools/linuxfs/preload/netif.c index 91ce93a067..1782d0f0d1 100644 --- a/tools/linuxfs/preload/netif.c +++ b/tools/linuxfs/preload/netif.c @@ -409,6 +409,8 @@ int bl_drm_gem_close(int fd, unsigned long request, void *arg, int *rc) __attrib int bl_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden"))); int bl_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg, void *caller, int *rc) __attribute__((visibility("hidden"))); +int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg, + int *rc) __attribute__((visibility("hidden"))); int ioctl(int fd, unsigned long request, ...) { typedef int (*ioctl_fn)(int, unsigned long, void *); @@ -424,6 +426,7 @@ int ioctl(int fd, unsigned long request, ...) { if (bl_drm_gem_close(fd, request, arg, &rc)) return rc; if (real == NULL) real = (ioctl_fn) dlsym(RTLD_NEXT, "ioctl"); if (bl_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc; + if (bl_writewatch_ioctl(real, fd, request, arg, &rc)) return rc; if (!bl_kgsl_poll(real, fd, request, arg, &rc)) rc = quest_kgsl_ioctl(real, fd, request, arg); bl_kgsl_chip_id_fixup(request, arg, rc); if (arg == NULL || !is_link_query(request)) return rc; diff --git a/tools/linuxfs/preload/robust.c b/tools/linuxfs/preload/robust.c index 81bf5d8cc2..e156e82c54 100644 --- a/tools/linuxfs/preload/robust.c +++ b/tools/linuxfs/preload/robust.c @@ -329,6 +329,8 @@ static int execveat_is_execve(const long *args) { return path[0] == '/' || (int)args[0] == AT_FDCWD; } +int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) __attribute__((visibility("hidden"))); + long syscall(long number, ...) { va_list ap; long args[6]; @@ -354,6 +356,9 @@ long syscall(long number, ...) { if (!real_syscall) { real_syscall = (long (*)(long, ...)) dlsym(RTLD_NEXT, "syscall"); } + if (bl_writewatch_syscall(number, args, real_syscall, &handled)) { + return handled; + } if (number == SYS_openat2) { if (!__atomic_load_n(&openat2_missing, __ATOMIC_RELAXED)) { long fd = real_syscall(number, args[0], args[1], args[2], args[3]); diff --git a/tools/linuxfs/preload/writewatch.c b/tools/linuxfs/preload/writewatch.c new file mode 100644 index 0000000000..b804e3f39c --- /dev/null +++ b/tools/linuxfs/preload/writewatch.c @@ -0,0 +1,113 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +#define WINE_ARM64_USERFAULTFD 374 +#define PM_UFFD_WP (1ULL << 57) +#define PM_SWAP (1ULL << 62) +#define PM_PRESENT (1ULL << 63) + +#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY) +static int ww_uffd = -1; +static int scan_blocked; +#endif + +__attribute__((visibility("hidden"))) int bl_writewatch_syscall(long number, const long *args, long (*real)(long, ...), long *rc) { +#if defined(__aarch64__) && defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY) + if (number != WINE_ARM64_USERFAULTFD || (args[0] & ~(long)(O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY))) return 0; + *rc = real(SYS_userfaultfd, args[0]); + if (*rc >= 0) __atomic_store_n(&ww_uffd, (int)*rc, __ATOMIC_RELEASE); + return 1; +#else + (void)number; + (void)args; + (void)real; + (void)rc; + return 0; +#endif +} + +#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY) +static int protect(int uffd, uint64_t start, uint64_t end) { + if (start == end) return 0; + struct uffdio_writeprotect wp = { .range = { start, end - start }, .mode = UFFDIO_WRITEPROTECT_MODE_WP }; + return ioctl(uffd, UFFDIO_WRITEPROTECT, &wp); +} + +static long emulate_scan(int fd, int uffd, struct pm_scan_arg *a) { + uint64_t page = (uint64_t)getpagesize(); + if (a->size < sizeof(*a) || (a->flags & ~(uint64_t)PM_SCAN_WP_MATCHING) || a->category_inverted || a->category_anyof_mask + || a->category_mask != PAGE_IS_WRITTEN || (a->return_mask & ~(uint64_t)PAGE_IS_WRITTEN) || a->end < a->start + || ((a->start | a->end) & (page - 1)) || (a->vec_len && !a->vec)) { + errno = EINVAL; + return -1; + } + struct page_region *vec = (struct page_region *)(uintptr_t)a->vec; + uint64_t regions = 0, pages = 0, addr = a->start, wp_start = 0, wp_end = 0; + uint64_t entries[512]; + int wp = (a->flags & PM_SCAN_WP_MATCHING) != 0; + while (addr < a->end) { + uint64_t want = (a->end - addr) / page; + if (want > 512) want = 512; + ssize_t got = pread(fd, entries, want * sizeof(entries[0]), (off_t)(addr / page * sizeof(entries[0]))); + if (got < 0) return -1; + if (got == 0) break; + for (uint64_t i = 0; i < (uint64_t)got / sizeof(entries[0]); i++, addr += page) { + uint64_t e = entries[i]; + if (!(e & (PM_PRESENT | PM_SWAP)) || (e & PM_UFFD_WP)) continue; + if (a->max_pages && pages >= a->max_pages) goto done; + if (a->vec_len) { + if (regions && vec[regions - 1].end == addr) { + vec[regions - 1].end += page; + } else { + if (regions == a->vec_len) goto done; + vec[regions].start = addr; + vec[regions].end = addr + page; + vec[regions].categories = PAGE_IS_WRITTEN & a->return_mask; + regions++; + } + } + pages++; + if (!wp) continue; + if (wp_end != addr) { + if (protect(uffd, wp_start, wp_end) < 0) return -1; + wp_start = addr; + } + wp_end = addr + page; + } + } +done: + if (wp && protect(uffd, wp_start, wp_end) < 0) return -1; + a->walk_end = addr < a->end ? addr : a->end; + return (long)regions; +} +#endif + +__attribute__((visibility("hidden"))) int bl_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, + void *arg, int *rc) { +#if defined(PAGEMAP_SCAN) && defined(UFFD_USER_MODE_ONLY) + if (request != PAGEMAP_SCAN) return 0; + int uffd = __atomic_load_n(&ww_uffd, __ATOMIC_ACQUIRE); + if (uffd < 0) return 0; + if (!__atomic_load_n(&scan_blocked, __ATOMIC_RELAXED)) { + *rc = real(fd, request, arg); + if (*rc >= 0 || errno != EACCES) return 1; + __atomic_store_n(&scan_blocked, 1, __ATOMIC_RELAXED); + } + *rc = (int)emulate_scan(fd, uffd, (struct pm_scan_arg *)arg); + return 1; +#else + (void)real; + (void)fd; + (void)request; + (void)arg; + (void)rc; + return 0; +#endif +} diff --git a/tools/tests/test_fsync_preload.py b/tools/tests/test_fsync_preload.py index c2242f4945..63007f3734 100644 --- a/tools/tests/test_fsync_preload.py +++ b/tools/tests/test_fsync_preload.py @@ -319,7 +319,7 @@ class FsyncPreloadTest(unittest.TestCase): source = base / "fsync-test.c" source.write_text(PROGRAM) subprocess.run(["cc", "-O2", "-Wall", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib), - str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), "-ldl"], check=True) + str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True) subprocess.run(["cc", "-O2", "-Wall", "-pthread", "-o", str(cls.exe), str(source)], check=True) @classmethod diff --git a/tools/tests/test_noexec_preload.py b/tools/tests/test_noexec_preload.py index 0e563ec49a..20a437d4cb 100644 --- a/tools/tests/test_noexec_preload.py +++ b/tools/tests/test_noexec_preload.py @@ -43,7 +43,7 @@ class NoexecPreloadTest(unittest.TestCase): cls.tmp = Path(tempfile.mkdtemp()) cls.lib = cls.tmp / "libnoexec.so" subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-DNOEXEC_TEST", "-o", str(cls.lib), - str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), "-ldl"], check=True) + str(PRELOAD / "noexec.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "robust.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True) source = cls.tmp / "program.c" source.write_text(PROGRAM) for name in ("FEX", "game"): diff --git a/tools/tests/test_openat2_preload.py b/tools/tests/test_openat2_preload.py index a30aa74dda..eb27bcc7eb 100644 --- a/tools/tests/test_openat2_preload.py +++ b/tools/tests/test_openat2_preload.py @@ -230,7 +230,7 @@ class Openat2PreloadTest(unittest.TestCase): cls.tmp = Path(tempfile.mkdtemp()) cls.lib = cls.tmp / "libopenat2.so" subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-fPIC", "-shared", "-pthread", "-o", str(cls.lib), - str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), "-ldl"], check=True) + str(PRELOAD / "robust.c"), str(PRELOAD / "fsync.c"), str(PRELOAD / "noexec.c"), str(PRELOAD / "writewatch.c"), "-ldl"], check=True) source = cls.tmp / "openat2.c" source.write_text(PROGRAM) subprocess.run(["cc", "-O2", "-Wall", "-Werror", "-o", str(cls.tmp / "openat2"), str(source), "-ldl"], check=True) diff --git a/tools/tests/test_vrclient_loader.py b/tools/tests/test_vrclient_loader.py index f468269925..fa1a778bf4 100644 --- a/tools/tests/test_vrclient_loader.py +++ b/tools/tests/test_vrclient_loader.py @@ -32,6 +32,7 @@ int probe(void) { #include #include int bl_fsync_syscall(long n, const long *a, long *r) { (void)n; (void)a; (void)r; return 0; } +int bl_writewatch_syscall(long n, const long *a, long (*real)(long, ...), long *r) { (void)n; (void)a; (void)real; (void)r; return 0; } #include """ + '"' + robust.as_posix() + '"' + r""" int main(int argc, char **argv) { assert(argc == 3); diff --git a/tools/tests/test_writewatch_preload.py b/tools/tests/test_writewatch_preload.py new file mode 100644 index 0000000000..38f2967557 --- /dev/null +++ b/tools/tests/test_writewatch_preload.py @@ -0,0 +1,119 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload" + +PROGRAM = r""" +#include "WRITEWATCH" +#include +#include +#include +#include + +#define PAGES 96 + +static int uffd, pagemap; +static uint64_t page; + +static char *region(void) { + char *m = mmap(NULL, PAGES * page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + madvise(m, PAGES * page, MADV_NOHUGEPAGE); + struct uffdio_register reg = { .range = { (uintptr_t)m, PAGES * page }, .mode = UFFDIO_REGISTER_MODE_WP }; + if (m == MAP_FAILED || ioctl(uffd, UFFDIO_REGISTER, ®)) exit(3); + protect(uffd, (uintptr_t)m, (uintptr_t)m + PAGES * page); + return m; +} + +static long scan(char *m, int emulated, uint64_t flags, uint64_t vec_len, uint64_t max_pages, uint64_t from, uint64_t to, + struct page_region *vec, uint64_t *walk_end) { + struct pm_scan_arg a = { .size = sizeof a, .flags = flags, .start = (uintptr_t)m + from * page, .end = (uintptr_t)m + to * page, + .vec = vec_len ? (uintptr_t)vec : 0, .vec_len = vec_len, .max_pages = max_pages, + .category_mask = PAGE_IS_WRITTEN, .return_mask = PAGE_IS_WRITTEN }; + long n = emulated ? emulate_scan(pagemap, uffd, &a) : ioctl(pagemap, PAGEMAP_SCAN, &a); + *walk_end = (a.walk_end - (uintptr_t)m) / page; + return n; +} + +int main(int argc, char **argv) { + page = (uint64_t)getpagesize(); + uffd = syscall(SYS_userfaultfd, O_CLOEXEC | O_NONBLOCK | UFFD_USER_MODE_ONLY); + struct uffdio_api api = { .api = UFFD_API, .features = UFFD_FEATURE_WP_ASYNC | UFFD_FEATURE_WP_UNPOPULATED }; + pagemap = open("/proc/self/pagemap", O_RDONLY | O_CLOEXEC); + if (uffd < 0 || ioctl(uffd, UFFDIO_API, &api) || pagemap < 0) { puts("unsupported"); return 0; } + char *kernel = region(), *ours = region(); + struct page_region probe[1]; + uint64_t end; + if (scan(kernel, 0, 0, 1, 0, 0, PAGES, probe, &end) < 0) { puts("unsupported"); return 0; } + srand(atoi(argv[1])); + int rounds = atoi(argv[2]), differing = 0; + for (int r = 0; r < rounds; r++) { + int writes = rand() % 24; + for (int w = 0; w < writes; w++) { + int p = rand() % PAGES; + if (rand() % 4 == 0) { + volatile char x = kernel[p * page] + ours[p * page]; + (void)x; + continue; + } + kernel[p * page + rand() % page] = (char)r; + ours[p * page + rand() % page] = (char)r; + } + uint64_t flags = rand() % 2 ? PM_SCAN_WP_MATCHING : 0; + uint64_t vec_len = rand() % 3 == 0 ? 0 : 1 + rand() % 5; + uint64_t max_pages = rand() % 3 == 0 ? 1 + rand() % 8 : 0; + if (!vec_len) flags = PM_SCAN_WP_MATCHING, max_pages = 0; + uint64_t from = rand() % PAGES, to = from + rand() % (PAGES - from + 1); + struct page_region kv[8], ov[8]; + uint64_t kend, oend; + long kn = scan(kernel, 0, flags, vec_len, max_pages, from, to, kv, &kend); + long on = scan(ours, 1, flags, vec_len, max_pages, from, to, ov, &oend); + int same = kn == on && (kn < 0 || kend == oend); + for (long i = 0; same && i < kn; i++) + same = kv[i].start - (uintptr_t)kernel == ov[i].start - (uintptr_t)ours + && kv[i].end - (uintptr_t)kernel == ov[i].end - (uintptr_t)ours && kv[i].categories == ov[i].categories; + struct page_region kall[PAGES], oall[PAGES]; + long ka = scan(kernel, 0, 0, PAGES, 0, 0, PAGES, kall, &kend), oa = scan(ours, 1, 0, PAGES, 0, 0, PAGES, oall, &oend); + same = same && ka == oa; + for (long i = 0; same && i < ka; i++) + same = kall[i].start - (uintptr_t)kernel == oall[i].start - (uintptr_t)ours && kall[i].end - (uintptr_t)kernel == oall[i].end - (uintptr_t)ours; + if (!same) { + differing++; + printf("round %d flags %llu vec %llu max %llu range %llu-%llu: kernel %ld end %llu, ours %ld end %llu\n", r, + (unsigned long long)flags, (unsigned long long)vec_len, (unsigned long long)max_pages, (unsigned long long)from, + (unsigned long long)to, kn, (unsigned long long)kend, on, (unsigned long long)oend); + } + } + printf("rounds %d differing %d\n", rounds, differing); + return 0; +} +""" + + +class WriteWatchPreloadTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + if shutil.which("gcc") is None: + raise unittest.SkipTest("gcc is not available") + cls.work = Path(tempfile.mkdtemp()) + source = cls.work / "writewatch.c" + source.write_text(PROGRAM.replace("WRITEWATCH", str(PRELOAD / "writewatch.c"))) + cls.program = cls.work / "writewatch" + subprocess.run(["gcc", "-O1", "-Wall", "-o", str(cls.program), str(source)], check=True) + + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.work, ignore_errors=True) + + def test_a_scan_android_refuses_answers_like_the_kernel_one(self): + for seed in range(4): + out = subprocess.run([str(self.program), str(seed), "400"], capture_output=True, text=True, timeout=60).stdout + if out.strip() == "unsupported": + self.skipTest("this kernel has no asynchronous userfaultfd write protection") + self.assertTrue(out.rstrip().endswith("differing 0"), out) + + +if __name__ == "__main__": + unittest.main()