From 6f42e1cf1a34ece588ecda090e8a83e7be60c5d4 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Fri, 27 Nov 2026 19:03:38 -0500 Subject: [PATCH] fix(session): recheck a taken exit's child and commit a launch only on a whole go line --- .../com/droiddeck/launcher/runtime/GuestWorkers.kt | 10 ++++++---- docs/phantom-processes.md | 6 +++--- tools/nativeguest/hook/handoff.c | 14 ++++++++++++-- tools/nativeguest/hook/privileged.c | 11 +++++++++-- tools/tests/test_native_hook_handoff.py | 5 +++++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt b/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt index 8535101162..5c3dcf26a1 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/GuestWorkers.kt @@ -652,13 +652,15 @@ object GuestWorkers { out.flush() answered = true client.soTimeout = ACK_TIMEOUT_MS + // the whole line: a hook whose write stopped short treats it as failed and runs the program itself val line = StringBuilder() - while (true) { + var ended = false + while (!ended) { val c = input.read() - if (c < 0 || c == '\n'.code) break - line.append(c.toChar()) + if (c < 0) break + if (c == '\n'.code) ended = true else line.append(c.toChar()) } - line.toString() == "go" + ended && line.toString() == "go" }.getOrDefault(false) } handedOver = true diff --git a/docs/phantom-processes.md b/docs/phantom-processes.md index 66dbaa7d40..124ce7b97f 100644 --- a/docs/phantom-processes.md +++ b/docs/phantom-processes.md @@ -454,9 +454,9 @@ adb shell device_config get_sync_disabled_for_tests Android 12 and some Android 13 builds have no `get_sync_disabled_for_tests`; there `adb shell device_config is_sync_disabled_for_tests` answers only true or false, so it cannot tell `persistent` -from `until_reboot`. Note `none` when it says false. When it says true, sync was already off and -stays off with `persistent`; if it was `until_reboot`, a reboot after the lab no longer turns it -back on, so set `until_reboot` again yourself if you know that was the mode. +from `until_reboot`. Note `none` when it says false. When it says true, sync is already off: skip +the `set_sync_disabled_for_tests persistent` step below, which keeps whichever mode it was, and +leave the mode alone when you restore the rest. ``` tools/phantomlab/build.sh # build/phantomlab.apk, debug-signed diff --git a/tools/nativeguest/hook/handoff.c b/tools/nativeguest/hook/handoff.c index 0a40f0b77e..532baad36a 100644 --- a/tools/nativeguest/hook/handoff.c +++ b/tools/nativeguest/hook/handoff.c @@ -992,6 +992,16 @@ static int ddh_selects(const struct ddh_entry *e, pid_t pid) { return e->pad[1] == (pid == 0 ? getpgrp() : -pid); } +// Takes e's exit for a wait for pid, checked again once taken: between finding e and taking it, +// another waiter may have released the slot and a later child of ours (or one of another group) +// have exited in it. +static int ddh_take_for(struct ddh_entry *e, pid_t pid) { + if (!ddh_take(e)) return 0; + if (e->parent == getpid() && ddh_selects(e, pid)) return 1; + ddh_untake(e); + return 0; +} + static int ddh_any_live(pid_t pid) { if (!g_ddh) return 0; pid_t me = getpid(); @@ -1112,7 +1122,7 @@ static pid_t ddh_wait(pid_t pid, int *status, int options, struct rusage *ru) { struct ddh_entry *e = pid > 0 ? ddh_find(pid) : NULL; if (pid > 0 && e) { if (!e->reaped && real(pid, NULL, WNOHANG, NULL) == pid) e->reaped = 1; - if (ddh_take(e)) { + if (ddh_take_for(e, pid)) { // the forked child is between its last write and its exit: reap it first, or a later // wait() would report its pid a second time (not blocking for WNOHANG: then later) if (!e->reaped && !ddh_reap_shim(real, pid, options)) { @@ -1132,7 +1142,7 @@ static pid_t ddh_wait(pid_t pid, int *status, int options, struct rusage *ru) { continue; } if (pid <= 0 && (e = ddh_exited(pid))) { - if (!ddh_take(e)) continue; // another waiter has it + if (!ddh_take_for(e, pid)) continue; // another waiter has it, or the slot went to another child pid_t child = e->child; if (!e->reaped && !ddh_reap_shim(real, child, options)) { ddh_untake(e); diff --git a/tools/nativeguest/hook/privileged.c b/tools/nativeguest/hook/privileged.c index 2f3a6f055a..91cfdf9ce1 100644 --- a/tools/nativeguest/hook/privileged.c +++ b/tools/nativeguest/hook/privileged.c @@ -458,9 +458,16 @@ int close_range(unsigned int first, unsigned int last, int flags) { const int *kept = (flags & CLOSE_RANGE_CLOEXEC) ? g_cloexec_kept_fds : g_kept_fds; int kept_count = (flags & CLOSE_RANGE_CLOEXEC) ? g_cloexec_kept_count : g_kept_fd_count; if (kept_count == 0) return close_range_all(first, last, flags); - // The table is unshared once, even when every fd in the range is kept (no fd has the top number) + // The table is unshared once, even when every fd in the range is kept: the system call itself on + // the one number no fd has (close_range_all() would emulate a missing call by closing fds) if (flags & CLOSE_RANGE_UNSHARE) { - if (close_range_all(~0U, ~0U, CLOSE_RANGE_UNSHARE) != 0) return -1; + static long (*raw_syscall)(long, ...) = NULL; + if (!raw_syscall) raw_syscall = (long (*)(long, ...))dlsym(RTLD_NEXT, "syscall"); + if (!raw_syscall) { + errno = ENOSYS; + return -1; + } + if (raw_syscall(__NR_close_range, ~0U, ~0U, CLOSE_RANGE_UNSHARE) != 0) return -1; flags &= ~CLOSE_RANGE_UNSHARE; } return for_each_range_without(first, last, kept, kept_count, close_range_part, &flags); diff --git a/tools/tests/test_native_hook_handoff.py b/tools/tests/test_native_hook_handoff.py index 555323ca12..1414141b3e 100644 --- a/tools/tests/test_native_hook_handoff.py +++ b/tools/tests/test_native_hook_handoff.py @@ -239,6 +239,7 @@ class NativeHookHandoffTest(unittest.TestCase): def test_a_posix_spawn_of_a_missing_program_fails_as_it_would(self): # posix_spawn() returns the exec's error, even with closefrom(3) among the file actions result, requests, out = self.run_parent("spawn", "6", "/bin/missing", handoff="missing") + self.assertEqual(0, out.returncode, out.stderr) self.assertEqual([], requests) self.assertEqual(str(errno.ENOENT), result.get("spawned"), out.stdout) self.assertNotIn("exit", result, out.stdout) @@ -249,6 +250,10 @@ class NativeHookHandoffTest(unittest.TestCase): # as a separate process, gets 0 and an exit status of 127. result, requests, out = self.run_parent("spawn", "6", "/bin/missing", handoff="other") plain, _, plain_out = self.run_parent("spawn", "6", "/bin/missing", handoff="other", hook=False) + self.assertEqual(0, out.returncode, out.stderr) + self.assertEqual(0, plain_out.returncode, plain_out.stderr) + self.assertIn("spawned", result, out.stdout) + self.assertIn("spawned", plain, plain_out.stdout) self.assertEqual([], requests) self.assertEqual(plain.get("spawned"), result.get("spawned"), out.stdout + plain_out.stdout) self.assertEqual(plain.get("exit"), result.get("exit"), out.stdout + plain_out.stdout)