diff --git a/tools/nativeguest/hook/handoff.c b/tools/nativeguest/hook/handoff.c index f0beb39a39..43bf79b8c0 100644 --- a/tools/nativeguest/hook/handoff.c +++ b/tools/nativeguest/hook/handoff.c @@ -164,14 +164,14 @@ static int ddh_record_pending(uint64_t r) { for (int i = 0; i < DDH_SLOTS; i++) { struct ddh_entry *e = &t->e[i]; int st = __atomic_load_n(&e->state, __ATOMIC_ACQUIRE); - if ((st == DDH_HANDED || st == DDH_EXITED || st == DDH_TAKEN) && e->parent == me && e->child == child && - ddh_record_started(r, (long)e->child_start)) return 1; + if ((st == DDH_CLAIMED || st == DDH_HANDED || st == DDH_EXITED || st == DDH_TAKEN) && e->parent == me && + e->child == child && ddh_record_started(r, (long)e->child_start)) return 1; } return 0; } -// Whether child has a handoff whose program is running or whose exit is not yet reported, other than -// one of a child that started at start (a time read for it, else any) +// Whether child has a handoff under way, whose program is running or whose exit is not yet reported, +// other than one of a child that started at start (a time read for it; -1: any) static int ddh_pid_pending_other(pid_t child, long start) { struct ddh_table *t = __atomic_load_n(&g_ddh, __ATOMIC_ACQUIRE); if (!t) return 0; @@ -179,7 +179,8 @@ static int ddh_pid_pending_other(pid_t child, long start) { for (int i = 0; i < DDH_SLOTS; i++) { struct ddh_entry *e = &t->e[i]; int st = __atomic_load_n(&e->state, __ATOMIC_ACQUIRE); - if ((st != DDH_HANDED && st != DDH_EXITED && st != DDH_TAKEN) || e->parent != me || e->child != child) continue; + if ((st != DDH_CLAIMED && st != DDH_HANDED && st != DDH_EXITED && st != DDH_TAKEN) || e->parent != me || e->child != child) + continue; if (!(start > 0 && e->child_start > 0 && ((uint64_t)e->child_start & DDH_REC_START) == ((uint64_t)start & DDH_REC_START))) return 1; } @@ -199,9 +200,11 @@ static void ddh_forked_by(pid_t child, pid_t thread) { uint64_t seen = __atomic_load_n(slot, __ATOMIC_ACQUIRE); for (;;) { // A fork child that could not read its own start time (out of fds, say) has no time to tell its - // record by: it stays while the pid has a handoff pending that is not this child's. + // record by: it stays while the pid has any handoff under way. The time read here may be that + // child's, whose handoff it then is, so it tells nothing. (Two children of the pid with a + // handoff under way each, this one's too, is the limit of one record per pid.) if (seen && ddh_record_child(seen) == child && (seen & DDH_REC_SELF) && !(seen & DDH_REC_KNOWN) && - ddh_pid_pending_other(child, start)) + ddh_pid_pending_other(child, -1)) return; if (seen && ddh_record_child(seen) == child && (seen & DDH_REC_KNOWN)) { // A child that recorded itself, started when the time read here says, has the pid: this diff --git a/tools/nativeguest/hook/privileged.c b/tools/nativeguest/hook/privileged.c index e8e63ccc86..d78c087236 100644 --- a/tools/nativeguest/hook/privileged.c +++ b/tools/nativeguest/hook/privileged.c @@ -88,18 +88,26 @@ static unsigned kept_close_begin(void) { static void kept_close_end(unsigned epoch) { __atomic_sub_fetch(&g_closers[epoch], 1, __ATOMIC_SEQ_CST); } -// 1 once every close that began before now has ended; 0 after a second without (one interrupted by -// a signal whose handler is the caller, say): then nothing it made since can be trusted. -static int kept_settle(void) { - unsigned old = __atomic_fetch_add(&g_close_epoch, 1, __ATOMIC_SEQ_CST) & 1; +static int kept_drained(unsigned epoch) { for (int i = 0; i < 1000; i++) { - if (__atomic_load_n(&g_closers[old], __ATOMIC_SEQ_CST) == 0) return 1; + if (__atomic_load_n(&g_closers[epoch], __ATOMIC_SEQ_CST) == 0) return 1; struct timespec pause = {0, 1000 * 1000}; nanosleep(&pause, NULL); } return 0; } +// 1 once every close that began before now has ended; 0 after a second without (one interrupted by +// a signal whose handler is the caller, say): then nothing it made since can be trusted. The epoch +// moves on only once the closes of the one before it are over too, so the two counts never mix one +// that is still under way from an earlier settle that gave up with the current one. +static int kept_settle(void) { + unsigned current = __atomic_load_n(&g_close_epoch, __ATOMIC_SEQ_CST) & 1; + if (!kept_drained(current ^ 1)) return 0; + unsigned old = __atomic_fetch_add(&g_close_epoch, 1, __ATOMIC_SEQ_CST) & 1; + return kept_drained(old); +} + // Calls fn for each part of [first, last] that leaves out the descriptors in kept (ascending); // the first nonzero answer of fn is returned, else 0. static int for_each_range_without(unsigned int first, unsigned int last, const int *kept, int nkept, diff --git a/tools/tests/test_native_hook_handoff.py b/tools/tests/test_native_hook_handoff.py index 22c7cc8364..382c3bf49c 100644 --- a/tools/tests/test_native_hook_handoff.py +++ b/tools/tests/test_native_hook_handoff.py @@ -121,7 +121,9 @@ static void *late_spawner(void *unused) { return NULL; } -static int late_record(const char *arg) { +// live: the late record is made while the later child is still handing its program off (the fake +// spawner holds its reply), so the start time it reads is that child's +static int late_record(const char *arg, int live) { int gate[2]; if (pipe(gate) != 0 || pipe(late_done) != 0 || pipe(late_hold) != 0) return 1; late_gate = gate[0]; @@ -143,6 +145,11 @@ static int late_record(const char *arg) { _exit(127); } printf("reused\t%d\n", pid == first); + int err = -1; + if (live) { + usleep(300000); // its handoff is with the app + if (write(gate[1], "1", 1) != 1 || read(late_done[0], &err, sizeof(err)) != sizeof(err)) return 1; + } // its forked child exits once the program runs: reaped here, the program's exit left alone char path[64]; snprintf(path, sizeof(path), "/proc/%d", (int)pid); @@ -151,8 +158,7 @@ static int late_record(const char *arg) { usleep(10000); } printf("reaped\t%d\n", access(path, F_OK) != 0); - int err = -1; - if (write(gate[1], "1", 1) != 1 || read(late_done[0], &err, sizeof(err)) != sizeof(err)) return 1; + if (!live && (write(gate[1], "1", 1) != 1 || read(late_done[0], &err, sizeof(err)) != sizeof(err))) return 1; printf("spawned\t%d\n", err); printf("own\t%d\n", (int)waitpid(pid, &status, WNOHANG | __WNOTHREAD) == 0); kill(pid, SIGKILL); @@ -197,9 +203,18 @@ static void *prep_closer(void *unused) { return NULL; } +static int prep_hold_ms = 0; // after the table's fd is there: past the hook's wait for the close + static void *prep_release(void *unused) { (void)unused; - usleep(300000); // the fork is making the table by now + // once the first fork has made the table's fd, while it waits for the close to end + char target[64]; + for (int i = 0; i < 1000; i++) { + ssize_t n = readlink("/proc/self/fd/800", target, sizeof(target) - 1); + if (n > 0 && (target[n] = 0, strncmp(target, "/memfd:ddnatv-handoff", 21) == 0)) break; + usleep(1000); + } + usleep(prep_hold_ms * 1000); if (write(prep_in[1], "1", 1) != 1) _exit(122); return NULL; } @@ -282,7 +297,8 @@ int main(int argc, char **argv) { nothread_go(1); pthread_exit(NULL); // the main thread ends too; the others finish the process } - if (strcmp(argv[1], "laterecord") == 0) return late_record(argv[2]); + if (strcmp(argv[1], "laterecord") == 0) return late_record(argv[2], 0); + if (strcmp(argv[1], "laterecordlive") == 0) return late_record(argv[2], 1); if (strcmp(argv[1], "nothreadgone") == 0) { nothread_arg = argv[2]; pthread_t forker; @@ -340,7 +356,11 @@ int main(int argc, char **argv) { pthread_join(forker, NULL); return 0; } - if (strcmp(argv[1], "prepclose") == 0) { + if (strcmp(argv[1], "prepclose") == 0 || strcmp(argv[1], "prepcloselate") == 0) { + // late: the close goes on past the hook's wait for it, through its second try; a later fork + // makes the table then + int late = argv[1][9] != 0; + prep_hold_ms = late ? 1500 : 0; if (pipe(prep_in) != 0 || pipe(prep_out) != 0) return 1; pthread_t closer, release; pthread_create(&closer, NULL, prep_closer, NULL); @@ -355,13 +375,21 @@ int main(int argc, char **argv) { pthread_join(release, NULL); pthread_join(closer, NULL); printf("ranged\t%ld\n", prep_closed); + int status = 0; + printf("same\t%d\n", waitpid(pid, &status, 0) == pid); + if (WIFEXITED(status)) printf("exit\t%d\n", WEXITSTATUS(status)); + if (late) { + pid_t again = fork(); + if (again == 0) { + execl("/bin/target", "target", argv[2], (char *)NULL); + _exit(127); + } + printf("again\t%d\n", waitpid(again, &status, 0) == again && WIFEXITED(status) ? WEXITSTATUS(status) : -1); + } char target[64]; ssize_t n = readlink("/proc/self/fd/800", target, sizeof(target) - 1); if (n > 0) target[n] = 0; printf("table\t%d\n", n > 0 && strncmp(target, "/memfd:ddnatv-handoff", 21) == 0); - int status = 0; - printf("same\t%d\n", waitpid(pid, &status, 0) == pid); - if (WIFEXITED(status)) printf("exit\t%d\n", WEXITSTATUS(status)); return 0; } // waitidpid: the child holds before its exec until the parent's wait has begun @@ -1056,7 +1084,11 @@ class NativeHookHandoffTest(unittest.TestCase): # the fork's child could not read its own start time: its record has none to be told by self.late_record({"DDH_TEST_FORK_START_UNKNOWN": "1"}) - def late_record(self, env=None): + def test_a_late_record_reading_the_new_childs_start_keeps_its_owner(self): + # made while the later child, which could not read its own start, still hands its program off + self.late_record({"DDH_TEST_FORK_START_UNKNOWN": "1"}, live=True) + + def late_record(self, env=None, live=False): if self.child_status != "3": self.skipTest("a child here cannot exec an aarch64 program (no binfmt_misc), so nothing can be spawned") # the pid has to be given again, which only a pid namespace of the test's own allows @@ -1073,7 +1105,8 @@ class NativeHookHandoffTest(unittest.TestCase): self.assertEqual(0, out.returncode, out.stderr) self.assertNotIn("skipped", out.stderr, out.stderr) return - result, requests, out = self.run_parent("laterecord", "sleep", extra_env=env) + result, requests, out = self.run_parent("laterecordlive" if live else "laterecord", "sleep", extra_env=env, + delay=1.0 if live else 0) self.assertEqual("1", result.get("reused"), out.stdout + out.stderr) self.assertEqual("0", result.get("spawned"), out.stdout) self.assertEqual("1", result.get("reaped"), out.stdout) @@ -1200,6 +1233,19 @@ class NativeHookHandoffTest(unittest.TestCase): self.assertEqual("1", result.get("same"), out.stdout) self.assertEqual("4", result.get("exit"), out.stdout) + def test_a_table_given_up_while_a_close_runs_past_the_wait_is_made_later(self): + if self.child_status != "3": + self.skipTest("a child here cannot exec an aarch64 program (no binfmt_misc), so the first runs nowhere") + # the close goes on past both tries of the first fork, whose child then runs here; the next + # fork makes the table + result, requests, out = self.run_parent("prepcloselate", "4", extra_env={"DDH_TEST_LATE_TABLE": "1"}) + self.assertEqual("0", result.get("ranged"), out.stdout + out.stderr) + self.assertEqual("1", result.get("same"), out.stdout) + self.assertEqual("4", result.get("exit"), out.stdout) + self.assertEqual("4", result.get("again"), out.stdout) + self.assertEqual(1, len(requests), out.stderr) + self.assertEqual("1", result.get("table"), out.stdout) + def test_a_dup2_onto_the_parents_pidfd_makes_no_descriptor_and_keeps_the_parent(self): parent, result, out = self.run_worker_program("dupover", DUPOVER) self.assertEqual("1", result.get("pidfd"), out.stdout + out.stderr)