package mac import ( "context" "errors" "os" "slices" "sync/atomic" "syscall" "testing" "time" ) // Fix round 1 (Astra round 1) regressions on synthetic tables: rows // made up 2026-11-28 in the darwin table's shape (see sweep_test.go). // liveTurn begins a sweeping turn on tb and starts a leader that exits at // once; what the turn left is what the test adds to tb. func liveTurn(t *testing.T, tb Procs, own *Own, settle time.Duration) (*Turns, *Turn, *Executor, *Run) { t.Helper() turns := NewTurns("").Sweep(tb) turns.own, turns.Settle = own, settle u, err := turns.Take(context.Background(), OpEngineering) if err != nil { t.Fatal(err) } x := &Executor{Turns: turns, Grace: 50 * time.Millisecond, Settle: settle, Poll: 10 * time.Millisecond} run, err := x.Start(u.Context(context.Background()), Command{Argv: []string{"/bin/true"}}, "attempt-l", func(string) error { return nil }) if err != nil { t.Fatal(err) } <-run.Exited() return turns, u, x, run } func untouched(t *testing.T, sig []int, pids ...int) { t.Helper() for _, pid := range pids { if slices.Contains(sig, pid) || slices.Contains(sig, -pid) { t.Fatalf("%d (there before the turn, or spared) was signalled: %v", pid, sig) } } } // #10: a step that double-forked to launchd between two polls and runs a // system binary (a /System stand-in for a script interpreter) is killed, // and while it lives the end is not proven and no turn begins. What was // there before the turn is never touched. func TestSweepKillsSystemPathEscapes(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) turns, u, _, run := liveTurn(t, tb, &Own{}, 250*time.Millisecond) tb.add(row(82100, 1, 82100, 82100, 5100, me, ""), "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc") tb.mark(&tb.unkillable, 82100) if run.Stop() { t.Fatal("proven over with a double-forked system-path process alive") } if !slices.Contains(tb.signalled(), 82100) { t.Fatalf("the escape was spared: %v", tb.signalled()) } u.Give() if _, err := turns.Take(context.Background(), OpCheck); !errors.Is(err, ErrUnsettled) { t.Fatalf("a turn began beside it: %v", err) } tb.unmark(&tb.unkillable, 82100) next, err := turns.Take(context.Background(), OpCheck) if err != nil { t.Fatalf("not settled once it could die: %v", err) } next.Give() if tb.has(82100) { t.Fatal("the escape survived settling") } untouched(t, tb.signalled(), 40100, 40200, 40300) } // #10: a system agent launchd started during the turn is killed like // anything else new; what launchd starts again from a system path after // the kill pass began is a relaunch, spared and not in the way of the // proof. Something new outside the system after the kill pass is killed. func TestSweepSparesLaunchdRelaunches(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) tb.after = func(tb *table, pid int, sig syscall.Signal) { if (pid == 82200 || pid == -82200) && tb.procs[82201].PID == 0 { tb.procs[82201] = row(82201, 1, 82201, 82201, 99000, me, "") tb.paths[82201] = "/usr/libexec/trustd" tb.procs[82300] = row(82300, 1, 82300, 82300, 99001, me, "") tb.paths[82300] = "/Users/paulequipo/w/late" } } _, u, _, run := liveTurn(t, tb, &Own{}, time.Second) defer u.Give() tb.add(row(82200, 1, 82200, 82200, 5200, me, ""), "/usr/libexec/trustd") if !run.Stop() { t.Fatalf("a launchd relaunch after the kill pass blocked the proof: %v", tb.signalled()) } sig := tb.signalled() for _, pid := range []int{82200, 82300} { if !slices.Contains(sig, pid) && !slices.Contains(sig, -pid) { t.Fatalf("%d (new during the turn) was spared: %v", pid, sig) } } untouched(t, sig, 82201, 40100, 40200, 40300) } // #3: a helper's child seen while the helper lived stays the runner's // own after the helper exits and the child is reparented; a process that // later has the helper's pid (another start) is not, nor its child. func TestSweepSparesOrphanedHelperDescendants(t *testing.T) { me, self := os.Getuid(), os.Getpid() tb := parseTable(t, turnBegan) own := &Own{} sig := sweepTurn(t, tb, own, func(*Turn) { tb.add(row(51100, self, self, self, 2000, me, ""), "/usr/bin/xcrun") own.Add(ProcID{PID: 51100, Start: 2000}) tb.add(row(51101, 51100, 51101, 51101, 2001, me, ""), "/Applications/Xcode.app/Contents/Developer/usr/bin/simctl") if _, _, err := own.Snapshot(time.Now().Add(time.Second), tb, nil); err != nil { t.Fatal(err) } tb.remove(51100) tb.reparent(51101, 1) tb.add(row(51100, 1, 51100, 51100, 2500, me, ""), "/Users/paulequipo/w/reused") tb.add(row(51102, 51100, 51100, 51100, 2501, me, ""), "/Users/paulequipo/w/reused-child") }) untouched(t, sig, 51101) for _, pid := range []int{51100, 51102} { if tb.has(pid) { t.Fatalf("%d (a reused helper pid and its child) was spared: %v", pid, sig) } } } // #5: a group is signalled whole only while a freshly read member is // still in it: a process that left the group between the table and the // re-check (setpgid, setsid) does not license a signal to the old group, // in the sweep or in the restart path. func TestGroupSignalNeedsFreshMembership(t *testing.T) { me := os.Getuid() moveOut := func(tb *table, target int) { moved := false tb.lookup = func(tb *table, pid int) { tb.mu.Lock() defer tb.mu.Unlock() if pid == target && !moved { moved = true q := tb.procs[pid] q.PGID = pid tb.procs[pid] = q } } } tb := parseTable(t, turnBegan) sig := sweepTurn(t, tb, &Own{}, func(u *Turn) { u.note(span{61200: 3000}, nil) tb.add(row(61201, 1, 61200, 61201, 3001, me, ""), "/Users/paulequipo/w/a") moveOut(tb, 61201) }) untouched(t, sig, 61200) tb = parseTable(t, turnBegan) tb.add(row(61301, 1, 61300, 61301, 3100, me, "attempt-v"), "/Users/paulequipo/w/b") moveOut(tb, 61301) x := &Executor{Procs: tb, Own: &Own{}, Settle: 300 * time.Millisecond} x.Verify(RecordID(61300, "attempt-v")) untouched(t, tb.signalled(), 61300) } // #6: every group and session the attempt made must be gone, through // the live proof and through recovery: one now held only by another // user's process (never signalled) keeps the attempt unproven until it // ends. func TestRecoveryKeepsEveryGroupAndSession(t *testing.T) { me := os.Getuid() for _, c := range []struct { name string other ProcInfo }{ // the group's id, now another user's group {"group", row(91001, 1, 91001, 91001, 7000, 501, "")}, // the session's only member left, another user's (a setuid program) {"session", row(91100, 1, 91100, 91001, 7000, 501, "")}, // our own process, which became root (a setuid program that // called setuid): the same identity, out of our reach {"setuid", row(91001, 1, 91002, 91002, 6001, 0, "")}, } { t.Run(c.name, func(t *testing.T) { tb := parseTable(t, turnBegan) // Once ours is killed, another user's process has its id (or // is what is left of its session). tb.after = func(tb *table, pid int, sig syscall.Signal) { if (pid == 91001 || pid == -91001) && tb.procs[c.other.PID].UID != 501 { tb.procs[c.other.PID] = c.other tb.paths[c.other.PID] = "/usr/bin/login" // Signals before this were at our own process. tb.sent = nil } } turns, u, x, run := liveTurn(t, tb, &Own{}, 200*time.Millisecond) tb.add(row(91001, 1, 91001, 91001, 6001, me, "attempt-l"), "/Users/paulequipo/w/m") if run.Stop() { t.Fatal("proven over while another user holds what it made") } u.Give() // Nothing of the runner user's is left: the Mac takes turns // again, and the attempt still owes what another user holds. next, err := turns.Take(context.Background(), OpCheck) if err != nil { t.Fatalf("the Mac stays blocked on another user's process: %v", err) } next.Give() if x.Verify(RecordID(0, "attempt-l")) { t.Fatal("recovery proved it over while that lasts") } untouched(t, tb.signalled(), c.other.PID) tb.remove(c.other.PID) if !x.Verify(RecordID(0, "attempt-l")) { t.Fatal("not proven once it ended") } }) } } // slowTable's List takes delay and then shows nothing: a read that // returns after the deadline with good news. type slowTable struct { *table delay time.Duration slow atomic.Bool } func (s *slowTable) List(deadline time.Time, markers []string) ([]ProcInfo, error) { if s.slow.Load() { time.Sleep(s.delay) return nil, nil } return s.table.List(deadline, markers) } // #7: one absolute deadline: a table read that returns after it never // makes a proof, in the live stop, in recovery, or settling a turn. func TestProofNeverAfterItsDeadline(t *testing.T) { tb := &slowTable{table: parseTable(t, turnBegan), delay: 250 * time.Millisecond} x := &Executor{Procs: tb, Own: &Own{}, Grace: 10 * time.Millisecond, Settle: 100 * time.Millisecond, Poll: time.Hour} run, err := x.Start(context.Background(), Command{Argv: []string{"/bin/true"}}, "attempt-slow", func(string) error { return nil }) if err != nil { t.Fatal(err) } <-run.Exited() tb.slow.Store(true) if run.Stop() { t.Fatal("the live stop proved it after its deadline") } if x.Verify(RecordID(0, "attempt-slow")) { t.Fatal("recovery proved it after its deadline") } turns := NewTurns("").Sweep(tb) turns.own, turns.Settle = &Own{}, 100*time.Millisecond if err := turns.saveTurn(turnRecord{ID: "old", Op: OpEngineering, Runner: selfID(), Sweep: true, Before: []ProcID{}, Markers: []string{"attempt-slow"}}); err != nil { t.Fatal(err) } if u, err := turns.Take(context.Background(), OpCheck); err == nil { u.Give() t.Fatal("an earlier turn was settled after the deadline") } } // #10: only a process that started after the first kill pass began can // be a relaunch. One with a system path and launchd as its parent that // started before it (here missing from that pass's read) is killed. func TestSweepRelaunchOnlyAfterTheKillPass(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) tb.after = func(tb *table, pid int, sig syscall.Signal) { if pid == 82500 || pid == -82500 { delete(tb.hidden, 82400) } } _, u, _, run := liveTurn(t, tb, &Own{}, time.Second) defer u.Give() tb.mark(&tb.hidden, 82400) tb.add(row(82400, 1, 82400, 82400, 5400, me, ""), "/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc") tb.add(row(82500, 1, 82500, 82500, 5500, me, ""), "/Users/paulequipo/w/first") if !run.Stop() { t.Fatalf("not proven: %v", tb.signalled()) } if sig := tb.signalled(); !slices.Contains(sig, 82400) && !slices.Contains(sig, -82400) || tb.has(82400) { t.Fatalf("a process from before the kill pass was taken for a relaunch: %v", tb.signalled()) } }