hermit-detcore 0.5.0

Detcore: the deterministic scheduler and syscall determinization core of the Hermit execution engine.
Documentation
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
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

//! Detcore tool global state, and centralized methods corresponding to the centralized portion of
//! the Detcore tool.

mod exec_identity;
pub mod native_exec;
mod parked;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::btree_map::Entry;
use std::fmt::Debug;
use std::fs;
use std::fs::File;
use std::io::Write;
use std::num::NonZeroUsize;
use std::os::fd::FromRawFd;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicU16;
use std::sync::atomic::Ordering::SeqCst;
use std::task::Poll;
use std::time::Duration;
use std::time::SystemTime;

use anyhow::bail;
use chrono::DateTime;
use chrono::Utc;
use detcore_model::host_input::HostFileIdentity;
use detcore_model::host_input::HostInputRecord;
use detcore_model::network_engine::NetworkEngine;
use detcore_model::network_engine::NetworkEngineError;
use detcore_model::network_engine::NetworkReply;
use detcore_model::network_engine::NetworkRequest;
use detcore_model::network_trace::NetworkTraceMode;
use detcore_model::network_trace::NetworkTraceV2;
use detcore_model::procfs::mount_ids_are_ordered_subset;
use detcore_model::summary::PreemptionSummary;
use detcore_model::summary::RunSummary;
use detcore_model::summary::TimesliceStats;
pub(crate) use exec_identity::reconnect_exec;
pub(crate) use exec_identity::retire_exec;
use nix::sys::signal;
use nix::sys::signal::Signal;
use nix::unistd::Pid;
pub(crate) use parked::child_wait_request;
pub(crate) use parked::parked_wait_request;
pub(crate) use parked::polled_read_request;
pub(crate) use parked::signal_dequeued;
use reverie::GlobalRPC;
use reverie::GlobalTool;
use reverie::Guest;
use reverie::Tid;
use reverie::syscalls::AddrMut;
use reverie::syscalls::CloneFlags;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::Sysno;
use serde::Deserialize;
use serde::Serialize;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::trace;
use tracing::warn;

use crate::config::Config;
#[cfg(test)]
use crate::consts::ROOT_DETPID;
use crate::fd::SharedOpenFileError;
use crate::ivar::Ivar;
use crate::preemptions::PreemptionReader;
use crate::preemptions::ThreadHistory;
use crate::procfs_inode;
use crate::procfs_inode::ProcfsName;
use crate::record_or_replay::RecordOrReplay;
use crate::resources::ChaosEpochTransition;
use crate::resources::Permission;
use crate::resources::ResourceID;
use crate::resources::Resources;
use crate::scheduler::AdmitIntent;
use crate::scheduler::AdmitSide;
use crate::scheduler::ConsumeResult;
use crate::scheduler::DEFAULT_PRIORITY;
use crate::scheduler::ExecReconnect;
use crate::scheduler::FutexSignalWatch;
use crate::scheduler::HostTimedSignalScope;
use crate::scheduler::MaybePrintStack;
use crate::scheduler::OwnProcessSignalInputs;
use crate::scheduler::Priority;
use crate::scheduler::SchedResponse;
use crate::scheduler::SchedValue;
use crate::scheduler::Scheduler;
use crate::scheduler::ThreadNextTurn;
use crate::scheduler::entropy_to_priority;
use crate::scheduler::parked::*;
use crate::scheduler::real_timer::DequeueAck;
use crate::scheduler::real_timer::ItimerSnapshot;
use crate::scheduler::real_timer::TimerFailure;
use crate::scheduler::runqueue::FIRST_PRIORITY;
use crate::scheduler::runqueue::LAST_PRIORITY;
use crate::scheduler::runqueue::REPLAY_DEFERRED_PRIORITY;
use crate::scheduler::runqueue::REPLAY_FOREGROUND_PRIORITY;
use crate::scheduler::runqueue::is_ordinary_priority;
use crate::scheduler::sched_loop;
use crate::scheduler::sched_loop_external;
use crate::scheduler::timed_waiters::SignalTarget;
use crate::shared_open_files::OpenFileControl;
use crate::shared_open_files::OpenFileControlReply;
use crate::shared_open_files::SharedOpenFileStore;
use crate::tool_local::Detcore;
use crate::tool_local::ExecFdBlockingOverrides;
use crate::tool_local::ExecFdState;
use crate::tool_local::RobustListWake;
use crate::types::*;

pub(crate) async fn yield_once() {
    let mut yielded = false;
    std::future::poll_fn(|context| {
        if yielded {
            Poll::Ready(())
        } else {
            yielded = true;
            context.waker().wake_by_ref();
            Poll::Pending
        }
    })
    .await;
}

#[derive(Debug)]
struct InodePool {
    // TODO(T87258449): merge these two maps:
    inodes: HashMap<RawInode, DetInode>,
    detinodes_info: HashMap<DetInode, DetInodeInfo>,
    /// Counter backing the minted [`DetInode`]s. Deliberately a plain integer:
    /// it is the *source* of deterministic inodes, not one itself, and typing
    /// it `RawInode` previously blurred that distinction.
    ///
    /// It counts *requests*, not distinct host inodes: see [`Self::add_inode`].
    next_inode: u64,
    /// Virtual mtimes set (by a write or `utimensat`) on host inodes that have
    /// no deterministic number yet. Setting an mtime does not number the file:
    /// whether those calls reach the pool depends on host state (see
    /// [`Self::set_mtime`]), so they must not consume counter values. The
    /// file's first numbering request takes the pending mtime.
    pending_mtimes: HashMap<RawInode, LogicalTime>,
    /// Mappings whose file lost its last name (see [`Self::retire`]), kept
    /// for the descriptors that can still reach it.
    retired: HashMap<RawInode, DetInode>,
    /// Files that lost their last name before anything numbered them, with
    /// the virtual mtime a write through a remaining descriptor set, if any.
    /// A descriptor sighting numbers such a file as retired, and a later name
    /// is another file (see [`Self::forget_retired`]), so the next file the
    /// host gives the inode inherits neither the number nor the mtime.
    retired_unnumbered: HashMap<RawInode, Option<LogicalTime>>,
    /// When each retired inode, numbered or not, was retired: the value of
    /// `retirements` then. A directory snapshot taken before that cannot show
    /// a new file under the inode (see [`InodeSighting::Listed`]).
    retired_at: HashMap<RawInode, u64>,
    /// How many retirements there have been. Each follows a guest's removal
    /// of a last name, so the count is the guest's, like the counter.
    retirements: u64,
    /// Every file that has held each host inode, oldest first, kept after a
    /// later file discards a retirement: a directory snapshot taken while an
    /// earlier file held the inode must still name that file (codex
    /// re-review of <https://github.com/rrnewton/hermit/pull/3849>, P1).
    generations: HashMap<RawInode, Vec<Generation>>,
    /// How many history records the snapshot lookups have examined, for the
    /// test that bounds the lookup's cost.
    #[cfg(test)]
    listed_probes: u64,
    /// How many retired mappings a later sighting showed the host had given to
    /// another file (see [`Self::forget_retired`]). Diagnostic only: it
    /// follows host state and never reaches the guest.
    reused: u64,
    /// The key each procfs host inode, and each procfs path key, is pooled
    /// under (see [`Self::procfs_key`]): the host inode its entry was first
    /// numbered under. Every value is also its own key.
    procfs_keys: HashMap<RawInode, RawInode>,
    /// For each thread ID, the value of `threads_counted` just after each
    /// guest thread given that ID was counted, in order (see
    /// [`Self::count_incarnation`]).
    incarnations: HashMap<i32, Vec<u64>>,
    /// How many guest threads have been counted (see
    /// [`Self::count_incarnation`]).
    threads_counted: u64,
    /// The state Linux keeps on a per-process `/proc` file's inode that
    /// Detcore keeps for the files it renders, by their inode number (see
    /// [`Self::rendered_file_state`]).
    rendered_file_states: HashMap<u64, crate::procfs::listing::RenderedFileState>,
}

/// How a numbering request reached its file, which decides whether a retired
/// mapping (see `InodePool::retire`) answers it.
#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum InodeSighting {
    /// Through a descriptor without a link count, or a stat that reports no
    /// link (`fstat` after `unlink`, `/proc/self/fd/N` of an unlinked file).
    /// Either can reach the file a retired mapping describes.
    Descriptor,
    /// A file that has a name: a directory entry read now, or any stat that
    /// reports a nonzero `st_nlink`. A retired file has no link and can never
    /// get one back, so this is another file that the host gave the freed
    /// inode.
    Name,
    /// A directory entry from a snapshot taken when there had been this many
    /// retirements. The snapshot can be older than the entry's removal: a
    /// stream reads the whole directory at its first `getdents` and returns
    /// the rest later (codex review of
    /// <https://github.com/rrnewton/hermit/pull/3849>, P1). So it shows a
    /// new file only for an inode retired before it was taken.
    Listed(u64),
}

/// What a directory snapshot records of the inode pool when it is taken, so
/// that the entries it returns later are numbered as they were then.
#[derive(PartialEq, Debug, Eq, Clone, Copy, Default, Serialize, Deserialize)]
pub struct SnapshotMark {
    /// How many retirements there had been (see [`InodeSighting::Listed`]):
    /// an entry can be returned after its file's last name is gone.
    pub retirements: u64,
    /// How many guest threads had been counted (see
    /// `InodePool::count_incarnation`): an entry can be returned after a task
    /// it names exited and a new thread took its ID.
    pub threads_counted: u64,
}

/// One file that held a host inode (see `InodePool::generations`). It held
/// the inode from just after the previous file's retirement until its own.
#[derive(Debug)]
struct Generation {
    /// Its number, or `None` while nothing has numbered it.
    number: Option<DetInode>,
    /// The retirement count at which it lost its last name, if it has.
    retired: Option<u64>,
    /// The mtime a write left on it before anything numbered it.
    pending: Option<LogicalTime>,
}

/// Everything we know (globally) about a DetInode.
#[derive(Debug)]
struct DetInodeInfo {
    /// The virtual mtime, or `None` while no stat has observed the file and no
    /// write or explicit time update has set it. Many paths (open, read,
    /// getdents) mint a DetInode before any stat, so the first-seen policy is
    /// applied at the first stat rather than at minting; otherwise whether a
    /// file kept a canonical mtime would depend on which syscall touched it
    /// first. An unresolved mtime reads as the epoch.
    mtime: Option<LogicalTime>,
}

/// Whole-second host mtimes, with a zero nanosecond part, that a file keeps
/// when Hermit first sees it, instead of having its mtime replaced by the
/// epoch (<https://github.com/rrnewton/hermit/issues/3639>).
///
/// These values are canonical and content-independent: they are written
/// deliberately by tools that erase timestamps, so they carry no host clock
/// or run-specific information. Revealing them makes whether a file's host
/// mtime is canonical part of Hermit's input, like the file's contents: a
/// guest that rewrites the host mtime of an input file (`touch f` after
/// `stat f` on a canonical file) changes the input of the next run, exactly
/// as rewriting its contents would.
/// - `1` (1970-01-01T00:00:01Z) is the mtime of every file and directory in
///   the Nix store (seconds 1, nanoseconds 0). nixpkgs' stdenv derives
///   `SOURCE_DATE_EPOCH` from the newest source mtime, so reporting the epoch
///   instead moves embedded build timestamps away from a native build's.
/// - `0` is what `SOURCE_DATE_EPOCH=0` tooling and archive normalizers write.
///
/// Matching is exact. A non-zero nanosecond part, or any other second, is a
/// real timestamp, and the file gets the epoch as before.
pub const CANONICAL_FILE_MTIME_SECONDS: [i64; 2] = [0, 1];

/// What a syscall observed about a file's host mtime, already reduced to the
/// only part the virtual mtime may depend on.
#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum ObservedMtime {
    /// The caller did not read the file's metadata (open, read, getdents...).
    Unobserved,
    /// A stat observed a host mtime that is a real timestamp.
    HostSpecific,
    /// A stat observed one of [`CANONICAL_FILE_MTIME_SECONDS`], which is kept.
    Canonical(LogicalTime),
}

impl ObservedMtime {
    /// Classify a host mtime that a stat reported.
    pub fn from_host_mtime(tv_sec: i64, tv_nsec: i64) -> Self {
        match u64::try_from(tv_sec) {
            Ok(secs) if tv_nsec == 0 && CANONICAL_FILE_MTIME_SECONDS.contains(&tv_sec) => {
                ObservedMtime::Canonical(LogicalTime::from_secs(secs))
            }
            _ => ObservedMtime::HostSpecific,
        }
    }

    /// The virtual mtime to assign to a file whose mtime is still unresolved:
    /// a canonical value is kept, a real timestamp becomes the epoch, and no
    /// observation leaves it unresolved.
    fn first_seen_mtime(self, epoch: LogicalTime) -> Option<LogicalTime> {
        match self {
            ObservedMtime::Unobserved => None,
            ObservedMtime::HostSpecific => Some(epoch),
            ObservedMtime::Canonical(mtime) => Some(mtime),
        }
    }
}

/// Everything the global scheduler needs to register a new child thread. A
/// normal clone is registered by the parent; a `CLONE_VFORK` child registers
/// itself (with `parent_is_kernel_blocked` set) because its parent is blocked
/// inside the kernel until the child execs or exits.
struct ChildRegistration {
    parent_dettid: DetTid,
    parent_detpid: DetPid,
    child_dettid: DetTid,
    child_tid_addr: usize,
    flags: Option<CloneFlags>,
    exit_signal: libc::c_int,
    physical_ids: Option<(i32, i32)>,
    maybe_priority: Option<Priority>,
    parent_is_kernel_blocked: bool,
}

#[derive(Debug)]
struct PendingInheritedInput {
    lease: reverie::InheritedInputLease,
    root: Option<(DetTid, MmId)>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
struct PendingExecState {
    caller: DetTid,
    process: DetPid,
    mm: MmId,
    fd_blocking: ExecFdState,
}

/// Separate terminal cleanup outcomes; neither replaces the backend failure.
pub struct BackendFailureCleanup {
    /// Natural scheduler completion, retaining a task panic or cancellation.
    pub scheduler: Result<(), tokio::task::JoinError>,
    /// The requested partial recording's write result; no destination is success.
    pub preemption_recording: Result<(), String>,
}

#[derive(Clone, Copy)]
struct RpcIncarnation {
    dettid: DetTid,
    mm: MmId,
}

impl Default for InodePool {
    fn default() -> Self {
        InodePool::new()
    }
}

impl InodePool {
    fn new() -> Self {
        InodePool {
            inodes: HashMap::new(),
            detinodes_info: HashMap::new(),
            // Above the fixed stdio inodes (`DET_SPECIAL_INODE_OFFSET`), so a
            // pooled number can never collide with one of them.
            next_inode: crate::consts::DET_INODE_OFFSET.as_raw(),
            pending_mtimes: HashMap::new(),
            retired: HashMap::new(),
            retired_unnumbered: HashMap::new(),
            retired_at: HashMap::new(),
            retirements: 0,
            generations: HashMap::new(),
            #[cfg(test)]
            listed_probes: 0,
            reused: 0,
            procfs_keys: HashMap::new(),
            incarnations: HashMap::new(),
            threads_counted: 0,
            rendered_file_states: HashMap::new(),
        }
    }

    /// How many guest threads have been given task ID `tid` in this run (see
    /// [`Self::count_incarnation`]): the incarnation of the one holding it
    /// now, 0 for an ID no guest thread has held.
    fn incarnation(&self, tid: i32) -> u32 {
        self.incarnations.get(&tid).map_or(0, |counted| {
            u32::try_from(counted.len()).unwrap_or(u32::MAX)
        })
    }

    /// What Detcore's process table says of the task `named` and its thread
    /// `thread` (`existence`: `Scheduler::procfs_task`'s whether it exists
    /// and whether it has exited), with the incarnation each number has now
    /// ([`Self::incarnation`]).
    fn procfs_task_answer(
        &self,
        named: DetTid,
        thread: Option<DetTid>,
        (exists, without_mm): (bool, bool),
    ) -> crate::procfs::listing::ProcfsTaskAnswer {
        crate::procfs::listing::ProcfsTaskAnswer {
            exists,
            without_mm,
            process_incarnation: self.incarnation(named.as_raw()),
            thread_incarnation: thread.map(|thread| self.incarnation(thread.as_raw())),
            mm: None,
            runtime_private_memory: None,
        }
    }

    /// Apply `change` to the state of the rendered per-process `/proc` file
    /// numbered `inode`, and return the state. Every change comes in its
    /// guest thread's turn, so their order is the scheduler's.
    ///
    /// Only a file whose owner wrote its state (an mtime, a write hint) has
    /// an entry; a read of any other inserts none. Entries are kept for the
    /// run: a per-process inode names one task generation
    /// (`procfs_inode::per_process_inode`), and a descriptor or a directory
    /// of a task that is gone can still reach its file (an `fstat` of a
    /// descriptor kept past a reap reads its mtime), so no entry can be
    /// retired exactly. The table holds one entry per per-process inode whose
    /// owner set a time or a hint, and an inode is a spelling's identity
    /// (`procfs_inode::per_process_inode`): for a thread group of N threads,
    /// up to `(N + N²) × PER_PROCESS_NAMES.len()` inodes per group of task
    /// generations (N process spellings, N² `/proc/<n>/task/<m>` spellings,
    /// `procfs::per_process_path`), and only those the guest wrote.
    // TODO-HUMAN-REVIEW(#4213): Review the per-inode mtime and write-hint
    // state Detcore keeps for its rendered per-process files, in the global
    // tool, changed in the changing thread's turn.
    fn rendered_file_state(
        &mut self,
        inode: u64,
        change: crate::procfs::listing::RenderedFileChange,
    ) -> crate::procfs::listing::RenderedFileState {
        use crate::procfs::listing::RenderedFileChange;
        let state = match change {
            RenderedFileChange::Read => {
                return self
                    .rendered_file_states
                    .get(&inode)
                    .copied()
                    .unwrap_or_default();
            }
            _ => self.rendered_file_states.entry(inode).or_default(),
        };
        match change {
            RenderedFileChange::Read => {}
            RenderedFileChange::Mtime(seconds, nanoseconds) => {
                state.mtime = Some((seconds, nanoseconds));
            }
            RenderedFileChange::WriteHint(hint) => state.write_hint = hint,
        }
        *state
    }

    /// The key to pool the procfs entry at host inode `host` under, given the
    /// key of the path it was reached by (see [`Self::procfs_path_key`]).
    ///
    /// Linux numbers per-process procfs entries when it builds their
    /// dentries, and builds an evicted one again under a new host inode
    /// (see `procfs_inode`). A host inode the pool has seen keeps its key,
    /// so aliases (`/proc/<pid>/net/tcp` and `/proc/<pid>/task/<tid>/net/tcp`
    /// are one inode) and sightings without a path agree. A host inode the
    /// pool has not seen, reached by a path that already named an entry, is
    /// that entry rebuilt: it takes the entry's key, and with it its number
    /// and virtual mtime. Consumes no counter value.
    fn procfs_key(&mut self, host: RawInode, path: RawInode) -> RawInode {
        let key = match self.procfs_keys.get(&host) {
            Some(key) => *key,
            None if self.inodes.contains_key(&host)
                || self.retired.contains_key(&host)
                || self.retired_unnumbered.contains_key(&host) =>
            {
                host
            }
            None => self.procfs_keys.get(&path).copied().unwrap_or(host),
        };
        self.procfs_keys.insert(host, key);
        self.procfs_keys.insert(path, key);
        // A write through a descriptor nothing had registered left its mtime
        // under the host inode.
        if key != host
            && let Some(mtime) = self.pending_mtimes.remove(&host)
        {
            self.set_mtime(key, mtime);
        }
        key
    }

    /// The key `raw_inode` is pooled under: its procfs key if it has one
    /// (see [`Self::procfs_key`]), otherwise itself.
    fn key(&self, raw_inode: RawInode) -> RawInode {
        self.procfs_keys
            .get(&raw_inode)
            .copied()
            .unwrap_or(raw_inode)
    }

    /// Counts a new guest thread with thread ID `tid`. A process or thread
    /// ID the kernel gives out again names a new task, and Linux drops the
    /// old task's procfs entries when it exits, so the new task's entries
    /// are new files (see [`Self::procfs_path_key`]).
    fn count_incarnation(&mut self, tid: i32) {
        self.threads_counted += 1;
        self.incarnations
            .entry(tid)
            .or_default()
            .push(self.threads_counted);
    }

    /// The path key of the procfs entry `name`: the key of its path, salted
    /// with the incarnation of each task the path names (see
    /// [`Self::count_incarnation`]), so that an entry of a task whose ID was
    /// reused does not adopt the number of the old task's entry, which a
    /// descriptor may still hold. An entry of a directory snapshot names the
    /// incarnation that held the ID when the snapshot was taken.
    fn procfs_path_key(&self, name: &ProcfsName) -> RawInode {
        name.tasks.iter().fold(name.path, |key, task| {
            let counted = self.incarnations.get(task).map_or(&[][..], Vec::as_slice);
            let incarnation = match name.threads_counted {
                Some(snapshot) => counted.partition_point(|at| *at <= snapshot),
                None => counted.len(),
            };
            match u32::try_from(incarnation).unwrap_or(u32::MAX) {
                0 => key,
                incarnation => procfs_inode::with_incarnation(key, *task, incarnation),
            }
        })
    }

    // Allocate the next deterministic inode.  This takes the raw-inode and
    // can return an existing mapping or extend the mapping by creating a
    // new deterministic inode. Minted numbers are strictly increasing and
    // never handed out twice.
    //
    // Host inode reuse: a filesystem such as ext4 or XFS gives a freed
    // `(dev, ino)` to a new file, and whether it does differs between runs
    // (https://github.com/rrnewton/hermit/issues/3840). A file's mapping, or
    // its pending mtime if nothing numbered it yet, is retired when the guest
    // removes its last name (`Self::retire`). A `Name` sighting of a retired
    // `(dev, ino)` discards it, so the new file is numbered by this request
    // in every run, reused or not, and inherits no mtime. A `Descriptor`
    // sighting still gets the retired number: an unlinked file reached through
    // a descriptor keeps its inode number, as on Linux, and a descriptor to a
    // new file was opened by name, which discarded the retired mapping
    // (`Self::forget_retired`).
    //
    // Still only contained, not handled: a file that never had a name
    // (`O_TMPFILE`, never linked) is never retired, nor is a file that
    // disappears without the guest removing its name (another process
    // deletes it, or its filesystem is unmounted). A named file the host
    // gives such a file's freed inode inherits its number and mtime.
    //
    // Every call consumes the next counter value, whether or not it mints.
    // Whether a host inode is "new" depends on host state the guest does not
    // control (a file replaced by another process, an inode the filesystem
    // reused, a hard link), while the sequence of requests is the guest's
    // own. Counting only mints let one such host-dependent sighting shift the
    // number of every file minted after it, on every device
    // (https://github.com/rrnewton/hermit/issues/2897); counting requests
    // confines the difference to the file whose host identity differed.
    // Numbers stay unique and increasing, just not dense.
    //
    // Also return the virtual mtime. When it is still unresolved, `observed`
    // resolves it under the first-seen policy (see `ObservedMtime`); an
    // unresolved mtime reads as `epoch`.
    fn add_sighted_inode(
        &mut self,
        raw_inode: RawInode,
        observed: ObservedMtime,
        epoch: LogicalTime,
        sighting: InodeSighting,
    ) -> (DetInode, LogicalTime) {
        let raw_inode = self.key(raw_inode);
        let ordinal = self.next_inode;
        self.next_inode += 1;
        match sighting {
            InodeSighting::Name => self.forget_retired(raw_inode),
            InodeSighting::Listed(snapshot) => {
                if let Some(answer) = self.listed_generation(raw_inode, snapshot, ordinal) {
                    let mtime = self
                        .detinodes_info
                        .get_mut(&answer)
                        .expect("Internal invariant broken, det_ino missing entry");
                    if mtime.mtime.is_none() {
                        mtime.mtime = observed.first_seen_mtime(epoch);
                    }
                    return (answer, mtime.mtime.unwrap_or(epoch));
                }
                if self
                    .retired_at
                    .get(&raw_inode)
                    .is_some_and(|retired| *retired < snapshot)
                {
                    self.forget_retired(raw_inode);
                }
            }
            InodeSighting::Descriptor => {}
        }
        let dino = match self
            .inodes
            .get(&raw_inode)
            .or_else(|| self.retired.get(&raw_inode))
        {
            Some(dino) => *dino,
            None => {
                // THE determinization boundary: the single place a host inode
                // is deliberately mapped to a deterministic one. The value is
                // minted from a monotonic counter, never derived from the host
                // inode's bits.
                let new = DetInode::mint(ordinal);
                // A file whose last name went away before it was numbered,
                // reached through a descriptor, is numbered as retired.
                let mtime = match self.retired_unnumbered.remove(&raw_inode) {
                    Some(mtime) => {
                        self.retired.insert(raw_inode, new);
                        if let Some(last) = self
                            .generations
                            .get_mut(&raw_inode)
                            .and_then(|generations| generations.last_mut())
                        {
                            last.number = Some(new);
                        }
                        mtime
                    }
                    None => {
                        assert!(self.inodes.insert(raw_inode, new).is_none());
                        self.generations
                            .entry(raw_inode)
                            .or_default()
                            .push(Generation {
                                number: Some(new),
                                retired: None,
                                pending: None,
                            });
                        self.pending_mtimes.remove(&raw_inode)
                    }
                };
                let prev = self.detinodes_info.insert(new, DetInodeInfo { mtime });
                assert!(prev.is_none()); // Should not have been previously used.
                new
            }
        };
        let info = self
            .detinodes_info
            .get_mut(&dino)
            .expect("Internal invariant broken, det_ino missing entry");
        if info.mtime.is_none() {
            info.mtime = observed.first_seen_mtime(epoch);
        }
        (dino, info.mtime.unwrap_or(epoch))
    }

    /// [`Self::add_sighted_inode`] for a [`InodeSighting::Descriptor`].
    fn add_inode(
        &mut self,
        raw_inode: RawInode,
        observed: ObservedMtime,
        epoch: LogicalTime,
    ) -> (DetInode, LogicalTime) {
        self.add_sighted_inode(raw_inode, observed, epoch, InodeSighting::Descriptor)
    }

    // Set a file's virtual mtime, replacing whatever the first-seen policy
    // chose or would choose. This never consumes a counter value: its callers
    // (a write's mtime bump, `utimensat`) reach the pool or not depending on
    // host state, such as whether the target's host identity held still
    // during the call, and a request counted here would shift the number of
    // every file numbered after it. A file not yet numbered keeps the mtime
    // pending until its first numbering request.
    fn set_mtime(&mut self, raw_inode: RawInode, mtime: LogicalTime) {
        let raw_inode = self.key(raw_inode);
        // A retired file is written through a descriptor opened before its
        // last name went away; a new file's descriptor was opened by name,
        // which discarded the retired mapping.
        match self
            .inodes
            .get(&raw_inode)
            .or_else(|| self.retired.get(&raw_inode))
        {
            Some(dino) => {
                self.detinodes_info
                    .get_mut(dino)
                    // TODO(T87258449): remove this `expect`:
                    .expect("Invariant violation: det inode missing from map.")
                    .mtime = Some(mtime);
            }
            None => match self.retired_unnumbered.get_mut(&raw_inode) {
                Some(retired) => {
                    *retired = Some(mtime);
                    // Also in its history record, which a directory snapshot
                    // consults once the host has reused the inode (claude
                    // re-check of https://github.com/rrnewton/hermit/pull/3849).
                    if let Some(last) = self
                        .generations
                        .get_mut(&raw_inode)
                        .and_then(|generations| generations.last_mut())
                    {
                        last.pending = Some(mtime);
                    }
                }
                None => {
                    self.pending_mtimes.insert(raw_inode, mtime);
                }
            },
        }
    }

    /// The file behind `raw_inode` lost its last name (an `unlink`,
    /// `rmdir` or replacing `rename` removed it). Its number stays for the
    /// descriptors that can still reach it, but a later [`InodeSighting::Name`]
    /// of `raw_inode` is another file. Consumes no counter value: whether a
    /// removal reaches here follows host state (its `st_nlink`), and must not
    /// shift other files' numbers.
    fn retire(&mut self, raw_inode: RawInode) {
        let raw_inode = self.key(raw_inode);
        let stamp = self.retirements;
        self.retired_at.insert(raw_inode, stamp);
        self.retirements += 1;
        let generations = self.generations.entry(raw_inode).or_default();
        match self.inodes.remove(&raw_inode) {
            Some(dino) => {
                self.retired.insert(raw_inode, dino);
                match generations.last_mut() {
                    Some(last) if last.number == Some(dino) && last.retired.is_none() => {
                        last.retired = Some(stamp);
                    }
                    _ => generations.push(Generation {
                        number: Some(dino),
                        retired: Some(stamp),
                        pending: None,
                    }),
                }
            }
            // Nothing numbered it yet, but a write may have left a pending
            // mtime, which must stay with this file (claude review of
            // https://github.com/rrnewton/hermit/pull/3849, P2-2).
            None => {
                let pending = self.pending_mtimes.remove(&raw_inode);
                self.retired_unnumbered.insert(raw_inode, pending);
                generations.push(Generation {
                    number: None,
                    retired: Some(stamp),
                    pending,
                });
            }
        }
    }

    /// The number of the file that held `raw_inode` when a directory snapshot
    /// was taken after `snapshot` retirements, if that file has since lost its
    /// last name. A file that held it then and was never numbered is numbered
    /// now, as `ordinal`. `None` when the inode's holder at the snapshot still
    /// holds it, or nothing ever retired one of its holders, so that the
    /// current file answers.
    ///
    /// A file held the inode from just after the previous holder's retirement
    /// (the host cannot reuse an inode before it is freed) through its own:
    /// the retirement with stamp `r` happened when the count was `r`, so a
    /// snapshot that saw it still named has `snapshot <= r`.
    fn listed_generation(
        &mut self,
        raw_inode: RawInode,
        snapshot: u64,
        ordinal: u64,
    ) -> Option<DetInode> {
        let generations = self.generations.get_mut(&raw_inode)?;
        // Holders are retired in order and the stamps only grow, and only the
        // last can still hold the inode, so the stamps are sorted. A snapshot
        // newer than the last retirement saw the current holder, the common
        // case, answered without a search; an older one finds its holder by
        // binary search: the first holder retired at or after the snapshot.
        // The previous holder was retired before the snapshot, so this one
        // held the inode then (codex re-check of
        // <https://github.com/rrnewton/hermit/pull/3849>: a scan from the
        // oldest made churn on one inode quadratic).
        let last_retired = match generations.last()?.retired {
            Some(retired) => Some(retired),
            None => generations.iter().rev().nth(1).and_then(|g| g.retired),
        };
        if last_retired.is_none_or(|retired| snapshot > retired) {
            return None;
        }
        #[cfg(test)]
        let mut probes = 0;
        let index = generations.partition_point(|generation| {
            #[cfg(test)]
            {
                probes += 1;
            }
            generation.retired.is_some_and(|retired| retired < snapshot)
        });
        #[cfg(test)]
        {
            self.listed_probes += probes;
        }
        // The holder at the snapshot still holds the inode: the current file
        // answers.
        generations.get(index)?.retired?;
        let current = index + 1 == generations.len();
        let generation = &mut generations[index];
        if let Some(number) = generation.number {
            return Some(number);
        }
        let new = DetInode::mint(ordinal);
        generation.number = Some(new);
        let mut mtime = generation.pending;
        // The latest holder, still retired and reached first by this entry, is
        // numbered as a descriptor would number it.
        if current && let Some(pending) = self.retired_unnumbered.remove(&raw_inode) {
            self.retired.insert(raw_inode, new);
            mtime = pending;
        }
        let prev = self.detinodes_info.insert(new, DetInodeInfo { mtime });
        assert!(prev.is_none()); // Should not have been previously used.
        Some(new)
    }

    /// `raw_inode` names a file that has a name, or that was just created
    /// (an `open` that found it linked, or `O_TMPFILE`), so a retired mapping
    /// of it described a file the host has freed: discard it. Consumes no
    /// counter value.
    fn forget_retired(&mut self, raw_inode: RawInode) {
        let raw_inode = self.key(raw_inode);
        self.retired_at.remove(&raw_inode);
        // Its number and mtime stay in `generations` and `detinodes_info` for
        // directory snapshots taken while it held the inode.
        if self.retired.remove(&raw_inode).is_some() {
            self.reused += 1;
        }
        if self.retired_unnumbered.remove(&raw_inode).is_some() {
            self.reused += 1;
        }
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1056): Deterministic remapping of device numbers (st_dev).
/// Deterministic remapping of device numbers (`st_dev`).
///
/// The kernel assigns anonymous block-device numbers to filesystems without a
/// backing block device (procfs, sysfs, tmpfs, devpts) from a global,
/// host-wide counter (`get_anon_bdev`). The raw `st_dev` a guest observes for
/// such a filesystem therefore drifts between otherwise-identical runs — and
/// even between the two runs of `--verify`, because the first run's mounts are
/// still live when the second run mounts fresh copies, so the second run's
/// procfs gets a different anonymous device number. That leaked host state into
/// a guest-visible `stat`/`statx` field.
///
/// We replace each distinct raw device number with a strictly-increasing
/// synthetic id assigned in first-observation order. Both `stat`/`statx` and a
/// virtualized mountinfo snapshot use this pool. A mountinfo read intentionally
/// pre-populates it in that snapshot's row order, and later metadata syscalls
/// reuse those assignments.
///
/// This guarantees identity consistency within one Detcore run. It is not an
/// unconditional cross-machine guarantee: hosts with different filesystem
/// layouts can expose different device equivalence classes or first-observation
/// order. The remapping still preserves distinctness and equality within the
/// run, so `find -xdev`, `du -x`, and `(st_dev, st_ino)` checks behave
/// consistently with the mountinfo device column.
#[derive(Debug)]
struct DevicePool {
    devices: HashMap<u64, u64>,
    next_device: u64,
}

/// Run-global guest-visible mount IDs, shared by `/proc/*/mountinfo` and
/// `/proc/*/fdinfo/*`.
///
/// Linux gives pseudo filesystems such as pipefs, sockfs, anon_inodefs, nsfs,
/// and pidfs their own mount IDs without listing those mounts in
/// `/proc/*/mountinfo`. The raw numbers are host-assigned. Preserve equality
/// and distinctness by keying on the raw mount ID, and number raw IDs in the
/// order the guest first observes them: an fdinfo read assigns its one ID, and
/// a mountinfo read assigns its not-yet-numbered row and parent IDs in row
/// order. The namespace's row order is kept only to validate later views.
///
/// Numbering must not depend on mounts the guest never observed. A run's mount
/// namespace starts as a copy of the host's, so its row count changes with
/// unrelated host activity, such as another process's loop mount under a
/// shared-propagation directory. Numbering by position in that table made the
/// fdinfo `mnt_id` of a pipe or of `/dev/null`, neither of which mountinfo
/// lists, differ between two runs of the same guest.
#[derive(Debug)]
enum MountIdPool {
    Uninitialized,
    Invalid,
    Ready {
        /// Raw mount ID to guest-visible mount ID, for every raw ID numbered
        /// so far. `mount_ids[&assignment_order[i]] == i + 1`.
        mount_ids: BTreeMap<u64, u64>,
        /// Raw mount IDs in the order they were numbered.
        assignment_order: Vec<u64>,
        /// The namespace's raw row/parent order. Validation only.
        mountinfo_order: Vec<u64>,
        allow_visible_subsets: bool,
    },
}

/// The raw identity orders observed by the producer and required to reconstruct
/// the same guest-visible mount IDs during replay.
pub struct MountIdentityProvenance {
    /// The namespace's raw mountinfo row/parent order, which later mountinfo
    /// views are validated against.
    pub mountinfo_order: Vec<u64>,
    /// Raw mount IDs in the order the run numbered them.
    pub assignment_order: Vec<u64>,
}

impl MountIdPool {
    fn from_config(mount_ids: &[u64], captured: bool, assignment_order: &[u64]) -> Self {
        if !captured {
            if !mount_ids.is_empty() || !assignment_order.is_empty() {
                return Self::Invalid;
            }
            return Self::Uninitialized;
        }
        Self::from_orders(mount_ids, assignment_order, true).unwrap_or(Self::Invalid)
    }

    fn from_orders(
        mount_ids: &[u64],
        assignment_order: &[u64],
        allow_visible_subsets: bool,
    ) -> Option<Self> {
        // A recorded assignment may include IDs mountinfo does not list, such
        // as pipefs. It may include raw 0 only if the namespace order names 0
        // as a row or parent: an fdinfo zero is the anonymous class and is
        // never numbered, and a mountinfo zero must pass order validation.
        if !detcore_model::procfs::mount_identity_provenance_is_well_formed(
            mount_ids,
            assignment_order,
        ) {
            return None;
        }
        let mut mappings = BTreeMap::new();
        for (index, raw) in assignment_order.iter().enumerate() {
            let virtual_mount_id = u64::try_from(index).ok()?.checked_add(1)?;
            if mappings.insert(*raw, virtual_mount_id).is_some() {
                return None;
            }
        }
        Some(Self::Ready {
            mount_ids: mappings,
            assignment_order: assignment_order.to_vec(),
            mountinfo_order: mount_ids.to_vec(),
            allow_visible_subsets,
        })
    }

    fn validate_mountinfo_order(&mut self, mountinfo_order: &[u64]) -> bool {
        if matches!(self, Self::Uninitialized) {
            *self = Self::from_orders(mountinfo_order, &[], false).unwrap_or(Self::Invalid);
        }
        let Self::Ready {
            mountinfo_order: expected,
            allow_visible_subsets,
            ..
        } = self
        else {
            return false;
        };
        if *allow_visible_subsets {
            mount_ids_are_ordered_subset(mountinfo_order, expected)
        } else {
            mountinfo_order == expected
        }
    }

    /// Return the guest-visible ID for `raw_mount_id`, numbering it next if
    /// this is its first observation. The pool must be `Ready`.
    fn assign(&mut self, raw_mount_id: u64) -> Option<u64> {
        let Self::Ready {
            mount_ids,
            assignment_order,
            ..
        } = self
        else {
            return None;
        };
        if let Some(virtual_mount_id) = mount_ids.get(&raw_mount_id) {
            return Some(*virtual_mount_id);
        }
        let virtual_mount_id = u64::try_from(assignment_order.len()).ok()?.checked_add(1)?;
        mount_ids.insert(raw_mount_id, virtual_mount_id);
        assignment_order.push(raw_mount_id);
        Some(virtual_mount_id)
    }

    fn determinize(&mut self, raw_mount_id: u64, mountinfo_order: Option<&[u64]>) -> Option<u64> {
        // Linux uses zero for anonymous objects such as memfd. It is one
        // equivalence class regardless of Detcore's descriptor classification.
        if raw_mount_id == 0 {
            return Some(0);
        }
        if let Some(order) = mountinfo_order {
            if !self.validate_mountinfo_order(order) {
                return None;
            }
        } else if matches!(self, Self::Uninitialized) {
            return None;
        }
        self.assign(raw_mount_id)
    }

    /// Validate one mountinfo snapshot and return the guest-visible ID of each
    /// entry of `observation_order`, numbering unseen entries in that order.
    ///
    /// `mountinfo_order` is the snapshot's canonical order, checked against
    /// the namespace order. `observation_order` is the same set of raw IDs in
    /// the order the view presents them (rows, then parent-only IDs). Only it
    /// decides numbering, so a captured namespace order that the guest cannot
    /// see never changes a guest-visible number.
    fn resolve_mountinfo(
        &mut self,
        mountinfo_order: &[u64],
        observation_order: &[u64],
    ) -> Option<Vec<u64>> {
        let entries = mountinfo_order.iter().copied().collect::<BTreeSet<_>>();
        let mut observed = BTreeSet::new();
        if entries.len() != mountinfo_order.len()
            || observation_order.len() != mountinfo_order.len()
            || !observation_order
                .iter()
                .all(|raw| entries.contains(raw) && observed.insert(*raw))
        {
            return None;
        }
        if !self.validate_mountinfo_order(mountinfo_order) {
            return None;
        }
        observation_order
            .iter()
            .map(|raw| self.assign(*raw))
            .collect()
    }

    fn provenance(&self) -> Result<Option<MountIdentityProvenance>, &'static str> {
        match self {
            Self::Uninitialized => Ok(None),
            Self::Invalid => Err("mount identity provenance is invalid"),
            Self::Ready {
                mountinfo_order,
                assignment_order,
                ..
            } => Ok(Some(MountIdentityProvenance {
                mountinfo_order: mountinfo_order.clone(),
                assignment_order: assignment_order.clone(),
            })),
        }
    }
}

impl Default for DevicePool {
    fn default() -> Self {
        DevicePool::new()
    }
}

impl DevicePool {
    fn new() -> Self {
        // Start at 1 so no file reports st_dev == 0, which some tools treat as
        // "no device".
        DevicePool {
            devices: HashMap::new(),
            next_device: 1,
        }
    }

    /// The pool of a run whose guest mount namespace is this process's: its
    /// procfs device reports Detcore's procfs device, as the procfs files
    /// Detcore renders do (<https://github.com/rrnewton/hermit/issues/4213>).
    /// Outside record and replay, the process holding the global state is in
    /// the guest's mount namespace on every backend. Replay's tracer is not,
    /// and its devices are the recording's, so record and replay number
    /// devices as before ([`Self::new`]).
    fn for_this_mount_namespace() -> Self {
        let mut pool = Self::new();
        match std::fs::metadata("/proc") {
            Ok(proc) => {
                pool.devices.insert(
                    std::os::unix::fs::MetadataExt::dev(&proc),
                    procfs_inode::VIRTUAL_PROC_DEVICE,
                );
            }
            Err(error) => warn!(
                "[detcore] /proc could not be read, so its device is numbered when first seen: {error}"
            ),
        }
        pool
    }

    /// Return the deterministic device id for `raw_device`, allocating a new one
    /// (in first-observation order) the first time a raw device is seen. The
    /// procfs device is never allocated to another device.
    fn determinize(&mut self, raw_device: u64) -> u64 {
        match self.devices.get(&raw_device) {
            Some(dev) => *dev,
            None => {
                if self.next_device == procfs_inode::VIRTUAL_PROC_DEVICE {
                    self.next_device += 1;
                }
                let new = self.next_device;
                self.next_device += 1;
                self.devices.insert(raw_device, new);
                new
            }
        }
    }
}

/// Global state associated with the detcore tool.
///
/// This is a singleton, and the one object of this type lives inside a central
/// address space, generally the "tracer" in a Reverie backend.
#[derive(Debug)]
pub struct GlobalState {
    sched: Arc<Mutex<Scheduler>>,

    inodes: Arc<Mutex<InodePool>>,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping state.
    devices: Arc<Mutex<DevicePool>>,

    /// Shared fdinfo mount-ID equivalence classes for this Detcore run.
    mount_ids: Mutex<MountIdPool>,

    // next port to use if input port is 0
    next_port: AtomicU16,

    // used ports
    used_ports: Mutex<HashSet<u16>>,

    // Unsupported syscall names observed across every process in this run.
    unsupported_syscalls: Mutex<BTreeSet<String>>,

    // Signals whose requested disposition Detcore did not install, already
    // announced in this run (`GlobalRequest::AnnounceSignalHandlerNotInstalled`).
    announced_handlers_not_installed: Mutex<BTreeSet<i32>>,
    /// The signal controls an in-guest runtime refused that this run has
    /// announced (`GlobalRequest::AnnounceRefusedSignalControls`): the signals,
    /// bit `n - 1` for signal `n`, and whether an alternate stack.
    announced_refused_signal_controls: Mutex<(u64, bool)>,

    // Why the fail-closed policy ended the run, from the Tool image that
    // refused (`GlobalRequest::PolicyRefusal`); the first report is kept.
    policy_refusal: Mutex<Option<String>>,

    // The host identity of each file the guest opened in this run, in the
    // order the opens were reported, when `Config::record_host_inputs` is set
    // (see `detcore_model::host_input`). Written to `Config::host_input_log`
    // when the run ends normally; dropped with this state otherwise.
    host_inputs: Mutex<Vec<HostInputRecord>>,

    // The paths the guest itself rebound in its file namespace in this run,
    // when `Config::record_host_inputs` is set (see
    // `detcore_model::host_input::HostMutationRecord`). Written beside
    // `host_inputs`.
    host_mutations: Mutex<BTreeSet<String>>,

    // The canonical models of open file descriptions that several guest
    // processes share (see `crate::shared_open_files`). Locked after `sched`,
    // never before it.
    shared_open_files: Arc<Mutex<SharedOpenFileStore>>,

    // Optional append-only sink shared by DBT fork descendants.
    unsupported_syscall_report_fd: Option<Mutex<File>>,

    // Open file description to bound port.
    open_file_to_port: Mutex<HashMap<OpenFileId, u16>>,

    port_start_range: AtomicU16,
    port_end_range: AtomicU16,

    // False initially after fork, and true when we begin executing the guest binary.
    past_first_execve: AtomicBool,
    /// Unlike the logging flag above, this changes only at successful image completion.
    initial_descriptor_image_complete: AtomicBool,
    /// Actual controller input, held only until the registered root image
    /// consumes it. The lease keeps its raw identity live, not guest-visible.
    inherited_input: Mutex<Option<PendingInheritedInput>>,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1154): Review the SaBRe exec descriptor-status handoff.
    /// Pre-exec identity and descriptor state awaiting a successful image transition.
    // TODO-HUMAN-REVIEW(PR-1173): Review SaBRe exec incarnation fencing.
    pending_exec_states: Arc<Mutex<BTreeMap<DetPid, PendingExecState>>>,
    // Installed only by the trusted watched native launcher, never by an RPC.
    native_exec_registry: OnceLock<Mutex<native_exec::NativeExecRegistry>>,
    /// Successful identity transfers whose RPC acknowledgment may still be in flight.
    /// Nesting either exec map with global_time requires holding sched first;
    /// the scheduler mutex serializes their inner lock orders. Process
    /// cleanup also holds sched before acquiring these stores.
    completed_exec_transfers: Arc<Mutex<BTreeMap<DetPid, exec_identity::ExecTransferReceipt>>>,

    /// Descriptor state retained after the one-shot scheduler transition is consumed.
    post_exec_fd_blocking: Arc<Mutex<BTreeMap<DetTid, ExecFdState>>>,

    /// The image each guest process executes, as its last successful exec
    /// recorded and the kernel's link confirmed (`crate::exec_record`), for
    /// another process's `/proc/<pid>/exe`. A new process starts with its
    /// creator's; the record goes with the process.
    exec_images: Arc<Mutex<BTreeMap<DetPid, crate::exec_record::ExecRecord>>>,
    /// The auxiliary vector of each guest process's current image, for its
    /// `/proc/<pid>/auxv` ([`ProcessAuxvTable`]).
    process_auxv: Arc<Mutex<ProcessAuxvTable>>,
    /// Which guest descriptor tables reference each open file description
    /// whose OFD or flock locks Detcore has read, and which locks it holds,
    /// so that `/proc/locks` keeps exactly the guest's own rows
    /// (`procfs::locks::LockLedger`). Locked after `sched`, never before it.
    lock_ledger: Mutex<crate::procfs::locks::LockLedger>,
    /// The registered root thread, whose first image
    /// `Config::launch_image` names, and whether that image was completed.
    root_dettid: OnceLock<DetTid>,
    root_image_marked: AtomicBool,
    /// The device and inode of `Config::launch_image`'s launcher, read once.
    launcher_identity: OnceLock<Option<(u64, u64)>>,

    sched_handle: Option<tokio::task::JoinHandle<()>>,

    /// Global time is a *volatile* vector clock of individual thread progress. Each
    /// thread can independently update its own progress, even (potentially) asynchronously.
    ///
    /// LockOrdering: this lock can be acquired while holding the sched lock (but not vice
    /// versa).
    //
    // TODO: it would be more future-proof to provide a non-blocking way to retrieve a
    // (nondeterministic) monotonic lower bound on global time.
    global_time: Arc<Mutex<GlobalTime>>,

    /// Just cache the config so we can access it from everywhere.
    cfg: Config,

    /// Storage for the preemption record read from `replay_preemptions_from`.
    preemptions_to_replay: Option<PreemptionReader>,

    /// The start is when we construct the global state.  Close enough.
    realtime_start: SystemTime,

    /// External network record or replay state, present only when a network
    /// trace mode is configured. Every operation reads global time under this
    /// lock, so an input's release point is fixed by the schedule alone.
    network: Option<Mutex<NetworkEngine>>,
}

/// The stores a process's physical exit must clean up, shared with the
/// GlobalState that owns them, so that the cleanup can run as one step under
/// the scheduler lock. Locked after `sched`, never before it, as every other
/// nested user of these stores does.
#[derive(Clone, Debug, Default)]
struct ProcessExitCleanup {
    shared_open_files: Arc<Mutex<SharedOpenFileStore>>,
    pending_exec_states: Arc<Mutex<BTreeMap<DetPid, PendingExecState>>>,
    completed_exec_transfers: Arc<Mutex<BTreeMap<DetPid, exec_identity::ExecTransferReceipt>>>,
    post_exec_fd_blocking: Arc<Mutex<BTreeMap<DetTid, ExecFdState>>>,
    exec_images: Arc<Mutex<BTreeMap<DetPid, crate::exec_record::ExecRecord>>>,
    process_auxv: Arc<Mutex<ProcessAuxvTable>>,
}

/// What `/proc/<pid>/auxv` reads for each guest process, on every backend
/// that reports an initial stack pointer (package K slice K2,
/// <https://github.com/rrnewton/hermit/issues/4213>): the auxiliary vector its
/// current image found on its initial stack when it was exec'd
/// (`Detcore::record_process_auxv`), as Linux's `saved_auxv`. Another
/// process's access to it is decided by the kernel beneath at the open
/// (`Detcore::ptrace_read_allowed`), so nothing about access is kept here.
///
/// One entry per live process, at most `procfs::MAX_AUXV_BYTES` (Linux's
/// `saved_auxv`, 52 words), forgotten at the process's exit. A descriptor
/// keeps the snapshot its open took, through any later exec or exit.
///
/// Not kept: the vectors of earlier images for a descriptor that a backend
/// rebuilds from its placeholder's name after an exec. Only backends that
/// rebuild descriptors that way (DBT, SaBRe, LiteInst) would need them, and
/// none of them reports an initial stack pointer, so none renders this file.
/// That path returns, with end-to-end tests, when DBT implements
/// `Guest::initial_stack_pointer`.
///
/// It also keeps the other fact a `/proc/<pid>` file Detcore renders depends
/// on: which tasks have a changed root, whose `mountinfo` and `mounts` are
/// then the kernel's view, rooted where the task is (K3a of
/// <https://github.com/rrnewton/hermit/issues/4213>). Linux keeps a task's
/// root in its `fs_struct`, which `clone(CLONE_FS)` shares between creator
/// and child (thread or process) and which a clone without it copies, so the
/// table keeps each task's filesystem group the same way ([`FsRoots`]).
#[derive(Debug, Default)]
pub(crate) struct ProcessAuxvTable {
    /// Each live process's current image's auxiliary vector.
    processes: BTreeMap<DetPid, Vec<u8>>,
    /// Each task's filesystem group, and the groups whose root changed.
    roots: FsRoots,
    /// Whether this run has warned that a backend reports no initial stack
    /// pointer, and that an initial stack held no plausible vector
    /// ([`ProcessAuxvTable::first_kernel_file`]).
    kernel_file_warned: [bool; 2],
}

/// Each task's filesystem group: the tasks that share one Linux `fs_struct`,
/// and so one root. A task is in its creator's group when it was created with
/// `CLONE_FS`, and in a new group, starting from a copy of its creator's
/// root, otherwise. `unshare(CLONE_FS)` would detach a task too, but Detcore
/// refuses every `unshare` (EPERM), so no guest task does. A task Detcore has
/// not seen created (none in practice) has an unchanged root.
#[derive(Debug, Default)]
struct FsRoots {
    /// Each task's group, by its thread number.
    groups: BTreeMap<DetPid, u64>,
    /// The groups whose root changed.
    changed: BTreeSet<u64>,
    /// The next group's number.
    next: u64,
}

impl FsRoots {
    fn new_group(&mut self, changed: bool) -> u64 {
        let group = self.next;
        self.next += 1;
        if changed {
            self.changed.insert(group);
        }
        group
    }

    fn clone_task(&mut self, parent: DetPid, child: DetPid, shares_fs: bool) {
        let group = match self.groups.get(&parent).copied() {
            Some(group) if shares_fs => group,
            parent_group => {
                let changed = parent_group.is_some_and(|group| self.changed.contains(&group));
                let group = self.new_group(changed);
                if shares_fs {
                    // A creator Detcore had not seen created shares the new
                    // group from now on.
                    self.groups.insert(parent, group);
                }
                group
            }
        };
        self.groups.insert(child, group);
    }

    fn mark_changed(&mut self, task: DetPid) {
        let group = match self.groups.get(&task).copied() {
            Some(group) => group,
            None => {
                let group = self.new_group(false);
                self.groups.insert(task, group);
                group
            }
        };
        self.changed.insert(group);
    }

    fn changed(&self, task: DetPid) -> bool {
        self.groups
            .get(&task)
            .is_some_and(|group| self.changed.contains(group))
    }

    fn transfer(&mut self, former: DetPid, current: DetPid) {
        if let Some(group) = self.groups.remove(&former) {
            self.groups.insert(current, group);
        }
    }

    fn forget(&mut self, task: DetPid) {
        if let Some(group) = self.groups.remove(&task)
            && !self.groups.values().any(|other| *other == group)
        {
            self.changed.remove(&group);
        }
    }
}

/// A process's `/proc/<pid>/auxv` ([`ProcessAuxvTable::current`]).
#[derive(PartialEq, Debug, Eq, Clone, Serialize, Deserialize)]
pub struct ProcessAuxv {
    /// The process, which a thread's number names too.
    pub process: DetPid,
    /// Its current image's auxiliary vector.
    pub bytes: Vec<u8>,
}

/// Why an image's `/proc/<pid>/auxv` is left to the kernel beneath
/// ([`ProcessAuxvTable::first_kernel_file`]).
#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum AuxvFromKernel {
    /// The backend reports no initial stack pointer.
    NoInitialStackPointer,
    /// The initial stack held no plausible auxiliary vector.
    Implausible,
}

impl ProcessAuxvTable {
    /// Record `bytes`, the auxiliary vector `process`'s new image found on its
    /// initial stack, as its current vector.
    pub(crate) fn record(&mut self, process: DetPid, bytes: Vec<u8>) {
        self.processes.insert(process, bytes);
    }

    /// `process`'s current image's auxiliary vector.
    pub(crate) fn current(&self, process: DetPid) -> Option<ProcessAuxv> {
        Some(ProcessAuxv {
            process,
            bytes: self.processes.get(&process)?.clone(),
        })
    }

    /// A new process `child` runs its creator `parent`'s image until it
    /// execs.
    pub(crate) fn inherit(&mut self, parent: DetPid, child: DetPid) {
        match self.processes.get(&parent).cloned() {
            Some(bytes) => self.processes.insert(child, bytes),
            None => self.processes.remove(&child),
        };
    }

    /// A new task `child` of `parent`: it shares `parent`'s filesystem group
    /// (its root) with `CLONE_FS`, and starts a group of its own with a copy
    /// of `parent`'s root without it, as Linux's `copy_fs` does.
    pub(crate) fn clone_root(&mut self, parent: DetPid, child: DetPid, shares_fs: bool) {
        self.roots.clone_task(parent, child, shares_fs);
    }

    /// `task` changed its root (a successful `chroot`): so did every task in
    /// its filesystem group.
    pub(crate) fn mark_changed_root(&mut self, task: DetPid) {
        self.roots.mark_changed(task);
    }

    /// Whether `task`'s root has changed.
    pub(crate) fn root_changed(&self, task: DetPid) -> bool {
        self.roots.changed(task)
    }

    /// A successful exec by the non-leader thread `former` gave it its
    /// process's number `current` (Linux's `de_thread`); its filesystem group
    /// goes with it. An exec does not detach a shared filesystem group:
    /// Linux's `check_unsafe_exec` only marks such an exec unsafe for set-id
    /// programs, and `begin_new_exec` unshares the descriptor table, not
    /// `fs_struct`.
    pub(crate) fn transfer_root(&mut self, former: DetPid, current: DetPid) {
        self.roots.transfer(former, current);
    }

    /// `process` is gone, or its new image has no vector Detcore recorded
    /// (whatever it held before the exec, its own earlier image's or its
    /// creator's, is stale), so its file is the kernel's. Descriptors already
    /// open keep their snapshots.
    pub(crate) fn forget(&mut self, process: DetPid) {
        self.processes.remove(&process);
    }

    /// `process` is gone: none of its facts stay.
    pub(crate) fn forget_process(&mut self, process: DetPid) {
        self.processes.remove(&process);
        self.roots.forget(process);
    }

    /// How many processes have a current vector.
    #[cfg(test)]
    pub(crate) fn len(&self) -> usize {
        self.processes.len()
    }

    /// Whether this is the run's first image whose `/proc/<pid>/auxv` is left
    /// to the kernel beneath for `why`, which the run warns of once.
    pub(crate) fn first_kernel_file(&mut self, why: AuxvFromKernel) -> bool {
        let index = match why {
            AuxvFromKernel::NoInitialStackPointer => 0,
            AuxvFromKernel::Implausible => 1,
        };
        !std::mem::replace(&mut self.kernel_file_warned[index], true)
    }
}

impl ProcessExitCleanup {
    /// Complete the exit `watch` observed, unless a later registration has
    /// replaced it. Nothing calls this in production yet; the daemon-point
    /// observation of <https://github.com/rrnewton/hermit/issues/4362> part 2
    /// will (2ee7190125's free-running observer was reverted).
    #[cfg_attr(not(test), allow(dead_code))]
    fn complete_observed_locked(
        &self,
        sched: &mut Scheduler,
        watch: &Arc<crate::scheduler::ProcessExitWatch>,
    ) -> Option<(bool, Option<String>)> {
        if !sched.process_exit_watch_is_current(watch) {
            return None;
        }
        sched.finish_process_exit_watch(watch);
        Some(self.complete_locked(sched, watch.process, true))
    }

    fn complete_locked(
        &self,
        sched: &mut Scheduler,
        process: DetPid,
        consume_unreported: bool,
    ) -> (bool, Option<String>) {
        let loss = (consume_unreported && sched.has_unreported_exit(process)).then(|| {
            let reason = format!(
                "process {process} exited without deregistering; its exit was not scheduled"
            );
            // Publish refusal before cancellation can wake a request. Normal
            // deregistration and this entire operation share the sched lock.
            crate::detlog::record_determinism_loss(&reason);
            reason
        });
        self.pending_exec_states.lock().unwrap().remove(&process);
        self.completed_exec_transfers
            .lock()
            .unwrap()
            .remove(&process);
        self.post_exec_fd_blocking.lock().unwrap().remove(&process);
        self.exec_images.lock().unwrap().remove(&process);
        self.process_auxv.lock().unwrap().forget_process(process);
        if consume_unreported {
            let consumed = sched.consume_unreported_exit(process);
            debug_assert_eq!(consumed, loss.is_some());
        }
        self.shared_open_files
            .lock()
            .unwrap()
            .end_leases_of(process);
        (sched.complete_physical_process_exit(process), loss)
    }
}

impl Default for GlobalState {
    fn default() -> Self {
        // TODO(T77816673): eventually we want to remove this requirement.
        // In the meantime... just don't call this.
        panic!("Detcore GlobalState Default impl should not be called");
    }
}

impl Drop for GlobalState {
    fn drop(&mut self) {
        // TODO-HUMAN-REVIEW(PR-643): Review shutdown-time aggregate warning delivery.
        if let Some(message) =
            format_unsupported_syscall_warning(&self.unsupported_syscalls.lock().unwrap())
        {
            warn!("{}", message);
        }
        info!("detcore shut down, destroying global state");
    }
}

impl GlobalState {
    /// Transfer the completed run's diagnostic aggregate to its output owner.
    /// The backend must first reclaim exclusive ownership after all RPC clients
    /// have drained. Callers that do not take it retain the ordinary Drop warning.
    pub fn take_unsupported_syscalls(&mut self) -> BTreeSet<String> {
        std::mem::take(self.unsupported_syscalls.get_mut().unwrap())
    }

    /// Ordinary RPC mutation must linearize before terminal publication under
    /// the same mutex as scheduler grants. A losing callback stays pending for
    /// the backend's failure subscription to drop; no normal reply is invented.
    /// Consuming exit RPCs retain their existing validation/accounting path.
    async fn lock_rpc_scheduler(
        &self,
        consuming_cleanup: bool,
    ) -> std::sync::MutexGuard<'_, Scheduler> {
        std::future::poll_fn(|_| {
            let sched = self.sched.lock().unwrap();
            if !consuming_cleanup && sched.backend_failed() {
                Poll::Pending
            } else {
                Poll::Ready(sched)
            }
        })
        .await
    }

    /// The deterministic numbers of the files whose last name the run removed
    /// while they were numbered and that no later name showed to be reused
    /// (see `InodePool::retire`), in increasing order. Diagnostic, for tests
    /// of the removal paths (<https://github.com/rrnewton/hermit/issues/3840>).
    pub fn retired_inodes(&self) -> Vec<DetInode> {
        let mut retired: Vec<DetInode> = self
            .inodes
            .lock()
            .unwrap()
            .retired
            .values()
            .copied()
            .collect();
        retired.sort();
        retired
    }

    /// How many retired inode numbers a later name or `open` showed the host
    /// had given to another file. Diagnostic, for tests that make a host reuse
    /// an inode (<https://github.com/rrnewton/hermit/issues/3840>).
    pub fn reused_inode_count(&self) -> u64 {
        self.inodes.lock().unwrap().reused
    }

    /// Return the producer-observed mount identity order after a run.
    ///
    /// The first vector is the exact mountinfo row/parent order. The second is
    /// the order in which the run numbered raw mount IDs.
    pub fn mount_identity_provenance(
        &self,
    ) -> Result<Option<MountIdentityProvenance>, &'static str> {
        self.mount_ids.lock().unwrap().provenance()
    }

    /// A global state with no scheduler task, for unit tests elsewhere in the
    /// crate that send RPCs to it directly.
    #[cfg(test)]
    pub(crate) fn for_tests(cfg: &Config) -> Self {
        Self::initialize(cfg, false)
    }

    fn initialize(cfg: &Config, spawn_scheduler: bool) -> Self {
        let sched = Arc::new(Mutex::new(Scheduler::new(cfg)));
        let global_time = Arc::new(Mutex::new(GlobalTime::new(cfg)));
        let process_exit_cleanup = ProcessExitCleanup::default();
        let handle = if cfg.sequentialize_threads && spawn_scheduler {
            // Announce before spawning, not from inside the spawned task. The
            // task's first poll is unordered with respect to the rest of this
            // bootstrap, so emitting there raced the root thread's seeding
            // lines and produced a nondeterministic INFO stream. Emitting here
            // sequences it: `Scheduler::new`'s SCHEDRAND line above, then this,
            // then the root `ThreadState::new` seeding lines.
            info!("[scheduler] daemon task starting up, waiting for guest thread start..");
            Some(tokio::spawn(sched_loop(sched.clone(), global_time.clone())))
        } else {
            None
        };

        let preemptions_to_replay: Option<PreemptionReader> = cfg
            .replay_preemptions_from
            .as_ref()
            .map(|path| PreemptionReader::new(path));
        let range = Self::read_port_range();
        let network = Self::network_engine(cfg).map(Mutex::new);

        let unsupported_syscall_report_fd = cfg.unsupported_syscall_report_fd.and_then(|fd| {
            // This writer is internal controller state. In an in-process DBT
            // runtime it must not leak into the next guest image across exec
            // (hence F_DUPFD_CLOEXEC), and it must not perturb the descriptor
            // namespace the *current* guest observes. The backend places the
            // report fd itself high, out of the guest's working range (e.g. 199
            // for the DBT backend). Duplicating with a min hint of `fd` keeps
            // this private copy up in that same reserved band instead of
            // grabbing the lowest free descriptor (fd 3), which would shift
            // every fd the guest subsequently opens and diverge from the golden
            // ptrace reference (where this fd is unset and no dup happens).
            let duplicate = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, fd) };
            if duplicate == -1 {
                warn!(
                    "failed to duplicate unsupported-syscall report fd {fd}: {}",
                    std::io::Error::last_os_error()
                );
                None
            } else {
                // SAFETY: dup returned a new owned descriptor.
                Some(Mutex::new(unsafe { File::from_raw_fd(duplicate) }))
            }
        });

        Self {
            sched,
            next_port: AtomicU16::new(range[0]),
            used_ports: Mutex::new(HashSet::new()),
            unsupported_syscalls: Mutex::new(BTreeSet::new()),
            announced_handlers_not_installed: Mutex::new(BTreeSet::new()),
            announced_refused_signal_controls: Mutex::new((0, false)),
            policy_refusal: Mutex::new(None),
            host_inputs: Mutex::new(Vec::new()),
            host_mutations: Mutex::new(BTreeSet::new()),
            shared_open_files: process_exit_cleanup.shared_open_files,
            unsupported_syscall_report_fd,
            port_start_range: AtomicU16::new(range[0]),
            port_end_range: AtomicU16::new(range[1]),
            open_file_to_port: Mutex::new(HashMap::new()),
            past_first_execve: AtomicBool::new(false),
            initial_descriptor_image_complete: AtomicBool::new(false),
            inherited_input: Mutex::new(None),
            pending_exec_states: process_exit_cleanup.pending_exec_states,
            completed_exec_transfers: process_exit_cleanup.completed_exec_transfers,
            post_exec_fd_blocking: process_exit_cleanup.post_exec_fd_blocking,
            exec_images: process_exit_cleanup.exec_images,
            process_auxv: process_exit_cleanup.process_auxv,
            lock_ledger: Mutex::new(Default::default()),
            root_dettid: OnceLock::new(),
            root_image_marked: AtomicBool::new(false),
            launcher_identity: OnceLock::new(),
            native_exec_registry: OnceLock::new(),
            inodes: Arc::new(Mutex::new(InodePool::new())),
            // AUTONOMOUS-BOT-IMPLEMENTED
            // TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping state.
            devices: Arc::new(Mutex::new(if cfg.recordreplay_modes {
                DevicePool::new()
            } else {
                DevicePool::for_this_mount_namespace()
            })),
            mount_ids: Mutex::new(MountIdPool::from_config(
                &cfg.mountinfo_mount_ids,
                cfg.mountinfo_mount_ids_captured,
                &cfg.mount_id_assignment_order,
            )),
            sched_handle: handle,
            cfg: cfg.clone(),
            realtime_start: SystemTime::now(),
            global_time,
            preemptions_to_replay,
            network,
        }
    }

    /// The network engine for the configured trace mode. The CLI validates a
    /// replay trace before launch; failing here is a backstop.
    fn network_engine(cfg: &Config) -> Option<NetworkEngine> {
        let path = cfg.network_trace.path.as_ref();
        match cfg.network_trace.mode {
            NetworkTraceMode::Off => None,
            NetworkTraceMode::Record => Some(NetworkEngine::new_record(cfg.epoch)),
            NetworkTraceMode::Replay => {
                let path = path.expect("network trace replay requires a trace path");
                let trace = File::open(path)
                    .map_err(|error| error.to_string())
                    .and_then(|mut file| {
                        NetworkTraceV2::read_framed(&mut file).map_err(|error| error.to_string())
                    })
                    .unwrap_or_else(|error| {
                        panic!("cannot read network trace {}: {error}", path.display())
                    });
                Some(NetworkEngine::new_replay(trace))
            }
        }
    }

    /// Write the network trace of a record run, or check that a replay used
    /// the whole recording. Returns the refusal a replay ends with.
    fn finish_network_trace(&mut self) -> Option<String> {
        let engine = self.network.take()?;
        let engine = engine.into_inner().unwrap();
        if !engine.is_record() {
            return engine
                .finish_replay()
                .err()
                .map(|error| format!("hermit: {error}. {}", error.remedy()));
        }
        let path = self
            .cfg
            .network_trace
            .path
            .as_ref()
            .expect("network trace recording requires a trace path");
        // Encode before opening the output, so a trace that cannot be encoded
        // leaves the file as it was instead of truncated.
        let result = engine
            .finish()
            .map_err(|error| error.to_string())
            .and_then(|trace| trace.encode_framed().map_err(|error| error.to_string()))
            .and_then(|bytes| {
                let mut file = File::create(path).map_err(|error| error.to_string())?;
                file.write_all(&bytes).map_err(|error| error.to_string())
            });
        if let Err(error) = result {
            panic!("cannot write network trace {}: {error}", path.display());
        }
        None
    }

    /// Initializes global state whose sequential scheduler is driven by an
    /// external backend executor.
    pub fn init_for_external_scheduler(cfg: &Config) -> Self {
        assert!(
            cfg.sequentialize_threads,
            "an external scheduler is only meaningful when threads are sequentialized"
        );
        Self::initialize(cfg, false)
    }

    /// Returns the sequential scheduler loop for a backend-owned executor to run.
    ///
    /// This is deliberately not an `async fn`: the announcement is logged when
    /// this is called, not when the returned future is first polled. A backend
    /// can then order it ahead of anything it releases before polling (DBT
    /// publishes `READY_IMAGE`, which lets the root guest thread log its
    /// seeding lines). See <https://github.com/rrnewton/hermit/issues/3463>.
    pub fn run_external_scheduler(
        &self,
        observer: Arc<dyn Fn(&'static str) + Send + Sync>,
    ) -> impl Future<Output = ()> + use<> {
        // Emitted at the call site for the same reason as the spawned path in
        // `initialize`, so both ways of starting the daemon place this line at
        // a deterministic point in the caller's program order.
        info!("[scheduler] daemon task starting up, waiting for guest thread start..");
        sched_loop_external(self.sched.clone(), self.global_time.clone(), observer)
    }

    /// Reports that a backend supervisor received a process's final kernel exit status.
    ///
    /// This records a barrier observation when the backend advertises physical-exit
    /// reporting or asynchronous exit completion; it is a no-op for ptrace, DBT and KVM.
    /// The exact process's barrier is released at this physical-waitability boundary.
    ///
    /// With asynchronous exit completion, registrations the process did not deregister
    /// itself are retired here first, in the same locked step, and a determinism loss is
    /// recorded: such a process died without finishing its exit hook, at a moment the host
    /// chose, so the run must not be compared.
    pub fn complete_physical_process_exit(&self, raw_pid: i32) {
        let detpid = DetPid::from_raw(raw_pid);
        let (completed, loss) = {
            let mut sched = self.sched.lock().unwrap();
            self.process_exit_cleanup().complete_locked(
                &mut sched,
                detpid,
                self.cfg.backend.process_exits_complete_asynchronously,
            )
        };
        if completed {
            trace!(
                "[detcore, dpid {}] backend completed final physical process exit",
                detpid
            );
        }
        if let Some(loss) = loss {
            crate::detlog::write_loss_notice(&loss);
        }
    }

    fn process_exit_cleanup(&self) -> ProcessExitCleanup {
        ProcessExitCleanup {
            shared_open_files: self.shared_open_files.clone(),
            pending_exec_states: self.pending_exec_states.clone(),
            completed_exec_transfers: self.completed_exec_transfers.clone(),
            post_exec_fd_blocking: self.post_exec_fd_blocking.clone(),
            exec_images: self.exec_images.clone(),
            process_auxv: self.process_auxv.clone(),
        }
    }

    /// The device and inode of the launcher `Config::launch_image` names,
    /// the file the kernel's `exe` link names in a launched guest's place.
    fn launcher_identity(&self) -> Option<(u64, u64)> {
        *self.launcher_identity.get_or_init(|| {
            use std::os::unix::fs::MetadataExt;
            let launcher = self.cfg.launch_image.as_ref()?.launcher.as_ref()?;
            let meta = std::fs::metadata(launcher).ok()?;
            Some((meta.dev(), meta.ino()))
        })
    }

    /// Answers a control message about a shared open file description. Only
    /// the thread that holds the scheduler's serial grant may send one, so
    /// access to a shared description is exclusive and happens in canonical
    /// order (`crate::shared_open_files`).
    ///
    /// A refusal is an infrastructure failure, not something the guest's
    /// syscall can report: the caller's process stops, and this reports a
    /// backend failure, which ends the whole run before any further grant.
    fn shared_open_file_control(
        &self,
        thread: DetTid,
        control: OpenFileControl,
    ) -> Result<OpenFileControlReply, SharedOpenFileError> {
        // The refusal and the failure it reports are one locked step, so the
        // scheduler can grant no other thread in between.
        let (result, wakes) = {
            let mut sched = self.sched.lock().unwrap();
            let process = sched.registered_process(thread);
            let result = if !sched.holds_serial_grant(thread) {
                Err(SharedOpenFileError(format!(
                    "thread {thread} sent {control:?} without holding the serial grant"
                )))
            } else if let Some(process) = process {
                self.shared_open_files
                    .lock()
                    .unwrap()
                    .handle(process, thread, control)
            } else {
                Err(SharedOpenFileError(format!(
                    "thread {thread} belongs to no registered process"
                )))
            };
            let wakes = result.as_ref().err().map(|error| {
                tracing::error!("[detcore] shared open file control refused: {error}");
                let wake = sched.report_backend_failure(reverie::BackendFailure {
                    pid: reverie::Pid::from_raw(process.unwrap_or(thread).as_raw()),
                    tid: Tid::from_raw(thread.as_raw()),
                    phase: "shared open file control",
                });
                (wake, sched.take_signal_failure_wakes())
            });
            (result, wakes)
        };
        // Notified after the lock is released, as `report_backend_failure`
        // does.
        if let Some((wake, deferred)) = wakes {
            for wake in deferred {
                let _ = wake.send(());
            }
            if let Some(wake) = wake {
                let _ = wake.send(());
            }
        }
        result
    }

    /// Releases all physical-process-exit barriers after a backend supervisor has drained every
    /// tracee and no guest thread can race another lifecycle event.
    pub fn release_all_physical_process_exits(&self) {
        self.pending_exec_states.lock().unwrap().clear();
        self.completed_exec_transfers.lock().unwrap().clear();
        self.post_exec_fd_blocking.lock().unwrap().clear();
        self.exec_images.lock().unwrap().clear();
        *self.process_auxv.lock().unwrap() = ProcessAuxvTable::default();
        let released = self
            .sched
            .lock()
            .unwrap()
            .release_all_physical_process_exits();
        if released != 0 {
            trace!("released {released} final physical process-exit barrier(s)");
        }
    }

    /// Why the fail-closed policy ended the run, when a Tool image reported
    /// it before exiting (`GlobalRequest::PolicyRefusal`).
    pub fn policy_refusal(&self) -> Option<String> {
        self.policy_refusal.lock().unwrap().clone()
    }

    /// Unrecoverable fatal erorr. Bring things to a close cleanly, but as quickly as
    /// possible.
    pub fn force_shutdown_with_error(&self) {
        let start = std::time::Instant::now();
        let sched = loop {
            if start.elapsed().as_millis() > 1000 {
                eprintln!(
                    "Could not acquire scheduler lock during forced shutdown (timeout)... proceeding anyway."
                );
                return;
            }
            match self.sched.try_lock() {
                Ok(guard) => {
                    break guard;
                }
                Err(std::sync::TryLockError::WouldBlock) => {
                    std::thread::yield_now();
                    continue;
                }
                Err(e) => {
                    eprintln!(
                        "Could not acquire scheduler lock during forced shutdown ({})... proceeding anyway.",
                        e
                    );
                    return;
                }
            }
        };
        info!("Scheduler state at exit:\n{}", sched.full_summary());
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-744): Review explicit abnormal-backend scheduler cancellation.
    /// Cancels the internally spawned scheduler task after a backend guest exits abnormally.
    ///
    /// External-scheduler states do not own a task and are left unchanged.
    pub async fn cancel_internal_scheduler(&mut self) {
        if let Some(handle) = self.sched_handle.take() {
            handle.abort();
            match handle.await {
                Ok(()) => {}
                Err(error) if error.is_cancelled() => {}
                Err(error) => panic!("cancelled scheduler task panicked: {error}"),
            }
        }
    }

    /// Consume failed-run state after the scheduler has naturally finished.
    ///
    /// A failed run has no successful run summary. Preserve the scheduler's
    /// join error and any requested partial preemption recording's write error
    /// for the caller, without allowing either to replace the backend failure.
    pub async fn clean_up_after_backend_failure(mut self) -> BackendFailureCleanup {
        let scheduler = if let Some(handle) = self.sched_handle.take() {
            handle.await
        } else {
            Ok(())
        };
        // A scheduler panic can poison this mutex. Its JoinError is returned
        // below; recovering only to finish output must not replace that error.
        let writer = self
            .sched
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .preemption_writer
            .take();
        let preemption_recording = if self.cfg.record_preemptions_to.is_some() {
            writer.map_or(Ok(()), |writer| writer.flush())
        } else {
            // The writer's destination is fixed from this same configuration.
            // In-memory-only recordings have no Drop write to finish.
            drop(writer);
            Ok(())
        };
        BackendFailureCleanup {
            scheduler,
            preemption_recording,
        }
    }

    /// Shut down anything running, in particular wait on the scheduler.
    ///
    /// This is basically the destructor for the global state, but is here rather than in the
    /// Drop instance so that it can be async, and is more explicitly sequenced in the program.
    ///
    /// Print a summary of the execution, typically called when it is complete.
    ///
    /// If the boolean argument is true, print to stderr, otherwise only print the summary
    /// to the log.
    pub async fn clean_up(self, to_stderr: bool, print_summary_to_json_file: &Option<PathBuf>) {
        self.clean_up_with_dispatch_stats(to_stderr, print_summary_to_json_file, None)
            .await
    }

    /// [`Self::clean_up`], also carrying the backend's dispatch record into the
    /// machine-readable summary. The human-readable report, including the INFO
    /// view `--verify` compares, never shows it.
    pub async fn clean_up_with_dispatch_stats(
        mut self,
        to_stderr: bool,
        print_summary_to_json_file: &Option<PathBuf>,
        dispatch_stats: Option<reverie::DispatchStats>,
    ) {
        if let Some(handle) = self.sched_handle.take() {
            debug!("Global state cleanup, confirming scheduler has shut down...");
            handle.await.expect("Global scheduler clean shutdown");
            debug!("Global state cleanup, continuing...");
        }
        let network_refusal = self.finish_network_trace();
        let banner =
            "  ------------------------------ hermit run report ------------------------------";
        let recording_destination = self.cfg.record_preemptions_to.clone();
        // The run is over: write what it opened for --verify.
        if let Some(log) = &self.cfg.host_input_log {
            crate::host_inputs::write(
                log,
                &self.host_inputs.lock().unwrap(),
                &self.host_mutations.lock().unwrap(),
            );
        }
        let (mut summary, info_reprio_descrip) = self.into_run_summary_for_log().unwrap();
        summary.dispatch_stats = dispatch_stats;

        // Print machine-readable summary:
        if let Some(path) = print_summary_to_json_file {
            let json = serde_json::to_string_pretty(&summary).unwrap();
            fs::write(path, json + "\n").unwrap();
        }

        // Print human-readable summary:
        if to_stderr {
            // In this case, print summary irrespective of logging level.
            // TODO: output summary in machine-readable, JSON form.
            //
            // NOT `eprint!`: a guest that set O_NONBLOCK on the inherited fd 2
            // makes `write_all` fail with EAGAIN here, which panics the print
            // macro and loses both the summary and the panic message. See
            // `crate::util::RetryingStderr`.
            {
                use std::io::Write;
                let _ = write!(crate::util::RetryingStderr, "{}\n{}", banner, summary);
            }
        } else {
            // Separate out the nondeterministic bits and print them at debug level:
            let rt = summary.realtime_elapsed.take();
            log_run_summary(
                banner,
                &summary,
                info_reprio_descrip.as_deref(),
                recording_destination.as_deref(),
            );
            if let Some(x) = rt {
                debug!("Nondeterministic realtime elapsed: {:?}", x);
            }
        }
        // After the report, so a refused replay still describes the run.
        if let Some(refusal) = network_refusal {
            {
                use std::io::Write;
                let _ = writeln!(crate::util::RetryingStderr, "{refusal}");
            }
            std::process::exit(detcore_model::HERMIT_POLICY_REFUSAL_EXIT);
        }
    }

    #[cfg(test)]
    fn into_run_summary(self) -> anyhow::Result<RunSummary> {
        self.into_run_summary_for_log().map(|(summary, _)| summary)
    }

    fn into_run_summary_for_log(self) -> anyhow::Result<(RunSummary, Option<String>)> {
        // First, the scheduler can generate part of the summary (and flush once).
        let (mut summary, info_reprio_descrip) = {
            let mut sched = self.sched.lock().unwrap();
            sched.generate_partial_run_summary_for_log(self.cfg.record_preemptions_to.as_ref())?
        };
        // Second, we fill in the rest based on global state.
        // Record configuration and the runtime's timer mechanism, not whether
        // a timer is currently armed or has delivered an event. This metadata
        // stays separate from the INFO guest-behavior view.
        summary.preemption = Some(PreemptionSummary {
            max_timeslice_ns: self.cfg.max_timeslice.map(|maximum| maximum.get()),
            timer_enabled: Some(self.cfg.use_preemption_timer()),
        });
        //
        // Real time report:
        // N.B.: We don't have a job-level exit hook atm (T76248597), so we use the
        // CURRENT time -- that we are calling summarize -- as the end time:
        summary.realtime_elapsed = Some(self.realtime_start.elapsed()?);

        if self.cfg.virtualize_time {
            let final_time = self.global_time.lock().unwrap();
            summary.virttime_final = final_time.as_nanos().as_nanos();
            summary.virttime_elapsed = match final_time.elapsed_nanos() {
                Some(elapsed) => elapsed.as_nanos(),
                None => bail!(
                    "Internal invariant violated! Global time {} is before its epoch baseline",
                    final_time.as_nanos()
                ),
            };
        }

        Ok((summary, info_reprio_descrip))
    }
}

fn log_run_summary(
    banner: &str,
    summary: &RunSummary,
    info_reprio_descrip: Option<&str>,
    recording_destination: Option<&std::path::Path>,
) {
    info!("\n{}\n{}", banner, summary.info(info_reprio_descrip));
    debug!(
        replayed_events = summary.schedevent_replayed,
        ?recording_destination,
        "Run recording/replay bookkeeping"
    );
}

#[reverie::global_tool]
impl GlobalTool for GlobalState {
    type Config = Config;

    /// A request asks the scheduler to perform an RPC, which includes multiple kinds of
    /// actions, and, most importantly, permission to acquire resources and run the guest thread.
    ///
    /// Irrespective of which method we execute, we can "tick" our local component of the
    /// global time in the process.
    type Request = (DetTime, MmId, GlobalRequest);

    /// Response from the global portion of the Detcore instrumentation tool.
    /// The exact form of the response depends on which method was executed.
    ///
    /// Irrespective of which method was called, the global handling may have consumed
    /// logical time, in which case the scheduler can send a new thread-local time back to
    /// the caller.  Unfortunately, information is lost as this is collapsed to a flat
    /// scalar instead of a rich `DetTime`.
    type Response = (Option<LogicalTime>, GlobalResponse);

    /// Called once during startup.
    async fn init_global_state(cfg: &Config) -> GlobalState {
        // A backend whose guest thread IDs are not host tasks never reads a
        // host task's `/proc` signal state for them (`read_kernel_signal_state`).
        crate::syscalls::refuse_host_signal_state_reads(&cfg.backend);
        GlobalState::initialize(cfg, true)
    }

    fn install_inherited_input(&mut self, input: reverie::InheritedInputLease) {
        // Called once by the launcher before this global state is published.
        // All fallible endpoint/lease creation happened before initialization.
        *self.inherited_input.get_mut().unwrap() = Some(PendingInheritedInput {
            lease: input,
            root: None,
        });
    }

    fn install_backend_signal_control(
        &self,
        control: Option<reverie::BackendSignalControl>,
    ) -> Result<reverie::BackendSignalControlMode, reverie::Error> {
        self.sched
            .lock()
            .unwrap()
            .install_signal_control(control, self.cfg.sequentialize_threads)
    }

    /// Dequeues carry real-timer signals only in a run that installed the
    /// backend's control; any other run leaves them to the guest kernel.
    fn observe_signal_dequeues(&self, mode: reverie::BackendSignalControlMode) -> bool {
        mode == reverie::BackendSignalControlMode::ToolControlled
    }

    fn authorize_backend_signal_boundary(
        &self,
        task: reverie::SignalTaskIdentity,
    ) -> Result<Option<reverie::SignalDeliveryPermit>, reverie::Error> {
        self.sched.lock().unwrap().authorize_signal_boundary(task)
    }

    async fn on_backend_signal_boundary(
        &self,
        receipt: reverie::SignalBoundaryReceipt,
    ) -> Result<(), reverie::Error> {
        self.sched.lock().unwrap().consume_signal_boundary(receipt)
    }

    fn on_backend_process_retired(
        &self,
        event: reverie::BackendProcessRetirement,
    ) -> Result<(), reverie::Error> {
        let (result, wakes) = {
            let mut sched = self.sched.lock().unwrap();
            let result = sched.consume_process_retirement(event);
            (result, sched.take_signal_failure_wakes())
        };
        for wake in wakes {
            let _ = wake.send(());
        }
        result
    }

    /// A backend whose process exits complete asynchronously reports each
    /// physical exit here; see [`GlobalState::complete_physical_process_exit`].
    fn on_backend_process_exited(&self, pid: i32) {
        if self.cfg.backend.reports_child_exit_publication {
            // The kernel has published this process's exit to its parent. It
            // is not a physical-exit barrier report on this backend.
            let released = self
                .sched
                .lock()
                .unwrap()
                .complete_child_exit_publication(DetPid::from_raw(pid));
            // This branch bypasses `ProcessExitCleanup::complete_locked`: the
            // process's `/proc/<pid>/auxv` vector goes here. A zombie's file
            // is served on the rendered path whether or not its vector is
            // still kept (`Detcore::auxv_kept_by_kernel`), so the host-timed
            // moment of this report changes nothing the guest sees.
            self.process_auxv
                .lock()
                .unwrap()
                .forget_process(DetPid::from_raw(pid));
            if released {
                trace!("[detcore, dpid {}] backend published the child exit", pid);
            }
        } else {
            self.complete_physical_process_exit(pid);
        }
    }

    /// The processes held between their exit grant and their physical exit,
    /// for the backend's watchdog.
    fn backend_pending_process_exits(&self) -> Vec<i32> {
        self.sched
            .lock()
            .unwrap()
            .pending_async_process_exits()
            .into_iter()
            .map(|detpid| detpid.as_raw())
            .collect()
    }

    fn on_fatal_signal_exit(&self, exit: &reverie::FatalSignalExit) {
        if let Some(capture) = &self.cfg.fatal_core_capture {
            crate::fatal_core::capture_and_log(capture, exit);
        }
    }

    fn report_backend_failure(&self, event: reverie::BackendFailure) {
        let (wake, deferred) = {
            let mut sched = self.sched.lock().unwrap();
            (
                sched.report_backend_failure(event),
                sched.take_signal_failure_wakes(),
            )
        };
        for wake in deferred {
            let _ = wake.send(());
        }
        if let Some(wake) = wake {
            // No waiter can begin consuming cleanup until the scheduler has
            // closed its selected transaction under the grant/commit mutex.
            let _ = wake.send(());
        }
    }

    async fn wait_for_backend_failure(&self) {
        let wake = self.sched.lock().unwrap().backend_failure_waiter();
        wake.await
            .expect("GlobalState owns the failure sender until publication");
    }

    async fn on_backend_child_wait_event(
        &self,
        event: reverie::BackendChildWaitEvent,
    ) -> Result<(), reverie::Error> {
        // KVM polls this future through its first suspension before exposing
        // waitability. The helper's first poll performs the complete
        // generation-bound publication under the scheduler mutex, then
        // self-wakes and returns Pending; its second poll consumes the typed
        // result and releases the stage-2 control barrier.
        crate::scheduler::signal_control::ChildExitPublicationFuture::new(self.sched.clone(), event)
            .await
    }

    async fn receive_rpc(&self, from: Tid, gr: Self::Request) -> Self::Response {
        if let GlobalRequest::CheckBrkMappings(check, fact) = &gr.2 {
            return (
                None,
                GlobalResponse::CheckBrkMappings(
                    self.capture_brk_mappings(from, gr.1, *check, *fact),
                ),
            );
        }
        // Intercept even malformed counted envelopes before taking a deadline
        // hint. Native controls belong to the already owned exec cut.
        if native_exec::is_native_exec_control(&gr.2) {
            return self.dispatch_native_exec_control(from, gr);
        }
        // The deadline hint of https://github.com/rrnewton/hermit/issues/4121
        // rides on whichever reply resumes the thread after its grant: an
        // ordinary or parked resource grant, a futex wake, a parent's
        // continuation, thread start, the exec continuation. Clearing on
        // arrival means a reply carries only a hint recorded while its own
        // request was pending (a grant, or a timer the request armed).
        // Only a request whose reply resumes the thread from a grant, or arms
        // a timer, touches the hint: control messages sent on another task's
        // behalf must neither consume nor carry it.
        if !resumes_from_grant(&gr.2) {
            return self.dispatch_rpc(from, gr).await;
        }
        let dtid = DetTid::from_raw(from.as_raw());
        self.sched.lock().unwrap().take_deadline_hint(dtid);
        let (time, response) = self.dispatch_rpc(from, gr).await;
        let hint = self.sched.lock().unwrap().take_deadline_hint(dtid);
        match hint {
            Some(remaining)
                if response != GlobalResponse::ThreadExited
                    && response != GlobalResponse::Retired =>
            {
                (
                    time,
                    GlobalResponse::WithDeadline(remaining, Box::new(response)),
                )
            }
            _ => (time, response),
        }
    }
}

impl GlobalState {
    /// Evaluate a brk predicate in the controller's unchanged procfs view.
    /// The scheduler lock retains the caller's grant and physical identity
    /// until all temporary descriptors have closed. The backend fact excludes
    /// native tasks that the scheduler cannot stop; it is not inferred here.
    fn capture_brk_mappings(
        &self,
        from: Tid,
        mm: MmId,
        check: crate::syscalls::memory::BrkMappingCheck,
        fact: reverie::ControllerCaptureFact,
    ) -> Option<bool> {
        use std::io::Read;
        use std::os::fd::AsRawFd;
        use std::os::unix::fs::MetadataExt;

        if !self.cfg.sequentialize_threads {
            return None;
        }
        let sched = self.sched.lock().unwrap();
        let dettid = DetTid::from_raw(from.as_raw());
        let (pid, tid) = sched.controller_capture_task(dettid, mm, fact)?;
        let root = File::open("/proc").ok()?;
        let metadata = root.metadata().ok()?;
        if !metadata.is_dir()
            || metadata.dev() != fact.procfs_device
            || metadata.ino() != fact.procfs_inode
        {
            return None;
        }
        let mut filesystem = std::mem::MaybeUninit::<libc::statfs>::uninit();
        // SAFETY: root is live and filesystem has space for the complete result.
        if unsafe { libc::fstatfs(root.as_raw_fd(), filesystem.as_mut_ptr()) } != 0
            || unsafe { filesystem.assume_init() }.f_type != libc::PROC_SUPER_MAGIC
        {
            return None;
        }
        // A live nonleader remains addressable by its TID after its leader
        // exits, when the leader's /task directory may no longer be readable.
        let relative = std::ffi::CString::new(format!("{tid}/smaps")).ok()?;
        // SAFETY: both the directory and nul-terminated relative path are live.
        let raw = unsafe {
            libc::openat(
                root.as_raw_fd(),
                relative.as_ptr(),
                libc::O_RDONLY | libc::O_CLOEXEC,
            )
        };
        if raw < 0 {
            return None;
        }
        // SAFETY: openat returned a new descriptor exclusively owned here.
        let mut smaps_file = unsafe { File::from_raw_fd(raw) };
        let mut smaps = Vec::new();
        smaps_file.read_to_end(&mut smaps).ok()?;
        // External death cannot turn a partial or former task's capture into
        // authority for a replacement. The registered pidfd is still held.
        if sched.controller_capture_task(dettid, mm, fact) != Some((pid, tid)) {
            return None;
        }
        Some(check.allows(&smaps))
        // smaps_file and root close before sched releases the grant guard and
        // before the caller receives the fixed, allocation-free answer.
    }

    /// Prints, once per run, a WARN for each signal whose handler an in-guest
    /// runtime refused and one for a refused alternate stack
    /// (`crate::refused_signal_controls`), whichever process reports it.
    fn announce_refused_signal_controls(&self, actions: u64, alternate_stack: bool) {
        let mut announced = self.announced_refused_signal_controls.lock().unwrap();
        let new_actions = actions & !announced.0;
        announced.0 |= actions;
        let new_stack = alternate_stack && !announced.1;
        announced.1 |= alternate_stack;
        drop(announced);
        for signum in (1..=64).filter(|signum| new_actions & (1 << (signum - 1)) != 0) {
            warn!(
                "{}",
                crate::refused_signal_controls::handler_refused_warning(signum)
            );
        }
        if new_stack {
            warn!(
                "{}",
                crate::refused_signal_controls::alternate_stack_refused_warning()
            );
        }
    }

    async fn dispatch_rpc(
        &self,
        from: Tid,
        gr: <Self as GlobalTool>::Request,
    ) -> <Self as GlobalTool>::Response {
        // This read stays inside the caller's existing grant. In particular,
        // it neither drains logs nor updates clocks or deadline ownership.
        if let GlobalRequest::CheckBrkMappings(check, fact) = &gr.2 {
            return (
                None,
                GlobalResponse::CheckBrkMappings(
                    self.capture_brk_mappings(from, gr.1, *check, *fact),
                ),
            );
        }
        // This entry is also used directly. The strict native cut must precede
        // ordinary forwarding drain/checks and all sender-clock accounting.
        if native_exec::is_native_exec_control(&gr.2) {
            return self.dispatch_native_exec_control(from, gr);
        }
        // A Detcore warning from inside the guest (`crate::guest_log`) carries
        // no logical time, changes no scheduler state, and its answer carries
        // no time back. It is answered before the forwarded-record drain below,
        // whose order state it must not touch: verification never compares a
        // warning's place, and a run's ordered records must not depend on
        // whether it warned. (`receive_rpc` takes no deadline hint for it.)
        if let GlobalRequest::GuestLog(record, unsent_before) = &gr.2 {
            crate::guest_log::emit(record.as_ref(), *unsent_before);
            return (None, GlobalResponse::GuestLog(()));
        }
        // Signal controls an in-guest runtime refused
        // (`crate::refused_signal_controls`): announced once per run, and,
        // like a warning, answered before the forwarded-record drain, with no
        // time either way.
        if let GlobalRequest::AnnounceRefusedSignalControls(actions, alternate_stack) = &gr.2 {
            self.announce_refused_signal_controls(*actions, *alternate_stack);
            return (None, GlobalResponse::AnnounceRefusedSignalControls(()));
        }
        // Records an in-guest Tool forwarded before sending this request are queued,
        // and the sender's are written now if it runs alone (the order rule at
        // `detlog::set_forwarded_source`).
        crate::detlog::drain_forwarded_at_request(from.as_raw());
        type R = GlobalResponse;
        let dtid = DetTid::from_raw(from.into()); // TODO(T78538674): FIXME
        let (guest_time, request_mm, request) = gr;
        let request = match request {
            GlobalRequest::WithForwardedCount(produced, request) => {
                crate::detlog::check_forwarded_count(from.as_raw(), produced);
                *request
            }
            request => request,
        };
        let time_from_guest = guest_time.as_nanos();
        // Exec transfer messages authenticate both incarnations before ordinary
        // sender-clock accounting. The carried clock still belongs to the former
        // thread until the successful reconnect commits.
        match &request {
            GlobalRequest::ReconnectExec { former, process } => {
                return self
                    .recv_exec_transfer(from, guest_time, request_mm, *former, *process)
                    .await;
            }
            GlobalRequest::RetireExec { thread, signaled } => {
                return self
                    .recv_retire_exec_transfer(
                        from,
                        guest_time,
                        request_mm,
                        thread.clone(),
                        *signaled,
                    )
                    .await;
            }
            _ => {}
        }
        // A committed SIGKILL victim's requests that return before ordinary
        // admission (https://github.com/rrnewton/hermit/issues/3994). Exec
        // transfers above, the child-exit SIGCHLD control (sent in the
        // sender's own turn, which a committed victim no longer gets, or by
        // the tracer at a signal stop) and the consuming `SignalDequeued` keep
        // their own answers. Shared open file control is in-guest LiteInst
        // transport, never sent on ptrace, where alone a SIGKILL is retired
        // at the commit. Observations and loss notices are still recorded,
        // since dropping one only loses information, but not answered. A
        // SIGALRM ledger change from a thread that is already logically dead
        // would land at a host-chosen time: it is refused, and not answered
        // either.
        let early_victim_request = match &request {
            GlobalRequest::RecordHostInput { .. }
            | GlobalRequest::RecordHostMutation { .. }
            | GlobalRequest::RecordDeterminismLoss(_)
            | GlobalRequest::Sigalrm(_) => self
                .sched
                .lock()
                .unwrap()
                .is_unaccounted_sigkill_victim(dtid),
            _ => false,
        };
        if early_victim_request && matches!(&request, GlobalRequest::Sigalrm(_)) {
            return (None, R::Retired);
        }
        // An observation for `hermit run --verify`, answered before any clock
        // or scheduler accounting: it carries no logical time, changes no
        // scheduler state, and its answer carries no time back.
        if let GlobalRequest::RecordHostInput {
            path,
            syscall,
            identity,
        } = request
        {
            self.host_inputs.lock().unwrap().push(HostInputRecord {
                path,
                dtid: dtid.as_raw() as u64,
                syscall,
                identity,
            });
            if early_victim_request {
                return (None, R::Retired);
            }
            return (None, R::RecordHostInput(()));
        }
        if let GlobalRequest::RecordHostMutation { path } = request {
            self.host_mutations.lock().unwrap().insert(path);
            if early_victim_request {
                return (None, R::Retired);
            }
            return (None, R::RecordHostMutation(()));
        }
        // A determinism loss is recorded where verification reads it, and,
        // like the observations above, moves no clock: whether the host made
        // a guest-side handler lose determinism must not change guest time.
        if let GlobalRequest::RecordDeterminismLoss(reason) = request {
            crate::detlog::write_loss_notice(&reason);
            if early_victim_request {
                return (None, R::Retired);
            }
            return (None, R::RecordDeterminismLoss(()));
        }
        // Control traffic for a shared open file description: like the
        // observations above, it carries no logical time, changes no
        // scheduler state, and its answer carries no time back.
        if let GlobalRequest::SharedOpenFile(control) = request {
            return (
                None,
                R::SharedOpenFile(self.shared_open_file_control(dtid, control)),
            );
        }
        // A SIGALRM ledger control message: like the messages above, it
        // carries no logical time and its answer carries none back. It reads
        // or records ledger state, which changes only at scheduler commits,
        // in the sender's own turn.
        if let GlobalRequest::Sigalrm(control) = request {
            let refused = self.sched.lock().unwrap().sigalrm_control(dtid, control);
            return (None, R::Sigalrm(refused));
        }
        // A child-exit SIGCHLD control message: like the SIGALRM ledger's, it
        // carries no logical time and its answer carries none back. It
        // records an exit status in the sender's own turn, or answers at a
        // signal stop, which no scheduler state waits for.
        if let GlobalRequest::ChildExitSigchld(control) = request {
            let answer = self
                .sched
                .lock()
                .unwrap()
                .child_exit_sigchld_control_for_receiver(dtid, control);
            return (None, R::ChildExitSigchld(answer));
        }
        // A discarded-at-send record message
        // (`scheduler::discarded_at_send`): like the child-exit ledger's, it
        // carries no logical time and its answer carries none back. It records
        // a send in the sender's own turn, or a consumption at a signal stop.
        if let GlobalRequest::DiscardedSignal(control) = request {
            let answer = self
                .sched
                .lock()
                .unwrap()
                .discarded_signal_control(dtid, control);
            return (None, R::DiscardedSignal(answer));
        }
        // A thread's copied signal dispositions
        // (`syscalls::signal_shadow::publish_dispositions`): like the control
        // messages above, it carries no logical time and its answer carries
        // none back. It replaces what the scheduler holds for the sender's
        // process, which the sender's own operations changed while it held
        // the turn.
        if let GlobalRequest::PublishSignalDispositions(generation, dispositions) = request {
            let held = self.sched.lock().unwrap().publish_signal_dispositions(
                dtid,
                request_mm,
                generation,
                dispositions,
            );
            return (None, R::PublishSignalDispositions(held));
        }
        // A thread's mask from Detcore's copy
        // (`syscalls::signal_shadow::publish_mask`): the same kind of message.
        if let GlobalRequest::PublishSignalMask(mask) = request {
            let taken = self
                .sched
                .lock()
                .unwrap()
                .publish_signal_mask(dtid, request_mm, mask);
            return (None, R::PublishSignalMask(taken));
        }
        if let GlobalRequest::PublishRuntimePrivateMemory(facts) = request {
            let accepted = self
                .sched
                .lock()
                .unwrap()
                .publish_runtime_private_memory(dtid, request_mm, facts);
            return (None, R::PublishRuntimePrivateMemory(accepted));
        }
        if let GlobalRequest::SignalDequeued {
            detpid,
            identity,
            dequeue,
        } = &request
        {
            return self
                .recv_signal_dequeued(dtid, request_mm, guest_time, *detpid, *identity, *dequeue)
                .await;
        }

        // A vfork child registers itself while the parent is kernel-blocked.
        // Reopening a former exec worker's TID requires that exact pending
        // parent grant and its address-space lineage, not an ordinary RPC.
        if let GlobalRequest::CreateVforkChildThread(parent, process, child, _, flags, ..) =
            &request
        {
            let mut sched = self.lock_rpc_scheduler(false).await;
            if sched.transferred_exec_tid_requires_registration(dtid) {
                if *child != dtid
                    || !flags.contains(CloneFlags::CLONE_VFORK)
                    || !sched.pending_vfork_registration_matches(
                        *parent,
                        *process,
                        *child,
                        request_mm,
                        flags.contains(CloneFlags::CLONE_VM),
                    )
                {
                    return (None, R::ThreadExited);
                }
                sched.register_reused_transferred_exec_tid(dtid, request_mm);
            }
        }

        // Ptrace may deliver a newborn's startup before the parent's clone
        // callback registers it. A former exec worker's reused Linux TID must
        // wait for that authenticated registration, not inherit the old
        // incarnation or turn a premature refusal into a silent exit(0).
        // No clock or scheduler request is published while it waits.
        if matches!(&request, GlobalRequest::StartNewThread(child, ..) if *child == dtid) {
            loop {
                {
                    let sched = self.lock_rpc_scheduler(false).await;
                    if !sched.transferred_exec_tid_requires_registration(dtid)
                        || sched.thread_is_logically_killed(dtid)
                    {
                        break;
                    }
                }
                yield_once().await;
            }
        }

        let is_deregister = matches!(&request, GlobalRequest::DeregisterThread(_));
        let consuming_cleanup =
            is_deregister || matches!(&request, GlobalRequest::RobustListWakes(_));
        // What a committed SIGKILL victim may still send and be answered
        // (`Scheduler::is_unaccounted_sigkill_victim`): its consuming cleanup,
        // and the failure reports that end a run.
        let answered_for_sigkill_victim = consuming_cleanup
            || matches!(
                &request,
                GlobalRequest::ParkedProtocolFailure(_)
                    | GlobalRequest::UnrecoverableShutdown
                    | GlobalRequest::PolicyRefusal(_)
            );

        let (exec_reconnect, is_exec_caller_after_local_mm_swap) = {
            let pending = self.pending_exec_states.lock().unwrap();
            let reconnect = match &request {
                GlobalRequest::CreateChildThread(child, process, _, None, _, _, _)
                    if *child == dtid && *child == *process =>
                {
                    pending.get(process).cloned()
                }
                _ => None,
            };
            let is_exec_caller_after_local_mm_swap = pending.values().any(|state| {
                state.caller == dtid && state.mm.for_exec(state.process) == request_mm
            });
            (reconnect, is_exec_caller_after_local_mm_swap)
        };

        // Retired incarnations cannot publish clocks. A transferred former TID
        // reopens only through fresh child registration; successful exec binds
        // the surviving leader separately. Hold admission through clock
        // accounting so logical teardown cannot linearize between the two.
        let mut tombstoned_deregistration = None;
        {
            let sched = self.lock_rpc_scheduler(consuming_cleanup).await;
            if exec_reconnect.is_none()
                && !is_exec_caller_after_local_mm_swap
                && !sched.rpc_incarnation_matches(dtid, request_mm)
            {
                debug!(
                    "[detcore, dtid {}] rejecting {:?} RPC from retired exec incarnation {:?}",
                    dtid, request, request_mm,
                );
                return if is_deregister {
                    (None, R::DeregisterThread(()))
                } else {
                    (None, R::ThreadExited)
                };
            }
            if let GlobalRequest::DeregisterThread(owner) = &request {
                assert_eq!(
                    owner.dettid, dtid,
                    "deregistration must belong to its sender"
                );
                assert_eq!(owner.mm, request_mm, "deregistration must retain its MmId");
                // DBT can reject StartNewThread before parent registration.
                // Its tombstone still needs the existing final accounting path.
                if !sched.thread_was_registered(dtid) && !sched.thread_is_logically_killed(dtid) {
                    assert!(
                        sched.backend_failed() || !owner.thread_start_entered,
                        "a started thread must have a scheduler registration before deregistration"
                    );
                    // The backend still consumes this constructed ThreadState,
                    // but no guest start/registration happened. Acknowledge its
                    // cleanup without creating a clock, tree entry or admission.
                    return (None, R::DeregisterThread(()));
                }
            }
            let child = match &request {
                GlobalRequest::CreateChildThread(child, ..)
                | GlobalRequest::CreateVforkChildThread(_, _, child, ..) => Some(*child),
                _ => None,
            };
            if sched.thread_is_logically_killed(dtid) && exec_reconnect.is_none() {
                trace!(
                    "[detcore, dtid {}] rejecting RPC after permanent logical-thread removal",
                    dtid
                );
                if let GlobalRequest::DeregisterThread(deregistration) = &request {
                    tombstoned_deregistration = Some(deregistration.clone());
                } else {
                    return (None, R::ThreadExited);
                }
            }
            if !answered_for_sigkill_victim && sched.is_unaccounted_sigkill_victim(dtid) {
                trace!(
                    "[detcore, dtid {}] leaving a SIGKILLed thread's request unanswered",
                    dtid
                );
                return (None, R::Retired);
            }
            if child.is_some_and(|child| sched.thread_is_logically_killed(child))
                && exec_reconnect.is_none()
            {
                trace!(
                    "[detcore, dtid {}] rejecting registration that reuses a tombstoned child TID",
                    dtid
                );
                return (None, R::ThreadExited);
            }

            if let GlobalRequest::ResumeExec(process) = &request
                && (dtid != *process
                    || sched.registered_process(dtid) != Some(*process)
                    || !sched.next_turns.contains_key(&dtid))
            {
                return (None, R::ThreadExited);
            }

            // An admitted ordinary request under the new identity proves the
            // Tool received the transfer acknowledgment and rebound its state.
            self.acknowledge_exec_transfer(dtid, request_mm);

            let is_thread_reconnect = matches!(
                &request,
                GlobalRequest::StartNewThread(child_dettid, ..) if *child_dettid == dtid
            ) && self.global_time.lock().unwrap().contains_thread(dtid);

            // This portion of the time updates "asynchronously", and we can tick it on every rpc:
            // TODO: eventually the vector clock should be in shared memory, and
            // the local clocks should update truly asynchrously.  Therefore it
            // SHOULD be safe to always push through this update on any rpc.
            if tombstoned_deregistration.is_none()
                && exec_reconnect.is_none()
                && !is_thread_reconnect
            {
                self.global_time.lock().unwrap().update_global_time(
                    dtid,
                    time_from_guest,
                    guest_time.inherited_nanos(),
                );
            }
        }
        if let Some(deregistration) = tombstoned_deregistration {
            self.recv_deregister_thread(from, deregistration).await;
            return (None, R::DeregisterThread(()));
        }

        // RPC boilerplate. (Hard to generate systematically now though, because of the
        // time payload piggy-backing on each rpc. Maybe eventually once ticking a
        // threads' own clock happens through shared memory.)
        #[allow(clippy::unit_arg)]
        let resp = match request {
            GlobalRequest::PrepareNativeExec(_)
            | GlobalRequest::SealNativeExecStats { .. }
            | GlobalRequest::IssueNativeExec { .. }
            | GlobalRequest::CancelNativeExec { .. }
            | GlobalRequest::CompleteNativeExec { .. } => {
                unreachable!("native controls handled before ordinary admission")
            }
            GlobalRequest::ReconnectExec { .. } | GlobalRequest::RetireExec { .. } => {
                unreachable!("exec transfer handled before ordinary admission")
            }
            GlobalRequest::WithForwardedCount(..) => {
                unreachable!("a counted request is unwrapped on arrival")
            }
            GlobalRequest::ResumeExec(process) => {
                let mut resources = Resources::new(dtid);
                resources.insert(ResourceID::MemAddrSpace(process), Permission::RW);
                let (response, duration) = self
                    .recv_request_resources(from, process, resources, Some(request_mm))
                    .await;
                match response {
                    SchedulerRpcResult::Continue(status) => R::ResumeExec(status, duration),
                    SchedulerRpcResult::ThreadExited => R::ThreadExited,
                }
            }
            GlobalRequest::SignalDequeued { .. } => {
                unreachable!("consuming path handled before ordinary cancellation")
            }
            GlobalRequest::RecordHostInput { .. } | GlobalRequest::RecordHostMutation { .. } => {
                unreachable!("host-input observation answered before clock accounting")
            }
            GlobalRequest::SharedOpenFile(_) => {
                unreachable!("shared open file control answered before clock accounting")
            }
            GlobalRequest::GuestLog(..) => {
                unreachable!("a guest log record is answered before clock accounting")
            }
            GlobalRequest::AnnounceRefusedSignalControls(..) => {
                unreachable!(
                    "refused signal controls are answered before the forwarded-record drain"
                )
            }
            GlobalRequest::CheckBrkMappings(..) => {
                unreachable!("mapping capture is answered before clock accounting")
            }
            GlobalRequest::Sigalrm(_) => {
                unreachable!("SIGALRM ledger control answered before clock accounting")
            }
            GlobalRequest::ChildExitSigchld(_) => {
                unreachable!("child-exit SIGCHLD control answered before clock accounting")
            }
            GlobalRequest::DiscardedSignal(_) => {
                unreachable!("discarded-at-send control answered before clock accounting")
            }
            GlobalRequest::RecordDeterminismLoss(_) => {
                unreachable!("determinism loss answered before clock accounting")
            }
            GlobalRequest::PublishSignalDispositions(..) => {
                unreachable!("published dispositions answered before clock accounting")
            }
            GlobalRequest::PublishSignalMask(_) => {
                unreachable!("a published mask is answered before clock accounting")
            }
            GlobalRequest::PublishRuntimePrivateMemory(_) => {
                unreachable!("private-memory publication precedes clock accounting")
            }
            GlobalRequest::ParkedRequest(rs, pid, capability) => {
                let (response, _) = self
                    .recv_resources_with_origin(
                        from,
                        pid,
                        rs,
                        Some(request_mm),
                        RpcOrigin::DirectRequestResources,
                        capability,
                    )
                    .await;
                match response {
                    SchedulerRpcResult::Continue(r) => R::ParkedRequest(r),
                    SchedulerRpcResult::ThreadExited => R::ThreadExited,
                }
            }
            GlobalRequest::ResumeParkedRequest {
                ticket,
                current_site,
            } => {
                self.recv_resume_parked(from, request_mm, ticket, current_site)
                    .await
            }
            GlobalRequest::FinishParkedObservation {
                wait,
                lease,
                site,
                finish,
            } => {
                let ack = Ivar::new();
                let intent = ControlIntent::Finish {
                    wait,
                    lease,
                    site,
                    finish,
                    ack: ack.clone(),
                };
                let posted = self
                    .sched
                    .lock()
                    .unwrap()
                    .post_control(dtid, request_mm, intent);
                if let Err(error) = posted {
                    self.sched.lock().unwrap().fail_parked(dtid, error);
                    return (None, R::ThreadExited);
                }
                R::FinishParkedObservation(ack.await)
            }
            GlobalRequest::ParkedProtocolFailure(error) => {
                self.sched.lock().unwrap().fail_parked(dtid, error);
                return (None, R::ThreadExited);
            }

            GlobalRequest::RequestResources(rs, pid) => {
                let (response, _endtime) = self
                    .recv_request_resources(from, pid, rs, Some(request_mm))
                    .await;
                match response {
                    SchedulerRpcResult::Continue(response) => R::RequestResources(response),
                    SchedulerRpcResult::ThreadExited => R::ThreadExited,
                }
            }
            GlobalRequest::ReleaseResources(rs) => {
                R::ReleaseResources(self.recv_release_resources(from, rs).await)
            }
            GlobalRequest::ReleaseAllResources => {
                R::ReleaseAllResources(self.recv_release_all_resources(from).await)
            }
            GlobalRequest::PolicyRefusal(reason) => {
                self.policy_refusal.lock().unwrap().get_or_insert(reason);
                R::PolicyRefusal(())
            }
            // TODO-HUMAN-REVIEW(PR-643): Review run-wide unsupported-syscall aggregation.
            GlobalRequest::ReportUnsupportedSyscall(name) => {
                let _sched = self.lock_rpc_scheduler(false).await;
                let inserted = self
                    .unsupported_syscalls
                    .lock()
                    .unwrap()
                    .insert(name.clone());
                if inserted
                    && let Some(report) = &self.unsupported_syscall_report_fd
                    && let Err(error) = writeln!(report.lock().unwrap(), "{name}")
                {
                    warn!("failed to append unsupported-syscall report: {error}");
                }
                R::ReportUnsupportedSyscall(())
            }
            GlobalRequest::AnnounceSignalHandlerNotInstalled(signum) => {
                if self
                    .announced_handlers_not_installed
                    .lock()
                    .unwrap()
                    .insert(signum)
                {
                    warn!("{}", signal_handler_not_installed_warning(signum));
                }
                R::AnnounceSignalHandlerNotInstalled(())
            }
            GlobalRequest::PrepareExec(process, mm, fd_blocking) => {
                let mut sched = self.lock_rpc_scheduler(false).await;
                if mm != request_mm
                    || fd_blocking.initialize_inherited_stdio
                    || fd_blocking.initial_input.is_some()
                {
                    return (None, R::ThreadExited);
                }
                if self.cfg.sequentialize_threads && !sched.prepare_exec_teardown(dtid, process, mm)
                {
                    return (None, R::ThreadExited);
                }
                trace!(
                    "[detcore, dtid {}] preparing exec for process {} with mm {:?} and logically blocking descriptors {:?}",
                    dtid, process, mm, fd_blocking,
                );
                self.pending_exec_states.lock().unwrap().insert(
                    process,
                    PendingExecState {
                        caller: dtid,
                        process,
                        mm,
                        fd_blocking: *fd_blocking,
                    },
                );
                R::PrepareExec(())
            }
            GlobalRequest::CancelExec(process) => {
                let mut sched = self.lock_rpc_scheduler(false).await;
                let mut pending = self.pending_exec_states.lock().unwrap();
                if pending
                    .get(&process)
                    .is_some_and(|state| state.caller == dtid)
                {
                    let prepared = pending.remove(&process).unwrap();
                    sched.finish_exec_teardown(prepared.caller, process, prepared.mm, false);
                }
                R::CancelExec(())
            }
            GlobalRequest::MarkPastFirstExecve(
                detpid,
                signal_identity,
                unprepared_exe,
                kernel_image,
            ) => {
                let mut sched = self.lock_rpc_scheduler(false).await;
                // The exec leaves its process's descriptor table
                // (`LockLedger::exec`); `TableExec` names what survived.
                self.lock_ledger.lock().unwrap().exec(detpid);
                // A backend preserving the leader's ThreadState does not use
                // the identity-transfer RPC. This successful-exec edge still
                // commits the frozen sibling cohort in scheduler order.
                let prepared = self.pending_exec_states.lock().unwrap().get(&dtid).cloned();
                if sched.signal_control_installed() {
                    let result = (|| {
                        let identity = signal_identity.ok_or(ProtocolFailure::Identity)?;
                        let pid = sched
                            .registered_process(dtid)
                            .ok_or(ProtocolFailure::Identity)?;
                        let mut pending = self.pending_exec_states.lock().unwrap();
                        if let Some(prepared) = pending.get(&pid) {
                            if prepared.caller != dtid || prepared.process != pid {
                                return Err(ProtocolFailure::Identity);
                            }
                            sched.complete_signal_exec(
                                pid,
                                dtid,
                                prepared.mm,
                                request_mm,
                                identity,
                            )?;
                            pending.remove(&pid);
                        } else {
                            // A backend that re-enters thread-start already bound
                            // the new image. A lifecycle notification alone cannot
                            // authorize a new address space or backend generation.
                            sched
                                .real_timers
                                .validate_task(pid, dtid, request_mm, identity)?;
                        }
                        Ok(())
                    })();
                    if let Err(error) = result {
                        sched.fail_parked(dtid, error);
                        return (None, R::ThreadExited);
                    }
                }
                // The exec caller still owns its turn: delete process-owned
                // POSIX deadlines before the replacement image can yield or run.
                // Failed exec takes CancelExec and never reaches this point.
                sched.delete_exec_posix_timers(detpid);
                // The exec resets the process's exit signal to SIGCHLD and turns
                // its existing children's notifications into SIGCHLD.
                sched.record_successful_exec(detpid);
                // Reloading backends consumed the preparation in their
                // registration reconnect; state-preserving leaders consume it
                // here after the terminal exec notification was authenticated.
                if let Some(prepared) = &prepared
                    && self.cfg.sequentialize_threads
                    && prepared.caller == dtid
                    && prepared.process == dtid
                    && prepared.mm.for_exec(dtid) == request_mm
                {
                    // Only a reloading backend (DBT) carries owner-death
                    // wakes with its preparation, and it delivered them at
                    // its reconnect; ptrace delivers its own after the exec
                    // (Claude review of
                    // https://github.com/rrnewton/hermit/pull/4352, F5).
                    assert!(
                        prepared.fd_blocking.robust_wakes.is_empty(),
                        "a state-preserving exec carried robust-list wakes to the coordinator"
                    );
                    sched.reconnect_after_exec(ExecReconnect {
                        caller: dtid,
                        new_leader: dtid,
                        detpid: dtid,
                        pre_exec_mm: prepared.mm,
                        post_exec_mm: request_mm,
                        child_tid_addr: 0,
                        reconnect_priority: None,
                    });
                    self.pending_exec_states.lock().unwrap().remove(&dtid);
                    // Native-only lifecycle receipt: the actual same-leader
                    // Mark edge has now installed this ticket's new mm. This
                    // hook grants no turn and performs no clock accounting.
                    if let Some(registry) = self.native_exec_registry.get()
                        && registry
                            .lock()
                            .unwrap()
                            .mark_past_first_exec(dtid, request_mm)
                            .is_err()
                    {
                        return (None, R::ThreadExited);
                    }
                }
                self.past_first_execve.store(true, SeqCst);
                let first_image = !self.initial_descriptor_image_complete.swap(true, SeqCst);
                let mut overrides = self
                    .post_exec_fd_blocking
                    .lock()
                    .unwrap()
                    .remove(&dtid)
                    .unwrap_or_default();
                trace!(
                    "[detcore, dtid {}] restoring logically blocking descriptors after exec: {:?}",
                    dtid, overrides,
                );
                overrides.initialize_inherited_stdio =
                    first_image && self.cfg.backend.tool_shares_guest_descriptor_table;
                // The image the exec ran: a reloading backend carried it in the
                // preparation it reconnected with (above); a state-preserving
                // one left the preparation to this edge. The registered root's
                // first image is the one its launch named, which a backend
                // whose launcher loads the guest (SaBRe) never execs.
                // Without sequentialized threads, sibling execs may prepare
                // concurrently and the preparation this edge consumed may be
                // another thread's, which the launcher check cannot tell
                // apart: no prepared record is kept (a launched run then
                // refuses the link by name).
                if !self.cfg.sequentialize_threads {
                    overrides.exe = None;
                } else if overrides.exe.is_none() {
                    overrides.exe = prepared
                        .filter(|prepared| prepared.caller == dtid)
                        .and_then(|prepared| prepared.fd_blocking.exe)
                        // An exec no Detcore prepared (a backend's copied
                        // vfork child runs it without Detcore): the record the
                        // new image resolved from the path the exec was given.
                        .or(unprepared_exe);
                }
                if self.root_dettid.get() == Some(&dtid)
                    && !self.root_image_marked.swap(true, SeqCst)
                    && let Some(launch) = &self.cfg.launch_image
                {
                    let launched = launch.exe.as_deref().and_then(|exe| {
                        crate::exec_record::ExecRecord::at(std::path::Path::new("/"), exe)
                    });
                    overrides.exe = if launch.launcher.is_some() {
                        // In a run that declares a launcher, the image's own
                        // record (from its `AT_EXECFN`) is the file the
                        // launcher actually ran, where it names one other
                        // than the launcher: it outranks the launch's own
                        // search, which stands where the image gave none.
                        overrides
                            .exe
                            .take()
                            .filter(|record| Some(record.identity()) != self.launcher_identity())
                            .or(launched)
                    } else {
                        launched
                    };
                }
                // A record stands only where the kernel's own link, which the
                // new image read (`kernel_image`), names the recorded file or
                // the launcher the run declared: Detcore's resolution looked the
                // path up in its own view, which a binfmt_misc handler, another
                // filesystem view or a racing exec can make wrong. Where the
                // tool cannot read the link (KVM), the record stands.
                if let Some(kernel) = kernel_image {
                    overrides.exe = overrides.exe.filter(|record| {
                        record.identity() == kernel || self.launcher_identity() == Some(kernel)
                    });
                }
                // The launcher itself is never the guest's image (a link to it
                // resolved, as the kernel's link names it).
                overrides.exe = overrides
                    .exe
                    .filter(|record| Some(record.identity()) != self.launcher_identity());
                {
                    let mut images = self.exec_images.lock().unwrap();
                    match &overrides.exe {
                        Some(record) => images.insert(detpid, record.clone()),
                        None => images.remove(&detpid),
                    };
                }
                if self.cfg.backend.tool_shares_guest_descriptor_table {
                    let mut input = self.inherited_input.lock().unwrap();
                    if input
                        .as_ref()
                        .is_some_and(|input| input.root == Some((dtid, request_mm)))
                    {
                        // Only this registered root/image consumes the launch
                        // seed. A descendant or later exec cannot acquire it.
                        // Drop the temporary reader at this handoff; the actual
                        // root's descriptor remains live until local validation.
                        overrides.initial_input =
                            input.take().map(|input| input.lease.identity().clone());
                    }
                }
                R::MarkPastFirstExecve(Box::new(overrides))
            }
            // Requested by the parent thread:
            GlobalRequest::CreateChildThread(
                dettid,
                parent_detpid,
                ctid,
                flags,
                exit_signal,
                physical_ids,
                priority,
            ) => {
                if let Some(prepared) = &exec_reconnect {
                    let mut sched = self.lock_rpc_scheduler(false).await;
                    let (mut pending, post_exec_mm) = {
                        let mut states = self.pending_exec_states.lock().unwrap();
                        let Some(pending) = states.remove(&parent_detpid) else {
                            return (None, R::ThreadExited);
                        };
                        assert_eq!(&pending, prepared);
                        let post_exec_mm = pending.mm.for_exec(pending.process);
                        (pending, post_exec_mm)
                    };
                    assert_eq!(pending.process, parent_detpid);
                    if let Some((physical_pid, physical_tid)) = physical_ids
                        && let Err(open_error) = sched.register_physical_thread(
                            dettid,
                            post_exec_mm,
                            physical_pid,
                            physical_tid,
                        )
                    {
                        error!(
                            "[detcore, dtid {}] failed to register post-exec host process {} thread {}: {}",
                            dettid, physical_pid, physical_tid, open_error,
                        );
                        return (None, R::ThreadExited);
                    }
                    let retired = sched.reconnect_after_exec(ExecReconnect {
                        caller: pending.caller,
                        new_leader: dettid,
                        detpid: parent_detpid,
                        pre_exec_mm: pending.mm,
                        post_exec_mm,
                        child_tid_addr: ctid,
                        reconnect_priority: priority,
                    });
                    if pending.caller != dettid {
                        self.global_time
                            .lock()
                            .unwrap()
                            .reassign_thread(pending.caller, dettid);
                    }
                    // Linux marked the old thread group's robust-list words
                    // during the exec, which this message from the new image
                    // follows, and the reconnect has retired the threads the
                    // exec killed. Wake one waiter per marked word now, under
                    // the same lock: the scheduler commits no turn between the
                    // exec's grant and this reconnect, and the admissions are
                    // drained in canonical order
                    // (https://github.com/rrnewton/hermit/issues/4268).
                    let robust_wakes = std::mem::take(&mut pending.fd_blocking.robust_wakes);
                    wake_exec_robust_waiters(&mut sched, &robust_wakes, "exec");
                    if !pending.fd_blocking.is_empty() {
                        self.post_exec_fd_blocking
                            .lock()
                            .unwrap()
                            .insert(dettid, pending.fd_blocking);
                    }
                    debug!(
                        "[detcore, dtid {}] reconciled successful exec from caller {}; retired prior identities {:?}",
                        dtid, pending.caller, retired
                    );
                    R::CreateChildThread(Some(post_exec_mm))
                } else {
                    match self
                        .recv_create_child_thread(
                            from,
                            request_mm,
                            ChildRegistration {
                                parent_dettid: DetTid::from_raw(from.into()),
                                parent_detpid,
                                child_dettid: dettid,
                                child_tid_addr: ctid,
                                flags,
                                exit_signal,
                                physical_ids,
                                maybe_priority: priority,
                                parent_is_kernel_blocked: false,
                            },
                        )
                        .await
                    {
                        SchedulerRpcResult::Continue(()) => R::CreateChildThread(None),
                        SchedulerRpcResult::ThreadExited => R::ThreadExited,
                    }
                }
            }
            // Requested by the vfork child on behalf of its kernel-blocked parent:
            GlobalRequest::CreateVforkChildThread(
                parent_dettid,
                parent_detpid,
                child_dettid,
                ctid,
                flags,
                exit_signal,
                priority,
            ) => match self
                .recv_create_child_thread(
                    from,
                    request_mm,
                    ChildRegistration {
                        parent_dettid,
                        parent_detpid,
                        child_dettid,
                        child_tid_addr: ctid,
                        flags: Some(flags),
                        exit_signal,
                        physical_ids: None,
                        maybe_priority: priority,
                        parent_is_kernel_blocked: true,
                    },
                )
                .await
            {
                SchedulerRpcResult::Continue(()) => R::CreateChildThread(None),
                SchedulerRpcResult::ThreadExited => R::ThreadExited,
            },
            // Requested by the child thread itself:
            GlobalRequest::StartNewThread(dettid, detpid, physical_ids, signal_identity) => {
                match self
                    .recv_start_new_thread(
                        from,
                        dettid,
                        detpid,
                        request_mm,
                        physical_ids,
                        signal_identity,
                    )
                    .await
                {
                    SchedulerRpcResult::Continue(history) => R::StartNewThread(history),
                    SchedulerRpcResult::ThreadExited => R::ThreadExited,
                }
            }
            GlobalRequest::DeregisterThread(deregistration) => {
                R::DeregisterThread(self.recv_deregister_thread(from, deregistration).await)
            }
            GlobalRequest::PublishedGuestSigmask(thread) => R::PublishedGuestSigmask(
                self.lock_rpc_scheduler(false)
                    .await
                    .published_guest_sigmask(thread),
            ),
            GlobalRequest::SetSavedGuestSigmask(mask) => {
                let updated = self
                    .lock_rpc_scheduler(false)
                    .await
                    .set_saved_guest_sigmask(dtid, mask);
                if updated {
                    R::SetSavedGuestSigmask(())
                } else {
                    R::ThreadExited
                }
            }
            GlobalRequest::SetSigtimedwaitSet(set) => {
                let updated = self
                    .lock_rpc_scheduler(false)
                    .await
                    .set_sigtimedwait_set(dtid, set);
                if updated {
                    R::SetSigtimedwaitSet(())
                } else {
                    R::ThreadExited
                }
            }
            GlobalRequest::RecordGuestMarkerSignal => {
                self.lock_rpc_scheduler(false)
                    .await
                    .record_guest_marker_signal();
                R::RecordGuestMarkerSignal(())
            }
            GlobalRequest::OwnProcessSignalInputs => R::OwnProcessSignalInputs(
                self.lock_rpc_scheduler(false)
                    .await
                    .own_process_signal_inputs(dtid),
            ),
            GlobalRequest::SetChildTidAddress(address) => {
                let updated = self
                    .lock_rpc_scheduler(false)
                    .await
                    .set_child_tid_address(dtid, address);
                if updated {
                    R::SetChildTidAddress(())
                } else {
                    R::ThreadExited
                }
            }
            GlobalRequest::FutexAction(dettid, action, futexid, init_read, mask) => R::FutexAction(
                self.recv_futex_action(
                    RpcIncarnation {
                        dettid,
                        mm: request_mm,
                    },
                    action,
                    futexid,
                    init_read,
                    mask,
                )
                .await,
            ),
            GlobalRequest::RobustListWakes(wakes) => {
                R::RobustListWakes(self.recv_robust_list_wakes(dtid, wakes))
            }
            GlobalRequest::DeterminizeInode(ino, observed, sighting, procfs) => {
                R::DeterminizeInode(
                    self.recv_determinize_inode(from, ino, procfs, observed, sighting)
                        .await,
                )
            }
            GlobalRequest::DeterminizeMappingInodes(inodes) => R::DeterminizeMappingInodes(
                self.recv_determinize_mapping_inodes(from, inodes).await,
            ),
            // AUTONOMOUS-BOT-IMPLEMENTED
            // TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping RPC.
            GlobalRequest::DeterminizeDevice(dev) => {
                R::DeterminizeDevice(self.recv_determinize_device(from, dev).await)
            }
            GlobalRequest::DeterminizeMountId(raw_mount_id, fallback_order) => {
                R::DeterminizeMountId(
                    self.recv_determinize_mount_id(from, raw_mount_id, fallback_order.as_deref())
                        .await,
                )
            }
            GlobalRequest::ResolveMountinfoIds(mountinfo_order, observation_order) => {
                R::ResolveMountinfoIds(
                    self.recv_resolve_mountinfo_ids(from, &mountinfo_order, &observation_order)
                        .await,
                )
            }
            GlobalRequest::RetireInode(ino) => {
                R::RetireInode(self.recv_retire_inode(from, ino).await)
            }
            GlobalRequest::ForgetRetiredInode(ino, procfs) => {
                R::ForgetRetiredInode(self.recv_forget_retired_inode(from, ino, procfs).await)
            }
            GlobalRequest::DirectorySnapshotMark => {
                let _sched = self.lock_rpc_scheduler(false).await;
                let inodes = self.inodes.lock().unwrap();
                R::DirectorySnapshotMark(SnapshotMark {
                    retirements: inodes.retirements,
                    threads_counted: inodes.threads_counted,
                })
            }
            GlobalRequest::ReadyChannel(channel, wants) => R::ReadyChannel(
                self.lock_rpc_scheduler(false)
                    .await
                    .ready_channel(dtid, channel, wants),
            ),
            GlobalRequest::HandOffTurn => {
                R::HandOffTurn(self.lock_rpc_scheduler(false).await.hand_off_turn(dtid))
            }
            GlobalRequest::RecordFatalSignal => {
                let mut scheduler = self.lock_rpc_scheduler(false).await;
                if let Some(process) = scheduler.registered_process(dtid) {
                    scheduler.record_committed_fatal_signal(process);
                }
                R::RecordFatalSignal(())
            }
            GlobalRequest::TouchFile(ino) => R::TouchFile(self.recv_touch_file(from, ino).await),
            GlobalRequest::SetFileMtime(ino, mtime) => {
                R::SetFileMtime(self.recv_set_file_mtime(from, ino, mtime).await)
            }
            GlobalRequest::GlobalTimeLowerBound => {
                let ns = self.global_time.lock().unwrap().as_nanos();
                R::GlobalTimeLowerBound(ns)
            }
            GlobalRequest::TraceSchedEvent(ev, detpid, command_bootstrap) => {
                match self
                    .recv_trace_schedevent(ev, detpid, request_mm, command_bootstrap)
                    .await
                {
                    SchedulerRpcResult::Continue(response) => R::TraceSchedEvent(response),
                    SchedulerRpcResult::ThreadExited => R::ThreadExited,
                }
            }
            // AUTONOMOUS-BOT-IMPLEMENTED
            // TODO-HUMAN-REVIEW(#663)
            // TODO-HUMAN-REVIEW(#869)
            GlobalRequest::RegisterAlarm(dpid, dtid, duration, interval, sig) => {
                let now = self.global_time.lock().unwrap().as_nanos();
                // O1: a dequeue earlier in this turn restarted the timer, so
                // the old value reports that restart
                // (https://github.com/rrnewton/hermit/issues/4139).
                self.sched.lock().unwrap().reconcile_alarm(dpid, now);
                let response = match self
                    .recv_register_alarm(
                        dpid,
                        RpcIncarnation {
                            dettid: dtid,
                            mm: request_mm,
                        },
                        now,
                        duration,
                        interval,
                        sig,
                    )
                    .await
                {
                    SchedulerRpcResult::Continue(remaining) => R::RegisterAlarm(remaining),
                    SchedulerRpcResult::ThreadExited => R::ThreadExited,
                };
                self.sched.lock().unwrap().note_arming_hint(dtid, now);
                response
            }
            // AUTONOMOUS-BOT-IMPLEMENTED
            // TODO-HUMAN-REVIEW(PR-841): Review logical alarm query RPC.
            GlobalRequest::AlarmRemaining(dpid) => {
                let now = self.global_time.lock().unwrap().as_nanos();
                let mut sched = self.lock_rpc_scheduler(false).await;
                // O1: reconcile a dequeue earlier in this turn first, and end
                // the caller's slice at a deadline it restarts
                // (https://github.com/rrnewton/hermit/issues/4139).
                if sched.reconcile_alarm(dpid, now).is_some() {
                    sched.note_arming_hint(dtid, now);
                }
                match sched.itimer_snapshot(dpid, now) {
                    Ok(snapshot) => R::AlarmRemaining(snapshot),
                    Err(error) => {
                        sched.fail_parked(dtid, ProtocolFailure::Timer(error));
                        R::ThreadExited
                    }
                }
            }
            // O2: a signalfd read or `rt_sigtimedwait` completion in this
            // turn may have dequeued the process's ITIMER_REAL copy
            // (https://github.com/rrnewton/hermit/issues/4139).
            GlobalRequest::NoteGuestSigalrm(target) => {
                self.sched.lock().unwrap().note_guest_sigalrm(dtid, target);
                R::NoteGuestSigalrm
            }
            GlobalRequest::NoteSigalrmDisposition => {
                self.sched.lock().unwrap().note_sigalrm_disposition(dtid);
                R::NoteGuestSigalrm
            }
            GlobalRequest::ReconcileAlarm(dpid) => {
                let now = self.global_time.lock().unwrap().as_nanos();
                let mut sched = self.sched.lock().unwrap();
                if sched.reconcile_alarm(dpid, now).is_some() {
                    sched.note_arming_hint(dtid, now);
                }
                R::ReconcileAlarm
            }
            // AUTONOMOUS-BOT-IMPLEMENTED
            // TODO-HUMAN-REVIEW(#869)
            GlobalRequest::RegisterPosixTimer(
                dpid,
                dtid,
                timer_id,
                deadline,
                interval,
                notification,
            ) => {
                let now = self.global_time.lock().unwrap().as_nanos();
                let response = match self
                    .recv_register_posix_timer(
                        dpid,
                        RpcIncarnation {
                            dettid: dtid,
                            mm: request_mm,
                        },
                        timer_id,
                        deadline,
                        interval,
                        notification,
                    )
                    .await
                {
                    SchedulerRpcResult::Continue(()) => R::RegisterPosixTimer(()),
                    SchedulerRpcResult::ThreadExited => R::ThreadExited,
                };
                self.sched.lock().unwrap().note_arming_hint(dtid, now);
                response
            }
            // AUTONOMOUS-BOT-IMPLEMENTED
            // TODO-HUMAN-REVIEW(#663)
            GlobalRequest::ResolveKillTargets(dpid) => {
                let scheduler = self.lock_rpc_scheduler(false).await;
                R::ResolveKillTargets(
                    scheduler.process_signal_targets(dpid),
                    scheduler.exited_process_is_zombie(dpid),
                )
            }
            GlobalRequest::NamesHermitTask(caller, tgid, tid) => {
                R::NamesHermitTask(hermit_task_named(caller, tgid, tid))
            }
            GlobalRequest::HoldTurnForGroupExit(dettid) => R::HoldTurnForGroupExit(
                self.lock_rpc_scheduler(false)
                    .await
                    .hold_turn_for_group_exit(dettid),
            ),
            GlobalRequest::NotifySignalPending(
                dettid,
                SigWrapper(signal),
                target_process,
                scope,
            ) => {
                let (fence, valve) = {
                    let mut scheduler = self.lock_rpc_scheduler(false).await;
                    scheduler.notify_signal_pending(dettid, SigWrapper(signal));
                    let dbt = self.cfg.backend.requires_thread_directed_process_signals;
                    // kill, rt_sigqueueinfo and a pidfd without
                    // PIDFD_SIGNAL_THREAD name a process; tgkill, tkill,
                    // rt_tgsigqueueinfo and a pidfd with PIDFD_SIGNAL_THREAD
                    // name a thread (the notice's SignalScope), whose SIGKILL ends its
                    // whole group only while that thread is alive
                    // (https://github.com/rrnewton/hermit/issues/4362; Claude
                    // review of https://github.com/rrnewton/hermit/pull/4439, R1).
                    let process_directed = scope == SignalScope::Process;
                    let target_process = if signal == libc::SIGKILL && dbt {
                        target_process.or_else(|| scheduler.registered_process(dettid))
                    } else {
                        target_process
                    };
                    let mut fence = None;
                    if signal == libc::SIGKILL {
                        scheduler.record_committed_sigkill(dettid, target_process);
                        if let Some(detpid) = target_process {
                            if dbt {
                                fence =
                                    fence_or_refuse(&scheduler, dettid, detpid, process_directed);
                            }
                            scheduler.note_process_sigkill(dettid, detpid, process_directed);
                        }
                    }
                    (fence, scheduler.sigkill_fence_valve)
                };
                // The sender's kill does not return until the victim process
                // is gone, as ptrace's SIGKILL fence makes it wait: a process
                // pidfd reports exit after the kernel has closed the victim's
                // files and sent its parent SIGCHLD. Only then can the sender,
                // which still holds the serial grant, observe the death, at a
                // point the schedule fixes (Claude review of
                // https://github.com/rrnewton/hermit/pull/4400, F1).
                if let Some((detpid, pidfd)) = fence
                    && let Err(reason) = wait_for_process_exit(pidfd, valve).await
                {
                    crate::scheduler::exit_on_scheduler_refusal(
                        &crate::scheduler::sigkill_retirement::UnfencedSigkill {
                            process: detpid,
                            reason,
                        },
                    );
                }
                R::NotifySignalPending(())
            }
            GlobalRequest::NotifyUnresolvedSigkill(group) => {
                self.lock_rpc_scheduler(false)
                    .await
                    .record_unresolved_sigkill(group);
                R::NotifyUnresolvedSigkill(())
            }
            GlobalRequest::ReserveSigkill(sender, target, uid) => {
                let mut scheduler = self.lock_rpc_scheduler(false).await;
                let reserved = scheduler
                    .registered_process(target)
                    .ok_or(crate::scheduler::sigkill_retirement::SigkillRefusal::NoTarget)
                    .and_then(|process| scheduler.reserve_sigkill(sender, process, uid));
                R::ReserveSigkill(reserved.ok())
            }
            GlobalRequest::ReserveSigkillGroup(sender, pgid, uid) => R::ReserveSigkill(
                self.lock_rpc_scheduler(false)
                    .await
                    .reserve_sigkill_group(sender, pgid, uid)
                    .ok(),
            ),
            GlobalRequest::CommitSigkill(token, sent) => {
                // A send that includes the sender is committed at its Exit
                // grant, before it is sent: its victims die only afterwards,
                // and its sender never returns, so it does not wait.
                let (victims, wait) = {
                    let mut sched = self.lock_rpc_scheduler(false).await;
                    let wait = sent && !sched.sigkill_reservation_includes_sender(token);
                    (sched.commit_sigkill(token, sent), wait)
                };
                // The sender's `kill` returns only once every victim's exit is
                // accounted and published to its parent. A parent that killed
                // its own child then finds the kernel's SIGCHLD pending when
                // `kill` returns, a point the schedule fixes, instead of
                // receiving it at a host-chosen moment in its own code. The
                // wait is bounded: past the valve the run is refused.
                let since = std::time::Instant::now();
                if wait {
                    loop {
                        {
                            let mut sched = self.lock_rpc_scheduler(false).await;
                            if sched.sigkill_settled(&victims) {
                                break;
                            }
                            if since.elapsed() >= sched.sigkill_fence_valve {
                                sched.refuse_unsettled_sigkill(since.elapsed());
                                break;
                            }
                        }
                        yield_once().await;
                    }
                }
                R::CommitSigkill(())
            }
            GlobalRequest::ThreadIsLive(dtid) => {
                R::ThreadIsLive(self.lock_rpc_scheduler(false).await.thread_is_live(dtid))
            }
            GlobalRequest::NamesARetiredExitedLeader(named) => R::NamesARetiredExitedLeader(
                self.lock_rpc_scheduler(false)
                    .await
                    .names_a_retired_exited_leader(named),
            ),
            GlobalRequest::ParentDeathSignal(arm) => {
                let mut sched = self.lock_rpc_scheduler(false).await;
                if let Some(signal) = arm {
                    sched.set_parent_death_signal(dtid, signal);
                }
                R::ParentDeathSignal(sched.parent_death_signal(dtid))
            }
            GlobalRequest::ThreadIdRefused(id) => {
                let (registered, zombie) = {
                    let sched = self.lock_rpc_scheduler(false).await;
                    (
                        sched.thread_was_registered(id),
                        sched.id_is_a_zombie_child_of(id, dtid),
                    )
                };
                let clocked = self.global_time.lock().unwrap().contains_thread(id);
                R::ThreadIdRefused((registered || clocked) && !zombie)
            }
            GlobalRequest::PendingRealTimeExpirations(detpid) => R::PendingRealTimeExpirations(
                self.lock_rpc_scheduler(false)
                    .await
                    .pending_real_time_expirations(detpid),
            ),
            GlobalRequest::RecordHandlerRestoreMask(detpid, unblocked) => {
                self.lock_rpc_scheduler(false)
                    .await
                    .record_handler_restore_unblocked(detpid, unblocked);
                R::RecordHandlerRestoreMask(())
            }
            GlobalRequest::RegisterSignalfd(detpid, mask) => {
                self.lock_rpc_scheduler(false)
                    .await
                    .register_signalfd(detpid, mask);
                R::RegisterSignalfd(())
            }
            GlobalRequest::DescriptorsReplaced(files, first, last) => {
                self.lock_rpc_scheduler(false)
                    .await
                    .note_descriptors_replaced(files, first, last);
                R::DescriptorsReplaced(())
            }
            GlobalRequest::DiscardPendingRealTimeExpirations(detpid, signals) => {
                self.lock_rpc_scheduler(false)
                    .await
                    .discard_pending_real_time_expirations(detpid, signals);
                R::DiscardPendingRealTimeExpirations(())
            }
            GlobalRequest::RecordHostTimedSignals(dtid, scope, signals) => R::HostTimedSignals(
                self.lock_rpc_scheduler(false)
                    .await
                    .record_host_timed_signals(dtid, scope, signals),
            ),
            GlobalRequest::HostTimedSignals(dtid) => R::HostTimedSignals(
                self.lock_rpc_scheduler(false)
                    .await
                    .host_timed_signals(dtid),
            ),
            GlobalRequest::HostTimedAndPrivateSigchld(dtid) => {
                let (host_timed, private) = self
                    .lock_rpc_scheduler(false)
                    .await
                    .host_timed_and_private_sigchld(dtid);
                R::HostTimedAndPrivateSigchld(host_timed, private)
            }
            GlobalRequest::ExactChildWaitState(parent, child) => R::ExactChildWaitState(
                self.lock_rpc_scheduler(false)
                    .await
                    .exact_child_wait_state(parent, child),
            ),
            GlobalRequest::ReadyChildWait(parent, selector) => {
                let sched = self.lock_rpc_scheduler(false).await;
                R::ReadyChildWait((
                    sched.ready_child_wait(parent, selector),
                    sched.has_child_wait_target(parent, selector),
                ))
            }
            GlobalRequest::ConsumeChildWait(parent, child) => R::ConsumeChildWait(
                self.lock_rpc_scheduler(false)
                    .await
                    .consume_child_wait(parent, child),
            ),
            GlobalRequest::ProcessGroup(process) => R::ProcessGroup(
                self.lock_rpc_scheduler(false)
                    .await
                    .thread_tree
                    .process_group(process),
            ),
            GlobalRequest::SetProcessGroup(process, group) => R::SetProcessGroup(
                self.lock_rpc_scheduler(false)
                    .await
                    .thread_tree
                    .set_process_group(process, group),
            ),
            GlobalRequest::CreateSession(process) => R::CreateSession(
                self.lock_rpc_scheduler(false)
                    .await
                    .thread_tree
                    .create_session(process),
            ),
            GlobalRequest::RegisteredProcessOf(dettid) => R::RegisteredProcessOf(
                self.lock_rpc_scheduler(false)
                    .await
                    .thread_tree
                    .process_of(dettid),
            ),
            GlobalRequest::ForgetThreadTimerCopies(process, signo) => {
                self.lock_rpc_scheduler(false)
                    .await
                    .forget_thread_timer_copies(process, signo);
                R::ForgetThreadTimerCopies(())
            }
            GlobalRequest::ExecTimerFlush(process) => R::ExecTimerFlush(
                self.lock_rpc_scheduler(false)
                    .await
                    .take_exec_timer_flush(dtid, process),
            ),
            GlobalRequest::RetireExitedLeader => R::RetireExitedLeader(
                self.lock_rpc_scheduler(false)
                    .await
                    .retire_exited_leader(dtid, request_mm),
            ),
            GlobalRequest::GuestPid(caller, pid) => {
                let sched = self.lock_rpc_scheduler(false).await;
                R::GuestPid(sched.guest_pid(caller, pid))
            }
            GlobalRequest::ProcfsListing(process) => {
                R::ProcfsListing(self.lock_rpc_scheduler(false).await.procfs_listing(process))
            }
            GlobalRequest::ProcfsIds(to_kernel, ids) => R::ProcfsIds(
                self.lock_rpc_scheduler(false)
                    .await
                    .procfs_ids(to_kernel, &ids),
            ),
            GlobalRequest::ProcessImage(process) => {
                let sched = self.lock_rpc_scheduler(false).await;
                // `/proc/<tid>/exe` names a thread's process's image.
                let process = sched
                    .registered_process(DetTid::from_raw(process.as_raw()))
                    .unwrap_or(process);
                R::ProcessImage(
                    self.exec_images
                        .lock()
                        .unwrap()
                        .get(&process)
                        .cloned()
                        .map(Box::new),
                )
            }
            GlobalRequest::RecordProcessAuxv(bytes) => {
                let sched = self.lock_rpc_scheduler(false).await;
                let process = sched.registered_process(dtid).unwrap_or(dtid);
                self.process_auxv.lock().unwrap().record(process, bytes);
                R::RecordProcessAuxv(())
            }
            GlobalRequest::ProcessAuxv(process) => {
                let sched = self.lock_rpc_scheduler(false).await;
                // `/proc/<tid>/auxv` is a thread's process's.
                let process = sched
                    .registered_process(DetTid::from_raw(process.as_raw()))
                    .unwrap_or(process);
                R::ProcessAuxv(self.process_auxv.lock().unwrap().current(process))
            }
            GlobalRequest::MarkChangedRoot => {
                // The sending task's own filesystem group: a thread created
                // without CLONE_FS has its own root.
                let _sched = self.lock_rpc_scheduler(false).await;
                self.process_auxv.lock().unwrap().mark_changed_root(dtid);
                R::MarkChangedRoot(())
            }
            GlobalRequest::RootChanged(task) => {
                // The task the path names: a process's number names its
                // leader thread, whose root Linux's `/proc/<pid>` shows.
                let _sched = self.lock_rpc_scheduler(false).await;
                R::RootChanged(self.process_auxv.lock().unwrap().root_changed(task))
            }
            GlobalRequest::DescriptionLocks(open_file, locks, slots) => {
                let sched = self.lock_rpc_scheduler(false).await;
                let process = sched.registered_process(dtid).unwrap_or(dtid);
                let mut ledger = self.lock_ledger.lock().unwrap();
                match locks {
                    Ok(locks) => ledger.note_locks(process, open_file, locks, slots),
                    Err(reason) => ledger.note_unknown(process, open_file, reason, slots),
                }
                R::DescriptionLocks(())
            }
            GlobalRequest::TableReleased(open_file, remaining) => {
                let sched = self.lock_rpc_scheduler(false).await;
                let process = sched.registered_process(dtid).unwrap_or(dtid);
                self.lock_ledger
                    .lock()
                    .unwrap()
                    .release(process, open_file, remaining);
                R::TableReleased(())
            }
            GlobalRequest::TableExec(surviving) => {
                let sched = self.lock_rpc_scheduler(false).await;
                let process = sched.registered_process(dtid).unwrap_or(dtid);
                self.lock_ledger
                    .lock()
                    .unwrap()
                    .exec_kept(process, &surviving);
                R::TableExec(())
            }
            GlobalRequest::AliasedInode(inode) => {
                let _sched = self.lock_rpc_scheduler(false).await;
                self.lock_ledger.lock().unwrap().alias_inode(inode);
                R::AliasedInode(())
            }
            GlobalRequest::LockLedgerRefused(reason) => {
                let _sched = self.lock_rpc_scheduler(false).await;
                self.lock_ledger.lock().unwrap().refuse(reason);
                R::LockLedgerRefused(())
            }
            GlobalRequest::GuestLockRows(rows) => {
                // A positive owner is the kernel's number for a process in the
                // container's PID namespace, as `/proc` spells it; it is the
                // guest's when it names a process `/proc` lists, in the
                // guest's numbering. Hermit's own init is not listed.
                let sched = self.lock_rpc_scheduler(false).await;
                let owners = rows
                    .iter()
                    .filter_map(|row| i32::try_from(row.owner).ok().filter(|owner| *owner > 0))
                    .collect::<BTreeSet<_>>()
                    .into_iter()
                    .collect::<Vec<_>>();
                let listed = sched.procfs_listing(None).processes;
                let guest_owners = owners
                    .iter()
                    .zip(sched.procfs_ids(false, &owners))
                    .filter(|(_, guest)| {
                        guest.is_some_and(|guest| listed.binary_search(&guest).is_ok())
                    })
                    .map(|(owner, _)| i64::from(*owner))
                    .collect::<BTreeSet<_>>();
                // A table a process that exited used, reaped or not,
                // references nothing (`LockLedger::prune`).
                let mut ledger = self.lock_ledger.lock().unwrap();
                ledger.prune(|process| sched.process_is_alive(process));
                // A referenced description whose locks are unknown could own
                // any OFD or flock row: the snapshot is refused
                // (`LockLedger::unknown`).
                if let Some(reason) = ledger.unknown() {
                    R::GuestLockRows(Err(reason.to_string()))
                } else {
                    let held = ledger.held();
                    R::GuestLockRows(Ok(crate::procfs::locks::guest_lock_rows(
                        &rows,
                        |owner| guest_owners.contains(&owner),
                        &held,
                    )))
                }
            }
            GlobalRequest::AuxvFromKernel(why) => {
                let sched = self.lock_rpc_scheduler(false).await;
                let process = sched.registered_process(dtid).unwrap_or(dtid);
                let mut table = self.process_auxv.lock().unwrap();
                table.forget(process);
                R::AuxvFromKernel(table.first_kernel_file(why))
            }
            GlobalRequest::GroupOrSession(pid, session) => R::GroupOrSession(
                self.lock_rpc_scheduler(false)
                    .await
                    .group_or_session_of(pid, session),
            ),
            GlobalRequest::SetsidDecision(process) => R::SetsidDecision(
                self.lock_rpc_scheduler(false)
                    .await
                    .setsid_decision(process)
                    .map_err(|errno| errno.into_raw()),
            ),
            GlobalRequest::SetpgidDecision(caller, pid, pgid) => R::SetpgidDecision(
                self.lock_rpc_scheduler(false)
                    .await
                    .setpgid_decision(caller, pid, pgid)
                    .map_err(|errno| errno.into_raw()),
            ),
            GlobalRequest::ProcfsTask(named, thread) => {
                let scheduler = self.lock_rpc_scheduler(false).await;
                let existence = scheduler.procfs_task(named, thread);
                let mut answer = self
                    .inodes
                    .lock()
                    .unwrap()
                    .procfs_task_answer(named, thread, existence);
                if existence.0 && !existence.1 {
                    answer.mm = scheduler.thread_mm(thread.unwrap_or(named));
                    answer.runtime_private_memory = answer
                        .mm
                        .and_then(|mm| scheduler.runtime_private_memory(mm));
                }
                R::ProcfsTask(answer)
            }
            GlobalRequest::RenderedFileState(inode, change) => R::RenderedFileState(
                self.inodes
                    .lock()
                    .unwrap()
                    .rendered_file_state(inode, change),
            ),
            GlobalRequest::LocalPid(caller, task) => R::LocalPid(
                self.lock_rpc_scheduler(false)
                    .await
                    .thread_tree
                    .pid_namespaces
                    .localize_for(caller, task),
            ),
            GlobalRequest::SignalTargetProcess(named) => R::SignalTargetProcess(
                self.lock_rpc_scheduler(false)
                    .await
                    .signal_target_process(named),
            ),
            GlobalRequest::PosixTimerThread(process, named) => R::PosixTimerThread(
                self.lock_rpc_scheduler(false)
                    .await
                    .posix_timer_thread(process, named),
            ),
            GlobalRequest::SetSigchldDisposition(process, ignored, nocldwait) => {
                R::SetSigchldDisposition(
                    self.lock_rpc_scheduler(false)
                        .await
                        .thread_tree
                        .set_sigchld_disposition(process, ignored, nocldwait),
                )
            }
            GlobalRequest::SetChildSubreaper(process, on) => R::SetChildSubreaper(
                self.lock_rpc_scheduler(false)
                    .await
                    .thread_tree
                    .set_child_subreaper(process, on),
            ),
            GlobalRequest::UnrecoverableShutdown => {
                self.force_shutdown_with_error();
                R::UnrecoverableShutdown(())
            }
            GlobalRequest::Network(request) => {
                let _sched = self.lock_rpc_scheduler(false).await;
                let mut engine = self
                    .network
                    .as_ref()
                    .expect("network request without a network trace mode")
                    .lock()
                    .unwrap();
                let now = self.global_time.lock().unwrap().as_nanos();
                R::Network(engine.apply(now, request))
            }
            GlobalRequest::RequestPort(open_file_id) => {
                let _sched = self.lock_rpc_scheduler(false).await;
                let mut mut_used_ports = self.used_ports.lock().unwrap();
                self.update_port_range();
                let total_available =
                    self.port_end_range.load(SeqCst) - self.port_start_range.load(SeqCst);
                let mut index = 0;
                while (*mut_used_ports).contains(&self.next_port.load(SeqCst))
                    && index < total_available
                {
                    self.next_port.fetch_add(1, SeqCst);
                    if self.next_port.load(SeqCst) > self.port_end_range.load(SeqCst) {
                        self.next_port
                            .store(self.port_start_range.load(SeqCst), SeqCst);
                    }
                    index += 1;
                }
                if index == total_available {
                    R::PortFull
                } else {
                    (*mut_used_ports).insert(self.next_port.load(SeqCst));
                    let mut open_file_to_port = self.open_file_to_port.lock().unwrap();
                    open_file_to_port.insert(open_file_id, self.next_port.load(SeqCst));
                    R::RequestPort(self.next_port.load(SeqCst))
                }
            }
            GlobalRequest::AddUsedPort(port, open_file_id) => {
                let _sched = self.lock_rpc_scheduler(false).await;
                let mut used_ports = self.used_ports.lock().unwrap();
                used_ports.insert(port);
                let mut open_file_to_port = self.open_file_to_port.lock().unwrap();
                open_file_to_port.insert(open_file_id, port);
                R::AddUsedPort
            }
            GlobalRequest::ReleasePort(open_file_id) => {
                let _sched = self.lock_rpc_scheduler(false).await;
                let mut used_ports = self.used_ports.lock().unwrap();
                let mut open_file_to_port = self.open_file_to_port.lock().unwrap();
                let port = open_file_to_port.remove(&open_file_id);
                if let Some(port) = port {
                    used_ports.remove(&port);
                }
                R::ReleasePort(port)
            }
        };

        // Awaited scheduler operations may have raced logical teardown. Never return their
        // operation-specific response after the sender acquired a permanent tombstone.
        // A SIGKILL committed meanwhile leaves the request unanswered, whatever
        // its operation-specific response, ThreadExited included.
        let (sender_became_terminal, sender_sigkill_retired) =
            if is_deregister || exec_reconnect.is_some() || is_exec_caller_after_local_mm_swap {
                (false, false)
            } else {
                let sched = self.lock_rpc_scheduler(consuming_cleanup).await;
                (
                    sched.thread_is_logically_killed(dtid)
                        || !sched.rpc_incarnation_matches(dtid, request_mm),
                    !answered_for_sigkill_victim && sched.is_unaccounted_sigkill_victim(dtid),
                )
            };
        if sender_sigkill_retired {
            return (None, R::Retired);
        }
        if resp == R::ThreadExited || sender_became_terminal {
            return (None, R::ThreadExited);
        }

        let time_from_sched = self.global_time.lock().unwrap().threads_time(dtid);
        let time_update = match time_from_sched.cmp(&time_from_guest) {
            Ordering::Equal => None,
            Ordering::Less => {
                panic!(
                    "internal error: thread time should never go down, only monotonically up: time in sched {}, thread local time was {}",
                    time_from_sched, time_from_guest
                )
            }
            Ordering::Greater => Some(time_from_sched),
        };
        (time_update, resp)
    }

    async fn recv_resources_with_origin(
        &self,
        from: Tid,
        detpid: DetPid,
        rs: Resources,
        request_mm: Option<MmId>,
        rpc: RpcOrigin,
        capability: ControlCapability,
    ) -> (SchedulerRpcResult<ResourceReply>, Option<LogicalTime>) {
        let dettid = DetTid::from_raw(from.into()); // TODO(T78538674): FIXME

        let resp2 = {
            let mut sched = self.lock_rpc_scheduler(false).await;
            if sched.thread_is_logically_killed(dettid)
                || request_mm.is_some_and(|mm| !sched.rpc_incarnation_matches(dettid, mm))
            {
                return (SchedulerRpcResult::ThreadExited, None);
            }
            // A host-timed request (a background operation's rejoin) can pass
            // admission just before a SIGKILL commit retires its thread. The
            // handler's response exit turns this into an unanswered request.
            if sched.is_unaccounted_sigkill_victim(dettid) {
                return (SchedulerRpcResult::ThreadExited, None);
            }
            let Some(nextturn) = sched.next_turns.get(&dettid).cloned() else {
                panic!(
                    "Detcore internal error: no entry for dettid {} in next_turns during resource request.",
                    dettid
                );
            };
            trace!(
                "[detcore, dtid {}] ResourceRequest, filling request into {}",
                &dettid, &nextturn.req
            );
            if let Some(mm) = request_mm
                && let Err(error) = sched.install_resource_origin(
                    dettid,
                    ResourceOrigin {
                        rpc,
                        mm,
                        control: capability,
                    },
                )
            {
                sched.fail_parked(dettid, error);
                return (SchedulerRpcResult::ThreadExited, None);
            }
            sched.request_put(&nextturn.req, rs.clone(), &self.global_time);
            nextturn.resp
        };
        trace!(
            "[detcore, dtid {}] waiting on {} for resources: {:?}",
            dettid, &resp2, rs
        );
        let answer = resp2.get().await; // Block on the scheduler allowing our guest to proceed.
        self.finish_resource_response(from, detpid, rs, request_mm, answer)
            .await
    }

    async fn finish_resource_response(
        &self,
        from: Tid,
        detpid: DetPid,
        rs: Resources,
        request_mm: Option<MmId>,
        answer: SchedResponse,
    ) -> (SchedulerRpcResult<ResourceReply>, Option<LogicalTime>) {
        let dettid = DetTid::from_raw(from.as_raw());
        let request_became_stale = {
            let sched = self.lock_rpc_scheduler(false).await;
            sched.thread_is_logically_killed(dettid)
                || request_mm.is_some_and(|mm| !sched.rpc_incarnation_matches(dettid, mm))
        };
        if request_became_stale {
            // `logically_kill_thread` wakes an already-pending request with a
            // signal response.  Treat that wake-up as terminal: otherwise a
            // caller that ignores `ResumeStatus::Signaled` can inject the
            // original syscall after the thread was logically removed.
            // TODO-HUMAN-REVIEW(PR-1023): Review pending SaBRe resource-request cancellation.
            trace!(
                "[detcore, dtid {}] terminating pending request after logical removal",
                dettid
            );
            return (SchedulerRpcResult::ThreadExited, None);
        }
        if matches!(answer, SchedResponse::InGuestFatalAlarm) {
            let mut sched = self.lock_rpc_scheduler(false).await;
            if !self.cfg.sequentialize_threads
                || !sched.in_guest_fatal_alarm_response_matches(dettid, detpid, request_mm, &rs)
            {
                sched.fail_parked(dettid, ProtocolFailure::UnexpectedControl);
                return (SchedulerRpcResult::ThreadExited, None);
            }
            return (
                SchedulerRpcResult::Continue(ResourceReply::Grant(ResumeStatus::InGuestFatalAlarm)),
                None,
            );
        }
        // A control spends only this response transport, not the resource
        // operation. It must precede exit-group or normal grant side effects.
        if let SchedResponse::ObserveSignal(control) = answer {
            return (
                SchedulerRpcResult::Continue(ResourceReply::ObserveSignal(control)),
                None,
            );
        }
        if let Some((true, process, mm)) = rs.exit_identity() {
            info!(
                "Scheduler authorized an exit-group scenario, from dettid {} / detpid {}",
                dettid, detpid
            );
            // Before allowing an `exit_group` to physically proceed, we
            // deregister the other threads in the thread group to reflect the
            // fact that they will not receive further logical turns.
            //
            // We trust the kernel to physically kill them irrespective of what they're
            // blocked on, including us having blocked them in the `futex_waiters` list.
            {
                let mut sched = self.lock_rpc_scheduler(false).await;
                if sched.thread_is_logically_killed(dettid)
                    || request_mm.is_some_and(|mm| !sched.rpc_incarnation_matches(dettid, mm))
                {
                    return (SchedulerRpcResult::ThreadExited, None);
                }
                for tid in sched.thread_tree.my_thread_group(&dettid) {
                    // We don't need to do anything extra for our own thread. That can use the
                    // same mechanics as a normal exit:
                    if tid != dettid {
                        sched.logically_kill_thread(&tid, &process, mm);
                    }
                }
            }
        }

        match answer {
            SchedResponse::ObserveSignal(_) | SchedResponse::InGuestFatalAlarm => {
                self.sched
                    .lock()
                    .unwrap()
                    .fail_parked(dettid, ProtocolFailure::UnexpectedControl);
                (SchedulerRpcResult::ThreadExited, None)
            }
            // In this context, SchedValue
            SchedResponse::Go(Some(schedval)) => {
                trace!(
                    "[dtid {}] resources granted, resuming normally: {:?}",
                    dettid, rs
                );

                let endtime_update = match schedval {
                    // Only syscalls timeout, and they don't need to update guest timeslice end.
                    // Nor does a futex requeue's answer, which a resource grant never carries.
                    SchedValue::TimeOut | SchedValue::Signaled(_) | SchedValue::Requeued { .. } => {
                        None
                    }
                    SchedValue::Value(timeslice) => Some(LogicalTime::from_nanos(timeslice)),
                };
                (
                    SchedulerRpcResult::Continue(ResourceReply::Grant(ResumeStatus::Normal)),
                    endtime_update,
                )
            }
            SchedResponse::GoAlone(schedval) => {
                trace!(
                    "[dtid {}] resources granted, and nothing else can run",
                    dettid
                );
                let endtime_update = match schedval {
                    Some(SchedValue::Value(timeslice)) => Some(LogicalTime::from_nanos(timeslice)),
                    _ => None,
                };
                (
                    SchedulerRpcResult::Continue(ResourceReply::Grant(
                        ResumeStatus::NothingElseCanRun,
                    )),
                    endtime_update,
                )
            }
            SchedResponse::Go(None) => {
                trace!(
                    "[dtid {}] resources granted but no timeslice specified",
                    dettid,
                );
                (
                    SchedulerRpcResult::Continue(ResourceReply::Grant(ResumeStatus::Normal)),
                    None,
                )
            }
            SchedResponse::Signaled(signal) => {
                trace!(
                    "[dtid {}] resources granted but interrupted by signal",
                    dettid,
                );
                (
                    SchedulerRpcResult::Continue(ResourceReply::Grant(ResumeStatus::Signaled(
                        signal,
                    ))),
                    None,
                )
            }
        }
    }

    async fn recv_release_resources(&self, from: Tid, rs: Resources) {
        // TODO(T78627377): add real resource-locking when we enable backgrounding actions.
        trace!("[detcore] Resources released to pid {}: {:?}", from, rs);
    }

    async fn recv_release_all_resources(&self, from: Tid) {
        // TODO(T78627377): add real resource-locking when we enable backgrounding actions.
        trace!("[detcore] All resources held by pid {} released", from);
    }

    /// Global portion of parent-forking-child protocol.  Called by the parent
    /// thread for an ordinary clone, or by the child itself for a vfork whose
    /// parent is blocked inside the kernel (`parent_is_kernel_blocked`).
    async fn recv_create_child_thread(
        &self,
        rpc_sender: Tid,
        request_mm: MmId,
        registration: ChildRegistration,
    ) -> SchedulerRpcResult<()> {
        let ChildRegistration {
            parent_dettid,
            parent_detpid,
            child_dettid,
            child_tid_addr: ctid,
            flags,
            exit_signal,
            physical_ids,
            maybe_priority,
            parent_is_kernel_blocked,
        } = registration;
        let initial_priority = if let Some(pr) = &self.preemptions_to_replay {
            assert!(maybe_priority.is_none());
            let prio = pr
                .thread_initial_priority(&child_dettid)
                .unwrap_or_else(|| {
                    warn!(
                        "Child thread {} not found in preemption history to replay",
                        child_dettid
                    );
                    DEFAULT_PRIORITY
                });
            if !is_ordinary_priority(prio) {
                panic!(
                    "Read a bad initial_prority from file: {}\nFull file: {}",
                    prio,
                    pr.load_all(),
                );
            }
            prio
        } else {
            let prio = maybe_priority.expect(
                "create_child_thread must take an initial priority unless replaying preemptions",
            );
            if !is_ordinary_priority(prio) {
                panic!(
                    "recv_create_child_thread received a bad prority argument : {}",
                    prio,
                );
            }
            prio
        };

        {
            let mut sched = self.lock_rpc_scheduler(false).await;
            let sender = DetTid::from_raw(rpc_sender.into());
            if sched.thread_is_logically_killed(sender)
                || !sched.rpc_incarnation_matches(sender, request_mm)
                || sched.thread_is_logically_killed(child_dettid)
            {
                return SchedulerRpcResult::ThreadExited;
            }
            // A process the backend already reported physically gone must not
            // be admitted: the scheduler would wait for a request it can never
            // send. A forked child is its own process.
            let child_detpid =
                if flags.is_some_and(|flags| flags.contains(CloneFlags::CLONE_THREAD)) {
                    parent_detpid
                } else {
                    DetPid::from_raw(child_dettid.as_raw())
                };
            if self.cfg.backend.process_exits_complete_asynchronously
                && sched.process_physically_gone(child_detpid)
            {
                drop(sched);
                self.report_backend_failure(reverie::BackendFailure {
                    pid: reverie::Pid::from_raw(child_detpid.as_raw()),
                    tid: Tid::from_raw(child_dettid.as_raw()),
                    phase: "a process was already physically gone when it was registered",
                });
                return SchedulerRpcResult::ThreadExited;
            }

            if parent_is_kernel_blocked && self.cfg.sequentialize_threads {
                sched.complete_vfork_registration(parent_dettid, child_dettid);
            }

            let child_mm = MmId::for_clone(
                request_mm,
                child_dettid,
                flags.is_some_and(|flags| flags.contains(CloneFlags::CLONE_VM)),
            );
            sched.register_reused_transferred_exec_tid(child_dettid, child_mm);
            sched.record_thread_mm(child_dettid, child_mm);
            // The guest's procfs paths name a task by its thread ID in the
            // guest's PID namespace, which is the raw value of its DetTid.
            self.inodes
                .lock()
                .unwrap()
                .count_incarnation(child_dettid.as_raw());

            // A new thread on a reused tid has not exited, and has published
            // no mask.
            sched.forget_committed_exit(child_dettid);
            sched.published_masks.remove(&child_dettid);
            // Don't fill in the request, as the child will do it:
            let _entry = sched
                .next_turns
                .entry(child_dettid)
                .or_insert_with(|| ThreadNextTurn {
                    dettid: child_dettid,
                    child_tid_addr: ctid,
                    req: Ivar::new(),
                    resp: Ivar::new(),
                    protocol: Default::default(),
                });

            {
                let is_group_leader = if let Some(f) = flags {
                    !f.contains(CloneFlags::CLONE_THREAD)
                } else {
                    true // root thread
                };
                sched.thread_tree.add_child_with_wait_metadata(
                    parent_dettid,
                    child_dettid,
                    is_group_leader,
                    flags.is_some_and(|flags| flags.contains(CloneFlags::CLONE_PARENT)),
                    exit_signal,
                );
                if is_group_leader {
                    sched.forget_previous_process_exit(child_dettid);
                    // The child's exit signal is posted to its parent at a
                    // host-timed moment: that pair stops being observable now,
                    // before the child can run (4262 design §3.4).
                    sched.child_ends_timer_observability(child_dettid);
                }
                let new_pid_namespace = is_group_leader
                    && flags.is_some_and(|flags| flags.contains(CloneFlags::CLONE_NEWPID));
                if new_pid_namespace {
                    sched.thread_tree.enter_new_pid_namespace(child_dettid);
                    sched.thread_tree.mark_pid_namespace_init(child_dettid);
                }
                // A nested task's pid in each namespace is fixed now, in its
                // creator's turn, before either runs again
                // (https://github.com/rrnewton/hermit/issues/4155).
                let pid_namespaces = &mut sched.thread_tree.pid_namespaces;
                let mut chain = pid_namespaces.chain_of(parent_dettid);
                if new_pid_namespace {
                    chain.push(child_dettid);
                }
                if let Some(reason) = pid_namespaces.untranslatable(parent_dettid) {
                    let reason = format!("its creator {parent_dettid}: {reason}");
                    pid_namespaces.mark_untranslatable(child_dettid, reason);
                } else if chain.is_empty() {
                    pid_namespaces.forget(child_dettid);
                } else {
                    let recorded =
                        crate::scheduler::pid_namespaces::read_nspid(child_detpid, child_dettid)
                            .and_then(|nspid| {
                                pid_namespaces
                                    .record(child_dettid, &chain, &nspid)
                                    .map_err(|error| error.to_string())
                            });
                    if let Err(reason) = recorded {
                        error!(
                            "[detcore, dtid {}] cannot record its pids in its PID namespaces: {}",
                            child_dettid, reason
                        );
                        pid_namespaces.mark_untranslatable(child_dettid, reason);
                    } else if is_group_leader {
                        sched.note_member_of_ended_namespace(child_dettid);
                    }
                }
                // A new task's root: its creator's filesystem group with
                // CLONE_FS, a copy of it without.
                self.process_auxv.lock().unwrap().clone_root(
                    parent_dettid,
                    child_dettid,
                    flags.is_some_and(|flags| flags.contains(CloneFlags::CLONE_FS)),
                );
                if is_group_leader && flags.is_some() {
                    // A new process runs its creator's image until it execs.
                    let mut images = self.exec_images.lock().unwrap();
                    match images.get(&parent_detpid).cloned() {
                        Some(image) => images.insert(child_dettid, image),
                        None => images.remove(&child_dettid),
                    };
                    drop(images);
                    self.process_auxv
                        .lock()
                        .unwrap()
                        .inherit(parent_detpid, child_dettid);
                    sched.inherit_real_time_records(parent_detpid, child_dettid);
                    // Its descriptor table: its creator's with CLONE_FILES, a
                    // copy without. Recorded here, in the creator's turn and
                    // before the child can run, so a lock the child takes
                    // first is never overwritten by its own fork
                    // (`procfs::locks::LockLedger::fork`).
                    self.lock_ledger.lock().unwrap().fork(
                        parent_detpid,
                        child_dettid,
                        flags.is_some_and(|flags| flags.contains(CloneFlags::CLONE_FILES)),
                    );
                }
                // A thread with a table of its own: the ledger counts its
                // calls as its process's, so that process's table can no
                // longer be followed (`procfs::locks::LockLedger::mark_opaque`).
                if !is_group_leader
                    && flags.is_some_and(|flags| !flags.contains(CloneFlags::CLONE_FILES))
                {
                    self.lock_ledger.lock().unwrap().mark_opaque(parent_detpid);
                }
            }

            if let Some((physical_pid, physical_tid)) = physical_ids {
                let child_is_thread =
                    flags.is_some_and(|flags| flags.contains(CloneFlags::CLONE_THREAD));
                let child_detpid = if child_is_thread {
                    parent_detpid
                } else {
                    child_dettid
                };
                if let Err(open_error) = sched.register_physical_thread(
                    child_dettid,
                    child_mm,
                    physical_pid,
                    physical_tid,
                ) {
                    error!(
                        "[detcore, dtid {}] cannot bind host process {} thread {} during child registration: {}",
                        child_dettid, physical_pid, physical_tid, open_error,
                    );
                    sched.logically_kill_thread(&child_dettid, &child_detpid, child_mm);
                    return SchedulerRpcResult::ThreadExited;
                }
            }

            // Bind only the existing root self-registration, after its
            // registration has succeeded. An arbitrary descendant's first
            // completion cannot claim a launcher-created input endpoint.
            if flags.is_none()
                && parent_dettid == child_dettid
                && parent_detpid == child_dettid
                && let Some(input) = self.inherited_input.lock().unwrap().as_mut()
                && input.root.is_none()
            {
                input.root = Some((child_dettid, child_mm));
            }
            // The same root self-registration names the thread whose first
            // image `Config::launch_image` describes.
            if flags.is_none() && parent_dettid == child_dettid && parent_detpid == child_dettid {
                let _ = self.root_dettid.set(child_dettid);
            }

            // Record this thread in deterministic creation order so a
            // happens-before anchor addressed by `spawn_ordinal` resolves to it.
            sched.hb_note_spawn(child_dettid);

            if self.cfg.replay_schedule_from.is_none() {
                // Give the thread an initial priority
                let old_prio = sched.priorities.insert(child_dettid, initial_priority);
                assert!(old_prio.is_none());
            } else {
                // In replay mode the schedule decides the thread's priority
                // (`Scheduler::replay_registration_priority`).
                if sched.replay_registration_priority(child_dettid, initial_priority) {
                    warn!(
                        "[detcore, dtid {}] registered before the replayed schedule switched to it, created by process {}; it takes its starting priority {}",
                        child_dettid, parent_detpid, initial_priority
                    );
                }
            }

            if let Some(pr) = &mut sched.preemption_writer {
                pr.register_thread(child_dettid, initial_priority);
            }

            // Describe *how* the admission side is chosen, but do not resolve it
            // (and in particular do not draw the post-fork PRNG) here: this
            // handler runs on whichever backend worker fielded the RPC, so on an
            // asynchronous backend (e.g. DBT, where the child self-registers
            // outside a scheduler turn) resolving the side now would consume the
            // PRNG draw in host RPC order. `admit_to_run_queue` resolves the
            // intent at the step2 drain -- which under ptrace is post-commit, in
            // schedule order, unchanged. Read its "What this does and does not
            // make deterministic" section before relying on that word: the drain
            // is a deterministic *point* and resolution within one drain is
            // `DetTid`-ordered, but on an asynchronous backend which drain a
            // given admission lands in is not itself schedule-determined unless
            // that admission is anchored (ordinary clone is, via the parent's
            // `ParentContinue`; vfork is, via `step2a`'s barrier). When threads
            // are not sequentialized, or the
            // parent is already kernel-blocked (vfork), the child takes the tail
            // and no PRNG is consumed.
            let intent = if self.cfg.sequentialize_threads && !parent_is_kernel_blocked {
                AdmitIntent::PostFork(self.cfg.runs_post_fork)
            } else {
                AdmitIntent::Fixed(AdmitSide::Back)
            };
            sched.admit_to_run_queue(child_dettid, intent);
            debug!(
                "[detcore] CreateChildThread with dtid {}: admit child via {:?}.",
                child_dettid, intent,
            );
            sched.started_up.try_put(());
        }
        // The child queue position above determines which equal-priority side
        // gets the first turn when the parent requests ParentContinue.
        // A vfork parent is already blocked by the kernel and is not in the run
        // queue, so it must not issue a ParentContinue request here.
        if self.cfg.sequentialize_threads && !parent_is_kernel_blocked {
            let mut rs = Resources::new(parent_detpid);
            rs.insert(
                ResourceID::ParentContinue {
                    parent: parent_dettid,
                    child: child_dettid,
                },
                Permission::W,
            );
            if matches!(
                self.recv_grant_resources(
                    rpc_sender,
                    parent_detpid,
                    rs,
                    Some(request_mm),
                    RpcOrigin::ParentContinue
                )
                .await
                .0,
                SchedulerRpcResult::ThreadExited
            ) {
                return SchedulerRpcResult::ThreadExited;
            }
        }
        SchedulerRpcResult::Continue(())
    }

    /// Called by the child thread upon startup.
    /// Returns a thread-preemption history for the new guest thread (if --replay-preemptions-from
    /// is used).
    async fn recv_start_new_thread(
        &self,
        from: Tid,
        dettid: DetTid,
        detpid: DetPid,
        request_mm: MmId,
        physical_ids: Option<(i32, i32)>,
        signal_identity: Option<reverie::SignalTaskIdentity>,
    ) -> SchedulerRpcResult<Option<ThreadHistory>> {
        let mut tries: u64 = 0;
        // TODO: eliminate this loop. Could instead signal with an ivar.
        let response_ivar = loop {
            yield_once().await;
            let mut sched = self.lock_rpc_scheduler(false).await;
            if sched.thread_is_logically_killed(dettid)
                || !sched.rpc_incarnation_matches(dettid, request_mm)
            {
                return SchedulerRpcResult::ThreadExited;
            }
            if self.cfg.backend.process_exits_complete_asynchronously
                && sched.process_physically_gone(detpid)
            {
                drop(sched);
                self.report_backend_failure(reverie::BackendFailure {
                    pid: reverie::Pid::from_raw(detpid.as_raw()),
                    tid: Tid::from_raw(dettid.as_raw()),
                    phase: "a process was already physically gone when it started",
                });
                return SchedulerRpcResult::ThreadExited;
            }
            if self.cfg.backend.requires_thread_directed_process_signals && physical_ids.is_none() {
                error!(
                    "[detcore, dtid {}] backend requires a host thread ID at StartNewThread",
                    dettid,
                );
                sched.logically_kill_thread(&dettid, &detpid, request_mm);
                return SchedulerRpcResult::ThreadExited;
            }
            // The resources that must be held for the fresh thread to run:
            let rsrcs = {
                let mut s = HashMap::new();
                s.insert(ResourceID::MemAddrSpace(detpid), Permission::RW); // TODO(T78055411): track mem aliasing.
                Resources {
                    tid: dettid,
                    resources: s,
                    poll_attempt: 0,
                    fyi: String::new(),
                    signal_interrupt_errno: None,
                    backend_runtime_bootstrap: false,
                    blocked_signal_mask: None,
                    blocked_signal_mask_unknown: false,
                    replay_served_from_log: false,
                    reads_signalfd: false,
                    read_fd: None,
                    polls_guest_state_only: false,
                    waits_until_nothing_else_can_run: false,
                    previous_attempt_made_progress: false,
                    upgraded_from_attempt: 0,
                    polls_channel: None,
                    poll_deadline: None,
                    in_guest_sleep_sigalrm: None,
                    signal_state_task: None,
                    decides_signal_interruption_itself: false,
                    continues_sigreturn: false,
                    private_sigchld_decides: false,
                }
            };
            let nextturn = match sched.next_turns.entry(dettid) {
                Entry::Vacant(_entry) => {
                    // CreateChildThread on the parent hasn't run yet.

                    // TODO: We could try to populate the entry since we get here
                    // first, but currently we lack the information right here to
                    // populate the child_tid_addr field.
                    if tries == 0 {
                        trace!(
                            "[detcore, dtid {}] thread showed up early, no queue entry yet.  Waiting...",
                            dettid
                        );
                    }
                    tries += 1;
                    continue;
                }
                Entry::Occupied(entry) => {
                    trace!(
                        "[detcore, dtid {}] handling StartNewThread rpc.  Found next_turns entry (after {} tries)",
                        from, tries
                    );
                    entry.get().clone()
                }
            };
            if let Some((physical_pid, physical_tid)) = physical_ids
                && let Err(open_error) =
                    sched.register_physical_thread(dettid, request_mm, physical_pid, physical_tid)
            {
                error!(
                    "[detcore, dtid {}] cannot bind host process {} thread {} for exact signal delivery: {}",
                    dettid, physical_pid, physical_tid, open_error,
                );
                sched.logically_kill_thread(&dettid, &detpid, request_mm);
                return SchedulerRpcResult::ThreadExited;
            }
            if sched.signal_control_installed() {
                let binding = signal_identity
                    .ok_or(TimerFailure::Identity)
                    .and_then(|identity| {
                        if from.as_raw() != dettid.as_raw()
                            || sched.registered_process(dettid) != Some(detpid)
                        {
                            return Err(TimerFailure::Identity);
                        }
                        sched.real_timers.bind(detpid, dettid, request_mm, identity)
                    });
                if let Err(error) = binding {
                    sched.fail_parked(dettid, ProtocolFailure::Timer(error));
                    return SchedulerRpcResult::ThreadExited;
                }
            }
            if let Err(error) = sched.install_resource_origin(
                dettid,
                ResourceOrigin {
                    rpc: RpcOrigin::ThreadStart,
                    mm: request_mm,
                    control: ControlCapability::None,
                },
            ) {
                sched.fail_parked(dettid, error);
                return SchedulerRpcResult::ThreadExited;
            }
            sched.request_put(&nextturn.req, rsrcs, &self.global_time);
            break nextturn.resp;
        };
        debug!(
            "[detcore, dtid {}] New thread will now wait for response on {}...",
            &dettid, &response_ivar
        );
        let answer = response_ivar.get().await;
        if matches!(
            answer,
            SchedResponse::ObserveSignal(_) | SchedResponse::InGuestFatalAlarm
        ) {
            self.sched
                .lock()
                .unwrap()
                .fail_parked(dettid, ProtocolFailure::UnexpectedControl);
            return SchedulerRpcResult::ThreadExited;
        }
        let request_became_stale = {
            let sched = self.lock_rpc_scheduler(false).await;
            sched.thread_is_logically_killed(dettid)
                || !sched.rpc_incarnation_matches(dettid, request_mm)
        };
        if request_became_stale {
            return SchedulerRpcResult::ThreadExited;
        }
        info!(
            "[detcore, dtid {}] New thread given go-ahead to proceed via {}",
            &dettid, &response_ivar
        );
        if let Some(pr) = &self.preemptions_to_replay {
            let (history, old_prio) = {
                let mut sched = self.lock_rpc_scheduler(false).await;
                if sched.thread_is_logically_killed(dettid)
                    || !sched.rpc_incarnation_matches(dettid, request_mm)
                {
                    return SchedulerRpcResult::ThreadExited;
                }
                let history = pr.extract_thread_record(&dettid).unwrap_or_else(|| {
                    warn!(
                        "Replaying preemptions, but no record found for thread {}",
                        dettid
                    );
                    ThreadHistory::new()
                });
                let old_prio = sched.priorities.insert(dettid, history.initial_priority());
                (history, old_prio)
            };
            debug!(
                "[replay-preemption] Enqueing new thread at priority {:?} (changed from {:?})",
                history.initial_priority(),
                old_prio,
            );
            SchedulerRpcResult::Continue(Some(history))
        } else {
            SchedulerRpcResult::Continue(None)
        }
    }

    /// Warning: this happens completely asynchronously, whenever the guest exit hook fires.
    /// Its timing is not coordinated by the scheduler.
    async fn recv_deregister_thread(&self, _from: Tid, mut deregistration: ThreadDeregistration) {
        // Keep the same scheduler -> pending-exec lock order as reconnect.
        let mut sched = self.sched.lock().unwrap();
        let ThreadDeregistration {
            dettid, detpid, mm, ..
        } = &deregistration;
        // A fatal signal can tear down the caller after its local state has advanced to the
        // candidate exec image but before the successful reconnect (or failed-exec cancel).
        // Retire that one-shot preparation before scheduler-incarnation admission rejects the
        // candidate image's final cleanup RPC.
        let mut pending = self.pending_exec_states.lock().unwrap();
        if pending.get(detpid).is_some_and(|state| {
            state.caller == *dettid && (*mm == state.mm || *mm == state.mm.for_exec(state.process))
        }) {
            let prepared = pending.remove(detpid).unwrap();
            // Unlike a recoverable failure (CancelExec), the caller's old image
            // deregistered before any reconnect: its group is dying, and Linux
            // walks every robust list of the group, in the exec or in each
            // thread's exit. On DBT this takes a fatal signal during the exec.
            // An exec whose target the kernel cannot load does not get here:
            // DynamoRIO's own exec succeeds, the new image reconnects and its
            // wakes are queued there, and the run then hangs on that image's
            // unretired death (https://github.com/rrnewton/hermit/issues/4362).
            // Hold turns until the kernel has ended the group, as an
            // exit_group's grant does, and wake the prepared waiters now, while
            // the caller's outstanding exec request still withholds every turn;
            // they run at the drain after the hold. A group that outlives this
            // is ended by the exited-thread valve with a named HERMIT_DEADLOCK
            // rather than losing the wakes (Codex review of
            // https://github.com/rrnewton/hermit/pull/4352, P2).
            if !prepared.fd_blocking.robust_wakes.is_empty() {
                sched.hold_turns_until_the_group_ends(*detpid);
                wake_exec_robust_waiters(
                    &mut sched,
                    &prepared.fd_blocking.robust_wakes,
                    "terminal exec",
                );
            }
            sched.finish_exec_teardown(prepared.caller, *detpid, prepared.mm, false);
            // The local caller can have installed its candidate exec MmId
            // before cancellation. It still owns the original registration;
            // consume that exact prepared incarnation rather than discarding
            // cleanup as a stale request after removing the preparation.
            deregistration.mm = prepared.mm;
        }
        drop(pending);

        // Invariant: will only be called when sequentialize-threads is on.
        assert!(self.cfg.sequentialize_threads);
        self.account_deregistered_thread(&mut sched, deregistration);
    }

    /// Consume final statistics and registration while holding admission closed.
    /// Authenticated exec cleanup uses this same once-only accounting path.
    fn account_deregistered_thread(
        &self,
        sched: &mut Scheduler,
        deregistration: ThreadDeregistration,
    ) {
        let ThreadDeregistration {
            dettid,
            detpid,
            mm,
            thread_start_entered: _,
            timeslice_stats,
            syscall_count,
            chaos_epochs,
        } = deregistration;
        if !sched.rpc_incarnation_matches(dettid, mm) {
            debug!(
                "[detcore, dtid {}] ignoring deregistration from retired exec incarnation {:?}",
                dettid, mm,
            );
            return;
        }
        self.post_exec_fd_blocking.lock().unwrap().remove(&dettid);
        // A SIGKILL victim's cleanup is accounted exactly once
        // (`scheduler::sigkill_retirement`), also on a backend that otherwise
        // accepts a repeated callback.
        if !sched.note_deregistration_accounted(dettid)
            || sched.sigkill_receipt_already_counted(dettid)
        {
            trace!(
                "[detcore, dtid {}] acknowledging already-accounted deregistration",
                dettid
            );
            return;
        }
        if let Some(writer) = &mut sched.preemption_writer {
            for transition in chaos_epochs {
                writer.insert_chaos_epoch(dettid, transition);
            }
        }
        sched.record_timeslice_stats(dettid, timeslice_stats);
        sched.record_syscall_count(dettid, syscall_count);
        // A SIGKILL victim is retired at its sender's turn, not here
        // (`scheduler::sigkill_retirement`): its receipt is accounted above
        // and only counted.
        if sched.route_sigkill_receipt(dettid)
            != crate::scheduler::sigkill_retirement::SigkillReceipt::Ordinary
        {
            return;
        }
        sched.retire_deregistered_thread(dettid, detpid, mm);
        trace!(
            "[detcore, dtid {}] thread deregistered, removed from sched structures.",
            dettid
        );
    }

    async fn recv_futex_action(
        &self,
        caller: RpcIncarnation,
        action: FutexAction,
        futexid: FutexID,
        init_read: i32,
        mask: u32,
    ) -> Option<SchedValue> {
        let RpcIncarnation { dettid, mm } = caller;
        trace!("[detcore, dtid {}] Futex action: {:?}", &dettid, action);
        let response_iv = {
            let mut sched = self.lock_rpc_scheduler(false).await;
            if sched.thread_is_logically_killed(dettid)
                || !sched.rpc_incarnation_matches(dettid, mm)
            {
                return Some(SchedValue::Value(nix::errno::Errno::EINTR as u64));
            }
            let Some(resp_iv) = sched
                .next_turns
                .get(&dettid)
                .map(|nextturn| nextturn.resp.clone())
            else {
                // AUTONOMOUS-BOT-IMPLEMENTED
                // TODO-HUMAN-REVIEW(PR-845): Review late RPCs from exit-group siblings.
                trace!(
                    "[detcore, dtid {}] ignoring futex action after logical thread removal",
                    dettid
                );
                return Some(SchedValue::Value(nix::errno::Errno::EINTR as u64));
            };
            match action {
                FutexAction::WaitRequest(maybe_timeout, signal_watch) => {
                    if sched.child_tid_was_cleared(futexid, init_read) {
                        trace!(
                            "[detcore, dtid {}] late wait on cleared child-TID futex {:?}",
                            dettid, futexid
                        );
                        return Some(SchedValue::Value(0));
                    }
                    if let Err(error) = sched.install_resource_origin(
                        dettid,
                        ResourceOrigin {
                            rpc: RpcOrigin::FutexAction,
                            mm,
                            control: ControlCapability::None,
                        },
                    ) {
                        sched.fail_parked(dettid, error);
                        return Some(SchedValue::Value(nix::errno::Errno::EINTR as u64));
                    }
                    sched.sleep_futex_waiter(&dettid, futexid, maybe_timeout, mask, signal_watch);
                    // block on ivar, below
                }
                FutexAction::WaitFinished => {
                    return None;
                }
                FutexAction::WakeRequest(num_threads) => {
                    let num = sched.wake_futex_waiters(dettid, futexid, num_threads, mask);
                    if self.cfg.yields_after_futex_wake(num) {
                        sched.requeue_futex_waker(dettid);
                    }
                    return Some(SchedValue::Value(num));
                }
                FutexAction::WakeFinished(_num_threads) => {
                    return None;
                }
                FutexAction::RequeueRequest(to, nr_wake, nr_requeue) => {
                    let (woken, moved) = sched.requeue_futex_waiters(
                        futexid,
                        to,
                        nr_wake as usize,
                        nr_requeue as usize,
                    );
                    if self.cfg.yields_after_futex_wake(woken) {
                        sched.requeue_futex_waker(dettid);
                    }
                    return Some(SchedValue::Requeued { woken, moved });
                }
            }
            // Blocking on the FUTEX_WAIT here, remove ourselves:
            assert!(sched.run_queue.remove_tid(dettid));
            resp_iv
        };
        // Wait for wake+scheduler response.
        match response_iv.get().await {
            // A futex wait never asks for `GoAlone`.
            SchedResponse::Go(answer) | SchedResponse::GoAlone(answer) => {
                trace!(
                    "[detcore, dtid {}] Unblocked from futex_wait! ({})",
                    &dettid, &response_iv
                );
                answer
            }
            SchedResponse::Signaled(signals) => Some(SchedValue::Signaled(
                signals.iter().flatten().fold(0, |set, signal| {
                    set | crate::scheduler::kernel_signal_bit(signal.0)
                }),
            )),
            SchedResponse::ObserveSignal(_) | SchedResponse::InGuestFatalAlarm => {
                self.sched
                    .lock()
                    .unwrap()
                    .fail_parked(dettid, ProtocolFailure::UnexpectedControl);
                self.wait_for_backend_failure().await;
                futures::future::pending().await
            }
        }
    }

    fn recv_robust_list_wakes(&self, from: DetTid, wakes: Vec<(DetTid, FutexID)>) -> Vec<u64> {
        let mut sched = self.sched.lock().unwrap();
        if sched.retain_sigkill_victim_wakes(from, &wakes) {
            // A SIGKILL victim's batch runs with its cohort; the empty answer
            // has no counts, so the victim logs nothing for it.
            return Vec::new();
        }
        sched.wake_futex_waiters_after_exit(&wakes)
    }

    /// The epoch as a logical time: the virtual mtime of a file first seen
    /// with a non-canonical host mtime.
    fn epoch_logical_time(&self) -> LogicalTime {
        let nanos = self
            .cfg
            .epoch
            .timestamp_nanos_opt()
            .expect("epoch cannot be represented in a timestamp with nanosecond precision")
            as u64;
        LogicalTime::from_nanos(nanos)
    }

    async fn recv_determinize_inode(
        &self,
        from: Tid,
        ino: RawInode,
        procfs: Option<ProcfsName>,
        observed: ObservedMtime,
        sighting: InodeSighting,
    ) -> (DetInode, LogicalTime) {
        let _sched = self.lock_rpc_scheduler(false).await;
        // Here we establish a policy that when we first see a file its mtime is
        // the epoch, unless its host mtime is one of the canonical values in
        // `CANONICAL_FILE_MTIME_SECONDS`, which it keeps.
        let epoch = self.epoch_logical_time();
        let mut pool = self.inodes.lock().unwrap();
        if let Some(name) = procfs {
            let path = pool.procfs_path_key(&name);
            pool.procfs_key(ino, path);
        }
        let (dino, ns) = pool.add_sighted_inode(ino, observed, epoch, sighting);
        drop(pool);
        trace!(
            "[detcore, dtid {}] resolved (raw) inode {:?} to {:?}, mtime {}",
            from, ino, dino, ns
        );
        (dino, ns)
    }

    async fn recv_determinize_mapping_inodes(
        &self,
        from: Tid,
        inodes: Vec<RawInode>,
    ) -> Vec<DetInode> {
        let _sched = self.lock_rpc_scheduler(false).await;
        let epoch = self.epoch_logical_time();
        let mut pool = self.inodes.lock().unwrap();
        let dinos: Vec<DetInode> = inodes
            .iter()
            .map(|ino| pool.add_inode(*ino, ObservedMtime::Unobserved, epoch).0)
            .collect();
        trace!(
            "[detcore, dtid {}] resolved mapping (raw) inodes {:?} to {:?}",
            from, inodes, dinos
        );
        dinos
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping RPC.
    async fn recv_determinize_device(&self, from: Tid, raw_device: u64) -> u64 {
        let _sched = self.lock_rpc_scheduler(false).await;
        let det_device = self.devices.lock().unwrap().determinize(raw_device);
        trace!(
            "[detcore, dtid {}] resolved (raw) device {} to {}",
            from, raw_device, det_device
        );
        det_device
    }

    async fn recv_determinize_mount_id(
        &self,
        from: Tid,
        raw_mount_id: u64,
        mountinfo_order: Option<&[u64]>,
    ) -> Option<u64> {
        let _sched = self.lock_rpc_scheduler(false).await;
        let virtual_mount_id = self
            .mount_ids
            .lock()
            .unwrap()
            .determinize(raw_mount_id, mountinfo_order);
        trace!(
            "[detcore, dtid {}] resolved fdinfo mount ID {} to {:?}",
            from, raw_mount_id, virtual_mount_id
        );
        virtual_mount_id
    }

    async fn recv_resolve_mountinfo_ids(
        &self,
        from: Tid,
        mountinfo_order: &[u64],
        observation_order: &[u64],
    ) -> Option<Vec<u64>> {
        let _sched = self.lock_rpc_scheduler(false).await;
        let virtual_mount_ids = self
            .mount_ids
            .lock()
            .unwrap()
            .resolve_mountinfo(mountinfo_order, observation_order);
        trace!(
            "[detcore, dtid {}] resolved mountinfo observation order {:?} (canonical {:?}) to {:?}",
            from, observation_order, mountinfo_order, virtual_mount_ids
        );
        virtual_mount_ids
    }

    async fn recv_retire_inode(&self, from: Tid, ino: RawInode) {
        let _sched = self.lock_rpc_scheduler(false).await;
        trace!("[detcore, dtid {}] retire (raw) inode {:?}", from, ino);
        self.inodes.lock().unwrap().retire(ino);
    }

    async fn recv_forget_retired_inode(
        &self,
        from: Tid,
        ino: RawInode,
        procfs: Option<ProcfsName>,
    ) {
        let _sched = self.lock_rpc_scheduler(false).await;
        trace!(
            "[detcore, dtid {}] forget retired (raw) inode {:?}",
            from, ino
        );
        let mut pool = self.inodes.lock().unwrap();
        if let Some(name) = procfs {
            let path = pool.procfs_path_key(&name);
            pool.procfs_key(ino, path);
        }
        pool.forget_retired(ino);
    }

    async fn recv_touch_file(&self, from: Tid, ino: RawInode) {
        let _sched = self.lock_rpc_scheduler(false).await;
        let mtime = if self.cfg.virtualize_time {
            self.global_time.lock().unwrap().as_nanos()
        } else {
            // In this scenario, virtualize_metadata is set and virtualize_time isn't.
            // We virtualize initial mtimes, but update using realtime.
            let dt: DateTime<Utc> = Utc::now();
            let nanos = dt.timestamp_nanos_opt().expect(
                "current time cannot be represented in a timestamp with nanosecond precision",
            ) as u64;
            LogicalTime::from_nanos(nanos)
        };
        trace!(
            "[dtid {}] bumping mtime on file (rawinode {:?}) to {}",
            from, ino, mtime,
        );
        self.set_inode_mtime(ino, mtime);
    }

    /// Set the virtual mtime to the time the guest asked for with utimensat
    /// (or utime/utimes, which are routed through it). Without this, a file's
    /// mtime is the time of its last write, so `tar` extraction, `cp -p` and
    /// `touch -r` could not restore one, and `make` saw files ordered by when
    /// they were unpacked.
    async fn recv_set_file_mtime(&self, from: Tid, ino: RawInode, mtime: LogicalTime) {
        let _sched = self.lock_rpc_scheduler(false).await;
        trace!(
            "[dtid {}] setting mtime on file (rawinode {:?}) to {}",
            from, ino, mtime,
        );
        self.set_inode_mtime(ino, mtime);
    }

    fn set_inode_mtime(&self, ino: RawInode, mtime: LogicalTime) {
        self.inodes.lock().unwrap().set_mtime(ino, mtime);
    }

    async fn recv_trace_schedevent(
        &self,
        ev: SchedEvent,
        detpid: DetPid,
        request_mm: MmId,
        command_bootstrap: bool,
    ) -> SchedulerRpcResult<TraceSchedEventResponse> {
        let ev = {
            let sched = self.lock_rpc_scheduler(false).await;
            if !sched.rpc_incarnation_matches(ev.dettid, request_mm) {
                return SchedulerRpcResult::ThreadExited;
            }
            // TODO(T124316762): debug address randomization in the tracer and get rid of this hack:
            let ev = {
                if self.past_first_execve.load(SeqCst) {
                    ev
                } else {
                    info!(
                        "Warning: erasing rip of pre-execve sched event! {:?}",
                        SchedEventForLog {
                            event: &ev,
                            command_bootstrap
                        }
                    );
                    SchedEvent {
                        end_rip: None,
                        start_rip: None,
                        ..ev
                    }
                }
            };
            // Future trace_schedevent calls will retain their rip values.
            if ev.op == Op::Syscall(Sysno::execve, SyscallPhase::Prehook) {
                self.past_first_execve.store(true, SeqCst);
            }
            ev
        };

        // Yield this guest thread if needed to follow schedule.
        let result = if self.cfg.replay_schedule_from.is_some() {
            let (consumed, print_stack2) = {
                let mut sched = self.lock_rpc_scheduler(false).await;
                if sched.thread_is_logically_killed(ev.dettid)
                    || !sched.rpc_incarnation_matches(ev.dettid, request_mm)
                {
                    return SchedulerRpcResult::ThreadExited;
                }
                let consumed = sched.consume_schedevent(&ev);
                let print_stack2 = if self.cfg.record_preemptions {
                    sched.record_event(&ev)
                } else {
                    None
                };
                (consumed, print_stack2)
            };
            let ConsumeResult {
                keep_running,
                print_stack,
                event_ix: _,
                timeslice_remaining: mut end_of_timeslice,
            } = consumed;
            trace!(
                "keep_running :{}, end_of_timeslice: {:?}",
                keep_running, end_of_timeslice
            );

            if !keep_running {
                trace!(
                    "[detcore, dtid {}] Thread yielding to follow replay schedule",
                    &ev.dettid,
                );
                let tid = reverie::Tid::from(ev.dettid.as_raw()); // TODO(T78538674): virtualize pid/tid:
                let mut rsrcs = Resources::new(ev.dettid);
                rsrcs.insert(ResourceID::TraceReplay, Permission::RW);
                let (response, timeslice) = self
                    .recv_grant_resources(
                        tid,
                        detpid,
                        rsrcs,
                        Some(request_mm),
                        RpcOrigin::TraceSchedEvent,
                    )
                    .await;
                if response == SchedulerRpcResult::ThreadExited {
                    return SchedulerRpcResult::ThreadExited;
                }
                end_of_timeslice = timeslice;
                trace!(
                    "[detcore, dtid {}] Thread reactivated after yielding for replay schedule",
                    &ev.dettid,
                );
            }

            TraceSchedEventResponse {
                print_stack_strace: print_stack.or(print_stack2),
                timeslice: end_of_timeslice,
            }
        } else {
            let print_stack_strace = {
                let mut sched = self.lock_rpc_scheduler(false).await;
                if sched.thread_is_logically_killed(ev.dettid)
                    || !sched.rpc_incarnation_matches(ev.dettid, request_mm)
                {
                    return SchedulerRpcResult::ThreadExited;
                }
                if self.cfg.record_preemptions {
                    sched.record_event(&ev)
                } else {
                    None
                }
            };
            TraceSchedEventResponse {
                print_stack_strace,
                timeslice: None,
            }
        };

        if result.print_stack_strace.is_some()
            && let Some(sig) = &self.cfg.stacktrace_signal
        {
            let _sched = self.lock_rpc_scheduler(false).await;
            trace!(
                "[dtid {}] signaling thread with {} at the point of stack trace printing.",
                ev.dettid, sig.0
            );
            let tid = Pid::from_raw(ev.dettid.as_raw());
            // TODO(T78538674): virtualize pid/tid:
            // We send a raw signal here and let the guest pick it up WHENEVER it resumes.
            // We don't use the "signal_guest" method because we don't necessarily respect that
            // protocol here.
            // Alarm/timer signals are guest-chosen and may be realtime, which
            // `nix` cannot name; fall through to the raw syscall for those.
            match sig.signal() {
                Some(named) => signal::kill(tid, named).unwrap(),
                None => {
                    // SAFETY: `tid` is a live thread this scheduler owns and
                    // `sig.raw()` is a signal number the guest already supplied.
                    let rc = unsafe { libc::kill(tid.as_raw(), sig.raw()) };
                    assert_eq!(rc, 0, "raw kill of signal {} failed", sig.raw());
                }
            }
        }

        SchedulerRpcResult::Continue(result)
    }

    // Ephemeral port range is in file /proc/sys/net/ipv4/ip_local_port_range"
    // This function reads from the file and returns the range
    // Start of range is at index 0, end of range is at index 1.
    fn read_port_range() -> Vec<u16> {
        let contents = fs::read_to_string("/proc/sys/net/ipv4/ip_local_port_range")
            .expect("File should be present");
        let range: Vec<u16> = contents
            .split_whitespace()
            .filter_map(|number| number.parse().ok())
            .collect();
        range
    }

    // Reflect ephemeral port range updated outside of the tracer program internally.
    fn update_port_range(&self) {
        let range = Self::read_port_range();
        self.port_start_range.store(range[0], SeqCst);
        self.port_end_range.store(range[1], SeqCst);
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#663)
    // TODO-HUMAN-REVIEW(#869)
    /// Register an alarm (delayed signal delivery) with the global scheduler.
    async fn recv_register_alarm(
        &self,
        detpid: DetPid,
        caller: RpcIncarnation,
        now: LogicalTime,
        duration: LogicalTime,
        interval: LogicalTime,
        sig: SigWrapper,
    ) -> SchedulerRpcResult<(LogicalTime, LogicalTime)> {
        let RpcIncarnation { dettid, mm } = caller;
        let mut sched = self.lock_rpc_scheduler(false).await;
        if sched.thread_is_logically_killed(dettid) || !sched.rpc_incarnation_matches(dettid, mm) {
            return SchedulerRpcResult::ThreadExited;
        }
        match sched.replace_real_timer(detpid, dettid, now, duration, interval, alarm_signal(sig)) {
            Ok(old) => SchedulerRpcResult::Continue(old),
            Err(error) => {
                sched.fail_parked(dettid, ProtocolFailure::Timer(error));
                SchedulerRpcResult::ThreadExited
            }
        }
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#869)
    /// Register, re-arm, or disarm a POSIX timer in the global scheduler.
    /// `notification` is the timer's signal and, for a `SIGEV_THREAD_ID`
    /// timer, the thread it notifies.
    async fn recv_register_posix_timer(
        &self,
        detpid: DetPid,
        caller: RpcIncarnation,
        timer_id: i32,
        deadline: Option<LogicalTime>,
        interval: LogicalTime,
        notification: PosixTimerNotification,
    ) -> SchedulerRpcResult<()> {
        let PosixTimerNotification {
            sig,
            thread,
            value,
            deleted,
        } = notification;
        let RpcIncarnation { dettid, mm } = caller;
        let mut sched = self.lock_rpc_scheduler(false).await;
        if sched.thread_is_logically_killed(dettid) || !sched.rpc_incarnation_matches(dettid, mm) {
            return SchedulerRpcResult::ThreadExited;
        }
        // A process-directed timer prefers the thread that armed it.
        let target = match thread {
            Some(thread) => SignalTarget::Thread(thread),
            None => SignalTarget::Process(dettid),
        };
        sched.note_posix_timer_notification(detpid, timer_id, sig.raw(), value, deleted);
        sched.register_posix_timer(
            detpid,
            target,
            timer_id,
            deadline,
            interval,
            match sig.signal() {
                Some(signal) => signal.into(),
                None => crate::scheduler::timed_waiters::TimerSignal::RefuseRealTime(sig.raw()),
            },
        );
        SchedulerRpcResult::Continue(())
    }
}

/// What a POSIX timer notifies (`GlobalRequest::RegisterPosixTimer`).
#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)]
pub struct PosixTimerNotification {
    /// The signal.
    pub sig: SigWrapper,
    /// The thread a `SIGEV_THREAD_ID` timer notifies, or `None` for a
    /// process-directed one.
    pub thread: Option<DetTid>,
    /// The `si_value` of its signal.
    pub value: u64,
    /// The timer is being deleted (`timer_delete`), not set.
    pub deleted: bool,
}

/// Identity and final accounting for an asynchronous scheduler deregistration.
#[derive(PartialEq, Debug, Eq, Clone, Serialize, Deserialize)]
pub struct ThreadDeregistration {
    pub(crate) dettid: DetTid,
    pub(crate) detpid: DetPid,
    pub(crate) mm: MmId,
    /// Carried by the consuming ThreadState owner, independently of detpid's
    /// delayed initialization and the parent's scheduler registration RPC.
    pub(crate) thread_start_entered: bool,
    pub(crate) timeslice_stats: TimesliceStats,
    pub(crate) syscall_count: u64,
    pub(crate) chaos_epochs: Vec<ChaosEpochTransition>,
}

/// A question about, or a record in, the scheduler's ledger of guest-handled
/// SIGALRMs (`Scheduler::sigalrm_control`), sent by a guest's Detcore in the
/// calling thread's turn before the call reaches the kernel. Signal phase 1
/// refuses each of these with EPERM while it could reach a process that
/// handles SIGALRM.
#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum SigalrmControl {
    /// A send of SIGALRM to the process of this thread.
    SendTo(DetTid),
    /// A send of SIGALRM to a process the caller cannot name to Detcore: a
    /// pidfd with no recorded target (`pidfd_open` records one).
    SendToUnnamed,
    /// An arming of a kernel producer of SIGALRM that Detcore does not
    /// emulate through its alarm path (a POSIX timer, `F_SETSIG` or
    /// `PR_SET_PDEATHSIG`), or of a file lease, which makes another process's
    /// open wait for the lease break with no scheduler admission. Recorded
    /// when allowed, so a later handler installation can be refused.
    ArmProducer,
    /// An arming of a recurring `ITIMER_REAL` (nonzero interval), which phase 1
    /// does not model for a handling process.
    ArmRecurringTimer,
    /// The caller, in a process that handles SIGALRM, is about to run
    /// synchronous I/O on inherited stdio, which may sleep with no scheduler
    /// admission. Never refused: a SIGALRM already due is recorded as a
    /// determinism loss (design closure 1).
    UnadmittedStdioIo,
    /// Before a guest SIGALRM handler is installed in the caller's process
    /// (phase 1 step I3, design section 6): refused once any guest armed a
    /// kernel producer of SIGALRM, anywhere in the run.
    InstallHandler,
    /// Before the caller's process changes its SIGALRM handler to `SIG_DFL`:
    /// refused while the process has a pending entry, which would take the
    /// default action inside the callback (design section 6).
    HandlerToDefault,
    /// Whether the caller's process has a pending entry, for `rt_sigpending`
    /// ("refused" is true when it has one). Never changes anything.
    PendingEntry,
    /// Records the caller's process's SIGALRM disposition and the caller's
    /// "SIGALRM virtually blocked" bit, after its runtime changed them inside
    /// this turn. Never refused.
    Publish {
        /// Whether the process's SIGALRM disposition is a guest handler.
        handled: bool,
        /// Whether the caller's virtual mask blocks SIGALRM.
        blocked: bool,
    },
    /// Records the caller's "SIGALRM virtually blocked" bit, after its runtime
    /// changed its mask inside this turn. Never refused.
    PublishBlocked(bool),
    /// Whether the caller's process has an entry that is due for the caller
    /// (its SIGALRM not virtually blocked; "refused" is true when it has one).
    /// Never changes anything.
    DueEntry,
    /// Takes the caller's process's entry for delivery to the caller, if it
    /// is still pending, handled and due ("refused" is true when taken), in
    /// the turn of the delivery's `InboundSignal` request (step I4, design
    /// section 4, step 3).
    TakeEntry,
    /// Records a determinism loss for the caller's process, here in the
    /// coordinator, where the verifier reads it: the runtime could not
    /// deliver an entry the caller took (or could not tell the caller's
    /// virtual mask). The caller then stops the process. Never refused.
    DeliveryLost,
    /// Records a determinism loss for the caller's process, here in the
    /// coordinator: the caller delivers an entry before a syscall that has
    /// not run (the I4 addendum's point c), whose handler cannot be shown the
    /// registers Linux would show it (Reverie restores rcx and r11 as the
    /// `syscall` instruction already wrote them). The delivery goes ahead and
    /// the run continues; verification refuses to compare it. Never refused.
    EntryDeliveryUnfaithful,
}

/// Messages to the global object.
///
/// Whether a signal send named a process (kill, rt_sigqueueinfo, a pidfd
/// without `PIDFD_SIGNAL_THREAD`) or one thread (tkill, tgkill,
/// rt_tgsigqueueinfo, a pidfd with `PIDFD_SIGNAL_THREAD`). A SIGKILL ends its
/// group either way while the named thread is alive; for an exited thread only
/// a process-directed one does (Claude re-check of
/// <https://github.com/rrnewton/hermit/pull/4439>, N1).
#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum SignalScope {
    /// The send named a process.
    Process,
    /// The send named one thread.
    Thread,
}

/// This is public only so it can be used in the `GlobalTool` trait.
/// It should NOT be used by any client outside of this file.
#[derive(PartialEq, Debug, Eq, Clone, Serialize, Deserialize)]
#[allow(clippy::enum_variant_names)]
pub enum GlobalRequest {
    /// `request`, sent with the number of records this guest thread produced
    /// for forwarding since its previous counted request, so the coordinator
    /// can check they all arrived (`detlog::check_forwarded_count`). Sent only
    /// by a thread that has produced some (`counted_request`).
    WithForwardedCount(u64, Box<GlobalRequest>),
    /// Lock the resources
    /// Also contains the `DetPid` of the process containing the thread requesting resources.
    RequestResources(Resources, DetPid),
    /// Request resources with the actual backend parked-callback capability.
    ParkedRequest(Resources, DetPid, ControlCapability),
    /// Resume a previously parked request at its actual current callback site.
    ResumeParkedRequest {
        /// Scheduler-issued one-use continuation ticket.
        ticket: ResumeTicket,
        /// Actual callback site supplied by the backend on resumption.
        current_site: reverie::CallbackSignalSite,
    },
    /// Finish the observation belonging to one scheduler-owned parked wait.
    FinishParkedObservation {
        /// Actual pending wait identity.
        wait: ContinuationId,
        /// Backend observation lease belonging to that wait.
        lease: reverie::ParkedObservationLease,
        /// Actual callback site at the observation boundary.
        site: reverie::CallbackSignalSite,
        /// Result of the observation, including an explicit failure.
        finish: ObservationFinish,
    },
    /// Report a terminal violation of the parked continuation protocol.
    ParkedProtocolFailure(ProtocolFailure),
    /// Report an actual signal removed from this process's pending set.
    SignalDequeued {
        /// Deterministic process whose signal was dequeued.
        detpid: DetPid,
        /// Backend identity of the actual signal task.
        identity: reverie::SignalTaskIdentity,
        /// Exact signal dequeue observation.
        dequeue: reverie::SignalDequeue,
    },
    /// Release the locks
    ReleaseResources(Resources),
    /// For convenience, release all the resources held by the current TID.
    ReleaseAllResources,

    // TODO-HUMAN-REVIEW(PR-643): Review this new Detcore global RPC request.
    /// Add a syscall to the run-wide unsupported-use summary.
    ReportUnsupportedSyscall(String),

    /// A guest asked for a disposition of this signal that Detcore answered
    /// with success but did not install. The coordinator announces it once per
    /// signal per run.
    AnnounceSignalHandlerNotInstalled(i32),

    /// The fail-closed policy is ending the run, for this reason. Sent just
    /// before the Tool image exits with `HERMIT_POLICY_REFUSAL_EXIT`, which on
    /// an in-guest backend ends only the guest process: the launcher reads the
    /// reason here to report the refusal (`GlobalState::policy_refusal`).
    PolicyRefusal(String),

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1154): Review the SaBRe exec descriptor-status handoff.
    /// Save the caller, address-space identity, and logically blocking descriptors before exec.
    /// A successful backend may reload the Tool or preserve and transfer its thread state.
    /// Boxed, as is `GlobalResponse::MarkPastFirstExecve`'s: inline, the
    /// largest payload sizes every RPC future, and each of the hundreds of
    /// handler futures `handle_syscall_event` awaits holds some, which in a
    /// debug build overflowed the 2 MiB test thread that runs ptrace record
    /// through the library (`record_replay`'s `public_record_*` tests).
    PrepareExec(DetPid, MmId, Box<ExecFdState>),

    /// Clear the saved transition after an exec attempt returns with an error.
    CancelExec(DetPid),

    /// Authenticate the preserved exec caller taking over its process leader's TID.
    ReconnectExec {
        /// Preserved caller before the kernel leader-identity transition.
        former: DetTid,
        /// Continuing process leader identity.
        process: DetPid,
    },
    /// Acquire the replacement's first turn without newborn state initialization.
    ResumeExec(DetPid),
    /// Consume a transferred owner if genuine cancellation interrupts local
    /// rebinding. The Tool copies `signaled` from the backend's exit status;
    /// ordinary exit requires a separately recorded backend failure.
    RetireExec {
        /// Final transferred thread state and accounting.
        thread: ThreadDeregistration,
        /// Whether the backend observed signal termination.
        signaled: bool,
    },

    /// Complete a successful image transition, including the initial image, and
    /// delete the process's POSIX timer deadlines before the new image runs.
    /// The last field is the device and inode of the file the kernel's
    /// `/proc/<pid>/exe` link names for the new image, where the tool can
    /// read it, which the image's record is checked against
    /// (`crate::exec_record`). Before it, in a run that declares a launcher,
    /// the record the new image resolved from the path its exec was given,
    /// for an exec Detcore did not prepare.
    MarkPastFirstExecve(
        DetPid,
        Option<reverie::SignalTaskIdentity>,
        Option<crate::exec_record::ExecRecord>,
        Option<(u64, u64)>,
    ),

    /// The parent is adding a child-thread to the round-robin pool.  Contains the dettid
    /// of the new child and it's starting scheduler priority IF it is available to the caller.
    /// The only scenario where the Priority will be missing is when we're replaying preemptions.
    /// In that case it is the global state that holds the information regarding the new thread's
    /// initial priority.
    CreateChildThread(
        DetTid,
        DetPid,
        usize,
        Option<CloneFlags>,
        libc::c_int,
        Option<(i32, i32)>,
        Option<Priority>,
    ),

    /// A vfork child registering itself while its parent is blocked inside the
    /// kernel. Contains the (real) parent dettid and detpid, the child dettid,
    /// the child TID address, the clone flags, and the starting priority (absent
    /// only when replaying preemptions).
    CreateVforkChildThread(
        DetTid,
        DetPid,
        DetTid,
        usize,
        CloneFlags,
        libc::c_int,
        Option<Priority>,
    ),

    /// New thread is alive and waiting to run its first instruction.  Contains the dettid
    /// and detpid of the new child.
    StartNewThread(
        DetTid,
        DetPid,
        Option<(i32, i32)>,
        Option<reverie::SignalTaskIdentity>,
    ),

    /// Remove a thread from scheduler data structures, guaranteeing that it will
    /// consume no further turns. Carries its final timeslice distribution and any
    /// chaos-epoch transitions not yet flushed by a priority-change commit.
    DeregisterThread(ThreadDeregistration),

    /// Replace the address cleared and woken when the calling thread exits.
    /// A zero address disables the exit-time store and wake.
    SetChildTidAddress(usize),

    /// Record (`Some`) or clear (`None`) the calling thread's own signal mask
    /// while Detcore holds it under a private blocking mask, so the scheduler's
    /// child-exit classification reads the guest's mask, not Detcore's.
    SetSavedGuestSigmask(Option<u64>),

    /// The guest's own signal mask that a thread published while Detcore
    /// holds it under a private blocking mask, if any
    /// (`Scheduler::published_guest_sigmask`), for its `/proc` status.
    PublishedGuestSigmask(DetTid),

    /// Record (`Some`) or clear (`None`) the set the calling thread waits for
    /// while it polls `rt_sigtimedwait`.
    SetSigtimedwaitSet(Option<u64>),

    /// The inputs to a process-directed signal the calling thread sends to its
    /// own process (`Scheduler::own_process_signal_inputs`).
    OwnProcessSignalInputs,

    /// The calling thread sends SIGSTKFLT (`Scheduler::record_guest_marker_signal`).
    RecordGuestMarkerSignal,

    /// Notify scheduler before/after futex action.
    /// The last two arguments are the initial contents of the memory word, and the mask.
    FutexAction(DetTid, FutexAction, FutexID, i32, u32),

    /// Translate nondeterministic to deterministic inode, and report what the
    /// caller observed of the host mtime (see `ObservedMtime`), how it
    /// reached the file (see `InodeSighting`), and, for a procfs entry, its
    /// name (see `procfs_inode::ProcfsName` and `InodePool::procfs_key`).
    DeterminizeInode(RawInode, ObservedMtime, InodeSighting, Option<ProcfsName>),

    /// Translate the inode of every file-backed line of one `/proc/*/maps`
    /// read, in line order, repeats included: one numbering request per line.
    DeterminizeMappingInodes(Vec<RawInode>),

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping RPC.
    /// Translate a nondeterministic (host-assigned) device number to a
    /// deterministic one.
    DeterminizeDevice(u64),

    /// Translate a host-assigned fdinfo mount ID to a run-local identity. The
    /// fallback order is populated only by low-level callers without captured
    /// namespace provenance.
    DeterminizeMountId(u64, Option<Vec<u64>>),

    /// Seed or validate the snapshot's canonical mountinfo identity order
    /// (first field), then return the run-local identity of each entry of the
    /// observation order (second field: the same raw IDs, rows then parents),
    /// numbering unseen entries in that observation order.
    ResolveMountinfoIds(Vec<u64>, Vec<u64>),

    /// The file behind this host inode lost its last name (see
    /// `InodePool::retire`).
    RetireInode(RawInode),

    /// An `open` found this host inode linked or freshly created (see
    /// `InodePool::forget_retired`); for a procfs entry, also its name (see
    /// `procfs_inode::ProcfsName` and `InodePool::procfs_key`).
    ForgetRetiredInode(RawInode, Option<ProcfsName>),

    /// The inode pool's state, for a directory snapshot (see `SnapshotMark`).
    DirectorySnapshotMark,

    /// `--pipe-wake-latency`: an action of the calling thread readied the
    /// pollers of a channel that want this (`Scheduler::ready_channel`).
    ReadyChannel(crate::pipe_wake::ChannelId, crate::pipe_wake::Wants),
    /// `--pipe-wake-latency`: the calling thread's hand-off is due
    /// (`Scheduler::hand_off_turn`).
    HandOffTurn,
    /// The calling thread is delivering a signal whose default action ends
    /// its process (`Scheduler::record_committed_fatal_signal`).
    RecordFatalSignal,

    /// Bump mtime
    TouchFile(RawInode),

    /// Set mtime to a guest-requested time (utimensat and friends).
    SetFileMtime(RawInode, LogicalTime),

    /// Retrieve global time.
    GlobalTimeLowerBound,

    /// Record scheduling event in a total order.
    // Logging provenance only; never serialized into a schedule artifact.
    TraceSchedEvent(SchedEvent, DetPid, bool),

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#663)
    // TODO-HUMAN-REVIEW(#869)
    /// Basically performs an alarm syscall, takes a logical duration.
    RegisterAlarm(DetPid, DetTid, LogicalTime, LogicalTime, SigWrapper),

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#869)
    /// Register, re-arm, or disarm one POSIX timer, or delete it.
    RegisterPosixTimer(
        DetPid,
        DetTid,
        i32,
        Option<LogicalTime>,
        LogicalTime,
        PosixTimerNotification,
    ),

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-841): Review logical alarm query RPC.
    /// Return the logical time remaining on a process's one-shot alarm.
    AlarmRemaining(DetPid),
    /// Reconcile the process's `Inactive` ITIMER_REAL after a call that may
    /// have dequeued its SIGALRM (`Scheduler::reconcile_alarm`).
    ReconcileAlarm(DetPid),
    /// A guest's committed SIGALRM send (rule 4, `Scheduler::note_guest_sigalrm`).
    NoteGuestSigalrm(crate::scheduler::itimer_coalesce::GuestSigalrmTarget),
    /// The caller changed SIGALRM's disposition (rule 5).
    NoteSigalrmDisposition,

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#663)
    /// Deterministically select a logically exited matching child.
    ReadyChildWait(DetPid, ChildWaitSpec),
    /// Retire a consumed terminal child wait status.
    ConsumeChildWait(DetPid, DetPid),
    /// Query scheduler-owned process-group membership.
    ProcessGroup(DetPid),
    /// Apply a successful setpgid transition.
    SetProcessGroup(DetPid, DetPid),
    /// Apply a successful setsid transition.
    CreateSession(DetPid),
    /// Query live threads before translating process-directed signal delivery,
    /// and the process's exit state (`Scheduler::exited_process_is_zombie`).
    ResolveKillTargets(DetPid),
    /// Whether a pid or tid that a guest task named (with the thread group it
    /// named, if any) is one of Hermit's own tasks; see `hermit_task_named`.
    NamesHermitTask(DetTid, Option<i32>, i32),
    /// A successful kill(2) queued a physical signal for this sole target,
    /// sent with the given scope.
    NotifySignalPending(DetTid, SigWrapper, Option<DetPid>, SignalScope),
    /// The sender, holding the serial grant in its signal-delivery turn,
    /// decided that a default action ends its thread group: its next request,
    /// the group `Exit`, is granted before any other thread runs
    /// (`Scheduler::hold_turn_for_group_exit`).
    HoldTurnForGroupExit(DetTid),
    /// A successful SIGKILL whose victims the scheduler is not told one by
    /// one: a process-group kill (`Some(group)`), or a pidfd whose process
    /// Detcore did not record (`None`).
    NotifyUnresolvedSigkill(Option<DetPid>),
    /// Liveness of one tid, leader or not; see `thread_is_live`.
    ThreadIsLive(DetTid),
    /// The pending real-time timer expirations of a process; see
    /// `pending_real_time_expirations`.
    PendingRealTimeExpirations(DetPid),
    /// Discard pending real-time timer expirations of a process (a kernel
    /// sigset) whose disposition became `SIG_IGN`.
    DiscardPendingRealTimeExpirations(DetPid, u64),
    /// A process created a signalfd for these signals (a kernel sigset).
    RegisterSignalfd(DetPid, u64),
    /// A handler's return in a process restores a mask leaving these
    /// real-time signals (a kernel sigset) unblocked.
    RecordHandlerRestoreMask(DetPid, u64),
    /// Scheduler-owned lifecycle state for a direct child process.
    ExactChildWaitState(DetPid, DetPid),

    /// The container is shutting down.  Exit the scheduler "thread".
    UnrecoverableShutdown,

    /// Request a port for an open file description.
    RequestPort(OpenFileId),

    /// Add a port to the used-port list for an open file description.
    AddUsedPort(u16, OpenFileId),

    /// Release the port when the last alias of its open file description closes.
    ReleasePort(OpenFileId),

    /// Deliver robust-futex wakes collected before exit after the backend has
    /// confirmed that Linux's physical task cleanup completed.
    RobustListWakes(Vec<(DetTid, FutexID)>),
    /// One external network record or replay operation.
    Network(NetworkRequest),

    /// Record that the sending thread, in its syscall number `syscall`, opened
    /// `path` and found the host file `identity` (see
    /// `detcore_model::host_input`). An observation for `hermit run --verify`
    /// only: answered before any clock or scheduler accounting. Appended at
    /// the end, so that adding it left every earlier variant's encoded tag
    /// unchanged.
    RecordHostInput {
        /// Guest pathname observed by the original syscall.
        path: String,
        /// Number of the syscall that observed the input.
        syscall: u64,
        /// Captured actual host file identity.
        identity: HostFileIdentity,
    },

    /// Record that the sending thread rebound `path` in the guest's file
    /// namespace (see `detcore_model::host_input::HostMutationRecord`). An
    /// observation for `hermit run --verify` only, answered like
    /// [`Self::RecordHostInput`]. Appended at the end, so that adding it left
    /// every earlier variant's encoded tag unchanged.
    RecordHostMutation {
        /// Guest pathname whose namespace binding changed.
        path: String,
    },
    /// This thread armed, in its turn, a host-timed source of these signals;
    /// see `record_host_timed_signals`. Appended after `RecordHostMutation`,
    /// so that adding it left every earlier variant's encoded tag unchanged.
    RecordHostTimedSignals(DetTid, HostTimedSignalScope, u64),
    /// The signals no gated wait of this thread ends for; see
    /// `host_timed_signals`. Appended after `RecordHostTimedSignals`, for the
    /// same reason.
    HostTimedSignals(DetTid),
    /// A control message about a shared open file description, answered
    /// before any clock or scheduler accounting (see
    /// `crate::shared_open_files`). Appended after `HostTimedSignals`, for the
    /// same reason.
    SharedOpenFile(OpenFileControl),
    /// A SIGALRM ledger control message, answered before any clock or
    /// scheduler accounting (see `sigalrm_refuses`). Appended after
    /// `SharedOpenFile`, for the same reason.
    Sigalrm(SigalrmControl),
    /// A child-exit `SIGCHLD` control message
    /// (`scheduler::child_exit_sigchld`).
    ChildExitSigchld(crate::scheduler::child_exit_sigchld::ChildExitSigchldControl),
    /// A determinism loss to record for the run (see
    /// `crate::detlog::write_loss_notice`), from a guest-side handler, which
    /// may run in a process whose own record verification never reads.
    /// Answered before any clock or scheduler accounting. Appended after
    /// `ChildExitSigchld`, for the same reason.
    RecordDeterminismLoss(String),
    /// Mirror a successful `prctl(PR_SET_CHILD_SUBREAPER)` by this process,
    /// in the caller's turn (`set_child_subreaper`). Appended after
    /// `RecordDeterminismLoss`, for the same reason.
    SetChildSubreaper(DetPid, bool),
    /// The process Detcore registered `DetTid` in: its own thread group's
    /// leader in the thread tree. Unlike the backend's `Guest::pid`, it names
    /// a raw clone child with a non-SIGCHLD exit signal as its own process.
    /// Appended after `SetChildSubreaper`, for the same reason.
    RegisteredProcessOf(DetTid),
    /// Mirror a successful `rt_sigaction(SIGCHLD, ...)` by this process:
    /// whether the action is `SIG_IGN`, and whether it has `SA_NOCLDWAIT`
    /// (`set_sigchld_disposition`). Appended after `RegisteredProcessOf`, for
    /// the same reason.
    SetSigchldDisposition(DetPid, bool, bool),
    /// The process a signal naming this id goes to
    /// (`Scheduler::signal_target_process`). Appended after
    /// `SetSigchldDisposition`, for the same reason.
    SignalTargetProcess(DetPid),
    /// Reserve the victims of a SIGKILL the sender is about to send to the
    /// process of the given thread, before the physical send
    /// (`scheduler::sigkill_retirement`). Appended after
    /// `RegisteredProcessOf`, for the same reason.
    ReserveSigkill(DetTid, DetTid, u32),
    /// Commit (true: the send succeeded) or unwind a SIGKILL reservation.
    /// Appended after `ReserveSigkill`, for the same reason.
    CommitSigkill(u64, bool),
    /// Reserve the victims of a `kill(-pgid, SIGKILL)`: every live process in
    /// the recorded process group. Appended after `CommitSigkill`, for the
    /// same reason.
    ReserveSigkillGroup(DetTid, DetPid, u32),
    /// The thread a `SIGEV_THREAD_ID` timer of this process names by this id
    /// (`Scheduler::posix_timer_thread`). Appended after
    /// `ReserveSigkillGroup`, for the same reason.
    PosixTimerThread(DetPid, i32),
    /// A disposition change in this process flushed this signal from every
    /// thread (`Scheduler::forget_thread_timer_copies`). Appended after
    /// `PosixTimerThread`, for the same reason.
    ForgetThreadTimerCopies(DetPid, i32),
    /// What a positive pid the given nested thread names, in its own PID
    /// namespace, names (`PidNamespaces::resolve_for`). Appended after
    /// `ReserveSigkillGroup`, for the same reason.
    GuestPid(DetTid, i32),
    /// The pid the given nested thread sees for a task
    /// (`PidNamespaces::localize_for`). Appended after `GuestPid`, for the
    /// same reason.
    LocalPid(DetTid, DetTid),
    /// The POSIX timer copies a successful exec of this process flushes
    /// (`Scheduler::take_exec_timer_flush`), asked by its surviving thread.
    /// Appended after `LocalPid`, for the same reason.
    ExecTimerFlush(DetPid),
    /// The calling thread-group leader is about to complete its own `exit`
    /// while its group lives on (`Scheduler::retire_exited_leader`).
    /// Appended after `ExecTimerFlush`, for the same reason.
    RetireExitedLeader,
    /// A discarded-at-send record message (`scheduler::discarded_at_send`),
    /// answered before any clock or scheduler accounting. Appended after
    /// `RetireExitedLeader`, for the same reason.
    DiscardedSignal(crate::scheduler::discarded_at_send::DiscardedSignalControl),
    /// A `close`, `close_range`, `dup2` or `dup3` released or replaced the
    /// descriptors `first..=last` of the caller's table, named by its identity
    /// (`Scheduler::note_descriptors_replaced`). Appended after
    /// `DiscardedSignal`, for the same reason.
    DescriptorsReplaced(detcore_model::fd::FilesId, u32, u32),
    /// Whether a task of this run had this id and it is not a zombie's pid
    /// (`thread_id_refused`). Appended after `DescriptorsReplaced`, for the
    /// same reason.
    ThreadIdRefused(DetTid),
    /// The processes `/proc` lists, and the threads the `task` directory of
    /// the given process lists (`Scheduler::procfs_listing`). Appended after
    /// `ThreadIdRefused`, for the same reason.
    ProcfsListing(Option<DetPid>),
    /// Arm (`Some`, 0 disarms) or read (`None`) the sender's parent-death
    /// signal that Detcore sends itself (`parent_death_signal`). Appended
    /// after `ProcfsListing`, for the same reason.
    ParentDeathSignal(Option<i32>),

    /// Neutral controls appended without changing any existing wire tag.
    PrepareNativeExec(native_exec::PrepareNativeExec),
    /// Seal the exact versioned runtime bytes before one-use descriptor provision.
    SealNativeExecStats {
        /// Retained prepared transaction ticket.
        ticket: native_exec::ExecTicket,
        /// SHA-256 of the exact runtime envelope bytes.
        runtime_digest: [u8; 32],
        /// Bounded actual runtime envelope, not host-decoded Tool state.
        runtime: Vec<u8>,
    },
    /// Last old-image control after the sealed empty forwarding cut.
    IssueNativeExec {
        /// Successfully provisioned retained transaction ticket.
        ticket: native_exec::ExecTicket,
        /// Independently computed composite Tool/runtime digest.
        state_digest: [u8; 32],
    },
    /// Invalidate a pre-redemption ticket; ordinary CancelExec still follows.
    CancelNativeExec {
        /// Prepared or Issued ticket whose physical exec did not succeed.
        ticket: native_exec::ExecTicket,
    },
    /// Acknowledge the real new-mm lifecycle after MarkPastFirstExecve.
    CompleteNativeExec {
        /// Consumed successor ticket after real lifecycle and frontend checks.
        ticket: native_exec::ExecTicket,
    },
    /// Whether a tid names a thread-group leader retired at its own exit
    /// while its group lives on (`names_a_retired_exited_leader`). Appended
    /// last, for the same reason as the controls above.
    NamesARetiredExitedLeader(DetTid),
    /// The kernel's numbers for guest process or thread numbers (`true`), or
    /// the guest's for kernel numbers (`false`), as `/proc` spells them
    /// (`Scheduler::procfs_ids`). Appended last, for the same reason.
    ProcfsIds(bool, Vec<i32>),
    /// The process group (`false`) or session (`true`) of the task with an id,
    /// as `getpgid` and `getsid` answer it (`Scheduler::group_or_session_of`).
    /// Appended last, for the same reason.
    GroupOrSession(DetPid, bool),
    /// Linux's answer to `setsid()` by a process
    /// (`Scheduler::setsid_decision`), deciding nothing yet. Appended last.
    SetsidDecision(DetPid),
    /// Linux's answer to `setpgid(pid, pgid)` by a process
    /// (`Scheduler::setpgid_decision`), deciding nothing yet. Appended last.
    SetpgidDecision(DetPid, i32, i32),
    /// The image a guest process executes, as Detcore recorded it at the
    /// process's exec (`crate::exec_record`), for its `/proc/<pid>/exe`
    /// link. Appended last.
    ProcessImage(DetPid),
    /// Whether the task a per-process `/proc` file names exists, whether it
    /// has exited unreaped, and the incarnation of its numbers
    /// (`Scheduler::procfs_task`). Appended last.
    ProcfsTask(DetTid, Option<DetTid>),
    /// Change or read the state Detcore keeps on a rendered per-process
    /// `/proc` file's inode (`InodePool::rendered_file_state`). Appended last.
    RenderedFileState(u64, crate::procfs::listing::RenderedFileChange),
    /// The sender's process's signal dispositions as Detcore's copy holds
    /// them, or `None` to withdraw them, with the publication's generation
    /// (`Scheduler::publish_signal_dispositions`). Answered before any clock
    /// or scheduler accounting. Appended last.
    PublishSignalDispositions(
        u64,
        Option<crate::syscalls::signal_shadow::CopiedDispositions>,
    ),
    /// A Detcore warning or error from inside the guest, which the coordinator
    /// emits at the run's own log level (`crate::guest_log`), or none, and how
    /// many of the sender's process could not be sent before it. Answered
    /// before forwarded records are drained and before any clock or scheduler
    /// accounting. Appended last.
    GuestLog(Option<crate::guest_log::GuestLogRecord>, u64),
    /// The sender's mask as Detcore's copy holds it, or `None` to withdraw
    /// it (`Scheduler::publish_signal_mask`). Answered before any clock or
    /// scheduler accounting. Appended last.
    PublishSignalMask(Option<u64>),
    /// The auxiliary vector the sender's process's new image found on its
    /// initial stack (`ProcessAuxvTable::record`). Appended last.
    RecordProcessAuxv(Vec<u8>),
    /// A process's image's auxiliary vector (`ProcessAuxvTable::current`), for
    /// `/proc/<pid>/auxv`. Appended last.
    ProcessAuxv(DetPid),
    /// The sender's process's new image's `/proc/<pid>/auxv` is left to the
    /// kernel beneath (`ProcessAuxvTable::forget`), for the reason given.
    /// Answers whether this is the run's first such image for that reason,
    /// which the sender warns of. Appended last.
    AuxvFromKernel(AuxvFromKernel),
    /// The signals whose new action an in-guest runtime refused, bit `n - 1`
    /// for signal `n`, and whether it refused an alternate signal stack
    /// (`crate::refused_signal_controls`). Answered before the forwarded-record
    /// drain and any clock or scheduler accounting. Appended last.
    AnnounceRefusedSignalControls(u64, bool),
    /// A fixed reply from an external controller while the sender retains its
    /// serial grant. The runtime fact and the scheduler jointly establish that
    /// no guest can observe the temporary procfs descriptor. Appended last.
    CheckBrkMappings(
        crate::syscalls::memory::BrkMappingCheck,
        reverie::ControllerCaptureFact,
    ),
    /// Current image's native ownership facts, authenticated by the RPC MmId.
    /// Answered without granting another task or advancing logical time.
    PublishRuntimePrivateMemory(reverie::RuntimePrivateMemory),
    /// The sender's process changed its root with `chroot`
    /// (`ProcessAuxvTable::mark_changed_root`). Appended last.
    MarkChangedRoot,
    /// Whether the process a number names has changed its root
    /// (`ProcessAuxvTable::root_changed`). Appended last.
    RootChanged(DetPid),
    /// The OFD and flock locks this open file description holds, as its
    /// fdinfo showed them after a call through one of the sender's
    /// descriptors, and how many slots of the sender's descriptor table name
    /// it (`procfs::locks::LockLedger::note_locks`), or why that fdinfo
    /// could not be read (`procfs::locks::LockLedger::note_unknown`).
    /// Appended last.
    DescriptionLocks(
        OpenFileId,
        Result<Vec<crate::procfs::locks::HeldLock>, String>,
        u32,
    ),
    /// How many slots of the sender's descriptor table still name this
    /// description after a close or replacement there
    /// (`procfs::locks::LockLedger::release`). Appended last.
    TableReleased(OpenFileId, u32),
    /// Which rows of a `/proc/locks` snapshot the guest owns
    /// (`procfs::locks::guest_lock_rows`). Appended last.
    GuestLockRows(Vec<crate::procfs::locks::LockRowFacts>),
    /// The descriptions whose locks Detcore read that the sender's process's
    /// table still names after its successful exec, with their slot counts
    /// (`procfs::locks::LockLedger::exec_kept`). Appended last.
    TableExec(Vec<(OpenFileId, u32)>),
    /// A lock or unlock went through a descriptor whose description the
    /// ledger may know under another identity, on the file with this inode
    /// number, or on an unknown one
    /// (`procfs::locks::LockLedger::alias_inode`). Appended last.
    AliasedInode(Option<String>),
    /// The calling thread's host-timed signals and the `SIGCHLD` the schedule
    /// recorded in its private queue (`Scheduler::host_timed_and_private_sigchld`).
    /// Appended last.
    HostTimedAndPrivateSigchld(DetTid),
    /// A lock or unlock through a descriptor whose description the ledger
    /// may know under another identity, whose fdinfo could not be read for
    /// this reason (`procfs::locks::LockLedger::refuse`). Appended last.
    LockLedgerRefused(String),
}

/// Responses from the global object
#[allow(missing_docs, clippy::unit_arg)]
#[derive(PartialEq, Debug, Eq, Clone, Serialize, Deserialize)]
pub enum GlobalResponse {
    /// The scheduler permanently removed this raw TID. Guest-side RPC handling retires
    /// this task without cancelling a live group-exit issuer or resuming the operation.
    ThreadExited,
    /// A reply, with the virtual time that remained before the earliest
    /// pending deadline when the thread was granted the turn it resumes, or
    /// armed a timer (`Scheduler::deadline_remaining_from`). Unwrapped by
    /// `send_and_update_time`, which caps the thread's slice there.
    WithDeadline(Duration, Box<GlobalResponse>),
    RequestResources(ResumeStatus),
    ParkedRequest(ResourceReply),
    ResumeParkedRequest(ResourceReply),
    FinishParkedObservation(Result<FinishAck, ProtocolFailure>),
    SignalDequeued {
        ack: Result<DequeueAck, TimerFailure>,
        terminal: bool,
    },
    ReleaseResources(()),
    ReleaseAllResources(()),
    // TODO-HUMAN-REVIEW(PR-643): Review this new Detcore global RPC response.
    ReportUnsupportedSyscall(()),
    AnnounceSignalHandlerNotInstalled(()),
    PolicyRefusal(()),
    PrepareExec(()),
    CancelExec(()),
    ReconnectExec(bool),
    ResumeExec(ResumeStatus, Option<LogicalTime>),
    RetireExec(bool),
    MarkPastFirstExecve(Box<ExecFdState>),
    CreateChildThread(Option<MmId>),
    /// Includes optional preemption points for the new thread.
    StartNewThread(Option<ThreadHistory>),
    DeregisterThread(()),
    SetChildTidAddress(()),
    SetSavedGuestSigmask(()),
    PublishedGuestSigmask(Option<u64>),
    SetSigtimedwaitSet(()),
    OwnProcessSignalInputs(OwnProcessSignalInputs),
    RecordGuestMarkerSignal(()),
    FutexAction(Option<SchedValue>),
    /// Return the mtime as well:
    DeterminizeInode((DetInode, LogicalTime)),
    DeterminizeMappingInodes(Vec<DetInode>),
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping RPC.
    DeterminizeDevice(u64),
    DeterminizeMountId(Option<u64>),
    ResolveMountinfoIds(Option<Vec<u64>>),
    RetireInode(()),
    ForgetRetiredInode(()),
    DirectorySnapshotMark(SnapshotMark),
    ReadyChannel(crate::pipe_wake::ReadyOutcome),
    HandOffTurn(bool),
    RecordFatalSignal(()),
    TouchFile(()),
    SetFileMtime(()),
    GlobalTimeLowerBound(LogicalTime),
    TraceSchedEvent(TraceSchedEventResponse),
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#663)
    // TODO-HUMAN-REVIEW(#869)
    RegisterAlarm((LogicalTime, LogicalTime)),
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#869)
    RegisterPosixTimer(()),
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-841): Review logical alarm query RPC.
    ReadyChildWait((Option<DetPid>, bool)),
    ConsumeChildWait(bool),
    ProcessGroup(Option<DetPid>),
    SetProcessGroup(bool),
    CreateSession(bool),
    AlarmRemaining(ItimerSnapshot),
    ReconcileAlarm,
    NoteGuestSigalrm,
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#663)
    ResolveKillTargets(Vec<DetTid>, Option<bool>),
    NamesHermitTask(bool),
    NotifySignalPending(()),
    HoldTurnForGroupExit(bool),
    NotifyUnresolvedSigkill(()),
    ThreadIsLive(bool),
    PendingRealTimeExpirations(u64),
    DiscardPendingRealTimeExpirations(()),
    RegisterSignalfd(()),
    RecordHandlerRestoreMask(()),
    ExactChildWaitState(ExactChildWaitState),
    // TODO: use void_send_rpc, and remove this bogus response:
    UnrecoverableShutdown(()),

    RequestPort(u16),
    AddUsedPort,
    ReleasePort(Option<u16>),
    PortFull,
    RobustListWakes(Vec<u64>),
    Network(Result<NetworkReply, NetworkEngineError>),
    /// Appended at the end, so that adding it left every earlier variant's
    /// tag unchanged.
    RecordHostInput(()),
    /// Appended at the end, so that adding it left every earlier variant's
    /// tag unchanged.
    RecordHostMutation(()),
    /// Appended after `RecordHostMutation`, so that adding it left every
    /// earlier variant's tag unchanged.
    HostTimedSignals(u64),
    /// Appended after `HostTimedSignals`, for the same reason.
    SharedOpenFile(Result<OpenFileControlReply, SharedOpenFileError>),
    /// Whether the SIGALRM ledger refused the control message. Appended after
    /// `SharedOpenFile`, for the same reason.
    Sigalrm(bool),
    ChildExitSigchld(crate::scheduler::child_exit_sigchld::ChildExitSigchldAnswer),
    /// Appended after `ChildExitSigchld`, for the same reason.
    RecordDeterminismLoss(()),
    /// Appended after `RecordDeterminismLoss`, for the same reason.
    SetChildSubreaper(()),
    /// Appended after `SetChildSubreaper`, for the same reason.
    RegisteredProcessOf(Option<DetPid>),
    /// Appended after `RegisteredProcessOf`, for the same reason.
    SetSigchldDisposition(()),
    /// Appended after `SetSigchldDisposition`, for the same reason.
    SignalTargetProcess(Option<DetPid>),
    /// The reservation token, or none when the send keeps the ordinary path.
    /// Appended after `RegisteredProcessOf`, for the same reason.
    ReserveSigkill(Option<crate::scheduler::sigkill_retirement::SigkillReservation>),
    /// Appended after `ReserveSigkill`, for the same reason.
    CommitSigkill(()),
    /// The sender is a committed SIGKILL victim
    /// (`Scheduler::is_unaccounted_sigkill_victim`): its request is never
    /// answered, and the client waits for the kernel to end it. Unlike
    /// `ThreadExited`, it never tail-injects an exit. Appended after
    /// `CommitSigkill`, for the same reason.
    Retired,
    /// Appended after `Retired`, for the same reason.
    PosixTimerThread(PosixTimerThread),
    /// Appended after `PosixTimerThread`, for the same reason.
    ForgetThreadTimerCopies(()),
    /// Appended after `Retired`, for the same reason.
    GuestPid(crate::scheduler::pid_namespaces::GuestPidAnswer),
    /// Appended after `GuestPid`, for the same reason.
    LocalPid(Result<Option<i32>, String>),
    /// Appended after `LocalPid`, for the same reason.
    ExecTimerFlush(Result<crate::scheduler::ExecTimerFlush, String>),
    /// Appended after `ExecTimerFlush`, for the same reason.
    RetireExitedLeader(bool),
    /// Appended after `RetireExitedLeader`, for the same reason.
    DiscardedSignal(crate::scheduler::discarded_at_send::DiscardedSignalAnswer),
    /// Appended after `DiscardedSignal`, for the same reason.
    DescriptorsReplaced(()),
    /// Appended after `DescriptorsReplaced`, for the same reason.
    ThreadIdRefused(bool),
    /// Appended after `ThreadIdRefused`, for the same reason.
    ProcfsListing(crate::procfs::listing::ListingFacts),
    /// Appended after `ProcfsListing`, for the same reason.
    ParentDeathSignal(i32),

    /// Exact native responses, appended without changing existing wire tags.
    PrepareNativeExec(Result<native_exec::ExecTicket, native_exec::ExecControlError>),
    SealNativeExecStats(Result<[u8; 32], native_exec::ExecControlError>),
    IssueNativeExec(Result<(), native_exec::ExecControlError>),
    CancelNativeExec(Result<(), native_exec::ExecControlError>),
    CompleteNativeExec(Result<(), native_exec::ExecControlError>),
    /// Appended last, for the same reason.
    NamesARetiredExitedLeader(bool),
    /// Appended last, for the same reason.
    ProcfsIds(Vec<Option<i32>>),
    /// Appended last, for the same reason.
    GroupOrSession(Option<DetPid>),
    /// Appended last; the error is an errno.
    SetsidDecision(Result<(), i32>),
    /// The process to move and its new group; the error is an errno. Appended
    /// last.
    SetpgidDecision(Result<(DetPid, DetPid), i32>),
    /// Boxed, to keep the response small. Appended last.
    ProcessImage(Option<Box<crate::exec_record::ExecRecord>>),
    /// Appended last.
    ProcfsTask(crate::procfs::listing::ProcfsTaskAnswer),
    /// Appended last.
    RenderedFileState(crate::procfs::listing::RenderedFileState),
    /// The generation the scheduler holds for the sender's process afterwards
    /// and whether this publication is what it holds
    /// (`Scheduler::publish_signal_dispositions`), or `None` when it took
    /// nothing from this sender. Appended last.
    PublishSignalDispositions(Option<crate::syscalls::signal_shadow::PublicationAnswer>),
    /// Appended last.
    GuestLog(()),
    /// Whether the scheduler took the mask (it takes nothing from a thread it
    /// does not know). Appended last.
    PublishSignalMask(bool),
    /// Appended last.
    RecordProcessAuxv(()),
    /// Appended last.
    ProcessAuxv(Option<ProcessAuxv>),
    /// Whether this is the run's first. Appended last.
    AuxvFromKernel(bool),
    /// Appended last.
    AnnounceRefusedSignalControls(()),
    /// None means the capture authority or mappings were unavailable.
    /// Appended last; this response owns no allocated storage.
    CheckBrkMappings(Option<bool>),
    /// Current image ownership publication was accepted. Appended last.
    PublishRuntimePrivateMemory(bool),
    /// Appended last.
    MarkChangedRoot(()),
    /// Whether the root changed. Appended last.
    RootChanged(bool),
    /// Appended last.
    DescriptionLocks(()),
    /// Appended last.
    TableReleased(()),
    /// One answer per row, in row order, or, while a referenced description's
    /// locks are unknown, why. Appended last.
    GuestLockRows(Result<Vec<bool>, String>),
    /// Appended last.
    TableExec(()),
    /// Appended last.
    AliasedInode(()),
    /// The host-timed signals, then the recorded private `SIGCHLD`. Appended
    /// last.
    HostTimedAndPrivateSigchld(u64, u64),
    /// Appended last.
    LockLedgerRefused(()),
}

/// Read only the mapping predicate through a backend's synchronous external
/// channel. No deadline wrapper, log accounting, or async future may allocate
/// or free guest-mm storage after the controller captures the mappings.
pub(crate) fn check_brk_mappings<G, T>(
    guest: &G,
    check: crate::syscalls::memory::BrkMappingCheck,
) -> Option<bool>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let fact = guest.controller_capture_fact()?;
    let state = guest.thread_state();
    let request = (
        state.thread_logical_time.clone(),
        state.mm_id,
        GlobalRequest::CheckBrkMappings(check, fact),
    );
    match guest.try_send_rpc_sync(request).ok()? {
        (None, GlobalResponse::CheckBrkMappings(answer)) => answer,
        _ => None,
    }
}

/// `request`, carrying the number of records this guest thread produced for
/// forwarding since its previous counted request when there are any
/// (`GlobalRequest::WithForwardedCount`). A thread of a backend whose Tool does
/// not forward records never has any, so its requests are unchanged.
pub(crate) fn counted_request(request: GlobalRequest) -> GlobalRequest {
    match crate::detlog::take_forwarded_since_request() {
        0 => request,
        produced => GlobalRequest::WithForwardedCount(produced, Box::new(request)),
    }
}

/// The global request that carries one shared open file control message. It
/// is answered before any clock accounting, so its clock and address-space
/// fields are placeholders. The sender is the thread the backend's transport
/// names for the request; for in-guest LiteInst that is the runtime's own
/// envelope, checked against the connecting process at admission, on a
/// connection the guest cannot write to.
pub fn shared_open_file_request(control: OpenFileControl) -> (DetTime, MmId, GlobalRequest) {
    (
        DetTime::default(),
        MmId::initial(DetTid::from_raw(0)),
        GlobalRequest::SharedOpenFile(control),
    )
}

/// The request [`crate::guest_log`] sends through a runtime's
/// [`crate::guest_log::CoordinatorCall`]: like [`shared_open_file_request`], it
/// carries no clock.
pub(crate) fn guest_log_request(
    record: Option<crate::guest_log::GuestLogRecord>,
    unsent_before: u64,
) -> (DetTime, MmId, GlobalRequest) {
    (
        DetTime::default(),
        MmId::initial(DetTid::from_raw(0)),
        GlobalRequest::GuestLog(record, unsent_before),
    )
}

/// The answer to a [`guest_log_request`].
pub(crate) fn guest_log_reply(
    response: (Option<LogicalTime>, GlobalResponse),
) -> std::io::Result<()> {
    match response {
        (None, GlobalResponse::GuestLog(())) => Ok(()),
        other => Err(std::io::Error::other(format!(
            "unexpected answer to a guest log record: {other:?}"
        ))),
    }
}

/// The answer to a [`shared_open_file_request`].
pub fn shared_open_file_reply(
    response: (Option<LogicalTime>, GlobalResponse),
) -> Result<OpenFileControlReply, SharedOpenFileError> {
    match response {
        (None, GlobalResponse::SharedOpenFile(reply)) => reply,
        other => Err(SharedOpenFileError(format!(
            "unexpected answer to a shared open file control message: {other:?}"
        ))),
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-644): Review the shared warning formatter API.
/// Formats one deterministic warning for a set of unsupported syscall names.
pub fn format_unsupported_syscall_warning(syscalls: &BTreeSet<String>) -> Option<String> {
    if syscalls.is_empty() {
        None
    } else {
        Some(format!(
            "syscalls {} used but not yet supported",
            syscalls.iter().cloned().collect::<Vec<_>>().join(",")
        ))
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1154): Review the SaBRe exec descriptor-status handoff.
/// Notifies the coordinator that `guest` is about to `execve`, recording the
/// pre-exec address space `mm` and any file-descriptor blocking overrides. A
/// backend that handles `execve` outside Detcore's syscall handler must call
/// this before the native syscall so the next image reconnects to the existing
/// scheduler identity and logical clock.
///
/// `robust_wakes` are the owner-death wakes a successful exec makes Linux
/// deliver, from [`Detcore::exec_robust_list_wakes`], where the backend runs
/// the native execve outside Detcore's handler; the coordinator delivers them
/// at the new image's reconnect, or when the caller's thread ends without
/// one, and drops them at [`cancel_exec`]. A backend whose execve Detcore handles stages
/// them in its own thread state instead and passes none.
///
/// `exe` is the image the exec runs, where the caller resolved it
/// ([`crate::exec_record::ExecRecord::resolve`]); the new image's
/// `/proc/<pid>/exe` link answers from it.
pub async fn prepare_exec<G, T>(
    guest: &mut G,
    mm: MmId,
    fd_blocking: ExecFdBlockingOverrides,
    robust_wakes: Vec<(DetTid, FutexID)>,
    exe: Option<crate::exec_record::ExecRecord>,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // An exec replaces the image holding this process's unsent warning count
    // (`crate::guest_log`); every exec path, DBT's native one included, comes
    // here first.
    crate::guest_log::flush_unsent();
    let detpid = guest.thread_state().detpid.expect("detpid unset");
    let fd_state = ExecFdState {
        blocking: fd_blocking,
        inherited_reads: guest.thread_state().inherited_reads_for_exec(),
        initialize_inherited_stdio: false,
        initial_input: None,
        robust_wakes,
        open_files: guest
            .thread_state()
            .shared_open_files_for_exec()
            .expect("prepared exec descriptors are no longer live"),
        maps: if guest.config().backend.rediscovers_descriptors_after_exec {
            guest
                .thread_state()
                .maps_for_exec(guest.runtime_private_memory_regions().as_ref())
                .expect("prepared maps descriptors are no longer live")
        } else {
            None
        },
        channels: Default::default(),
        open_file_namespace: None,
        exe,
    };
    // `--pipe-wake-latency` on a backend that rediscovers its descriptors
    // after exec: carry the channels, and reserve the old numbering at every
    // exec (`crate::pipe_wake`).
    let fd_state = if guest.config().pipe_wake_latency_in_effect().is_some()
        && guest.config().backend.rediscovers_descriptors_after_exec
    {
        ExecFdState {
            channels: guest.thread_state().pipe_channels_for_exec(),
            open_file_namespace: Some(guest.thread_state().open_file_namespace_for_exec()),
            ..fd_state
        }
    } else {
        fd_state
    };
    let (_, response) = send_and_update_time(
        guest,
        GlobalRequest::PrepareExec(detpid, mm, Box::new(fd_state)),
    )
    .await;
    assert_eq!(response, GlobalResponse::PrepareExec(()));
}

/// Cancels a matching [`prepare_exec`] after a native exec fails. Call this
/// before returning to the old image, preserving its thread state and address
/// space identity so frozen siblings can resume without an exec transition.
pub async fn cancel_exec<G, T>(guest: &mut G)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let detpid = guest.thread_state().detpid.expect("detpid unset");
    let (_, response) = send_and_update_time(guest, GlobalRequest::CancelExec(detpid)).await;
    assert_eq!(response, GlobalResponse::CancelExec(()));
}

pub async fn mark_past_first_execve<G, T>(
    guest: &mut G,
    unprepared_exe: Option<crate::exec_record::ExecRecord>,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let controlled =
        guest.signal_control_mode() == reverie::BackendSignalControlMode::ToolControlled;
    let signal_identity = controlled.then(|| guest.signal_task_identity()).flatten();
    let detpid = guest.thread_state().detpid.expect("detpid unset");
    // The device and inode of the file the kernel's `exe` link names for the
    // new image, which the coordinator checks the exec's record against.
    let kernel_image = crate::syscalls::guest_task_procfs(
        &guest.config().backend,
        guest.tid().as_raw(),
        guest
            .thread_state()
            .changed_root
            .load(std::sync::atomic::Ordering::SeqCst),
    )
    .and_then(|task| std::fs::metadata(task.join("exe")).ok())
    .map(|meta| {
        use std::os::unix::fs::MetadataExt;
        (meta.dev(), meta.ino())
    });
    let (_, response) = send_and_update_time(
        guest,
        GlobalRequest::MarkPastFirstExecve(detpid, signal_identity, unprepared_exe, kernel_image),
    )
    .await;
    let mut overrides = match response {
        GlobalResponse::MarkPastFirstExecve(overrides) => *overrides,
        _ => unreachable!(),
    };
    if controlled {
        guest.thread_state_mut().signal_task_identity = signal_identity;
    }
    // The new image's `/proc/<pid>/exe` link answers from its record, or
    // without one is the kernel's.
    guest.thread_state_mut().exe = overrides.exe.take().map(Arc::new);
    if !overrides.is_empty() {
        if let Some(creator) = overrides.open_file_creator() {
            // Every carried description continues the old namespace, including
            // maps-only handoffs and the pipe-wake-latency channel namespace.
            guest.thread_state_mut().set_open_file_creator(creator);
        }
        let dettid = guest.thread_state().dettid;
        let metadata = Arc::clone(&guest.thread_state().file_metadata);
        metadata
            .lock()
            .unwrap()
            .apply_exec_fd_state(
                dettid,
                overrides,
                guest.config().backend.tool_shares_guest_descriptor_table,
            )
            .expect("prepared exec descriptor provenance no longer matches the guest table");
    }
}

/// Report to the global state that this thread, in its current syscall,
/// opened `path` and found the host file `identity`, for `hermit run
/// --verify` (see `detcore_model::host_input`). The answer carries no time.
pub(crate) async fn record_host_input<G, T>(guest: &mut G, path: String, identity: HostFileIdentity)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let syscall = guest.thread_state().stats.syscall_count;
    let (time, response) = send_and_update_time(
        guest,
        GlobalRequest::RecordHostInput {
            path,
            syscall,
            identity,
        },
    )
    .await;
    assert_eq!(
        (time, response),
        (None, GlobalResponse::RecordHostInput(()))
    );
}

/// Report to the global state that this thread rebound `path` in the guest's
/// file namespace, for `hermit run --verify` (see
/// `detcore_model::host_input::HostMutationRecord`). The answer carries no
/// time.
pub(crate) async fn record_host_mutation<G, T>(guest: &mut G, path: String)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (time, response) =
        send_and_update_time(guest, GlobalRequest::RecordHostMutation { path }).await;
    assert_eq!(
        (time, response),
        (None, GlobalResponse::RecordHostMutation(()))
    );
}

/// Record a determinism loss for the run in the global state's process,
/// whose losses verification reads (see `crate::detlog::determinism_loss`),
/// whichever process this handler runs in. The answer carries no time.
pub(crate) async fn report_determinism_loss<G, T>(guest: &mut G, reason: String)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (time, response) =
        send_and_update_time(guest, GlobalRequest::RecordDeterminismLoss(reason)).await;
    assert_eq!(
        (time, response),
        (None, GlobalResponse::RecordDeterminismLoss(()))
    );
}

// TODO-HUMAN-REVIEW(PR-643): Review the guest-to-global unsupported-syscall report path.
pub async fn report_unsupported_syscall<G, T>(guest: &mut G, sysno: Sysno)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) = send_and_update_time(
        guest,
        GlobalRequest::ReportUnsupportedSyscall(sysno.to_string()),
    )
    .await;
    assert_eq!(response, GlobalResponse::ReportUnsupportedSyscall(()));
}

/// The once-per-run WARN for a signal disposition a guest asked for and
/// Detcore answered with success but did not install.
pub(crate) fn signal_handler_not_installed_warning(signum: i32) -> String {
    format!(
        "HERMIT_SIGNAL_HANDLER_NOT_INSTALLED signum={signum} (SIGSTKFLT): the guest set a handler \
         or SIG_IGN for it and rt_sigaction reported success, but Hermit did not install it, \
         because the backend reserves SIGSTKFLT (Reverie's PMU preemption timer, SaBRe's \
         controlled exit). A SIGSTKFLT the guest receives takes the default action. Hermit \
         answers success on purpose so that programs that register it routinely, such as the \
         Go runtime, still start."
    )
}

/// Tells the coordinator the signal controls an in-guest runtime refused
/// (`GlobalRequest::AnnounceRefusedSignalControls`). Like the publications of
/// Detcore's signal-state copy, it is sent directly, not through
/// `send_and_update_time`, and carries no logical time either way.
pub(crate) async fn announce_refused_signal_controls<G, T>(
    guest: &mut G,
    actions: u64,
    alternate_stack: bool,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // Answered before any clock accounting, so the clock and address-space
    // fields are placeholders, as `shared_open_file_request`'s are.
    let (_, response) = guest
        .send_rpc((
            DetTime::default(),
            MmId::initial(DetTid::from_raw(0)),
            GlobalRequest::AnnounceRefusedSignalControls(actions, alternate_stack),
        ))
        .await;
    debug_assert_eq!(response, GlobalResponse::AnnounceRefusedSignalControls(()));
}

/// Tells the coordinator a guest's disposition for `signum` was answered with
/// success but not installed (`GlobalRequest::AnnounceSignalHandlerNotInstalled`).
pub(crate) async fn announce_signal_handler_not_installed<G, T>(guest: &mut G, signum: i32)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) = send_and_update_time(
        guest,
        GlobalRequest::AnnounceSignalHandlerNotInstalled(signum),
    )
    .await;
    assert_eq!(
        response,
        GlobalResponse::AnnounceSignalHandlerNotInstalled(())
    );
}

/// Tells the coordinator why the fail-closed policy is ending the run, before
/// this Tool image exits (`GlobalRequest::PolicyRefusal`).
pub(crate) async fn report_policy_refusal<G, T>(guest: &mut G, reason: String)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) = send_and_update_time(guest, GlobalRequest::PolicyRefusal(reason)).await;
    assert!(
        matches!(
            response,
            GlobalResponse::PolicyRefusal(()) | GlobalResponse::ThreadExited
        ),
        "unexpected response to PolicyRefusal: {response:?}"
    );
}

/// Mirrors a successful `set_tid_address(2)` into scheduler-owned exit state.
/// Tells the scheduler the calling thread's own signal mask while Detcore holds
/// it under a private blocking mask (`Some`), or that the guest's mask is back
/// (`None`).
pub(crate) async fn set_saved_guest_sigmask<G, T>(guest: &mut G, mask: Option<u64>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) =
        send_and_update_time(guest, GlobalRequest::SetSavedGuestSigmask(mask)).await;
    assert!(
        matches!(
            response,
            GlobalResponse::SetSavedGuestSigmask(()) | GlobalResponse::ThreadExited
        ),
        "unexpected response to SetSavedGuestSigmask: {response:?}"
    );
}

/// The guest's own signal mask that `thread` published while Detcore holds it
/// under a private blocking mask, if any (`Scheduler::published_guest_sigmask`):
/// the `SigBlk` its `/proc` status reports in place of the kernel's
/// (https://github.com/rrnewton/hermit/issues/4273, R2b-2 stage 2).
pub(crate) async fn published_guest_sigmask<G, T>(guest: &mut G, thread: DetTid) -> Option<u64>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) =
        send_and_update_time(guest, GlobalRequest::PublishedGuestSigmask(thread)).await;
    match response {
        GlobalResponse::PublishedGuestSigmask(mask) => mask,
        GlobalResponse::ThreadExited => None,
        other => panic!("unexpected response to PublishedGuestSigmask: {other:?}"),
    }
}

/// Tells the scheduler the set the calling thread waits for while it polls
/// `rt_sigtimedwait` (`Some`), or that it polls no more (`None`).
pub(crate) async fn set_sigtimedwait_set<G, T>(guest: &mut G, set: Option<u64>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) = send_and_update_time(guest, GlobalRequest::SetSigtimedwaitSet(set)).await;
    assert!(
        matches!(
            response,
            GlobalResponse::SetSigtimedwaitSet(()) | GlobalResponse::ThreadExited
        ),
        "unexpected response to SetSigtimedwaitSet: {response:?}"
    );
}

/// Tells the scheduler the calling thread sends SIGSTKFLT, in its own turn
/// (`Scheduler::record_guest_marker_signal`).
pub(crate) async fn record_guest_marker_signal<G, T>(guest: &mut G)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) = send_and_update_time(guest, GlobalRequest::RecordGuestMarkerSignal).await;
    assert!(
        matches!(response, GlobalResponse::RecordGuestMarkerSignal(())),
        "unexpected response to RecordGuestMarkerSignal: {response:?}"
    );
}

/// The inputs to a process-directed signal the calling thread sends to its own
/// process, read in its turn (`Scheduler::own_process_signal_inputs`).
pub(crate) async fn own_process_signal_inputs<G, T>(guest: &mut G) -> OwnProcessSignalInputs
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    match send_and_update_time(guest, GlobalRequest::OwnProcessSignalInputs)
        .await
        .1
    {
        GlobalResponse::OwnProcessSignalInputs(inputs) => inputs,
        response => unreachable!("unexpected response to OwnProcessSignalInputs: {response:?}"),
    }
}

pub(crate) async fn set_child_tid_address<G, T>(guest: &mut G, address: usize)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let (_, response) =
        send_and_update_time(guest, GlobalRequest::SetChildTidAddress(address)).await;
    assert_eq!(response, GlobalResponse::SetChildTidAddress(()));
}

/// Publishes `dispositions`, publication `generation`, as the calling
/// thread's process's (`Scheduler::publish_signal_dispositions`; `None`
/// withdraws them), and returns the scheduler's answer: the generation it
/// holds afterwards and whether this publication is what it holds, or `None`
/// when it took nothing from this thread. Like the zero-time control messages it carries no logical time and
/// its answer carries none back. It is sent from `send_and_update_time`, not
/// through it, so it neither consumes a grant's deadline nor counts forwarded
/// records.
pub(crate) async fn publish_signal_dispositions<G, T>(
    guest: &mut G,
    generation: u64,
    dispositions: Option<crate::syscalls::signal_shadow::CopiedDispositions>,
) -> Option<crate::syscalls::signal_shadow::PublicationAnswer>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let state = guest.thread_state();
    let message = (
        state.thread_logical_time.clone(),
        state.mm_id,
        GlobalRequest::PublishSignalDispositions(generation, dispositions),
    );
    match guest.send_rpc(message).await.1 {
        GlobalResponse::PublishSignalDispositions(answer) => answer,
        _ => None,
    }
}

/// Whether the scheduler took `mask` as the calling thread's
/// (`Scheduler::publish_signal_mask`; `None` withdraws it). Sent like
/// [`publish_signal_dispositions`].
pub(crate) async fn publish_signal_mask<G, T>(guest: &mut G, mask: Option<u64>) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let state = guest.thread_state();
    let message = (
        state.thread_logical_time.clone(),
        state.mm_id,
        GlobalRequest::PublishSignalMask(mask),
    );
    matches!(
        guest.send_rpc(message).await.1,
        GlobalResponse::PublishSignalMask(true)
    )
}

async fn publish_runtime_private_memory<G, T>(guest: &mut G)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let Some(facts) = guest.runtime_private_memory_regions() else {
        return;
    };
    let state = guest.thread_state();
    let mm = state.mm_id;
    if state.published_runtime_private_memory.as_ref() == Some(&(mm, facts.clone())) {
        return;
    }
    let message = (
        state.thread_logical_time.clone(),
        mm,
        GlobalRequest::PublishRuntimePrivateMemory(facts.clone()),
    );
    if guest.send_rpc(message).await.1 == GlobalResponse::PublishRuntimePrivateMemory(true) {
        guest.thread_state_mut().published_runtime_private_memory = Some((mm, facts));
    }
}

pub async fn send_and_update_time<G, T>(
    guest: &mut G,
    request: GlobalRequest,
) -> (Option<LogicalTime>, GlobalResponse)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // The successful exec callback must bind a surviving worker to its new
    // backend TID before issuing ordinary replacement-image requests. In
    // particular, a missed reconnect must not turn ThreadExited into exit(0).
    // Consuming cancellation and the reconnect itself use their authenticated
    // direct RPC paths instead of this ordinary-request helper.
    assert_eq!(
        DetTid::from_raw(guest.tid().as_raw()),
        guest.thread_state().dettid,
        "replacement image must reconnect before ordinary RPCs"
    );
    // Any request may let another thread run, which may then decide from
    // this thread's process's dispositions or this thread's mask.
    crate::syscalls::signal_shadow::publish_dispositions(guest).await;
    crate::syscalls::signal_shadow::publish_mask(guest).await;
    if !matches!(request, GlobalRequest::StartNewThread(..)) {
        publish_runtime_private_memory(guest).await;
    }
    let mytime = guest.thread_state().thread_logical_time.clone();
    let mm = guest.thread_state().mm_id;
    let mut resp = guest.send_rpc((mytime, mm, counted_request(request))).await;
    let deadline = match resp.1 {
        GlobalResponse::WithDeadline(remaining, response) => {
            resp.1 = *response;
            Some(remaining)
        }
        _ => None,
    };
    guest.thread_state_mut().last_grant_deadline = deadline;
    cap_timeslice_at_deadline(guest, deadline);
    if resp.1 == GlobalResponse::Retired {
        // A committed SIGKILL victim (`Scheduler::is_unaccounted_sigkill_victim`):
        // this callback never resumes. No error, errno or injection reaches
        // the backend; its terminal owner consumes the real death.
        trace!(
            "[detcore, dtid {}] waiting for the kernel to end a SIGKILLed thread",
            guest.thread_state().dettid
        );
        return std::future::pending().await;
    }
    if resp.1 == GlobalResponse::ThreadExited {
        let dettid = guest.thread_state().dettid;
        // A thread an exit_group killed, on DBT where the exit_group's handler
        // walks the group's robust lists after the grant: its own native exit
        // would let Linux mark and clear the same words before the walk reads
        // them, so it retires only after the walk
        // (https://github.com/rrnewton/hermit/issues/4302).
        guest.thread_state().cancel_exit_group_filing();
        if !guest
            .thread_state()
            .wait_for_exit_group_robust_walk(crate::tool_local::EXIT_GROUP_ROBUST_WALK_WAIT)
        {
            // Retiring now would let this thread's native exit race a walk
            // that may still be running, so the run fails instead.
            panic!(
                "[detcore, dtid {}] EXIT_GROUP_ROBUST_WALK_TIMEOUT: the exit_group's robust-list walk neither finished nor was cancelled within {:?}",
                dettid,
                crate::tool_local::EXIT_GROUP_ROBUST_WALK_WAIT,
            );
        }
        trace!(
            "[detcore, dtid {}] exiting after terminal scheduler cancellation",
            dettid
        );
        // This exact task must never resume, but a sibling may still own the
        // winning exit-group commit. Retire only this task; explicit backend
        // cancellation could cancel that issuer or lose its later status.
        guest.retire_current_thread().await
    }
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-845): Review applying the coordinator clock after exec reload.
    if let Some(time) = resp.0 {
        guest
            .thread_state_mut()
            .thread_logical_time
            .advance_to(time);
    }
    resp
}

/// When the thread resumes after a potentially-blocking scheduler request, is it a normal
/// continuation of execution, or is it because the thread will now execute a signal handler.
/// If the latter, that interrupts logically blocking syscalls that were in progress.
#[derive(PartialEq, Debug, Eq, Clone, Serialize, Deserialize)]
pub enum ResumeStatus {
    Normal,
    Signaled(Option<Vec<SigWrapper>>),
    /// A validated one-shot SIGALRM wake of this exact in-guest sleep.
    InGuestFatalAlarm,
    /// Granted, and nothing else can run (`SchedResponse::GoAlone`).
    NothingElseCanRun,
}

/// Internal result of a scheduler operation. Terminal results become
/// [`GlobalResponse::ThreadExited`] before returning to the guest-side RPC helper.
#[derive(PartialEq, Debug, Eq, Clone)]
enum SchedulerRpcResult<T> {
    Continue(T),
    ThreadExited,
}

/// Wake one waiter per word an exec's owner death marked, for the next
/// deterministic drain (https://github.com/rrnewton/hermit/issues/4268).
/// The INFO record is not logged on DBT, whose coordinator has no tracing
/// subscriber, so there an L2 run compares the guest's output only (Claude
/// review of https://github.com/rrnewton/hermit/pull/4352, F2).
fn wake_exec_robust_waiters(sched: &mut Scheduler, wakes: &[(DetTid, FutexID)], at: &str) {
    if wakes.is_empty() {
        return;
    }
    let counts = sched.wake_futex_waiters_after_exit(wakes);
    for ((owner, futex), count) in wakes.iter().zip(counts) {
        info!(
            "[detcore, dtid {}] robust-list owner death woke {} waiter(s) on futex {:?} at {}",
            owner, count, futex, at,
        );
    }
}

/// A process pidfd for the host process `pid` (`pidfd_open` with no flags),
/// which reports exit only once the whole thread group has exited.
pub(crate) fn open_process_pidfd(pid: i32) -> std::io::Result<std::os::fd::OwnedFd> {
    // SAFETY: pidfd_open takes no pointers.
    let raw = unsafe { libc::syscall(libc::SYS_pidfd_open, pid, 0) };
    if raw < 0 {
        return Err(std::io::Error::last_os_error());
    }
    // SAFETY: pidfd_open returned a new descriptor owned by this process.
    Ok(unsafe { std::os::fd::OwnedFd::from_raw_fd(raw as libc::c_int) })
}

/// The pidfd that holds a SIGKILL's sender until victim `detpid` is gone, or
/// none when there is nothing left to wait for. The sender is never left
/// unfenced (Codex design review of
/// https://github.com/rrnewton/hermit/issues/4362 part 2, P2-3):
/// - for a kill that named the process (`process_directed`: kill or a pidfd),
///   a zombie whose exit the schedule granted: nothing to wait for
///   (https://github.com/rrnewton/hermit/issues/4411);
/// - otherwise the victim's host process is the one registered for `named`,
///   the thread the kill named, or else, for a kill that named the process,
///   for the victim's lowest registered thread, as when a pidfd names a leader
///   that has exited while its siblings run on
///   (https://github.com/rrnewton/hermit/issues/4411). A thread-directed
///   SIGKILL to an exited thread gets no fallback, because Linux does not end
///   the group for it (Claude review of
///   https://github.com/rrnewton/hermit/pull/4439, R1). With no such
///   thread, the run is refused: whether the victim still has a live thread
///   cannot decide it, because the kill may already have reached the kernel
///   (Claude re-check of https://github.com/rrnewton/hermit/pull/4423, N1);
/// - opening the process pidfd fails with ESRCH (or EINVAL, which some older
///   kernels gave for a reaped process whose pid number lived on as a process
///   group or session id): the process is gone only if that thread's own
///   `PIDFD_THREAD` pidfd, held since it registered, reports that it exited;
///   otherwise the run is refused;
/// - any other open failure, such as EMFILE: the run is refused.
fn sigkill_fence_for(
    scheduler: &Scheduler,
    named: DetTid,
    detpid: DetPid,
    process_directed: bool,
    open: impl Fn(i32) -> std::io::Result<std::os::fd::OwnedFd>,
) -> Result<Option<std::os::fd::OwnedFd>, crate::scheduler::sigkill_retirement::UnfencedSigkill> {
    let unfenced = |reason: String| crate::scheduler::sigkill_retirement::UnfencedSigkill {
        process: detpid,
        reason,
    };
    if process_directed && scheduler.process_exit_granted(detpid) {
        return Ok(None);
    }
    let Some(via) = scheduler.sigkill_victim_registered_thread(named, detpid, process_directed)
    else {
        return Err(unfenced(if process_directed {
            format!(
                "neither the thread it named (dettid {named}) nor any other thread of it has a registered host process"
            )
        } else {
            format!(
                "the thread it named (dettid {named}) has no registered host process, and a SIGKILL to that thread alone does not end its group"
            )
        }));
    };
    let host = scheduler
        .physical_process_of(via)
        .expect("a registered thread has a host process");
    match open(host) {
        Ok(pidfd) => Ok(Some(pidfd)),
        Err(error)
            if matches!(error.raw_os_error(), Some(libc::ESRCH | libc::EINVAL))
                && scheduler.registered_thread_has_exited(via) == Some(true) =>
        {
            Ok(None)
        }
        Err(error) => Err(unfenced(format!(
            "pidfd_open of its host process failed: {error}"
        ))),
    }
}

/// `sigkill_fence_for`, ending the run on its refusal before the sender is
/// answered.
fn fence_or_refuse(
    scheduler: &Scheduler,
    named: DetTid,
    detpid: DetPid,
    process_directed: bool,
) -> Option<(DetPid, std::os::fd::OwnedFd)> {
    match sigkill_fence_for(
        scheduler,
        named,
        detpid,
        process_directed,
        open_process_pidfd,
    ) {
        Ok(fence) => fence.map(|pidfd| (detpid, pidfd)),
        Err(refusal) => crate::scheduler::exit_on_scheduler_refusal(&refusal),
    }
}

/// Wait until the process behind `pidfd` has exited, for at most `valve`;
/// why not, otherwise.
async fn wait_for_process_exit(pidfd: std::os::fd::OwnedFd, valve: Duration) -> Result<(), String> {
    let fd = tokio::io::unix::AsyncFd::with_interest(pidfd, tokio::io::Interest::READABLE)
        .map_err(|error| format!("its pidfd cannot be awaited: {error}"))?;
    match tokio::time::timeout(valve, fd.readable()).await {
        Ok(Ok(_)) => Ok(()),
        Ok(Err(error)) => Err(format!("waiting on its pidfd failed: {error}")),
        Err(_) => Err(format!("it was not gone within {valve:?}")),
    }
}

/// Whether the reply to `request` can resume its thread from a scheduler grant
/// (`Scheduler::unblock_guest`), or answers a timer it armed: the requests
/// whose replies carry a deadline hint
/// (<https://github.com/rrnewton/hermit/issues/4121>). Every handler that
/// awaits a grant is reached from one of these.
fn resumes_from_grant(request: &GlobalRequest) -> bool {
    match request {
        GlobalRequest::WithForwardedCount(_, request) => resumes_from_grant(request),
        GlobalRequest::RequestResources(..)
        | GlobalRequest::ParkedRequest(..)
        | GlobalRequest::ResumeParkedRequest { .. }
        | GlobalRequest::ResumeExec(..)
        | GlobalRequest::CreateChildThread(..)
        | GlobalRequest::CreateVforkChildThread(..)
        | GlobalRequest::StartNewThread(..)
        | GlobalRequest::FutexAction(..)
        | GlobalRequest::TraceSchedEvent(..)
        | GlobalRequest::RegisterAlarm(..)
        | GlobalRequest::AlarmRemaining(..)
        | GlobalRequest::ReconcileAlarm(..)
        | GlobalRequest::RegisterPosixTimer(..) => true,
        _ => false,
    }
}

/// End this thread's slice no later than the earliest deadline that was
/// pending when it was granted the turn it resumes
/// (<https://github.com/rrnewton/hermit/issues/4121>).
///
/// A deadline is acted on only at a scheduler turn, and a thread running a
/// long slice of cheap syscalls takes none, so without this a sleeper's
/// wake-up waits for the whole slice.
pub(crate) fn cap_timeslice_at_deadline<G, T>(guest: &mut G, deadline: Option<Duration>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let Some(remaining) = deadline else {
        return;
    };
    let clock_multiplier = guest.config().clock_multiplier.unwrap_or(1.0);
    let schedule_replay = guest.config().replay_schedule_from.is_some();
    // A configuration with an invalid turn cost is refused before any guest
    // runs; zero here only drops the floor.
    let min_slice = Duration::from_nanos(guest.config().scheduler_turn_charge().unwrap_or(0));
    guest.thread_state_mut().cap_timeslice_at_deadline(
        remaining,
        clock_multiplier,
        min_slice,
        schedule_replay,
    );
}

/// Global method RPC to request to control a resource.
///
/// Blocking: future returns only when resources are fully acquired.
pub async fn resource_request<G, T>(guest: &mut G, mut r: Resources) -> ResumeStatus
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // A request made while handling a syscall whose cost the bootstrap window
    // withholds carries that fact to the scheduler, which then withholds the
    // turn's scheduler time too (see `Scheduler::bump_global_time`). Only a
    // backend whose `is_backend_runtime_bootstrap` can return true ever sets it.
    r.backend_runtime_bootstrap |=
        guest.thread_state().in_uncharged_bootstrap_syscall && guest.is_backend_runtime_bootstrap();
    // Any turn of its own is a hand-off under `--pipe-wake-latency`: the
    // thread goes back behind the pollers it readied (`crate::pipe_wake`).
    guest.thread_state_mut().pipe_handoff_due = None;
    // Count consecutive continuations; any other request, a turn in which
    // other threads may run, starts the count again
    // (`ThreadState::consecutive_continuations`).
    let count = &mut guest.thread_state_mut().consecutive_continuations;
    *count = if r.continues_sigreturn {
        count.saturating_add(1)
    } else {
        0
    };
    if guest.config().sequentialize_threads {
        if let Some(lease) = guest.signal_observation_lease() {
            if let Some(site) = guest.parked_signal_site() {
                return parked::capable_resource_request(
                    guest,
                    r,
                    ControlCapability::PublishOnly { lease, site },
                )
                .await;
            }
            parked::terminate_protocol(guest, ProtocolFailure::Identity).await;
        }
        let dettid = guest.thread_state().dettid;
        let detpid = guest.thread_state().detpid.expect("detpid unset");
        trace!(
            "[detcore, dtid {}] BLOCKING on resource_request rpc... {:?}",
            &dettid, r
        );
        let resp =
            send_and_update_time(guest, GlobalRequest::RequestResources(r.clone(), detpid)).await;
        match resp.1 {
            GlobalResponse::RequestResources(x) => {
                trace!(
                    "[detcore, dtid {}] UNBLOCKED, acquired resources: {:?}",
                    &dettid, r
                );
                x
            }
            _ => unreachable!(),
        }
    } else {
        ResumeStatus::Normal
    }
}

/// Global method RPC to release all held resources.
///
/// Nonblocking: future may return immediately before the central global object has
/// processed the resource release.
pub async fn resource_release_all<G, T>(guest: &mut G)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    if guest.config().sequentialize_threads {
        let resp = send_and_update_time(guest, GlobalRequest::ReleaseAllResources).await;
        match resp.1 {
            GlobalResponse::ReleaseAllResources(x) => x,
            _ => unreachable!(),
        }
    }
}

/// Global method RPC to allow a new thread to begin execution, called from the child thread.
///
/// Blocking: future returns only when the thread execution is truly ready to proceed.
///
/// Returns: a history of the thread preemptions, for it to play back when --replay-preemptions-from
/// is used.
pub async fn thread_start_request<G, T>(
    cfg: &Config,
    guest: &mut G,
    detpid: DetPid,
) -> Option<ThreadHistory>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettid = guest.thread_state().dettid;
    if cfg.sequentialize_threads {
        trace!("[detcore, dtid {}] new thread BLOCKING on rpc...", &dettid);
        let physical_ids = guest
            .thread_state()
            .physical_tid
            .map(|tid| (guest.pid().as_raw(), tid));
        let signal_identity = guest.signal_task_identity();
        guest.thread_state_mut().signal_task_identity = signal_identity;
        let resp = send_and_update_time(
            guest,
            GlobalRequest::StartNewThread(dettid, detpid, physical_ids, signal_identity),
        )
        .await;
        match resp.1 {
            GlobalResponse::StartNewThread(preempts) => {
                // Publish only after the deterministic start grant. Before
                // it, parent registration and this RPC can race in host time.
                publish_runtime_private_memory(guest).await;
                trace!("[detcore, dtid {}] new thread UNBLOCKED (post-rpc)", dettid);
                preempts
            }
            _ => unreachable!(),
        }
    } else {
        None
    }
}

/// Keep only the clone pointer that requests an exit-time clear and wake.
/// SETTID alone requests a birth-time store and must not register an exit wake.
pub(crate) fn child_tid_clear_address(flags: CloneFlags, address: usize) -> usize {
    if flags.contains(CloneFlags::CLONE_CHILD_CLEARTID) {
        address
    } else {
        0
    }
}

/// Global method RPC for the parent to add a child-thread to the round-robin pool.
///
/// Nonblocking: future returning does not guarantee anything about the central scheduler,
/// except that it will eventually give a slot to the child.  Then the protocol is that
/// child will subsequently make a `thread_start_request` to gate the start of its execution.
pub async fn create_child_thread<G, T>(
    guest: &mut G,
    child_dettid: DetTid,
    ctid: usize,
    flags: Option<CloneFlags>,
    exit_signal: libc::c_int,
    physical_ids: Option<(i32, i32)>,
) -> Option<MmId>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // Random (or replayed) starting priority in chaos mode, constant priority otherwise.
    let starting_priority = if guest.config().replay_preemptions_from.is_some() {
        // In preemption replay mode, the initial priority is set on the other
        // side of the rpc, in recv_create_child_thread.
        None
    } else if guest.config().replay_schedule_from.is_some() {
        // FIXME!  Find a cleaner way to make the root thread start off high-priority:
        if child_dettid <= DetTid::from_raw(3) {
            Some(REPLAY_FOREGROUND_PRIORITY)
        } else {
            Some(REPLAY_DEFERRED_PRIORITY)
        }
    } else if guest.config().chaos {
        let entropy = guest
            .thread_state_mut()
            .chaos_prng_next_u64("child_priority");
        if guest.config().chaos_target_races {
            // Targeted chaos: bias a freshly created child to an extreme priority
            // so it either runs before the parent resumes or strictly after it,
            // instead of landing at a uniformly random priority. This maximizes
            // parent/child ordering divergence to surface fork/exec races.
            // Reproducible under `--fuzz-seed`/`--sched-seed`.
            if entropy.is_multiple_of(2) {
                Some(FIRST_PRIORITY)
            } else {
                Some(LAST_PRIORITY)
            }
        } else {
            Some(entropy_to_priority(entropy))
        }
    } else {
        Some(DEFAULT_PRIORITY)
    };

    let detpid = guest.thread_state().detpid.expect("detpid unset");

    let resp = send_and_update_time(
        guest,
        GlobalRequest::CreateChildThread(
            child_dettid,
            detpid,
            ctid,
            flags,
            exit_signal,
            physical_ids,
            starting_priority,
        ),
    )
    .await;
    match resp.1 {
        GlobalResponse::CreateChildThread(x) => x,
        _ => unreachable!(),
    }
}

/// Register a vfork child while its parent is blocked inside `clone(2)`.
///
/// Unlike an ordinary clone, the parent cannot perform this registration
/// because the kernel has blocked it until the child execs or exits. The child
/// therefore registers itself, carrying the inherited parent identity, flags,
/// and starting priority. The starting priority is derived the same way as an
/// ordinary clone so that chaos and replay scheduling stay deterministic.
pub async fn create_vfork_child_thread<G, T>(
    guest: &mut G,
    child_dettid: DetTid,
    vfork: crate::tool_local::PendingVfork,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let starting_priority = if guest.config().replay_preemptions_from.is_some() {
        None
    } else if guest.config().replay_schedule_from.is_some() {
        Some(if child_dettid <= DetTid::from_raw(3) {
            REPLAY_FOREGROUND_PRIORITY
        } else {
            REPLAY_DEFERRED_PRIORITY
        })
    } else if guest.config().chaos {
        Some(entropy_to_priority(vfork.child_priority_entropy.expect(
            "vfork child priority entropy missing in chaos mode",
        )))
    } else {
        // POSIX vfork suspends the parent until the child execs or _exits. Give
        // the vfork child a strictly higher priority (lower number) than the
        // parent's DEFAULT_PRIORITY so the deterministic scheduler always runs
        // the child first, rather than round-robining the parent and child at
        // equal priority (which leaves fork/exec ordering nondeterministic).
        Some(DEFAULT_PRIORITY - 1)
    };

    let resp = send_and_update_time(
        guest,
        GlobalRequest::CreateVforkChildThread(
            vfork.parent_dettid,
            vfork.parent_detpid,
            child_dettid,
            vfork.child_tid_addr,
            vfork.flags,
            vfork.exit_signal,
            starting_priority,
        ),
    )
    .await;
    match resp.1 {
        GlobalResponse::CreateChildThread(_) => (),
        _ => unreachable!(),
    }
}

/// Remove the thread from the scheduler.
///
/// Nonblocking: the future may return immediately, not guaranteeing the changes to the
/// scheduler have been completed.
pub(crate) async fn deregister_thread<R>(
    threads_time: DetTime,
    cfg: &Config,
    reverie: &R,
    thread: ThreadDeregistration,
) where
    // Note, this is called from a context where we DON'T have a full, operable `Guest`.
    R: GlobalRPC<GlobalState>,
{
    if cfg.sequentialize_threads {
        let mm = thread.mm;
        // TODO: void_send_rpc
        let resp = reverie
            .send_rpc((threads_time, mm, {
                // The thread's last count; its slot is free for a later thread.
                let request = counted_request(GlobalRequest::DeregisterThread(thread));
                crate::detlog::release_forwarded_count();
                request
            }))
            .await;
        // We can't update the thread time here.  But it's dead anyway!
        match resp.1 {
            GlobalResponse::DeregisterThread(x) => x,
            _ => unreachable!(),
        }
    }
}

/// Account this physical-exit callback's own clock before it joins a staged
/// robust-list batch. The empty wake request has no scheduler effects, but its
/// ordinary RPC header is admitted and accounted before it is acknowledged.
/// A retired incarnation must not contribute to the completed-exit barrier.
pub(crate) async fn acknowledge_robust_list_exit_time<R>(
    threads_time: DetTime,
    reverie: &R,
    mm: MmId,
) -> bool
where
    R: GlobalRPC<GlobalState>,
{
    let response = reverie
        .send_rpc((threads_time, mm, GlobalRequest::RobustListWakes(Vec::new())))
        .await;
    match response.1 {
        GlobalResponse::RobustListWakes(counts) => {
            assert!(
                counts.is_empty(),
                "an empty exit-clock acknowledgement woke a waiter"
            );
            true
        }
        GlobalResponse::ThreadExited => false,
        _ => unreachable!(),
    }
}

/// Deliver owner-death wakes from an exit callback that no longer has guest
/// memory access. The callback runs only after ptrace has observed physical
/// exit, so Linux's atomic robust-list word update is already complete.
pub(crate) async fn robust_list_wakes_after_exit<R>(
    threads_time: DetTime,
    reverie: &R,
    mm: MmId,
    wakes: Vec<(DetTid, RobustListWake)>,
) -> Vec<u64>
where
    R: GlobalRPC<GlobalState>,
{
    if wakes.is_empty() {
        return Vec::new();
    }
    let response = reverie
        .send_rpc((
            threads_time,
            mm,
            GlobalRequest::RobustListWakes(
                wakes
                    .into_iter()
                    .map(|(owner, wake)| (owner, wake.futex))
                    .collect(),
            ),
        ))
        .await;
    match response.1 {
        GlobalResponse::RobustListWakes(counts) => counts,
        _ => unreachable!(),
    }
}

/// Deliver the owner-death wakes staged at a successful exec's entry. The
/// exec has returned, so Linux has already walked every robust list in the old
/// thread group and changed each owner word atomically.
pub(crate) async fn robust_list_wakes_after_exec<G, T>(
    guest: &mut G,
    wakes: Vec<(DetTid, RobustListWake)>,
) -> Vec<u64>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    if wakes.is_empty() {
        return Vec::new();
    }
    let (_, response) = send_and_update_time(
        guest,
        GlobalRequest::RobustListWakes(
            wakes
                .into_iter()
                .map(|(owner, wake)| (owner, wake.futex))
                .collect(),
        ),
    )
    .await;
    match response {
        GlobalResponse::RobustListWakes(counts) => counts,
        _ => unreachable!(),
    }
}

/// Which actions we can take before/after a futex system call.
#[derive(PartialEq, Debug, Eq, Clone, Copy, Serialize, Deserialize)]
pub enum FutexAction {
    /// Check in before a FUTEX_WAIT, including an optional timeout and, on a backend
    /// that reports the kernel's signal state, the waiter's mask and where the
    /// scheduler reads its dispositions when it commits a wake. With `None`, as
    /// before, a scheduler-sent signal wakes the waiter and a cross-task signal
    /// does not.
    WaitRequest(Option<LogicalTime>, Option<FutexSignalWatch>),
    /// Check in after a FUTEX_WAIT
    WaitFinished,
    /// Check in before a FUTEX_WAKE, parameterized by the number of threads woken.
    WakeRequest(i32),
    /// Check in after a FUTEX_WAKE, parameterized by the number of threads woken.
    WakeFinished(i32),
    /// Check in for a plain FUTEX_REQUEUE or FUTEX_CMP_REQUEUE of the request's
    /// futex onto the given one, waking up to the first count and moving up to
    /// the second (`Scheduler::requeue_futex_waiters`). Both counts are already
    /// checked to be non-negative, and the futex word already compared for
    /// FUTEX_CMP_REQUEUE. Answers with the number woken plus the number moved,
    /// and is followed by a `WakeFinished`, as a wake is.
    RequeueRequest(FutexID, u32, u32),
}

/// Ask scheduler for permission to proceed before/after futex operation.
/// Returns true if the operation completed normally, and false if it timed out.
pub async fn futex_action<G, T>(
    guest: &mut G,
    futex_action: FutexAction,
    futexid: &FutexID,
    init_read: i32,
    mask: u32,
) -> Option<SchedValue>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    assert!(guest.config().sequentialize_threads);
    let dettid = guest.thread_state().dettid;
    let req = GlobalRequest::FutexAction(dettid, futex_action, *futexid, init_read, mask);
    trace!(
        "BLOCKING on futex_action: sending request to scheduler: {:?}",
        req
    );
    // Update local time from potentially blocking operation:
    let resp = send_and_update_time(guest, req.clone()).await;
    match resp.1 {
        GlobalResponse::FutexAction(answer) => {
            trace!("UNBLOCKING after futex_action. Request was: {:?}", req);
            answer
        }
        _ => unreachable!(),
    }
}

/// track a (possibly new) inode, by returning a deterministic inode.
/// Also return the logical mtime for the inode, though this is only
/// used if `virtualize_metadata` is set.
///
/// For callers that have not read the file's metadata. A stat must use
/// [`determinize_inode_observing_mtime`] so the first-seen mtime policy sees
/// the host mtime.
pub async fn determinize_inode<G, T>(guest: &mut G, inode: RawInode) -> (DetInode, LogicalTime)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    determinize_inode_observing_mtime(
        guest,
        inode,
        None,
        ObservedMtime::Unobserved,
        InodeSighting::Descriptor,
    )
    .await
}

/// [`determinize_inode`] for a directory entry, which names a linked file
/// (see [`InodeSighting::Name`]). `procfs` names a procfs entry (see
/// `procfs_inode::ProcfsName`).
pub async fn determinize_named_inode<G, T>(
    guest: &mut G,
    inode: RawInode,
    procfs: Option<ProcfsName>,
) -> (DetInode, LogicalTime)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    determinize_inode_observing_mtime(
        guest,
        inode,
        procfs,
        ObservedMtime::Unobserved,
        InodeSighting::Name,
    )
    .await
}

/// Like [`determinize_inode`], for a caller that observed the file's host
/// mtime. If the file's virtual mtime is still unresolved, `observed` resolves
/// it: a canonical mtime (see [`CANONICAL_FILE_MTIME_SECONDS`]) is kept, and any
/// other becomes the epoch. `procfs` names a procfs entry (see
/// `procfs_inode::ProcfsName`).
pub async fn determinize_inode_observing_mtime<G, T>(
    guest: &mut G,
    inode: RawInode,
    procfs: Option<ProcfsName>,
    observed: ObservedMtime,
    sighting: InodeSighting,
) -> (DetInode, LogicalTime)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(
        guest,
        GlobalRequest::DeterminizeInode(inode, observed, sighting, procfs),
    )
    .await;
    match resp.1 {
        GlobalResponse::DeterminizeInode(x) => x,
        _ => unreachable!(),
    }
}

/// Determinize the inode of every file-backed line of one `/proc/*/maps` read.
/// `inodes` holds one entry per line, in line order, with repeats: each entry
/// is one numbering request, so how many requests a read makes depends on the
/// guest's mappings alone, never on whether two lines name the same host file
/// (<https://github.com/rrnewton/hermit/issues/2897>). The result is in the
/// same order.
pub async fn determinize_mapping_inodes<G, T>(guest: &mut G, inodes: Vec<RawInode>) -> Vec<DetInode>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::DeterminizeMappingInodes(inodes)).await;
    match resp.1 {
        GlobalResponse::DeterminizeMappingInodes(x) => x,
        _ => unreachable!(),
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping RPC.
/// Translate a host-assigned device number (`st_dev`) to a deterministic one.
pub async fn determinize_device<G, T>(guest: &mut G, raw_device: u64) -> u64
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::DeterminizeDevice(raw_device)).await;
    match resp.1 {
        GlobalResponse::DeterminizeDevice(x) => x,
        _ => unreachable!(),
    }
}

/// Translate an observed fdinfo mount ID to the shared run-local identity.
pub async fn determinize_mount_id<G, T>(
    guest: &mut G,
    raw_mount_id: u64,
    mountinfo_order: Option<Vec<u64>>,
) -> Option<u64>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(
        guest,
        GlobalRequest::DeterminizeMountId(raw_mount_id, mountinfo_order),
    )
    .await;
    match resp.1 {
        GlobalResponse::DeterminizeMountId(value) => value,
        _ => unreachable!(),
    }
}

/// Seed or validate the run-global mount-ID pool against one mountinfo
/// snapshot's canonical order, and return the run-local identity of each entry
/// of `observation_order`, numbering unseen entries in that order. `None`
/// means validation failed or the two orders name different raw IDs.
pub async fn resolve_mountinfo_identities<G, T>(
    guest: &mut G,
    mountinfo_order: Vec<u64>,
    observation_order: Vec<u64>,
) -> Option<Vec<u64>>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(
        guest,
        GlobalRequest::ResolveMountinfoIds(mountinfo_order, observation_order),
    )
    .await;
    match resp.1 {
        GlobalResponse::ResolveMountinfoIds(value) => value,
        _ => unreachable!(),
    }
}

/// The file behind `inode` lost its last name (see `InodePool::retire`).
pub async fn retire_inode<G, T>(guest: &mut G, inode: RawInode)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::RetireInode(inode)).await;
    match resp.1 {
        GlobalResponse::RetireInode(x) => x,
        _ => unreachable!(),
    }
}

/// The inode pool's state for a directory snapshot about to be taken (see
/// [`SnapshotMark`]).
pub async fn directory_snapshot_mark<G, T>(guest: &mut G) -> SnapshotMark
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::DirectorySnapshotMark).await;
    match resp.1 {
        GlobalResponse::DirectorySnapshotMark(mark) => mark,
        _ => unreachable!(),
    }
}

/// `--pipe-wake-latency`: report that the calling thread's action readied the
/// pollers of `channel` that want `wants` (`Scheduler::ready_channel`).
pub async fn ready_channel<G, T>(
    guest: &mut G,
    channel: crate::pipe_wake::ChannelId,
    wants: crate::pipe_wake::Wants,
) -> crate::pipe_wake::ReadyOutcome
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::ReadyChannel(channel, wants)).await;
    match resp.1 {
        GlobalResponse::ReadyChannel(outcome) => outcome,
        _ => unreachable!(),
    }
}

/// `--pipe-wake-latency`: the calling thread's hand-off is due. Returns
/// whether the scheduler moved it behind the pollers it readied, so that it
/// must now yield (`Scheduler::hand_off_turn`).
pub async fn hand_off_turn<G, T>(guest: &mut G) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::HandOffTurn).await;
    match resp.1 {
        GlobalResponse::HandOffTurn(yields) => yields,
        _ => false,
    }
}

/// Record that the calling thread is delivering a signal whose default action
/// ends its process (`Scheduler::record_committed_fatal_signal`).
pub async fn record_fatal_signal<G, T>(guest: &mut G)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let _ = send_and_update_time(guest, GlobalRequest::RecordFatalSignal).await;
}

/// [`determinize_inode`] for an entry of a directory snapshot taken at
/// `mark` (see [`SnapshotMark`]). `procfs` names a procfs entry (see
/// `procfs_inode::ProcfsName`).
pub async fn determinize_listed_inode<G, T>(
    guest: &mut G,
    inode: RawInode,
    procfs: Option<ProcfsName>,
    mark: SnapshotMark,
) -> (DetInode, LogicalTime)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    determinize_inode_observing_mtime(
        guest,
        inode,
        procfs.map(|name| name.in_snapshot(mark.threads_counted)),
        ObservedMtime::Unobserved,
        InodeSighting::Listed(mark.retirements),
    )
    .await
}

/// An `open` found `inode` linked or freshly created (see
/// `InodePool::forget_retired`). `procfs` names a procfs entry (see
/// `procfs_inode::ProcfsName`), so that writes through the new descriptor
/// update the entry that stats number.
pub async fn forget_retired_inode<G, T>(guest: &mut G, inode: RawInode, procfs: Option<ProcfsName>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::ForgetRetiredInode(inode, procfs)).await;
    match resp.1 {
        GlobalResponse::ForgetRetiredInode(x) => x,
        _ => unreachable!(),
    }
}

/// Update the modification time for a file, using its inode.
/// This will set the mtime to a coherent global-time value.
pub async fn touch_file<G, T>(guest: &mut G, inode: RawInode)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::TouchFile(inode)).await;
    match resp.1 {
        GlobalResponse::TouchFile(x) => x,
        _ => unreachable!(),
    }
}

/// Set a file's virtual mtime to a guest-requested time.
pub async fn set_file_mtime<G, T>(guest: &mut G, inode: RawInode, mtime: LogicalTime)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::SetFileMtime(inode, mtime)).await;
    match resp.1 {
        GlobalResponse::SetFileMtime(x) => x,
        _ => unreachable!(),
    }
}

/// Read the global clock, or at least a deterministic lower bound on it.
pub async fn global_time_lower_bound<G, T>(guest: &mut G) -> LogicalTime
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, GlobalRequest::GlobalTimeLowerBound).await;
    match resp.1 {
        GlobalResponse::GlobalTimeLowerBound(x) => x,
        _ => unreachable!(),
    }
}

/// Take a time observation from the current thread. This extra indirection
/// helps abstract over whether or not we need to use local or global
/// information for this.
pub async fn thread_observe_time<G, T>(guest: &mut G) -> LogicalTime
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    global_time_lower_bound(guest).await
}

/// Writes a structured json backtrace to a given file
fn write_backtrace<G, T>(guest: &mut G, m_path: Option<&PathBuf>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    if let Some(backtrace) = guest.backtrace() {
        if let Some(path) = m_path {
            let file = File::create(path).expect("Failed to open preemption stacktrace log file");
            serde_json::to_writer(file, &backtrace.force_pretty()).unwrap();
        } else {
            crate::util::capped_eprintln!("{}", backtrace.force_pretty());
        }
    } else {
        warn!("Could not read backtrace!");
    }
}

/// Additional instructions to a guest after shed events is consumed by global tool
#[derive(PartialEq, Debug, Eq, Clone, Serialize, Deserialize)]
pub struct TraceSchedEventResponse {
    print_stack_strace: MaybePrintStack,
    timeslice: Option<LogicalTime>,
}

struct SchedEventForLog<'a> {
    event: &'a SchedEvent,
    command_bootstrap: bool,
}

struct CommandBootstrapInstructionPointer(NonZeroUsize);

impl std::fmt::Debug for CommandBootstrapInstructionPointer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", crate::logdiff::host_addr(self.0.get()))
    }
}

impl std::fmt::Debug for SchedEventForLog<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if !self.command_bootstrap {
            return std::fmt::Debug::fmt(self.event, f);
        }
        f.debug_struct("SchedEvent")
            .field("dettid", &self.event.dettid)
            .field("op", &self.event.op)
            .field("count", &self.event.count)
            .field(
                "start_rip",
                &self.event.start_rip.map(CommandBootstrapInstructionPointer),
            )
            .field(
                "end_rip",
                &self.event.end_rip.map(CommandBootstrapInstructionPointer),
            )
            .field("end_time", &self.event.end_time)
            .finish()
    }
}

/// Record an event in the schedule trace, OR check the event on replay.
/// This also prints the backtrace of the schedevent, if indicated.
///
/// Arguments:
/// - tag_end_rip: read the current guest registers to fill in the `end_rip` on the event with the
///   current instruction pointer.
pub async fn trace_schedevent<G, T>(guest: &mut G, ev: SchedEvent, tag_end_rip: bool)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    assert!(guest.config().sequentialize_threads);

    // trace_schedevent is called AFTER the event is complete, and the rip is resting just after it.
    let ev = if tag_end_rip {
        let end_rip = if let Some(r) = ev.end_rip {
            r
        } else {
            // While a SIGALRM delivery's signal pipeline runs at a syscall's
            // completion, the registers are its completed projection.
            let regs = match guest.thread_state().signal_register_projection {
                Some(projection) => projection,
                None => guest.regs().await,
            };
            NonZeroUsize::new(regs.rip.try_into().unwrap()).unwrap()
        };
        SchedEvent {
            end_rip: Some(end_rip),
            ..ev
        }
    } else {
        ev
    };

    if tracing::enabled!(tracing::Level::TRACE)
        && let Some(rip) = ev.end_rip
    {
        let rip_addr = AddrMut::<u16>::from_raw(rip.into()).unwrap();
        // These bytes are diagnostic, not part of the recorded event. A guest
        // may legitimately make its memory unreadable (PR_SET_DUMPABLE), so
        // preserve that observation without unwinding the live run owner.
        let rip_contents = guest.memory().read_value(rip_addr);
        trace!(
            "Tracing sched event, after which rip is {}, next two instruction bytes {:?}",
            rip, rip_contents
        );
    }

    let detpid = guest.thread_state().detpid.expect("detpid unset");
    let command_bootstrap = guest.is_command_bootstrap();
    let resp = send_and_update_time(
        guest,
        GlobalRequest::TraceSchedEvent(ev, detpid, command_bootstrap),
    )
    .await;

    trace!("trace_schedevent result: {:?}", resp);
    match resp {
        (
            _,
            GlobalResponse::TraceSchedEvent(TraceSchedEventResponse {
                print_stack_strace,
                timeslice,
            }),
        ) => {
            if let Some(m_path) = print_stack_strace {
                trace!("[trace_schedevent] writing stacktrace via Reverie...");
                write_backtrace(guest, m_path.as_ref());
            }

            if let Some(timeslice) = timeslice
                && guest.thread_state().past_global_first_execve
            {
                let end_of_timeslice =
                    guest.thread_state().thread_logical_time.as_nanos() + timeslice;
                trace!(
                    "[detcore][dettid {}] setting end_of_timeslice to {:?} as instructed by replayer",
                    guest.thread_state().dettid,
                    end_of_timeslice
                );
                guest.thread_state_mut().end_of_timeslice = Some(end_of_timeslice);
                if guest.config().max_timeslice.is_some() {
                    guest.thread_state_mut().max_timeslice_end = Some(end_of_timeslice);
                }
            }
        }
        _ => {
            unreachable!()
        }
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
// TODO-HUMAN-REVIEW(#869)
/// Register an alarm (delayed signal delivery) with the global scheduler.
/// Returns the logical duration remaining until any previously scheduled alarm.
pub async fn register_alarm<G, T>(
    guest: &mut G,
    duration: LogicalTime,
    interval: LogicalTime,
    sig: Signal,
) -> (LogicalTime, LogicalTime)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettid = guest.thread_state().dettid;
    let detpid = guest.thread_state().detpid.expect("detpid unset");
    let resp = send_and_update_time(
        guest,
        GlobalRequest::RegisterAlarm(detpid, dettid, duration, interval, SigWrapper::from(sig)),
    )
    .await;
    match resp.1 {
        GlobalResponse::RegisterAlarm(x) => x,
        _ => unreachable!(),
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-841): Review logical alarm query API.
/// Return the logical duration remaining on the process's one-shot alarm.
pub async fn alarm_remaining<G, T>(guest: &mut G) -> ItimerSnapshot
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let detpid = guest.thread_state().detpid.expect("detpid unset");
    let resp = send_and_update_time(guest, GlobalRequest::AlarmRemaining(detpid)).await;
    match resp.1 {
        GlobalResponse::AlarmRemaining(remaining) => remaining,
        _ => unreachable!(),
    }
}

/// Rule 4/5 notifications for the coalescing ITIMER_REAL
/// (<https://github.com/rrnewton/hermit/issues/4139>): a committed guest
/// SIGALRM send, or a change of SIGALRM's disposition.
pub async fn note_sigalrm_event<G, T>(guest: &mut G, request: GlobalRequest)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let resp = send_and_update_time(guest, request).await;
    match resp.1 {
        GlobalResponse::NoteGuestSigalrm => {}
        _ => unreachable!(),
    }
}

/// O2: after a call that may have dequeued a SIGALRM from this process's
/// shared queue (a signalfd read, an `rt_sigtimedwait` completion), let the
/// scheduler restart an `Inactive` ITIMER_REAL at this point in the turn
/// (<https://github.com/rrnewton/hermit/issues/4139>).
pub async fn reconcile_alarm<G, T>(guest: &mut G)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let detpid = guest.thread_state().detpid.expect("detpid unset");
    let resp = send_and_update_time(guest, GlobalRequest::ReconcileAlarm(detpid)).await;
    match resp.1 {
        GlobalResponse::ReconcileAlarm => {}
        _ => unreachable!(),
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#869)
/// Register, re-arm, or disarm a POSIX timer with the global scheduler, or
/// delete it.
pub async fn register_posix_timer<G, T>(
    guest: &mut G,
    timer_id: i32,
    deadline: Option<LogicalTime>,
    interval: LogicalTime,
    notification: PosixTimerNotification,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettid = guest.thread_state().dettid;
    let detpid = guest.thread_state().detpid.expect("detpid unset");
    let resp = send_and_update_time(
        guest,
        GlobalRequest::RegisterPosixTimer(
            detpid,
            dettid,
            timer_id,
            deadline,
            interval,
            notification,
        ),
    )
    .await;
    match resp.1 {
        GlobalResponse::RegisterPosixTimer(()) => {}
        _ => unreachable!(),
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
/// Return the scheduler's live threads for a positive process ID.
/// Does a live thread with this tid exist, leader or not?
///
/// Distinct from [`resolve_kill_targets`], which models `kill(2)` and therefore
/// only recognises thread-group leaders. Syscalls that resolve a task through
/// `find_task_by_vpid` -- `sched_setattr` among them -- must use this instead,
/// or a non-leader thread reports ESRCH while it is plainly running.
pub async fn thread_is_live<G, T>(guest: &mut G, dettid: DetTid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ThreadIsLive(dettid)).await;
    match response.1 {
        GlobalResponse::ThreadIsLive(live) => live,
        _ => unreachable!(),
    }
}

/// Whether a `clone3` `set_tid` naming `dettid` is refused: a task of this
/// run had that id, and it is not the pid of a zombie child of the caller's
/// process (`Scheduler::id_is_a_zombie_child_of`). "Had" is a registration with
/// the scheduler, which is never removed, or a clock component in the global
/// time (which exec reassignment can move away from an id that stays
/// registered). Such a zombie's pid is left to the kernel, which answers
/// EEXIST, and only the caller's own process can release it. Any other used id is
/// refused, a live task's included: whether a live thread is still alive when
/// the kernel runs the call can depend on host timing (a thread killed
/// without a granted exit deregisters when the host says), and a freed id
/// given to a new task would merge two tasks' state, keyed by that id, in
/// Detcore (`Detcore::clone3_set_tid_refusal`,
/// https://github.com/rrnewton/hermit/issues/4370).
pub(crate) async fn thread_id_refused<G, T>(guest: &mut G, dettid: DetTid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ThreadIdRefused(dettid)).await;
    match response.1 {
        GlobalResponse::ThreadIdRefused(refused) => refused,
        _ => unreachable!(),
    }
}

/// Whether `named` is a thread-group leader the scheduler retired at its own
/// exit while its group lives on, so the kernel shows it a zombie
/// (`Scheduler::names_a_retired_exited_leader`,
/// https://github.com/rrnewton/hermit/issues/4445).
pub(crate) async fn names_a_retired_exited_leader<G, T>(guest: &mut G, named: DetTid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response =
        send_and_update_time(guest, GlobalRequest::NamesARetiredExitedLeader(named)).await;
    match response.1 {
        GlobalResponse::NamesARetiredExitedLeader(retired) => retired,
        _ => unreachable!(),
    }
}

/// Arm (`Some(signal)`, 0 disarms) or read (`None`) the calling task's
/// parent-death signal that Detcore sends itself when its parent thread
/// leaves (`Scheduler::deliver_parent_death_signals`,
/// https://github.com/rrnewton/hermit/issues/4440). Returns the value held
/// after the call.
pub(crate) async fn parent_death_signal<G, T>(guest: &mut G, arm: Option<i32>) -> i32
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ParentDeathSignal(arm)).await;
    match response.1 {
        GlobalResponse::ParentDeathSignal(signal) => signal,
        _ => unreachable!(),
    }
}

/// The real-time signals (a kernel sigset) of `detpid`'s POSIX timers that
/// expired while every thread of the process blocked them, and that are still
/// pending (https://github.com/rrnewton/hermit/issues/4089). Asked in the
/// calling thread's turn, while no timed event can pop, so the answer is a
/// function of the schedule.
pub(crate) async fn pending_real_time_expirations<G, T>(guest: &mut G, detpid: DetPid) -> u64
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response =
        send_and_update_time(guest, GlobalRequest::PendingRealTimeExpirations(detpid)).await;
    match response.1 {
        GlobalResponse::PendingRealTimeExpirations(pending) => pending,
        GlobalResponse::ThreadExited => 0,
        other => unreachable!("unexpected response to PendingRealTimeExpirations: {other:?}"),
    }
}

/// Record that a handler's return in `detpid` restores a mask leaving
/// `unblocked` (real-time signals) unblocked; see
/// `Scheduler::record_handler_restore_unblocked`.
pub(crate) async fn record_handler_restore_mask<G, T>(guest: &mut G, detpid: DetPid, unblocked: u64)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(
        guest,
        GlobalRequest::RecordHandlerRestoreMask(detpid, unblocked),
    )
    .await;
    assert!(
        matches!(
            response.1,
            GlobalResponse::RecordHandlerRestoreMask(()) | GlobalResponse::ThreadExited
        ),
        "unexpected response to RecordHandlerRestoreMask: {:?}",
        response.1
    );
}

/// Record that `detpid` created a signalfd for `mask` (a kernel sigset with a
/// real-time signal); see `Scheduler::register_signalfd`.
pub(crate) async fn register_signalfd<G, T>(guest: &mut G, detpid: DetPid, mask: u64)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::RegisterSignalfd(detpid, mask)).await;
    assert!(
        matches!(
            response.1,
            GlobalResponse::RegisterSignalfd(()) | GlobalResponse::ThreadExited
        ),
        "unexpected response to RegisterSignalfd: {:?}",
        response.1
    );
}

/// Record, under record and replay, that the calling thread's `close`,
/// `close_range`, `dup2` or `dup3` released or replaced its descriptors
/// `first..=last`; see `Scheduler::note_descriptors_replaced`.
pub(crate) async fn descriptors_replaced<G, T>(guest: &mut G, first: u32, last: u32)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    if !guest.config().recordreplay_modes {
        return;
    }
    let files = guest.thread_state().files_id();
    let response = send_and_update_time(
        guest,
        GlobalRequest::DescriptorsReplaced(files, first, last),
    )
    .await;
    assert!(
        matches!(
            response.1,
            GlobalResponse::DescriptorsReplaced(()) | GlobalResponse::ThreadExited
        ),
        "unexpected response to DescriptorsReplaced: {:?}",
        response.1
    );
}

/// Discard `signals` from `detpid`'s pending real-time timer expirations, as
/// Linux flushes a pending signal whose disposition becomes `SIG_IGN`.
pub(crate) async fn discard_pending_real_time_expirations<G, T>(
    guest: &mut G,
    detpid: DetPid,
    signals: u64,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(
        guest,
        GlobalRequest::DiscardPendingRealTimeExpirations(detpid, signals),
    )
    .await;
    assert!(
        matches!(
            response.1,
            GlobalResponse::DiscardPendingRealTimeExpirations(()) | GlobalResponse::ThreadExited
        ),
        "unexpected response to DiscardPendingRealTimeExpirations: {:?}",
        response.1
    );
}

/// Records, in the calling thread's turn, that it armed a host-timed source of
/// `signals` (a kernel sigset) reaching the processes in `scope`, before the
/// arming call reaches the kernel. Returns the signals the caller's gated waits
/// now hold (`Scheduler::record_host_timed_signals`).
pub async fn record_host_timed_signals<G, T>(
    guest: &mut G,
    scope: HostTimedSignalScope,
    signals: u64,
) -> u64
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettid = guest.thread_state().dettid;
    let response = send_and_update_time(
        guest,
        GlobalRequest::RecordHostTimedSignals(dettid, scope, signals),
    )
    .await;
    match response.1 {
        GlobalResponse::HostTimedSignals(signals) => signals,
        _ => unreachable!(),
    }
}

/// The signals (a kernel sigset) that no gated wait of the calling thread ends
/// for, because a host-timed source a guest armed can post them to its process
/// (`Scheduler::host_timed_signals`). Read it in the waiting thread's turn.
pub async fn host_timed_signals<G, T>(guest: &mut G) -> u64
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettid = guest.thread_state().dettid;
    let response = send_and_update_time(guest, GlobalRequest::HostTimedSignals(dettid)).await;
    match response.1 {
        GlobalResponse::HostTimedSignals(signals) => signals,
        _ => unreachable!(),
    }
}

/// The calling thread's host-timed signals (`host_timed_signals`) and the
/// `SIGCHLD` a guest sent it that the schedule recorded in its private queue,
/// in one round trip (`Scheduler::host_timed_and_private_sigchld`). Read it in
/// the waiting thread's turn, before any injection of that turn.
pub(crate) async fn host_timed_and_private_sigchld<G, T>(guest: &mut G) -> (u64, u64)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettid = guest.thread_state().dettid;
    let response =
        send_and_update_time(guest, GlobalRequest::HostTimedAndPrivateSigchld(dettid)).await;
    match response.1 {
        GlobalResponse::HostTimedAndPrivateSigchld(host_timed, private) => (host_timed, private),
        _ => unreachable!(),
    }
}

/// The scheduler's answer to a child-exit `SIGCHLD` control message from the
/// calling thread (`Scheduler::child_exit_sigchld_control`). It carries no
/// logical time either way.
pub(crate) async fn child_exit_sigchld<G, T>(
    guest: &mut G,
    control: crate::scheduler::child_exit_sigchld::ChildExitSigchldControl,
) -> crate::scheduler::child_exit_sigchld::ChildExitSigchldAnswer
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ChildExitSigchld(control)).await;
    match response.1 {
        GlobalResponse::ChildExitSigchld(answer) => answer,
        _ => unreachable!(),
    }
}

/// The scheduler's answer to a discarded-at-send record message from the
/// calling thread (`Scheduler::discarded_signal_control`). It carries no
/// logical time either way.
pub(crate) async fn discarded_signal<G, T>(
    guest: &mut G,
    control: crate::scheduler::discarded_at_send::DiscardedSignalControl,
) -> crate::scheduler::discarded_at_send::DiscardedSignalAnswer
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::DiscardedSignal(control)).await;
    match response.1 {
        GlobalResponse::DiscardedSignal(answer) => answer,
        _ => unreachable!(),
    }
}

/// Whether the scheduler's SIGALRM ledger refuses `control` for the calling
/// thread (`Scheduler::sigalrm_control`). Asked in the thread's own turn; it
/// carries no logical time either way.
pub async fn sigalrm_refuses<G, T>(guest: &mut G, control: SigalrmControl) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::Sigalrm(control)).await;
    match response.1 {
        GlobalResponse::Sigalrm(refused) => refused,
        _ => unreachable!(),
    }
}

/// EPERM when the scheduler's SIGALRM ledger refuses `control`
/// ([`sigalrm_refuses`]).
pub async fn refuse_sigalrm<G, T>(
    guest: &mut G,
    control: SigalrmControl,
) -> Result<(), nix::errno::Errno>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    if sigalrm_refuses(guest, control).await {
        Err(nix::errno::Errno::EPERM)
    } else {
        Ok(())
    }
}

/// Return scheduler-owned lifecycle state for an exact child-process wait.
pub async fn exact_child_wait_state<G, T>(guest: &mut G, child: DetPid) -> ExactChildWaitState
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let parent = guest.thread_state().detpid.expect("detpid unset");
    let response =
        send_and_update_time(guest, GlobalRequest::ExactChildWaitState(parent, child)).await;
    match response.1 {
        GlobalResponse::ExactChildWaitState(state) => state,
        _ => unreachable!(),
    }
}

/// Wait without requesting a scheduler turn for a backend's physical-exit report.
pub async fn await_exact_child_physical_exit<G, T>(
    guest: &mut G,
    child: DetPid,
) -> ExactChildWaitState
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let mut state = exact_child_wait_state(guest, child).await;
    if matches!(
        state,
        ExactChildWaitState::PhysicalExitPending | ExactChildWaitState::PhysicallyExited
    ) {
        let dettid = guest.thread_state().dettid;
        let mut resources = Resources::new(dettid);
        resources.insert(ResourceID::WaitPhysicalChild(child), Permission::R);
        resources.fyi("wait-child-physical-exit");
        let _ = resource_request(guest, resources).await;
        state = exact_child_wait_state(guest, child).await;
    }
    state
}

/// Park until an exact or any-child process wait has a logical exit to reap.
pub async fn wait_for_child_lifecycle<G, T>(guest: &mut G, spec: ChildWaitSpec) -> ResumeStatus
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettid = guest.thread_state().dettid;
    let parent = guest.thread_state().detpid.expect("detpid unset");
    let mut resources = Resources::new(dettid);
    resources.insert(ResourceID::WaitChild { parent, spec }, Permission::R);
    resources.fyi("wait-child-lifecycle");
    resource_request(guest, resources).await
}

pub async fn ready_child_wait<G, T>(guest: &mut G, spec: ChildWaitSpec) -> (Option<DetPid>, bool)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let parent = guest.thread_state().detpid.expect("detpid unset");
    let response = send_and_update_time(guest, GlobalRequest::ReadyChildWait(parent, spec)).await;
    match response.1 {
        GlobalResponse::ReadyChildWait(snapshot) => snapshot,
        _ => unreachable!(),
    }
}

pub async fn process_group<G, T>(guest: &mut G, process: DetPid) -> Option<DetPid>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ProcessGroup(process)).await;
    match response.1 {
        GlobalResponse::ProcessGroup(group) => group,
        _ => unreachable!(),
    }
}

/// Linux's answer to `setsid()` by `process`, from Detcore's model
/// (`Scheduler::setsid_decision`).
pub async fn setsid_decision<G, T>(guest: &mut G, process: DetPid) -> Result<(), reverie::Errno>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::SetsidDecision(process)).await;
    match response.1 {
        GlobalResponse::SetsidDecision(decision) => decision.map_err(reverie::Errno::new),
        _ => unreachable!(),
    }
}

/// Linux's answer to `setpgid(pid, pgid)` by `caller`, from Detcore's model
/// (`Scheduler::setpgid_decision`): the process to move and its group.
pub async fn setpgid_decision<G, T>(
    guest: &mut G,
    caller: DetPid,
    pid: i32,
    pgid: i32,
) -> Result<(DetPid, DetPid), reverie::Errno>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response =
        send_and_update_time(guest, GlobalRequest::SetpgidDecision(caller, pid, pgid)).await;
    match response.1 {
        GlobalResponse::SetpgidDecision(decision) => decision.map_err(reverie::Errno::new),
        _ => unreachable!(),
    }
}

/// The process group (`session` false) or session (`session` true) of the
/// task with id `pid` in Detcore's model, as `getpgid` and `getsid` answer it
/// (`Scheduler::group_or_session_of`); `None` where Linux has no such task.
pub async fn group_or_session_of<G, T>(guest: &mut G, pid: DetPid, session: bool) -> Option<DetPid>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::GroupOrSession(pid, session)).await;
    match response.1 {
        GlobalResponse::GroupOrSession(answer) => answer,
        _ => unreachable!(),
    }
}

/// The image guest process `process` executes, as Detcore recorded it at its
/// exec (`crate::exec_record`); `None` without a record, and for a process
/// that has exited.
pub(crate) async fn process_image<G, T>(
    guest: &mut G,
    process: DetPid,
) -> Option<crate::exec_record::ExecRecord>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    match send_and_update_time(guest, GlobalRequest::ProcessImage(process))
        .await
        .1
    {
        GlobalResponse::ProcessImage(image) => image.map(|image| *image),
        _ => unreachable!(),
    }
}

pub async fn set_process_group<G, T>(guest: &mut G, process: DetPid, group: DetPid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response =
        send_and_update_time(guest, GlobalRequest::SetProcessGroup(process, group)).await;
    match response.1 {
        GlobalResponse::SetProcessGroup(updated) => updated,
        _ => unreachable!(),
    }
}

/// The process Detcore registered `dettid` in (`ThreadTree::process_of`), in
/// the calling thread's turn.
pub async fn registered_process_of<G, T>(guest: &mut G, dettid: DetTid) -> Option<DetPid>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::RegisteredProcessOf(dettid)).await;
    match response.1 {
        GlobalResponse::RegisteredProcessOf(process) => process,
        _ => unreachable!(),
    }
}

/// Reserve the victims of the SIGKILL the calling thread is about to send to
/// `target`'s process (`scheduler::sigkill_retirement`). `None` keeps the
/// ordinary path.
pub async fn reserve_sigkill<G, T>(
    guest: &mut G,
    target: DetTid,
) -> Option<crate::scheduler::sigkill_retirement::SigkillReservation>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // Backends that cannot retire at the kill turn send no extra RPC.
    if !crate::scheduler::sigkill_retirement::retires_sigkill_at_the_kill_turn(
        &guest.config().backend,
    ) {
        return None;
    }
    let sender = guest.thread_state().dettid;
    // Read before the send, while the target certainly exists.
    let uid =
        crate::syscalls::real_uid(target.as_raw()).unwrap_or_else(|| unsafe { libc::getuid() });
    let response =
        send_and_update_time(guest, GlobalRequest::ReserveSigkill(sender, target, uid)).await;
    match response.1 {
        GlobalResponse::ReserveSigkill(token) => token,
        _ => unreachable!(),
    }
}

/// Mirror a successful `prctl(PR_SET_CHILD_SUBREAPER, on)` by `process` in
/// the scheduler's thread tree, in the calling thread's turn.
pub async fn set_child_subreaper<G, T>(guest: &mut G, process: DetPid, on: bool)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::SetChildSubreaper(process, on)).await;
    match response.1 {
        GlobalResponse::SetChildSubreaper(()) => {}
        _ => unreachable!(),
    }
}

/// What the `sigev_notify_thread_id` `named` of a `timer_create` by the
/// calling thread names (`Scheduler::posix_timer_thread`).
pub async fn posix_timer_thread<G, T>(guest: &mut G, named: i32) -> PosixTimerThread
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let process = guest.thread_state().detpid.expect("detpid unset");
    let response =
        send_and_update_time(guest, GlobalRequest::PosixTimerThread(process, named)).await;
    match response.1 {
        GlobalResponse::PosixTimerThread(answer) => answer,
        _ => unreachable!(),
    }
}

/// A disposition change in the calling thread's process flushed `signo` from
/// every thread (`Scheduler::forget_thread_timer_copies`).
pub async fn forget_thread_timer_copies<G, T>(guest: &mut G, signo: i32)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let process = guest.thread_state().detpid.expect("detpid unset");
    let response = send_and_update_time(
        guest,
        GlobalRequest::ForgetThreadTimerCopies(process, signo),
    )
    .await;
    match response.1 {
        GlobalResponse::ForgetThreadTimerCopies(()) => {}
        _ => unreachable!(),
    }
}

/// The POSIX timer copies the calling thread's successful exec flushes
/// (`Scheduler::take_exec_timer_flush`). Asked in the exec's turn, only in a
/// run that has recorded a timer copy or kept a real-time expiration pending.
pub async fn exec_timer_flush<G, T>(
    guest: &mut G,
) -> Result<crate::scheduler::ExecTimerFlush, String>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let process = guest.thread_state().detpid.expect("detpid unset");
    let response = send_and_update_time(guest, GlobalRequest::ExecTimerFlush(process)).await;
    match response.1 {
        GlobalResponse::ExecTimerFlush(answer) => answer,
        _ => unreachable!(),
    }
}

/// Retire the calling thread-group leader, which has made its last request
/// before completing its own `exit`, if its group lives on
/// (`Scheduler::retire_exited_leader`). Returns whether it was retired.
pub async fn retire_exited_leader<G, T>(guest: &mut G) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::RetireExitedLeader).await;
    match response.1 {
        GlobalResponse::RetireExitedLeader(retired) => retired,
        _ => unreachable!(),
    }
}

/// What the positive pid `pid`, named by the calling thread in its nested PID
/// namespace, names (`PidNamespaces::resolve_for`). Asked only by a thread
/// below the container's namespace (`ThreadState::nested_pid_namespace`).
pub async fn guest_pid<G, T>(
    guest: &mut G,
    pid: i32,
) -> crate::scheduler::pid_namespaces::GuestPidAnswer
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let caller = guest.thread_state().dettid;
    let response = send_and_update_time(guest, GlobalRequest::GuestPid(caller, pid)).await;
    match response.1 {
        GlobalResponse::GuestPid(answer) => answer,
        _ => unreachable!(),
    }
}

/// The processes `/proc` lists and, for `process`, the threads its `task`
/// directory lists, as a thread in the container's PID namespace numbers
/// them (`Scheduler::procfs_listing`).
pub async fn procfs_listing<G, T>(
    guest: &mut G,
    process: Option<DetPid>,
) -> crate::procfs::listing::ListingFacts
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ProcfsListing(process)).await;
    match response.1 {
        GlobalResponse::ProcfsListing(facts) => facts,
        _ => unreachable!(),
    }
}

/// What Detcore's process table says of the task a per-process `/proc` file
/// names (`Scheduler::procfs_task`), with the incarnation of its numbers.
pub async fn procfs_task<G, T>(
    guest: &mut G,
    task: crate::procfs::BoundTask,
) -> crate::procfs::listing::ProcfsTaskAnswer
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let request = GlobalRequest::ProcfsTask(
        DetTid::from_raw(task.process),
        task.thread.map(DetTid::from_raw),
    );
    let response = send_and_update_time(guest, request).await;
    match response.1 {
        GlobalResponse::ProcfsTask(answer) => answer,
        _ => unreachable!(),
    }
}

/// Record the auxiliary vector the sender's process's new image found on
/// its initial stack (`ProcessAuxvTable::record`).
pub(crate) async fn record_process_auxv<G, T>(guest: &mut G, bytes: Vec<u8>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::RecordProcessAuxv(bytes))
        .await
        .1;
    assert_eq!(response, GlobalResponse::RecordProcessAuxv(()));
}

/// The process whose image `/proc/<process>/auxv` describes (a thread's
/// number names its process), and that image's auxiliary vector
/// (`ProcessAuxvTable::current`).
pub(crate) async fn process_auxv<G, T>(guest: &mut G, process: DetPid) -> Option<ProcessAuxv>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    match send_and_update_time(guest, GlobalRequest::ProcessAuxv(process))
        .await
        .1
    {
        GlobalResponse::ProcessAuxv(answer) => answer,
        _ => unreachable!(),
    }
}

/// Record that the sender's process changed its root with `chroot`
/// (`ProcessAuxvTable::mark_changed_root`).
pub(crate) async fn mark_changed_root<G, T>(guest: &mut G)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::MarkChangedRoot)
        .await
        .1;
    assert_eq!(response, GlobalResponse::MarkChangedRoot(()));
}

/// Whether the process `task` names has changed its root
/// (`ProcessAuxvTable::root_changed`).
pub(crate) async fn root_changed<G, T>(guest: &mut G, task: DetPid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    match send_and_update_time(guest, GlobalRequest::RootChanged(task))
        .await
        .1
    {
        GlobalResponse::RootChanged(changed) => changed,
        _ => unreachable!(),
    }
}

/// Record the OFD and flock locks `open_file` holds, as its fdinfo showed
/// them after a call through one of the sender's descriptors, and that
/// `slots` slots of the sender's table name it
/// (`procfs::locks::LockLedger::note_locks`), or why that fdinfo could not be
/// read (`procfs::locks::LockLedger::note_unknown`).
pub(crate) async fn note_description_locks<G, T>(
    guest: &mut G,
    open_file: OpenFileId,
    locks: Result<Vec<crate::procfs::locks::HeldLock>, String>,
    slots: u32,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(
        guest,
        GlobalRequest::DescriptionLocks(open_file, locks, slots),
    )
    .await
    .1;
    assert_eq!(response, GlobalResponse::DescriptionLocks(()));
}

/// Record that `remaining` slots of the sender's table still name
/// `open_file` (`procfs::locks::LockLedger::release`).
pub(crate) async fn note_table_released<G, T>(guest: &mut G, open_file: OpenFileId, remaining: u32)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::TableReleased(open_file, remaining))
        .await
        .1;
    assert_eq!(response, GlobalResponse::TableReleased(()));
}

/// Record which descriptions whose locks Detcore read the sender's table
/// still names after its successful exec
/// (`procfs::locks::LockLedger::exec_kept`).
pub(crate) async fn note_table_exec<G, T>(guest: &mut G, surviving: Vec<(OpenFileId, u32)>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::TableExec(surviving))
        .await
        .1;
    assert_eq!(response, GlobalResponse::TableExec(()));
}

/// Record that a lock or unlock went through a descriptor whose description
/// the ledger may know under another identity, on the file with inode number
/// `inode` (`None`: unknown) (`procfs::locks::LockLedger::alias_inode`).
pub(crate) async fn note_aliased_inode<G, T>(guest: &mut G, inode: Option<String>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::AliasedInode(inode))
        .await
        .1;
    assert_eq!(response, GlobalResponse::AliasedInode(()));
}

/// Record that every later `/proc/locks` snapshot is refused, for `reason`
/// (`procfs::locks::LockLedger::refuse`).
pub(crate) async fn note_lock_ledger_refused<G, T>(guest: &mut G, reason: String)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::LockLedgerRefused(reason))
        .await
        .1;
    assert_eq!(response, GlobalResponse::LockLedgerRefused(()));
}

/// Which of the rows of a `/proc/locks` snapshot the guest owns, in row order
/// (`procfs::locks::guest_lock_rows`), or why no snapshot can be decided
/// (`procfs::locks::LockLedger::unknown`).
pub(crate) async fn guest_lock_rows<G, T>(
    guest: &mut G,
    rows: Vec<crate::procfs::locks::LockRowFacts>,
) -> Result<Vec<bool>, String>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    match send_and_update_time(guest, GlobalRequest::GuestLockRows(rows))
        .await
        .1
    {
        GlobalResponse::GuestLockRows(kept) => kept,
        _ => unreachable!(),
    }
}

/// Report that the sender's process's new image's `/proc/<pid>/auxv` is left
/// to the kernel beneath for `why`, and learn whether it is the run's first
/// for that reason, which the caller warns of: the guest's tool prints where
/// the run's output shows it, which the global tool's log does not on DBT.
pub(crate) async fn auxv_from_kernel<G, T>(guest: &mut G, why: AuxvFromKernel) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    match send_and_update_time(guest, GlobalRequest::AuxvFromKernel(why))
        .await
        .1
    {
        GlobalResponse::AuxvFromKernel(first) => first,
        _ => unreachable!(),
    }
}

/// Change or read the state Detcore keeps on the rendered per-process `/proc`
/// file numbered `inode` (`InodePool::rendered_file_state`).
pub async fn rendered_file_state<G, T>(
    guest: &mut G,
    inode: u64,
    change: crate::procfs::listing::RenderedFileChange,
) -> crate::procfs::listing::RenderedFileState
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response =
        send_and_update_time(guest, GlobalRequest::RenderedFileState(inode, change)).await;
    match response.1 {
        GlobalResponse::RenderedFileState(state) => state,
        _ => unreachable!(),
    }
}

/// The kernel's number for each guest process or thread number in `ids`
/// (`to_kernel`), or the guest's for each kernel number, as `/proc` spells
/// them (`Scheduler::procfs_ids`).
pub async fn procfs_ids<G, T>(guest: &mut G, to_kernel: bool, ids: Vec<i32>) -> Vec<Option<i32>>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ProcfsIds(to_kernel, ids)).await;
    match response.1 {
        GlobalResponse::ProcfsIds(ids) => ids,
        _ => unreachable!(),
    }
}

/// The pid the calling thread, in its nested PID namespace, sees for `task`
/// (`PidNamespaces::localize_for`): `Ok(None)` when `task` is not visible
/// there.
pub async fn local_pid<G, T>(guest: &mut G, task: DetTid) -> Result<Option<i32>, String>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let caller = guest.thread_state().dettid;
    let response = send_and_update_time(guest, GlobalRequest::LocalPid(caller, task)).await;
    match response.1 {
        GlobalResponse::LocalPid(answer) => answer,
        _ => unreachable!(),
    }
}

/// The process a process-directed signal naming `named` goes to: a live
/// non-leader thread's id names its thread group, as Linux's kill_pid_info
/// does (`Scheduler::signal_target_process`).
pub async fn signal_target_process<G, T>(guest: &mut G, named: DetPid) -> Option<DetPid>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::SignalTargetProcess(named)).await;
    match response.1 {
        GlobalResponse::SignalTargetProcess(process) => process,
        _ => unreachable!(),
    }
}

/// Mirror a successful `rt_sigaction(SIGCHLD, ...)` by `process` in the
/// scheduler's thread tree, in the calling thread's turn.
pub async fn set_sigchld_disposition<G, T>(
    guest: &mut G,
    process: DetPid,
    ignored: bool,
    nocldwait: bool,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(
        guest,
        GlobalRequest::SetSigchldDisposition(process, ignored, nocldwait),
    )
    .await;
    match response.1 {
        GlobalResponse::SetSigchldDisposition(()) => {}
        _ => unreachable!(),
    }
}

/// Reserve the victims of the `kill(-pgid, SIGKILL)` the calling thread is
/// about to send (`Scheduler::reserve_sigkill_group`). Every guest process has
/// the same credentials, so the members share the caller's real uid.
pub async fn reserve_sigkill_group<G, T>(
    guest: &mut G,
    pgid: DetPid,
) -> Option<crate::scheduler::sigkill_retirement::SigkillReservation>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // Backends that cannot retire at the kill turn send no extra RPC.
    if !crate::scheduler::sigkill_retirement::retires_sigkill_at_the_kill_turn(
        &guest.config().backend,
    ) {
        return None;
    }
    let sender = guest.thread_state().dettid;
    let uid = crate::syscalls::real_uid(guest.tid().as_raw())
        .unwrap_or_else(|| unsafe { libc::getuid() });
    let response =
        send_and_update_time(guest, GlobalRequest::ReserveSigkillGroup(sender, pgid, uid)).await;
    match response.1 {
        GlobalResponse::ReserveSigkill(token) => token,
        _ => unreachable!(),
    }
}

/// Commit (`sent`) or unwind a SIGKILL reservation at the sending boundary.
pub async fn commit_sigkill<G, T>(guest: &mut G, token: u64, sent: bool)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::CommitSigkill(token, sent)).await;
    match response.1 {
        GlobalResponse::CommitSigkill(()) => {}
        _ => unreachable!(),
    }
}

pub async fn create_session<G, T>(guest: &mut G, process: DetPid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::CreateSession(process)).await;
    match response.1 {
        GlobalResponse::CreateSession(updated) => updated,
        _ => unreachable!(),
    }
}

pub async fn consume_child_wait<G, T>(guest: &mut G, child: DetPid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let parent = guest.thread_state().detpid.expect("detpid unset");
    let response =
        send_and_update_time(guest, GlobalRequest::ConsumeChildWait(parent, child)).await;
    match response.1 {
        GlobalResponse::ConsumeChildWait(consumed) => consumed,
        _ => unreachable!(),
    }
}

pub async fn resolve_kill_targets<G, T>(guest: &mut G, detpid: DetPid) -> Vec<DetTid>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    resolve_kill_targets_and_exit(guest, detpid).await.0
}

/// The live threads of `detpid`, and its exit state
/// (`Scheduler::exited_process_is_zombie`).
pub async fn resolve_kill_targets_and_exit<G, T>(
    guest: &mut G,
    detpid: DetPid,
) -> (Vec<DetTid>, Option<bool>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::ResolveKillTargets(detpid)).await;
    match response.1 {
        GlobalResponse::ResolveKillTargets(targets, zombie) => (targets, zombie),
        _ => unreachable!(),
    }
}

/// Whether `tid`, a pid or tid the calling guest task named (in thread group
/// `tgid`, for the calls that name one), is one of Hermit's own tasks; see
/// [`hermit_task_named`].
pub async fn names_hermit_task<G, T>(guest: &mut G, tgid: Option<i32>, tid: i32) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let caller = guest.thread_state().dettid;
    let response =
        send_and_update_time(guest, GlobalRequest::NamesHermitTask(caller, tgid, tid)).await;
    match response.1 {
        GlobalResponse::NamesHermitTask(named) => named,
        _ => unreachable!(),
    }
}

/// Whether guest task `caller`, naming `tid` (and thread group `tgid`, for the
/// calls that name one), names one of Hermit's own tasks: a thread of the
/// process Detcore's global state runs in (<https://github.com/rrnewton/hermit/issues/4066>).
/// Under ptrace that is Hermit's, the container init, PID 1 of the guest's PID
/// namespace. Under DBT the global state runs in the root guest process, whose
/// guest ids are virtual and are not host tasks descended from it, so the
/// answer there is always no, as for KVM below.
///
/// Hermit's tasks are identified directly, as its own thread group, not as
/// "not a registered guest task": a guest task Detcore has not registered yet
/// (a newborn, before its parent's `clone` returns) is not Hermit's. The
/// thread-group test is `tgkill(hermit, tid, 0)`, which sends nothing.
///
/// Only a caller that is a host process descended from Hermit, in Hermit's
/// own PID namespace, can name Hermit's tasks:
/// - a caller in a nested PID namespace names tasks by that namespace's ids,
///   where Hermit's tasks are not visible;
/// - a backend whose guest tasks are not host tasks (KVM, whose guest ids may
///   equal the ids of Hermit's own threads) never reaches Hermit by a pid;
/// - a caller that is one of Hermit's threads, or whose `/proc` entry cannot
///   be read, is not such a host task: a guest task in a system call is alive.
pub fn hermit_task_named(caller: DetTid, tgid: Option<i32>, tid: i32) -> bool {
    use std::os::unix::fs::MetadataExt;
    let hermit = std::process::id() as i32;
    if tid <= 0 || tgid.is_some_and(|tgid| tgid != hermit) {
        return false;
    }
    // SAFETY: signal 0 to a thread of this process only checks it exists.
    let in_hermits_group =
        tid == hermit || unsafe { libc::syscall(libc::SYS_tgkill, hermit, tid, 0) } == 0;
    if !in_hermits_group || !descends_from(caller.as_raw(), hermit) {
        return false;
    }
    let namespace = |path: String| fs::metadata(path).map(|meta| (meta.dev(), meta.ino()));
    match (
        namespace(format!("/proc/{}/ns/pid", caller.as_raw())),
        namespace("/proc/self/ns/pid".to_owned()),
    ) {
        (Ok(callers), Ok(hermits)) => callers == hermits,
        _ => false,
    }
}

/// Whether host task `task`'s process is a descendant of process `ancestor`,
/// following parent pids in `/proc` (field 4 of `stat`, the process's parent
/// for any of its threads). A task that cannot be read is not.
fn descends_from(task: i32, ancestor: i32) -> bool {
    let parent = |pid: i32| -> Option<i32> {
        let stat = fs::read_to_string(format!("/proc/{pid}/stat")).ok()?;
        // The command name may hold spaces and parentheses: fields resume
        // after the last ')', as state, ppid.
        stat.rsplit_once(')')?
            .1
            .split_whitespace()
            .nth(1)?
            .parse()
            .ok()
    };
    let mut pid = task;
    // Bounded by the depth of any real process tree; a reparenting race
    // cannot make it loop.
    for _ in 0..4096 {
        match parent(pid) {
            Some(next) if next == ancestor => return true,
            Some(next) if next > 0 => pid = next,
            _ => return false,
        }
    }
    false
}

/// Tell the scheduler that a SIGKILL it is not told about one victim at a time
/// succeeded: a process-group kill (`Some(group)`), or a pidfd with no recorded
/// process (`None`). See `Scheduler::record_unresolved_sigkill`.
pub async fn notify_unresolved_sigkill<G, T>(guest: &mut G, group: Option<DetPid>)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::NotifyUnresolvedSigkill(group)).await;
    match response.1 {
        GlobalResponse::NotifyUnresolvedSigkill(()) => {}
        _ => unreachable!(),
    }
}

/// Tell the scheduler that a successful kill(2) left a physical
/// signal pending for a sole, known target.
/// The `nix` signal a timer will raise.
///
/// Timer registration still models a named signal: `setitimer`/`timer_create`
/// reach here with one, and the timer wheel stores it. Widening that is a
/// separate axis from the notification defect this change fixes, so the
/// conversion is asserted here rather than silently widened — a realtime timer
/// signal would be a NEW capability, not a regression of an existing one.
fn alarm_signal(sig: SigWrapper) -> Signal {
    sig.signal().unwrap_or_else(|| {
        panic!(
            "timer registration received unnameable signal {}",
            sig.raw()
        )
    })
}

/// Ask that this thread's next request, its group `Exit`, be granted before
/// any other thread runs (`GlobalRequest::HoldTurnForGroupExit`). Returns
/// whether the scheduler agreed, which it does only for the thread holding the
/// serial grant.
pub async fn hold_turn_for_group_exit<G, T>(guest: &mut G, dettid: DetTid) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(guest, GlobalRequest::HoldTurnForGroupExit(dettid)).await;
    match response.1 {
        GlobalResponse::HoldTurnForGroupExit(held) => held,
        _ => unreachable!(),
    }
}

pub async fn notify_signal_pending<G, T>(
    guest: &mut G,
    dettid: DetTid,
    signal: SigWrapper,
    target_process: Option<DetPid>,
    scope: SignalScope,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let response = send_and_update_time(
        guest,
        GlobalRequest::NotifySignalPending(dettid, signal, target_process, scope),
    )
    .await;
    match response.1 {
        GlobalResponse::NotifySignalPending(()) => {}
        _ => unreachable!(),
    }
}

/// Signal an unrecoverable error that exits the entire container.
/// Such exits are not determinizable (see "quasi-determinism").
///
/// ⚠️ `status` IS NOT A DEFAULTABLE PARAMETER, AND THAT IS THE POINT. This
/// function has four callers and they do not all mean the same thing: three are
/// fail-closed policy refusals (`HERMIT_POLICY_REFUSAL_EXIT`) and one is an
/// operator interrupt (`HERMIT_SIGINT_DEATH_EXIT`). While the status was baked in
/// here, every caller inherited whatever the last edit chose, so the SIGINT path
/// reported "hermit refused this run" for a run hermit did not refuse. Making it
/// an argument turns the meaning into a visible claim at each call site, which is
/// the same rule `scripts/check-exit-status-class.rs` enforces on the test side:
/// a status you can read is worth less than a status that says which channel
/// produced it.
///
/// Adding a caller means choosing; there is deliberately no default to fall into.
pub async fn unrecoverable_shutdown<G, T>(guest: &G, status: i32) -> !
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    // The process ends here (`crate::guest_log`).
    crate::guest_log::flush_unsent();
    if cfg!(debug_assertions) {
        let mytime = guest.thread_state().thread_logical_time.clone();
        let mm = guest.thread_state().mm_id;
        // TODO: void_send_rpc
        let _ = guest
            .send_rpc((mytime, mm, GlobalRequest::UnrecoverableShutdown))
            .await;
    }

    // In this scenario a backtrace doesn't really help us.
    //
    // ⚠️ THE STATUS IS THE ONLY THING THAT CROSSES THIS BOUNDARY, SO IT HAS TO
    // CARRY THE MEANING. This is a DELIBERATE, policy-driven shutdown: the run
    // hit an operation it cannot service under a fail-closed configuration and
    // hermit chose to stop. The parent sees only a container child that exited,
    // and its classifier treats an exit it cannot account for as unchosen — so
    // exiting 1 here made `classify_container_result` report
    // `class=container-child-exit` and 125, i.e. "hermit broke", for a shutdown
    // that worked exactly as designed. `HERMIT_POLICY_REFUSAL_EXIT` is the
    // agreed value for "hermit refused"; see its doc for why it is not 1.
    //
    // ⚠️ AND WHICH VALUE IS THE CALLER'S TO SAY, NOT THIS FUNCTION'S. Baking one
    // in here is what made an operator's Ctrl-C report as a policy refusal: the
    // condition differs per caller and only the caller knows it.
    //
    // The RPC above is `cfg!(debug_assertions)`-only, so it cannot be the
    // signal — in a release build the parent would learn nothing.
    std::process::exit(status);
}

#[cfg(test)]
mod tests {
    /// A process reads its current image's vector; a forked child its
    /// creator's until it execs; an exec replaces it; a forgotten process
    /// (gone, or an exec with no recorded vector) has none.
    #[test]
    fn a_changed_root_follows_fork_and_goes_at_exit() {
        use super::ProcessAuxvTable;
        use crate::types::DetPid;
        let (parent, child, other) = (
            DetPid::from_raw(3),
            DetPid::from_raw(4),
            DetPid::from_raw(5),
        );
        let mut table = ProcessAuxvTable::default();
        table.clone_root(DetPid::from_raw(1), parent, false);
        assert!(!table.root_changed(parent));
        table.mark_changed_root(parent);
        assert!(table.root_changed(parent));
        // A new task without CLONE_FS starts with a copy of its creator's
        // root.
        table.clone_root(parent, child, false);
        assert!(table.root_changed(child));
        table.clone_root(other, child, false);
        assert!(!table.root_changed(child), "a reused number starts afresh");
        // An exec keeps the root, so only an exit forgets it.
        table.forget(parent);
        assert!(table.root_changed(parent));
        table.forget_process(parent);
        assert!(!table.root_changed(parent));
    }

    /// Tasks created with CLONE_FS share one root, as Linux's `fs_struct`:
    /// a chroot by the child changes its creator's root too, a task created
    /// without CLONE_FS keeps its own, and a non-leader thread's exec takes
    /// its group to the process's number.
    #[test]
    fn a_root_is_shared_by_a_filesystem_group() {
        use super::ProcessAuxvTable;
        use crate::types::DetPid;
        let pid = DetPid::from_raw;
        let mut table = ProcessAuxvTable::default();
        table.clone_root(pid(1), pid(3), false);
        // A CLONE_FS|SIGCHLD child, and an ordinary fork as an observer.
        table.clone_root(pid(3), pid(4), true);
        table.clone_root(pid(3), pid(5), false);
        table.mark_changed_root(pid(4));
        assert!(
            table.root_changed(pid(3)),
            "the child's chroot is its creator's"
        );
        assert!(
            !table.root_changed(pid(5)),
            "a copied root stays where it was"
        );
        // A thread created without CLONE_FS has a root of its own.
        table.clone_root(pid(5), pid(6), false);
        table.mark_changed_root(pid(6));
        assert!(!table.root_changed(pid(5)));
        // Thread 6's exec gives it process 5's number, and its root.
        table.transfer_root(pid(6), pid(5));
        assert!(table.root_changed(pid(5)));
        // The group outlives a member that exits.
        table.forget_process(pid(4));
        assert!(table.root_changed(pid(3)));
    }

    #[test]
    fn process_auxv_follows_exec_and_fork() {
        use super::ProcessAuxv;
        use super::ProcessAuxvTable;
        use crate::types::DetPid;
        let (parent, child) = (DetPid::from_raw(3), DetPid::from_raw(4));
        let mut table = ProcessAuxvTable::default();
        assert_eq!(table.current(parent), None);
        table.record(parent, vec![1; 16]);
        table.inherit(parent, child);
        assert_eq!(
            table.current(child),
            Some(ProcessAuxv {
                process: child,
                bytes: vec![1; 16]
            })
        );
        table.record(child, vec![2; 32]);
        assert_eq!(
            table.current(child).map(|entry| entry.bytes),
            Some(vec![2; 32])
        );
        assert_eq!(
            table.current(parent).map(|entry| entry.bytes),
            Some(vec![1; 16])
        );
        table.forget(parent);
        assert_eq!(table.current(parent), None);
        // A creator with no vector gives its child none.
        table.inherit(parent, child);
        assert_eq!(table.current(child), None);
        assert_eq!(table.len(), 0);
    }

    /// Hermit's tasks are exactly its own thread group, named from its own PID
    /// namespace (<https://github.com/rrnewton/hermit/issues/4066>). The test
    /// process stands in for Hermit.
    #[test]
    fn only_hermits_own_thread_group_is_hermits() {
        use super::hermit_task_named;
        use crate::types::DetTid;
        let me = std::process::id() as i32;
        // A child process stands in for a guest task.
        let mut guest = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .unwrap();
        let caller = DetTid::from_raw(guest.id() as i32);
        assert!(hermit_task_named(caller, None, me));
        assert!(hermit_task_named(caller, Some(me), me));
        // One of Hermit's own threads is not a guest task, and names nothing
        // of Hermit's (KVM guest ids can equal them).
        // SAFETY: gettid has no preconditions.
        let own_thread = DetTid::from_raw(unsafe { libc::gettid() });
        assert!(!hermit_task_named(own_thread, None, me));
        let (send, receive) = std::sync::mpsc::channel();
        let (stop, stopped) = std::sync::mpsc::channel::<()>();
        let thread = std::thread::spawn(move || {
            // SAFETY: as above.
            send.send(unsafe { libc::gettid() }).unwrap();
            let _ = stopped.recv();
        });
        let other_thread = receive.recv().unwrap();
        assert!(hermit_task_named(caller, None, other_thread));
        assert!(hermit_task_named(caller, Some(me), other_thread));
        let guest_pid = guest.id() as i32;
        assert!(!hermit_task_named(caller, None, guest_pid));
        assert!(!hermit_task_named(caller, Some(guest_pid), guest_pid));
        assert!(!hermit_task_named(caller, Some(guest_pid), other_thread));
        assert!(!hermit_task_named(caller, None, 0));
        assert!(!hermit_task_named(caller, None, -1));
        // A caller that is not a host task descended from Hermit names nothing
        // of Hermit's: one that cannot be read, or Hermit's own parent.
        assert!(!hermit_task_named(DetTid::from_raw(i32::MAX), None, me));
        // SAFETY: getppid has no preconditions.
        let parent = DetTid::from_raw(unsafe { libc::getppid() });
        assert!(!hermit_task_named(parent, None, me));
        guest.kill().unwrap();
        guest.wait().unwrap();
        stop.send(()).unwrap();
        thread.join().unwrap();
    }

    /// The DBT SIGKILL fence waits on a process pidfd: a live victim keeps it
    /// waiting to the valve, and a dead one, even unreaped, releases it
    /// (https://github.com/rrnewton/hermit/issues/4362).
    #[test]
    fn the_dbt_sigkill_fence_waits_until_the_victim_process_is_gone() {
        use super::open_process_pidfd;
        use super::wait_for_process_exit;
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        let mut victim = std::process::Command::new("sleep")
            .arg("30")
            .spawn()
            .unwrap();
        let pid = victim.id() as i32;
        let live = open_process_pidfd(pid).expect("a live child has a pidfd");
        let waited = runtime.block_on(wait_for_process_exit(
            live,
            std::time::Duration::from_millis(50),
        ));
        assert!(waited.unwrap_err().contains("not gone within"));
        let dying = open_process_pidfd(pid).expect("a live child has a pidfd");
        victim.kill().unwrap();
        assert_eq!(
            runtime.block_on(wait_for_process_exit(
                dying,
                std::time::Duration::from_secs(30)
            )),
            Ok(())
        );
        victim.wait().unwrap();
        assert!(open_process_pidfd(pid).is_err());
    }

    /// The DBT SIGKILL fence never leaves the sender unfenced while the victim
    /// may still be running: an open failure refuses, and an ESRCH counts as
    /// the victim gone only once the thread the kill named has exited, as its
    /// registration pidfd shows (Codex design review of
    /// https://github.com/rrnewton/hermit/issues/4362 part 2, P2-3).
    #[test]
    fn the_dbt_sigkill_fence_refuses_rather_than_leave_the_sender_unfenced() {
        use super::sigkill_fence_for;
        use crate::scheduler::Scheduler;
        use crate::types::DetPid;
        use crate::types::DetTid;
        use crate::types::MmId;
        let config = crate::config::Config::default()
            .with_backend(|backend| *backend = reverie::BackendCapabilities::DBT);
        let mut scheduler = Scheduler::new(&config);
        let me = std::process::id() as i32;
        let errno = |code| move |_: i32| Err(std::io::Error::from_raw_os_error(code));
        // A live registered thread: EMFILE and an unconfirmed ESRCH refuse.
        let live = DetTid::from_raw(41);
        let live_process = DetPid::from_raw(41);
        // SAFETY: gettid has no preconditions.
        let my_tid = unsafe { libc::gettid() };
        scheduler
            .register_physical_thread(live, MmId::initial(live_process), me, my_tid)
            .unwrap();
        let refusal = sigkill_fence_for(&scheduler, live, live_process, true, errno(libc::EMFILE))
            .unwrap_err();
        assert_eq!(refusal.process, live_process);
        assert!(refusal.reason.contains("pidfd_open"), "{}", refusal.reason);
        assert!(
            sigkill_fence_for(&scheduler, live, live_process, true, errno(libc::ESRCH)).is_err()
        );
        assert!(
            sigkill_fence_for(&scheduler, live, live_process, true, errno(libc::EINVAL)).is_err()
        );
        assert!(
            sigkill_fence_for(
                &scheduler,
                live,
                live_process,
                true,
                super::open_process_pidfd
            )
            .unwrap()
            .is_some()
        );
        // A registered thread that has exited: an ESRCH means the victim is gone.
        let gone = DetTid::from_raw(43);
        let gone_process = DetPid::from_raw(43);
        let (send, receive) = std::sync::mpsc::channel();
        let (stop, stopped) = std::sync::mpsc::channel::<()>();
        let thread = std::thread::spawn(move || {
            // SAFETY: as above.
            send.send(unsafe { libc::gettid() }).unwrap();
            let _ = stopped.recv();
        });
        let gone_tid = receive.recv().unwrap();
        scheduler
            .register_physical_thread(gone, MmId::initial(gone_process), me, gone_tid)
            .unwrap();
        stop.send(()).unwrap();
        thread.join().unwrap();
        // pthread_join returns at the CLONE_CHILD_CLEARTID wake, before the
        // kernel marks the thread exited, so its pidfd can turn readable a
        // little later (Claude review of
        // https://github.com/rrnewton/hermit/pull/4423, F1).
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        while scheduler.registered_thread_has_exited(gone) != Some(true) {
            assert!(
                std::time::Instant::now() < deadline,
                "the joined thread's pidfd never reported its exit"
            );
            std::thread::sleep(std::time::Duration::from_millis(1));
        }
        assert!(
            sigkill_fence_for(&scheduler, gone, gone_process, true, errno(libc::EINVAL))
                .unwrap()
                .is_none()
        );
        assert!(
            sigkill_fence_for(&scheduler, gone, gone_process, true, errno(libc::ESRCH))
                .unwrap()
                .is_none()
        );
        assert!(
            sigkill_fence_for(&scheduler, gone, gone_process, true, errno(libc::EMFILE)).is_err()
        );
        // A named thread with no registration refuses, even when the victim
        // seems to have no live thread left: that can be its threads' exits
        // racing this notice after the kill reached the kernel (Claude
        // re-check of https://github.com/rrnewton/hermit/pull/4423, N1).
        let unregistered = DetTid::from_raw(47);
        let empty = DetPid::from_raw(47);
        let refusal = sigkill_fence_for(&scheduler, unregistered, empty, true, errno(libc::EMFILE))
            .unwrap_err();
        assert!(
            refusal.reason.contains("has a registered host process"),
            "{}",
            refusal.reason
        );
        // A zombie whose exit the schedule granted needs no fence
        // (https://github.com/rrnewton/hermit/issues/4411).
        scheduler.grant_exit_for_test(empty);
        assert!(
            sigkill_fence_for(&scheduler, unregistered, empty, true, errno(libc::EMFILE))
                .unwrap()
                .is_none()
        );
        // A named leader that has exited while a sibling runs on: the fence
        // goes through the sibling's registration, and an ESRCH counts as gone
        // only once that sibling has exited
        // (https://github.com/rrnewton/hermit/issues/4411).
        let leader = DetTid::from_raw(51);
        let process = DetPid::from_raw(51);
        let sibling = DetTid::from_raw(52);
        scheduler.thread_tree.add_child(leader, leader, true);
        scheduler.thread_tree.add_child(leader, sibling, false);
        // SAFETY: as above.
        let my_tid = unsafe { libc::gettid() };
        scheduler
            .register_physical_thread(sibling, MmId::initial(process), me, my_tid)
            .unwrap();
        assert!(
            sigkill_fence_for(&scheduler, leader, process, true, super::open_process_pidfd)
                .unwrap()
                .is_some()
        );
        assert!(sigkill_fence_for(&scheduler, leader, process, true, errno(libc::ESRCH)).is_err());
        // A thread-directed SIGKILL (tgkill, tkill, rt_tgsigqueueinfo) to the
        // exited leader gets no fallback and refuses; Linux does not end the
        // group for it (Claude review of
        // https://github.com/rrnewton/hermit/pull/4439, R1). Nor does a
        // thread-directed kill to a zombie skip the fence.
        assert!(
            sigkill_fence_for(
                &scheduler,
                leader,
                process,
                false,
                super::open_process_pidfd
            )
            .is_err()
        );
        assert!(
            sigkill_fence_for(&scheduler, unregistered, empty, false, errno(libc::EMFILE)).is_err()
        );
        // An exited leader whose surviving sibling has since exited too: an
        // ESRCH counts as gone, confirmed through the sibling's own
        // registration pidfd, not the leader's (Claude review of 4439, R2).
        let late_leader = DetTid::from_raw(61);
        let late_process = DetPid::from_raw(61);
        let late_sibling = DetTid::from_raw(62);
        scheduler
            .thread_tree
            .add_child(late_leader, late_leader, true);
        scheduler
            .thread_tree
            .add_child(late_leader, late_sibling, false);
        let (send, receive) = std::sync::mpsc::channel();
        let (stop, stopped) = std::sync::mpsc::channel::<()>();
        let thread = std::thread::spawn(move || {
            // SAFETY: as above.
            send.send(unsafe { libc::gettid() }).unwrap();
            let _ = stopped.recv();
        });
        let sibling_tid = receive.recv().unwrap();
        scheduler
            .register_physical_thread(late_sibling, MmId::initial(late_process), me, sibling_tid)
            .unwrap();
        stop.send(()).unwrap();
        thread.join().unwrap();
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        while scheduler.registered_thread_has_exited(late_sibling) != Some(true) {
            assert!(
                std::time::Instant::now() < deadline,
                "the joined thread's pidfd never reported its exit"
            );
            std::thread::sleep(std::time::Duration::from_millis(1));
        }
        assert!(
            sigkill_fence_for(
                &scheduler,
                late_leader,
                late_process,
                true,
                errno(libc::ESRCH)
            )
            .unwrap()
            .is_none()
        );
    }

    /// DBT publishes READY_IMAGE between creating the scheduler future and
    /// polling it, so the announcement must already be logged when
    /// `run_external_scheduler` returns:
    /// <https://github.com/rrnewton/hermit/issues/3463>.
    #[test]
    fn external_scheduler_announces_before_its_future_is_polled() {
        #[derive(Clone, Default)]
        struct Capture(std::sync::Arc<Mutex<Vec<String>>>);
        struct Visitor(String);
        impl tracing::field::Visit for Visitor {
            fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
                if field.name() == "message" {
                    use std::fmt::Write;
                    write!(self.0, "{value:?}").unwrap();
                }
            }
        }
        impl tracing::Subscriber for Capture {
            fn enabled(&self, metadata: &tracing::Metadata<'_>) -> bool {
                *metadata.level() == tracing::Level::INFO
            }
            fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
                tracing::span::Id::from_u64(1)
            }
            fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}
            fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}
            fn event(&self, event: &tracing::Event<'_>) {
                let mut visitor = Visitor(String::new());
                event.record(&mut visitor);
                self.0.lock().unwrap().push(visitor.0);
            }
            fn enter(&self, _: &tracing::span::Id) {}
            fn exit(&self, _: &tracing::span::Id) {}
        }
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let state = GlobalState::init_for_external_scheduler(&config);
        let captured = Capture::default();
        let scheduler = tracing::subscriber::with_default(captured.clone(), || {
            state.run_external_scheduler(std::sync::Arc::new(|_| {}))
        });
        let announcement = "[scheduler] daemon task starting up, waiting for guest thread start..";
        assert_eq!(*captured.0.lock().unwrap(), [announcement]);
        drop(scheduler);
    }

    #[test]
    fn schedule_event_host_markers_require_command_bootstrap_provenance() {
        let event = SchedEvent::branches(DetTid::from_raw(3), 223)
            .with_end_rip(std::num::NonZeroUsize::new(0x1234).unwrap())
            .with_time(LogicalTime::from_nanos(2230));
        let original = serde_json::to_string(&event).unwrap();
        let plain = format!(
            "{:?}",
            super::SchedEventForLog {
                event: &event,
                command_bootstrap: false
            }
        );
        assert_eq!(plain, format!("{event:?}"));
        let marked = format!(
            "{:?}",
            super::SchedEventForLog {
                event: &event,
                command_bootstrap: true
            }
        );
        assert_eq!(
            marked,
            "SchedEvent { dettid: DetPid(3), op: Branch, count: 223, start_rip: None, end_rip: Some(<hostaddr 0x1234>), end_time: Some(LogicalTime(2230)) }"
        );
        assert_eq!(serde_json::to_string(&event).unwrap(), original);
    }

    #[test]
    fn summary_preemption_views_keep_counts_full_report_and_single_flush() {
        let (_config, state, tid, _) = cancellation_test_state();
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("recording");
        let mut writer = crate::preemptions::PreemptionWriter::new(Some(path.clone()));
        writer.register_thread(tid, DEFAULT_PRIORITY);
        writer.insert_reprioritization(tid, LogicalTime::from_nanos(100), 10, DEFAULT_PRIORITY, 20);
        let mut scheduler = state.sched.lock().unwrap();
        scheduler.preemption_writer = Some(writer);
        let (summary, info_description) = scheduler
            .generate_partial_run_summary_for_log(Some(&path))
            .unwrap();
        assert!(scheduler.preemption_writer.is_none());
        let recorded = std::fs::read(&path).unwrap();
        let parsed: serde_json::Value = serde_json::from_slice(&recorded).unwrap();
        assert_eq!(
            parsed["per_thread"][tid.to_string()]["prio_changes"],
            serde_json::json!([[100, DEFAULT_PRIORITY]])
        );
        let count_line = "Record of 1 preemption and reprioritization events:\n";
        assert_eq!(info_description.as_deref(), Some(count_line));
        assert_eq!(
            summary.reprio_descrip.as_deref(),
            Some(format!("{count_line}  (Writing to file {path:?})\n").as_str())
        );
        let full = summary.to_string();
        let json = serde_json::to_vec(&summary).unwrap();
        let info = summary.info(info_description.as_deref()).to_string();
        assert!(info.contains(count_line));
        assert!(!info.contains("Writing to file"));
        assert!(full.contains(&format!("Writing to file {path:?}")));
        // Formatting and another empty summary do not flush the consumed writer again.
        let _ = scheduler.generate_partial_run_summary(None).unwrap();
        assert_eq!(std::fs::read(&path).unwrap(), recorded);
        assert_eq!(summary.to_string(), full);
        assert_eq!(serde_json::to_vec(&summary).unwrap(), json);
    }

    #[test]
    fn run_summary_records_the_effective_shared_preemption_configuration() {
        // Config::default() is unprepared: launch installs the timer capability.
        // Specify that mechanism fact here independently of the maximum.
        for (config, expected_maximum, expected_timer) in [
            (
                Config::default().with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                }),
                Some(200_000_000),
                true,
            ),
            (
                Config {
                    max_timeslice: None,
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                }),
                None,
                false,
            ),
            (
                Config {
                    max_timeslice: std::num::NonZeroU64::new(12_340),
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                }),
                Some(12_340),
                true,
            ),
            (
                Config::default().with_backend(|backend| {
                    backend.delivers_preemption_timer_events = false;
                }),
                Some(200_000_000),
                false,
            ),
            (
                Config {
                    max_timeslice: None,
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = false;
                }),
                None,
                false,
            ),
        ] {
            let summary = GlobalState::for_tests(&config).into_run_summary().unwrap();
            assert_eq!(
                summary.preemption,
                Some(super::PreemptionSummary {
                    max_timeslice_ns: expected_maximum,
                    timer_enabled: Some(expected_timer),
                })
            );
        }
    }

    #[test]
    fn run_summary_info_keeps_semantics_and_debug_retains_bookkeeping() {
        #[derive(Clone)]
        struct Capture(std::sync::Arc<Mutex<Vec<(tracing::Level, String)>>>);
        struct Visitor(String);
        impl tracing::field::Visit for Visitor {
            fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
                use std::fmt::Write;
                write!(self.0, "{}={value:?};", field.name()).unwrap();
            }
        }
        impl tracing::Subscriber for Capture {
            fn enabled(&self, _: &tracing::Metadata<'_>) -> bool {
                true
            }
            fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
                tracing::span::Id::from_u64(1)
            }
            fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}
            fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}
            fn event(&self, event: &tracing::Event<'_>) {
                let mut visitor = Visitor(String::new());
                event.record(&mut visitor);
                self.0
                    .lock()
                    .unwrap()
                    .push((*event.metadata().level(), visitor.0));
            }
            fn enter(&self, _: &tracing::span::Id) {}
            fn exit(&self, _: &tracing::span::Id) {}
        }
        let summary = super::RunSummary {
            sched_turns: 4,
            schedevent_recorded: 11,
            schedevent_replayed: 11,
            schedevent_desynced: 2,
            desync_descrip: Some("two real desyncs\n".into()),
            num_processes: 1,
            num_threads: 1,
            threads_descrip: "[3]".into(),
            syscalls: Some(3),
            virttime_elapsed: 2_512_380,
            virttime_final: 2_512_380,
            timeslice_stats: TimesliceStats {
                count: 1,
                sum_ns: 512_380,
                min_ns: 512_380,
                max_ns: 512_380,
            },
            ..Default::default()
        };
        let description = "Record of 7 preemption and reprioritization events:\n";
        let captured = Capture(Default::default());
        tracing::subscriber::with_default(captured.clone(), || {
            super::log_run_summary(
                "report",
                &summary,
                Some(description),
                Some(std::path::Path::new("/host/recording")),
            );
        });
        let events = captured.0.lock().unwrap();
        assert_eq!(events.len(), 2);
        assert_eq!(events[0].0, tracing::Level::INFO);
        assert_eq!(events[1].0, tracing::Level::DEBUG);
        let info = &events[0].1;
        for text in [
            "1 group leaders of 1 thread(s)",
            "3 syscalls",
            "4 turns, recorded 11 events (2 desynced)",
            "two real desyncs",
            "Record of 7 preemption",
            "2_512_380ns",
            "min=512380ns max=512380ns mean=512380ns count=1",
        ] {
            assert!(info.contains(text), "missing semantic field {text}: {info}");
        }
        assert!(!info.contains("/host/recording"));
        assert!(!info.contains("replayed"));
        assert!(events[1].1.contains("replayed_events=11"));
        assert!(events[1].1.contains("/host/recording"));
        let baseline = summary.info(Some(description)).to_string();
        let mutations: [fn(&mut super::RunSummary); 8] = [
            |s| s.sched_turns += 1,
            |s| s.schedevent_recorded += 1,
            |s| s.schedevent_desynced += 1,
            |s| s.num_threads += 1,
            |s| s.syscalls = Some(4),
            |s| s.virttime_elapsed += 1,
            |s| s.timeslice_stats.count += 1,
            |s| s.threads_descrip.push_str(",4"),
        ];
        for mutate in mutations {
            let mut changed = summary.clone();
            mutate(&mut changed);
            assert_ne!(changed.info(Some(description)).to_string(), baseline);
        }
        assert_ne!(
            summary
                .info(Some(
                    "Record of 8 preemption and reprioritization events:\n"
                ))
                .to_string(),
            baseline
        );
    }

    mod backend_failure_tests;
    mod exec_identity_tests;
    mod exec_teardown_tests;
    mod timerless_clock_tests;
    use std::collections::BTreeSet;
    use std::os::fd::AsRawFd;
    use std::os::fd::FromRawFd;
    use std::os::fd::OwnedFd;
    use std::sync::Mutex;
    use std::time::Duration;

    use nix::sys::signal::Signal;
    use reverie::GlobalRPC;
    use reverie::GlobalTool;
    use reverie::Guest;
    use reverie::Tid;
    use reverie::syscalls::CloneFlags;

    use super::FutexAction;
    use super::GlobalRequest;
    use super::GlobalResponse;
    use super::GlobalState;
    use super::MountIdPool;
    use super::PendingExecState;
    use super::ResumeStatus;
    use super::RpcIncarnation;
    use super::SchedulerRpcResult;
    use super::SigWrapper;
    use super::ThreadDeregistration;
    use super::TimesliceStats;
    use super::format_unsupported_syscall_warning;
    use crate::Detcore;
    use crate::config::Config;
    use crate::config::RunsPostFork;
    use crate::ivar::Ivar;
    use crate::preemptions::PreemptionRecord;
    use crate::resources::ExternalOpId;
    use crate::resources::Permission;
    use crate::resources::ResourceID;
    use crate::resources::Resources;
    use crate::scheduler::DEFAULT_PRIORITY;
    use crate::scheduler::SchedRequest;
    use crate::scheduler::SchedResponse;
    use crate::scheduler::SchedValue;
    use crate::scheduler::ThreadNextTurn;
    use crate::tool_local::ExecFdBlockingOverrides;
    use crate::tool_local::ExecFdState;
    use crate::types::DetPid;
    use crate::types::DetTid;
    use crate::types::DetTime;
    use crate::types::FutexID;
    use crate::types::LogicalTime;
    use crate::types::MmId;
    use crate::types::Op;
    use crate::types::SchedEvent;

    #[test]
    fn fdinfo_mount_ids_preserve_raw_equivalence_and_distinctness() {
        let mut pool = MountIdPool::from_config(&[10, 20, 30], true, &[]);
        // Numbered in first-observation order, not by row position: 20 is
        // the second captured row but the first ID the guest observes.
        assert_eq!(pool.determinize(20, None), Some(1));

        // Unlisted nsfs, anon_inodefs, and pidfs IDs are distinct even when
        // their descriptors have the same broad Detcore FdType.
        assert_eq!(pool.determinize(700, None), Some(2));
        assert_eq!(pool.determinize(701, None), Some(3));
        assert_eq!(pool.determinize(702, None), Some(4));
        assert_eq!(pool.determinize(701, None), Some(3));
        assert_eq!(pool.determinize(20, None), Some(1));
    }

    #[test]
    fn fdinfo_mount_ids_seed_from_low_level_snapshot_and_refuse_drift() {
        let mut pool = MountIdPool::from_config(&[], false, &[]);
        assert_eq!(pool.determinize(20, Some(&[10, 20, 30])), Some(1));
        assert_eq!(pool.determinize(700, Some(&[10, 20, 30])), Some(2));
        assert_eq!(pool.determinize(20, Some(&[10, 20, 30])), Some(1));
        assert_eq!(pool.determinize(20, Some(&[10, 99, 30])), None);
        assert_eq!(pool.determinize(20, Some(&[10, 20])), None);
        assert_eq!(pool.determinize(20, Some(&[10, 20, 30, 40])), None);
    }

    /// The compat/lsof divergence: a run's namespace starts as a copy of the
    /// host's, so unrelated host mounts (here 25, 26, 27, as one loop mount
    /// propagated to three peers) can change the row count between two runs
    /// of the same guest. The IDs the guest observes must not move.
    #[test]
    fn mount_ids_do_not_depend_on_rows_the_guest_never_observed() {
        let observe = |mut pool: MountIdPool, visible: &[u64]| {
            vec![
                pool.determinize(700, None),
                pool.determinize(701, None),
                pool.determinize(20, None),
            ]
            .into_iter()
            .chain(
                pool.resolve_mountinfo(visible, visible)
                    .unwrap()
                    .into_iter()
                    .map(Some),
            )
            .collect::<Vec<_>>()
        };
        let quiet = observe(
            MountIdPool::from_config(&[10, 20, 30], true, &[]),
            &[10, 30],
        );
        let churned = observe(
            MountIdPool::from_config(&[10, 20, 25, 26, 27, 30], true, &[]),
            &[10, 30],
        );
        assert_eq!(quiet, [Some(1), Some(2), Some(3), Some(4), Some(5)]);
        assert_eq!(quiet, churned);

        // Uncaptured runs validate each fdinfo read against a tracer-side
        // snapshot of the same namespace; the snapshot's size is irrelevant.
        let mut quiet = MountIdPool::from_config(&[], false, &[]);
        let mut churned = MountIdPool::from_config(&[], false, &[]);
        assert_eq!(quiet.determinize(700, Some(&[10, 20, 30])), Some(1));
        assert_eq!(
            churned.determinize(700, Some(&[10, 20, 25, 26, 27, 30])),
            Some(1)
        );
        assert_eq!(quiet.determinize(701, Some(&[10, 20, 30])), Some(2));
        assert_eq!(
            churned.determinize(701, Some(&[10, 20, 25, 26, 27, 30])),
            Some(2)
        );
    }

    #[test]
    fn mountinfo_views_keep_earlier_numbers_and_number_the_rest_in_row_order() {
        let mut pool = MountIdPool::from_config(&[10, 20, 30, 40], true, &[]);
        assert_eq!(pool.determinize(30, None), Some(1));
        assert_eq!(pool.determinize(700, None), Some(2));
        assert_eq!(
            pool.resolve_mountinfo(&[10, 20, 30], &[10, 20, 30]),
            Some(vec![3, 4, 1])
        );
        assert_eq!(
            pool.resolve_mountinfo(&[20, 40], &[20, 40]),
            Some(vec![4, 5])
        );
        // A refused view numbers nothing.
        assert_eq!(pool.resolve_mountinfo(&[40, 10], &[40, 10]), None);
        assert_eq!(pool.resolve_mountinfo(&[10, 99], &[10, 99]), None);
        assert_eq!(pool.determinize(701, None), Some(6));
        assert_eq!(pool.determinize(10, None), Some(3));
    }

    /// One chroot view: row 20 whose parent 10 is outside the view. The
    /// captured namespace may list 10 before 20 or after it, which the guest
    /// cannot see. The view's canonical order follows the capture, but the
    /// numbers must follow the view: row 20 first, then parent 10.
    #[test]
    fn captured_namespace_order_never_decides_mountinfo_numbers() {
        let mut parent_first = MountIdPool::from_config(&[10, 20, 9], true, &[]);
        let mut parent_last = MountIdPool::from_config(&[20, 10], true, &[]);
        assert_eq!(
            parent_first.resolve_mountinfo(&[10, 20], &[20, 10]),
            Some(vec![1, 2])
        );
        assert_eq!(
            parent_last.resolve_mountinfo(&[20, 10], &[20, 10]),
            Some(vec![1, 2])
        );
        assert_eq!(parent_first.determinize(10, None), Some(2));
        assert_eq!(parent_last.determinize(10, None), Some(2));
    }

    #[test]
    fn mountinfo_observation_order_must_name_exactly_the_canonical_ids() {
        for (canonical, observation) in [
            (&[10, 20][..], &[20][..]),
            (&[10, 20][..], &[20, 30][..]),
            (&[10, 20][..], &[20, 20][..]),
            (&[10, 20][..], &[10, 20, 30][..]),
        ] {
            let mut captured = MountIdPool::from_config(&[10, 20, 30], true, &[]);
            assert_eq!(captured.resolve_mountinfo(canonical, observation), None);
            assert_eq!(captured.determinize(30, None), Some(1));

            // A refused uncaptured view must not seed the namespace order.
            let mut uncaptured = MountIdPool::from_config(&[], false, &[]);
            assert_eq!(uncaptured.resolve_mountinfo(canonical, observation), None);
            assert!(matches!(uncaptured, MountIdPool::Uninitialized));
        }
    }

    #[test]
    fn mountinfo_reads_seed_once_and_refuse_later_table_changes() {
        let mut pool = MountIdPool::from_config(&[], false, &[]);
        assert!(pool.validate_mountinfo_order(&[10, 20, 30]));
        assert!(pool.validate_mountinfo_order(&[10, 20, 30]));
        assert!(!pool.validate_mountinfo_order(&[10, 99, 30]));

        let mut empty = MountIdPool::from_config(&[], false, &[]);
        assert!(empty.validate_mountinfo_order(&[]));
        assert!(!empty.validate_mountinfo_order(&[10]));
    }

    #[test]
    fn configured_mountinfo_order_accepts_only_ordered_known_subsets() {
        let mut pool = MountIdPool::from_config(&[10, 20, 30, 40], true, &[]);
        assert!(pool.validate_mountinfo_order(&[10, 30, 40]));
        assert_eq!(pool.determinize(30, None), Some(1));
        assert!(pool.validate_mountinfo_order(&[20, 40]));
        assert!(!pool.validate_mountinfo_order(&[30, 20]));
        assert!(!pool.validate_mountinfo_order(&[10, 10]));
    }

    #[test]
    fn configured_mountinfo_order_refuses_new_namespace_ids() {
        let mut pool = MountIdPool::from_config(&[10, 20, 30, 40], true, &[]);
        assert!(pool.validate_mountinfo_order(&[10, 30, 40]));
        assert!(
            !pool.validate_mountinfo_order(&[10, 99, 40]),
            "a namespace-local mount ID absent from captured provenance must fail closed"
        );
    }

    #[test]
    fn fdinfo_mount_ids_refuse_malformed_configured_provenance() {
        let mut pool = MountIdPool::from_config(&[10, 10], true, &[]);
        assert_eq!(pool.determinize(10, None), None);
        let mut pool = MountIdPool::from_config(&[10], true, &[700, 700]);
        assert_eq!(pool.determinize(10, None), None);
        // An assignment order without a captured namespace has nothing to
        // validate later views against.
        let mut pool = MountIdPool::from_config(&[], false, &[700]);
        assert_eq!(pool.determinize(700, Some(&[10])), None);
        assert!(pool.provenance().is_err());
        // No recording numbers raw 0 unless mountinfo named it: an fdinfo zero
        // is the anonymous class, and a mountinfo zero must pass validation.
        let mut pool = MountIdPool::from_config(&[10], true, &[0]);
        assert_eq!(pool.determinize(10, None), None);
        assert!(pool.provenance().is_err());
        let mut pool = MountIdPool::from_config(&[10, 0], true, &[10, 0]);
        assert_eq!(pool.resolve_mountinfo(&[10, 0], &[10, 0]), Some(vec![1, 2]));
        assert_eq!(pool.determinize(0, None), Some(0));
    }

    #[test]
    fn raw_zero_is_one_mount_identity_without_descriptor_type_partitioning() {
        let mut pool = MountIdPool::from_config(&[10, 0], true, &[]);
        // The pool API intentionally accepts no descriptor type: memfd and
        // every other Linux descriptor reporting raw mnt_id 0 share this one
        // equivalence class, even when mountinfo uses zero as an outside parent
        // ID for its root row.
        assert_eq!(pool.determinize(0, None), Some(0));
        assert_eq!(pool.determinize(20, None), Some(1));
        assert_eq!(pool.determinize(0, None), Some(0));
    }

    #[tokio::test]
    async fn completing_an_unreported_exit_retires_it_and_records_determinism_loss() {
        if !crate::scheduler::exec_teardown_tests::in_isolated_log_test(
            module_path!(),
            "completing_an_unreported_exit_retires_it_and_records_determinism_loss",
        ) {
            return;
        }
        use std::os::unix::process::ExitStatusExt;

        use super::open_process_pidfd;
        use super::wait_for_process_exit;

        struct Reap(std::process::Child);
        impl Drop for Reap {
            fn drop(&mut self) {
                let _ = self.0.kill();
                let _ = self.0.wait();
            }
        }
        let mut child = Reap(
            std::process::Command::new("sleep")
                .arg("30")
                .spawn()
                .unwrap(),
        );
        let physical_pid = child.0.id() as i32;
        let pidfd = open_process_pidfd(physical_pid).expect("a live child has a process pidfd");
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.process_exits_complete_asynchronously = true;
            backend.needs_killed_thread_rpc_cancellation = true;
        });
        let state = GlobalState::initialize(&config, false);
        let dettid = DetTid::from_raw(41);
        let mm = MmId::initial(dettid);
        assert_ne!(dettid.as_raw(), physical_pid);
        let pending_request = {
            let mut sched = state.sched.lock().unwrap();
            sched.thread_tree.add_child(dettid, dettid, true);
            sched.record_thread_mm(dettid, mm);
            sched
                .register_physical_thread(dettid, mm, physical_pid, physical_pid)
                .unwrap();
            sched.next_turns.insert(
                dettid,
                crate::scheduler::ThreadNextTurn {
                    dettid,
                    child_tid_addr: 0,
                    req: Ivar::new(),
                    resp: Ivar::new(),
                    protocol: Default::default(),
                },
            );
            sched.next_turns[&dettid].req.clone()
        };
        assert!(pending_request.is_empty());
        child.0.kill().unwrap();
        assert!(
            wait_for_process_exit(pidfd, Duration::from_secs(5))
                .await
                .is_ok()
        );

        // Physical readiness authenticates death, but cleanup takes the
        // registered logical process ID and must not reap the parent's child.
        state.complete_physical_process_exit(dettid.as_raw());
        assert!(pending_request.get().await.is_err());
        let sched = state.sched.lock().unwrap();
        assert!(!sched.next_turns.contains_key(&dettid));
        assert!(sched.process_physically_gone(DetPid::from_raw(41)));
        assert!(!sched.process_physically_gone(DetPid::from_raw(physical_pid)));
        drop(sched);
        assert_eq!(
            crate::detlog::determinism_loss().as_deref(),
            Some("process 41 exited without deregistering; its exit was not scheduled")
        );
        let status = child
            .0
            .wait()
            .expect("cleanup must leave the kernel status for the parent");
        assert_eq!(status.signal(), Some(libc::SIGKILL));
        assert_eq!(status.code(), None);
    }

    #[tokio::test]
    async fn stale_process_exit_cannot_clear_replacement_and_normal_exit_adds_no_loss() {
        if !crate::scheduler::exec_teardown_tests::in_isolated_log_test(
            module_path!(),
            "stale_process_exit_cannot_clear_replacement_and_normal_exit_adds_no_loss",
        ) {
            return;
        }
        use nix::fcntl::OFlag;

        use super::open_process_pidfd;
        use super::wait_for_process_exit;
        use crate::fd::DetFd;
        use crate::fd::FdType;
        use crate::shared_open_files::OpenFileControl;
        use crate::types::OpenFileId;

        struct Reap(std::process::Child);
        impl Drop for Reap {
            fn drop(&mut self) {
                let _ = self.0.kill();
                let _ = self.0.wait();
            }
        }
        let mut child = Reap(
            std::process::Command::new("sleep")
                .arg("30")
                .spawn()
                .unwrap(),
        );
        let physical_pid = child.0.id() as i32;
        let pidfd = open_process_pidfd(physical_pid).unwrap();
        let config = Config {
            sequentialize_threads: true,
            backend: reverie::BackendCapabilities::DBT,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let process = DetPid::from_raw(41);
        let old_mm = MmId::initial(process);
        let mm = old_mm.for_exec(process);
        let (old, current) = {
            let mut sched = state.sched.lock().unwrap();
            sched.thread_tree.add_child(process, process, true);
            sched.record_thread_mm(process, old_mm);
            sched.next_turns.insert(
                process,
                ThreadNextTurn {
                    dettid: process,
                    child_tid_addr: 0,
                    req: Ivar::new(),
                    resp: Ivar::new(),
                    protocol: Default::default(),
                },
            );
            sched
                .register_physical_thread(process, old_mm, physical_pid, physical_pid)
                .unwrap();
            let old = sched.arm_process_exit_watch(
                process,
                old_mm,
                physical_pid,
                open_process_pidfd(physical_pid).unwrap(),
            );
            sched.record_thread_mm(process, mm);
            sched
                .register_physical_thread(process, mm, physical_pid, physical_pid)
                .unwrap();
            let current = sched.arm_process_exit_watch(
                process,
                mm,
                physical_pid,
                open_process_pidfd(physical_pid).unwrap(),
            );
            (old, current)
        };
        let pending = PendingExecState {
            caller: process,
            process,
            mm,
            fd_blocking: Default::default(),
        };
        state
            .pending_exec_states
            .lock()
            .unwrap()
            .insert(process, pending.clone());
        state
            .post_exec_fd_blocking
            .lock()
            .unwrap()
            .insert(process, ExecFdState::default());
        let id = OpenFileId::new(process, 0);
        {
            let mut files = state.shared_open_files.lock().unwrap();
            files
                .insert(DetFd::new(3, OFlag::O_RDWR, FdType::Regular, id).model_for_test())
                .unwrap();
            files
                .handle(process, process, OpenFileControl::Take { id })
                .unwrap();
        }
        child.0.kill().unwrap();
        assert!(
            wait_for_process_exit(pidfd, Duration::from_secs(5))
                .await
                .is_ok()
        );
        let cleanup = state.process_exit_cleanup();
        {
            let mut sched = state.sched.lock().unwrap();
            assert_eq!(cleanup.complete_observed_locked(&mut sched, &old), None);
            assert!(sched.next_turns.contains_key(&process));
            assert_eq!(
                state.pending_exec_states.lock().unwrap().get(&process),
                Some(&pending)
            );
            assert!(
                state
                    .post_exec_fd_blocking
                    .lock()
                    .unwrap()
                    .contains_key(&process)
            );
            assert!(state.shared_open_files.lock().unwrap().is_leased(id));
            assert_eq!(crate::detlog::determinism_loss(), None);

            // Model the normal hook winning the same mutex before the
            // readiness callback. Its later physical completion is no loss.
            state.account_deregistered_thread(
                &mut sched,
                ThreadDeregistration {
                    dettid: process,
                    detpid: process,
                    mm,
                    thread_start_entered: true,
                    timeslice_stats: TimesliceStats::default(),
                    syscall_count: 0,
                    chaos_epochs: Vec::new(),
                },
            );
            assert_eq!(
                cleanup.complete_observed_locked(&mut sched, &current),
                Some((false, None))
            );
            assert_eq!(cleanup.complete_observed_locked(&mut sched, &current), None);
        }
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        assert!(state.post_exec_fd_blocking.lock().unwrap().is_empty());
        assert!(!state.shared_open_files.lock().unwrap().is_leased(id));
        assert_eq!(crate::detlog::determinism_loss(), None);
        assert!(child.0.wait().unwrap().code().is_none());
    }

    /// An admitted process can die before any registration names it. The
    /// production completion callback must then record it physically gone,
    /// without panicking on the unknown thread group, so that its late
    /// registration is refused (Codex implementation review, HIGH 1).
    #[test]
    fn completing_a_process_that_never_registered_records_it_gone() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.process_exits_complete_asynchronously = true;
            backend.needs_killed_thread_rpc_cancellation = true;
        });
        let state = GlobalState::initialize(&config, false);
        state.complete_physical_process_exit(57);
        let sched = state.sched.lock().unwrap();
        assert!(sched.process_physically_gone(DetPid::from_raw(57)));
        assert!(!sched.thread_tree.contains(&DetTid::from_raw(57)));
        assert!(!sched.next_turns.contains_key(&DetTid::from_raw(57)));
    }

    /// Two backends call `GlobalTool::on_backend_process_exited`, for two
    /// different events, and Detcore tells them apart only by capability. The
    /// ptrace tracer (PTRACE and E9PATCH declare
    /// `reports_child_exit_publication`) reports that the kernel published a
    /// leader's exit to its parent: that releases the scheduler's publication
    /// hold and must leave the physical-exit and exec state alone. In-guest
    /// LiteInst (`process_exits_complete_asynchronously`, no publication
    /// reports) reports the process's physical exit: that releases its
    /// physical-exit barrier and clears its exec state, and is not a
    /// publication. Each case starts from the state both paths could touch, so
    /// taking the other path fails it.
    #[test]
    fn backend_exit_reports_take_the_path_the_backend_capabilities_select() {
        use reverie::BackendCapabilities;
        for (name, backend) in [
            ("PTRACE", BackendCapabilities::PTRACE),
            ("E9PATCH", BackendCapabilities::E9PATCH),
            ("LITEINST_IN_GUEST", BackendCapabilities::LITEINST_IN_GUEST),
        ] {
            let publishes = backend.reports_child_exit_publication;
            assert_eq!(publishes, name != "LITEINST_IN_GUEST", "{name}");
            let config = Config {
                sequentialize_threads: true,
                backend,
                ..Config::default()
            };
            let state = GlobalState::initialize(&config, false);
            let detpid = DetPid::from_raw(43);
            let leader = DetTid::from_raw(43);
            state.pending_exec_states.lock().unwrap().insert(
                detpid,
                PendingExecState {
                    caller: leader,
                    process: detpid,
                    mm: MmId::initial(detpid),
                    fd_blocking: Default::default(),
                },
            );
            state.post_exec_fd_blocking.lock().unwrap().insert(
                leader,
                ExecFdState::from([42].into_iter().collect::<ExecFdBlockingOverrides>()),
            );
            {
                let mut sched = state.sched.lock().unwrap();
                sched.install_test_child_exit_publication_hold(detpid);
                // Only a backend whose exits complete asynchronously (or that
                // reports physical exits) installs a physical-exit barrier.
                assert_eq!(
                    sched.begin_physical_process_exit(detpid),
                    !publishes,
                    "{name}"
                );
            }

            <GlobalState as GlobalTool>::on_backend_process_exited(&state, detpid.as_raw());

            let sched = state.sched.lock().unwrap();
            let exec_state_kept = state
                .pending_exec_states
                .lock()
                .unwrap()
                .contains_key(&detpid);
            let fd_blocking_kept = state
                .post_exec_fd_blocking
                .lock()
                .unwrap()
                .contains_key(&leader);
            if publishes {
                // The hold is released, not recorded as an early report.
                assert_eq!(
                    sched.test_child_exit_publication_state(detpid),
                    (false, false),
                    "{name}"
                );
                assert!(!sched.process_physically_gone(detpid), "{name}");
                assert!(exec_state_kept, "{name}");
                assert!(fd_blocking_kept, "{name}");
            } else {
                assert!(sched.pending_async_process_exits().is_empty(), "{name}");
                assert!(sched.process_physically_gone(detpid), "{name}");
                assert!(!exec_state_kept, "{name}");
                assert!(!fd_blocking_kept, "{name}");
                // Not a publication: the hold is neither released nor recorded.
                assert_eq!(
                    sched.test_child_exit_publication_state(detpid),
                    (true, false),
                    "{name}"
                );
            }
        }
    }

    #[test]
    fn recorded_assignment_order_rebuilds_the_same_mapping_in_any_replay_order() {
        let mut recording = MountIdPool::from_config(&[], false, &[]);
        assert_eq!(recording.determinize(700, Some(&[10, 20])), Some(1));
        assert_eq!(
            recording.resolve_mountinfo(&[10, 20], &[10, 20]),
            Some(vec![2, 3])
        );
        assert_eq!(recording.determinize(701, None), Some(4));
        let provenance = recording.provenance().unwrap().unwrap();
        assert_eq!(provenance.mountinfo_order, [10, 20]);
        assert_eq!(provenance.assignment_order, [700, 10, 20, 701]);

        // Replay observes the same raw IDs in a different order and still
        // gets every recorded number.
        let mut replay = MountIdPool::from_config(
            &provenance.mountinfo_order,
            true,
            &provenance.assignment_order,
        );
        assert_eq!(replay.determinize(701, None), Some(4));
        assert_eq!(replay.resolve_mountinfo(&[20], &[20]), Some(vec![3]));
        assert_eq!(replay.determinize(10, None), Some(2));
        assert_eq!(replay.determinize(700, None), Some(1));
        assert_eq!(
            replay.provenance().unwrap().unwrap().assignment_order,
            provenance.assignment_order
        );
    }

    /// The global tool's `/proc/<pid>/auxv` rows, driven through its RPC
    /// handlers and the backend's exit reports as a ptrace run drives them:
    /// a forked child (and a CLONE_VM child, sharing the creator's memory
    /// map) inherit the creator's vector; an exec whose vector could not be
    /// recorded drops the child's stale one (`AuxvFromKernel`), and the run
    /// warns only of the first; the exit reports a ptrace backend sends
    /// (child-exit publication, which bypasses the physical-exit cleanup)
    /// leave no row behind, the last process of the shared map included.
    #[tokio::test]
    async fn auxv_rows_follow_fork_failed_exec_and_exit_through_the_handlers() {
        use super::AuxvFromKernel;
        use super::ChildRegistration;

        let config = Config {
            sequentialize_threads: false,
            ..Config::default()
        }
        .with_backend(|backend| backend.reports_child_exit_publication = true);
        let state = GlobalState::initialize(&config, false);
        let root = DetTid::from_raw(3);
        let (forked, shared) = (DetTid::from_raw(4), DetTid::from_raw(5));
        let root_mm = MmId::initial(root);
        assert!(matches!(
            state
                .recv_create_child_thread(
                    Tid::from_raw(root.as_raw()),
                    root_mm,
                    ChildRegistration {
                        parent_dettid: root,
                        parent_detpid: root,
                        child_dettid: root,
                        child_tid_addr: 0,
                        flags: None,
                        exit_signal: libc::SIGCHLD,
                        physical_ids: None,
                        maybe_priority: Some(DEFAULT_PRIORITY),
                        parent_is_kernel_blocked: false,
                    },
                )
                .await,
            SchedulerRpcResult::Continue(())
        ));
        let rpc = |task: DetTid, mm: MmId, request: GlobalRequest| {
            state.receive_rpc(
                Tid::from_raw(task.as_raw()),
                (DetTime::new(&config), mm, request),
            )
        };
        let response = rpc(root, root_mm, GlobalRequest::RecordProcessAuxv(vec![7; 32]))
            .await
            .1;
        assert_eq!(response, GlobalResponse::RecordProcessAuxv(()));
        for (task, flags) in [
            (forked, CloneFlags::empty()),
            (shared, CloneFlags::CLONE_VM),
        ] {
            assert!(matches!(
                state
                    .recv_create_child_thread(
                        Tid::from_raw(root.as_raw()),
                        root_mm,
                        ChildRegistration {
                            parent_dettid: root,
                            parent_detpid: root,
                            child_dettid: task,
                            child_tid_addr: 0,
                            flags: Some(flags),
                            exit_signal: libc::SIGCHLD,
                            physical_ids: None,
                            maybe_priority: Some(DEFAULT_PRIORITY),
                            parent_is_kernel_blocked: false,
                        },
                    )
                    .await,
                SchedulerRpcResult::Continue(())
            ));
        }
        let current = |task: DetTid| {
            rpc(
                root,
                root_mm,
                GlobalRequest::ProcessAuxv(DetPid::from_raw(task.as_raw())),
            )
        };
        for task in [root, forked, shared] {
            let GlobalResponse::ProcessAuxv(Some(entry)) = current(task).await.1 else {
                panic!("{task} has no vector");
            };
            assert_eq!(entry.bytes, vec![7; 32], "{task}");
        }
        assert_eq!(state.process_auxv.lock().unwrap().len(), 3);
        // The forked child's exec finds no plausible vector: its creator's is
        // stale and goes; only the first such exec of the run warns.
        let forked_mm = MmId::initial(forked);
        for first in [true, false] {
            let response = rpc(
                forked,
                forked_mm,
                GlobalRequest::AuxvFromKernel(AuxvFromKernel::Implausible),
            )
            .await
            .1;
            assert_eq!(response, GlobalResponse::AuxvFromKernel(first));
        }
        assert_eq!(current(forked).await.1, GlobalResponse::ProcessAuxv(None));
        assert_eq!(state.process_auxv.lock().unwrap().len(), 2);
        // The exits as a ptrace backend reports them.
        for task in [forked, shared, root] {
            state.on_backend_process_exited(task.as_raw());
        }
        assert_eq!(state.process_auxv.lock().unwrap().len(), 0);
    }

    #[tokio::test]
    async fn inherited_input_belongs_only_to_the_registered_root_initial_image() {
        check_inherited_input_root_ownership(super::ROOT_DETPID, DetTid::from_raw(4)).await;
    }

    #[tokio::test]
    async fn inherited_input_uses_registered_root_identity_instead_of_pid_three() {
        // A descendant with the old numeric root ID must not acquire the lease.
        check_inherited_input_root_ownership(DetTid::from_raw(37), DetTid::from_raw(3)).await;
    }

    async fn check_inherited_input_root_ownership(root: DetTid, child: DetTid) {
        use super::ChildRegistration;

        let config = Config {
            sequentialize_threads: false,
            ..Config::default()
        }
        .with_backend(|backend| backend.tool_shares_guest_descriptor_table = true);
        let mut state = GlobalState::initialize(&config, false);
        let pipe = reverie::InheritedInput::pipe(0).unwrap();
        state.install_inherited_input(pipe.lease().unwrap());
        let worker = DetTid::from_raw(root.as_raw() + 2);
        let root_mm = MmId::initial(root);
        for (parent, task, flags) in [
            (root, root, None),
            (root, child, Some(CloneFlags::empty())),
            (
                root,
                worker,
                Some(CloneFlags::CLONE_VM | CloneFlags::CLONE_SIGHAND | CloneFlags::CLONE_THREAD),
            ),
        ] {
            assert!(matches!(
                state
                    .recv_create_child_thread(
                        Tid::from_raw(parent.as_raw()),
                        root_mm,
                        ChildRegistration {
                            parent_dettid: parent,
                            parent_detpid: root,
                            child_dettid: task,
                            child_tid_addr: 0,
                            flags,
                            exit_signal: libc::SIGCHLD,
                            physical_ids: None,
                            maybe_priority: Some(DEFAULT_PRIORITY),
                            parent_is_kernel_blocked: false,
                        },
                    )
                    .await,
                SchedulerRpcResult::Continue(())
            ));
        }
        assert_eq!(
            state.inherited_input.lock().unwrap().as_ref().unwrap().root,
            Some((root, root_mm))
        );

        // Neither a descendant nor a sibling sharing the root's exact MmId
        // may consume the seed before the root's own completion.
        for (task, detpid, mm) in [
            (child, child, MmId::initial(child)),
            (worker, root, root_mm),
        ] {
            let response = state
                .receive_rpc(
                    Tid::from_raw(task.as_raw()),
                    (
                        DetTime::new(&config),
                        mm,
                        GlobalRequest::MarkPastFirstExecve(detpid, None, None, None),
                    ),
                )
                .await
                .1;
            let GlobalResponse::MarkPastFirstExecve(non_root_state) = response else {
                panic!("{response:?}")
            };
            assert!(non_root_state.initial_input.is_none());
            assert!(state.inherited_input.lock().unwrap().is_some());
        }

        // Nor can a later root image, even with the correct descriptor slot.
        let response = state
            .receive_rpc(
                Tid::from_raw(root.as_raw()),
                (
                    DetTime::new(&config),
                    root_mm.for_exec(root),
                    GlobalRequest::MarkPastFirstExecve(root, None, None, None),
                ),
            )
            .await
            .1;
        // An initial registration has no retired-exec incarnation entry, so
        // the existing RPC layer returns an empty completion here. The launch
        // lease's stricter root/image binding must still refuse the seed.
        assert_eq!(
            response,
            GlobalResponse::MarkPastFirstExecve(Default::default())
        );
        assert!(state.inherited_input.lock().unwrap().is_some());

        let response = state
            .receive_rpc(
                Tid::from_raw(root.as_raw()),
                (
                    DetTime::new(&config),
                    root_mm,
                    GlobalRequest::MarkPastFirstExecve(root, None, None, None),
                ),
            )
            .await
            .1;
        let GlobalResponse::MarkPastFirstExecve(root_state) = response else {
            panic!("{response:?}")
        };
        assert_eq!(root_state.initial_input.as_ref(), Some(pipe.identity()));
        assert!(state.inherited_input.lock().unwrap().is_none());
        let response = state
            .receive_rpc(
                Tid::from_raw(root.as_raw()),
                (
                    DetTime::new(&config),
                    root_mm,
                    GlobalRequest::MarkPastFirstExecve(root, None, None, None),
                ),
            )
            .await
            .1;
        let GlobalResponse::MarkPastFirstExecve(repeated) = response else {
            panic!("{response:?}")
        };
        assert!(repeated.initial_input.is_none());

        // An exec preparation is a return of existing OFD state, not an
        // installation channel for copied launch identities.
        let response = state
            .receive_rpc(
                Tid::from_raw(root.as_raw()),
                (
                    DetTime::new(&config),
                    root_mm,
                    GlobalRequest::PrepareExec(root, root_mm, root_state),
                ),
            )
            .await
            .1;
        assert_eq!(response, GlobalResponse::ThreadExited);
    }

    #[tokio::test]
    async fn failed_root_registration_does_not_bind_inherited_input() {
        for raw_root in [3, 37] {
            let config = Config {
                sequentialize_threads: false,
                ..Config::default()
            }
            .with_backend(|backend| backend.tool_shares_guest_descriptor_table = true);
            let mut state = GlobalState::initialize(&config, false);
            let pipe = reverie::InheritedInput::pipe(0).unwrap();
            state.install_inherited_input(pipe.lease().unwrap());
            let root = DetTid::from_raw(raw_root);
            let root_mm = MmId::initial(root);
            let response = state
                .recv_create_child_thread(
                    Tid::from_raw(raw_root),
                    root_mm,
                    super::ChildRegistration {
                        parent_dettid: root,
                        parent_detpid: root,
                        child_dettid: root,
                        child_tid_addr: 0,
                        flags: None,
                        exit_signal: libc::SIGCHLD,
                        // The real physical-registration boundary rejects
                        // nonpositive IDs before opening any kernel pidfd.
                        physical_ids: Some((-1, -1)),
                        maybe_priority: Some(DEFAULT_PRIORITY),
                        parent_is_kernel_blocked: false,
                    },
                )
                .await;
            assert!(matches!(response, SchedulerRpcResult::ThreadExited));
            let input = state.inherited_input.lock().unwrap();
            let input = input
                .as_ref()
                .expect("failed registration consumed the lease");
            assert!(input.root.is_none(), "failed registration bound the root");
            assert_eq!(input.lease.identity(), pipe.identity());
        }
    }

    fn cancellation_test_state() -> (Config, GlobalState, DetTid, DetPid) {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.needs_killed_thread_rpc_cancellation = true;
        });
        let state = GlobalState::initialize(&config, false);
        let dettid = DetTid::from_raw(17);
        let detpid = DetPid::from_raw(17);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(dettid, dettid, true);
        // The root inherits its process group from outside the namespace; this
        // test's root leads its own, as after setpgid(0, 0).
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .set_process_group(detpid, detpid);
        (config, state, dettid, detpid)
    }

    fn install_test_registration(state: &GlobalState, dettid: DetTid, request: Ivar<SchedRequest>) {
        let mut scheduler = state.sched.lock().unwrap();
        assert!(!scheduler.thread_is_logically_killed(dettid));
        scheduler.next_turns.insert(
            dettid,
            ThreadNextTurn {
                dettid,
                child_tid_addr: 0,
                req: request,
                resp: Ivar::new(),
                protocol: Default::default(),
            },
        );
        scheduler.priorities.insert(dettid, DEFAULT_PRIORITY);
        scheduler.runqueue_push_back(dettid);
    }

    // Exercise external registration and post-exec through the real global RPC
    // without a backend or guest process. Unexpected guest operations fail.
    struct ExternalRegistrationGuest<'a> {
        global: &'a GlobalState,
        config: &'a Config,
        thread: crate::ThreadState<()>,
        requests: Mutex<Vec<GlobalRequest>>,
        post_exec: bool,
        ppid: Option<reverie::Pid>,
        private_memory: Option<reverie::RuntimePrivateMemory>,
    }

    struct ExternalRegistrationStack;
    struct ExternalRegistrationStackGuard;

    impl Drop for ExternalRegistrationStackGuard {
        fn drop(&mut self) {}
    }

    impl reverie::Stack for ExternalRegistrationStack {
        type StackGuard = ExternalRegistrationStackGuard;

        fn size(&self) -> usize {
            panic!("external registration must not use a guest stack")
        }

        fn capacity(&self) -> usize {
            panic!("external registration must not use a guest stack")
        }

        fn push<'stack, T>(&mut self, _value: T) -> reverie::syscalls::Addr<'stack, T> {
            panic!("external registration must not use a guest stack")
        }

        fn reserve<'stack, T>(&mut self) -> reverie::syscalls::AddrMut<'stack, T> {
            panic!("external registration must not use a guest stack")
        }

        fn commit(self) -> Result<Self::StackGuard, reverie::syscalls::Errno> {
            panic!("external registration must not use a guest stack")
        }
    }

    #[reverie::tool]
    impl GlobalRPC<GlobalState> for ExternalRegistrationGuest<'_> {
        async fn send_rpc(
            &self,
            message: <GlobalState as GlobalTool>::Request,
        ) -> <GlobalState as GlobalTool>::Response {
            self.requests.lock().unwrap().push(message.2.clone());
            self.global
                .receive_rpc(Tid::from_raw(self.thread.dettid.as_raw()), message)
                .await
        }

        fn config(&self) -> &Config {
            self.config
        }
    }

    #[reverie::tool]
    impl Guest<Detcore> for ExternalRegistrationGuest<'_> {
        type Memory = reverie::syscalls::LocalMemory;
        type Stack = ExternalRegistrationStack;

        fn tid(&self) -> reverie::Pid {
            reverie::Pid::from_raw(self.thread.dettid.as_raw())
        }

        fn pid(&self) -> reverie::Pid {
            reverie::Pid::from_raw(self.thread.detpid.unwrap().as_raw())
        }

        fn ppid(&self) -> Option<reverie::Pid> {
            self.ppid
        }

        fn runtime_private_memory_regions(&self) -> Option<reverie::RuntimePrivateMemory> {
            self.private_memory.clone()
        }

        fn auxv(&self) -> reverie::Auxv {
            assert!(self.post_exec, "external registration must not read auxv");
            reverie::Auxv::from_entries([])
        }

        fn memory(&self) -> Self::Memory {
            panic!("external registration must not access guest memory")
        }

        fn thread_state_mut(&mut self) -> &mut crate::ThreadState<()> {
            &mut self.thread
        }

        fn thread_state(&self) -> &crate::ThreadState<()> {
            &self.thread
        }

        async fn regs(&mut self) -> libc::user_regs_struct {
            assert!(
                self.post_exec,
                "external registration must not read guest registers"
            );
            // SAFETY: every field is an integer register word. Post-exec hooks
            // may log these synthetic registers but never execute them.
            unsafe { std::mem::zeroed() }
        }

        async fn stack(&mut self) -> Self::Stack {
            panic!("external registration must not use a guest stack")
        }

        async fn daemonize(&mut self) {
            panic!("external registration must not daemonize")
        }

        async fn inject<S: reverie::syscalls::SyscallInfo>(
            &mut self,
            _syscall: S,
        ) -> Result<i64, reverie::syscalls::Errno> {
            panic!("external registration must not inject a syscall")
        }

        async fn tail_inject<S: reverie::syscalls::SyscallInfo>(
            &mut self,
            _syscall: S,
        ) -> reverie::Never {
            panic!("external registration unexpectedly retired its live parent")
        }

        fn set_timer(&mut self, _schedule: reverie::TimerSchedule) -> Result<(), reverie::Error> {
            panic!("external registration must not set a timer")
        }

        fn set_timer_precise(
            &mut self,
            _schedule: reverie::TimerSchedule,
        ) -> Result<(), reverie::Error> {
            panic!("external registration must not set a timer")
        }

        fn read_clock(&mut self) -> Result<u64, reverie::Error> {
            panic!("external registration must not read a host clock")
        }
    }

    struct RetirementGuest<'a> {
        global: &'a GlobalState,
        config: &'a Config,
        thread: crate::ThreadState<()>,
        requests: Mutex<Vec<GlobalRequest>>,
        retired: std::sync::atomic::AtomicBool,
    }

    #[reverie::tool]
    impl GlobalRPC<GlobalState> for RetirementGuest<'_> {
        async fn send_rpc(
            &self,
            message: <GlobalState as GlobalTool>::Request,
        ) -> <GlobalState as GlobalTool>::Response {
            self.requests.lock().unwrap().push(message.2.clone());
            self.global
                .receive_rpc(Tid::from_raw(self.thread.dettid.as_raw()), message)
                .await
        }

        fn config(&self) -> &Config {
            self.config
        }
    }

    #[reverie::tool]
    impl Guest<Detcore> for RetirementGuest<'_> {
        type Memory = reverie::syscalls::LocalMemory;
        type Stack = ExternalRegistrationStack;

        fn tid(&self) -> reverie::Pid {
            reverie::Pid::from_raw(self.thread.dettid.as_raw())
        }

        fn pid(&self) -> reverie::Pid {
            reverie::Pid::from_raw(self.thread.detpid.unwrap().as_raw())
        }

        fn ppid(&self) -> Option<reverie::Pid> {
            None
        }

        fn memory(&self) -> Self::Memory {
            panic!("external registration must not access guest memory")
        }

        fn thread_state_mut(&mut self) -> &mut crate::ThreadState<()> {
            &mut self.thread
        }

        fn thread_state(&self) -> &crate::ThreadState<()> {
            &self.thread
        }

        async fn regs(&mut self) -> libc::user_regs_struct {
            panic!("external registration must not read guest registers")
        }

        async fn stack(&mut self) -> Self::Stack {
            panic!("external registration must not use a guest stack")
        }

        async fn daemonize(&mut self) {
            panic!("external registration must not daemonize")
        }

        async fn inject<S: reverie::syscalls::SyscallInfo>(
            &mut self,
            _syscall: S,
        ) -> Result<i64, reverie::syscalls::Errno> {
            panic!("external registration must not inject a syscall")
        }

        async fn retire_current_thread(&mut self) -> reverie::Never {
            self.retired
                .store(true, std::sync::atomic::Ordering::SeqCst);
            futures::future::pending().await
        }

        async fn cancel_current_thread(&mut self) -> reverie::Never {
            panic!("scheduler retirement must not request group cancellation")
        }

        async fn tail_inject<S: reverie::syscalls::SyscallInfo>(
            &mut self,
            _syscall: S,
        ) -> reverie::Never {
            panic!("external registration unexpectedly retired its live parent")
        }

        fn set_timer(&mut self, _schedule: reverie::TimerSchedule) -> Result<(), reverie::Error> {
            panic!("external registration must not set a timer")
        }

        fn set_timer_precise(
            &mut self,
            _schedule: reverie::TimerSchedule,
        ) -> Result<(), reverie::Error> {
            panic!("external registration must not set a timer")
        }

        fn read_clock(&mut self) -> Result<u64, reverie::Error> {
            panic!("external registration must not read a host clock")
        }
    }

    /// While the caller's own process handles SIGALRM, every SIGALRM send and
    /// producer arming the handlers check is refused with EPERM before the
    /// kernel: the guest injects nothing (`RetirementGuest` panics on an
    /// injection) and sends no RPC but the ledger's control message.
    #[tokio::test]
    async fn sigalrm_sends_and_producer_armings_are_refused_before_the_kernel() {
        use reverie::Tool;
        use reverie::syscalls;

        let (config, state, tid, pid) = cancellation_test_state();
        install_test_registration(&state, tid, Ivar::new());
        {
            let mut sched = state.sched.lock().unwrap();
            sched.set_sigalrm_handled(pid, true);
            sched.set_running_for_test(tid);
        }
        let tool: Detcore = Detcore::new(Tid::from_raw(tid.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(tid.as_raw()), None);
        thread.detpid = Some(pid);
        let mut guest = RetirementGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            retired: std::sync::atomic::AtomicBool::new(false),
        };
        let (raw_pid, raw_tid) = (pid.as_raw(), tid.as_raw());
        let eperm = |result: Result<i64, reverie::Error>, what: &str| {
            assert!(
                matches!(result, Err(reverie::Error::Errno(errno)) if errno == reverie::Errno::EPERM),
                "{what}: {result:?}"
            );
        };

        let sigalrm = libc::SIGALRM;
        let tgkill = syscalls::Tgkill::new()
            .with_tgid(raw_pid)
            .with_tid(raw_tid)
            .with_sig(sigalrm);
        eperm(tool.handle_tgkill(&mut guest, tgkill).await, "tgkill");
        let tkill = syscalls::Tkill::new().with_tid(raw_tid).with_sig(sigalrm);
        eperm(tool.handle_tkill(&mut guest, tkill).await, "tkill");
        let kill = syscalls::Kill::new().with_pid(raw_pid).with_sig(sigalrm);
        eperm(tool.handle_kill(&mut guest, kill).await, "kill");
        let queue = syscalls::RtSigqueueinfo::new()
            .with_tgid(raw_pid)
            .with_sig(sigalrm);
        eperm(
            tool.handle_rt_sigqueueinfo(&mut guest, queue).await,
            "rt_sigqueueinfo",
        );
        let tgqueue = syscalls::RtTgsigqueueinfo::new()
            .with_tgid(raw_pid)
            .with_tid(raw_tid)
            .with_sig(sigalrm);
        eperm(
            tool.handle_rt_tgsigqueueinfo(&mut guest, tgqueue).await,
            "rt_tgsigqueueinfo",
        );
        let pdeathsig = syscalls::Prctl::new()
            .with_option(libc::PR_SET_PDEATHSIG)
            .with_arg2(sigalrm as libc::c_ulong);
        eperm(
            tool.handle_prctl(&mut guest, pdeathsig).await,
            "PR_SET_PDEATHSIG",
        );
        // A NULL sigevent names SIGALRM; the id pointer is never written.
        let timer = syscalls::TimerCreate::new()
            .with_clockid(syscalls::ClockId::CLOCK_MONOTONIC)
            .with_timerid(reverie::syscalls::AddrMut::from_raw(0x1000));
        eperm(
            tool.handle_timer_create(&mut guest, timer).await,
            "timer_create",
        );

        let requests = guest.requests.lock().unwrap();
        assert_eq!(requests.len(), 7);
        assert!(
            requests
                .iter()
                .all(|request| matches!(request, GlobalRequest::Sigalrm(_))),
            "{requests:?}"
        );
    }

    /// In a process that handles SIGALRM, signal phase 1's gate refuses what
    /// its table does not place, with EOPNOTSUPP, and allows what it places,
    /// with no RPC either way.
    #[tokio::test]
    async fn sigalrm_phase1_gate_refuses_unplaced_calls() {
        use reverie::Tool;
        use reverie::syscalls::Syscall;
        use reverie::syscalls::SyscallArgs;
        use reverie::syscalls::Sysno;

        let (config, state, tid, pid) = cancellation_test_state();
        install_test_registration(&state, tid, Ivar::new());
        let tool: Detcore = Detcore::new(Tid::from_raw(tid.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(tid.as_raw()), None);
        thread.detpid = Some(pid);
        thread.sigalrm_handled = true;
        let guest = RetirementGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            retired: std::sync::atomic::AtomicBool::new(false),
        };
        let raw = |sysno, a0, a1| Syscall::from_raw(sysno, SyscallArgs::new(a0, a1, 0, 0, 0, 0));

        for (sysno, a0, a1) in [
            (Sysno::rt_sigsuspend, 0x1000, 8),
            (Sysno::signalfd4, usize::MAX, 0x1000),
            (Sysno::epoll_pwait2, 3, 0),
            (Sysno::nanosleep, 0x1000, 0),
            (Sysno::fcntl, 3, libc::F_SETLKW as usize),
            (Sysno::execve, 0x1000, 0),
            // stdin, and a descriptor Detcore does not model.
            (Sysno::read, 0, 0x1000),
            (Sysno::read, 77, 0x1000),
            // stderr, which may be a socket Detcore labels Regular.
            (Sysno::close, 2, 0),
            // No ioctl is placed.
            (Sysno::ioctl, 1, libc::TCGETS as usize),
            // Buffered credentials and native process relations remain outside
            // the four scalar identity queries.
            (Sysno::getresuid, 0x1000, 0x2000),
            (Sysno::getresgid, 0x1000, 0x2000),
            (Sysno::getgroups, 1, 0x1000),
            (Sysno::getppid, 0, 0),
            (Sysno::getpgid, 0, 0),
            (Sysno::getsid, 0, 0),
        ] {
            assert_eq!(
                tool.sigalrm_phase1_gate(&guest, raw(sysno, a0, a1)),
                Some(nix::errno::Errno::EOPNOTSUPP),
                "{sysno}"
            );
        }
        for (sysno, a0, a1) in [
            (Sysno::getpid, 0, 0),
            (Sysno::write, 1, 0x1000),
            (Sysno::getuid, 0, 0),
            (Sysno::geteuid, 0, 0),
            (Sysno::getgid, 0, 0),
            (Sysno::getegid, 0, 0),
        ] {
            assert_eq!(
                tool.sigalrm_phase1_gate(&guest, raw(sysno, a0, a1)),
                None,
                "{sysno}"
            );
        }
        assert!(guest.requests.lock().unwrap().is_empty(), "no RPC");
    }

    /// While any process handles SIGALRM, a file lease is refused before the
    /// kernel: its break would make another process's open wait with no
    /// scheduler admission. Releasing a lease is not refused.
    #[tokio::test]
    async fn a_file_lease_is_refused_while_a_guest_handles_sigalrm() {
        use reverie::Tool;
        use reverie::syscalls;
        use reverie::syscalls::FcntlCmd;

        let (config, state, tid, pid) = cancellation_test_state();
        install_test_registration(&state, tid, Ivar::new());
        {
            let mut sched = state.sched.lock().unwrap();
            sched.set_sigalrm_handled(DetPid::from_raw(99), true);
            sched.set_running_for_test(tid);
        }
        let tool: Detcore = Detcore::new(Tid::from_raw(tid.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(tid.as_raw()), None);
        thread.detpid = Some(pid);
        let mut guest = RetirementGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            retired: std::sync::atomic::AtomicBool::new(false),
        };
        for lease in [libc::F_RDLCK, libc::F_WRLCK] {
            let fcntl = syscalls::Fcntl::new()
                .with_fd(3)
                .with_cmd(FcntlCmd::F_SETLEASE(lease));
            let result = tool.handle_fcntl(&mut guest, fcntl).await;
            assert!(
                matches!(result, Err(reverie::Error::Errno(errno)) if errno == reverie::Errno::EPERM),
                "{lease}: {result:?}"
            );
        }
        let requests = guest.requests.lock().unwrap();
        assert_eq!(requests.len(), 2);
        assert!(requests.iter().all(|request| matches!(
            request,
            GlobalRequest::Sigalrm(super::SigalrmControl::ArmProducer)
        )));
    }

    /// A committed SIGKILL victim's late ordinary request is never answered
    /// (https://github.com/rrnewton/hermit/issues/3994, design revision 4,
    /// R1). The two late states are a background operation's rejoin posted
    /// after the commit (`BlockedExternalContinue`, through
    /// `resource_request`, as `helpers.rs` posts it once the operation's
    /// outcome is in) and a plain request. In both, the callback stays
    /// pending: no response, no injection (`RetirementGuest` panics on one),
    /// and no `retire_current_thread`, so the backend's terminal owner
    /// consumes the real death. Its consuming cleanup is still answered.
    #[tokio::test]
    async fn a_sigkilled_threads_late_ordinary_request_stays_unanswered() {
        use std::sync::atomic::Ordering;

        use reverie::Tool;

        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let [root, sender, victim] = [1, 2, 3].map(DetPid::from_raw);
        let commit = |state: &GlobalState| {
            let mut sched = state.sched.lock().unwrap();
            let token = sched.reserve_sigkill(sender, victim, 0).unwrap().token;
            assert_eq!(sched.commit_sigkill(token, true).len(), 1);
            assert!(sched.is_unaccounted_sigkill_victim(victim));
        };
        // "filed": the rejoin is admitted and waiting for its turn when the
        // commit happens. "rejoin" and "plain" arrive after the commit.
        for late in ["filed", "rejoin", "plain"] {
            let state = GlobalState::initialize(&config, false);
            {
                let mut sched = state.sched.lock().unwrap();
                sched.thread_tree.add_child(root, root, true);
                sched.thread_tree.add_child(root, sender, true);
                sched.thread_tree.add_child(root, victim, true);
            }
            for tid in [root, sender, victim] {
                install_test_registration(&state, tid, Ivar::new());
                state
                    .sched
                    .lock()
                    .unwrap()
                    .record_thread_mm(tid, MmId::initial(tid));
            }
            if late != "filed" {
                commit(&state);
            }
            let tool: Detcore = Detcore::new(Tid::from_raw(victim.as_raw()), &config);
            let mut thread = tool.init_thread_state(Tid::from_raw(victim.as_raw()), None);
            thread.detpid = Some(victim);
            let mut guest = RetirementGuest {
                global: &state,
                config: &config,
                thread,
                requests: Mutex::new(Vec::new()),
                retired: std::sync::atomic::AtomicBool::new(false),
            };
            {
                let mut rejoin = Resources::new(victim);
                rejoin.insert(
                    ResourceID::BlockedExternalContinue(ExternalOpId::new(victim, 7)),
                    Permission::RW,
                );
                let pending = if late == "plain" {
                    let mut call = std::pin::pin!(super::send_and_update_time(
                        &mut guest,
                        GlobalRequest::GlobalTimeLowerBound
                    ));
                    futures::poll!(call.as_mut()).is_pending()
                } else {
                    let mut call = std::pin::pin!(super::resource_request(&mut guest, rejoin));
                    let mut pending = futures::poll!(call.as_mut()).is_pending();
                    if late == "filed" {
                        commit(&state);
                        for _ in 0..8 {
                            tokio::task::yield_now().await;
                            pending &= futures::poll!(call.as_mut()).is_pending();
                        }
                    }
                    pending
                };
                assert!(pending, "{late}: the callback resumed");
            }
            assert!(
                !guest.retired.load(Ordering::SeqCst),
                "{late}: retire_current_thread tail-injects exit"
            );
            assert_eq!(guest.requests.lock().unwrap().len(), 1, "{late}");
            // The consuming cleanup of the same victim is admitted and answered.
            let (_, ack) = state
                .receive_rpc(
                    Tid::from_raw(victim.as_raw()),
                    (
                        guest.thread.thread_logical_time.clone(),
                        guest.thread.mm_id,
                        GlobalRequest::RobustListWakes(Vec::new()),
                    ),
                )
                .await;
            assert_eq!(ack, GlobalResponse::RobustListWakes(Vec::new()), "{late}");
            // Requests answered before ordinary admission get no answer either:
            // a SIGALRM ledger change is refused, and a loss notice is
            // recorded but not answered.
            for request in [
                GlobalRequest::Sigalrm(super::SigalrmControl::ArmProducer),
                GlobalRequest::RecordDeterminismLoss("victim notice".to_string()),
            ] {
                let (_, response) = state
                    .receive_rpc(
                        Tid::from_raw(victim.as_raw()),
                        (
                            guest.thread.thread_logical_time.clone(),
                            guest.thread.mm_id,
                            request,
                        ),
                    )
                    .await;
                assert_eq!(response, GlobalResponse::Retired, "{late}");
            }
        }
    }

    /// A victim's request admitted before the commit whose response is
    /// granted, and observed by the handler, only after the commit reaches the
    /// handler's single response exit, which must not answer it
    /// (https://github.com/rrnewton/hermit/issues/3994, R1). Without that exit
    /// guard the granted response would resume the callback.
    #[tokio::test]
    async fn a_victims_request_granted_across_the_commit_stays_unanswered() {
        use reverie::Tool;

        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let [root, sender, victim] = [1, 2, 3].map(DetPid::from_raw);
        let state = GlobalState::initialize(&config, false);
        {
            let mut sched = state.sched.lock().unwrap();
            sched.thread_tree.add_child(root, root, true);
            sched.thread_tree.add_child(root, sender, true);
            sched.thread_tree.add_child(root, victim, true);
        }
        for tid in [root, sender, victim] {
            install_test_registration(&state, tid, Ivar::new());
            state
                .sched
                .lock()
                .unwrap()
                .record_thread_mm(tid, MmId::initial(tid));
        }
        let tool: Detcore = Detcore::new(Tid::from_raw(victim.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(victim.as_raw()), None);
        thread.detpid = Some(victim);
        let mut guest = RetirementGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            retired: std::sync::atomic::AtomicBool::new(false),
        };
        let mut rejoin = Resources::new(victim);
        rejoin.insert(
            ResourceID::BlockedExternalContinue(ExternalOpId::new(victim, 7)),
            Permission::RW,
        );
        let response = state.sched.lock().unwrap().next_turns[&victim].resp.clone();
        {
            let mut call = std::pin::pin!(super::resource_request(&mut guest, rejoin));
            // Admitted and filed: the handler waits for the grant.
            assert!(futures::poll!(call.as_mut()).is_pending());
            // The scheduler grants it, and only then does the commit retire the
            // victim, before the handler has looked at the grant.
            response.put(SchedResponse::Go(None));
            {
                let mut sched = state.sched.lock().unwrap();
                let token = sched.reserve_sigkill(sender, victim, 0).unwrap().token;
                assert_eq!(sched.commit_sigkill(token, true).len(), 1);
            }
            for _ in 0..8 {
                tokio::task::yield_now().await;
                assert!(
                    futures::poll!(call.as_mut()).is_pending(),
                    "the granted response resumed a SIGKILLed thread's callback"
                );
            }
        }
        assert!(!guest.retired.load(std::sync::atomic::Ordering::SeqCst));
    }

    /// Runs the calling test's body in a child copy of this test binary, and
    /// requires the child to exit with the policy-refusal status and print
    /// `expected`. Returns whether this is the child, which runs the body.
    /// Used where the body ends the process by refusing.
    fn in_child_expecting_refusal(name: &str, expected: &str) -> bool {
        const CHILD: &str = "HERMIT_TEST_REFUSAL_CHILD";
        let test = format!("{}::{name}", module_path!().split_once("::").unwrap().1);
        if std::env::var(CHILD).as_deref() == Ok(test.as_str()) {
            return true;
        }
        let output = std::process::Command::new(std::env::current_exe().unwrap())
            .args(["--exact", &test, "--nocapture"])
            .env(CHILD, &test)
            .output()
            .unwrap();
        let stderr = String::from_utf8_lossy(&output.stderr);
        assert_eq!(
            output.status.code(),
            Some(detcore_model::HERMIT_POLICY_REFUSAL_EXIT),
            "{test}: the child was not refused\n{stderr}"
        );
        assert!(
            stderr.contains(expected),
            "{test}: missing {expected:?}\n{stderr}"
        );
        false
    }

    /// Sends the DBT SIGKILL notice for process 3, from process 1, with a
    /// 20 ms fence valve. Process 3's thread is registered on the host as
    /// `victim`'s, when given, and otherwise only modelled.
    async fn send_dbt_sigkill_notice(victim: Option<i32>) {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        }
        .with_backend(|backend| *backend = reverie::BackendCapabilities::DBT);
        let state = GlobalState::initialize(&config, false);
        let [root, target] = [1, 3].map(DetPid::from_raw);
        {
            let mut sched = state.sched.lock().unwrap();
            sched.thread_tree.add_child(root, root, true);
            sched.thread_tree.add_child(root, target, true);
        }
        for tid in [root, target] {
            install_test_registration(&state, tid, Ivar::new());
            state
                .sched
                .lock()
                .unwrap()
                .record_thread_mm(tid, MmId::initial(tid));
        }
        {
            let mut sched = state.sched.lock().unwrap();
            sched.sigkill_fence_valve = std::time::Duration::from_millis(20);
            if let Some(pid) = victim {
                sched
                    .register_physical_thread(target, MmId::initial(target), pid, pid)
                    .unwrap();
            }
        }
        let rpc = state.receive_rpc(
            Tid::from_raw(root.as_raw()),
            (
                crate::ThreadState::<()>::new(root, &config, ())
                    .thread_logical_time
                    .clone(),
                MmId::initial(root),
                GlobalRequest::NotifySignalPending(
                    target,
                    SigWrapper(libc::SIGKILL),
                    Some(target),
                    super::SignalScope::Process,
                ),
            ),
        );
        let _ = tokio::time::timeout(std::time::Duration::from_secs(10), rpc).await;
    }

    /// On DBT, a SIGKILL notice whose victim is not gone within the fence
    /// valve ends the run with the named refusal before the sender is
    /// answered (shortened valve; Claude re-check of
    /// https://github.com/rrnewton/hermit/pull/4423, N3).
    #[tokio::test]
    async fn a_dbt_sigkill_notice_past_its_valve_refuses_before_answering() {
        if !in_child_expecting_refusal(
            "a_dbt_sigkill_notice_past_its_valve_refuses_before_answering",
            "cannot be held until it is gone: it was not gone within",
        ) {
            return;
        }
        // The victim outlives the valve by far, and dies with this child
        // (PR_SET_PDEATHSIG) rather than outliving it (Claude re-check of
        // 4423 at 858d81ec, N5). Its stdio is not this child's, which the
        // parent reads to the end.
        let mut victim = std::process::Command::new("sleep");
        victim
            .arg("60")
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null());
        // SAFETY: prctl is async-signal-safe and touches no shared state.
        unsafe {
            use std::os::unix::process::CommandExt;
            victim.pre_exec(|| {
                if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGKILL) == 0 {
                    Ok(())
                } else {
                    Err(std::io::Error::last_os_error())
                }
            });
        }
        let mut victim = victim.spawn().unwrap();
        send_dbt_sigkill_notice(Some(victim.id() as i32)).await;
        let _ = victim.kill();
        let _ = victim.wait();
        panic!("the SIGKILL notice answered its sender although the victim was not gone");
    }

    /// On DBT, a SIGKILL notice for a victim with a live thread but no thread
    /// registered on the host ends the run with the named refusal before the
    /// sender is answered, never left unfenced (Claude review of
    /// https://github.com/rrnewton/hermit/pull/4423, F2: the end-to-end form
    /// of this case became a passing run with
    /// https://github.com/rrnewton/hermit/issues/4411's fix).
    #[tokio::test]
    async fn a_dbt_sigkill_notice_with_no_host_registration_refuses_before_answering() {
        if !in_child_expecting_refusal(
            "a_dbt_sigkill_notice_with_no_host_registration_refuses_before_answering",
            "nor any other thread of it has a registered host process",
        ) {
            return;
        }
        send_dbt_sigkill_notice(None).await;
        panic!("the SIGKILL notice answered its sender without a fence");
    }

    /// A returning sender's wait for its victims is bounded by the valve: a
    /// commit whose victim never dies returns once the valve expires and
    /// refuses the run by name, instead of hanging the sender (shortened
    /// valve; the 30 s production bound is the same field).
    #[tokio::test]
    async fn a_sigkill_commit_wait_past_its_valve_refuses_the_run() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let [root, sender, target] = [1, 2, 3].map(DetPid::from_raw);
        {
            let mut sched = state.sched.lock().unwrap();
            sched.thread_tree.add_child(root, root, true);
            sched.thread_tree.add_child(root, sender, true);
            sched.thread_tree.add_child(root, target, true);
        }
        for tid in [root, sender, target] {
            install_test_registration(&state, tid, Ivar::new());
            state
                .sched
                .lock()
                .unwrap()
                .record_thread_mm(tid, MmId::initial(tid));
        }
        let token = {
            let mut sched = state.sched.lock().unwrap();
            sched.sigkill_fence_valve = std::time::Duration::from_millis(20);
            sched.reserve_sigkill(sender, target, 0).unwrap().token
        };
        let rpc = state.receive_rpc(
            Tid::from_raw(sender.as_raw()),
            (
                crate::ThreadState::<()>::new(sender, &config, ())
                    .thread_logical_time
                    .clone(),
                MmId::initial(sender),
                GlobalRequest::CommitSigkill(token, true),
            ),
        );
        let (_, response) = tokio::time::timeout(std::time::Duration::from_secs(10), rpc)
            .await
            .expect("the commit's wait outlived its valve");
        assert_eq!(response, GlobalResponse::CommitSigkill(()));
        let sched = state.sched.lock().unwrap();
        let refusal = sched
            .sigkill_fence_refusal_for_test()
            .expect("no refusal recorded");
        assert!(refusal.waited >= std::time::Duration::from_millis(20));
        assert_eq!(refusal.unaccounted, [target]);
    }

    /// Two victims of one SIGKILL send their owner-death batches through this
    /// real RPC handler, as authenticated registered victims, in either order,
    /// with a step2 between the receipts (3994 design revision 4, R2). Each
    /// batch is acknowledged with no counts and runs only when the cohort is
    /// complete, so the waiters woken, the run-queue order and the fuzz arm's
    /// next draw do not depend on the order, under FIFO and under the fuzz
    /// arm. This covers the retention protocol, not a SIGKILL producer: a
    /// SIGKILL victim stages no batch today
    /// (<https://github.com/rrnewton/hermit/issues/2082>).
    #[tokio::test]
    async fn victims_owner_death_wakes_through_the_handler_do_not_depend_on_receipt_order() {
        async fn run(fuzz: bool, reverse: bool) -> (Vec<Vec<DetTid>>, Vec<DetTid>, u64) {
            let config = Config {
                sequentialize_threads: true,
                fuzz_futexes: fuzz,
                ..Config::default()
            };
            let state = GlobalState::initialize(&config, false);
            let [root, sender, target, worker] = [1, 2, 3, 4].map(DetPid::from_raw);
            let mm = MmId::initial(target);
            let futexes = [0x404100, 0x404200].map(|addr| FutexID::private(mm, addr));
            {
                let mut sched = state.sched.lock().unwrap();
                sched.thread_tree.add_child(root, root, true);
                sched.thread_tree.add_child(root, sender, true);
                sched.thread_tree.add_child(root, target, true);
                sched.thread_tree.add_child(target, worker, false);
            }
            for tid in [root, sender, target, worker] {
                install_test_registration(&state, tid, Ivar::new());
                state
                    .sched
                    .lock()
                    .unwrap()
                    .record_thread_mm(tid, MmId::initial(tid));
            }
            for (futex, waiters) in futexes.iter().zip([[11, 12, 13], [14, 15, 16]]) {
                for raw in waiters {
                    let tid = DetTid::from_raw(raw);
                    let mut sched = state.sched.lock().unwrap();
                    sched.thread_tree.add_child(root, tid, true);
                    sched.next_turns.insert(
                        tid,
                        ThreadNextTurn {
                            dettid: tid,
                            child_tid_addr: 0,
                            req: Ivar::new(),
                            resp: Ivar::new(),
                            protocol: Default::default(),
                        },
                    );
                    sched.priorities.insert(tid, DEFAULT_PRIORITY);
                    sched.sleep_futex_waiter(&tid, *futex, None, u32::MAX, None);
                }
            }
            {
                let mut sched = state.sched.lock().unwrap();
                let token = sched.reserve_sigkill(sender, target, 0).unwrap().token;
                assert_eq!(sched.commit_sigkill(token, true).len(), 2);
            }
            let mut receipts = [(target, futexes[0]), (worker, futexes[1])];
            if reverse {
                receipts.reverse();
            }
            for (victim, futex) in receipts {
                let (_, ack) = state
                    .receive_rpc(
                        Tid::from_raw(victim.as_raw()),
                        (
                            crate::ThreadState::<()>::new(victim, &config, ())
                                .thread_logical_time
                                .clone(),
                            MmId::initial(victim),
                            GlobalRequest::RobustListWakes(vec![(victim, futex)]),
                        ),
                    )
                    .await;
                assert_eq!(
                    ack,
                    GlobalResponse::RobustListWakes(Vec::new()),
                    "a victim's batch is retained, not run at receipt"
                );
                state.sched.lock().unwrap().step2_drain_prefix_for_test();
                // The victim's real consuming deregistration, and a duplicate:
                // the first is accounted once, the second changes nothing.
                let deregistration = ThreadDeregistration {
                    dettid: victim,
                    detpid: target,
                    mm: MmId::initial(victim),
                    thread_start_entered: true,
                    timeslice_stats: TimesliceStats {
                        count: 1,
                        sum_ns: 10,
                        min_ns: 10,
                        max_ns: 10,
                    },
                    syscall_count: 3,
                    chaos_epochs: Vec::new(),
                };
                for _ in 0..2 {
                    let (_, done) = state
                        .receive_rpc(
                            Tid::from_raw(victim.as_raw()),
                            (
                                crate::ThreadState::<()>::new(victim, &config, ())
                                    .thread_logical_time
                                    .clone(),
                                MmId::initial(victim),
                                GlobalRequest::DeregisterThread(deregistration.clone()),
                            ),
                        )
                        .await;
                    assert_eq!(done, GlobalResponse::DeregisterThread(()));
                }
                state.sched.lock().unwrap().step2_drain_prefix_for_test();
            }
            assert!(
                state.sched.lock().unwrap().sigkill_fence_holds(),
                "the victim's exit is not yet published to its parent"
            );
            <GlobalState as GlobalTool>::on_backend_process_exited(&state, target.as_raw());
            state.sched.lock().unwrap().step2_drain_prefix_for_test();
            {
                let sched = state.sched.lock().unwrap();
                assert!(
                    !sched.sigkill_fence_holds(),
                    "every victim is accounted and published"
                );
                for victim in [target, worker] {
                    assert_eq!(
                        sched
                            .timeslice_stats_for_test(victim)
                            .map(|stats| stats.count),
                        Some(1),
                        "victim {victim} accounted exactly once"
                    );
                }
            }
            let mut sched = state.sched.lock().unwrap();
            let still_waiting = futexes
                .iter()
                .map(|futex| sched.futex_waiters_for_test(*futex))
                .collect();
            let queue = sched.run_queue_for_test();
            (still_waiting, queue, sched.next_fuzz_draw_for_test())
        }
        for fuzz in [false, true] {
            let forward = run(fuzz, false).await;
            assert_eq!(forward.0.iter().map(Vec::len).collect::<Vec<_>>(), [2, 2]);
            assert_eq!(forward, run(fuzz, true).await, "fuzz={fuzz}");
        }
    }

    /// A victim with a filed request, whose real consuming deregistration
    /// reaches the handler before the sender's commit (an early receipt, held)
    /// or after it. Both readiness orders give the same retirement: the
    /// callback stays pending with nothing injected (`RetirementGuest` panics
    /// on an injection) and is not retired, the cleanup is accounted once, the
    /// fence is released, and the victim's process is waitable
    /// (https://github.com/rrnewton/hermit/issues/3994, R1).
    #[tokio::test]
    async fn a_victims_receipt_before_or_after_the_commit_retires_it_the_same_way() {
        use reverie::Tool;

        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let [root, sender, victim] = [1, 2, 3].map(DetPid::from_raw);
        for early in [true, false] {
            let state = GlobalState::initialize(&config, false);
            {
                let mut sched = state.sched.lock().unwrap();
                sched.thread_tree.add_child(root, root, true);
                sched.thread_tree.add_child(root, sender, true);
                sched.thread_tree.add_child(root, victim, true);
            }
            for tid in [root, sender, victim] {
                install_test_registration(&state, tid, Ivar::new());
                state
                    .sched
                    .lock()
                    .unwrap()
                    .record_thread_mm(tid, MmId::initial(tid));
            }
            let tool: Detcore = Detcore::new(Tid::from_raw(victim.as_raw()), &config);
            let mut thread = tool.init_thread_state(Tid::from_raw(victim.as_raw()), None);
            thread.detpid = Some(victim);
            let clock = thread.thread_logical_time.clone();
            let mut guest = RetirementGuest {
                global: &state,
                config: &config,
                thread,
                requests: Mutex::new(Vec::new()),
                retired: std::sync::atomic::AtomicBool::new(false),
            };
            let token = state
                .sched
                .lock()
                .unwrap()
                .reserve_sigkill(sender, victim, 0)
                .unwrap()
                .token;
            let deregister = || {
                state.receive_rpc(
                    Tid::from_raw(victim.as_raw()),
                    (
                        clock.clone(),
                        MmId::initial(victim),
                        GlobalRequest::DeregisterThread(ThreadDeregistration {
                            dettid: victim,
                            detpid: victim,
                            mm: MmId::initial(victim),
                            thread_start_entered: true,
                            timeslice_stats: TimesliceStats {
                                count: 1,
                                sum_ns: 10,
                                min_ns: 10,
                                max_ns: 10,
                            },
                            syscall_count: 3,
                            chaos_epochs: Vec::new(),
                        }),
                    ),
                )
            };
            {
                let mut rejoin = Resources::new(victim);
                rejoin.insert(
                    ResourceID::BlockedExternalContinue(ExternalOpId::new(victim, 7)),
                    Permission::RW,
                );
                let mut call = std::pin::pin!(super::resource_request(&mut guest, rejoin));
                assert!(futures::poll!(call.as_mut()).is_pending());
                if early {
                    assert_eq!(deregister().await.1, GlobalResponse::DeregisterThread(()));
                }
                state.sched.lock().unwrap().commit_sigkill(token, true);
                if !early {
                    assert_eq!(deregister().await.1, GlobalResponse::DeregisterThread(()));
                }
                for _ in 0..4 {
                    tokio::task::yield_now().await;
                    assert!(futures::poll!(call.as_mut()).is_pending(), "early={early}");
                }
            }
            assert!(!guest.retired.load(std::sync::atomic::Ordering::SeqCst));
            assert!(
                state.sched.lock().unwrap().sigkill_fence_holds(),
                "early={early}: the exit is not yet published"
            );
            <GlobalState as GlobalTool>::on_backend_process_exited(&state, victim.as_raw());
            let sched = state.sched.lock().unwrap();
            assert!(!sched.sigkill_fence_holds(), "early={early}");
            assert!(sched.process_is_unreaped_zombie(victim), "early={early}");
            assert_eq!(
                sched
                    .timeslice_stats_for_test(victim)
                    .map(|stats| stats.count),
                Some(1),
                "early={early}"
            );
        }
    }

    #[tokio::test]
    async fn thread_exited_uses_natural_retirement_for_killed_stale_and_failed_rpcs() {
        use std::sync::atomic::Ordering;

        use reverie::Tool;

        for cause in ["killed", "stale image", "backend failure", "live"] {
            let (config, state, tid, pid) = cancellation_test_state();
            install_test_registration(&state, tid, Ivar::new());
            let tool: Detcore = Detcore::new(Tid::from_raw(tid.as_raw()), &config);
            let mut thread = tool.init_thread_state(Tid::from_raw(tid.as_raw()), None);
            thread.detpid = Some(pid);
            thread.thread_logical_time.add_syscall_with_cost(37);
            match cause {
                "killed" => {
                    state
                        .sched
                        .lock()
                        .unwrap()
                        .logically_kill_thread(&tid, &pid, thread.mm_id)
                }
                "stale image" => {
                    state
                        .sched
                        .lock()
                        .unwrap()
                        .install_test_exec_incarnation(tid, thread.mm_id);
                    thread.mm_id = thread.mm_id.for_exec(pid);
                }
                "backend failure" => state.report_backend_failure(reverie::BackendFailure {
                    pid: Tid::from_raw(pid.as_raw()),
                    tid: Tid::from_raw(tid.as_raw()),
                    phase: "natural retirement control",
                }),
                "live" => {}
                _ => unreachable!(),
            }
            let mut guest = RetirementGuest {
                global: &state,
                config: &config,
                thread,
                requests: Mutex::new(Vec::new()),
                retired: std::sync::atomic::AtomicBool::new(false),
            };
            let before = guest.thread.thread_logical_time.as_nanos();
            {
                let mut call = std::pin::pin!(super::send_and_update_time(
                    &mut guest,
                    GlobalRequest::GlobalTimeLowerBound
                ));
                if cause == "live" {
                    assert!(matches!(
                        futures::poll!(call.as_mut()),
                        std::task::Poll::Ready((_, GlobalResponse::GlobalTimeLowerBound(_)))
                    ));
                } else {
                    assert!(futures::poll!(call.as_mut()).is_pending(), "{cause}");
                }
            }
            // A terminal backend failure closes ordinary RPC admission before
            // any ThreadExited response. The backend failure subscription owns
            // cancellation of that pending callback; it must not retire as success.
            assert_eq!(
                guest.retired.load(Ordering::SeqCst),
                matches!(cause, "killed" | "stale image"),
                "{cause}"
            );
            assert_eq!(guest.requests.lock().unwrap().len(), 1);
            assert_eq!(guest.thread.thread_logical_time.as_nanos(), before);
            if cause == "backend failure" {
                assert!(state.sched.lock().unwrap().backend_failed());
                assert!(
                    futures::poll!(std::pin::pin!(state.wait_for_backend_failure())).is_ready()
                );
            }
        }
    }

    /// A5 step 2b: an ordinary request first publishes the caller's process's
    /// copied dispositions, through the real global RPC, when they differ
    /// from what the scheduler holds, and not otherwise; the publication
    /// carries the next generation, and a copy whose actions became unknown
    /// withdraws them. A discard reads the result.
    #[tokio::test]
    async fn an_ordinary_request_first_publishes_changed_dispositions() {
        use reverie::Tool;

        use crate::syscalls::signal_shadow::CopiedDispositions;
        use crate::syscalls::signal_shadow::PublishedDispositions;

        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        }
        .with_backend(|backend| backend.supports_blocked_wait_signal_interruption = true);
        let state = GlobalState::initialize(&config, false);
        let leader = DetTid::from_raw(17);
        let process = DetPid::from_raw(17);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(leader, leader, true);
        install_test_registration(&state, leader, Ivar::new());
        let tool = <Detcore as Tool>::new(reverie::Pid::from_raw(leader.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(leader.as_raw()), None);
        thread.detpid = Some(process);
        let usr1 = 1 << (libc::SIGUSR1 - 1);
        thread.signal_shadow = Some(crate::signal_state::SignalShadow::seeded(usr1, 0));
        let mut guest = ExternalRegistrationGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            post_exec: false,
            ppid: None,
            private_memory: None,
        };
        let mutation = |n: u32| GlobalRequest::RecordHostMutation {
            path: format!("/a5-step-2b/{n}"),
        };
        let ignoring = CopiedDispositions {
            ignored: usr1,
            caught: 0,
        };
        let held = |state: &GlobalState| {
            state
                .sched
                .lock()
                .unwrap()
                .published_dispositions
                .get(&process)
                .copied()
        };

        super::send_and_update_time(&mut guest, mutation(1)).await;
        super::send_and_update_time(&mut guest, mutation(2)).await;
        assert_eq!(
            *guest.requests.lock().unwrap(),
            vec![
                GlobalRequest::PublishSignalDispositions(1, Some(ignoring)),
                GlobalRequest::PublishSignalMask(Some(0)),
                mutation(1),
                mutation(2),
            ],
            "published once, before the first request"
        );
        // The scheduler's entry, and the process's record of it.
        let entry = |generation, dispositions| PublishedDispositions {
            generation,
            dispositions,
            ..Default::default()
        };
        let record = |generation, accepted, dispositions| PublishedDispositions {
            generation,
            accepted,
            dispositions,
        };
        assert_eq!(held(&state), Some(entry(1, Some(ignoring))));
        assert_eq!(
            *guest.thread.published_dispositions.lock().unwrap(),
            record(1, 1, Some(ignoring))
        );
        assert!(
            state
                .sched
                .lock()
                .unwrap()
                .published_dispositions_for(leader, libc::SIGUSR1)
                .is_some()
        );

        // A clone that shares the actions with a new process withdraws them
        // before it runs (`before_clone`).
        guest.requests.lock().unwrap().clear();
        crate::syscalls::signal_shadow::before_clone(
            &mut guest,
            (libc::CLONE_VM | libc::CLONE_SIGHAND) as u64,
        )
        .await;
        assert_eq!(
            *guest.requests.lock().unwrap(),
            vec![GlobalRequest::PublishSignalDispositions(2, None)]
        );
        assert_eq!(held(&state), Some(entry(2, None)));
        assert_eq!(
            *guest.thread.published_dispositions.lock().unwrap(),
            record(2, 2, None)
        );
        super::send_and_update_time(&mut guest, mutation(3)).await;
        assert_eq!(
            guest.requests.lock().unwrap().len(),
            2,
            "nothing more to withdraw"
        );

        // A thread the scheduler does not know is refused: the record keeps
        // its dispositions, and once known, the next request publishes again.
        guest.thread.signal_shadow = Some(crate::signal_state::SignalShadow::seeded(usr1, 0));
        let mut turn = state.sched.lock().unwrap().next_turns.remove(&leader);
        guest.requests.lock().unwrap().clear();
        super::send_and_update_time(&mut guest, mutation(4)).await;
        assert_eq!(held(&state), Some(entry(2, None)), "refused");
        assert_eq!(
            *guest.thread.published_dispositions.lock().unwrap(),
            record(3, 2, None)
        );
        state
            .sched
            .lock()
            .unwrap()
            .next_turns
            .insert(leader, turn.take().unwrap());
        super::send_and_update_time(&mut guest, mutation(5)).await;
        assert_eq!(held(&state), Some(entry(4, Some(ignoring))));
        assert_eq!(
            *guest.thread.published_dispositions.lock().unwrap(),
            record(4, 4, Some(ignoring))
        );

        // The scheduler holds a generation this process never used (a stale
        // entry): the publication is not taken, the record carries on after
        // that generation, and the thread publishes again at once, before its
        // request goes out, and is taken.
        let stale = entry(
            50,
            Some(CopiedDispositions {
                ignored: 0,
                caught: usr1,
            }),
        );
        state
            .sched
            .lock()
            .unwrap()
            .published_dispositions
            .insert(process, stale);
        *guest.thread.published_dispositions.lock().unwrap() = record(4, 4, None);
        super::send_and_update_time(&mut guest, mutation(6)).await;
        assert_eq!(held(&state), Some(entry(51, Some(ignoring))));
        assert_eq!(
            *guest.thread.published_dispositions.lock().unwrap(),
            record(51, 51, Some(ignoring))
        );
        // The same with a stale entry of the very generation the process uses
        // next, the likeliest shape: not taken, so not mistaken for it.
        state
            .sched
            .lock()
            .unwrap()
            .published_dispositions
            .insert(process, entry(52, stale.dispositions));
        *guest.thread.published_dispositions.lock().unwrap() = record(51, 51, None);
        super::send_and_update_time(&mut guest, mutation(7)).await;
        assert_eq!(held(&state), Some(entry(53, Some(ignoring))));
        assert_eq!(
            *guest.thread.published_dispositions.lock().unwrap(),
            record(53, 53, Some(ignoring))
        );
        assert_eq!(
            *guest.requests.lock().unwrap(),
            vec![
                GlobalRequest::PublishSignalDispositions(3, Some(ignoring)),
                mutation(4),
                GlobalRequest::PublishSignalDispositions(4, Some(ignoring)),
                mutation(5),
                GlobalRequest::PublishSignalDispositions(5, Some(ignoring)),
                GlobalRequest::PublishSignalDispositions(51, Some(ignoring)),
                mutation(6),
                GlobalRequest::PublishSignalDispositions(52, Some(ignoring)),
                GlobalRequest::PublishSignalDispositions(53, Some(ignoring)),
                mutation(7),
            ]
        );
    }

    /// A5 step 2b: an ordinary request first publishes the caller's mask
    /// from Detcore's copy, through the real global RPC, when it differs
    /// from what the scheduler holds, withdraws it inside a temporary-mask
    /// wait, and is refused for a thread the scheduler does not know, which
    /// publishes again once known. The scheduler's reader answers from it.
    #[tokio::test]
    async fn an_ordinary_request_first_publishes_a_changed_mask() {
        use reverie::Tool;

        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        }
        .with_backend(|backend| backend.supports_blocked_wait_signal_interruption = true);
        let state = GlobalState::initialize(&config, false);
        let leader = DetTid::from_raw(17);
        let process = DetPid::from_raw(17);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(leader, leader, true);
        install_test_registration(&state, leader, Ivar::new());
        let tool = <Detcore as Tool>::new(reverie::Pid::from_raw(leader.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(leader.as_raw()), None);
        thread.detpid = Some(process);
        let usr1 = 1 << (libc::SIGUSR1 - 1);
        thread.signal_shadow = Some(crate::signal_state::SignalShadow::seeded(0, usr1));
        let mut guest = ExternalRegistrationGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            post_exec: false,
            ppid: None,
            private_memory: None,
        };
        let mutation = |n: u32| GlobalRequest::RecordHostMutation {
            path: format!("/a5-step-2b-mask/{n}"),
        };
        let masks = |guest: &ExternalRegistrationGuest<'_>| -> Vec<GlobalRequest> {
            guest
                .requests
                .lock()
                .unwrap()
                .iter()
                .filter(|request| !matches!(request, GlobalRequest::PublishSignalDispositions(..)))
                .cloned()
                .collect()
        };
        let held = |state: &GlobalState| {
            state
                .sched
                .lock()
                .unwrap()
                .published_masks
                .get(&leader)
                .copied()
        };

        super::send_and_update_time(&mut guest, mutation(1)).await;
        super::send_and_update_time(&mut guest, mutation(2)).await;
        // The whole stream: the process's dispositions, then the mask, then
        // the request.
        assert_eq!(
            *guest.requests.lock().unwrap(),
            vec![
                GlobalRequest::PublishSignalDispositions(
                    1,
                    Some(crate::syscalls::signal_shadow::CopiedDispositions {
                        ignored: 0,
                        caught: 0,
                    })
                ),
                GlobalRequest::PublishSignalMask(Some(usr1)),
                mutation(1),
                mutation(2),
            ],
            "published once, before the first request"
        );
        assert_eq!(held(&state), Some(usr1));
        assert_eq!(guest.thread.published_mask, Some(Some(usr1)));
        assert_eq!(
            state
                .sched
                .lock()
                .unwrap()
                .published_mask_for(leader, libc::SIGUSR1),
            Some(usr1)
        );

        // Inside a temporary-mask wait the kernel may hold the wait's mask:
        // withdrawn.
        guest
            .thread
            .signal_shadow
            .as_mut()
            .unwrap()
            .thread
            .wait_mask = Some(0);
        guest.requests.lock().unwrap().clear();
        super::send_and_update_time(&mut guest, mutation(3)).await;
        assert_eq!(
            masks(&guest),
            vec![GlobalRequest::PublishSignalMask(None), mutation(3)]
        );
        assert_eq!(held(&state), None);

        // Refused while the scheduler does not know the thread; published
        // again once it does.
        guest
            .thread
            .signal_shadow
            .as_mut()
            .unwrap()
            .thread
            .wait_mask = None;
        let mut turn = state.sched.lock().unwrap().next_turns.remove(&leader);
        guest.requests.lock().unwrap().clear();
        super::send_and_update_time(&mut guest, mutation(4)).await;
        assert_eq!(held(&state), None, "refused");
        assert_eq!(guest.thread.published_mask, Some(None));
        state
            .sched
            .lock()
            .unwrap()
            .next_turns
            .insert(leader, turn.take().unwrap());
        super::send_and_update_time(&mut guest, mutation(5)).await;
        assert_eq!(
            masks(&guest),
            vec![
                GlobalRequest::PublishSignalMask(Some(usr1)),
                mutation(4),
                GlobalRequest::PublishSignalMask(Some(usr1)),
                mutation(5),
            ]
        );
        assert_eq!(held(&state), Some(usr1));

        // An exec that loses the copy: what the scheduler holds is unknown
        // after an exec, so the next request withdraws the mask.
        guest
            .thread
            .signal_shadow
            .as_ref()
            .unwrap()
            .forget_dispositions();
        crate::syscalls::signal_shadow::at_exec(&mut guest);
        assert!(guest.thread.signal_shadow.is_none(), "the copy was lost");
        guest.requests.lock().unwrap().clear();
        super::send_and_update_time(&mut guest, mutation(6)).await;
        assert_eq!(
            masks(&guest),
            vec![GlobalRequest::PublishSignalMask(None), mutation(6)]
        );
        assert_eq!(held(&state), None);
    }

    async fn check_external_child_tid_registration(
        flags: CloneFlags,
        supplied_address: usize,
        expected_address: usize,
    ) {
        use reverie::Tool;

        let config = Config {
            sequentialize_threads: true,
            // This control observes registration before the child starts; use
            // the supported parent-first order for the one turn it drives.
            runs_post_fork: crate::RunsPostFork::Parent,
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.needs_killed_thread_rpc_cancellation = true;
        });
        let state = GlobalState::initialize(&config, false);
        let parent = DetTid::from_raw(17);
        let parent_pid = DetPid::from_raw(17);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(parent, parent, true);
        install_test_registration(&state, parent, Ivar::new());
        let tool = Detcore::new(reverie::Pid::from_raw(parent.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(parent.as_raw()), None);
        thread.detpid = Some(parent_pid);
        let mut guest = ExternalRegistrationGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            post_exec: false,
            ppid: None,
            private_memory: None,
        };
        let child = DetTid::from_raw(18);
        let exit_signal = if flags.contains(CloneFlags::CLONE_THREAD) {
            0
        } else {
            libc::SIGCHLD
        };
        tokio::time::timeout(Duration::from_secs(2), async {
            let mut registration = std::pin::pin!(tool.register_external_child(
                &mut guest,
                Tid::from_raw(child.as_raw()),
                supplied_address,
                flags,
                exit_signal,
                None,
            ));
            assert!(futures::poll!(registration.as_mut()).is_pending());
            // The production RPC parks the parent on ParentContinue. Drive
            // that actual scheduler turn instead of pre-filling a response or
            // disabling sequentialization to make registration return.
            let committed = crate::scheduler::do_a_turn_blocking(
                state.sched.clone(),
                state.global_time.clone(),
                &Err(crate::scheduler::SkipTurn),
            )
            .await
            .expect("the parent continuation must commit");
            assert_eq!(committed.tid, parent);
            assert_eq!(
                committed.resources,
                std::collections::HashMap::from([(
                    crate::resources::ResourceID::ParentContinue { parent, child },
                    crate::resources::Permission::W,
                )]),
            );
            registration.await;
        })
        .await
        .expect("external child registration must return without running a guest");

        assert_eq!(guest.thread.clone_flags, None);
        assert_eq!(guest.thread.dettid, parent);
        let mut scheduler = state.sched.lock().unwrap();
        assert_eq!(scheduler.next_turns.len(), 2);
        assert!(scheduler.next_turns.contains_key(&parent));
        assert_eq!(
            scheduler.next_turns[&child].child_tid_addr,
            expected_address
        );
        assert_eq!(
            *guest.requests.lock().unwrap(),
            vec![GlobalRequest::CreateChildThread(
                child,
                parent_pid,
                expected_address,
                Some(flags),
                exit_signal,
                None,
                Some(DEFAULT_PRIORITY),
            )],
            "external registration must send one correctly gated real RPC"
        );
        let child_pid = if flags.contains(CloneFlags::CLONE_THREAD) {
            parent_pid
        } else {
            child
        };
        let child_mm = MmId::for_clone(
            MmId::initial(parent_pid),
            child,
            flags.contains(CloneFlags::CLONE_VM),
        );
        scheduler.logically_kill_thread(&child, &child_pid, child_mm);
        assert!(!scheduler.next_turns.contains_key(&child));
        assert!(scheduler.next_turns.contains_key(&parent));
        assert_eq!(
            scheduler.child_tid_was_cleared(
                FutexID::private(child_mm, supplied_address),
                child.as_raw(),
            ),
            expected_address != 0,
            "exit must use only the registered child-TID address"
        );
        assert!(!scheduler.child_tid_was_cleared(FutexID::private(child_mm, 0), child.as_raw(),));
        assert!(!scheduler.child_tid_was_cleared(
            FutexID::private(child_mm, supplied_address),
            parent.as_raw(),
        ));
    }

    #[tokio::test]
    async fn external_registration_without_child_cleartid_disables_exit_wake() {
        for kind in [
            CloneFlags::empty(),
            CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM | CloneFlags::CLONE_SIGHAND,
        ] {
            for registration in [CloneFlags::empty(), CloneFlags::CLONE_CHILD_SETTID] {
                check_external_child_tid_registration(kind | registration, 0x1234, 0).await;
            }
        }
    }

    #[tokio::test]
    async fn external_registration_with_child_cleartid_preserves_exact_exit_wake() {
        for kind in [
            CloneFlags::empty(),
            CloneFlags::CLONE_THREAD | CloneFlags::CLONE_VM | CloneFlags::CLONE_SIGHAND,
        ] {
            for registration in [
                CloneFlags::CLONE_CHILD_CLEARTID,
                CloneFlags::CLONE_CHILD_CLEARTID | CloneFlags::CLONE_CHILD_SETTID,
            ] {
                check_external_child_tid_registration(kind | registration, 0x1234, 0x1234).await;
                check_external_child_tid_registration(kind | registration, 0, 0).await;
            }
        }
    }

    #[tokio::test]
    async fn set_child_tid_address_rpc_updates_and_resets_the_registration() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        install_test_registration(&state, dettid, Ivar::new());
        state
            .sched
            .lock()
            .unwrap()
            .next_turns
            .get_mut(&dettid)
            .unwrap()
            .child_tid_addr = 0x1234;

        let response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    DetTime::new(&config),
                    MmId::initial(detpid),
                    GlobalRequest::SetChildTidAddress(0),
                ),
            )
            .await;

        assert_eq!(response.1, GlobalResponse::SetChildTidAddress(()));
        assert_eq!(
            state
                .sched
                .lock()
                .unwrap()
                .next_turns
                .get(&dettid)
                .unwrap()
                .child_tid_addr,
            0
        );
    }

    /// A successful SIGKILL reaches the scheduler's record through the RPCs a
    /// sender makes in its turn: a process named by `kill`, and a SIGKILL with
    /// no named victim; another signal records nothing
    /// (https://github.com/rrnewton/hermit/issues/3904).
    #[tokio::test]
    async fn sigkill_notifications_record_the_doomed_process() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        let notify = |request| {
            state.receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (DetTime::new(&config), MmId::initial(detpid), request),
            )
        };
        let victim = DetPid::from_raw(41);
        assert_eq!(
            notify(GlobalRequest::NotifySignalPending(
                DetTid::from_raw(41),
                SigWrapper(libc::SIGKILL),
                Some(victim),
                super::SignalScope::Process,
            ))
            .await
            .1,
            GlobalResponse::NotifySignalPending(())
        );
        assert_eq!(
            state.sched.lock().unwrap().sigkill_recorded(victim),
            (true, false)
        );
        let other = DetPid::from_raw(43);
        notify(GlobalRequest::NotifySignalPending(
            DetTid::from_raw(43),
            SigWrapper(libc::SIGTERM),
            Some(other),
            super::SignalScope::Process,
        ))
        .await;
        assert_eq!(
            state.sched.lock().unwrap().sigkill_recorded(other),
            (false, false)
        );
        // A process-group kill records every process in the group.
        assert_eq!(
            notify(GlobalRequest::NotifyUnresolvedSigkill(Some(detpid)))
                .await
                .1,
            GlobalResponse::NotifyUnresolvedSigkill(())
        );
        assert_eq!(
            state.sched.lock().unwrap().sigkill_recorded(detpid),
            (true, false)
        );
        assert_eq!(
            notify(GlobalRequest::NotifyUnresolvedSigkill(None)).await.1,
            GlobalResponse::NotifyUnresolvedSigkill(())
        );
        assert_eq!(
            state.sched.lock().unwrap().sigkill_recorded(other),
            (false, true)
        );
    }

    async fn child_start_clock_trajectory(
        start_before_selection: bool,
        child_first: bool,
    ) -> (Vec<LogicalTime>, Vec<DetTid>, bool) {
        let config = Config {
            sequentialize_threads: true,
            runs_post_fork: if child_first {
                RunsPostFork::Child
            } else {
                RunsPostFork::Parent
            },
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let parent = DetTid::from_raw(17);
        let parent_pid = parent;
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(parent, parent, true);
        let child = DetTid::from_raw(parent.as_raw() + 1);
        let parent_mm = MmId::initial(parent_pid);
        let child_mm = MmId::for_clone(parent_mm, child, false);
        install_test_registration(&state, parent, Ivar::new());
        let epoch = DetTime::new(&config).as_nanos();
        let mut parent_clock = DetTime::new(&config);
        parent_clock.advance_to(epoch + LogicalTime::from_nanos(1_001));
        let child_clock = parent_clock.clone_for_child();

        // Polling the real parent RPC publishes both admission and
        // ParentContinue before it waits, just as the clone handler does.
        let mut registration = Box::pin(state.receive_rpc(
            Tid::from_raw(parent.as_raw()),
            (
                parent_clock.clone(),
                parent_mm,
                GlobalRequest::CreateChildThread(
                    child,
                    parent_pid,
                    0,
                    Some(CloneFlags::empty()),
                    libc::SIGCHLD,
                    None,
                    Some(DEFAULT_PRIORITY),
                ),
            ),
        ));
        assert!(futures::poll!(&mut registration).is_pending());
        let child_request = state.sched.lock().unwrap().next_turns[&child].req.clone();
        let mut startup = Box::pin(state.receive_rpc(
            Tid::from_raw(child.as_raw()),
            (
                child_clock.clone(),
                child_mm,
                GlobalRequest::StartNewThread(child, child, None, None),
            ),
        ));
        if start_before_selection {
            // recv_start_new_thread yields once before filling its request.
            assert!(futures::poll!(&mut startup).is_pending());
            assert!(futures::poll!(&mut startup).is_pending());
            assert!(child_request.try_read().is_some());
        }

        let skipped = Err(crate::scheduler::SkipTurn);
        let mut turn = Box::pin(crate::scheduler::do_a_turn_blocking(
            state.sched.clone(),
            state.global_time.clone(),
            &skipped,
        ));
        if !start_before_selection && child_first {
            assert!(futures::poll!(&mut turn).is_pending());
            assert_eq!(child_request.to_string(), "<ivar HasWaiter>");
            assert!(futures::poll!(&mut startup).is_pending());
            assert!(futures::poll!(&mut startup).is_pending());
            assert!(child_request.try_read().is_some());
        }
        let first = turn.await.expect("first post-fork turn must commit");
        if !start_before_selection && !child_first {
            assert!(child_request.try_read().is_none());
            assert!(futures::poll!(&mut startup).is_pending());
            assert!(futures::poll!(&mut startup).is_pending());
            assert!(child_request.try_read().is_some());
        }
        let child_resource = ResourceID::MemAddrSpace(child);
        let parent_resource = ResourceID::ParentContinue { parent, child };
        let (first_tid, first_resource, first_permission, second_resource, second_permission) =
            if child_first {
                (
                    child,
                    &child_resource,
                    Permission::RW,
                    &parent_resource,
                    Permission::W,
                )
            } else {
                (
                    parent,
                    &parent_resource,
                    Permission::W,
                    &child_resource,
                    Permission::RW,
                )
            };
        assert_eq!(first.tid, first_tid);
        assert_eq!(first.resources.len(), 1);
        assert_eq!(first.resources.get(first_resource), Some(&first_permission));
        if child_first {
            assert_eq!(
                startup.as_mut().await,
                (None, GlobalResponse::StartNewThread(None))
            );
        } else {
            assert_eq!(
                registration.as_mut().await,
                (None, GlobalResponse::CreateChildThread(None))
            );
        }
        let first_time = state.sched.lock().unwrap().committed_time;
        assert_eq!(first_time, epoch + LogicalTime::from_nanos(1_001));
        assert_eq!(
            state.global_time.lock().unwrap().threads_time(child),
            child_clock.as_nanos()
        );

        // The selected thread's first new nanosecond and the ordinary scheduler
        // increment must both remain observable on the other thread's turn.
        let (mut first_clock, first_mm) = if child_first {
            (child_clock, child_mm)
        } else {
            (parent_clock, parent_mm)
        };
        first_clock.advance_to(first_clock.as_nanos() + LogicalTime::from_nanos(1));
        let mut resources = Resources::new(first_tid);
        resources.insert(ResourceID::MemAddrSpace(first_tid), Permission::RW);
        let mut work = Box::pin(state.receive_rpc(
            Tid::from_raw(first_tid.as_raw()),
            (
                first_clock,
                first_mm,
                GlobalRequest::RequestResources(resources, first_tid),
            ),
        ));
        assert!(futures::poll!(&mut work).is_pending());
        let second = crate::scheduler::do_a_turn_blocking(
            state.sched.clone(),
            state.global_time.clone(),
            &Ok(first),
        )
        .await
        .expect("other thread's continuation must commit");
        assert_eq!(second.resources.len(), 1);
        assert_eq!(
            second.resources.get(second_resource),
            Some(&second_permission)
        );
        if child_first {
            assert_eq!(
                registration.await,
                (None, GlobalResponse::CreateChildThread(None))
            );
        } else {
            assert_eq!(startup.await, (None, GlobalResponse::StartNewThread(None)));
        }
        let mut scheduler = state.sched.lock().unwrap();
        let next_time = scheduler.committed_time;
        assert_eq!(next_time, epoch + LogicalTime::from_nanos(501_002));
        let queue = scheduler.run_queue.tids().copied().collect();
        let next_random = scheduler.child_runs_first_post_fork(RunsPostFork::Random);
        (vec![first_time, next_time], queue, next_random)
    }

    #[tokio::test]
    async fn child_start_clock_is_independent_of_first_rpc_arrival() {
        for child_first in [true, false] {
            assert_eq!(
                child_start_clock_trajectory(true, child_first).await,
                child_start_clock_trajectory(false, child_first).await,
            );
        }
    }

    #[tokio::test]
    async fn vfork_registration_does_not_charge_inherited_work_before_startup() {
        let (config, state, parent, parent_pid) = cancellation_test_state();
        let child = DetTid::from_raw(parent.as_raw() + 1);
        let mm = MmId::initial(parent_pid);
        install_test_registration(&state, parent, Ivar::new());
        let mut parent_clock = DetTime::new(&config);
        let epoch = parent_clock.as_nanos();
        parent_clock.advance_to(epoch + LogicalTime::from_nanos(1_001));
        let child_clock = parent_clock.clone_for_child();
        let mut resources = Resources::new(parent);
        resources.insert(
            ResourceID::BlockingVfork(ExternalOpId::new(parent, 1)),
            Permission::RW,
        );
        let mut blocking = Box::pin(state.receive_rpc(
            Tid::from_raw(parent.as_raw()),
            (
                parent_clock,
                mm,
                GlobalRequest::RequestResources(resources, parent_pid),
            ),
        ));
        assert!(futures::poll!(&mut blocking).is_pending());
        let background = crate::scheduler::do_a_turn_blocking(
            state.sched.clone(),
            state.global_time.clone(),
            &Err(crate::scheduler::SkipTurn),
        )
        .await;
        assert!(background.is_err());
        assert_eq!(
            blocking.await,
            (None, GlobalResponse::RequestResources(ResumeStatus::Normal))
        );
        let before_child = state.global_time.lock().unwrap().as_nanos();
        assert_eq!(before_child, epoch + LogicalTime::from_nanos(1_001));

        // Unlike ordinary clone, this first RPC is sent by the child itself.
        let created = state
            .receive_rpc(
                Tid::from_raw(child.as_raw()),
                (
                    child_clock.clone(),
                    mm,
                    GlobalRequest::CreateVforkChildThread(
                        parent,
                        parent_pid,
                        child,
                        0,
                        CloneFlags::CLONE_VFORK | CloneFlags::CLONE_VM,
                        libc::SIGCHLD,
                        Some(DEFAULT_PRIORITY - 1),
                    ),
                ),
            )
            .await;
        assert_eq!(created, (None, GlobalResponse::CreateChildThread(None)));
        assert_eq!(state.global_time.lock().unwrap().as_nanos(), before_child);
        assert_eq!(
            state.global_time.lock().unwrap().threads_time(child),
            child_clock.as_nanos()
        );

        let mut startup = Box::pin(state.receive_rpc(
            Tid::from_raw(child.as_raw()),
            (
                child_clock,
                mm,
                GlobalRequest::StartNewThread(child, child, None, None),
            ),
        ));
        assert!(futures::poll!(&mut startup).is_pending());
        assert!(futures::poll!(&mut startup).is_pending());
        let first = crate::scheduler::do_a_turn_blocking(
            state.sched.clone(),
            state.global_time.clone(),
            &background,
        )
        .await
        .expect("vfork child must receive its first turn");
        assert_eq!(first.tid, child);
        assert_eq!(startup.await, (None, GlobalResponse::StartNewThread(None)));
        assert_eq!(state.global_time.lock().unwrap().as_nanos(), before_child);
    }

    #[tokio::test]
    async fn first_nonstartup_rpc_counts_only_new_child_work() {
        let config = Config {
            sequentialize_threads: false,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let parent = DetTid::from_raw(17);
        let child = DetTid::from_raw(18);
        let mut parent_clock = DetTime::new(&config);
        let epoch = parent_clock.as_nanos();
        parent_clock.advance_to(epoch + LogicalTime::from_nanos(101));
        let mut child_clock = parent_clock.clone_for_child();
        let _ = state
            .receive_rpc(
                Tid::from_raw(parent.as_raw()),
                (
                    parent_clock,
                    MmId::initial(parent),
                    GlobalRequest::GlobalTimeLowerBound,
                ),
            )
            .await;
        child_clock.advance_to(child_clock.as_nanos() + LogicalTime::from_nanos(1));
        let observed = state
            .receive_rpc(
                Tid::from_raw(child.as_raw()),
                (
                    child_clock,
                    MmId::initial(child),
                    GlobalRequest::GlobalTimeLowerBound,
                ),
            )
            .await;
        assert_eq!(
            observed,
            (
                None,
                GlobalResponse::GlobalTimeLowerBound(epoch + LogicalTime::from_nanos(102))
            )
        );
    }

    #[tokio::test]
    async fn exec_reconnect_retains_inherited_work_accounting_across_local_reload() {
        let (config, state, leader, detpid) = cancellation_test_state();
        let ancestor = DetTid::from_raw(leader.as_raw() - 1);
        let worker = DetTid::from_raw(leader.as_raw() + 1);
        let old_mm = MmId::initial(detpid);
        install_test_registration(&state, leader, Ivar::new());
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(leader, worker, false);
        install_test_registration(&state, worker, Ivar::new());

        let mut ancestor_clock = DetTime::new(&config);
        let epoch = ancestor_clock.as_nanos();
        ancestor_clock.advance_to(epoch + LogicalTime::from_nanos(1_000));
        let mut leader_clock = ancestor_clock.clone_for_child();
        leader_clock.advance_to(epoch + LogicalTime::from_nanos(1_100));
        let mut worker_clock = leader_clock.clone_for_child();
        worker_clock.advance_to(epoch + LogicalTime::from_nanos(1_350));
        for (tid, clock) in [
            (ancestor, ancestor_clock),
            (leader, leader_clock),
            (worker, worker_clock.clone()),
        ] {
            let _ = state
                .receive_rpc(
                    Tid::from_raw(tid.as_raw()),
                    (clock, old_mm, GlobalRequest::GlobalTimeLowerBound),
                )
                .await;
        }
        let total = state.global_time.lock().unwrap().as_nanos();
        assert_eq!(total, epoch + LogicalTime::from_nanos(1_350));
        // A failed exec cancels its pending transfer without changing either
        // inherited component. The next successful attempt must use the same
        // clocks, not charge either component's inherited work again.
        let _ = state
            .receive_rpc(
                Tid::from_raw(worker.as_raw()),
                (
                    worker_clock.clone(),
                    old_mm,
                    GlobalRequest::PrepareExec(detpid, old_mm, Default::default()),
                ),
            )
            .await;
        let cancelled = state
            .receive_rpc(
                Tid::from_raw(worker.as_raw()),
                (
                    worker_clock.clone(),
                    old_mm,
                    GlobalRequest::CancelExec(detpid),
                ),
            )
            .await;
        assert_eq!(cancelled, (None, GlobalResponse::CancelExec(())));
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        assert_eq!(state.global_time.lock().unwrap().as_nanos(), total);
        assert_eq!(
            state.global_time.lock().unwrap().threads_time(worker),
            worker_clock.as_nanos()
        );

        let prepared = state
            .receive_rpc(
                Tid::from_raw(worker.as_raw()),
                (
                    worker_clock.clone(),
                    old_mm,
                    GlobalRequest::PrepareExec(detpid, old_mm, Default::default()),
                ),
            )
            .await;
        assert_eq!(prepared, (None, GlobalResponse::PrepareExec(())));

        let mut fresh = DetTime::new(&config);
        let recreated = state
            .receive_rpc(
                Tid::from_raw(leader.as_raw()),
                (
                    fresh.clone(),
                    MmId::initial(leader),
                    GlobalRequest::CreateChildThread(
                        leader,
                        detpid,
                        0,
                        None,
                        libc::SIGCHLD,
                        None,
                        Some(DEFAULT_PRIORITY),
                    ),
                ),
            )
            .await;
        assert_eq!(
            recreated,
            (
                Some(worker_clock.as_nanos()),
                GlobalResponse::CreateChildThread(Some(old_mm.for_exec(detpid)))
            )
        );
        // A delayed request from the destroyed image must be rejected before
        // its absolute clock or its inherited metadata reaches accounting.
        let stale = state
            .receive_rpc(
                Tid::from_raw(leader.as_raw()),
                (
                    worker_clock.clone(),
                    old_mm,
                    GlobalRequest::RequestResources(Resources::new(leader), detpid),
                ),
            )
            .await;
        assert_eq!(stale, (None, GlobalResponse::ThreadExited));
        assert_eq!(state.global_time.lock().unwrap().as_nanos(), total);

        fresh.advance_to(recreated.0.unwrap());
        assert_eq!(fresh.inherited_nanos(), LogicalTime::ZERO);
        let mut startup = Box::pin(state.receive_rpc(
            Tid::from_raw(leader.as_raw()),
            (
                fresh.clone(),
                old_mm.for_exec(detpid),
                GlobalRequest::StartNewThread(leader, detpid, None, None),
            ),
        ));
        assert!(futures::poll!(&mut startup).is_pending());
        assert!(futures::poll!(&mut startup).is_pending());
        let first = crate::scheduler::do_a_turn_blocking(
            state.sched.clone(),
            state.global_time.clone(),
            &Err(crate::scheduler::SkipTurn),
        )
        .await
        .expect("replacement leader must run");
        assert_eq!(first.tid, leader);
        assert_eq!(startup.await, (None, GlobalResponse::StartNewThread(None)));
        assert_eq!(state.global_time.lock().unwrap().as_nanos(), total);

        fresh.advance_to(fresh.as_nanos() + LogicalTime::from_nanos(1));
        let observed = state
            .receive_rpc(
                Tid::from_raw(leader.as_raw()),
                (
                    fresh.clone(),
                    old_mm.for_exec(detpid),
                    GlobalRequest::GlobalTimeLowerBound,
                ),
            )
            .await;
        assert_eq!(
            observed,
            (
                None,
                GlobalResponse::GlobalTimeLowerBound(total + LogicalTime::from_nanos(1))
            )
        );
        let global = state.global_time.lock().unwrap();
        assert_eq!(global.threads_time(leader), fresh.as_nanos());
        assert!(!global.contains_thread(worker));
    }

    #[test]
    fn live_registration_without_next_turn_is_not_terminal() {
        let (_, state, dettid, detpid) = cancellation_test_state();
        install_test_registration(&state, dettid, Ivar::new());
        state.sched.lock().unwrap().next_turns.remove(&dettid);

        assert!(
            !state
                .sched
                .lock()
                .unwrap()
                .thread_is_logically_killed(dettid),
            "transient next-turn absence must not imply logical death"
        );

        state
            .sched
            .lock()
            .unwrap()
            .logically_kill_thread(&dettid, &detpid, MmId::initial(detpid));
        assert!(
            state
                .sched
                .lock()
                .unwrap()
                .thread_is_logically_killed(dettid),
            "explicit logical death must install a permanent TID tombstone"
        );
    }

    #[tokio::test]
    async fn exec_reconnect_retires_siblings_and_reuses_live_scheduler_and_clock_state() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        let old_mm = MmId::initial(detpid).for_exec(detpid);
        install_test_registration(&state, dettid, Ivar::new());
        let sibling = DetTid::from_raw(dettid.as_raw() + 1);
        let sibling_request = Ivar::new();
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(dettid, sibling, false);
        install_test_registration(&state, sibling, sibling_request.clone());
        {
            let mut scheduler = state.sched.lock().unwrap();
            scheduler.next_turns.get_mut(&dettid).unwrap().resp =
                Ivar::full(SchedResponse::Go(None));
            scheduler
                .next_turns
                .get_mut(&sibling)
                .unwrap()
                .child_tid_addr = 0x1234;
        }
        let mut existing_time = DetTime::new(&config);
        existing_time.add_syscall();
        existing_time.add_syscall();
        state.global_time.lock().unwrap().update_global_time(
            dettid,
            existing_time.as_nanos(),
            LogicalTime::ZERO,
        );
        let (global_before, thread_before) = {
            let global_time = state.global_time.lock().unwrap();
            (global_time.as_nanos(), global_time.threads_time(dettid))
        };
        let fresh_local_time = DetTime::new(&config);
        let physical_pid = std::process::id() as i32;
        let physical_tid = unsafe { libc::syscall(libc::SYS_gettid) as i32 };
        let physical_ids = Some((physical_pid, physical_tid));
        state.pending_exec_states.lock().unwrap().insert(
            detpid,
            PendingExecState {
                caller: dettid,
                process: detpid,
                mm: old_mm,
                fd_blocking: Default::default(),
            },
        );
        state
            .sched
            .lock()
            .unwrap()
            .install_test_exec_incarnation(dettid, old_mm);
        let in_flight_exec_response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    existing_time.clone(),
                    old_mm.for_exec(detpid),
                    GlobalRequest::ReportUnsupportedSyscall("exec-in-flight".to_owned()),
                ),
            )
            .await;
        assert_eq!(
            in_flight_exec_response.1,
            GlobalResponse::ReportUnsupportedSyscall(())
        );

        let create_response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    fresh_local_time.clone(),
                    MmId::initial(dettid),
                    GlobalRequest::CreateChildThread(
                        dettid,
                        detpid,
                        0,
                        None,
                        libc::SIGCHLD,
                        physical_ids,
                        Some(DEFAULT_PRIORITY),
                    ),
                ),
            )
            .await;
        assert_eq!(
            create_response,
            (
                Some(thread_before),
                GlobalResponse::CreateChildThread(Some(old_mm.for_exec(detpid)))
            )
        );
        assert_eq!(
            state.sched.lock().unwrap().physical_thread_identity(dettid),
            Some((old_mm.for_exec(detpid), physical_pid, physical_tid)),
            "post-exec host identity must be installed by CreateChildThread before admission"
        );

        let start_response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    fresh_local_time,
                    old_mm.for_exec(detpid),
                    GlobalRequest::StartNewThread(dettid, detpid, physical_ids, None),
                ),
            )
            .await;
        assert_eq!(
            start_response,
            (Some(thread_before), GlobalResponse::StartNewThread(None))
        );

        let scheduler = state.sched.lock().unwrap();
        assert!(!scheduler.thread_is_logically_killed(dettid));
        assert!(scheduler.thread_is_logically_killed(sibling));
        assert_eq!(scheduler.next_turns.len(), 1);
        assert!(matches!(sibling_request.try_read(), Some(Err(_))));
        assert!(
            scheduler.child_tid_was_cleared(FutexID::private(old_mm, 0x1234), sibling.as_raw())
        );
        drop(scheduler);
        let global_time = state.global_time.lock().unwrap();
        assert_eq!(global_time.as_nanos(), global_before);
        assert_eq!(global_time.threads_time(dettid), thread_before);
    }

    /// A waiter in another process, parked on a robust word the exec's caller
    /// owned, for the exec owner-death tests below.
    fn park_exec_robust_waiter(state: &GlobalState, futex: FutexID) -> DetTid {
        park_exec_robust_waiter_as(state, futex, 40)
    }

    /// [`park_exec_robust_waiter`] for the thread `raw`.
    fn park_exec_robust_waiter_as(state: &GlobalState, futex: FutexID, raw: i32) -> DetTid {
        let waiter = DetTid::from_raw(raw);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(waiter, waiter, true);
        install_test_registration(state, waiter, Ivar::new());
        let mut scheduler = state.sched.lock().unwrap();
        scheduler.sleep_futex_waiter(&waiter, futex, None, u32::MAX, None);
        assert!(scheduler.run_queue.remove_tid(waiter));
        waiter
    }

    /// On a backend whose new image reconnects with a fresh runtime (DBT),
    /// the owner-death wakes the exec's caller read before the native exec
    /// travel with the prepared exec, and the reconnect admits their waiters
    /// at the next deterministic drain
    /// (https://github.com/rrnewton/hermit/issues/4268).
    #[tokio::test]
    async fn exec_reconnect_wakes_the_robust_waiters_its_exec_marked() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        let old_mm = MmId::initial(detpid).for_exec(detpid);
        install_test_registration(&state, dettid, Ivar::new());
        let mut existing_time = DetTime::new(&config);
        existing_time.add_syscall();
        state.global_time.lock().unwrap().update_global_time(
            dettid,
            existing_time.as_nanos(),
            LogicalTime::ZERO,
        );
        let futex = FutexID::private(old_mm, 0x4000);
        let waiter = park_exec_robust_waiter(&state, futex);
        state.pending_exec_states.lock().unwrap().insert(
            detpid,
            PendingExecState {
                caller: dettid,
                process: detpid,
                mm: old_mm,
                fd_blocking: ExecFdState {
                    robust_wakes: vec![(dettid, futex)],
                    ..Default::default()
                },
            },
        );
        state
            .sched
            .lock()
            .unwrap()
            .install_test_exec_incarnation(dettid, old_mm);
        assert!(!state.sched.lock().unwrap().admission_pending(waiter));

        let create_response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    DetTime::new(&config),
                    MmId::initial(dettid),
                    GlobalRequest::CreateChildThread(
                        dettid,
                        detpid,
                        0,
                        None,
                        libc::SIGCHLD,
                        None,
                        Some(DEFAULT_PRIORITY),
                    ),
                ),
            )
            .await;
        assert_eq!(
            create_response.1,
            GlobalResponse::CreateChildThread(Some(old_mm.for_exec(detpid)))
        );
        assert!(state.sched.lock().unwrap().admission_pending(waiter));
        assert!(
            state
                .post_exec_fd_blocking
                .lock()
                .unwrap()
                .get(&dettid)
                .is_none_or(|carried| carried.robust_wakes.is_empty()),
            "the wakes are delivered once, at the reconnect"
        );
    }

    /// An exec that fails before its point of no return (here ENOENT, which
    /// takes CancelExec) marks no robust word, so its prepared wakes are
    /// dropped with the prepared exec and the waiter stays parked. An exec
    /// that ends its group without a reconnect delivers them instead
    /// (`a_terminal_exec_wakes_its_robust_waiters`).
    #[tokio::test]
    async fn a_failed_exec_drops_its_robust_wakes() {
        let (config, state, leader, detpid) = cancellation_test_state();
        install_test_registration(&state, leader, Ivar::new());
        let futex = FutexID::private(MmId::initial(detpid), 0x4000);
        let waiter = park_exec_robust_waiter(&state, futex);
        let clock = DetTime::new(&config);
        let prepared = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    clock.clone(),
                    MmId::initial(leader),
                    GlobalRequest::PrepareExec(
                        detpid,
                        MmId::initial(detpid),
                        Box::new(ExecFdState {
                            robust_wakes: vec![(leader, futex)],
                            ..Default::default()
                        }),
                    ),
                ),
            )
            .await;
        assert_eq!(prepared.1, GlobalResponse::PrepareExec(()));
        let cancelled = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    clock,
                    MmId::initial(leader),
                    GlobalRequest::CancelExec(detpid),
                ),
            )
            .await;
        assert_eq!(cancelled.1, GlobalResponse::CancelExec(()));
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        assert!(!state.sched.lock().unwrap().admission_pending(waiter));
    }

    /// An exec caller whose old image deregisters before any reconnect (its
    /// group killed during the exec) still had Linux walk the group's robust
    /// lists. The deregistration delivers the prepared wakes and holds turns
    /// until the kernel has ended the group, instead of dropping them with
    /// the preparation (Codex review of
    /// https://github.com/rrnewton/hermit/pull/4352, P2). This builds the
    /// deregistration directly: no guest action is known to reach it on DBT
    /// (an exec whose target cannot be loaded reconnects first, then hangs
    /// the run: <https://github.com/rrnewton/hermit/issues/4362>).
    #[tokio::test]
    async fn a_terminal_exec_wakes_its_robust_waiters() {
        let (config, state, leader, detpid) = cancellation_test_state();
        install_test_registration(&state, leader, Ivar::new());
        let (tid_tx, tid_rx) = std::sync::mpsc::channel();
        let (exit_tx, exit_rx) = std::sync::mpsc::channel::<()>();
        let host_thread = std::thread::spawn(move || {
            tid_tx
                .send(unsafe { libc::syscall(libc::SYS_gettid) as i32 })
                .unwrap();
            let _ = exit_rx.recv();
        });
        state
            .sched
            .lock()
            .unwrap()
            .register_physical_thread(
                leader,
                MmId::initial(detpid),
                std::process::id() as i32,
                tid_rx.recv().unwrap(),
            )
            .unwrap();
        let futex = FutexID::private(MmId::initial(detpid), 0x4000);
        let waiter = park_exec_robust_waiter(&state, futex);
        // A second waiter on the same word stays parked: one wake per marked
        // word (Claude re-check of
        // https://github.com/rrnewton/hermit/pull/4352, N3).
        let second = park_exec_robust_waiter_as(&state, futex, 41);
        let prepared = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    DetTime::new(&config),
                    MmId::initial(leader),
                    GlobalRequest::PrepareExec(
                        detpid,
                        MmId::initial(detpid),
                        Box::new(ExecFdState {
                            robust_wakes: vec![(leader, futex)],
                            ..Default::default()
                        }),
                    ),
                ),
            )
            .await;
        assert_eq!(prepared.1, GlobalResponse::PrepareExec(()));
        assert!(!state.sched.lock().unwrap().admission_pending(waiter));

        state
            .recv_deregister_thread(
                reverie::Tid::from_raw(leader.as_raw()),
                ThreadDeregistration {
                    thread_start_entered: true,
                    dettid: leader,
                    detpid,
                    mm: MmId::initial(detpid),
                    timeslice_stats: TimesliceStats::default(),
                    syscall_count: 0,
                    chaos_epochs: Vec::new(),
                },
            )
            .await;
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        {
            let scheduler = state.sched.lock().unwrap();
            assert!(scheduler.admission_pending(waiter));
            assert!(!scheduler.admission_pending(second));
            assert!(scheduler.holds_turns_until_gone(leader));
        }
        exit_tx.send(()).unwrap();
        host_thread.join().unwrap();
    }

    #[tokio::test]
    async fn nonleader_exec_rebinds_caller_to_leader_and_preserves_its_clock() {
        let (config, state, leader, detpid) = cancellation_test_state();
        let worker = DetTid::from_raw(leader.as_raw() + 1);
        let sibling = DetTid::from_raw(leader.as_raw() + 2);
        let old_mm = MmId::initial(detpid).for_exec(detpid);
        let leader_request = Ivar::new();
        let worker_request = Ivar::new();
        let sibling_request = Ivar::new();
        install_test_registration(&state, leader, leader_request.clone());
        {
            let mut scheduler = state.sched.lock().unwrap();
            scheduler.thread_tree.add_child(leader, worker, false);
            scheduler.thread_tree.add_child(leader, sibling, false);
        }
        install_test_registration(&state, worker, worker_request.clone());
        install_test_registration(&state, sibling, sibling_request.clone());
        {
            let mut scheduler = state.sched.lock().unwrap();
            scheduler
                .next_turns
                .get_mut(&sibling)
                .unwrap()
                .child_tid_addr = 0x5678;
            scheduler
                .timeslices
                .insert(leader, Some(LogicalTime::from_nanos(99)));
            scheduler.install_test_vfork_barrier(leader, sibling);
        }

        let mut leader_clock = DetTime::new(&config);
        leader_clock.add_syscall();
        let mut worker_clock = DetTime::new(&config);
        worker_clock.add_syscall();
        worker_clock.add_syscall();
        worker_clock.add_syscall();
        {
            let mut global_time = state.global_time.lock().unwrap();
            global_time.update_global_time(leader, leader_clock.as_nanos(), LogicalTime::ZERO);
            global_time.update_global_time(worker, worker_clock.as_nanos(), LogicalTime::ZERO);
        }
        let total_before = state.global_time.lock().unwrap().as_nanos();
        let fd_blocking = ExecFdState {
            blocking: [42].into_iter().collect(),
            inherited_reads: std::collections::BTreeMap::from([(
                43,
                crate::fd::InheritedExternalRead {
                    origin: crate::types::OpenFileId::new(leader, 0),
                    dev: 1,
                    inode: 2,
                    rdev: 3,
                    file_type: libc::S_IFCHR,
                    access_mode: libc::O_RDONLY,
                    resource: Some(ResourceID::Device(crate::resources::Device::ContainerStdin)),
                },
            )]),
            initialize_inherited_stdio: false,
            initial_input: None,
            robust_wakes: Vec::new(),
            open_files: None,
            maps: None,
            channels: Default::default(),
            open_file_namespace: None,
            exe: None,
        };
        state.pending_exec_states.lock().unwrap().insert(
            detpid,
            PendingExecState {
                caller: worker,
                process: detpid,
                mm: old_mm,
                fd_blocking: fd_blocking.clone(),
            },
        );
        let fresh_local_time = DetTime::new(&config);

        let create_response = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    fresh_local_time.clone(),
                    MmId::initial(leader),
                    GlobalRequest::CreateChildThread(
                        leader,
                        detpid,
                        0,
                        None,
                        libc::SIGCHLD,
                        None,
                        Some(DEFAULT_PRIORITY),
                    ),
                ),
            )
            .await;
        assert_eq!(
            create_response,
            (
                Some(worker_clock.as_nanos()),
                GlobalResponse::CreateChildThread(Some(old_mm.for_exec(detpid)))
            )
        );
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        assert_eq!(
            state.post_exec_fd_blocking.lock().unwrap().get(&leader),
            Some(&fd_blocking)
        );

        let late_old_request = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    leader_clock.clone(),
                    old_mm,
                    GlobalRequest::RequestResources(Resources::new(leader), detpid),
                ),
            )
            .await;
        assert_eq!(late_old_request, (None, GlobalResponse::ThreadExited));
        let admitted_before_fence = state
            .recv_request_resources(
                reverie::Tid::from_raw(leader.as_raw()),
                detpid,
                Resources::new(leader),
                Some(old_mm),
            )
            .await;
        assert_eq!(
            admitted_before_fence,
            (SchedulerRpcResult::ThreadExited, None)
        );
        let late_old_deregister = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    leader_clock.clone(),
                    old_mm,
                    GlobalRequest::DeregisterThread(ThreadDeregistration {
                        thread_start_entered: true,
                        dettid: leader,
                        detpid,
                        mm: old_mm,
                        timeslice_stats: TimesliceStats::default(),
                        syscall_count: 0,
                        chaos_epochs: Vec::new(),
                    }),
                ),
            )
            .await;
        assert_eq!(
            late_old_deregister,
            (None, GlobalResponse::DeregisterThread(()))
        );
        let duplicate_create = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    fresh_local_time.clone(),
                    MmId::initial(leader),
                    GlobalRequest::CreateChildThread(
                        leader,
                        detpid,
                        0,
                        None,
                        libc::SIGCHLD,
                        None,
                        Some(DEFAULT_PRIORITY),
                    ),
                ),
            )
            .await;
        assert_eq!(duplicate_create, (None, GlobalResponse::ThreadExited));

        {
            let scheduler = state.sched.lock().unwrap();
            assert!(!scheduler.thread_is_logically_killed(leader));
            assert!(scheduler.thread_is_logically_killed(worker));
            assert!(scheduler.thread_is_logically_killed(sibling));
            assert_eq!(scheduler.next_turns.len(), 1);
            assert!(scheduler.next_turns.contains_key(&leader));
            assert!(!scheduler.timeslices.contains_key(&leader));
            assert!(!scheduler.vfork_barrier_mentions(leader));
            assert!(!scheduler.vfork_barrier_mentions(sibling));
            assert!(matches!(leader_request.try_read(), Some(Err(_))));
            assert!(matches!(worker_request.try_read(), Some(Err(_))));
            assert!(matches!(sibling_request.try_read(), Some(Err(_))));
            assert!(
                scheduler
                    .child_tid_was_cleared(FutexID::private(old_mm, 0x5678), sibling.as_raw(),)
            );
        }

        // Drive the real daemon through step2 rather than manually pre-filling
        // the replacement's response. This drains the destroyed leader's
        // physical removal and the same-raw-TID replacement admission before
        // StartNewThread supplies the fresh image's first request.
        let turn_sched = state.sched.clone();
        let turn_time = state.global_time.clone();
        let turn = tokio::spawn(async move {
            let last: Result<Resources, crate::scheduler::SkipTurn> =
                Err(crate::scheduler::SkipTurn);
            crate::scheduler::do_a_turn_blocking(turn_sched, turn_time, &last).await
        });
        let start_response = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    fresh_local_time,
                    old_mm.for_exec(detpid),
                    GlobalRequest::StartNewThread(leader, detpid, None, None),
                ),
            )
            .await;
        assert!(
            turn.await
                .expect("replacement scheduler turn panicked")
                .is_ok(),
            "replacement leader did not survive the first step2 drain"
        );
        assert_eq!(
            start_response,
            (
                Some(worker_clock.as_nanos()),
                GlobalResponse::StartNewThread(None)
            )
        );
        {
            let scheduler = state.sched.lock().unwrap();
            assert_eq!(
                scheduler
                    .run_queue
                    .tids()
                    .filter(|dettid| **dettid == leader)
                    .count(),
                1
            );
            assert!(!scheduler.run_queue.contains_tid(worker));
            assert!(!scheduler.run_queue.contains_tid(sibling));
        }
        {
            let global_time = state.global_time.lock().unwrap();
            assert_eq!(global_time.as_nanos(), total_before);
            assert_eq!(global_time.threads_time(leader), worker_clock.as_nanos());
            assert!(!global_time.contains_thread(worker));
        }

        let mark_response = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    worker_clock,
                    old_mm.for_exec(detpid),
                    GlobalRequest::MarkPastFirstExecve(detpid, None, None, None),
                ),
            )
            .await;
        assert_eq!(
            mark_response.1,
            GlobalResponse::MarkPastFirstExecve(Box::new(fd_blocking))
        );
        assert!(state.post_exec_fd_blocking.lock().unwrap().is_empty());
    }

    async fn start_external_image(
        tool: &Detcore,
        guest: &mut ExternalRegistrationGuest<'_>,
        expected_grants: &[ResourceID],
    ) {
        use reverie::Tool;

        let state = guest.global;
        let tid = guest.thread.dettid;
        tokio::time::timeout(Duration::from_secs(2), async {
            let mut start = std::pin::pin!(tool.handle_thread_start(guest));
            assert!(futures::poll!(start.as_mut()).is_pending());
            // Initial self-registration requests ParentContinue before
            // StartNewThread. Reload registration has no parent turn.
            // Drive these actual grants, without fabricating responses.
            let grants = async {
                let mut last = Err(crate::scheduler::SkipTurn);
                for expected in expected_grants {
                    let granted = crate::scheduler::do_a_turn_blocking(
                        state.sched.clone(),
                        state.global_time.clone(),
                        &last,
                    )
                    .await
                    .expect("image admission must receive its scheduler grant");
                    assert_eq!(granted.tid, tid);
                    assert!(granted.resources.contains_key(expected));
                    last = Ok(granted);
                }
            };
            let (started, ()) = tokio::join!(start, grants);
            started.unwrap();
        })
        .await
        .expect("image admission must finish without guest instructions");
    }

    /// A reloading backend supplies a real in-tree parent for a fresh image.
    /// That image must reconnect its prepared exec before ordinary requests can
    /// publish the replacement native memory owner.
    #[tokio::test]
    async fn a_reloaded_descendant_reconnects_before_publishing_its_new_memory_owner() {
        use reverie::Tool;

        let parent = super::ROOT_DETPID;
        let child = DetTid::from_raw(17);
        for (case, old_mm) in [
            ("first exec", MmId::initial(child)),
            ("later exec", MmId::initial(child).for_exec(child)),
        ] {
            let config = Config {
                sequentialize_threads: true,
                max_timeslice: None,
                no_rcb_time: true,
                ..Config::default()
            }
            .with_backend(|backend| backend.needs_killed_thread_rpc_cancellation = true);
            let state = GlobalState::initialize(&config, false);
            let facts = |epoch| {
                reverie::RuntimePrivateMemory::Ready(reverie::RuntimePrivateMemorySnapshot {
                    owner: [1, 2, 3, epoch],
                    version: 1,
                    regions: vec![reverie::RuntimePrivateMemoryRegion {
                        start: 0x1000,
                        end: 0x2000,
                    }],
                })
            };
            // Seed the already registered descendant's old image. The exec and
            // replacement-image admission below use actual global RPCs and grants.
            {
                let mut sched = state.sched.lock().unwrap();
                sched.thread_tree.add_child(parent, parent, true);
                sched.thread_tree.add_child(parent, child, true);
                assert_eq!(sched.thread_tree.process_of(child), Some(child), "{case}");
                assert_eq!(
                    sched.thread_tree.parent_process(&child),
                    Some(parent),
                    "{case}"
                );
                sched.record_thread_mm(child, old_mm);
                if old_mm != MmId::initial(child) {
                    sched.install_test_exec_incarnation(child, old_mm);
                }
            }
            install_test_registration(&state, child, Ivar::new());
            let tool = Detcore::new(reverie::Pid::from_raw(child.as_raw()), &config);
            let mut old = ExternalRegistrationGuest {
                global: &state,
                config: &config,
                thread: tool.init_thread_state(Tid::from_raw(child.as_raw()), None),
                requests: Mutex::new(Vec::new()),
                post_exec: false,
                ppid: Some(reverie::Pid::from_raw(parent.as_raw())),
                private_memory: Some(facts(1)),
            };
            old.thread.detpid = Some(child);
            old.thread.mm_id = old_mm;
            old.thread.past_global_first_execve = true;
            start_external_image(&tool, &mut old, &[ResourceID::MemAddrSpace(child)]).await;
            super::send_and_update_time(&mut old, GlobalRequest::GlobalTimeLowerBound).await;
            assert_eq!(
                state.sched.lock().unwrap().runtime_private_memory(old_mm),
                Some(facts(1)),
                "{case}"
            );
            super::prepare_exec(&mut old, old_mm, Default::default(), Vec::new(), None).await;
            assert!(
                state
                    .pending_exec_states
                    .lock()
                    .unwrap()
                    .contains_key(&child),
                "{case}"
            );
            // Assert the old image's authority before the successor's first
            // request. The obsolete key need not remain Ready after exec.
            assert_eq!(
                state.sched.lock().unwrap().runtime_private_memory(old_mm),
                Some(facts(1)),
                "{case}: prepared old image"
            );
            let registered_children = state
                .sched
                .lock()
                .unwrap()
                .thread_tree
                .get_children(&parent)
                .clone();
            drop(old);

            let mut successor = ExternalRegistrationGuest {
                global: &state,
                config: &config,
                thread: tool.init_thread_state(Tid::from_raw(child.as_raw()), None),
                requests: Mutex::new(Vec::new()),
                post_exec: false,
                ppid: Some(reverie::Pid::from_raw(parent.as_raw())),
                private_memory: Some(facts(2)),
            };
            successor.thread.detpid = Some(child);
            assert!(!successor.is_root_process(), "{case}");
            assert!(!successor.thread.past_global_first_execve, "{case}");
            start_external_image(&tool, &mut successor, &[ResourceID::MemAddrSpace(child)]).await;
            let new_mm = old_mm.for_exec(child);
            assert_eq!(successor.thread.mm_id, new_mm, "{case}");
            assert!(
                state.pending_exec_states.lock().unwrap().is_empty(),
                "{case}"
            );
            super::send_and_update_time(&mut successor, GlobalRequest::GlobalTimeLowerBound).await;
            let sched = state.sched.lock().unwrap();
            assert_eq!(sched.thread_mm(child), Some(new_mm), "{case}");
            assert_eq!(
                sched.thread_tree.get_children(&parent),
                &registered_children,
                "{case}: exec is not a new child birth"
            );
            assert_eq!(sched.thread_tree.process_of(child), Some(child), "{case}");
            assert_eq!(
                sched.thread_tree.parent_process(&child),
                Some(parent),
                "{case}"
            );
            assert_eq!(
                sched.runtime_private_memory(new_mm),
                Some(facts(2)),
                "{case}"
            );
            let requests = successor.requests.lock().unwrap();
            assert_eq!(
                requests
                    .iter()
                    .filter(|request| matches!(request, GlobalRequest::CreateChildThread(..)))
                    .count(),
                1,
                "{case}"
            );
        }
    }

    #[tokio::test]
    async fn reloaded_post_exec_restores_prepared_input_aliases_once() {
        use nix::fcntl::OFlag;
        use reverie::Tool;

        let config = Config {
            sequentialize_threads: true,
            max_timeslice: None,
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.needs_killed_thread_rpc_cancellation = true;
            backend.tool_shares_guest_descriptor_table = true;
        });
        let mut state = GlobalState::initialize(&config, false);
        let leader = super::ROOT_DETPID;
        let tool = Detcore::new(reverie::Pid::from_raw(leader.as_raw()), &config);

        // Use the real launch-input owner and its registered-root seed, in a
        // privately owned slot rather than the test harness's stdin.
        let reader: OwnedFd = std::fs::File::open("/dev/null").unwrap().into();
        let fd = reader.as_raw_fd();
        let input = reverie::InheritedInput::pipe(fd).unwrap();
        let identity = input.identity().clone();
        state.install_inherited_input(input.lease().unwrap());
        let (source, _writer) = input.into_reader_and_writer();
        // SAFETY: dup2 replaces only this test's owned destination.
        assert_eq!(unsafe { libc::dup2(source.as_raw_fd(), fd) }, fd);
        drop(source);
        let alias = reader.try_clone().unwrap();
        let cloexec = reader.try_clone().unwrap();
        let alias_fd = alias.as_raw_fd();
        let cloexec_fd = cloexec.as_raw_fd();
        // SAFETY: only the test-owned alias's descriptor flag is changed.
        assert_eq!(unsafe { libc::fcntl(alias_fd, libc::F_SETFD, 0) }, 0);
        let flags = |fd| {
            // SAFETY: both no-argument queries inspect owned descriptors.
            let result = unsafe {
                (
                    libc::fcntl(fd, libc::F_GETFL),
                    libc::fcntl(fd, libc::F_GETFD),
                )
            };
            assert!(result.0 >= 0 && result.1 >= 0);
            result
        };
        let kernel_before = [flags(fd), flags(alias_fd)];
        assert_eq!(
            kernel_before[0].0 & (libc::O_ACCMODE | libc::O_NONBLOCK),
            libc::O_RDONLY
        );
        assert_eq!(flags(cloexec_fd).1 & libc::FD_CLOEXEC, libc::FD_CLOEXEC);

        let mut old = ExternalRegistrationGuest {
            global: &state,
            config: &config,
            thread: tool.init_thread_state(Tid::from_raw(leader.as_raw()), None),
            requests: Mutex::new(Vec::new()),
            post_exec: true,
            ppid: None,
            private_memory: None,
        };
        old.thread.detpid = Some(leader);
        start_external_image(
            &tool,
            &mut old,
            &[
                ResourceID::ParentContinue {
                    parent: leader,
                    child: leader,
                },
                ResourceID::MemAddrSpace(leader),
            ],
        )
        .await;
        tool.handle_post_exec(&mut old).await.unwrap();
        assert!(state.inherited_input.lock().unwrap().is_none());
        old.thread.dup_fd(fd, alias_fd, OFlag::empty()).unwrap();
        old.thread.dup_fd(fd, cloexec_fd, OFlag::O_CLOEXEC).unwrap();
        let origin = old.thread.file_metadata.lock().unwrap().file_handles[&fd]
            .inherited_external_read()
            .expect("the actual registered launch seed must establish read provenance");
        assert_eq!(
            (origin.dev, origin.inode, origin.rdev),
            (identity.dev(), identity.inode(), identity.rdev())
        );
        let old_mm = old.thread.mm_id;
        super::prepare_exec(&mut old, old_mm, Default::default(), Vec::new(), None).await;
        {
            let pending = state.pending_exec_states.lock().unwrap();
            let prepared = &pending[&leader].fd_blocking.inherited_reads;
            assert_eq!(
                prepared.keys().copied().collect::<BTreeSet<_>>(),
                BTreeSet::from([fd, alias_fd])
            );
            assert!(prepared.values().all(|entry| entry == &origin));
            assert!(!prepared.contains_key(&cloexec_fd));
        }
        // Model the physical successful-exec boundary: the old Tool state is
        // gone and Linux has closed the CLOEXEC alias. No native exec is claimed.
        drop(old);
        drop(cloexec);
        let mut successor = ExternalRegistrationGuest {
            global: &state,
            config: &config,
            thread: tool.init_thread_state(Tid::from_raw(leader.as_raw()), None),
            requests: Mutex::new(Vec::new()),
            post_exec: true,
            ppid: None,
            private_memory: None,
        };
        successor.thread.detpid = Some(leader);
        start_external_image(&tool, &mut successor, &[ResourceID::MemAddrSpace(leader)]).await;
        assert_eq!(successor.thread.mm_id, old_mm.for_exec(leader));
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        assert_eq!(
            state.post_exec_fd_blocking.lock().unwrap()[&leader]
                .inherited_reads
                .len(),
            2
        );
        assert!(
            !successor
                .thread
                .file_metadata
                .lock()
                .unwrap()
                .file_handles
                .contains_key(&fd)
        );

        // This is the actual Tool callback, not a raw completion-RPC check or
        // a direct metadata restore. Empty auxv deliberately excludes AT_RANDOM.
        tool.handle_post_exec(&mut successor).await.unwrap();
        assert!(state.post_exec_fd_blocking.lock().unwrap().is_empty());
        {
            let metadata = successor.thread.file_metadata.lock().unwrap();
            for slot in [fd, alias_fd] {
                let entry = metadata
                    .file_handles
                    .get(&slot)
                    .expect("post-exec must restore every prepared surviving input alias");
                assert_eq!(
                    entry.inherited_external_read(),
                    Some(origin.clone()),
                    "post-exec must restore transferred origin, not plain pipe discovery"
                );
                assert_eq!(entry.ty(), crate::fd::FdType::Pipe);
                assert!(!entry.is_nonblocking());
                assert!(!entry.physically_nonblocking());
                assert!(!entry.is_cloexec());
                assert_eq!(entry.resource(), origin.resource);
            }
            assert_eq!(
                metadata.file_handles[&fd].open_file_id(),
                metadata.file_handles[&alias_fd].open_file_id()
            );
            assert_eq!(metadata.file_handles[&fd].open_file_alias_count(), 2);
            assert!(!metadata.file_handles.contains_key(&cloexec_fd));
        }
        assert_eq!([flags(fd), flags(alias_fd)], kernel_before);
        let snapshot =
            || serde_json::to_value(&*successor.thread.file_metadata.lock().unwrap()).unwrap();
        let before_rpc = snapshot();
        let response = successor
            .send_rpc((
                successor.thread.thread_logical_time.clone(),
                successor.thread.mm_id,
                GlobalRequest::GlobalTimeLowerBound,
            ))
            .await;
        assert!(matches!(
            response.1,
            GlobalResponse::GlobalTimeLowerBound(_)
        ));
        assert_eq!(
            snapshot(),
            before_rpc,
            "ordinary admitted RPC must leave restored metadata unchanged"
        );
        assert!(state.post_exec_fd_blocking.lock().unwrap().is_empty());
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        let requests = successor.requests.lock().unwrap();
        assert_eq!(
            requests
                .iter()
                .filter(|r| matches!(r, GlobalRequest::MarkPastFirstExecve(..)))
                .count(),
            1
        );
        assert!(matches!(
            requests.first(),
            Some(GlobalRequest::CreateChildThread(..))
        ));
        assert!(matches!(
            requests.get(1),
            Some(GlobalRequest::StartNewThread(..))
        ));
        assert!(matches!(
            requests.last(),
            Some(GlobalRequest::GlobalTimeLowerBound)
        ));
    }

    #[tokio::test]
    async fn post_exec_deletes_armed_disarmed_and_non_notifying_timers() {
        use reverie::Tool;

        let config = Config {
            sequentialize_threads: true,
            // This test enters one callback while the caller owns its turn;
            // no guest instructions or PMU timer are needed to drive it.
            max_timeslice: None,
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.needs_killed_thread_rpc_cancellation = true;
        });
        let state = GlobalState::initialize(&config, false);
        let leader = DetTid::from_raw(17);
        let detpid = DetPid::from_raw(17);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(leader, leader, true);
        let next_request = Ivar::new();
        install_test_registration(&state, leader, next_request.clone());
        let tool = Detcore::new(reverie::Pid::from_raw(leader.as_raw()), &config);
        let mut thread = tool.init_thread_state(Tid::from_raw(leader.as_raw()), None);
        thread.detpid = Some(detpid);
        // The exec's own syscall, intercepted and counted, as a backend that
        // traces the guest counts it before post-exec. (A backend that starts
        // the guest already executing reaches its first post-exec with nothing
        // counted, and post-exec charges the execve itself.)
        thread.stats.count_syscall();
        thread.thread_logical_time.add_syscall_with_cost(137);
        let mut guest = ExternalRegistrationGuest {
            global: &state,
            config: &config,
            thread,
            requests: Mutex::new(Vec::new()),
            post_exec: true,
            ppid: None,
            private_memory: None,
        };
        let t = LogicalTime::from_nanos;
        let [periodic, disarmed, silent] = {
            let mut timers = guest.thread.posix_timers.lock().unwrap();
            let periodic = timers.create(Some(libc::SIGUSR2), None, None);
            let disarmed = timers.create(Some(libc::SIGALRM), None, None);
            let silent = timers.create(None, None, None);
            timers.settime(periodic, 50, Some(t(100)), t(0));
            timers.settime(silent, 0, Some(t(200)), t(0));
            [periodic, disarmed, silent]
        };

        // Drive the production callback, including its actual global RPCs.
        // Removing its clear_for_exec call must leave these IDs live and fail.
        // A second successful exec also exercises deletion of an empty table.
        for _ in 0..2 {
            let old_mm = guest.thread.mm_id;
            super::prepare_exec(&mut guest, old_mm, Default::default(), Vec::new(), None).await;
            guest.thread.mm_id = old_mm.for_exec(detpid);
            let before = guest.thread.thread_logical_time.as_nanos();
            tokio::time::timeout(Duration::from_secs(2), tool.handle_post_exec(&mut guest))
                .await
                .expect("post-exec must finish within the caller's scheduler turn")
                .unwrap();
            assert_eq!(guest.thread.thread_logical_time.as_nanos(), before);
            assert!(
                next_request.try_read().is_none(),
                "post-exec unexpectedly yielded"
            );
            let mut timers = guest.thread.posix_timers.lock().unwrap();
            for id in [periodic, disarmed, silent] {
                assert!(!timers.contains(id), "post-exec retained POSIX timer {id}");
                assert_eq!(timers.gettime(id, t(10)), None);
                assert_eq!(timers.settime(id, 0, Some(t(300)), t(10)), None);
                assert_eq!(timers.signal(id), None);
                assert!(!timers.remove(id));
            }
        }
        let mut timers = guest.thread.posix_timers.lock().unwrap();
        let new = timers.create(Some(libc::SIGUSR1), None, None);
        assert_eq!(new, 3);
        assert_eq!(timers.settime(new, 0, Some(t(300)), t(10)), Some((0, 0)));
        assert_eq!(timers.gettime(new, t(20)), Some((280, 0)));
    }

    /// A state like [`cancellation_test_state`]'s for a run that declared
    /// `launcher` as the image the kernel execs in the guest's place.
    fn exec_record_test_state(
        sequentialize_threads: bool,
        launcher: &std::path::Path,
    ) -> (Config, GlobalState, DetTid, DetPid) {
        let config = Config {
            sequentialize_threads,
            launch_image: Some(crate::LaunchImage {
                exe: None,
                launcher: Some(launcher.to_path_buf()),
            }),
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let dettid = DetTid::from_raw(17);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(dettid, dettid, true);
        (config, state, dettid, DetPid::from_raw(17))
    }

    /// An exec's record stands only where the kernel's link, as the new
    /// image read it, names the recorded file or the launcher the run
    /// declared; it is what other processes then read for the process, and a
    /// dropped record leaves them nothing. Without sequentialized threads no
    /// record is kept.
    #[tokio::test]
    #[allow(clippy::disallowed_methods)] // std::fs::canonicalize is the independent oracle
    async fn an_exec_record_stands_only_where_the_kernel_names_it_or_the_launcher() {
        use std::os::unix::fs::MetadataExt;
        let directory = tempfile::tempdir().unwrap();
        let directory = std::fs::canonicalize(directory.path()).unwrap();
        let (image, launcher, other) = (
            directory.join("image"),
            directory.join("launcher"),
            directory.join("other"),
        );
        for file in [&image, &launcher, &other] {
            std::fs::write(file, file.as_os_str().as_encoded_bytes()).unwrap();
        }
        let identity = |path: &std::path::Path| {
            let meta = std::fs::metadata(path).unwrap();
            (meta.dev(), meta.ino())
        };
        let record = crate::exec_record::ExecRecord::at(std::path::Path::new("/"), &image).unwrap();
        for (sequentialize, kernel, kept) in [
            (true, identity(&image), true),
            (true, identity(&launcher), true),
            (true, identity(&other), false),
            (false, identity(&image), false),
        ] {
            let (config, state, leader, detpid) = exec_record_test_state(sequentialize, &launcher);
            install_test_registration(&state, leader, Ivar::new());
            let clock = DetTime::new(&config);
            let mm = MmId::initial(detpid);
            let tid = reverie::Tid::from_raw(leader.as_raw());
            let prepared = ExecFdState {
                exe: Some(record.clone()),
                ..Default::default()
            };
            let response = state
                .receive_rpc(
                    tid,
                    (
                        clock.clone(),
                        mm,
                        GlobalRequest::PrepareExec(detpid, mm, Box::new(prepared)),
                    ),
                )
                .await;
            assert_eq!(response.1, GlobalResponse::PrepareExec(()));
            let response = state
                .receive_rpc(
                    tid,
                    (
                        clock,
                        mm.for_exec(detpid),
                        GlobalRequest::MarkPastFirstExecve(detpid, None, None, Some(kernel)),
                    ),
                )
                .await;
            let GlobalResponse::MarkPastFirstExecve(overrides) = response.1 else {
                panic!("unexpected response {:?}", response.1);
            };
            let expected = kept.then(|| record.clone());
            assert_eq!(overrides.exe, expected, "{sequentialize} {kernel:?}");
            assert_eq!(
                state.exec_images.lock().unwrap().get(&detpid).cloned(),
                expected,
                "{sequentialize} {kernel:?}"
            );
        }
    }

    /// The launch's first image goes to the registered root's first
    /// completed image only: another thread's completion, first or not,
    /// gets none, and the root's later images get none from the launch. The
    /// root's record stands without sequentialized threads too, as it comes
    /// from no preparation.
    #[tokio::test]
    #[allow(clippy::disallowed_methods)] // std::fs::canonicalize is the independent oracle
    async fn the_launch_image_goes_to_the_registered_roots_first_image_only() {
        let directory = tempfile::tempdir().unwrap();
        let directory = std::fs::canonicalize(directory.path()).unwrap();
        let (image, launcher) = (directory.join("image"), directory.join("launcher"));
        for file in [&image, &launcher] {
            std::fs::write(file, file.as_os_str().as_encoded_bytes()).unwrap();
        }
        let record = crate::exec_record::ExecRecord::at(std::path::Path::new("/"), &image).unwrap();
        for sequentialize in [true, false] {
            let config = Config {
                sequentialize_threads: sequentialize,
                launch_image: Some(crate::LaunchImage {
                    exe: Some(image.clone()),
                    launcher: Some(launcher.clone()),
                }),
                ..Config::default()
            };
            let state = GlobalState::initialize(&config, false);
            let (root, other) = (DetTid::from_raw(17), DetTid::from_raw(23));
            for tid in [root, other] {
                state
                    .sched
                    .lock()
                    .unwrap()
                    .thread_tree
                    .add_child(tid, tid, true);
                install_test_registration(&state, tid, Ivar::new());
            }
            state.root_dettid.set(root).unwrap();
            let clock = DetTime::new(&config);
            let mark = |tid: DetTid| {
                let process = DetPid::from_raw(tid.as_raw());
                (
                    reverie::Tid::from_raw(tid.as_raw()),
                    (
                        clock.clone(),
                        MmId::initial(process),
                        GlobalRequest::MarkPastFirstExecve(process, None, None, None),
                    ),
                )
            };
            for (tid, expected) in [(other, None), (root, Some(&record)), (root, None)] {
                let (from, request) = mark(tid);
                let response = state.receive_rpc(from, request).await;
                let GlobalResponse::MarkPastFirstExecve(overrides) = response.1 else {
                    panic!("unexpected response {:?}", response.1);
                };
                assert_eq!(overrides.exe.as_ref(), expected, "{sequentialize} {tid:?}");
            }
        }
    }

    /// The record a new image resolved from its own `AT_EXECFN` (the
    /// unprepared one) fills in only where no exec was prepared, never
    /// outranks a prepared record, never stands for the launcher, and is
    /// dropped without sequentialized threads, as a prepared one is. For the
    /// registered root's first image it outranks the launch's own search,
    /// which stands where the image gave none or named the launcher; in a run
    /// that declares no launcher, the launch's record stands at the root
    /// whatever the image's exec prepared, as before.
    #[tokio::test]
    #[allow(clippy::disallowed_methods)] // std::fs::canonicalize is the independent oracle
    async fn an_images_own_record_fills_in_and_outranks_only_the_launchs_search() {
        let directory = tempfile::tempdir().unwrap();
        let directory = std::fs::canonicalize(directory.path()).unwrap();
        let (image, other, launcher) = (
            directory.join("image"),
            directory.join("other"),
            directory.join("launcher"),
        );
        for file in [&image, &other, &launcher] {
            std::fs::write(file, file.as_os_str().as_encoded_bytes()).unwrap();
        }
        let record_of = |path: &std::path::Path| {
            crate::exec_record::ExecRecord::at(std::path::Path::new("/"), path)
        };
        let (image_record, other_record, launcher_record) =
            (record_of(&image), record_of(&other), record_of(&launcher));

        // A process other than the root.
        for (sequentialize, prepared, unprepared, expected) in [
            (true, None, other_record.clone(), other_record.clone()),
            (
                true,
                image_record.clone(),
                other_record.clone(),
                image_record.clone(),
            ),
            (true, None, launcher_record.clone(), None),
            (false, None, other_record.clone(), None),
        ] {
            let (config, state, leader, detpid) = exec_record_test_state(sequentialize, &launcher);
            install_test_registration(&state, leader, Ivar::new());
            let clock = DetTime::new(&config);
            let mm = MmId::initial(detpid);
            let tid = reverie::Tid::from_raw(leader.as_raw());
            let preparation = ExecFdState {
                exe: prepared.clone(),
                ..Default::default()
            };
            let response = state
                .receive_rpc(
                    tid,
                    (
                        clock.clone(),
                        mm,
                        GlobalRequest::PrepareExec(detpid, mm, Box::new(preparation)),
                    ),
                )
                .await;
            assert_eq!(response.1, GlobalResponse::PrepareExec(()));
            let response = state
                .receive_rpc(
                    tid,
                    (
                        clock,
                        mm.for_exec(detpid),
                        GlobalRequest::MarkPastFirstExecve(detpid, None, unprepared.clone(), None),
                    ),
                )
                .await;
            let GlobalResponse::MarkPastFirstExecve(overrides) = response.1 else {
                panic!("unexpected response {:?}", response.1);
            };
            assert_eq!(
                overrides.exe, expected,
                "{sequentialize} prepared {prepared:?} unprepared {unprepared:?}"
            );
        }

        // The registered root's first image of a run that declares no
        // launcher (ptrace): the launch's record stands over the one its
        // bootstrap exec prepared.
        {
            let config = Config {
                sequentialize_threads: true,
                launch_image: Some(crate::LaunchImage {
                    exe: Some(image.clone()),
                    launcher: None,
                }),
                ..Config::default()
            };
            let state = GlobalState::initialize(&config, false);
            let root = DetTid::from_raw(17);
            state
                .sched
                .lock()
                .unwrap()
                .thread_tree
                .add_child(root, root, true);
            install_test_registration(&state, root, Ivar::new());
            state.root_dettid.set(root).unwrap();
            let process = DetPid::from_raw(root.as_raw());
            let tid = reverie::Tid::from_raw(root.as_raw());
            let clock = DetTime::new(&config);
            let mm = MmId::initial(process);
            let preparation = ExecFdState {
                exe: other_record.clone(),
                ..Default::default()
            };
            let response = state
                .receive_rpc(
                    tid,
                    (
                        clock.clone(),
                        mm,
                        GlobalRequest::PrepareExec(process, mm, Box::new(preparation)),
                    ),
                )
                .await;
            assert_eq!(response.1, GlobalResponse::PrepareExec(()));
            let response = state
                .receive_rpc(
                    tid,
                    (
                        clock,
                        mm.for_exec(process),
                        GlobalRequest::MarkPastFirstExecve(process, None, None, None),
                    ),
                )
                .await;
            let GlobalResponse::MarkPastFirstExecve(overrides) = response.1 else {
                panic!("unexpected response {:?}", response.1);
            };
            assert_eq!(
                overrides.exe, image_record,
                "no launcher: the launch's record"
            );
        }

        // The registered root's first image of a run that declares a
        // launcher.
        for (launch_exe, unprepared, expected) in [
            (Some(&image), other_record.clone(), other_record.clone()),
            (Some(&image), None, image_record.clone()),
            (None, other_record.clone(), other_record.clone()),
            (Some(&image), launcher_record.clone(), image_record.clone()),
        ] {
            let config = Config {
                sequentialize_threads: true,
                launch_image: Some(crate::LaunchImage {
                    exe: launch_exe.cloned(),
                    launcher: Some(launcher.clone()),
                }),
                ..Config::default()
            };
            let state = GlobalState::initialize(&config, false);
            let root = DetTid::from_raw(17);
            state
                .sched
                .lock()
                .unwrap()
                .thread_tree
                .add_child(root, root, true);
            install_test_registration(&state, root, Ivar::new());
            state.root_dettid.set(root).unwrap();
            let process = DetPid::from_raw(root.as_raw());
            let response = state
                .receive_rpc(
                    reverie::Tid::from_raw(root.as_raw()),
                    (
                        DetTime::new(&config),
                        MmId::initial(process),
                        GlobalRequest::MarkPastFirstExecve(process, None, unprepared.clone(), None),
                    ),
                )
                .await;
            let GlobalResponse::MarkPastFirstExecve(overrides) = response.1 else {
                panic!("unexpected response {:?}", response.1);
            };
            assert_eq!(
                overrides.exe, expected,
                "launch {launch_exe:?} unprepared {unprepared:?}"
            );
        }
    }

    #[tokio::test]
    async fn only_successful_exec_cancels_posix_deadlines() {
        let (config, state, leader, detpid) = cancellation_test_state();
        install_test_registration(&state, leader, Ivar::new());
        let clock = DetTime::new(&config);
        let mm = MmId::initial(detpid);
        let deadline = LogicalTime::from_nanos(1_000_000);
        {
            let mut sched = state.sched.lock().unwrap();
            sched.register_alarm(
                detpid,
                leader,
                LogicalTime::ZERO,
                deadline,
                LogicalTime::ZERO,
                Signal::SIGALRM,
            );
            sched.register_posix_timer(
                detpid,
                crate::scheduler::timed_waiters::SignalTarget::Process(leader),
                0,
                Some(deadline),
                deadline,
                Signal::SIGUSR2.into(),
            );
        }
        let before: Vec<_> = state
            .sched
            .lock()
            .unwrap()
            .blocked
            .timed_waiters
            .iter()
            .collect();
        for request in [
            GlobalRequest::PrepareExec(detpid, mm, Default::default()),
            GlobalRequest::CancelExec(detpid),
            GlobalRequest::PrepareExec(detpid, mm, Default::default()),
        ] {
            state
                .receive_rpc(
                    reverie::Tid::from_raw(leader.as_raw()),
                    (clock.clone(), mm, request),
                )
                .await;
            assert_eq!(
                state
                    .sched
                    .lock()
                    .unwrap()
                    .blocked
                    .timed_waiters
                    .iter()
                    .collect::<Vec<_>>(),
                before
            );
        }

        let response = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    clock,
                    mm.for_exec(detpid),
                    GlobalRequest::MarkPastFirstExecve(detpid, None, None, None),
                ),
            )
            .await;
        assert_eq!(
            response.1,
            GlobalResponse::MarkPastFirstExecve(Default::default())
        );
        let sched = state.sched.lock().unwrap();
        assert_eq!(
            sched.blocked.timed_waiters.alarm_state(detpid),
            Some((deadline, LogicalTime::ZERO))
        );
        assert_eq!(sched.blocked.timed_waiters.iter().count(), 1);
    }

    #[tokio::test]
    async fn failed_exec_clears_prepared_state_without_retiring_siblings() {
        let (config, state, leader, detpid) = cancellation_test_state();
        let sibling = DetTid::from_raw(leader.as_raw() + 1);
        install_test_registration(&state, leader, Ivar::new());
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(leader, sibling, false);
        install_test_registration(&state, sibling, Ivar::new());
        let clock = DetTime::new(&config);

        let prepared = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    clock.clone(),
                    MmId::initial(leader),
                    GlobalRequest::PrepareExec(detpid, MmId::initial(detpid), Default::default()),
                ),
            )
            .await;
        assert_eq!(prepared.1, GlobalResponse::PrepareExec(()));
        assert!(
            state
                .pending_exec_states
                .lock()
                .unwrap()
                .contains_key(&detpid)
        );

        let cancelled = state
            .receive_rpc(
                reverie::Tid::from_raw(leader.as_raw()),
                (
                    clock,
                    MmId::initial(leader),
                    GlobalRequest::CancelExec(detpid),
                ),
            )
            .await;
        assert_eq!(cancelled.1, GlobalResponse::CancelExec(()));
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        {
            let scheduler = state.sched.lock().unwrap();
            assert!(!scheduler.thread_is_logically_killed(leader));
            assert!(!scheduler.thread_is_logically_killed(sibling));
            assert_eq!(scheduler.next_turns.len(), 2);
        }

        state.pending_exec_states.lock().unwrap().insert(
            detpid,
            PendingExecState {
                caller: leader,
                process: detpid,
                mm: MmId::initial(detpid),
                fd_blocking: Default::default(),
            },
        );
        state.post_exec_fd_blocking.lock().unwrap().insert(
            leader,
            ExecFdState::from([42].into_iter().collect::<ExecFdBlockingOverrides>()),
        );
        state
            .recv_deregister_thread(
                reverie::Tid::from_raw(leader.as_raw()),
                ThreadDeregistration {
                    thread_start_entered: true,
                    dettid: leader,
                    detpid,
                    mm: MmId::initial(detpid),
                    timeslice_stats: TimesliceStats::default(),
                    syscall_count: 0,
                    chaos_epochs: Vec::new(),
                },
            )
            .await;
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        assert!(state.post_exec_fd_blocking.lock().unwrap().is_empty());

        state.pending_exec_states.lock().unwrap().insert(
            detpid,
            PendingExecState {
                caller: leader,
                process: detpid,
                mm: MmId::initial(detpid),
                fd_blocking: Default::default(),
            },
        );
        state
            .recv_deregister_thread(
                reverie::Tid::from_raw(leader.as_raw()),
                ThreadDeregistration {
                    thread_start_entered: true,
                    dettid: leader,
                    detpid,
                    mm: MmId::initial(detpid).for_exec(detpid),
                    timeslice_stats: TimesliceStats::default(),
                    syscall_count: 0,
                    chaos_epochs: Vec::new(),
                },
            )
            .await;
        assert!(state.pending_exec_states.lock().unwrap().is_empty());

        state.pending_exec_states.lock().unwrap().insert(
            detpid,
            PendingExecState {
                caller: leader,
                process: detpid,
                mm: MmId::initial(detpid),
                fd_blocking: Default::default(),
            },
        );
        state.post_exec_fd_blocking.lock().unwrap().insert(
            leader,
            ExecFdState::from([42].into_iter().collect::<ExecFdBlockingOverrides>()),
        );
        state.complete_physical_process_exit(detpid.as_raw());
        assert!(state.pending_exec_states.lock().unwrap().is_empty());
        assert!(state.post_exec_fd_blocking.lock().unwrap().is_empty());
    }

    #[test]
    fn completed_unsupported_syscalls_transfer_once_without_duplicates() {
        let mut state = GlobalState::initialize(&Config::default(), false);
        for name in ["restart_syscall", "io_setup", "restart_syscall"] {
            state
                .unsupported_syscalls
                .get_mut()
                .unwrap()
                .insert(name.to_owned());
        }
        assert_eq!(
            state.take_unsupported_syscalls(),
            ["io_setup".to_owned(), "restart_syscall".to_owned()]
                .into_iter()
                .collect()
        );
        assert!(state.take_unsupported_syscalls().is_empty());
    }

    #[test]
    fn unsupported_syscall_report_duplicate_is_close_on_exec() {
        let mut descriptors = [-1; 2];
        assert_eq!(
            unsafe { libc::pipe2(descriptors.as_mut_ptr(), libc::O_CLOEXEC) },
            0
        );
        // SAFETY: pipe2 initialized both descriptors and transfers ownership.
        let _reader = unsafe { OwnedFd::from_raw_fd(descriptors[0]) };
        let writer = unsafe { OwnedFd::from_raw_fd(descriptors[1]) };
        let config = Config {
            unsupported_syscall_report_fd: Some(writer.as_raw_fd()),
            ..Config::default()
        };

        let state = GlobalState::initialize(&config, false);
        let duplicate = state
            .unsupported_syscall_report_fd
            .as_ref()
            .expect("report writer should be duplicated")
            .lock()
            .unwrap();
        let flags = unsafe { libc::fcntl(duplicate.as_raw_fd(), libc::F_GETFD) };
        assert_ne!(flags, -1);
        assert_ne!(flags & libc::FD_CLOEXEC, 0);
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1056): Deterministic st_dev remapping test.
    #[test]
    fn device_pool_remaps_deterministically() {
        use super::DevicePool;

        // Raw device numbers a guest might observe; the procfs/tmpfs ones
        // (large anon-bdev values) are exactly what drifts between runs.
        let raw_root = 0x20; // e.g. a real block device
        let raw_proc_run1 = 3_145_792; // anon bdev in run 1
        let raw_proc_run2 = 3_145_788; // same procfs, different number in run 2

        // Run 1: observe root then proc.
        let mut pool1 = DevicePool::new();
        let root1 = pool1.determinize(raw_root);
        let proc1 = pool1.determinize(raw_proc_run1);
        // Run 2: same observation order, different raw proc number.
        let mut pool2 = DevicePool::new();
        let root2 = pool2.determinize(raw_root);
        let proc2 = pool2.determinize(raw_proc_run2);

        // The synthetic ids depend only on first-observation order, so they are
        // identical across the two runs despite the raw proc number differing.
        assert_eq!(root1, root2);
        assert_eq!(proc1, proc2);

        // Distinct raw devices get distinct ids; ids start at 1 (never 0).
        assert_ne!(root1, proc1);
        assert_eq!(root1, 1);
        assert_eq!(proc1, 2);

        // Re-observing a raw device is stable within a run.
        assert_eq!(pool1.determinize(raw_root), root1);
        assert_eq!(pool1.determinize(raw_proc_run1), proc1);
    }

    #[test]
    fn the_procfs_device_is_never_minted_for_another_device() {
        use super::DevicePool;

        let mut pool = DevicePool::new();
        pool.next_device = crate::procfs_inode::VIRTUAL_PROC_DEVICE - 1;
        let before = pool.determinize(libc::makedev(8, 1));
        let after = pool.determinize(libc::makedev(8, 2));
        assert_eq!(before, crate::procfs_inode::VIRTUAL_PROC_DEVICE - 1);
        assert_eq!(after, crate::procfs_inode::VIRTUAL_PROC_DEVICE + 1);
    }

    #[test]
    fn this_namespaces_procfs_device_is_detcores() {
        use std::os::unix::fs::MetadataExt;

        use super::DevicePool;

        let mut pool = DevicePool::for_this_mount_namespace();
        let proc = std::fs::metadata("/proc").unwrap().dev();
        assert_eq!(
            pool.determinize(proc),
            crate::procfs_inode::VIRTUAL_PROC_DEVICE
        );
        assert_eq!(pool.determinize(libc::makedev(8, 1)), 1);
    }

    #[test]
    fn mountinfo_prepopulation_is_reused_by_later_stat_observations() {
        use super::DevicePool;

        let mountinfo_devices = [libc::makedev(8, 1), libc::makedev(0, 44)];
        let mut pool = DevicePool::new();
        let rendered = mountinfo_devices
            .into_iter()
            .map(|raw| pool.determinize(raw))
            .collect::<Vec<_>>();

        assert_eq!(rendered, [1, 2]);
        assert_eq!(pool.determinize(libc::makedev(0, 44)), rendered[1]);
        assert_eq!(pool.determinize(libc::makedev(8, 1)), rendered[0]);
        assert_eq!(pool.determinize(libc::makedev(259, 7)), 3);
    }

    #[tokio::test]
    async fn late_futex_rpc_after_thread_removal_returns_eintr() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let dettid = DetTid::from_raw(17);
        let detpid = DetPid::from_raw(17);
        let response = state
            .recv_futex_action(
                RpcIncarnation {
                    dettid,
                    mm: MmId::initial(detpid),
                },
                FutexAction::WaitRequest(None, None),
                FutexID::private(MmId::initial(detpid), 0x1000),
                0,
                u32::MAX,
            )
            .await;

        assert!(matches!(
            response,
            Some(SchedValue::Value(value)) if value == nix::errno::Errno::EINTR as u64
        ));
    }

    #[tokio::test]
    async fn late_child_tid_wait_after_exit_returns_spurious_wake() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let detpid = DetPid::from_raw(17);
        let child = DetTid::from_raw(18);
        let futex = FutexID::private(MmId::initial(detpid), 0x1000);
        state.sched.lock().unwrap().next_turns.insert(
            detpid,
            ThreadNextTurn {
                dettid: detpid,
                child_tid_addr: 0,
                req: Ivar::new(),
                resp: Ivar::new(),
                protocol: Default::default(),
            },
        );
        state
            .sched
            .lock()
            .unwrap()
            .wake_futex_child_cleartid(futex, child);
        assert!(
            state
                .sched
                .lock()
                .unwrap()
                .child_tid_was_cleared(futex, child.as_raw())
        );
        assert!(
            !state
                .sched
                .lock()
                .unwrap()
                .child_tid_was_cleared(futex, child.as_raw() + 1)
        );

        let response = state
            .recv_futex_action(
                RpcIncarnation {
                    dettid: detpid,
                    mm: MmId::initial(detpid),
                },
                FutexAction::WaitRequest(None, None),
                futex,
                child.as_raw(),
                u32::MAX,
            )
            .await;

        assert!(matches!(response, Some(SchedValue::Value(0))));
        assert!(state.sched.lock().unwrap().blocked.futex_waiters.is_empty());
    }

    #[tokio::test]
    async fn late_resource_request_after_logical_kill_is_cancelled() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        install_test_registration(&state, dettid, Ivar::new());
        let mut current_time = DetTime::new(&config);
        current_time.add_syscall();
        state.global_time.lock().unwrap().update_global_time(
            dettid,
            current_time.as_nanos(),
            LogicalTime::ZERO,
        );
        state
            .sched
            .lock()
            .unwrap()
            .logically_kill_thread(&dettid, &detpid, MmId::initial(detpid));
        let (global_before, thread_before) = {
            let global_time = state.global_time.lock().unwrap();
            (global_time.as_nanos(), global_time.threads_time(dettid))
        };
        let mut late_time = current_time;
        late_time.add_syscall();

        let response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    late_time,
                    MmId::initial(dettid),
                    GlobalRequest::RequestResources(Resources::new(dettid), detpid),
                ),
            )
            .await;

        assert_eq!(response, (None, GlobalResponse::ThreadExited));
        assert!(!state.sched.lock().unwrap().next_turns.contains_key(&dettid));
        let global_time = state.global_time.lock().unwrap();
        assert_eq!(global_time.as_nanos(), global_before);
        assert_eq!(global_time.threads_time(dettid), thread_before);
    }

    #[tokio::test]
    async fn duplicate_deregistration_is_acknowledged_without_clock_or_scheduler_mutation() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        install_test_registration(&state, dettid, Ivar::new());
        let mut current_time = DetTime::new(&config);
        current_time.add_syscall();
        state.global_time.lock().unwrap().update_global_time(
            dettid,
            current_time.as_nanos(),
            LogicalTime::ZERO,
        );
        state
            .sched
            .lock()
            .unwrap()
            .logically_kill_thread(&dettid, &detpid, MmId::initial(detpid));
        let (global_before, thread_before) = {
            let global_time = state.global_time.lock().unwrap();
            (global_time.as_nanos(), global_time.threads_time(dettid))
        };
        let mut late_time = current_time;
        late_time.add_syscall();
        let mut final_stats = TimesliceStats::default();
        final_stats.record(7);

        let first_response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    late_time.clone(),
                    MmId::initial(dettid),
                    GlobalRequest::DeregisterThread(ThreadDeregistration {
                        thread_start_entered: true,
                        dettid,
                        detpid,
                        mm: MmId::initial(detpid),
                        timeslice_stats: final_stats,
                        syscall_count: 17,
                        chaos_epochs: Vec::new(),
                    }),
                ),
            )
            .await;
        assert_eq!(first_response, (None, GlobalResponse::DeregisterThread(())));
        assert_eq!(
            state
                .sched
                .lock()
                .unwrap()
                .per_thread_timeslice
                .get(&dettid),
            Some(&final_stats)
        );
        assert_eq!(
            state.sched.lock().unwrap().per_thread_syscalls.get(&dettid),
            Some(&17)
        );

        late_time.add_syscall();
        let duplicate_response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    late_time,
                    MmId::initial(dettid),
                    GlobalRequest::DeregisterThread(ThreadDeregistration {
                        thread_start_entered: true,
                        dettid,
                        detpid,
                        mm: MmId::initial(detpid),
                        timeslice_stats: final_stats,
                        syscall_count: 99,
                        chaos_epochs: Vec::new(),
                    }),
                ),
            )
            .await;
        assert_eq!(
            duplicate_response,
            (None, GlobalResponse::DeregisterThread(()))
        );
        assert_eq!(
            state
                .sched
                .lock()
                .unwrap()
                .per_thread_timeslice
                .get(&dettid),
            Some(&final_stats)
        );
        assert_eq!(
            state.sched.lock().unwrap().per_thread_syscalls.get(&dettid),
            Some(&17),
            "a duplicate deregistration must not double-count or replace final accounting"
        );
        let summary = state
            .sched
            .lock()
            .unwrap()
            .generate_partial_run_summary(None)
            .unwrap();
        assert_eq!(summary.syscalls, Some(17));
        assert!(!state.sched.lock().unwrap().next_turns.contains_key(&dettid));
        let global_time = state.global_time.lock().unwrap();
        assert_eq!(global_time.as_nanos(), global_before);
        assert_eq!(global_time.threads_time(dettid), thread_before);
    }

    #[tokio::test]
    async fn child_registration_fails_closed_for_a_tombstoned_tid() {
        let (config, state, parent, detpid) = cancellation_test_state();
        install_test_registration(&state, parent, Ivar::new());
        let child = DetTid::from_raw(18);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(parent, child, false);
        install_test_registration(&state, child, Ivar::new());
        state
            .sched
            .lock()
            .unwrap()
            .logically_kill_thread(&child, &detpid, MmId::initial(detpid));

        let response = state
            .receive_rpc(
                reverie::Tid::from_raw(parent.as_raw()),
                (
                    DetTime::new(&config),
                    MmId::initial(parent),
                    GlobalRequest::CreateChildThread(
                        child,
                        detpid,
                        0,
                        None,
                        libc::SIGCHLD,
                        None,
                        Some(DEFAULT_PRIORITY),
                    ),
                ),
            )
            .await;

        assert_eq!(response, (None, GlobalResponse::ThreadExited));
        let scheduler = state.sched.lock().unwrap();
        assert!(scheduler.thread_is_logically_killed(child));
        assert!(!scheduler.next_turns.contains_key(&child));
        assert!(!scheduler.priorities.contains_key(&child));
        assert!(!state.global_time.lock().unwrap().contains_thread(parent));
    }

    #[tokio::test]
    async fn pending_resource_request_woken_by_logical_kill_is_terminal() {
        let (_, state, dettid, detpid) = cancellation_test_state();
        let request_seen = Ivar::new();
        install_test_registration(&state, dettid, request_seen.clone());

        let request = state.recv_request_resources(
            reverie::Tid::from_raw(dettid.as_raw()),
            detpid,
            Resources::new(dettid),
            None,
        );
        let kill_after_request = async {
            while request_seen.try_read().is_none() {
                tokio::task::yield_now().await;
            }
            state.sched.lock().unwrap().logically_kill_thread(
                &dettid,
                &detpid,
                MmId::initial(detpid),
            );
        };

        let (response, ()) = tokio::join!(request, kill_after_request);
        assert_eq!(response, (SchedulerRpcResult::ThreadExited, None));
    }

    #[tokio::test]
    async fn trace_replay_yield_propagates_terminal_scheduler_cancellation() {
        let dettid = DetTid::from_raw(17);
        let detpid = DetPid::from_raw(17);
        let next_tid = DetTid::from_raw(18);
        let event = SchedEvent {
            dettid,
            op: Op::OtherInstructions,
            count: 1,
            start_rip: None,
            end_rip: None,
            end_time: Some(LogicalTime::from_nanos(1)),
        };
        let next_event = SchedEvent {
            dettid: next_tid,
            end_time: Some(LogicalTime::from_nanos(2)),
            ..event.clone()
        };
        let trace_file = tempfile::NamedTempFile::new().unwrap();
        std::fs::write(
            trace_file.path(),
            PreemptionRecord::from_sched_events(vec![event.clone(), next_event]).to_string(),
        )
        .unwrap();
        let config = Config {
            sequentialize_threads: true,
            replay_schedule_from: Some(trace_file.path().to_path_buf()),
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.needs_killed_thread_rpc_cancellation = true;
        });
        let state = GlobalState::initialize(&config, false);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(dettid, dettid, true);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(dettid, next_tid, false);
        let request_seen = Ivar::new();
        install_test_registration(&state, dettid, request_seen.clone());
        install_test_registration(&state, next_tid, Ivar::new());

        let replay = state.recv_trace_schedevent(event, detpid, MmId::initial(detpid), false);
        let kill_after_replay_yield = async {
            while request_seen.try_read().is_none() {
                tokio::task::yield_now().await;
            }
            state.sched.lock().unwrap().logically_kill_thread(
                &dettid,
                &detpid,
                MmId::initial(detpid),
            );
        };
        let (response, ()) = tokio::time::timeout(Duration::from_secs(1), async {
            tokio::join!(replay, kill_after_replay_yield)
        })
        .await
        .expect("trace replay cancellation did not terminate the pending scheduler RPC");

        assert_eq!(response, SchedulerRpcResult::ThreadExited);
    }

    #[tokio::test]
    async fn pending_start_request_woken_by_logical_kill_is_terminal() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        let request_seen = Ivar::new();
        install_test_registration(&state, dettid, request_seen.clone());
        let request = state.receive_rpc(
            reverie::Tid::from_raw(dettid.as_raw()),
            (
                DetTime::new(&config),
                MmId::initial(dettid),
                GlobalRequest::StartNewThread(dettid, detpid, None, None),
            ),
        );
        let kill_after_request = async {
            while request_seen.try_read().is_none() {
                tokio::task::yield_now().await;
            }
            state.sched.lock().unwrap().logically_kill_thread(
                &dettid,
                &detpid,
                MmId::initial(detpid),
            );
        };

        let (response, ()) = tokio::join!(request, kill_after_request);
        assert_eq!(response, (None, GlobalResponse::ThreadExited));
        assert!(!state.sched.lock().unwrap().priorities.contains_key(&dettid));
    }

    #[tokio::test]
    async fn required_physical_thread_id_missing_is_terminal() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        }
        .with_backend(|backend| {
            backend.needs_killed_thread_rpc_cancellation = true;
            backend.requires_thread_directed_process_signals = true;
        });
        let state = GlobalState::initialize(&config, false);
        let dettid = DetTid::from_raw(17);
        let detpid = DetPid::from_raw(17);
        state
            .sched
            .lock()
            .unwrap()
            .thread_tree
            .add_child(dettid, dettid, true);
        install_test_registration(&state, dettid, Ivar::new());

        let response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    DetTime::new(&config),
                    MmId::initial(detpid),
                    GlobalRequest::StartNewThread(dettid, detpid, None, None),
                ),
            )
            .await;

        assert_eq!(response, (None, GlobalResponse::ThreadExited));
        let scheduler = state.sched.lock().unwrap();
        assert!(!scheduler.next_turns.contains_key(&dettid));
    }

    #[tokio::test]
    async fn tombstoned_timer_registration_cannot_mutate_scheduler_state() {
        let (_, state, dettid, detpid) = cancellation_test_state();
        install_test_registration(&state, dettid, Ivar::new());
        state
            .sched
            .lock()
            .unwrap()
            .logically_kill_thread(&dettid, &detpid, MmId::initial(detpid));
        let now = LogicalTime::from_nanos(100);

        let alarm = state
            .recv_register_alarm(
                detpid,
                RpcIncarnation {
                    dettid,
                    mm: MmId::initial(detpid),
                },
                now,
                LogicalTime::from_nanos(10),
                LogicalTime::ZERO,
                SigWrapper::from(Signal::SIGALRM),
            )
            .await;
        assert_eq!(alarm, SchedulerRpcResult::ThreadExited);

        let posix = state
            .recv_register_posix_timer(
                detpid,
                RpcIncarnation {
                    dettid,
                    mm: MmId::initial(detpid),
                },
                1,
                Some(now + LogicalTime::from_nanos(10)),
                LogicalTime::ZERO,
                super::PosixTimerNotification {
                    sig: SigWrapper::from(Signal::SIGALRM),
                    thread: None,
                    value: 1,
                    deleted: false,
                },
            )
            .await;
        assert_eq!(posix, SchedulerRpcResult::ThreadExited);
        assert!(state.sched.lock().unwrap().blocked.timed_waiters.is_empty());
    }

    #[tokio::test]
    async fn pending_futex_request_woken_by_logical_kill_is_terminal() {
        let (config, state, dettid, detpid) = cancellation_test_state();
        install_test_registration(&state, dettid, Ivar::new());
        let request = state.receive_rpc(
            reverie::Tid::from_raw(dettid.as_raw()),
            (
                DetTime::new(&config),
                MmId::initial(dettid),
                GlobalRequest::FutexAction(
                    dettid,
                    FutexAction::WaitRequest(None, None),
                    FutexID::private(MmId::initial(detpid), 0x1000),
                    0,
                    u32::MAX,
                ),
            ),
        );
        let kill_after_wait = async {
            while state.sched.lock().unwrap().blocked.futex_waiters.is_empty() {
                tokio::task::yield_now().await;
            }
            state.sched.lock().unwrap().logically_kill_thread(
                &dettid,
                &detpid,
                MmId::initial(detpid),
            );
        };

        let (response, ()) = tokio::time::timeout(Duration::from_secs(1), async {
            tokio::join!(request, kill_after_wait)
        })
        .await
        .expect("futex teardown did not wake the blocked RPC");
        assert_eq!(response, (None, GlobalResponse::ThreadExited));
    }

    #[tokio::test]
    async fn parent_continue_propagates_terminal_scheduler_cancellation() {
        let (config, state, parent, detpid) = cancellation_test_state();
        let parent_request = Ivar::new();
        install_test_registration(&state, parent, parent_request.clone());
        let child = DetTid::from_raw(18);
        let physical_pid = std::process::id() as i32;
        let physical_tid = unsafe { libc::syscall(libc::SYS_gettid) as i32 };
        let request = state.receive_rpc(
            reverie::Tid::from_raw(parent.as_raw()),
            (
                DetTime::new(&config),
                MmId::initial(parent),
                GlobalRequest::CreateChildThread(
                    child,
                    detpid,
                    0,
                    None,
                    libc::SIGCHLD,
                    Some((physical_pid, physical_tid)),
                    Some(DEFAULT_PRIORITY),
                ),
            ),
        );
        let kill_after_parent_parks = async {
            while parent_request.try_read().is_none() {
                tokio::task::yield_now().await;
            }
            let mut scheduler = state.sched.lock().unwrap();
            let (registered_mm, registered_pid, registered_tid) = scheduler
                .physical_thread_identity(child)
                .expect("parent registration must install the child pidfd before continuing");
            assert_eq!(
                registered_mm,
                MmId::for_clone(MmId::initial(parent), child, false)
            );
            assert_eq!(
                (registered_pid, registered_tid),
                (physical_pid, physical_tid)
            );
            scheduler.logically_kill_thread(&parent, &detpid, MmId::initial(detpid));
        };

        let (response, ()) = tokio::join!(request, kill_after_parent_parks);
        assert_eq!(response, (None, GlobalResponse::ThreadExited));
    }

    #[test]
    fn unsupported_syscall_warning_is_sorted_and_aggregated() {
        let syscalls = BTreeSet::from([
            "vmsplice".to_owned(),
            "getppid".to_owned(),
            "getppid".to_owned(),
        ]);

        assert_eq!(
            format_unsupported_syscall_warning(&syscalls).as_deref(),
            Some("syscalls getppid,vmsplice used but not yet supported")
        );
        assert_eq!(format_unsupported_syscall_warning(&BTreeSet::new()), None);
    }

    #[tokio::test]
    async fn abnormal_cleanup_cancels_an_unstarted_scheduler() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let mut state = GlobalState::initialize(&config, true);
        state.cancel_internal_scheduler().await;
        let summary_path = None;
        let cleanup = state.clean_up(false, &summary_path);

        assert!(
            tokio::time::timeout(Duration::from_millis(100), cleanup)
                .await
                .is_ok(),
            "cleanup waited for a scheduler whose guest never registered"
        );
    }

    #[tokio::test]
    async fn abnormal_cleanup_cancels_a_registered_scheduler() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let mut state = GlobalState::initialize(&config, true);
        let dettid = DetTid::from_raw(1);
        {
            let mut scheduler = state.sched.lock().unwrap();
            scheduler.priorities.insert(dettid, DEFAULT_PRIORITY);
            scheduler.next_turns.insert(
                dettid,
                ThreadNextTurn {
                    dettid,
                    child_tid_addr: 0,
                    req: Ivar::new(),
                    resp: Ivar::new(),
                    protocol: Default::default(),
                },
            );
            scheduler.runqueue_push_back(dettid);
            scheduler.started_up.put(());
        }
        tokio::task::yield_now().await;

        state.cancel_internal_scheduler().await;
        let summary_path = None;
        assert!(
            tokio::time::timeout(
                Duration::from_millis(100),
                state.clean_up(false, &summary_path),
            )
            .await
            .is_ok(),
            "cleanup waited after cancelling a registered scheduler"
        );
    }

    /// A deterministic inode must be minted from the monotonic counter, never
    /// derived from the host inode's bits. This is the behavioural half of the
    /// guarantee whose static half is `DetInode` being a newtype: even for a
    /// large, realistic host inode the det value stays small, so a leaked host
    /// inode is distinguishable from a genuine det one.
    #[test]
    fn det_inodes_are_minted_not_passed_through() {
        use crate::types::DetInode;

        let mut pool = super::InodePool::new();
        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;

        // 221_742_951 is the value observed leaking into FileContents.
        let host_a = crate::types::RawInode::new(33, 221_742_951);
        let host_b = crate::types::RawInode::new(33, 998_877_665);
        let (a, _) = pool.add_inode(host_a, seen, t);
        let (b, _) = pool.add_inode(host_b, seen, t);

        assert_ne!(
            a.as_raw(),
            host_a.ino,
            "det inode must not be the host inode"
        );
        assert_ne!(
            b.as_raw(),
            host_b.ino,
            "det inode must not be the host inode"
        );
        assert_eq!(
            a,
            crate::consts::DET_INODE_OFFSET,
            "minting starts above the fixed stdio inodes"
        );
        assert_eq!(b, DetInode::mint(9001), "minting is monotonic");

        // Re-determinizing the same host inode is stable, not a fresh mint.
        let (a_again, _) = pool.add_inode(host_a, seen, t);
        assert_eq!(a, a_again, "mapping must be stable per host inode");
    }

    /// <https://github.com/rrnewton/hermit/issues/3840>: a filesystem such as
    /// ext4 or XFS gives a freed inode to a new file, and whether it does
    /// differs between runs. The same guest steps (number a file, remove its
    /// last name, create another file, look it up) must number the new file,
    /// and every file after it, the same way whether or not the host reused
    /// the inode, however the new file is first reached.
    #[test]
    fn a_host_reusing_a_freed_inode_does_not_renumber_the_new_file() {
        use super::InodeSighting::Descriptor;
        use super::InodeSighting::Name;
        use crate::types::DetInode;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        const EXT4: u64 = 2049;
        let old = RawInode::new(EXT4, 12);
        let fresh = RawInode::new(EXT4, 13);
        // `opened` says whether the new file is created by an `open`, which
        // discards a retired mapping, before its first numbering request.
        let steps = |new: RawInode, opened: bool, first: super::InodeSighting| {
            let mut pool = super::InodePool::new();
            let old_number = pool.add_sighted_inode(old, seen, t, Name).0;
            pool.retire(old);
            if opened {
                pool.forget_retired(new);
            }
            let new_number = pool.add_sighted_inode(new, seen, t, first).0;
            let listed = pool.add_sighted_inode(new, seen, t, Name).0;
            let later: Vec<DetInode> = [RawInode::new(EXT4, 40), RawInode::new(EXT4, 41)]
                .into_iter()
                .map(|raw| pool.add_sighted_inode(raw, seen, t, Name).0)
                .collect();
            (old_number, new_number, listed, later)
        };
        for (opened, first) in [(true, Descriptor), (true, Name), (false, Name)] {
            let reused = steps(old, opened, first);
            let not_reused = steps(fresh, opened, first);
            assert_eq!(
                reused, not_reused,
                "opened={opened} first={first:?}: reuse changed a number"
            );
            let (old_number, new_number, listed, _) = reused;
            assert_ne!(old_number, new_number, "the new file took the old number");
            assert_eq!(new_number, listed, "the new file's number is stable");
        }
    }

    /// An unlinked file reached through a descriptor (open, unlink, fstat)
    /// keeps its number and its mtime updates, as on Linux, until a name shows
    /// that the host gave its inode to another file. A path that finds no
    /// link (`/proc/self/fd/N`) is a `Descriptor` sighting.
    #[test]
    fn an_unlinked_file_keeps_its_number_through_its_descriptors() {
        use super::InodeSighting::Descriptor;
        use super::InodeSighting::Name;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let raw = RawInode::new(2049, 12);
        let mut pool = super::InodePool::new();
        let (number, _) = pool.add_sighted_inode(raw, seen, t, Name);
        pool.retire(raw);
        assert_eq!(pool.add_sighted_inode(raw, seen, t, Descriptor).0, number);
        let written = LogicalTime::from_nanos(7_000);
        pool.set_mtime(raw, written);
        assert_eq!(
            pool.add_sighted_inode(raw, seen, t, Descriptor),
            (number, written),
            "a write through the descriptor reached the retired file"
        );
        let (reused, _) = pool.add_sighted_inode(raw, seen, t, Name);
        assert_ne!(reused, number, "a name of a retired inode is another file");
        assert_eq!(
            pool.add_sighted_inode(raw, seen, t, Descriptor).0,
            reused,
            "descriptors now reach the new file"
        );
        // Without a retire, a name finds the live mapping, as before.
        let other = RawInode::new(2049, 99);
        let (live, _) = pool.add_sighted_inode(other, seen, t, Descriptor);
        assert_eq!(pool.add_sighted_inode(other, seen, t, Name).0, live);
    }

    /// A write's mtime update can reach a file before anything numbers it
    /// (`echo x > f`, then `rm f`). The pending mtime must stay with that file,
    /// for a descriptor that still reaches it, and must not reach the next
    /// file the host gives the inode (claude review of
    /// <https://github.com/rrnewton/hermit/pull/3849>, P2-2).
    #[test]
    fn a_pending_mtime_stays_with_its_file_after_the_last_name_goes() {
        use super::InodeSighting::Descriptor;
        use super::InodeSighting::Name;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let old = RawInode::new(2049, 12);
        let fresh = RawInode::new(2049, 13);
        let written = LogicalTime::from_nanos(7_000);
        // The next file is created by an open, or first seen in a listing.
        let next_file = |new: RawInode, opened: bool| {
            let mut pool = super::InodePool::new();
            pool.set_mtime(old, written);
            pool.retire(old);
            if opened {
                pool.forget_retired(new);
            }
            pool.add_sighted_inode(new, seen, t, Name)
        };
        for opened in [true, false] {
            assert_eq!(
                next_file(old, opened),
                next_file(fresh, opened),
                "opened={opened}: the reused inode inherited the removed file's mtime"
            );
        }

        // Through a remaining descriptor, the removed file keeps its mtime,
        // and later writes through it reach it.
        let mut pool = super::InodePool::new();
        pool.set_mtime(old, written);
        pool.retire(old);
        let (number, mtime) = pool.add_sighted_inode(old, seen, t, Descriptor);
        assert_eq!(mtime, written);
        let rewritten = LogicalTime::from_nanos(9_000);
        pool.set_mtime(old, rewritten);
        assert_eq!(
            pool.add_sighted_inode(old, seen, t, Descriptor),
            (number, rewritten)
        );
        // It was numbered as retired: a name is another file.
        let (other, _) = pool.add_sighted_inode(old, seen, t, Name);
        assert_ne!(other, number);
    }

    /// A directory stream snapshots the whole directory at its first
    /// `getdents` and can return an entry after the entry's file lost its
    /// last name, while a descriptor still holds that file. Such an entry is
    /// not a new file, so it must not renumber the held one; an entry from a
    /// snapshot taken after the retirement is (codex review of
    /// <https://github.com/rrnewton/hermit/pull/3849>, P1).
    #[test]
    fn a_stale_directory_entry_does_not_renumber_a_held_file() {
        use super::InodeSighting::Descriptor;
        use super::InodeSighting::Listed;
        use super::InodeSighting::Name;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let raw = RawInode::new(2049, 12);
        let mut pool = super::InodePool::new();
        let (number, _) = pool.add_sighted_inode(raw, seen, t, Name);
        let before = pool.retirements;
        pool.retire(raw);
        assert_eq!(
            pool.add_sighted_inode(raw, seen, t, Listed(before)).0,
            number
        );
        assert_eq!(pool.add_sighted_inode(raw, seen, t, Descriptor).0, number);
        let after = pool.retirements;
        let (reused, _) = pool.add_sighted_inode(raw, seen, t, Listed(after));
        assert_ne!(
            reused, number,
            "a snapshot taken after the retirement shows a new file"
        );
    }

    /// A snapshot entry must name the file that held the inode when the
    /// snapshot was taken, even after a later file the host gave the freed
    /// inode discarded that file's retirement. Codex re-review of
    /// https://github.com/rrnewton/hermit/pull/3849, P1 (Claude re-review
    /// P2-A): number F, take a snapshot, unlink F, create and number G, then
    /// read F's cached entry; reuse and a fresh inode must agree on every
    /// number, whether or not F was numbered before the snapshot.
    #[test]
    fn a_stale_directory_entry_keeps_its_file_after_the_inode_is_reused() {
        use super::InodeSighting::Listed;
        use super::InodeSighting::Name;
        use crate::types::DetInode;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let f = RawInode::new(2049, 12);
        let fresh = RawInode::new(2049, 13);
        let history = |g: RawInode, numbered_first: bool| -> Vec<DetInode> {
            let mut pool = super::InodePool::new();
            let mut numbers = Vec::new();
            if numbered_first {
                numbers.push(pool.add_sighted_inode(f, seen, t, Name).0);
            }
            let snapshot = pool.retirements;
            pool.retire(f);
            pool.forget_retired(g); // G's open.
            numbers.push(pool.add_sighted_inode(g, seen, t, Name).0);
            numbers.push(pool.add_sighted_inode(f, seen, t, Listed(snapshot)).0);
            numbers.push(pool.add_sighted_inode(f, seen, t, Listed(snapshot)).0);
            numbers.push(
                pool.add_sighted_inode(RawInode::new(2049, 40), seen, t, Name)
                    .0,
            );
            numbers
        };
        for numbered_first in [true, false] {
            let reused = history(f, numbered_first);
            assert_eq!(
                reused,
                history(fresh, numbered_first),
                "numbered_first={numbered_first}: host reuse changed a number"
            );
            let cached = reused[reused.len() - 3];
            let g = reused[reused.len() - 4];
            assert_ne!(cached, g, "F's cached entry took G's number");
            assert_eq!(
                cached,
                reused[reused.len() - 2],
                "the cached entry is stable"
            );
        }
    }

    /// A write through a descriptor after the last name of a never-numbered
    /// file went away reaches that file's history record too, so a directory
    /// snapshot that names it after the host reused the inode sees the same
    /// mtime as one that names it without reuse (claude re-check of
    /// https://github.com/rrnewton/hermit/pull/3849).
    #[test]
    fn a_write_after_the_last_name_reaches_the_snapshot_record() {
        use super::InodeSighting::Listed;
        use super::InodeSighting::Name;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let f = RawInode::new(2049, 12);
        let fresh = RawInode::new(2049, 13);
        let cached = |g: RawInode| {
            let mut pool = super::InodePool::new();
            pool.set_mtime(f, LogicalTime::from_nanos(1_000));
            let snapshot = pool.retirements;
            pool.retire(f);
            pool.set_mtime(f, LogicalTime::from_nanos(2_000));
            pool.forget_retired(g);
            pool.add_sighted_inode(g, seen, t, Name);
            pool.add_sighted_inode(f, seen, t, Listed(snapshot))
        };
        assert_eq!(cached(f), cached(fresh));
        assert_eq!(cached(f).1, LogicalTime::from_nanos(2_000));
    }

    /// Ordinary churn reuses one host inode many times. A snapshot lookup
    /// must not scan the inode's whole history (codex re-check of
    /// https://github.com/rrnewton/hermit/pull/3849): a snapshot newer than
    /// the last retirement is answered without examining any record, and an
    /// older one examines about log2 of the history's length, while still
    /// naming the file that held the inode when it was taken.
    #[test]
    fn a_snapshot_lookup_does_not_scan_the_inodes_history() {
        use super::InodeSighting::Listed;
        use super::InodeSighting::Name;
        use crate::types::RawInode;

        const GENERATIONS: usize = 4096;
        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let raw = RawInode::new(2049, 12);
        let mut pool = super::InodePool::new();
        // Each holder is numbered, seen in a snapshot, and retired; the next
        // file the host gives the inode is opened by name.
        let mut held = Vec::with_capacity(GENERATIONS);
        for _ in 0..GENERATIONS {
            pool.forget_retired(raw);
            let number = pool.add_sighted_inode(raw, seen, t, Name).0;
            held.push((pool.retirements, number));
            pool.retire(raw);
        }
        // The current holder, numbered after the last retirement.
        pool.forget_retired(raw);
        let current = pool.add_sighted_inode(raw, seen, t, Name).0;

        pool.listed_probes = 0;
        for _ in 0..1000 {
            let snapshot = pool.retirements;
            assert_eq!(
                pool.add_sighted_inode(raw, seen, t, Listed(snapshot)).0,
                current
            );
        }
        assert_eq!(
            pool.listed_probes, 0,
            "a current snapshot examined history records"
        );

        let bound = (GENERATIONS.ilog2() + 2) as u64;
        for step in (0..GENERATIONS).step_by(37) {
            let (snapshot, number) = held[step];
            pool.listed_probes = 0;
            assert_eq!(
                pool.add_sighted_inode(raw, seen, t, Listed(snapshot)).0,
                number,
                "the snapshot taken while holder {step} held the inode"
            );
            assert!(
                pool.listed_probes <= bound,
                "holder {step}: {} probes for {GENERATIONS} generations",
                pool.listed_probes
            );
        }
    }

    /// Retiring and discarding follow host state (a link count, whether the
    /// host reused an inode), so they must not consume counter values: files
    /// numbered after them get the same numbers either way.
    #[test]
    fn retiring_and_forgetting_consume_no_counter_values() {
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let raw = RawInode::new(2049, 12);
        let numbers = |retire: bool| {
            let mut pool = super::InodePool::new();
            pool.add_inode(raw, seen, t);
            if retire {
                pool.retire(raw);
                pool.forget_retired(raw);
                pool.forget_retired(RawInode::new(2049, 77));
            }
            [RawInode::new(2049, 20), RawInode::new(2049, 21)]
                .map(|raw| pool.add_inode(raw, seen, t).0)
        };
        assert_eq!(numbers(true), numbers(false));
    }

    /// Whether a host inode is new to the pool depends on host state the guest
    /// does not control. In https://github.com/rrnewton/hermit/issues/2897,
    /// `sar` and its child `sadc` each opened one file; in one run another
    /// process replaced it between the two opens, so the second open saw a new
    /// host inode. Every later file, on every device, came out one number
    /// higher, and every `getdents64` chunk of a `/sys` listing differed. Only
    /// the file whose host identity differed may change number.
    #[test]
    fn a_host_dependent_new_inode_does_not_renumber_later_files() {
        use crate::types::DetInode;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        const ROOTFS: u64 = 2049;
        const SYSFS: u64 = 22;
        let later_files = |pool: &mut super::InodePool| -> Vec<DetInode> {
            [
                RawInode::new(SYSFS, 76_927),
                RawInode::new(SYSFS, 76_925),
                RawInode::new(SYSFS, 243_324),
                RawInode::new(ROOTFS, 1_311),
                RawInode::new(ROOTFS, 1_312),
            ]
            .into_iter()
            .map(|raw| pool.add_inode(raw, seen, t).0)
            .collect()
        };

        // Both opens find the same host file.
        let mut unchanged = super::InodePool::new();
        unchanged.add_inode(RawInode::new(ROOTFS, 94_136_091), seen, t);
        unchanged.add_inode(RawInode::new(ROOTFS, 94_136_091), seen, t);
        // The file is replaced between the two opens.
        let mut replaced = super::InodePool::new();
        replaced.add_inode(RawInode::new(ROOTFS, 94_136_091), seen, t);
        replaced.add_inode(RawInode::new(ROOTFS, 94_154_661), seen, t);
        assert_eq!(
            later_files(&mut unchanged),
            later_files(&mut replaced),
            "a host-dependent new inode renumbered files minted after it"
        );

        // The converse: two paths that are one host file (a hard link, or an
        // inode the filesystem reused) versus two separate host files.
        let mut linked = super::InodePool::new();
        linked.add_inode(RawInode::new(ROOTFS, 500), seen, t);
        linked.add_inode(RawInode::new(ROOTFS, 500), seen, t);
        let mut separate = super::InodePool::new();
        separate.add_inode(RawInode::new(ROOTFS, 500), seen, t);
        separate.add_inode(RawInode::new(ROOTFS, 501), seen, t);
        assert_eq!(
            later_files(&mut linked),
            later_files(&mut separate),
            "a host-dependent reused inode renumbered files minted after it"
        );
    }

    /// Whether an mtime update reaches the pool depends on host state:
    /// `utimensat` skips it when the target's host identity changed during the
    /// call or the kernel stored an unexpected time. Setting an mtime must not
    /// consume a number, so a skipped update cannot renumber later files; and
    /// a file that gets its mtime before its number keeps that mtime.
    #[test]
    fn an_mtime_update_does_not_number_files() {
        use crate::types::DetInode;
        use crate::types::RawInode;

        let epoch = LogicalTime::from_nanos(0);
        let set = LogicalTime::from_nanos(5_000_000_000);
        let seen = super::ObservedMtime::Unobserved;
        let touched = RawInode::new(2049, 77);
        let later_files = |pool: &mut super::InodePool| -> Vec<DetInode> {
            (1_000..1_003)
                .map(|ino| pool.add_inode(RawInode::new(2049, ino), seen, epoch).0)
                .collect()
        };

        // The update reached the pool, before and after the file was numbered.
        let mut applied = super::InodePool::new();
        applied.set_mtime(touched, set);
        let (_, mtime) = applied.add_inode(touched, seen, epoch);
        assert_eq!(
            mtime, set,
            "a pending mtime is lost when the file is numbered"
        );
        applied.set_mtime(touched, set);
        // The same calls with the updates skipped.
        let mut skipped = super::InodePool::new();
        skipped.add_inode(touched, seen, epoch);
        assert_eq!(
            later_files(&mut applied),
            later_files(&mut skipped),
            "a host-dependent mtime update renumbered later files"
        );
    }

    /// The serial grant follows the scheduler's real transitions: a granted
    /// thread holds it; filing its next request surrenders it; and a grant
    /// that releases the thread into a background call does not confer it,
    /// although that grant is the last one (`parked.running`).
    #[tokio::test]
    async fn the_serial_grant_ends_at_the_next_request_and_is_not_a_background_grant() {
        let (_config, state, tid, _) = cancellation_test_state();
        let first = Ivar::new();
        first.put(Ok(Resources::new(tid)));
        install_test_registration(&state, tid, first);
        assert!(!state.sched.lock().unwrap().holds_serial_grant(tid));

        let grant = |state: &GlobalState| {
            let (chosen, request, response) =
                state.sched.lock().unwrap().select_test_turn().unwrap();
            assert_eq!(chosen, tid);
            crate::scheduler::finish_selected_turn(
                state.sched.clone(),
                state.global_time.clone(),
                chosen,
                request,
                response,
            )
        };
        grant(&state).await.unwrap();
        assert!(state.sched.lock().unwrap().holds_serial_grant(tid));

        // The thread files its next request: a blocking external call.
        let next = state.sched.lock().unwrap().next_turns[&tid].req.clone();
        let mut background = Resources::new(tid);
        background.insert(
            ResourceID::BlockingExternalIO(ExternalOpId::new(tid, 1)),
            Permission::RW,
        );
        next.put(Ok(background));
        assert!(!state.sched.lock().unwrap().holds_serial_grant(tid));

        // The scheduler releases it into the background. A background grant
        // unblocks the thread in step 4 and then skips the rest of the turn.
        assert!(grant(&state).await.is_err());
        let sched = state.sched.lock().unwrap();
        assert!(sched.blocked.external_io_blockers.contains_key(&tid));
        assert!(!sched.holds_serial_grant(tid));
    }

    /// Controller mapping capture authenticates its task and procfs view,
    /// retains the existing serial grant, and returns no logical time.
    #[tokio::test]
    async fn controller_brk_capture_authenticates_the_view_without_spending_the_grant() {
        use std::os::unix::fs::MetadataExt;

        use crate::syscalls::memory::BrkMappingCheck;

        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let tid = DetTid::from_raw(37);
        let mm = MmId::initial(tid);
        let pid = std::process::id() as i32;
        let physical_tid = unsafe { libc::syscall(libc::SYS_gettid) as i32 };
        let procfs = std::fs::metadata("/proc").unwrap();
        let fact = reverie::ControllerCaptureFact {
            physical_pid: reverie::Pid::from_raw(pid),
            physical_tid: reverie::Pid::from_raw(physical_tid),
            procfs_device: procfs.dev(),
            procfs_inode: procfs.ino(),
        };
        {
            let mut sched = state.sched.lock().unwrap();
            sched.thread_tree.add_child(tid, tid, true);
            sched.record_thread_mm(tid, mm);
            sched.next_turns.insert(
                tid,
                ThreadNextTurn {
                    dettid: tid,
                    child_tid_addr: 0,
                    req: Ivar::new(),
                    resp: Ivar::new(),
                    protocol: Default::default(),
                },
            );
            sched
                .register_physical_thread(tid, mm, pid, physical_tid)
                .unwrap();
            sched.set_running_for_test(tid);
        }
        let before = state.global_time.lock().unwrap().as_nanos();
        // This real smaps capture must deny an overflowing growth, without
        // allocating a guest-visible snapshot or advancing the logical clock.
        let check = BrkMappingCheck::Grow {
            old_end: 0,
            new_end: u64::MAX,
        };
        let answer = state
            .receive_rpc(
                Tid::from_raw(tid.as_raw()),
                (
                    DetTime::new(&config),
                    mm,
                    GlobalRequest::CheckBrkMappings(check, fact),
                ),
            )
            .await;
        assert_eq!(
            answer,
            (None, GlobalResponse::CheckBrkMappings(Some(false)))
        );
        assert_eq!(state.global_time.lock().unwrap().as_nanos(), before);
        assert!(state.sched.lock().unwrap().holds_serial_grant(tid));
        assert_eq!(
            state.capture_brk_mappings(
                Tid::from_raw(tid.as_raw()),
                mm,
                check,
                reverie::ControllerCaptureFact {
                    procfs_inode: fact.procfs_inode + 1,
                    ..fact
                },
            ),
            None,
        );
        let address = &before as *const _ as u64;
        assert_eq!(
            state.capture_brk_mappings(
                Tid::from_raw(tid.as_raw()),
                mm,
                BrkMappingCheck::Shrink {
                    new_end: address,
                    old_end: address + 1
                },
                fact,
            ),
            Some(true),
        );
        let unsequentialized = GlobalState::initialize(
            &Config {
                sequentialize_threads: false,
                ..config
            },
            false,
        );
        assert_eq!(
            unsequentialized.capture_brk_mappings(Tid::from_raw(tid.as_raw()), mm, check, fact,),
            None
        );
    }

    /// Only the thread holding the serial grant reaches a shared open file
    /// description. Its control messages move no clock, and the death of the
    /// lease holder's process ends the lease. A refused message fails the run.
    #[tokio::test]
    async fn shared_open_file_control_needs_the_serial_grant() {
        use nix::fcntl::OFlag;

        use crate::fd::DetFd;
        use crate::fd::FdType;
        use crate::shared_open_files::OpenFileControl;
        use crate::shared_open_files::OpenFileControlReply;
        use crate::types::OpenFileId;

        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let granted = DetTid::from_raw(21);
        let other = DetTid::from_raw(22);
        let id = OpenFileId::new(granted, 0);
        {
            let mut sched = state.sched.lock().unwrap();
            for tid in [granted, other] {
                sched.thread_tree.add_child(tid, tid, true);
                sched.next_turns.insert(
                    tid,
                    ThreadNextTurn {
                        dettid: tid,
                        child_tid_addr: 0,
                        req: Ivar::new(),
                        resp: Ivar::new(),
                        protocol: Default::default(),
                    },
                );
            }
            sched.set_running_for_test(granted);
        }
        state
            .shared_open_files
            .lock()
            .unwrap()
            .insert(DetFd::new(3, OFlag::O_RDWR, FdType::Regular, id).model_for_test())
            .unwrap();
        let global_before = state.global_time.lock().unwrap().as_nanos();
        let take = |tid: DetTid| {
            state.receive_rpc(
                reverie::Tid::from_raw(tid.as_raw()),
                (
                    DetTime::new(&config),
                    MmId::initial(tid),
                    GlobalRequest::SharedOpenFile(OpenFileControl::Take { id }),
                ),
            )
        };

        assert!(matches!(
            take(granted).await,
            (
                None,
                GlobalResponse::SharedOpenFile(Ok(OpenFileControlReply::Taken { .. }))
            )
        ));
        assert!(state.shared_open_files.lock().unwrap().is_leased(id));
        {
            let global_time = state.global_time.lock().unwrap();
            assert_eq!(global_time.as_nanos(), global_before, "no clock accounting");
            assert!(!global_time.contains_thread(granted));
        }

        state.complete_physical_process_exit(granted.as_raw());
        assert!(!state.shared_open_files.lock().unwrap().is_leased(id));
        assert!(!state.sched.lock().unwrap().backend_failed());

        // A thread that does not hold the grant is refused, takes nothing,
        // and the refusal is a backend failure.
        assert!(matches!(
            take(other).await,
            (None, GlobalResponse::SharedOpenFile(Err(_)))
        ));
        assert!(!state.shared_open_files.lock().unwrap().is_leased(id));
        assert!(state.sched.lock().unwrap().backend_failed());
    }

    /// A SIGALRM ledger control message is answered with no logical time and
    /// moves no clock, and its answer is the ledger's: a send to a process that
    /// handles SIGALRM is refused, one to another process is not.
    #[tokio::test]
    async fn sigalrm_control_is_answered_before_clock_accounting() {
        let config = Config {
            sequentialize_threads: true,
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let (handler, sender) = (DetTid::from_raw(21), DetTid::from_raw(22));
        {
            let mut sched = state.sched.lock().unwrap();
            for tid in [handler, sender] {
                sched.thread_tree.add_child(tid, tid, true);
                sched.next_turns.insert(
                    tid,
                    ThreadNextTurn {
                        dettid: tid,
                        child_tid_addr: 0,
                        req: Ivar::new(),
                        resp: Ivar::new(),
                        protocol: Default::default(),
                    },
                );
            }
            sched.set_sigalrm_handled(DetPid::from_raw(21), true);
            sched.set_running_for_test(sender);
        }
        let global_before = state.global_time.lock().unwrap().as_nanos();
        let send_to = |target: DetTid| {
            state.receive_rpc(
                reverie::Tid::from_raw(sender.as_raw()),
                (
                    DetTime::new(&config),
                    MmId::initial(sender),
                    GlobalRequest::Sigalrm(super::SigalrmControl::SendTo(target)),
                ),
            )
        };

        assert_eq!(
            send_to(handler).await,
            (None, GlobalResponse::Sigalrm(true))
        );
        assert_eq!(
            send_to(sender).await,
            (None, GlobalResponse::Sigalrm(false))
        );
        let global_time = state.global_time.lock().unwrap();
        assert_eq!(global_time.as_nanos(), global_before, "no clock accounting");
        assert!(!global_time.contains_thread(sender));
    }

    /// A host-input observation, and a report of a path the guest rebound,
    /// belong to the run whose global state receives them. Each is answered
    /// with no logical time, before any clock accounting,
    /// so it cannot move the global clock or the sender's, and the run's
    /// records are written with their end line at normal clean-up. The same
    /// request reaches this state from every backend: the openat handler sends
    /// it over the backend's RPC whether Detcore runs in the tracer or inside
    /// the guest.
    #[tokio::test]
    async fn host_input_observations_are_kept_by_the_run_and_written_at_its_end() {
        use detcore_model::host_input::HostFileIdentity;
        use detcore_model::host_input::HostInputLogEnd;
        use detcore_model::host_input::HostInputRecord;

        let directory = tempfile::tempdir().unwrap();
        let log = directory.path().join("host-inputs");
        let config = Config {
            sequentialize_threads: true,
            record_host_inputs: true,
            host_input_log: Some(log.clone()),
            ..Config::default()
        };
        let state = GlobalState::initialize(&config, false);
        let dettid = DetTid::from_raw(17);
        let identity = HostFileIdentity {
            dev: 33,
            ino: 7,
            size: 100,
            mtime_sec: 1_700_000_000,
            mtime_nsec: 5,
        };
        let mut sender_time = DetTime::new(&config);
        sender_time.add_syscall();
        sender_time.add_syscall();
        let global_before = state.global_time.lock().unwrap().as_nanos();
        let response = state
            .receive_rpc(
                reverie::Tid::from_raw(dettid.as_raw()),
                (
                    sender_time.clone(),
                    MmId::initial(dettid),
                    GlobalRequest::RecordHostInput {
                        path: "/etc/ld.so.cache".into(),
                        syscall: 12,
                        identity,
                    },
                ),
            )
            .await;
        assert_eq!(response, (None, GlobalResponse::RecordHostInput(())));
        for _ in 0..2 {
            let response = state
                .receive_rpc(
                    reverie::Tid::from_raw(dettid.as_raw()),
                    (
                        sender_time.clone(),
                        MmId::initial(dettid),
                        GlobalRequest::RecordHostMutation {
                            path: "/data/F".into(),
                        },
                    ),
                )
                .await;
            assert_eq!(response, (None, GlobalResponse::RecordHostMutation(())));
        }
        {
            let global_time = state.global_time.lock().unwrap();
            assert_eq!(global_time.as_nanos(), global_before, "no clock accounting");
            assert!(
                !global_time.contains_thread(dettid),
                "the sender's clock was not pushed"
            );
        }
        assert!(!log.exists(), "nothing is written while the run goes on");

        state.clean_up(false, &None).await;
        let text = std::fs::read_to_string(&log).unwrap();
        let lines: Vec<&str> = text.lines().collect();
        assert_eq!(lines.len(), 3, "{text}");
        assert_eq!(
            serde_json::from_str::<HostInputRecord>(lines[0]).unwrap(),
            HostInputRecord {
                path: "/etc/ld.so.cache".into(),
                dtid: 17,
                syscall: 12,
                identity,
            }
        );
        // A path rebound twice is one line.
        assert_eq!(
            serde_json::from_str::<detcore_model::host_input::HostMutationRecord>(lines[1])
                .unwrap()
                .mutated,
            "/data/F"
        );
        assert_eq!(
            serde_json::from_str::<HostInputLogEnd>(lines[2]).unwrap(),
            HostInputLogEnd { records: 2 }
        );
    }

    /// Two files on different filesystems can share an inode number; they
    /// are different files and must get different deterministic inodes, with
    /// first-seen mtimes of their own. Keyed by the number alone they shared
    /// one, so a guest comparing inode numbers took them for the same file,
    /// and the second file reported the first one's mtime.
    #[test]
    fn files_on_different_devices_with_one_inode_number_stay_distinct() {
        use super::ObservedMtime;

        let epoch = LogicalTime::from_secs(1_798_761_600);
        let mut pool = super::InodePool::new();
        // 76927 was /sys/bus/i2c/devices on the host that motivated this.
        let sysfs = crate::types::RawInode::new(23, 76_927);
        let rootfs = crate::types::RawInode::new(33, 76_927);

        let (on_sysfs, sysfs_mtime) = pool.add_inode(sysfs, ObservedMtime::HostSpecific, epoch);
        let (on_rootfs, rootfs_mtime) = pool.add_inode(
            rootfs,
            ObservedMtime::Canonical(LogicalTime::from_secs(1)),
            epoch,
        );
        assert_ne!(on_sysfs, on_rootfs, "different files, different det inodes");
        assert_eq!(sysfs_mtime, epoch);
        assert_eq!(rootfs_mtime, LogicalTime::from_secs(1), "its own mtime");
        assert_eq!(
            pool.add_inode(sysfs, ObservedMtime::Unobserved, epoch).0,
            on_sysfs
        );
        assert_eq!(
            pool.add_inode(rootfs, ObservedMtime::Unobserved, epoch).0,
            on_rootfs
        );
    }

    /// Linux numbers a per-process procfs entry when it builds the entry's
    /// dentry, and the host can evict the dentry and build it again under a
    /// new number. Two `lsof` listings of `/proc/3/fd` found the same numbers
    /// in one `--verify` run and new ones in the other, where the host had
    /// rebuilt the entries between the listings. A rebuilt entry reached by
    /// its path keeps its number, so the guest sees the same numbers either
    /// way.
    #[test]
    fn a_rebuilt_procfs_entry_keeps_its_number() {
        use std::path::PathBuf;

        use crate::procfs_inode::DirectoryInodes;
        use crate::procfs_inode::ProcfsDirectory;
        use crate::types::DetInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let proc_device = libc::makedev(0, 23);
        let names: [&[u8]; 4] = [b".", b"..", b"0", b"1"];
        // The guest numbers of two listings of `/proc/3/fd`, in which the
        // host reports the inode numbers `first` and then `second`, as
        // `recv_determinize_inode` numbers them.
        let listings = |with_paths: bool, first: [u64; 4], second: [u64; 4]| -> Vec<DetInode> {
            let mut pool = super::InodePool::new();
            let directory = with_paths.then(|| ProcfsDirectory::new(PathBuf::from("/3/fd")));
            let inodes = DirectoryInodes::new(proc_device, directory);
            let mut numbers = Vec::new();
            for host in [first, second] {
                for (name, ino) in names.iter().zip(host) {
                    let (raw, procfs) = inodes.entry_inode(name, ino);
                    if let Some(procfs) = procfs {
                        let path = pool.procfs_path_key(&procfs);
                        pool.procfs_key(raw, path);
                    }
                    let sighting = super::InodeSighting::Listed(0);
                    numbers.push(pool.add_sighted_inode(raw, seen, t, sighting).0);
                }
            }
            numbers
        };
        let cached = [4_100, 4_000, 4_101, 4_102];
        // The open directory pins `.` and `..`; the fd entries were rebuilt.
        let rebuilt = [4_100, 4_000, 4_901, 4_902];
        assert_eq!(
            listings(true, cached, cached),
            listings(true, cached, rebuilt),
            "a host rebuilding procfs dentries changed guest inode numbers"
        );
        // The control: by host inode alone, the rebuilt entries were renumbered.
        assert_ne!(
            listings(false, cached, cached),
            listings(false, cached, rebuilt)
        );
    }

    /// One procfs entry reached by two paths, as `/proc/<pid>/net/tcp` and
    /// `/proc/<pid>/task/<pid>/net/tcp` are, is one host inode, so it keeps
    /// one number however it is reached, and after the host rebuilds it.
    #[test]
    fn procfs_aliases_share_one_number() {
        use std::path::Path;

        use crate::procfs_inode::path_inode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let device = libc::makedev(0, 23);
        let host = |ino| crate::types::RawInode::new(device, ino);
        let net = path_inode(device, Path::new("/3/net/tcp"));
        let task = path_inode(device, Path::new("/3/task/3/net/tcp"));
        let mut pool = super::InodePool::new();
        let mut number = |raw, path| {
            pool.procfs_key(raw, path);
            pool.add_inode(raw, seen, t).0
        };
        let listed = number(host(4_100), net);
        assert_eq!(number(host(4_100), task), listed, "an alias");
        assert_eq!(number(host(4_900), task), listed, "rebuilt, by the alias");
        assert_eq!(number(host(4_901), net), listed, "rebuilt again");
        let other = number(host(4_200), path_inode(device, Path::new("/3/net/udp")));
        assert_ne!(other, listed);
    }

    /// A rebuilt procfs entry first seen without its path is new to the pool,
    /// as it was before paths were named; its path then leads to its new
    /// number, which it keeps across the next rebuild.
    #[test]
    fn a_numbered_host_inode_keeps_its_number_when_its_path_is_named() {
        use std::path::Path;

        use crate::procfs_inode::path_inode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let device = libc::makedev(0, 23);
        let host = |ino| crate::types::RawInode::new(device, ino);
        let path = path_inode(device, Path::new("/3/fd/0"));
        let mut pool = super::InodePool::new();
        pool.procfs_key(host(4_100), path);
        let first = pool.add_inode(host(4_100), seen, t).0;
        let without_path = pool.add_inode(host(4_900), seen, t).0;
        assert_ne!(without_path, first);
        pool.procfs_key(host(4_900), path);
        assert_eq!(pool.add_inode(host(4_900), seen, t).0, without_path);
        pool.procfs_key(host(4_901), path);
        assert_eq!(pool.add_inode(host(4_901), seen, t).0, without_path);
        assert_eq!(pool.add_inode(host(4_100), seen, t).0, first);
    }

    /// A write to a rebuilt procfs entry, which names the host inode only,
    /// sets the mtime of the number the entry kept, whether the descriptor's
    /// open named the path before the write (`procfs_key` then `set_mtime`) or
    /// after it (a pending mtime).
    #[test]
    fn a_write_to_a_rebuilt_procfs_entry_sets_the_mtime_of_its_number() {
        use std::path::Path;

        use crate::procfs_inode::path_inode;

        let t = LogicalTime::from_nanos(0);
        let written = LogicalTime::from_secs(7);
        let seen = super::ObservedMtime::Unobserved;
        let device = libc::makedev(0, 23);
        let host = |ino| crate::types::RawInode::new(device, ino);
        let path = path_inode(device, Path::new("/3/comm"));
        for open_first in [true, false] {
            let mut pool = super::InodePool::new();
            pool.procfs_key(host(4_100), path);
            let (number, mtime) = pool.add_inode(host(4_100), seen, t);
            assert_ne!(mtime, written);
            if open_first {
                pool.procfs_key(host(4_900), path);
                pool.set_mtime(host(4_900), written);
            } else {
                pool.set_mtime(host(4_900), written);
                pool.procfs_key(host(4_900), path);
            }
            assert_eq!(
                pool.add_inode(host(4_100), seen, t),
                (number, written),
                "open_first={open_first}"
            );
            assert_eq!(pool.add_inode(host(4_900), seen, t), (number, written));
        }
    }

    /// A process ID the kernel gives out again names a new process, whose
    /// procfs entries are new files: Linux drops the old process's entries
    /// when it exits. The new process's `/proc/<pid>` takes a number of its
    /// own and keeps it across rebuilds, while a descriptor still open on
    /// the old process's entry keeps the old number.
    #[test]
    fn a_reused_task_id_names_new_procfs_entries() {
        use std::path::Path;

        use crate::procfs_inode::ProcfsName;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let device = libc::makedev(0, 23);
        let host = |ino| RawInode::new(device, ino);
        let process = ProcfsName::new(device, Path::new("/7"));
        let number = |pool: &mut super::InodePool, raw: RawInode| {
            let path = pool.procfs_path_key(&process);
            pool.procfs_key(raw, path);
            pool.add_inode(raw, seen, t).0
        };
        let mut pool = super::InodePool::new();
        pool.count_incarnation(7);
        let first = number(&mut pool, host(4_100));
        assert_eq!(number(&mut pool, host(4_900)), first, "rebuilt");
        pool.count_incarnation(7);
        let second = number(&mut pool, host(5_000));
        assert_ne!(second, first, "a new process took the old one's number");
        assert_eq!(pool.add_inode(host(4_900), seen, t).0, first, "held");
        assert_eq!(number(&mut pool, host(5_001)), second, "rebuilt");
        // A thread's directory also names the thread's incarnation.
        let thread = ProcfsName::new(device, Path::new("/7/task/9/stat"));
        assert_eq!(thread.tasks, [7, 9]);
        let before = pool.procfs_path_key(&thread);
        pool.count_incarnation(9);
        assert_ne!(pool.procfs_path_key(&thread), before);
    }

    /// A pid Detcore gives out again is a new task: each registration of a
    /// task ID counts one more incarnation (`count_incarnation`), which the
    /// `ProcfsTask` answer reports for the process and the thread, and a file
    /// or directory bound to the earlier incarnation no longer names the
    /// task holding the numbers (`ProcfsTaskAnswer::still_names`).
    #[test]
    fn a_reused_pid_is_a_new_incarnation_for_procfs_files() {
        use crate::procfs::TaskIncarnation;
        use crate::types::DetTid;
        let mut pool = super::InodePool::new();
        assert_eq!(pool.incarnation(7), 0, "an ID no task has held");
        pool.count_incarnation(7);
        pool.count_incarnation(9);
        assert_eq!((pool.incarnation(7), pool.incarnation(9)), (1, 1));
        let (seven, nine) = (DetTid::from_raw(7), DetTid::from_raw(9));
        let first = pool.procfs_task_answer(seven, Some(nine), (true, false));
        assert_eq!(
            first.incarnation(),
            TaskIncarnation {
                process: 1,
                thread: Some(1),
            }
        );
        let bound = first.incarnation();
        assert!(first.still_names(bound));
        // The process exits and is reaped; a new process takes pid 7.
        pool.count_incarnation(7);
        let reused = pool.procfs_task_answer(seven, Some(nine), (true, false));
        assert_eq!(reused.process_incarnation, 2);
        assert_eq!(reused.thread_incarnation, Some(1));
        assert!(!reused.still_names(bound), "the file of the old pid 7");
        assert!(reused.still_names(reused.incarnation()));
        // A thread ID given out again is a new thread of the same process.
        pool.count_incarnation(9);
        let thread_reused = pool.procfs_task_answer(seven, Some(nine), (true, false));
        assert!(!thread_reused.still_names(reused.incarnation()));
        // A task that is gone, or a zombie that was reaped, is no task,
        // whatever its incarnation.
        let gone = pool.procfs_task_answer(seven, None, (false, false));
        assert!(!gone.still_names(gone.incarnation()));
        // The process's own file names no thread.
        assert_eq!(gone.thread_incarnation, None);
    }

    /// Reading a rendered file's state inserts nothing; a write keeps one
    /// entry for that inode only.
    #[test]
    fn a_read_of_rendered_file_state_keeps_nothing() {
        use crate::procfs::listing::RenderedFileChange;
        let mut pool = super::InodePool::new();
        let read = pool.rendered_file_state(5, RenderedFileChange::Read);
        assert_eq!(read, Default::default());
        assert!(pool.rendered_file_states.is_empty());
        let written = pool.rendered_file_state(5, RenderedFileChange::Mtime(3, 4));
        assert_eq!(written.mtime, Some((3, 4)));
        let hinted = pool.rendered_file_state(5, RenderedFileChange::WriteHint(2));
        assert_eq!((hinted.mtime, hinted.write_hint), (Some((3, 4)), 2));
        assert_eq!(
            pool.rendered_file_state(6, RenderedFileChange::Read),
            Default::default()
        );
        assert_eq!(pool.rendered_file_states.len(), 1);
        assert_eq!(
            pool.rendered_file_state(5, RenderedFileChange::Read),
            hinted
        );
    }

    /// A directory snapshot returns its entries after it was taken, and a
    /// task it lists can exit, and a new thread take its ID, in between. The
    /// entry still names the task the snapshot listed: an entry of the old
    /// task rebuilt since keeps the old task's number, rather than adopting
    /// the new task's.
    #[test]
    fn a_snapshot_entry_names_the_task_that_held_its_id() {
        use std::path::Path;

        use crate::procfs_inode::ProcfsName;
        use crate::types::RawInode;

        let t = LogicalTime::from_nanos(0);
        let seen = super::ObservedMtime::Unobserved;
        let device = libc::makedev(0, 23);
        let host = |ino| RawInode::new(device, ino);
        let number = |pool: &mut super::InodePool, raw: RawInode, name: &ProcfsName| {
            let path = pool.procfs_path_key(name);
            pool.procfs_key(raw, path);
            pool.add_inode(raw, seen, t).0
        };
        let process = ProcfsName::new(device, Path::new("/7"));
        let mut pool = super::InodePool::new();
        pool.count_incarnation(7);
        let first = number(&mut pool, host(4_100), &process);
        let listed = process.clone().in_snapshot(pool.threads_counted);
        pool.count_incarnation(7);
        let second = number(&mut pool, host(5_000), &process);
        assert_ne!(second, first, "a new process took the old one's number");
        assert_eq!(
            number(&mut pool, host(4_900), &listed),
            first,
            "the listed process's entry, rebuilt"
        );
        // An ID no thread held when the snapshot was taken names no task.
        let unborn = ProcfsName::new(device, Path::new("/8"));
        let listed = unborn.clone().in_snapshot(pool.threads_counted);
        let unsalted = pool.procfs_path_key(&listed);
        assert_eq!(unsalted, pool.procfs_path_key(&unborn));
        pool.count_incarnation(8);
        assert_eq!(pool.procfs_path_key(&listed), unsalted);
        assert_ne!(pool.procfs_path_key(&unborn), unsalted);
    }

    /// Only an exact whole-second 0 or 1 host mtime is canonical
    /// (https://github.com/rrnewton/hermit/issues/3639).
    #[test]
    fn only_exact_canonical_host_mtimes_are_kept() {
        use super::ObservedMtime;

        assert_eq!(
            ObservedMtime::from_host_mtime(1, 0),
            ObservedMtime::Canonical(LogicalTime::from_secs(1))
        );
        assert_eq!(
            ObservedMtime::from_host_mtime(0, 0),
            ObservedMtime::Canonical(LogicalTime::from_secs(0))
        );
        for (secs, nanos) in [(1, 5), (0, 1), (2, 0), (-1, 0), (1_600_000_000, 0)] {
            assert_eq!(
                ObservedMtime::from_host_mtime(secs, nanos),
                ObservedMtime::HostSpecific,
                "{secs}.{nanos:09} is a real timestamp, not a canonical one"
            );
        }
    }

    /// The first-seen policy is applied at the first stat, whichever syscall
    /// minted the inode, and is not re-applied once resolved.
    #[test]
    fn first_seen_mtime_is_resolved_by_the_first_stat() {
        use super::ObservedMtime;

        let epoch = LogicalTime::from_secs(1_798_761_600);
        let canonical = ObservedMtime::Canonical(LogicalTime::from_secs(1));
        let mut pool = super::InodePool::new();

        // An ordinary file reports the epoch.
        let (_, mtime) = pool.add_inode(
            crate::types::RawInode::new(1, 10),
            ObservedMtime::HostSpecific,
            epoch,
        );
        assert_eq!(mtime, epoch);
        // A canonical file keeps its mtime.
        let (_, mtime) = pool.add_inode(crate::types::RawInode::new(1, 11), canonical, epoch);
        assert_eq!(mtime, LogicalTime::from_secs(1));

        // A read or getdents mints the inode first; the later stat decides.
        let (_, mtime) = pool.add_inode(
            crate::types::RawInode::new(1, 12),
            ObservedMtime::Unobserved,
            epoch,
        );
        assert_eq!(mtime, epoch, "an unresolved mtime reads as the epoch");
        let (_, mtime) = pool.add_inode(crate::types::RawInode::new(1, 12), canonical, epoch);
        assert_eq!(mtime, LogicalTime::from_secs(1));

        // Once resolved, a later observation does not change it.
        let (_, mtime) = pool.add_inode(crate::types::RawInode::new(1, 10), canonical, epoch);
        assert_eq!(mtime, epoch);
        let (_, mtime) = pool.add_inode(
            crate::types::RawInode::new(1, 11),
            ObservedMtime::HostSpecific,
            epoch,
        );
        assert_eq!(mtime, LogicalTime::from_secs(1));
    }
}

#[cfg(test)]
mod robust_exit_clock_tests {
    use std::sync::Mutex;
    use std::task::Poll;

    use nix::sys::signal::Signal;
    use reverie::ExitStatus;
    use reverie::GlobalRPC;
    use reverie::GlobalTool;
    use reverie::Tid;
    use reverie::Tool;

    use super::GlobalRequest;
    use super::GlobalResponse;
    use super::GlobalState;
    use crate::Detcore;
    use crate::ThreadState;
    use crate::config::Config;
    use crate::ivar::Ivar;
    use crate::resources::Resources;
    use crate::scheduler::DEFAULT_PRIORITY;
    use crate::scheduler::SkipTurn;
    use crate::scheduler::ThreadNextTurn;
    use crate::tool_local::RobustListExit;
    use crate::tool_local::RobustListWake;
    use crate::types::*;

    #[derive(Debug, PartialEq)]
    struct WakeObservation {
        wakes: Vec<(DetTid, FutexID)>,
        counts: Vec<u64>,
        clocks: serde_json::Value,
        turn: u64,
    }

    #[derive(Debug)]
    struct RpcObservation {
        sender: DetTid,
        kind: &'static str,
        accepted: bool,
        clocks: serde_json::Value,
        queued: Vec<DetTid>,
        waiters: usize,
        turn: u64,
    }

    // This is only a sender-bound transport, as in Reverie's WrappedFrom.
    // Replies, clock accounting, wakes and deregistration come from GlobalState.
    struct ExitRpc<'a> {
        state: &'a GlobalState,
        sender: DetTid,
        observations: &'a Mutex<Vec<WakeObservation>>,
        rpc_observations: &'a Mutex<Vec<RpcObservation>>,
    }

    #[reverie::tool]
    impl GlobalRPC<GlobalState> for ExitRpc<'_> {
        async fn send_rpc(
            &self,
            request: <GlobalState as GlobalTool>::Request,
        ) -> <GlobalState as GlobalTool>::Response {
            let kind = match &request.2 {
                GlobalRequest::RobustListWakes(wakes) if wakes.is_empty() => "empty-wake",
                GlobalRequest::RobustListWakes(_) => "wake",
                GlobalRequest::DeregisterThread(_) => "deregister",
                _ => panic!("unexpected exit RPC: {:?}", request.2),
            };
            let wakes = match &request.2 {
                GlobalRequest::RobustListWakes(wakes) if !wakes.is_empty() => Some(wakes.clone()),
                _ => None,
            };
            let response = self
                .state
                .receive_rpc(Tid::from_raw(self.sender.as_raw()), request)
                .await;
            let clocks = serde_json::to_value(&*self.state.global_time.lock().unwrap()).unwrap();
            {
                let sched = self.state.sched.lock().unwrap();
                self.rpc_observations.lock().unwrap().push(RpcObservation {
                    sender: self.sender,
                    kind,
                    accepted: !matches!(response.1, GlobalResponse::ThreadExited),
                    clocks: clocks.clone(),
                    queued: sched.run_queue.tids().copied().collect(),
                    waiters: sched
                        .blocked
                        .futex_waiters
                        .values()
                        .map(Vec::len)
                        .sum::<usize>(),
                    turn: sched.turn,
                });
            }
            if let Some(wakes) = wakes {
                let GlobalResponse::RobustListWakes(counts) = &response.1 else {
                    panic!("a complete admitted exit batch was refused: {response:?}");
                };
                let clocks =
                    serde_json::to_value(&*self.state.global_time.lock().unwrap()).unwrap();
                let turn = self.state.sched.lock().unwrap().turn;
                self.observations.lock().unwrap().push(WakeObservation {
                    wakes,
                    counts: counts.clone(),
                    clocks,
                    turn,
                });
            }
            response
        }

        fn config(&self) -> &Config {
            &self.state.cfg
        }
    }

    struct Fixture {
        state: GlobalState,
        tool: Detcore,
        owners: [ThreadState<()>; 2],
        initial_clocks: [DetTime; 2],
        waiters: [DetTid; 2],
        peer: DetTid,
        futexes: [FutexID; 2],
        observations: Mutex<Vec<WakeObservation>>,
        rpc_observations: Mutex<Vec<RpcObservation>>,
    }

    impl Fixture {
        fn new(
            reason: RobustListExit,
            equal_clocks: bool,
            empty_owner: Option<usize>,
            needs_rpc_cancellation: bool,
        ) -> Self {
            let config = Config {
                sequentialize_threads: true,
                ..Config::default().with_backend(|backend| {
                    backend.needs_killed_thread_rpc_cancellation = needs_rpc_cancellation
                })
            };
            let state = GlobalState::initialize(&config, false);
            let parent = DetTid::from_raw(1);
            let leader = DetTid::from_raw(17);
            let worker = DetTid::from_raw(18);
            let waiters = [DetTid::from_raw(21), DetTid::from_raw(23)];
            let peer = DetTid::from_raw(25);
            let mm = MmId::initial(leader);
            let mut first = ThreadState::new(leader, &config, ());
            first.detpid = Some(leader);
            let mut second = first.clone();
            second.dettid = worker;
            let mut inherited = DetTime::new(&config);
            inherited.add_syscall_with_cost(1_000);
            let first_initial = inherited.clone_for_child();
            inherited.add_syscall_with_cost(370);
            let second_initial = inherited.clone_for_child();
            first.thread_logical_time = first_initial.clone();
            second.thread_logical_time = second_initial.clone();
            first
                .thread_logical_time
                .add_syscall_with_cost(if equal_clocks { 407 } else { 37 });
            second.thread_logical_time.add_syscall_with_cost(37);
            first.record_robust_list_head(Some(0x404100));
            second.record_robust_list_head(Some(0x404200));
            let object = SharedMemoryObjectId::Anonymous {
                origin: MmId::initial(parent),
                sequence: 1,
            };
            let futexes = [FutexID::shared(object, 0), FutexID::shared(object, 8)];
            first.stage_robust_list_wakes(
                reason,
                vec![
                    (
                        worker,
                        if empty_owner == Some(1) {
                            Vec::new()
                        } else {
                            vec![RobustListWake { futex: futexes[1] }]
                        },
                    ),
                    (
                        leader,
                        if empty_owner == Some(0) {
                            Vec::new()
                        } else {
                            vec![RobustListWake { futex: futexes[0] }]
                        },
                    ),
                ],
            );
            {
                let mut sched = state.sched.lock().unwrap();
                sched.thread_tree.add_child(parent, parent, true);
                sched.thread_tree.add_child(parent, leader, true);
                sched.thread_tree.add_child(leader, worker, false);
                for tid in [waiters[0], waiters[1], peer] {
                    sched.thread_tree.add_child(parent, tid, true);
                }
                for tid in [leader, worker, waiters[0], waiters[1], peer] {
                    sched.priorities.insert(tid, DEFAULT_PRIORITY);
                    sched.next_turns.insert(
                        tid,
                        ThreadNextTurn {
                            dettid: tid,
                            child_tid_addr: 0,
                            req: Ivar::new(),
                            resp: Ivar::new(),
                            protocol: Default::default(),
                        },
                    );
                    sched.install_test_exec_incarnation(
                        tid,
                        if tid == leader || tid == worker {
                            mm
                        } else {
                            MmId::initial(tid)
                        },
                    );
                }
                // Both owners have returned from their last guest turn and
                // retain its empty next request until their exit callbacks.
                // The independent peer is ready, but cannot pass quiescence.
                sched.runqueue_push_back(leader);
                sched.runqueue_push_back(worker);
                sched.runqueue_push_back(peer);
                sched.next_turns[&peer].req.put(Ok(Resources::new(peer)));
                for (waiter, futex) in waiters.into_iter().zip(futexes) {
                    sched.sleep_futex_waiter(&waiter, futex, None, u32::MAX, None);
                }
            }
            {
                let mut time = state.global_time.lock().unwrap();
                for (tid, clock) in [(leader, &first_initial), (worker, &second_initial)] {
                    time.update_global_time(tid, clock.as_nanos(), clock.inherited_nanos());
                }
            }
            let tool = Detcore::new(Tid::from_raw(leader.as_raw()), &config);
            Self {
                state,
                tool,
                owners: [first, second],
                initial_clocks: [first_initial, second_initial],
                waiters,
                peer,
                futexes,
                observations: Mutex::new(Vec::new()),
                rpc_observations: Mutex::new(Vec::new()),
            }
        }

        fn assert_clocks(&self, completed: &[usize]) -> serde_json::Value {
            let time = self.state.global_time.lock().unwrap();
            let snapshot = serde_json::to_value(&*time).unwrap();
            let epoch = DetTime::new(&self.state.cfg).as_nanos();
            let mut expected = epoch;
            for (index, owner) in self.owners.iter().enumerate() {
                let clock = if completed.contains(&index) {
                    &owner.thread_logical_time
                } else {
                    &self.initial_clocks[index]
                };
                assert_eq!(time.threads_time(owner.dettid), clock.as_nanos());
                assert_eq!(
                    snapshot["inherited_time"][owner.dettid.as_raw().to_string()],
                    serde_json::to_value(clock.inherited_nanos()).unwrap()
                );
                expected = expected + (clock.as_nanos() - epoch - clock.inherited_nanos());
            }
            assert_eq!(
                time.as_nanos(),
                expected,
                "only each owner's own uninherited work contributes"
            );
            snapshot
        }

        async fn exit(&self, index: usize, status: ExitStatus) {
            self.exit_thread(self.owners[index].clone(), status).await;
        }

        async fn exit_thread(&self, thread: ThreadState<()>, status: ExitStatus) {
            let rpc = ExitRpc {
                state: &self.state,
                sender: thread.dettid,
                observations: &self.observations,
                rpc_observations: &self.rpc_observations,
            };
            let exit =
                self.tool
                    .on_exit_thread(Tid::from_raw(rpc.sender.as_raw()), &rpc, thread, status);
            let mut exit = std::pin::pin!(exit);
            // On the CLI's current-thread ptrace route, Ready on the first
            // poll excludes a new queued-observer window inside this callback.
            // Multi-thread embeddings still admit concurrent scheduler reads;
            // the RPC observations separately check the actual global actions.
            assert!(
                matches!(futures::poll!(exit.as_mut()), Poll::Ready(Ok(()))),
                "exit callback yielded before its accounting and cleanup completed"
            );
        }

        async fn nonmember_exit(&self, raw_tid: i32) {
            let mut thread = self.owners[0].clone();
            thread.dettid = DetTid::from_raw(raw_tid);
            thread.thread_logical_time = DetTime::new(&self.state.cfg);
            let tid = thread.dettid;
            {
                let mut sched = self.state.sched.lock().unwrap();
                sched
                    .thread_tree
                    .add_child(self.owners[0].dettid, tid, false);
                sched.priorities.insert(tid, DEFAULT_PRIORITY);
                sched.next_turns.insert(
                    tid,
                    ThreadNextTurn {
                        dettid: tid,
                        child_tid_addr: 0,
                        req: Ivar::new(),
                        resp: Ivar::new(),
                        protocol: Default::default(),
                    },
                );
                sched.install_test_exec_incarnation(tid, thread.mm_id);
                sched.runqueue_push_back(tid);
            }
            let before = self.rpc_observations.lock().unwrap().len();
            self.exit_thread(thread, ExitStatus::Exited(0)).await;
            let observations = self.rpc_observations.lock().unwrap();
            assert_eq!(
                observations.len(),
                before + 1,
                "a nonmember sent an acknowledgement or wake RPC"
            );
            assert_eq!(observations[before].sender, tid);
            assert_eq!(observations[before].kind, "deregister");
            assert!(observations[before].accepted);
        }
    }

    #[tokio::test]
    async fn robust_exit_acknowledgements_preserve_global_action_order_and_eligibility() {
        for order in [[0, 1], [1, 0]] {
            for empty_owner in [0, 1] {
                let f = Fixture::new(RobustListExit::ExitGroup, false, Some(empty_owner), false);
                f.nonmember_exit(30).await;
                assert!(f.observations.lock().unwrap().is_empty());
                let queue_before_first: Vec<_> = f
                    .state
                    .sched
                    .lock()
                    .unwrap()
                    .run_queue
                    .tids()
                    .copied()
                    .collect();
                let first_start = f.rpc_observations.lock().unwrap().len();
                f.exit(order[0], ExitStatus::Exited(0)).await;
                let first_clocks = f.assert_clocks(&[order[0]]);
                {
                    let observations = f.rpc_observations.lock().unwrap();
                    let actions = &observations[first_start..];
                    assert_eq!(
                        actions.iter().map(|a| a.kind).collect::<Vec<_>>(),
                        ["empty-wake", "deregister"]
                    );
                    assert!(actions.iter().all(|a| a.accepted
                        && a.sender == f.owners[order[0]].dettid
                        && a.clocks == first_clocks
                        && a.waiters == 2
                        && a.turn == 0));
                    assert_eq!(
                        actions[0].queued, queue_before_first,
                        "empty acknowledgement changed scheduler eligibility"
                    );
                }
                f.nonmember_exit(31).await;
                f.assert_clocks(&[order[0]]);
                assert!(
                    f.observations.lock().unwrap().is_empty(),
                    "a nonmember completed the physical-exit barrier"
                );
                let queue_before_last: Vec<_> = f
                    .state
                    .sched
                    .lock()
                    .unwrap()
                    .run_queue
                    .tids()
                    .copied()
                    .collect();
                let last_start = f.rpc_observations.lock().unwrap().len();
                f.exit(order[1], ExitStatus::Exited(0)).await;
                let final_clocks = f.assert_clocks(&[0, 1]);
                {
                    let observations = f.rpc_observations.lock().unwrap();
                    let actions = &observations[last_start..];
                    assert_eq!(
                        actions.iter().map(|a| a.kind).collect::<Vec<_>>(),
                        ["empty-wake", "wake", "deregister"]
                    );
                    assert!(actions.iter().all(|a| a.accepted
                        && a.sender == f.owners[order[1]].dettid
                        && a.clocks == final_clocks
                        && a.turn == 0));
                    assert_eq!(actions[0].waiters, 2);
                    assert_eq!(actions[1].waiters, 1);
                    assert_eq!(actions[0].queued, queue_before_last);
                    assert_eq!(
                        actions[1].queued, queue_before_last,
                        "wake bypassed deferred admission"
                    );
                }
                f.nonmember_exit(32).await;
                f.assert_clocks(&[0, 1]);
                assert_eq!(f.observations.lock().unwrap().len(), 1);
                let observations = f.rpc_observations.lock().unwrap();
                for owner in &f.owners {
                    assert_eq!(
                        observations
                            .iter()
                            .filter(|a| a.sender == owner.dettid && a.kind == "empty-wake")
                            .count(),
                        1,
                        "each unique matching owner, including an empty-wake owner, must acknowledge before the batch clears"
                    );
                }
            }
        }
    }

    #[tokio::test]
    async fn robust_exit_callbacks_preserve_owner_clocks_across_arrival_orders() {
        for reason in [
            RobustListExit::ExitGroup,
            RobustListExit::Signal(libc::SIGTERM),
        ] {
            for equal_clocks in [false, true] {
                for empty_owner in [None, Some(0), Some(1)] {
                    let mut results = Vec::new();
                    for order in [[0, 1], [1, 0]] {
                        let f = Fixture::new(reason, equal_clocks, empty_owner, false);
                        let status = match reason {
                            RobustListExit::ExitGroup => ExitStatus::Exited(0),
                            RobustListExit::Signal(_) => {
                                ExitStatus::Signaled(Signal::SIGTERM, false)
                            }
                        };
                        f.exit(order[0], status).await;
                        f.assert_clocks(&[order[0]]);
                        assert!(f.observations.lock().unwrap().is_empty());
                        {
                            let sched = f.state.sched.lock().unwrap();
                            assert_eq!(
                                sched
                                    .blocked
                                    .futex_waiters
                                    .values()
                                    .map(Vec::len)
                                    .sum::<usize>(),
                                2
                            );
                            assert_eq!(sched.turn, 0);
                        }
                        // An actually runnable peer must still wait for the
                        // remaining owner's outstanding next request.
                        {
                            let last = Err(SkipTurn);
                            let turn = crate::scheduler::do_a_turn_blocking(
                                f.state.sched.clone(),
                                f.state.global_time.clone(),
                                &last,
                            );
                            let mut turn = std::pin::pin!(turn);
                            assert!(matches!(futures::poll!(turn.as_mut()), Poll::Pending));
                        }
                        f.exit(order[0], status).await;
                        f.assert_clocks(&[order[0]]);
                        assert!(
                            f.observations.lock().unwrap().is_empty(),
                            "duplicate physical exit released an incomplete group"
                        );
                        f.exit(order[1], status).await;
                        let clocks = f.assert_clocks(&[0, 1]);
                        {
                            let observations = f.observations.lock().unwrap();
                            assert_eq!(observations.len(), 1);
                            let expected: Vec<_> = (0..2)
                                .filter(|i| Some(*i) != empty_owner)
                                .map(|i| (f.owners[i].dettid, f.futexes[i]))
                                .collect();
                            assert_eq!(observations[0].wakes, expected);
                            assert_eq!(observations[0].counts, vec![1; expected.len()]);
                            assert_eq!(
                                observations[0].clocks, clocks,
                                "all owner clocks must be accounted before wake admission"
                            );
                            assert_eq!(observations[0].turn, 0);
                        }
                        f.exit(order[1], status).await;
                        assert_eq!(f.assert_clocks(&[0, 1]), clocks);
                        assert_eq!(
                            f.observations.lock().unwrap().len(),
                            1,
                            "repeated cleanup emitted a second batch"
                        );
                        {
                            let sched = f.state.sched.lock().unwrap();
                            assert!(!sched.run_queue.contains_tid(f.waiters[0]));
                            assert!(!sched.run_queue.contains_tid(f.waiters[1]));
                        }
                        // Drive the real step1/step2 drain and one peer turn;
                        // no test-only scheduler implementation or reply shim.
                        let result = crate::scheduler::do_a_turn_blocking(
                            f.state.sched.clone(),
                            f.state.global_time.clone(),
                            &Err(SkipTurn),
                        )
                        .await;
                        assert!(result.is_ok());
                        let sched = f.state.sched.lock().unwrap();
                        assert_eq!(sched.turn, 1);
                        let queued: Vec<_> = sched.run_queue.tids().copied().collect();
                        for (i, waiter) in f.waiters.iter().enumerate() {
                            assert_eq!(queued.contains(waiter), Some(i) != empty_owner);
                        }
                        assert!(queued.contains(&f.peer));
                        results.push((
                            clocks,
                            queued,
                            std::mem::take(&mut *f.observations.lock().unwrap()),
                        ));
                    }
                    assert_eq!(
                        results[0], results[1],
                        "callback order changed final clocks, typed wake observations or the actual scheduler drain"
                    );
                }
            }
        }
    }

    #[tokio::test]
    async fn robust_exit_callbacks_keep_rejected_or_mismatched_batches_incomplete() {
        for rejection in ["old-mm", "tombstone", "wrong-signal", "normal-exit"] {
            let f = Fixture::new(
                RobustListExit::Signal(libc::SIGTERM),
                false,
                None,
                rejection == "tombstone",
            );
            let rejected = f.owners[0].dettid;
            let mm = f.owners[0].mm_id;
            f.exit(1, ExitStatus::Signaled(Signal::SIGTERM, false))
                .await;
            let replacement_request = Ivar::new();
            if rejection == "old-mm" {
                let mut sched = f.state.sched.lock().unwrap();
                sched.install_test_exec_incarnation(rejected, mm.for_exec(rejected));
                sched.next_turns.insert(
                    rejected,
                    ThreadNextTurn {
                        dettid: rejected,
                        child_tid_addr: 0,
                        req: replacement_request.clone(),
                        resp: Ivar::new(),
                        protocol: Default::default(),
                    },
                );
                let mut replacement_time = f.owners[0].thread_logical_time.clone();
                replacement_time.add_syscall_with_cost(500);
                f.state.global_time.lock().unwrap().update_global_time(
                    rejected,
                    replacement_time.as_nanos(),
                    replacement_time.inherited_nanos(),
                );
            } else if rejection == "tombstone" {
                // Use the real cancelling backend gate; keep its matching Mm.
                let mut sched = f.state.sched.lock().unwrap();
                sched.logically_kill_thread(&rejected, &rejected, mm);
            }
            let before = serde_json::to_value(&*f.state.global_time.lock().unwrap()).unwrap();
            let status = match rejection {
                "wrong-signal" => ExitStatus::Signaled(Signal::SIGKILL, false),
                "normal-exit" => ExitStatus::Exited(0),
                _ => ExitStatus::Signaled(Signal::SIGTERM, false),
            };
            f.exit(0, status).await;
            assert!(
                f.observations.lock().unwrap().is_empty(),
                "{rejection} released a group"
            );
            if rejection == "old-mm" || rejection == "tombstone" {
                assert_eq!(
                    serde_json::to_value(&*f.state.global_time.lock().unwrap()).unwrap(),
                    before,
                    "rejected acknowledgement changed clock state"
                );
            }
            let sched = f.state.sched.lock().unwrap();
            assert_eq!(
                sched
                    .blocked
                    .futex_waiters
                    .values()
                    .map(Vec::len)
                    .sum::<usize>(),
                2,
                "rejected group lost a real waiter"
            );
            assert!(
                f.waiters
                    .iter()
                    .all(|tid| !sched.run_queue.contains_tid(*tid))
            );
            if rejection == "old-mm" {
                assert!(sched.rpc_incarnation_matches(rejected, mm.for_exec(rejected)));
                assert_eq!(
                    sched.next_turns[&rejected].req, replacement_request,
                    "old cleanup destroyed replacement registration"
                );
            }
        }
    }

    #[tokio::test]
    #[should_panic(expected = "Attempted to update tid 17 time")]
    async fn robust_exit_clock_ack_still_refuses_a_backwards_owner_sample() {
        let f = Fixture::new(RobustListExit::ExitGroup, false, None, false);
        let owner = &f.owners[0];
        let mut later = owner.thread_logical_time.clone();
        later.add_syscall_with_cost(1);
        f.state.global_time.lock().unwrap().update_global_time(
            owner.dettid,
            later.as_nanos(),
            later.inherited_nanos(),
        );
        f.exit(0, ExitStatus::Exited(0)).await;
    }

    #[tokio::test]
    async fn backend_failure_preserves_consuming_robust_exit_clock_accounting() {
        for order in [[0, 1], [1, 0]] {
            let f = Fixture::new(RobustListExit::ExitGroup, false, None, false);
            let selected = {
                let mut sched = f.state.sched.lock().unwrap();
                sched.next_turns.get_mut(&f.peer).unwrap().req = Ivar::new();
                sched.select_test_turn().unwrap()
            };
            let responses = {
                let sched = f.state.sched.lock().unwrap();
                f.waiters.map(|tid| sched.next_turns[&tid].resp.clone())
            };
            let mut turn = std::pin::pin!(crate::scheduler::finish_selected_turn(
                f.state.sched.clone(),
                f.state.global_time.clone(),
                selected.0,
                selected.1,
                selected.2,
            ));
            assert!(futures::poll!(turn.as_mut()).is_pending());
            f.state.report_backend_failure(reverie::BackendFailure {
                pid: Tid::from_raw(17),
                tid: Tid::from_raw(18),
                phase: "native robust cleanup control",
            });
            for index in order {
                f.exit(index, ExitStatus::Exited(0)).await;
            }
            assert!(matches!(futures::poll!(turn.as_mut()), Poll::Ready(Err(_))));
            f.assert_clocks(&[0, 1]);
            let observations = f.observations.lock().unwrap();
            assert_eq!(observations.len(), 1, "one complete batch");
            assert_eq!(observations[0].counts, vec![1, 1]);
            assert!(
                responses
                    .iter()
                    .all(|response| response.try_read().is_none())
            );
            let mut sched = f.state.sched.lock().unwrap();
            assert_eq!(sched.turn, 0);
            for owner in &f.owners {
                assert!(!sched.next_turns.contains_key(&owner.dettid));
                assert!(!sched.note_deregistration_accounted(owner.dettid));
            }
        }
    }
}