mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
test(handoff): exited-entry precondition, atomic checkpoints, a pidfd-only zombie check; validate the shim hold fd
This commit is contained in:
1 parent
3b793b9dbe
commit
359af2bc0b
2 files changed
+50
-22
No files matched your search
@@ -1586,9 +1586,11 @@ static void ddh_try_as(const char *filename, char *const argv[], char *const env
|
||||
// Tests only ("FD"): the forked child waits for a byte on FD before it exits, as one descheduled
|
||||
// here would, while its program's exit may already be in the table.
|
||||
const char *hold = ddh_env(envp, "DDH_TEST_SHIM_HOLD");
|
||||
if (hold) {
|
||||
char *end = NULL;
|
||||
long hold_fd = hold && *hold ? strtol(hold, &end, 10) : -1;
|
||||
if (hold_fd > 0 && hold_fd < INT_MAX && end && *end == 0) {
|
||||
char c;
|
||||
while (syscall(SYS_read, atoi(hold), &c, 1) < 0 && errno == EINTR) {}
|
||||
while (syscall(SYS_read, (int)hold_fd, &c, 1) < 0 && errno == EINTR) {}
|
||||
}
|
||||
syscall(SYS_exit_group, 0);
|
||||
}
|
||||
|
||||
@@ -437,8 +437,14 @@ static int nohang_two(const char *arg) {
|
||||
}
|
||||
if (a < 0) return 1;
|
||||
unsetenv("DDH_TEST_SHIM_HOLD");
|
||||
// exited in the table (2), or handed off (1) with the app's report of its exit in: either is an exit
|
||||
// the wait's look for exited entries reaches, not a claim (3) still under way
|
||||
int done = 0;
|
||||
for (int i = 0; i < 1000 && !(done = reported(a) || entry_state_of(a, NULL) == 2); i++) usleep(10000);
|
||||
for (int i = 0; i < 1000 && !done; i++) {
|
||||
int st = entry_state_of(a, NULL);
|
||||
done = st == 2 || (st == 1 && reported(a));
|
||||
if (!done) usleep(10000);
|
||||
}
|
||||
printf("aexited\t%d\n", done);
|
||||
pid_t b = fork();
|
||||
if (b == 0) _exit(5);
|
||||
@@ -465,22 +471,20 @@ static int nohang_two(const char *arg) {
|
||||
// record gate); meanwhile another thread's blocking wait reaps the child's forked child and must go
|
||||
// on waiting for the program, not drop it when the record is made
|
||||
static int rbr_gate[2];
|
||||
static volatile pid_t rbr_tid;
|
||||
static volatile pid_t rbr_pid;
|
||||
static volatile pid_t rbr_got = -2;
|
||||
static pid_t rbr_pid; // all three read before the join of their writer: atomic
|
||||
static pid_t rbr_got = -2;
|
||||
static int rbr_status;
|
||||
|
||||
static void *rbr_forker(void *arg) {
|
||||
char gate[32];
|
||||
snprintf(gate, sizeof(gate), "%d %d", rbr_gate[0], (int)syscall(SYS_gettid));
|
||||
setenv("DDH_TEST_RECORD_GATE", gate, 1);
|
||||
rbr_tid = (pid_t)syscall(SYS_gettid);
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
execl("/bin/target", "target", (const char *)arg, (char *)NULL);
|
||||
_exit(127);
|
||||
}
|
||||
rbr_pid = pid;
|
||||
__atomic_store_n(&rbr_pid, pid, __ATOMIC_RELEASE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -489,7 +493,7 @@ static void *rbr_waiter(void *unused) {
|
||||
int status = 0;
|
||||
pid_t r = waitpid(-1, &status, 0);
|
||||
rbr_status = status;
|
||||
rbr_got = r;
|
||||
__atomic_store_n(&rbr_got, r, __ATOMIC_RELEASE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -501,20 +505,27 @@ static int reap_before_record(const char *arg) {
|
||||
int st = -1;
|
||||
for (int i = 0; i < 1000 && (st = entry_state_of(0, &child)) != 1; i++) usleep(10000);
|
||||
int exited = 0;
|
||||
for (int i = 0; st == 1 && i < 500 && !(exited = zombie(child)); i++) usleep(10000);
|
||||
printf("handed\t%d\n", st == 1 && exited);
|
||||
for (int i = 0; st == 1 && child > 0 && i < 500 && !(exited = zombie(child)); i++) usleep(10000);
|
||||
int handed = st == 1 && child > 0 && exited;
|
||||
printf("handed\t%d\n", handed);
|
||||
if (!handed) {
|
||||
// no handed-off child to go on with: the forker let go, and nothing signalled
|
||||
if (write(rbr_gate[1], "1", 1) != 1) return 1;
|
||||
pthread_join(forker, NULL);
|
||||
return 0;
|
||||
}
|
||||
pthread_create(&waiter, NULL, rbr_waiter, NULL);
|
||||
char path[32];
|
||||
snprintf(path, sizeof(path), "/proc/%d", (int)child);
|
||||
int reaped = 0;
|
||||
for (int i = 0; i < 500 && !(reaped = access(path, F_OK) != 0); i++) usleep(10000);
|
||||
printf("shimreaped\t%d\n", reaped);
|
||||
printf("unrecorded\t%d\n", rbr_pid == 0);
|
||||
printf("unrecorded\t%d\n", __atomic_load_n(&rbr_pid, __ATOMIC_ACQUIRE) == 0);
|
||||
if (write(rbr_gate[1], "1", 1) != 1) return 1;
|
||||
pthread_join(forker, NULL);
|
||||
printf("forked\t%d\n", rbr_pid == child);
|
||||
usleep(500000);
|
||||
printf("waiting\t%d\n", rbr_got == -2);
|
||||
printf("waiting\t%d\n", __atomic_load_n(&rbr_got, __ATOMIC_ACQUIRE) == -2);
|
||||
kill(child, SIGTERM);
|
||||
pthread_join(waiter, NULL);
|
||||
printf("got\t%d\n", rbr_got == child);
|
||||
@@ -524,16 +535,17 @@ static int reap_before_record(const char *arg) {
|
||||
|
||||
// waitidblocked: the child holds before its exec until the parent's waitid(P_PID) has been seen blocked
|
||||
// (its thread asleep) with no entry claimed yet, so the wait begins before the handoff exists
|
||||
static siginfo_t wb_info;
|
||||
static volatile int wb_done = -2;
|
||||
static siginfo_t wb_info; // read only after the join
|
||||
static int wb_done = -2; // these two polled meanwhile: atomic
|
||||
static pid_t wb_tid;
|
||||
static pid_t wb_child;
|
||||
static volatile pid_t wb_tid;
|
||||
|
||||
static void *wb_waiter(void *unused) {
|
||||
(void)unused;
|
||||
wb_tid = (pid_t)syscall(SYS_gettid);
|
||||
memset(&wb_info, 0, sizeof(wb_info));
|
||||
wb_done = waitid(P_PID, (id_t)wb_child, &wb_info, WEXITED);
|
||||
__atomic_store_n(&wb_tid, (pid_t)syscall(SYS_gettid), __ATOMIC_RELEASE);
|
||||
int r = waitid(P_PID, (id_t)wb_child, &wb_info, WEXITED);
|
||||
__atomic_store_n(&wb_done, r, __ATOMIC_RELEASE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -555,7 +567,8 @@ static int waitid_blocked(const char *arg) {
|
||||
int asleep = 0;
|
||||
for (int i = 0; i < 500 && asleep < 10; i++) {
|
||||
usleep(20000);
|
||||
asleep = wb_tid && wb_done == -2 && thread_state(wb_tid) == 'S' ? asleep + 1 : 0;
|
||||
pid_t tid = __atomic_load_n(&wb_tid, __ATOMIC_ACQUIRE);
|
||||
asleep = tid && __atomic_load_n(&wb_done, __ATOMIC_ACQUIRE) == -2 && thread_state(tid) == 'S' ? asleep + 1 : 0;
|
||||
}
|
||||
printf("blocked\t%d\n", asleep >= 10);
|
||||
printf("unclaimed\t%d\n", entry_state_of(wb_child, NULL) < 0);
|
||||
@@ -1172,10 +1185,14 @@ int main(void) {
|
||||
}
|
||||
"""
|
||||
|
||||
# A worker's program that checks its parent through the hook's pidfd (kept at 900) when it has one
|
||||
# A worker's program that checks its parent through the hook's pidfd (kept at 900) when it has one.
|
||||
# Built with NOFDS, it first takes every free descriptor, so that /proc cannot be read: the answer can
|
||||
# then only come from the pidfd, which needs no new one (and /proc's failure counts as alive).
|
||||
PIDFDPPID = r"""
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/resource.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
@@ -1183,6 +1200,13 @@ int main(void) {
|
||||
ssize_t n = readlink("/proc/self/fd/900", target, sizeof(target) - 1);
|
||||
target[n > 0 ? n : 0] = 0;
|
||||
printf("pidfd\t%d\n", strcmp(target, "anon_inode:[pidfd]") == 0);
|
||||
fflush(stdout);
|
||||
#ifdef NOFDS
|
||||
struct rlimit lim = {512, 512}; // below the hook's kept fds, which stay open
|
||||
setrlimit(RLIMIT_NOFILE, &lim);
|
||||
while (open("/dev/null", O_RDONLY) >= 0) {}
|
||||
printf("nofds\t%d\n", open("/proc/self/stat", O_RDONLY) < 0);
|
||||
#endif
|
||||
printf("parent\t%d\n", (int)getppid());
|
||||
return 0;
|
||||
}
|
||||
@@ -1895,9 +1919,11 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
break
|
||||
pause.wait(0.05)
|
||||
self.assertEqual("Z", stat.read_text().rsplit(")", 1)[1].split()[0])
|
||||
# a pidfd opens for an exited parent that is not reaped, and reads as exited
|
||||
_, result, out = self.run_worker_program("pidfdzombie", PIDFDPPID, parent=forker.pid)
|
||||
# a pidfd opens for an exited parent that is not reaped, and reads as exited; /proc cannot be
|
||||
# read meanwhile (no free fd), where a failure counts as alive: only the pidfd gives init
|
||||
_, result, out = self.run_worker_program("pidfdzombie", "#define NOFDS 1\n" + PIDFDPPID, parent=forker.pid)
|
||||
self.assertEqual("1", result.get("pidfd"), out.stdout + out.stderr)
|
||||
self.assertEqual("1", result.get("nofds"), out.stdout)
|
||||
self.assertEqual("1", result.get("parent"), out.stdout)
|
||||
self.assertEqual("Z", stat.read_text().rsplit(")", 1)[1].split()[0])
|
||||
|
||||
|
||||
Reference in new issue
Block a user