From 006db1456139c83ef7592139d28eab888b48cd20 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Thu, 22 Oct 2026 15:11:29 -0400 Subject: [PATCH] perf: SD card library files resolve through the fast path instead of proot (#1331) perf: files in an SD card library resolve through the fast path instead of proot A library bound at several guest names (droiddeck-sd, bannerlator-sd and case variants) made every symlink and /proc/self/fd readlink under it fall back to proot. The fast path now orders its binds the way proot does and gives each host path the name proot would, and lstat("link/.") follows the link as the kernel does. Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> --- README.md | 2 +- tools/proot/fastpath/fastpath.c | 78 +++++++++---- tools/tests/test_fastpath_bind_names.py | 139 ++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 20 deletions(-) create mode 100644 tools/tests/test_fastpath_bind_names.py diff --git a/README.md b/README.md index 97043973e..e6c4165c4 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. 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. 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/proot/fastpath/fastpath.c b/tools/proot/fastpath/fastpath.c index 5056ec41e..811b0057c 100644 --- a/tools/proot/fastpath/fastpath.c +++ b/tools/proot/fastpath/fastpath.c @@ -337,34 +337,73 @@ static int to_guest(const char *h, char *g, size_t size) { return 0; } -static int guest_text(const char *h, char *g) { - int in = h[0] == '/' && under(h, root, rootlen); - for (int i = 0; h[0] == '/' && !in && i < nbinds; i++) { - int live = held(&binds[i]); - if (live < 0) return -1; - in = live && under(h, binds[i].host, binds[i].hlen); +static int host_order[MAX_BINDS + 1], norder, ordered; + +static int contains(const char *a, const char *b) { + size_t la = strlen(a); + return strlen(b) > la && under(b, a, la); +} + +static const char *side_of(int i, int host) { + return host ? (i < 0 ? root : binds[i].host) : (i < 0 ? "/" : binds[i].guest); +} + +static int insort(int *list, int m, int i, int host) { + const char *p = side_of(i, host); + int prev = -1; + for (int j = 0; j < m; j++) { + const char *q = side_of(list[j], host); + if (!strcmp(p, q) && !host) { + list[j] = i; + return m; + } + if (!strcmp(p, q) || contains(p, q)) prev = j; } - if (in) return to_guest(h, g, PATH_MAX); - memcpy(g, h, strlen(h) + 1); + memmove(list + prev + 2, list + prev + 1, (m - prev - 1) * sizeof *list); + list[prev + 1] = i; + return m + 1; +} + +static int order_binds(void) { + if (__atomic_load_n(&ordered, __ATOMIC_ACQUIRE)) return 0; + for (int i = 0; i < nbinds; i++) + if (held(&binds[i]) < 0) return -1; + if (pthread_mutex_trylock(&bind_lock) != 0) return -1; + if (!ordered) { + int pending[MAX_BINDS + 1], m = 1; + pending[0] = -1; + for (int i = 0; i < nbinds; i++) + if (!binds[i].dropped) m = insort(pending, m, i, 0); + norder = 0; + for (int j = m - 1; j >= 0; j--) norder = insort(host_order, norder, pending[j], 1); + __atomic_store_n(&ordered, 1, __ATOMIC_RELEASE); + } + pthread_mutex_unlock(&bind_lock); return 0; } static int host_bind(const char *h) { - int found = -1; - for (int i = 0; i < nbinds; i++) { - int live = held(&binds[i]); - if (live < 0) return -2; - if (!live || !under(h, binds[i].host, binds[i].hlen)) continue; - if (found < 0 || binds[i].hlen > binds[found].hlen) found = i; - else if (binds[i].hlen == binds[found].hlen && strcmp(binds[i].guest, binds[found].guest) != 0) found = -3; - } - return found == -3 ? -2 : found; + if (order_binds() < 0) return -2; + for (int j = 0; j < norder; j++) + if (host_order[j] >= 0 && under(h, binds[host_order[j]].host, binds[host_order[j]].hlen)) return host_order[j]; + return -1; +} + +static int guest_text(const char *h, char *g) { + if (h[0] == '/' && under(h, root, rootlen)) return to_guest(h, g, PATH_MAX); + int i = h[0] == '/' ? host_bind(h) : -1; + if (i == -2) return -1; + if (i >= 0) + return snprintf(g, PATH_MAX, "%s%s", binds[i].guest, h + binds[i].hlen) >= PATH_MAX || reserved(g) ? -1 : 0; + memcpy(g, h, strlen(h) + 1); + return 0; } static long detranslated(char *text, const char *at) { if (text[0] != '/') return 0; if (!under(at, root, rootlen) && !under(text, root, rootlen)) { int own = host_bind(at), to = host_bind(text); + if (to == -1) return 0; if (own == -2 || to == -2) return FP_SLOW; if (own >= 0 && to >= 0 && strcmp(binds[own].host, binds[to].host) == 0) { const struct bind *bd = &binds[to]; @@ -635,8 +674,9 @@ static long resolve_in(const char *base, const char *path, int follow, struct re char norm[PATH_MAX]; size_t o = 0; int dotdot = 0; - size_t plen = strlen(path); - int trailing = plen > 1 && path[plen - 1] == '/'; + size_t plen = strlen(path), end = plen; + while (end > 1 && path[end - 1] == '/') end--; + int trailing = (plen > 1 && path[plen - 1] == '/') || (end > 0 && path[end - 1] == '.' && (end == 1 || path[end - 2] == '/')); for (int part = base ? 0 : 1; part < 2; part++) { for (const char *c = part ? path : base; *c;) { while (*c == '/') c++; diff --git a/tools/tests/test_fastpath_bind_names.py b/tools/tests/test_fastpath_bind_names.py new file mode 100644 index 000000000..2e8a8b11f --- /dev/null +++ b/tools/tests/test_fastpath_bind_names.py @@ -0,0 +1,139 @@ +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +FASTPATH = Path(__file__).resolve().parents[1] / "proot/fastpath/fastpath.c" + +PROGRAM = r""" +#include "FASTPATH" + +static long host_sys(long nr, long a, long b, long c, long d, long e, long f) { + long r = syscall(nr, a, b, c, d, e, f); + return r < 0 ? -errno : r; +} + +int main(int argc, char **argv) { + static char hosts[MAX_BINDS][PATH_MAX], guests[MAX_BINDS][512]; + fp_sys = host_sys; + snprintf(root, sizeof root, "%s", argv[1]); + rootlen = strlen(root); + int i = 2; + for (; i + 1 < argc && strcmp(argv[i], "--"); i += 2) { + struct bind *bd = &binds[nbinds]; + bd->host = hosts[nbinds]; + bd->guest = guests[nbinds]; + snprintf(bd->host, PATH_MAX, "%s", argv[i]); + snprintf(bd->guest, sizeof guests[0], "%s", argv[i + 1]); + bd->hlen = strlen(bd->host); + bd->glen = strlen(bd->guest); + nbinds++; + } + pthread_once(&init_once, fp_init); + fp_on = 1; + for (i++; i < argc; i++) { + char text[PATH_MAX]; + if (!strcmp(argv[i], "name")) { + if (guest_text(argv[++i], text) < 0) strcpy(text, "SLOW"); + puts(text); + } else if (!strcmp(argv[i], "link")) { + snprintf(text, sizeof text, "%s", argv[i + 2]); + if (detranslated(text, argv[i + 1]) == FP_SLOW) strcpy(text, "SLOW"); + i += 2; + puts(text); + } else if (!strcmp(argv[i], "lstat")) { + struct stat st; + long r = fstatat(AT_FDCWD, argv[++i], &st, AT_SYMLINK_NOFOLLOW); + puts(r < 0 ? "error" : S_ISDIR(st.st_mode) ? "dir" : S_ISLNK(st.st_mode) ? "link" : "other"); + } + } + return 0; +} +""" + + +class FastPathBindNamesTest(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 / "names.c" + source.write_text(PROGRAM.replace("FASTPATH", str(FASTPATH))) + cls.program = cls.work / "names" + subprocess.run(["gcc", "-O1", "-w", "-pthread", "-o", str(cls.program), str(source), "-ldl"], check=True) + t = cls.work / "tree" + for d in ["root/etc", "root/mnt", "root/opt", "root/d/real", "lib/common/game", "fonts", "a", "b"]: + (t / d).mkdir(parents=True, exist_ok=True) + for f in ["root/etc/x", "lib/f", "lib/common/game/f", "fonts/f", "a/f", "b/f"]: + (t / f).touch() + os.symlink(t / "lib/f", t / "lib/l2") + os.symlink(t / "lib/common", t / "lib/l") + os.symlink("real", t / "root/d/link") + cls.tree = t + cls.binds = [ + (t / "lib", "/mnt/droiddeck-sd"), + (t / "lib", "/mnt/bannerlator-sd"), + (t / "lib", "/mnt/DroidDeck-SD"), + (t / "lib/common", "/opt/common"), + (t / "fonts", "/usr/share/fonts/android"), + (t / "a", "/opt/g"), + (t / "b", "/opt/g"), + (t / "fonts", "/mnt/x"), + ] + + @classmethod + def tearDownClass(cls): + shutil.rmtree(cls.work, ignore_errors=True) + + def ask(self, *queries): + argv = [str(self.program), str(self.tree / "root")] + for host, guest in self.binds: + argv += [str(host), guest] + argv.append("--") + for q in queries: + argv += [str(x) for x in q] + out = subprocess.run(argv, capture_output=True, text=True, timeout=20, check=True).stdout + return out.splitlines() + + def test_a_host_file_takes_the_name_proot_gives_it(self): + t = self.tree + self.assertEqual(self.ask( + ("name", t / "lib/f"), + ("name", t / "lib/common/game/f"), + ("name", t / "fonts/f"), + ("name", t / "b/f"), + ("name", t / "a/f"), + ("name", t / "root/etc/x"), + ), [ + "/mnt/droiddeck-sd/f", + "/opt/common/game/f", + "/usr/share/fonts/android/f", + "/opt/g/f", + str(t / "a/f"), + "/etc/x", + ]) + + def test_a_link_inside_a_library_reads_back_like_proot(self): + t = self.tree + self.assertEqual(self.ask( + ("link", t / "lib/l2", t / "lib/f"), + ("link", t / "lib/l", t / "lib/common"), + ), [ + "/mnt/droiddeck-sd/f", + str(t / "lib/common"), + ]) + + def test_a_final_dot_follows_the_link(self): + self.assertEqual(self.ask( + ("lstat", "/d/link"), + ("lstat", "/d/link/."), + ("lstat", "/d/link/./"), + ("lstat", "/d/link/"), + ), ["link", "dir", "dir", "dir"]) + + +if __name__ == "__main__": + unittest.main()