From 9a8f54dee7b847d3b2e8c843469274d5d79933e8 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:31:52 -0400 Subject: [PATCH] fix(proot): preserve cleared robust-list state --- tools/proot/bench/svc-check.c | 27 +++++++++++++++++ tools/proot/bench/svc-threads.c | 47 +++++++++++++++++++++++++++++ tools/proot/fastpath/svc-dispatch.h | 3 +- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tools/proot/bench/svc-threads.c diff --git a/tools/proot/bench/svc-check.c b/tools/proot/bench/svc-check.c index 80040b61b..59f46ff86 100644 --- a/tools/proot/bench/svc-check.c +++ b/tools/proot/bench/svc-check.c @@ -147,6 +147,8 @@ static void robust_checks(void) { assert(raw(SYS_get_robust_list, gettid(), (long)&got, (long)&size, 0, 0, 0) == 0); assert(got == (long)&head && size == sizeof head); assert(raw(SYS_get_robust_list, 0, 1, (long)&size, 0, 0, 0) == -EFAULT); + assert(raw(SYS_set_robust_list, 0, sizeof head, 0, 0, 0, 0) == -ENOSYS); + assert(raw(SYS_get_robust_list, 0, (long)&got, (long)&size, 0, 0, 0) == -ENOSYS); assert(raw(SYS_set_robust_list, original, sizeof head, 0, 0, 0, 0) == -ENOSYS); assert(raw(SYS_faccessat2, AT_FDCWD, (long)"/usr/lib/libc.so.6", R_OK, 0, 0, 0) == -ENOSYS); if (svc_android_api < 35) assert(raw(SYS_clone3, 0, 0, 0, 0, 0, 0) == -ENOSYS); @@ -219,6 +221,18 @@ static void *cancel_worker(void *unused) { } } +struct cancel_open { const char *path; int (*open)(const char *, int, ...); int ready, cleaned; }; +static void cancel_cleanup(void *arg) { ((struct cancel_open *)arg)->cleaned = 1; } +static void *cancel_fifo(void *arg) { + struct cancel_open *test = arg; + pthread_cleanup_push(cancel_cleanup, test); + __atomic_store_n(&test->ready, 1, __ATOMIC_RELEASE); + int fd = test->open(test->path, O_RDONLY); + if (fd >= 0) close(fd); + pthread_cleanup_pop(1); + return NULL; +} + static void signal_checks(void) { stack_t old_stack, stack = {.ss_sp = malloc(128 * 1024), .ss_size = 128 * 1024}; assert(stack.ss_sp && sigaltstack(&stack, &old_stack) == 0); @@ -242,6 +256,19 @@ static void signal_checks(void) { void *result; assert(pthread_create(&thread, NULL, cancel_worker, NULL) == 0); assert(pthread_cancel(thread) == 0 && pthread_join(thread, &result) == 0 && result == PTHREAD_CANCELED); + char fifo[] = "/tmp/proot-expanded-fifo-XXXXXX"; + int fd = mkstemp(fifo); + assert(fd >= 0 && close(fd) == 0 && unlink(fifo) == 0 && mkfifo(fifo, 0600) == 0); + void *libc = dlopen("libc.so.6", RTLD_NOW); + struct cancel_open test = {.path = fifo, .open = dlsym(libc, "open")}; + assert(test.open && pthread_create(&thread, NULL, cancel_fifo, &test) == 0); + while (!__atomic_load_n(&test.ready, __ATOMIC_ACQUIRE)) sched_yield(); + struct timespec deadline = {0, 20000000}; + nanosleep(&deadline, NULL); + assert(pthread_cancel(thread) == 0 && clock_gettime(CLOCK_REALTIME, &deadline) == 0); + deadline.tv_sec += 2; + assert(pthread_timedjoin_np(thread, &result, &deadline) == 0 && result == PTHREAD_CANCELED && test.cleaned); + assert(unlink(fifo) == 0 && dlclose(libc) == 0); for (int i = 0; i < 32; i++) { assert(pthread_create(&thread, NULL, thread_check, NULL) == 0); assert(pthread_join(thread, NULL) == 0); diff --git a/tools/proot/bench/svc-threads.c b/tools/proot/bench/svc-threads.c new file mode 100644 index 000000000..88988bd2e --- /dev/null +++ b/tools/proot/bench/svc-threads.c @@ -0,0 +1,47 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include + +static double now(void) { + struct timespec t; + assert(clock_gettime(CLOCK_MONOTONIC, &t) == 0); + return t.tv_sec * 1e9 + t.tv_nsec; +} + +static void *thread(void *arg) { return arg; } + +int main(void) { + void *libc = dlopen("libc.so.6", RTLD_NOW); + assert(libc); + int (*access)(int, const char *, int, int) = dlsym(libc, "faccessat"); + char *(*cwd)(char *, size_t) = dlsym(libc, "getcwd"); + assert(access && cwd); + struct rusage before, after; + assert(getrusage(RUSAGE_SELF, &before) == 0); + const int threads = 128, calls = 2000; + double start = now(); + for (int i = 0; i < threads; i++) { + pthread_t id; + void *value; + assert(pthread_create(&id, NULL, thread, (void *)1) == 0); + assert(pthread_join(id, &value) == 0 && value == (void *)1); + } + double thread_us = (now() - start) / threads / 1000; + start = now(); + for (int i = 0; i < calls; i++) assert(access(AT_FDCWD, "/etc/os-release", R_OK, 0) == 0); + double access_ns = (now() - start) / calls; + char path[4096]; + start = now(); + for (int i = 0; i < calls; i++) assert(cwd(path, sizeof path)); + double cwd_ns = (now() - start) / calls; + assert(getrusage(RUSAGE_SELF, &after) == 0 && dlclose(libc) == 0); + printf("{\"thread_us\":%.0f,\"access_ns\":%.0f,\"cwd_ns\":%.0f,\"switches\":%ld}\n", + thread_us, access_ns, cwd_ns, after.ru_nvcsw - before.ru_nvcsw); + return 0; +} diff --git a/tools/proot/fastpath/svc-dispatch.h b/tools/proot/fastpath/svc-dispatch.h index 04afa1577..f8cdbbf76 100644 --- a/tools/proot/fastpath/svc-dispatch.h +++ b/tools/proot/fastpath/svc-dispatch.h @@ -52,7 +52,8 @@ static long svc_blocked(const long *r) { } if (r[8] == SYS_get_robust_list) { long tid = sc(SYS_gettid, 0, 0, 0, 0, 0); - if ((r[0] && r[0] != tid) || svc_robust.tid != tid || !svc_robust.head) return FP_SLOW; + if ((r[0] && r[0] != tid) || svc_robust.tid != tid) return FP_SLOW; + if (!svc_robust.head) return -ENOSYS; size_t size = 3 * sizeof(long); if (svc_memory(&svc_robust.head, r[1], sizeof(long), 1) != sizeof(long) || svc_memory(&size, r[2], sizeof size, 1) != sizeof size) return -EFAULT;