mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
fix(handoff): table setup not cancellable; test spawner never runs what it cannot end
This commit is contained in:
1 parent
d2f270d93c
commit
80ec4d7517
2 files changed
+70
-37
No files matched your search
@@ -274,7 +274,12 @@ static void ddh_prepare(void) {
|
||||
return;
|
||||
}
|
||||
// a second try for a table another thread's close took before it was kept
|
||||
// Not cancelled meanwhile (the wait for earlier closes sleeps, a cancellation point), which would
|
||||
// leave the guard taken and the candidate's state half kept for every later fork.
|
||||
int cancel = PTHREAD_CANCEL_ENABLE;
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel);
|
||||
for (int attempt = 0; attempt < 2 && !ddh_prepare_table(); attempt++) {}
|
||||
pthread_setcancelstate(cancel, NULL);
|
||||
__atomic_store_n(&g_ddh_preparing, 0, __ATOMIC_RELEASE);
|
||||
}
|
||||
|
||||
@@ -1811,12 +1816,12 @@ static ddh_wait4_f ddh_real_wait4(void) {
|
||||
// The app's and the program's SIGCHLD are the usual ones.
|
||||
// Tests only ("IN OUT TID"): thread TID's wait, once its look has found a child, writes a byte to OUT
|
||||
// and waits for one on IN, as a waiter descheduled before it takes that child would.
|
||||
static void ddh_wait_gate(void) {
|
||||
static void ddh_wait_gate(int code) {
|
||||
const char *gate = getenv("DDH_TEST_WAIT_GATE");
|
||||
int in, out, tid;
|
||||
if (!gate || sscanf(gate, "%d %d %d", &in, &out, &tid) != 3 || tid != (int)syscall(SYS_gettid)) return;
|
||||
int saved = errno;
|
||||
char c = 1;
|
||||
char c = (char)code; // the event it found (CLD_EXITED, CLD_STOPPED...)
|
||||
syscall(SYS_write, out, &c, 1);
|
||||
while (syscall(SYS_read, in, &c, 1) < 0 && errno == EINTR) {}
|
||||
errno = saved;
|
||||
@@ -2006,7 +2011,7 @@ static pid_t ddh_wait(pid_t pid, int *status, int options, struct rusage *ru) {
|
||||
(unsigned long)look, 0, 0);
|
||||
if (r == 0 && seen.si_pid > 0) {
|
||||
pid_t child = seen.si_pid;
|
||||
ddh_wait_gate();
|
||||
ddh_wait_gate(seen.si_code);
|
||||
// A stop or a continue reaps nothing: reported as it is, the entry and the claim left alone
|
||||
int ended = seen.si_code == CLD_EXITED || seen.si_code == CLD_KILLED || seen.si_code == CLD_DUMPED;
|
||||
struct ddh_entry *h = ended ? ddh_find(child) : NULL;
|
||||
@@ -2154,7 +2159,7 @@ static int ddh_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
|
||||
}
|
||||
} else if (seen.si_pid > 0) {
|
||||
pid_t child = seen.si_pid;
|
||||
ddh_wait_gate();
|
||||
ddh_wait_gate(seen.si_code);
|
||||
int exited = seen.si_code == CLD_EXITED || seen.si_code == CLD_KILLED || seen.si_code == CLD_DUMPED;
|
||||
struct ddh_entry *h = exited ? ddh_find(child) : NULL;
|
||||
int32_t gen = h ? ddh_gen_of(h, child) : 0;
|
||||
|
||||
@@ -674,7 +674,9 @@ int main(int argc, char **argv) {
|
||||
pthread_t waiter;
|
||||
pthread_create(&waiter, NULL, peek_waiter, NULL);
|
||||
char c;
|
||||
if (read(peek_out[0], &c, 1) != 1) return 1; // it has seen the stop
|
||||
if (read(peek_out[0], &c, 1) != 1) return 1; // it has seen an event: the gate says which
|
||||
printf("peeked\t%d\n", c);
|
||||
if (c != CLD_STOPPED) return 0; // not the stop, so not the ordering this is about: the test fails
|
||||
syscall(SYS_tgkill, peek_child, peek_child, SIGCONT);
|
||||
// the reply has come, the program runs, the forked child has exited (its exit still not taken)
|
||||
int gone = 0;
|
||||
@@ -955,8 +957,12 @@ class FakeSpawner(threading.Thread):
|
||||
self.reports = reports # False: as an app that died with its worker, no exit report
|
||||
self.requests = []
|
||||
# The programs it ran, as pidfds: ended with the test whatever becomes of them, and never another
|
||||
# process given a pid of theirs once they have been reaped
|
||||
# process given a pid of theirs once they have been reaped. A program it could not have one for
|
||||
# is never run (failures says so); none is started once the ending has begun (closed).
|
||||
self.pidfds = []
|
||||
self.failures = []
|
||||
self.closed = False
|
||||
self.lock = threading.Lock()
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
@@ -988,33 +994,44 @@ class FakeSpawner(threading.Thread):
|
||||
threading.Event().wait(self.delay)
|
||||
# As a worker does: a process whose pid goes back first, which runs the program only
|
||||
# once the hook takes it on with "go"
|
||||
gate_r, gate_w = os.pipe()
|
||||
pid = os.fork()
|
||||
if pid:
|
||||
try:
|
||||
self.pidfds.append(os.pidfd_open(pid))
|
||||
except (AttributeError, OSError):
|
||||
pass # no pidfds here: such a program is only ended by its own end
|
||||
if pid == 0:
|
||||
os.close(gate_w)
|
||||
if os.read(gate_r, 1) == b"1":
|
||||
argv = list(self.prefix) + [program] + request["argv"][1:]
|
||||
os.execv(argv[0] if os.path.isabs(argv[0]) else shutil.which(argv[0]), argv)
|
||||
os._exit(127)
|
||||
os.close(gate_r)
|
||||
conn.sendall(b"pid %d\n" % pid)
|
||||
went = not request.get("ack") or self.read(conn, 3) == b"go\n"
|
||||
for fd in fds:
|
||||
if fd != table:
|
||||
os.close(fd)
|
||||
if went:
|
||||
if request.get("ack"):
|
||||
conn.sendall(b"ok\n") # the app's last word, before the worker may run it
|
||||
os.write(gate_w, b"1")
|
||||
conn.close()
|
||||
os.close(gate_w)
|
||||
with self.lock:
|
||||
pid, pidfd = -1, None
|
||||
if not self.closed:
|
||||
gate_r, gate_w = os.pipe()
|
||||
pid = os.fork()
|
||||
if pid == 0:
|
||||
os.close(gate_w)
|
||||
if os.read(gate_r, 1) == b"1":
|
||||
argv = list(self.prefix) + [program] + request["argv"][1:]
|
||||
os.execv(argv[0] if os.path.isabs(argv[0]) else shutil.which(argv[0]), argv)
|
||||
os._exit(127)
|
||||
os.close(gate_r)
|
||||
try:
|
||||
pidfd = os.pidfd_open(pid)
|
||||
self.pidfds.append(pidfd)
|
||||
except (AttributeError, OSError) as error:
|
||||
self.failures.append("no pidfd for a worker: %s" % error)
|
||||
went = False
|
||||
try:
|
||||
if pidfd is not None:
|
||||
conn.sendall(b"pid %d\n" % pid)
|
||||
went = not request.get("ack") or self.read(conn, 3) == b"go\n"
|
||||
if went and request.get("ack"):
|
||||
conn.sendall(b"ok\n") # the app's last word, before the worker may run it
|
||||
if went:
|
||||
os.write(gate_w, b"1")
|
||||
except OSError:
|
||||
went = False # the fixture went away mid-handshake
|
||||
finally:
|
||||
for fd in fds:
|
||||
if fd != table:
|
||||
os.close(fd)
|
||||
conn.close()
|
||||
if pid > 0:
|
||||
os.close(gate_w) # a worker not told to go ends at once
|
||||
if not went:
|
||||
os.waitpid(pid, 0)
|
||||
if pid > 0:
|
||||
os.waitpid(pid, 0)
|
||||
os.close(table)
|
||||
continue
|
||||
threading.Thread(target=self.report if self.reports else self.reap, args=(pid, table, request), daemon=True).start()
|
||||
@@ -1081,13 +1098,21 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
|
||||
@staticmethod
|
||||
def end_programs(spawner):
|
||||
while spawner.pidfds:
|
||||
fd = spawner.pidfds.pop()
|
||||
with spawner.lock:
|
||||
spawner.closed = True # no program starts after this
|
||||
pidfds, spawner.pidfds = spawner.pidfds, []
|
||||
failed = []
|
||||
for fd in pidfds:
|
||||
try:
|
||||
signal.pidfd_send_signal(fd, signal.SIGKILL)
|
||||
except (AttributeError, OSError):
|
||||
pass
|
||||
os.close(fd)
|
||||
except ProcessLookupError:
|
||||
pass # gone already
|
||||
except OSError as error:
|
||||
failed.append(str(error))
|
||||
finally:
|
||||
os.close(fd)
|
||||
if failed:
|
||||
raise RuntimeError("workers that may still run: %s" % failed)
|
||||
|
||||
def run_parent(self, *argv, handoff="target", hook=True, reports=True, pressure=None, as_shell=False, terminal=False,
|
||||
delay=0, extra_env=None):
|
||||
@@ -1169,6 +1194,8 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
stdout, err = "", "(its output still open after it was ended)"
|
||||
self.fail("the fixture did not finish:\n%s\n%s" % (stdout, err))
|
||||
out = subprocess.CompletedProcess(proc.args, proc.returncode, stdout, err)
|
||||
if spawner.failures:
|
||||
self.fail("the fake spawner could not run a program safely: %s" % spawner.failures)
|
||||
result = dict(line.split("\t", 1) for line in out.stdout.splitlines() if "\t" in line)
|
||||
return result, spawner.requests, out
|
||||
|
||||
@@ -1391,6 +1418,7 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
|
||||
def test_a_stop_seen_before_the_child_hands_off_reports_its_program(self):
|
||||
result, requests, out = self.run_parent("stoppeek", "6", delay=1.0)
|
||||
self.assertEqual(str(os.CLD_STOPPED), result.get("peeked"), out.stdout + out.stderr) # the look saw the stop
|
||||
self.assertEqual(1, len(requests), out.stderr)
|
||||
self.assertEqual("1", result.get("shimexited"), out.stdout + out.stderr)
|
||||
# the one wait that saw the stop reports the program's end, not a stop and not the forked child's
|
||||
|
||||
Reference in new issue
Block a user