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

use std::num::NonZeroUsize;
use std::time::Duration;

use async_trait::async_trait;
use reverie::Errno;
use reverie::Error;
use reverie::Guest;
use reverie::MaskChangeRefused;
use reverie::Stack;
use reverie::syscalls;
use reverie::syscalls::Addr;
use reverie::syscalls::AddrMut;
use reverie::syscalls::Displayable;
use reverie::syscalls::MapFlags;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::ProtFlags;
use reverie::syscalls::Syscall;
use reverie::syscalls::SyscallInfo;
use reverie::syscalls::Sysno;
use reverie::syscalls::Timespec;
use reverie::syscalls::WaitPidFlag;

use crate::fd::FdType;
use crate::record_or_replay::RecordOrReplay;
use crate::resources::ExternalOpId;
use crate::resources::Permission;
use crate::resources::ResourceID;
use crate::resources::Resources;
use crate::syscalls::threads::BlockedWaitSignalError;
use crate::syscalls::threads::KERNEL_SIGSET_SIZE;
use crate::syscalls::threads::KernelSigaction;
use crate::syscalls::threads::KernelSignalState;
use crate::syscalls::threads::KernelSigset;
use crate::syscalls::threads::SignalQueue;
use crate::syscalls::threads::WaitSignalDisposition;
use crate::syscalls::threads::block_signals_for_disposition;
use crate::syscalls::threads::blocked_signal_mask;
use crate::syscalls::threads::kernel_sigset_bit;
use crate::syscalls::threads::read_thread_wait_signal_state;
use crate::syscalls::threads::read_wait_signal_state;
use crate::syscalls::threads::restore_signals_after_disposition;
use crate::syscalls::threads::wait_signal_disposition;
use crate::tool_global::ResumeStatus;
use crate::tool_global::host_timed_signals;
use crate::tool_global::resource_request;
use crate::tool_global::thread_observe_time;
use crate::tool_global::trace_schedevent;
use crate::tool_local::Detcore;
use crate::tool_local::finish_partial_record_or_replay_write;
use crate::types::DetTid;
use crate::types::LogicalTime;
use crate::types::OpenFileId;
use crate::types::SchedEvent;
use crate::types::SyscallPhase;

impl<T: RecordOrReplay> Detcore<T> {
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#2373)
    /// Apply the established unsupported-syscall refusal policy to a *supported*
    /// syscall whose particular operation Detcore cannot serve deterministically.
    ///
    /// This exists so such a refusal cannot invent its own policy. Three separate
    /// config knobs govern how a fail-closed run dies -- `shutdown_on_unsupported_syscall`
    /// (hard `exit(1)` through `unrecoverable_shutdown`), `exit_on_unsupported_syscall`
    /// (a typed `UnsupportedSyscallError` the backend terminates on without
    /// unwinding), and the `panic!` fallback -- and `Detcore::handle_unsupported_syscall`
    /// consults all three in that order. The normal `hermit run` CLI happens to set
    /// `shutdown_on_unsupported_syscall = panic_on_unsupported_syscalls`, so reading
    /// only the latter looks equivalent, but that coupling is a CLI default, not an
    /// invariant: an embedder that sets just `exit_on_unsupported_syscall` would get a
    /// process-wide `exit(1)` from a bespoke call site where the standard path returns
    /// a catchable error.
    ///
    /// When the run is *not* fail-closed, the caller's `fallback` errno is returned,
    /// because the operation itself is legal and the guest is entitled to a normal
    /// failure code (`handle_unsupported_syscall` passes the call through instead,
    /// which is not available here -- passing through is precisely the thing the
    /// caller has determined it cannot do).
    pub(crate) async fn refuse_unserviceable_operation<G: Guest<Self>>(
        &self,
        guest: &mut G,
        sysno: reverie::syscalls::Sysno,
        fallback: Errno,
    ) -> Result<i64, Error> {
        refuse_unserviceable_operation_with(
            guest,
            self.cfg.panic_on_unsupported_syscalls,
            sysno,
            fallback,
            None,
        )
        .await
    }

    /// [`Self::refuse_unserviceable_operation`], with the backend's own
    /// description of why it cannot perform the operation, which a
    /// fail-closed refusal reports as its reason.
    pub(crate) async fn refuse_unserviceable_operation_because<G: Guest<Self>>(
        &self,
        guest: &mut G,
        sysno: reverie::syscalls::Sysno,
        fallback: Errno,
        reason: &str,
    ) -> Result<i64, Error> {
        refuse_unserviceable_operation_with(
            guest,
            self.cfg.panic_on_unsupported_syscalls,
            sysno,
            fallback,
            Some(reason),
        )
        .await
    }

    /// Record or replay a BLOCKING syscall without stalling the current thread (and thus
    /// deadlocking).  This uses a protocol of an extra resource request before/after the
    /// syscall to inform the scheduler that the thread is leaving/rejoining the runnable
    /// threads pool.
    ///
    /// This is only valid to use (1) in hermit record/replay modes, or (2)
    /// when we're in "hermit run", but we're NOT sequentializing threads, because in
    /// that case it's ok to use the blocking versions of system calls.
    pub async fn record_or_replay_blocking<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
    ) -> Result<i64, Error> {
        self.record_or_replay_blocking_with_mask(guest, call, None)
            .await
    }

    /// `record_or_replay_blocking` for a call that sleeps under its own
    /// temporary signal mask (`ppoll`, `pselect6`, or an `rt_sigsuspend` that
    /// finds a signal already pending): `blocked_signal_mask` is the mask the
    /// kernel installs for the call, read in this thread's turn
    /// (`Resources::blocked_signal_mask`). `None` means the call sleeps under
    /// the thread's own mask.
    pub async fn record_or_replay_blocking_with_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
        blocked_signal_mask: Option<u64>,
    ) -> Result<i64, Error> {
        let dettid = guest.thread_state().dettid;
        let op_id = ExternalOpId::new(dettid, guest.thread_state().stats.syscall_count);
        self.record_or_replay_blocking_resource(
            guest,
            call,
            ResourceID::BlockingExternalIO(op_id),
            blocked_signal_mask,
            false,
        )
        .await
    }

    /// `record_or_replay_blocking` for a call that sleeps under a temporary
    /// mask its turn cannot vouch for (`Resources::blocked_signal_mask_unknown`):
    /// the scheduler records no sleeping mask for it.
    pub async fn record_or_replay_blocking_with_unknown_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
    ) -> Result<i64, Error> {
        self.record_or_replay_blocking_with_unknown_mask_then(guest, call, |_| {})
            .await
    }

    /// `record_or_replay_blocking_with_unknown_mask`, running `after_grant` once
    /// the scheduler has granted the call and before it runs: after any
    /// sibling that ran while the request waited. In replay nothing else runs
    /// until the served call finishes; in recording a sibling turn may still
    /// run before the call enters the kernel.
    pub async fn record_or_replay_blocking_with_unknown_mask_then<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
        after_grant: impl FnOnce(&mut G),
    ) -> Result<i64, Error> {
        let dettid = guest.thread_state().dettid;
        let op_id = ExternalOpId::new(dettid, guest.thread_state().stats.syscall_count);
        self.record_or_replay_blocking_resource_then(
            guest,
            call,
            ResourceID::BlockingExternalIO(op_id),
            None,
            true,
            after_grant,
        )
        .await
    }

    /// Execute the real `rt_sigsuspend` outside the runnable set while preserving
    /// its signal-only completion condition for the scheduler. `temporary_mask`
    /// is the mask the call sleeps under, as `Resources::blocked_signal_mask`
    /// describes.
    pub async fn record_or_replay_rt_sigsuspend<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::RtSigsuspend,
        temporary_mask: u64,
    ) -> Result<i64, Error> {
        let dettid = guest.thread_state().dettid;
        let op_id = ExternalOpId::new(dettid, guest.thread_state().stats.syscall_count);
        let result = self
            .record_or_replay_blocking_resource(
                guest,
                call.into(),
                ResourceID::BlockingRtSigsuspend(op_id),
                Some(temporary_mask),
                false,
            )
            .await;
        // Linux's rt_sigsuspend ends only with ERESTARTNOHAND, which a handler
        // turns into EINTR and never restarts. The host call reports
        // ERESTARTSYS instead when the signal reached the thread before the
        // call ran, which depends on host timing; under SA_RESTART the guest
        // then slept again where Linux returns
        // (https://github.com/rrnewton/hermit/issues/4320). This fixes the
        // result and the restart only: in that case the call never ran, so
        // the kernel never installed its temporary mask, and the handler runs
        // under the thread's pre-call mask, still a host-timed difference when
        // the two masks differ (https://github.com/rrnewton/hermit/issues/4338).
        match result {
            Err(Error::Errno(Errno::ERESTARTSYS)) => Err(Errno::ERESTARTNOHAND.into()),
            other => other,
        }
    }

    async fn record_or_replay_blocking_resource<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
        blocking_resource: ResourceID,
        blocked_signal_mask: Option<u64>,
        blocked_signal_mask_unknown: bool,
    ) -> Result<i64, Error> {
        self.record_or_replay_blocking_resource_then(
            guest,
            call,
            blocking_resource,
            blocked_signal_mask,
            blocked_signal_mask_unknown,
            |_| {},
        )
        .await
    }

    /// `record_or_replay_blocking_resource`, running `after_grant` between the
    /// grant and the call.
    async fn record_or_replay_blocking_resource_then<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
        blocking_resource: ResourceID,
        blocked_signal_mask: Option<u64>,
        blocked_signal_mask_unknown: bool,
        after_grant: impl FnOnce(&mut G),
    ) -> Result<i64, Error> {
        // A host wait on descriptors that may include a timerfd on virtual
        // time would never see it readable (`crate::syscalls::timerfd`).
        crate::syscalls::timerfd::refuse_host_wait_for(guest, call);
        let dettid = guest.thread_state().dettid;
        let op_id = match &blocking_resource {
            ResourceID::BlockingExternalIO(op_id) | ResourceID::BlockingRtSigsuspend(op_id) => {
                *op_id
            }
            _ => unreachable!("blocking syscall helper requires a blocking resource"),
        };
        // Replay serves some of these calls from the recording, so they cannot block.
        // They still run in the background, as they did during recording, so that the
        // schedule matches the recording's. The flag makes the scheduler commit no
        // other turn until they finish. Otherwise a stand-in descriptor that replay
        // creates for an accepted connection could take its slot before or after
        // another thread's open or close, and which of these calls had finished, and
        // so the order in which they rejoin, would depend on host timing
        // (https://github.com/rrnewton/hermit/pull/3871).
        let replay_served_from_log = self.cfg.replaying
            && (replay_serves_blocking_call_from_log(call)
                || replay_serves_socket_call_from_log(guest, call));
        // Internal-vs-external fd classification happens at the call sites that hold the
        // typed, nonblockize-able syscall (see execute_nonblockable_fd_syscall):
        // container-internal pipes are routed to the InternalIOPolling nonblockize-retry
        // path and must NOT reach this external-blocking protocol. BlockingExternalIO
        // deschedules the thread to run in the background and rejoin nondeterministically,
        // which is unsafe for a pipe whose reader and writer are interdependent -- doing
        // so is the root cause of the record/replay pipe deadlock. The remaining callers
        // (external poll, wait4) are external by construction (their fd is not a single
        // extractable internal pipe). Guard the invariant in debug builds while the
        // deterministic scheduler is active. With thread sequentialization disabled,
        // resource requests are no-ops and internal pipes intentionally use a blocking
        // host syscall, as documented by this method.
        debug_assert!(
            !self.cfg.sequentialize_threads || !syscall_targets_internal_fd(guest, call),
            "record_or_replay_blocking (BlockingExternalIO) reached for an internal fd \
             (pipe or socketpair endpoint) on syscall {}; internal fds must use the \
             InternalIOPolling path",
            call.name()
        );
        {
            let mut rsrcs = Resources::new(dettid);
            // With sequentialization enabled, only truly EXTERNAL endpoints reach here.
            // Without it, resource_request is a no-op and internal fds may block directly.
            rsrcs.reads_signalfd = matches!(blocking_resource, ResourceID::BlockingExternalIO(_))
                && call_reads_signalfd(guest, call);
            if matches!(blocking_resource, ResourceID::BlockingExternalIO(_)) {
                rsrcs.read_fd =
                    read_family_fd(call).map(|fd| (guest.thread_state().files_id(), fd));
            }
            rsrcs.insert(blocking_resource, Permission::RW);
            rsrcs.fyi(call.name());
            rsrcs.blocked_signal_mask = blocked_signal_mask;
            rsrcs.blocked_signal_mask_unknown = blocked_signal_mask_unknown;
            rsrcs.replay_served_from_log = replay_served_from_log;
            resource_request(guest, rsrcs).await;
        }
        after_grant(guest);
        // Race probes (docs/RACE_PROBES.md) may delay the call or its rejoin;
        // both are host timing this protocol already treats as nondeterministic.
        crate::race_probe::delay(crate::race_probe::BG_PRE_CALL).await;
        tracing::trace!(
            "Guest proceeding to execute potentially blocking call {}...",
            call.name()
        );
        let res = self
            .record_or_replay_preserving_tool_errors(guest, call)
            .await;
        crate::race_probe::delay(crate::race_probe::BG_POST_CALL).await;
        // N.B. BlockingExternalIO is a "oneshot" resource, so no need to release
        // explicitly here:
        {
            let mut rsrcs = Resources::new(dettid);
            rsrcs.insert(ResourceID::BlockedExternalContinue(op_id), Permission::RW);
            rsrcs.fyi(call.name());
            // The mask the thread is stopped under here, as the scheduler may
            // decide from it before this turn (`BlockedPool::continuation_masks`).
            match continuation_mask(call, &res) {
                ContinuationMask::Temporary => {
                    rsrcs.blocked_signal_mask = blocked_signal_mask;
                    rsrcs.blocked_signal_mask_unknown = blocked_signal_mask_unknown;
                }
                ContinuationMask::Unknown => rsrcs.blocked_signal_mask_unknown = true,
                ContinuationMask::Restored => {}
            }
            resource_request(guest, rsrcs).await;
        }
        res
    }

    /// Executes a nonblockable syscall according to the following strategy:
    /// - Record mode: Execute possibly blocking syscall
    /// - Run mode: Transform the syscall to nonblocking if required before executing
    ///
    /// These are fd-oriented syscalls in the sense that whether they block or not depends
    /// on whether NONBLOCK was set on the corresponding file descriptor.
    pub async fn execute_nonblockable_fd_syscall<
        G: Guest<Self>,
        C: SyscallInfo + NonblockableSyscall + Into<Syscall>,
    >(
        &self,
        guest: &mut G,
        call: C,
    ) -> Result<i64, Error> {
        let wrapped: Syscall = call.into();

        let action = match ioaction_based_on_fd_status(guest, call) {
            Ok(action) => action,
            Err(errno) => {
                // Descriptor metadata is advisory for choosing an execution strategy. If the
                // descriptor is invalid or cannot be classified, execute through the
                // scheduler-safe blocking path and let the kernel provide the syscall errno.
                // Returning the metadata error (or panicking on it) can change Linux error
                // precedence, for example connect(-1, invalid_sockaddr, ...).
                tracing::trace!(
                    "NonblockableSyscall: fd classification failed with {}; executing kernel-authoritatively: {}",
                    errno,
                    call.name()
                );
                return self.record_or_replay_blocking(guest, wrapped).await;
            }
        };

        // Is this operation on a container-INTERNAL fd (a pipe or a socketpair
        // endpoint)? Internal fds are made physically nonblocking even in
        // record/replay (see handle_pipe2 and handle_socketpair), so they can take the
        // deterministic InternalIOPolling nonblockize-and-retry path. They must NOT be
        // forced onto the BlockingExternalIO path in R/R: a reader and its paired
        // writer are not independent, so descheduling the reader as "external
        // blocking IO" deadlocks the sequentialized scheduler (the documented R/R pipe
        // hang), and a one-shot read on the physically nonblocking fd returns EAGAIN.
        // Truly external endpoints (host fds, network sockets) still use
        // BlockingExternalIO. Other sockets are left external for now: there is no
        // internal-vs-external socket detection yet (see the handle_accept4 comment).
        let internal_fd = syscall_targets_internal_fd(guest, wrapped);

        if !self.cfg.sequentialize_threads
            || (self.cfg.recordreplay_modes && !internal_fd)
            || action == IOAction::Blocking
        {
            tracing::trace!(
                "NonblockableSyscall: executing in blocking mode after all: {}",
                call.name()
            );
            // We let these have nondeterminstic timing in record mode:
            Ok(self.record_or_replay_blocking(guest, wrapped).await?)
            // If in the future we want to record EXTERNAL network traffic only, we have a
            // challenge to overcome.  We don't know if we need to record until after the
            // accept completes, so we need an API for *post-facto* recording.
        } else if action == IOAction::NonblockizeRetry {
            tracing::trace!(
                "NonblockableSyscall: converting to nonblocking syscall (internal polling): {}",
                call.name()
            );
            let mut rsrc = Resources::new(guest.thread_state().dettid);
            rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
            rsrc.fyi(call.name());
            // Only guests write or read the other end of an internal fd.
            rsrc.polls_guest_state_only = internal_fd;
            // A pipe read: a recorded private `SIGCHLD` ends it as on Linux.
            rsrc.private_sigchld_decides = reads_a_pipe(guest, wrapped);
            // A read waits for data, a write for room (`--pipe-wake-latency`).
            rsrc.polls_channel = crate::pipe_wake::poll_of(&wrapped).and_then(|(fd, wants)| {
                self.pipe_channel_end(guest, fd)
                    .map(|end| Box::new((end.channel, wants)))
            });
            // In record/replay mode, route an internal-fd (pipe or socketpair) read/write through the
            // record/replay subtool so its data is captured on record and reproduced on
            // replay (see retry_nonblocking_syscall). In plain `hermit run` there is no
            // recorder, so execute directly (subtool = None).
            let subtool = (self.cfg.recordreplay_modes && internal_fd).then_some(self);
            Ok(retry_nonblocking_syscall(guest, call, rsrc, subtool).await?)
        } else {
            assert!(action == IOAction::PassThru);
            tracing::trace!(
                "NonblockableSyscall: just passing it through: {}",
                call.name()
            );
            // Otherwise, the socket was already nonblocking, so we can safely execute it just once.
            self.record_or_replay_preserving_tool_errors(guest, wrapped)
                .await
        }
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#547)
    /// Complete a logically blocking pipe writev after Hermit has made the pipe physically
    /// nonblocking. A positive short write is an implementation artifact here: without
    /// O_NONBLOCK, Linux blocks until the full vector is written unless a signal or error
    /// interrupts it. Atomic vectors retain a private iovec snapshot for every retry; larger
    /// vectors advance a positive short-write remainder through scalar writes.
    pub async fn execute_blocking_pipe_writev<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Writev,
        expected_open_file: OpenFileId,
    ) -> Result<i64, Error> {
        const MAX_IOVECS: usize = 1024;
        // Linux limits a single vectored transfer to INT_MAX rounded down to a page.
        const MAX_RW_COUNT: usize = 0x7fff_f000;
        // Linux guarantees pipe writes through this size are atomic.
        const PIPE_BUF: usize = 4096;
        // Every backend provides at least 512 bytes of tool scratch. Linux's
        // own fast-iovec path is smaller; this covers common vectors.
        const STACK_IOVECS: usize = 32;

        let Some(iov_addr) = call.iov() else {
            return self.execute_nonblockable_fd_syscall(guest, call).await;
        };
        if call.len() == 0 || call.len() > MAX_IOVECS {
            return self.execute_nonblockable_fd_syscall(guest, call).await;
        }

        let iovecs: Vec<(usize, usize)> = {
            let mut raw_iovecs = vec![
                libc::iovec {
                    iov_base: std::ptr::null_mut(),
                    iov_len: 0,
                };
                call.len()
            ];
            guest.memory().read_values(iov_addr, &mut raw_iovecs)?;
            raw_iovecs
                .into_iter()
                .map(|iovec| (iovec.iov_base as usize, iovec.iov_len))
                .collect()
        };
        let requested = iovecs.iter().try_fold(0usize, |total, (_, length)| {
            total.checked_add(*length).ok_or(Errno::EINVAL)
        })?;
        if requested > isize::MAX as usize {
            return Err(Errno::EINVAL.into());
        }
        let target = requested.min(MAX_RW_COUNT);
        if target == 0 {
            return self.execute_nonblockable_fd_syscall(guest, call).await;
        }

        let atomic_pipe_write = target <= PIPE_BUF;

        tracing::trace!(
            "NonblockableSyscall: converting to nonblocking syscall (internal polling): writev"
        );
        let mut resources = pipe_writev_resources(guest.thread_state().dettid, call);
        resources.polls_channel = self
            .pipe_channel_end(guest, call.fd())
            .map(|end| Box::new((end.channel, crate::pipe_wake::Wants::Space)));
        let subtool = self.cfg.recordreplay_modes.then_some(self);
        let mut current = Syscall::Writev(call);
        let mut written_total = 0usize;

        // Keep ordinary signals pending while the scheduler and the target-side
        // disposition check decide whether a wakeup interrupts this write. The
        // ptrace-owned backends need the explicit mask while inspecting the
        // target's current disposition.
        let blocked_mask = blocked_signal_mask();
        let mut stack = guest.stack().await;
        let atomic_scratch_iov = if atomic_pipe_write && iovecs.len() <= STACK_IOVECS {
            let mut raw_iovecs = [libc::iovec {
                iov_base: std::ptr::null_mut(),
                iov_len: 0,
            }; STACK_IOVECS];
            for (raw, (base, length)) in raw_iovecs.iter_mut().zip(&iovecs) {
                raw.iov_base = *base as *mut libc::c_void;
                raw.iov_len = *length;
            }
            let scratch_iov: Addr<libc::iovec> = stack.push(raw_iovecs).cast();
            Some(scratch_iov.as_raw())
        } else {
            None
        };
        let blocked_mask_addr = stack.push(blocked_mask);
        let old_mask_addr = stack.reserve::<KernelSigset>();
        let turn_mask_cell = stack.reserve::<KernelSigset>();
        let action_addr = stack.reserve::<KernelSigaction>();
        let _mask_guard = stack.commit()?;
        let checks_at_turn = decides_interruption_at_turn(guest, guest_signal_state_task(guest));
        // Where the turn decides (`read_turn_signals`), the state is read, and
        // the first attempt made, in the slice of the call's own entry, as the
        // scalar writer's first attempt is, under the guest's own mask: that
        // attempt is the guest's own `writev`, which the backend runs as plain
        // ptrace runs it, with every pending signal left queued in the kernel,
        // and a copy pending at entry ends the call after it (rules 5 and 6).
        // No other thread runs between Detcore's snapshot of the iovec array,
        // taken as it handles the call, and that attempt, so the kernel imports
        // the array Detcore copied, which every later attempt writes from. A
        // rewrite that lands before the snapshot (while the call waits at a
        // checkpoint before its handler runs) is one Linux could also see,
        // before the call imports the array.
        // Requesting a turn first let another thread rewrite the array before
        // the import, and an atomic write to a full pipe then wrote its bytes
        // (writev_determinism's atomic full-pipe case, which reverted the
        // first landing of R2b-1). Only a call that goes on polling installs the
        // private mask, through `KernelSignalWait::block`, in the same turn
        // (`install_turn_mask`); no copy that ends the call is pending then,
        // and one Linux discarded at send is held until the call returns.
        // Installing it first let two such copies stop the installation, and
        // the backend's single held slot kept only the second (the Claude
        // implementation review of R2b-1, F2;
        // https://github.com/rrnewton/reverie/issues/1012).
        let mut first_turn = if checks_at_turn {
            let turn = read_turn_signals(guest, None, 0, NamedRealTime::ByDisposition).await?;
            Some((ResumeStatus::Normal, turn))
        } else {
            None
        };
        let mut turn_wait = checks_at_turn
            .then(|| KernelSignalWait::new(guest, 0, false, call.signal_interrupt_errno()));
        let mut turn_mask: Option<KernelSigset> = None;
        let guest_signal_mask = if checks_at_turn {
            0
        } else {
            block_signals_for_disposition(guest, blocked_mask_addr, old_mask_addr).await?
        };

        let result: Result<i64, Error> = loop {
            let written_before = written_total;
            // A cross-task signal can replace the initial scheduler request,
            // before the first physical attempt, as well as a later retry.
            let (status, turn) = match first_turn.take() {
                Some((status, turn)) => (status, Some(turn)),
                None => {
                    if let Some(wait) = turn_wait.as_mut()
                        && turn_mask.is_none()
                    {
                        match install_turn_mask(guest, wait, turn_mask_cell).await {
                            Ok(mask) => turn_mask = Some(mask),
                            Err(Error::Errno(errno)) => {
                                break if written_total > 0 {
                                    Ok(written_total as i64)
                                } else {
                                    Err(errno.into())
                                };
                            }
                            Err(error) => break Err(error),
                        }
                    }
                    let status = resource_request(guest, resources.clone()).await;
                    let turn = if checks_at_turn {
                        // A named signal counts if the guest's own mask leaves
                        // it unblocked; the private mask keeps every such
                        // signal pending until this read.
                        match read_turn_signals(
                            guest,
                            turn_mask,
                            committed_signals(&status),
                            NamedRealTime::ByDisposition,
                        )
                        .await
                        {
                            Ok(turn) => Some(turn),
                            Err(error) => break Err(error),
                        }
                    } else {
                        None
                    };
                    (status, turn)
                }
            };
            if turn.is_none() {
                let disposition = match wait_signal_disposition(
                    guest,
                    status.clone(),
                    &guest_signal_mask,
                    action_addr,
                    true,
                )
                .await
                {
                    Ok(disposition) => disposition,
                    Err(error) => break Err(error),
                };
                if matches!(status, ResumeStatus::Signaled(_))
                    && let Some(result) =
                        interrupted_write_result(&call, written_total, disposition)
                {
                    break result;
                }
            }

            if resources.poll_attempt > 0
                && !guest
                    .thread_state()
                    .with_detfd(call.fd(), |detfd| {
                        detfd.open_file_id() == expected_open_file
                    })
                    .unwrap_or(false)
            {
                break if written_total > 0 {
                    Ok(written_total as i64)
                } else {
                    self.refuse_unserviceable_operation(guest, Sysno::writev, Errno::EOPNOTSUPP)
                        .await
                };
            }

            // The first attempt of a call that decides at its turn runs under
            // the guest's own mask, where a pending signal would stop a
            // private injection and the backend's single held slot keeps only
            // the last of two. So it is always the guest's own `writev`, which
            // the backend runs with every pending signal left queued in the
            // kernel (the Claude re-check of R2b-1, N2).
            let own_first_attempt = turn_wait.is_some() && turn_mask.is_none();
            let result = if atomic_pipe_write && !own_first_attempt {
                self.execute_atomic_pipe_writev_attempt(guest, call, &iovecs, atomic_scratch_iov)
                    .await
            } else {
                match subtool {
                    Some(detcore) => {
                        detcore
                            .record_or_replay_preserving_tool_errors(guest, current)
                            .await
                    }
                    None => guest.inject_with_retry(current).await.map_err(Error::from),
                }
            };
            match result {
                Ok(written) if written > 0 => {
                    let written = match usize::try_from(written) {
                        Ok(written) => written,
                        Err(_) => break Err(Errno::EIO.into()),
                    };
                    written_total = match written_total.checked_add(written) {
                        Some(written_total) => written_total,
                        None => break Err(Errno::EIO.into()),
                    };
                    if written_total >= target {
                        break Ok(written_total as i64);
                    }
                    if atomic_pipe_write {
                        break Ok(written_total as i64);
                    }
                    // A short attempt filled the pipe, so the write would wait:
                    // a wake ends it with the bytes written, decided after the
                    // attempt, as Linux tests for room first (R2b-1 rule 6).
                    if turn.is_some_and(|turn| turn.ends(true)) {
                        break Ok(written_total as i64);
                    }
                    current = match remaining_writev_segment(
                        call.fd(),
                        &iovecs,
                        written_total,
                        target - written_total,
                    ) {
                        Ok(Some(write)) => Syscall::Write(write),
                        Ok(None) => break Ok(written_total as i64),
                        Err(_) => break Ok(written_total as i64),
                    };
                }
                Ok(0) => break Ok(written_total as i64),
                Err(Error::Errno(Errno::EAGAIN))
                    if turn.is_some_and(|turn| turn.ends(written_total > 0)) =>
                {
                    break if written_total > 0 {
                        Ok(written_total as i64)
                    } else {
                        Err(call.signal_interrupt_errno().into())
                    };
                }
                Err(Error::Errno(Errno::EAGAIN)) => {
                    if !atomic_pipe_write && matches!(current, Syscall::Writev(_)) {
                        current = match remaining_writev_segment(call.fd(), &iovecs, 0, target) {
                            Ok(Some(write)) => Syscall::Write(write),
                            Ok(None) => break Ok(0),
                            Err(error) => break Err(error.into()),
                        };
                    }
                }
                Err(error) => {
                    break finish_partial_record_or_replay_write(written_total as i64, error);
                }
                Ok(_) => break Err(Errno::EIO.into()),
            }

            resources.previous_attempt_made_progress = written_total > written_before;
            // Bytes this attempt wrote ready the readers before this thread
            // polls for room (`--pipe-wake-latency`).
            if resources.previous_attempt_made_progress {
                self.note_fd_readied(guest, call.fd(), crate::pipe_wake::Wants::Data)
                    .await;
            }
            resources.poll_attempt += 1;
            tracing::trace!(
                "Retry #{} for {}blocking pipe writev after EAGAIN: {}",
                resources.poll_attempt,
                if atomic_pipe_write { "atomic " } else { "" },
                call.display(&guest.memory())
            );
            record_retry_event(guest, call).await;
        };

        match turn_wait {
            Some(mut wait) => {
                let restored = wait.restore(guest, Some(turn_mask_cell)).await;
                if turn_mask.is_some() {
                    crate::tool_global::set_saved_guest_sigmask(guest, None).await;
                }
                match result_after_restore(restored, result) {
                    Ok(result) => refuse_held_signal_loss(guest, Sysno::writev, result).await,
                    Err(error) => Err(error),
                }
            }
            None => {
                restore_signals_after_disposition(guest, old_mask_addr).await?;
                result
            }
        }
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#2176): Review scalar blocking-pipe write completion and
    // the fail-closed descriptor-replacement boundary.
    /// Complete a logically blocking scalar pipe write after Hermit has made the pipe
    /// physically nonblocking.
    ///
    /// Linux may return a positive short write for a request larger than `PIPE_BUF`, then
    /// continue blocking for the remainder. Hermit's physical `O_NONBLOCK` is an internal
    /// scheduler mechanism, so exposing that first short write changes guest behavior. Retry
    /// the unconsumed suffix until the logical write completes, a signal arrives, or a real
    /// error occurs. A signal or error after progress returns the partial byte count, matching
    /// Linux.
    ///
    /// A concurrent close/dup2 can replace the numeric fd while this helper is yielded. Linux
    /// keeps the original open-file description alive inside a blocking syscall, but Reverie
    /// does not yet expose a backend-neutral retained-fd handle. Detect replacement before a
    /// retry and fail closed rather than writing the suffix into an unrelated object.
    pub async fn execute_blocking_pipe_write<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Write,
        expected_open_file: OpenFileId,
    ) -> Result<i64, Error> {
        const MAX_RW_COUNT: usize = 0x7fff_f000;

        let target = call.len().min(MAX_RW_COUNT);
        if target == 0 {
            return self.execute_nonblockable_fd_syscall(guest, call).await;
        }

        tracing::trace!(
            "NonblockableSyscall: converting to nonblocking syscall (internal polling): write"
        );
        let mut resources = Resources::new(guest.thread_state().dettid);
        resources.insert(ResourceID::InternalIOPolling, Permission::W);
        resources.fyi(call.name());
        // Interruptible by a cross-task signal the kernel would deliver, as a
        // polled read is (`retry_nonblocking_syscall_helper`): the scheduler
        // answers the next request `Signaled`, and the write returns the
        // bytes it wrote, or ERESTARTSYS before any
        // (<https://github.com/rrnewton/hermit/issues/4187>).
        resources.set_signal_interrupt_errno(call.signal_interrupt_errno());
        resources.signal_state_task = guest_signal_state_task(guest);
        // A pipe write: a recorded private `SIGCHLD` ends it as on Linux
        // (`Resources::private_sigchld_decides`).
        resources.private_sigchld_decides = true;
        resources.polls_guest_state_only = true;
        resources.polls_channel = self
            .pipe_channel_end(guest, call.fd())
            .map(|end| Box::new((end.channel, crate::pipe_wake::Wants::Space)));
        // Without a host task whose kernel state the scheduler can filter by
        // (KVM), the scheduler keeps every signal and the write checks after
        // the wake, as `execute_blocking_pipe_writev` does, whether one would
        // be delivered: a blocked or ignored signal must not end a write that
        // has made progress, which would return a short count where Linux
        // keeps waiting.
        let disposition_scratch = if resources.signal_state_task.is_none() {
            resources.decides_signal_interruption_itself = true;
            let mut stack = guest.stack().await;
            let mask_addr = stack.reserve::<KernelSigset>();
            let action_addr = stack.reserve::<KernelSigaction>();
            let guard = stack.commit()?;
            Some((mask_addr, action_addr, guard))
        } else {
            None
        };
        let subtool = self.cfg.recordreplay_modes.then_some(self);
        let mut current = call;
        let mut written_total = 0usize;
        let checks_at_turn = decides_interruption_at_turn(guest, resources.signal_state_task);
        // The write owns the guest's mask while it blocks every blockable
        // signal, as the generic poll does (`retry_nonblocking_syscall_helper`):
        // an attempt after the first is a private injection of the
        // unwritten suffix, which a pending signal could stop and move into
        // the backend's single held slot, where a second one replaces it
        // (https://github.com/rrnewton/hermit/issues/4273, stage 2).
        let mut turn_wait = checks_at_turn
            .then(|| KernelSignalWait::new(guest, 0, false, call.signal_interrupt_errno()));
        let mut published = PublishedWaitMask::default();

        let result: Result<i64, Error> = loop {
            let written_before = written_total;
            // A signal ends the write only if it would still wait for room:
            // the attempt below runs first, as Linux's `pipe_write` checks
            // for a pending signal only when it must wait. After the signal,
            // the bytes written so far are returned, or the restart errno if
            // there are none.
            let status = if resources.poll_attempt > 0 {
                Some(resource_request(guest, resources.clone()).await)
            } else {
                None
            };
            // Read before every attempt, the turnless first one included
            // (`read_turn_signals`): a copy pending at the write's own entry
            // is decided in this slice, before the writer can yield, so a later
            // `SIG_IGN` and a new handler cannot take it
            // (https://github.com/rrnewton/hermit/issues/4273, R2b-1 rule 4).
            // Once the write blocks, the turn decides under the guest's own
            // mask, which the write owns (Codex review of the R2b-2 design
            // rev3, P2-1).
            let turn = if checks_at_turn {
                let committed = status.as_ref().map_or(0, committed_signals);
                // The first attempt is the guest's own write, which the
                // backend runs with every pending signal left queued, so only
                // a later attempt needs the block.
                match turn_signals_with_block(
                    guest,
                    &mut turn_wait,
                    resources.poll_attempt > 0,
                    committed,
                    resources.private_sigchld_decides,
                    &mut published,
                )
                .await
                {
                    Ok(turn) => Some(turn),
                    Err(error) => break Err(error),
                }
            } else {
                None
            };
            let signaled = if turn.is_some() {
                false
            } else {
                match (&status, &disposition_scratch) {
                    (Some(ResumeStatus::Signaled(_)), None) => true,
                    (
                        Some(status @ ResumeStatus::Signaled(_)),
                        Some((mask_addr, action_addr, _)),
                    ) => {
                        match own_disposition_interrupts(
                            guest,
                            status.clone(),
                            *mask_addr,
                            *action_addr,
                        )
                        .await
                        {
                            Ok(interrupts) => interrupts,
                            Err(error) => break Err(error),
                        }
                    }
                    _ => false,
                }
            };

            if resources.poll_attempt > 0
                && !guest
                    .thread_state()
                    .with_detfd(call.fd(), |detfd| {
                        detfd.open_file_id() == expected_open_file
                    })
                    .unwrap_or(false)
            {
                break if written_total > 0 {
                    Ok(written_total as i64)
                } else {
                    self.refuse_unserviceable_operation(guest, Sysno::write, Errno::EOPNOTSUPP)
                        .await
                };
            }

            let result = match subtool {
                Some(detcore) => {
                    detcore
                        .record_or_replay_preserving_tool_errors(guest, current)
                        .await
                }
                None => guest.inject_with_retry(current).await.map_err(Error::from),
            };
            // As in `retry_nonblocking_syscall_helper`, an attempt that the
            // pending signal stopped under record and replay is not made again.
            match result {
                Ok(written) if written > 0 => {
                    let Ok(written) = usize::try_from(written) else {
                        break Err(Errno::EIO.into());
                    };
                    let Some(remaining) = target.checked_sub(written_total) else {
                        break Err(Errno::EIO.into());
                    };
                    if written > remaining {
                        break Err(Errno::EIO.into());
                    }
                    let Some(total) = written_total.checked_add(written) else {
                        break Err(Errno::EIO.into());
                    };
                    written_total = total;
                    // A short attempt filled the pipe, so the write would wait:
                    // a wake ends it with the bytes written (`TurnSignals::ends`).
                    if written_total == target || turn.map_or(signaled, |turn| turn.ends(true)) {
                        break Ok(written_total as i64);
                    }
                    let Some(buffer) = call.buf() else {
                        break Err(Errno::EFAULT.into());
                    };
                    let Some(next_buffer) = buffer
                        .as_raw()
                        .checked_add(written_total)
                        .and_then(Addr::<u8>::from_raw)
                    else {
                        break finish_partial_record_or_replay_write(
                            written_total as i64,
                            Errno::EFAULT.into(),
                        );
                    };
                    current = call
                        .with_buf(Some(next_buffer))
                        .with_len(target - written_total);
                }
                Ok(0) => break Ok(written_total as i64),
                Err(Error::Errno(Errno::EAGAIN))
                    if turn.map_or(signaled, |turn| turn.ends(written_total > 0)) =>
                {
                    break if written_total > 0 {
                        Ok(written_total as i64)
                    } else {
                        Err(call.signal_interrupt_errno().into())
                    };
                }
                Err(Error::Errno(Errno::EAGAIN)) => {}
                Err(error) => {
                    break finish_partial_record_or_replay_write(written_total as i64, error);
                }
                Ok(_) => break Err(Errno::EIO.into()),
            }

            resources.previous_attempt_made_progress = written_total > written_before;
            // Bytes this attempt wrote ready the readers before this thread
            // polls for room (`--pipe-wake-latency`).
            if resources.previous_attempt_made_progress {
                self.note_fd_readied(guest, call.fd(), crate::pipe_wake::Wants::Data)
                    .await;
            }
            resources.poll_attempt += 1;
            tracing::trace!(
                "Retry #{} for blocking pipe write: {}",
                resources.poll_attempt,
                call.display(&guest.memory())
            );
            record_retry_event(guest, call).await;
        };

        // The single exit, as in `retry_nonblocking_syscall_helper`: the
        // guest's mask goes back, through `Guest::set_blocked_signals` with no
        // scratch memory, before the result is returned.
        match turn_wait {
            Some(mut wait) if wait.effective_mask().is_some() => {
                let restored = wait.restore(guest, None).await;
                publish_wait_mask(guest, None, &mut published).await;
                match result_after_restore(restored, result) {
                    Ok(result) => refuse_held_signal_loss(guest, Sysno::write, result).await,
                    Err(error) => Err(error),
                }
            }
            _ => result,
        }
    }

    async fn execute_atomic_pipe_writev_attempt<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Writev,
        iovecs: &[(usize, usize)],
        stack_iov: Option<usize>,
    ) -> Result<i64, Error> {
        if let Some(stack_iov) = stack_iov {
            let scratch_call = call.with_iov(Addr::from_raw(stack_iov));
            return if self.cfg.recordreplay_modes {
                self.record_or_replay_preserving_tool_errors(guest, scratch_call)
                    .await
            } else {
                guest
                    .inject_with_retry(scratch_call)
                    .await
                    .map_err(Error::from)
            };
        }

        let mapping_len = iovecs
            .len()
            .checked_mul(std::mem::size_of::<libc::iovec>())
            .expect("validated iovec count cannot overflow scratch length");
        let mapped = guest
            .inject_with_retry(Syscall::Mmap(
                syscalls::Mmap::new()
                    .with_addr(None)
                    .with_len(mapping_len)
                    .with_prot(ProtFlags::PROT_READ | ProtFlags::PROT_WRITE)
                    .with_flags(MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS)
                    .with_fd(-1)
                    .with_offset(0),
            ))
            .await
            .unwrap_or_else(|error| panic!("failed to map atomic writev scratch: {error}"));
        let mapped = usize::try_from(mapped)
            .unwrap_or_else(|_| panic!("atomic writev scratch mmap returned {mapped}"));
        let scratch_iov = Addr::<libc::iovec>::from_raw(mapped)
            .unwrap_or_else(|| panic!("atomic writev scratch mmap returned a null address"));
        let mapping_addr: Addr<libc::c_void> = scratch_iov.cast();
        let write_result = {
            let raw_iovecs: Vec<libc::iovec> = iovecs
                .iter()
                .map(|(base, length)| libc::iovec {
                    iov_base: *base as *mut libc::c_void,
                    iov_len: *length,
                })
                .collect();
            // SAFETY: the injected anonymous mapping is exclusively owned scratch space.
            guest
                .memory()
                .write_values(unsafe { scratch_iov.into_mut() }, &raw_iovecs)
        };
        if let Err(write_error) = write_result {
            guest
                .inject_with_retry(Syscall::Munmap(
                    syscalls::Munmap::new()
                        .with_addr(Some(mapping_addr))
                        .with_len(mapping_len),
                ))
                .await
                .unwrap_or_else(|cleanup_error| {
                    panic!(
                        "failed to populate atomic writev scratch ({write_error}); cleanup failed ({cleanup_error})"
                    )
                });
            panic!("failed to populate atomic writev scratch: {write_error}");
        }

        let scratch_call = call.with_iov(Some(scratch_iov));
        let result = if self.cfg.recordreplay_modes {
            self.record_or_replay_preserving_tool_errors(guest, scratch_call)
                .await
        } else {
            guest
                .inject_with_retry(scratch_call)
                .await
                .map_err(Error::from)
        };
        guest
            .inject_with_retry(Syscall::Munmap(
                syscalls::Munmap::new()
                    .with_addr(Some(mapping_addr))
                    .with_len(mapping_len),
            ))
            .await
            .unwrap_or_else(|error| panic!("failed to unmap atomic writev scratch: {error}"));
        result
    }

    /// Override physically_nonblocking to true for the file descriptor, if appropriate.
    pub fn maybe_set_nonblocking_fd<G: Guest<Self>>(&self, guest: &G, fd: i32) {
        if self.cfg.sequentialize_threads && !self.cfg.debug_externalize_sockets {
            guest
                .thread_state()
                .with_detfd(fd, |detfd| {
                    detfd.set_physically_nonblocking();
                })
                .unwrap();
        }
    }
}

fn remaining_writev_segment(
    fd: i32,
    iovecs: &[(usize, usize)],
    mut consumed: usize,
    remaining_limit: usize,
) -> Result<Option<syscalls::Write>, Errno> {
    for (base, length) in iovecs {
        if consumed >= *length {
            consumed -= *length;
            continue;
        }
        let base = base.checked_add(consumed).ok_or(Errno::EFAULT)?;
        let buffer = Addr::<u8>::from_raw(base).ok_or(Errno::EFAULT)?;
        return Ok(Some(
            syscalls::Write::new()
                .with_fd(fd)
                .with_buf(Some(buffer))
                .with_len((*length - consumed).min(remaining_limit)),
        ));
    }
    Ok(None)
}

/// A blocking syscall that involves a fail descriptor may be handled in these three ways:
#[derive(PartialEq, Eq, Debug)]
pub enum IOAction {
    /// It may physically block and we can't change that.  Treat it as ExternalBlockingIO.
    Blocking,
    /// We can nonblockize and retry the call.
    NonblockizeRetry,
    /// The call is nonblocking already, and safe to execute.
    PassThru,
}

/// Returns the strategy for an FD-based call that may block when executed.
///
/// Failure means descriptor metadata could not classify the call; the caller must preserve the
/// kernel's authority over the syscall result rather than exposing this advisory lookup error.
pub fn ioaction_based_on_fd_status<
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
    C: SyscallInfo + Into<Syscall>,
>(
    guest: &mut G,
    call: C,
) -> Result<IOAction, Errno> {
    let wrapped: Syscall = call.into();
    let fd = get_fd(wrapped).unwrap_or_else(|| panic!("Failed to get fd for {}", call.name()));
    let (phys, virt) = guest.thread_state().with_detfd(fd, |detfd| {
        (detfd.physically_nonblocking(), detfd.is_nonblocking())
    })?;
    tracing::trace!(
        "Checking FD {} for nonblocking: physical {} / virtual {}",
        fd,
        phys,
        virt
    );
    if virt && !phys {
        // TF: simulate nonblocking on top of physically blocking? How?
        panic!(
            "Invariant violation, fd {}: we cannot simulate nonblocking behavior when set to blocking mode in the kernel.",
            fd
        );
    } else if !virt && !phys {
        // FF: logically blocking, physically blocking, this could only work with BlockingExternalIO.
        Ok(IOAction::Blocking)
    } else if virt && phys {
        // TT: both nonblocking, so firing once is sufficient
        Ok(IOAction::PassThru)
    } else {
        // FT: Need to simulate blocking on top of nonblocking.
        Ok(IOAction::NonblockizeRetry)
    }
}

/// Does this single-fd syscall operate on a container-INTERNAL file descriptor?
///
/// This recognizes pipes, whose two endpoints are always both owned by guest
/// processes inside the deterministic container, and `socketpair(2)` endpoints, which
/// have that same property for the same reason. Internal fds are made physically
/// nonblocking (see `handle_pipe2` and `handle_socketpair`) so a potentially-blocking
/// op on them can use the deterministic `InternalIOPolling` nonblockize-and-retry
/// strategy instead of `BlockingExternalIO`. Treating an internal fd as external
/// blocking IO deadlocks the sequentialized scheduler in record/replay, because a
/// reader and its paired writer are not independent.
///
/// A socketpair is the one socket case where internality is known at creation time:
/// the kernel creates BOTH ends, so no peer outside the container can ever hold one.
/// That is why it does not need the general internal-vs-external socket detection
/// (loopback / AF_UNIX-to-another-guest vs a real host peer) that remains unsolved;
/// every OTHER socket still conservatively remains external.
///
/// The residual assumption, stated because it is an assumption and not a proof: a
/// guest could pass a socketpair endpoint out of the container over `SCM_RIGHTS` to a
/// process on the host. A pipe fd can escape by exactly the same route and this code
/// has always accepted that for pipes, so recognizing socketpairs widens no exposure
/// class -- but neither claim is airtight.
///
/// The converse gap: a socketpair endpoint or pipe that a guest RECEIVES over
/// `SCM_RIGHTS` is untracked by Detcore, so it is not recognized here and keeps the
/// kernel-authoritative external path. Its open file description is still physically
/// nonblocking, so a blocking read through the received descriptor can return EAGAIN
/// under record, as it already could for a received pipe.
///
/// Syscalls whose fd is not directly extractable (e.g. poll/ppoll, which carry a
/// pointer to an fd array) return false and keep their existing handling.
pub fn syscall_targets_internal_fd<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    call: Syscall,
) -> bool {
    // An epoll wait's descriptor is the epoll set it waits on, never a pipe it
    // reads; a guest that passes a pipe gets EINVAL from the kernel.
    if matches!(call, Syscall::EpollWait(_) | Syscall::EpollPwait(_)) {
        return false;
    }
    match get_fd(call) {
        Some(fd) => guest
            .thread_state()
            .with_detfd(fd, |detfd| {
                // This is read-end provenance, not a blanket external-fd
                // classification: writes and socketpairs retain the guard.
                if matches!(call, Syscall::Read(_) | Syscall::Readv(_))
                    && detfd.inherited_external_read().is_some()
                {
                    return false;
                }
                matches!(detfd.ty(), FdType::Pipe) || detfd.is_socketpair_endpoint()
            })
            .unwrap_or(false),
        None => false,
    }
}

/// Whether `call` is a `read` or `readv` of a descriptor Detcore records as
/// a pipe (`FdType::Pipe`: a pipe, or a FIFO that joined internal polling),
/// the restartable reads a recorded private `SIGCHLD` ends
/// (`Resources::private_sigchld_decides`; R2b-2 stage 3,
/// https://github.com/rrnewton/hermit/issues/4273). A socket read keeps
/// holding it (rev4 1e).
fn reads_a_pipe<G: Guest<Detcore<T>>, T: RecordOrReplay>(guest: &mut G, call: Syscall) -> bool {
    matches!(call, Syscall::Read(_) | Syscall::Readv(_))
        && get_fd(call).is_some_and(|fd| {
            guest
                .thread_state()
                .with_detfd(fd, |detfd| matches!(detfd.ty(), FdType::Pipe))
                .unwrap_or(false)
        })
}

/// Does `call` read a descriptor Detcore records as a signalfd
/// (`Resources::reads_signalfd`)? Only `read`, `readv` and `preadv2` at the
/// current offset can: a positioned read of a signalfd fails with ESPIPE.
fn call_reads_signalfd<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    call: Syscall,
) -> bool {
    let fd = match call {
        Syscall::Read(c) => c.fd(),
        Syscall::Readv(c) => c.fd(),
        Syscall::Preadv2(c) => c.fd(),
        _ => return false,
    };
    guest
        .thread_state()
        .with_detfd(fd, |detfd| detfd.ty() == crate::fd::FdType::Signalfd)
        .unwrap_or(false)
}

/// The descriptor a `read`, `readv` or `preadv2` reads (`Resources::read_fd`).
fn read_family_fd(call: Syscall) -> Option<i32> {
    match call {
        Syscall::Read(c) => Some(c.fd()),
        Syscall::Readv(c) => Some(c.fd()),
        Syscall::Preadv2(c) => Some(c.fd()),
        _ => None,
    }
}

/// Does replay answer this call from the recording rather than by running it?
///
/// This follows the replayer's dispatch. It serves `accept`, `accept4`, `connect`,
/// `sendto`, `sendmsg`, `recvfrom` and `recvmsg` from its log whatever the descriptor
/// is, so they count even on a descriptor Detcore does not track, such as a listener
/// received over `SCM_RIGHTS`; an untagged accept there would let its stand-in take a
/// slot at a host-timed point. `read`, `readv`, `write` and `writev` count only on a
/// socket Detcore created with `socket(2)` or `accept(2)`: on other descriptors,
/// replay may run them for real as eventfd operations, which can block, so an unknown
/// descriptor is not assumed to be served from the log. `recvmmsg` and `sendmmsg` are
/// recorded by neither recorder nor replayer (on an accepted connection record/replay
/// refuses them, see `refuse_log_served_mmsg`).
fn replay_serves_socket_call_from_log<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    call: Syscall,
) -> bool {
    let fd = match call {
        Syscall::Accept(_)
        | Syscall::Accept4(_)
        | Syscall::Connect(_)
        | Syscall::Sendto(_)
        | Syscall::Sendmsg(_)
        | Syscall::Recvfrom(_)
        | Syscall::Recvmsg(_) => return true,
        Syscall::Read(s) => s.fd(),
        Syscall::Readv(s) => s.fd(),
        Syscall::Write(s) => s.fd(),
        Syscall::Writev(s) => s.fd(),
        _ => return false,
    };
    is_log_served_socket(guest, fd)
}

/// Does replay answer this otherwise blocking call from the recording alone?
///
/// The replayer serves `poll`, `ppoll`, `select` and `pselect6` from the log
/// without running them. `epoll_wait` is excluded because replay reapplies its
/// side effects on live descriptors, which may block.
fn replay_serves_blocking_call_from_log(call: Syscall) -> bool {
    matches!(
        call,
        Syscall::Poll(_) | Syscall::Ppoll(_) | Syscall::Select(_) | Syscall::Pselect6(_)
    )
}

/// Is `fd` a socket whose connection record and replay keep only in the log?
///
/// That is a socket Detcore created with `socket(2)` or `accept(2)`, but not a
/// socketpair endpoint: replay recreates a socketpair, and its operations run
/// live.
fn is_log_served_socket<G: Guest<Detcore<T>>, T: RecordOrReplay>(guest: &mut G, fd: i32) -> bool {
    guest
        .thread_state()
        .with_detfd(fd, |detfd| {
            matches!(detfd.ty(), FdType::Socket) && !detfd.is_socketpair_endpoint()
        })
        .unwrap_or(false)
}

/// Is `fd` a connection that `accept(2)` or `accept4(2)` returned? Record and
/// replay keep such a connection only in the log, and replay stands in an
/// eventfd for it, so an operation that runs live during replay meets that
/// eventfd rather than a connected socket.
pub(crate) fn is_accepted_connection<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    fd: i32,
) -> bool {
    guest
        .thread_state()
        .with_detfd(fd, |detfd| detfd.is_accepted_connection())
        .unwrap_or(false)
}

/// A large subset of system calls have a single, unique file descriptor argument.  This
/// is a convenience function for grabbing that argument.
///
/// It does not cover system calls with multiple fd arguments, with pointers to heap
/// structures that contain fds.
pub(crate) fn get_fd(s: Syscall) -> Option<i32> {
    match s {
        Syscall::Recvfrom(s) => Some(s.fd()),
        Syscall::Recvmsg(s) => Some(s.sockfd()),
        Syscall::Recvmmsg(s) => Some(s.fd()),
        Syscall::Sendto(s) => Some(s.fd()),
        Syscall::Sendmsg(s) => Some(s.fd()),
        Syscall::Sendmmsg(s) => Some(s.sockfd()),
        Syscall::Accept(s) => Some(s.sockfd()),
        Syscall::Accept4(s) => Some(s.sockfd()),
        Syscall::Connect(s) => Some(s.fd()),
        Syscall::Bind(s) => Some(s.fd()),
        Syscall::Listen(s) => Some(s.fd()),
        Syscall::Getsockname(s) => Some(s.fd()),
        Syscall::Getpeername(s) => Some(s.fd()),
        Syscall::Setsockopt(s) => Some(s.fd()),
        Syscall::Getsockopt(s) => Some(s.fd()),

        Syscall::Read(s) => Some(s.fd()),
        Syscall::Write(s) => Some(s.fd()),
        Syscall::Close(s) => Some(s.fd()),
        Syscall::Fstat(s) => Some(s.fd()),
        Syscall::Lseek(s) => Some(s.fd()),
        Syscall::Mmap(s) => Some(s.fd()),
        Syscall::Ioctl(s) => Some(s.fd()),
        Syscall::Pread64(s) => Some(s.fd()),
        Syscall::Pwrite64(s) => Some(s.fd()),
        Syscall::Readv(s) => Some(s.fd()),
        Syscall::Writev(s) => Some(s.fd()),

        Syscall::Shutdown(s) => Some(s.fd()),
        Syscall::Fcntl(s) => Some(s.fd()),
        Syscall::Flock(s) => Some(s.fd()),
        Syscall::Fsync(s) => Some(s.fd()),
        Syscall::Fdatasync(s) => Some(s.fd()),
        Syscall::Ftruncate(s) => Some(s.fd()),
        Syscall::Fchdir(s) => Some(s.fd()),
        Syscall::Fchmod(s) => Some(s.fd()),
        Syscall::Fchown(s) => Some(s.fd()),
        Syscall::Fstatfs(s) => Some(s.fd()),
        Syscall::Readahead(s) => Some(s.fd()),
        Syscall::Fsetxattr(s) => Some(s.fd()),
        Syscall::Fgetxattr(s) => Some(s.fd()),
        Syscall::Flistxattr(s) => Some(s.fd()),
        Syscall::Fremovexattr(s) => Some(s.fd()),
        Syscall::Fadvise64(s) => Some(s.fd()),
        Syscall::InotifyAddWatch(s) => Some(s.fd()),
        Syscall::InotifyRmWatch(s) => Some(s.fd()),
        Syscall::SyncFileRange(s) => Some(s.fd()),
        Syscall::Vmsplice(s) => Some(s.fd()),
        Syscall::Utimensat(s) => Some(s.dirfd()),
        Syscall::Signalfd(s) => Some(s.fd()),
        Syscall::Fallocate(s) => Some(s.fd()),
        Syscall::TimerfdSettime(s) => Some(s.fd()),
        Syscall::TimerfdGettime(s) => Some(s.fd()),
        Syscall::Signalfd4(s) => Some(s.fd()),
        Syscall::Preadv(s) => Some(s.fd()),
        Syscall::Pwritev(s) => Some(s.fd()),
        Syscall::Syncfs(s) => Some(s.fd()),
        Syscall::Setns(s) => Some(s.fd()),
        Syscall::FinitModule(s) => Some(s.fd()),
        Syscall::Preadv2(s) => Some(s.fd()),
        Syscall::Pwritev2(s) => Some(s.fd()),

        Syscall::Openat(s) => Some(s.dirfd()),
        Syscall::Mkdirat(s) => Some(s.dirfd()),
        Syscall::Mknodat(s) => Some(s.dirfd()),
        Syscall::Fchownat(s) => Some(s.dirfd()),
        Syscall::Futimesat(s) => Some(s.dirfd()),
        Syscall::Newfstatat(s) => Some(s.dirfd()),
        Syscall::Unlinkat(s) => Some(s.dirfd()),
        Syscall::Readlinkat(s) => Some(s.dirfd()),
        Syscall::Fchmodat(s) => Some(s.dirfd()),
        Syscall::Faccessat(s) => Some(s.dirfd()),
        Syscall::NameToHandleAt(s) => Some(s.dirfd()),
        Syscall::Execveat(s) => Some(s.dirfd()),
        Syscall::Statx(s) => Some(s.dirfd()),
        Syscall::Symlinkat(s) => Some(s.newdirfd()),
        Syscall::PerfEventOpen(s) => Some(s.group_fd()),
        Syscall::OpenByHandleAt(s) => Some(s.mount_fd()),

        Syscall::EpollCtl(s) => Some(s.epfd()),
        Syscall::EpollWait(s) => Some(s.epfd()),
        Syscall::EpollPwait(s) => Some(s.epfd()),

        // Ambiguous, 2 fds, no answer:
        Syscall::Dup2(_) => None,
        // Ambiguous, 2 fds, no answer:
        Syscall::Sendfile(_) => None,
        // Ambiguous, 2 fds, no answer:
        Syscall::Renameat(_) => None,
        // Ambiguous, 2 fds, no answer:
        Syscall::Linkat(_) => None,
        // Ambiguous, 2 fds, no answer:
        Syscall::FanotifyMark(_) => None,
        // Ambiguous, 2 fds, no answer:
        Syscall::Renameat2(_) => None,
        // Ambiguous, 2 fds, no answer:
        Syscall::Dup3(_) => None,
        // Ambiguous, 2 fds, no answer:
        Syscall::KexecLoad(_) => None,

        // Takes a pointer to fd, not directly accessible:
        Syscall::Poll(_) => None,
        // Takes a pointer to fd, not directly accessible:
        Syscall::Ppoll(_) => None,

        _ => None,
    }
}

/// A system call which may or may not block, but which can be MADE nonblocking.
// `async_trait` rewrites `into_nonblocking` to return a boxed future and marks it
// `#[must_use]`; the future is already `#[must_use]` in its own right, so clippy sees a double
// annotation. Both are generated, so the lint's own suggestion -- give the `must_use` an explicit
// reason -- cannot be applied at this source. Allowed at the item rather than crate-wide so any
// hand-written double `must_use` elsewhere still fails `#![deny(clippy::all)]` (detcore/src/lib.rs:35).
// Appeared with nightly-2026-08-08 (rustc 1.99.0-nightly 1a98b1e13) against an unchanged tree.
#[allow(clippy::double_must_use)]
#[async_trait]
pub trait NonblockableSyscall: SyscallInfo {
    /// Convert the system call to a nonblocking version of itself.  Sometimes this means
    /// setting a zero timeout, and sometimes it means something else.
    ///
    /// This may need to stack allocate, so it returns a StackGuard.
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>);

    /// Check if the result (in nonblocking mode) is analogous to blocking in blocking mode.
    /// I.e. the result means "try again".
    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Ok(0)
    }

    /// Return the errno used when a signal interrupts this internally polled syscall.
    /// Most blocking I/O is restartable when its handler uses `SA_RESTART`.
    fn signal_interrupt_errno(&self) -> Errno {
        Errno::ERESTARTSYS
    }

    /// Return the errno for an interrupting signal that ends this wait after it began,
    /// on a backend that decides interruption from the kernel's signal state
    /// (`supports_blocked_wait_signal_interruption`). The kernel applies the
    /// guest's disposition to it on resume, so it must be the restart code Linux uses
    /// for this call: a handler then turns it into `EINTR` or a restart, and a signal
    /// with no handler restarts the call.
    fn kernel_restart_errno(&self) -> Errno {
        self.signal_interrupt_errno()
    }

    /// Whether Linux restarts this call, after [`kernel_restart_errno`](Self::kernel_restart_errno)
    /// with no handler, through a restart block that keeps its absolute deadline: a
    /// `poll` with a positive timeout (`do_restart_poll`) and a timed `FUTEX_WAIT` or
    /// `FUTEX_WAIT_BITSET` (`futex_wait_restart`). Its restart code is then
    /// `ERESTART_RESTARTBLOCK`, and
    /// Detcore keeps the deadline in a [`RestartBlock`] for the `restart_syscall` that
    /// the kernel runs next (<https://github.com/rrnewton/hermit/issues/3358>).
    ///
    /// Such a wait is also not ended by a default job-control stop signal
    /// (`KernelSignalState::interrupting_wait`), so that a stop Linux discards in an
    /// orphaned process group does not end the call before its deadline.
    fn restart_keeps_deadline(&self) -> bool {
        false
    }

    /// Signals the wait itself accepts rather than being interrupted by, read from
    /// guest memory. Only `rt_sigtimedwait` has any.
    fn signals_consumed_by_wait<M: MemoryAccess>(&self, _memory: &M) -> KernelSigset {
        0
    }

    /// Convert a physical nonblocking completion into the result expected by the guest.
    /// `retried` is true after a prior result was classified as blocked.
    fn normalize_nonblocking_result(
        &self,
        res: Result<i64, Errno>,
        _retried: bool,
    ) -> Result<i64, Errno> {
        res
    }
}

/// A system call which can logically timeout and then would return a given value
/// indicating that timeout.
pub trait TimeoutableSyscall: SyscallInfo {
    /// What would the syscall return IF it timed out?
    fn timeout_return_val(&self) -> Result<i64, Errno>;
}

fn interrupted_write_result<C: NonblockableSyscall>(
    call: &C,
    written_total: usize,
    disposition: Option<WaitSignalDisposition>,
) -> Option<Result<i64, Error>> {
    match disposition {
        None => None,
        Some(_) if written_total > 0 => Some(Ok(written_total as i64)),
        Some(_) => Some(Err(call.signal_interrupt_errno().into())),
    }
}

fn pipe_writev_resources(dettid: DetTid, call: reverie::syscalls::Writev) -> Resources {
    let mut resources = Resources::new(dettid);
    resources.insert(ResourceID::InternalIOPolling, Permission::W);
    resources.fyi(call.name());
    resources.set_signal_interrupt_errno(call.signal_interrupt_errno());
    // Polls with every ordinary signal blocked and checks the target's
    // disposition itself after a `Signaled` wake, so the scheduler's drain
    // must not filter by the kernel mask (`execute_blocking_pipe_writev`).
    resources.decides_signal_interruption_itself = true;
    resources.polls_guest_state_only = true;
    resources
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Poll {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        _guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        (self.with_timeout(0), None)
    }

    fn signal_interrupt_errno(&self) -> Errno {
        Errno::EINTR
    }

    /// Linux ends an interrupted `poll` with a restart code that a handler turns into
    /// `EINTR` and that restarts the call when no handler runs.
    ///
    /// With a positive timeout that code is Linux's own, `ERESTART_RESTARTBLOCK`: the
    /// kernel then runs `restart_syscall`, which Detcore resumes with the deadline it
    /// kept ([`RestartBlock`]), as `do_restart_poll` does
    /// (https://github.com/rrnewton/hermit/issues/3358). An infinite timeout has no
    /// deadline to keep, so it returns `ERESTARTNOHAND`, and running `poll` again with
    /// its original arguments is the same restart.
    fn kernel_restart_errno(&self) -> Errno {
        if self.restart_keeps_deadline() {
            Errno::ERESTART_RESTARTBLOCK
        } else {
            Errno::ERESTARTNOHAND
        }
    }

    /// A zero timeout never blocks, and a negative one never expires.
    fn restart_keeps_deadline(&self) -> bool {
        self.timeout() > 0
    }
}

impl TimeoutableSyscall for reverie::syscalls::Poll {
    fn timeout_return_val(&self) -> Result<i64, Errno> {
        Ok(0)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Ppoll {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        let (tp, guard) = zero_timespec(guest).await;
        // SAFETY: `tp` points to exclusively owned scratch storage kept alive by `guard`.
        let tp = unsafe { tp.into_mut() };
        (self.with_timeout(Some(tp)), Some(guard))
    }

    fn signal_interrupt_errno(&self) -> Errno {
        Errno::EINTR
    }

    /// Linux ends an interrupted `ppoll` with a restart code that a handler turns into
    /// `EINTR` and that restarts the call when no handler runs.
    fn kernel_restart_errno(&self) -> Errno {
        Errno::ERESTARTNOHAND
    }
}

impl TimeoutableSyscall for reverie::syscalls::Ppoll {
    fn timeout_return_val(&self) -> Result<i64, Errno> {
        Ok(0)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::EpollWait {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        _guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        (self.with_timeout(0), None)
    }

    fn signal_interrupt_errno(&self) -> Errno {
        Errno::EINTR
    }
}

impl TimeoutableSyscall for reverie::syscalls::EpollWait {
    fn timeout_return_val(&self) -> Result<i64, Errno> {
        Ok(0)
    }
}

// `epoll_pwait` was the one member of the
// poll/epoll family with no nonblocking form, even though `Ppoll` -- the
// sigmask variant of `poll` -- has had one all along. Programs that issue
// `epoll_pwait` DIRECTLY (libuv does, which is how the `cmake` hang surfaced)
// therefore reached an unhandled path while the `EpollWait` impl above went
// unused by them. Note this is NOT glibc's `epoll_wait(2)` on x86_64: glibc
// only spells it `epoll_pwait` where `__NR_epoll_wait` is absent, which x86_64
// is not. With a NULL sigmask the two calls are semantically identical, so the
// nonblocking form is the same: timeout 0, EINTR, and a 0 (no events) timeout
// return.
#[async_trait]
impl NonblockableSyscall for reverie::syscalls::EpollPwait {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        _guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        (self.with_timeout(0), None)
    }

    fn signal_interrupt_errno(&self) -> Errno {
        Errno::EINTR
    }
}

impl TimeoutableSyscall for reverie::syscalls::EpollPwait {
    fn timeout_return_val(&self) -> Result<i64, Errno> {
        Ok(0)
    }
}

/// What to do with the copy of the standard `signal` this thread just took
/// (`rt_sigtimedwait`, a signalfd record), after which its queues held
/// `after` (when read): drop it, as a copy Linux discarded when it was sent
/// or a stale POSIX timer entry, or return it with another siginfo
/// (`DiscardedSignalControl::Take`). The scheduler pops and promotes either
/// way.
pub(crate) async fn take_outcome<T, G>(
    guest: &mut G,
    signal: i32,
    after: Option<(u64, u64)>,
) -> crate::scheduler::posix_timer_coalesce::TakeOutcome
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    use crate::scheduler::discarded_at_send::DiscardedSignalAnswer;
    use crate::scheduler::discarded_at_send::DiscardedSignalControl;
    use crate::scheduler::posix_timer_coalesce::TakeOutcome;
    let at = guest.thread_state().thread_logical_time.as_nanos();
    match crate::tool_global::discarded_signal(
        guest,
        DiscardedSignalControl::Take { signal, at, after },
    )
    .await
    {
        DiscardedSignalAnswer::Taken(outcome) => outcome,
        _ => TakeOutcome {
            skip: false,
            rewrite: None,
        },
    }
}

async fn zero_timespec<'stack, T: RecordOrReplay, G: Guest<Detcore<T>>>(
    guest: &mut G,
) -> (Addr<'stack, Timespec>, <G::Stack as Stack>::StackGuard) {
    let mut stack = guest.stack().await;
    let tp_val = Timespec {
        tv_sec: 0,
        tv_nsec: 0,
    };
    let tp = stack.push(tp_val);
    let guard = stack.commit().expect("stack.commit to succeed");
    (tp, guard)
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Wait4 {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        _guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        let call2 = self.with_options(self.options() | WaitPidFlag::WNOHANG);
        (call2, None)
    }

    // Child has not changed state yet, so we go to the scheduler and wait to poll again.
    // In scenarios with lots of outstanding waits, this polling strategy can change the asymptotic
    // complexity of the program. Ideally, we would model the blocking `wait4` (and process state
    // transitions) directly in the scheduler, and execute it only when we know it will complete.
    //
    // The polling backoff strategy mitigates this problem however.
    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Ok(0)
    }
}

#[async_trait]
/// Used only for FUTEX_WAIT
impl NonblockableSyscall for reverie::syscalls::Futex {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        let (tp, guard) = zero_timespec(guest).await;
        (self.with_timeout(Some(tp)), Some(guard))
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        // EAGAIN can mean the futex wait's compare-and-block failed and we should return that to
        // the guest.  With timeout=0, the timeout is what shows that it would have blocked.
        res == Err(Errno::ETIMEDOUT)
    }

    /// Linux restarts an untimed `FUTEX_WAIT` under `SA_RESTART` (`-ERESTARTSYS`), but a
    /// timed wait returns `-ERESTART_RESTARTBLOCK`, which a handler always turns into
    /// `EINTR` (https://github.com/rrnewton/hermit/issues/3146).
    ///
    /// A timed `FUTEX_WAIT` or `FUTEX_WAIT_BITSET` returns Linux's code: with no
    /// handler the kernel then runs `restart_syscall`, which Detcore resumes with the
    /// deadline it kept ([`RestartBlock`]), as `futex_wait_restart` does
    /// (https://github.com/rrnewton/hermit/issues/3358). Linux's `futex_wait` keeps
    /// the absolute deadline of every timed wait in the restart block, so a
    /// `FUTEX_WAIT_BITSET` deadline the guest moves in memory after the wait began
    /// does not change when the restarted wait ends. `FUTEX_WAIT_BITSET` used to
    /// return `ERESTARTNOHAND`, which ran the call again with its original
    /// arguments and so read the deadline again (round-10 finding Medium 4 on
    /// https://github.com/rrnewton/hermit/pull/3361).
    fn kernel_restart_errno(&self) -> Errno {
        if self.restart_keeps_deadline() {
            Errno::ERESTART_RESTARTBLOCK
        } else if self.timeout().is_some() {
            Errno::ERESTARTNOHAND
        } else {
            Errno::ERESTARTSYS
        }
    }

    /// Every timed wait: `FUTEX_WAIT`'s relative timeout and `FUTEX_WAIT_BITSET`'s
    /// absolute deadline both become the absolute deadline Linux's restart keeps.
    fn restart_keeps_deadline(&self) -> bool {
        matches!(
            self.futex_op() & libc::FUTEX_CMD_MASK,
            libc::FUTEX_WAIT | libc::FUTEX_WAIT_BITSET
        ) && self.timeout().is_some()
    }
}

impl TimeoutableSyscall for reverie::syscalls::Futex {
    fn timeout_return_val(&self) -> Result<i64, Errno> {
        Err(Errno::ETIMEDOUT)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::RtSigtimedwait {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        // This is a bit more complicated because we need a new timespec to point to in
        // the guest memory.
        let (tp, guard) = zero_timespec(guest).await;
        (self.with_timeout(Some(tp)), Some(guard))
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN)
    }

    fn signal_interrupt_errno(&self) -> Errno {
        Errno::EINTR
    }

    /// Signals in the wait's own set are accepted by it, not interrupting. A set
    /// that cannot be read counts as empty, which nothing observes: the wait's
    /// first probe runs before any pending signal is considered
    /// (`KernelSignalWait::inject_first_probe`) and fails with `EFAULT`, as Linux
    /// fails the call when it cannot copy the set.
    fn signals_consumed_by_wait<M: MemoryAccess>(&self, memory: &M) -> KernelSigset {
        self.set()
            .and_then(|set| memory.read_value(set.cast::<KernelSigset>()).ok())
            .unwrap_or(0)
    }
}

impl TimeoutableSyscall for reverie::syscalls::RtSigtimedwait {
    fn timeout_return_val(&self) -> Result<i64, Errno> {
        Err(Errno::EAGAIN)
    }
}

/// While the read syscall is quite general, this nonblocking capacity is used
/// ONLY for sockets and pipes.
#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Read {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        // A return value of Ok(0) indicates end of file.
        // Note that we've ruled out 0-count reads before this point.
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

/// While the read syscall is quite general, this nonblocking capacity is used
/// ONLY for sockets and pipes.
#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Write {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        // A return value of Ok(0) indicates end of file.
        // Note that we've ruled out 0-count reads before this point.
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#794)
/// Vectored reads have the same blocking behavior as scalar reads on pipes and sockets.
#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Readv {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#547)
/// Vectored writes have the same blocking behavior as scalar writes on pipes and sockets.
#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Writev {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

/// A common helper shared among several network syscalls.
/// We can't actually CONVERT these syscalls into nonblocking, but we can assert that they are by
/// checking the status of their file descriptor.
fn network_comm_syscall<T: RecordOrReplay, G: Guest<Detcore<T>>, C: SyscallInfo + Into<Syscall>>(
    call: C,
    guest: &mut G,
) -> (C, Option<<G::Stack as Stack>::StackGuard>) {
    // Already nonblocking because we've assured the socket is.
    let fd = get_fd(call.into()).unwrap_or_else(|| {
        panic!(
            "network_comm_syscall called on invalid syscall / unknown fd: {}",
            call.name()
        );
    });
    guest
        .thread_state()
        .with_detfd(fd, |detfd| {
            assert!(
                detfd.physically_nonblocking(),
                "expecting sockets/pipes to be physically nonblocking"
            );
        })
        .unwrap();
    (call, None)
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Accept4 {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

impl TimeoutableSyscall for reverie::syscalls::Accept4 {
    fn timeout_return_val(&self) -> Result<i64, Errno> {
        Ok(0)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Recvfrom {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Recvmsg {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Recvmmsg {
    // This system call has a timeout argument, but we ignore it because the underlying
    // socket is nonblocking anyway (in runs where we call this).
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Sendto {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Sendmmsg {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Sendmsg {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN) || res == Err(Errno::EWOULDBLOCK)
    }
}

#[async_trait]
impl NonblockableSyscall for reverie::syscalls::Connect {
    async fn into_nonblocking<T: RecordOrReplay, G: Guest<Detcore<T>>>(
        self,
        guest: &mut G,
    ) -> (Self, Option<<G::Stack as Stack>::StackGuard>) {
        network_comm_syscall(self, guest)
    }

    fn syscall_would_have_blocked(&self, res: Result<i64, Errno>) -> bool {
        res == Err(Errno::EAGAIN)
            || res == Err(Errno::EWOULDBLOCK)
            || res == Err(Errno::EINPROGRESS)
            || res == Err(Errno::EALREADY)
    }

    fn normalize_nonblocking_result(
        &self,
        res: Result<i64, Errno>,
        retried: bool,
    ) -> Result<i64, Errno> {
        match (retried, res) {
            (true, Err(Errno::EISCONN)) => Ok(0),
            (_, res) => res,
        }
    }
}

/// Transform a syscall to nonblocking, then retry it until it returns a successful result.
/// Retry a nonblockizable syscall (e.g. a pipe/socket read or write) until it succeeds.
///
/// `subtool` selects how each poll iteration executes the underlying syscall. Pass
/// `Some(detcore)` in record/replay mode for a container-INTERNAL fd (currently pipes):
/// each iteration is then routed through `Detcore::record_or_replay`, so the read's
/// bytes (and every intervening `EAGAIN`) are captured in the recording and reproduced
/// verbatim on replay. Without this, an internal-pipe read on the InternalIOPolling path
/// bypasses the recorder and replay reads live from a pipe whose cross-process writer
/// schedule is not reproduced -- the reader sees EOF instead of the recorded data and
/// replay desyncs. Pass `None` for plain `hermit run` (no recording) or for external fds.
pub async fn retry_nonblocking_syscall<T, G, C>(
    guest: &mut G,
    call: C,
    rsrc: Resources,
    subtool: Option<&Detcore<T>>,
) -> Result<i64, Error>
where
    C: NonblockableSyscall + Into<Syscall>,
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    // Bogus 99 return value is dead code below:
    retry_nonblocking_syscall_helper(guest, call, rsrc, None, subtool).await
}

/// What a wait that ended with `ERESTART_RESTARTBLOCK` keeps for the
/// `restart_syscall` that Linux runs next when no handler runs
/// (`NonblockableSyscall::restart_keeps_deadline`). Linux keeps the call and its
/// absolute deadline in the thread's restart block (`do_restart_poll`,
/// `futex_wait_restart`). Detcore emulates the wait, so the kernel's restart block
/// does not describe it, and Detcore keeps its own
/// (https://github.com/rrnewton/hermit/issues/3358).
///
/// The record lives until the thread's next syscall, which takes it
/// (`Detcore::handle_syscall_event`). Other first syscalls, sigreturn and exec
/// discard the record; separate threads and newly initialized children have
/// independent records. A caught handler does not discard it: Linux resets its
/// restart block at sigreturn (`restore_sigcontext`), not when it sets up the
/// handler, so a handler whose first syscall is `restart_syscall` resumes the
/// interrupted wait with the copied arguments and deadline. Detcore does the
/// same when that call stops at the interrupted call's address, as it does
/// through libc's `syscall()` (the `restartfirst` mode of the
/// `external_signal_interrupt` guest). Linux also keeps its restart block across
/// the handler's other syscalls and resumes it from any address; Detcore does
/// not, so a `restart_syscall` after another syscall or at another address takes
/// the unsupported-syscall policy.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct RestartBlock {
    /// The instruction pointer at the interrupted call's syscall stop: the address
    /// after its `syscall` instruction. The kernel's restart moves the instruction
    /// pointer back onto that instruction with `restart_syscall` in `rax`, so the
    /// restart stops at the same address.
    pub(crate) rip: u64,
    /// The wait's absolute deadline.
    pub(crate) deadline: Option<LogicalTime>,
    /// The interrupted call.
    pub(crate) call: RestartCall,
}

/// A call whose restart keeps its deadline.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum RestartCall {
    Poll(syscalls::Poll),
    Futex(syscalls::Futex),
    /// A relative `nanosleep` or `clock_nanosleep` that ended for a claimed
    /// `SIGCHLD` (`syscalls::signal::SleepSignal::Claimed`).
    Nanosleep(reverie::syscalls::family::NanosleepFamily),
}

impl RestartBlock {
    /// The record, if a `restart_syscall` that stopped at `rip` is the kernel's
    /// restart of the interrupted call. At any other address the guest made the
    /// call itself.
    pub(crate) fn resumed_at(self, rip: u64) -> Option<Self> {
        (self.rip == rip).then_some(self)
    }
}

/// Keeps `deadline` for the kernel's restart of `call` when the wait ended with
/// `ERESTART_RESTARTBLOCK` (`RestartBlock`).
pub(crate) async fn keep_restart_block<T, G>(
    guest: &mut G,
    result: &Result<i64, Error>,
    call: RestartCall,
    deadline: Option<LogicalTime>,
) where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    if matches!(result, Err(Error::Errno(errno)) if *errno == Errno::ERESTART_RESTARTBLOCK) {
        let rip = guest.regs().await.rip;
        let block = RestartBlock {
            rip,
            deadline,
            call,
        };
        tracing::trace!(
            "[tid {}] the wait keeps {:?} for its restart",
            guest.tid(),
            block
        );
        guest.thread_state_mut().restart_block = Some(block);
    }
}

/// The restart code that ends a wait for a signal the backend holds and delivers
/// when the guest resumes. The backend passes a held signal on without reporting it
/// to the tool when the call returns `ERESTART_RESTARTBLOCK`, so for any signal but
/// `SIGSTOP`, which it never reports, the wait returns `ERESTARTNOHAND` instead. A
/// handler turns either code into `EINTR`, and a signal that kills the process
/// leaves no restart. The difference is a stop, which `SIGSTOP` alone can be here:
/// a default job-control stop does not end a wait whose restart keeps its deadline
/// (`KernelSignalState::interrupting_wait`).
///
/// A claimed relative `nanosleep` makes the opposite choice on purpose
/// (`syscalls::time`, `RestartCall::Nanosleep`): it returns
/// `ERESTART_RESTARTBLOCK`, so the held `SIGCHLD` is passed on unreported, and
/// the kernel's `restart_syscall` alone says no handler ran
/// (https://github.com/rrnewton/hermit/issues/4346).
fn held_signal_restart_errno(restart_errno: Errno, signal: i32) -> Errno {
    if restart_errno == Errno::ERESTART_RESTARTBLOCK && signal != libc::SIGSTOP {
        Errno::ERESTARTNOHAND
    } else {
        restart_errno
    }
}

/// The deadline a polling request carries so the scheduler can restore a
/// backed-off poller once committed virtual time passes it
/// (`Resources::poll_deadline`, <https://github.com/rrnewton/hermit/issues/3952>).
/// `None` under record/replay, whose schedule must not change.
pub(crate) fn poll_deadline_for<T, G>(
    guest: &G,
    deadline: Option<LogicalTime>,
) -> Option<LogicalTime>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    if guest.config().recordreplay_modes {
        None
    } else {
        deadline
    }
}

/// Retry a non-blocking syscall until it succeeds. Set the timeout to zero for the actual
/// syscalls (retries), while monitoring the clock to see if/when the logical timeout
/// should trigger.  Timeout is passed as an ABSOLUTE TIME (not duration).
pub async fn retry_nonblocking_syscall_with_timeout<T, G, C>(
    guest: &mut G,
    call: C,
    rsrc: Resources,
    // Logical timeout:
    maybe_timeout: Option<LogicalTime>,
) -> Result<i64, Error>
where
    C: NonblockableSyscall + TimeoutableSyscall + Into<Syscall>,
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let maybe_tup = maybe_timeout.map(|t| (t, call.timeout_return_val()));
    if guest
        .config()
        .backend
        .supports_blocked_wait_signal_interruption
    {
        return retry_blocking_wait_with_kernel_signal_state(guest, call, rsrc, maybe_tup).await;
    }
    // poll/epoll_wait/futex/rt_sigtimedwait keep their existing execution (raw
    // inject_with_retry): their record/replay handling is out of scope for the internal
    // pipe data-ordering fix, and their fds are not necessarily internal pipes.
    retry_nonblocking_syscall_helper(guest, call, rsrc, maybe_tup, None).await
}

/// The task whose kernel signal state the scheduler may read to decide which
/// signal ends an internal IO poll: the guest thread itself where guest thread
/// IDs name host tasks, and `None` elsewhere (KVM), where
/// `/proc/<pid>/task/<tid>` would describe an unrelated host task.
pub(crate) fn guest_signal_state_task<T, G>(guest: &G) -> Option<(i32, i32)>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    crate::procfs_inode::guest_tids_name_host_tasks(&guest.config().backend, None)
        .then(|| (guest.pid().as_raw(), guest.tid().as_raw()))
}

/// Whether a signal that woke a poll would be delivered to this thread now,
/// read from the backend's own state of the thread: its current mask (the
/// old set of a query-only `rt_sigprocmask`) and each signal's disposition
/// (`wait_signal_disposition`). A blocked or ignored signal, or one whose
/// default action ignores it, does not.
async fn own_disposition_interrupts<T, G>(
    guest: &mut G,
    status: ResumeStatus,
    mask_addr: AddrMut<'_, KernelSigset>,
    action_addr: AddrMut<'_, KernelSigaction>,
) -> Result<bool, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let query = syscalls::RtSigprocmask::new()
        .with_how(libc::SIG_BLOCK)
        .with_set(None)
        .with_oldset(Some(mask_addr.cast()))
        .with_sigsetsize(KERNEL_SIGSET_SIZE);
    guest.inject_with_retry(query).await?;
    let mask: KernelSigset = guest.memory().read_value(mask_addr)?;
    Ok(
        wait_signal_disposition(guest, status, &mask, action_addr, true)
            .await?
            .is_some(),
    )
}

// Private helper.
async fn retry_nonblocking_syscall_helper<T, G, C>(
    guest: &mut G,
    call0: C,
    rsrc: Resources,
    maybe_timeout: Option<(LogicalTime, Result<i64, Errno>)>,
    subtool: Option<&Detcore<T>>,
) -> Result<i64, Error>
where
    C: NonblockableSyscall + Into<Syscall>,
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    // The signals the wait takes from the queue (rt_sigtimedwait's set).
    let consumed = call0.signals_consumed_by_wait(&guest.memory());
    // The stack-allocated memory here needs to live across the loop, which means
    // surviving multiple syscall injections:
    let (mut call, mut maybe_stackguard) = call0.into_nonblocking(guest).await;
    let mut rsrc = rsrc.clone();
    rsrc.poll_deadline = poll_deadline_for(guest, maybe_timeout.map(|(deadline, _)| deadline));
    // A restartable call parked in internal IO polling, such as a `read` of an
    // empty pipe, is interrupted by a signal another task sends, as a parked
    // pipe write is (`pipe_writev_resources`). The marker lets the scheduler
    // record the signal for it (`Scheduler::notify_signal_pending`) and answer
    // its next turn `Signaled`; the call then returns `ERESTARTSYS`, and the
    // kernel applies the guest's disposition when it resumes: a handler runs
    // and the call fails with `EINTR` or restarts under `SA_RESTART`. Without
    // it, the poll never saw the signal and waited forever
    // (<https://github.com/rrnewton/hermit/issues/4187>).
    if rsrc.signal_interrupt_errno().is_none()
        && rsrc.resources.len() == 1
        && rsrc.resources.contains_key(&ResourceID::InternalIOPolling)
        && call.signal_interrupt_errno() == Errno::ERESTARTSYS
    {
        rsrc.set_signal_interrupt_errno(Errno::ERESTARTSYS);
        // Where guest thread IDs are not host tasks (KVM) the scheduler keeps
        // every signal: the call has made no progress when it returns
        // ERESTARTSYS, and the backend restarts it unless a signal it would
        // deliver is pending, so a blocked or ignored signal leaves it waiting.
        rsrc.signal_state_task = guest_signal_state_task(guest);
    }
    let checks_at_turn = rsrc.signal_interrupt_errno() == Some(Errno::ERESTARTSYS.into_raw())
        && decides_interruption_at_turn(guest, rsrc.signal_state_task);
    // Where the turn decides, the call owns the guest's mask while it blocks
    // every blockable signal (`KernelSignalWait::block_in_place`), from the
    // first turn that finds a pending signal a private injection could take
    // until the call returns: two such signals once stopped the probe and
    // its retry in turn, and the backend's single held slot kept only the
    // second, so one handler never ran (Claude review of the R2b-2 design,
    // F1; https://github.com/rrnewton/hermit/issues/4273, stage 2).
    let mut turn_wait = checks_at_turn
        .then(|| KernelSignalWait::new(guest, 0, false, call.signal_interrupt_errno()));
    let mut published = PublishedWaitMask::default();

    let result: Result<i64, Error> = loop {
        let resumed = match call.into() {
            Syscall::Read(read) if maybe_timeout.is_none() && maybe_stackguard.is_none() => {
                crate::tool_global::polled_read_request(guest, read, rsrc.clone()).await
            }
            _ => resource_request(guest, rsrc.clone()).await,
        };
        // The turn's signals, read before its first injection
        // (`read_turn_signals`): the timerfd publication and the real-time
        // action below may inject, and a pending copy could stop that
        // injection and move into the backend's held slot. Every turn reads,
        // the first included, since a signal sent before the poll filed its
        // first request was admitted by no drain
        // (https://github.com/rrnewton/hermit/issues/4273). A backend's own
        // observation of a signal (`Signaled(None)`) is trusted as it is: that
        // signal has already left the kernel's queue.
        // Once the call blocks, the turn decides under the guest's own mask,
        // which the call owns, never under the full one now installed (Codex
        // review of the R2b-2 design rev3, P2-1).
        let turn = if checks_at_turn && !matches!(resumed, ResumeStatus::Signaled(None)) {
            match turn_signals_with_block(
                guest,
                &mut turn_wait,
                true,
                committed_signals(&resumed),
                rsrc.private_sigchld_decides,
                &mut published,
            )
            .await
            {
                Ok(turn) => Some(turn),
                Err(error) => break Err(error),
            }
        } else {
            None
        };
        // A virtual timerfd this probe may observe is readable on the host
        // exactly when its virtual deadline has passed by this turn
        // (`crate::syscalls::timerfd`); its next deadline restores this
        // poller if it backs off.
        // Publishing may take the scratch stack, which the nonblocking form
        // holds; release it first and convert again after.
        let reconvert =
            maybe_stackguard.is_some() && crate::syscalls::timerfd::may_publish(guest, call.into());
        if reconvert {
            maybe_stackguard = None;
        }
        rsrc.poll_deadline = crate::syscalls::timerfd::publish_before_probe(
            guest,
            call.into(),
            maybe_timeout.map(|(deadline, _)| deadline),
        )
        .await;
        if reconvert {
            (call, maybe_stackguard) = call0.into_nonblocking(guest).await;
        }
        // A signal ends the call only if it would still block, as Linux
        // checks for a pending signal only when the call must wait: a byte
        // written, or the last writer closed, before the signal is read
        // first. The probe below goes through the record/replay path like
        // every attempt (Codex review of
        // https://github.com/rrnewton/hermit/pull/4198).
        // This call transfers nothing until it completes, so only a copy still
        // pending ends it (`TurnSignals::ends`).
        let signaled = match turn {
            Some(turn) => turn.ends(false),
            None => matches!(resumed, ResumeStatus::Signaled(_)),
        };
        // A pending real-time timer expiration in the set this wait takes
        // (https://github.com/rrnewton/hermit/issues/4089).
        let pending = pending_real_time_among(guest, consumed).await;
        act_on_pending_real_time(guest, pending, consumed, 0).await;
        // Route through the record/replay subtool for internal pipes so each poll (an
        // EAGAIN, or the final data-bearing read) becomes one recorded event that replay
        // reproduces deterministically; otherwise execute the syscall directly.
        let res = match subtool {
            Some(detcore) => {
                detcore
                    .record_or_replay_preserving_tool_errors(guest, call)
                    .await
            }
            None => guest.inject_with_retry(call).await.map_err(Error::from),
        };
        // Under record and replay the attempt is a plain injection, which the
        // signal that woke this call, still pending, stops before it runs
        // (ERESTARTSYS, not run): the call then returns that restart errno,
        // as on main, though a byte, end of file or room may have been ready.
        // Making the attempt again would replace the held signal with any
        // other pending one (https://github.com/rrnewton/reverie/issues/1012),
        // and record and replay could not agree on when it is safe.
        let syscall_result = match res {
            Ok(value) => Ok(value),
            Err(Error::Errno(error)) => Err(error),
            Err(error) => break Err(error),
        };
        if signaled && call.syscall_would_have_blocked(syscall_result) {
            let errno = call.signal_interrupt_errno();
            tracing::trace!(
                "retry_nonblocking_syscall: interrupted by signal while {} would block: {:?}",
                call.display(&guest.memory()),
                errno
            );
            break Err(errno.into());
        }
        if call.syscall_would_have_blocked(syscall_result) {
            rsrc.poll_attempt += 1;
            if let Some((timeout, timeout_result)) = maybe_timeout {
                let new_time = thread_observe_time(guest).await;
                if new_time >= timeout {
                    tracing::trace!(
                        "Timing out syscall after #{} retries: {}",
                        rsrc.poll_attempt - 1,
                        call.display(&guest.memory())
                    );
                    break timeout_result.map_err(|e| e.into());
                } else {
                    tracing::trace!(
                        "Retry #{} for syscall due to result {:?}, {} from timeout: {}",
                        rsrc.poll_attempt,
                        syscall_result,
                        timeout - new_time,
                        call.display(&guest.memory())
                    );
                    record_retry_event(guest, call).await;
                }
            } else {
                tracing::trace!(
                    "Retry #{} for syscall due to result {:?}: {}",
                    rsrc.poll_attempt,
                    syscall_result,
                    call.display(&guest.memory())
                );
                record_retry_event(guest, call).await;
            }
        } else {
            let res = call
                .normalize_nonblocking_result(syscall_result, rsrc.poll_attempt > 0)
                .map_err(|e| e.into());
            tracing::trace!(
                "retry_nonblocking_syscall: syscall completed after {} retries: {} = {:?}",
                rsrc.poll_attempt,
                call.display(&guest.memory()),
                res
            );
            break res;
        }
    };

    // The single exit: the guest's mask goes back before the result is
    // returned. A mask `block_in_place` installed goes back through
    // `Guest::set_blocked_signals`, with no scratch memory, and only a
    // refusal falls back to an injected restoration (`KernelSignalWait::restore`).
    // A mask that cannot be put back ends the run before the guest resumes.
    match turn_wait {
        Some(mut wait) if wait.effective_mask().is_some() => {
            let restored = wait.restore(guest, None).await;
            publish_wait_mask(guest, None, &mut published).await;
            match result_after_restore(restored, result) {
                Ok(result) => refuse_held_signal_loss(guest, call0.number(), result).await,
                Err(error) => Err(error),
            }
        }
        _ => result,
    }
}

/// Retry a blocking wait on a backend that decides signal interruption from the
/// kernel's signal state (`supports_blocked_wait_signal_interruption`).
///
/// A wait ends early only for a signal that would end it natively: one the guest
/// does not block and that is caught, or whose default action terminates or stops
/// the process (https://github.com/rrnewton/hermit/issues/3146). Blocked,
/// ignored, and default-ignored signals do not end it, and neither does a default
/// job-control stop when Linux's restart of the call keeps its deadline
/// (`NonblockableSyscall::restart_keeps_deadline`). Such a signal that stops one
/// of the wait's injections is absorbed, and the wait runs on. A stop that
/// `KernelSignalWait::inject_absorbing` cannot absorb ends the wait with a
/// transparent restart when the backend holds nothing whose loss matters, and
/// otherwise with a [`HeldSignalRefusal`], which `refuse_held_signal_loss`
/// turns into the unsupported-operation refusal once the guest's mask is back.
///
/// The first probe runs under the guest's own mask, and before the first check
/// for a pending signal (`KernelSignalWait::inject_first_probe`): Linux reports a
/// descriptor that is ready when the call begins, a queued event, and an
/// argument error before it looks at pending signals, so the probe's result
/// stands whenever the signal arrived. If it would block, every blockable signal
/// is blocked for the rest of the wait, so later probes cannot be stopped by one
/// and every signal that arrives stays pending in the kernel, where `/proc`
/// reports it. Each turn classifies the pending set against the guest's mask and
/// dispositions, and an interrupting signal ends the wait with the call's restart
/// errno (see `KernelSignalWait::interrupted_with_state`). The guest's mask is
/// restored before returning, so the signal is delivered as the call returns.
/// The signals a scheduler resume names (`ResumeStatus::Signaled`): signals the
/// scheduler committed to the thread at that point.
fn committed_signals(resumed: &ResumeStatus) -> KernelSigset {
    match resumed {
        ResumeStatus::Signaled(Some(signals)) => signals.iter().fold(0, |set, signal| {
            set | kernel_sigset_bit(signal.0 as libc::c_int)
        }),
        _ => 0,
    }
}

/// What a restartable internal IO poll or a parked pipe write finds at a turn,
/// read from the thread's own state before the turn's first injection, while it
/// is still stopped at its request: every signal sent since is still in the
/// kernel's pending set, and none is yet in the backend's held slot. A copy
/// the turn's own injections later absorb is counted here
/// (<https://github.com/rrnewton/hermit/issues/4273>, R2b-1 rules 2-4).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct TurnSignals {
    /// The signals the turn's answer names (`committed_signals`) that the
    /// call's mask leaves unblocked: a kept send's wake, which Linux's
    /// `TIF_SIGPENDING` keeps in force until the call returns, whatever a later
    /// flush does. For `writev`, which the drain never filters, a real-time
    /// signal, outside the record of copies discarded at send, counts only if
    /// its disposition delivers it now (`NamedRealTime::ByDisposition`); for
    /// the generic poll and the scalar writer the drain's own disposition test
    /// has already decided it, at the wake (R2b-1 rules 1 and 2).
    named: KernelSigset,
    /// The kept copies pending now that would end the call: unblocked under the
    /// call's mask, caught or acting by default, not held (`SIGCHLD` and the
    /// host-timed signals, except one the scheduler committed:
    /// `futex_wait_held_after_wake`), and not discarded at send
    /// (`threads::without_discarded`).
    pending: KernelSigset,
}

impl TurnSignals {
    /// Whether a call that would wait now ends. After progress (`progress`),
    /// Linux returns the bytes transferred on any wake, so a named wake or a
    /// pending copy ends the call. With none, Linux returns `-ERESTARTSYS`,
    /// which delivers a pending copy or restarts invisibly, so only a pending
    /// copy ends it; otherwise the call keeps polling, which the guest cannot
    /// tell from that restart (R2b-1 rule 3).
    fn ends(&self, progress: bool) -> bool {
        self.pending != 0 || (progress && self.named != 0)
    }
}

/// [`TurnSignals`] for a call whose mask is `mask` (the kernel's, by default:
/// the generic poll and the scalar writer install none) and to which the
/// scheduler committed `committed` at this wake (`committed_signals`; none
/// before a turnless first attempt). A thread that is gone ends the call with
/// `ERESTARTNOINTR`, which nothing observes, and a live one whose state cannot
/// be read ends the run (`read_wait_signal_state`).
async fn read_turn_signals<T, G>(
    guest: &mut G,
    mask: Option<KernelSigset>,
    committed: KernelSigset,
    real_time: NamedRealTime,
) -> Result<TurnSignals, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    Ok(
        read_turn_signals_and_state(guest, mask, committed, real_time)
            .await?
            .0,
    )
}

/// `read_turn_signals`, also returning the thread's signal state that the
/// turn read.
async fn read_turn_signals_and_state<T, G>(
    guest: &mut G,
    mask: Option<KernelSigset>,
    committed: KernelSigset,
    real_time: NamedRealTime,
) -> Result<(TurnSignals, KernelSignalState), Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let state = read_wait_signal_state(guest.pid(), guest.tid())?;
    let mask = mask.unwrap_or(state.blocked);
    let host_timed = host_timed_signals(guest).await;
    Ok((
        turn_signals_from_state(guest, &state, mask, host_timed, committed, real_time, 0).await,
        state,
    ))
}

/// [`TurnSignals`] from the thread's state `state` read in this turn, with the
/// record of copies Linux discarded at send applied
/// (`threads::without_discarded`); `private` as for
/// `turn_signals_before_discard`.
async fn turn_signals_from_state<T, G>(
    guest: &mut G,
    state: &KernelSignalState,
    mask: KernelSigset,
    host_timed: KernelSigset,
    committed: KernelSigset,
    real_time: NamedRealTime,
    private: KernelSigset,
) -> TurnSignals
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let before =
        turn_signals_before_discard(state, mask, host_timed, committed, real_time, private);
    let pending =
        crate::syscalls::threads::without_discarded(guest, state, before.pending, committed).await;
    TurnSignals { pending, ..before }
}

/// A restartable poll's turn on a pipe or a socket that decides at its turn:
/// read the thread's signal state before the turn's first injection, block
/// every blockable signal in place if one pending could stop that injection
/// and `may_block` (`KernelSignalWait::block_in_place`, stage 2 of
/// https://github.com/rrnewton/hermit/issues/4273), then decide the turn's
/// signals under the guest's own mask, which the call owns once it blocks.
///
/// A `SIGCHLD` a guest sent the thread, which the schedule recorded in its
/// private queue, ends the call where `private_decides`
/// (`Resources::private_sigchld_decides`), as on Linux (stage 3): it is
/// admitted only while the block holds (`saved_mask`), so no injection of
/// this turn can take it into the backend's single held slot, and only if
/// the read found it in the thread's own queue, from which only this stopped
/// thread can take it. Nothing runs between the read and the block. The
/// host-timed signals and the record come in one round trip, before any
/// injection (`host_timed_and_private_sigchld`).
async fn turn_signals_with_block<T, G>(
    guest: &mut G,
    wait: &mut Option<KernelSignalWait>,
    may_block: bool,
    committed: KernelSigset,
    private_decides: bool,
    published: &mut PublishedWaitMask,
) -> Result<TurnSignals, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let mask = wait.as_ref().and_then(KernelSignalWait::effective_mask);
    let state = read_wait_signal_state(guest.pid(), guest.tid())?;
    let (host_timed, recorded) = crate::tool_global::host_timed_and_private_sigchld(guest).await;
    let mut private = 0;
    if let Some(wait) = wait.as_mut() {
        // Block before the publication and the probe, so neither can be
        // stopped by a blockable signal, and every pending one stays in the
        // kernel's queue until the guest resumes.
        if may_block
            && wait.needs_block()
            && pending_could_stop_injection(&state)
            && wait.block_in_place(guest, host_timed).await?
        {
            publish_wait_mask(guest, wait.effective_mask(), published).await;
        }
        if private_decides
            && wait.saved_mask.is_some()
            && state.thread_pending & kernel_sigset_bit(libc::SIGCHLD) != 0
        {
            private = recorded;
        }
    }
    let mask = mask.unwrap_or(state.blocked);
    Ok(turn_signals_from_state(
        guest,
        &state,
        mask,
        host_timed,
        committed,
        NamedRealTime::Woken,
        private,
    )
    .await)
}

/// Whether a private injection could be stopped by a signal pending in
/// `state`: one pending, on the thread's or the process's queue, that the
/// thread's current mask leaves unblocked and that a mask can block. Such a
/// stop moves the signal into the backend's single held slot, where a second
/// stop replaces it (https://github.com/rrnewton/hermit/issues/4273, stage 2).
fn pending_could_stop_injection(state: &KernelSignalState) -> bool {
    state.pending & !state.blocked & blocked_signal_mask() != 0
}

/// [`TurnSignals`] from the thread's state (`state`), the call's mask (`mask`),
/// the host-timed signals (`host_timed`) and the scheduler's commitment
/// (`committed`), before the record of copies Linux discarded at send is
/// applied to `pending` (`threads::without_discarded`). The hold leaves out
/// `SIGCHLD` and every host-timed signal, except a signal the scheduler
/// committed that no host-timed source can post (`futex_wait_held_after_wake`),
/// and a recorded private `SIGCHLD` the turn admits (`private`).
fn turn_signals_before_discard(
    state: &KernelSignalState,
    mask: KernelSigset,
    host_timed: KernelSigset,
    committed: KernelSigset,
    real_time: NamedRealTime,
    private: KernelSigset,
) -> TurnSignals {
    use crate::scheduler::discarded_at_send::STANDARD_SIGNALS;
    // A `SIGCHLD` a guest sent this thread, which the schedule recorded in its
    // private queue, is not held where the turn admits it (`private`,
    // `turn_signals_with_block`).
    let held =
        crate::syscalls::threads::futex_wait_held_after_wake(host_timed, committed) & !private;
    let pending = state.pending_interrupting(mask, false) & !held;
    let counted = match real_time {
        NamedRealTime::Woken => !0,
        NamedRealTime::ByDisposition => {
            STANDARD_SIGNALS | (state.interrupting(mask) & !STANDARD_SIGNALS)
        }
    };
    let named = committed & !mask & counted;
    TurnSignals { named, pending }
}

/// How a turn counts a real-time signal its answer names
/// ([`TurnSignals::named`]).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum NamedRealTime {
    /// As any named signal: the drain woke the call only for one whose
    /// disposition delivered it then, and Linux's wake stands after a later
    /// flush (the generic poll and the scalar writer).
    Woken,
    /// Only if its disposition delivers it now: the drain never filters
    /// `writev` (R2b-1 rule 2).
    ByDisposition,
}

/// Whether a poll or a parked pipe write decides interruption at its own turn
/// (`signals_interrupting_poll`): where the record of copies Linux discarded
/// at send is kept (`discarded_at_send::records`, ptrace with sequentialized
/// threads) and the thread's state can be read (`task`, its
/// `Resources::signal_state_task`, which needs guest thread IDs that name host
/// tasks). Elsewhere the earlier decision stands: on KVM the backend restarts
/// the call unless it would deliver a signal, and its polled-read observation
/// (`Guest::polled_read_signal_site`) takes a caught signal before the turn,
/// where a `/proc` read could not see it (the Claude re-check of the R2b-1
/// design, R4-4).
fn decides_interruption_at_turn<T, G>(guest: &G, task: Option<(i32, i32)>) -> bool
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    task.is_some() && crate::scheduler::discarded_at_send::records(guest.config())
}

/// Install the private mask of a pipe `writev` that decides at its turn
/// (`decides_interruption_at_turn`) and goes on polling after its first
/// attempt, through `wait` (`KernelSignalWait::block`, scratch `cell`), and
/// publish the guest's own mask to the scheduler while it is installed
/// (`set_saved_guest_sigmask`). Returns the guest's own mask. A signal that
/// stops the installation is held as `inject_absorbing` holds one, a
/// host-timed one and a copy Linux discarded at send included
/// (`hold_until_return`), and one that would replace a
/// held signal whose loss matters ends the call: with the restart errno, as
/// `Error::Errno`, when the held one would end it natively, and otherwise with
/// a [`HeldSignalRefusal`], which the caller turns into the refusal once the
/// guest's mask is back (`refuse_held_signal_loss`; R2b-1 rule 5). A mask that
/// cannot be installed ends the call with `EFAULT`, as a failed
/// scratch-stack commit does.
async fn install_turn_mask<T, G>(
    guest: &mut G,
    wait: &mut KernelSignalWait,
    cell: AddrMut<'_, KernelSigset>,
) -> Result<KernelSigset, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    // A copy Linux discarded at send that the kernel still holds (a traced
    // task's copy is queued whatever its disposition) is left out of the
    // turn's `pending` (`without_discarded`), so it must not end the call
    // here either: it is held until the call returns, where it is delivered
    // late (https://github.com/rrnewton/hermit/issues/3995), as the turn
    // decided (the Claude re-check of R2b-1, N1).
    let state = read_wait_signal_state(guest.pid(), guest.tid())?;
    let kept = crate::syscalls::threads::without_discarded(guest, &state, state.pending, 0).await;
    wait.hold_until_return(host_timed_signals(guest).await | (state.pending & !kept));
    wait.block(guest, Some(cell)).await?;
    let Some(mask) = wait.effective_mask() else {
        return Err(Errno::EFAULT.into());
    };
    crate::tool_global::set_saved_guest_sigmask(guest, Some(mask)).await;
    Ok(mask)
}

/// The waiting thread's mask as last told to the scheduler
/// (`publish_wait_mask`). It is deliberately not `Copy`: a wait loop in an
/// `async move` block must reborrow it, or the block would publish into a
/// copy and the clear after the block would see nothing to clear.
#[derive(Debug, Default)]
pub(crate) struct PublishedWaitMask(Option<KernelSigset>);

/// Tell the scheduler the waiting thread's effective mask
/// (`KernelSignalWait::effective_mask`, or `None` once the wait is over) if it
/// differs from `published`, the last one told. The scheduler reads it in
/// place of the thread's `SigBlk` (`set_saved_guest_sigmask`), for example to
/// decide whether a leader ignores an `ITIMER_REAL` expiry at generation
/// (https://github.com/rrnewton/hermit/issues/4139). Only under signal-target
/// modelling, the only configuration whose scheduler reads it for a wait.
pub(crate) async fn publish_wait_mask<T, G>(
    guest: &mut G,
    mask: Option<KernelSigset>,
    published: &mut PublishedWaitMask,
) where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    if mask == published.0
        || !detcore_model::bundled_signal_facts::models_signal_targets(guest.config())
    {
        return;
    }
    crate::tool_global::set_saved_guest_sigmask(guest, mask).await;
    published.0 = mask;
}

async fn retry_blocking_wait_with_kernel_signal_state<T, G, C>(
    guest: &mut G,
    call0: C,
    rsrc: Resources,
    maybe_timeout: Option<(LogicalTime, Result<i64, Errno>)>,
) -> Result<i64, Error>
where
    C: NonblockableSyscall + Into<Syscall>,
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    retry_blocking_wait_under_mask(guest, call0, rsrc, maybe_timeout, None)
        .await
        .0
}

/// `retry_blocking_wait_with_kernel_signal_state` for a call that sleeps under
/// `wait_mask` instead of the guest's own mask, when it is set: the temporary
/// mask of an `epoll_pwait` (`KernelSignalWait::with_wait_mask`). `call0`
/// carries no mask, so its probes run under the guest's own mask until every
/// blockable signal is blocked, as for any other wait. Also returns whether the
/// signal that ended the wait must still be delivered under `wait_mask`: one
/// that only `wait_mask` lets in, pending in the kernel under the guest's own
/// mask once it is put back, which the caller delivers with
/// `inject_signal_delivery`, as a masked pselect6 does.
pub(crate) async fn retry_blocking_wait_under_mask<T, G, C>(
    guest: &mut G,
    call0: C,
    rsrc: Resources,
    maybe_timeout: Option<(LogicalTime, Result<i64, Errno>)>,
    wait_mask: Option<KernelSigset>,
) -> (Result<i64, Error>, bool)
where
    C: NonblockableSyscall + Into<Syscall>,
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let mut signals = KernelSignalWait::new(
        guest,
        call0.signals_consumed_by_wait(&guest.memory()),
        call0.restart_keeps_deadline(),
        call0.kernel_restart_errno(),
    )
    .with_deadline(maybe_timeout.is_some())
    .with_wait_mask(wait_mask);
    // Set when a signal that only `wait_mask` lets in ends the wait.
    let mut deliver_under_wait_mask = false;
    let mut published = PublishedWaitMask::default();
    publish_wait_mask(guest, signals.effective_mask(), &mut published).await;
    let mut rsrc = rsrc.clone();
    rsrc.poll_deadline = poll_deadline_for(guest, maybe_timeout.map(|(deadline, _)| deadline));
    let (mut call, mut guard) = call0.into_nonblocking(guest).await;
    let mut first_turn = true;
    // Set when only a signal the scheduler committed in this turn ends the
    // wait: the turn's probe runs first, and the wait ends with the restart
    // errno only if the call has nothing to report.
    let mut interrupt_after_probe;

    let result = loop {
        interrupt_after_probe = false;
        // A scheduler `Signaled` answer only says a signal may be pending. The kernel's
        // state below decides whether it ends the wait.
        let resumed = resource_request(guest, rsrc.clone()).await;
        // Read in this turn, before any injection: another thread may have
        // armed a host-timed source since the last turn, and none can until
        // this turn ends. The same round trip gives the `SIGCHLD` a guest sent
        // this thread that the schedule recorded in its private queue, which
        // an injection of this turn (the timerfd publication below) could
        // otherwise take into the backend's single held slot (R2b-2 stage 3,
        // https://github.com/rrnewton/hermit/issues/4273).
        let (host_timed, recorded) =
            crate::tool_global::host_timed_and_private_sigchld(guest).await;
        signals.hold_until_return(host_timed);
        // A `SIGCHLD` the scheduler committed to this thread in this turn, sent
        // at a commit point (a child's child-exit timer, or a POSIX timer whose
        // signal is `SIGCHLD`), ends the wait, as on Linux
        // (https://github.com/rrnewton/hermit/issues/4005), and so does a
        // recorded private one: both are admitted (`KernelSignalWait::admit`).
        let committed = committed_signals(&resumed) & kernel_sigset_bit(libc::SIGCHLD);
        let private = match signals
            .admit_under_block(guest, committed, recorded, host_timed, &mut published)
            .await
        {
            Ok(private) => private,
            Err(error) => break Err(error),
        };
        signals.admit(committed, private);
        // As in `retry_nonblocking_syscall_helper`: virtual timerfds first.
        // As in `retry_nonblocking_syscall_helper`: release the nonblocking
        // form's scratch stack before publishing, and convert again after.
        let reconvert =
            guard.is_some() && crate::syscalls::timerfd::may_publish(guest, call.into());
        if reconvert {
            guard = None;
        }
        rsrc.poll_deadline = crate::syscalls::timerfd::publish_before_probe(
            guest,
            call.into(),
            maybe_timeout.map(|(deadline, _)| deadline),
        )
        .await;
        if reconvert {
            (call, guard) = call0.into_nonblocking(guest).await;
        }
        // A pending real-time timer expiration that this wait would consume or
        // be ended by (https://github.com/rrnewton/hermit/issues/4089).
        let pending = pending_real_time_among(guest, REAL_TIME_SIGNALS).await;
        if pending != 0 {
            match signals.observable_now() {
                Ok(observable) => act_on_pending_real_time(guest, pending, observable, 0).await,
                Err(error) => break Err(error),
            }
        }
        let first = std::mem::take(&mut first_turn);
        // Never `inject_with_retry`: see `KernelSignalWait`.
        let injected = if first {
            // The first probe runs before the check, so a source that was ready when
            // the call began, or an argument error, is reported as Linux reports it
            // whatever signal is pending (`inject_first_probe`).
            signals.inject_first_probe(guest, call).await
        } else {
            let state = match signals.interrupted_with_state() {
                Ok((false, state)) => state,
                // Linux takes a completion before it returns EINTR: a queued
                // signal of rt_sigtimedwait's set (`do_sigtimedwait` dequeues
                // first, and again after it sleeps), a ready descriptor of poll
                // or epoll_wait. A signal the scheduler committed in this turn
                // was sent at the same fixed point as any completion this turn
                // can see, so the completion wins, as on Linux (Codex review of
                // https://github.com/rrnewton/hermit/pull/4039).
                Ok((true, state)) if signals.interrupts_only_by_committed(&state) => {
                    interrupt_after_probe = true;
                    deliver_under_wait_mask = signals.interrupts_only_under_wait_mask(&state);
                    state
                }
                Ok((true, state)) => {
                    deliver_under_wait_mask = signals.interrupts_only_under_wait_mask(&state);
                    let errno = call0.kernel_restart_errno();
                    tracing::trace!(
                        "retry_nonblocking_syscall: pending signals interrupt {}: {:?}",
                        call.display(&guest.memory()),
                        errno
                    );
                    break Err(errno.into());
                }
                Err(error) => break Err(error),
            };
            signals.inject_absorbing_after(guest, call, state).await
        };
        let syscall_result = match injected {
            Ok(result) => result,
            Err(error) => {
                tracing::trace!(
                    "retry_nonblocking_syscall: a signal stop ends the wait in {}: {:?}",
                    call.display(&guest.memory()),
                    error
                );
                break Err(error);
            }
        };
        if !call.syscall_would_have_blocked(syscall_result) {
            let res = call
                .normalize_nonblocking_result(syscall_result, rsrc.poll_attempt > 0)
                .map_err(|e| e.into());
            tracing::trace!(
                "retry_nonblocking_syscall: syscall completed after {} retries: {} = {:?}",
                rsrc.poll_attempt,
                call.display(&guest.memory()),
                res
            );
            break res;
        }
        if interrupt_after_probe {
            let errno = call0.kernel_restart_errno();
            tracing::trace!(
                "retry_nonblocking_syscall: a committed signal interrupts {}: {:?}",
                call.display(&guest.memory()),
                errno
            );
            break Err(errno.into());
        }
        if first {
            // The first turn's check, after its probe found nothing to report.
            match signals.interrupted_with_state() {
                Ok((false, _)) => {}
                Ok((true, state)) => {
                    deliver_under_wait_mask = signals.interrupts_only_under_wait_mask(&state);
                    let errno = call0.kernel_restart_errno();
                    tracing::trace!(
                        "retry_nonblocking_syscall: pending signals interrupt {}: {:?}",
                        call.display(&guest.memory()),
                        errno
                    );
                    break Err(errno.into());
                }
                Err(error) => break Err(error),
            }
        }
        if signals.needs_block() {
            // Only one scratch-stack guard may be live, so release the probe's, block
            // signals from a fresh one, and rebuild the probe.
            guard = None;
            if let Err(error) = signals.block(guest, None).await {
                break Err(error);
            }
            publish_wait_mask(guest, signals.effective_mask(), &mut published).await;
            (call, guard) = call0.into_nonblocking(guest).await;
        }
        rsrc.poll_attempt += 1;
        if let Some((timeout, timeout_result)) = maybe_timeout {
            let new_time = thread_observe_time(guest).await;
            if new_time >= timeout {
                tracing::trace!(
                    "Timing out syscall after #{} retries: {}",
                    rsrc.poll_attempt - 1,
                    call.display(&guest.memory())
                );
                break timeout_result.map_err(|e| e.into());
            }
            tracing::trace!(
                "Retry #{} for syscall due to result {:?}, {} from timeout: {}",
                rsrc.poll_attempt,
                syscall_result,
                timeout - new_time,
                call.display(&guest.memory())
            );
        } else {
            tracing::trace!(
                "Retry #{} for syscall due to result {:?}: {}",
                rsrc.poll_attempt,
                syscall_result,
                call.display(&guest.memory())
            );
        }
        record_retry_event(guest, call).await;
    };

    drop(guard);
    let restored = signals.restore(guest, None).await;
    publish_wait_mask(guest, None, &mut published).await;
    let result = match result_after_restore(restored, result) {
        Ok(result) => result,
        Err(error) => return (Err(error), false),
    };
    // A signal the backend holds is delivered as the guest resumes, under the
    // guest's own mask (https://github.com/rrnewton/reverie/issues/1011).
    let deliver = deliver_under_wait_mask && !signals.holds_signal() && result.is_err();
    (
        refuse_held_signal_loss(guest, call0.number(), result).await,
        deliver,
    )
}

/// The policy of `Detcore::refuse_unserviceable_operation`, for a caller that
/// holds the guest but not the tool. `panic_on_unsupported_syscalls` is the
/// setting of that name: the tool's own copy in the method, the guest's
/// configuration in `refuse_held_signal_loss`.
pub(crate) async fn refuse_unserviceable_operation_with<T, G>(
    guest: &mut G,
    panic_on_unsupported_syscalls: bool,
    sysno: Sysno,
    fallback: Errno,
    reason: Option<&str>,
) -> Result<i64, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    if !panic_on_unsupported_syscalls {
        return Err(fallback.into());
    }
    if guest.config().shutdown_on_unsupported_syscall {
        // Fail-closed policy: the operation is unserviceable and the config
        // forbids passing it through. Say why first: on an in-guest backend
        // the exit below ends only the guest process, and the launcher learns
        // the reason from the coordinator.
        let reason = match reason {
            Some(reason) => format!("{sysno}: {reason}"),
            None => format!(
                "{sysno} cannot be serviced on this backend (with \
                 --allow-unsupported-syscalls the guest would see {fallback})"
            ),
        };
        crate::tool_global::report_policy_refusal(guest, reason).await;
        crate::tool_global::unrecoverable_shutdown(
            guest,
            detcore_model::HERMIT_POLICY_REFUSAL_EXIT,
        )
        .await;
    }
    if guest.config().exit_on_unsupported_syscall {
        return Err(Error::Tool(anyhow::Error::new(
            crate::UnsupportedSyscallError(sysno),
        )));
    }
    panic!("unserviceable operation on syscall: {sysno:?}");
}

/// Refuse the guest's `sysno` call when its wait ended with a
/// [`HeldSignalRefusal`], through the unsupported-operation policy
/// (`refuse_unserviceable_operation_with`): a fail-closed run, which ordinary
/// `hermit run` is, ends with the policy's exit, and a run started with
/// `--allow-unsupported-syscalls` gets `ERESTARTNOINTR`, the transparent
/// restart that such a wait returned before (round-10 High 1 on
/// https://github.com/rrnewton/hermit/pull/3361). The message names the call,
/// the signals and that pull request. Callers pass the wait's result after the
/// wait has put the guest's mask back. Every other result passes through.
///
/// Whether a wait meets such a stop can depend on host timing, as a signal
/// sent from outside the guest does, so a refusal can be host-timed: it
/// replaces a run that would have gone on to depend on that timing.
pub(crate) async fn refuse_held_signal_loss<T, G>(
    guest: &mut G,
    sysno: Sysno,
    result: Result<i64, Error>,
) -> Result<i64, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    use std::io::Write as _;
    let refusal = match &result {
        Err(Error::Tool(error)) => match error.downcast_ref::<HeldSignalRefusal>() {
            Some(refusal) => HeldSignalRefusal {
                wait: Some(sysno),
                ..*refusal
            },
            None => return result,
        },
        _ => return result,
    };
    let panic_on_unsupported_syscalls = guest.config().panic_on_unsupported_syscalls;
    tracing::error!("[tid {}] {}", guest.tid(), refusal);
    if panic_on_unsupported_syscalls {
        // The run ends: report why even when logging is disabled or redirected,
        // as the refusal of a nonleader exec does.
        let _ = writeln!(crate::util::RetryingStderr, "{refusal}");
    }
    refuse_unserviceable_operation_with(
        guest,
        panic_on_unsupported_syscalls,
        sysno,
        Errno::ERESTARTNOINTR,
        None,
    )
    .await
}

/// Signal handling for one blocking wait whose interruption is decided from the
/// kernel's signal state (`supports_blocked_wait_signal_interruption`).
///
/// A wait ends early only for a signal that would end it natively: one the guest
/// does not block and that is caught, or whose default action terminates or stops
/// the process (https://github.com/rrnewton/hermit/issues/3146). Blocked,
/// ignored, and default-ignored signals do not end it, and neither does a default
/// job-control stop when `defers_default_stops` is set
/// (`KernelSignalState::interrupting_wait`), apart from the stops that
/// `inject_absorbing` cannot absorb safely, which end it with a restart.
///
/// The first probe runs under the guest's own mask. If it would block, `block`
/// blocks every blockable signal for the rest of the wait, so later probes cannot
/// be stopped by one and every signal that arrives stays pending in the kernel,
/// where `/proc` reports it. `interrupted_with_state` classifies the pending set
/// against the guest's mask and dispositions each turn, and `restore` puts the
/// guest's mask back before the call returns, so a pending interrupting signal
/// is delivered as the call returns its restart errno.
///
/// Probes and the mask change go through `inject_absorbing`, never
/// `inject_with_retry`. A signal that stops the guest around an injection is
/// dequeued from the kernel and held by the backend in a single slot, so a blind
/// retry that is stopped again would replace it. `inject_absorbing` works out
/// from `/proc` which signal the backend holds and injects again only when that
/// signal would not end the wait natively. Before the mask is set, a signal
/// pending at the turn's `/proc` read that would end the wait is reported by
/// `interrupted_with_state`, and any other unblocked one stops the next
/// injection and is absorbed; a signal that arrives after the read can stop an
/// injection in a way that cannot be identified, and is held without ending the
/// wait, so the wait keeps its deadline and its checks, unless the backend held
/// a signal whose loss matters, which ends the wait with a
/// [`HeldSignalRefusal`] (`inject_absorbing`).
/// Afterwards only a signal that cannot be blocked can stop an injection. A stop
/// after a probe ran replaces its result, so a probe that consumed something (an
/// edge-triggered event, a dequeued signal) loses it; that remains a known gap.
///
/// No failure of this machinery reaches the guest as an errno the wait call
/// cannot return. A `/proc` read that fails for a thread that still exists, or a
/// guest mask that cannot be put back, ends the run with a
/// [`BlockedWaitSignalError`]; a thread that no longer exists gets
/// `ERESTARTNOINTR`, which nothing observes (`read_wait_signal_state`). If the
/// mask cannot be changed at all (no scratch room below the guest's stack
/// pointer, https://github.com/rrnewton/hermit/issues/3328), `block` leaves the
/// guest's own mask in place and every later probe runs under it, as the first
/// probe does.
/// The real-time signals, 32 to 64, as kernel sigset bits.
pub(crate) const REAL_TIME_SIGNALS: KernelSigset = !0 << 31;

/// The signals a thread whose mask becomes `mask` lets the kernel take from
/// its queues: those `mask` does not block, split into the ones whose delivery
/// the guest observes and the ones Linux discards on delivery because their
/// disposition is `SIG_IGN`.
pub(crate) fn unblocked_by(
    state: &KernelSignalState,
    mask: KernelSigset,
) -> (KernelSigset, KernelSigset) {
    unblocked_by_ignored(state.ignored, mask)
}

/// [`unblocked_by`] given only the thread's ignored set, from wherever it
/// comes (the kernel's `/proc` view or Detcore's copy).
pub(crate) fn unblocked_by_ignored(
    ignored: KernelSigset,
    mask: KernelSigset,
) -> (KernelSigset, KernelSigset) {
    let unblocked = !mask;
    (unblocked & !ignored, unblocked & ignored)
}

/// The real-time timer expirations among `candidates` (a kernel sigset) that
/// are pending for the calling thread's process because every thread blocked
/// them when they expired (`Scheduler::pending_real_time_expirations`,
/// https://github.com/rrnewton/hermit/issues/4089). 0 without asking the
/// scheduler until one has been kept pending in this run
/// (`scheduler::real_time_expiration_kept_pending`), or when `candidates`
/// holds no real-time signal. Asked in the calling thread's turn.
pub(crate) async fn pending_real_time_among<T, G>(guest: &mut G, candidates: KernelSigset) -> u64
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let candidates = candidates & REAL_TIME_SIGNALS;
    if candidates == 0 || !crate::scheduler::real_time_expiration_kept_pending() {
        return 0;
    }
    let Some(detpid) = guest.thread_state().detpid else {
        return 0;
    };
    crate::tool_global::pending_real_time_expirations(guest, detpid).await & candidates
}

/// Where a call lets the guest observe the signals in `observable` (deliver,
/// dequeue or report them), refuse the run by name if one of the `pending`
/// real-time timer expirations (`pending_real_time_among`) is among them, and
/// discard the pending ones in `discarded`, which Linux discards there.
pub(crate) async fn act_on_pending_real_time<T, G>(
    guest: &mut G,
    pending: KernelSigset,
    observable: KernelSigset,
    discarded: KernelSigset,
) where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let observed = pending & observable;
    if observed != 0 {
        crate::scheduler::refuse_real_time_expiration(observed.trailing_zeros() as i32 + 1);
    }
    if pending & discarded != 0
        && let Some(detpid) = guest.thread_state().detpid
    {
        crate::tool_global::discard_pending_real_time_expirations(
            guest,
            detpid,
            pending & discarded,
        )
        .await;
    }
}

/// For a call that runs under the temporary mask `mask`: refuse by name for a
/// pending real-time timer expiration the mask lets the kernel deliver, and
/// discard one it lets the kernel discard. Reads the thread's dispositions
/// only when one is pending.
pub(crate) async fn observe_under_mask<T, G>(guest: &mut G, mask: KernelSigset) -> Result<(), Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let pending = pending_real_time_among(guest, !mask).await;
    if pending != 0 {
        let ignored = match crate::syscalls::signal_shadow::copied_dispositions(guest) {
            Some(copied) => copied.ignored,
            None => read_thread_wait_signal_state(guest.pid(), guest.tid())?.ignored,
        };
        let (observable, discarded) = unblocked_by_ignored(ignored, mask);
        act_on_pending_real_time(guest, pending, observable, discarded).await;
    }
    Ok(())
}

pub(crate) struct KernelSignalWait {
    pid: reverie::Pid,
    tid: reverie::Pid,
    /// Signals the wait itself consumes (rt_sigtimedwait's set), which never
    /// interrupt it.
    consumed: KernelSigset,
    /// Whether a default job-control stop leaves the wait running, because Linux's
    /// restart of the call keeps its deadline
    /// (`NonblockableSyscall::restart_keeps_deadline`).
    defers_default_stops: bool,
    /// The errno that ends the wait for an interrupting signal: the restart code
    /// Linux uses for the call (`NonblockableSyscall::kernel_restart_errno`). A
    /// signal the backend holds turns `ERESTART_RESTARTBLOCK` into `ERESTARTNOHAND`
    /// (`held_signal_restart_errno`).
    restart_errno: Errno,
    /// Signals that never end the wait, even ones that would end it natively.
    /// They stay pending in the kernel, or held by the backend, and are
    /// delivered as the call returns, once the guest's mask is put back. In
    /// every wait but `select`'s and `pselect6`'s (`for_select`) this is
    /// `SIGCHLD`, whichever process sent it: the kernel also posts one for a
    /// child's exit, stop or continue at a moment set by host timing, and
    /// `/proc` shows no siginfo that would tell the two apart
    /// (https://github.com/rrnewton/hermit/issues/3146). The scheduler's own
    /// send of a `SIGCHLD` to this thread at a commit point lifts the hold for the
    /// turn whose resume names it (`admit`,
    /// https://github.com/rrnewton/hermit/issues/4005). The gated loop adds,
    /// in each of its turns, every signal a host-timed source armed by a guest
    /// can post to this process (`hold_until_return`).
    held_until_return: KernelSigset,
    /// Whether the wait has a finite deadline (`with_deadline`). Hermit cannot
    /// install the restart block with which Linux keeps the absolute deadline
    /// of a restarted `poll` or timed futex wait, and a restarted `ppoll`,
    /// `epoll_wait`, `epoll_pwait` or `rt_sigtimedwait` runs again with the
    /// guest's unchanged timeout. So a transparent restart would start the
    /// timeout again, and such a wait never ends with `ERESTARTNOINTR`
    /// (`inject_absorbing`).
    deadline: bool,
    /// The guest's own mask while the wait runs with every signal blocked.
    saved_mask: Option<KernelSigset>,
    /// `block` could not change the mask, so probes run under the guest's own.
    unblockable: bool,
    /// `saved_mask` was installed by `block_in_place`, through
    /// `Guest::set_blocked_signals`, so `restore` puts it back the same way,
    /// with no injection and no scratch memory.
    blocked_in_place: bool,
    /// The signal the backend holds since `inject_absorbing` absorbed a stop;
    /// `None` also after a stop it could not identify (`held_unidentified`).
    held: Option<HeldSignal>,
    /// The backend holds a signal from a stop that `inject_absorbing` could not
    /// identify, which may be one whose loss matters, so no later stop may
    /// replace it (`would_replace_precious`).
    held_unidentified: bool,
    /// The signals `hold_until_return` added because a host-timed source armed
    /// by a guest can post them; a committed signal never lifts these
    /// (`admit`).
    host_timed: KernelSigset,
    /// Signals the scheduler committed to this thread in the current turn,
    /// which end the wait even though it holds them (`admit`).
    committed: KernelSigset,
    /// The `SIGCHLD` a guest sent this thread, which the schedule recorded in
    /// its private queue, admitted in the current turn (`admit_under_block`):
    /// it ends the wait from the thread's own queue only, even though the
    /// wait holds it (R2b-2 stage 3,
    /// https://github.com/rrnewton/hermit/issues/4273).
    private: KernelSigset,
    /// The mask the wait sleeps under, when it is not the guest's own: the
    /// temporary mask of a `pselect6` (`with_wait_mask`). A signal decides
    /// against it whether it ends the wait; `restore` still puts back the
    /// guest's own mask (https://github.com/rrnewton/hermit/issues/3991).
    wait_mask: Option<KernelSigset>,
}

/// A signal that stopped one of a wait's injections and that the backend holds
/// for delivery when the guest resumes (`KernelSignalWait::inject_absorbing`).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct HeldSignal {
    signal: i32,
    queue: SignalQueue,
    kind: HeldKind,
}

/// Whether losing a held signal, which a later stop would replace, changes what
/// the guest or the scheduler observes.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
enum HeldKind {
    /// Its delivery does nothing: it is ignored, or default-ignored and not
    /// `SIGCHLD`.
    Harmless,
    /// Its delivery matters, or is kept as if it did: a signal the wait holds
    /// until the call returns (`held_until_return`), which may run a handler
    /// then, any other `SIGCHLD`, or a default job-control stop that the wait
    /// defers.
    Precious,
}

/// How a wait would lose a signal that the backend holds, or deliver it in
/// place of the result Linux gives (`HeldSignalRefusal`).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum HeldSignalLoss {
    /// Another pending signal would stop the next injection, and the backend,
    /// which has one slot for a held signal, would drop the held one for it.
    Replaced,
    /// A stop that `/proc` cannot identify replaced a held signal whose loss
    /// matters (`HeldKind::Precious`), or one that could not be identified
    /// either and so may matter.
    UnidentifiedStop,
    /// The wait reached `MAX_ABSORBED_STOPS` absorbed stops while the backend
    /// held a signal whose loss matters or that could not be identified.
    StopBound,
    /// A signal that a wait without a deadline consumes stopped an injection,
    /// so the backend holds it and delivers it rather than the wait returning
    /// it, and either it is one the wait holds until the call returns
    /// (`held_until_return`) or it replaced a held signal whose loss matters.
    Consumed,
    /// A signal stopped an injection of `restore`, which puts the guest's mask
    /// back and so must run whatever is pending, and the backend dropped a held
    /// signal whose loss matters, or one that could not be identified, for it.
    Restoration,
}

/// A wait that ends because going on, or restarting the call, would lose a
/// signal the backend holds, or deliver it in place of the result Linux gives.
///
/// Every path that used to end such a wait with a fallback that returns
/// success (a transparent `ERESTARTNOINTR` restart, or a wait that went on and
/// let a stop replace the held signal) returns this instead (round-10 High 1 on
/// https://github.com/rrnewton/hermit/pull/3361). The wait puts the guest's mask
/// back, and its caller then applies the unsupported-operation policy
/// (`refuse_held_signal_loss`). A complete successful repair preserving signal
/// information needs additional framework support with the current generic
/// API: the backend holds one signal, does not say which, and delivers it
/// without the tool. A Detcore-only refusal or capability withdrawal can
/// prevent the determinism regression.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct HeldSignalRefusal {
    /// The guest call whose wait ends, once the caller names it
    /// (`refuse_held_signal_loss`).
    pub(crate) wait: Option<Sysno>,
    /// The signal the backend holds that would be lost or delivered wrongly;
    /// `None` when it could not be identified.
    pub(crate) held: Option<i32>,
    /// The signal that would replace it, or that stopped the injection, when
    /// known.
    pub(crate) replacing: Option<i32>,
    /// How it would be lost.
    pub(crate) loss: HeldSignalLoss,
}

/// A signal's name, such as `SIGCHLD`, or its number for a real-time signal.
fn signal_label(signal: Option<i32>) -> String {
    match signal {
        Some(signal) => match nix::sys::signal::Signal::try_from(signal) {
            Ok(named) => named.as_str().to_string(),
            Err(_) => format!("signal {signal}"),
        },
        None => "a signal that could not be identified".to_string(),
    }
}

impl std::fmt::Display for HeldSignalRefusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let wait = self
            .wait
            .map_or_else(|| "a blocking".to_string(), |sysno| sysno.to_string());
        let held = match self.held {
            Some(_) => format!("the held signal {}", signal_label(self.held)),
            None => "a held signal that could not be identified".to_string(),
        };
        let replacing = signal_label(self.replacing);
        match self.loss {
            HeldSignalLoss::Replaced => write!(
                f,
                "unsupported: the {wait} wait cannot keep {held}: the pending {replacing} \
                 would stop the next injection and replace it"
            )?,
            HeldSignalLoss::UnidentifiedStop => write!(
                f,
                "unsupported: the {wait} wait cannot keep {held}: a signal stop that /proc \
                 cannot identify replaced it"
            )?,
            HeldSignalLoss::StopBound => write!(
                f,
                "unsupported: the {wait} wait cannot keep {held}: the wait absorbed the \
                 maximum number of signal stops"
            )?,
            HeldSignalLoss::Consumed => {
                write!(
                    f,
                    "unsupported: the {wait} wait cannot return {replacing}, which it consumes: \
                     the signal stopped an injection, so the backend would deliver it instead"
                )?;
                if self.held.is_some() {
                    write!(f, ", in place of {held}")?;
                }
            }
            HeldSignalLoss::Restoration => write!(
                f,
                "unsupported: the {wait} wait cannot keep {held}: {replacing} stopped the \
                 injection that puts the guest's signal mask back and replaced it"
            )?,
        }
        write!(
            f,
            " (scope reduction of https://github.com/rrnewton/hermit/pull/3361; rerun with \
             --allow-unsupported-syscalls to restart the call instead)"
        )
    }
}

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

/// What a signal that stopped one of a wait's injections means for the wait.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum StopVerdict {
    /// The wait goes on; the backend holds the signal.
    Absorb(HeldKind),
    /// The wait ends with this errno.
    End(Errno),
}

/// The first real-time signal number in the kernel. A standard signal below it
/// is pending at most once; a second one sent while it is pending is merged.
const KERNEL_SIGRTMIN: i32 = 32;

impl KernelSignalWait {
    pub(crate) fn new<T, G>(
        guest: &G,
        consumed: KernelSigset,
        defers_default_stops: bool,
        restart_errno: Errno,
    ) -> Self
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
    {
        Self {
            pid: guest.pid(),
            tid: guest.tid(),
            consumed,
            defers_default_stops,
            restart_errno,
            held_until_return: kernel_sigset_bit(libc::SIGCHLD),
            deadline: false,
            saved_mask: None,
            unblockable: false,
            blocked_in_place: false,
            held: None,
            held_unidentified: false,
            host_timed: 0,
            committed: 0,
            private: 0,
            wait_mask: None,
        }
    }

    /// This wait, with a finite deadline when `deadline` is set (the `deadline`
    /// field): no fallback of `inject_absorbing` then ends it with
    /// `ERESTARTNOINTR`, which would start the guest's timeout again.
    pub(crate) fn with_deadline(self, deadline: bool) -> Self {
        Self { deadline, ..self }
    }

    /// The wait of `select` and of `pselect6`, which holds no signal until the
    /// call returns (`held_until_return` is empty); a `pselect6` with a
    /// temporary mask adds it with `with_wait_mask`:
    /// any pending signal that could interrupt it ends it, `SIGCHLD` included.
    ///
    /// Linux ends these calls with `ERESTARTNOHAND` whenever no descriptor is
    /// ready and a signal is pending (`core_sys_select`), so a caught `SIGCHLD`
    /// ends them whoever sent it. Detcore's select and pselect6 waits did the
    /// same before this machinery existed: their probes ran under the guest's
    /// own mask, and a pending caught signal stopped one.
    ///
    /// This is a known gap that these waits had before, and it is left as it
    /// was: the `SIGCHLD` the kernel posts for a child's exit, stop or continue
    /// ends the wait at the first turn whose read sees it, and host timing
    /// decides which turn that is (https://github.com/rrnewton/hermit/issues/3146).
    /// Every other wait holds `SIGCHLD` until it returns, unless the scheduler
    /// commits it to the thread in that turn (`admit`).
    pub(crate) fn for_select<T, G>(guest: &G) -> Self
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
    {
        Self {
            held_until_return: 0,
            ..Self::new(guest, 0, false, Errno::ERESTARTNOHAND)
        }
    }

    /// This wait, sleeping under `mask` instead of the guest's own (`wait_mask`):
    /// a `pselect6` with a temporary mask. Linux installs that mask for the
    /// whole call, so a signal it blocks never ends the call, even one the
    /// guest's own mask leaves unblocked, and one it leaves unblocked ends the
    /// call although the guest's own mask blocks it.
    pub(crate) fn with_wait_mask(self, mask: Option<KernelSigset>) -> Self {
        Self {
            wait_mask: mask,
            ..self
        }
    }

    /// The mask Linux's `blocked` holds for the waiting thread when the
    /// kernel's `SigBlk` does not show it: the wait's temporary mask
    /// (`wait_mask`), or else the guest's own mask once `block` has replaced
    /// it (`saved_mask`). `None` while `SigBlk` is the guest's own mask.
    pub(crate) fn effective_mask(&self) -> Option<KernelSigset> {
        self.wait_mask.or(self.saved_mask)
    }

    /// Whether the backend holds a signal from a stop of one of this wait's
    /// injections (`held`, `held_unidentified`), which it delivers when the
    /// guest resumes.
    pub(crate) fn holds_signal(&self) -> bool {
        self.held.is_some() || self.held_unidentified
    }

    /// Whether a signal that ends the wait, pending in the kernel per `state`,
    /// is one the guest's own mask blocks: it ends the wait only because the
    /// wait mask (`with_wait_mask`) leaves it unblocked, and it stays pending
    /// when the guest's own mask is put back. A signal the wait holds until
    /// the call returns counts once the scheduler committed it in this turn
    /// (`admit`), as in `interrupted_with_state`: otherwise a
    /// committed `SIGCHLD` that only the wait mask lets in ended the call
    /// without its handler (Claude review of
    /// https://github.com/rrnewton/hermit/pull/4217).
    pub(crate) fn interrupts_only_under_wait_mask(&self, state: &KernelSignalState) -> bool {
        let own_mask = self.saved_mask.unwrap_or(state.blocked);
        let held_until_return = self.held_until_return & !self.committed;
        let could_interrupt = self.could_interrupt(state);
        let interrupting = (state.pending & !held_until_return
            | state.thread_pending & self.private)
            & could_interrupt;
        self.wait_mask.is_some() && interrupting & own_mask != 0
    }

    /// Adds `signals` to the ones this wait holds until the call returns
    /// (`held_until_return`). The gated loop passes, in each of its turns, the
    /// signals that a host-timed source armed by a guest can post to this
    /// process, such as a parent-death signal (`tool_global::host_timed_signals`):
    /// the kernel posts those at a moment set by host timing, so ending the wait
    /// for one would make the run depend on host timing. Never called for a
    /// `select` wait (`for_select`), which holds nothing.
    pub(crate) fn hold_until_return(&mut self, signals: KernelSigset) {
        self.held_until_return |= signals;
        self.host_timed |= signals;
    }

    /// Sets the signals the scheduler committed to this thread in this turn
    /// (`committed`): its resume named them (`ResumeStatus::Signaled`). Such a
    /// signal was sent at a fixed point of the schedule, so a wait may end for
    /// it although the wait holds it, unless a host-timed source can post it
    /// too (`host_timed`). The loop passes a committed `SIGCHLD`, which the
    /// scheduler sends at a commit point (a child's child-exit timer, or a
    /// POSIX timer whose signal is `SIGCHLD`); the kernel's
    /// own copy of it, posted at a host-timed moment, decides nothing, since
    /// only the resume that names it lifts the hold, for that turn alone
    /// (https://github.com/rrnewton/hermit/issues/4005).
    ///
    /// `private` is the `SIGCHLD` a guest sent this thread, recorded in its
    /// private queue, that the turn admits (`admit_under_block`): it lifts the
    /// hold only against the thread's own queue, for this turn alone.
    pub(crate) fn admit(&mut self, committed: KernelSigset, private: KernelSigset) {
        self.committed = committed & !self.host_timed;
        self.private = private & !self.host_timed;
    }

    /// The private `SIGCHLD` this turn admits (`admit`), from `recorded`, the
    /// schedule's record (`Scheduler::private_sigchld`). A turn that admits a
    /// `SIGCHLD`, committed or recorded, first blocks every blockable signal
    /// in place, before any injection of the turn, and publishes the guest's
    /// mask while it holds (`block_in_place`, `publish_wait_mask`). The
    /// recorded one is then kept only while that block holds (`saved_mask`;
    /// an earlier injected `block` that failed leaves none), and only if the
    /// thread's own queue holds `SIGCHLD`, read after the block: only this
    /// stopped thread can take it from there, and no injection can now. If
    /// the setter refuses, the wait keeps its fallback, the injected `block`
    /// after the first probe (rev4 1f), and holds the recorded `SIGCHLD` as
    /// before.
    pub(crate) async fn admit_under_block<T, G>(
        &mut self,
        guest: &mut G,
        committed: KernelSigset,
        recorded: KernelSigset,
        host_timed: KernelSigset,
        published: &mut PublishedWaitMask,
    ) -> Result<KernelSigset, Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
    {
        if (committed | recorded) & !host_timed == 0 {
            return Ok(0);
        }
        if self.needs_block() {
            if self.block_in_place(guest, host_timed).await? {
                publish_wait_mask(guest, self.effective_mask(), published).await;
            } else {
                // Keep today's fallback for this wait.
                self.unblockable = false;
            }
        }
        if recorded == 0 || self.saved_mask.is_none() {
            return Ok(0);
        }
        let state = read_wait_signal_state(self.pid, self.tid)?;
        Ok(recorded & state.thread_pending)
    }

    /// Whether, of the signals that end the wait per `state`, only signals the
    /// scheduler committed in this turn do (`admit`): without them
    /// the wait would go on.
    pub(crate) fn interrupts_only_by_committed(&self, state: &KernelSignalState) -> bool {
        let held = self.held.map_or(0, |held| kernel_sigset_bit(held.signal));
        let interrupting = (state.pending | held) & self.could_interrupt(state);
        (self.committed | self.private) != 0 && interrupting & !self.held_until_return == 0
    }

    /// Whether the caller should still call `block`: it has neither blocked the
    /// guest's signals nor found that it cannot.
    pub(crate) fn needs_block(&self) -> bool {
        self.saved_mask.is_none() && !self.unblockable
    }

    /// The signals that would end the wait natively under the guest's mask, given
    /// the kernel's `state`.
    /// The signals whose pending presence this wait would observe now: those
    /// it consumes, and those that would end it under the guest's mask, or
    /// the wait mask when it has one (`could_interrupt`). Read from the kernel's state in this turn.
    pub(crate) fn observable_now(&self) -> Result<KernelSigset, Error> {
        let state = read_wait_signal_state(self.pid, self.tid)?;
        Ok(self.consumed | self.could_interrupt(&state))
    }

    fn could_interrupt(&self, state: &KernelSignalState) -> KernelSigset {
        let guest_mask = self.wait_mask.or(self.saved_mask).unwrap_or(state.blocked);
        state.interrupting_wait(guest_mask, self.defers_default_stops) & !self.consumed
    }

    /// `interrupted_with_state` without the state, for the tests.
    #[cfg(test)]
    pub(crate) fn interrupted(&self) -> Result<bool, Error> {
        Ok(self.interrupted_with_state()?.0)
    }

    /// Whether a signal that would end the wait natively is pending, in which case
    /// the caller returns the call's restart errno, with the kernel's state that
    /// this read, which `inject_absorbing_after` takes as its first read in the same
    /// turn. The guest thread has been stopped inside the call since it was
    /// intercepted, so such a signal arrived while the call was waiting, and Linux
    /// returns the restart errno for that. Linux reports a source that was ready
    /// when the call began, or an argument error, before any pending signal, so
    /// `retry_blocking_wait_with_kernel_signal_state` runs its first probe before
    /// this check (`inject_first_probe`). There, a source that becomes ready in a
    /// later turn in which a signal is also pending is put behind the signal,
    /// because the turn cannot tell which came first. The waits of `select` and
    /// `pselect6` run every probe before this check, as Linux's do_select polls
    /// every descriptor on each pass before it tests for a pending signal. A
    /// signal the wait holds until the call returns (`held_until_return`) never
    /// counts. A signal that the backend holds since `inject_absorbing` absorbed
    /// it counts as pending. A failed read is never returned as the call's errno
    /// (`read_wait_signal_state`).
    pub(crate) fn interrupted_with_state(&self) -> Result<(bool, KernelSignalState), Error> {
        let state = read_wait_signal_state(self.pid, self.tid)?;
        let could_interrupt = self.could_interrupt(&state);
        let held = self.held.map_or(0, |held| kernel_sigset_bit(held.signal));
        let held_until_return = self.held_until_return & !self.committed;
        // An admitted private `SIGCHLD` counts against the thread's own queue
        // only (`admit`).
        let interrupting = ((state.pending | held) & !held_until_return
            | state.thread_pending & self.private)
            & could_interrupt;
        if interrupting != 0 {
            tracing::trace!(
                "[tid {}] pending signals {:#x} interrupt a blocking wait",
                self.tid,
                interrupting
            );
        }
        Ok((interrupting != 0, state))
    }

    /// Inject `call`, a probe or a mask change, absorbing the signal stops that do
    /// not end the wait. `Ok` carries the call's own result; `Err` ends the wait.
    ///
    /// A signal that stops the guest around an injection is dequeued from the
    /// kernel and held by the backend, which delivers it when the guest resumes,
    /// and the injection returns a restart errno instead of the call's result.
    /// The backend does not say which signal it holds, so the kernel's state is
    /// read before each injection (in a polling turn of
    /// `retry_blocking_wait_with_kernel_signal_state` under the full mask, the
    /// turn's own read: `inject_absorbing_after`), which names the signal the
    /// kernel dequeues next (`KernelSignalState::next_dequeued`), and again
    /// after a stop. The stop is identified only if exactly that signal left its queue
    /// and the mask and dispositions did not change; it then decides:
    ///
    /// - A signal the wait consumes (`consumed`) is delivered rather than
    ///   consumed. In a wait without a deadline it ends the wait with
    ///   `ERESTARTNOINTR`, and the call runs again, unless the wait holds it
    ///   until the call returns (`held_until_return`), so that its delivery now
    ///   could follow host timing: then the wait ends with a
    ///   [`HeldSignalRefusal`]. In a wait with a deadline (`deadline`) it is
    ///   classified as if the wait did not consume it, by the rules below.
    /// - A signal that would end the wait natively ends it with the restart
    ///   errno, except a signal the wait holds until the call returns
    ///   (`held_until_return`), which is held.
    /// - Any other `SIGCHLD`, and a default job-control stop that the wait
    ///   defers, are held, and so are an ignored signal and a default-ignored
    ///   one. The injection runs again.
    /// - A signal that the wait mask (`with_wait_mask`) blocks is held: Linux
    ///   keeps it pending until the call returns.
    /// - Anything else, which is the backend's own preemption signal, ends the
    ///   wait with `ERESTARTNOINTR`, or is held in a wait with a deadline.
    ///
    /// A pending signal that would end the wait ends it before the injection,
    /// with the restart errno, so it is never held, except by a probe made by
    /// `inject_first_probe`: the first probe of a wait, and every probe of the
    /// waits of `select` and `pselect6`. A stop that cannot be identified, by a
    /// signal that arrived after the read, does not end the wait while the
    /// backend held nothing whose loss matters: the backend holds that signal
    /// in place of any held one and the injection runs again, as the blind
    /// retry before this machinery did, so the wait keeps its deadline and
    /// every check that ends it. Which signal it was would need the backend to
    /// say, so it is recorded only as unidentified (`held_unidentified`), and
    /// it is delivered as the call returns.
    ///
    /// The backend has one slot for a held signal, and a stop replaces what it
    /// holds. A held signal whose loss matters (`HeldKind::Precious`), or one
    /// that could not be identified and so may matter, is never given up for a
    /// result that reads as success, with or without a deadline:
    ///
    /// - An injection that another pending signal would stop is not made. If
    ///   the held signal would end the wait natively, the wait ends with the
    ///   restart errno, as Linux ends it for that signal; otherwise it ends
    ///   with a `HeldSignalRefusal`. The same standard signal on the same queue
    ///   is the exception, because Linux merges the two.
    /// - A stop that cannot be identified, which has already replaced such a
    ///   signal, ends the wait with a `HeldSignalRefusal`.
    /// - After `MAX_ABSORBED_STOPS` absorbed stops, a wait without a deadline
    ///   ends with a `HeldSignalRefusal`, or with `ERESTARTNOINTR` when the
    ///   backend holds nothing whose loss matters. A wait with a deadline,
    ///   whose restart would start the guest's timeout again, absorbs stops
    ///   without a bound.
    ///
    /// Each refusal is a scope reduction. It replaces a transparent
    /// `ERESTARTNOINTR` restart, or, in a wait with a deadline, an injection
    /// that went on and let a stop replace the held signal (round-10 High 1 on
    /// https://github.com/rrnewton/hermit/pull/3361): the restart made the
    /// backend deliver the held signal before the wait completed, at a moment
    /// host timing could set. A complete successful repair preserving signal
    /// information needs additional framework support with the current generic
    /// API. A Detcore-only refusal or capability withdrawal can prevent the
    /// determinism regression.
    ///
    /// In a probe made by `inject_first_probe` that two pending signals would
    /// stop in turn, the first of which would end the wait, the wait ends with
    /// the restart errno before the probe has run, so a ready source, or an
    /// argument error, is reported behind the signal: the order compromise that
    /// a later turn of `retry_blocking_wait_with_kernel_signal_state` already
    /// makes (`interrupted_with_state`).
    pub(crate) async fn inject_absorbing<T, G, S>(
        &mut self,
        guest: &mut G,
        call: S,
    ) -> Result<Result<i64, Errno>, Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
        S: SyscallInfo,
    {
        self.inject_absorbing_from(guest, call, None, false).await
    }

    /// The first probe of a wait, and every probe of the waits of `select` and
    /// `pselect6`, which runs before the turn's `interrupted_with_state` check:
    /// `inject_absorbing`, except that a signal that would end the wait neither
    /// ends it before the injection nor when it stops the injection.
    ///
    /// Linux reports a descriptor that is ready when the call begins, a queued
    /// event, and an argument error before it looks at pending signals:
    /// `do_poll` tests `signal_pending` only when no descriptor is ready, and so
    /// does `core_sys_select`, whose `max_select_fd` first returns `EBADF` for a
    /// descriptor in the sets that is not open; `ep_poll` sends queued events
    /// before it tests it, `do_epoll_wait` rejects a bad descriptor or
    /// `maxevents` first, and `do_sigtimedwait` copies its set first. So the
    /// probe runs even when such a signal is pending. `do_select` also polls
    /// every descriptor on each pass before it tests `signal_pending`, so the
    /// waits of `select` and `pselect6` make every probe this way; once `block`
    /// has blocked every blockable signal, a pending one cannot stop a probe
    /// there, and the check that follows a probe that would block counts it.
    /// A pending signal that the guest does not block stops the injection
    /// before the probe runs; when the stop is identified, the signal is held
    /// as a precious one (`HeldKind::Precious`) and the probe runs again, so
    /// its result stands, and the backend delivers the signal as the call
    /// returns. If the probe would block, the caller's check that follows
    /// counts the held signal (`interrupted_with_state`) and ends the wait with
    /// the restart errno. A stop that cannot be identified is held, as
    /// `inject_absorbing` describes.
    pub(crate) async fn inject_first_probe<T, G, S>(
        &mut self,
        guest: &mut G,
        call: S,
    ) -> Result<Result<i64, Errno>, Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
        S: SyscallInfo,
    {
        self.inject_absorbing_from(guest, call, None, true).await
    }

    /// `inject_absorbing` in a turn whose `interrupted_with_state` read `state`
    /// and found no signal that ends the wait.
    ///
    /// Once `block` has blocked every blockable signal (`saved_mask`), `state`
    /// serves as the first read before the injection, so a polling turn reads
    /// `/proc` once, as it did before injections absorbed stops. The caller must
    /// run nothing between the two that resumes the guest thread or changes its
    /// signal state. `retry_blocking_wait_with_kernel_signal_state`, which serves
    /// ppoll, poll, epoll_pwait, epoll_wait, rt_sigtimedwait and polling futex
    /// waits and is the only caller, runs nothing there. So between the two reads
    /// the guest thread stays stopped and only it can change its own mask, and
    /// with threads sequentialized no other guest thread runs in its turn, so a
    /// signal that arrives meanwhile arrives at a
    /// host-timed moment: sent from outside the guest, or posted by the kernel.
    /// A signal that the full mask blocks stays pending, cannot stop the
    /// injection, and is classified at the next turn's read, as one that arrives
    /// just after a fresh read is. One that the full mask leaves unblocked
    /// (`SIGKILL`, `SIGSTOP`, glibc's two reserved signals, `PERF_EVENT_SIGNAL`)
    /// and that stops the injection is not named by `state`, so the stop is not
    /// identified and is handled as a stop by a signal that arrives just after a
    /// fresh read is. `interrupted_with_state` found
    /// no signal in `state` that ends the wait, so the check before the injection
    /// does not end it either.
    ///
    /// Before `block` takes effect, or when it cannot, the probe runs under the
    /// guest's own mask and a fresh read is taken, so a signal that arrived
    /// after `state` is still identified or ends the wait before the injection.
    pub(crate) async fn inject_absorbing_after<T, G, S>(
        &mut self,
        guest: &mut G,
        call: S,
        state: KernelSignalState,
    ) -> Result<Result<i64, Errno>, Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
        S: SyscallInfo,
    {
        let first = self.saved_mask.is_some().then_some(state);
        self.inject_absorbing_from(guest, call, first, false).await
    }

    /// `inject_absorbing`, taking `first`, if any, as the state read before the
    /// first injection. For `first_probe`, a signal that would end the wait does
    /// not end it, and one that stops the injection is held
    /// (`inject_first_probe`).
    async fn inject_absorbing_from<T, G, S>(
        &mut self,
        guest: &mut G,
        call: S,
        mut first: Option<KernelSignalState>,
        first_probe: bool,
    ) -> Result<Result<i64, Errno>, Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
        S: SyscallInfo,
    {
        // Each absorbed stop takes one signal off a kernel queue, so only signals
        // sent faster than the injections run can reach this. A wait with a
        // deadline has no bound, as the blind retry before this machinery had
        // none: ending it would start its timeout again (`deadline`).
        const MAX_ABSORBED_STOPS: usize = 64;
        let mut absorbed = 0;
        loop {
            let before = match first.take() {
                Some(state) => state,
                None => read_wait_signal_state(self.pid, self.tid)?,
            };
            let ends_wait = self.could_interrupt(&before) & !self.held_until_return;
            if !first_probe && before.pending & ends_wait != 0 {
                return Err(self.restart_errno.into());
            }
            let next = before.next_dequeued(before.blocked);
            if let Some((signal, queue)) = next
                && self.would_replace_precious(signal, queue)
            {
                if let Some(held) = self.held
                    && self.held_ends_wait(&before, held.signal)
                {
                    tracing::trace!(
                        "[tid {}] signal {} would replace the held signal {}, which ends the wait",
                        self.tid,
                        signal,
                        held.signal
                    );
                    return Err(held_signal_restart_errno(self.restart_errno, held.signal).into());
                }
                // Round-10 High 1 on https://github.com/rrnewton/hermit/pull/3361:
                // this used to end a wait without a deadline with ERESTARTNOINTR,
                // and let a wait with one go on and lose the held signal.
                tracing::trace!(
                    "[tid {}] signal {} would replace the held signal {:?}; the wait is refused",
                    self.tid,
                    signal,
                    self.held
                );
                return Err(self.refusal(Some(signal), HeldSignalLoss::Replaced));
            }
            let result = guest.inject(call).await;
            let Err(errno) = result else {
                return Ok(result);
            };
            if !probe_was_interrupted_by_signal(errno) {
                return Ok(result);
            }
            let after = read_wait_signal_state(self.pid, self.tid)?;
            // A probe that carries the call's temporary mask, as a masked
            // ppoll's does (`with_wait_mask`), and that the kernel itself
            // ended, for a pending signal the mask lets in, leaves that mask
            // installed with its restore pending until the signal is delivered
            // as the guest resumes, so `/proc` reports a mask other than the one
            // before the injection. That ERESTARTNOHAND is the call's own
            // result, not a stop, and no injection may follow it: a repeated
            // probe would save the temporary mask as the guest's
            // (https://github.com/rrnewton/reverie/issues/1003). A pselect6
            // wait's probes carry no mask, so this never holds for one.
            if self.wait_mask.is_some()
                && errno == Errno::ERESTARTNOHAND
                && after.blocked != before.blocked
            {
                return Ok(result);
            }
            let identified =
                next.filter(|&(signal, queue)| stop_took_only(&before, &after, signal, queue));
            let Some((signal, queue)) = identified else {
                // A signal that `before` did not show stopped the injection. The
                // backend holds it in place of any held one and delivers it when
                // the guest resumes, but `/proc` cannot name it. If the backend
                // held a signal whose loss matters, or one it could not identify
                // either, that signal is gone, and the wait is refused. Otherwise
                // the wait goes on and the injection runs again, as the blind
                // retry before this machinery did, so the wait keeps its
                // deadline; the signals that `/proc` still shows are classified
                // as before, and every check that ends the wait still runs at
                // the read before the injection and at each turn's
                // `interrupted_with_state`.
                if self.holds_precious() {
                    tracing::trace!(
                        "[tid {}] a signal stop that cannot be identified replaced the held signal {:?}; the wait is refused",
                        self.tid,
                        self.held
                    );
                    return Err(self.refusal(None, HeldSignalLoss::UnidentifiedStop));
                }
                if !self.deadline && absorbed >= MAX_ABSORBED_STOPS {
                    // The backend now holds the unidentified signal.
                    self.held = None;
                    self.held_unidentified = true;
                    return Err(self.refusal(None, HeldSignalLoss::StopBound));
                }
                absorbed += 1;
                tracing::trace!(
                    "[tid {}] a signal stop that cannot be identified is held; the injection runs again",
                    self.tid
                );
                self.held = None;
                self.held_unidentified = true;
                continue;
            };
            let bit = kernel_sigset_bit(signal);
            let verdict = match self.stop_verdict(&before, signal) {
                // A probe made by `inject_first_probe` holds a signal that would
                // end the wait and runs again.
                StopVerdict::End(_)
                    if first_probe
                        && self.consumed & bit == 0
                        && self.could_interrupt(&before) & bit != 0 =>
                {
                    StopVerdict::Absorb(HeldKind::Precious)
                }
                verdict => verdict,
            };
            match verdict {
                StopVerdict::Absorb(kind) => {
                    let kind = self.held.map_or(kind, |held| held.kind.max(kind));
                    let unidentified = self.held_unidentified;
                    self.held = Some(HeldSignal {
                        signal,
                        queue,
                        kind,
                    });
                    self.held_unidentified = false;
                    if !self.deadline && absorbed >= MAX_ABSORBED_STOPS {
                        if kind == HeldKind::Precious || unidentified {
                            return Err(self.refusal(None, HeldSignalLoss::StopBound));
                        }
                        return Err(Errno::ERESTARTNOINTR.into());
                    }
                    absorbed += 1;
                    tracing::trace!(
                        "[tid {}] signal {} stopped an injection and is held ({:?})",
                        self.tid,
                        signal,
                        kind
                    );
                }
                // A signal that a wait without a deadline consumes, delivered
                // rather than returned: refused when the wait holds it until the
                // call returns, or when it replaced a held signal whose loss
                // matters, and otherwise ended with ERESTARTNOINTR (stop_verdict).
                StopVerdict::End(_)
                    if !self.deadline
                        && self.consumed & bit != 0
                        && (self.held_until_return & bit != 0 || self.holds_precious()) =>
                {
                    tracing::trace!(
                        "[tid {}] consumed signal {} stopped an injection; the wait is refused",
                        self.tid,
                        signal
                    );
                    return Err(self.refusal(Some(signal), HeldSignalLoss::Consumed));
                }
                // The backend holds `signal`, which stopped the injection.
                StopVerdict::End(errno) => {
                    return Err(held_signal_restart_errno(errno, signal).into());
                }
            }
        }
    }

    /// What a stop by `signal` means for the wait, given the kernel's state
    /// `before` the injection (see `inject_absorbing`).
    fn stop_verdict(&self, before: &KernelSignalState, signal: i32) -> StopVerdict {
        let bit = kernel_sigset_bit(signal);
        let interrupts = if self.consumed & bit != 0 {
            if !self.deadline {
                return StopVerdict::End(Errno::ERESTARTNOINTR);
            }
            // The backend delivers the signal rather than the wait consuming it,
            // as Linux does for one the guest does not block, so it is classified
            // as if the wait did not consume it.
            let guest_mask = self.wait_mask.or(self.saved_mask).unwrap_or(before.blocked);
            before.interrupting_wait(guest_mask, self.defers_default_stops) & bit != 0
        } else {
            self.could_interrupt(before) & bit != 0
        };
        if interrupts {
            return if self.held_until_return & bit != 0 {
                StopVerdict::Absorb(HeldKind::Precious)
            } else {
                StopVerdict::End(self.restart_errno)
            };
        }
        if signal == libc::SIGCHLD
            || (self.defers_default_stops && before.default_job_control_stops() & bit != 0)
        {
            return StopVerdict::Absorb(HeldKind::Precious);
        }
        let default_ignored = [libc::SIGCONT, libc::SIGURG, libc::SIGWINCH]
            .into_iter()
            .fold(0, |set, signal| set | kernel_sigset_bit(signal));
        if (before.ignored | (default_ignored & !before.caught)) & bit != 0 {
            return StopVerdict::Absorb(HeldKind::Harmless);
        }
        // A signal that the wait mask blocks, and the guest's own mask does
        // not, can stop an injection that runs before `block` takes effect.
        // Linux keeps it pending for the whole call, under the temporary mask,
        // and delivers it as the call returns, so it is held and never ends
        // the wait (review of
        // https://github.com/rrnewton/hermit/pull/4051).
        if self.deadline || self.wait_mask.is_some_and(|mask| mask & bit != 0) {
            StopVerdict::Absorb(HeldKind::Precious)
        } else {
            StopVerdict::End(Errno::ERESTARTNOINTR)
        }
    }

    /// Whether an injection that `signal`, pending on `queue`, stops would make
    /// the backend drop a held signal whose delivery matters
    /// (`HeldKind::Precious`), or one it could not identify, which may matter
    /// (`held_unidentified`). Linux merges a standard signal into the same one
    /// pending on the same queue, so that one replaces nothing.
    fn would_replace_precious(&self, signal: i32, queue: SignalQueue) -> bool {
        self.held_unidentified
            || self.held.is_some_and(|held| {
                held.kind == HeldKind::Precious
                    && !(signal == held.signal && queue == held.queue && signal < KERNEL_SIGRTMIN)
            })
    }

    /// Whether the backend holds a signal whose loss matters
    /// (`HeldKind::Precious`), or one it could not identify, which may matter.
    fn holds_precious(&self) -> bool {
        self.held_unidentified
            || self
                .held
                .is_some_and(|held| held.kind == HeldKind::Precious)
    }

    /// The `HeldSignalRefusal` that ends the wait, naming the signal the backend
    /// holds, if known, and `replacing`, the one that would replace it or that
    /// stopped the injection.
    fn refusal(&self, replacing: Option<i32>, loss: HeldSignalLoss) -> Error {
        Error::Tool(anyhow::Error::new(HeldSignalRefusal {
            wait: None,
            held: self.held.map(|held| held.signal),
            replacing,
            loss,
        }))
    }

    /// Whether a held `signal` would end the wait natively, given the kernel's
    /// state `before` an injection: it could interrupt the wait, and the wait
    /// does not hold it until the call returns (`held_until_return`).
    fn held_ends_wait(&self, before: &KernelSignalState, signal: i32) -> bool {
        self.could_interrupt(before) & !self.held_until_return & kernel_sigset_bit(signal) != 0
    }

    /// Block every blockable signal for the rest of the wait, and keep blocked the
    /// signals the guest blocks itself, which the blockable set leaves out (the C
    /// library's reserved signals). `scratch` is a guest cell for the mask when
    /// the caller holds the only scratch-stack guard; otherwise a fresh guard is
    /// taken. The change goes through `inject_absorbing`, so a signal that stops
    /// it is handled as one that stops a probe. If that ends the wait, the guest's
    /// mask is still recorded, and the caller's final `restore` reads the
    /// kernel's mask and puts the guest's back if the change took effect.
    ///
    /// If the mask cannot be changed for another reason, the guest's mask is
    /// unchanged and no wait call can return that error, so the wait continues
    /// with every probe under the guest's own mask, as the first probe runs, and
    /// `block` is not tried again (`needs_block`). `interrupted_with_state` reads
    /// that mask from the kernel each turn. The scratch-stack commit fails this
    /// way on a guest stack with no room below its stack pointer
    /// (https://github.com/rrnewton/hermit/issues/3328); poll and epoll_wait
    /// probes need no scratch of their own, so for them this is the first need.
    pub(crate) async fn block<'a, T, G>(
        &mut self,
        guest: &mut G,
        scratch: Option<AddrMut<'a, KernelSigset>>,
    ) -> Result<(), Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
    {
        let guest_mask = read_wait_signal_state(self.pid, self.tid)?.blocked;
        let mask = blocked_signal_mask() | guest_mask;
        let result = match scratch {
            Some(cell) => match guest.memory().write_value(cell, &mask) {
                Ok(()) => {
                    self.inject_absorbing(guest, set_signal_mask_call(cell.into()))
                        .await
                }
                Err(errno) => Ok(Err(errno)),
            },
            None => {
                let mut stack = guest.stack().await;
                let cell = stack.push(mask);
                match stack.commit() {
                    Ok(_guard) => {
                        self.inject_absorbing(guest, set_signal_mask_call(cell))
                            .await
                    }
                    Err(_) => Ok(Err(Errno::EFAULT)),
                }
            }
        };
        match result {
            Ok(Ok(_)) => {
                self.saved_mask = Some(guest_mask);
                Ok(())
            }
            Ok(Err(errno)) => {
                tracing::debug!(
                    "[tid {}] cannot block signals for a wait ({}); its probes run under the \
                     guest's mask",
                    self.tid,
                    errno
                );
                self.unblockable = true;
                Ok(())
            }
            Err(error) => {
                self.saved_mask = Some(guest_mask);
                Err(error)
            }
        }
    }

    /// Block every blockable signal for the rest of the wait, as `block` does,
    /// but through `Guest::set_blocked_signals`: no guest code runs, so no
    /// pending signal is taken from the kernel's queues, and every one stays
    /// there for the kernel to deliver, in its own order, once `restore` puts
    /// the guest's mask back. `block` instead injects `rt_sigprocmask`, which a
    /// pending signal can stop before it runs, moving that signal into the
    /// backend's single held slot, where a second one replaces it
    /// (https://github.com/rrnewton/hermit/issues/4273, stage 2). No scratch
    /// memory is needed, here or in `restore` (the Claude review of the R2b-2
    /// design, M1).
    ///
    /// `host_timed` is the turn's host-timed signals, held until the call
    /// returns as `hold_until_return` holds them.
    ///
    /// Returns whether the mask is now blocked. A refusal changes nothing
    /// (`MaskChangeRefused::Unsupported`, `RestorePending`), and neither does a
    /// failure that put the old mask back, so the wait goes on under the
    /// guest's own mask, as before this existed. A failure that leaves the mask
    /// unknown ends the run before the guest resumes
    /// ([`BlockedWaitSignalError::MaskNotRestored`]).
    pub(crate) async fn block_in_place<T, G>(
        &mut self,
        guest: &mut G,
        host_timed: KernelSigset,
    ) -> Result<bool, Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
    {
        // As `install_turn_mask` does for `writev`: the signals a host-timed
        // source can post (`host_timed`) are held until the call returns, so a
        // fallback injected restoration holds one that stops it
        // (`absorb_restoration_stop`) rather than counting it lost.
        self.hold_until_return(host_timed);
        let guest_mask = read_wait_signal_state(self.pid, self.tid)?.blocked;
        match guest
            .set_blocked_signals(blocked_signal_mask() | guest_mask)
            .await
        {
            Ok(old) => {
                self.saved_mask = Some(old);
                self.blocked_in_place = true;
                Ok(true)
            }
            Err(MaskChangeRefused::Failed {
                errno,
                mask_unknown: true,
            }) => Err(self.mask_not_restored(1, Some(errno))),
            Err(refused) => {
                tracing::debug!(
                    "[tid {}] cannot block signals in place for a wait ({}); its attempts run \
                     under the guest's mask",
                    self.tid,
                    refused
                );
                // Not tried again for this wait (`needs_block`).
                self.unblockable = true;
                Ok(false)
            }
        }
    }

    /// The error that ends the run when the guest's mask cannot be put back.
    fn mask_not_restored(&self, attempts: usize, last_error: Option<Errno>) -> Error {
        Error::Tool(anyhow::Error::new(
            BlockedWaitSignalError::MaskNotRestored {
                pid: self.pid,
                tid: self.tid,
                attempts,
                last_error,
            },
        ))
    }

    /// Put back the guest's mask, if `block` replaced it. A signal can stop the guest
    /// around the call, before or after it takes effect, so success is read back
    /// from the kernel and the call is repeated until the kernel reports the
    /// guest's mask. The guest never resumes with every signal blocked: if the
    /// mask cannot be put back, the run ends with
    /// [`BlockedWaitSignalError::MaskNotRestored`].
    ///
    /// The backend holds the signal that stops one of these injections, in place
    /// of any it held, as it does for a probe (`inject_absorbing`). Each such
    /// stop is recorded from the read before the injection and the read after it
    /// (`absorb_restoration_stop`). If one replaced a held signal whose loss
    /// matters, or one that could not be identified, the mask is still put back
    /// and then the first such loss is returned as a [`HeldSignalRefusal`]
    /// (`HeldSignalLoss::Restoration`), which the caller turns into the wait's
    /// result (`result_after_restore`). Round-11 High F1 on
    /// https://github.com/rrnewton/hermit/pull/3361: these stops used to be
    /// retried without that check, so the held signal was lost silently.
    pub(crate) async fn restore<'a, T, G>(
        &mut self,
        guest: &mut G,
        scratch: Option<AddrMut<'a, KernelSigset>>,
    ) -> Result<(), Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
    {
        let Some(guest_mask) = self.saved_mask else {
            return Ok(());
        };
        // A mask `block_in_place` installed goes back the same way, with no
        // injection, so no signal can stop the restoration and none is held.
        // That call is refused under the same rules as the block; a refusal
        // or a failure that left the mask as it was falls back to the
        // injected restoration below, which needs scratch memory (the Codex
        // and Claude reviews of https://github.com/rrnewton/reverie/pull/1083,
        // F4).
        if std::mem::take(&mut self.blocked_in_place) {
            match guest.set_blocked_signals(guest_mask).await {
                Ok(_) | Err(MaskChangeRefused::Gone) => {
                    self.saved_mask = None;
                    return Ok(());
                }
                Err(MaskChangeRefused::Failed {
                    errno,
                    mask_unknown: true,
                }) => {
                    self.saved_mask = None;
                    return Err(self.mask_not_restored(1, Some(errno)));
                }
                Err(refused) => {
                    tracing::debug!(
                        "[tid {}] cannot restore the mask in place ({}); injecting it",
                        self.tid,
                        refused
                    );
                }
            }
        }
        // `stop_verdict` reads the guest's mask from `saved_mask`, so it is cleared
        // only once the restoration has ended.
        let restored = self.restore_mask(guest, guest_mask, scratch).await;
        self.saved_mask = None;
        restored
    }

    /// `restore`, for a wait whose guest mask is `guest_mask`.
    async fn restore_mask<'a, T, G>(
        &mut self,
        guest: &mut G,
        guest_mask: KernelSigset,
        scratch: Option<AddrMut<'a, KernelSigset>>,
    ) -> Result<(), Error>
    where
        T: RecordOrReplay,
        G: Guest<Detcore<T>>,
    {
        // Under the all-blocked mask only a signal that cannot be blocked (SIGKILL,
        // SIGSTOP) can stop the call before it takes effect, and each such stop needs
        // another signal sent, so a bound this large is never reached in practice.
        const ATTEMPTS: usize = 16;
        let mut attempts = 0;
        let mut last_error = None;
        // The kernel's state before an injection that a signal stopped, until the
        // next read records the stop.
        let mut stopped = None;
        // The first held signal that a stop replaced, as the refusal to return once
        // the mask is back.
        let mut loss = None;
        while attempts < ATTEMPTS {
            let state = read_wait_signal_state(self.pid, self.tid)?;
            if let Some(before) = stopped.take() {
                let replaced = self.absorb_restoration_stop(&before, &state);
                loss = loss.or(replaced);
            }
            if state.blocked == guest_mask {
                return loss.map_or(Ok(()), Err);
            }
            attempts += 1;
            match inject_signal_mask(guest, guest_mask, scratch).await {
                Ok(()) => return loss.map_or(Ok(()), Err),
                // Stopped by a signal: the next read records the stop and decides
                // whether the mask took effect.
                Err(errno) if probe_was_interrupted_by_signal(errno) => {
                    last_error = Some(errno);
                    stopped = Some(state);
                }
                // The call could not run, so the mask is unchanged.
                Err(errno) => {
                    last_error = Some(errno);
                    break;
                }
            }
        }
        let state = read_wait_signal_state(self.pid, self.tid)?;
        if let Some(before) = stopped.take() {
            let replaced = self.absorb_restoration_stop(&before, &state);
            loss = loss.or(replaced);
        }
        if state.blocked == guest_mask {
            return loss.map_or(Ok(()), Err);
        }
        Err(Error::Tool(anyhow::Error::new(
            BlockedWaitSignalError::MaskNotRestored {
                pid: self.pid,
                tid: self.tid,
                attempts,
                last_error,
            },
        )))
    }

    /// Record a signal stop of an injection of `restore`, given the kernel's state
    /// `before` the injection and `after` it, as `inject_absorbing` records a stop
    /// of a probe: the backend holds the signal that stopped it in place of any it
    /// held. Returns the `HeldSignalRefusal` when that replaced a held signal
    /// whose loss matters, or one that could not be identified
    /// (`would_replace_precious`, `holds_precious`), naming the held signal as it
    /// was before the stop.
    ///
    /// A stop is identified only when exactly the signal that the kernel would
    /// take next under the mask `before` left its queue and the mask did not
    /// change (`stop_took_only`); a stop after which the mask took effect, or one
    /// that `before` did not show, is recorded as unidentified. The call's result
    /// is already decided, so a signal that would have ended the wait is kept as
    /// one whose delivery matters.
    fn absorb_restoration_stop(
        &mut self,
        before: &KernelSignalState,
        after: &KernelSignalState,
    ) -> Option<Error> {
        let identified = before
            .next_dequeued(before.blocked)
            .filter(|&(signal, queue)| stop_took_only(before, after, signal, queue));
        let Some((signal, queue)) = identified else {
            let loss = self
                .holds_precious()
                .then(|| self.refusal(None, HeldSignalLoss::Restoration));
            tracing::trace!(
                "[tid {}] a signal stop that cannot be identified stopped the mask restoration; it replaced the held signal {:?}",
                self.tid,
                self.held
            );
            self.held = None;
            self.held_unidentified = true;
            return loss;
        };
        let loss = self
            .would_replace_precious(signal, queue)
            .then(|| self.refusal(Some(signal), HeldSignalLoss::Restoration));
        let kind = match self.stop_verdict(before, signal) {
            StopVerdict::Absorb(kind) => kind,
            StopVerdict::End(_) => HeldKind::Precious,
        };
        let kind = self.held.map_or(kind, |held| held.kind.max(kind));
        tracing::trace!(
            "[tid {}] signal {} stopped the mask restoration and is held ({:?}) in place of {:?}",
            self.tid,
            signal,
            kind,
            self.held
        );
        self.held = Some(HeldSignal {
            signal,
            queue,
            kind,
        });
        self.held_unidentified = false;
        loss
    }
}

/// The wait's result once `restore` has put the guest's mask back. A
/// [`HeldSignalRefusal`] from the restoration (`HeldSignalLoss::Restoration`)
/// replaces `result`, as a loss during the wait does, unless `result` is
/// already such a refusal, which came first; the caller then applies the
/// unsupported-operation policy (`refuse_held_signal_loss`). Every other
/// restoration error, such as [`BlockedWaitSignalError::MaskNotRestored`], is
/// returned as the error that ends the run, as before.
pub(crate) fn result_after_restore(
    restored: Result<(), Error>,
    result: Result<i64, Error>,
) -> Result<Result<i64, Error>, Error> {
    let is_refusal =
        |error: &Error| matches!(error, Error::Tool(error) if error.is::<HeldSignalRefusal>());
    match restored {
        Ok(()) => Ok(result),
        Err(refusal) if is_refusal(&refusal) => match &result {
            Err(error) if is_refusal(error) => Ok(result),
            _ => Ok(Err(refusal)),
        },
        Err(error) => Err(error),
    }
}

/// Replace the guest's signal mask with `mask`, written to `scratch` or, when the
/// caller holds no scratch-stack guard, to a fresh one.
async fn inject_signal_mask<'a, T, G>(
    guest: &mut G,
    mask: KernelSigset,
    scratch: Option<AddrMut<'a, KernelSigset>>,
) -> Result<(), Errno>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    match scratch {
        Some(cell) => {
            guest.memory().write_value(cell, &mask)?;
            guest
                .inject(set_signal_mask_call(cell.into()))
                .await
                .map(drop)
        }
        None => {
            let mut stack = guest.stack().await;
            let cell = stack.push(mask);
            let _guard = stack.commit().map_err(|_| Errno::EFAULT)?;
            guest.inject(set_signal_mask_call(cell)).await.map(drop)
        }
    }
}

/// `rt_sigprocmask(SIG_SETMASK, cell, NULL)`.
fn set_signal_mask_call(cell: Addr<'_, KernelSigset>) -> syscalls::RtSigprocmask {
    syscalls::RtSigprocmask::new()
        .with_how(libc::SIG_SETMASK)
        .with_set(Some(cell.cast()))
        .with_oldset(None)
        .with_sigsetsize(KERNEL_SIGSET_SIZE)
}

/// Whether a signal stop between the kernel states `before` and `after` took
/// exactly `signal` off `queue`: it is no longer there, no other signal left
/// either queue, and the mask and dispositions did not change. Signals may have
/// arrived in between.
fn stop_took_only(
    before: &KernelSignalState,
    after: &KernelSignalState,
    signal: i32,
    queue: SignalQueue,
) -> bool {
    let bit = kernel_sigset_bit(signal);
    let other = match queue {
        SignalQueue::Thread => SignalQueue::Shared,
        SignalQueue::Shared => SignalQueue::Thread,
    };
    before.blocked == after.blocked
        && before.ignored == after.ignored
        && before.caught == after.caught
        && after.queued(queue) & bit == 0
        && before.queued(queue) & !bit & !after.queued(queue) == 0
        && before.queued(other) & !after.queued(other) == 0
}

/// Whether an injected call's error means a signal stopped the guest around it
/// rather than that the call produced a result: the backend's restart errno, or
/// `EINTR` where a backend runs the call itself. None of these calls blocks.
pub(crate) fn probe_was_interrupted_by_signal(errno: Errno) -> bool {
    matches!(
        errno,
        Errno::EINTR
            | Errno::ERESTARTSYS
            | Errno::ERESTARTNOINTR
            | Errno::ERESTARTNOHAND
            | Errno::ERESTART_RESTARTBLOCK
    )
}

pub(crate) async fn record_retry_event<G, C, T>(guest: &mut G, call: C)
where
    C: SyscallInfo,
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let dettid = guest.thread_state().dettid;
    let cfg = &guest.config();
    if cfg.sequentialize_threads && cfg.should_trace_schedevent() {
        trace_schedevent(
            guest,
            with_guest_time(
                guest,
                SchedEvent::syscall(dettid, call.number(), SyscallPhase::Polling),
            ),
            true,
        )
        .await;
    }
}

// A helper function for enriching the schedevent with local information.
pub fn with_guest_time<G, T>(guest: &G, event: SchedEvent) -> SchedEvent
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let dettime = &guest.thread_state().thread_logical_time;
    event.with_dettime(dettime)
}

// Enrich the event with the RIP register from the current guest state, but only if it is unset.
pub async fn with_guest_rip<G, T>(guest: &mut G, mut event: SchedEvent) -> SchedEvent
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    assert!(event.end_rip.is_none());

    let regs = guest.regs().await;
    let end_rip = NonZeroUsize::new(regs.rip.try_into().unwrap()).unwrap();
    event.end_rip = Some(end_rip);
    event
}

// Convert to absolute logical time point for the timeout.
// 0 duration means no timeout, and this will return None.
pub async fn millis_duration_to_absolute_timeout<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    timeout_millis: i32,
) -> Option<LogicalTime> {
    match positive_millis_as_nanos(timeout_millis) {
        Some(timeout_nanos) => nanos_duration_to_absolute_timeout(guest, timeout_nanos).await,
        None => None,
    }
}

/// Milliseconds to nanoseconds for a strictly positive timeout; `None` for the
/// non-positive values Linux treats as "return immediately" (0) or "wait
/// forever" (-1), neither of which is a deadline.
///
/// Kept as a separate, unit-bracketed function on purpose. This conversion was
/// previously inlined as `* 1000` instead of `* 1_000_000`, which made every
/// finite deadline 1000x too short (a 1 ms timeout expired after 1 us). That
/// is invisible in an end-to-end test that only checks a syscall's return
/// value, so the arithmetic is pinned here by `millis_to_nanos_conversion`.
fn positive_millis_as_nanos(timeout_millis: i32) -> Option<u128> {
    (timeout_millis > 0).then(|| (timeout_millis as u128) * 1_000_000)
}

// Convert to absolute logical time point for the timeout.
// 0 duration means no timeout, and this will return None.
pub async fn nanos_duration_to_absolute_timeout<G: Guest<Detcore<T>>, T: RecordOrReplay>(
    guest: &mut G,
    timeout_nanos: u128,
) -> Option<LogicalTime> {
    if timeout_nanos > 0 {
        let ns_delta = Duration::from_nanos(timeout_nanos as u64);
        let base_time = thread_observe_time(guest).await;
        let target_time = base_time + ns_delta;
        Some(target_time)
    } else {
        None
    }
}

/// The number of `pollfd` entries `poll` and `ppoll` read. Linux declares
/// their `nfds` parameter `unsigned int`, so only the low 32 bits of the
/// register count; a guest may leave the high word set. Every reader of a
/// guest's pollfd array sizes it from this, never from `nfds` itself.
pub fn poll_nfds(nfds: libc::nfds_t) -> u32 {
    nfds as u32
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Bracket the millisecond-to-nanosecond timeout conversion in both
    /// directions: the non-positive values that are NOT deadlines, and the
    /// positive values whose magnitude must be exactly 1e6 ns per ms.
    ///
    /// The 1 ms case is the specific regression guard: an inlined `* 1000`
    /// yields 1_000 here instead of 1_000_000, i.e. a deadline 1000x too
    /// short. Asserting the exact value (not merely "nonzero" or "greater
    /// than") is what makes that failure visible.
    #[test]
    fn millis_to_nanos_conversion() {
        // Not deadlines: -1 is "infinite", 0 is "return immediately".
        assert_eq!(positive_millis_as_nanos(-1), None);
        assert_eq!(positive_millis_as_nanos(i32::MIN), None);
        assert_eq!(positive_millis_as_nanos(0), None);

        // Positive timeouts: exactly 1e6 nanoseconds per millisecond.
        assert_eq!(positive_millis_as_nanos(1), Some(1_000_000));
        assert_eq!(positive_millis_as_nanos(1_000), Some(1_000_000_000));
        assert_eq!(
            positive_millis_as_nanos(i32::MAX),
            Some(i32::MAX as u128 * 1_000_000)
        );

        // The scale itself, stated independently of any single case so a
        // future refactor cannot satisfy the above by coincidence.
        for millis in [1, 2, 7, 250, 1_000, 86_400_000] {
            assert_eq!(
                positive_millis_as_nanos(millis),
                Some(millis as u128 * 1_000_000),
                "1 ms must convert to 1_000_000 ns, not 1_000"
            );
        }
    }

    #[test]
    fn connect_nonblocking_results() {
        let call = reverie::syscalls::Connect::new();
        assert!(call.syscall_would_have_blocked(Err(Errno::EINPROGRESS)));
        assert!(call.syscall_would_have_blocked(Err(Errno::EALREADY)));
        assert_eq!(
            call.normalize_nonblocking_result(Err(Errno::EISCONN), true),
            Ok(0)
        );
        assert_eq!(
            call.normalize_nonblocking_result(Err(Errno::EISCONN), false),
            Err(Errno::EISCONN)
        );
    }

    #[test]
    fn signal_interruption_errno_matches_linux_restart_policy() {
        assert_eq!(
            reverie::syscalls::Poll::new().signal_interrupt_errno(),
            Errno::EINTR
        );
        assert_eq!(
            reverie::syscalls::Ppoll::new().signal_interrupt_errno(),
            Errno::EINTR
        );
        assert_eq!(
            reverie::syscalls::EpollWait::new().signal_interrupt_errno(),
            Errno::EINTR
        );
        let sigtimedwait = reverie::syscalls::RtSigtimedwait::new();
        assert_eq!(sigtimedwait.signal_interrupt_errno(), Errno::EINTR);
        assert!(sigtimedwait.syscall_would_have_blocked(Err(Errno::EAGAIN)));
        assert_eq!(sigtimedwait.timeout_return_val(), Err(Errno::EAGAIN));
        assert_eq!(
            reverie::syscalls::Read::new().signal_interrupt_errno(),
            Errno::ERESTARTSYS
        );
        // A zero-progress writev interruption returns this internal errno on
        // ptrace so Linux applies the handler's SA_RESTART policy.
        assert_eq!(
            reverie::syscalls::Writev::new().signal_interrupt_errno(),
            Errno::ERESTARTSYS
        );
        assert_eq!(
            reverie::syscalls::Futex::new().signal_interrupt_errno(),
            Errno::ERESTARTSYS
        );
    }

    #[test]
    fn kernel_restart_errno_matches_linux_restart_policy() {
        // Signal-state interruption hands the kernel the wait's own restart code, so
        // the guest's disposition decides between a handler's EINTR and a restart
        // (https://github.com/rrnewton/hermit/issues/3146).
        // A poll with a positive timeout and a timed futex wait end with Linux's own
        // code: a handler turns it into EINTR, and with no handler the kernel runs
        // restart_syscall, which Detcore resumes with the deadline it kept
        // (`RestartBlock`, https://github.com/rrnewton/hermit/issues/3358).
        assert_eq!(
            reverie::syscalls::Poll::new()
                .with_timeout(300)
                .kernel_restart_errno(),
            Errno::ERESTART_RESTARTBLOCK
        );
        // A zero timeout never blocks and a negative one never expires, so there is
        // no deadline to keep, and running the call again is Linux's restart.
        assert_eq!(
            reverie::syscalls::Poll::new().kernel_restart_errno(),
            Errno::ERESTARTNOHAND
        );
        assert_eq!(
            reverie::syscalls::Poll::new()
                .with_timeout(-1)
                .kernel_restart_errno(),
            Errno::ERESTARTNOHAND
        );
        assert_eq!(
            reverie::syscalls::Ppoll::new().kernel_restart_errno(),
            Errno::ERESTARTNOHAND
        );
        // epoll_wait and sigtimedwait never restart after a handler.
        assert_eq!(
            reverie::syscalls::EpollWait::new().kernel_restart_errno(),
            Errno::EINTR
        );
        assert_eq!(
            reverie::syscalls::RtSigtimedwait::new().kernel_restart_errno(),
            Errno::EINTR
        );
        assert_eq!(
            reverie::syscalls::Futex::new().kernel_restart_errno(),
            Errno::ERESTARTSYS
        );
        // A timed wait is never restarted after a handler runs. FUTEX_WAIT's timeout
        // is relative, so its deadline is kept as poll's is.
        let timeout = reverie::syscalls::Addr::from_raw(0x1000).unwrap();
        assert_eq!(
            reverie::syscalls::Futex::new()
                .with_timeout(Some(timeout))
                .kernel_restart_errno(),
            Errno::ERESTART_RESTARTBLOCK
        );
        // FUTEX_WAIT_BITSET's timeout is an absolute deadline, which Linux copies
        // into the restart block as it does FUTEX_WAIT's, so a deadline the guest
        // moves in memory after the wait began has no effect. Running the call
        // again, which ERESTARTNOHAND did, read the moved deadline (round-10
        // finding Medium 4 on https://github.com/rrnewton/hermit/pull/3361).
        let bitset = reverie::syscalls::Futex::new()
            .with_futex_op(libc::FUTEX_WAIT_BITSET | libc::FUTEX_PRIVATE_FLAG)
            .with_val3(-1);
        assert_eq!(bitset.kernel_restart_errno(), Errno::ERESTARTSYS);
        assert!(!bitset.restart_keeps_deadline());
        assert_eq!(
            bitset.with_timeout(Some(timeout)).kernel_restart_errno(),
            Errno::ERESTART_RESTARTBLOCK
        );
        assert!(bitset.with_timeout(Some(timeout)).restart_keeps_deadline());
    }

    #[test]
    fn a_held_signal_is_reported_unless_it_is_sigstop() {
        // The backend passes a held signal on unreported when the call returns
        // ERESTART_RESTARTBLOCK, so a held signal other than SIGSTOP ends the wait
        // with ERESTARTNOHAND, which a handler also turns into EINTR.
        for signal in [libc::SIGUSR1, libc::SIGTERM, libc::SIGTSTP, libc::SIGCHLD] {
            assert_eq!(
                held_signal_restart_errno(Errno::ERESTART_RESTARTBLOCK, signal),
                Errno::ERESTARTNOHAND,
                "signal {signal}"
            );
        }
        // SIGSTOP is never reported, and its restart must keep the deadline.
        assert_eq!(
            held_signal_restart_errno(Errno::ERESTART_RESTARTBLOCK, libc::SIGSTOP),
            Errno::ERESTART_RESTARTBLOCK
        );
        // Every other restart code is the call's own.
        for errno in [
            Errno::ERESTARTNOHAND,
            Errno::ERESTARTSYS,
            Errno::ERESTARTNOINTR,
            Errno::EINTR,
        ] {
            for signal in [libc::SIGUSR1, libc::SIGSTOP] {
                assert_eq!(held_signal_restart_errno(errno, signal), errno);
            }
        }
    }

    #[test]
    fn a_restart_block_resumes_only_at_the_interrupted_call() {
        let timeout = reverie::syscalls::Addr::from_raw(0x1000).unwrap();
        let block = RestartBlock {
            rip: 0x4000_1234,
            deadline: Some(LogicalTime::from_nanos(300_000_000)),
            call: RestartCall::Futex(reverie::syscalls::Futex::new().with_timeout(Some(timeout))),
        };
        // The kernel's restart runs restart_syscall at the interrupted call's address.
        assert_eq!(block.resumed_at(0x4000_1234), Some(block));
        // A restart_syscall anywhere else is the guest's own call.
        assert_eq!(block.resumed_at(0x4000_1236), None);
        assert_eq!(block.resumed_at(0x4000_1232), None);
    }

    #[test]
    fn writev_signal_result_uses_disposition_and_progress() {
        let call = reverie::syscalls::Writev::new();
        for disposition in [
            WaitSignalDisposition::Interrupt,
            WaitSignalDisposition::Restart,
        ] {
            assert!(matches!(
                interrupted_write_result(&call, 0, Some(disposition)),
                Some(Err(Error::Errno(Errno::ERESTARTSYS)))
            ));
            assert!(matches!(
                interrupted_write_result(&call, 17, Some(disposition)),
                Some(Ok(17))
            ));
        }
        assert!(interrupted_write_result(&call, 0, None).is_none());
        assert!(interrupted_write_result(&call, 17, None).is_none());
    }

    #[test]
    fn pipe_writev_requests_signal_disposition() {
        let dettid = DetTid::from_raw(42);
        let call = reverie::syscalls::Writev::new();
        let request = pipe_writev_resources(dettid, call);

        assert_eq!(
            request.signal_interrupt_errno(),
            Some(Errno::ERESTARTSYS.into_raw())
        );
        assert!(request.decides_signal_interruption_itself);
        assert_eq!(request.resources.len(), 1);
    }
}

/// `KernelSignalWait` against a scripted guest and a scripted `/proc` read
/// (`signal_state_read_seam`). No failure of the wait's signal handling may reach
/// the guest as an errno its wait call cannot return, and the guest must never
/// resume with every signal blocked
/// (https://github.com/rrnewton/hermit/issues/3146).
#[cfg(test)]
mod kernel_signal_wait_failures {
    use std::collections::VecDeque;
    use std::sync::Arc;
    use std::sync::Mutex;

    use reverie::GlobalRPC;
    use reverie::GlobalTool;
    use reverie::Pid;
    use reverie::syscalls::LocalMemory;

    use super::*;
    use crate::Config;
    use crate::GlobalState;
    use crate::ThreadState;
    use crate::syscalls::threads::KernelSignalState;
    use crate::syscalls::threads::kernel_sigset_bit;
    use crate::syscalls::threads::signal_state_read_seam;
    use crate::types::DetPid;

    /// What an injected `rt_sigprocmask` does.
    #[derive(Clone, Copy, Debug)]
    enum MaskOutcome {
        /// The call runs: the mask changes and it returns 0.
        Apply,
        /// A signal stops the guest before the call runs: the mask is unchanged
        /// and the backend reports its restart errno.
        StoppedBefore,
        /// The call cannot run at all.
        Fail(Errno),
        /// `StoppedBefore`, by a signal that `/proc` never showed because it
        /// arrived after the read before the injection, such as a `SIGSTOP`
        /// sent from outside: the backend holds it in its single slot
        /// (`FakeKernel::held_slot`).
        StoppedBy(i32),
    }

    /// The kernel's view of the guest thread, shared by the scripted `/proc`
    /// read and the scripted guest.
    #[derive(Default)]
    struct FakeKernel {
        /// The thread's mask (`SigBlk`).
        blocked: KernelSigset,
        /// Pending signals (`SigPnd | ShdPnd`).
        pending: KernelSigset,
        /// The signals of `pending` pending only on the thread group's shared
        /// queue (`ShdPnd`); every other one is on the thread's own
        /// (`SigPnd`).
        shared: KernelSigset,
        /// Signals with a handler (`SigCgt`).
        caught: KernelSigset,
        /// Signals whose disposition is `SIG_IGN` (`SigIgn`).
        ignored: KernelSigset,
        /// Whether a pending signal outside the mask stops each injection before
        /// the call runs, as on ptrace, where an ignored signal still queues for
        /// a traced task: it leaves the queue for the backend to hold
        /// (`taken`), and the injection reports a restart errno.
        stops_for_pending: bool,
        /// The signals that stopped an injection, in order.
        taken: Vec<i32>,
        /// Errors for the next reads, one per read.
        read_failures: VecDeque<Errno>,
        /// Outcomes for the next `rt_sigprocmask` calls; `Apply` once empty.
        outcomes: VecDeque<MaskOutcome>,
        /// Every mask the guest was asked to set, in order.
        requested: Vec<KernelSigset>,
        /// How many more signals that stop an injection are sent again, as by
        /// a sender faster than the injections: each such signal is pending
        /// again from the read after the one that follows the stop, which still
        /// shows it gone, so the stop is identified (`pending_repost`).
        repost_after_take: usize,
        /// The signals sent again, which the read after the next one shows.
        pending_repost: KernelSigset,
        /// The backend's one slot for a held signal: each signal that stops an
        /// injection (`taken`, `MaskOutcome::StoppedBy`) replaces the one there,
        /// as Reverie's ptrace backend does (`hold_pending_signal`).
        held_slot: Option<i32>,
        /// The signals that a stop dropped from `held_slot`, in order. A standard
        /// signal that replaces itself is merged, as Linux merges a second one
        /// sent while the first is pending, and is not lost.
        lost: Vec<i32>,
        /// Answers for the next `Guest::set_blocked_signals` calls: `None`
        /// sets the mask and returns the old one; once empty, every call is
        /// refused as `Unsupported`, as the trait's default refuses.
        setter: VecDeque<Option<MaskChangeRefused>>,
        /// Every mask `Guest::set_blocked_signals` was asked to set, in order.
        set_calls: Vec<KernelSigset>,
        /// The scheduler's answer to `HostTimedAndPrivateSigchld`: the
        /// host-timed signals, then the recorded private `SIGCHLD`. `None`
        /// panics, as every other request does.
        private_record: Option<(KernelSigset, KernelSigset)>,
    }

    impl FakeKernel {
        /// The backend holds `signal`, which stopped an injection.
        fn hold(&mut self, signal: i32) {
            if let Some(previous) = self.held_slot.replace(signal)
                && !(previous == signal && signal < KERNEL_SIGRTMIN)
            {
                self.lost.push(previous);
            }
        }
    }

    type Kernel = Arc<Mutex<FakeKernel>>;

    /// Room for one mask, 8-byte aligned like a real stack slot.
    const ARENA_WORDS: usize = 2;

    /// A scratch stack in this process's memory whose commit can fail like the
    /// ptrace scratch below an `rsp` with no writable memory under it
    /// (https://github.com/rrnewton/hermit/issues/3328).
    struct WaitStack {
        commit_fails: bool,
        arena: usize,
    }

    struct WaitStackGuard;

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

    impl reverie::Stack for WaitStack {
        type StackGuard = WaitStackGuard;

        fn size(&self) -> usize {
            panic!("a blocked wait must not query the scratch size")
        }
        fn capacity(&self) -> usize {
            panic!("a blocked wait must not query the scratch capacity")
        }
        fn push<'stack, T>(&mut self, value: T) -> Addr<'stack, T> {
            assert!(std::mem::size_of::<T>() <= ARENA_WORDS * std::mem::size_of::<u64>());
            // SAFETY: the arena is a live, 8-byte aligned buffer owned by the guest,
            // large enough for `T` (asserted above).
            unsafe { std::ptr::write(self.arena as *mut T, value) };
            Addr::from_raw(self.arena).unwrap()
        }
        fn reserve<'stack, T>(&mut self) -> AddrMut<'stack, T> {
            panic!("a blocked wait pushes its mask rather than reserving room")
        }
        fn commit(self) -> Result<Self::StackGuard, Errno> {
            if self.commit_fails {
                Err(Errno::EFAULT)
            } else {
                Ok(WaitStackGuard)
            }
        }
    }

    struct WaitGuest {
        config: Config,
        thread: ThreadState<()>,
        pid: Pid,
        tid: Pid,
        commit_fails: bool,
        arena: Box<[u64; ARENA_WORDS]>,
        kernel: Kernel,
    }

    impl WaitGuest {
        /// A guest for thread `tid` of this process, with the guest's own `mask`
        /// installed.
        fn new(tid: Pid, mask: KernelSigset) -> (Self, Kernel) {
            let config = Config::default();
            let thread = ThreadState::new(DetPid::from_raw(1), &config, ());
            let kernel = Arc::new(Mutex::new(FakeKernel {
                blocked: mask,
                ..FakeKernel::default()
            }));
            let guest = Self {
                config,
                thread,
                pid: Pid::from_raw(std::process::id() as i32),
                tid,
                commit_fails: false,
                arena: Box::new([u64::MAX; ARENA_WORDS]),
                kernel: kernel.clone(),
            };
            (guest, kernel)
        }

        /// A guest for the calling thread, which exists.
        fn live(mask: KernelSigset) -> (Self, Kernel) {
            // SAFETY: gettid has no preconditions.
            Self::new(Pid::from_raw(unsafe { libc::gettid() }), mask)
        }
    }

    #[test]
    fn inherited_input_exempts_only_authenticated_reads_from_the_internal_guard() {
        use std::os::fd::AsRawFd;
        let (mut guest, _) = WaitGuest::live(guest_mask());
        let slot = tempfile::tempfile().unwrap();
        let fd = slot.as_raw_fd();
        let input = reverie::InheritedInput::pipe(fd).unwrap();
        let identity = input.identity().clone();
        let (reader, _writer) = input.into_reader_and_writer();
        assert_eq!(unsafe { libc::dup2(reader.as_raw_fd(), fd) }, fd);
        guest
            .thread
            .add_fd(fd, nix::fcntl::OFlag::O_RDONLY, FdType::Pipe, None)
            .unwrap();
        let calls = [
            Syscall::from(syscalls::Read::new().with_fd(fd)),
            Syscall::from(syscalls::Readv::new().with_fd(fd)),
            Syscall::from(syscalls::Write::new().with_fd(fd)),
            Syscall::from(syscalls::Writev::new().with_fd(fd)),
        ];
        for call in calls {
            assert!(syscall_targets_internal_fd(&mut guest, call));
        }
        guest
            .thread
            .file_metadata
            .lock()
            .unwrap()
            .apply_exec_fd_state(
                guest.thread.dettid,
                crate::tool_local::ExecFdState {
                    initial_input: Some(identity),
                    ..Default::default()
                },
                true,
            )
            .unwrap();
        for (index, call) in calls.into_iter().enumerate() {
            assert_eq!(syscall_targets_internal_fd(&mut guest, call), index >= 2);
        }
        assert!(
            !guest
                .thread
                .with_detfd(fd, |d| d.physically_nonblocking())
                .unwrap()
        );
        assert_eq!(
            unsafe { libc::fcntl(fd, libc::F_GETFL) } & libc::O_NONBLOCK,
            0
        );
        let (internal, _writer) = nix::unistd::pipe().unwrap();
        guest
            .thread
            .add_fd(
                internal.as_raw_fd(),
                nix::fcntl::OFlag::O_RDONLY,
                FdType::Pipe,
                None,
            )
            .unwrap();
        assert!(syscall_targets_internal_fd(
            &mut guest,
            syscalls::Read::new().with_fd(internal.as_raw_fd()).into()
        ));
        // Socketpair ownership remains internal as well.
        let (socket, _peer) = std::os::unix::net::UnixStream::pair().unwrap();
        guest
            .thread
            .add_fd(
                socket.as_raw_fd(),
                nix::fcntl::OFlag::O_RDWR,
                FdType::Socket,
                None,
            )
            .unwrap();
        guest
            .thread
            .with_detfd(socket.as_raw_fd(), |d| d.set_socketpair_endpoint())
            .unwrap();
        assert!(syscall_targets_internal_fd(
            &mut guest,
            syscalls::Readv::new().with_fd(socket.as_raw_fd()).into()
        ));
    }

    /// Answer this thread's `/proc` reads from `kernel` until the result drops.
    fn scripted_proc(kernel: &Kernel) -> signal_state_read_seam::Installed {
        let kernel = kernel.clone();
        signal_state_read_seam::install(move |_, _| {
            let mut kernel = kernel.lock().unwrap();
            let read = match kernel.read_failures.pop_front() {
                Some(errno) => Err(errno),
                None => Ok(KernelSignalState {
                    pending: kernel.pending,
                    thread_pending: kernel.pending & !kernel.shared,
                    shared_pending: kernel.pending & kernel.shared,
                    blocked: kernel.blocked,
                    caught: kernel.caught,
                    ignored: kernel.ignored,
                }),
            };
            kernel.pending |= std::mem::take(&mut kernel.pending_repost);
            Some(read)
        })
    }

    #[reverie::tool]
    impl GlobalRPC<GlobalState> for WaitGuest {
        async fn send_rpc(
            &self,
            message: <GlobalState as GlobalTool>::Request,
        ) -> <GlobalState as GlobalTool>::Response {
            match (&message.2, self.kernel.lock().unwrap().private_record) {
                (
                    crate::tool_global::GlobalRequest::HostTimedAndPrivateSigchld(_),
                    Some((host_timed, private)),
                ) => (
                    None,
                    crate::tool_global::GlobalResponse::HostTimedAndPrivateSigchld(
                        host_timed, private,
                    ),
                ),
                _ => panic!("these waits must not send an RPC: {:?}", message.2),
            }
        }
        fn config(&self) -> &Config {
            &self.config
        }
    }

    #[reverie::tool]
    impl Guest<Detcore> for WaitGuest {
        type Memory = LocalMemory;
        type Stack = WaitStack;

        fn tid(&self) -> Pid {
            self.tid
        }
        fn pid(&self) -> Pid {
            self.pid
        }
        fn ppid(&self) -> Option<Pid> {
            None
        }
        fn memory(&self) -> Self::Memory {
            LocalMemory::new()
        }
        fn thread_state_mut(&mut self) -> &mut ThreadState<()> {
            &mut self.thread
        }
        fn thread_state(&self) -> &ThreadState<()> {
            &self.thread
        }
        async fn regs(&mut self) -> libc::user_regs_struct {
            panic!("a blocked wait must not read registers")
        }
        async fn stack(&mut self) -> Self::Stack {
            WaitStack {
                commit_fails: self.commit_fails,
                arena: self.arena.as_mut_ptr() as usize,
            }
        }
        async fn daemonize(&mut self) {
            panic!("a blocked wait must not daemonize")
        }
        async fn inject<S: SyscallInfo>(&mut self, syscall: S) -> Result<i64, Errno> {
            let (number, args) = syscall.into_parts();
            let Syscall::RtSigprocmask(call) = Syscall::from_raw(number, args) else {
                panic!("a blocked wait injected {number}, not rt_sigprocmask")
            };
            assert_eq!(call.how(), libc::SIG_SETMASK);
            assert!(call.oldset().is_none());
            let set = call.set().expect("rt_sigprocmask without a mask");
            let mask: KernelSigset = LocalMemory::new().read_value(set.cast::<KernelSigset>())?;
            let mut kernel = self.kernel.lock().unwrap();
            kernel.requested.push(mask);
            let deliverable = kernel.pending & !kernel.blocked;
            if kernel.stops_for_pending && deliverable != 0 {
                let signal = deliverable.trailing_zeros() as i32 + 1;
                kernel.pending &= !kernel_sigset_bit(signal);
                kernel.taken.push(signal);
                kernel.hold(signal);
                if kernel.repost_after_take > 0 {
                    kernel.repost_after_take -= 1;
                    kernel.pending_repost |= kernel_sigset_bit(signal);
                }
                return Err(Errno::ERESTARTSYS);
            }
            match kernel.outcomes.pop_front().unwrap_or(MaskOutcome::Apply) {
                MaskOutcome::Apply => {
                    kernel.blocked = mask;
                    Ok(0)
                }
                MaskOutcome::StoppedBefore => Err(Errno::ERESTARTNOINTR),
                MaskOutcome::StoppedBy(signal) => {
                    kernel.hold(signal);
                    Err(Errno::ERESTARTNOINTR)
                }
                MaskOutcome::Fail(errno) => Err(errno),
            }
        }
        async fn set_blocked_signals(&mut self, mask: u64) -> Result<u64, MaskChangeRefused> {
            let mut kernel = self.kernel.lock().unwrap();
            kernel.set_calls.push(mask);
            match kernel.setter.pop_front() {
                Some(None) => Ok(std::mem::replace(&mut kernel.blocked, mask)),
                Some(Some(refused)) => Err(refused),
                None => Err(MaskChangeRefused::Unsupported),
            }
        }

        async fn tail_inject<S: SyscallInfo>(&mut self, _: S) -> reverie::Never {
            panic!("a blocked wait must not retire the guest")
        }
        fn set_timer(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
            panic!("a blocked wait must not set a timer")
        }
        fn set_timer_precise(&mut self, _: reverie::TimerSchedule) -> Result<(), Error> {
            panic!("a blocked wait must not set a timer")
        }
        fn read_clock(&mut self) -> Result<u64, Error> {
            panic!("a blocked wait must not read a clock")
        }
    }

    /// The guest's own mask in these tests.
    fn guest_mask() -> KernelSigset {
        kernel_sigset_bit(libc::SIGUSR2)
    }

    /// The diagnostic a result ends the run with.
    fn diagnostic<V: std::fmt::Debug>(result: Result<V, Error>) -> BlockedWaitSignalError {
        match result {
            Err(Error::Tool(error)) => *error
                .downcast_ref::<BlockedWaitSignalError>()
                .unwrap_or_else(|| panic!("not a blocked-wait diagnostic: {error:#}")),
            other => panic!("expected the run to end with a diagnostic, got {other:?}"),
        }
    }

    /// The held-signal refusal a result ends the wait with.
    fn refusal<V: std::fmt::Debug>(result: Result<V, Error>) -> HeldSignalRefusal {
        match result {
            Err(Error::Tool(error)) => *error
                .downcast_ref::<HeldSignalRefusal>()
                .unwrap_or_else(|| panic!("not a held-signal refusal: {error:#}")),
            other => panic!("expected the wait to end with a held-signal refusal, got {other:?}"),
        }
    }

    /// A wait whose guest mask `block` replaced with the all-blocked mask.
    async fn blocked_wait(guest: &mut WaitGuest, kernel: &Kernel) -> KernelSignalWait {
        let mut wait = KernelSignalWait::new(guest, 0, false, Errno::ERESTARTSYS);
        wait.block(guest, None).await.unwrap();
        assert!(!wait.needs_block());
        assert_eq!(kernel.lock().unwrap().blocked, blocked_signal_mask());
        wait
    }

    /// Each stopped attempt is a stop by a signal that `/proc` cannot identify,
    /// which the backend holds in place of the one before. `restore` used to
    /// return success here; the second stop drops the first unidentified one,
    /// which may matter, so the mask is still put back and the wait now ends
    /// with a `HeldSignalRefusal`, as a wait loop that meets two such stops
    /// does (round-11 High F1 on
    /// https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn restore_repeats_the_mask_change_until_the_kernel_reports_it() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        let mut wait = blocked_wait(&mut guest, &kernel).await;
        kernel
            .lock()
            .unwrap()
            .outcomes
            .extend([MaskOutcome::StoppedBefore; 5]);

        let result = wait.restore(&mut guest, None).await;

        assert_eq!(
            refusal(result),
            HeldSignalRefusal {
                wait: None,
                held: None,
                replacing: None,
                loss: HeldSignalLoss::Restoration,
            }
        );
        let kernel = kernel.lock().unwrap();
        assert_eq!(kernel.blocked, guest_mask());
        assert_eq!(
            kernel.requested.len(),
            1 + 6,
            "block, then five stopped attempts and one that ran"
        );
        assert!(
            kernel.requested[1..]
                .iter()
                .all(|&mask| mask == guest_mask())
        );
    }

    #[tokio::test]
    async fn a_mask_that_signals_keep_stopping_ends_the_run_instead_of_resuming_the_guest() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        let mut wait = blocked_wait(&mut guest, &kernel).await;
        kernel
            .lock()
            .unwrap()
            .outcomes
            .extend([MaskOutcome::StoppedBefore; 64]);

        let error = diagnostic(wait.restore(&mut guest, None).await);

        assert_eq!(
            error,
            BlockedWaitSignalError::MaskNotRestored {
                pid: guest.pid,
                tid: guest.tid,
                attempts: 16,
                last_error: Some(Errno::ERESTARTNOINTR),
            }
        );
        let message = error.to_string();
        assert!(
            message.starts_with("cannot restore the signal mask of guest thread")
                && message.contains("(16 attempts, last error ")
                && message.contains("ERESTARTNOINTR")
                && message.ends_with("); it would resume with every signal blocked"),
            "{message}"
        );
        assert_eq!(kernel.lock().unwrap().requested.len(), 1 + 16);
    }

    #[tokio::test]
    async fn a_mask_change_that_cannot_run_ends_the_run_instead_of_resuming_the_guest() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        let mut wait = blocked_wait(&mut guest, &kernel).await;
        kernel
            .lock()
            .unwrap()
            .outcomes
            .push_back(MaskOutcome::Fail(Errno::EFAULT));

        let error = diagnostic(wait.restore(&mut guest, None).await);

        assert_eq!(
            error,
            BlockedWaitSignalError::MaskNotRestored {
                pid: guest.pid,
                tid: guest.tid,
                attempts: 1,
                last_error: Some(Errno::EFAULT),
            }
        );
    }

    /// `block_in_place` blocks through `Guest::set_blocked_signals`, with no
    /// injection, and `restore` puts the guest's mask back the same way: no
    /// scratch memory is touched, so a stack with no room below it (a failing
    /// commit) changes nothing (Claude review of the R2b-2 design, M1).
    #[tokio::test]
    async fn an_in_place_block_and_its_restore_make_no_injection() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        guest.commit_fails = true;
        kernel.lock().unwrap().setter.extend([None, None]);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert!(wait.block_in_place(&mut guest, 0).await.unwrap());
        assert_eq!(wait.effective_mask(), Some(guest_mask()));
        assert_eq!(
            kernel.lock().unwrap().blocked,
            blocked_signal_mask() | guest_mask()
        );
        wait.restore(&mut guest, None).await.unwrap();
        let kernel = kernel.lock().unwrap();
        assert_eq!(kernel.blocked, guest_mask());
        assert_eq!(
            kernel.set_calls,
            vec![blocked_signal_mask() | guest_mask(), guest_mask()]
        );
        assert!(
            kernel.requested.is_empty(),
            "no rt_sigprocmask was injected"
        );
        assert_eq!(wait.effective_mask(), None);
    }

    /// The in-place block holds the turn's host-timed signals until the call
    /// returns (`hold_until_return`), as `install_turn_mask` does for
    /// `writev`, so a fallback injected restoration that one of them stops
    /// holds it rather than counting it lost (Claude re-check of R2b-2 stage
    /// 2, F2).
    #[tokio::test]
    async fn an_in_place_block_holds_the_host_timed_signals() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        kernel.lock().unwrap().setter.push_back(None);
        let host_timed = kernel_sigset_bit(libc::SIGIO) | kernel_sigset_bit(libc::SIGHUP);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert!(wait.block_in_place(&mut guest, host_timed).await.unwrap());
        assert_eq!(wait.held_until_return & host_timed, host_timed);
        assert_eq!(wait.host_timed & host_timed, host_timed);
    }

    /// A refused in-place block changes nothing: the wait goes on under the
    /// guest's own mask, does not try again (`needs_block`), and has nothing
    /// to restore.
    #[tokio::test]
    async fn a_refused_in_place_block_leaves_the_guest_mask() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        kernel
            .lock()
            .unwrap()
            .setter
            .push_back(Some(MaskChangeRefused::RestorePending {
                saved: guest_mask(),
                effective: 0,
            }));
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert!(!wait.block_in_place(&mut guest, 0).await.unwrap());
        assert!(!wait.needs_block());
        assert_eq!(wait.effective_mask(), None);
        wait.restore(&mut guest, None).await.unwrap();
        let kernel = kernel.lock().unwrap();
        assert_eq!(kernel.blocked, guest_mask());
        assert_eq!(kernel.set_calls.len(), 1);
        assert!(kernel.requested.is_empty());
    }

    /// A restore the setter refuses falls back to the injected restoration,
    /// which puts the guest's mask back (the reviews of
    /// https://github.com/rrnewton/reverie/pull/1083, F4).
    #[tokio::test]
    async fn a_refused_in_place_restore_falls_back_to_an_injection() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        kernel
            .lock()
            .unwrap()
            .setter
            .extend([None, Some(MaskChangeRefused::Unsupported)]);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert!(wait.block_in_place(&mut guest, 0).await.unwrap());
        wait.restore(&mut guest, None).await.unwrap();
        let kernel = kernel.lock().unwrap();
        assert_eq!(kernel.blocked, guest_mask());
        assert_eq!(kernel.requested, vec![guest_mask()], "one injected restore");
    }

    /// A block or restore whose failure leaves the mask unknown ends the run
    /// before the guest resumes.
    #[tokio::test]
    async fn an_unknown_mask_after_an_in_place_change_ends_the_run() {
        let unknown = Some(MaskChangeRefused::Failed {
            errno: Errno::EIO,
            mask_unknown: true,
        });
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        let not_restored = BlockedWaitSignalError::MaskNotRestored {
            pid: guest.pid,
            tid: guest.tid,
            attempts: 1,
            last_error: Some(Errno::EIO),
        };
        kernel.lock().unwrap().setter.push_back(unknown);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert_eq!(
            diagnostic(wait.block_in_place(&mut guest, 0).await),
            not_restored
        );

        kernel.lock().unwrap().setter.extend([None, unknown]);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert!(wait.block_in_place(&mut guest, 0).await.unwrap());
        assert_eq!(
            diagnostic(wait.restore(&mut guest, None).await),
            not_restored
        );
        assert!(kernel.lock().unwrap().requested.is_empty());
    }

    /// A thread that is gone when the mask goes back has nothing to restore.
    #[tokio::test]
    async fn a_gone_thread_needs_no_restore() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        kernel
            .lock()
            .unwrap()
            .setter
            .extend([None, Some(MaskChangeRefused::Gone)]);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert!(wait.block_in_place(&mut guest, 0).await.unwrap());
        wait.restore(&mut guest, None).await.unwrap();
        assert!(kernel.lock().unwrap().requested.is_empty());
    }

    #[tokio::test]
    async fn an_unreadable_signal_state_of_a_live_thread_ends_the_run() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        let _proc = scripted_proc(&kernel);
        let (pid, tid) = (guest.pid, guest.tid);
        let expected = |errno| BlockedWaitSignalError::StateUnreadable { pid, tid, errno };

        // The turn's check, before the mask is set.
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        kernel.lock().unwrap().read_failures.push_back(Errno::EIO);
        assert_eq!(diagnostic(wait.interrupted()), expected(Errno::EIO));

        // `block`'s read of the guest's mask.
        kernel.lock().unwrap().read_failures.push_back(Errno::ESRCH);
        assert_eq!(
            diagnostic(wait.block(&mut guest, None).await),
            expected(Errno::ESRCH)
        );
        assert!(kernel.lock().unwrap().requested.is_empty());

        // `restore`'s read, with every signal blocked.
        wait.block(&mut guest, None).await.unwrap();
        kernel.lock().unwrap().read_failures.push_back(Errno::ESRCH);
        assert_eq!(
            diagnostic(wait.restore(&mut guest, None).await),
            expected(Errno::ESRCH)
        );
        let error = expected(Errno::EIO).to_string();
        assert!(
            error.starts_with("cannot read the signal state of guest thread"),
            "{error}"
        );
    }

    #[tokio::test]
    async fn a_thread_that_no_longer_exists_ends_its_wait_with_erestartnointr() {
        // SAFETY: gettid has no preconditions.
        let exited = std::thread::spawn(|| unsafe { libc::gettid() })
            .join()
            .unwrap();
        // `join` returns when the kernel clears the thread's TID word, early in
        // its exit; the thread stays in `/proc` and answers `tgkill` until the
        // kernel releases it a moment later. Measured on the development host,
        // 62 of 20000 joined threads were still listed right after `join`, and
        // a GitHub-hosted run read such a thread's status here
        // (https://github.com/rrnewton/hermit/actions/runs/37683205050). So wait
        // for the release. The path is in this process's own task directory, so
        // no other process's thread can appear under it, and Linux hands out
        // TIDs cyclically, so this one is not reissued here before the counter
        // wraps.
        let task = format!("/proc/self/task/{exited}");
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
        while std::path::Path::new(&task).exists() {
            assert!(
                std::time::Instant::now() < deadline,
                "thread {exited} is still in /proc 10 s after it was joined"
            );
            std::thread::sleep(std::time::Duration::from_millis(1));
        }
        let (guest, _kernel) = WaitGuest::new(Pid::from_raw(exited), guest_mask());
        // No script: the read goes to the real `/proc`, where the thread is gone.

        let wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        assert!(matches!(
            wait.interrupted(),
            Err(Error::Errno(Errno::ERESTARTNOINTR))
        ));
        assert!(matches!(
            read_wait_signal_state(guest.pid, guest.tid),
            Err(Error::Errno(Errno::ERESTARTNOINTR))
        ));
    }

    #[tokio::test]
    async fn a_stack_with_no_scratch_room_leaves_the_wait_under_the_guest_mask() {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        guest.commit_fails = true;
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);

        wait.block(&mut guest, None).await.unwrap();

        assert!(!wait.needs_block(), "block must not be tried again");
        assert_eq!(kernel.lock().unwrap().blocked, guest_mask());
        assert!(kernel.lock().unwrap().requested.is_empty());

        // Later turns classify against the guest's own mask, read each turn.
        {
            let mut kernel = kernel.lock().unwrap();
            kernel.caught = kernel_sigset_bit(libc::SIGUSR1) | kernel_sigset_bit(libc::SIGUSR2);
            kernel.pending = kernel_sigset_bit(libc::SIGUSR2);
        }
        assert!(!wait.interrupted().unwrap(), "SIGUSR2 is blocked");
        kernel.lock().unwrap().pending = kernel_sigset_bit(libc::SIGUSR1);
        assert!(wait.interrupted().unwrap(), "SIGUSR1 is not blocked");

        wait.restore(&mut guest, None).await.unwrap();
        assert!(
            kernel.lock().unwrap().requested.is_empty(),
            "nothing to put back"
        );
        assert_eq!(kernel.lock().unwrap().blocked, guest_mask());
    }

    /// A guest whose injections a pending signal outside the mask stops, as on
    /// ptrace, with `pending` queued and `ignored` set to `SIG_IGN`.
    fn stopping_guest(pending: KernelSigset, ignored: KernelSigset) -> (WaitGuest, Kernel) {
        let (guest, kernel) = WaitGuest::live(guest_mask());
        {
            let mut kernel = kernel.lock().unwrap();
            kernel.stops_for_pending = true;
            kernel.pending = pending;
            kernel.ignored = ignored;
        }
        (guest, kernel)
    }

    /// Inject a mask change to `mask` through `wait.inject_absorbing`.
    async fn inject_mask_absorbing(
        wait: &mut KernelSignalWait,
        guest: &mut WaitGuest,
        mask: KernelSigset,
    ) -> Result<Result<i64, Errno>, Error> {
        let mut stack = guest.stack().await;
        let cell = stack.push(mask);
        let _guard = stack.commit().unwrap();
        wait.inject_absorbing(guest, set_signal_mask_call(cell))
            .await
    }

    /// An ignored signal that was already pending stops the mask change of
    /// `block`, which used to end the wait with a restart (review finding F2 on
    /// https://github.com/rrnewton/hermit/pull/3361). Linux would not end the
    /// wait for it, so the stop is absorbed: the backend holds the signal, the
    /// change runs again, and the held signal does not interrupt the wait.
    #[tokio::test]
    async fn an_ignored_signal_that_stops_the_mask_change_does_not_end_the_wait() {
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        let (mut guest, kernel) = stopping_guest(usr1, usr1);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);

        wait.block(&mut guest, None).await.unwrap();

        assert!(!wait.needs_block());
        let all_blocked = blocked_signal_mask() | guest_mask();
        {
            let kernel = kernel.lock().unwrap();
            assert_eq!(kernel.taken, vec![libc::SIGUSR1]);
            assert_eq!(kernel.requested, vec![all_blocked, all_blocked]);
            assert_eq!(kernel.blocked, all_blocked);
        }
        assert_eq!(
            wait.held,
            Some(HeldSignal {
                signal: libc::SIGUSR1,
                queue: SignalQueue::Thread,
                kind: HeldKind::Harmless,
            })
        );
        assert!(
            !wait.interrupted().unwrap(),
            "an ignored signal does not end the wait"
        );
        wait.restore(&mut guest, None).await.unwrap();
        assert_eq!(kernel.lock().unwrap().blocked, guest_mask());
    }

    /// A default-ignored signal that stops an injection is held the same way,
    /// and the injection then returns its own result (review finding F2 on
    /// https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn a_default_ignored_signal_that_stops_an_injection_is_held() {
        let (mut guest, kernel) = stopping_guest(kernel_sigset_bit(libc::SIGWINCH), 0);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert!(matches!(result, Ok(Ok(0))), "{result:?}");
        assert_eq!(kernel.lock().unwrap().taken, vec![libc::SIGWINCH]);
        assert_eq!(kernel.lock().unwrap().requested.len(), 2);
        assert_eq!(wait.held.map(|held| held.kind), Some(HeldKind::Harmless));
        assert!(!wait.interrupted().unwrap());
    }

    /// A pending signal that would end the wait natively ends it before any
    /// injection, with the restart errno, and stays queued for the guest.
    #[tokio::test]
    async fn a_pending_caught_signal_ends_the_wait_before_the_injection() {
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        let (mut guest, kernel) = stopping_guest(usr1, 0);
        kernel.lock().unwrap().caught = usr1;
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTSYS))),
            "{result:?}"
        );
        let kernel = kernel.lock().unwrap();
        assert!(kernel.requested.is_empty(), "nothing is injected");
        assert_eq!(kernel.pending, usr1);
        assert_eq!(wait.held, None);
    }

    /// The backend holds one signal. Once it holds one whose loss would matter,
    /// a `SIGCHLD` here, an injection that a different pending signal would stop
    /// is not made. The wait used to end with `ERESTARTNOINTR`, a transparent
    /// restart; it now ends with a `HeldSignalRefusal`, which its caller turns
    /// into the unsupported-operation refusal (round-10 High 1 on
    /// https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn a_held_sigchld_is_never_replaced_by_another_stop() {
        let (mut guest, kernel) = stopping_guest(
            kernel_sigset_bit(libc::SIGCHLD) | kernel_sigset_bit(libc::SIGWINCH),
            0,
        );
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert_eq!(
            refusal(result),
            HeldSignalRefusal {
                wait: None,
                held: Some(libc::SIGCHLD),
                replacing: Some(libc::SIGWINCH),
                loss: HeldSignalLoss::Replaced,
            }
        );
        let kernel = kernel.lock().unwrap();
        assert_eq!(kernel.taken, vec![libc::SIGCHLD]);
        assert_eq!(
            kernel.requested.len(),
            1,
            "the second injection is not made"
        );
        assert_eq!(kernel.pending, kernel_sigset_bit(libc::SIGWINCH));
        assert_eq!(wait.held.map(|held| held.kind), Some(HeldKind::Precious));
    }

    /// A wait whose `block` was stopped by a default `SIGCHLD`, which the
    /// backend holds as a signal whose loss matters (`HeldKind::Precious`).
    async fn wait_holding_sigchld(guest: &mut WaitGuest, kernel: &Kernel) -> KernelSignalWait {
        let mut wait = KernelSignalWait::new(&*guest, 0, false, Errno::ERESTARTSYS);
        wait.block(guest, None).await.unwrap();
        assert_eq!(
            wait.held.map(|held| (held.signal, held.kind)),
            Some((libc::SIGCHLD, HeldKind::Precious))
        );
        let kernel = kernel.lock().unwrap();
        assert_eq!(kernel.blocked, blocked_signal_mask() | guest_mask());
        assert_eq!(kernel.held_slot, Some(libc::SIGCHLD));
        wait
    }

    /// A signal that `/proc` never showed, a `SIGSTOP` sent from outside here,
    /// stops the injection that puts the guest's mask back before it runs, and
    /// the next attempt runs. The backend holds one signal, so the `SIGSTOP`
    /// replaced the held `SIGCHLD`, and `restore` used to return success, so
    /// the guest resumed without its `SIGCHLD` (round-11 High F1 on
    /// https://github.com/rrnewton/hermit/pull/3361). The mask is still put
    /// back, and the wait now ends with a `HeldSignalRefusal`.
    #[tokio::test]
    async fn a_stop_of_the_mask_restoration_that_replaces_a_held_sigchld_is_refused() {
        let (mut guest, kernel) = stopping_guest(kernel_sigset_bit(libc::SIGCHLD), 0);
        let _proc = scripted_proc(&kernel);
        let mut wait = wait_holding_sigchld(&mut guest, &kernel).await;
        kernel
            .lock()
            .unwrap()
            .outcomes
            .push_back(MaskOutcome::StoppedBy(libc::SIGSTOP));

        let result = wait.restore(&mut guest, None).await;

        {
            let kernel = kernel.lock().unwrap();
            assert_eq!(kernel.blocked, guest_mask(), "the guest's mask is back");
            assert_eq!(
                kernel.requested[2..],
                [guest_mask(), guest_mask()],
                "one stopped restoration attempt and one that ran"
            );
            assert_eq!(kernel.held_slot, Some(libc::SIGSTOP));
            assert_eq!(kernel.lost, vec![libc::SIGCHLD]);
        }
        assert_eq!(
            refusal(result),
            HeldSignalRefusal {
                wait: None,
                held: Some(libc::SIGCHLD),
                replacing: None,
                loss: HeldSignalLoss::Restoration,
            }
        );
    }

    /// The same with a signal that `/proc` shows before the restoration and
    /// that the all-blocked mask leaves unblocked: signal 32, which the C
    /// library reserves and leaves out of the blockable set. The refusal names
    /// it.
    #[tokio::test]
    async fn an_identified_stop_of_the_mask_restoration_that_replaces_a_held_sigchld_is_refused() {
        const RESERVED: i32 = 32;
        assert_eq!(blocked_signal_mask() & kernel_sigset_bit(RESERVED), 0);
        let (mut guest, kernel) = stopping_guest(kernel_sigset_bit(libc::SIGCHLD), 0);
        let _proc = scripted_proc(&kernel);
        let mut wait = wait_holding_sigchld(&mut guest, &kernel).await;
        kernel.lock().unwrap().pending = kernel_sigset_bit(RESERVED);

        let result = wait.restore(&mut guest, None).await;

        {
            let kernel = kernel.lock().unwrap();
            assert_eq!(kernel.blocked, guest_mask(), "the guest's mask is back");
            assert_eq!(kernel.taken, vec![libc::SIGCHLD, RESERVED]);
            assert_eq!(kernel.held_slot, Some(RESERVED));
            assert_eq!(kernel.lost, vec![libc::SIGCHLD]);
        }
        let refused = refusal(result);
        assert_eq!(
            refused,
            HeldSignalRefusal {
                wait: None,
                held: Some(libc::SIGCHLD),
                replacing: Some(RESERVED),
                loss: HeldSignalLoss::Restoration,
            }
        );
        let message = refused.to_string();
        assert!(
            message.starts_with(
                "unsupported: the a blocking wait cannot keep the held signal SIGCHLD: signal 32 \
                 stopped the injection that puts the guest's signal mask back and replaced it"
            ),
            "{message}"
        );
    }

    /// A refusal from the mask restoration replaces the wait's result, whether
    /// that is a value or an errno, unless the wait already ended with a refusal
    /// of its own, which came first. A mask that could not be put back still
    /// ends the run.
    #[test]
    fn a_restoration_refusal_replaces_the_result_unless_the_wait_refused_first() {
        let tool = |refusal: HeldSignalRefusal| Error::Tool(anyhow::Error::new(refusal));
        let restoration = HeldSignalRefusal {
            wait: None,
            held: Some(libc::SIGCHLD),
            replacing: None,
            loss: HeldSignalLoss::Restoration,
        };
        let replaced = HeldSignalRefusal {
            wait: None,
            held: Some(libc::SIGCHLD),
            replacing: Some(libc::SIGUSR1),
            loss: HeldSignalLoss::Replaced,
        };
        let not_restored = BlockedWaitSignalError::MaskNotRestored {
            pid: Pid::from_raw(1),
            tid: Pid::from_raw(1),
            attempts: 16,
            last_error: Some(Errno::ERESTARTNOINTR),
        };

        assert_eq!(result_after_restore(Ok(()), Ok(3)).unwrap().unwrap(), 3);
        assert_eq!(
            refusal(result_after_restore(Err(tool(restoration)), Ok(3)).unwrap()),
            restoration
        );
        assert_eq!(
            refusal(
                result_after_restore(Err(tool(restoration)), Err(Errno::ERESTARTSYS.into()))
                    .unwrap()
            ),
            restoration
        );
        assert_eq!(
            refusal(result_after_restore(Err(tool(restoration)), Err(tool(replaced))).unwrap()),
            replaced
        );
        assert_eq!(
            diagnostic(result_after_restore(
                Err(Error::Tool(anyhow::Error::new(not_restored))),
                Ok(3)
            )),
            not_restored
        );
    }

    /// A guest whose next `stops` injections are stopped by a signal that
    /// `/proc` never shows, as one that arrives after the read before an
    /// injection and that the backend then holds: nothing leaves a queue, so the
    /// stop cannot be identified.
    fn unidentified_stops_guest(stops: usize) -> (WaitGuest, Kernel) {
        let (guest, kernel) = WaitGuest::live(guest_mask());
        kernel.lock().unwrap().outcomes =
            std::iter::repeat_n(MaskOutcome::StoppedBefore, stops).collect();
        (guest, kernel)
    }

    /// A stop that cannot be identified, such as a default-ignored `SIGCHLD`
    /// that arrives after the read, used to end the wait with `ERESTARTNOHAND`
    /// for a call whose restart code is not a kernel restart code. With no
    /// handler to run, the call then restarted, and a relative `poll` timeout
    /// or a timed polling `FUTEX_WAIT` started again with a fresh deadline
    /// (round-8 High 1 on https://github.com/rrnewton/hermit/pull/3361). The
    /// stop is now held and the injection runs again, so the wait goes on with
    /// its absolute deadline, as the blind retry before this machinery did.
    #[tokio::test]
    async fn a_stop_that_cannot_be_identified_keeps_a_timed_wait_and_its_deadline() {
        for deadline in [true, false] {
            let (mut guest, kernel) = unidentified_stops_guest(1);
            let _proc = scripted_proc(&kernel);
            let mut wait = KernelSignalWait::new(&guest, 0, true, Errno::ERESTARTNOHAND)
                .with_deadline(deadline);

            let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

            assert!(
                matches!(result, Ok(Ok(0))),
                "deadline={deadline}: {result:?}"
            );
            assert_eq!(
                kernel.lock().unwrap().requested.len(),
                2,
                "deadline={deadline}"
            );
            assert_eq!(wait.held, None, "deadline={deadline}");
        }
    }

    /// A wait with a deadline absorbs stops without the bound that ends any
    /// other wait after `MAX_ABSORBED_STOPS`, because a restart would start its
    /// timeout again (round-8 High 1 on
    /// https://github.com/rrnewton/hermit/pull/3361). A wait without one still
    /// ends at the bound, and, while the backend holds only a signal whose loss
    /// does not matter (an ignored `SIGUSR1` sent again and again here), with
    /// `ERESTARTNOINTR`: it is not refused.
    #[tokio::test]
    async fn only_a_wait_without_a_deadline_ends_after_the_absorbed_stop_bound() {
        let stops = 100;
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        let (mut guest, kernel) = stopping_guest(usr1, usr1);
        kernel.lock().unwrap().repost_after_take = stops - 1;
        let _proc = scripted_proc(&kernel);
        let mut wait =
            KernelSignalWait::new(&guest, 0, true, Errno::ERESTARTNOHAND).with_deadline(true);
        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;
        assert!(matches!(result, Ok(Ok(0))), "{result:?}");
        assert_eq!(kernel.lock().unwrap().requested.len(), stops + 1);
        assert_eq!(wait.held.map(|held| held.kind), Some(HeldKind::Harmless));

        let (mut guest, kernel) = stopping_guest(usr1, usr1);
        kernel.lock().unwrap().repost_after_take = stops - 1;
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, true, Errno::ERESTARTNOHAND);
        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;
        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTNOINTR))),
            "{result:?}"
        );
        assert_eq!(kernel.lock().unwrap().requested.len(), 65);
    }

    /// At the same bound, a wait without a deadline whose backend holds a
    /// signal whose loss matters (a default `SIGCHLD`, sent again and again)
    /// used to end with `ERESTARTNOINTR` too. It now ends with a
    /// `HeldSignalRefusal` (round-10 High 1 on
    /// https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn a_wait_that_reaches_the_stop_bound_holding_a_sigchld_is_refused() {
        let (mut guest, kernel) = stopping_guest(kernel_sigset_bit(libc::SIGCHLD), 0);
        kernel.lock().unwrap().repost_after_take = 100;
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert_eq!(
            refusal(result),
            HeldSignalRefusal {
                wait: None,
                held: Some(libc::SIGCHLD),
                replacing: None,
                loss: HeldSignalLoss::StopBound,
            }
        );
        assert_eq!(kernel.lock().unwrap().requested.len(), 65);
    }

    /// At the same bound, a stop that cannot be identified, after stops by a
    /// signal whose loss does not matter (an ignored `SIGUSR1` sent again and
    /// again), used to end a wait without a deadline with `ERESTARTNOINTR`,
    /// which let the backend deliver the unidentified signal, possibly a
    /// `SIGCHLD`, at a moment host timing could set. It now ends with a
    /// `HeldSignalRefusal`; a wait with a deadline, which has no bound, goes on
    /// (round-10 High 1 on https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn an_unidentified_stop_at_the_stop_bound_is_refused() {
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        for deadline in [false, true] {
            let (mut guest, kernel) = stopping_guest(usr1, usr1);
            {
                let mut kernel = kernel.lock().unwrap();
                // 64 identified stops, the bound, then one that `/proc` never
                // shows.
                kernel.repost_after_take = 63;
                kernel.outcomes = VecDeque::from([MaskOutcome::StoppedBefore]);
            }
            let _proc = scripted_proc(&kernel);
            let mut wait = KernelSignalWait::new(&guest, 0, true, Errno::ERESTARTNOHAND)
                .with_deadline(deadline);

            let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

            if deadline {
                assert!(matches!(result, Ok(Ok(0))), "{result:?}");
                assert_eq!(kernel.lock().unwrap().requested.len(), 66);
                assert!(wait.held_unidentified);
            } else {
                assert_eq!(
                    refusal(result),
                    HeldSignalRefusal {
                        wait: None,
                        held: None,
                        replacing: None,
                        loss: HeldSignalLoss::StopBound,
                    }
                );
                assert_eq!(kernel.lock().unwrap().requested.len(), 65);
            }
            assert_eq!(
                kernel.lock().unwrap().taken.len(),
                64,
                "deadline={deadline}"
            );
        }
    }

    /// A stop that cannot be identified while the backend holds a `SIGCHLD`
    /// replaced the `SIGCHLD` in the backend's one slot. The wait used to go on
    /// and lose it; it now ends with a `HeldSignalRefusal`, with or without a
    /// deadline (round-10 High 1 on https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn an_unidentified_stop_over_a_held_sigchld_is_refused() {
        for deadline in [true, false] {
            let (mut guest, kernel) = stopping_guest(kernel_sigset_bit(libc::SIGCHLD), 0);
            kernel.lock().unwrap().outcomes = VecDeque::from([MaskOutcome::StoppedBefore]);
            let _proc = scripted_proc(&kernel);
            let mut wait =
                KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS).with_deadline(deadline);

            let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

            assert_eq!(
                refusal(result),
                HeldSignalRefusal {
                    wait: None,
                    held: Some(libc::SIGCHLD),
                    replacing: None,
                    loss: HeldSignalLoss::UnidentifiedStop,
                },
                "deadline={deadline}"
            );
            let kernel = kernel.lock().unwrap();
            assert_eq!(kernel.requested.len(), 2, "deadline={deadline}");
            assert_eq!(kernel.taken, vec![libc::SIGCHLD], "deadline={deadline}");
        }
    }

    /// A second stop that cannot be identified replaced a first one, which may
    /// have been a signal whose loss matters. The wait used to go on; it now
    /// ends with a `HeldSignalRefusal`, with or without a deadline (round-10
    /// High 1 on https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn a_second_unidentified_stop_is_refused() {
        for deadline in [true, false] {
            let (mut guest, kernel) = unidentified_stops_guest(2);
            let _proc = scripted_proc(&kernel);
            let mut wait = KernelSignalWait::new(&guest, 0, true, Errno::ERESTARTNOHAND)
                .with_deadline(deadline);

            let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

            assert_eq!(
                refusal(result),
                HeldSignalRefusal {
                    wait: None,
                    held: None,
                    replacing: None,
                    loss: HeldSignalLoss::UnidentifiedStop,
                },
                "deadline={deadline}"
            );
            assert_eq!(
                kernel.lock().unwrap().requested.len(),
                2,
                "deadline={deadline}"
            );
        }
    }

    /// A `SIGCHLD` that arrives after the read before an injection stops it in a
    /// way that cannot be identified, and a second one, pending at the next
    /// read, would stop the injection made again and replace the first in the
    /// backend's one slot. The first may have been a signal whose loss matters,
    /// so the wait is refused before that injection, with or without a
    /// deadline (round-10 High 1 on https://github.com/rrnewton/hermit/pull/3361),
    /// and the backend still holds the first signal. This is the refusal a
    /// flood of external `SIGCHLD` racing a timed futex wait's first injections
    /// met on hermit main ccbe5ae8 (`external_signal_interrupt`'s test of the
    /// flood, now
    /// `ptrace_timed_futex_wait_and_poll_keep_their_deadline_after_a_sigchld_flood`,
    /// which sends its flood before the wait starts).
    #[tokio::test]
    async fn a_sigchld_after_an_unidentified_stop_is_refused_before_it_replaces_it() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        for deadline in [true, false] {
            let (mut guest, kernel) = stopping_guest(0, 0);
            {
                let mut kernel = kernel.lock().unwrap();
                // Pending only after the first read: the injection's stop is not
                // identified. Sent once more, so it is pending again at the read
                // before the injection made again.
                kernel.pending_repost = chld;
                kernel.repost_after_take = 1;
            }
            let _proc = scripted_proc(&kernel);
            let mut wait = KernelSignalWait::new(&guest, 0, true, Errno::ERESTARTNOHAND)
                .with_deadline(deadline);

            let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

            let refused = refusal(result);
            assert_eq!(
                refused,
                HeldSignalRefusal {
                    wait: None,
                    held: None,
                    replacing: Some(libc::SIGCHLD),
                    loss: HeldSignalLoss::Replaced,
                },
                "deadline={deadline}"
            );
            let message = HeldSignalRefusal {
                wait: Some(Sysno::futex),
                ..refused
            }
            .to_string();
            assert!(
                message.starts_with(
                    "unsupported: the futex wait cannot keep a held signal that could not be \
                     identified: the pending SIGCHLD would stop the next injection and replace it"
                ),
                "deadline={deadline}: {message}"
            );
            assert!(wait.held_unidentified, "deadline={deadline}");
            let kernel = kernel.lock().unwrap();
            assert_eq!(
                kernel.requested.len(),
                1,
                "deadline={deadline}: the second injection is not made"
            );
            assert_eq!(kernel.taken, vec![libc::SIGCHLD], "deadline={deadline}");
            assert_eq!(kernel.held_slot, Some(libc::SIGCHLD), "deadline={deadline}");
            assert!(kernel.lost.is_empty(), "deadline={deadline}");
            assert_eq!(kernel.pending, chld, "deadline={deadline}");
        }
    }

    /// In a wait with a deadline, a held `SIGCHLD` that would not end the wait
    /// is not dropped when another pending signal would stop the next
    /// injection. The injection used to be made, and the stop replaced the held
    /// signal, as the blind retry allowed (round-8 High 1 on
    /// https://github.com/rrnewton/hermit/pull/3361); a restart would start the
    /// timeout again. The wait now ends with a `HeldSignalRefusal` before the
    /// injection (round-10 High 1 on the same pull request).
    #[tokio::test]
    async fn a_timed_wait_refuses_to_let_another_stop_replace_a_held_sigchld() {
        let (mut guest, kernel) = stopping_guest(
            kernel_sigset_bit(libc::SIGCHLD) | kernel_sigset_bit(libc::SIGWINCH),
            0,
        );
        let _proc = scripted_proc(&kernel);
        let mut wait =
            KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND).with_deadline(true);

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert_eq!(
            refusal(result),
            HeldSignalRefusal {
                wait: None,
                held: Some(libc::SIGCHLD),
                replacing: Some(libc::SIGWINCH),
                loss: HeldSignalLoss::Replaced,
            }
        );
        let kernel = kernel.lock().unwrap();
        assert_eq!(kernel.taken, vec![libc::SIGCHLD]);
        assert_eq!(
            kernel.requested.len(),
            1,
            "the second injection is not made"
        );
        assert_eq!(kernel.pending, kernel_sigset_bit(libc::SIGWINCH));
    }

    /// In a wait with a deadline, a held signal that would end the wait natively
    /// ends it with the restart errno before another stop can replace it, as
    /// Linux ends the wait for that signal.
    #[tokio::test]
    async fn a_timed_wait_ends_for_a_held_signal_that_would_end_it() {
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        let (mut guest, kernel) = stopping_guest(usr1 | kernel_sigset_bit(libc::SIGWINCH), 0);
        let _proc = scripted_proc(&kernel);
        let mut wait =
            KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND).with_deadline(true);
        wait.held = Some(HeldSignal {
            signal: libc::SIGUSR1,
            queue: SignalQueue::Thread,
            kind: HeldKind::Precious,
        });
        kernel.lock().unwrap().pending = kernel_sigset_bit(libc::SIGWINCH);
        kernel.lock().unwrap().caught = usr1;

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTNOHAND))),
            "{result:?}"
        );
        assert!(
            kernel.lock().unwrap().requested.is_empty(),
            "nothing is injected"
        );
    }

    /// In a wait without a deadline, a held signal that would end the wait
    /// natively also ends it with the restart errno before another stop can
    /// replace it, as Linux ends the wait for that signal. It used to end with
    /// `ERESTARTNOINTR`, which restarted the call after the handler ran
    /// (round-10 High 1 on https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn a_wait_without_a_deadline_ends_for_a_held_signal_that_would_end_it() {
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        let (mut guest, kernel) = stopping_guest(kernel_sigset_bit(libc::SIGWINCH), 0);
        kernel.lock().unwrap().caught = usr1;
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);
        wait.held = Some(HeldSignal {
            signal: libc::SIGUSR1,
            queue: SignalQueue::Thread,
            kind: HeldKind::Precious,
        });

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTSYS))),
            "{result:?}"
        );
        assert!(
            kernel.lock().unwrap().requested.is_empty(),
            "nothing is injected"
        );
    }

    /// A signal that a wait without a deadline consumes and that stops an
    /// injection is delivered rather than returned. One whose loss does not
    /// matter still ends the wait with `ERESTARTNOINTR`, and is not refused;
    /// a `SIGCHLD`, which the wait holds until the call returns, used to end it
    /// the same way and now ends it with a `HeldSignalRefusal` (round-10 High 1
    /// on https://github.com/rrnewton/hermit/pull/3361).
    #[tokio::test]
    async fn a_consumed_sigchld_that_stops_an_injection_is_refused() {
        let usr1 = kernel_sigset_bit(libc::SIGUSR1);
        let (mut guest, kernel) = stopping_guest(usr1, 0);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, usr1, false, Errno::ERESTARTSYS);
        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;
        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTNOINTR))),
            "{result:?}"
        );
        assert_eq!(kernel.lock().unwrap().taken, vec![libc::SIGUSR1]);
        drop(_proc);

        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let (mut guest, kernel) = stopping_guest(chld, 0);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, chld, false, Errno::ERESTARTSYS);
        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;
        assert_eq!(
            refusal(result),
            HeldSignalRefusal {
                wait: None,
                held: None,
                replacing: Some(libc::SIGCHLD),
                loss: HeldSignalLoss::Consumed,
            }
        );
        assert_eq!(kernel.lock().unwrap().taken, vec![libc::SIGCHLD]);
    }

    /// A stop by a signal outside every class above, such as the backend's own
    /// preemption signal, still ends a wait without a deadline with
    /// `ERESTARTNOINTR`: the backend holds nothing whose loss matters, so it is
    /// not refused.
    #[tokio::test]
    async fn a_stop_by_the_preemption_signal_still_restarts_a_wait_without_a_deadline() {
        let signal = reverie::PERF_EVENT_SIGNAL as i32;
        let (mut guest, kernel) = stopping_guest(kernel_sigset_bit(signal), 0);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS);

        let result = inject_mask_absorbing(&mut wait, &mut guest, guest_mask()).await;

        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTNOINTR))),
            "{result:?}"
        );
        assert_eq!(kernel.lock().unwrap().taken, vec![signal]);
    }

    /// A wait that ends with a `HeldSignalRefusal` is refused through the
    /// unsupported-operation policy that `refuse_unserviceable_operation`
    /// applies: a run that is not fail-closed gets the `ERESTARTNOINTR` restart
    /// it got before, and a fail-closed one with `exit_on_unsupported_syscall`
    /// ends with the typed `UnsupportedSyscallError` for the wait's call. Every
    /// other result passes through, and the message names the wait, the
    /// signals and the pull request.
    #[tokio::test]
    async fn a_held_signal_refusal_follows_the_unsupported_operation_policy() {
        let refused = || {
            Err::<i64, Error>(Error::Tool(anyhow::Error::new(HeldSignalRefusal {
                wait: None,
                held: Some(libc::SIGCHLD),
                replacing: Some(libc::SIGWINCH),
                loss: HeldSignalLoss::Replaced,
            })))
        };
        let (mut guest, _kernel) = WaitGuest::live(guest_mask());
        guest.config.panic_on_unsupported_syscalls = false;
        let result = refuse_held_signal_loss(&mut guest, Sysno::poll, refused()).await;
        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTNOINTR))),
            "{result:?}"
        );
        let result = refuse_held_signal_loss(&mut guest, Sysno::poll, Ok(3)).await;
        assert!(matches!(result, Ok(3)), "{result:?}");
        let result =
            refuse_held_signal_loss(&mut guest, Sysno::poll, Err(Errno::ERESTARTSYS.into())).await;
        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTSYS))),
            "{result:?}"
        );

        guest.config.panic_on_unsupported_syscalls = true;
        guest.config.exit_on_unsupported_syscall = true;
        guest.config.shutdown_on_unsupported_syscall = false;
        let result = refuse_held_signal_loss(&mut guest, Sysno::poll, refused()).await;
        match result {
            Err(Error::Tool(error)) => assert!(
                matches!(
                    error.downcast_ref::<crate::UnsupportedSyscallError>(),
                    Some(crate::UnsupportedSyscallError(Sysno::poll))
                ),
                "{error:#}"
            ),
            other => panic!("expected the typed refusal, got {other:?}"),
        }

        let message = HeldSignalRefusal {
            wait: Some(Sysno::poll),
            held: Some(libc::SIGCHLD),
            replacing: Some(libc::SIGWINCH),
            loss: HeldSignalLoss::Replaced,
        }
        .to_string();
        for part in [
            "poll",
            "SIGCHLD",
            "SIGWINCH",
            "https://github.com/rrnewton/hermit/pull/3361",
        ] {
            assert!(message.contains(part), "{part} missing from: {message}");
        }
    }

    /// A guest with a caught `SIGCHLD` pending, outside its mask. With
    /// `gated_backend`, its configuration is the one under which the scheduler
    /// models signal targets: serialized threads and a backend that reports the
    /// kernel's signal state.
    fn caught_sigchld_guest(gated_backend: bool) -> (WaitGuest, Kernel) {
        let (mut guest, kernel) = WaitGuest::live(guest_mask());
        guest.config.sequentialize_threads = gated_backend;
        guest
            .config
            .backend
            .supports_blocked_wait_signal_interruption = gated_backend;
        {
            let mut kernel = kernel.lock().unwrap();
            kernel.pending = kernel_sigset_bit(libc::SIGCHLD);
            kernel.caught = kernel_sigset_bit(libc::SIGCHLD);
        }
        (guest, kernel)
    }

    /// A caught `SIGCHLD` pending outside the guest's mask, which the scheduler
    /// did not commit in this turn (`admit`), never ends a gated
    /// wait, on any backend and whoever sent it: `/proc` cannot tell a
    /// `SIGCHLD` a guest or the scheduler sent at a deterministic point from the
    /// one the kernel posts for a child event at a moment set by host timing
    /// (https://github.com/rrnewton/hermit/issues/3146). The signal stays queued
    /// and is delivered when the call returns. The waits of `select` and
    /// `pselect6` hold nothing and end for it, as Linux ends them for any caught
    /// signal (`core_sys_select`).
    #[tokio::test]
    async fn a_pending_caught_sigchld_ends_a_select_wait_but_no_gated_wait() {
        for gated_backend in [false, true] {
            let (guest, kernel) = caught_sigchld_guest(gated_backend);
            let _proc = scripted_proc(&kernel);

            let gated = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
            assert!(
                !gated.interrupted().unwrap(),
                "a pending caught SIGCHLD does not end a gated wait \
                 (gated_backend={gated_backend})"
            );

            let select = KernelSignalWait::for_select(&guest);
            assert!(
                select.interrupted().unwrap(),
                "the pending caught SIGCHLD ends a select wait (gated_backend={gated_backend})"
            );
            assert_eq!(
                kernel.lock().unwrap().pending,
                kernel_sigset_bit(libc::SIGCHLD),
                "the signal stays queued for the guest's handler"
            );
        }
    }

    /// A caught `SIGCHLD` the scheduler committed to the thread ends a gated
    /// wait, but only in the turn whose resume names it
    /// (`KernelSignalWait::admit`), and never when a host-timed
    /// source a guest armed can post `SIGCHLD` too: that copy could arrive at a
    /// moment host timing sets (https://github.com/rrnewton/hermit/issues/4005).
    #[tokio::test]
    async fn a_committed_sigchld_ends_a_gated_wait_unless_a_host_timed_source_can_post_it() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let (guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);

        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        assert!(
            !wait.interrupted().unwrap(),
            "an uncommitted SIGCHLD is held"
        );
        wait.admit(chld, 0);
        assert!(
            wait.interrupted().unwrap(),
            "a committed SIGCHLD ends the wait"
        );
        wait.admit(0, 0);
        assert!(
            !wait.interrupted().unwrap(),
            "the next turn's resume names nothing, so the SIGCHLD is held again"
        );

        let mut host_timed = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        host_timed.hold_until_return(chld);
        host_timed.admit(chld, 0);
        assert!(
            !host_timed.interrupted().unwrap(),
            "a SIGCHLD a host-timed source can post stays held although committed"
        );
    }

    /// A `SIGCHLD` a guest sent this thread, which the schedule recorded in its
    /// private queue and the turn admits (`KernelSignalWait::admit`), ends a
    /// gated wait from the thread's own queue only: one pending only on the
    /// shared queue, which a host-timed child event can fill, decides nothing.
    /// It is admitted for the turn alone, and never when a host-timed source
    /// can post `SIGCHLD` (R2b-2 stage 3,
    /// https://github.com/rrnewton/hermit/issues/4273).
    #[tokio::test]
    async fn an_admitted_private_sigchld_ends_a_gated_wait_from_the_threads_own_queue_only() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let (guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);

        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        wait.admit(0, chld);
        assert!(
            wait.interrupted().unwrap(),
            "the thread's own copy ends the wait"
        );
        assert!(
            wait.interrupts_only_by_committed(
                &read_wait_signal_state(guest.pid(), guest.tid()).unwrap()
            ),
            "only the admitted copy ends it, so the turn's probe runs first"
        );
        kernel.lock().unwrap().shared = chld;
        assert!(
            !wait.interrupted().unwrap(),
            "a SIGCHLD pending only on the shared queue is held"
        );
        kernel.lock().unwrap().shared = 0;
        wait.admit(0, 0);
        assert!(!wait.interrupted().unwrap(), "the next turn holds it again");

        let mut host_timed = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        host_timed.hold_until_return(chld);
        host_timed.admit(0, chld);
        assert!(
            !host_timed.interrupted().unwrap(),
            "a SIGCHLD a host-timed source can post stays held"
        );
    }

    /// An admitted private `SIGCHLD` that only the wait mask lets in is
    /// delivered under that mask (`interrupts_only_under_wait_mask`), as a
    /// committed one is: an `epoll_pwait` whose temporary mask unblocks a
    /// `SIGCHLD` the thread's own mask blocks (R2b-2 stage 3).
    #[tokio::test]
    async fn an_admitted_private_sigchld_that_only_the_wait_mask_lets_in_is_delivered_under_it() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let (guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);
        kernel.lock().unwrap().blocked |= chld;
        let mut wait =
            KernelSignalWait::new(&guest, 0, false, Errno::EINTR).with_wait_mask(Some(0));
        let state = read_wait_signal_state(guest.pid(), guest.tid()).unwrap();
        assert!(
            !wait.interrupts_only_under_wait_mask(&state),
            "held while not admitted"
        );
        wait.admit(0, chld);
        assert!(wait.interrupts_only_under_wait_mask(&state));
    }

    /// `admit_under_block` keeps a recorded private `SIGCHLD` only once every
    /// blockable signal is blocked in place, and only if the thread's own
    /// queue holds `SIGCHLD`, read after the block. A turn that admits nothing
    /// makes no request; a refused setter keeps the wait's fallback, the
    /// injected `block` after the first probe; a wait whose earlier injected
    /// `block` failed (`unblockable`, no `saved_mask`) keeps nothing (R2b-2
    /// stage 3, M3c of the rev5 review).
    #[tokio::test]
    async fn a_recorded_private_sigchld_is_kept_only_under_a_block_in_place() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        // The block publishes the guest's own mask, already published here,
        // so no request leaves the test (`publish_wait_mask`).
        let mut published = PublishedWaitMask(Some(guest_mask()));

        let (mut guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);
        kernel.lock().unwrap().setter.push_back(None);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        assert_eq!(
            wait.admit_under_block(&mut guest, 0, chld, 0, &mut published)
                .await
                .unwrap(),
            chld,
            "blocked in place, and the thread's own queue holds it"
        );
        assert!(wait.blocked_in_place);
        wait.restore(&mut guest, None).await.unwrap();
        drop(_proc);

        let (mut guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        assert_eq!(
            wait.admit_under_block(&mut guest, 0, 0, 0, &mut published)
                .await
                .unwrap(),
            0
        );
        assert!(
            kernel.lock().unwrap().set_calls.is_empty(),
            "nothing admitted, no request"
        );
        assert_eq!(
            wait.admit_under_block(&mut guest, 0, chld, chld, &mut published)
                .await
                .unwrap(),
            0,
            "a SIGCHLD a host-timed source can post is not admitted"
        );
        assert!(kernel.lock().unwrap().set_calls.is_empty());
        drop(_proc);

        let (mut guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);
        kernel.lock().unwrap().shared = chld;
        kernel.lock().unwrap().setter.push_back(None);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        assert_eq!(
            wait.admit_under_block(&mut guest, 0, chld, 0, &mut published)
                .await
                .unwrap(),
            0,
            "only on the shared queue: not the recorded copy"
        );
        wait.restore(&mut guest, None).await.unwrap();
        drop(_proc);

        let (mut guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        assert_eq!(
            wait.admit_under_block(&mut guest, 0, chld, 0, &mut published)
                .await
                .unwrap(),
            0,
            "the setter refuses (no answer queued)"
        );
        assert!(
            wait.needs_block(),
            "the injected block after the first probe stays"
        );
        drop(_proc);

        let (mut guest, kernel) = caught_sigchld_guest(true);
        let _proc = scripted_proc(&kernel);
        let mut wait = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        wait.unblockable = true;
        assert_eq!(
            wait.admit_under_block(&mut guest, 0, chld, 0, &mut published)
                .await
                .unwrap(),
            0,
            "an earlier injected block failed: no block, no private copy"
        );
        assert!(kernel.lock().unwrap().set_calls.is_empty());
    }

    /// A pipe turn (`turn_signals_with_block`) counts the recorded private
    /// `SIGCHLD` only where the gate holds, a block is in force and the
    /// thread's own queue holds it. Not on the scalar writer's first attempt,
    /// the guest's own write, which blocks nothing (`may_block` false): an
    /// injection of that turn could take the copy into the backend's single
    /// held slot. Not without the gate, as on a socket. Not from the shared
    /// queue alone (R2b-2 stage 3).
    #[tokio::test]
    async fn a_pipe_turn_counts_a_recorded_private_sigchld_only_under_a_block_in_place() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let turn = |may_block: bool, gate: bool, shared: bool| async move {
            // Thread 1, the thread state's own number, so a request may leave.
            let (mut guest, kernel) = WaitGuest::new(Pid::from_raw(1), guest_mask());
            {
                let mut kernel = kernel.lock().unwrap();
                kernel.pending = chld;
                kernel.caught = chld;
                kernel.shared = if shared { chld } else { 0 };
                kernel.setter.push_back(None);
                kernel.private_record = Some((0, chld));
            }
            let _proc = scripted_proc(&kernel);
            let mut wait = Some(KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTSYS));
            let mut published = PublishedWaitMask(Some(guest_mask()));
            let pending =
                turn_signals_with_block(&mut guest, &mut wait, may_block, 0, gate, &mut published)
                    .await
                    .unwrap()
                    .pending;
            if let Some(wait) = wait.as_mut() {
                wait.restore(&mut guest, None).await.unwrap();
            }
            let blocked_in_place = !kernel.lock().unwrap().set_calls.is_empty();
            (pending & chld, blocked_in_place)
        };
        assert_eq!(
            turn(true, true, false).await,
            (chld, true),
            "blocked in place: admitted"
        );
        assert_eq!(
            turn(false, true, false).await,
            (0, false),
            "the writer's first attempt blocks nothing: held"
        );
        assert_eq!(turn(true, false, false).await, (0, true), "no gate: held");
        assert_eq!(
            turn(true, true, true).await,
            (0, true),
            "the shared queue alone: held"
        );
    }

    /// The pipe path's hold leaves out an admitted private `SIGCHLD`
    /// (`turn_signals_before_discard`'s `private`): it counts in the turn's
    /// `pending`, where without it the hold keeps it out (R2b-2 stage 3).
    #[test]
    fn a_pipe_turn_counts_an_admitted_private_sigchld() {
        let chld = kernel_sigset_bit(libc::SIGCHLD);
        let state = KernelSignalState {
            pending: chld,
            thread_pending: chld,
            caught: chld,
            ..KernelSignalState::default()
        };
        assert_eq!(
            turn_signals_before_discard(&state, 0, 0, 0, NamedRealTime::Woken, 0).pending,
            0,
            "held"
        );
        assert_eq!(
            turn_signals_before_discard(&state, 0, 0, 0, NamedRealTime::Woken, chld).pending,
            chld,
            "admitted"
        );
    }

    /// The same `SIGCHLD` ends a select wait before an injection that is not a
    /// probe (`inject_absorbing`: the change of mask that blocks signals for the
    /// wait), with `ERESTARTNOHAND`, which the handler's run turns into `EINTR`.
    /// The waits of `select` and `pselect6` make every probe with
    /// `inject_first_probe`. A gated wait makes the injection, and the `SIGCHLD`
    /// that stops it is held until the call returns.
    #[tokio::test]
    async fn a_pending_caught_sigchld_ends_a_select_wait_before_the_injection() {
        let (mut guest, kernel) = caught_sigchld_guest(true);
        kernel.lock().unwrap().stops_for_pending = true;
        let _proc = scripted_proc(&kernel);

        let mut select = KernelSignalWait::for_select(&guest);
        let result = inject_mask_absorbing(&mut select, &mut guest, guest_mask()).await;
        assert!(
            matches!(result, Err(Error::Errno(Errno::ERESTARTNOHAND))),
            "{result:?}"
        );
        {
            let kernel = kernel.lock().unwrap();
            assert!(kernel.requested.is_empty(), "nothing is injected");
            assert_eq!(kernel.pending, kernel_sigset_bit(libc::SIGCHLD));
        }
        assert_eq!(select.held, None);

        let mut gated = KernelSignalWait::new(&guest, 0, false, Errno::ERESTARTNOHAND);
        let result = inject_mask_absorbing(&mut gated, &mut guest, guest_mask()).await;
        assert!(matches!(result, Ok(Ok(0))), "{result:?}");
        {
            let kernel = kernel.lock().unwrap();
            assert_eq!(kernel.taken, vec![libc::SIGCHLD]);
            assert_eq!(kernel.requested.len(), 2, "stopped once, then ran");
        }
        assert_eq!(gated.held.map(|held| held.kind), Some(HeldKind::Precious));
    }

    /// The kernel's next dequeue: the private queue before the shared one, a
    /// synchronous signal first, then the lowest number, outside the mask.
    #[test]
    fn next_dequeued_follows_the_kernel_order() {
        let bit = kernel_sigset_bit;
        let state = KernelSignalState {
            thread_pending: bit(libc::SIGUSR2) | bit(libc::SIGSEGV),
            shared_pending: bit(libc::SIGHUP),
            ..KernelSignalState::default()
        };
        assert_eq!(
            state.next_dequeued(0),
            Some((libc::SIGSEGV, SignalQueue::Thread))
        );
        assert_eq!(
            state.next_dequeued(bit(libc::SIGSEGV)),
            Some((libc::SIGUSR2, SignalQueue::Thread))
        );
        assert_eq!(
            state.next_dequeued(bit(libc::SIGSEGV) | bit(libc::SIGUSR2)),
            Some((libc::SIGHUP, SignalQueue::Shared))
        );
        assert_eq!(state.next_dequeued(!0), None);
    }

    /// A stop is identified only when exactly the predicted signal left its
    /// queue and the mask and dispositions are unchanged; arrivals do not matter.
    #[test]
    fn a_stop_is_identified_only_by_exactly_its_signal() {
        let bit = kernel_sigset_bit;
        let before = KernelSignalState {
            thread_pending: bit(libc::SIGUSR1) | bit(libc::SIGWINCH),
            shared_pending: bit(libc::SIGHUP),
            ..KernelSignalState::default()
        };
        let took = |after: KernelSignalState, signal, queue| {
            stop_took_only(&before, &after, signal, queue)
        };
        let after = KernelSignalState {
            thread_pending: bit(libc::SIGWINCH) | bit(libc::SIGUSR2),
            ..before
        };
        assert!(took(after, libc::SIGUSR1, SignalQueue::Thread));
        assert!(
            !took(after, libc::SIGWINCH, SignalQueue::Thread),
            "SIGWINCH is still queued"
        );
        let two_left = KernelSignalState {
            thread_pending: 0,
            ..before
        };
        assert!(!took(two_left, libc::SIGUSR1, SignalQueue::Thread));
        let shared_left = KernelSignalState {
            thread_pending: bit(libc::SIGWINCH),
            shared_pending: 0,
            ..before
        };
        assert!(!took(shared_left, libc::SIGUSR1, SignalQueue::Thread));
        let mask_changed = KernelSignalState {
            blocked: bit(libc::SIGUSR2),
            ..after
        };
        assert!(!took(mask_changed, libc::SIGUSR1, SignalQueue::Thread));
        let disposition_changed = KernelSignalState {
            ignored: bit(libc::SIGWINCH),
            ..after
        };
        assert!(!took(
            disposition_changed,
            libc::SIGUSR1,
            SignalQueue::Thread
        ));
    }

    /// A select wait with a pselect6 temporary mask (`with_wait_mask`) holds a
    /// caught signal that the temporary mask blocks, and the guest's own mask
    /// does not, when it stops an injection before `block` takes effect, as
    /// Linux keeps it pending for the whole call; without the temporary mask
    /// the same stop ends the wait, and a signal only the temporary mask
    /// unblocks ends it too (review of
    /// https://github.com/rrnewton/hermit/pull/4051).
    #[test]
    fn a_signal_the_temporary_mask_blocks_is_held_not_ended() {
        let bit = kernel_sigset_bit;
        let (guest, _kernel) = WaitGuest::live(guest_mask());
        let before = KernelSignalState {
            pending: bit(libc::SIGALRM),
            thread_pending: bit(libc::SIGALRM),
            blocked: guest_mask(),
            caught: bit(libc::SIGALRM) | bit(libc::SIGUSR2),
            ..KernelSignalState::default()
        };
        let plain = KernelSignalWait::for_select(&guest);
        assert_eq!(
            plain.stop_verdict(&before, libc::SIGALRM),
            StopVerdict::End(Errno::ERESTARTNOHAND)
        );
        let masked = KernelSignalWait::for_select(&guest).with_wait_mask(Some(bit(libc::SIGALRM)));
        assert_eq!(
            masked.stop_verdict(&before, libc::SIGALRM),
            StopVerdict::Absorb(HeldKind::Precious)
        );
        let unblocking = KernelSignalWait::for_select(&guest).with_wait_mask(Some(0));
        assert_eq!(
            unblocking.stop_verdict(&before, libc::SIGUSR2),
            StopVerdict::End(Errno::ERESTARTNOHAND)
        );
    }
}

#[cfg(test)]
mod turn_signal_tests {
    use super::*;

    fn bit(signal: libc::c_int) -> KernelSigset {
        kernel_sigset_bit(signal)
    }

    fn state(
        pending: KernelSigset,
        caught: KernelSigset,
        blocked: KernelSigset,
    ) -> KernelSignalState {
        KernelSignalState {
            pending,
            thread_pending: pending,
            caught,
            blocked,
            ..Default::default()
        }
    }

    /// Rule 3 of R2b-1 (https://github.com/rrnewton/hermit/issues/4273): with
    /// no progress only a pending copy ends the call; after progress a named
    /// wake does too.
    #[test]
    fn a_named_wake_ends_a_call_only_after_progress() {
        let named = TurnSignals {
            named: bit(libc::SIGUSR1),
            pending: 0,
        };
        assert!(!named.ends(false));
        assert!(named.ends(true));
        let pending = TurnSignals {
            named: 0,
            pending: bit(libc::SIGUSR1),
        };
        assert!(pending.ends(false));
        assert!(pending.ends(true));
        assert!(!TurnSignals::default().ends(true));
    }

    /// A caught SIGCHLD pending at a poll's turn is held unless the scheduler
    /// committed it, and a host-timed signal is held even then (arm A8).
    #[test]
    fn the_hold_leaves_out_an_uncommitted_sigchld_and_every_host_timed_signal() {
        let chld = bit(libc::SIGCHLD);
        let hup = bit(libc::SIGHUP);
        let s = state(chld | hup, chld | hup, 0);
        assert_eq!(
            turn_signals_before_discard(&s, 0, hup, 0, NamedRealTime::Woken, 0).pending,
            0
        );
        assert_eq!(
            turn_signals_before_discard(&s, 0, hup, chld, NamedRealTime::Woken, 0).pending,
            chld
        );
        assert_eq!(
            turn_signals_before_discard(&s, 0, hup, chld | hup, NamedRealTime::Woken, 0).pending,
            chld
        );
    }

    /// A named signal counts only if the call's mask leaves it unblocked
    /// (rule 2). A real-time one counts only if its disposition delivers it
    /// for `writev`, which the drain never filters (rule 2), and as woken for
    /// the generic poll and the scalar writer, whose drain already tested it
    /// (rule 1), so a flush after the wake does not undo it (the Claude
    /// implementation review of R2b-1, F1).
    #[test]
    fn a_named_signal_counts_only_unblocked_and_a_real_time_one_by_its_call() {
        use NamedRealTime::ByDisposition;
        use NamedRealTime::Woken;
        let usr1 = bit(libc::SIGUSR1);
        let rt = bit(libc::SIGRTMIN());
        let s = state(0, usr1, usr1);
        for real_time in [Woken, ByDisposition] {
            assert_eq!(
                turn_signals_before_discard(&s, usr1, 0, usr1, real_time, 0).named,
                0
            );
            assert_eq!(
                turn_signals_before_discard(&s, 0, 0, usr1, real_time, 0).named,
                usr1
            );
        }
        let ignored_rt = KernelSignalState {
            ignored: rt,
            ..state(0, 0, 0)
        };
        assert_eq!(
            turn_signals_before_discard(&ignored_rt, 0, 0, rt, ByDisposition, 0).named,
            0
        );
        assert_eq!(
            turn_signals_before_discard(&ignored_rt, 0, 0, rt, Woken, 0).named,
            rt
        );
        assert_eq!(
            turn_signals_before_discard(&ignored_rt, rt, 0, rt, Woken, 0).named,
            0
        );
        let caught_rt = state(0, rt, 0);
        assert_eq!(
            turn_signals_before_discard(&caught_rt, 0, 0, rt, ByDisposition, 0).named,
            rt
        );
    }
}

/// The mask a thread is stopped under where its blocking call returned, until
/// its continuation turn (`continuation_mask`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ContinuationMask {
    /// The call's temporary mask: the call ran and a signal ended it.
    Temporary,
    /// Not known to the scheduler, in record and replay alike.
    Unknown,
    /// The thread's own mask, which the kernel restored before the call
    /// returned and holds in record and replay alike.
    Restored,
}

/// The mask `call`, having returned `result`, leaves its thread stopped under,
/// decided from what the kernel did, which the raw result tells and replay
/// serves as recorded:
/// - pselect6 and ppoll that a signal ended return `-ERESTARTNOHAND`, or the
///   `-EINTR` that `poll_select_finish` can still turn it into after keeping
///   the mask (a timeout write-back that faults, or the STICKY_TIMEOUTS
///   personality); epoll_pwait and epoll_pwait2 return `-EINTR`. Linux then
///   delivers the signal under the temporary mask and `rt_sigreturn` restores
///   the saved one (`restore_saved_sigmask_unless`, fs/select.c, fs/eventpoll.c
///   and kernel/signal.c at v6.9): `Temporary`.
/// - Their normal return: Linux restored the saved mask before returning. Their
///   `-ERESTARTSYS`, which the host reports when the signal reached the thread
///   before the call ran: the kernel never installed the mask, and the
///   recording holds that result. Either way `Restored`.
/// - rt_sigsuspend: `Unknown`, whatever its result. Whether it ran is not
///   recorded: record injects it live, where a signal arriving first yields a
///   host-timed `-ERESTARTSYS` (the kernel never installed the mask), and
///   replay re-injects it with retry, which never shows that. A rule on its
///   result would differ between the two (the Claude review of d1b9404c), and
///   a known temporary mask would claim one the kernel may not have installed
///   (the Codex review of da662bb2). The scheduler treats an unknown mask
///   alike in both: no `SIGCHLD` taker or signal target, no discard.
/// - Any other call: `Restored`; it installed no temporary mask.
pub(crate) fn continuation_mask(call: Syscall, result: &Result<i64, Error>) -> ContinuationMask {
    let ended_by = |errno: Errno| matches!(result, Err(Error::Errno(error)) if *error == errno);
    let kept = match call {
        Syscall::RtSigsuspend(_) => return ContinuationMask::Unknown,
        Syscall::Pselect6(_) | Syscall::Ppoll(_) => {
            ended_by(Errno::ERESTARTNOHAND) || ended_by(Errno::EINTR)
        }
        Syscall::EpollPwait(_) | Syscall::Other(Sysno::epoll_pwait2, _) => ended_by(Errno::EINTR),
        _ => false,
    };
    if kept {
        ContinuationMask::Temporary
    } else {
        ContinuationMask::Restored
    }
}

#[cfg(test)]
mod temporary_mask_tests {
    use super::*;

    /// The continuation mask follows what the kernel did: the temporary mask
    /// for an interrupted pselect6 or ppoll (`-ERESTARTNOHAND`, or the `-EINTR`
    /// it can become after the kernel kept the mask) and an interrupted
    /// epoll_pwait or epoll_pwait2 (`-EINTR`); the restored own mask for their
    /// normal return, a call the signal reached before it ran (`-ERESTARTSYS`)
    /// or another errno; unknown for every rt_sigsuspend, whose `-ERESTARTSYS`
    /// is host-timed in record and absent in replay; restored for a call
    /// without a temporary mask.
    #[test]
    fn the_continuation_mask_follows_what_the_kernel_did() {
        let restart = Err(Error::Errno(Errno::ERESTARTNOHAND));
        let eintr = Err(Error::Errno(Errno::EINTR));
        let pselect = Syscall::Pselect6(syscalls::Pselect6::new());
        let ppoll = Syscall::Ppoll(syscalls::Ppoll::new());
        let epoll = Syscall::EpollPwait(syscalls::EpollPwait::new());
        let epoll2 = Syscall::Other(
            Sysno::epoll_pwait2,
            reverie::syscalls::SyscallArgs::new(0, 0, 0, 0, 0, 0),
        );
        let before_entry = Err(Error::Errno(Errno::ERESTARTSYS));
        let sigsuspend = Syscall::RtSigsuspend(syscalls::RtSigsuspend::new());
        let read = Syscall::Read(syscalls::Read::new());
        let ebadf = Err(Error::Errno(Errno::EBADF));
        use ContinuationMask::*;
        for call in [pselect, ppoll] {
            assert_eq!(continuation_mask(call, &restart), Temporary, "{call:?}");
            assert_eq!(continuation_mask(call, &eintr), Temporary, "{call:?}");
            assert_eq!(continuation_mask(call, &before_entry), Restored, "{call:?}");
            assert_eq!(continuation_mask(call, &ebadf), Restored, "{call:?}");
            assert_eq!(continuation_mask(call, &Ok(1)), Restored, "{call:?}");
        }
        for result in [&restart, &eintr, &before_entry, &Ok(0)] {
            assert_eq!(continuation_mask(sigsuspend, result), Unknown, "{result:?}");
        }
        for call in [epoll, epoll2] {
            assert_eq!(continuation_mask(call, &eintr), Temporary, "{call:?}");
            assert_eq!(continuation_mask(call, &restart), Restored, "{call:?}");
            assert_eq!(continuation_mask(call, &before_entry), Restored, "{call:?}");
            assert_eq!(continuation_mask(call, &Ok(0)), Restored, "{call:?}");
        }
        assert_eq!(continuation_mask(read, &restart), Restored);
    }
}