mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
fix(handoff): fork record generations, no pidfd restore after a failed move
This commit is contained in:
1 parent
f7a90151ec
commit
b65af1fa9f
2 files changed
+20
-7
No files matched your search
@@ -116,8 +116,15 @@ static __thread pid_t t_ddh_only_thread;
|
||||
#define DDH_FORKS (1u << 22) /* PID_MAX_LIMIT */
|
||||
static uint64_t *g_ddh_forks;
|
||||
|
||||
static uint64_t ddh_fork_record(pid_t child, pid_t thread) { return (uint64_t)(uint32_t)child << 32 | (uint32_t)thread; }
|
||||
static pid_t ddh_record_child(uint64_t r) { return (pid_t)(uint32_t)(r >> 32); }
|
||||
// child << 42 | thread << 20 | generation: pids and thread ids are below 2^22 (PID_MAX_LIMIT), and
|
||||
// the generation (this process's fork count) tells a record from an identical one made after it
|
||||
static uint32_t g_ddh_fork_generation;
|
||||
static uint64_t ddh_fork_record(pid_t child, pid_t thread) {
|
||||
uint64_t generation = __atomic_add_fetch(&g_ddh_fork_generation, 1, __ATOMIC_RELAXED) & 0xfffff;
|
||||
return (uint64_t)((uint32_t)child & 0x3fffff) << 42 | (uint64_t)((uint32_t)thread & 0x3fffff) << 20 | generation;
|
||||
}
|
||||
static pid_t ddh_record_child(uint64_t r) { return (pid_t)(r >> 42); }
|
||||
static pid_t ddh_record_thread(uint64_t r) { return (pid_t)((r >> 20) & 0x3fffff); }
|
||||
|
||||
static uint64_t *ddh_forks(int create) {
|
||||
uint64_t *map = __atomic_load_n(&g_ddh_forks, __ATOMIC_ACQUIRE);
|
||||
@@ -140,7 +147,7 @@ static void ddh_forked_by(pid_t child, pid_t thread) {
|
||||
static pid_t ddh_forking_thread(pid_t child) {
|
||||
uint64_t *map = ddh_forks(0);
|
||||
uint64_t r = map && child > 0 ? __atomic_load_n(&map[(uint32_t)child % DDH_FORKS], __ATOMIC_ACQUIRE) : 0;
|
||||
return r && ddh_record_child(r) == child ? (pid_t)(uint32_t)r : 0;
|
||||
return r && ddh_record_child(r) == child ? ddh_record_thread(r) : 0;
|
||||
}
|
||||
|
||||
// The child's end has been taken: its record goes (unless a new child of the pid has one already).
|
||||
@@ -370,7 +377,8 @@ static int ddh_parent_pidfd_moves(int target) {
|
||||
int moved = (int)call_orig_syscall(SYS_fcntl, (unsigned long)fd, F_DUPFD_CLOEXEC, 900, 0, 0, 0);
|
||||
ddh_unkeep(fd);
|
||||
__atomic_store_n(&g_ddh_parent_pidfd, moved, __ATOMIC_RELEASE);
|
||||
if (moved >= 0) ddh_keep(moved);
|
||||
if (moved < 0) return -1; // no copy (out of fds): the pidfd is given up, /proc answers from now on
|
||||
ddh_keep(moved);
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
||||
@@ -136,15 +136,17 @@ static void *late_bystander(void *unused) {
|
||||
// both waits only once the main thread has ended (its task a zombie)
|
||||
char path[64], stat[256];
|
||||
snprintf(path, sizeof(path), "/proc/self/task/%d/stat", (int)getpid());
|
||||
for (int i = 0; i < 500; i++) {
|
||||
int ended = 0;
|
||||
for (int i = 0; i < 500 && !ended; i++) {
|
||||
FILE *f = fopen(path, "r");
|
||||
size_t n = f ? fread(stat, 1, sizeof(stat) - 1, f) : 0;
|
||||
if (f) fclose(f);
|
||||
stat[n] = 0;
|
||||
char *s = strrchr(stat, ')');
|
||||
if (!f || (s && s[1] == ' ' && (s[2] == 'Z' || s[2] == 'X'))) break;
|
||||
usleep(10000);
|
||||
ended = f && s && s[1] == ' ' && (s[2] == 'Z' || s[2] == 'X');
|
||||
if (!ended) usleep(10000);
|
||||
}
|
||||
printf("mainended\t%d\n", ended);
|
||||
int status = 0;
|
||||
printf("bystander\t%d\n", (int)waitpid(nothread_child, &status, WNOHANG | __WNOTHREAD));
|
||||
nothread_go(2);
|
||||
@@ -665,6 +667,9 @@ class NativeHookHandoffTest(unittest.TestCase):
|
||||
# (qemu-user has a thread of its own first) is what the same program shows without the hook.
|
||||
result, requests, out = self.run_parent("nothreadheir", "7")
|
||||
plain, _, plain_out = self.run_parent("nothreadheir", "7", hook=False)
|
||||
# the waits ran only after the main thread had ended, in both runs
|
||||
self.assertEqual("1", result.get("mainended"), out.stdout)
|
||||
self.assertEqual("1", plain.get("mainended"), plain_out.stdout)
|
||||
self.assertEqual(1, len(requests), out.stderr)
|
||||
self.assertEqual(plain.get("bystander"), result.get("bystander"), (out.stdout, plain_out.stdout))
|
||||
self.assertEqual(plain.get("own"), result.get("own"), (out.stdout, plain_out.stdout))
|
||||
|
||||
Reference in new issue
Block a user