native-ipc 0.6.0

One safe API for least-authority native shared memory: sealed memfd on Linux, Mach memory entries on macOS, exact-rights sections on Windows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
//! Private Mach bootstrap channel with audit-token process authentication.

use std::ffi::{CStr, CString, c_char, c_int, c_void};
use std::fmt;
use std::mem::{size_of, size_of_val, zeroed};
#[cfg(test)]
use std::sync::atomic::AtomicU64;
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::sync::{Arc, Condvar, Mutex};
use std::time::Duration;

use super::{KERN_SUCCESS, MachPort, current_task, deallocate_port};
use crate::backend::reaper_ownership::{ReaperOwnership, ReaperTermination};
use crate::backend::{PeerState, SessionTransportError};
use crate::protocol::{
    CONTROL_FRAME_LEN, ManifestEntry, NativeRegionSpec, PeerAccess, TransferManifest,
    TransferProvenance, mint_channel_id,
};
use crate::session::{
    AbsoluteDeadline, ChildCleanupFacts, ChildExitStatus, DescendantCleanupStatus,
};
type MachMsgReturn = c_int;
type PosixSpawnAttr = *mut c_void;
type PosixSpawnFileActions = *mut c_void;

const MACH_PORT_NULL: MachPort = 0;
const MACH_PORT_RIGHT_RECEIVE: c_int = 1;
const MACH_PORT_RIGHT_SEND: c_int = 0;
const MACH_PORT_TYPE_SEND: u32 = 0x0001_0000;
const MACH_PORT_TYPE_DEAD_NAME: u32 = 0x0010_0000;
const MACH_MSG_TYPE_COPY_SEND: u8 = 19;
const MACH_MSG_TYPE_MAKE_SEND: u8 = 20;
const MACH_MSG_TYPE_PORT_SEND: u8 = 17;
const MACH_MSG_PORT_DESCRIPTOR: u8 = 0;
const MACH_MSGH_BITS_COMPLEX: u32 = 0x8000_0000;
const MACH_SEND_MSG: u32 = 0x0000_0001;
const MACH_RCV_MSG: u32 = 0x0000_0002;
const MACH_SEND_TIMEOUT: u32 = 0x0000_0010;
const MACH_RCV_TIMEOUT: u32 = 0x0000_0100;
const MACH_RCV_TRAILER_AUDIT: u32 = 3 << 24;
const MACH_RCV_TOO_LARGE: c_int = 0x1000_4004;
const MACH_SEND_TIMED_OUT: c_int = 0x1000_0004;
const MACH_RCV_TIMED_OUT: c_int = 0x1000_4003;
const MACH_SEND_INTERRUPTED: c_int = 0x1000_0007;
const MACH_RCV_INTERRUPTED: c_int = 0x1000_4005;
const TASK_BOOTSTRAP_PORT: c_int = 4;
const MESSAGE_ID: c_int = 0x4e49_5043;
const VNEXT_MESSAGE_ID: c_int = 0x4e49_5044;
const MESSAGE_MAGIC: [u8; 8] = *b"NIPCMACH";
const VNEXT_MESSAGE_MAGIC: [u8; 8] = *b"NIPCVNXT";
const CAPABILITY_MAGIC: [u8; 8] = *b"NIPCCAP1";
const READY_MAGIC: [u8; 8] = *b"NIPCRDY1";
const COMMIT_MAGIC: [u8; 8] = *b"NIPCCMT1";
const ENV_NONCE: &str = "NATIVE_IPC_MACH_NONCE";
const ENV_PARENT_PID: &str = "NATIVE_IPC_PARENT_PID";
const TIMEOUT_MS: u32 = 10_000;
pub(super) const MAX_VNEXT_RECORD_BYTES: usize = 64 * 1024;
const MAX_VNEXT_CAPABILITIES: usize = 16;
const WNOHANG: c_int = 1;
const WUNTRACED: c_int = 2;
const WEXITED: c_int = 0x0000_0004;
const WNOWAIT: c_int = 0x0000_0020;
const P_PID: c_int = 1;
// Transient-glitch tolerance for the pinned-witness re-observation. Kernel
// artifacts of back-to-back nonblocking queries last microseconds, so a few
// retried milliseconds cover them; the bound keeps a persistent refusal from
// stalling cleanup, which then honestly reports the unverified status.
const GROUP_ATTEMPT_LIMIT: usize = 50;
const EPERM: c_int = 1;
const ESRCH: c_int = 3;
const SIGSTOP: c_int = 17;
const SIGCONT: c_int = 19;
const PT_TRACE_ME: c_int = 0;
const PT_CONTINUE: c_int = 7;
const PT_KILL: c_int = 8;
const RLIMIT_NPROC: c_int = 7;
const TASK_AUDIT_TOKEN: c_int = 15;
const TASK_AUDIT_TOKEN_COUNT: u32 = 8;
const POSIX_SPAWN_START_SUSPENDED: i16 = 0x0080;
const POSIX_SPAWN_SETSID: i16 = 0x0400;
const POSIX_SPAWN_CLOEXEC_DEFAULT: i16 = 0x4000;
const PROC_PIDPATHINFO_MAXSIZE: usize = 4096;

unsafe extern "C" {
    fn mach_port_allocate(task: MachPort, right: c_int, name: *mut MachPort) -> c_int;
    fn mach_port_insert_right(
        task: MachPort,
        name: MachPort,
        poly: MachPort,
        poly_poly: c_int,
    ) -> c_int;
    fn mach_port_mod_refs(task: MachPort, name: MachPort, right: c_int, delta: c_int) -> c_int;
    fn mach_port_type(task: MachPort, name: MachPort, port_type: *mut u32) -> c_int;
    fn mach_msg(
        message: *mut MachMsgHeader,
        option: u32,
        send_size: u32,
        receive_limit: u32,
        receive_name: MachPort,
        timeout: u32,
        notify: MachPort,
    ) -> MachMsgReturn;
    fn mach_msg_destroy(message: *mut MachMsgHeader);
    fn proc_signal_with_audittoken(token: *mut AuditToken, signal: c_int) -> c_int;
    fn getppid() -> Pid;
    fn ptrace(request: c_int, pid: Pid, address: *mut c_void, data: c_int) -> c_int;
    fn raise(signal: c_int) -> c_int;
    fn setrlimit(resource: c_int, limit: *const ResourceLimit) -> c_int;
    fn task_name_for_pid(task: MachPort, pid: Pid, name: *mut MachPort) -> c_int;
    fn task_info(task: MachPort, flavor: c_int, information: *mut c_int, count: *mut u32) -> c_int;
    fn task_get_special_port(task: MachPort, which: c_int, port: *mut MachPort) -> c_int;
    fn task_set_special_port(task: MachPort, which: c_int, port: MachPort) -> c_int;
    fn posix_spawnattr_init(attributes: *mut PosixSpawnAttr) -> c_int;
    fn posix_spawnattr_destroy(attributes: *mut PosixSpawnAttr) -> c_int;
    fn posix_spawnattr_setspecialport_np(
        attributes: *mut PosixSpawnAttr,
        port: MachPort,
        which: c_int,
    ) -> c_int;
    fn posix_spawnattr_setflags(attributes: *mut PosixSpawnAttr, flags: i16) -> c_int;
    fn posix_spawn(
        pid: *mut Pid,
        path: *const c_char,
        file_actions: *const PosixSpawnFileActions,
        attributes: *const PosixSpawnAttr,
        argv: *const *mut c_char,
        envp: *const *mut c_char,
    ) -> c_int;
    fn kill(pid: Pid, signal: c_int) -> c_int;
    fn waitpid(pid: Pid, status: *mut c_int, options: c_int) -> Pid;
    fn waitid(idtype: c_int, id: u32, information: *mut DarwinSigInfo, options: c_int) -> c_int;
    fn killpg(process_group: Pid, signal: c_int) -> c_int;
    fn getpgid(pid: Pid) -> Pid;
    fn getsid(pid: Pid) -> Pid;
}

/// Darwin `siginfo_t` transcribed from the macOS SDK's `sys/signal.h`.
#[repr(C)]
struct DarwinSigInfo {
    si_signo: c_int,
    si_errno: c_int,
    si_code: c_int,
    si_pid: Pid,
    si_uid: u32,
    si_status: c_int,
    si_addr: *mut c_void,
    si_value: *mut c_void,
    si_band: isize,
    __pad: [u64; 7],
}

#[link(name = "proc")]
unsafe extern "C" {
    fn proc_pidpath(pid: c_int, buffer: *mut c_void, buffer_size: u32) -> c_int;
}

#[link(name = "bsm")]
unsafe extern "C" {
    fn audit_token_to_pid(token: AuditToken) -> Pid;
}

type Pid = c_int;

#[repr(C)]
#[derive(Clone, Copy)]
struct MachMsgHeader {
    bits: u32,
    size: u32,
    remote_port: MachPort,
    local_port: MachPort,
    voucher_port: MachPort,
    id: c_int,
}

#[repr(C)]
#[derive(Clone, Copy)]
struct MachMsgBody {
    descriptor_count: u32,
}

#[repr(C)]
#[derive(Clone, Copy)]
struct MachMsgPortDescriptor {
    name: MachPort,
    pad1: u32,
    pad2: u16,
    disposition: u8,
    descriptor_type: u8,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct AuditToken {
    values: [u32; 8],
}

#[repr(C)]
struct ResourceLimit {
    current: u64,
    maximum: u64,
}

#[repr(C)]
#[derive(Clone, Copy)]
struct AuditTrailer {
    trailer_type: u32,
    trailer_size: u32,
    sequence: u32,
    sender_security: [u32; 2],
    audit: AuditToken,
}

#[repr(C)]
#[derive(Clone, Copy)]
struct VnextEnvelope {
    magic: [u8; 8],
    nonce: [u8; 32],
    kind: u32,
    payload_len: u32,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum VnextRecordKind {
    ZeroRights = 1,
    Capabilities = 2,
}

pub(super) struct VnextCapabilityRecord {
    pub(super) bytes: Vec<u8>,
    pub(super) rights: Vec<SendRight>,
}

#[repr(C)]
struct PortMessage {
    header: MachMsgHeader,
    body: MachMsgBody,
    descriptor: MachMsgPortDescriptor,
    magic: [u8; 8],
    nonce: [u8; 32],
    transcript: [u8; CONTROL_FRAME_LEN],
}

#[repr(C)]
struct ReceiveBuffer {
    message: PortMessage,
    trailer: AuditTrailer,
}

/// Mach bootstrap or authenticated port-transfer failure.
#[derive(Debug)]
pub enum BootstrapError {
    /// A bounded Mach operation failed.
    Mach {
        /// Bounded Mach operation.
        operation: &'static str,
        /// Kernel return code.
        code: c_int,
    },
    /// `posix_spawn` setup or launch failed.
    Spawn(c_int),
    /// Received message shape, nonce, or descriptor was noncanonical.
    InvalidMessage,
    /// Kernel audit trailer identified another process.
    WrongPeer {
        /// Held spawned or parent PID.
        expected: u32,
        /// PID from the kernel audit trailer.
        actual: u32,
    },
    /// Spawn environment was present but malformed.
    InvalidEnvironment,
    /// The bootstrap designation is absent: this process was not spawned as
    /// a receiver, so no peer exists and nothing was negotiated.
    MissingEnvironment,
    /// The caller-derived absolute deadline expired.
    DeadlineExpired,
    /// A send completed at the deadline boundary with ambiguous peer state.
    Ambiguous,
    /// Exact child authority could not be retained, so no numeric signal was sent.
    ExactAuthorityUnavailable {
        /// Native capture error when one was available.
        native_error: Option<c_int>,
    },
}

impl fmt::Display for BootstrapError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(formatter, "Mach bootstrap failed: {self:?}")
    }
}
impl std::error::Error for BootstrapError {}

/// Received send right owned by this process.
pub struct SendRight(MachPort);
impl SendRight {
    /// Raw port name for native mapping APIs inside this crate.
    pub(super) const fn name(&self) -> MachPort {
        self.0
    }

    #[cfg(test)]
    pub(super) fn copy_existing(name: MachPort) -> Result<Self, BootstrapError> {
        if name == MACH_PORT_NULL {
            return Err(BootstrapError::InvalidMessage);
        }
        // SAFETY: the caller supplies a live send-right name in the current
        // task; incrementing its user-reference count creates one owned copy.
        mach("mach_port_mod_refs(send,+1)", unsafe {
            mach_port_mod_refs(current_task(), name, MACH_PORT_RIGHT_SEND, 1)
        })?;
        Ok(Self(name))
    }
}
impl Drop for SendRight {
    fn drop(&mut self) {
        deallocate_port(current_task(), self.0);
        #[cfg(test)]
        super::observe_vnext_drop_for_test("send-right");
    }
}

#[cfg(test)]
pub(super) struct TestSendRight {
    _receive: ReceiveRight,
    send: SendRight,
}

#[cfg(test)]
impl TestSendRight {
    pub(super) fn allocate() -> Result<Self, BootstrapError> {
        let receive = ReceiveRight::allocate()?;
        receive.make_send()?;
        let send = SendRight(receive.0);
        Ok(Self {
            _receive: receive,
            send,
        })
    }

    pub(super) const fn name(&self) -> MachPort {
        self.send.0
    }
}

struct ReceiveRight(MachPort);
impl ReceiveRight {
    fn allocate() -> Result<Self, BootstrapError> {
        let mut name = MACH_PORT_NULL;
        // SAFETY: output pointer is valid for the current task.
        let result =
            unsafe { mach_port_allocate(current_task(), MACH_PORT_RIGHT_RECEIVE, &mut name) };
        mach("mach_port_allocate", result)?;
        if name == MACH_PORT_NULL {
            return Err(BootstrapError::InvalidMessage);
        }
        Ok(Self(name))
    }
    fn make_send(&self) -> Result<(), BootstrapError> {
        // SAFETY: this object owns the receive right from which MAKE_SEND is valid.
        mach("mach_port_insert_right", unsafe {
            mach_port_insert_right(
                current_task(),
                self.0,
                self.0,
                MACH_MSG_TYPE_MAKE_SEND.into(),
            )
        })
    }
}
impl Drop for ReceiveRight {
    fn drop(&mut self) {
        // SAFETY: this object uniquely owns one receive-right reference.
        let _ = unsafe { mach_port_mod_refs(current_task(), self.0, MACH_PORT_RIGHT_RECEIVE, -1) };
    }
}

/// Low-privilege kernel identity for one exact Mach task.
///
/// Unlike a task-control port this right cannot suspend, mutate, or terminate
/// the task. It lets the suspended-spawn path obtain an execution-scoped audit
/// token before the child runs. Native testing shows that an ordinary `exec`
/// invalidates this right, so it is not a cross-exec lifecycle capability.
struct TaskNameRight(MachPort);

/// Kernel audit identity sampled while an exact direct child is ptrace-stopped.
pub(super) struct TaskAuditIdentity {
    audit: AuditToken,
    executable: Vec<u8>,
}

impl TaskAuditIdentity {
    /// Exact native audit token bytes for a clean-exec Security guest lookup.
    ///
    /// The caller must continue to pin the corresponding stopped execution;
    /// these bytes are identity evidence, never lifecycle authority.
    pub(super) fn audit_identity(&self) -> [u8; 32] {
        let mut encoded = [0_u8; 32];
        for (destination, value) in encoded.chunks_exact_mut(4).zip(self.audit.values) {
            destination.copy_from_slice(&value.to_ne_bytes());
        }
        encoded
    }

    /// Requires one exact PID, executable, and complete real/effective
    /// credential tuple while the caller pins the stopped process.
    pub(super) fn proves_exact_process_image(
        &self,
        pid: c_int,
        expected_ruid: u32,
        expected_euid: u32,
        expected_rgid: u32,
        expected_egid: u32,
        expected_executable: &[u8],
    ) -> bool {
        let Ok(expected_pid) = u32::try_from(pid) else {
            return false;
        };
        self.audit.values[5] == expected_pid
            && self.audit.values[1] == expected_euid
            && self.audit.values[2] == expected_egid
            && self.audit.values[3] == expected_ruid
            && self.audit.values[4] == expected_rgid
            && self.executable == expected_executable
    }

    /// Requires the same exact PID, the expected post-drop credentials, and a
    /// changed PID version. Darwin changes the audit PID version on `exec`.
    pub(super) fn proves_exec_transition_from(
        &self,
        before: &Self,
        pid: c_int,
        expected_euid: u32,
        expected_egid: u32,
        expected_executable: &[u8],
    ) -> bool {
        let Ok(expected_pid) = u32::try_from(pid) else {
            return false;
        };
        before.audit.values[5] == expected_pid
            && self.audit.values[5] == expected_pid
            && before.audit.values[7] != self.audit.values[7]
            && self.audit.values[1] == expected_euid
            && self.audit.values[2] == expected_egid
            && self.audit.values[3] == expected_euid
            && self.audit.values[4] == expected_egid
            && self.executable == expected_executable
    }
}

/// Captures only an execution-scoped task-name identity, never task control.
/// The caller must independently pin `pid` against reuse while this runs.
pub(super) fn capture_task_audit_identity(pid: c_int) -> Result<TaskAuditIdentity, BootstrapError> {
    let (_right, audit) = TaskNameRight::capture(pid)?;
    let mut path = [0_u8; PROC_PIDPATHINFO_MAXSIZE];
    // SAFETY: exact stopped-child authority pins pid while proc_pidpath writes
    // at most the supplied live buffer. libproc returns a NUL-terminated path.
    let result = unsafe {
        proc_pidpath(
            pid,
            path.as_mut_ptr().cast(),
            u32::try_from(path.len()).unwrap_or(u32::MAX),
        )
    };
    if result <= 0 {
        return Err(BootstrapError::InvalidMessage);
    }
    let executable = CStr::from_bytes_until_nul(&path)
        .map_err(|_| BootstrapError::InvalidMessage)?
        .to_bytes()
        .to_vec();
    if executable.is_empty() {
        return Err(BootstrapError::InvalidMessage);
    }
    Ok(TaskAuditIdentity { audit, executable })
}

impl TaskNameRight {
    fn capture_suspended(pid: Pid) -> Result<(Self, AuditToken), BootstrapError> {
        Self::capture(pid)
    }

    fn capture(pid: Pid) -> Result<(Self, AuditToken), BootstrapError> {
        let mut name = MACH_PORT_NULL;
        // SAFETY: the output pointer is valid. Callers that require a proof
        // against PID reuse must independently establish that the process
        // cannot exit during this numeric lookup; the production spawn path
        // does so by keeping the fresh child suspended.
        mach("task_name_for_pid", unsafe {
            task_name_for_pid(current_task(), pid, &mut name)
        })?;
        if name == MACH_PORT_NULL {
            return Err(BootstrapError::InvalidMessage);
        }
        let right = Self(name);
        let audit = right.audit_token()?;
        // SAFETY: `audit` came from TASK_AUDIT_TOKEN for this exact task.
        let actual = unsafe { audit_token_to_pid(audit) };
        if actual != pid {
            return Err(BootstrapError::WrongPeer {
                expected: pid as u32,
                actual: actual as u32,
            });
        }
        Ok((right, audit))
    }

    fn audit_token(&self) -> Result<AuditToken, BootstrapError> {
        let mut audit = AuditToken { values: [0; 8] };
        let mut count = TASK_AUDIT_TOKEN_COUNT;
        // SAFETY: TASK_AUDIT_TOKEN writes exactly `count` natural words into
        // the aligned audit-token storage, and this object owns a live task-
        // name send right accepted by `task_info` for this flavor.
        mach("task_info(TASK_AUDIT_TOKEN)", unsafe {
            task_info(
                self.0,
                TASK_AUDIT_TOKEN,
                audit.values.as_mut_ptr().cast(),
                &mut count,
            )
        })?;
        if count != TASK_AUDIT_TOKEN_COUNT {
            return Err(BootstrapError::InvalidMessage);
        }
        Ok(audit)
    }
}

impl Drop for TaskNameRight {
    fn drop(&mut self) {
        deallocate_port(current_task(), self.0);
    }
}

/// Parent-owned exact helper and authenticated bidirectional Mach channel.
pub struct SpawnedHelper {
    pid: Pid,
    nonce: [u8; 32],
    receive: Option<ReceiveRight>,
    lifecycle: Option<MacChildLifecycle>,
}

impl SpawnedHelper {
    /// Spawns an absolute helper path with a private bootstrap send right.
    pub fn spawn(path: &CString, arguments: &[CString]) -> Result<Self, BootstrapError> {
        let environment = std::env::vars_os()
            .filter(|(key, _)| key != ENV_NONCE && key != ENV_PARENT_PID)
            .filter_map(|(key, value)| {
                CString::new(format!(
                    "{}={}",
                    key.to_string_lossy(),
                    value.to_string_lossy()
                ))
                .ok()
            })
            .collect();
        Self::spawn_inner(path, arguments, environment, false, false)
    }

    pub(super) fn spawn_explicit(
        path: &CString,
        arguments: &[CString],
        environment: &[CString],
    ) -> Result<Self, BootstrapError> {
        Self::spawn_inner(path, arguments, environment.to_vec(), true, true)
    }

    fn spawn_inner(
        path: &CString,
        arguments: &[CString],
        mut environment: Vec<CString>,
        fresh_session: bool,
        arguments_include_arg0: bool,
    ) -> Result<Self, BootstrapError> {
        let nonce = random_nonce()?;
        let receive = ReceiveRight::allocate()?;
        receive.make_send()?;
        let mut attributes: PosixSpawnAttr = std::ptr::null_mut();
        // SAFETY: attribute output pointer is valid.
        spawn_result(unsafe { posix_spawnattr_init(&mut attributes) })?;
        struct AttributeGuard(PosixSpawnAttr);
        impl Drop for AttributeGuard {
            fn drop(&mut self) {
                // SAFETY: initialized posix_spawn attributes are destroyed once.
                let _ = unsafe { posix_spawnattr_destroy(&mut self.0) };
            }
        }
        let mut guard = AttributeGuard(attributes);
        // SAFETY: attributes are initialized and receive port has a live send right.
        spawn_result(unsafe {
            posix_spawnattr_setspecialport_np(&mut guard.0, receive.0, TASK_BOOTSTRAP_PORT)
        })?;
        if fresh_session {
            // SAFETY: attributes are initialized and the flag is defined by
            // the macOS SDK to create a fresh session for the spawned child.
            spawn_result(unsafe {
                posix_spawnattr_setflags(
                    &mut guard.0,
                    POSIX_SPAWN_START_SUSPENDED | POSIX_SPAWN_SETSID | POSIX_SPAWN_CLOEXEC_DEFAULT,
                )
            })?;
        }

        let mut argv_storage =
            Vec::with_capacity(arguments.len() + usize::from(!arguments_include_arg0));
        if !arguments_include_arg0 {
            argv_storage.push(path.clone());
        }
        argv_storage.extend(arguments.iter().cloned());
        let mut argv: Vec<*mut c_char> = argv_storage
            .iter_mut()
            .map(|argument| argument.as_ptr().cast_mut())
            .collect();
        argv.push(std::ptr::null_mut());

        let nonce_value = hex(&nonce);
        let parent_pid = std::process::id().to_string();
        environment.push(CString::new(format!("{ENV_NONCE}={nonce_value}")).expect("hex env"));
        environment.push(CString::new(format!("{ENV_PARENT_PID}={parent_pid}")).expect("pid env"));
        let mut envp: Vec<*mut c_char> = environment
            .iter_mut()
            .map(|entry| entry.as_ptr().cast_mut())
            .collect();
        envp.push(std::ptr::null_mut());
        let lifecycle = fresh_session
            .then(MacChildLifecycle::prepare)
            .transpose()
            .map_err(bootstrap_lifecycle_error)?;
        let mut pid = 0;
        // SAFETY: path/argv/envp and initialized attributes remain live for the call.
        let result = unsafe {
            posix_spawn(
                &mut pid,
                path.as_ptr(),
                std::ptr::null(),
                &guard.0,
                argv.as_ptr(),
                envp.as_ptr(),
            )
        };
        // Drop the parent's extra send reference on every outcome before the
        // launch result is inspected; the receive right remains owned.
        deallocate_port(current_task(), receive.0);
        spawn_result(result)?;
        if let Some(owner) = &lifecycle {
            let (task_name, mut audit_token) = match TaskNameRight::capture_suspended(pid) {
                Ok(identity) => identity,
                Err(error) => {
                    // A hostile process-global SIGCHLD policy or broad waiter
                    // can consume an externally killed child and release its
                    // PID before this branch runs. Once exact task authority
                    // acquisition fails, never fall back to a numeric signal.
                    // Perform no PID-addressed action at all: after auto-reap,
                    // even waitpid could consume a different concurrently
                    // spawned direct child that reused the numeric PID. An
                    // unobservable suspended child therefore remains an
                    // explicit incomplete-cleanup failure of this private
                    // prototype rather than a risk to another child.
                    owner.request_termination();
                    return Err(BootstrapError::ExactAuthorityUnavailable {
                        native_error: bootstrap_native_error(&error),
                    });
                }
            };
            owner.install_task_identity(task_name, audit_token);
            owner.activate(pid);
            owner.verify_fresh_group_while_suspended(pid);
            // Resume the exact captured execution rather than addressing the
            // reusable PID with a numeric SIGCONT.
            if let Err(error) = signal_with_audit_token(&mut audit_token, SIGCONT) {
                owner.request_termination();
                return Err(BootstrapError::Spawn(error));
            }
        }
        Ok(Self {
            pid,
            nonce,
            receive: Some(receive),
            lifecycle,
        })
    }

    /// Receives the helper's control port and authenticates its audit PID.
    pub fn authenticate(self) -> Result<ParentChannel, BootstrapError> {
        self.authenticate_inner(None)
    }

    pub(super) fn authenticate_vnext_until(
        mut self,
        deadline: AbsoluteDeadline,
    ) -> Result<(ParentChannel, MacChildLifecycle), (BootstrapError, ChildCleanupFacts)> {
        let Some(receive) = self.receive.take() else {
            let cleanup = self.cleanup_vnext_until(deadline);
            return Err((BootstrapError::InvalidMessage, cleanup));
        };
        let Some(lifecycle) = self.lifecycle.take() else {
            drop(receive);
            let cleanup = self.cleanup_vnext_until(deadline);
            return Err((BootstrapError::InvalidMessage, cleanup));
        };
        let peer_pid = self.pid as u32;
        self.pid = 0;
        let (child_send, child_audit) = match receive_port_with_audit(
            &receive,
            &self.nonce,
            peer_pid,
            &[0; CONTROL_FRAME_LEN],
            Some(deadline),
        ) {
            Ok(received) => received,
            Err(error) => {
                drop(receive);
                return Err((error, lifecycle.terminate_and_reap_facts(deadline)));
            }
        };
        if let Err(error) = lifecycle.install_authenticated_audit_token(child_audit) {
            drop(child_send);
            drop(receive);
            return Err((
                bootstrap_lifecycle_error(error),
                lifecycle.terminate_and_reap_facts(deadline),
            ));
        }
        let channel = ParentChannel {
            peer_send: child_send,
            _receive: receive,
            nonce: self.nonce,
            peer_pid,
            peer_audit: Some(child_audit),
            reaped: true,
            pending_entries: Vec::new(),
            channel_id: mint_channel_id(),
            next_transfer_id: 1,
            poisoned: false,
        };
        Ok((channel, lifecycle))
    }

    pub(super) fn cleanup_vnext_until(mut self, deadline: AbsoluteDeadline) -> ChildCleanupFacts {
        self.receive.take();
        self.pid = 0;
        self.lifecycle.take().map_or_else(
            || ChildCleanupFacts::new(None, DescendantCleanupStatus::FreshGroupUnverified, None),
            |lifecycle| lifecycle.terminate_and_reap_facts(deadline),
        )
    }

    fn authenticate_inner(
        mut self,
        deadline: Option<AbsoluteDeadline>,
    ) -> Result<ParentChannel, BootstrapError> {
        let receive = self.receive.take().ok_or(BootstrapError::InvalidMessage)?;
        let child_send = match receive_port(
            &receive,
            &self.nonce,
            self.pid as u32,
            &[0; CONTROL_FRAME_LEN],
            deadline,
        ) {
            Ok(right) => right,
            Err(error) => {
                terminate_and_reap(self.pid);
                self.pid = 0;
                return Err(error);
            }
        };
        let channel = ParentChannel {
            peer_send: child_send,
            _receive: receive,
            nonce: self.nonce,
            peer_pid: self.pid as u32,
            peer_audit: None,
            reaped: false,
            pending_entries: Vec::new(),
            channel_id: mint_channel_id(),
            next_transfer_id: 1,
            poisoned: false,
        };
        self.pid = 0;
        Ok(channel)
    }

    /// Spawned process ID held unreaped by the caller's lifecycle policy.
    pub const fn pid(&self) -> u32 {
        self.pid as u32
    }

    /// Audit-token words captured while the fresh child was still suspended,
    /// pinning the exact pre-resume execution for kernel identity queries.
    pub(super) fn suspended_audit_token_values(&self) -> Option<[u32; 8]> {
        self.lifecycle
            .as_ref()
            .and_then(MacChildLifecycle::audit_token_values)
    }
}

impl Drop for SpawnedHelper {
    fn drop(&mut self) {
        if self.lifecycle.is_none() && self.pid > 0 {
            terminate_and_reap(self.pid);
        }
    }
}

impl Drop for ParentChannel {
    fn drop(&mut self) {
        if !self.reaped {
            terminate_and_reap(self.peer_pid as Pid);
        }
    }
}

/// Parent side of an authenticated bidirectional port-transfer channel.
pub struct ParentChannel {
    peer_send: SendRight,
    _receive: ReceiveRight,
    nonce: [u8; 32],
    peer_pid: u32,
    peer_audit: Option<AuditToken>,
    reaped: bool,
    pending_entries: Vec<ManifestEntry>,
    channel_id: u64,
    next_transfer_id: u64,
    poisoned: bool,
}

struct MacChildLifecycleState {
    reaped: bool,
    last_error: Option<i32>,
    exit_status: Option<i32>,
    task_name: Option<TaskNameRight>,
    audit_token: Option<AuditToken>,
    descendants: DescendantCleanupStatus,
}

struct MacChildLifecycleShared {
    pid: AtomicI32,
    termination: ReaperTermination,
    traced: AtomicBool,
    fresh_group_verified: AtomicBool,
    reaper_gate: Mutex<()>,
    state: Mutex<MacChildLifecycleState>,
    changed: Condvar,
    #[cfg(test)]
    reap_delay_ms: AtomicU64,
    #[cfg(test)]
    wait_interrupts: AtomicU64,
}

/// Durable exact-child owner whose destructor never waits on the caller.
pub(super) struct MacChildLifecycle {
    shared: Arc<MacChildLifecycleShared>,
    ownership: ReaperOwnership,
}

impl MacChildLifecycle {
    fn prepare() -> Result<Self, SessionTransportError> {
        let ownership = ReaperOwnership::new();
        let termination = ownership.termination();
        let shared = Arc::new(MacChildLifecycleShared {
            pid: AtomicI32::new(0),
            termination,
            traced: AtomicBool::new(false),
            fresh_group_verified: AtomicBool::new(false),
            reaper_gate: Mutex::new(()),
            state: Mutex::new(MacChildLifecycleState {
                reaped: false,
                last_error: None,
                exit_status: None,
                task_name: None,
                audit_token: None,
                descendants: DescendantCleanupStatus::FreshGroupUnverified,
            }),
            changed: Condvar::new(),
            #[cfg(test)]
            reap_delay_ms: AtomicU64::new(0),
            #[cfg(test)]
            wait_interrupts: AtomicU64::new(0),
        });
        let worker_shared = Arc::clone(&shared);
        std::thread::Builder::new()
            .name("native-ipc-macos-child-reaper".into())
            .spawn(move || mac_child_reaper(worker_shared))
            .map_err(|error| SessionTransportError::Native(error.raw_os_error()))?;
        Ok(Self { shared, ownership })
    }

    fn start(pid: Pid) -> Result<Self, SessionTransportError> {
        let lifecycle = Self::prepare()?;
        lifecycle.activate(pid);
        Ok(lifecycle)
    }

    fn activate(&self, pid: Pid) {
        debug_assert!(pid > 0);
        let previous = self.shared.pid.swap(pid, Ordering::AcqRel);
        debug_assert_eq!(previous, 0);
        self.shared.changed.notify_all();
    }

    pub(super) fn pid(&self) -> u32 {
        self.shared.pid.load(Ordering::Acquire) as u32
    }

    fn install_task_identity(&self, task_name: TaskNameRight, audit_token: AuditToken) {
        let mut state = lock_lifecycle(&self.shared.state);
        debug_assert!(state.task_name.is_none());
        debug_assert!(state.audit_token.is_none());
        state.task_name = Some(task_name);
        state.audit_token = Some(audit_token);
        self.shared.changed.notify_all();
    }

    fn install_authenticated_audit_token(
        &self,
        audit_token: AuditToken,
    ) -> Result<(), SessionTransportError> {
        let mut state = lock_lifecycle(&self.shared.state);
        if let Some(task_name) = &state.task_name {
            let current = task_name
                .audit_token()
                .map_err(bootstrap_lifecycle_transport_error)?;
            if current != audit_token {
                return Err(SessionTransportError::IdentityMismatch);
            }
        }
        state.audit_token = Some(audit_token);
        self.shared.changed.notify_all();
        Ok(())
    }

    fn mark_traced(&self) {
        self.shared.traced.store(true, Ordering::Release);
        self.shared.changed.notify_all();
    }

    pub(super) fn audit_token_values(&self) -> Option<[u32; 8]> {
        lock_lifecycle(&self.shared.state)
            .audit_token
            .map(|token| token.values)
    }

    /// Kernel-verifies the fresh session while the suspended child cannot run.
    ///
    /// Session leadership is irrevocable — a leader can never change its own
    /// process group or session — so this one pre-resume observation remains
    /// true for the child's whole lifetime, including its unreaped zombie,
    /// even though Darwin refuses these queries once the process is a zombie.
    fn verify_fresh_group_while_suspended(&self, pid: Pid) {
        // SAFETY: scalar identity queries about the suspended pinned child.
        if unsafe { getpgid(pid) == pid && getsid(pid) == pid } {
            self.shared
                .fresh_group_verified
                .store(true, Ordering::Release);
        }
    }

    fn pause_reaping(&self) -> MacReapingPause<'_> {
        MacReapingPause {
            _guard: match self.shared.reaper_gate.lock() {
                Ok(guard) => guard,
                Err(poisoned) => poisoned.into_inner(),
            },
        }
    }

    #[cfg(test)]
    fn current_task_audit_token_for_test(&self) -> Result<AuditToken, BootstrapError> {
        let state = lock_lifecycle(&self.shared.state);
        state
            .task_name
            .as_ref()
            .ok_or(BootstrapError::InvalidMessage)?
            .audit_token()
    }

    pub(super) fn try_poll(&self) -> Result<PeerState, SessionTransportError> {
        let state = lock_lifecycle(&self.shared.state);
        if state.reaped {
            Ok(PeerState::ExitedUnknown)
        } else if let Some(error) = state.last_error {
            Err(SessionTransportError::Native(Some(error)))
        } else {
            Ok(PeerState::Running)
        }
    }

    pub(super) fn exited_successfully_for_test(&self) -> bool {
        lock_lifecycle(&self.shared.state).exit_status == Some(0)
    }

    pub(super) fn wait_and_reap_status(
        &self,
        deadline: AbsoluteDeadline,
    ) -> Result<i32, SessionTransportError> {
        self.wait_for_status(deadline, false)
    }

    pub(super) fn wait_and_reap_facts(&self, deadline: AbsoluteDeadline) -> ChildCleanupFacts {
        let status = self.wait_and_reap_status(deadline);
        mac_child_cleanup_facts(status, self.descendant_status())
    }

    fn descendant_status(&self) -> DescendantCleanupStatus {
        lock_lifecycle(&self.shared.state).descendants
    }

    pub(super) fn terminate_and_reap_status(
        &self,
        deadline: AbsoluteDeadline,
    ) -> Result<i32, SessionTransportError> {
        self.wait_for_status(deadline, true)
    }

    pub(super) fn terminate_and_reap_facts(&self, deadline: AbsoluteDeadline) -> ChildCleanupFacts {
        let status = self.terminate_and_reap_status(deadline);
        mac_child_cleanup_facts(status, self.descendant_status())
    }

    pub(super) fn terminate_and_reap(
        &self,
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        self.terminate_and_reap_status(deadline).map(|_| ())
    }

    fn wait_for_status(
        &self,
        deadline: AbsoluteDeadline,
        terminate: bool,
    ) -> Result<i32, SessionTransportError> {
        if terminate {
            self.request_termination();
        }
        let mut state = lock_lifecycle(&self.shared.state);
        loop {
            if state.reaped {
                return state.exit_status.ok_or(SessionTransportError::Native(None));
            }
            if let Some(error) = state.last_error
                && error != ESRCH
            {
                return Err(SessionTransportError::Native(Some(error)));
            }
            let remaining = deadline.remaining();
            if remaining.is_zero() {
                return Err(match state.last_error {
                    Some(error) => SessionTransportError::Native(Some(error)),
                    None => SessionTransportError::DeadlineExpired,
                });
            }
            state = match self.shared.changed.wait_timeout(state, remaining) {
                Ok((state, _)) => state,
                Err(poisoned) => poisoned.into_inner().0,
            };
        }
    }

    fn request_termination(&self) {
        self.shared.termination.request();
        self.shared.changed.notify_all();
    }

    #[cfg(test)]
    pub(super) fn delay_reap_for_test(&self, milliseconds: u64) {
        self.shared
            .reap_delay_ms
            .store(milliseconds, Ordering::Release);
    }

    #[cfg(test)]
    pub(super) fn interrupt_wait_for_test(&self, count: u64) {
        self.shared.wait_interrupts.store(count, Ordering::Release);
    }
}

struct MacReapingPause<'a> {
    _guard: std::sync::MutexGuard<'a, ()>,
}

fn mac_child_cleanup_facts(
    result: Result<i32, SessionTransportError>,
    descendants: DescendantCleanupStatus,
) -> ChildCleanupFacts {
    match result {
        Ok(status) if status & 0x7f == 0 => ChildCleanupFacts::new(
            Some(ChildExitStatus::Exited((status >> 8) & 0xff)),
            descendants,
            None,
        ),
        Ok(status) if status & 0x7f != 0x7f => ChildCleanupFacts::new(
            Some(ChildExitStatus::Signaled {
                signal: status & 0x7f,
                dumped_core: status & 0x80 != 0,
            }),
            descendants,
            None,
        ),
        Ok(_) => ChildCleanupFacts::new(None, descendants, None),
        Err(SessionTransportError::Native(code)) => ChildCleanupFacts::new(None, descendants, code),
        Err(
            SessionTransportError::DeadlineExpired
            | SessionTransportError::PeerExited
            | SessionTransportError::MalformedRecord
            | SessionTransportError::RecordTooLarge
            | SessionTransportError::IdentityMismatch
            | SessionTransportError::Ambiguous
            | SessionTransportError::Poisoned,
        ) => ChildCleanupFacts::new(None, descendants, None),
    }
}

impl Drop for MacChildLifecycle {
    fn drop(&mut self) {
        // The worker retains one Arc until exact wait completion. Request
        // cancellation/cleanup when this is the final external owner.
        if self.ownership.release() {
            self.shared.changed.notify_all();
        }
    }
}

impl Clone for MacChildLifecycle {
    fn clone(&self) -> Self {
        Self {
            shared: Arc::clone(&self.shared),
            ownership: self.ownership.clone(),
        }
    }
}

impl ParentChannel {
    pub(super) const fn vnext_nonce(&self) -> [u8; 32] {
        self.nonce
    }

    /// Authenticated peer audit-token words for exact kernel identity queries.
    pub(super) fn peer_audit_values(&self) -> Option<[u32; 8]> {
        self.peer_audit.map(|token| token.values)
    }

    pub(super) fn send_vnext_zero_rights(
        &mut self,
        bytes: &[u8],
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        send_vnext_message(
            self.peer_send.0,
            &self.nonce,
            VnextRecordKind::ZeroRights,
            bytes,
            &[],
            deadline,
        )
    }

    pub(super) fn receive_vnext_zero_rights(
        &mut self,
        maximum: usize,
        deadline: AbsoluteDeadline,
    ) -> Result<Vec<u8>, SessionTransportError> {
        let record = receive_vnext_message(
            &self._receive,
            &self.nonce,
            self.peer_pid,
            self.peer_audit.as_ref(),
            maximum,
            deadline,
        )?;
        if record.kind != VnextRecordKind::ZeroRights || !record.rights.is_empty() {
            return Err(SessionTransportError::MalformedRecord);
        }
        Ok(record.bytes)
    }

    pub(super) fn send_vnext_capabilities(
        &mut self,
        bytes: &[u8],
        rights: &[MachPort],
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        send_vnext_message(
            self.peer_send.0,
            &self.nonce,
            VnextRecordKind::Capabilities,
            bytes,
            rights,
            deadline,
        )
    }

    #[cfg(test)]
    pub(super) fn send_vnext_zero_with_rights_for_test(
        &mut self,
        bytes: &[u8],
        rights: &[MachPort],
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        send_vnext_message_inner(
            self.peer_send.0,
            &self.nonce,
            VnextRecordKind::ZeroRights,
            bytes,
            rights,
            deadline,
            true,
        )
    }

    pub(super) fn receive_vnext_capabilities(
        &mut self,
        maximum: usize,
        deadline: AbsoluteDeadline,
    ) -> Result<VnextCapabilityRecord, SessionTransportError> {
        let record = receive_vnext_message(
            &self._receive,
            &self.nonce,
            self.peer_pid,
            self.peer_audit.as_ref(),
            maximum,
            deadline,
        )?;
        if record.kind != VnextRecordKind::Capabilities || record.rights.is_empty() {
            return Err(SessionTransportError::MalformedRecord);
        }
        Ok(VnextCapabilityRecord {
            bytes: record.bytes,
            rights: record.rights,
        })
    }

    /// Completes the broker half of the cooperative traced-launcher gate.
    ///
    /// The child must call [`ChildChannel::prepare_traced_target_exec`] and
    /// exec the target immediately after that method returns.
    pub(super) fn start_traced_launcher(
        &mut self,
        lifecycle: &MacChildLifecycle,
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        let pid = self.peer_pid as Pid;
        if lifecycle.pid() != self.peer_pid {
            return Err(SessionTransportError::IdentityMismatch);
        }
        // Darwin reports traced stops to waitpid even without WUNTRACED. Keep
        // the background sole waiter from consuming either handshake stop.
        let _reaping_pause = lifecycle.pause_reaping();

        let (_self_task, self_audit) = TaskNameRight::capture(std::process::id() as Pid)
            .map_err(bootstrap_lifecycle_transport_error)?;
        let mut launchd_bootstrap = MACH_PORT_NULL;
        // SAFETY: output storage is valid for one copied send right.
        let result = unsafe {
            task_get_special_port(current_task(), TASK_BOOTSTRAP_PORT, &mut launchd_bootstrap)
        };
        if result != KERN_SUCCESS || launchd_bootstrap == MACH_PORT_NULL {
            return Err(SessionTransportError::Native(Some(result)));
        }
        let launchd_bootstrap = SendRight(launchd_bootstrap);
        self.send_vnext_capabilities(
            &encode_audit_token(self_audit),
            &[launchd_bootstrap.0],
            deadline,
        )?;

        if self.receive_vnext_zero_rights(1, deadline)? != [1] {
            return Err(SessionTransportError::MalformedRecord);
        }
        let status = wait_for_traced_stop_until(pid, deadline)?;
        if traced_stop_signal(status) != Some(SIGSTOP) {
            return Err(SessionTransportError::IdentityMismatch);
        }
        lifecycle.mark_traced();
        ptrace_continue(pid)?;

        if self.receive_vnext_zero_rights(1, deadline)? != [2] {
            return Err(SessionTransportError::MalformedRecord);
        }
        let status = wait_for_traced_stop_until(pid, deadline)?;
        if traced_stop_signal(status) != Some(5) {
            return Err(SessionTransportError::IdentityMismatch);
        }
        ptrace_continue(pid)
    }

    #[cfg(test)]
    pub(super) fn take_vnext_lifecycle(
        &mut self,
    ) -> Result<MacChildLifecycle, SessionTransportError> {
        if self.reaped {
            return Err(SessionTransportError::PeerExited);
        }
        let lifecycle = MacChildLifecycle::start(self.peer_pid as Pid)?;
        // The durable worker is now the sole waiter and exact-child cleanup
        // owner. Suppress the legacy blocking ParentChannel destructor.
        self.reaped = true;
        Ok(lifecycle)
    }

    /// Sends one port right to the authenticated helper.
    pub(super) fn send(
        &mut self,
        port: MachPort,
        native: NativeRegionSpec,
        access: PeerAccess,
    ) -> Result<(), BootstrapError> {
        let result = (|| {
            let entry = ManifestEntry::from_native(native, access);
            let transcript = self.single_manifest(entry)?.encode(CAPABILITY_MAGIC);
            send_port(
                self.peer_send.0,
                port,
                MACH_MSG_TYPE_COPY_SEND,
                &self.nonce,
                &transcript,
                None,
            )?;
            self.pending_entries.push(entry);
            Ok(())
        })();
        if result.is_err() {
            self.poison();
        }
        result
    }
    /// Kernel-authenticated helper PID.
    pub const fn peer_pid(&self) -> u32 {
        self.peer_pid
    }
    /// Waits for authenticated READY and acknowledges it with COMMIT.
    pub(super) fn ready_and_commit(&mut self) -> Result<(), BootstrapError> {
        let result = (|| {
            let manifest = self.batch_manifest()?;
            drop(receive_port(
                &self._receive,
                &self.nonce,
                self.peer_pid,
                &manifest.encode(READY_MAGIC),
                None,
            )?);
            #[cfg(test)]
            std::thread::sleep(std::time::Duration::from_millis(50));
            let marker = ReceiveRight::allocate()?;
            send_port(
                self.peer_send.0,
                marker.0,
                MACH_MSG_TYPE_MAKE_SEND,
                &self.nonce,
                &manifest.encode(COMMIT_MAGIC),
                None,
            )?;
            self.pending_entries.clear();
            self.next_transfer_id = self
                .next_transfer_id
                .checked_add(1)
                .ok_or(BootstrapError::InvalidMessage)?;
            Ok(())
        })();
        if result.is_err() {
            self.poison();
        }
        result
    }
    /// Waits for normal helper exit and consumes the child cleanup ledger.
    pub fn wait(mut self) -> Result<(), BootstrapError> {
        let mut status = 0;
        // SAFETY: PID is the held unreaped child and output pointer is valid.
        let result = unsafe { waitpid(self.peer_pid as Pid, &mut status, 0) };
        self.reaped = result == self.peer_pid as Pid;
        if self.reaped && status == 0 {
            Ok(())
        } else {
            Err(BootstrapError::Spawn(status))
        }
    }

    fn single_manifest(&self, entry: ManifestEntry) -> Result<TransferManifest, BootstrapError> {
        TransferManifest::new(
            self.nonce,
            std::process::id(),
            self.peer_pid,
            self.next_transfer_id,
            vec![entry],
        )
        .ok_or(BootstrapError::InvalidMessage)
    }

    fn batch_manifest(&self) -> Result<TransferManifest, BootstrapError> {
        if self.poisoned {
            return Err(BootstrapError::InvalidMessage);
        }
        TransferManifest::new(
            self.nonce,
            std::process::id(),
            self.peer_pid,
            self.next_transfer_id,
            self.pending_entries.clone(),
        )
        .ok_or(BootstrapError::InvalidMessage)
    }

    fn poison(&mut self) {
        self.poisoned = true;
        if !self.reaped {
            terminate_and_reap(self.peer_pid as Pid);
            self.reaped = true;
        }
    }

    pub(super) fn poison_transaction(&mut self) {
        self.poison();
    }

    /// Provenance stamp binding pending values to the open transaction.
    pub(super) const fn pending_provenance(&self) -> TransferProvenance {
        TransferProvenance::new(self.channel_id, self.next_transfer_id)
    }
}

/// Child side obtained from its injected special bootstrap port.
pub struct ChildChannel {
    _parent_send: SendRight,
    receive: ReceiveRight,
    nonce: [u8; 32],
    parent_pid: u32,
    parent_audit: Option<AuditToken>,
    pending_entries: Vec<ManifestEntry>,
    channel_id: u64,
    next_transfer_id: u64,
    poisoned: bool,
}

impl ChildChannel {
    /// Connects using the injected special port and authenticated environment.
    pub fn connect_from_environment() -> Result<Self, BootstrapError> {
        Self::connect_from_environment_inner(None)
    }

    pub(super) fn connect_from_environment_until(
        deadline: AbsoluteDeadline,
    ) -> Result<Self, BootstrapError> {
        Self::connect_from_environment_inner(Some(deadline))
    }

    fn connect_from_environment_inner(
        deadline: Option<AbsoluteDeadline>,
    ) -> Result<Self, BootstrapError> {
        let nonce = parse_nonce(
            &std::env::var(ENV_NONCE).map_err(|_| BootstrapError::MissingEnvironment)?,
        )?;
        let parent_pid = std::env::var(ENV_PARENT_PID)
            .map_err(|_| BootstrapError::MissingEnvironment)?
            .parse()
            .map_err(|_| BootstrapError::InvalidEnvironment)?;
        // Scrub the inherited bootstrap identity so descendants of this receiver
        // cannot reuse the nonce or parent designation, matching the Windows
        // connect scrub and the Linux pre-init scrub.
        // SAFETY: process-local startup state consumed exactly once here before
        // any application or descendant code runs.
        unsafe {
            std::env::remove_var(ENV_NONCE);
            std::env::remove_var(ENV_PARENT_PID);
        }
        let mut parent = MACH_PORT_NULL;
        // SAFETY: output pointer is valid for the current task.
        mach("task_get_special_port", unsafe {
            task_get_special_port(current_task(), TASK_BOOTSTRAP_PORT, &mut parent)
        })?;
        if parent == MACH_PORT_NULL {
            return Err(BootstrapError::InvalidEnvironment);
        }
        let receive = ReceiveRight::allocate()?;
        send_port(
            parent,
            receive.0,
            MACH_MSG_TYPE_MAKE_SEND,
            &nonce,
            &[0; CONTROL_FRAME_LEN],
            deadline,
        )?;
        Ok(Self {
            _parent_send: SendRight(parent),
            receive,
            nonce,
            parent_pid,
            parent_audit: None,
            pending_entries: Vec::new(),
            channel_id: mint_channel_id(),
            next_transfer_id: 1,
            poisoned: false,
        })
    }

    pub(super) const fn vnext_nonce(&self) -> [u8; 32] {
        self.nonce
    }

    pub(super) const fn vnext_parent_pid(&self) -> u32 {
        self.parent_pid
    }

    pub(super) fn send_vnext_zero_rights(
        &mut self,
        bytes: &[u8],
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        send_vnext_message(
            self._parent_send.0,
            &self.nonce,
            VnextRecordKind::ZeroRights,
            bytes,
            &[],
            deadline,
        )
    }

    pub(super) fn receive_vnext_zero_rights(
        &mut self,
        maximum: usize,
        deadline: AbsoluteDeadline,
    ) -> Result<Vec<u8>, SessionTransportError> {
        let record = receive_vnext_message(
            &self.receive,
            &self.nonce,
            self.parent_pid,
            self.parent_audit.as_ref(),
            maximum,
            deadline,
        )?;
        // Pin the coordinator execution identity at the first authenticated
        // record; every later record must carry the identical complete token.
        self.parent_audit.get_or_insert(record.audit);
        if record.kind != VnextRecordKind::ZeroRights || !record.rights.is_empty() {
            return Err(SessionTransportError::MalformedRecord);
        }
        Ok(record.bytes)
    }

    pub(super) fn send_vnext_capabilities(
        &mut self,
        bytes: &[u8],
        rights: &[MachPort],
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        send_vnext_message(
            self._parent_send.0,
            &self.nonce,
            VnextRecordKind::Capabilities,
            bytes,
            rights,
            deadline,
        )
    }

    pub(super) fn receive_vnext_capabilities(
        &mut self,
        maximum: usize,
        deadline: AbsoluteDeadline,
    ) -> Result<VnextCapabilityRecord, SessionTransportError> {
        let record = receive_vnext_message(
            &self.receive,
            &self.nonce,
            self.parent_pid,
            self.parent_audit.as_ref(),
            maximum,
            deadline,
        )?;
        // Pin the coordinator execution identity at the first authenticated
        // record; every later record must carry the identical complete token.
        self.parent_audit.get_or_insert(record.audit);
        if record.kind != VnextRecordKind::Capabilities || record.rights.is_empty() {
            return Err(SessionTransportError::MalformedRecord);
        }
        Ok(VnextCapabilityRecord {
            bytes: record.bytes,
            rights: record.rights,
        })
    }

    /// Establishes cooperative tracing and an irreversible no-descendants
    /// limit before a trusted launcher execs untrusted target code.
    pub(super) fn prepare_traced_target_exec(
        &mut self,
        deadline: AbsoluteDeadline,
    ) -> Result<(), SessionTransportError> {
        let bootstrap = self.receive_vnext_capabilities(32, deadline)?;
        if bootstrap.bytes.len() != 32 || bootstrap.rights.len() != 1 {
            return Err(SessionTransportError::MalformedRecord);
        }
        let expected_parent_audit = decode_audit_token(&bootstrap.bytes)?;
        // SAFETY: the authenticated broker transferred one live send right to
        // its launchd bootstrap namespace. The MIG call copies that send right
        // into this task's special-port slot before target exec.
        let result = unsafe {
            task_set_special_port(current_task(), TASK_BOOTSTRAP_PORT, bootstrap.rights[0].0)
        };
        if result != KERN_SUCCESS {
            return Err(SessionTransportError::Native(Some(result)));
        }

        let parent_pid = self.parent_pid as Pid;
        let (parent_task, parent_audit) =
            TaskNameRight::capture(parent_pid).map_err(bootstrap_lifecycle_transport_error)?;
        if parent_audit != expected_parent_audit {
            return Err(SessionTransportError::IdentityMismatch);
        }
        // SAFETY: getppid has no preconditions.
        if unsafe { getppid() } != parent_pid {
            return Err(SessionTransportError::IdentityMismatch);
        }
        // SAFETY: the trusted launcher voluntarily binds tracing to its exact
        // current parent. XNU rechecks reparenting while establishing it.
        if unsafe { ptrace(PT_TRACE_ME, 0, std::ptr::null_mut(), 0) } != 0 {
            return Err(last_native_error());
        }
        if parent_task
            .audit_token()
            .map_err(bootstrap_lifecycle_transport_error)?
            != parent_audit
        {
            return Err(SessionTransportError::IdentityMismatch);
        }
        // SAFETY: getppid has no preconditions.
        if unsafe { getppid() } != parent_pid {
            return Err(SessionTransportError::IdentityMismatch);
        }

        self.send_vnext_zero_rights(&[1], deadline)?;
        // SAFETY: this creates a traced stop that only the intended broker can
        // observe and continue, proving the relationship before target exec.
        if unsafe { raise(SIGSTOP) } != 0 {
            return Err(last_native_error());
        }
        if parent_task
            .audit_token()
            .map_err(bootstrap_lifecycle_transport_error)?
            != parent_audit
        {
            return Err(SessionTransportError::IdentityMismatch);
        }

        let limit = ResourceLimit {
            current: 1,
            maximum: 1,
        };
        // SAFETY: install an irreversible hard per-UID process limit before
        // target exec. Non-root code cannot raise it again.
        if unsafe { setrlimit(RLIMIT_NPROC, &limit) } != 0 {
            return Err(last_native_error());
        }
        self.send_vnext_zero_rights(&[2], deadline)
    }

    pub(super) fn try_poll_vnext_peer(&self) -> Result<PeerState, SessionTransportError> {
        port_peer_state(self._parent_send.0)
    }
    /// Receives one port right from the authenticated parent.
    pub(super) fn receive(
        &mut self,
        native: NativeRegionSpec,
        access: PeerAccess,
    ) -> Result<SendRight, BootstrapError> {
        let result = (|| {
            let entry = ManifestEntry::from_native(native, access);
            let transcript = self.single_manifest(entry)?.encode(CAPABILITY_MAGIC);
            let right = receive_port(
                &self.receive,
                &self.nonce,
                self.parent_pid,
                &transcript,
                None,
            )?;
            self.pending_entries.push(entry);
            Ok(right)
        })();
        if result.is_err() {
            self.poisoned = true;
        }
        result
    }
    /// Signals validation and waits for the creator's COMMIT acknowledgement.
    pub(super) fn ready_and_wait_commit(&mut self) -> Result<(), BootstrapError> {
        let result = (|| {
            let manifest = self.batch_manifest()?;
            let marker = ReceiveRight::allocate()?;
            send_port(
                self._parent_send.0,
                marker.0,
                MACH_MSG_TYPE_MAKE_SEND,
                &self.nonce,
                &manifest.encode(READY_MAGIC),
                None,
            )?;
            drop(receive_port(
                &self.receive,
                &self.nonce,
                self.parent_pid,
                &manifest.encode(COMMIT_MAGIC),
                None,
            )?);
            self.pending_entries.clear();
            self.next_transfer_id = self
                .next_transfer_id
                .checked_add(1)
                .ok_or(BootstrapError::InvalidMessage)?;
            Ok(())
        })();
        if result.is_err() {
            self.poisoned = true;
        }
        result
    }

    fn single_manifest(&self, entry: ManifestEntry) -> Result<TransferManifest, BootstrapError> {
        TransferManifest::new(
            self.nonce,
            self.parent_pid,
            std::process::id(),
            self.next_transfer_id,
            vec![entry],
        )
        .ok_or(BootstrapError::InvalidMessage)
    }

    fn batch_manifest(&self) -> Result<TransferManifest, BootstrapError> {
        if self.poisoned {
            return Err(BootstrapError::InvalidMessage);
        }
        TransferManifest::new(
            self.nonce,
            self.parent_pid,
            std::process::id(),
            self.next_transfer_id,
            self.pending_entries.clone(),
        )
        .ok_or(BootstrapError::InvalidMessage)
    }

    pub(super) fn poison_transaction(&mut self) {
        self.poisoned = true;
    }

    /// Provenance stamp binding pending values to the open transaction.
    pub(super) const fn pending_provenance(&self) -> TransferProvenance {
        TransferProvenance::new(self.channel_id, self.next_transfer_id)
    }
}

fn send_port(
    remote: MachPort,
    port: MachPort,
    disposition: u8,
    nonce: &[u8; 32],
    transcript: &[u8; CONTROL_FRAME_LEN],
    deadline: Option<AbsoluteDeadline>,
) -> Result<(), BootstrapError> {
    let mut message = PortMessage {
        header: MachMsgHeader {
            bits: MACH_MSGH_BITS_COMPLEX | u32::from(MACH_MSG_TYPE_COPY_SEND),
            size: size_of::<PortMessage>() as u32,
            remote_port: remote,
            local_port: MACH_PORT_NULL,
            voucher_port: MACH_PORT_NULL,
            id: MESSAGE_ID,
        },
        body: MachMsgBody {
            descriptor_count: 1,
        },
        descriptor: MachMsgPortDescriptor {
            name: port,
            pad1: 0,
            pad2: 0,
            disposition,
            descriptor_type: MACH_MSG_PORT_DESCRIPTOR,
        },
        magic: MESSAGE_MAGIC,
        nonce: *nonce,
        transcript: *transcript,
    };
    loop {
        let timeout = bootstrap_timeout(deadline)?;
        // SAFETY: complete initialized message buffer is live for bounded send.
        let result = unsafe {
            mach_msg(
                &mut message.header,
                MACH_SEND_MSG | MACH_SEND_TIMEOUT,
                size_of::<PortMessage>() as u32,
                0,
                MACH_PORT_NULL,
                timeout,
                MACH_PORT_NULL,
            )
        };
        match result {
            KERN_SUCCESS => break,
            MACH_SEND_INTERRUPTED if deadline.is_some() => continue,
            MACH_SEND_TIMED_OUT if deadline.is_some_and(|value| !value.is_expired()) => continue,
            MACH_SEND_TIMED_OUT => return Err(BootstrapError::DeadlineExpired),
            code => {
                return Err(BootstrapError::Mach {
                    operation: "mach_msg(send)",
                    code,
                });
            }
        }
    }
    if deadline.is_some_and(|value| value.is_expired()) {
        return Err(BootstrapError::Ambiguous);
    }
    Ok(())
}

fn receive_port(
    receive: &ReceiveRight,
    nonce: &[u8; 32],
    expected_pid: u32,
    expected_transcript: &[u8; CONTROL_FRAME_LEN],
    deadline: Option<AbsoluteDeadline>,
) -> Result<SendRight, BootstrapError> {
    receive_port_with_audit(receive, nonce, expected_pid, expected_transcript, deadline)
        .map(|(right, _)| right)
}

fn receive_port_with_audit(
    receive: &ReceiveRight,
    nonce: &[u8; 32],
    expected_pid: u32,
    expected_transcript: &[u8; CONTROL_FRAME_LEN],
    deadline: Option<AbsoluteDeadline>,
) -> Result<(SendRight, AuditToken), BootstrapError> {
    // SAFETY: zero is valid initialization for receive buffer/out descriptor.
    let mut buffer: ReceiveBuffer = unsafe { zeroed() };
    loop {
        let timeout = bootstrap_timeout(deadline)?;
        // SAFETY: receive buffer is sized for message plus requested audit trailer.
        let result = unsafe {
            mach_msg(
                &mut buffer.message.header,
                MACH_RCV_MSG | MACH_RCV_TIMEOUT | MACH_RCV_TRAILER_AUDIT,
                0,
                size_of::<ReceiveBuffer>() as u32,
                receive.0,
                timeout,
                MACH_PORT_NULL,
            )
        };
        match result {
            KERN_SUCCESS => break,
            MACH_RCV_INTERRUPTED if deadline.is_some() => continue,
            MACH_RCV_TIMED_OUT if deadline.is_some_and(|value| !value.is_expired()) => continue,
            MACH_RCV_TIMED_OUT => return Err(BootstrapError::DeadlineExpired),
            code => {
                return Err(BootstrapError::Mach {
                    operation: "mach_msg(receive)",
                    code,
                });
            }
        }
    }
    if deadline.is_some_and(|value| value.is_expired()) {
        destroy_legacy_received_if_complex(&mut buffer);
        return Err(BootstrapError::DeadlineExpired);
    }
    let expected_bits = MACH_MSGH_BITS_COMPLEX | (u32::from(MACH_MSG_TYPE_PORT_SEND) << 8);
    let complex = buffer.message.header.bits & MACH_MSGH_BITS_COMPLEX != 0;
    if buffer.message.header.bits != expected_bits
        || buffer.message.header.size as usize != size_of::<PortMessage>()
        || buffer.message.header.remote_port != MACH_PORT_NULL
        || buffer.message.header.local_port != receive.0
        || buffer.message.header.voucher_port != MACH_PORT_NULL
        || buffer.message.header.id != MESSAGE_ID
        || buffer.message.body.descriptor_count != 1
        || buffer.message.descriptor.descriptor_type != MACH_MSG_PORT_DESCRIPTOR
        || buffer.message.descriptor.disposition != MACH_MSG_TYPE_PORT_SEND
        || buffer.message.descriptor.pad1 != 0
        || buffer.message.descriptor.pad2 != 0
        || buffer.message.magic != MESSAGE_MAGIC
        || buffer.message.nonce != *nonce
        || buffer.message.transcript != *expected_transcript
        || buffer.message.descriptor.name == MACH_PORT_NULL
        || buffer.trailer.trailer_type != 0
        || buffer.trailer.trailer_size as usize != size_of::<AuditTrailer>()
    {
        if complex {
            // SAFETY: the kernel delivered a complex message into this live buffer;
            // libSystem destroys every delivered descriptor according to its type.
            unsafe { mach_msg_destroy(&mut buffer.message.header) };
        }
        return Err(BootstrapError::InvalidMessage);
    }
    // SAFETY: kernel supplied a complete audit trailer of the checked size.
    let actual = unsafe { audit_token_to_pid(buffer.trailer.audit) } as u32;
    if actual != expected_pid {
        // SAFETY: the exact checked complex message is still wholly owned by
        // this receive buffer; destroy every delivered right on rejection.
        unsafe { mach_msg_destroy(&mut buffer.message.header) };
        return Err(BootstrapError::WrongPeer {
            expected: expected_pid,
            actual,
        });
    }
    Ok((
        SendRight(buffer.message.descriptor.name),
        buffer.trailer.audit,
    ))
}

struct ReceivedVnextRecord {
    kind: VnextRecordKind,
    bytes: Vec<u8>,
    rights: Vec<SendRight>,
    audit: AuditToken,
}

fn send_vnext_message(
    remote: MachPort,
    nonce: &[u8; 32],
    kind: VnextRecordKind,
    payload: &[u8],
    rights: &[MachPort],
    deadline: AbsoluteDeadline,
) -> Result<(), SessionTransportError> {
    send_vnext_message_inner(remote, nonce, kind, payload, rights, deadline, false)
}

fn send_vnext_message_inner(
    remote: MachPort,
    nonce: &[u8; 32],
    kind: VnextRecordKind,
    payload: &[u8],
    rights: &[MachPort],
    deadline: AbsoluteDeadline,
    allow_kind_rights_mismatch_for_test: bool,
) -> Result<(), SessionTransportError> {
    if payload.is_empty() || payload.len() > MAX_VNEXT_RECORD_BYTES {
        return Err(SessionTransportError::RecordTooLarge);
    }
    let capability_record = kind == VnextRecordKind::Capabilities;
    if (!allow_kind_rights_mismatch_for_test && capability_record != !rights.is_empty())
        || rights.len() > MAX_VNEXT_CAPABILITIES
        || rights.contains(&MACH_PORT_NULL)
    {
        return Err(SessionTransportError::MalformedRecord);
    }
    let descriptor_bytes = rights
        .len()
        .checked_mul(size_of::<MachMsgPortDescriptor>())
        .ok_or(SessionTransportError::RecordTooLarge)?;
    let body_bytes = if rights.is_empty() {
        0
    } else {
        size_of::<MachMsgBody>()
    };
    let unrounded = size_of::<MachMsgHeader>()
        .checked_add(body_bytes)
        .and_then(|size| size.checked_add(descriptor_bytes))
        .and_then(|size| size.checked_add(size_of::<VnextEnvelope>()))
        .and_then(|size| size.checked_add(payload.len()))
        .ok_or(SessionTransportError::RecordTooLarge)?;
    let message_size = round_message(unrounded).ok_or(SessionTransportError::RecordTooLarge)?;
    let words = message_size.div_ceil(size_of::<u64>());
    let mut storage = vec![0_u64; words];
    let bytes = slice_as_bytes_mut(&mut storage);
    let header = MachMsgHeader {
        bits: u32::from(MACH_MSG_TYPE_COPY_SEND)
            | if rights.is_empty() {
                0
            } else {
                MACH_MSGH_BITS_COMPLEX
            },
        size: u32::try_from(message_size).map_err(|_| SessionTransportError::RecordTooLarge)?,
        remote_port: remote,
        local_port: MACH_PORT_NULL,
        voucher_port: MACH_PORT_NULL,
        id: VNEXT_MESSAGE_ID,
    };
    write_value(bytes, 0, header);
    let mut offset = size_of::<MachMsgHeader>();
    if !rights.is_empty() {
        write_value(
            bytes,
            offset,
            MachMsgBody {
                descriptor_count: rights.len() as u32,
            },
        );
        offset += size_of::<MachMsgBody>();
        for right in rights {
            write_value(
                bytes,
                offset,
                MachMsgPortDescriptor {
                    name: *right,
                    pad1: 0,
                    pad2: 0,
                    disposition: MACH_MSG_TYPE_COPY_SEND,
                    descriptor_type: MACH_MSG_PORT_DESCRIPTOR,
                },
            );
            offset += size_of::<MachMsgPortDescriptor>();
        }
    }
    write_value(
        bytes,
        offset,
        VnextEnvelope {
            magic: VNEXT_MESSAGE_MAGIC,
            nonce: *nonce,
            kind: kind as u32,
            payload_len: payload.len() as u32,
        },
    );
    offset += size_of::<VnextEnvelope>();
    bytes[offset..offset + payload.len()].copy_from_slice(payload);

    loop {
        let timeout = deadline_timeout(deadline)?;
        // SAFETY: storage is naturally aligned and contains one fully
        // initialized bounded inline Mach message for the duration of the call.
        let result = unsafe {
            mach_msg(
                bytes.as_mut_ptr().cast(),
                MACH_SEND_MSG | MACH_SEND_TIMEOUT,
                message_size as u32,
                0,
                MACH_PORT_NULL,
                timeout,
                MACH_PORT_NULL,
            )
        };
        match result {
            KERN_SUCCESS => break,
            MACH_SEND_INTERRUPTED => continue,
            MACH_SEND_TIMED_OUT if !deadline.is_expired() => continue,
            MACH_SEND_TIMED_OUT => return Err(SessionTransportError::DeadlineExpired),
            other => return Err(SessionTransportError::Native(Some(other))),
        }
    }
    if deadline.is_expired() {
        return Err(SessionTransportError::Ambiguous);
    }
    Ok(())
}

fn receive_vnext_message(
    receive: &ReceiveRight,
    nonce: &[u8; 32],
    expected_pid: u32,
    expected_audit: Option<&AuditToken>,
    maximum: usize,
    deadline: AbsoluteDeadline,
) -> Result<ReceivedVnextRecord, SessionTransportError> {
    if maximum == 0 || maximum > MAX_VNEXT_RECORD_BYTES {
        return Err(SessionTransportError::RecordTooLarge);
    }
    let maximum_message = size_of::<MachMsgHeader>()
        + size_of::<MachMsgBody>()
        + MAX_VNEXT_CAPABILITIES * size_of::<MachMsgPortDescriptor>()
        + size_of::<VnextEnvelope>()
        + maximum;
    let receive_bytes = round_message(maximum_message)
        .and_then(|size| size.checked_add(size_of::<AuditTrailer>()))
        .ok_or(SessionTransportError::RecordTooLarge)?;
    let words = receive_bytes.div_ceil(size_of::<u64>());
    let mut storage = vec![0_u64; words];
    let bytes = slice_as_bytes_mut(&mut storage);
    loop {
        bytes.fill(0);
        let timeout = deadline_timeout(deadline)?;
        // SAFETY: storage is naturally aligned, zero initialized, and sized for
        // the bounded message plus the requested full audit trailer.
        let result = unsafe {
            mach_msg(
                bytes.as_mut_ptr().cast(),
                MACH_RCV_MSG | MACH_RCV_TIMEOUT | MACH_RCV_TRAILER_AUDIT,
                0,
                u32::try_from(receive_bytes).map_err(|_| SessionTransportError::RecordTooLarge)?,
                receive.0,
                timeout,
                MACH_PORT_NULL,
            )
        };
        match result {
            KERN_SUCCESS => break,
            MACH_RCV_INTERRUPTED => continue,
            MACH_RCV_TIMED_OUT if !deadline.is_expired() => continue,
            MACH_RCV_TIMED_OUT => return Err(SessionTransportError::DeadlineExpired),
            MACH_RCV_TOO_LARGE => return Err(SessionTransportError::RecordTooLarge),
            other => return Err(SessionTransportError::Native(Some(other))),
        }
    }
    if deadline.is_expired() {
        destroy_received_if_complex(bytes);
        return Err(SessionTransportError::DeadlineExpired);
    }
    parse_vnext_message(
        bytes,
        receive.0,
        nonce,
        expected_pid,
        expected_audit,
        maximum,
    )
}

fn parse_vnext_message(
    bytes: &mut [u8],
    expected_receive: MachPort,
    nonce: &[u8; 32],
    expected_pid: u32,
    expected_audit: Option<&AuditToken>,
    maximum: usize,
) -> Result<ReceivedVnextRecord, SessionTransportError> {
    let header =
        read_value::<MachMsgHeader>(bytes, 0).ok_or(SessionTransportError::MalformedRecord)?;
    let complex = header.bits & MACH_MSGH_BITS_COMPLEX != 0;
    let message_size = header.size as usize;
    let trailer_offset =
        round_message(message_size).ok_or(SessionTransportError::MalformedRecord)?;
    let trailer = read_value::<AuditTrailer>(bytes, trailer_offset);
    let mut offset = size_of::<MachMsgHeader>();
    let descriptor_count = if complex {
        let Some(body) = read_value::<MachMsgBody>(bytes, offset) else {
            destroy_received_if_complex(bytes);
            return Err(SessionTransportError::MalformedRecord);
        };
        offset += size_of::<MachMsgBody>();
        body.descriptor_count as usize
    } else {
        0
    };
    let descriptor_bytes = descriptor_count.checked_mul(size_of::<MachMsgPortDescriptor>());
    let envelope_offset = descriptor_bytes.and_then(|size| offset.checked_add(size));
    let envelope = envelope_offset.and_then(|at| read_value::<VnextEnvelope>(bytes, at));
    let expected_bits =
        u32::from(MACH_MSG_TYPE_PORT_SEND) << 8 | if complex { MACH_MSGH_BITS_COMPLEX } else { 0 };
    let canonical_shape = header.bits == expected_bits
        && header.remote_port == MACH_PORT_NULL
        && header.local_port == expected_receive
        && header.voucher_port == MACH_PORT_NULL
        && header.id == VNEXT_MESSAGE_ID
        && descriptor_count <= MAX_VNEXT_CAPABILITIES
        && complex == (descriptor_count != 0)
        && trailer.is_some_and(|value| {
            value.trailer_type == 0 && value.trailer_size as usize == size_of::<AuditTrailer>()
        })
        && envelope
            .is_some_and(|value| value.magic == VNEXT_MESSAGE_MAGIC && value.nonce == *nonce);
    if !canonical_shape {
        destroy_received_if_complex(bytes);
        return Err(SessionTransportError::MalformedRecord);
    }
    let trailer = trailer.expect("checked trailer");
    // SAFETY: the checked complete audit trailer was supplied by the kernel.
    let actual_pid = unsafe { audit_token_to_pid(trailer.audit) } as u32;
    if actual_pid != expected_pid {
        destroy_received_if_complex(bytes);
        return Err(SessionTransportError::IdentityMismatch);
    }
    // When the channel pinned the peer's authentication-time audit token,
    // every later record must carry the identical complete token. A helper
    // `exec` keeps the PID but changes the PID version, so this rejects any
    // record sent by a different execution of the same process.
    if expected_audit.is_some_and(|expected| trailer.audit != *expected) {
        destroy_received_if_complex(bytes);
        return Err(SessionTransportError::IdentityMismatch);
    }
    let envelope = envelope.expect("checked envelope");
    let kind = match envelope.kind {
        1 => VnextRecordKind::ZeroRights,
        2 => VnextRecordKind::Capabilities,
        _ => {
            destroy_received_if_complex(bytes);
            return Err(SessionTransportError::MalformedRecord);
        }
    };
    let payload_len = envelope.payload_len as usize;
    if payload_len == 0 || payload_len > maximum || payload_len > MAX_VNEXT_RECORD_BYTES {
        destroy_received_if_complex(bytes);
        return Err(SessionTransportError::MalformedRecord);
    }
    let payload_offset = envelope_offset
        .and_then(|at| at.checked_add(size_of::<VnextEnvelope>()))
        .ok_or(SessionTransportError::MalformedRecord)?;
    let unrounded = payload_offset
        .checked_add(payload_len)
        .ok_or(SessionTransportError::MalformedRecord)?;
    let canonical_size = round_message(unrounded).ok_or(SessionTransportError::MalformedRecord)?;
    if message_size != canonical_size
        || trailer_offset + size_of::<AuditTrailer>() > bytes.len()
        || unrounded > bytes.len()
        || bytes[unrounded..message_size].iter().any(|byte| *byte != 0)
    {
        destroy_received_if_complex(bytes);
        return Err(SessionTransportError::MalformedRecord);
    }
    let mut right_names = Vec::with_capacity(descriptor_count);
    let mut descriptor_offset = size_of::<MachMsgHeader>() + size_of::<MachMsgBody>();
    for _ in 0..descriptor_count {
        let Some(descriptor) = read_value::<MachMsgPortDescriptor>(bytes, descriptor_offset) else {
            destroy_received_if_complex(bytes);
            return Err(SessionTransportError::MalformedRecord);
        };
        if descriptor.descriptor_type != MACH_MSG_PORT_DESCRIPTOR
            || descriptor.disposition != MACH_MSG_TYPE_PORT_SEND
            || descriptor.name == MACH_PORT_NULL
            || descriptor.pad1 != 0
            || descriptor.pad2 != 0
        {
            destroy_received_if_complex(bytes);
            return Err(SessionTransportError::MalformedRecord);
        }
        right_names.push(descriptor.name);
        descriptor_offset += size_of::<MachMsgPortDescriptor>();
    }
    let rights = right_names.into_iter().map(SendRight).collect();
    Ok(ReceivedVnextRecord {
        kind,
        bytes: bytes[payload_offset..unrounded].to_vec(),
        rights,
        audit: trailer.audit,
    })
}

fn destroy_received_if_complex(bytes: &mut [u8]) {
    let Some(header) = read_value::<MachMsgHeader>(bytes, 0) else {
        return;
    };
    if header.bits & MACH_MSGH_BITS_COMPLEX != 0 {
        // SAFETY: the kernel delivered this complex message into the live
        // aligned buffer; libSystem owns the descriptor-shape destruction ABI.
        unsafe { mach_msg_destroy(bytes.as_mut_ptr().cast()) };
    }
}

fn read_value<T: Copy>(bytes: &[u8], offset: usize) -> Option<T> {
    let end = offset.checked_add(size_of::<T>())?;
    (end <= bytes.len()).then(|| {
        // SAFETY: the byte range is in bounds; unaligned reads support every
        // field offset used by the packed Mach wire representation.
        unsafe { bytes.as_ptr().add(offset).cast::<T>().read_unaligned() }
    })
}

fn write_value<T: Copy>(bytes: &mut [u8], offset: usize, value: T) {
    debug_assert!(offset + size_of::<T>() <= bytes.len());
    // SAFETY: callers reserve the complete in-bounds byte range; unaligned
    // writes support every descriptor offset.
    unsafe {
        bytes
            .as_mut_ptr()
            .add(offset)
            .cast::<T>()
            .write_unaligned(value)
    };
}

fn slice_as_bytes_mut(words: &mut [u64]) -> &mut [u8] {
    // SAFETY: a u64 slice is contiguous initialized storage; byte access spans
    // exactly the same allocation and preserves its natural alignment.
    unsafe { core::slice::from_raw_parts_mut(words.as_mut_ptr().cast(), size_of_val(words)) }
}

fn round_message(size: usize) -> Option<usize> {
    size.checked_add(size_of::<u32>() - 1)
        .map(|value| value & !(size_of::<u32>() - 1))
}

fn deadline_timeout(deadline: AbsoluteDeadline) -> Result<u32, SessionTransportError> {
    let remaining = deadline.remaining();
    if remaining.is_zero() {
        return Err(SessionTransportError::DeadlineExpired);
    }
    Ok(remaining
        .as_nanos()
        .div_ceil(1_000_000)
        .min(u32::MAX as u128) as u32)
}

fn bootstrap_timeout(deadline: Option<AbsoluteDeadline>) -> Result<u32, BootstrapError> {
    let Some(deadline) = deadline else {
        return Ok(TIMEOUT_MS);
    };
    let remaining = deadline.remaining();
    if remaining.is_zero() {
        return Err(BootstrapError::DeadlineExpired);
    }
    Ok(remaining
        .as_nanos()
        .div_ceil(1_000_000)
        .min(u32::MAX as u128) as u32)
}

fn bootstrap_lifecycle_error(error: SessionTransportError) -> BootstrapError {
    match error {
        SessionTransportError::DeadlineExpired => BootstrapError::DeadlineExpired,
        SessionTransportError::IdentityMismatch => BootstrapError::InvalidMessage,
        SessionTransportError::Native(Some(code)) => BootstrapError::Spawn(code),
        SessionTransportError::PeerExited
        | SessionTransportError::MalformedRecord
        | SessionTransportError::RecordTooLarge
        | SessionTransportError::Ambiguous
        | SessionTransportError::Poisoned
        | SessionTransportError::Native(None) => BootstrapError::InvalidMessage,
    }
}

fn bootstrap_lifecycle_transport_error(error: BootstrapError) -> SessionTransportError {
    match error {
        BootstrapError::Mach { code, .. } | BootstrapError::Spawn(code) => {
            SessionTransportError::Native(Some(code))
        }
        BootstrapError::WrongPeer { .. } => SessionTransportError::IdentityMismatch,
        BootstrapError::DeadlineExpired => SessionTransportError::DeadlineExpired,
        BootstrapError::Ambiguous => SessionTransportError::Ambiguous,
        BootstrapError::ExactAuthorityUnavailable { native_error } => {
            SessionTransportError::Native(native_error)
        }
        BootstrapError::InvalidMessage
        | BootstrapError::InvalidEnvironment
        | BootstrapError::MissingEnvironment => SessionTransportError::MalformedRecord,
    }
}

fn destroy_legacy_received_if_complex(buffer: &mut ReceiveBuffer) {
    if buffer.message.header.bits & MACH_MSGH_BITS_COMPLEX != 0 {
        // SAFETY: the kernel delivered a complex message into this live buffer.
        unsafe { mach_msg_destroy(&mut buffer.message.header) };
    }
}

fn lock_lifecycle(
    state: &Mutex<MacChildLifecycleState>,
) -> std::sync::MutexGuard<'_, MacChildLifecycleState> {
    match state.lock() {
        Ok(state) => state,
        Err(poisoned) => poisoned.into_inner(),
    }
}

fn encode_audit_token(token: AuditToken) -> [u8; 32] {
    let mut encoded = [0_u8; 32];
    for (destination, value) in encoded.chunks_exact_mut(4).zip(token.values) {
        destination.copy_from_slice(&value.to_ne_bytes());
    }
    encoded
}

fn decode_audit_token(encoded: &[u8]) -> Result<AuditToken, SessionTransportError> {
    if encoded.len() != 32 {
        return Err(SessionTransportError::MalformedRecord);
    }
    let mut values = [0_u32; 8];
    for (destination, source) in values.iter_mut().zip(encoded.chunks_exact(4)) {
        *destination = u32::from_ne_bytes(
            source
                .try_into()
                .map_err(|_| SessionTransportError::MalformedRecord)?,
        );
    }
    Ok(AuditToken { values })
}

fn last_native_error() -> SessionTransportError {
    SessionTransportError::Native(std::io::Error::last_os_error().raw_os_error())
}

fn traced_stop_signal(status: c_int) -> Option<c_int> {
    (status & 0xff == 0x7f).then_some((status >> 8) & 0xff)
}

fn wait_for_traced_stop_until(
    pid: Pid,
    deadline: AbsoluteDeadline,
) -> Result<c_int, SessionTransportError> {
    loop {
        let mut status = 0;
        // SAFETY: the caller is the exact parent/tracer and output is valid.
        let result = unsafe { waitpid(pid, &mut status, WNOHANG | WUNTRACED) };
        if result == pid {
            if traced_stop_signal(status).is_some() {
                return Ok(status);
            }
            return Err(SessionTransportError::PeerExited);
        }
        if result < 0 {
            let error = std::io::Error::last_os_error();
            if error.kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            return Err(SessionTransportError::Native(error.raw_os_error()));
        }
        if deadline.remaining().is_zero() {
            return Err(SessionTransportError::DeadlineExpired);
        }
        std::thread::sleep(Duration::from_millis(1));
    }
}

fn ptrace_continue(pid: Pid) -> Result<(), SessionTransportError> {
    // SAFETY: address 1 is Darwin's sentinel for continuing at the current
    // program counter; the caller already observed this exact tracee stopped.
    if unsafe {
        ptrace(
            PT_CONTINUE,
            pid,
            std::ptr::without_provenance_mut::<c_void>(1),
            0,
        )
    } == 0
    {
        Ok(())
    } else {
        Err(last_native_error())
    }
}

fn signal_with_audit_token(token: &mut AuditToken, signal: c_int) -> Result<(), i32> {
    // SAFETY: the token was supplied by the kernel for the exact task-name
    // right retained by this lifecycle owner.
    let result = unsafe { proc_signal_with_audittoken(token, signal) };
    if result == 0 {
        Ok(())
    } else {
        Err(std::io::Error::last_os_error()
            .raw_os_error()
            .unwrap_or(result))
    }
}

fn mac_child_reaper(shared: Arc<MacChildLifecycleShared>) {
    let mut termination_attempted = false;
    let mut group_termination_attempted = false;
    let mut pending_signal_error = None;
    loop {
        let pid = shared.pid.load(Ordering::Acquire);
        if pid == 0 {
            if shared.termination.requested() {
                return;
            }
            let state = lock_lifecycle(&shared.state);
            let _ = match shared.changed.wait_timeout(state, Duration::from_millis(1)) {
                Ok(result) => result,
                Err(poisoned) => poisoned.into_inner(),
            };
            continue;
        }

        // Serialize every lifecycle signal/wait decision with the launch
        // handshake. A concurrent termination request must not inject a stop
        // between the launcher's proof SIGSTOP and its exec SIGTRAP.
        let reaper_gate = match shared.reaper_gate.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        };

        if shared.termination.requested() && !termination_attempted {
            if shared.traced.load(Ordering::Acquire) {
                termination_attempted = true;
                // The sole waiter has not reaped this direct child, so a live
                // child owns this PID and an exited child remains a PID-pinning
                // zombie. The numeric stop therefore cannot hit a replacement.
                // SAFETY: pid is this worker's exact unreaped traced child.
                if unsafe { kill(pid, SIGSTOP) } != 0 {
                    let error = std::io::Error::last_os_error()
                        .raw_os_error()
                        .unwrap_or(ESRCH);
                    if error != ESRCH {
                        pending_signal_error = Some(error);
                    }
                }
            } else {
                let audit_token = lock_lifecycle(&shared.state).audit_token;
                if let Some(mut audit_token) = audit_token {
                    termination_attempted = true;
                    if let Err(error) = signal_with_audit_token(&mut audit_token, 9) {
                        // A post-capture `exec` changes the audit-token PID version.
                        // The private exact-signal SPI then returns ESRCH while the
                        // direct child may still be alive; retain that incomplete
                        // cleanup fact rather than falling back to its numeric PID.
                        pending_signal_error = Some(error);
                    }
                }
            }
        }

        #[cfg(test)]
        {
            let delay = shared.reap_delay_ms.swap(0, Ordering::AcqRel);
            if delay != 0 {
                std::thread::sleep(Duration::from_millis(delay));
            }
        }

        #[cfg(test)]
        if shared
            .wait_interrupts
            .fetch_update(Ordering::AcqRel, Ordering::Acquire, |remaining| {
                remaining.checked_sub(1)
            })
            .is_ok()
        {
            continue;
        }

        let traced = shared.traced.load(Ordering::Acquire);
        // Non-traced public children can lead a fresh session whose ordinary
        // descendants must be group-terminated before this sole waiter reaps
        // the pinning direct-child status. Liveness is polled with the
        // lightweight `getpgid`: it answers while the fresh-session leader
        // lives and ESRCHes once it is an unreaped zombie (Darwin refuses
        // group queries on zombies). This avoids a per-iteration wait-queue
        // scan on the live child, whose concurrent Mach negotiation must not
        // be perturbed; the heavier `WNOWAIT` pin confirmation and the group
        // signal run only at that running->zombie transition, so the group
        // kill still precedes the reap that releases the identity pin. A
        // traced child (backend-private launcher, never a fresh public group)
        // uses the exact original `waitpid` path below, untouched.
        if !traced && !group_termination_attempted {
            // SAFETY: a scalar identity query about this worker's exact child.
            if unsafe { getpgid(pid) } > 0 {
                // Still running: nothing to reap or group-terminate yet.
                drop(reaper_gate);
                if let Some(error) = pending_signal_error.take() {
                    let mut state = lock_lifecycle(&shared.state);
                    state.last_error = Some(error);
                    shared.changed.notify_all();
                }
                let state = lock_lifecycle(&shared.state);
                let _ = match shared.changed.wait_timeout(state, Duration::from_millis(1)) {
                    Ok(result) => result,
                    Err(poisoned) => poisoned.into_inner(),
                };
                continue;
            }
            // The leader is an unreaped zombie (this worker is the sole
            // waiter). Perform bounded group termination under its identity
            // pin before the reaping waitpid below.
            group_termination_attempted = true;
            if shared.fresh_group_verified.load(Ordering::Acquire)
                && terminate_descendant_group_under_pin(pid)
            {
                lock_lifecycle(&shared.state).descendants =
                    DescendantCleanupStatus::FreshGroupTerminated;
            }
        }

        let mut status = 0;
        // SAFETY: this worker is the sole waiter for the exact spawned PID.
        let wait_options = if termination_attempted && traced {
            WNOHANG | WUNTRACED
        } else {
            WNOHANG
        };
        // The traced-launcher handshake holds this gate while it consumes the
        // initial SIGSTOP and exec SIGTRAP. Excluding the background waiter is
        // mandatory because Darwin reports trace stops to a direct parent even
        // when its waitpid call omitted WUNTRACED.
        // SAFETY: this worker is the sole background waiter for the exact
        // spawned PID, and the handshake gate excludes its only peer.
        let result = unsafe { waitpid(pid, &mut status, wait_options) };
        if result == pid {
            if traced_stop_signal(status).is_some() {
                // SAFETY: XNU accepts PT_KILL only from this tracee's exact
                // tracer while the tracee is stopped.
                if unsafe { ptrace(PT_KILL, pid, std::ptr::null_mut(), 0) } != 0 {
                    pending_signal_error = Some(
                        std::io::Error::last_os_error()
                            .raw_os_error()
                            .unwrap_or(ESRCH),
                    );
                }
                continue;
            }
            let mut state = lock_lifecycle(&shared.state);
            state.reaped = true;
            state.exit_status = Some(status);
            shared.changed.notify_all();
            return;
        }
        if result < 0 {
            let error = std::io::Error::last_os_error();
            if error.kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            let mut state = lock_lifecycle(&shared.state);
            state.last_error = error.raw_os_error();
            shared.changed.notify_all();
            return;
        }

        if let Some(error) = pending_signal_error.take() {
            let mut state = lock_lifecycle(&shared.state);
            state.last_error = Some(error);
            shared.changed.notify_all();
        }

        drop(reaper_gate);
        let state = lock_lifecycle(&shared.state);
        let _ = match shared.changed.wait_timeout(state, Duration::from_millis(1)) {
            Ok(result) => result,
            Err(poisoned) => poisoned.into_inner(),
        };
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum QueuedExit {
    /// The exact child's exit status is queued and unconsumed, which pins its
    /// PID and process-group identity against reuse until this owner reaps.
    Pinned,
    /// The exact child has not exited.
    NotExited,
    /// The queued-exit question could not be answered; reaping proceeds
    /// without any numeric group operation.
    Unavailable,
}

fn queued_exit_probe(pid: Pid) -> QueuedExit {
    if pid <= 0 {
        return QueuedExit::Unavailable;
    }
    loop {
        // SAFETY: zero is valid initialization for waitid output storage.
        let mut information: DarwinSigInfo = unsafe { core::mem::zeroed() };
        // SAFETY: P_PID targets this owner's exact unreaped child and WNOWAIT
        // leaves the reported status queued rather than consuming it.
        let result = unsafe {
            waitid(
                P_PID,
                pid as u32,
                &mut information,
                WEXITED | WNOHANG | WNOWAIT,
            )
        };
        if result != 0 {
            if std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            return QueuedExit::Unavailable;
        }
        return if information.si_pid == pid {
            QueuedExit::Pinned
        } else {
            QueuedExit::NotExited
        };
    }
}

/// Bounded ordinary-descendant group termination after the caller observed
/// the queued-exit pin on a birth-verified fresh session leader. Returns true
/// only when the pin is observed again afterwards, proving the numeric group
/// identity held throughout because only the sole waiter's own reap can
/// release it.
///
/// `killpg`'s own return distinguishes the outcomes without any system-wide
/// process scan: `0` signaled at least one live in-group descendant, while
/// Darwin reports `EPERM`/`ESRCH` for a group whose only member is the pinned
/// zombie leader — a vacuously terminated group. Any other errno is a real
/// failure.
fn terminate_descendant_group_under_pin(pid: Pid) -> bool {
    // Confirm the queued-exit pin before signaling: while it holds, this sole
    // waiter has not reaped the leader, so the numeric group `pid` cannot have
    // been reused by another session. `getpgid` already reported the leader an
    // unreaped zombie, so the exit is queued; a non-`Pinned` `waitid` answer
    // here is a transient nonblocking-probe glitch and is retried.
    if !witnessed_pin(pid) {
        return false;
    }
    // SAFETY: the pinned zombie leader plus the irrevocable pre-resume session
    // verification prove this numeric group is still the fresh session created
    // for the exact child, so SIGKILL to it cannot reach any process outside
    // that owned session.
    let terminated = unsafe { killpg(pid, 9) } == 0
        || matches!(
            std::io::Error::last_os_error().raw_os_error(),
            Some(EPERM) | Some(ESRCH)
        );
    terminated && witnessed_pin(pid)
}

/// Confirms the queued-exit pin. An exit is permanent, so once `getpgid` has
/// reported the leader an unreaped zombie, a not-exited or unanswerable
/// `waitid` answer can only be a transient kernel artifact of back-to-back
/// nonblocking `WNOWAIT` queries under load; retry it within the bound. Only a
/// persistent refusal (the status actually consumed by a waiter this design
/// excludes) refutes the pin.
fn witnessed_pin(pid: Pid) -> bool {
    for _ in 0..GROUP_ATTEMPT_LIMIT {
        match queued_exit_probe(pid) {
            QueuedExit::Pinned => return true,
            QueuedExit::Unavailable | QueuedExit::NotExited => {
                std::thread::sleep(Duration::from_millis(1));
            }
        }
    }
    false
}

fn port_peer_state(name: MachPort) -> Result<PeerState, SessionTransportError> {
    let mut port_type = 0;
    // SAFETY: output points to one writable type value and name is retained by
    // the authenticated endpoint owner.
    let result = unsafe { mach_port_type(current_task(), name, &mut port_type) };
    if result != KERN_SUCCESS {
        return Err(SessionTransportError::Native(Some(result)));
    }
    if port_type & MACH_PORT_TYPE_DEAD_NAME != 0 {
        Ok(PeerState::ExitedUnknown)
    } else if port_type & MACH_PORT_TYPE_SEND != 0 {
        Ok(PeerState::Running)
    } else {
        Err(SessionTransportError::Native(None))
    }
}

pub(super) fn random_nonce() -> Result<[u8; 32], BootstrapError> {
    let mut nonce = [0_u8; 32];
    // arc4random_buf is provided by libSystem and has no failure mode.
    unsafe extern "C" {
        fn arc4random_buf(buffer: *mut c_void, length: usize);
    }
    // SAFETY: output buffer is valid for its complete length.
    unsafe { arc4random_buf(nonce.as_mut_ptr().cast(), nonce.len()) };
    if nonce == [0; 32] {
        Err(BootstrapError::InvalidEnvironment)
    } else {
        Ok(nonce)
    }
}

fn mach(operation: &'static str, code: c_int) -> Result<(), BootstrapError> {
    if code == KERN_SUCCESS {
        Ok(())
    } else {
        Err(BootstrapError::Mach { operation, code })
    }
}
fn spawn_result(code: c_int) -> Result<(), BootstrapError> {
    if code == 0 {
        Ok(())
    } else {
        Err(BootstrapError::Spawn(code))
    }
}

const fn bootstrap_native_error(error: &BootstrapError) -> Option<c_int> {
    match error {
        BootstrapError::Mach { code, .. } | BootstrapError::Spawn(code) => Some(*code),
        BootstrapError::ExactAuthorityUnavailable { native_error } => *native_error,
        BootstrapError::InvalidMessage
        | BootstrapError::WrongPeer { .. }
        | BootstrapError::InvalidEnvironment
        | BootstrapError::MissingEnvironment
        | BootstrapError::DeadlineExpired
        | BootstrapError::Ambiguous => None,
    }
}
fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}
fn parse_nonce(encoded: &str) -> Result<[u8; 32], BootstrapError> {
    if encoded.len() != 64 {
        return Err(BootstrapError::InvalidEnvironment);
    }
    let mut nonce = [0; 32];
    for (output, pair) in nonce.iter_mut().zip(encoded.as_bytes().chunks_exact(2)) {
        let pair = std::str::from_utf8(pair).map_err(|_| BootstrapError::InvalidEnvironment)?;
        *output = u8::from_str_radix(pair, 16).map_err(|_| BootstrapError::InvalidEnvironment)?;
    }
    Ok(nonce)
}

fn terminate_and_reap(pid: Pid) {
    if pid <= 0 {
        return;
    }
    // SAFETY: SIGKILL cannot be ignored and PID is the held spawned child.
    let _ = unsafe { kill(pid, 9) };
    let mut status = 0;
    // SAFETY: status pointer is valid; held child is reaped at most once here.
    let _ = unsafe { waitpid(pid, &mut status, 0) };
}

const _: () = assert!(size_of::<MachMsgHeader>() == 24);
const _: () = assert!(size_of::<MachMsgPortDescriptor>() == 12);
const _: () = assert!(size_of::<AuditTrailer>() == 52);

#[cfg(test)]
#[path = "bootstrap_test.rs"]
mod tests;