hermit-detcore 0.5.2

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
/*
 * 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.
 */

//! System calls for dealing with threads and concurrency.

use std::collections::HashMap;
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;

use detcore_model::bundled_signal_facts;
use procfs::process::Process;
use rand::Rng;
use reverie::BackendCapabilities;
use reverie::Error;
use reverie::Guest;
use reverie::Pid;
use reverie::Stack;
use reverie::syscalls;
use reverie::syscalls::Addr;
use reverie::syscalls::AddrMut;
use reverie::syscalls::CloneFlags;
use reverie::syscalls::Errno;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::ReadAddr;
use reverie::syscalls::Syscall;
use reverie::syscalls::SyscallInfo;
use reverie::syscalls::Timespec;
use reverie::syscalls::WaitPidFlag;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::trace;

use crate::SharedMemoryObjectId;
use crate::config::BlockingMode;
use crate::config::Config;
use crate::config::SchedHeuristic;
use crate::memory::MemoryMetadata;
use crate::record_or_replay::RecordOrReplay;
use crate::resources::ExternalOpId;
use crate::resources::Permission;
use crate::resources::ResourceID;
use crate::resources::Resources;
use crate::scheduler::FutexSignalWatch;
use crate::scheduler::HostTimedSignalScope;
use crate::scheduler::SchedValue;
use crate::scheduler::pid_namespaces::GuestPidAnswer;
use crate::syscalls::guest_siginfo::GuestSiginfo;
use crate::syscalls::helpers::NonblockableSyscall;
use crate::syscalls::helpers::RestartCall;
use crate::syscalls::helpers::keep_restart_block;
use crate::syscalls::helpers::record_retry_event;
use crate::syscalls::helpers::retry_nonblocking_syscall;
use crate::syscalls::helpers::retry_nonblocking_syscall_with_timeout;
use crate::syscalls::robust_list;
use crate::tool_global::FutexAction;
use crate::tool_global::ResumeStatus;
use crate::tool_global::await_exact_child_physical_exit;
use crate::tool_global::cancel_exec;
use crate::tool_global::child_tid_clear_address;
use crate::tool_global::consume_child_wait;
use crate::tool_global::create_child_thread;
use crate::tool_global::futex_action;
use crate::tool_global::hold_turn_for_group_exit;
use crate::tool_global::host_timed_signals;
use crate::tool_global::prepare_exec;
use crate::tool_global::process_group;
use crate::tool_global::ready_child_wait;
use crate::tool_global::record_host_timed_signals;
use crate::tool_global::resource_request;
use crate::tool_global::set_child_tid_address;
use crate::tool_global::thread_is_live;
use crate::tool_global::thread_observe_time;
use crate::tool_global::wait_for_child_lifecycle;
use crate::tool_global::yield_once;
use crate::tool_local::Detcore;
use crate::tool_local::PendingVfork;
use crate::tool_local::RobustListExit;
use crate::tool_local::RobustListOwner;
use crate::tool_local::RobustListWake;
use crate::types::ChildWaitExitClass;
use crate::types::ChildWaitSelector;
use crate::types::ChildWaitSpec;
use crate::types::DetPid;
use crate::types::DetTid;
use crate::types::ExactChildWaitState;
use crate::types::FutexID;
use crate::types::LogicalTime;
use crate::types::SigWrapper;

#[path = "emulated_waitid.rs"]
mod emulated_waitid;

// Preserve the historical Detcore ABI while hiding the host's configured CPU
// count. This represents one virtual CPU in a fixed 128-bit kernel mask.
pub(crate) const VIRTUAL_CPUSET_BYTES: usize = 16;

const IOPRIO_WHO_PROCESS: libc::c_int = 1;
const IOPRIO_WHO_PGRP: libc::c_int = 2;
const IOPRIO_WHO_USER: libc::c_int = 3;
const IOPRIO_CLASS_SHIFT: libc::c_int = 13;
const IOPRIO_CLASS_BE: libc::c_int = 2;
const IOPRIO_BE_NORM: libc::c_int = 4;
const IOPRIO_DEFAULT_EFFECTIVE: libc::c_int =
    (IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT) | IOPRIO_BE_NORM;

// sched_attr wire contract, from include/uapi/linux/sched/types.h. These are
// spelled out rather than derived from `size_of::<libc::sched_attr>()`: the
// value the kernel reports back on the E2BIG paths and the offset past which
// trailing bytes must be zero are properties of the KERNEL's struct, which is
// larger than the libc crate's 48-byte mirror. Deriving either from a Rust type
// would silently re-point the contract if that type ever grows a field.
const SCHED_ATTR_SIZE_VER0: u32 = 48; // first published struct
const SCHED_ATTR_SIZE_VER1: u32 = 56; // adds sched_util_{min,max}
/// The kernel's own `sizeof(struct sched_attr)`. Two things key off it: bytes at
/// or beyond this offset must be zero, and it is the value written back into
/// `uattr->size` when the request is refused with E2BIG.
const SCHED_ATTR_KERNEL_SIZE: u32 = SCHED_ATTR_SIZE_VER1;
/// `sched_copy_attr` refuses a buffer larger than one page. Hermit is x86-64
/// only, where PAGE_SIZE is 4096.
const SCHED_ATTR_MAX_SIZE: u32 = 4096;

/// The scheduling policy Detcore presents as every thread's current one.
///
/// This is not a guess about the host. It is the value `handle_sched_getattr`
/// writes back for every thread, so it is the policy a guest observes and the
/// only policy `SCHED_FLAG_KEEP_POLICY` can be keeping.
const VIRTUAL_CURRENT_POLICY: u32 = libc::SCHED_OTHER as u32;

/// Outcome of scanning the bytes past the kernel's `struct sched_attr`.
#[derive(Debug, Eq, PartialEq)]
enum TailVerdict {
    AllZero,
    /// A byte the kernel does not understand was set: `copy_struct_from_user`
    /// reports this as E2BIG, after storing its own size back.
    NotZeroed,
    /// Guest memory could not be read before any non-zero byte was seen.
    Faulted,
}

/// Reads of eight bytes or fewer take safeptrace's `PTRACE_PEEKDATA` path,
/// which reads a whole aligned word and BYPASSES GUEST PAGE PROTECTIONS. Only a
/// read strictly larger than that reaches `process_vm_readv`, which honours
/// them. Note this is not symmetric with the write side: `write` special-cases
/// a length of exactly eight, so splitting a write in half escapes it, while
/// `read` special-cases eight *or fewer* and splitting only makes it worse.
const MIN_PROTECTION_RESPECTING_READ: usize = std::mem::size_of::<u64>() + 1;

/// Scan `tail_len` bytes at `base + tail_off` the way `check_zeroed_user` does.
///
/// Two things here are load-bearing and neither is visible from the outcome
/// alone, only from the ORDER the outcome is reached in.
///
/// First, Linux scans FORWARD and stops at the first thing it finds. A non-zero
/// byte followed later by an unreadable page is E2BIG, because the scan never
/// reaches the page; an unreadable page reached before any non-zero byte is
/// EFAULT. Reading the whole tail up front and judging afterwards collapses
/// both into EFAULT and silently changes the errno a guest sees.
///
/// Second, every read here is kept strictly larger than eight bytes, for the
/// reason on [`MIN_PROTECTION_RESPECTING_READ`]. Where the remainder is
/// smaller, the window is extended BACKWARDS over bytes already scanned rather
/// than shortened; re-reading a byte is free and only the new bytes are judged.
fn scan_tail_is_zeroed<M: MemoryAccess>(
    memory: &M,
    base: AddrMut<u8>,
    tail_off: usize,
    tail_len: usize,
) -> TailVerdict {
    const CHUNK: usize = 256;
    let mut done = 0usize;
    let mut buf = [0u8; CHUNK];
    while done < tail_len {
        let remaining = tail_len - done;
        let want = remaining.min(CHUNK);
        // Extend backwards when the remainder alone would be a small read.
        // The window may reach back past the start of the tail into the
        // `sched_attr` prefix: the kernel has already read those bytes, so they
        // are readable, and only the new bytes are judged below. Clamping this
        // to `done` alone left a 1-byte tail with nowhere to grow into and
        // issued exactly the small read this constant exists to avoid.
        let back = MIN_PROTECTION_RESPECTING_READ
            .saturating_sub(want)
            .min(done + tail_off);
        let read_len = want + back;
        let start = tail_off + done - back;
        let Some(addr) = base
            .as_raw()
            .checked_add(start)
            .and_then(AddrMut::<u8>::from_raw)
        else {
            return TailVerdict::Faulted;
        };
        // A failed read means zero NEW bytes were obtained; the `got < read_len`
        // check below is what turns that into `Faulted`. Written `unwrap_or(0)`
        // rather than `unwrap_or_default()` so the 0 stays visible.
        let got = memory.read(addr, &mut buf[..read_len]).unwrap_or(0);
        // Judge only the bytes that are genuinely new AND genuinely read.
        let new_from = back.min(got);
        if buf[new_from..got].iter().any(|byte| *byte != 0) {
            return TailVerdict::NotZeroed;
        }
        if got < read_len {
            // The scan stopped at an unreadable byte with nothing non-zero
            // before it, which is `check_zeroed_user`'s -EFAULT.
            return TailVerdict::Faulted;
        }
        done += want;
    }
    TailVerdict::AllZero
}

// Scheduling policies, from include/uapi/linux/sched.h.
const SCHED_FIFO: u32 = 1;
const SCHED_RR: u32 = 2;
const SCHED_DEADLINE: u32 = 6;
const SCHED_EXT: u32 = 7;
/// The kernel's `MAX_RT_PRIO`; valid real-time priorities are 1..=99.
const MAX_RT_PRIO: u32 = 100;

// Byte offsets of the fields this handler inspects. Taken from the UAPI struct
// rather than from a Rust mirror, for the reason given above.
const SCHED_ATTR_OFF_POLICY: usize = 4;
const SCHED_ATTR_OFF_FLAGS: usize = 8;
const SCHED_ATTR_OFF_PRIORITY: usize = 20;
const SCHED_ATTR_OFF_RUNTIME: usize = 24;
const SCHED_ATTR_OFF_DEADLINE: usize = 32;
const SCHED_ATTR_OFF_PERIOD: usize = 40;

// sched_attr.sched_flags bits, from include/uapi/linux/sched.h.
const SCHED_FLAG_RESET_ON_FORK: u64 = 0x01;
const SCHED_FLAG_RECLAIM: u64 = 0x02;
const SCHED_FLAG_DL_OVERRUN: u64 = 0x04;
const SCHED_FLAG_KEEP_POLICY: u64 = 0x08;
const SCHED_FLAG_KEEP_PARAMS: u64 = 0x10;
const SCHED_FLAG_UTIL_CLAMP_MIN: u64 = 0x20;
const SCHED_FLAG_UTIL_CLAMP_MAX: u64 = 0x40;
const SCHED_FLAG_UTIL_CLAMP: u64 = SCHED_FLAG_UTIL_CLAMP_MIN | SCHED_FLAG_UTIL_CLAMP_MAX;
const SCHED_FLAG_ALL: u64 = SCHED_FLAG_RESET_ON_FORK
    | SCHED_FLAG_RECLAIM
    | SCHED_FLAG_DL_OVERRUN
    | SCHED_FLAG_KEEP_POLICY
    | SCHED_FLAG_KEEP_PARAMS
    | SCHED_FLAG_UTIL_CLAMP;

/// Whether Linux accepts `policy` as a scheduling policy for `sched_setattr`.
/// This is the kernel's `valid_policy()`: idle, fair, rt, deadline or ext. Note
/// the gap at 4 -- SCHED_ISO is reserved and never valid -- and that 7 is
/// SCHED_EXT, which `valid_policy()` accepts on a kernel built with
/// CONFIG_SCHED_CLASS_EXT.
fn is_valid_sched_policy(policy: u32) -> bool {
    matches!(
        policy,
        // SCHED_OTHER/NORMAL, FIFO, RR, BATCH
        0 | 1 | 2 | 3
        // SCHED_IDLE
        | 5
        | SCHED_DEADLINE
        | SCHED_EXT
    )
}

/// The kernel's `rt_policy()`: the two fixed-priority real-time policies.
fn is_rt_policy(policy: u32) -> bool {
    policy == SCHED_FIFO || policy == SCHED_RR
}

/// The kernel's `__checkparam_dl()`, restricted to the parts that are pure ABI.
///
/// The kernel's final test compares the period against
/// `sysctl_sched_dl_period_{min,max}`, which are runtime-tunable. That bound is
/// a property of the host's configuration rather than of the interface, so it
/// is deliberately not reproduced here -- see the handler's doc comment for the
/// other cases in that class.
fn deadline_params_are_valid(runtime: u64, deadline: u64, period: u64) -> bool {
    // deadline != 0
    if deadline == 0 {
        return false;
    }
    // The kernel truncates DL_SCALE (10) bits, so runtime must be at least that
    // big to survive the truncation.
    if runtime < (1u64 << 10) {
        return false;
    }
    // The MSB is reserved for wrap-around and sign handling.
    if deadline & (1u64 << 63) != 0 || period & (1u64 << 63) != 0 {
        return false;
    }
    // A zero period means "same as the deadline".
    let period = if period == 0 { deadline } else { period };
    // runtime <= deadline <= period
    runtime <= deadline && deadline <= period
}

/// Apply `sched_copy_attr`'s size rules to the `size` the guest declared.
///
/// `Ok(n)` is the effective size to copy with; `Err(())` means the request is
/// refused with E2BIG *and* the kernel first writes its own struct size back
/// into `uattr->size`, so the caller owes that store.
///
/// The zero case is not a mistake: the kernel carries an explicit ABI
/// compatibility quirk, `if (!size) size = SCHED_ATTR_SIZE_VER0;`, so a
/// zero-sized request is a well-formed VER0 request and succeeds.
fn sched_attr_effective_size(declared: u32) -> Result<u32, ()> {
    let size = if declared == 0 {
        SCHED_ATTR_SIZE_VER0
    } else {
        declared
    };
    if !(SCHED_ATTR_SIZE_VER0..=SCHED_ATTR_MAX_SIZE).contains(&size) {
        return Err(());
    }
    Ok(size)
}

/// The descriptor fields this handler inspects, decoded from the guest's bytes.
#[derive(Clone, Copy, Debug)]
struct SchedAttrFields {
    policy: u32,
    sched_flags: u64,
    priority: u32,
    runtime: u64,
    deadline: u64,
    period: u64,
}

/// The checks that run in `sched_copy_attr` and in the `sched_setattr` wrapper,
/// i.e. everything the kernel decides *before* it looks the target pid up.
///
/// `size` is the effective size from [`sched_attr_effective_size`], so the
/// util-clamp rule can be stated in the same terms the kernel uses.
///
/// Measured: with a pid that does not exist, both of these still report their
/// own errno rather than ESRCH, which is what places them on this side of the
/// lookup.
fn validate_sched_attr_before_lookup(size: u32, attr: &SchedAttrFields) -> Result<(), Errno> {
    // `sched_copy_attr`: util-clamp lives past the VER0 tail, so asking for it
    // with a VER0 buffer is incoherent.
    if attr.sched_flags & SCHED_FLAG_UTIL_CLAMP != 0 && size < SCHED_ATTR_SIZE_VER1 {
        return Err(Errno::EINVAL);
    }
    // `sched_setattr`: the policy is compared as a *signed* int, and this test
    // sits above the KEEP_POLICY substitution below, so the sign bit is refused
    // even when the policy field is otherwise ignored.
    if (attr.policy as i32) < 0 {
        return Err(Errno::EINVAL);
    }
    Ok(())
}

/// The checks inside `__sched_setscheduler`, i.e. everything the kernel decides
/// *after* it has resolved the target pid.
///
/// Measured: with a pid that does not exist, every rule here reports ESRCH
/// instead, which is what places them on this side of the lookup.
fn validate_sched_attr_after_lookup(attr: &SchedAttrFields) -> Result<(), Errno> {
    // `sched_setattr` rewrites the policy to SETPARAM_POLICY (-1) when
    // KEEP_POLICY is set, and `__sched_setscheduler` then takes its
    // `policy < 0` branch. That branch does not skip the policy-dependent
    // rules -- it REUSES THE TASK'S CURRENT POLICY and applies them against
    // that. `valid_policy()` is the only thing it skips, which is why an
    // undefined value in the ignored field still passes.
    //
    // Detcore's current policy is not unknown: `handle_sched_getattr` reports
    // SCHED_OTHER, nice 0, priority 0 for every thread, unconditionally, and
    // that is the whole of the guest-visible scheduling state this sandbox
    // exposes. So KEEP_POLICY substitutes SCHED_OTHER here. Treating it as
    // "no policy" and skipping the rules accepted, for instance,
    // KEEP_POLICY with sched_priority=1, which Linux refuses because a
    // non-real-time current policy requires priority 0.
    //
    // These two sites must stay in step: if `handle_sched_getattr` ever
    // reports a different virtual policy, this substitution follows it.
    let policy = if attr.sched_flags & SCHED_FLAG_KEEP_POLICY != 0 {
        Some(VIRTUAL_CURRENT_POLICY)
    } else {
        if !is_valid_sched_policy(attr.policy) {
            return Err(Errno::EINVAL);
        }
        Some(attr.policy)
    };
    // No undefined sched_flags bits.
    if attr.sched_flags & !SCHED_FLAG_ALL != 0 {
        return Err(Errno::EINVAL);
    }
    // Valid priorities are 1..=MAX_RT_PRIO-1 for the real-time policies and
    // exactly 0 for every other policy, so the priority and the policy have to
    // agree; the kernel states that as `rt_policy(policy) != (prio != 0)`.
    if attr.priority > MAX_RT_PRIO - 1 {
        return Err(Errno::EINVAL);
    }
    if let Some(policy) = policy {
        if is_rt_policy(policy) != (attr.priority != 0) {
            return Err(Errno::EINVAL);
        }
        if policy == SCHED_DEADLINE
            && !deadline_params_are_valid(attr.runtime, attr.deadline, attr.period)
        {
            return Err(Errno::EINVAL);
        }
    }
    Ok(())
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-881)
fn virtual_ioprio(which: libc::c_int) -> Result<i64, Errno> {
    match which {
        IOPRIO_WHO_PROCESS => Ok(0),
        IOPRIO_WHO_PGRP | IOPRIO_WHO_USER => Ok(i64::from(IOPRIO_DEFAULT_EFFECTIVE)),
        _ => Err(Errno::EINVAL),
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
struct WaitidSigchldFields {
    pid: libc::pid_t,
    uid: libc::uid_t,
    status: libc::c_int,
    utime: libc::c_long,
    stime: libc::c_long,
}

#[repr(C)]
union WaitidSiginfoFields {
    _alignment: *mut libc::c_void,
    sigchld: WaitidSigchldFields,
}

#[repr(C)]
struct WaitidSiginfoHead {
    _base: [libc::c_int; 3],
    fields: WaitidSiginfoFields,
}

fn wait_status_is_termination(status: libc::c_int) -> bool {
    libc::WIFEXITED(status) || libc::WIFSIGNALED(status)
}

fn waitid_code_is_termination(code: libc::c_int) -> bool {
    matches!(code, libc::CLD_EXITED | libc::CLD_KILLED | libc::CLD_DUMPED)
}

fn canonicalize_waitid_siginfo(info: &mut libc::siginfo_t) {
    debug_assert!(
        std::mem::size_of::<WaitidSiginfoHead>() <= std::mem::size_of::<libc::siginfo_t>()
    );
    // SAFETY: Linux siginfo_t starts with three c_int fields followed by a
    // pointer-aligned union. Its SIGCHLD member is pid, uid, status, utime,
    // and stime in that order. The local repr(C) mirror changes only the two
    // host CPU-accounting fields and preserves the kernel-populated event.
    let sigchld = unsafe {
        &mut (*(info as *mut libc::siginfo_t).cast::<WaitidSiginfoHead>())
            .fields
            .sigchld
    };
    sigchld.utime = 0;
    sigchld.stime = 0;
}

/// `child` is the task `info_value.si_pid` names (`Detcore::task_of_guest_pid`).
fn finish_waitid_result<T, G>(
    guest: &mut G,
    call: syscalls::Waitid,
    value: i64,
    mut info_value: libc::siginfo_t,
    child: DetPid,
) -> Result<i64, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    // SAFETY: waitid writes either zeroed output or the SIGCHLD siginfo_t
    // variant, for which libc exposes si_pid.
    let child_pid = unsafe { info_value.si_pid() };
    if child_pid != 0 {
        canonicalize_waitid_siginfo(&mut info_value);
        guest.memory().write_value(
            call.info().expect("waitid infop checked before execution"),
            &info_value,
        )?;
        if call.options() & libc::WNOWAIT == 0 && waitid_code_is_termination(info_value.si_code) {
            guest.thread_state_mut().reap_child_process_cpu_time(child);
        }
        if let Some(rusage) = call.rusage() {
            // Host CPU and scheduling counters are not deterministic.
            let usage: libc::rusage = unsafe { std::mem::zeroed() };
            guest.memory().write_value(rusage, &usage)?;
        }
    }
    Ok(value)
}

#[derive(Debug, Eq, PartialEq)]
enum ExactWaitPollDecision {
    ChildReady,
    AwaitPhysicalExit,
    ReapAfterLogicalExit,
    Interrupted,
    Retry,
}

fn exact_wait_poll_decision(
    child_ready: bool,
    signaled: bool,
    lifecycle: Option<ExactChildWaitState>,
) -> ExactWaitPollDecision {
    if child_ready {
        ExactWaitPollDecision::ChildReady
    } else if lifecycle == Some(ExactChildWaitState::PhysicalExitPending) {
        ExactWaitPollDecision::AwaitPhysicalExit
    } else if matches!(
        lifecycle,
        Some(ExactChildWaitState::LogicallyExited | ExactChildWaitState::PhysicallyExited)
    ) {
        ExactWaitPollDecision::ReapAfterLogicalExit
    } else if signaled {
        ExactWaitPollDecision::Interrupted
    } else {
        ExactWaitPollDecision::Retry
    }
}

fn stale_any_wait_must_interrupt(signaled: bool, next_ready: Option<DetPid>) -> bool {
    signaled && next_ready.is_none()
}

fn terminal_child_wait_spec(
    selector: ChildWaitSelector,
    caller: DetTid,
    options: libc::c_int,
) -> ChildWaitSpec {
    let exit_class = if options & libc::__WALL != 0 {
        ChildWaitExitClass::Any
    } else if options & libc::__WCLONE != 0 {
        ChildWaitExitClass::Clone
    } else {
        ChildWaitExitClass::Sigchld
    };
    ChildWaitSpec {
        selector,
        owner: (options & libc::__WNOTHREAD != 0).then_some(caller),
        exit_class,
    }
}

fn validate_wait4_arguments(pid: libc::pid_t, options: WaitPidFlag) -> Result<(), Errno> {
    let allowed_options = WaitPidFlag::WNOHANG
        | WaitPidFlag::WUNTRACED
        | WaitPidFlag::WCONTINUED
        | WaitPidFlag::__WNOTHREAD
        | WaitPidFlag::__WCLONE
        | WaitPidFlag::__WALL;

    // Linux kernel/exit.c:kernel_wait4 rejects unknown option bits before it
    // interprets the pid selector.
    if options.bits() & !allowed_options.bits() != 0 {
        return Err(Errno::EINVAL);
    }
    // The same function returns ESRCH for INT_MIN before looking for children,
    // because negating that selector is not representable as a pid_t.
    if pid == libc::pid_t::MIN {
        return Err(Errno::ESRCH);
    }
    Ok(())
}

// A serial emulated child wait (today KVM) represents terminal events only, including the terminal subset
// of WUNTRACED. `__WNOTHREAD` is enforced by the logical owner filter in
// `terminal_child_wait_spec`, and exact-child completion strips it before
// the backend call. Keep unsupported and unknown low-int bits on the
// backend's prevalidation path; they must not select or consume a logical
// child.
pub(super) fn wait4_uses_terminal_selector(
    options: WaitPidFlag,
    serial_emulated_wait: bool,
) -> bool {
    if serial_emulated_wait {
        options.bits() & !(libc::WNOHANG | libc::WUNTRACED | libc::__WNOTHREAD) == 0
    } else {
        !options.intersects(
            WaitPidFlag::WUNTRACED
                | WaitPidFlag::WCONTINUED
                | WaitPidFlag::__WCLONE
                | WaitPidFlag::__WALL,
        )
    }
}

/// The one completion order a backend that emulates child waits
/// (`BackendCapabilities::emulates_child_waits`) needs for both wait4 and
/// waitid, split around the backend's exact-child call.
///
/// Backend waitability precedes the child's final owner-hook CPU prefix, so
/// the caller first waits, under its retained grant and birth-owned identity,
/// for that publication. No new scheduler turn or user-memory identity read
/// occurs.
pub(super) async fn await_selected_child_cpu_publication<G, T>(
    guest: &mut G,
    child: DetPid,
    call: &str,
) -> Result<(), Error>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    if guest
        .thread_state()
        .wait_for_child_cpu_publication(child)
        .await
    {
        Ok(())
    } else {
        Err(Error::Tool(anyhow::anyhow!(
            "emulated {call} selected child has no owned final CPU publication"
        )))
    }
}

/// After the backend's exact-child call consumed `child` (it returned the
/// child, or EFAULT after selecting it), retire it: reap its final CPU time
/// and consume Detcore's logical wait. Shared by wait4 and waitid.
pub(super) async fn retire_selected_child<G, T>(
    guest: &mut G,
    child: DetPid,
    call: &str,
) -> Result<(), Error>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    guest.thread_state_mut().reap_child_process_cpu_time(child);
    if consume_child_wait(guest, child).await {
        Ok(())
    } else {
        Err(Error::Tool(anyhow::anyhow!(
            "emulated {call} selected child was not logically consumed"
        )))
    }
}

pub(super) async fn complete_selected_emulated_wait4<G, T>(
    guest: &mut G,
    call: syscalls::Wait4,
    child: DetPid,
) -> Result<i64, Error>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    await_selected_child_cpu_publication(guest, child, "wait4").await?;
    // Selection already applied `__WNOTHREAD` through the logical owner
    // filter; the backend's raw wait4 accepts only WNOHANG and WUNTRACED.
    // Keep exactly those, as the waitid exact-child completion does.
    let exact = call
        .with_pid(child.as_raw())
        .with_options(WaitPidFlag::from_bits_retain(
            call.options().bits() & (libc::WNOHANG | libc::WUNTRACED),
        ));
    let result = guest.inject(exact).await;
    match result {
        Ok(pid) if pid == i64::from(child.as_raw()) => {}
        // The supported KVM wait4 selects/consumes before either status or
        // rusage copyout. EFAULT therefore consumed this exact child too.
        Err(Errno::EFAULT) => {}
        unexpected => {
            return Err(Error::Tool(anyhow::anyhow!(
                "emulated wait4 selected-child invariant failed: {unexpected:?} for {child}"
            )));
        }
    }
    retire_selected_child(guest, child, "wait4").await?;
    result.map_err(Error::from)
}

fn child_wait_can_retry_after_stale(spec: ChildWaitSpec) -> bool {
    !matches!(spec.selector, ChildWaitSelector::Exact(_))
}

pub(super) type KernelSigset = u64;
pub(super) const KERNEL_SIGSET_SIZE: usize = std::mem::size_of::<KernelSigset>();

fn signal_is_blocked(mask: &KernelSigset, signal: SigWrapper) -> bool {
    let raw_signal = signal.raw();
    (1..=KernelSigset::BITS as i32).contains(&raw_signal)
        && mask & (1_u64 << (raw_signal as u32 - 1)) != 0
}

#[repr(C)]
#[derive(Clone, Copy)]
pub(super) struct KernelSigaction {
    pub(super) handler: u64,
    pub(super) flags: u64,
    pub(super) restorer: u64,
    pub(super) mask: KernelSigset,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum WaitSignalDisposition {
    Interrupt,
    Restart,
}

fn signal_default_disposition_does_not_interrupt_child_wait(signal: SigWrapper) -> bool {
    matches!(
        signal.raw(),
        libc::SIGCHLD | libc::SIGCONT | libc::SIGURG | libc::SIGWINCH
    )
}

/// Whether `signal`'s default action ends the process, by terminating it or
/// dumping core (signal(7)): every signal except those whose default is to be
/// ignored, to stop or to continue. SIGKILL, which no handler or callback
/// sees, is left out as well. On DBT, real-time signals do not reach
/// Detcore's signal path today (<https://github.com/rrnewton/hermit/issues/4192>).
fn signal_default_action_ends_the_process(signal: SigWrapper) -> bool {
    !matches!(
        signal.raw(),
        libc::SIGCHLD
            | libc::SIGCONT
            | libc::SIGURG
            | libc::SIGWINCH
            | libc::SIGSTOP
            | libc::SIGTSTP
            | libc::SIGTTIN
            | libc::SIGTTOU
            | libc::SIGKILL
    )
}

fn signal_has_uncatchable_default_disposition(signal: SigWrapper) -> bool {
    matches!(signal.raw(), libc::SIGKILL | libc::SIGSTOP)
}

pub(super) async fn wait_signal_disposition<G, T>(
    guest: &mut G,
    status: ResumeStatus,
    guest_signal_mask: &KernelSigset,
    action_addr: AddrMut<'_, KernelSigaction>,
    inspect_action: bool,
) -> Result<Option<WaitSignalDisposition>, Error>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let ResumeStatus::Signaled(signals) = status else {
        return Ok(None);
    };
    let Some(mut signals) = signals else {
        return Ok(Some(WaitSignalDisposition::Interrupt));
    };
    signals.sort_by_key(|signal| signal.raw());
    for signal in signals {
        if signal_is_blocked(guest_signal_mask, signal) {
            continue;
        }
        if signal_has_uncatchable_default_disposition(signal) {
            return Ok(Some(WaitSignalDisposition::Interrupt));
        }
        if !inspect_action {
            return Ok(Some(WaitSignalDisposition::Interrupt));
        }
        let call = syscalls::RtSigaction::new()
            .with_signum(signal.raw())
            .with_action(None)
            .with_old_action(Some(action_addr.cast()))
            .with_sigsetsize(std::mem::size_of::<u64>());
        guest.inject_with_retry(call).await?;
        let action: KernelSigaction = guest.memory().read_value(action_addr)?;
        if action.handler == libc::SIG_IGN as u64
            || action.handler == libc::SIG_DFL as u64
                && signal_default_disposition_does_not_interrupt_child_wait(signal)
        {
            continue;
        }
        return Ok(Some(
            if action.handler != libc::SIG_DFL as u64 && action.flags & libc::SA_RESTART as u64 != 0
            {
                WaitSignalDisposition::Restart
            } else {
                WaitSignalDisposition::Interrupt
            },
        ));
    }
    Ok(None)
}

/// The signals a precise futex wait holds in its own check after a wake:
/// `SIGCHLD` and every signal a host-timed source can post (`host_timed`),
/// except a signal the scheduler committed to it at the wake (`committed`)
/// that no host-timed source can post too
/// (https://github.com/rrnewton/hermit/issues/4005).
pub(crate) fn futex_wait_held_after_wake(
    host_timed: KernelSigset,
    committed: KernelSigset,
) -> KernelSigset {
    (kernel_sigset_bit(libc::SIGCHLD) | host_timed) & !(committed & !host_timed)
}

/// The signals that end a precise futex wait at its own check: those pending
/// and interrupting it (`interrupting`) that it does not hold (`held`), or,
/// when there are none, a guest's thread-directed `SIGCHLD` in its private
/// queue that the schedule recorded (`PrivatePending`). Such a copy may have
/// been sent before this wait parked, and the drain that ends a parked wait for
/// it never saw this wait, so only this check can. `/proc` cannot tell it from
/// a copy a host-timed source posted, so its bit only prompts the question
/// (https://github.com/rrnewton/hermit/issues/4132).
///
/// A pending copy Linux discarded when it was sent does not end the wait,
/// whatever handler exists now: the scheduler's record of such copies
/// (`DiscardedSignalControl::Bits`) is subtracted, per queue, from every
/// signal (`without_discarded`,
/// <https://github.com/rrnewton/hermit/issues/4293>).
pub(crate) async fn futex_wait_pending_signals<G, T>(
    guest: &mut G,
    state: &KernelSignalState,
    interrupting: KernelSigset,
    held: KernelSigset,
    host_timed: KernelSigset,
    committed: KernelSigset,
) -> KernelSigset
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let pending = without_discarded(guest, state, interrupting & !held, committed).await;
    let sigchld = kernel_sigset_bit(libc::SIGCHLD) & !host_timed;
    if pending == 0
        && interrupting & state.thread_pending & held & sigchld != 0
        && matches!(
            crate::tool_global::child_exit_sigchld(
                guest,
                crate::scheduler::child_exit_sigchld::ChildExitSigchldControl::PrivatePending,
            )
            .await,
            crate::scheduler::child_exit_sigchld::ChildExitSigchldAnswer::Placed(true)
        )
    {
        return sigchld;
    }
    pending
}

/// `pending`, a set of this thread's pending signals, without each one whose
/// only pending copies Linux discarded when they were sent, as the scheduler
/// recorded them (`scheduler::discarded_at_send`). Asks the scheduler only
/// when such a signal could be there.
///
/// No signal is exempt, not even one the wake's answer names: that answer
/// carries the drain's batch too, whose copy a sender may have flushed
/// (`SIG_IGN`) and sent again, discarded, before this check; Linux then has
/// no copy to deliver (the Claude re-check of the R2b-1 design, R4-1,
/// https://github.com/rrnewton/hermit/issues/4273). A copy the scheduler
/// itself sent kept clears its bit, so applying the record to it changes
/// nothing. `committed` is kept for the callers' hold and is not used here.
pub(crate) async fn without_discarded<G, T>(
    guest: &mut G,
    state: &KernelSignalState,
    pending: KernelSigset,
    committed: KernelSigset,
) -> KernelSigset
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    use crate::scheduler::discarded_at_send::DiscardedSignalAnswer;
    use crate::scheduler::discarded_at_send::DiscardedSignalControl;
    use crate::scheduler::discarded_at_send::STANDARD_SIGNALS;
    if pending & STANDARD_SIGNALS == 0
        || !crate::scheduler::discarded_at_send::records(guest.config())
    {
        return pending;
    }
    let DiscardedSignalAnswer::Bits { thread, process } =
        crate::tool_global::discarded_signal(guest, DiscardedSignalControl::Bits).await
    else {
        return pending;
    };
    with_kept_copies(state, thread, process, pending, committed)
}

/// `pending` without each signal whose only pending copies the record marks
/// as discarded at send (`thread` for the thread's queue, `process` for the
/// shared queue). The pure part of `without_discarded`. No signal is exempt,
/// not even one in `committed`, the set the wake's answer names, which can
/// hold a drain batch's copy that was flushed and sent again, discarded
/// (R4-1 of the R2b-1 design re-checks,
/// <https://github.com/rrnewton/hermit/issues/4273>).
pub(crate) fn with_kept_copies(
    state: &KernelSignalState,
    thread: u64,
    process: u64,
    pending: KernelSigset,
    committed: KernelSigset,
) -> KernelSigset {
    let _ = committed;
    let kept = (state.thread_pending & !thread) | (state.shared_pending & !process);
    pending & kept
}

/// The one-bit kernel sigset for `raw_signal` (1-based), or 0 when out of range.
pub(crate) fn kernel_sigset_bit(raw_signal: i32) -> KernelSigset {
    if (1..=KernelSigset::BITS as i32).contains(&raw_signal) {
        1_u64 << (raw_signal as u32 - 1)
    } else {
        0
    }
}

/// The exit signal that a child created by this clone-family call sends to
/// the calling process when it exits, at a moment set by host timing, as a
/// kernel sigset; 0 when there is none to hold. Linux sends a child's exit
/// signal to the process that created it. A thread (`CLONE_THREAD`) sends
/// none. SIGCHLD is already held in every gated wait. A `CLONE_PARENT` child
/// sends the caller's own exit signal to the caller's parent instead, and that
/// signal is SIGCHLD or was recorded for that parent when it created the
/// caller's process (a reparent to another process resets it to SIGCHLD).
pub(crate) fn clone_host_timed_signals(
    flags: CloneFlags,
    exit_signal: libc::c_int,
) -> KernelSigset {
    if flags.intersects(CloneFlags::CLONE_THREAD | CloneFlags::CLONE_PARENT)
        || exit_signal == libc::SIGCHLD
    {
        0
    } else {
        kernel_sigset_bit(exit_signal)
    }
}

/// One thread's signal state as the kernel reports it in
/// `/proc/<pid>/task/<tid>/status`.
///
/// The kernel is the authority for a guest thread's mask, dispositions, and
/// pending signals on backends that run the guest as real host threads. Reading
/// the state has no effect on the guest, unlike a probe syscall around which a
/// signal can be dequeued and held by the backend
/// (https://github.com/rrnewton/hermit/issues/3146).
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(crate) struct KernelSignalState {
    /// Signals pending for the thread or its whole thread group (`SigPnd | ShdPnd`).
    pub(crate) pending: KernelSigset,
    /// Signals pending on the thread's private queue (`SigPnd`).
    pub(crate) thread_pending: KernelSigset,
    /// Signals pending on the thread group's shared queue (`ShdPnd`).
    pub(crate) shared_pending: KernelSigset,
    /// The thread's current signal mask (`SigBlk`).
    pub(crate) blocked: KernelSigset,
    /// Signals whose disposition is `SIG_IGN` (`SigIgn`).
    pub(crate) ignored: KernelSigset,
    /// Signals with a handler installed (`SigCgt`).
    pub(crate) caught: KernelSigset,
}

/// [`KernelSignalState::default_action_ends_process`] given only the
/// thread's ignored and caught sets, from wherever they come (the kernel's
/// `/proc` view or Detcore's copy, `signal_shadow::copied_dispositions`).
pub(crate) fn default_action_ends_process(
    ignored: KernelSigset,
    caught: KernelSigset,
    signal: i32,
) -> bool {
    let bit = kernel_sigset_bit(signal);
    let stops_or_continues = [
        libc::SIGSTOP,
        libc::SIGTSTP,
        libc::SIGTTIN,
        libc::SIGTTOU,
        libc::SIGCONT,
    ]
    .into_iter()
    .fold(0, |set, signal| set | kernel_sigset_bit(signal));
    signal == libc::SIGKILL
        || ((ignored | caught | default_ignored_signals() | stops_or_continues) & bit) == 0
}

/// The signals a process with dispositions `ignored` and `caught` discards
/// when they are generated and unblocked: those set to `SIG_IGN`, and the
/// default-ignored ones with no handler (`sig_task_ignored`).
pub(crate) fn ignored_at_generation(ignored: KernelSigset, caught: KernelSigset) -> KernelSigset {
    ignored | (default_ignored_signals() & !caught)
}

impl KernelSignalState {
    /// Parse the five signal lines of a `/proc/<pid>/task/<tid>/status` file.
    pub(crate) fn parse(status: &str) -> Option<Self> {
        let mut thread_pending = None;
        let mut shared_pending = None;
        let mut state = KernelSignalState::default();
        let mut blocked = None;
        let mut ignored = None;
        let mut caught = None;
        for line in status.lines() {
            let Some((key, value)) = line.split_once(':') else {
                continue;
            };
            let slot = match key {
                "SigPnd" => &mut thread_pending,
                "ShdPnd" => &mut shared_pending,
                "SigBlk" => &mut blocked,
                "SigIgn" => &mut ignored,
                "SigCgt" => &mut caught,
                _ => continue,
            };
            *slot = Some(KernelSigset::from_str_radix(value.trim(), 16).ok()?);
        }
        state.thread_pending = thread_pending?;
        state.shared_pending = shared_pending?;
        state.pending = state.thread_pending | state.shared_pending;
        state.blocked = blocked?;
        state.ignored = ignored?;
        state.caught = caught?;
        Some(state)
    }

    /// Signals whose delivery, while unblocked in `mask`, ends a blocking wait: a
    /// caught signal runs its handler, and a `SIG_DFL` signal terminates, dumps
    /// core, or stops the process. Ignored signals and the default-ignored
    /// `SIGCHLD`, `SIGCONT`, `SIGURG`, and `SIGWINCH` do not, and neither does the
    /// backend's own preemption signal.
    pub(crate) fn interrupting(&self, mask: KernelSigset) -> KernelSigset {
        let default_action = !(self.ignored | self.caught);
        (self.caught | (default_action & !default_ignored_signals()))
            & !mask
            & !kernel_sigset_bit(reverie::PERF_EVENT_SIGNAL as i32)
    }

    /// Signals Linux discards when they are generated while unblocked: those
    /// set to `SIG_IGN`, and the default-ignored `SIGCHLD`, `SIGCONT`, `SIGURG`
    /// and `SIGWINCH` while they have no handler (`sig_task_ignored`). A blocked
    /// signal is never discarded, since its disposition may change before it is
    /// unblocked. Under ptrace the kernel queues even these for the tracer, so
    /// the pending set alone does not show the discard.
    pub(crate) fn discarded_at_generation(&self) -> KernelSigset {
        self.ignored_at_generation() & !self.blocked
    }

    /// The signals [`discarded_at_generation`](Self::discarded_at_generation)
    /// names, before the mask: those set to `SIG_IGN`, and the default-ignored
    /// ones with no handler. A caller that knows a better mask than `SigBlk`
    /// (`scheduler::discarded_at_send`) applies its own.
    pub(crate) fn ignored_at_generation(&self) -> KernelSigset {
        ignored_at_generation(self.ignored, self.caught)
    }

    /// Whether delivering `signal` now ends the process: its disposition is
    /// `SIG_DFL` and its default action terminates or dumps core, not ignores,
    /// stops or continues. `SIGKILL` always does.
    pub(crate) fn default_action_ends_process(&self, signal: i32) -> bool {
        default_action_ends_process(self.ignored, self.caught, signal)
    }

    /// The job-control stop signals `SIGTSTP`, `SIGTTIN`, and `SIGTTOU` whose
    /// disposition is still `SIG_DFL`.
    ///
    /// Linux does not stop a process for one of these when its process group is
    /// orphaned: `get_signal` discards the signal (`is_current_pgrp_orphaned`),
    /// and a wait it reached restarts as if no signal had arrived. Whether a
    /// group is orphaned depends on the parents and sessions of every process in
    /// it, which Detcore does not track.
    pub(crate) fn default_job_control_stops(&self) -> KernelSigset {
        [libc::SIGTSTP, libc::SIGTTIN, libc::SIGTTOU]
            .into_iter()
            .fold(0, |set, signal| set | kernel_sigset_bit(signal))
            & !(self.ignored | self.caught)
    }

    /// The signals that end a blocking wait under the guest's `mask`: those
    /// [`interrupting`](Self::interrupting) names, without the default job-control
    /// stops ([`default_job_control_stops`](Self::default_job_control_stops)) when
    /// `defers_default_stops` is set.
    ///
    /// A wait sets it when Linux's restart of the call keeps its deadline
    /// (`NonblockableSyscall::restart_keeps_deadline`): a `poll` with a positive
    /// timeout, and a timed futex wait. Detcore keeps that deadline for the
    /// kernel's restart only when the wait ends with `ERESTART_RESTARTBLOCK`, which
    /// a stop signal the backend holds does not (`held_signal_restart_errno`), so
    /// ending such a wait for a default stop that Linux discards in an orphaned
    /// process group could restart it with a fresh timeout, and it would return
    /// late. Left alone, the wait keeps its deadline, as on hermit main, and a stop
    /// that Linux does not discard takes effect when the call returns rather than
    /// when the signal arrives (https://github.com/rrnewton/hermit/issues/3358).
    /// `SIGSTOP`, a caught stop signal, and a fatal one still end the wait.
    pub(crate) fn interrupting_wait(
        &self,
        mask: KernelSigset,
        defers_default_stops: bool,
    ) -> KernelSigset {
        let interrupting = self.interrupting(mask);
        if defers_default_stops {
            interrupting & !self.default_job_control_stops()
        } else {
            interrupting
        }
    }

    /// Pending signals that would end a blocking wait under the guest's `mask`
    /// (see [`interrupting_wait`](Self::interrupting_wait)).
    pub(crate) fn pending_interrupting(
        &self,
        mask: KernelSigset,
        defers_default_stops: bool,
    ) -> KernelSigset {
        self.pending & self.interrupting_wait(mask, defers_default_stops)
    }

    /// The signal the kernel dequeues next for this thread while `mask` is
    /// blocked, and the queue it comes from, or `None` if no unblocked signal is
    /// pending.
    ///
    /// As in `dequeue_signal` and `next_signal` (`kernel/signal.c`), the thread's
    /// private queue goes before the thread group's shared queue, and within a
    /// queue a synchronous signal (`SIGSEGV`, `SIGBUS`, `SIGILL`, `SIGTRAP`,
    /// `SIGFPE`, `SIGSYS`) goes before the lowest-numbered other one.
    pub(crate) fn next_dequeued(&self, mask: KernelSigset) -> Option<(i32, SignalQueue)> {
        let synchronous = SYNCHRONOUS_SIGNALS
            .into_iter()
            .fold(0, |set, signal| set | kernel_sigset_bit(signal));
        let first = |queued: KernelSigset| {
            let deliverable = queued & !mask;
            let preferred = if deliverable & synchronous != 0 {
                deliverable & synchronous
            } else {
                deliverable
            };
            (preferred != 0).then(|| preferred.trailing_zeros() as i32 + 1)
        };
        first(self.thread_pending)
            .map(|signal| (signal, SignalQueue::Thread))
            .or_else(|| first(self.shared_pending).map(|signal| (signal, SignalQueue::Shared)))
    }

    /// The signals pending on `queue`.
    pub(crate) fn queued(&self, queue: SignalQueue) -> KernelSigset {
        match queue {
            SignalQueue::Thread => self.thread_pending,
            SignalQueue::Shared => self.shared_pending,
        }
    }
}

/// The signals whose default action ignores them.
fn default_ignored_signals() -> KernelSigset {
    [libc::SIGCHLD, libc::SIGCONT, libc::SIGURG, libc::SIGWINCH]
        .into_iter()
        .fold(0, |set, signal| set | kernel_sigset_bit(signal))
}

/// The signals an instruction raises, which the kernel dequeues before any other
/// signal on the same queue (`SYNCHRONOUS_MASK` in `kernel/signal.c`).
const SYNCHRONOUS_SIGNALS: [i32; 6] = [
    libc::SIGSEGV,
    libc::SIGBUS,
    libc::SIGILL,
    libc::SIGTRAP,
    libc::SIGFPE,
    libc::SIGSYS,
];

/// One of a thread's two queues of pending signals.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SignalQueue {
    /// The thread's private queue (`SigPnd`).
    Thread,
    /// The thread group's shared queue (`ShdPnd`).
    Shared,
}

/// Set once per Hermit process, by [`refuse_host_signal_state_reads`], when
/// the backend's guest thread IDs do not name host tasks (KVM): a host
/// `/proc/<pid>/task/<tid>/status` would then describe an unrelated host task,
/// if any, chosen by host thread-ID allocation, so its signal state must never
/// decide a guest-visible outcome. Every read that exists today is gated off on
/// such a backend by a capability (`models_signal_targets`,
/// `supports_blocked_wait_signal_interruption` or one of its named readings in
/// `detcore_model::bundled_signal_facts`, `reports_child_exit_publication`, a
/// nested PID namespace, or the guard in
/// `restartable_internal_io_interrupting_signals`); this latch is the backstop
/// for a future path that misses its gate.
#[cfg(not(test))]
static HOST_SIGNAL_STATE_READS_REFUSED: std::sync::atomic::AtomicBool =
    std::sync::atomic::AtomicBool::new(false);

#[cfg(test)]
thread_local! {
    /// The latch for unit tests, per test thread, so that tests reading their
    /// own real `/proc` state run beside one that sets it.
    static HOST_SIGNAL_STATE_READS_REFUSED_FOR_TEST: std::cell::Cell<bool> =
        const { std::cell::Cell::new(false) };
}

/// Whether a backend with `backend`'s capabilities forbids host `/proc` signal
/// state reads for its guest threads: exactly when its guest thread IDs do not
/// name host tasks (`procfs_inode::guest_tids_name_host_tasks`).
pub(crate) fn refuses_host_signal_state_reads(backend: &BackendCapabilities) -> bool {
    !crate::procfs_inode::guest_tids_name_host_tasks(backend, None)
}

/// Latch the refusal for this process when `backend` forbids host `/proc`
/// signal state reads ([`refuses_host_signal_state_reads`]). Called once, when
/// Detcore's global state is created for a run.
pub(crate) fn refuse_host_signal_state_reads(backend: &BackendCapabilities) {
    if !refuses_host_signal_state_reads(backend) {
        return;
    }
    #[cfg(not(test))]
    HOST_SIGNAL_STATE_READS_REFUSED.store(true, std::sync::atomic::Ordering::Relaxed);
    #[cfg(test)]
    HOST_SIGNAL_STATE_READS_REFUSED_FOR_TEST.with(|refused| refused.set(true));
}

/// Whether host `/proc` signal state reads are refused in this process
/// ([`refuse_host_signal_state_reads`]).
pub(crate) fn host_signal_state_reads_refused() -> bool {
    #[cfg(not(test))]
    {
        HOST_SIGNAL_STATE_READS_REFUSED.load(std::sync::atomic::Ordering::Relaxed)
    }
    #[cfg(test)]
    {
        HOST_SIGNAL_STATE_READS_REFUSED_FOR_TEST.with(|refused| refused.get())
    }
}

/// Read `tid`'s signal state from the kernel.
///
/// On a backend whose guest thread IDs do not name host tasks
/// ([`host_signal_state_reads_refused`]) the read is refused with `ENOSYS`
/// before it reaches `/proc`: a caller that needs the state ends the run by
/// name (`BlockedWaitSignalError::StateUnreadable`), and one that treats an
/// unreadable state as unknown keeps its documented fallback. Either way no
/// unrelated host task's state reaches a decision.
pub(crate) fn read_kernel_signal_state(pid: Pid, tid: Pid) -> Result<KernelSignalState, Errno> {
    #[cfg(test)]
    if let Some(result) = signal_state_read_seam::scripted(pid, tid) {
        return result;
    }
    if host_signal_state_reads_refused() {
        static REPORTED: std::sync::Once = std::sync::Once::new();
        REPORTED.call_once(|| {
            error!(
                "refused a host /proc signal state read of guest thread {} (process {}): this \
                 backend's guest thread IDs do not name host tasks",
                tid.as_raw(),
                pid.as_raw()
            );
        });
        return Err(Errno::ENOSYS);
    }
    let path = format!("/proc/{}/task/{}/status", pid.as_raw(), tid.as_raw());
    let status = std::fs::read_to_string(path).map_err(|_| Errno::ESRCH)?;
    KernelSignalState::parse(&status).ok_or(Errno::EIO)
}

/// Why a blocked wait ended the run instead of returning to the guest
/// (https://github.com/rrnewton/hermit/issues/3146). A wait call cannot return
/// the underlying error natively, and resuming the guest after the second
/// failure would leave every blockable signal blocked in it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BlockedWaitSignalError {
    /// The thread still exists, but its signal state could not be read from
    /// `/proc` (`errno` is `ESRCH` for an unreadable file, `EIO` for one that
    /// does not parse).
    StateUnreadable { pid: Pid, tid: Pid, errno: Errno },
    /// The guest's own signal mask could not be put back after the wait blocked
    /// every signal. `last_error` is the last injected call's error.
    MaskNotRestored {
        pid: Pid,
        tid: Pid,
        attempts: usize,
        last_error: Option<Errno>,
    },
}

impl std::fmt::Display for BlockedWaitSignalError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::StateUnreadable { pid, tid, errno } => write!(
                f,
                "cannot read the signal state of guest thread {tid} (process {pid}) from \
                 /proc while it waits: {errno}"
            ),
            Self::MaskNotRestored {
                pid,
                tid,
                attempts,
                last_error,
            } => {
                write!(
                    f,
                    "cannot restore the signal mask of guest thread {tid} (process {pid}) \
                     after a blocked wait ({attempts} attempts"
                )?;
                if let Some(errno) = last_error {
                    write!(f, ", last error {errno}")?;
                }
                f.write_str("); it would resume with every signal blocked")
            }
        }
    }
}

impl std::error::Error for BlockedWaitSignalError {}

/// Whether thread `tid` of process `pid` no longer exists.
pub(crate) fn guest_thread_is_gone(pid: Pid, tid: Pid) -> bool {
    // SAFETY: signal 0 only checks that the thread exists and may be signalled.
    let result = unsafe { libc::syscall(libc::SYS_tgkill, pid.as_raw(), tid.as_raw(), 0) };
    result == -1 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH)
}

/// Read `tid`'s signal state when `pid` may not be its thread group.
///
/// Reverie can report the creating process's pid for a process that a raw
/// `clone` without `CLONE_THREAD` created, so `/proc/<pid>/task/<tid>` does not
/// exist although the thread does. `/proc/<tid>/task/<tid>` names any live
/// thread, its own thread group's leader or not, so it is read before the
/// thread is taken to be gone (https://github.com/rrnewton/hermit/issues/3982).
pub(crate) fn read_thread_signal_state(pid: Pid, tid: Pid) -> Result<KernelSignalState, Errno> {
    read_kernel_signal_state(pid, tid).or_else(|error| {
        if pid == tid {
            Err(error)
        } else {
            read_kernel_signal_state(tid, tid)
        }
    })
}

/// Whether thread `tid` no longer exists, when `pid` may not be its thread
/// group ([`read_thread_signal_state`]). `tkill` names the thread without a
/// thread group, so a live thread under a wrong `pid` is not taken to be gone.
pub(crate) fn thread_is_gone(pid: Pid, tid: Pid) -> bool {
    if !guest_thread_is_gone(pid, tid) {
        return false;
    }
    // SAFETY: signal 0 only checks that the thread exists and may be signalled.
    let result = unsafe { libc::syscall(libc::SYS_tkill, tid.as_raw(), 0) };
    result == -1 && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH)
}

/// [`read_wait_signal_state`] for a thread whose reported `pid` may not be its
/// thread group ([`read_thread_signal_state`]).
pub(crate) fn read_thread_wait_signal_state(
    pid: Pid,
    tid: Pid,
) -> Result<KernelSignalState, Error> {
    match read_thread_signal_state(pid, tid) {
        Ok(state) => Ok(state),
        Err(_) if thread_is_gone(pid, tid) => Err(Errno::ERESTARTNOINTR.into()),
        Err(errno) => Err(Error::Tool(anyhow::Error::new(
            BlockedWaitSignalError::StateUnreadable { pid, tid, errno },
        ))),
    }
}

/// Read the signal state of `tid`, which is stopped inside a blocked wait.
///
/// The read's own error never reaches the guest, because no wait call returns
/// `ESRCH` or `EIO` for it. If the thread no longer exists (it was killed while
/// it waited), the call ends with `ERESTARTNOINTR`, which no thread observes.
/// Otherwise the run ends with [`BlockedWaitSignalError::StateUnreadable`].
pub(crate) fn read_wait_signal_state(pid: Pid, tid: Pid) -> Result<KernelSignalState, Error> {
    match read_kernel_signal_state(pid, tid) {
        Ok(state) => Ok(state),
        Err(_) if guest_thread_is_gone(pid, tid) => Err(Errno::ERESTARTNOINTR.into()),
        Err(errno) => Err(Error::Tool(anyhow::Error::new(
            BlockedWaitSignalError::StateUnreadable { pid, tid, errno },
        ))),
    }
}

/// A test seam in front of the `/proc` read in [`read_kernel_signal_state`].
#[cfg(test)]
pub(crate) mod signal_state_read_seam {
    use std::cell::RefCell;

    use reverie::Pid;
    use reverie::syscalls::Errno;

    use super::KernelSignalState;

    type Script = Box<dyn FnMut(Pid, Pid) -> Option<Result<KernelSignalState, Errno>>>;

    thread_local! {
        static SCRIPT: RefCell<Option<Script>> = const { RefCell::new(None) };
    }

    /// Removes the installed script when dropped.
    pub(crate) struct Installed(());

    impl Drop for Installed {
        fn drop(&mut self) {
            SCRIPT.with(|script| script.borrow_mut().take());
        }
    }

    /// Answer this thread's reads with `script`; `None` falls through to `/proc`.
    pub(crate) fn install(
        script: impl FnMut(Pid, Pid) -> Option<Result<KernelSignalState, Errno>> + 'static,
    ) -> Installed {
        SCRIPT.with(|slot| *slot.borrow_mut() = Some(Box::new(script)));
        Installed(())
    }

    pub(super) fn scripted(pid: Pid, tid: Pid) -> Option<Result<KernelSignalState, Errno>> {
        SCRIPT.with(|slot| {
            slot.borrow_mut()
                .as_mut()
                .and_then(|script| script(pid, tid))
        })
    }
}

pub(super) fn blocked_signal_mask() -> KernelSigset {
    // Preserve libc's definition of the blockable set (notably its reserved
    // NPTL signals) while converting the result to the kernel's one-word ABI.
    let mut libc_mask: libc::sigset_t = unsafe { std::mem::zeroed() };
    unsafe {
        libc::sigfillset(&mut libc_mask);
        libc::sigdelset(&mut libc_mask, reverie::PERF_EVENT_SIGNAL as i32);
    }
    (1..=KernelSigset::BITS as i32).fold(0, |mask, raw_signal| {
        if unsafe { libc::sigismember(&libc_mask, raw_signal) } == 1 {
            mask | (1_u64 << (raw_signal as u32 - 1))
        } else {
            mask
        }
    })
}

pub(super) async fn block_signals_for_disposition<G, T>(
    guest: &mut G,
    blocked_mask_addr: Addr<'_, KernelSigset>,
    old_mask_addr: AddrMut<'_, KernelSigset>,
) -> Result<KernelSigset, Error>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let block_signals = syscalls::RtSigprocmask::new()
        .with_how(libc::SIG_SETMASK)
        .with_set(
            (!guest
                .config()
                .backend
                .requires_thread_directed_process_signals)
                .then_some(blocked_mask_addr.cast()),
        )
        .with_oldset(Some(old_mask_addr.cast()))
        .with_sigsetsize(KERNEL_SIGSET_SIZE);
    guest.inject_with_retry(block_signals).await?;
    let guest_mask: KernelSigset = guest.memory().read_value(old_mask_addr)?;
    if guest.config().backend.reports_child_exit_publication
        || crate::scheduler::discarded_at_send::records(guest.config())
    {
        // While the private mask is installed, the kernel reports Detcore's
        // mask for this thread; the scheduler's child-exit classification and
        // its record of copies Linux discarded at send must read the guest's
        // own (the Claude re-check of the R2b-1 design, R4-3).
        crate::tool_global::set_saved_guest_sigmask(guest, Some(guest_mask)).await;
    }
    Ok(guest_mask)
}

pub(super) async fn restore_signals_after_disposition<G, T>(
    guest: &mut G,
    old_mask_addr: AddrMut<'_, KernelSigset>,
) -> Result<(), Error>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    if !guest
        .config()
        .backend
        .requires_thread_directed_process_signals
    {
        let old_mask: Addr<'_, KernelSigset> = old_mask_addr.into();
        let restore_signals = syscalls::RtSigprocmask::new()
            .with_how(libc::SIG_SETMASK)
            .with_set(Some(old_mask.cast()))
            .with_oldset(None)
            .with_sigsetsize(KERNEL_SIGSET_SIZE);
        guest.inject_with_retry(restore_signals).await?;
    }
    if guest.config().backend.reports_child_exit_publication
        || crate::scheduler::discarded_at_send::records(guest.config())
    {
        crate::tool_global::set_saved_guest_sigmask(guest, None).await;
    }
    Ok(())
}

async fn interrupted_child_wait_result<G, T, S>(
    guest: &mut G,
    call: S,
    disposition: WaitSignalDisposition,
) -> Result<i64, Error>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
    S: SyscallInfo,
{
    if !guest
        .config()
        .backend
        .requires_thread_directed_process_signals
    {
        return Err(Errno::ERESTARTSYS.into());
    }
    if disposition == WaitSignalDisposition::Interrupt {
        return Err(Errno::EINTR.into());
    }

    guest.tail_inject(call).await
}

fn snapshot_process_group(pid: Pid) -> Result<libc::pid_t, Errno> {
    let pgrp = Process::new(pid.as_raw())
        .and_then(|process| process.stat())
        .map(|stat| stat.pgrp)
        .map_err(|_| Errno::ESRCH)?;
    if pgrp == 0 {
        Err(Errno::EOPNOTSUPP)
    } else {
        Ok(pgrp)
    }
}

fn guest_fd_status_flags(pid: Pid, fd: libc::c_int) -> Result<libc::c_int, Errno> {
    let path = format!("/proc/{}/fdinfo/{}", pid.as_raw(), fd);
    let contents = std::fs::read_to_string(path).map_err(|_| Errno::EBADF)?;
    let flags = contents
        .lines()
        .find_map(|line| line.strip_prefix("flags:"))
        .map(str::trim)
        .ok_or(Errno::EINVAL)?;
    libc::c_int::from_str_radix(flags, 8).map_err(|_| Errno::EINVAL)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FutexTimeout {
    Relative(u64),
    Absolute(LogicalTime),
}

/// Linux's `timespec64_valid`, which `futex_init_timeout` applies: a negative
/// second count or a nanosecond count outside `0..1e9` is EINVAL. Any other
/// value is valid, however large.
fn futex_timespec_is_valid(timeout: Timespec) -> bool {
    timeout.tv_sec >= 0 && (0..1_000_000_000).contains(&timeout.tv_nsec)
}

/// The largest futex timeout in nanoseconds: `timespec64_to_ktime` clamps a
/// valid timespec beyond it to `KTIME_MAX` rather than refusing it.
const FUTEX_KTIME_MAX_NANOS: u64 = i64::MAX as u64;

fn parse_futex_timeout(futex_op: i32, timeout: Timespec) -> Result<FutexTimeout, Errno> {
    if !futex_timespec_is_valid(timeout) {
        return Err(Errno::EINVAL);
    }
    let seconds = timeout.tv_sec as u64;
    let nanoseconds = timeout.tv_nsec as u64;
    // `timespec64_to_ktime`: KTIME_MAX from KTIME_SEC_MAX seconds on.
    let timeout_nanos = if seconds >= FUTEX_KTIME_MAX_NANOS / 1_000_000_000 {
        FUTEX_KTIME_MAX_NANOS
    } else {
        seconds * 1_000_000_000 + nanoseconds
    };
    // Mask off FUTEX_PRIVATE_FLAG / FUTEX_CLOCK_REALTIME before matching the
    // command: FUTEX_WAIT_BITSET measures its timeout as an *absolute* deadline,
    // whereas plain FUTEX_WAIT uses a *relative* one. A private-flagged
    // FUTEX_WAIT_BITSET (e.g. 0x89) must still be recognized as the BITSET
    // command; comparing the raw op would misclassify it as relative and add
    // the absolute deadline to the current time (leaking the epoch).
    if futex_op & libc::FUTEX_CMD_MASK == libc::FUTEX_WAIT_BITSET {
        Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
            timeout_nanos,
        )))
    } else {
        Ok(FutexTimeout::Relative(timeout_nanos))
    }
}

fn rebase_absolute_timeout(
    deadline: LogicalTime,
    clock_now: LogicalTime,
    logical_now: LogicalTime,
) -> LogicalTime {
    logical_now + Duration::from_nanos(deadline.as_nanos().saturating_sub(clock_now.as_nanos()))
}

fn absolute_timeout_uses_host_clock(
    deadline: LogicalTime,
    host_clock_now: LogicalTime,
    logical_now: LogicalTime,
) -> bool {
    deadline.as_nanos().abs_diff(host_clock_now.as_nanos())
        < deadline.as_nanos().abs_diff(logical_now.as_nanos())
}

/// Where a futex wait takes its deadline from.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum FutexDeadline {
    /// The call's own timeout.
    FromCall,
    /// The deadline an interrupted timed futex wait kept for the kernel's restart
    /// (`RestartBlock`).
    Restart(Option<LogicalTime>),
}

/// How a sequentialized `sched_yield` yields to the scheduler
/// ([`sched_yield_route`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SchedYieldRoute {
    /// A recorded chaos priority change point with a fresh random priority.
    ChaosReprioritize,
    /// A one-turn `SchedYield`, with the timeslice statistics reset.
    ReplayStrongYield,
    /// A `SchedYield` that the scheduler turns into a yield epoch.
    ChaosYieldEpoch,
    /// A `SleepUntil(0)` yield, granted at once.
    WeakYield,
    /// The end of the caller's timeslice, as at a timer expiry.
    EndTimeslice,
}

/// Choose how a sequentialized `sched_yield` yields; it reads only the
/// configuration, so a record and its replay choose alike.
///
/// - In chaos mode, thread-interleaving diversity (and thus fairness) comes
///   from re-randomizing thread priorities at preemption-timer expirations.
///   When timer preemption is disabled (`--max-timeslice disabled`),
///   priorities are fixed at thread creation and never change, so a thread
///   that spins on sched_yield while holding the numerically-lowest priority
///   would starve every thread it waits on (GH #81). There sched_yield is an
///   explicit chaos reprioritization point, recorded for chaos replay.
/// - With the timer on, chaos still redraws a priority only when a slice
///   expires, which a sched_yield loop may never reach
///   (https://github.com/rrnewton/hermit/issues/4068). There sched_yield is a
///   `SchedYield`, which the scheduler turns into a yield epoch
///   (`Scheduler::requeue_after`): the caller keeps its band, and once it has
///   passed another runnable thread over more than
///   `PLAIN_YIELDS_BEFORE_EPOCH` times it is not selected again until a thread
///   that did not yield has had a turn. It changes no
///   priority, draws nothing from the chaos PRNG and consumes no recorded
///   preemption point, so a preemption replay under `--chaos` makes the same
///   decision. A schedule replay keeps the weak yield it always had, and so
///   does a backend that does not deliver the preemption timer
///   (`BackendCapabilities::delivers_preemption_timer_events`): there a thread handed the
///   turn that never makes a syscall would never give it back.
/// - `--sched-heuristic=random` also keeps the weak yield. It draws a uniform
///   pick among the queued threads at every turn, so a yielder cannot hold
///   the CPU from the thread it waits on, and an epoch would only force the
///   yielders into near-equal shares: on the fp-reduction chaos cell
///   (https://github.com/rrnewton/hermit/issues/4150) it cut the outcomes
///   over 32 seeds to fewer than the 7 of 8 classes the cell requires.
///   `StickyRandom` keeps its pick across turns, so it keeps the epoch.
fn sched_yield_route(cfg: &Config) -> SchedYieldRoute {
    if cfg.chaos && cfg.max_timeslice.is_none() {
        SchedYieldRoute::ChaosReprioritize
    } else if !cfg.chaos && cfg.replay_preemptions_from.is_some() {
        SchedYieldRoute::ReplayStrongYield
    } else if cfg.chaos
        && cfg.replay_schedule_from.is_none()
        && cfg.backend.delivers_preemption_timer_events
        && cfg.sched_heuristic != SchedHeuristic::Random
    {
        SchedYieldRoute::ChaosYieldEpoch
    } else if cfg.chaos || cfg.replay_schedule_from.is_some() {
        SchedYieldRoute::WeakYield
    } else {
        SchedYieldRoute::EndTimeslice
    }
}

impl<T: RecordOrReplay> Detcore<T> {
    async fn futex_timeout_deadline<G: Guest<Self>>(
        &self,
        guest: &mut G,
        futex_flags: i32,
        timeout: Option<Addr<'_, Timespec>>,
    ) -> Result<Option<LogicalTime>, Error> {
        let Some(timeout) = timeout else {
            return Ok(None);
        };
        let timeout = parse_futex_timeout(futex_flags, guest.memory().read_value(timeout)?)?;
        match timeout {
            FutexTimeout::Relative(nanos) => {
                let now = thread_observe_time(guest).await;
                Ok(Some(now + Duration::from_nanos(nanos)))
            }
            FutexTimeout::Absolute(deadline)
                if self.cfg.virtualize_time
                    && !self.cfg.backend.guest_clock_reads_bypass_backend =>
            {
                Ok(Some(deadline))
            }
            FutexTimeout::Absolute(deadline) => {
                let clockid = if futex_flags & libc::FUTEX_CLOCK_REALTIME != 0 {
                    syscalls::ClockId::CLOCK_REALTIME
                } else {
                    syscalls::ClockId::CLOCK_MONOTONIC
                };

                let mut stack = guest.stack().await;
                let clock_output = syscalls::TimespecMutPtr(stack.reserve());
                let _stack_guard = stack.commit()?;
                let clock_call = syscalls::ClockGettime::new()
                    .with_clockid(clockid)
                    .with_tp(Some(clock_output));
                if self.cfg.virtualize_time && self.cfg.backend.guest_clock_reads_bypass_backend {
                    // Read the same live host clock as a direct guest vDSO call. Replaying a
                    // recorded value here would compare this run's host-domain deadline with the
                    // previous run's clock and turn a short timeout into an arbitrary long one.
                    guest.inject(Syscall::from(clock_call)).await?;
                } else {
                    self.record_or_replay(guest, clock_call).await?;
                }
                let clock_now = match parse_futex_timeout(
                    libc::FUTEX_WAIT_BITSET,
                    guest.memory().read_value(clock_output.0)?,
                )? {
                    FutexTimeout::Absolute(time) => time,
                    FutexTimeout::Relative(_) => unreachable!(),
                };
                let logical_now = thread_observe_time(guest).await;
                if self.cfg.virtualize_time
                    && !absolute_timeout_uses_host_clock(deadline, clock_now, logical_now)
                {
                    return Ok(Some(deadline));
                }
                Ok(Some(rebase_absolute_timeout(
                    deadline,
                    clock_now,
                    logical_now,
                )))
            }
        }
    }

    /// The deadline of a futex wait, from the call's timeout or from the record an
    /// interrupted wait kept for its restart (`FutexDeadline`).
    async fn futex_deadline<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
        deadline: FutexDeadline,
    ) -> Result<Option<LogicalTime>, Error> {
        match deadline {
            FutexDeadline::FromCall => {
                self.futex_timeout_deadline(guest, call.futex_op(), call.timeout())
                    .await
            }
            FutexDeadline::Restart(deadline) => Ok(deadline),
        }
    }

    /// Promotes the open file descriptions whose model must stay one copy
    /// (random devices and timerfds, `ThreadState::promote_shared_open_files`)
    /// before this handler's native fork, process-copying clone or exec, on a
    /// backend whose tool runs inside the guest and whose runtime routes those
    /// operations through this handler (in-guest LiteInst). DBT runs them
    /// natively and its runtime promotes there; ptrace and KVM keep the one
    /// copy outside the guest. It runs inside the caller's turn, before the
    /// operation can expose a child or a new image
    /// (<https://github.com/rrnewton/hermit/issues/4381>).
    fn promote_before_native_lifecycle<G: Guest<Self>>(&self, guest: &G) {
        let backend = &guest.config().backend;
        if self.cfg.sequentialize_threads
            && backend.promotes_open_files_before_native_lifecycle
            && !crate::syscalls::timerfd::detcore_runs_outside_the_guest(backend)
        {
            guest.thread_state().promote_shared_open_files().expect(
                "could not publish shared open file descriptions before a native fork or exec",
            );
        }
    }

    /// Clone, clone3, fork, vfork system calls
    pub async fn handle_clone_family<G: Guest<Self>>(
        &self,
        guest: &mut G,
        clone_family: syscalls::family::CloneFamily,
    ) -> Result<i64, Error> {
        if let syscalls::family::CloneFamily::Clone3(clone) = &clone_family
            && let Some(refusal) = self.clone3_set_tid_refusal(guest, clone).await
        {
            return refusal;
        }
        // clone3's `struct clone_args` is read field by field
        // (`clone3_field`): Reverie's whole-struct read fails for a valid
        // 64-byte one at the end of a mapping, which lost its flags, exit
        // signal and child tid address.
        let (flags, exit_signal, child_tid) = match &clone_family {
            syscalls::family::CloneFamily::Clone3(clone) => {
                let memory = guest.memory();
                (
                    clone3_field(&memory, clone, 0)
                        .map_or_else(CloneFlags::empty, CloneFlags::from_bits_truncate),
                    clone3_field(&memory, clone, 32).map_or(0, |signal| signal as libc::c_int),
                    clone3_field(&memory, clone, 16).map_or(0, |address| address as usize),
                )
            }
            syscalls::family::CloneFamily::Clone(clone) => (
                clone_family.flags(&guest.memory()),
                (clone.flags().bits() & 0xff) as libc::c_int,
                clone_family.child_tid(&guest.memory()),
            ),
            #[cfg(not(target_arch = "aarch64"))]
            _ => (
                clone_family.flags(&guest.memory()),
                libc::SIGCHLD,
                clone_family.child_tid(&guest.memory()),
            ),
        };
        let ctid = child_tid_clear_address(flags, child_tid);
        // Without sequentialized threads a sibling can run while this call
        // does, and rewrite the clone's arguments after they are read here or
        // the pidfd word before it is read back, so no pidfd is tracked then.
        let pidfd_address = guest
            .config()
            .sequentialize_threads
            .then(|| clone_pidfd_address(&clone_family, &guest.memory(), flags))
            .flatten();
        // Only a CLONE_VFORK parent waits for its child. Every backend runs a
        // forked process child concurrently with its parent and dispatches a
        // cloned thread's syscalls through this tool.
        let is_vfork = flags.contains(CloneFlags::CLONE_VFORK);

        let ts = guest.thread_state_mut();
        assert_eq!(ts.clone_flags, None);
        assert!(ts.pending_vfork.is_none());
        ts.clone_flags = Some(flags);

        let parent_dettid = ts.dettid;
        let child_priority_entropy = if is_vfork
            && self.cfg.chaos
            && self.cfg.replay_preemptions_from.is_none()
            && self.cfg.replay_schedule_from.is_none()
        {
            let mut parent_chaos_prng = ts.chaos_prng.clone();
            Some(parent_chaos_prng.next_u64())
        } else {
            None
        };
        if is_vfork {
            ts.pending_vfork = Some(PendingVfork {
                parent_dettid,
                parent_detpid: ts.detpid.expect("detpid unset"),
                child_tid_addr: ctid,
                flags,
                exit_signal,
                child_priority_entropy,
            });
        }

        // A child's exit signal other than SIGCHLD reaches this process when
        // the child exits, at a moment set by host timing. Record it in this
        // turn, before the child exists, for every thread of the caller's
        // process, and never forget it (`Scheduler::record_host_timed_signals`).
        let exit_signals = clone_host_timed_signals(flags, exit_signal);
        if exit_signals != 0
            && bundled_signal_facts::host_timed_signal_sources_are_physical(&guest.config().backend)
        {
            record_host_timed_signals(guest, HostTimedSignalScope::Caller, exit_signals).await;
        }

        if !flags.contains(CloneFlags::CLONE_THREAD) {
            self.promote_before_native_lifecycle(guest);
        }

        // Actions shared with a new process are withdrawn from the
        // scheduler before the child exists.
        super::signal_shadow::before_clone(guest, flags.bits()).await;

        trace!("[detcore, dtid {}] parent invoking clone.", parent_dettid);
        let blocking_child_op_id =
            ExternalOpId::new(parent_dettid, guest.thread_state().stats.syscall_count);

        // A CLONE_VFORK parent cannot resume until the child execs or exits.
        // Relinquish the parent's scheduler turn before entering that
        // blocking operation.
        if is_vfork && self.cfg.sequentialize_threads {
            let mut resources = Resources::new(parent_dettid);
            resources.insert(
                ResourceID::BlockingVfork(blocking_child_op_id),
                Permission::RW,
            );
            resources.fyi("clone_vfork");
            resource_request(guest, resources).await;
        }

        let call = Syscall::from(clone_family);
        let sysno = call.number();
        let maybe_res = guest.inject(call).await;
        // Ask before anything else is injected: the next injection replaces
        // the backend's record.
        let refusal = if maybe_res.is_err() {
            guest.take_unsupported_refusal()
        } else {
            None
        };

        if is_vfork && self.cfg.sequentialize_threads {
            let mut resources = Resources::new(parent_dettid);
            if maybe_res.is_err() {
                // TODO-HUMAN-REVIEW(PR-1152): Review failed deferred-vfork cancellation.
                // A deferred-spawn backend cannot infer failure from the absence of a registered
                // child: a successful child also registers after this continuation. Report the
                // known injected-syscall outcome explicitly so the scheduler can cancel the
                // barrier and re-admit this parent before we propagate the original error below.
                resources.insert(
                    ResourceID::VforkFailed(blocking_child_op_id),
                    Permission::RW,
                );
                resources.fyi("clone_vfork_failed");
            } else {
                resources.insert(
                    ResourceID::BlockedExternalContinue(blocking_child_op_id),
                    Permission::RW,
                );
                resources.fyi("clone_vfork");
            }
            resource_request(guest, resources).await;
        }

        let ts = guest.thread_state_mut();
        ts.clone_flags = None; // Unset, now that it has been read by the child.
        ts.pending_vfork = None;

        // A backend that cannot create the child says so with a typed refusal
        // (an in-guest backend runs one thread of the guest). Answering the
        // guest with the refusal's errno would be an error Linux never gives
        // a valid clone, so the run is refused instead, as for an exec the
        // backend cannot perform.
        if let (Some(refusal), Err(errno)) = (refusal, &maybe_res) {
            tracing::error!(
                "[detcore, dtid {parent_dettid}] {}; no child was created",
                refusal.diagnostic
            );
            if !self.cfg.panic_on_unsupported_syscalls {
                crate::tool_global::report_unsupported_syscall(guest, sysno).await;
            }
            return self
                .refuse_unserviceable_operation_because(guest, sysno, *errno, refusal.diagnostic)
                .await;
        }

        let res = maybe_res?;

        if !flags.contains(CloneFlags::CLONE_THREAD) {
            // Only a successful process clone can let another process mutate
            // inherited open file descriptions. Failed clone-family calls leave
            // the previously observed flock state authoritative.
            guest.thread_state().forget_flock_modes();
        }

        // Match ordinary clone: the parent consumes the priority entropy after
        // the child has inherited the parent state.
        if is_vfork
            && self.cfg.chaos
            && self.cfg.replay_preemptions_from.is_none()
            && self.cfg.replay_schedule_from.is_none()
        {
            let _ = guest
                .thread_state_mut()
                .chaos_prng_next_u64("child_priority");
        }

        let child_tid = Pid::from_raw(res as i32);
        let child_dettid = DetTid::from_raw(child_tid.into()); // TODO(T78538674), virtualized tid/pid
        trace!(
            "[detcore] dtid {} cloned, continuing parent + register new thread.",
            child_dettid
        );

        if !is_vfork {
            create_child_thread(guest, child_dettid, ctid, Some(flags), exit_signal, None).await;
        }

        if let Some(address) = pidfd_address {
            self.register_clone_pidfd(guest, address, child_dettid)
                .await;
        }

        {
            // The child will have updated their pedigree, we update ours before continuing.
            let parent_pedigree = &mut guest.thread_state_mut().pedigree;
            let child_pedigree = parent_pedigree.fork_mut();
            debug!(
                "[dtid {}] after creating child thread (tid {}, pedigree {}) parents pedigree becomes {}",
                parent_dettid, child_dettid, child_pedigree, parent_pedigree,
            );
        }

        // A nested creator sees its child's pid in its own PID namespace, as
        // the kernel returned it; Reverie reports the container's
        // (https://github.com/rrnewton/hermit/issues/4090).
        if guest.thread_state().nested_pid_namespace {
            let refusal = match crate::tool_global::local_pid(guest, child_dettid).await {
                Ok(Some(local)) => return Ok(local as i64),
                Ok(None) => {
                    format!("child {child_dettid} has no pid recorded in its creator's namespace")
                }
                Err(reason) => reason,
            };
            tracing::error!(
                "[detcore, dtid {}] the pid a clone in a nested PID namespace returns cannot be translated: {} (https://github.com/rrnewton/hermit/issues/4090)",
                parent_dettid,
                refusal,
            );
            return self
                .refuse_unserviceable_operation(
                    guest,
                    reverie::syscalls::Sysno::clone,
                    Errno::ENOSYS,
                )
                .await;
        }
        Ok(child_dettid.as_raw() as i64)
    }

    // TODO-HUMAN-REVIEW(PR-2985): Review scheduler tracking of set_tid_address.
    /// `set_tid_address` system call.
    ///
    /// Linux owns the guest-visible registration and return value. Detcore
    /// mirrors the accepted address into the scheduler so its modeled
    /// CHILD_CLEARTID wake targets the same word as the backend.
    pub async fn handle_set_tid_address<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SetTidAddress,
    ) -> Result<i64, Error> {
        let address = call.tidptr().map_or(0, |pointer| pointer.as_raw());
        let result = self
            .record_or_replay(guest, Syscall::SetTidAddress(call))
            .await?;
        if guest.config().sequentialize_threads {
            set_child_tid_address(guest, address).await;
        }
        trace!(
            "[detcore, dtid {}] child TID clear address registered: {address:#x}",
            guest.thread_state().dettid,
        );
        Ok(result)
    }

    /// `set_robust_list` system call.
    ///
    /// Still a pass-through: Linux owns the registration and supplies the
    /// result, and Detcore only records the head address so it can replay
    /// `exit_robust_list()` when the thread dies (see
    /// `Self::run_robust_list_owner_death`). Recording happens only after the
    /// kernel accepts the call, so a rejected length or address never becomes
    /// Detcore state.
    ///
    /// The call goes through `record_or_replay`, like every other pass-through.
    /// Using `Guest::inject` directly would keep the classification but drop the
    /// behavior it implies: the syscall would vanish from a `hermit record`
    /// trace, and a log recorded by a build that did record it would no longer
    /// replay.
    // AUTONOMOUS-BOT-IMPLEMENTED
    pub async fn handle_set_robust_list<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SetRobustList,
    ) -> Result<i64, Error> {
        let head = call.head().map(AddrMut::as_raw);
        let len = call.len();
        let res = self
            .record_or_replay(guest, Syscall::SetRobustList(call))
            .await?;
        // TODO-HUMAN-REVIEW(PR-2223): Review robust-list
        // head tracking used to drive owner-death wakeups.
        let recorded = match head {
            // `set_robust_list(NULL, ...)` unregisters the list.
            None => None,
            // Fail closed, and not dead code: `len` is the guest's own argument,
            // so a 32-bit guest (or a bug) can present the 12-byte
            // `compat_robust_list_head`, whose fields sit at different offsets.
            // The 64-bit walk would misread it, so refuse to record it at all
            // rather than walk a layout we cannot parse.
            Some(_) if len != robust_list::ROBUST_LIST_HEAD_LEN => None,
            Some(addr) => Some(addr),
        };
        guest.thread_state_mut().record_robust_list_head(recorded);
        trace!(
            "[detcore, dtid {}] robust-list head registered: {:?}",
            guest.thread_state().dettid,
            recorded,
        );
        Ok(res)
    }

    /// Replay Linux's `exit_robust_list()` for the calling thread.
    ///
    /// Linux runs this from `mm_release()` while a task dies. Detcore performs
    /// the same walk and issues the wake against its own modeled waiter pool,
    /// which is where precise-mode futex waiters actually live. Ptrace leaves
    /// the atomic word transition to Linux on every backend that enters this
    /// path ([`OwnerDeathPath`]), and the wake reaches a waiter only after
    /// Linux has made it. The algorithm is in
    /// [`crate::syscalls::robust_list`]; this function only supplies guest
    /// memory and the scheduler wake.
    ///
    /// Only the precise futex model needs this. Polling and external modes park
    /// waiters in the host kernel, which performs its own robust-list cleanup;
    /// and without thread sequentialization Detcore does not model futexes at
    /// all.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-2223): Review owner-death wakeup
    // emulation, which changes how a dying thread's peers are scheduled.
    async fn run_robust_list_owner_death<G: Guest<Self>>(&self, guest: &mut G) {
        let Some(path) = self.robust_owner_death_path() else {
            return;
        };
        let Some(head) = guest.thread_state().robust_list_head else {
            return;
        };
        let dettid = guest.thread_state().dettid;
        // Which TID goes in the comparison? Not one Detcore hands out: `gettid`
        // is a reviewed pass-through, and glibc puts the value the kernel
        // supplied through `CLONE_PARENT_SETTID` into the lock word, not a
        // `gettid` result. The comparison is valid for a narrower reason —
        // `DetTid` currently *is* the raw namespaced `Tid`. `init_thread_state`
        // builds every one with `DetPid::from_raw(tid.into())` (detcore/src/lib.rs,
        // `lib.rs:1257` at this commit), four lines under
        // `// TODO(T78538674): virtualize tid, extend tid<=>dettid mapping here.`
        // Completing that TODO breaks this comparison, which would then have to
        // map back to the guest-visible TID.
        // `hermit-cli/tests/robust_futex_owner_death.rs` is the tripwire: it
        // fails the moment the two diverge.
        let owner_tid = dettid.as_raw() as u32;

        let mut marked = HashMap::new();
        let mut effects = GuestRobustEffects::<'_, G, T> {
            guest,
            dettid,
            staged_wakes: None,
            marked: &mut marked,
            tool: PhantomData,
        };
        let outcome = robust_list::exit_robust_list(&mut effects, head, owner_tid).await;
        if path == OwnerDeathPath::Barrier {
            guest.thread_state().mark_robust_list_walked_by_tool();
        }
        if outcome.head_unreadable {
            trace!(
                "[detcore, dtid {}] unreadable robust-list head at {:#x}; no owner-death wakeups",
                dettid, head,
            );
        } else if outcome.aborted || outcome.next_faulted || outcome.truncated {
            trace!(
                "[detcore, dtid {}] robust-list walk stopped early after {} entr(ies): {:?}",
                dettid, outcome.entries_visited, outcome,
            );
        }
    }

    /// Read every registered robust list in the current thread group while
    /// its address space still exists, but hold its modeled wakes until the
    /// corresponding exit callback. On ptrace that callback confirms Linux has
    /// completed the atomic owner-word update.
    ///
    /// On [`OwnerDeathPath::Barrier`] nothing is staged. An exit_group walks
    /// the words and wakes their waiters at once instead
    /// (`Self::handle_exit_group`), and no waiter runs before the kernel has
    /// ended the group. A delivered signal
    /// may still be caught or ignored: Detcore must not mark a live owner's
    /// word, and that backend's native exit callback cannot tell which signal
    /// ended the thread group.
    pub(crate) async fn stage_thread_group_robust_list_wakes<G: Guest<Self>>(
        &self,
        guest: &mut G,
        reason: RobustListExit,
    ) {
        match self.robust_owner_death_path() {
            None | Some(OwnerDeathPath::Barrier) => {}
            Some(OwnerDeathPath::Kernel) => {
                let staged = self
                    .read_thread_group_robust_list_wakes(guest, OwnerDeath::Exit)
                    .await;
                guest.thread_state().stage_robust_list_wakes(reason, staged);
            }
        }
    }

    /// Replay Linux's `exit_robust_list()` for each `(owner, head)` of the
    /// current thread group, in owner order, as that owner's list, waking at
    /// once ([`OwnerDeathPath::Barrier`]). The threads the grant killed have not made
    /// their native exits (`ThreadState::wait_for_exit_group_robust_walk`), so
    /// every word reads as the guest left it.
    async fn run_thread_group_robust_list_owner_death<G: Guest<Self>>(
        &self,
        guest: &mut G,
        heads: Vec<RobustListOwner>,
    ) {
        // One group exit: a word Linux marks under one owner reads as marked
        // under every later one.
        let mut marked = HashMap::new();
        for RobustListOwner {
            dettid: owner,
            head,
        } in heads
        {
            let mut effects = GuestRobustEffects::<'_, G, T> {
                guest,
                dettid: owner,
                staged_wakes: None,
                marked: &mut marked,
                tool: PhantomData,
            };
            let outcome =
                robust_list::exit_robust_list(&mut effects, head, owner.as_raw() as u32).await;
            if outcome.head_unreadable || outcome.aborted {
                trace!(
                    "[detcore, dtid {}] robust-list owner death stopped early: {:?}",
                    owner, outcome,
                );
            }
        }
    }

    /// How this backend orders owner-death wakes after Linux's walk, or `None`
    /// where Detcore does not model robust-futex owner death.
    ///
    /// Only the precise futex model needs owner death modeled. Polling and
    /// external modes park waiters in the host kernel, which performs its own
    /// robust-list cleanup; and without thread sequentialization Detcore does
    /// not model futexes at all.
    fn robust_owner_death_path(&self) -> Option<OwnerDeathPath> {
        owner_death_path(&self.cfg)
    }

    /// The owner-death wakes a successful execve makes Linux deliver, in
    /// delivery order, for a backend that runs the native execve outside
    /// Detcore's handler and so cannot stage them in this thread's state (DBT,
    /// whose new image starts a fresh runtime). Read while the old image is
    /// mapped, before the native exec; none where Detcore does not model
    /// owner death (<https://github.com/rrnewton/hermit/issues/4268>).
    pub async fn exec_robust_list_wakes<G: Guest<Self>>(
        &self,
        guest: &mut G,
    ) -> Vec<(DetTid, FutexID)> {
        if self.robust_owner_death_path().is_none() {
            return Vec::new();
        }
        let wakes = self
            .read_thread_group_robust_list_wakes(guest, OwnerDeath::Exec)
            .await;
        crate::tool_local::flatten_exec_robust_list_wakes(wakes)
            .into_iter()
            .map(|(owner, wake)| (owner, wake.futex))
            .collect()
    }

    /// The owner-death wakes Linux's `exit_robust_list` would make for every
    /// registered robust list in the current thread group, read without
    /// changing guest memory: Linux makes the owner-word transitions. Callers
    /// decide whether Detcore models owner death at all
    /// ([`Self::robust_owner_death_path`]).
    ///
    /// Each list is walked as its owner's, with that owner's TID, except the
    /// caller's own list in an exec by a thread that is not the group leader.
    /// `de_thread` gives that caller the leader's TID (`exchange_tids`) before
    /// `exec_mm_release` walks its list, so `handle_futex_death` compares the
    /// owner words with the thread group ID and leaves a word the caller owns
    /// under its old TID alone (fs/exec.c, kernel/futex/core.c).
    async fn read_thread_group_robust_list_wakes<G: Guest<Self>>(
        &self,
        guest: &mut G,
        death: OwnerDeath,
    ) -> Vec<(DetTid, Vec<RobustListWake>)> {
        let caller = guest.thread_state().dettid;
        let tgid = guest.thread_state().detpid.expect("detpid unset");
        // A thread whose own exit Detcore already walked (its head stays
        // registered while an exit_group is filed) is left out, as an
        // exit_group's walk leaves it out, so no list is walked twice (Claude
        // review of https://github.com/rrnewton/hermit/pull/4352, F4).
        let mut heads: Vec<(DetTid, usize)> = guest
            .thread_state()
            .robust_list_owners_for_exit_group()
            .into_iter()
            .map(|owner| (owner.dettid, owner.head))
            .collect();
        if death == OwnerDeath::Exec {
            // de_thread waits until every other thread has exited, each walking
            // its own list, before exec_mm_release walks the caller's; with
            // marks carried across lists, the caller's must come last (Claude
            // review of https://github.com/rrnewton/hermit/pull/4277 at
            // 65138a17, N5).
            heads.sort_by_key(|(owner, _)| *owner == caller);
        }
        let mut staged = Vec::with_capacity(heads.len());
        let mut marked = HashMap::new();
        for (owner, head) in heads {
            let walked_as = if death == OwnerDeath::Exec && owner == caller {
                tgid.as_raw()
            } else {
                owner.as_raw()
            };
            let mut wakes = Vec::new();
            let mut effects = GuestRobustEffects::<'_, G, T> {
                guest,
                dettid: owner,
                staged_wakes: Some(&mut wakes),
                marked: &mut marked,
                tool: PhantomData,
            };
            let outcome = robust_list::exit_robust_list(&mut effects, head, walked_as as u32).await;
            if outcome.head_unreadable || outcome.aborted {
                trace!(
                    "[detcore, dtid {}] could not stage complete robust-list owner-death effects: {:?}",
                    owner, outcome,
                );
            }
            staged.push((owner, wakes));
        }
        staged
    }

    /// Exit system call
    pub async fn handle_exit<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Exit,
    ) -> Result<i64, Error> {
        // This thread may be its process's last (`crate::guest_log`).
        crate::guest_log::flush_unsent();
        // A raw `exit` of a process's last thread ends the process as an
        // `exit_group` does, and the scheduler then sends the parent's SIGCHLD
        // with Linux's siginfo, as for `handle_exit_group`. Only the exit grant
        // knows whether this thread is the last, so every raw exit reports its
        // status, kept for this thread alone until that grant settles it.
        if self.cfg.backend.reports_kernel_child_exit_signals {
            let uid = real_uid(guest.tid().as_raw()).unwrap_or_else(|| unsafe { libc::getuid() });
            crate::tool_global::child_exit_sigchld(
                guest,
                crate::scheduler::child_exit_sigchld::ChildExitSigchldControl::RawExitStatus {
                    code: libc::CLD_EXITED,
                    status: call.status() & 0xff,
                    uid,
                },
            )
            .await;
        }
        let request = guest.thread_state().mk_request(
            ResourceID::Exit {
                group: false,
                process: guest.thread_state().detpid.expect("detpid unset"),
                mm: guest.thread_state().mm_id,
            },
            Permission::RW,
        );
        resource_request(guest, request).await;
        self.run_robust_list_owner_death(guest).await;
        // A leader whose group lives on is a zombie from here until its last
        // sibling leaves, and its backend reports its exit only then: it
        // leaves the scheduler now, after its last request
        // (https://github.com/rrnewton/hermit/issues/4100).
        let state = guest.thread_state();
        if self.cfg.sequentialize_threads
            && state
                .detpid
                .is_some_and(|pid| pid.as_raw() == state.dettid.as_raw())
            && crate::tool_global::retire_exited_leader(guest).await
            && self.cfg.replay_data.is_some()
        {
            // The recorder reads the process's descriptor table through its
            // leader, which is a zombie from here, so it would record the
            // group's later output as ordinary writes that replay drops
            // (https://github.com/rrnewton/hermit/issues/4244).
            let message = format!(
                "hermit: thread-group leader {} exited while its threads run on, which record \
                 and replay cannot yet capture, so the run is refused \
                 (https://github.com/rrnewton/hermit/issues/4244)",
                guest.thread_state().dettid
            );
            tracing::error!("{message}");
            let _ = std::io::Write::write_fmt(
                &mut crate::util::RetryingStderr,
                format_args!("{message}\n"),
            );
            crate::tool_global::unrecoverable_shutdown(
                guest,
                detcore_model::HERMIT_POLICY_REFUSAL_EXIT,
            )
            .await;
        }
        // It's ok here that we skip running the posthook:
        guest.tail_inject(call).await
    }

    /// Exit_group system call
    pub async fn handle_exit_group<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::ExitGroup,
    ) -> Result<i64, Error> {
        // One of the last points where this process's unsent warnings can
        // still be reported (`crate::guest_log`).
        crate::guest_log::flush_unsent();
        // Where the kernel also notifies the parent, the scheduler sends the
        // parent's SIGCHLD itself and gives it the siginfo Linux would
        // (`scheduler::child_exit_sigchld`): CLD_EXITED and the exit code.
        if self.cfg.backend.reports_kernel_child_exit_signals {
            let uid = real_uid(guest.tid().as_raw()).unwrap_or_else(|| unsafe { libc::getuid() });
            crate::tool_global::child_exit_sigchld(
                guest,
                crate::scheduler::child_exit_sigchld::ChildExitSigchldControl::ExitStatus {
                    code: libc::CLD_EXITED,
                    status: call.status() & 0xff,
                    uid,
                },
            )
            .await;
        }
        // On [`OwnerDeathPath::Barrier`] the walk follows the grant and wakes
        // at once, as an exit does (`Self::run_robust_list_owner_death`):
        // nothing can call the exit_group off, the grant logically kills the
        // group's other threads, so none of them runs guest code again, and
        // the scheduler commits no turn until the kernel has ended every
        // thread of the group, so no waiter runs before Linux has marked its
        // word. Holding the wakes for the exit callbacks would lose them on
        // DBT, which answers a logically killed thread's callback
        // `ThreadExited`. Read the registrations at the grant, as Linux's
        // walks use the ones current when each thread dies. Filing first keeps
        // an exiting thread's head registered until then and holds the killed
        // threads' native exits until the walk is done
        // (`ThreadState::file_exit_group_for_robust_lists`,
        // `ThreadState::wait_for_exit_group_robust_walk`): a killed thread's
        // native exit would otherwise let Linux mark and clear its words before
        // the walk reads them. The lists stay mapped until the native
        // exit_group.
        let barrier = self.robust_owner_death_path() == Some(OwnerDeathPath::Barrier);
        if barrier {
            guest.thread_state().file_exit_group_for_robust_lists();
        }
        let request = guest.thread_state().mk_request(
            ResourceID::Exit {
                group: true,
                process: guest.thread_state().detpid.expect("detpid unset"),
                mm: guest.thread_state().mm_id,
            },
            Permission::RW,
        );
        resource_request(guest, request).await;
        if barrier {
            guest.thread_state().begin_exit_group_robust_walk();
            let owners = guest.thread_state().robust_list_owners_for_exit_group();
            self.run_thread_group_robust_list_owner_death(guest, owners)
                .await;
            guest.thread_state().finish_exit_group_robust_walk();
        } else {
            self.stage_thread_group_robust_list_wakes(guest, RobustListExit::ExitGroup)
                .await
        }
        // It's ok here that we skip running the posthook:
        guest.tail_inject(call).await
    }

    /// On DBT, end the thread group in the schedule before a signal that
    /// ends it by default is delivered natively
    /// (<https://github.com/rrnewton/hermit/issues/4278>). DynamoRIO carries
    /// out such a default action itself, and before the process exits it
    /// synchronizes with every other thread of it. A thread waiting for its
    /// turn is blocked in the client's call to the coordinator, inside a
    /// DynamoRIO event callback, where DynamoRIO cannot suspend it, so the
    /// dying thread waited for it forever. The `Exit` grant of an exit_group
    /// logically kills the group's other threads and cancels their pending
    /// requests, so each returns from its callback and can be synchronized;
    /// the robust lists are walked as an exit_group's are. Returns whether it
    /// ended the group.
    ///
    /// The decision is the action read in this thread's `InboundSignal` turn.
    /// Linux commits it under the signal lock: for an unblocked signal,
    /// `complete_signal` sets `SIGNAL_GROUP_EXIT` and queues SIGKILL to every
    /// other thread of the group, so no sibling runs another user instruction
    /// after the decision. Filing the `Exit` request ends this thread's turn,
    /// so the thread first asks that the request be granted at the very next
    /// turn (`Scheduler::hold_turn_for_group_exit`). No sibling takes a turn
    /// between the decision and the grant, which logically kills the siblings,
    /// so none runs guest code before the native delivery (Claude re-check of
    /// https://github.com/rrnewton/hermit/pull/4387).
    ///
    /// One path can still run a sibling first: an exit parked behind a
    /// sibling's signal-delivery permit (`Scheduler::exit_awaits_sibling_permit`).
    /// DynamoRIO reads the action again when it delivers, so after the grant
    /// the action is read again and, if a sibling changed it, set back to
    /// `SIG_DFL` in this dying process, and the delivery carries out the
    /// committed decision (Codex review of
    /// https://github.com/rrnewton/hermit/pull/4387, P1).
    ///
    /// The gate is this backend's: DynamoRIO carries out a default action in
    /// the process and synchronizes every thread before it exits. Today only
    /// DBT sends process signals to a named thread
    /// (`requires_thread_directed_process_signals`), so that flag selects it.
    pub(crate) async fn end_thread_group_before_fatal_delivery<G: Guest<Self>>(
        &self,
        guest: &mut G,
        signal: SigWrapper,
    ) -> bool {
        if !(self.cfg.sequentialize_threads
            && self.cfg.backend.requires_thread_directed_process_signals)
            || !signal_default_action_ends_the_process(signal)
        {
            return false;
        }
        match self.signal_is_left_at_default(guest, signal).await {
            Ok(true) => {}
            Ok(false) => return false,
            Err(error) => {
                tracing::warn!(
                    "[detcore, dtid {}] cannot read the disposition of {}: {}",
                    guest.thread_state().dettid,
                    signal,
                    error
                );
                return false;
            }
        }
        info!(
            "[detcore, dtid {}] {} ends its thread group by default",
            guest.thread_state().dettid,
            signal
        );
        let barrier = self.robust_owner_death_path() == Some(OwnerDeathPath::Barrier);
        if barrier {
            guest.thread_state().file_exit_group_for_robust_lists();
        }
        let dettid = guest.thread_state().dettid;
        if !hold_turn_for_group_exit(guest, dettid).await {
            tracing::warn!(
                "[detcore, dtid {}] the scheduler did not hold the turn for the group exit of {}",
                dettid,
                signal
            );
        }
        let request = guest.thread_state().mk_request(
            ResourceID::Exit {
                group: true,
                process: guest.thread_state().detpid.expect("detpid unset"),
                mm: guest.thread_state().mm_id,
            },
            Permission::RW,
        );
        resource_request(guest, request).await;
        match self.signal_is_left_at_default(guest, signal).await {
            Ok(true) => {}
            changed => {
                info!(
                    "[detcore, dtid {}] the action for {} changed after the delivery decision \
                     ({:?}); restoring SIG_DFL in the dying process",
                    guest.thread_state().dettid,
                    signal,
                    changed.map(|default| !default)
                );
                if let Err(error) = self.restore_default_action(guest, signal).await {
                    tracing::warn!(
                        "[detcore, dtid {}] cannot restore SIG_DFL for {}: {}",
                        guest.thread_state().dettid,
                        signal,
                        error
                    );
                }
            }
        }
        if barrier {
            guest.thread_state().begin_exit_group_robust_walk();
            let owners = guest.thread_state().robust_list_owners_for_exit_group();
            self.run_thread_group_robust_list_owner_death(guest, owners)
                .await;
            guest.thread_state().finish_exit_group_robust_walk();
        }
        true
    }

    /// Set the guest's action for `signal` to `SIG_DFL`.
    async fn restore_default_action<G: Guest<Self>>(
        &self,
        guest: &mut G,
        signal: SigWrapper,
    ) -> Result<(), Error> {
        let default = KernelSigaction {
            handler: libc::SIG_DFL as u64,
            flags: 0,
            restorer: 0,
            mask: 0,
        };
        let mut stack = guest.stack().await;
        let action_addr = stack.push(default);
        let _guard = stack.commit()?;
        let call = syscalls::RtSigaction::new()
            .with_signum(signal.raw())
            .with_action(Some(action_addr.cast()))
            .with_old_action(None)
            .with_sigsetsize(std::mem::size_of::<u64>());
        guest.inject_with_retry(call).await?;
        // Detcore's own change, which the signal-state copy does not record:
        // the copy, and the actions it shares, are unknown now.
        super::signal_shadow::give_up(
            guest,
            "Detcore reset an action to the default before a fatal delivery",
        );
        Ok(())
    }

    /// Whether the guest's action for `signal` is the default (`SIG_DFL`),
    /// as the backend reports it to the guest.
    async fn signal_is_left_at_default<G: Guest<Self>>(
        &self,
        guest: &mut G,
        signal: SigWrapper,
    ) -> Result<bool, Error> {
        let mut stack = guest.stack().await;
        let action_addr = stack.reserve::<KernelSigaction>();
        let _guard = stack.commit()?;
        let call = syscalls::RtSigaction::new()
            .with_signum(signal.raw())
            .with_action(None)
            .with_old_action(Some(action_addr.cast()))
            .with_sigsetsize(std::mem::size_of::<u64>());
        guest.inject_with_retry(call).await?;
        let action: KernelSigaction = guest.memory().read_value(action_addr)?;
        Ok(action.handler == libc::SIG_DFL as u64)
    }

    /// Futex system call, which can block.
    pub async fn handle_futex<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
    ) -> Result<i64, Error> {
        self.handle_futex_from(guest, call, FutexDeadline::FromCall)
            .await
    }

    /// `handle_futex`, taking a wait's deadline from `deadline`. A `restart_syscall`
    /// resumes an interrupted timed futex wait here with the deadline it kept,
    /// and the value is compared again, as in Linux's `futex_wait_restart`
    /// (`Detcore::handle_restart_syscall`).
    pub(crate) async fn handle_futex_from<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
        deadline: FutexDeadline,
    ) -> Result<i64, Error> {
        let dettid = guest.thread_state().dettid;
        // Detcore emulates the futex commands below, so it also answers, as Linux's
        // `do_futex` (kernel/futex/syscalls.c) does, for the ones it does not serve:
        // before the futex address is even looked at.
        if self.cfg.sequentialize_threads
            && !matches!(self.cfg.debug_futex_mode, BlockingMode::External)
        {
            match classify_futex_command(call.futex_op()) {
                FutexCommand::Served => {}
                FutexCommand::NoSys => return Err(Error::Errno(Errno::ENOSYS)),
                FutexCommand::PriorityInheritance(name) => {
                    return self
                        .refuse_futex_command(
                            guest,
                            name,
                            "Detcore does not emulate priority-inheritance futexes",
                        )
                        .await;
                }
            }
        }
        // FUTEX_REQUEUE, FUTEX_CMP_REQUEUE and FUTEX_WAKE_OP key their words
        // without reading `uaddr` (only CMP_REQUEUE compares it), and NULL is a
        // valid private key for them, so they take their own path before the
        // NULL passthrough and the read below.
        if self.cfg.sequentialize_threads
            && !matches!(self.cfg.debug_futex_mode, BlockingMode::External)
        {
            let command = call.futex_op() & libc::FUTEX_CMD_MASK;
            // FUTEX_WAKE and FUTEX_WAKE_BITSET only key their word too, and a
            // requeue can leave a modeled waiter on any admitted private key,
            // NULL or unmapped included, which a wake must still reach.
            if matches!(command, libc::FUTEX_WAKE | libc::FUTEX_WAKE_BITSET)
                && matches!(self.cfg.debug_futex_mode, BlockingMode::Precise)
            {
                return self.handle_futex_wake(guest, call, command).await;
            }
            let name = match command {
                libc::FUTEX_REQUEUE => Some("FUTEX_REQUEUE"),
                libc::FUTEX_CMP_REQUEUE => Some("FUTEX_CMP_REQUEUE"),
                libc::FUTEX_WAKE_OP => Some("FUTEX_WAKE_OP"),
                _ => None,
            };
            if let Some(name) = name {
                // Polling waiters are not on a waiter list that a requeue or a
                // WAKE_OP's second wake could act on.
                if matches!(self.cfg.debug_futex_mode, BlockingMode::Polling) {
                    return self
                        .refuse_futex_command(guest, name, POLLING_REFUSAL)
                        .await;
                }
                return if command == libc::FUTEX_WAKE_OP {
                    // AUTONOMOUS-BOT-IMPLEMENTED
                    self.handle_futex_wake_op(guest, call).await
                } else {
                    // AUTONOMOUS-BOT-IMPLEMENTED
                    self.handle_futex_requeue(guest, call, command).await
                };
            }
        }
        let ptr = match call.uaddr() {
            None => {
                // null pointer error:
                return Ok(guest.inject(call).await?);
            }
            Some(x) => x,
        };
        if self.cfg.sequentialize_threads
            && !matches!(self.cfg.debug_futex_mode, BlockingMode::External)
            && !AddrMut::as_raw(ptr).is_multiple_of(std::mem::size_of::<u32>())
        {
            // `get_futex_key` refuses a misaligned futex word before any access.
            return Err(Error::Errno(Errno::EINVAL));
        }
        if !self.cfg.sequentialize_threads {
            return Ok(guest.inject(call).await?);
        }
        let polling = match self.cfg.debug_futex_mode {
            BlockingMode::External => {
                return self.record_or_replay_blocking(guest, call.into()).await;
            }
            BlockingMode::Precise => false,
            BlockingMode::Polling => true,
        };
        // Only the waits compare the word, and they read it as Linux's
        // `futex_wait_setup` does, with user-mode permissions: EFAULT for an
        // unmapped, PROT_NONE or otherwise unreadable word
        // (https://github.com/rrnewton/hermit/issues/4030). A wake left to the
        // polling mode reads nothing, as Linux's `futex_wake` reads nothing.
        //
        // Linux checks a wait's other arguments first, without touching the
        // word: `sys_futex` (kernel/futex/syscalls.c) copies and validates the
        // timeout (EFAULT, EINVAL), then `__futex_wait` refuses a zero bitset
        // (EINVAL). A restarted wait keeps its validated deadline.
        let init_val = match call.futex_op() & libc::FUTEX_CMD_MASK {
            command @ (libc::FUTEX_WAIT | libc::FUTEX_WAIT_BITSET) => {
                if matches!(deadline, FutexDeadline::FromCall)
                    && let Some(timeout) = call.timeout()
                {
                    let timeout = read_user_timespec(guest, timeout.as_raw())?;
                    if !futex_timespec_is_valid(timeout) {
                        return Err(Error::Errno(Errno::EINVAL));
                    }
                }
                if command == libc::FUTEX_WAIT_BITSET && call.val3() == 0 {
                    return Err(Error::Errno(Errno::EINVAL));
                }
                read_futex_word(guest, AddrMut::as_raw(ptr))?
            }
            _ => 0,
        };
        trace!(
            "[detcore, dtid {}] futex op with memory address containing value {}",
            &dettid, init_val
        );
        if polling {
            self.handle_futex_polling(guest, call, init_val, deadline)
                .await
        } else {
            self.handle_futex_blocking(guest, call, init_val, deadline)
                .await
        }
    }

    /// Blocking (precise) Futex implementation.
    /// Here we use a two-phase request to the scheduler: before and after the futex wait/wake
    /// side effects. We EMULATE futex calls and NEVER run them inside the kernel.
    pub(crate) async fn handle_futex_blocking<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
        init_val: i32,
        deadline: FutexDeadline,
    ) -> Result<i64, Error> {
        let ptr = call.uaddr().unwrap();
        let futexid = guest.thread_state().futex_id(
            AddrMut::as_raw(ptr),
            call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0,
        );
        let futex_op = call.futex_op() & libc::FUTEX_CMD_MASK;
        let bitset = match futex_op {
            libc::FUTEX_WAKE_BITSET | libc::FUTEX_WAIT_BITSET => call.val3() as u32,
            _ => u32::MAX,
        };
        if bitset == 0 {
            return Err(Error::Errno(Errno::EINVAL));
        }
        let dettid = guest.thread_state().dettid;
        match futex_op {
            libc::FUTEX_WAIT | libc::FUTEX_WAIT_BITSET => {
                if init_val != call.val() {
                    info!(
                        "[detcore, dtid {}] Futex wait running immediately because it will fizzle ({} != {}).",
                        &dettid,
                        init_val,
                        call.val()
                    );
                    Err(Error::Errno(Errno::EAGAIN))
                } else {
                    let maybe_timeout_lt = self.futex_deadline(guest, call, deadline).await?;
                    let signal_interruption = guest
                        .config()
                        .backend
                        .supports_blocked_wait_signal_interruption;
                    // Whether this wait was woken for a signal and is checking again.
                    let mut rewait = false;
                    // The signals the scheduler committed to this waiter at that wake
                    // (`SchedValue::Signaled`), which this turn's check counts although
                    // the wait holds them (https://github.com/rrnewton/hermit/issues/4005).
                    let mut committed_wake: KernelSigset = 0;
                    // Whether a wait request reached the scheduler. A wait that
                    // ends before its first one, on a pending signal or a failed
                    // read, sends no `WaitFinished`, as before.
                    let mut requested = false;
                    let res = loop {
                        // On a backend whose kernel reports the guest's signal state,
                        // a blocked, ignored, or default-ignored signal leaves the wait
                        // parked until its wakeup or its original deadline. As in
                        // Linux, one already pending when the value matches ends the
                        // wait at once with the futex's restart errno. A `SIGCHLD`
                        // ends it only when the scheduler committed it to this
                        // waiter at its wake, sent at a commit point (a child's
                        // child-exit timer, or a POSIX timer: `committed_wake`,
                        // https://github.com/rrnewton/hermit/issues/4005), or when it
                        // is a guest's thread-directed copy in this thread's private
                        // queue that the schedule recorded (`futex_wait_pending_signals`,
                        // https://github.com/rrnewton/hermit/issues/4132); any other
                        // stays pending until the call returns, because the kernel
                        // also posts one for a child event at a moment set by host
                        // timing (https://github.com/rrnewton/hermit/issues/3146). Nor does
                        // a signal that a host-timed source armed by a guest can
                        // post to this process, such as a parent-death signal
                        // (`host_timed_signals`, read here in this thread's turn).
                        //
                        // The same check decides a wait the scheduler woke for a
                        // signal. The scheduler commits such a wake without this
                        // thread running, and queues the thread to run next so that it
                        // is the thread that dequeues the signal. Checking again here,
                        // in the thread's own turn, keeps the restart errno for a
                        // signal the kernel will deliver to this thread as the call
                        // returns. If another thread took the signal first (a chaos
                        // schedule can run one ahead) or it became ignored, the wait
                        // parks again with its original absolute deadline, as
                        // `__futex_wait` retries a wait woken with no signal pending:
                        // an expired deadline ends it at the scheduler's next timed pop
                        // with ETIMEDOUT. Before parking again it reads the word, as
                        // `futex_wait_setup` does on that retry (see the re-check below).
                        // Returning the restart errno with nothing to deliver would
                        // instead have leaked it to the guest, or restarted the call
                        // with a fresh timeout.
                        //
                        // The scheduler is given only the mask, which only this thread
                        // can change and so cannot change while it is parked. A sibling
                        // can change the dispositions at any time, so the scheduler
                        // reads them when it commits a wake, not here
                        // (https://github.com/rrnewton/hermit/issues/3146).
                        let signal_watch = if signal_interruption {
                            // A failed read is never the call's errno
                            // (`read_wait_signal_state`).
                            let state = match read_wait_signal_state(guest.pid(), guest.tid()) {
                                Ok(state) => state,
                                Err(error) => break Err(error),
                            };
                            // A timed futex wait lets a default job-control stop
                            // wait for its deadline (`KernelSignalState::interrupting_wait`).
                            let defers_default_stops = call.restart_keeps_deadline();
                            let host_timed = host_timed_signals(guest).await;
                            let committed = std::mem::take(&mut committed_wake);
                            let held = futex_wait_held_after_wake(host_timed, committed);
                            let pending = futex_wait_pending_signals(
                                guest,
                                &state,
                                state.pending_interrupting(state.blocked, defers_default_stops),
                                held,
                                host_timed,
                                committed,
                            )
                            .await;
                            if pending != 0 {
                                let errno = call.kernel_restart_errno();
                                trace!(
                                    "[detcore, dtid {}] futex wait interrupted by pending signals {:#x}: {:?}",
                                    &dettid, pending, errno
                                );
                                break Err(Error::Errno(errno));
                            }
                            Some(FutexSignalWatch {
                                unblocked: !state.blocked
                                    & !kernel_sigset_bit(reverie::PERF_EVENT_SIGNAL as i32),
                                defers_default_stops,
                                pid: guest.pid().as_raw(),
                                tid: guest.tid().as_raw(),
                            })
                        } else {
                            None
                        };
                        if rewait {
                            // No signal is pending for this thread, so Linux's
                            // `__futex_wait` retries `futex_wait_setup`, which reads the
                            // word again with user-mode permissions: EFAULT if it cannot,
                            // EAGAIN if it changed, and otherwise the wait parks again
                            // (https://github.com/rrnewton/hermit/issues/4033). While the
                            // wait was off the waiter list a `FUTEX_WAKE` could not reach
                            // it, and a waker that saw no waiter is consistent with
                            // exactly this retry window.
                            let observed = match read_futex_word(guest, AddrMut::as_raw(ptr)) {
                                Ok(observed) => observed,
                                Err(error) => break Err(error),
                            };
                            if observed != call.val() {
                                trace!(
                                    "[detcore, dtid {}] futex value changed while its wait was woken for a signal ({} != {}); EAGAIN",
                                    &dettid,
                                    observed,
                                    call.val()
                                );
                                break Err(Error::Errno(Errno::EAGAIN));
                            }
                            debug!(
                                "[detcore, dtid {}] futex wait woken for a signal that is not pending for it; waiting again until {:?}",
                                &dettid, maybe_timeout_lt
                            );
                        }
                        requested = true;
                        let ans = futex_action(
                            guest,
                            FutexAction::WaitRequest(maybe_timeout_lt, signal_watch),
                            &futexid,
                            init_val,
                            bitset,
                        )
                        .await;
                        if signal_interruption && let Some(SchedValue::Signaled(committed)) = ans {
                            committed_wake = committed;
                            // The scheduler ended the wait for a signal that
                            // interrupted it under the dispositions the kernel held
                            // when the wake was committed. The check at the top of the
                            // loop decides, in this thread's turn, whether it is still
                            // pending here.
                            rewait = true;
                            continue;
                        }
                        break if ans != Some(SchedValue::TimeOut) {
                            let expected = call.val();
                            // AUTONOMOUS-BOT-IMPLEMENTED
                            // TODO-HUMAN-REVIEW(#845): Review exited-thread futex diagnostics.
                            match guest.memory().read_value(ptr) {
                                Ok(observed) => {
                                    trace!(
                                        "[detcore, dtid {}] after (emulated) futex wait, memory value is {}, expected {}",
                                        &dettid, observed, expected,
                                    );
                                    if expected == observed {
                                        debug!(
                                            "WARNING: fishy that the futex value did not change before wakeup. Weird application-level protocol.\n"
                                        );
                                    }
                                }
                                Err(error) => trace!(
                                    "[detcore, dtid {}] skipped post-wait futex memory diagnostic: {}",
                                    &dettid, error,
                                ),
                            }
                            Ok(0)
                        } else {
                            trace!("[detcore, dtid {}] futex wait timed out", &dettid);
                            Err(Error::Errno(Errno::ETIMEDOUT))
                        };
                    };
                    if requested {
                        futex_action(guest, FutexAction::WaitFinished, &futexid, init_val, bitset)
                            .await;
                    }
                    keep_restart_block(guest, &res, RestartCall::Futex(call), maybe_timeout_lt)
                        .await;
                    res
                }
            }
            // `classify_futex_command` already answered every other command.
            _ => Err(Error::Errno(Errno::ENOSYS)),
        }
    }

    /// FUTEX_WAKE and FUTEX_WAKE_BITSET, as Linux's `futex_wake`
    /// (kernel/futex/waitwake.c): `-EINVAL` for a zero bitset, then the word is
    /// keyed for reading (`futex_key`) and never read, and up to `val` modeled
    /// waiters whose bitset matches are woken (a count of 0 or below wakes one).
    async fn handle_futex_wake<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
        command: i32,
    ) -> Result<i64, Error> {
        let bitset = if command == libc::FUTEX_WAKE_BITSET {
            call.val3() as u32
        } else {
            u32::MAX
        };
        if bitset == 0 {
            return Err(Error::Errno(Errno::EINVAL));
        }
        let private = call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0;
        let futexid = futex_key(guest, futex_uaddr(&call), private, KeyAccess::Read).await?;
        let num = match futex_action(
            guest,
            FutexAction::WakeRequest(call.val()),
            &futexid,
            0,
            bitset,
        )
        .await
        .expect("futex wake must return value")
        {
            SchedValue::Value(num) => num,
            other => panic!("impossible answer to a futex wake: {:?}", other),
        };
        let _ = futex_action(guest, FutexAction::WakeFinished(0), &futexid, 0, bitset).await;
        if self.cfg.yields_after_futex_wake(num) {
            guest.thread_state_mut().yield_after_futex_wake = true;
        }
        Ok(num as i64)
    }

    /// FUTEX_REQUEUE and FUTEX_CMP_REQUEUE on plain futexes, as Linux's
    /// `futex_requeue` (kernel/futex/requeue.c) with `requeue_pi == 0`, in its
    /// order:
    /// - `-EINVAL` if `nr_wake` (`val`) or `nr_requeue` (`val2`, passed in the
    ///   timeout slot) is negative as an `int`, before any address is looked at;
    /// - both words keyed (`futex_key`): `-EINVAL` if misaligned, `-EFAULT` if
    ///   outside the user address range or, for a shared key, unmapped;
    /// - for FUTEX_CMP_REQUEUE only, the `uaddr` word read with the guest's own
    ///   permissions (`-EFAULT`), then `-EAGAIN` if it differs from `val3`.
    ///   Plain FUTEX_REQUEUE never reads either word;
    /// - wake the first `nr_wake` waiters, move up to `nr_requeue` more onto
    ///   `uaddr2` (`Scheduler::requeue_futex_waiters`), and return how many were
    ///   woken plus moved.
    // TODO-HUMAN-REVIEW(https://github.com/rrnewton/hermit/pull/4020): Review the
    // emulated futex requeue and its waiter ordering.
    async fn handle_futex_requeue<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
        command: i32,
    ) -> Result<i64, Error> {
        let nr_wake = call.val();
        let nr_requeue = futex_val2(&call) as i32;
        if nr_wake < 0 || nr_requeue < 0 {
            return Err(Error::Errno(Errno::EINVAL));
        }
        let private = call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0;
        let uaddr = futex_uaddr(&call);
        let futexid = futex_key(guest, uaddr, private, KeyAccess::Read).await?;
        let futexid2 = futex_key(guest, futex_uaddr2(&call), private, KeyAccess::Read).await?;
        let mut current = 0;
        if command == libc::FUTEX_CMP_REQUEUE {
            current = read_futex_word(guest, uaddr)?;
            if current != call.val3() {
                return Err(Error::Errno(Errno::EAGAIN));
            }
        }
        let woken_and_moved = match futex_action(
            guest,
            FutexAction::RequeueRequest(futexid2, nr_wake as u32, nr_requeue as u32),
            &futexid,
            current,
            u32::MAX,
        )
        .await
        {
            Some(SchedValue::Requeued { woken, moved }) => {
                if self.cfg.yields_after_futex_wake(woken) {
                    guest.thread_state_mut().yield_after_futex_wake = true;
                }
                woken + moved
            }
            // A logically killed or replaced caller is answered with a plain value, as
            // its wakes are; it never returns to the guest.
            Some(SchedValue::Value(num)) => num,
            other => panic!("impossible answer to a futex requeue: {:?}", other),
        };
        let _ = futex_action(
            guest,
            FutexAction::WakeFinished(0),
            &futexid,
            current,
            u32::MAX,
        )
        .await;
        Ok(woken_and_moved as i64)
    }

    /// FUTEX_WAKE_OP, as Linux's `futex_wake_op` (kernel/futex/waitwake.c), in
    /// its order: key both words (`futex_key`; the `uaddr` word is never read);
    /// apply the operation encoded in `val3` to the word at `uaddr2`
    /// (`futex_atomic_op_inuser`); wake up to `nr_wake` (`val`) waiters on
    /// `uaddr` and, if the encoded comparison of the old value holds, up to
    /// `nr_wake2` (`val2`, in the timeout slot) waiters on `uaddr2`. Returns the
    /// total woken.
    /// - An unknown operation is `-ENOSYS` before the word is touched; an
    ///   unknown comparison is `-ENOSYS` after it was written, and nobody is
    ///   woken. So a private NULL `uaddr2` (a valid key) with an unknown
    ///   operation is `-ENOSYS`.
    /// - The word is read and written as exactly four bytes with the guest's own
    ///   permissions, so an unreadable or unwritable word is `-EFAULT` and a word
    ///   at the end of a page next to a hole is fine.
    ///
    /// Both wakes count a waiter before comparing, so a count of 0 or below wakes
    /// one, and neither looks at the waiters' bitsets, as `futex_wake_op` does not.
    /// The read-modify-write is atomic with respect to the guest because Detcore
    /// runs one guest thread at a time and this one holds the turn, and only
    /// guest threads can reach a private or anonymous shared word. A word in a
    /// file-backed shared mapping can also be written by a process outside
    /// Hermit, which Linux's locked instruction would not lose, so that case is
    /// refused by name before any side effect.
    // TODO-HUMAN-REVIEW(https://github.com/rrnewton/hermit/pull/4020): Review the
    // emulated FUTEX_WAKE_OP and its read-modify-write of the guest word.
    async fn handle_futex_wake_op<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
    ) -> Result<i64, Error> {
        let nr_wake = call.val();
        let nr_wake2 = futex_val2(&call) as i32;
        let private = call.futex_op() & libc::FUTEX_PRIVATE_FLAG != 0;
        let uaddr2 = futex_uaddr2(&call);
        let futexid = futex_key(guest, futex_uaddr(&call), private, KeyAccess::Read).await?;
        let futexid2 = futex_key(guest, uaddr2, private, KeyAccess::Write).await?;
        let op = FutexWakeOp::decode(call.val3() as u32);
        if !op.has_known_operation() {
            return Err(Error::Errno(Errno::ENOSYS));
        }
        if matches!(
            guest.thread_state().futex_id(uaddr2, false),
            FutexID::Shared {
                object: SharedMemoryObjectId::File { .. } | SharedMemoryObjectId::OpenFile { .. },
                ..
            }
        ) {
            return self
                .refuse_futex_command(
                    guest,
                    "FUTEX_WAKE_OP",
                    "its word is in a file-backed shared mapping that a process outside Hermit \
                     could update concurrently, and Detcore cannot apply the operation atomically",
                )
                .await;
        }
        let old = read_futex_word(guest, uaddr2)?;
        let new = op.apply(old).expect("operation was checked to be known");
        let word2 = AddrMut::<libc::c_int>::from_raw(uaddr2).ok_or(Errno::EFAULT)?;
        guest
            .memory()
            .write_value(word2, &new)
            .map_err(|_| Error::Errno(Errno::EFAULT))?;
        let Some(wake2) = op.compare(old) else {
            return Err(Error::Errno(Errno::ENOSYS));
        };
        let mut woken = 0;
        for (futex, count, wake) in [(futexid, nr_wake, true), (futexid2, nr_wake2, wake2)] {
            if !wake {
                continue;
            }
            woken += match futex_action(guest, FutexAction::WakeRequest(count), &futex, 0, u32::MAX)
                .await
            {
                Some(SchedValue::Value(num)) => num,
                other => panic!("impossible answer to a futex wake: {:?}", other),
            };
            let _ = futex_action(guest, FutexAction::WakeFinished(0), &futex, 0, u32::MAX).await;
        }
        if self.cfg.yields_after_futex_wake(woken) {
            guest.thread_state_mut().yield_after_futex_wake = true;
        }
        Ok(woken as i64)
    }

    /// Refuse a futex command Detcore cannot serve deterministically, naming it,
    /// through the unsupported-syscall policy (`refuse_unserviceable_operation`).
    /// A run that is not fail-closed gets `-ENOSYS`, which is what Linux returns
    /// for every priority-inheritance command when built without CONFIG_FUTEX_PI
    /// (kernel/futex/pi.c, kernel/futex/requeue.c).
    async fn refuse_futex_command<G: Guest<Self>>(
        &self,
        guest: &mut G,
        name: &'static str,
        reason: &'static str,
    ) -> Result<i64, Error> {
        tracing::error!(
            "[detcore, dtid {}] futex command {} is not supported: {}",
            guest.thread_state().dettid,
            name,
            reason,
        );
        self.refuse_unserviceable_operation(guest, reverie::syscalls::Sysno::futex, Errno::ENOSYS)
            .await
    }

    /// Futex system call, alternative implemenattion where we treat futexes as InternalIOPolling
    /// operations.
    pub(crate) async fn handle_futex_polling<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Futex,
        init_val: i32,
        deadline: FutexDeadline,
    ) -> Result<i64, Error> {
        fn make_futex_wake_request(dettid: DetTid) -> Resources {
            let mut rsrc = Resources::new(dettid);
            rsrc.fyi("futex_wake");
            rsrc
        }

        fn make_futex_wait_request(dettid: DetTid) -> Resources {
            let mut rsrc = Resources::new(dettid);
            rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
            rsrc.fyi("futex_wait");
            rsrc
        }

        let dettid = guest.thread_state().dettid;
        let futex_op = call.futex_op() & libc::FUTEX_CMD_MASK;
        match futex_op {
            libc::FUTEX_WAKE | libc::FUTEX_WAKE_BITSET => {
                let rsrc = make_futex_wake_request(dettid);
                resource_request(guest, rsrc.clone()).await; // Linearize this operation as a separate COMMIT.
                let res = guest.inject(call).await;
                // FIXME: With the non-blocking version of futex_wait, `res` will always be 0.  It
                // is quite difficult to tell how many polling waiters we unblocked with a given
                // wake, without going back to modeling futexes like `handle_futex_blocking` does.
                Ok(res?)
            }
            libc::FUTEX_WAIT | libc::FUTEX_WAIT_BITSET => {
                if init_val != call.val() {
                    info!(
                        "[detcore, dtid {}] Futex wait running immediately because it will fizzle ({} != {}).",
                        dettid,
                        init_val,
                        call.val()
                    );
                    let res = guest.inject(call).await;
                    Ok(res?)
                } else {
                    let rsrc = make_futex_wait_request(dettid);
                    let deadline = self.futex_deadline(guest, call, deadline).await?;
                    let res =
                        retry_nonblocking_syscall_with_timeout(guest, call, rsrc, deadline).await;
                    keep_restart_block(guest, &res, RestartCall::Futex(call), deadline).await;
                    let res = res?;
                    trace!(
                        "[detcore, dtid {}] after futex wait, memory value is {}",
                        &dettid,
                        guest.memory().read_value(call.uaddr().unwrap()).unwrap()
                    );
                    Ok(res)
                }
            }
            // `classify_futex_command` already answered every other command.
            _ => Err(Error::Errno(Errno::ENOSYS)),
        }
    }

    /// The image `call` runs if it succeeds
    /// ([`crate::exec_record::ExecRecord::resolve`]), looked up as the kernel
    /// will look it up: through the calling thread's root and working
    /// directory, or the directory descriptor, or with `AT_EMPTY_PATH` the
    /// descriptor itself. `None` where Detcore cannot see the thread's
    /// filesystem ([`Self::guest_task_directory`]) or the kernel would
    /// refuse the exec; the new image's `/proc/<pid>/exe` link is then the
    /// kernel's.
    fn exec_image<G: Guest<Self>>(
        &self,
        guest: &G,
        call: &syscalls::Execveat,
    ) -> Option<crate::exec_record::ExecRecord> {
        let path: std::path::PathBuf = call.path()?.read(&guest.memory()).ok()?;
        self.exec_image_at(guest, call.dirfd(), &path)
    }

    /// The image this one's exec ran, from the pathname the exec was given
    /// (`AT_EXECFN`), resolved as [`Self::exec_image`] resolves an `execve`'s
    /// from the working directory, which an exec keeps. For an exec Detcore
    /// did not prepare: a backend's copied vfork child runs its `execve`
    /// without Detcore. `None` where the auxiliary vector has no such entry
    /// or its string cannot be read.
    pub(crate) fn exec_image_from_execfn<G: Guest<Self>>(
        &self,
        guest: &G,
    ) -> Option<crate::exec_record::ExecRecord> {
        use reverie::syscalls::FromToRaw;
        let address = guest.auxv().at_execfn()?.as_raw();
        let path: std::path::PathBuf = Option::<syscalls::PathPtr>::from_raw(address)?
            .read(&guest.memory())
            .ok()?;
        self.exec_image_at(guest, libc::AT_FDCWD, &path)
    }

    /// `Self::exec_image` for a `path` the caller already read from the
    /// guest: public for a backend that runs the native `execve` outside
    /// Detcore's handler, prepares the exec itself (`crate::prepare_exec`)
    /// and reads guest memory its own way.
    pub fn exec_image_at<G: Guest<Self>>(
        &self,
        guest: &G,
        dirfd: i32,
        path: &std::path::Path,
    ) -> Option<crate::exec_record::ExecRecord> {
        let task = self.guest_task_directory(guest)?;
        let root = std::fs::read_link(task.join("root")).ok()?;
        let inside = |link: std::path::PathBuf| {
            Some(std::path::Path::new("/").join(link.strip_prefix(&root).ok()?))
        };
        let cwd = inside(std::fs::read_link(task.join("cwd")).ok()?)?;
        let spelled = if path.is_absolute() || dirfd == libc::AT_FDCWD {
            cwd.join(path)
        } else {
            let descriptor = task.join("fd").join(dirfd.to_string());
            let directory = inside(std::fs::read_link(descriptor).ok()?)?;
            // An empty path names the descriptor itself; without
            // `AT_EMPTY_PATH` the exec fails, and the record with it.
            if path.as_os_str().is_empty() {
                directory
            } else {
                directory.join(path)
            }
        };
        // An exec of the caller's own `exe` link runs the image it executes,
        // which the link names, also where the kernel's link names the
        // guest's launcher (SaBRe's loader).
        match self.resolve_exe_subject(guest, &spelled, true) {
            Some(crate::syscalls::namespace::ExeLinkSubject::Own) => {
                return guest.thread_state().exe.as_deref().cloned();
            }
            // Another process's image, which only the coordinator records.
            Some(crate::syscalls::namespace::ExeLinkSubject::Other(_)) => return None,
            None => {}
        }
        crate::exec_record::ExecRecord::resolve(&root, &cwd, &spelled)
    }

    /// Execveat system call.  Doesn't return if successful.
    pub async fn handle_execveat<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Execveat,
    ) -> Result<i64, Error> {
        self.promote_before_native_lifecycle(guest);
        let (old_metadata, old_memory_metadata, table_is_shared, dettid, detpid, old_mm_id) = {
            let thread_state = guest.thread_state();
            (
                Arc::clone(&thread_state.file_metadata),
                Arc::clone(&thread_state.memory_metadata),
                Arc::strong_count(&thread_state.file_metadata) > 1,
                thread_state.dettid,
                thread_state.detpid.expect("detpid unset"),
                thread_state.mm_id,
            )
        };
        let (new_metadata, closed_open_files, exec_fd_blocking) = {
            let metadata = old_metadata.lock().unwrap();
            let new_metadata = metadata.for_exec(dettid);
            (
                new_metadata.clone(),
                metadata.open_files_closed_on_exec(table_is_shared),
                new_metadata.exec_blocking_overrides(),
            )
        };
        let preserve_exec_fd_status = guest.config().backend.rediscovers_descriptors_after_exec;
        let exe = self.exec_image(guest, &call);

        prepare_exec(
            guest,
            old_mm_id,
            if preserve_exec_fd_status {
                exec_fd_blocking
            } else {
                Default::default()
            },
            // This handler stages the exec's owner-death wakes itself, below.
            Vec::new(),
            exe,
        )
        .await;

        let mut released_ports = Vec::new();
        for open_file_id in closed_open_files {
            if let Some(port) = self.release_port_for_open_file(guest, open_file_id).await {
                released_ports.push((open_file_id, port));
            }
        }

        // A successful execve walks every robust list in the thread group
        // before it replaces the address space (see
        // `ThreadState::stage_robust_list_wakes_for_exec`), then clears
        // `task->robust_list`; the new image re-registers its own. Read the
        // lists while the old image is still mapped.
        // Linux marks the owner words itself at a successful exec, and a failed
        // exec must leave them alone, so Detcore only reads them here. Only
        // where Linux's exec runs under this handler ([`OwnerDeathPath::Kernel`]):
        // on DBT Detcore never sees the execve, and a word the native client
        // does not rewrite to a TID Linux knows is never marked
        // (https://github.com/rrnewton/hermit/issues/4268).
        let exec_robust_wakes = match self.robust_owner_death_path() {
            Some(OwnerDeathPath::Kernel) => Some(
                self.read_thread_group_robust_list_wakes(guest, OwnerDeath::Exec)
                    .await,
            ),
            Some(OwnerDeathPath::Barrier) | None => None,
        };
        let old_robust_list_head;
        // The exec'ing thread leads its process after a successful exec, so
        // the table exec gives it is the process's (`process_files`), also
        // for a thread that did not lead it before.
        let old_process_files = guest.thread_state().process_files;

        {
            let thread_state = guest.thread_state_mut();
            thread_state.file_metadata = Arc::new(Mutex::new(new_metadata));
            thread_state.adopt_process_files();
            thread_state.memory_metadata = Arc::new(Mutex::new(MemoryMetadata::new()));
            thread_state.mm_id = old_mm_id.for_exec(detpid);
            old_robust_list_head = thread_state.take_robust_list_for_exec();
            if let Some(wakes) = exec_robust_wakes {
                thread_state.stage_robust_list_wakes_for_exec(wakes);
            }
        }

        // execve(2) doesn't return upon success.
        let errno = self.record_or_replay(guest, call).await.unwrap_err();
        // Ask before anything else is injected: the next injection replaces
        // the backend's record.
        let refusal = guest.take_unsupported_refusal();

        {
            let thread_state = guest.thread_state_mut();
            thread_state.file_metadata = old_metadata;
            thread_state.process_files = old_process_files;
            thread_state.memory_metadata = old_memory_metadata;
            thread_state.mm_id = old_mm_id;
            thread_state.restore_robust_list_after_failed_exec(old_robust_list_head);
            // The old image runs on, and so do its robust-mutex owners.
            thread_state.take_robust_list_wakes_for_exec();
        }

        cancel_exec(guest).await;
        for (open_file_id, port) in released_ports {
            self.restore_port_for_open_file(guest, open_file_id, port)
                .await;
        }

        // A backend that cannot perform this exec says so with a typed
        // refusal: for example one that cannot promote a worker thread to
        // group leader, which refuses after validating the replacement image,
        // and also when a host syscall its exec preflight makes (such as the
        // KVM backend's faccessat2 permission check) fails with ENOSYS.
        // Diagnose it only after normal failed-exec rollback. Any other
        // failure, including an ENOSYS the backend did not report as a
        // refusal, is the guest's ordinary exec error.
        if let Some(refusal) = refusal {
            tracing::error!(
                "[detcore, dtid {dettid}] {}; the replacement image did not run",
                refusal.diagnostic
            );
            if !self.cfg.panic_on_unsupported_syscalls {
                crate::tool_global::report_unsupported_syscall(guest, call.number()).await;
            }
            return self
                .refuse_unserviceable_operation_because(
                    guest,
                    call.number(),
                    errno,
                    refusal.diagnostic,
                )
                .await;
        }

        Err(errno.into())
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#258): Confirm one-turn exclusion semantics across scheduler modes.
    /// End the current logical timeslice for a sequentialized sched_yield.
    pub async fn handle_sched_yield<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SchedYield,
    ) -> Result<i64, Error> {
        if self.cfg.sequentialize_threads {
            match sched_yield_route(&self.cfg) {
                SchedYieldRoute::ChaosReprioritize => {
                    let change_time = guest.thread_state().thread_logical_time.as_nanos();
                    let request = Self::random_priority_changepoint_request(guest, change_time);
                    resource_request(guest, request).await;
                }
                SchedYieldRoute::ReplayStrongYield => {
                    if self.cfg.max_timeslice.is_some() {
                        guest
                            .thread_state_mut()
                            .reset_timeslice_for_explicit_yield();
                    }
                    let request = Self::sched_yield_request(guest);
                    resource_request(guest, request).await;
                }
                SchedYieldRoute::ChaosYieldEpoch => {
                    let request = Self::sched_yield_request(guest);
                    resource_request(guest, request).await;
                }
                SchedYieldRoute::WeakYield => {
                    let request = Self::yield_request(guest);
                    resource_request(guest, request).await;
                }
                SchedYieldRoute::EndTimeslice => self.end_timeslice_for_sched_yield(guest).await,
            }
            trace!("sched_yield yielded to the scheduler; NOT performing actual syscall");
            Ok(0)
        } else {
            Ok(self.record_or_replay(guest, call).await?)
        }
    }

    /// `child`'s pid as the calling thread names it: its DetTid at depth 0,
    /// its pid in the caller's namespace below that
    /// (https://github.com/rrnewton/hermit/issues/4090). Detcore picks the
    /// child of a wait itself and injects the exact wait, which the kernel
    /// reads in the caller's namespace.
    async fn guest_visible_pid<G: Guest<Self>>(
        &self,
        guest: &mut G,
        sysno: reverie::syscalls::Sysno,
        child: DetPid,
    ) -> Result<i32, Error> {
        if !guest.thread_state().nested_pid_namespace {
            return Ok(child.as_raw());
        }
        let refusal = match crate::tool_global::local_pid(guest, child).await {
            Ok(Some(local)) => return Ok(local),
            Ok(None) => format!("child {child} is not visible in the caller's namespace"),
            Err(reason) => reason,
        };
        Err(self.refuse_nested_pid(guest, sysno, &refusal).await)
    }

    /// The task a pid the kernel answered the calling thread with names: the
    /// pid itself at depth 0, translated from the caller's namespace below
    /// that, before it enters Detcore's bookkeeping
    /// (https://github.com/rrnewton/hermit/issues/4090).
    async fn task_of_guest_pid<G: Guest<Self>>(
        &self,
        guest: &mut G,
        sysno: reverie::syscalls::Sysno,
        pid: i32,
    ) -> Result<DetPid, Error> {
        if !guest.thread_state().nested_pid_namespace || pid <= 0 {
            return Ok(DetPid::from_raw(pid));
        }
        let refusal = match crate::tool_global::guest_pid(guest, pid).await {
            GuestPidAnswer::Task { task, .. } => return Ok(task),
            GuestPidAnswer::NoSuchPid => {
                format!("the kernel answered pid {pid}, which no recorded task has")
            }
            GuestPidAnswer::Untranslatable(reason) => reason,
        };
        Err(self.refuse_nested_pid(guest, sysno, &refusal).await)
    }

    /// Track the pidfd a `CLONE_PIDFD` clone wrote at `address` as
    /// `pidfd_open`'s is tracked: a close-on-exec pidfd naming the new
    /// process, `child`, so that `pidfd_send_signal`, `waitid(P_PIDFD)` and
    /// the descriptor calls know it. Before, Detcore did not know the
    /// descriptor, and `pidfd_send_signal` on it failed with EBADF
    /// (https://github.com/rrnewton/hermit/issues/4430). The kernel installs it
    /// in the caller's descriptor table after copying that table for the
    /// child, so the child does not have it; the child's Detcore table was
    /// copied while the clone ran, so the same holds there.
    ///
    /// Only a child that shares neither the caller's memory nor its
    /// descriptor table, and is no vfork child, gets here
    /// (`clone_pidfd_address`), and only with sequentialized threads: no other
    /// task can then run between the kernel's clone and this, in the caller's
    /// own turn, so neither the clone's arguments nor the word nor the
    /// descriptor can change in between. The clone has
    /// succeeded whatever happens here, so nothing here fails it: a pidfd
    /// that cannot be tracked stays open and unknown to Detcore (EBADF through
    /// it, as before), and the reason is logged.
    async fn register_clone_pidfd<G: Guest<Self>>(
        &self,
        guest: &mut G,
        address: usize,
        child: DetTid,
    ) {
        let dettid = guest.thread_state().dettid;
        let skip = |reason: String| {
            tracing::error!(
                "[detcore, dtid {}] the pidfd of CLONE_PIDFD child {} is not tracked: {}; pidfd \
                 calls through it answer EBADF (https://github.com/rrnewton/hermit/issues/4430)",
                dettid,
                child,
                reason,
            );
        };
        let fd: libc::c_int = match Addr::<libc::c_int>::from_raw(address)
            .map(|address| guest.memory().read_value(address))
        {
            Some(Ok(fd)) => fd,
            // The kernel wrote it, or the clone would have failed with EFAULT;
            // a task sharing the memory may have unmapped it since.
            _ => return skip(format!("its word at {address:#x} cannot be read back")),
        };
        // The kernel has just allocated it, so a number Detcore already
        // tracks means another thread of the caller rewrote the word.
        if guest.thread_state().with_detfd(fd, |_| ()).is_ok() {
            return skip(format!(
                "its word names descriptor {fd}, which Detcore already tracks"
            ));
        }
        if let Err(errno) = self
            .track_held_fd(
                guest,
                fd,
                nix::fcntl::OFlag::O_CLOEXEC,
                crate::fd::FdType::Pidfd,
            )
            .await
        {
            return skip(format!("tracking descriptor {fd} failed with {errno}"));
        }
        if let Err(error) = guest.thread_state().with_detfd(fd, |detfd| {
            detfd.set_pidfd_target(DetPid::from_raw(child.as_raw()))
        }) {
            skip(format!("descriptor {fd} has no Detcore entry: {error}"));
        }
    }

    /// Refuse, by name and through the unsupported-operation policy, a
    /// `clone3` whose `set_tid` asks for an id that a task of this run had
    /// (`thread_id_refused`): a reaped child's pid or an exited thread's tid,
    /// which Linux would give out again, or a live task's id, whose release
    /// can race the call (https://github.com/rrnewton/hermit/issues/4370).
    /// Detcore keeps per-task state by that id (the task's clock, its
    /// recorded schedule and preemptions, its priority), and a new task given
    /// it took over the old one's: the run crashed with "thread time should
    /// never go down", or recorded two tasks as one. `None` lets the call go
    /// ahead, and the kernel's own answer stands, whenever the call cannot
    /// reuse such an id or the kernel would fail it first: no `set_tid`; a
    /// `clone_args` size, exit signal or `set_tid` array the kernel rejects
    /// (E2BIG, EINVAL, EFAULT); a caller without the capability to choose a
    /// pid (EPERM); an id never used; or the pid of a zombie child of the
    /// caller's process (EEXIST).
    ///
    /// The entry that names Detcore's id is `set_tid[0]`, or `set_tid[1]`
    /// with `CLONE_NEWPID`, whose `set_tid[0]` is the pid inside the new
    /// namespace; any later entries name pids in the namespaces holding
    /// Hermit's container. Where those would make Linux fail the call, with
    /// EINVAL for more levels than exist or EPERM for a level the caller has
    /// no capability over, the refusal is given instead: the conservative
    /// side, since the id is not handed out either way. So is a nonzero tail
    /// past the fields this code knows, which a kernel that does not know
    /// them answers with E2BIG. From a nested PID namespace, whose pids
    /// Detcore does not translate for this, any `set_tid` the checks above
    /// let through is refused (issue 4155).
    async fn clone3_set_tid_refusal<G: Guest<Self>>(
        &self,
        guest: &mut G,
        clone: &syscalls::Clone3,
    ) -> Option<Result<i64, Error>> {
        // Refuse only a call the kernel would accept as far as `set_tid` goes:
        // any other shape goes to the kernel, whose own error stands, so a
        // refusal never replaces an E2BIG, EINVAL, EFAULT or EPERM Linux
        // would give for these arguments. `struct clone_args` gained `set_tid`
        // (offset 64) and `set_tid_size` (offset 72) with its second version,
        // 80 bytes.
        const CLONE_ARGS_SIZE_VER1: usize = 80;
        // The kernel's limit on PID namespace nesting (MAX_PID_NS_LEVEL).
        const MAX_SET_TID: u64 = 32;
        let size = clone.size();
        let page = usize::try_from(unsafe { libc::sysconf(libc::_SC_PAGESIZE) }).ok()?;
        if size < CLONE_ARGS_SIZE_VER1 || size > page {
            return None;
        }
        // The kernel copies all `size` bytes first: a fault there is its
        // EFAULT. A nonzero tail beyond the fields this code knows is not
        // read as a reason to let the call through, since a newer kernel may
        // accept it.
        let mut raw = vec![0u8; size];
        let memory = guest.memory();
        memory
            .read_exact(Addr::<u8>::from_raw(clone.args()?.as_raw())?, &mut raw)
            .ok()?;
        let field = |offset: usize| u64::from_ne_bytes(raw[offset..offset + 8].try_into().unwrap());
        let flags = field(0);
        let exit_signal = field(32);
        let set_tid = field(64);
        let set_tid_size = field(72);
        // An exit signal must be a signal number, with nothing in CSIGNAL.
        if exit_signal > 64 || flags & 0xff != 0 {
            return None;
        }
        if set_tid_size == 0 || set_tid_size > MAX_SET_TID {
            return None;
        }
        // The kernel copies the whole array next (EFAULT) and takes no entry
        // below 1 (EINVAL).
        let mut tids = vec![0 as libc::pid_t; set_tid_size as usize];
        memory
            .read_values(Addr::<libc::pid_t>::from_raw(set_tid as usize)?, &mut tids)
            .ok()?;
        // With CLONE_NEWPID the first entry is the pid in the new namespace,
        // which must be 1 since that namespace has no init yet (EINVAL).
        let newpid = flags & libc::CLONE_NEWPID as u64 != 0;
        if tids.iter().any(|tid| *tid < 1) || (newpid && tids[0] != 1) {
            return None;
        }
        // Choosing a pid takes CAP_SYS_ADMIN or CAP_CHECKPOINT_RESTORE over
        // the namespace, and a caller with neither in its effective set gets
        // EPERM. A set that cannot be read counts as privileged, so the
        // refusal below still guards Detcore's state.
        const CAP_SYS_ADMIN: u32 = 21;
        const CAP_CHECKPOINT_RESTORE: u32 = 40;
        let thread = guest.thread_state();
        let status = format!(
            "/proc/{}/task/{}/status",
            thread.detpid.expect("detpid unset"),
            thread.dettid
        );
        let capabilities = std::fs::read_to_string(status).ok().and_then(|status| {
            status
                .lines()
                .find_map(|line| line.strip_prefix("CapEff:"))
                .and_then(|mask| u64::from_str_radix(mask.trim(), 16).ok())
        });
        let pid_capabilities = (1u64 << CAP_SYS_ADMIN) | (1u64 << CAP_CHECKPOINT_RESTORE);
        if capabilities.is_some_and(|mask| mask & pid_capabilities == 0) {
            return None;
        }
        // From a nested PID namespace the array names pids at several levels,
        // which Detcore does not translate for this: refuse any such call.
        if guest.thread_state().nested_pid_namespace {
            return Some(Err(self
                .refuse_nested_pid(
                    guest,
                    reverie::syscalls::Sysno::clone3,
                    "clone3 set_tid names pids in a namespace Detcore does not translate",
                )
                .await));
        }
        // The array runs from the new task's namespace outwards, so the
        // caller's (the container's) pid is entry 0, or entry 1 with
        // CLONE_NEWPID. Entries after it name pids in the namespaces holding
        // Hermit's container, which Detcore does not track.
        if set_tid_size <= u64::from(newpid) {
            return None;
        }
        let tid = tids[usize::from(newpid)];
        if !crate::tool_global::thread_id_refused(guest, DetTid::from_raw(tid)).await {
            return None;
        }
        let message = format!(
            "hermit: clone3 set_tid asks for id {tid}, which an earlier task of this run had; \
             Detcore keeps per-task state by that id and cannot give it to a new task \
             (https://github.com/rrnewton/hermit/issues/4370)"
        );
        tracing::error!(
            "[detcore, dtid {}] {}",
            guest.thread_state().dettid,
            message
        );
        if guest.config().panic_on_unsupported_syscalls {
            use std::io::Write as _;
            let _ = writeln!(crate::util::RetryingStderr, "{message}");
        }
        Some(
            self.refuse_unserviceable_operation(
                guest,
                reverie::syscalls::Sysno::clone3,
                Errno::EEXIST,
            )
            .await,
        )
    }

    /// Refuse by name a call from a nested PID namespace whose pids Detcore
    /// cannot translate (https://github.com/rrnewton/hermit/issues/4155).
    async fn refuse_nested_pid<G: Guest<Self>>(
        &self,
        guest: &mut G,
        sysno: reverie::syscalls::Sysno,
        reason: &str,
    ) -> Error {
        tracing::error!(
            "[detcore, dtid {}] {} from a nested PID namespace is not supported: {} (https://github.com/rrnewton/hermit/issues/4155)",
            guest.thread_state().dettid,
            sysno,
            reason,
        );
        match self
            .refuse_unserviceable_operation(guest, sysno, Errno::ENOSYS)
            .await
        {
            Err(error) => error,
            Ok(_) => Errno::ENOSYS.into(),
        }
    }

    /// wait4 system call
    /// This is handled by the scheduler and not passed to the record/replay layer.
    // TODO-HUMAN-REVIEW(PR-587): Confirm wait4 rusage canonicalization boundaries.
    pub async fn handle_wait4<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Wait4,
    ) -> Result<i64, Error> {
        let serial_emulated_wait =
            guest.config().backend.emulates_child_waits && guest.config().sequentialize_threads;
        let mut emulated_wait_consumed = false;
        let dettid = guest.thread_state().dettid;
        let mut rsrc = Resources::new(dettid);
        rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
        rsrc.fyi("wait4");

        validate_wait4_arguments(call.pid(), call.options())?;

        let parent = guest.thread_state().detpid.expect("detpid unset");
        // A nested caller names its children by its own namespace's pids
        // (https://github.com/rrnewton/hermit/issues/4090). Its process-group
        // ids are not translated yet, nor are emulated waits.
        let nested = guest.thread_state().nested_pid_namespace;
        if nested && (call.pid() == 0 || call.pid() < -1 || serial_emulated_wait) {
            return Err(self
                .refuse_nested_pid(
                    guest,
                    reverie::syscalls::Sysno::wait4,
                    "a process-group wait, or a wait on a backend that emulates child waits",
                )
                .await);
        }
        let named_child = if call.pid() > 0 && nested {
            match crate::tool_global::guest_pid(guest, call.pid()).await {
                GuestPidAnswer::Task { task, .. } => task,
                // No task of the caller's namespace has that pid, so it is
                // not a child: Linux answers ECHILD.
                GuestPidAnswer::NoSuchPid => return Err(Errno::ECHILD.into()),
                GuestPidAnswer::Untranslatable(reason) => {
                    return Err(self
                        .refuse_nested_pid(guest, reverie::syscalls::Sysno::wait4, &reason)
                        .await);
                }
            }
        } else {
            DetPid::from_raw(call.pid())
        };
        // Stop/continue events need a backend waitability callback. Non-SIGCHLD
        // process clones also remain legacy until backends distinguish
        // PTRACE_EVENT_CLONE from CLONE_THREAD. The common matcher already
        // carries those filters so activation does not require another model.
        let selector = if !wait4_uses_terminal_selector(call.options(), serial_emulated_wait) {
            None
        } else {
            match call.pid() {
                pid if pid > 0 => Some(ChildWaitSelector::Exact(named_child)),
                -1 => Some(ChildWaitSelector::Any),
                0 => process_group(guest, parent)
                    .await
                    .map(ChildWaitSelector::ProcessGroup),
                pid if pid < -1 => Some(ChildWaitSelector::ProcessGroup(DetPid::from_raw(-pid))),
                _ => unreachable!(),
            }
        };
        let spec = selector
            .map(|selector| terminal_child_wait_spec(selector, dettid, call.options().bits()));
        let complete_lineage = guest.config().backend.tracks_process_children;
        let managed_spec = if let Some(spec) = spec {
            let (_, has_child) = ready_child_wait(guest, spec).await;
            (has_child || complete_lineage).then_some(spec)
        } else {
            None
        };

        let value = if call.options().contains(WaitPidFlag::WNOHANG) {
            resource_request(guest, rsrc.clone()).await;
            info!(
                "[dtid {}] Executing non-blocking wait4 in one shot.",
                dettid
            );
            if let Some(spec) = spec {
                'select_child: loop {
                    let (ready, has_child) = ready_child_wait(guest, spec).await;
                    let Some(child) = ready else {
                        break if has_child {
                            0
                        } else if complete_lineage {
                            return Err(Errno::ECHILD.into());
                        } else {
                            guest.inject_with_retry(call).await?
                        };
                    };
                    let _ = await_exact_child_physical_exit(guest, child).await;
                    let exact_call = call.with_pid(
                        self.guest_visible_pid(guest, reverie::syscalls::Sysno::wait4, child)
                            .await?,
                    );
                    loop {
                        let result = if serial_emulated_wait {
                            complete_selected_emulated_wait4(guest, exact_call, child).await
                        } else {
                            guest
                                .inject_with_retry(exact_call)
                                .await
                                .map_err(Error::from)
                        };
                        emulated_wait_consumed =
                            serial_emulated_wait && matches!(result, Ok(value) if value > 0);
                        match result {
                            Ok(value) if value != 0 => break 'select_child value,
                            Ok(_) => yield_once().await,
                            Err(Error::Errno(Errno::ECHILD)) => {
                                let _ = consume_child_wait(guest, child).await;
                                if child_wait_can_retry_after_stale(spec) {
                                    continue 'select_child;
                                }
                                return Err(Errno::ECHILD.into());
                            }
                            Err(error) => return Err(error),
                        }
                    }
                }
            } else {
                guest.inject_with_retry(call).await?
            }
        } else if let Some(spec) = managed_spec {
            {
                // The ptrace backend must block ordinary signals until child
                // readiness is resolved. DBT already delays application signal
                // delivery while this callback is active, and replacing its
                // application mask here also hides those signals from
                // rt_sigpending. Read that mask without changing it instead.
                let blocked_mask = blocked_signal_mask();
                let mut stack = guest.stack().await;
                let blocked_mask_addr = stack.push(blocked_mask);
                let old_mask_addr = stack.reserve::<KernelSigset>();
                let action_addr = stack.reserve::<KernelSigaction>();
                let _mask_guard = stack.commit()?;
                let guest_signal_mask =
                    block_signals_for_disposition(guest, blocked_mask_addr, old_mask_addr).await?;
                let inspect_signal_action = guest
                    .config()
                    .backend
                    .requires_thread_directed_process_signals;

                let poll_call = call.with_options(call.options() | WaitPidFlag::WNOHANG);
                let mut pending_signal = None;
                let result: Result<i64, Error> = loop {
                    let status = wait_for_child_lifecycle(guest, spec).await;
                    if pending_signal.is_none() {
                        pending_signal = wait_signal_disposition(
                            guest,
                            status,
                            &guest_signal_mask,
                            action_addr,
                            inspect_signal_action,
                        )
                        .await?;
                    }
                    let (ready, has_child) = ready_child_wait(guest, spec).await;
                    if let Some(child) = ready {
                        let _ = await_exact_child_physical_exit(guest, child).await;
                        let result = if serial_emulated_wait {
                            complete_selected_emulated_wait4(guest, call, child).await
                        } else {
                            match self
                                .guest_visible_pid(guest, reverie::syscalls::Sysno::wait4, child)
                                .await
                            {
                                Ok(pid) => guest
                                    .inject_with_retry(call.with_pid(pid))
                                    .await
                                    .map_err(Error::from),
                                Err(error) => Err(error),
                            }
                        };
                        emulated_wait_consumed =
                            serial_emulated_wait && matches!(result, Ok(value) if value > 0);
                        match result {
                            Ok(value) => break Ok(value),
                            Err(Error::Errno(Errno::ECHILD)) => {
                                let _ = consume_child_wait(guest, child).await;
                                if child_wait_can_retry_after_stale(spec) {
                                    let (next_ready, _) = ready_child_wait(guest, spec).await;
                                    if stale_any_wait_must_interrupt(
                                        pending_signal.is_some(),
                                        next_ready,
                                    ) {
                                        break interrupted_child_wait_result(
                                            guest,
                                            call,
                                            pending_signal.expect("signal checked above"),
                                        )
                                        .await;
                                    }
                                    continue;
                                }
                                break Err(Errno::ECHILD.into());
                            }
                            Err(error) => break Err(error),
                        }
                    }
                    if !has_child {
                        break Err(Errno::ECHILD.into());
                    }
                    if serial_emulated_wait {
                        // A broad backend poll can become consuming while the
                        // owner callback is still publishing logical/CPU state.
                        // Only a selected exact identity may cross that boundary.
                        if let Some(disposition) = pending_signal {
                            break interrupted_child_wait_result(guest, call, disposition).await;
                        }
                        continue;
                    }
                    match guest.inject(poll_call).await {
                        Ok(value) => {
                            if value > 0 {
                                break Ok(value);
                            }
                            if let Some(disposition) = pending_signal {
                                break interrupted_child_wait_result(guest, call, disposition)
                                    .await;
                            }
                        }
                        Err(errno) => break Err(errno.into()),
                    }
                };

                restore_signals_after_disposition(guest, old_mask_addr).await?;
                result?
            }
        } else {
            // wait4 is a scheduler poll, not a record/replay data read (see doc above),
            // so it is not routed through the record/replay subtool.
            retry_nonblocking_syscall(guest, call, rsrc, None).await?
        };
        // The kernel answered in the caller's namespace; the guest gets that
        // pid, and Detcore's bookkeeping the task it names.
        let reaped = if value > 0 {
            self.task_of_guest_pid(guest, reverie::syscalls::Sysno::wait4, value as i32)
                .await?
        } else {
            DetPid::from_raw(value as i32)
        };
        let consumed_termination = if emulated_wait_consumed || value <= 0 {
            false
        } else if let Some(status) = call.wstatus() {
            wait_status_is_termination(guest.memory().read_value(status)?)
        } else if !call
            .options()
            .intersects(WaitPidFlag::WUNTRACED | WaitPidFlag::WCONTINUED)
        {
            // Without them, wait4 reports only a child that terminated. (The
            // exited child's CPU time is recorded where its own Tool runs,
            // which on the in-guest backends is not the parent's, so it
            // cannot tell; https://github.com/rrnewton/hermit/issues/4266.)
            true
        } else {
            // A stop or continue report leaves the child running; once it
            // has exited, the report was its termination.
            guest
                .thread_state()
                .has_exited_child_process_cpu_time(reaped)
                || matches!(
                    crate::tool_global::exact_child_wait_state(guest, reaped).await,
                    ExactChildWaitState::LogicallyExited
                        | ExactChildWaitState::PhysicalExitPending
                        | ExactChildWaitState::PhysicallyExited
                )
        };
        if consumed_termination {
            guest.thread_state_mut().reap_child_process_cpu_time(reaped);
            let _ = consume_child_wait(guest, reaped).await;
        }
        if value > 0
            && let Some(rusage) = call.rusage()
        {
            // Host CPU and scheduling counters are not deterministic.
            let usage: libc::rusage = unsafe { std::mem::zeroed() };
            guest.memory().write_value(rusage, &usage)?;
        }
        Ok(value)
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#274): Review waitid polling and compatibility boundaries.
    // TODO-HUMAN-REVIEW(#2246): Review waitid progress and child-ready signal precedence.
    /// waitid system call
    /// This is handled by the scheduler and not passed to the record/replay layer.
    pub async fn handle_waitid<G: Guest<Self>>(
        &self,
        guest: &mut G,
        mut call: syscalls::Waitid,
    ) -> Result<i64, Error> {
        // A backend that emulates child waits (today KVM) can delegate ordered
        // user copyout to itself on the serial path. Enter before the legacy
        // NULL rejection and whole-siginfo writes; ptrace, DBT and
        // nonsequential execution retain their existing path.
        if guest.config().backend.emulates_child_waits && guest.config().sequentialize_threads {
            // Its selector reads the guest's pid as a DetPid
            // (`emulated_waitid::handle`), so a nested caller is refused by name
            // (https://github.com/rrnewton/hermit/issues/4155).
            if guest.thread_state().nested_pid_namespace {
                return Err(self
                    .refuse_nested_pid(
                        guest,
                        reverie::syscalls::Sysno::waitid,
                        "a wait on a backend that emulates child waits",
                    )
                    .await);
            }
            return emulated_waitid::handle(guest, call).await;
        }
        // A nested caller names its children by its own namespace's pids
        // (https://github.com/rrnewton/hermit/issues/4090); its process-group
        // ids are not translated yet.
        let nested = guest.thread_state().nested_pid_namespace;
        if nested && call.which() == libc::P_PGID as i32 {
            return Err(self
                .refuse_nested_pid(
                    guest,
                    reverie::syscalls::Sysno::waitid,
                    "a process-group wait",
                )
                .await);
        }
        let named_child = if nested && call.which() == libc::P_PID as i32 && call.pid() > 0 {
            match crate::tool_global::guest_pid(guest, call.pid()).await {
                GuestPidAnswer::Task { task, .. } => task,
                GuestPidAnswer::NoSuchPid => return Err(Errno::ECHILD.into()),
                GuestPidAnswer::Untranslatable(reason) => {
                    return Err(self
                        .refuse_nested_pid(guest, reverie::syscalls::Sysno::waitid, &reason)
                        .await);
                }
            }
        } else {
            DetPid::from_raw(call.pid())
        };
        let dettid = guest.thread_state().dettid;
        let mut rsrc = Resources::new(dettid);
        rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
        rsrc.fyi("waitid");

        let event_options = libc::WEXITED | libc::WSTOPPED | libc::WCONTINUED;
        let allowed_options = event_options
            | libc::WNOHANG
            | libc::WNOWAIT
            | libc::__WNOTHREAD
            | libc::__WALL
            | libc::__WCLONE;
        if call.options() & event_options == 0 || call.options() & !allowed_options != 0 {
            return Err(Errno::EINVAL.into());
        }

        // POSIX requires non-null infop. Linux accepts null, but that form can
        // expose host rusage and requires backend-neutral scratch memory for
        // deterministic polling. Reject it uniformly instead of diverging or
        // panicking on DBT's unsupported scratch stack.
        if call.info().is_none() {
            return Err(Errno::EFAULT.into());
        }

        // Keep clone-class waits on the legacy path until ptrace and KVM both
        // register non-SIGCHLD clone children as processes rather than threads.
        let terminal_events_only = call.options() & libc::WEXITED != 0
            && call.options() & (libc::WSTOPPED | libc::WCONTINUED | libc::__WCLONE | libc::__WALL)
                == 0;

        // The parked nonterminal path still uses kernel polling. Preserve its
        // P_PGID(0) entry snapshot until backend lifecycle events replace it.
        if !terminal_events_only && call.which() == libc::P_PGID as i32 && call.pid() == 0 {
            call = call.with_pid(snapshot_process_group(guest.pid())?);
        }

        // A blocking waitid on an O_NONBLOCK pidfd must return EAGAIN rather
        // than being converted to WNOHANG. Acquire the scheduler resource first,
        // then snapshot fdinfo and issue the one-shot wait without another yield.
        let pidfd_nonblocking =
            if call.which() == libc::P_PIDFD as i32 && call.options() & libc::WNOHANG == 0 {
                resource_request(guest, rsrc.clone()).await;
                guest_fd_status_flags(guest.pid(), call.pid())? & libc::O_NONBLOCK != 0
            } else {
                false
            };
        if call.which() == libc::P_PIDFD as i32
            && call.options() & libc::WNOHANG == 0
            && !pidfd_nonblocking
        {
            // Polling a numeric pidfd cannot preserve Linux's held file
            // reference if another thread closes and reuses the descriptor.
            // Reject the blocking form until Detcore can retain that identity.
            return Err(Errno::EOPNOTSUPP.into());
        }
        let info = call
            .info()
            .expect("waitid infop checked above")
            .cast::<GuestSiginfo>();

        // Unlike wait4, waitid returns zero both when it reports a child event and
        // when WNOHANG finds nothing. Polling must inspect si_pid to distinguish
        // those cases.
        // Known limitation: without backend-neutral scratch memory, an invalid
        // non-null infop faults on the first physical poll rather than after a
        // child becomes waitable.
        // The lifecycle scheduler currently models terminal child events for
        // exact and any-child selectors. Group membership and stop/continue
        // state remain on the legacy kernel-polling path.
        let terminal_spec = if terminal_events_only {
            let selector = match call.which() {
                which if which == libc::P_PID as i32 => Some(ChildWaitSelector::Exact(named_child)),
                which if which == libc::P_ALL as i32 => Some(ChildWaitSelector::Any),
                which if which == libc::P_PGID as i32 => {
                    let group = if call.pid() == 0 {
                        process_group(guest, guest.thread_state().detpid.expect("detpid unset"))
                            .await
                    } else {
                        Some(DetPid::from_raw(call.pid()))
                    };
                    group.map(ChildWaitSelector::ProcessGroup)
                }
                _ => None,
            };
            selector.map(|selector| terminal_child_wait_spec(selector, dettid, call.options()))
        } else {
            None
        };
        let complete_lineage = guest.config().backend.tracks_process_children;
        let managed_terminal_spec = if let Some(spec) = terminal_spec {
            let (_, has_child) = ready_child_wait(guest, spec).await;
            (has_child || complete_lineage).then_some(spec)
        } else {
            None
        };

        if call.options() & libc::WNOHANG != 0 || pidfd_nonblocking {
            if !pidfd_nonblocking {
                resource_request(guest, rsrc).await;
            }
            info!(
                "[dtid {}] Executing non-blocking waitid in one shot.",
                dettid
            );
            'select_child: loop {
                let selected = if let Some(spec) = terminal_spec {
                    let (ready, has_child) = ready_child_wait(guest, spec).await;
                    if ready.is_none() && has_child {
                        guest.memory().write_value(info, &GuestSiginfo::zeroed())?;
                        return Ok(0);
                    }
                    if ready.is_none() && complete_lineage {
                        return Err(Errno::ECHILD.into());
                    }
                    ready
                } else {
                    None
                };
                if let Some(child) = selected {
                    let _ = await_exact_child_physical_exit(guest, child).await;
                }
                let effective_call = match selected {
                    Some(child) => call.with_which(libc::P_PID as i32).with_pid(
                        self.guest_visible_pid(guest, reverie::syscalls::Sysno::waitid, child)
                            .await?,
                    ),
                    None => call,
                };
                loop {
                    guest.memory().write_value(info, &GuestSiginfo::zeroed())?;
                    let value = match guest.inject_with_retry(effective_call).await {
                        Ok(value) => value,
                        Err(Errno::ECHILD) if selected.is_some() => {
                            let child = selected.expect("selected child checked above");
                            let _ = consume_child_wait(guest, child).await;
                            if terminal_spec.is_some_and(child_wait_can_retry_after_stale) {
                                continue 'select_child;
                            }
                            return Err(Errno::ECHILD.into());
                        }
                        Err(errno) => return Err(errno.into()),
                    };
                    let info_value: GuestSiginfo = guest.memory().read_value(info)?;
                    let child_pid = info_value.pid();
                    if child_pid == 0 && selected.is_some() {
                        yield_once().await;
                        continue;
                    }
                    let consumed = child_pid != 0
                        && call.options() & libc::WNOWAIT == 0
                        && waitid_code_is_termination(info_value.code());
                    let child = self
                        .task_of_guest_pid(guest, reverie::syscalls::Sysno::waitid, child_pid)
                        .await?;
                    let result = finish_waitid_result(guest, call, value, info_value.0, child)?;
                    if consumed {
                        let _ = consume_child_wait(guest, child).await;
                    }
                    return Ok(result);
                }
            }
        }

        {
            // A signal can arrive after the scheduler wakes this logical wait but
            // before the zero-timeout kernel probe that resolves Linux's
            // child-ready-versus-interrupt precedence. The ptrace backend blocks
            // ordinary signals across that probe, then restores the guest's exact
            // mask before returning. DBT reads the mask without replacing it because
            // DynamoRIO already delays application delivery while this callback runs.
            // The tracer's private preemption signal must remain unblocked.
            let blocked_mask = blocked_signal_mask();
            let mut stack = guest.stack().await;
            let blocked_mask_addr = stack.push(blocked_mask);
            let old_mask_addr = stack.reserve::<KernelSigset>();
            let action_addr = stack.reserve::<KernelSigaction>();
            let _mask_guard = stack.commit()?;
            let guest_signal_mask =
                block_signals_for_disposition(guest, blocked_mask_addr, old_mask_addr).await?;
            let inspect_signal_action = guest
                .config()
                .backend
                .requires_thread_directed_process_signals;

            let poll_call = call.with_options(call.options() | libc::WNOHANG);
            let mut pending_signal = None;
            let result: Result<i64, Error> = loop {
                // Match the polling protocol used by wait4: the first request with
                // poll_attempt zero establishes an ordinary runnable turn, while later
                // nonzero attempts receive the scheduler's poller backoff. Omitting the
                // first request starts directly as a poller and can keep the run queue
                // nonempty forever, preventing logical time from reaching a pending
                // signal's deadline.
                //
                // Do not return on Signaled yet. Linux lets an already-waitable child
                // status win over an interrupt, so the zero-timeout kernel probe below
                // remains authoritative when readiness and a signal coincide.
                let managed_spec = managed_terminal_spec;
                // Both ways of parking inside waitid -- the scheduler-managed child
                // wait and the legacy kernel-polling loop -- can now be resumed with
                // the signals that woke the thread, so both consult the guest mask.
                let status = if let Some(spec) = managed_spec {
                    wait_for_child_lifecycle(guest, spec).await
                } else {
                    resource_request(guest, rsrc.clone()).await
                };
                if pending_signal.is_none() {
                    pending_signal = wait_signal_disposition(
                        guest,
                        status,
                        &guest_signal_mask,
                        action_addr,
                        inspect_signal_action,
                    )
                    .await?;
                }
                let (ready, has_child) = if let Some(spec) = managed_spec {
                    ready_child_wait(guest, spec).await
                } else {
                    (None, true)
                };
                if let Some(child) = ready {
                    let _ = await_exact_child_physical_exit(guest, child).await;
                    if let Err(error) = guest.memory().write_value(info, &GuestSiginfo::zeroed()) {
                        break Err(error.into());
                    }
                    let pid = match self
                        .guest_visible_pid(guest, reverie::syscalls::Sysno::waitid, child)
                        .await
                    {
                        Ok(pid) => pid,
                        Err(error) => break Err(error),
                    };
                    let exact_call = call.with_which(libc::P_PID as i32).with_pid(pid);
                    match guest.inject_with_retry(exact_call).await {
                        Ok(value) => {
                            let info_value = match guest.memory().read_value(info) {
                                Ok(value) => value,
                                Err(error) => break Err(error.into()),
                            };
                            let reported = match self
                                .task_of_guest_pid(
                                    guest,
                                    reverie::syscalls::Sysno::waitid,
                                    info_value.pid(),
                                )
                                .await
                            {
                                Ok(task) => task,
                                Err(error) => break Err(error),
                            };
                            break finish_waitid_result(guest, call, value, info_value.0, reported);
                        }
                        Err(Errno::ECHILD) => {
                            let _ = consume_child_wait(guest, child).await;
                            if managed_spec.is_some_and(child_wait_can_retry_after_stale) {
                                let (next_ready, _) =
                                    ready_child_wait(guest, managed_spec.expect("managed spec"))
                                        .await;
                                if stale_any_wait_must_interrupt(
                                    pending_signal.is_some(),
                                    next_ready,
                                ) {
                                    break interrupted_child_wait_result(
                                        guest,
                                        call,
                                        pending_signal.expect("signal checked above"),
                                    )
                                    .await;
                                }
                                continue;
                            }
                            break Err(Errno::ECHILD.into());
                        }
                        Err(errno) => break Err(errno.into()),
                    }
                }
                if managed_spec.is_some() && !has_child {
                    break Err(Errno::ECHILD.into());
                }

                if let Err(error) = guest.memory().write_value(info, &GuestSiginfo::zeroed()) {
                    break Err(error.into());
                }
                let result = guest.inject(poll_call).await;
                match result {
                    Ok(value) => {
                        let info_value = match guest.memory().read_value(info) {
                            Ok(value) => value,
                            Err(error) => break Err(error.into()),
                        };
                        // waitid writes the SIGCHLD variant of siginfo_t. A zeroed
                        // structure is used only for the no-event WNOHANG result.
                        let child_pid = info_value.pid();
                        match exact_wait_poll_decision(
                            child_pid != 0,
                            pending_signal.is_some(),
                            None,
                        ) {
                            ExactWaitPollDecision::ChildReady => {
                                let reported = match self
                                    .task_of_guest_pid(
                                        guest,
                                        reverie::syscalls::Sysno::waitid,
                                        child_pid,
                                    )
                                    .await
                                {
                                    Ok(task) => task,
                                    Err(error) => break Err(error),
                                };
                                break finish_waitid_result(
                                    guest,
                                    call,
                                    value,
                                    info_value.0,
                                    reported,
                                );
                            }
                            ExactWaitPollDecision::Interrupted => {
                                break interrupted_child_wait_result(
                                    guest,
                                    call,
                                    pending_signal.expect("signal checked above"),
                                )
                                .await;
                            }
                            ExactWaitPollDecision::Retry => {}
                            ExactWaitPollDecision::AwaitPhysicalExit
                            | ExactWaitPollDecision::ReapAfterLogicalExit => unreachable!(),
                        }
                        if managed_spec.is_some() {
                            if !has_child {
                                break Ok(value);
                            }
                            continue;
                        }
                        rsrc.poll_attempt += 1;
                        trace!(
                            "Retry #{} for waitid because no child state is ready",
                            rsrc.poll_attempt
                        );
                        record_retry_event(guest, poll_call).await;
                    }
                    Err(Errno::ERESTARTSYS) if pending_signal.is_some() => {
                        break Err(Errno::EINTR.into());
                    }
                    Err(errno) => break Err(errno.into()),
                }
            };

            restore_signals_after_disposition(guest, old_mask_addr).await?;
            if result.is_ok() && call.options() & libc::WNOWAIT == 0 {
                let info_value: GuestSiginfo = guest.memory().read_value(info)?;
                let child_pid = info_value.pid();
                if child_pid != 0 && waitid_code_is_termination(info_value.code()) {
                    let child = self
                        .task_of_guest_pid(guest, reverie::syscalls::Sysno::waitid, child_pid)
                        .await?;
                    let _ = consume_child_wait(guest, child).await;
                }
            }
            result
        }
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#546)
    /// Accept valid affinity masks without changing the host scheduler.
    pub async fn handle_sched_setaffinity<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SchedSetaffinity,
    ) -> Result<i64, Error> {
        let size_bytes = call.len() as usize;
        if size_bytes == 0 {
            return Err(Errno::EINVAL.into());
        }

        let mask = call.mask().ok_or(Errno::EFAULT)?;
        let mask: Addr<u8> = mask.cast();
        let mut requested = [0u8; VIRTUAL_CPUSET_BYTES];
        let bytes_to_read = size_bytes.min(VIRTUAL_CPUSET_BYTES);
        guest
            .memory()
            .read_exact(mask, &mut requested[..bytes_to_read])?;
        info!(
            "Suppressing sched_setaffinity mask {:?}; affinity remains virtual CPU 0",
            requested
        );
        Ok(0)
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#546)
    /// Report that we are on cpu 0, irrespective of what physical CPU we are on.
    ///
    /// Linux's `sched_getaffinity` accepts any length whose bits cover
    /// `nr_cpu_ids` (one, for Hermit's single virtual CPU) and that is a
    /// multiple of `sizeof(long)`, copies `min(len, cpumask_size())` bytes and
    /// returns that count. Detcore's mask is `VIRTUAL_CPUSET_BYTES` long
    /// (`kernel_max` 127), so an 8-byte set, which `CPU_ALLOC` gives for the
    /// one CPU that `sysconf(_SC_NPROCESSORS_CONF)` reports, receives its
    /// first 8 bytes (<https://github.com/rrnewton/hermit/issues/4160>).
    pub async fn handle_sched_getaffinity<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SchedGetaffinity,
    ) -> Result<i64, Error> {
        let size_bytes: usize = call.len() as usize;
        if size_bytes == 0 || !size_bytes.is_multiple_of(std::mem::size_of::<libc::c_ulong>()) {
            return Err(Errno::EINVAL.into());
        }
        let copied = size_bytes.min(VIRTUAL_CPUSET_BYTES);

        // N.B. we can't use an opaque, type-safe representation such as
        // nix::sched::CpuSet currently.  The problem is that the
        // SchedGetAffinity syscall treats this field as a u64.
        let mut cpu_set = [0u8; VIRTUAL_CPUSET_BYTES];
        cpu_set[0] = 1;

        info!(
            "Suppressing sched_getaffinity and returning {}-byte virtualized result, {:?}",
            copied,
            &cpu_set[..copied]
        );
        if let Some(mask) = call.mask() {
            let mask: AddrMut<u8> = mask.cast();
            guest.memory().write_exact(mask, &cpu_set[..copied])?;
            // From the man page:
            // > On success, the raw sched_getaffinity() system call returns the size (in bytes) of
            // > the cpumask_t data type that is used internally by the kernel to represent the CPU
            // > set bit mask.
            // Linux returns the bytes it copied, the smaller of that size and the caller's.
            Ok(copied as i64)
        } else {
            Err(Error::Errno(Errno::EFAULT))
        }
    }

    /// sched_getparam under Hermit. Detcore replaces the Linux scheduler with its
    /// own deterministic one, so a thread's Linux scheduling parameters are
    /// inert. Report a fixed SCHED_OTHER priority of 0. The value is emulated
    /// (never injected), so it is identical across --verify runs and
    /// record/replay.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#720)
    pub async fn handle_sched_getparam<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SchedGetparam,
    ) -> Result<i64, Error> {
        if let Some(param) = call.param() {
            let p = libc::sched_param { sched_priority: 0 };
            guest.memory().write_value(param, &p)?;
        }
        Ok(0)
    }

    /// sched_rr_get_interval under Hermit. The round-robin quantum is a property
    /// of the Linux scheduler, which Detcore does not use, so report a fixed zero
    /// interval. Being a constant, it is deterministic across --verify and
    /// record/replay.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#720)
    pub async fn handle_sched_rr_get_interval<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SchedRrGetInterval,
    ) -> Result<i64, Error> {
        if let Some(tp) = call.tp() {
            let t = Timespec {
                tv_sec: 0,
                tv_nsec: 0,
            };
            guest.memory().write_value(tp, &t)?;
        }
        Ok(0)
    }

    /// sched_getattr under Hermit. Detcore replaces the Linux scheduler with its
    /// own deterministic one, so a thread's Linux scheduling attributes are inert.
    /// Report a fixed SCHED_OTHER policy with zeroed nice/priority/flags. The
    /// value is emulated (never injected), so it is identical across --verify runs
    /// and record/replay. Re-enables `chrt` under --strict.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#791)
    pub async fn handle_sched_getattr<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SchedGetattr,
    ) -> Result<i64, Error> {
        // `flags` is reserved and must be zero; the kernel rejects anything else.
        if call.flags() != 0 {
            return Err(Errno::EINVAL.into());
        }
        // The caller must provide room for at least the base sched_attr (VER0).
        let attr_size = std::mem::size_of::<libc::sched_attr>();
        if (call.size() as usize) < attr_size {
            return Err(Errno::EINVAL.into());
        }
        let attr = call.attr().ok_or(Errno::EINVAL)?;
        // SAFETY: sched_attr is a plain-old-data struct; an all-zero bit pattern is
        // a valid SCHED_OTHER descriptor (nice/priority/flags/runtime/... all 0).
        let mut sa: libc::sched_attr = unsafe { std::mem::zeroed() };
        sa.size = attr_size as u32;
        sa.sched_policy = libc::SCHED_OTHER as u32;
        // SAFETY: reinterpret the POD struct as its raw bytes to copy it into the
        // guest's buffer.
        let bytes = unsafe {
            std::slice::from_raw_parts(&sa as *const libc::sched_attr as *const u8, attr_size)
        };
        let dst: AddrMut<u8> = attr.cast();
        guest.memory().write_exact(dst, bytes)?;
        info!(
            "Emulating sched_getattr(pid={}): fixed SCHED_OTHER, nice 0, priority 0",
            call.pid()
        );
        Ok(0)
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-841): Review virtual sched_setattr no-op policy.
    /// Linux scheduler attributes cannot affect Detcore's replacement
    /// scheduler, so a *well-formed* request is accepted as a deterministic
    /// no-op, matching the existing sched_setscheduler and sched_setparam
    /// policy.
    ///
    /// Suppressing the effect is not the same as accepting arguments Linux
    /// refuses, nor as refusing arguments Linux accepts. Both directions are
    /// guest-visible: a probe that expects EINVAL and sees success takes the
    /// wrong branch, and so does one that expects success and sees E2BIG.
    ///
    /// The order below is the kernel's, and it is guest-visible when two
    /// arguments are wrong at once, because the kernel returns the *first*
    /// applicable error. `sched_setattr()` screens `uattr`, `pid` and `flags`
    /// together; `sched_copy_attr()` then handles the size, the trailing bytes
    /// and the util-clamp size rule; the signed-policy test follows; the target
    /// pid is resolved *there*, in the middle; and only then does
    /// `__sched_setscheduler()` judge the policy, the flags, the priority and
    /// the deadline parameters. Putting any of that last group before the pid
    /// lookup makes a request against a nonexistent pid report EINVAL where
    /// Linux reports ESRCH.
    ///
    /// # Determinism
    ///
    /// Every check is a pure function of the guest's own arguments. The one
    /// piece of state consulted is the pid lookup, which asks the scheduler's
    /// own task table via `thread_is_live` -- Detcore state, replayed
    /// identically -- rather than the host's process table, which would leak
    /// unrelated host processes into a guest-visible answer.
    ///
    /// Specifically NOT `tool_global::resolve_kill_targets`, which
    /// models `kill(2)` and so recognises only thread-group leaders; asking it
    /// this question reports ESRCH for a live non-leader thread.
    ///
    /// # Deliberately not emulated
    ///
    /// Three behaviours are excluded, under one rule: **an answer that depends
    /// on the host's kernel configuration or the caller's privileges is not
    /// reproduced, because a deterministic sandbox must not vary with the
    /// machine underneath it.** Each was measured natively and would differ on
    /// a differently-built or differently-privileged host:
    ///
    /// * EPERM for a real-time priority, a SCHED_DEADLINE admission, or a
    ///   negative nice, which depends on `CAP_SYS_NICE` and `RLIMIT_RTPRIO`.
    /// * EOPNOTSUPP for util-clamp on a VER1 buffer, which depends on
    ///   `CONFIG_UCLAMP_TASK`.
    /// * The `sysctl_sched_dl_period_{min,max}` bound on SCHED_DEADLINE
    ///   periods, which is runtime-tunable.
    ///
    /// In all three Hermit accepts the request as the same no-op as any other
    /// well-formed one. The same rule is why SCHED_EXT (policy 7) is accepted
    /// unconditionally even though `valid_policy()` admits it only with
    /// `CONFIG_SCHED_CLASS_EXT`: picking one answer keeps the sandbox stable
    /// across hosts, and accepting is the choice consistent with suppressing
    /// the effect rather than refusing the request.
    ///
    /// The bracketed regression test in `hermit-cli/tests/sched_setattr_abi.rs`
    /// compares Hermit against the running kernel case by case, and therefore
    /// deliberately omits exactly these cases -- including them would make its
    /// verdict depend on the host it runs on.
    pub async fn handle_sched_setattr<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::SchedSetattr,
    ) -> Result<i64, Error> {
        // `if (!uattr || pid < 0 || flags)` -- one clause in the kernel, so all
        // three are EINVAL and none of them is ordered against the others. A
        // null pointer is EINVAL rather than EFAULT because it is refused
        // before any access is attempted.
        if call.flags() != 0 || call.pid() < 0 {
            return Err(Errno::EINVAL.into());
        }
        let attr = call.attr().ok_or(Errno::EINVAL)?;

        // `get_user(size, &uattr->size)` -- the declared size is read before
        // anything else, and a fault here is EFAULT with no store back.
        // `AddrMut` is not `Copy` and `cast` consumes it, so each access below
        // re-derives the address from the syscall argument.
        // A fault reading guest memory is EFAULT for this syscall. The
        // backend's memory layer reports a failed peek as EIO, which is not an
        // errno `sched_setattr` can return, so it is mapped here; measured
        // native, an unmapped `uattr` gives EFAULT.
        let declared: u32 = guest
            .memory()
            .read_value(attr.cast())
            .map_err(|_| Errno::EFAULT)?;

        // Both E2BIG exits below run the kernel's `err_size` path, which stores
        // the kernel's own struct size into `uattr->size` before returning. A
        // guest that reads the field back learns the size it should have sent,
        // which is the entire point of the store; the kernel ignores whether it
        // succeeds, and so do we.
        fn refuse_too_big<S: MemoryAccess>(mut memory: S, attr: AddrMut<libc::c_void>) -> Error {
            let _ = memory.write_value(attr.cast::<u32>(), &SCHED_ATTR_KERNEL_SIZE);
            Errno::E2BIG.into()
        }

        let size = match sched_attr_effective_size(declared) {
            Ok(size) => size,
            Err(()) => {
                let back = call.attr().ok_or(Errno::EINVAL)?;
                return Err(refuse_too_big(guest.memory(), back));
            }
        };

        // `copy_struct_from_user` with a user size past the kernel's struct:
        // every trailing byte must be zero, or the guest is sending a field
        // this kernel does not know and must not silently drop. A fault while
        // scanning is EFAULT and skips the store back -- only the not-all-zero
        // verdict is E2BIG.
        if size > SCHED_ATTR_KERNEL_SIZE {
            let base: AddrMut<u8> = call.attr().ok_or(Errno::EINVAL)?.cast();
            match scan_tail_is_zeroed(
                &guest.memory(),
                base,
                SCHED_ATTR_KERNEL_SIZE as usize,
                (size - SCHED_ATTR_KERNEL_SIZE) as usize,
            ) {
                TailVerdict::AllZero => {}
                TailVerdict::NotZeroed => {
                    let back = call.attr().ok_or(Errno::EINVAL)?;
                    return Err(refuse_too_big(guest.memory(), back));
                }
                TailVerdict::Faulted => return Err(Errno::EFAULT.into()),
            }
        }

        // Copy the interoperable prefix, zero-filling anything the guest's
        // buffer is too short to carry, exactly as `copy_struct_from_user`
        // does for a short read.
        let copied = std::cmp::min(size, SCHED_ATTR_KERNEL_SIZE) as usize;
        let mut raw = [0u8; SCHED_ATTR_KERNEL_SIZE as usize];
        let base: AddrMut<u8> = call.attr().ok_or(Errno::EINVAL)?.cast();
        guest
            .memory()
            .read_exact(base, &mut raw[..copied])
            .map_err(|_| Errno::EFAULT)?;
        let field32 = |offset: usize| -> u32 {
            u32::from_ne_bytes(raw[offset..offset + 4].try_into().expect("4 bytes"))
        };
        let field64 = |offset: usize| -> u64 {
            u64::from_ne_bytes(raw[offset..offset + 8].try_into().expect("8 bytes"))
        };
        let fields = SchedAttrFields {
            policy: field32(SCHED_ATTR_OFF_POLICY),
            sched_flags: field64(SCHED_ATTR_OFF_FLAGS),
            priority: field32(SCHED_ATTR_OFF_PRIORITY),
            runtime: field64(SCHED_ATTR_OFF_RUNTIME),
            deadline: field64(SCHED_ATTR_OFF_DEADLINE),
            period: field64(SCHED_ATTR_OFF_PERIOD),
        };

        // Everything the kernel decides before it resolves the pid.
        validate_sched_attr_before_lookup(size, &fields)?;

        // `find_process_by_pid()` sits here, between the two groups of checks,
        // and the position is guest-visible: a request that is malformed only
        // in a way judged below reports ESRCH rather than EINVAL when the pid
        // does not exist. pid 0 means the calling thread, which always exists;
        // otherwise ask the scheduler's own task table, so the answer comes
        // from Detcore's state rather than from the host's process table.
        // `find_task_by_vpid` resolves ANY LIVE TASK, not just a thread-group
        // leader. The kill-target resolver is the wrong question here: it
        // models `kill(2)`, whose first act is to refuse anything that is not a
        // leader, so a perfectly live non-leader thread's tid reported ESRCH.
        // A nested caller names the thread by its own namespace's tid
        // (https://github.com/rrnewton/hermit/issues/4155).
        let named = if call.pid() > 0 && guest.thread_state().nested_pid_namespace {
            match crate::tool_global::guest_pid(guest, call.pid()).await {
                GuestPidAnswer::Task { task, .. } => Some(task),
                GuestPidAnswer::NoSuchPid => None,
                GuestPidAnswer::Untranslatable(reason) => {
                    return Err(self
                        .refuse_nested_pid(guest, reverie::syscalls::Sysno::sched_setattr, &reason)
                        .await);
                }
            }
        } else {
            Some(DetTid::from_raw(call.pid()))
        };
        if call.pid() != 0 {
            match named {
                Some(task) if thread_is_live(guest, task).await => {}
                _ => return Err(Errno::ESRCH.into()),
            }
        }

        // And everything `__sched_setscheduler` decides after it.
        validate_sched_attr_after_lookup(&fields)?;

        info!(
            "Suppressing sched_setattr(pid={}, flags={}); Linux scheduler attributes are virtual",
            call.pid(),
            call.flags()
        );
        Ok(0)
    }

    /// ioprio_set under Hermit. Detcore serializes guest threads onto one virtual
    /// CPU, so the block-layer I/O scheduling class and priority cannot change
    /// guest-visible computation. Accept and suppress the request as a
    /// deterministic no-op success, mirroring how sched_setaffinity is handled.
    /// Re-enables `ionice` under --strict.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#791)
    pub async fn handle_ioprio_set<G: Guest<Self>>(
        &self,
        _guest: &mut G,
        call: syscalls::IoprioSet,
    ) -> Result<i64, Error> {
        info!(
            "Suppressing ioprio_set(which={}, who={}, priority={}); I/O priority is virtual",
            call.which(),
            call.who(),
            call.priority()
        );
        Ok(0)
    }

    /// ioprio_get under Hermit. I/O priority is inert under Detcore's serialized
    /// scheduler, so process queries observe the fixed raw IOPRIO_CLASS_NONE
    /// value while group/user queries observe the effective SCHED_OTHER default
    /// of IOPRIO_CLASS_BE/4, without consulting host block-scheduler state.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-881)
    pub async fn handle_ioprio_get<G: Guest<Self>>(
        &self,
        _guest: &mut G,
        call: syscalls::IoprioGet,
    ) -> Result<i64, Error> {
        let priority = virtual_ioprio(call.which())?;

        info!(
            "Emulating ioprio_get(which={}, who={}): fixed priority {}",
            call.which(),
            call.who(),
            priority
        );
        Ok(priority)
    }
}

/// The one real implementation of [`robust_list::RobustDeathEffects`]: guest
/// memory plus a wake against Detcore's modeled futex waiter pool.
///
/// It exists so the walk itself never mentions `Guest`, and can therefore be
/// unit-tested against fake memory.
struct GuestRobustEffects<'a, G, T> {
    guest: &'a mut G,
    dettid: DetTid,
    staged_wakes: Option<&'a mut Vec<RobustListWake>>,
    /// The words this exit's walks found owned by their dying owner, keyed as
    /// futexes (so two mappings of one shared page are one word), with the
    /// value Linux's walk stores there. Detcore does not store it, so a later
    /// read of the same word, through another entry or another owner's list
    /// in the same group exit, sees this value, as it would after Linux's
    /// store.
    marked: &'a mut HashMap<FutexID, u32>,
    tool: PhantomData<T>,
}

impl<G, T> robust_list::RobustDeathEffects for GuestRobustEffects<'_, G, T>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    fn read_u64(&mut self, address: usize) -> Option<u64> {
        let at = Addr::<u64>::from_raw(address)?;
        self.guest.memory().read_value::<_, u64>(at).ok()
    }

    fn read_u32(&mut self, address: usize) -> Option<u32> {
        let at = Addr::<u32>::from_raw(address)?;
        let value = self.guest.memory().read_value::<_, u32>(at).ok()?;
        let futexid = self.guest.thread_state().futex_id(address, false);
        Some(self.marked.get(&futexid).copied().unwrap_or(value))
    }

    fn compare_and_swap(
        &mut self,
        address: usize,
        expected: u32,
        desired: u32,
    ) -> robust_list::FutexCasOutcome {
        use robust_list::FutexCasOutcome;

        let Some(read_at) = Addr::<u32>::from_raw(address) else {
            return FutexCasOutcome::Faulted;
        };
        // Detcore never stores the owner word itself. It re-reads the word to
        // decide whether the kernel's retry loop would take another pass, then
        // leaves the replacement to the native task exit, which every
        // [`OwnerDeathPath`] completes before another modeled thread runs.
        let observed = match self.guest.memory().read_value::<_, u32>(read_at) {
            Ok(value) => value,
            Err(_) => return FutexCasOutcome::Faulted,
        };
        let futexid = self.guest.thread_state().futex_id(address, false);
        let observed = self.marked.get(&futexid).copied().unwrap_or(observed);
        if observed != expected {
            return FutexCasOutcome::Changed(observed);
        }
        self.marked.insert(futexid, desired);
        // No other scheduler turn runs until Linux has repeated the owner
        // check and changed the word atomically. Do not perform a separate
        // write here: a process outside Hermit's scheduler can share the
        // mapping and acquire the mutex between this read and that write.
        // Deliberately DEBUG, not INFO: this line carries a raw guest address,
        // and INFO is the surface `--verify-strict` compares.
        debug!(
            "[detcore, dtid {}] robust-list owner death: leaving futex word {:#x} for backend exit cleanup",
            self.dettid, address,
        );
        FutexCasOutcome::Matched
    }

    async fn wake_one(&mut self, address: usize, observed: u32) {
        // glibc always issues robust-mutex futex operations with the shared
        // flag, and so does the kernel's owner-death wake; resolve the same key.
        let futexid = self.guest.thread_state().futex_id(address, false);
        if let Some(wakes) = self.staged_wakes.as_mut() {
            wakes.push(RobustListWake { futex: futexid });
            debug!(
                "[detcore, dtid {}] staged robust-list owner-death wake until physical exit",
                self.dettid,
            );
            return;
        }
        let woken = match futex_action(
            self.guest,
            FutexAction::WakeRequest(1),
            &futexid,
            observed as i32,
            u32::MAX,
        )
        .await
        {
            Some(SchedValue::Value(count)) => count,
            // A wake never carries a timeout, and a cancelled RPC wakes nobody.
            Some(SchedValue::TimeOut)
            | Some(SchedValue::Signaled(_))
            | Some(SchedValue::Requeued { .. })
            | None => 0,
        };
        // Guest-level identities only: dettid, the modeled futex key, and a
        // count. No host pointers and no iteration order leak into this line,
        // which is compared exactly under `--verify-strict`.
        info!(
            "[detcore, dtid {}] robust-list owner death woke {} waiter(s) on futex {:?}",
            self.dettid, woken, futexid,
        );
        let _ = futex_action(
            self.guest,
            FutexAction::WakeFinished(0),
            &futexid,
            observed as i32,
            u32::MAX,
        )
        .await;
    }
}

/// How a thread group's robust-mutex owners die, for
/// `Detcore::read_thread_group_robust_list_wakes`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OwnerDeath {
    /// exit_group or a fatal signal: every thread exits with its own TID.
    Exit,
    /// A successful execve: the other threads exit with their own TIDs, and the
    /// caller's list is walked under the TID it holds after `de_thread`.
    Exec,
}

/// `Detcore::robust_owner_death_path` for `cfg`.
///
/// The Barrier path needs three properties of a backend: an exit callback that
/// runs before the native exit, a native exit that runs Linux's own robust
/// walk, and a registered host pidfd for every thread, which the scheduler's
/// exited-thread hold polls. `requires_thread_directed_process_signals` is
/// the flag that makes Detcore refuse a thread without host IDs, so it implies
/// the third; today only DBT sets it, and DBT has the other two.
/// `owner_death_path_is_tied_to_the_backends_that_have_its_properties` fails
/// if another backend starts to set it (Claude review of
/// https://github.com/rrnewton/hermit/pull/4277 at 65138a17, N4).
fn owner_death_path(cfg: &Config) -> Option<OwnerDeathPath> {
    if !cfg.sequentialize_threads || cfg.debug_futex_mode != BlockingMode::Precise {
        return None;
    }
    if cfg.backend.runs_exit_robust_list {
        Some(OwnerDeathPath::Kernel)
    } else if cfg.backend.requires_thread_directed_process_signals {
        Some(OwnerDeathPath::Barrier)
    } else {
        None
    }
}

/// How Detcore orders its modeled owner-death wakes after Linux's own walk.
/// In both, Linux changes each owner word to `FUTEX_OWNER_DIED` in the native
/// exit, and Detcore only reads the words.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum OwnerDeathPath {
    /// The backend's exit callback comes after the native exit
    /// (`BackendCapabilities::runs_exit_robust_list`, ptrace). Wakes for a
    /// whole group's death wait for the owners' exit callbacks.
    Kernel,
    /// The backend reports a thread's exit before the native exit runs, and
    /// registers a pidfd for every host thread
    /// (`BackendCapabilities::requires_thread_directed_process_signals`, DBT).
    /// The scheduler then commits no turn until the kernel has ended a thread
    /// that exited by its own grant, or any thread of a group whose exit_group
    /// was granted (`Scheduler::exited_thread_barriers`). An exit_group's
    /// killed threads also hold their native exits until the group's walk is
    /// done (`ThreadState::wait_for_exit_group_robust_walk`), so that walk reads
    /// every word as the guest left it.
    Barrier,
}

/// How precise and polling futex modes answer a futex command, decided as
/// Linux's `do_futex` (kernel/futex/syscalls.c) decides it, before the futex
/// address is looked at.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FutexCommand {
    /// Emulated: FUTEX_WAIT, FUTEX_WAIT_BITSET, FUTEX_WAKE, FUTEX_WAKE_BITSET,
    /// FUTEX_REQUEUE, FUTEX_CMP_REQUEUE and FUTEX_WAKE_OP.
    Served,
    /// `-ENOSYS`, exactly as Linux: FUTEX_CLOCK_REALTIME with any command but
    /// FUTEX_WAIT_BITSET, FUTEX_WAIT_REQUEUE_PI or FUTEX_LOCK_PI2; the removed
    /// FUTEX_FD; and any unknown command.
    NoSys,
    /// A priority-inheritance command, which Detcore does not emulate; refused
    /// by name (`Detcore::refuse_futex_command`).
    PriorityInheritance(&'static str),
}

/// [`FutexCommand`] for a raw `futex_op`.
// AUTONOMOUS-BOT-IMPLEMENTED
pub(crate) fn classify_futex_command(futex_op: i32) -> FutexCommand {
    let cmd = futex_op & libc::FUTEX_CMD_MASK;
    if futex_op & libc::FUTEX_CLOCK_REALTIME != 0
        && !matches!(
            cmd,
            libc::FUTEX_WAIT_BITSET | libc::FUTEX_WAIT_REQUEUE_PI | libc::FUTEX_LOCK_PI2
        )
    {
        return FutexCommand::NoSys;
    }
    match cmd {
        libc::FUTEX_WAIT
        | libc::FUTEX_WAIT_BITSET
        | libc::FUTEX_WAKE
        | libc::FUTEX_WAKE_BITSET
        | libc::FUTEX_REQUEUE
        | libc::FUTEX_CMP_REQUEUE
        | libc::FUTEX_WAKE_OP => FutexCommand::Served,
        libc::FUTEX_LOCK_PI => FutexCommand::PriorityInheritance("FUTEX_LOCK_PI"),
        libc::FUTEX_LOCK_PI2 => FutexCommand::PriorityInheritance("FUTEX_LOCK_PI2"),
        libc::FUTEX_UNLOCK_PI => FutexCommand::PriorityInheritance("FUTEX_UNLOCK_PI"),
        libc::FUTEX_TRYLOCK_PI => FutexCommand::PriorityInheritance("FUTEX_TRYLOCK_PI"),
        libc::FUTEX_WAIT_REQUEUE_PI => FutexCommand::PriorityInheritance("FUTEX_WAIT_REQUEUE_PI"),
        libc::FUTEX_CMP_REQUEUE_PI => FutexCommand::PriorityInheritance("FUTEX_CMP_REQUEUE_PI"),
        _ => FutexCommand::NoSys,
    }
}

/// Why `--debug-futex-mode polling` refuses FUTEX_REQUEUE, FUTEX_CMP_REQUEUE and
/// FUTEX_WAKE_OP.
const POLLING_REFUSAL: &str = "--debug-futex-mode polling does not model futex waiter lists";

/// The `val2` of FUTEX_REQUEUE, FUTEX_CMP_REQUEUE and FUTEX_WAKE_OP, which the
/// syscall passes in the timeout argument: `sys_futex` hands
/// `(unsigned long)utime` to `do_futex` as a `u32`, and the commands read it as
/// an `int`.
fn futex_val2(call: &syscalls::Futex) -> u32 {
    call.timeout().map_or(0, Addr::as_raw) as u32
}

/// The raw `uaddr2` argument; NULL is address 0, as Linux keys it.
fn futex_uaddr2(call: &syscalls::Futex) -> usize {
    call.uaddr2().map_or(0, AddrMut::as_raw)
}

/// The raw `uaddr` of a futex call; NULL is a valid private key for the
/// commands that only key their words.
fn futex_uaddr(call: &syscalls::Futex) -> usize {
    call.uaddr().map_or(0, AddrMut::as_raw)
}

/// How a command acquires a futex key: Linux's `enum futex_access`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum KeyAccess {
    /// `FUTEX_READ`: the word is only compared or woken.
    Read,
    /// `FUTEX_WRITE`: the word is changed (WAKE_OP's second word).
    Write,
}

/// The highest address `access_ok` accepts for a futex word on this host,
/// `USER_PTR_MAX` (arch/x86/include/asm/uaccess_64.h). It depends on the
/// paging mode the kernel booted with, so it is measured once from the host
/// kernel, which is also the guest's kernel: a private FUTEX_WAKE with count 0
/// keys the word without touching it and fails with EFAULT exactly when
/// `access_ok` refuses it.
fn user_ptr_max() -> usize {
    static MAX: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *MAX.get_or_init(|| {
        let accepted = |address: usize| {
            // SAFETY: a private FUTEX_WAKE with count 0 never accesses `address`.
            let ret = unsafe {
                libc::syscall(
                    libc::SYS_futex,
                    address,
                    libc::FUTEX_WAKE | libc::FUTEX_PRIVATE_FLAG,
                    0,
                    0,
                    0,
                    0,
                )
            };
            ret >= 0
        };
        // Binary search over 4-aligned addresses: 0 is accepted, and an address
        // with the top bit set never is.
        let (mut ok, mut bad) = (0usize, 1usize << 63);
        while bad - ok > 4 {
            let mid = (ok + (bad - ok) / 2) & !3;
            if accepted(mid) { ok = mid } else { bad = mid }
        }
        ok
    })
}

/// The futex key of a word that a command keys, as Linux's `get_futex_key`
/// (kernel/futex/core.c) admits it, without reading the word:
/// - `-EINVAL` if it is not 4-aligned;
/// - `-EFAULT` if `access_ok` refuses it (`user_ptr_max`);
/// - a private key needs nothing more, so NULL is a valid one;
/// - a shared key needs its backing page, acquired for reading or writing.
///   Which pages the kernel can acquire (a copy-on-write page made read-only, a
///   page past the end of a truncated file, a read-only anonymous page) is not
///   visible in `/proc` metadata, so the kernel is asked with an effect-free
///   probe on the guest's own address (`probe_shared_futex_key`).
async fn futex_key<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    address: usize,
    private: bool,
    access: KeyAccess,
) -> Result<FutexID, Error> {
    if !address.is_multiple_of(std::mem::size_of::<u32>()) {
        return Err(Error::Errno(Errno::EINVAL));
    }
    if address > user_ptr_max() {
        return Err(Error::Errno(Errno::EFAULT));
    }
    if !private {
        probe_shared_futex_key(guest, address, access).await?;
    }
    Ok(guest.thread_state().futex_id(address, private))
}

/// Whether the kernel admits `address` as a shared futex key with `access`,
/// asked with a futex call injected into the guest that runs `get_futex_key`
/// and has no other effect, even on a waiter outside Hermit:
/// - a READ key: a non-private FUTEX_REQUEUE from the word to itself with both
///   counts 0. `futex_requeue` acquires both keys for reading, and its loop
///   then stops before the first waiter (`task_count - nr_wake >= nr_requeue`).
///   A FUTEX_WAKE with count 0 would not do: it still wakes one waiter;
/// - a WRITE key: a non-private FUTEX_WAKE_OP from the word to itself with an
///   unknown operation. `futex_wake_op` acquires the first key for reading and
///   the second for writing, then `futex_atomic_op_inuser` returns -ENOSYS for
///   the unknown operation before it touches the word or wakes anyone.
///
/// Linux's refusal (-EFAULT, or -EINVAL) is returned as is.
async fn probe_shared_futex_key<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    address: usize,
    access: KeyAccess,
) -> Result<(), Error> {
    let probe = shared_futex_key_probe(address, access);
    match run_shared_futex_key_probe(guest, probe, access, |guest, probe| guest.inject(probe)).await
    {
        SharedKeyProbe::Admitted => Ok(()),
        SharedKeyProbe::Refused(errno) => Err(Error::Errno(errno)),
        SharedKeyProbe::Interrupted(errno) => {
            refuse_interrupted_shared_key_probe(guest, address, errno).await
        }
    }
}

/// The effect-free futex call that asks the kernel whether `address` is a
/// shared key with `access` (see `probe_shared_futex_key`).
fn shared_futex_key_probe(address: usize, access: KeyAccess) -> syscalls::Futex {
    let word = AddrMut::<libc::c_int>::from_raw(address);
    match access {
        KeyAccess::Read => syscalls::Futex::new()
            .with_uaddr(word)
            .with_futex_op(libc::FUTEX_REQUEUE)
            .with_val(0)
            .with_uaddr2(word),
        KeyAccess::Write => syscalls::Futex::new()
            .with_uaddr(word)
            .with_futex_op(libc::FUTEX_WAKE_OP)
            .with_val(0)
            .with_uaddr2(word)
            .with_val3((7 << 28) | (1 << 12)),
    }
}

/// The pending answer to one injected shared-key probe.
type KeyProbeAnswer<'a> =
    std::pin::Pin<Box<dyn std::future::Future<Output = Result<i64, Errno>> + Send + 'a>>;

/// What one injected shared-key probe answered.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SharedKeyProbe {
    /// The kernel admitted the key: 0, or ENOSYS for a WRITE key (the unknown
    /// WAKE_OP operation is rejected only after both keys are acquired).
    Admitted,
    /// The kernel refused the key with this errno, which is the guest's.
    Refused(Errno),
    /// A signal stopped the probe's instruction before it ran (Reverie's
    /// `Guest::inject` returns ERESTARTSYS; EINTR is never a key answer either,
    /// since neither probe sleeps). The kernel answered nothing.
    Interrupted(Errno),
}

/// Inject a shared-key `probe` once with `inject` and classify the answer.
///
/// The probe is never injected again: Reverie holds the signal that stopped
/// it in a single slot, and a second interrupted injection would replace that
/// signal, losing one the kernel already dequeued (Codex re-check #4 of
/// https://github.com/rrnewton/hermit/pull/4020). An interruption is returned
/// as such, never as an admission or as the guest's errno.
async fn run_shared_futex_key_probe<C>(
    context: &mut C,
    probe: syscalls::Futex,
    access: KeyAccess,
    inject: for<'a> fn(&'a mut C, syscalls::Futex) -> KeyProbeAnswer<'a>,
) -> SharedKeyProbe {
    match inject(context, probe).await {
        Ok(_) => SharedKeyProbe::Admitted,
        Err(errno @ (Errno::ERESTARTSYS | Errno::EINTR)) => SharedKeyProbe::Interrupted(errno),
        Err(Errno::ENOSYS) if access == KeyAccess::Write => SharedKeyProbe::Admitted,
        Err(errno) => SharedKeyProbe::Refused(errno),
    }
}

/// Stop the run by name because a signal interrupted the shared-key probe for
/// the futex word at `address`.
///
/// The original call has no answer to return: the interruption token is not a
/// Linux result for it, retrying could lose the held signal
/// (`run_shared_futex_key_probe`), and admitting the key unchecked could be a
/// silent wrong answer. The run ends with the policy-refusal status whatever
/// the unsupported-operation policy, since `--allow-unsupported-syscalls` has
/// no errno to offer here either. A signal must arrive at exactly that
/// instruction, which is rare.
async fn refuse_interrupted_shared_key_probe<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    address: usize,
    errno: Errno,
) -> ! {
    {
        use std::io::Write;
        let _ = writeln!(
            crate::util::RetryingStderr,
            "hermit: a signal interrupted Detcore's shared futex key check for word {:#x} in thread {} ({}); \
             refusing the run rather than retrying it, which could lose that signal \
             (https://github.com/rrnewton/hermit/pull/4020)",
            address,
            guest.thread_state().dettid,
            errno,
        );
    }
    crate::tool_global::unrecoverable_shutdown(guest, detcore_model::HERMIT_POLICY_REFUSAL_EXIT)
        .await
}

/// A futex word's value as a user-mode access reads it, which is what Linux's
/// `futex_get_value_locked`/`get_user` and `futex_atomic_op_inuser` do:
/// `-EFAULT` if the word cannot be read.
///
/// The word is read as exactly four bytes with the guest's own permissions. On
/// x86 a writable page is also readable from user mode, but that read refuses a
/// mapping without `PROT_READ`, so for a writable one the 8-byte-aligned word
/// containing it is read instead. That cannot cross into the next page, unlike a
/// debugger read at the word's own address. A debugger read alone would also
/// ignore `PROT_NONE`.
fn read_futex_word<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    address: usize,
) -> Result<i32, Error> {
    let word = Addr::<u8>::from_raw(address).ok_or(Errno::EFAULT)?;
    let mut bytes = [0u8; 4];
    if guest
        .memory()
        .read_exact_with_user_access(word, &mut bytes)
        .is_ok()
    {
        return Ok(i32::from_ne_bytes(bytes));
    }
    // Only a backend that runs the guest as real host threads has a host
    // `/proc/<pid>/maps` describing the guest; elsewhere (KVM's guest pid is
    // virtual) the user-access read above is the backend's whole answer.
    if !bundled_signal_facts::host_task_reads_enabled(&guest.config().backend)
        || !mapping_is_writable(guest.pid().as_raw(), address)
    {
        return Err(Error::Errno(Errno::EFAULT));
    }
    let aligned = address & !(std::mem::size_of::<u64>() - 1);
    let pair: u64 = guest
        .memory()
        .read_value(Addr::<u64>::from_raw(aligned).ok_or(Errno::EFAULT)?)
        .map_err(|_| Error::Errno(Errno::EFAULT))?;
    let offset = address - aligned;
    let pair = pair.to_ne_bytes();
    bytes.copy_from_slice(&pair[offset..offset + 4]);
    Ok(i32::from_ne_bytes(bytes))
}

/// Whether the guest mapping containing `address` is writable, from
/// `/proc/<pid>/maps`. False if no mapping contains it or the file cannot be
/// read.
fn mapping_is_writable(pid: i32, address: usize) -> bool {
    mapping_permissions(pid, address).is_some_and(|perms| perms[1] == b'w')
}

/// The `rwxp` permissions of the guest mapping containing `address`, from
/// `/proc/<pid>/maps`, or `None` if no mapping contains it or the file cannot be
/// read.
fn mapping_permissions(pid: i32, address: usize) -> Option<[u8; 4]> {
    let maps = std::fs::read_to_string(format!("/proc/{pid}/maps")).ok()?;
    maps.lines().find_map(|line| {
        let mut fields = line.split_whitespace();
        let (Some(range), Some(perms)) = (fields.next(), fields.next()) else {
            return None;
        };
        let (start, end) = range.split_once('-')?;
        let (Ok(start), Ok(end)) = (
            usize::from_str_radix(start, 16),
            usize::from_str_radix(end, 16),
        ) else {
            return None;
        };
        if !(start..end).contains(&address) {
            return None;
        }
        perms.as_bytes().get(..4)?.try_into().ok()
    })
}

/// A `struct timespec` copied from the guest as Linux's `get_timespec64`
/// (`copy_from_user`) copies it: every byte must lie in a mapping user mode can
/// read, which on x86 includes a write-only one, or the copy is EFAULT, with no
/// partial result. On a backend whose guest is real host threads, the guest's
/// host `/proc/<pid>/maps` decides; elsewhere the backend's user-access read. The bytes are then read through the tracer, which is only
/// safe because every page they span was checked first: the tracer's own read
/// would force its way through a PROT_NONE page.
fn read_user_timespec<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    address: usize,
) -> Result<Timespec, Error> {
    let size = std::mem::size_of::<Timespec>();
    let last = address.checked_add(size - 1).ok_or(Errno::EFAULT)?;
    if last > user_ptr_max() {
        return Err(Error::Errno(Errno::EFAULT));
    }
    if !bundled_signal_facts::host_task_reads_enabled(&guest.config().backend) {
        // No host `/proc` describes this guest (KVM's guest pid is virtual), so
        // the backend's own user-access read decides, all or nothing.
        let mut bytes = [0u8; std::mem::size_of::<Timespec>()];
        guest
            .memory()
            .read_exact_with_user_access(
                Addr::<u8>::from_raw(address).ok_or(Errno::EFAULT)?,
                &mut bytes,
            )
            .map_err(|_| Error::Errno(Errno::EFAULT))?;
        let field = |at: usize| -> i64 {
            let mut raw = [0u8; 8];
            raw.copy_from_slice(&bytes[at..at + 8]);
            i64::from_ne_bytes(raw)
        };
        return Ok(Timespec {
            tv_sec: field(0) as libc::time_t,
            tv_nsec: field(8) as libc::c_long,
        });
    }
    // 4096 divides every page size, so probing each 4 KiB step checks every page.
    let page = 4096;
    let pid = guest.pid().as_raw();
    let mut cursor = address & !(page - 1);
    while cursor <= last {
        let probe = cursor.max(address);
        let readable = mapping_permissions(pid, probe)
            .is_some_and(|perms| perms[0] == b'r' || perms[1] == b'w');
        if !readable {
            return Err(Error::Errno(Errno::EFAULT));
        }
        cursor += page;
    }
    // Read the bytes as aligned 8-byte words, which the tracer reads one at a
    // time even from a write-only page; a larger read goes through
    // `process_vm_readv`, which needs the page to be readable.
    let start = address & !7;
    let mut bytes = Vec::with_capacity(last + 1 - start);
    let mut word = start;
    while word <= last {
        let value: u64 = guest
            .memory()
            .read_value(Addr::<u64>::from_raw(word).ok_or(Errno::EFAULT)?)
            .map_err(|_| Error::Errno(Errno::EFAULT))?;
        bytes.extend_from_slice(&value.to_ne_bytes());
        word += 8;
    }
    let field = |at: usize| -> i64 {
        let mut raw = [0u8; 8];
        raw.copy_from_slice(&bytes[address - start + at..address - start + at + 8]);
        i64::from_ne_bytes(raw)
    };
    Ok(Timespec {
        tv_sec: field(0) as libc::time_t,
        tv_nsec: field(8) as libc::c_long,
    })
}

// FUTEX_WAKE_OP operations and comparisons (include/uapi/linux/futex.h).
const FUTEX_OP_SET: u32 = 0;
const FUTEX_OP_ADD: u32 = 1;
const FUTEX_OP_OR: u32 = 2;
const FUTEX_OP_ANDN: u32 = 3;
const FUTEX_OP_XOR: u32 = 4;
const FUTEX_OP_OPARG_SHIFT: u32 = 8;
const FUTEX_OP_CMP_EQ: u32 = 0;
const FUTEX_OP_CMP_NE: u32 = 1;
const FUTEX_OP_CMP_LT: u32 = 2;
const FUTEX_OP_CMP_GE: u32 = 3;
const FUTEX_OP_CMP_LE: u32 = 4;
const FUTEX_OP_CMP_GT: u32 = 5;

/// FUTEX_WAKE_OP's encoded operation, decoded as Linux's
/// `futex_atomic_op_inuser` (kernel/futex/waitwake.c) decodes it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct FutexWakeOp {
    op: u32,
    cmp: u32,
    oparg: i32,
    cmparg: i32,
}

impl FutexWakeOp {
    pub(crate) fn decode(encoded: u32) -> Self {
        let sign_extend_12 = |v: u32| ((v << 20) as i32) >> 20;
        let op = (encoded & 0x7000_0000) >> 28;
        let cmp = (encoded & 0x0f00_0000) >> 24;
        let mut oparg = sign_extend_12((encoded & 0x00ff_f000) >> 12);
        let cmparg = sign_extend_12(encoded & 0x0000_0fff);
        if encoded & (FUTEX_OP_OPARG_SHIFT << 28) != 0 {
            // Linux masks an out-of-range shift (and logs "fix this program").
            if !(0..=31).contains(&oparg) {
                oparg &= 31;
            }
            oparg = 1i32.wrapping_shl(oparg as u32);
        }
        FutexWakeOp {
            op,
            cmp,
            oparg,
            cmparg,
        }
    }

    /// Whether the operation is one x86's `arch_futex_atomic_op_inuser` knows;
    /// it returns `-ENOSYS` for any other before touching the word.
    pub(crate) fn has_known_operation(&self) -> bool {
        self.apply(0).is_some()
    }

    /// The new value of the word, from its old value.
    pub(crate) fn apply(&self, old: i32) -> Option<i32> {
        Some(match self.op {
            FUTEX_OP_SET => self.oparg,
            FUTEX_OP_ADD => old.wrapping_add(self.oparg),
            FUTEX_OP_OR => old | self.oparg,
            FUTEX_OP_ANDN => old & !self.oparg,
            FUTEX_OP_XOR => old ^ self.oparg,
            _ => return None,
        })
    }

    /// Whether to wake on `uaddr2`, from the word's old value; `None` (`-ENOSYS`)
    /// for an unknown comparison.
    pub(crate) fn compare(&self, old: i32) -> Option<bool> {
        Some(match self.cmp {
            FUTEX_OP_CMP_EQ => old == self.cmparg,
            FUTEX_OP_CMP_NE => old != self.cmparg,
            FUTEX_OP_CMP_LT => old < self.cmparg,
            FUTEX_OP_CMP_GE => old >= self.cmparg,
            FUTEX_OP_CMP_LE => old <= self.cmparg,
            FUTEX_OP_CMP_GT => old > self.cmparg,
            _ => return None,
        })
    }
}

#[cfg(test)]
mod tests {

    /// A copy the record marks as discarded at send is left out even when the
    /// wake's answer names its signal (`committed`), as for a drain batch's
    /// copy that was flushed and sent again, discarded (R4-1 of the R2b-1
    /// design re-checks, https://github.com/rrnewton/hermit/issues/4273; arm
    /// A9). A kept copy in either queue stays.
    #[test]
    fn a_discarded_copy_is_left_out_even_when_committed() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        let state = KernelSignalState {
            pending: chld | usr1,
            thread_pending: chld,
            shared_pending: usr1,
            ..Default::default()
        };
        assert_eq!(with_kept_copies(&state, chld, 0, chld | usr1, chld), usr1);
        assert_eq!(with_kept_copies(&state, 0, usr1, chld | usr1, usr1), chld);
        assert_eq!(with_kept_copies(&state, 0, 0, chld | usr1, 0), chld | usr1);
    }
    use std::io::Read;
    use std::io::Seek;
    use std::os::fd::AsRawFd;

    use reverie::GlobalRPC;
    use reverie::GlobalTool;
    use reverie::Tid;
    use reverie::Tool;

    use super::*;

    /// The Kernel path is ptrace's, and the Barrier path is DBT's alone: a
    /// backend gets the Barrier path only by setting a flag whose other
    /// properties it must then have (`owner_death_path`).
    #[test]
    fn owner_death_path_is_tied_to_the_backends_that_have_its_properties() {
        let path = |backend: reverie::BackendCapabilities| {
            let mut cfg = Config::default().with_backend(|capabilities| *capabilities = backend);
            cfg.sequentialize_threads = true;
            cfg.debug_futex_mode = BlockingMode::Precise;
            owner_death_path(&cfg)
        };
        assert_eq!(
            path(reverie::BackendCapabilities::PTRACE),
            Some(OwnerDeathPath::Kernel)
        );
        assert_eq!(
            path(reverie::BackendCapabilities::DBT),
            Some(OwnerDeathPath::Barrier)
        );
        for backend in [
            reverie::BackendCapabilities::KVM,
            reverie::BackendCapabilities::SABRE,
            reverie::BackendCapabilities::LITEINST_IN_GUEST,
        ] {
            assert_eq!(path(backend), None, "{backend:?}");
        }
    }

    /// Each configuration takes its own `sched_yield` route; only chaos with
    /// the timer on and delivered, no schedule replay, and a heuristic other
    /// than `Random` takes the yield epoch, and an ordinary run still ends its
    /// timeslice (https://github.com/rrnewton/hermit/issues/4068,
    /// https://github.com/rrnewton/hermit/issues/4150).
    #[test]
    fn each_configuration_takes_its_sched_yield_route() {
        let path = || Some(std::path::PathBuf::from("record.json"));
        let cases = [
            (Config::default(), SchedYieldRoute::EndTimeslice),
            // The ordinary ptrace run: timers delivered, no chaos.
            (
                Config {
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                    backend.runs_guest_code_between_injections = false;
                }),
                SchedYieldRoute::EndTimeslice,
            ),
            (
                Config {
                    chaos: true,
                    replay_schedule_from: path(),
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                    backend.runs_guest_code_between_injections = false;
                }),
                SchedYieldRoute::WeakYield,
            ),
            (
                Config {
                    chaos: true,
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                    backend.runs_guest_code_between_injections = false;
                }),
                SchedYieldRoute::ChaosYieldEpoch,
            ),
            (
                Config {
                    chaos: true,
                    sched_heuristic: SchedHeuristic::Random,
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                    backend.runs_guest_code_between_injections = false;
                }),
                SchedYieldRoute::WeakYield,
            ),
            (
                Config {
                    chaos: true,
                    sched_heuristic: SchedHeuristic::StickyRandom,
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                    backend.runs_guest_code_between_injections = false;
                }),
                SchedYieldRoute::ChaosYieldEpoch,
            ),
            (
                Config {
                    chaos: true,
                    sched_heuristic: SchedHeuristic::ConnectBind,
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                    backend.runs_guest_code_between_injections = false;
                }),
                SchedYieldRoute::ChaosYieldEpoch,
            ),
            (
                Config {
                    chaos: true,
                    max_timeslice: None,
                    sched_heuristic: SchedHeuristic::Random,
                    ..Config::default()
                },
                SchedYieldRoute::ChaosReprioritize,
            ),
            // A backend that delivers no preemption timer events.
            (
                Config {
                    chaos: true,
                    ..Config::default()
                }
                .with_backend(|backend| backend.delivers_preemption_timer_events = false),
                SchedYieldRoute::WeakYield,
            ),
            (
                Config {
                    chaos: true,
                    max_timeslice: None,
                    ..Config::default()
                },
                SchedYieldRoute::ChaosReprioritize,
            ),
            (
                Config {
                    chaos: true,
                    replay_preemptions_from: path(),
                    ..Config::default()
                }
                .with_backend(|backend| {
                    backend.delivers_preemption_timer_events = true;
                    backend.runs_guest_code_between_injections = false;
                }),
                SchedYieldRoute::ChaosYieldEpoch,
            ),
            (
                Config {
                    chaos: true,
                    replay_schedule_from: path(),
                    ..Config::default()
                },
                SchedYieldRoute::WeakYield,
            ),
            (
                Config {
                    replay_schedule_from: path(),
                    ..Config::default()
                },
                SchedYieldRoute::WeakYield,
            ),
            (
                Config {
                    replay_preemptions_from: path(),
                    ..Config::default()
                },
                SchedYieldRoute::ReplayStrongYield,
            ),
        ];
        for (cfg, route) in cases {
            assert_eq!(
                sched_yield_route(&cfg),
                route,
                "chaos={} timer delivered={} heuristic={:?} max_timeslice={:?} preemptions={:?} schedule={:?}",
                cfg.chaos,
                cfg.backend.delivers_preemption_timer_events,
                cfg.sched_heuristic,
                cfg.max_timeslice,
                cfg.replay_preemptions_from,
                cfg.replay_schedule_from
            );
        }
    }

    /// A thread whose reported pid is not its thread group, as Reverie
    /// reports for a process that a raw `clone` without `CLONE_THREAD` made,
    /// still has a readable signal state and is not taken to be gone
    /// (https://github.com/rrnewton/hermit/issues/3982). The test's own thread
    /// stands in for it, with this process's parent as the wrong pid.
    #[test]
    fn a_thread_reported_under_another_pid_is_read_and_alive() {
        let tid = Pid::from_raw(unsafe { libc::syscall(libc::SYS_gettid) } as i32);
        let wrong = Pid::from_raw(unsafe { libc::getppid() });
        assert_ne!(wrong, tid);
        assert!(read_kernel_signal_state(wrong, tid).is_err());
        assert!(read_thread_signal_state(wrong, tid).is_ok());
        assert!(!thread_is_gone(wrong, tid));
        assert!(read_thread_wait_signal_state(wrong, tid).is_ok());
    }

    /// Host `/proc` signal state reads are forbidden exactly where guest
    /// thread IDs do not name host tasks: KVM, and no other backend today.
    #[test]
    fn host_signal_state_reads_are_refused_only_where_guest_tids_are_not_host_tasks() {
        assert!(refuses_host_signal_state_reads(&BackendCapabilities::KVM));
        for backend in [
            BackendCapabilities::PTRACE,
            BackendCapabilities::E9PATCH,
            BackendCapabilities::SABRE,
            BackendCapabilities::DBT,
            BackendCapabilities::LITEINST_IN_GUEST,
        ] {
            assert!(!refuses_host_signal_state_reads(&backend));
        }
    }

    /// Once the refusal is latched for a backend whose guest thread IDs are
    /// not host tasks, a signal state read never reaches `/proc`: the test's
    /// own live thread, whose state is readable before, is refused after.
    #[test]
    fn a_latched_refusal_never_reads_a_host_task_signal_state() {
        let pid = Pid::from_raw(unsafe { libc::getpid() });
        let tid = Pid::from_raw(unsafe { libc::syscall(libc::SYS_gettid) } as i32);
        refuse_host_signal_state_reads(&BackendCapabilities::PTRACE);
        assert!(!host_signal_state_reads_refused());
        assert!(read_kernel_signal_state(pid, tid).is_ok());
        refuse_host_signal_state_reads(&BackendCapabilities::KVM);
        assert!(host_signal_state_reads_refused());
        assert_eq!(read_kernel_signal_state(pid, tid), Err(Errno::ENOSYS));
        assert_eq!(read_thread_signal_state(pid, tid), Err(Errno::ENOSYS));
    }
    use crate::config::Config;
    use crate::tool_global::GlobalRequest;
    use crate::tool_global::GlobalState;
    use crate::types::MmId;

    /// A new process whose exit signal is not SIGCHLD makes its creator hold
    /// that signal; a thread, a `CLONE_PARENT` child, a SIGCHLD child, and an
    /// out-of-range signal add nothing
    /// (https://github.com/rrnewton/hermit/issues/3146).
    #[test]
    fn a_child_exit_signal_other_than_sigchld_is_host_timed_for_its_creator() {
        let usr1 = 1_u64 << (libc::SIGUSR1 - 1);
        let none = CloneFlags::empty();
        assert_eq!(clone_host_timed_signals(none, libc::SIGUSR1), usr1);
        assert_eq!(
            clone_host_timed_signals(CloneFlags::CLONE_VFORK, libc::SIGUSR1),
            usr1
        );
        assert_eq!(clone_host_timed_signals(none, libc::SIGCHLD), 0);
        assert_eq!(clone_host_timed_signals(none, 0), 0);
        assert_eq!(clone_host_timed_signals(none, 65), 0);
        let thread = CloneFlags::CLONE_THREAD | CloneFlags::CLONE_SIGHAND | CloneFlags::CLONE_VM;
        assert_eq!(clone_host_timed_signals(thread, libc::SIGUSR1), 0);
        assert_eq!(
            clone_host_timed_signals(CloneFlags::CLONE_PARENT, libc::SIGUSR1),
            0
        );
    }

    struct FailedExecStack;
    struct FailedExecStackGuard;

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

    impl reverie::Stack for FailedExecStack {
        type StackGuard = FailedExecStackGuard;

        fn size(&self) -> usize {
            panic!("failed exec must not use the guest stack")
        }
        fn capacity(&self) -> usize {
            panic!("failed exec must not use the guest stack")
        }
        fn push<'stack, T>(&mut self, _: T) -> Addr<'stack, T> {
            panic!("failed exec must not use the guest stack")
        }
        fn reserve<'stack, T>(&mut self) -> AddrMut<'stack, T> {
            panic!("failed exec must not use the guest stack")
        }
        fn commit(self) -> Result<Self::StackGuard, Errno> {
            panic!("failed exec must not use the guest stack")
        }
    }

    // Inject only the backend's errno and, when the backend refuses the exec
    // as unsupported, its typed refusal. Preparation, rollback, cancellation,
    // unsupported reporting and the configured refusal policy are real code.
    struct FailedExecGuest<'a> {
        config: &'a Config,
        global: &'a GlobalState,
        thread: crate::ThreadState<()>,
        sender: Tid,
        process: Tid,
        old_mm: MmId,
        errno: Errno,
        backend_refuses: bool,
        refusal: Option<reverie::UnsupportedRefusal>,
        refusal_takes: usize,
        injections: usize,
        requests: Mutex<Vec<GlobalRequest>>,
    }

    #[reverie::tool]
    impl GlobalRPC<GlobalState> for FailedExecGuest<'_> {
        async fn send_rpc(
            &self,
            message: <GlobalState as GlobalTool>::Request,
        ) -> <GlobalState as GlobalTool>::Response {
            assert_eq!(message.1, self.old_mm, "RPC follows restored exec identity");
            self.requests.lock().unwrap().push(message.2.clone());
            self.global.receive_rpc(self.sender, message).await
        }
        fn config(&self) -> &Config {
            self.config
        }
    }

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

        fn tid(&self) -> Tid {
            self.sender
        }
        fn pid(&self) -> Tid {
            self.process
        }
        fn ppid(&self) -> Option<Tid> {
            None
        }
        fn memory(&self) -> Self::Memory {
            panic!("failed exec rollback must not read guest memory")
        }
        fn thread_state(&self) -> &crate::ThreadState<()> {
            &self.thread
        }
        fn thread_state_mut(&mut self) -> &mut crate::ThreadState<()> {
            &mut self.thread
        }
        async fn regs(&mut self) -> libc::user_regs_struct {
            panic!("failed exec rollback must not read registers")
        }
        async fn stack(&mut self) -> Self::Stack {
            panic!("failed exec rollback must not use a guest stack")
        }
        async fn daemonize(&mut self) {
            panic!("failed exec rollback must not daemonize")
        }
        async fn inject<S: SyscallInfo>(&mut self, call: S) -> Result<i64, Errno> {
            assert_eq!(call.number(), syscalls::Sysno::execveat);
            assert_eq!(
                self.thread.mm_id,
                self.old_mm.for_exec(self.thread.detpid.unwrap())
            );
            assert_eq!(self.thread.robust_list_head, None);
            self.injections += 1;
            // As reverie-kvm does, every injection replaces the record.
            self.refusal = self.backend_refuses.then(|| {
                reverie::UnsupportedRefusal::new(
                    reverie::UnsupportedOperation::NonLeaderExec,
                    self.errno,
                    "KVM nonleader exec is unsupported",
                )
            });
            Err(self.errno)
        }
        fn take_unsupported_refusal(&mut self) -> Option<reverie::UnsupportedRefusal> {
            assert_eq!(
                self.thread.mm_id,
                self.old_mm.for_exec(self.thread.detpid.unwrap()),
                "the refusal is taken before the failed exec is rolled back"
            );
            self.refusal_takes += 1;
            self.refusal.take()
        }
        async fn tail_inject<S: SyscallInfo>(&mut self, _: S) -> reverie::Never {
            panic!("failed exec must not retire a live guest")
        }
        fn set_timer(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
            panic!("failed exec rollback must not replace the timer")
        }
        fn set_timer_precise(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
            panic!("failed exec rollback must not replace the timer")
        }
        fn read_clock(&mut self) -> Result<u64, Error> {
            panic!("failed exec rollback must not sample the clock")
        }
    }

    #[tokio::test]
    async fn backend_exec_refusal_preserves_failed_exec_rollback_and_policy() {
        let process = DetPid::from_raw(17);
        let worker = DetTid::from_raw(18);
        // `backend_refuses` is whether the backend reports a typed refusal for
        // the exec. reverie-kvm reports one for every worker exec that returns
        // ENOSYS, whether its own refusal after preflight or an ENOSYS from a
        // host syscall the preflight makes: the first two rows. The last row
        // is a worker ENOSYS for which a backend with KVM's other behaviours
        // reports no refusal (as KvmGuest::inject's early admission refusals
        // do); Detcore must not infer a refusal from the errno, so it is an
        // ordinary exec failure.
        for (kvm_behaviours, caller, errno, backend_refuses, fail_closed, refused, reported) in [
            (true, worker, Errno::ENOSYS, true, true, true, false),
            (true, worker, Errno::ENOSYS, true, false, false, true),
            (true, worker, Errno::ENOENT, false, true, false, false),
            (true, worker, Errno::ENOEXEC, false, true, false, false),
            (true, worker, Errno::EFAULT, false, true, false, false),
            (true, worker, Errno::EACCES, false, true, false, false),
            (true, worker, Errno::EOPNOTSUPP, false, true, false, false),
            (true, process, Errno::ENOSYS, false, true, false, false),
            (false, worker, Errno::ENOSYS, false, true, false, false),
            (true, worker, Errno::ENOSYS, false, true, false, false),
        ] {
            let mut report = tempfile::tempfile().unwrap();
            let config = Config {
                sequentialize_threads: false,
                panic_on_unsupported_syscalls: fail_closed,
                exit_on_unsupported_syscall: true,
                shutdown_on_unsupported_syscall: false,
                unsupported_syscall_report_fd: Some(report.as_raw_fd()),
                ..Config::default().with_backend(|backend| {
                    if kvm_behaviours {
                        // The behaviours the old `backend_is_kvm` identity flag
                        // selected, other than the exec refusal, which the
                        // backend now reports per exec (`backend_refuses`), and
                        // the user address limit, which it reports per guest.
                        backend.provides_process_signal_control = true;
                        backend.emulates_child_waits = true;
                    }
                })
            };
            let global = GlobalState::init_global_state(&config).await;
            let tool = Detcore::new(Tid::from_raw(process.as_raw()), &config);
            let mut thread = crate::ThreadState::new(caller, &config, ());
            thread.detpid = Some(process);
            thread.mm_id = MmId::initial(process);
            thread.record_robust_list_head(Some(0x12340));
            thread.thread_logical_time.add_syscall_with_cost(123);
            let old_time = thread.thread_logical_time.as_nanos();
            let old_files = Arc::clone(&thread.file_metadata);
            let old_memory = Arc::clone(&thread.memory_metadata);
            let old_mm = thread.mm_id;
            let mut guest = FailedExecGuest {
                config: &config,
                global: &global,
                thread,
                sender: Tid::from_raw(caller.as_raw()),
                process: Tid::from_raw(process.as_raw()),
                old_mm,
                errno,
                backend_refuses,
                refusal: None,
                refusal_takes: 0,
                injections: 0,
                requests: Mutex::new(Vec::new()),
            };
            let result = tool
                .handle_execveat(&mut guest, syscalls::Execveat::new())
                .await;
            match result {
                Err(Error::Tool(error)) if refused => assert_eq!(
                    error
                        .downcast_ref::<crate::UnsupportedSyscallError>()
                        .unwrap()
                        .0,
                    syscalls::Sysno::execveat
                ),
                Err(Error::Errno(actual)) if !refused => assert_eq!(actual, errno),
                result => panic!("wrong failed-exec policy: {result:?}"),
            }
            assert_eq!(guest.injections, 1, "backend preflight must run first");
            assert_eq!(guest.refusal_takes, 1, "the refusal must be asked for once");
            assert_eq!(guest.refusal, None, "a reported refusal must be taken");
            assert_eq!(guest.thread.dettid, caller);
            assert_eq!(guest.thread.detpid, Some(process));
            assert_eq!(guest.thread.mm_id, old_mm);
            assert_eq!(guest.thread.robust_list_head, Some(0x12340));
            assert_eq!(guest.thread.thread_logical_time.as_nanos(), old_time);
            assert!(Arc::ptr_eq(&guest.thread.file_metadata, &old_files));
            assert!(Arc::ptr_eq(&guest.thread.memory_metadata, &old_memory));
            let requests = guest.requests.lock().unwrap();
            assert!(matches!(requests[0], GlobalRequest::PrepareExec(..)));
            assert!(matches!(requests[1], GlobalRequest::CancelExec(..)));
            assert_eq!(requests.len(), if reported { 3 } else { 2 });
            if reported {
                assert!(
                    matches!(&requests[2], GlobalRequest::ReportUnsupportedSyscall(name) if name == "execveat")
                );
            }
            report.rewind().unwrap();
            let mut aggregate = String::new();
            report.read_to_string(&mut aggregate).unwrap();
            assert_eq!(aggregate, if reported { "execveat\n" } else { "" });
        }
    }

    /// A guest whose backend fails every clone with `errno`, recording a typed
    /// refusal when `backend_refuses`, as an in-guest backend does for a
    /// thread it cannot create.
    struct FailedCloneGuest<'a> {
        config: &'a Config,
        global: &'a GlobalState,
        thread: crate::ThreadState<()>,
        sender: Tid,
        errno: Errno,
        backend_refuses: bool,
        refusal: Option<reverie::UnsupportedRefusal>,
        refusal_takes: usize,
        injections: usize,
        requests: Mutex<Vec<GlobalRequest>>,
    }

    #[reverie::tool]
    impl GlobalRPC<GlobalState> for FailedCloneGuest<'_> {
        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(self.sender, message).await
        }
        fn config(&self) -> &Config {
            self.config
        }
    }

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

        fn tid(&self) -> Tid {
            self.sender
        }
        fn pid(&self) -> Tid {
            self.sender
        }
        fn ppid(&self) -> Option<Tid> {
            None
        }
        fn memory(&self) -> Self::Memory {
            reverie::syscalls::LocalMemory::new()
        }
        fn thread_state(&self) -> &crate::ThreadState<()> {
            &self.thread
        }
        fn thread_state_mut(&mut self) -> &mut crate::ThreadState<()> {
            &mut self.thread
        }
        async fn regs(&mut self) -> libc::user_regs_struct {
            panic!("a failed clone must not read registers")
        }
        async fn stack(&mut self) -> Self::Stack {
            panic!("a failed clone must not use a guest stack")
        }
        async fn daemonize(&mut self) {
            panic!("a failed clone must not daemonize")
        }
        async fn inject<S: SyscallInfo>(&mut self, call: S) -> Result<i64, Errno> {
            assert_eq!(call.number(), syscalls::Sysno::clone);
            self.injections += 1;
            self.refusal = self.backend_refuses.then(|| {
                reverie::UnsupportedRefusal::new(
                    reverie::UnsupportedOperation::NativeExec,
                    self.errno,
                    "this backend cannot create a thread",
                )
            });
            Err(self.errno)
        }
        fn take_unsupported_refusal(&mut self) -> Option<reverie::UnsupportedRefusal> {
            self.refusal_takes += 1;
            self.refusal.take()
        }
        async fn tail_inject<S: SyscallInfo>(&mut self, _: S) -> reverie::Never {
            panic!("a failed clone must not retire a live guest")
        }
        fn set_timer(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
            panic!("a failed clone must not replace the timer")
        }
        fn set_timer_precise(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
            panic!("a failed clone must not replace the timer")
        }
        fn read_clock(&mut self) -> Result<u64, Error> {
            panic!("a failed clone must not sample the clock")
        }
    }

    /// A clone the backend refuses with a typed refusal is refused by the
    /// unsupported-operation policy, never answered with the refusal's errno in
    /// a fail-closed run; a clone that merely fails keeps its errno.
    #[tokio::test]
    async fn backend_clone_refusal_follows_the_unsupported_operation_policy() {
        let process = DetPid::from_raw(23);
        for (errno, backend_refuses, fail_closed, refused, reported) in [
            (Errno::EOPNOTSUPP, true, true, true, false),
            (Errno::EOPNOTSUPP, true, false, false, true),
            (Errno::EAGAIN, false, true, false, false),
            (Errno::EOPNOTSUPP, false, true, false, false),
        ] {
            let mut report = tempfile::tempfile().unwrap();
            let config = Config {
                sequentialize_threads: false,
                panic_on_unsupported_syscalls: fail_closed,
                exit_on_unsupported_syscall: true,
                shutdown_on_unsupported_syscall: false,
                unsupported_syscall_report_fd: Some(report.as_raw_fd()),
                ..Config::default()
            };
            let global = GlobalState::init_global_state(&config).await;
            let tool = Detcore::new(Tid::from_raw(process.as_raw()), &config);
            let mut thread = crate::ThreadState::new(process, &config, ());
            thread.detpid = Some(process);
            let mut guest = FailedCloneGuest {
                config: &config,
                global: &global,
                thread,
                sender: Tid::from_raw(process.as_raw()),
                errno,
                backend_refuses,
                refusal: None,
                refusal_takes: 0,
                injections: 0,
                requests: Mutex::new(Vec::new()),
            };
            let flags = (libc::CLONE_VM
                | libc::CLONE_FS
                | libc::CLONE_FILES
                | libc::CLONE_SIGHAND
                | libc::CLONE_THREAD
                | libc::CLONE_SYSVSEM) as u64;
            let clone = syscalls::Clone::new()
                .with_flags(nix::sched::CloneFlags::from_bits_truncate(flags as _));
            let result = tool
                .handle_clone_family(&mut guest, syscalls::family::CloneFamily::Clone(clone))
                .await;
            match result {
                Err(Error::Tool(error)) if refused => assert_eq!(
                    error
                        .downcast_ref::<crate::UnsupportedSyscallError>()
                        .unwrap()
                        .0,
                    syscalls::Sysno::clone
                ),
                Err(Error::Errno(actual)) if !refused => assert_eq!(actual, errno),
                result => panic!("wrong failed-clone policy: {result:?}"),
            }
            assert_eq!(guest.injections, 1);
            assert_eq!(guest.refusal_takes, 1, "the refusal must be asked for once");
            assert_eq!(guest.refusal, None, "a reported refusal must be taken");
            assert_eq!(guest.thread.clone_flags, None);
            let requests = guest.requests.lock().unwrap();
            assert_eq!(
                requests
                    .iter()
                    .filter(|request| matches!(request, GlobalRequest::ReportUnsupportedSyscall(name) if name == "clone"))
                    .count(),
                usize::from(reported)
            );
            report.rewind().unwrap();
            let mut aggregate = String::new();
            report.read_to_string(&mut aggregate).unwrap();
            assert_eq!(aggregate, if reported { "clone\n" } else { "" });
        }
    }

    fn bits(signals: &[i32]) -> KernelSigset {
        signals
            .iter()
            .fold(0, |set, &signal| set | kernel_sigset_bit(signal))
    }

    #[test]
    fn a_precise_futex_wait_holds_a_committed_signal_only_if_a_host_timed_source_can_post_it() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        // Nothing committed: SIGCHLD and the host-timed signals are held.
        assert_eq!(futex_wait_held_after_wake(0, 0), chld);
        assert_eq!(futex_wait_held_after_wake(usr1, 0), chld | usr1);
        // A committed SIGCHLD is not held (https://github.com/rrnewton/hermit/issues/4005).
        assert_eq!(futex_wait_held_after_wake(0, chld), 0);
        assert_eq!(futex_wait_held_after_wake(usr1, chld), usr1);
        // Unless a host-timed source can post it too, and a committed
        // host-timed signal stays held.
        assert_eq!(futex_wait_held_after_wake(chld, chld), chld);
        assert_eq!(futex_wait_held_after_wake(usr1, usr1 | chld), usr1);
    }

    #[test]
    fn kernel_signal_state_parses_proc_status() {
        let status = "Name:\tguest\nSigQ:\t1/1024\nSigPnd:\t0000000000000200\n\
                      ShdPnd:\t0000000000000400\nSigBlk:\t0000000000000800\n\
                      SigIgn:\t0000000000001000\nSigCgt:\t0000000000000400\n";
        assert_eq!(
            KernelSignalState::parse(status),
            Some(KernelSignalState {
                pending: bits(&[libc::SIGUSR1, libc::SIGSEGV]),
                thread_pending: bits(&[libc::SIGUSR1]),
                shared_pending: bits(&[libc::SIGSEGV]),
                blocked: bits(&[libc::SIGUSR2]),
                ignored: bits(&[libc::SIGPIPE]),
                caught: bits(&[libc::SIGSEGV]),
            })
        );
        // Every line is required: a missing one is not an empty set.
        assert_eq!(
            KernelSignalState::parse("SigPnd:\t0\nShdPnd:\t0\nSigBlk:\t0\nSigIgn:\t0\n"),
            None
        );
        assert_eq!(
            KernelSignalState::parse(
                "SigPnd:\tzz\nShdPnd:\t0\nSigBlk:\t0\nSigIgn:\t0\nSigCgt:\t0\n"
            ),
            None
        );
    }

    #[test]
    fn only_unblocked_caught_or_fatal_signals_interrupt_a_wait() {
        let state = KernelSignalState {
            pending: bits(&[
                libc::SIGUSR1,
                libc::SIGUSR2,
                libc::SIGPIPE,
                libc::SIGTERM,
                libc::SIGCHLD,
                libc::SIGWINCH,
                libc::SIGURG,
                libc::SIGCONT,
            ]),
            blocked: bits(&[libc::SIGUSR2]),
            ignored: bits(&[libc::SIGPIPE]),
            caught: bits(&[libc::SIGUSR1, libc::SIGUSR2, libc::SIGWINCH]),
            ..Default::default()
        };
        // Caught and unblocked (SIGUSR1, SIGWINCH) and default-fatal (SIGTERM) end the
        // wait; blocked (SIGUSR2), ignored (SIGPIPE), and default-ignored ones do not.
        assert_eq!(
            state.pending_interrupting(state.blocked, false),
            bits(&[libc::SIGUSR1, libc::SIGWINCH, libc::SIGTERM])
        );
        // The mask the guest had when the wait began decides, not the current one.
        assert_eq!(
            state.pending_interrupting(0, false),
            bits(&[libc::SIGUSR1, libc::SIGUSR2, libc::SIGWINCH, libc::SIGTERM])
        );
        let interrupting = state.interrupting(state.blocked);
        assert_ne!(interrupting & kernel_sigset_bit(libc::SIGKILL), 0);
        assert_ne!(interrupting & kernel_sigset_bit(libc::SIGSTOP), 0);
        assert_eq!(
            interrupting & kernel_sigset_bit(reverie::PERF_EVENT_SIGNAL as i32),
            0
        );
        assert_eq!(kernel_sigset_bit(0), 0);
        assert_eq!(kernel_sigset_bit(65), 0);
    }

    /// A wait whose restart starts its relative timeout again does not end for a
    /// default `SIGTSTP`, `SIGTTIN`, or `SIGTTOU`, which Linux discards in an
    /// orphaned process group; every other wait still does. `SIGSTOP`, a caught
    /// stop signal, and a fatal signal end both kinds, and an ignored stop signal
    /// ends neither (review of https://github.com/rrnewton/hermit/pull/3361 at
    /// `cbb36408`, finding 4).
    #[test]
    fn a_rearming_wait_is_not_ended_by_a_default_job_control_stop() {
        let stops = bits(&[libc::SIGTSTP, libc::SIGTTIN, libc::SIGTTOU]);
        let state = KernelSignalState {
            pending: stops | bits(&[libc::SIGSTOP, libc::SIGTERM]),
            ..Default::default()
        };
        assert_eq!(state.default_job_control_stops(), stops);
        assert_eq!(state.pending_interrupting(0, false), state.pending);
        assert_eq!(
            state.pending_interrupting(0, true),
            bits(&[libc::SIGSTOP, libc::SIGTERM])
        );
        let rearming = state.interrupting_wait(0, true);
        assert_eq!(rearming & stops, 0);
        assert_eq!(rearming, state.interrupting(0) & !stops);

        // A handler makes SIGTSTP an ordinary caught signal, and SIG_IGN takes
        // SIGTTIN out of every wait; SIGTTOU keeps its default action.
        let state = KernelSignalState {
            pending: stops,
            caught: bits(&[libc::SIGTSTP]),
            ignored: bits(&[libc::SIGTTIN]),
            ..Default::default()
        };
        assert_eq!(state.default_job_control_stops(), bits(&[libc::SIGTTOU]));
        assert_eq!(state.pending_interrupting(0, true), bits(&[libc::SIGTSTP]));
        assert_eq!(
            state.pending_interrupting(0, false),
            bits(&[libc::SIGTSTP, libc::SIGTTOU])
        );
        // The mask still decides first.
        assert_eq!(state.pending_interrupting(bits(&[libc::SIGTSTP]), true), 0);
    }

    #[test]
    fn kernel_blocked_mask_preserves_libc_signal_membership() {
        let mask = blocked_signal_mask();
        let mut libc_mask: libc::sigset_t = unsafe { std::mem::zeroed() };
        unsafe {
            libc::sigfillset(&mut libc_mask);
            libc::sigdelset(&mut libc_mask, reverie::PERF_EVENT_SIGNAL as i32);
        }
        for raw_signal in 1..=KernelSigset::BITS as i32 {
            assert_eq!(
                signal_is_blocked(&mask, SigWrapper(raw_signal)),
                unsafe { libc::sigismember(&libc_mask, raw_signal) == 1 },
                "signal {raw_signal} membership changed while converting to the kernel ABI"
            );
        }
    }

    #[test]
    fn clone_without_child_cleartid_does_not_register_the_pointer() {
        assert_eq!(
            child_tid_clear_address(CloneFlags::CLONE_CHILD_SETTID, 0x1234),
            0
        );
    }

    #[test]
    fn clone_with_child_cleartid_registers_the_pointer() {
        assert_eq!(
            child_tid_clear_address(CloneFlags::CLONE_CHILD_CLEARTID, 0x1234),
            0x1234
        );
    }

    #[test]
    fn linux_default_dispositions_that_do_not_interrupt_child_waits() {
        for signal in [libc::SIGCHLD, libc::SIGCONT, libc::SIGURG, libc::SIGWINCH] {
            assert!(signal_default_disposition_does_not_interrupt_child_wait(
                SigWrapper(signal)
            ));
        }
        for signal in [libc::SIGALRM, libc::SIGSTOP, libc::SIGUSR1] {
            assert!(!signal_default_disposition_does_not_interrupt_child_wait(
                SigWrapper(signal)
            ));
        }
    }

    /// signal(7): the signals whose default action is to terminate or dump
    /// core end the process; those ignored, stopping or continuing by
    /// default do not, and SIGKILL never reaches a delivery callback
    /// (https://github.com/rrnewton/hermit/issues/4278).
    #[test]
    fn default_actions_that_end_the_process() {
        for signal in [
            libc::SIGTERM,
            libc::SIGINT,
            libc::SIGHUP,
            libc::SIGQUIT,
            libc::SIGABRT,
            libc::SIGSEGV,
            libc::SIGBUS,
            libc::SIGFPE,
            libc::SIGILL,
            libc::SIGPIPE,
            libc::SIGALRM,
            libc::SIGUSR1,
            libc::SIGUSR2,
            libc::SIGRTMIN(),
            libc::SIGRTMAX(),
        ] {
            assert!(
                signal_default_action_ends_the_process(SigWrapper(signal)),
                "{signal}"
            );
        }
        for signal in [
            libc::SIGCHLD,
            libc::SIGCONT,
            libc::SIGURG,
            libc::SIGWINCH,
            libc::SIGSTOP,
            libc::SIGTSTP,
            libc::SIGTTIN,
            libc::SIGTTOU,
            libc::SIGKILL,
        ] {
            assert!(
                !signal_default_action_ends_the_process(SigWrapper(signal)),
                "{signal}"
            );
        }
    }

    #[test]
    fn uncatchable_signals_do_not_require_a_sigaction_query() {
        assert!(signal_has_uncatchable_default_disposition(SigWrapper(
            libc::SIGKILL
        )));
        assert!(signal_has_uncatchable_default_disposition(SigWrapper(
            libc::SIGSTOP
        )));
        assert!(!signal_has_uncatchable_default_disposition(SigWrapper(
            libc::SIGUSR1
        )));
    }

    #[test]
    fn waitid_ready_child_wins_when_scheduler_also_reports_a_signal() {
        assert_eq!(
            exact_wait_poll_decision(true, true, Some(ExactChildWaitState::Running)),
            ExactWaitPollDecision::ChildReady
        );
        assert_eq!(
            exact_wait_poll_decision(false, true, Some(ExactChildWaitState::LogicallyExited)),
            ExactWaitPollDecision::ReapAfterLogicalExit
        );
        assert_eq!(
            exact_wait_poll_decision(false, true, Some(ExactChildWaitState::PhysicalExitPending)),
            ExactWaitPollDecision::AwaitPhysicalExit
        );
        assert_eq!(
            exact_wait_poll_decision(false, true, Some(ExactChildWaitState::Running)),
            ExactWaitPollDecision::Interrupted
        );
        assert_eq!(
            exact_wait_poll_decision(false, false, Some(ExactChildWaitState::Running)),
            ExactWaitPollDecision::Retry
        );
    }

    #[test]
    fn wait4_argument_validation_follows_linux_precedence() {
        let valid_bits = [0, 1, 3, 29, 30, 31];
        for bit in valid_bits {
            let options = WaitPidFlag::from_bits_retain((1_u32 << bit) as libc::c_int);
            assert_eq!(validate_wait4_arguments(-1, options), Ok(()), "bit {bit}");
        }

        for bit in (0..u32::BITS).filter(|bit| !valid_bits.contains(bit)) {
            let options = WaitPidFlag::from_bits_retain((1_u32 << bit) as libc::c_int);
            assert_eq!(
                validate_wait4_arguments(-1, options),
                Err(Errno::EINVAL),
                "bit {bit}"
            );
        }

        // Only INT_MIN is refused: every other pid, including the nearest
        // process-group selector, must still reach the wait.
        for pid in [libc::pid_t::MIN + 1, -2, 0, 1, libc::pid_t::MAX] {
            assert_eq!(
                validate_wait4_arguments(pid, WaitPidFlag::empty()),
                Ok(()),
                "pid {pid}"
            );
        }
        assert_eq!(
            validate_wait4_arguments(libc::pid_t::MIN, WaitPidFlag::empty()),
            Err(Errno::ESRCH)
        );
        assert_eq!(
            validate_wait4_arguments(libc::pid_t::MIN, WaitPidFlag::from_bits_retain(0x10)),
            Err(Errno::EINVAL),
            "invalid options must win over the INT_MIN selector"
        );
    }

    #[test]
    fn stale_any_child_preserves_interrupt_until_no_ready_child_remains() {
        let next_child = DetPid::from_raw(200);

        assert!(
            !stale_any_wait_must_interrupt(true, Some(next_child)),
            "another ready child must retain child-ready precedence"
        );
        assert!(
            stale_any_wait_must_interrupt(true, None),
            "a pending signal must interrupt before the wait parks again"
        );
        assert!(!stale_any_wait_must_interrupt(false, None));
    }

    #[test]
    fn ioprio_query_reports_fixed_raw_and_effective_defaults() {
        assert_eq!(virtual_ioprio(IOPRIO_WHO_PROCESS), Ok(0));
        assert_eq!(
            virtual_ioprio(IOPRIO_WHO_PGRP),
            Ok(i64::from(IOPRIO_DEFAULT_EFFECTIVE))
        );
        assert_eq!(
            virtual_ioprio(IOPRIO_WHO_USER),
            Ok(i64::from(IOPRIO_DEFAULT_EFFECTIVE))
        );
        assert_eq!(virtual_ioprio(0), Err(Errno::EINVAL));
        assert_eq!(virtual_ioprio(4), Err(Errno::EINVAL));
    }

    #[test]
    fn waitid_siginfo_canonicalization_clears_only_cpu_accounting() {
        let mut info: libc::siginfo_t = unsafe { std::mem::zeroed() };
        info.si_signo = libc::SIGCHLD;
        info.si_code = libc::CLD_EXITED;
        // SAFETY: This uses the same Linux SIGCHLD layout mirror validated by
        // canonicalize_waitid_siginfo.
        let fields = unsafe {
            &mut (*(std::ptr::addr_of_mut!(info)).cast::<WaitidSiginfoHead>())
                .fields
                .sigchld
        };
        fields.pid = 123;
        fields.uid = 456;
        fields.status = 7;
        fields.utime = 8;
        fields.stime = 9;

        canonicalize_waitid_siginfo(&mut info);

        assert_eq!(info.si_signo, libc::SIGCHLD);
        assert_eq!(info.si_code, libc::CLD_EXITED);
        assert_eq!(unsafe { info.si_pid() }, 123);
        assert_eq!(unsafe { info.si_uid() }, 456);
        assert_eq!(unsafe { info.si_status() }, 7);
        assert_eq!(unsafe { info.si_utime() }, 0);
        assert_eq!(unsafe { info.si_stime() }, 0);
    }

    #[test]
    fn wait_status_rollup_only_accepts_process_termination() {
        assert!(wait_status_is_termination(0));
        assert!(wait_status_is_termination(libc::SIGTERM));
        assert!(!wait_status_is_termination((libc::SIGSTOP << 8) | 0x7f));
        assert!(!wait_status_is_termination(0xffff));

        assert!(waitid_code_is_termination(libc::CLD_EXITED));
        assert!(waitid_code_is_termination(libc::CLD_KILLED));
        assert!(waitid_code_is_termination(libc::CLD_DUMPED));
        assert!(!waitid_code_is_termination(libc::CLD_STOPPED));
        assert!(!waitid_code_is_termination(libc::CLD_CONTINUED));
        assert!(!waitid_code_is_termination(libc::CLD_TRAPPED));
    }

    /// `do_futex` (kernel/futex/syscalls.c): FUTEX_CLOCK_REALTIME only with
    /// FUTEX_WAIT_BITSET, FUTEX_WAIT_REQUEUE_PI or FUTEX_LOCK_PI2, else -ENOSYS;
    /// the removed FUTEX_FD and unknown commands are -ENOSYS; the PI commands are
    /// refused by name; the rest are emulated
    /// (https://github.com/rrnewton/hermit/issues/3958).
    #[test]
    fn futex_commands_are_classified_as_linux_dispatches_them() {
        use FutexCommand::*;
        let private = libc::FUTEX_PRIVATE_FLAG;
        let realtime = libc::FUTEX_CLOCK_REALTIME;
        for cmd in [
            libc::FUTEX_WAIT,
            libc::FUTEX_WAIT_BITSET,
            libc::FUTEX_WAKE,
            libc::FUTEX_WAKE_BITSET,
            libc::FUTEX_REQUEUE,
            libc::FUTEX_CMP_REQUEUE,
            libc::FUTEX_WAKE_OP,
        ] {
            assert_eq!(classify_futex_command(cmd), Served, "cmd {cmd}");
            assert_eq!(classify_futex_command(cmd | private), Served, "cmd {cmd}");
        }
        assert_eq!(
            classify_futex_command(libc::FUTEX_WAIT_BITSET | realtime),
            Served
        );
        for cmd in [
            libc::FUTEX_WAIT,
            libc::FUTEX_WAKE,
            libc::FUTEX_REQUEUE,
            libc::FUTEX_WAKE_OP,
        ] {
            assert_eq!(classify_futex_command(cmd | realtime), NoSys, "cmd {cmd}");
        }
        for cmd in [libc::FUTEX_FD, 14, 99, libc::FUTEX_CMD_MASK] {
            assert_eq!(classify_futex_command(cmd), NoSys, "cmd {cmd}");
        }
        assert_eq!(
            classify_futex_command(libc::FUTEX_LOCK_PI | private),
            PriorityInheritance("FUTEX_LOCK_PI")
        );
        assert_eq!(
            classify_futex_command(libc::FUTEX_LOCK_PI2 | realtime),
            PriorityInheritance("FUTEX_LOCK_PI2")
        );
        assert_eq!(
            classify_futex_command(libc::FUTEX_LOCK_PI | realtime),
            NoSys
        );
        for (cmd, name) in [
            (libc::FUTEX_UNLOCK_PI, "FUTEX_UNLOCK_PI"),
            (libc::FUTEX_TRYLOCK_PI, "FUTEX_TRYLOCK_PI"),
            (libc::FUTEX_WAIT_REQUEUE_PI, "FUTEX_WAIT_REQUEUE_PI"),
            (libc::FUTEX_CMP_REQUEUE_PI, "FUTEX_CMP_REQUEUE_PI"),
        ] {
            assert_eq!(classify_futex_command(cmd), PriorityInheritance(name));
        }
    }

    /// FUTEX_WAKE_OP's encoding, as `futex_atomic_op_inuser` decodes it: 12-bit
    /// signed arguments, the shift flag (with an out-of-range shift masked to
    /// 0..31), and -ENOSYS for an unknown operation or comparison.
    /// An injected probe's futex op, uaddr, uaddr2, val and val3.
    type ProbeArgs = (i32, usize, usize, i32, i32);

    /// A guest standing in for the kernel: each injected probe is recorded and
    /// answered with the next scripted result.
    struct ScriptedProbes {
        answers: std::collections::VecDeque<Result<i64, Errno>>,
        probes: Vec<ProbeArgs>,
    }

    impl ScriptedProbes {
        fn inject(&mut self, probe: syscalls::Futex) -> KeyProbeAnswer<'_> {
            self.probes.push((
                probe.futex_op(),
                probe.uaddr().map_or(0, AddrMut::as_raw),
                probe.uaddr2().map_or(0, AddrMut::as_raw),
                probe.val(),
                probe.val3(),
            ));
            let answer = self
                .answers
                .pop_front()
                .expect("the probe was injected more often than scripted");
            Box::pin(async move { answer })
        }
    }

    fn run_scripted(
        access: KeyAccess,
        answer: Result<i64, Errno>,
    ) -> (SharedKeyProbe, Vec<ProbeArgs>) {
        let mut guest = ScriptedProbes {
            answers: [answer].into_iter().collect(),
            probes: Vec::new(),
        };
        let result = futures::executor::block_on(run_shared_futex_key_probe(
            &mut guest,
            shared_futex_key_probe(0x1000, access),
            access,
            |guest, probe| guest.inject(probe),
        ));
        (result, guest.probes)
    }

    /// The shared-key admission probe is injected exactly once
    /// (https://github.com/rrnewton/hermit/pull/4020). When a signal stops it
    /// before it runs (`Guest::inject` returns ERESTARTSYS, or EINTR), it is not
    /// injected again, because Reverie holds that signal in a single slot that a
    /// second interruption would overwrite, and the interruption is reported as
    /// such: never as an admitted key and never as the guest's errno. The
    /// caller stops the run by name on it. The kernel's own answers keep their
    /// meaning: 0 admits, ENOSYS admits only a WRITE key, EFAULT is the
    /// guest's.
    #[test]
    fn an_interrupted_shared_key_probe_is_never_retried_or_taken_as_an_answer() {
        let read = (libc::FUTEX_REQUEUE, 0x1000, 0x1000, 0, 0);
        let write = (
            libc::FUTEX_WAKE_OP,
            0x1000,
            0x1000,
            0,
            (7 << 28) | (1 << 12),
        );
        for (access, args) in [(KeyAccess::Read, read), (KeyAccess::Write, write)] {
            for errno in [Errno::ERESTARTSYS, Errno::EINTR] {
                assert_eq!(
                    run_scripted(access, Err(errno)),
                    (SharedKeyProbe::Interrupted(errno), vec![args]),
                    "{access:?} {errno}"
                );
            }
            assert_eq!(
                run_scripted(access, Ok(0)),
                (SharedKeyProbe::Admitted, vec![args])
            );
            assert_eq!(
                run_scripted(access, Err(Errno::EFAULT)),
                (SharedKeyProbe::Refused(Errno::EFAULT), vec![args])
            );
        }
        assert_eq!(
            run_scripted(KeyAccess::Read, Err(Errno::ENOSYS)),
            (SharedKeyProbe::Refused(Errno::ENOSYS), vec![read])
        );
        assert_eq!(
            run_scripted(KeyAccess::Write, Err(Errno::ENOSYS)),
            (SharedKeyProbe::Admitted, vec![write])
        );
    }

    #[test]
    fn futex_wake_op_decodes_and_applies_as_linux() {
        let encode = |op: u32, cmp: u32, oparg: u32, cmparg: u32| {
            ((op & 0xf) << 28) | ((cmp & 0xf) << 24) | ((oparg & 0xfff) << 12) | (cmparg & 0xfff)
        };
        let set5_eq0 = FutexWakeOp::decode(encode(FUTEX_OP_SET, FUTEX_OP_CMP_EQ, 5, 0));
        assert_eq!(set5_eq0.apply(0), Some(5));
        assert_eq!(set5_eq0.compare(0), Some(true));
        assert_eq!(set5_eq0.compare(1), Some(false));
        let add_minus1 = FutexWakeOp::decode(encode(FUTEX_OP_ADD, FUTEX_OP_CMP_LT, 0xfff, 0x800));
        assert_eq!(add_minus1.apply(10), Some(9));
        assert_eq!(add_minus1.apply(i32::MIN), Some(i32::MAX));
        assert_eq!(add_minus1.compare(-2048), Some(false));
        assert_eq!(add_minus1.compare(-2049), Some(true));
        let or = FutexWakeOp::decode(encode(FUTEX_OP_OR, FUTEX_OP_CMP_NE, 0b100, 7));
        assert_eq!(or.apply(0b011), Some(0b111));
        assert_eq!(or.compare(7), Some(false));
        let andn = FutexWakeOp::decode(encode(FUTEX_OP_ANDN, FUTEX_OP_CMP_GE, 0b110, 3));
        assert_eq!(andn.apply(0b111), Some(0b001));
        assert_eq!(andn.compare(3), Some(true));
        let xor = FutexWakeOp::decode(encode(FUTEX_OP_XOR, FUTEX_OP_CMP_LE, 0xff, 0));
        assert_eq!(xor.apply(0x0f), Some(0xf0));
        assert_eq!(xor.compare(1), Some(false));
        let shift = FutexWakeOp::decode(encode(
            FUTEX_OP_OPARG_SHIFT | FUTEX_OP_OR,
            FUTEX_OP_CMP_GT,
            3,
            0,
        ));
        assert_eq!(shift.apply(0), Some(8));
        assert_eq!(shift.compare(1), Some(true));
        // A shift of -1 (0xfff) is masked to 31, as Linux does.
        let masked = FutexWakeOp::decode(encode(FUTEX_OP_OPARG_SHIFT | FUTEX_OP_SET, 0, 0xfff, 0));
        assert_eq!(masked.apply(0), Some(i32::MIN));
        let unknown_op = FutexWakeOp::decode(encode(7, FUTEX_OP_CMP_EQ, 1, 0));
        assert!(!unknown_op.has_known_operation());
        assert_eq!(unknown_op.apply(0), None);
        let unknown_cmp = FutexWakeOp::decode(encode(FUTEX_OP_SET, 6, 1, 0));
        assert!(unknown_cmp.has_known_operation());
        assert_eq!(unknown_cmp.compare(0), None);
    }

    #[test]
    fn futex_timeout_units_and_modes_match_linux() {
        let timeout = Timespec {
            tv_sec: 2,
            tv_nsec: 3,
        };
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT, timeout),
            Ok(FutexTimeout::Relative(2_000_000_003))
        );
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT_BITSET, timeout),
            Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
                2_000_000_003
            )))
        );
        // The command bits must be matched after masking off FUTEX_PRIVATE_FLAG
        // (and FUTEX_CLOCK_REALTIME): a private FUTEX_WAIT_BITSET still uses an
        // absolute deadline, and a private FUTEX_WAIT still uses a relative one.
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT_BITSET | libc::FUTEX_PRIVATE_FLAG, timeout),
            Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
                2_000_000_003
            )))
        );
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT | libc::FUTEX_PRIVATE_FLAG, timeout),
            Ok(FutexTimeout::Relative(2_000_000_003))
        );
    }

    #[test]
    fn absolute_futex_timeout_is_rebased_to_logical_time() {
        let logical_now = LogicalTime::from_secs(100);
        let clock_now = LogicalTime::from_secs(5_000);
        let deadline = clock_now + Duration::from_millis(100);
        assert_eq!(
            rebase_absolute_timeout(deadline, clock_now, logical_now),
            logical_now + Duration::from_millis(100)
        );
        assert_eq!(
            rebase_absolute_timeout(
                clock_now - LogicalTime::from_nanos(1),
                clock_now,
                logical_now
            ),
            logical_now
        );
    }

    #[test]
    fn absolute_futex_timeout_detects_host_and_logical_clock_domains() {
        let host_monotonic_now = LogicalTime::from_secs(374_766);
        let logical_now = LogicalTime::from_secs(1_640_995_199);
        let delta = Duration::from_millis(100);

        assert!(absolute_timeout_uses_host_clock(
            host_monotonic_now + delta,
            host_monotonic_now,
            logical_now
        ));
        assert!(!absolute_timeout_uses_host_clock(
            logical_now + delta,
            host_monotonic_now,
            logical_now
        ));

        let host_realtime_now = LogicalTime::from_secs(1_785_142_800);
        assert!(absolute_timeout_uses_host_clock(
            host_realtime_now + delta,
            host_realtime_now,
            logical_now
        ));
    }

    /// Linux's `futex_init_timeout` refuses only an invalid timespec, and
    /// `timespec64_to_ktime` clamps a valid one beyond `KTIME_MAX` instead of
    /// refusing it (Codex re-check of https://github.com/rrnewton/hermit/pull/4061).
    #[test]
    fn futex_timeout_clamps_a_huge_valid_timespec_like_ktime() {
        let huge = Timespec {
            tv_sec: libc::time_t::MAX,
            tv_nsec: 0,
        };
        assert!(futex_timespec_is_valid(huge));
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT, huge),
            Ok(FutexTimeout::Relative(FUTEX_KTIME_MAX_NANOS))
        );
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT_BITSET, huge),
            Ok(FutexTimeout::Absolute(LogicalTime::from_nanos(
                FUTEX_KTIME_MAX_NANOS
            )))
        );
        let at_ktime_sec_max = Timespec {
            tv_sec: (FUTEX_KTIME_MAX_NANOS / 1_000_000_000) as libc::time_t,
            tv_nsec: 0,
        };
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT, at_ktime_sec_max),
            Ok(FutexTimeout::Relative(FUTEX_KTIME_MAX_NANOS))
        );
        let just_below = Timespec {
            tv_sec: (FUTEX_KTIME_MAX_NANOS / 1_000_000_000 - 1) as libc::time_t,
            tv_nsec: 999_999_999,
        };
        assert_eq!(
            parse_futex_timeout(libc::FUTEX_WAIT, just_below),
            Ok(FutexTimeout::Relative(
                (FUTEX_KTIME_MAX_NANOS / 1_000_000_000) * 1_000_000_000 - 1
            ))
        );
        for invalid in [(-1, 0), (0, -1), (0, 1_000_000_000)] {
            assert!(!futex_timespec_is_valid(Timespec {
                tv_sec: invalid.0,
                tv_nsec: invalid.1,
            }));
        }
    }

    #[test]
    fn futex_timeout_rejects_invalid_timespecs() {
        assert_eq!(
            parse_futex_timeout(
                libc::FUTEX_WAIT,
                Timespec {
                    tv_sec: -1,
                    tv_nsec: 0,
                },
            ),
            Err(Errno::EINVAL)
        );
        assert_eq!(
            parse_futex_timeout(
                libc::FUTEX_WAIT_BITSET,
                Timespec {
                    tv_sec: 0,
                    tv_nsec: 1_000_000_000,
                },
            ),
            Err(Errno::EINVAL)
        );
    }

    // The expectations below are not guesses at the kernel's behaviour: each is
    // a row measured natively on this host (Linux 6.19, x86-64) with a raw
    // `sched_setattr` probe. Where a row differs from what the handler used to
    // do, the difference is called out.

    /// A well-formed VER0 SCHED_OTHER descriptor, as the decoded fields.
    fn plain_attr() -> SchedAttrFields {
        SchedAttrFields {
            policy: 0,
            sched_flags: 0,
            priority: 0,
            runtime: 0,
            deadline: 0,
            period: 0,
        }
    }

    #[test]
    fn size_zero_is_a_well_formed_ver0_request() {
        // ABI compatibility quirk: `if (!size) size = SCHED_ATTR_SIZE_VER0;`.
        // Measured native: ret 0. PR #2288 returned E2BIG.
        assert_eq!(sched_attr_effective_size(0), Ok(SCHED_ATTR_SIZE_VER0));
    }

    #[test]
    fn size_below_ver0_or_past_a_page_is_too_big() {
        // Measured native: size 1, 47 and 4097 all give E2BIG.
        assert_eq!(sched_attr_effective_size(1), Err(()));
        assert_eq!(sched_attr_effective_size(SCHED_ATTR_SIZE_VER0 - 1), Err(()));
        assert_eq!(sched_attr_effective_size(SCHED_ATTR_MAX_SIZE + 1), Err(()));
    }

    #[test]
    fn size_from_ver0_through_one_page_is_accepted_unchanged() {
        // Measured native: 48, 56, 57 and 4096 all give ret 0.
        for size in [
            SCHED_ATTR_SIZE_VER0,
            SCHED_ATTR_SIZE_VER1,
            SCHED_ATTR_SIZE_VER1 + 1,
            SCHED_ATTR_MAX_SIZE,
        ] {
            assert_eq!(sched_attr_effective_size(size), Ok(size), "size {}", size);
        }
    }

    #[test]
    fn sched_ext_is_a_valid_policy_and_sched_iso_is_not() {
        // Measured native: policy 7 gives ret 0; policy 4 gives EINVAL. PR
        // #2288 refused 7.
        assert!(is_valid_sched_policy(SCHED_EXT), "SCHED_EXT is accepted");
        assert!(!is_valid_sched_policy(4), "SCHED_ISO is reserved");
        for policy in [0, 1, 2, 3, 5, 6] {
            assert!(is_valid_sched_policy(policy), "policy {}", policy);
        }
        // Measured native: policy 8 and 99 both give EINVAL.
        for policy in [8, 99] {
            assert!(!is_valid_sched_policy(policy), "policy {}", policy);
        }
    }

    // ---- which side of the pid lookup each rule falls on --------------------
    // The kernel resolves the pid between `sched_setattr()` and
    // `__sched_setscheduler()`, so a request naming a nonexistent pid reports
    // ESRCH for every rule on the far side and its own errno for every rule on
    // the near side. Measured natively with pid 0x3fffffff:
    //     bad sched_flags    -> ESRCH    (far side)
    //     bad priority       -> ESRCH    (far side)
    //     negative policy    -> EINVAL   (near side)
    //     util-clamp size 48 -> EINVAL   (near side)
    // The split between the two functions below is exactly that boundary.

    #[test]
    fn util_clamp_size_rule_is_decided_before_the_pid_lookup() {
        // Measured native: EINVAL even with a nonexistent pid.
        let mut attr = plain_attr();
        attr.sched_flags = SCHED_FLAG_UTIL_CLAMP_MIN;
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &attr),
            Err(Errno::EINVAL)
        );
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER1, &attr),
            Ok(())
        );
        // ...and it is not re-decided on the far side.
        assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
    }

    #[test]
    fn a_negative_policy_is_decided_before_the_pid_lookup() {
        // Measured native: EINVAL even with a nonexistent pid, and EINVAL with
        // KEEP_POLICY too -- the signed test sits above the substitution.
        let mut attr = plain_attr();
        attr.policy = 0x8000_0000;
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &attr),
            Err(Errno::EINVAL)
        );
        attr.sched_flags = SCHED_FLAG_KEEP_POLICY;
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &attr),
            Err(Errno::EINVAL),
            "KEEP_POLICY must not hide a negative policy"
        );
    }

    #[test]
    fn policy_and_flag_rules_are_decided_after_the_pid_lookup() {
        // Measured native: with a nonexistent pid both of these report ESRCH,
        // so neither may be decided on the near side.
        let mut bad_policy = plain_attr();
        bad_policy.policy = 99;
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &bad_policy),
            Ok(()),
            "an undefined policy must survive the near side so ESRCH can win"
        );
        assert_eq!(
            validate_sched_attr_after_lookup(&bad_policy),
            Err(Errno::EINVAL)
        );

        let mut bad_flag = plain_attr();
        bad_flag.sched_flags = 0x80;
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &bad_flag),
            Ok(()),
            "an undefined sched_flags bit must survive the near side"
        );
        assert_eq!(
            validate_sched_attr_after_lookup(&bad_flag),
            Err(Errno::EINVAL)
        );
    }

    #[test]
    fn keep_policy_makes_the_policy_field_irrelevant() {
        // Measured native: policy 99 alone is EINVAL, but policy 99 with
        // SCHED_FLAG_KEEP_POLICY is ret 0 -- the kernel overwrites the field
        // with SETPARAM_POLICY and never runs valid_policy(). PR #2288 refused
        // it.
        let mut attr = plain_attr();
        attr.policy = 99;
        assert_eq!(validate_sched_attr_after_lookup(&attr), Err(Errno::EINVAL));
        attr.sched_flags = SCHED_FLAG_KEEP_POLICY;
        assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
    }

    #[test]
    fn defined_sched_flags_bits_are_accepted_and_undefined_ones_are_not() {
        // Measured native: sched_flags 0x80 gives EINVAL; RESET_ON_FORK,
        // KEEP_PARAMS, KEEP_ALL, RECLAIM and DL_OVERRUN are all ret 0 at
        // size 48. PR #2288 ignored sched_flags entirely.
        let mut attr = plain_attr();
        attr.sched_flags = 0x80;
        assert_eq!(validate_sched_attr_after_lookup(&attr), Err(Errno::EINVAL));
        for flag in [
            SCHED_FLAG_RESET_ON_FORK,
            SCHED_FLAG_RECLAIM,
            SCHED_FLAG_DL_OVERRUN,
            SCHED_FLAG_KEEP_PARAMS,
            SCHED_FLAG_KEEP_POLICY,
        ] {
            let mut ok = plain_attr();
            ok.sched_flags = flag;
            assert_eq!(
                validate_sched_attr_after_lookup(&ok),
                Ok(()),
                "flag {:#x}",
                flag
            );
        }
        // The whole mask at once is refused on the NEAR side, and for the
        // util-clamp size reason rather than the flag-validity one: measured
        // native EINVAL at size 48.
        let mut all = plain_attr();
        all.sched_flags = SCHED_FLAG_ALL;
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER0, &all),
            Err(Errno::EINVAL)
        );
        assert_eq!(
            validate_sched_attr_before_lookup(SCHED_ATTR_SIZE_VER1, &all),
            Ok(())
        );
        assert_eq!(validate_sched_attr_after_lookup(&all), Ok(()));
    }

    #[test]
    fn priority_must_agree_with_the_policy() {
        // The kernel states this as `rt_policy(policy) != (prio != 0)`.
        // Measured native, all EINVAL: OTHER/BATCH/IDLE with priority 1, and
        // FIFO/RR with priority 0. PR #2288 accepted every one of them.
        for policy in [0u32, 3, 5] {
            let mut attr = plain_attr();
            attr.policy = policy;
            attr.priority = 1;
            assert_eq!(
                validate_sched_attr_after_lookup(&attr),
                Err(Errno::EINVAL),
                "policy {} with priority 1",
                policy
            );
            attr.priority = 0;
            assert_eq!(
                validate_sched_attr_after_lookup(&attr),
                Ok(()),
                "policy {} with priority 0",
                policy
            );
        }
        for policy in [SCHED_FIFO, SCHED_RR] {
            let mut attr = plain_attr();
            attr.policy = policy;
            attr.priority = 0;
            assert_eq!(
                validate_sched_attr_after_lookup(&attr),
                Err(Errno::EINVAL),
                "rt policy {} with priority 0",
                policy
            );
            // A real-time priority is accepted here; whether the *caller* may
            // ask for it is EPERM, which depends on privilege and is not
            // emulated.
            attr.priority = 1;
            assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
        }
    }

    #[test]
    fn a_priority_past_max_rt_prio_is_refused() {
        // Measured native: FIFO with priority 100 is EINVAL rather than the
        // EPERM that priorities 1..=99 give, so the range test runs first.
        let mut attr = plain_attr();
        attr.policy = SCHED_FIFO;
        attr.priority = MAX_RT_PRIO;
        assert_eq!(validate_sched_attr_after_lookup(&attr), Err(Errno::EINVAL));
        attr.priority = MAX_RT_PRIO - 1;
        assert_eq!(validate_sched_attr_after_lookup(&attr), Ok(()));
    }

    #[test]
    fn deadline_parameters_are_checked() {
        // Measured native: SCHED_DEADLINE with all-zero parameters is EINVAL,
        // and with runtime > deadline is EINVAL. PR #2288 accepted both.
        let mut zeroed = plain_attr();
        zeroed.policy = SCHED_DEADLINE;
        assert_eq!(
            validate_sched_attr_after_lookup(&zeroed),
            Err(Errno::EINVAL),
            "all-zero deadline parameters"
        );

        let mut inverted = plain_attr();
        inverted.policy = SCHED_DEADLINE;
        inverted.runtime = 90_000_000;
        inverted.deadline = 30_000_000;
        inverted.period = 30_000_000;
        assert_eq!(
            validate_sched_attr_after_lookup(&inverted),
            Err(Errno::EINVAL),
            "runtime > deadline"
        );

        // Sane parameters pass the ABI rules. Natively this host answers EPERM,
        // which is a privilege question rather than an ABI one.
        let mut sane = plain_attr();
        sane.policy = SCHED_DEADLINE;
        sane.runtime = 10_000_000;
        sane.deadline = 30_000_000;
        sane.period = 30_000_000;
        assert_eq!(validate_sched_attr_after_lookup(&sane), Ok(()));
    }

    #[test]
    fn deadline_parameter_edges_follow_checkparam_dl() {
        // deadline == 0 is refused whatever else is set.
        assert!(!deadline_params_are_valid(1 << 20, 0, 0));
        // runtime below the DL_SCALE truncation floor (1 << 10) is refused.
        assert!(!deadline_params_are_valid((1 << 10) - 1, 1 << 20, 1 << 20));
        assert!(deadline_params_are_valid(1 << 10, 1 << 20, 1 << 20));
        // The MSB is reserved on both deadline and period.
        assert!(!deadline_params_are_valid(1 << 20, 1 << 63, 0));
        assert!(!deadline_params_are_valid(1 << 20, 1 << 20, 1 << 63));
        // A zero period means "same as the deadline", so this is runtime <=
        // deadline <= deadline and is accepted.
        assert!(deadline_params_are_valid(1 << 20, 1 << 21, 0));
        // deadline > period is refused.
        assert!(!deadline_params_are_valid(1 << 20, 1 << 22, 1 << 21));
    }

    #[test]
    fn the_size_written_back_on_refusal_is_the_kernel_struct_not_the_libc_mirror() {
        // Measured native: every E2BIG row leaves uattr->size holding 56, not
        // 48. Deriving that from the libc crate's `sched_attr` would report 48
        // and tell the guest to retry with a size the kernel already accepted.
        assert_eq!(SCHED_ATTR_KERNEL_SIZE, 56);
        assert_eq!(std::mem::size_of::<libc::sched_attr>(), 48);
        assert_ne!(
            SCHED_ATTR_KERNEL_SIZE as usize,
            std::mem::size_of::<libc::sched_attr>()
        );
    }

    /// A `MemoryAccess` whose readable region ends at `readable_len`, recording
    /// every read length it is asked for.
    struct BoundedMemory {
        bytes: Vec<u8>,
        readable_len: usize,
        reads: std::cell::RefCell<Vec<usize>>,
    }

    impl reverie::syscalls::MemoryAccess for BoundedMemory {
        fn read_vectored(
            &self,
            read_from: &[std::io::IoSlice<'_>],
            write_to: &mut [std::io::IoSliceMut<'_>],
        ) -> Result<usize, Errno> {
            let start = read_from[0].as_ptr() as usize;
            let want = read_from.iter().map(|slice| slice.len()).sum::<usize>();
            self.reads.borrow_mut().push(want);
            if start >= self.readable_len {
                return Err(Errno::EFAULT);
            }
            let avail = (self.readable_len - start).min(want);
            let mut copied = 0;
            for out in write_to.iter_mut() {
                if copied == avail {
                    break;
                }
                let take = out.len().min(avail - copied);
                out[..take].copy_from_slice(&self.bytes[start + copied..start + copied + take]);
                copied += take;
            }
            Ok(copied)
        }

        fn write_vectored(
            &mut self,
            _read_from: &[std::io::IoSlice<'_>],
            _write_to: &mut [std::io::IoSliceMut<'_>],
        ) -> Result<usize, Errno> {
            unimplemented!("this fixture never writes")
        }
    }

    fn bounded(bytes: Vec<u8>, readable_len: usize) -> BoundedMemory {
        BoundedMemory {
            bytes,
            readable_len,
            reads: std::cell::RefCell::new(Vec::new()),
        }
    }

    fn tail_base() -> AddrMut<'static, u8> {
        AddrMut::<u8>::from_raw(1).expect("nonzero base")
    }

    /// ITEM 2 REGRESSION, ORDERING HALF: a non-zero byte BEFORE an unreadable
    /// page is E2BIG, not EFAULT.
    ///
    /// `check_zeroed_user` scans forward and stops at the first thing it finds,
    /// so it never reaches the fault. Reading the whole tail up front and
    /// judging afterwards turns this into EFAULT and changes the errno the
    /// guest sees.
    #[test]
    fn a_nonzero_byte_before_a_fault_is_e2big_not_efault() {
        let off = SCHED_ATTR_KERNEL_SIZE as usize;
        let mut bytes = vec![0u8; off + 4096];
        bytes[off + 1] = 0xAA; // non-zero, well before the boundary
        let readable = off + 512; // everything past this faults
        let memory = bounded(bytes, readable);
        assert_eq!(
            scan_tail_is_zeroed(&memory, tail_base(), off, 4096),
            TailVerdict::NotZeroed,
            "a non-zero byte the scan reaches first must win over a later fault"
        );
    }

    /// ITEM 2 REGRESSION, the other side of the same order: a fault with
    /// nothing non-zero before it is EFAULT.
    #[test]
    fn a_fault_before_any_nonzero_byte_is_efault() {
        let off = SCHED_ATTR_KERNEL_SIZE as usize;
        let bytes = vec![0u8; off + 4096];
        let memory = bounded(bytes, off + 512);
        assert_eq!(
            scan_tail_is_zeroed(&memory, tail_base(), off, 4096),
            TailVerdict::Faulted,
            "an unreadable byte reached before anything non-zero must be EFAULT"
        );
    }

    /// ITEM 2 REGRESSION, PROTECTION HALF: never issue a read of eight bytes or
    /// fewer.
    ///
    /// safeptrace serves those through `PTRACE_PEEKDATA`, which reads a whole
    /// aligned word and bypasses guest page protections, so a small tail would
    /// be readable under Hermit where Linux reports EFAULT. The assertion is on
    /// the READ LENGTHS ASKED FOR, because that is the mechanism; the returned
    /// verdict cannot show it.
    #[test]
    fn tail_reads_are_never_small_enough_to_bypass_guest_protection() {
        let off = SCHED_ATTR_KERNEL_SIZE as usize;
        for tail_len in 1..=16usize {
            let bytes = vec![0u8; off + tail_len + 64];
            let memory = bounded(bytes, off + tail_len + 64);
            assert_eq!(
                scan_tail_is_zeroed(&memory, tail_base(), off, tail_len),
                TailVerdict::AllZero
            );
            let reads = memory.reads.borrow().clone();
            assert!(!reads.is_empty(), "tail_len {tail_len} issued no read");
            for length in reads {
                assert!(
                    length > std::mem::size_of::<u64>(),
                    "tail_len {tail_len} issued a {length}-byte read, which safeptrace \
                     serves with PTRACE_PEEKDATA and which therefore bypasses guest \
                     page protections"
                );
            }
        }
    }

    /// ITEM 3 REGRESSION: KEEP_POLICY reuses the CURRENT policy; it does not
    /// switch the policy-dependent rules off.
    ///
    /// Detcore's current policy is the one `handle_sched_getattr` reports for
    /// every thread, SCHED_OTHER, and a non-real-time policy requires priority
    /// 0. Skipping the rules accepted this.
    #[test]
    fn keep_policy_validates_against_the_virtual_current_policy() {
        let attr = SchedAttrFields {
            policy: 99, // ignored under KEEP_POLICY, and deliberately invalid
            sched_flags: SCHED_FLAG_KEEP_POLICY,
            priority: 1,
            runtime: 0,
            deadline: 0,
            period: 0,
        };
        assert_eq!(
            validate_sched_attr_after_lookup(&attr),
            Err(Errno::EINVAL),
            "priority 1 under the virtual SCHED_OTHER current policy must be refused"
        );

        // The companion that must keep passing: the ignored policy field really
        // is ignored, so the same request at priority 0 is accepted.
        let ok = SchedAttrFields {
            priority: 0,
            ..attr
        };
        assert_eq!(
            validate_sched_attr_after_lookup(&ok),
            Ok(()),
            "KEEP_POLICY must still ignore the policy field itself"
        );
    }

    /// ITEM 3 COHERENCE: the substituted policy is the one the guest can
    /// actually observe, so the two sites cannot drift apart silently.
    #[test]
    fn the_virtual_current_policy_is_what_sched_getattr_reports() {
        assert_eq!(
            VIRTUAL_CURRENT_POLICY,
            libc::SCHED_OTHER as u32,
            "handle_sched_getattr writes SCHED_OTHER into sched_policy for every thread; \
             KEEP_POLICY must substitute that same value"
        );
    }
}

/// The real uid of thread `tid`, from `/proc/<tid>/status`.
pub(crate) fn real_uid(tid: i32) -> Option<u32> {
    let status = std::fs::read_to_string(format!("/proc/{tid}/status")).ok()?;
    status
        .lines()
        .find_map(|line| line.strip_prefix("Uid:"))?
        .split_whitespace()
        .next()?
        .parse()
        .ok()
}

/// The 8-byte field at `offset` of a clone3 call's `struct clone_args`,
/// which may be any size the kernel accepts from 64 bytes (its first
/// version) up: read alone, so that a shorter struct at the end of a mapping
/// is read as the kernel reads it. `None` for a size below 64, a field past
/// the size, or an unreadable field.
fn clone3_field<M: MemoryAccess>(
    memory: &M,
    clone: &syscalls::Clone3,
    offset: usize,
) -> Option<u64> {
    const CLONE_ARGS_SIZE_VER0: usize = 64;
    if clone.size() < CLONE_ARGS_SIZE_VER0 || offset + 8 > clone.size() {
        return None;
    }
    let base = clone.args()?.as_raw();
    memory
        .read_value(Addr::<u64>::from_raw(base + offset)?)
        .ok()
}

/// Where a `CLONE_PIDFD` clone writes the new process's pidfd (`clone`'s
/// `parent_tid` argument, or `clone3`'s `pidfd` field), for a child Detcore
/// tracks the pidfd of (`Detcore::register_clone_pidfd`): one that shares
/// neither the caller's memory nor its descriptor table and is no vfork
/// child. Such a child could rewrite the word, or close or replace the
/// descriptor, before the caller tracks it, and a vfork parent resumes, and
/// tracks it, only after its child is done, while a sibling may already
/// use the pidfd; Detcore leaves those pidfds untracked, as before
/// https://github.com/rrnewton/hermit/issues/4430
/// (https://github.com/rrnewton/hermit/issues/4443).
fn clone_pidfd_address<M: MemoryAccess>(
    clone_family: &syscalls::family::CloneFamily,
    memory: &M,
    flags: CloneFlags,
) -> Option<usize> {
    let (flags, address) = match clone_family {
        syscalls::family::CloneFamily::Clone(clone) => {
            (flags, clone.ptid().map(|ptid| ptid.as_raw()))
        }
        syscalls::family::CloneFamily::Clone3(clone) => (
            flags,
            Some(usize::try_from(clone3_field(memory, clone, 8)?).ok()?),
        ),
        #[cfg(not(target_arch = "aarch64"))]
        _ => return None,
    };
    if !flags.contains(CloneFlags::CLONE_PIDFD)
        || flags
            .intersects(CloneFlags::CLONE_VM | CloneFlags::CLONE_FILES | CloneFlags::CLONE_VFORK)
    {
        return None;
    }
    address
}