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
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

//! System calls dealing with IO and networking.
//!
//! Of course this overlaps somewhat with "files.rs".

use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::os::unix::io::RawFd;
use std::time::Duration;

use nix::fcntl::OFlag;
use reverie::Errno;
use reverie::Error;
use reverie::Guest;
use reverie::Stack;
use reverie::syscalls;
use reverie::syscalls::Addr;
use reverie::syscalls::AddrMut;
use reverie::syscalls::Displayable;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::Syscall;
use reverie::syscalls::SyscallArgs;
use reverie::syscalls::SyscallInfo;
use reverie::syscalls::Sysno;
use reverie::syscalls::Timespec;
use tracing::debug;
use tracing::trace;

use crate::config::SchedHeuristic;
use crate::fd::FdType;
use crate::record_or_replay::RecordOrReplay;
use crate::resources::LOOPBACK_POLL_YIELD_FYI;
use crate::resources::Permission;
use crate::resources::ResourceID;
use crate::resources::Resources;
use crate::scheduler::runqueue::FIRST_PRIORITY;
use crate::syscalls::helpers::KernelSignalWait;
use crate::syscalls::helpers::NonblockableSyscall;
use crate::syscalls::helpers::PublishedWaitMask;
use crate::syscalls::helpers::REAL_TIME_SIGNALS;
use crate::syscalls::helpers::RestartCall;
use crate::syscalls::helpers::act_on_pending_real_time;
use crate::syscalls::helpers::is_accepted_connection;
use crate::syscalls::helpers::keep_restart_block;
use crate::syscalls::helpers::millis_duration_to_absolute_timeout;
use crate::syscalls::helpers::observe_under_mask;
use crate::syscalls::helpers::pending_real_time_among;
use crate::syscalls::helpers::poll_deadline_for;
use crate::syscalls::helpers::publish_wait_mask;
use crate::syscalls::helpers::record_retry_event;
use crate::syscalls::helpers::refuse_held_signal_loss;
use crate::syscalls::helpers::result_after_restore;
use crate::syscalls::helpers::retry_blocking_wait_under_mask;
use crate::syscalls::helpers::retry_nonblocking_syscall_with_timeout;
use crate::syscalls::helpers::unblocked_by;
use crate::syscalls::network_trace::received_descriptors;
use crate::syscalls::signal::STACK_RED_ZONE;
use crate::syscalls::signal::kernel_installed_signal_mask;
use crate::syscalls::signal::read_kernel_sigset;
use crate::syscalls::threads::KernelSigset;
use crate::syscalls::threads::read_thread_signal_state;
use crate::syscalls::threads::read_thread_wait_signal_state;
use crate::tool_global::*;
use crate::tool_local::Detcore;
use crate::types::DetTid;
use crate::types::LogicalTime;

// Printing helper
// TODO: this should be subsumed by better syscall printing.
fn print_poll(call: &syscalls::Poll) {
    let len = call.nfds();
    debug!("POLL: on {} fds, timeout {}", len, call.timeout());
    // TODO: nicer API for reading arrays from the guest:
    unsafe {
        for i in 0..len {
            debug!(
                "POLL: fd {} = {}",
                i,
                call.fds().unwrap().offset(i as isize)
            );
        }
    }
}

/// Build the scheduler request for a zero-timeout poll.
///
/// An empty request only rotates the caller within its current priority band. That is not a
/// yield when a busy poller has a higher priority than the producer whose readiness it is
/// probing. `SchedYield` excludes the caller from the next selection, allowing exactly one
/// other runnable guest to make progress before the nonblocking poll executes. Limit that strong
/// yield to SaBRe tasks with a loopback peer: libcurl alternates its loopback socket with an
/// internal wakeup fd, and either zero-timeout probe can otherwise starve the peer. General
/// build-tool polling retains the existing empty-turn behavior.
fn zero_timeout_poll_request(dettid: DetTid, yield_to_peer: bool) -> Resources {
    let mut request = Resources::new(dettid);
    if yield_to_peer {
        request.insert(ResourceID::SchedYield, Permission::W);
        request.fyi(LOOPBACK_POLL_YIELD_FYI);
    }
    request
}

fn connect_result_allows_peer_classification(result: &Result<i64, Error>) -> bool {
    match result {
        Ok(_) => true,
        Err(Error::Errno(errno)) => *errno == Errno::EINPROGRESS,
        Err(_) => false,
    }
}

const KERNEL_SIGSET_SIZE: usize = std::mem::size_of::<u64>();
pub(super) const PSELECT6_INTERNAL_MAX_NFDS: i32 =
    (std::mem::size_of::<libc::c_ulong>() * 8) as i32;

#[derive(Clone, Copy)]
#[repr(C)]
pub(super) struct Pselect6SigmaskArg {
    pub(super) sigmask: usize,
    pub(super) sigsetsize: usize,
}

/// Where a call's private cells (`PrivateCells`) live while the kernel reads
/// them (`stage_private_cells`).
enum StagedCellsStorage<Guard> {
    /// The guest stack scratch below the red zone, checked out by the guard.
    Stack(Guard),
    /// Part of the guest's red zone, whose first `len` bytes `saved` holds
    /// until `StagedCells::release` writes them back.
    RedZone {
        at: usize,
        len: usize,
        saved: [u8; MAX_PRIVATE_CELLS_LEN],
    },
}

/// What a call hands the kernel in place of the guest's own memory: a
/// temporary signal mask, which a sibling could rewrite while the call waits
/// and which Hermit sanitizes, and the cell that goes with it. The mask comes
/// first.
///
/// Every layout with a mask takes 24 bytes, the size of replay's signal
/// delivery for an interrupted call (`end_replayed_pselect6_under_its_temporary_mask`),
/// so replay can deliver wherever recording found room for the call (Claude
/// re-review of https://github.com/rrnewton/hermit/pull/4087).
#[derive(Clone, Copy)]
enum PrivateCells {
    /// A pselect6 temporary mask and the `{ sigmask, sigsetsize }` wrapper
    /// that names it.
    Pselect6Mask(KernelSigset),
    /// A temporary mask for a call that takes the mask without a wrapper
    /// (`epoll_pwait`, a ppoll the kernel waits in), and 16 unused bytes.
    Mask(KernelSigset),
    /// A temporary mask and a zero timeout (a zero-timeout ppoll probe, and
    /// the replayed signal delivery).
    MaskAndZeroTimeout(KernelSigset),
    /// A zero timeout alone (a zero-timeout ppoll probe with no mask).
    ZeroTimeout,
}

const MASK_LEN: usize = std::mem::size_of::<KernelSigset>();

/// The most any `PrivateCells` takes: a mask and a 16-byte cell.
const MAX_PRIVATE_CELLS_LEN: usize = MASK_LEN + std::mem::size_of::<Timespec>();

// The wrapper and the timeout share the cell after the mask.
const _: () = assert!(std::mem::size_of::<Pselect6SigmaskArg>() == std::mem::size_of::<Timespec>());

impl PrivateCells {
    fn len(self) -> usize {
        match self {
            Self::Pselect6Mask(_) | Self::Mask(_) | Self::MaskAndZeroTimeout(_) => {
                MAX_PRIVATE_CELLS_LEN
            }
            Self::ZeroTimeout => std::mem::size_of::<Timespec>(),
        }
    }

    /// The cells' bytes when they start at `at`. Only the first `len` count,
    /// and a zero timeout is all zeroes.
    fn bytes(self, at: usize) -> [u8; MAX_PRIVATE_CELLS_LEN] {
        let mut cells = [0u8; MAX_PRIVATE_CELLS_LEN];
        match self {
            Self::Pselect6Mask(mask) => {
                cells[..MASK_LEN].copy_from_slice(&mask.to_ne_bytes());
                cells[MASK_LEN..MASK_LEN + 8].copy_from_slice(&at.to_ne_bytes());
                cells[MASK_LEN + 8..].copy_from_slice(&KERNEL_SIGSET_SIZE.to_ne_bytes());
            }
            Self::Mask(mask) | Self::MaskAndZeroTimeout(mask) => {
                cells[..MASK_LEN].copy_from_slice(&mask.to_ne_bytes());
            }
            Self::ZeroTimeout => {}
        }
        cells
    }
}

/// A call's private cells in guest memory (`stage_private_cells`).
struct StagedCells<Guard> {
    /// The first cell: the mask, or the zero timeout of
    /// `PrivateCells::ZeroTimeout`.
    at: usize,
    storage: StagedCellsStorage<Guard>,
    /// Memory for making an expired timerfd readable while these cells hold
    /// the scratch stack (`crate::syscalls::timerfd::publish_expired_in`);
    /// `None` for red-zone cells, which hold no stack.
    timerfd_scratch: Option<crate::syscalls::timerfd::Scratch>,
}

impl<Guard> StagedCells<Guard> {
    /// The mask copy.
    fn mask(&self) -> usize {
        self.at
    }

    /// The cell after the mask: a pselect6 wrapper, or a zero timeout.
    fn after_mask(&self) -> usize {
        self.at + MASK_LEN
    }

    /// Gives the storage back once the call has returned: drops the stack
    /// guard, or writes the guest's red-zone bytes back. That is a write to
    /// guest memory, not an injected syscall, so a call that ended with a
    /// restart code goes back to the kernel untouched.
    fn release<G, T>(self, guest: &mut G)
    where
        G: Guest<Detcore<T>>,
        T: RecordOrReplay,
    {
        if let StagedCellsStorage::RedZone { at, len, saved } = self.storage
            && let Err(errno) = AddrMut::<u8>::from_raw(at)
                .ok_or(Errno::EFAULT)
                .and_then(|bytes| guest.memory().write_exact(bytes, &saved[..len]))
        {
            // Not expected: the same bytes were written before the call.
            tracing::warn!("[detcore] could not restore the red zone after a syscall: {errno}");
        }
    }
}

/// Stages `cells` where the kernel can read them in place of the guest's own
/// memory.
///
/// The cells go in the guest stack scratch below the red zone when that is
/// writable. A thread may run with no writable memory there, on a stack it
/// placed near the start of a page, and a call on such a stack is valid
/// (Codex re-check of https://github.com/rrnewton/hermit/pull/4053). Then the
/// cells go in the red zone, whose bytes are saved here and written back by
/// `StagedCells::release` when the call returns. The red zone may hold the
/// call's own buffers, which Linux reads and writes, so the cells take the
/// highest 8-aligned window that overlaps none of `live` (Codex re-check #3).
/// The guest's own copies of the cells (its mask, wrapper or timeout) may be
/// overwritten: Linux reads none of them, as the call is redirected to the
/// private cells, and all are restored. When no window is free and writable,
/// this returns `None`, and the caller refuses the call
/// (`refuse_unstageable_pselect6_mask`). The guest runs no code in between:
/// the call holds the thread in the kernel, a signal frame is built below the
/// red zone, and a call the kernel restarts comes back as a new syscall with
/// the guest's own arguments, which is staged again. Nothing is mapped, so the
/// guest's address space is unchanged (Codex re-check of the earlier
/// per-thread page).
///
/// Whether the scratch faults is a function of the guest's own stack, so the
/// choice is deterministic.
async fn stage_private_cells<G, T>(
    guest: &mut G,
    cells: PrivateCells,
    live: &[std::ops::Range<usize>],
) -> Result<Option<StagedCells<<G::Stack as Stack>::StackGuard>>, Errno>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let len = cells.len();
    let mut stack = guest.stack().await;
    let at = match cells {
        PrivateCells::ZeroTimeout => stack.reserve::<Timespec>().as_raw(),
        _ => stack.reserve::<[u64; 3]>().as_raw(),
    };
    // Only a process with an armed virtual timerfd can need the publication
    // scratch; any other keeps the whole stack for its cells, as before
    // timerfds were virtualized.
    let timerfd_scratch = crate::syscalls::timerfd::holds_armed_virtual_timerfd(guest)
        .then(|| crate::syscalls::timerfd::reserve_scratch(&mut stack));
    match stack.commit() {
        Ok(guard) => {
            let bytes = AddrMut::<u8>::from_raw(at).ok_or(Errno::EFAULT)?;
            // The commit wrote zeroes, which the cells' address fills in.
            guest.memory().write_exact(bytes, &cells.bytes(at)[..len])?;
            return Ok(Some(StagedCells {
                at,
                storage: StagedCellsStorage::Stack(guard),
                timerfd_scratch,
            }));
        }
        Err(Errno::EFAULT) => {}
        Err(errno) => return Err(errno),
    }
    let rsp = guest.regs().await.rsp as usize;
    let Some(top) = rsp.checked_sub(len).map(|top| top & !7) else {
        return Ok(None);
    };
    let bottom = rsp.saturating_sub(STACK_RED_ZONE);
    let mut saved = [0u8; MAX_PRIVATE_CELLS_LEN];
    let mut found = None;
    for at in (bottom..=top).rev().step_by(8) {
        let window = at..at + len;
        if live
            .iter()
            .any(|range| range.start < window.end && window.start < range.end)
        {
            continue;
        }
        let Some(bytes) = AddrMut::<u8>::from_raw(at) else {
            continue;
        };
        if guest.memory().read_exact(bytes, &mut saved[..len]).is_ok() {
            found = Some((at, bytes));
            break;
        }
    }
    let Some((at, bytes)) = found else {
        return Ok(None);
    };
    let staged = StagedCells {
        at,
        storage: StagedCellsStorage::RedZone { at, len, saved },
        timerfd_scratch: None,
    };
    if guest
        .memory()
        .write_exact(bytes, &cells.bytes(at)[..len])
        .is_err()
    {
        // A read-only stack page. A write that failed partway may have changed
        // some bytes, so put them back.
        staged.release(guest);
        return Ok(None);
    }
    Ok(Some(staged))
}

/// The temporary mask a record/replay pselect6 that may wait sleeps under,
/// read at the call's entry, where Linux copies it (recording waits on a
/// private copy of it, `handle_pselect6`). The mask is read through the kernel
/// (`read_kernel_sigset`), so a mask the kernel's copy faults on, such as one
/// on a `PROT_NONE` page that a tracer read would still return, gives `None`:
/// the call then keeps the guest's wrapper and the kernel fails it with EFAULT
/// in Linux's order (Claude review of
/// https://github.com/rrnewton/hermit/pull/4166). `None` too when the call
/// has no mask, its wrapper cannot be read, or the size is not the kernel's.
async fn entry_pselect6_temporary_mask<G, T>(
    guest: &mut G,
    call: syscalls::Pselect6,
) -> Option<KernelSigset>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let wrapper = call.sigmask()?;
    let argument: Pselect6SigmaskArg = guest.memory().read_value(wrapper.cast()).ok()?;
    if argument.sigsetsize != KERNEL_SIGSET_SIZE {
        return None;
    }
    let mask_addr = Addr::<libc::sigset_t>::from_raw(argument.sigmask)?;
    let mask = read_kernel_sigset(guest, mask_addr).await.ok()?;
    super::signal_shadow::note_wait_mask(guest, mask);
    Some(mask)
}

/// The temporary mask a replayed zero-timeout pselect6 sleeps under, read
/// once the scheduler grants the call, which keeps its turn until it returns;
/// `None` when the call has no mask or the wrapper or the mask cannot be read
/// (`end_replayed_pselect6_under_its_temporary_mask`).
fn replayed_pselect6_temporary_mask<G, T>(
    guest: &mut G,
    call: syscalls::Pselect6,
) -> Option<KernelSigset>
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let wrapper = call.sigmask()?;
    let argument: Pselect6SigmaskArg = guest.memory().read_value(wrapper.cast()).ok()?;
    if argument.sigsetsize != KERNEL_SIGSET_SIZE {
        return None;
    }
    let mask_addr = Addr::<KernelSigset>::from_raw(argument.sigmask)?;
    let mask = guest.memory().read_value(mask_addr).ok()?;
    super::signal_shadow::note_wait_mask(guest, mask);
    Some(mask)
}

/// Whether replay could deliver a signal that interrupts this record/replay
/// pselect6 (`end_replayed_pselect6_under_its_temporary_mask`), whose 24 bytes
/// go in the stack scratch or at the top of the red zone. Recording a
/// zero-timeout call passes the guest's own wrapper to the kernel, so it
/// accepts the call on any stack, and a call whose stack pointer is less than
/// 24 bytes above unwritable memory left replay nowhere to deliver: the kernel
/// restarted the served call and replay stopped at the next syscall
/// (https://github.com/rrnewton/hermit/issues/4112). The top 24 bytes of the
/// red zone are readable exactly when the stack pointer is at least 24 bytes
/// above unwritable memory, and the stack scratch lies below them, so this
/// reads them. `true` for a call with no mask to deliver under, and for one
/// whose wrapper Linux cannot read, which it fails before it waits. A function
/// of the guest's stack and memory, which replay reproduces.
async fn replayed_pselect6_delivery_fits<G, T>(guest: &mut G, call: syscalls::Pselect6) -> bool
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let Some(wrapper) = call.sigmask() else {
        return true;
    };
    let argument: Result<Pselect6SigmaskArg, _> = guest.memory().read_value(wrapper.cast());
    match argument {
        Ok(argument) if argument.sigmask != 0 => {}
        _ => return true,
    }
    let rsp = guest.regs().await.rsp as usize;
    let mut cells = [0u8; MAX_PRIVATE_CELLS_LEN];
    rsp.checked_sub(MAX_PRIVATE_CELLS_LEN)
        .and_then(|top| AddrMut::<u8>::from_raw(top & !7))
        .is_some_and(|top| guest.memory().read_exact(top, &mut cells).is_ok())
}

/// Ends a replayed pselect6 that the recording says a signal interrupted
/// (`ERESTARTNOHAND`), when the signal is one that only the call's temporary
/// mask unblocks.
///
/// Replay serves pselect6 from the recording, so the kernel never installs the
/// call's temporary mask. Under the thread's own mask, which blocks the signal
/// the scheduler sent at the recorded point, the kernel finds nothing to
/// deliver, restarts the call, and the guest's next system call no longer
/// matches the recording (https://github.com/rrnewton/hermit/issues/3992).
/// Linux delivers such a signal before it puts the thread's own mask back, and
/// the handler's frame restores that mask when it returns. Only the kernel can
/// do both, so replay injects a ppoll with no descriptors, a zero timeout
/// and a scratch copy of the mask the call read at entry (`temporary_mask`,
/// from `replayed_pselect6_temporary_mask`), never the guest's buffer, which a
/// sibling may have rewritten while the call was served (review of
/// https://github.com/rrnewton/hermit/pull/4052): the kernel installs
/// the mask, finds the signal pending, returns `ERESTARTNOHAND` and delivers
/// the signal, as it did when the recorded call returned. A zero timeout never
/// sleeps, and Linux writes no remaining time back for it, so neither host
/// timing nor host time reaches the guest.
///
/// A zero-timeout masked pselect6 and a zero-timeout masked ppoll end the same
/// way when a signal their mask unblocks is already pending, and replay serves
/// them the same way, so both end through this too
/// (https://github.com/rrnewton/hermit/issues/4054).
///
/// Every value this reads (guest memory, and the kernel's pending set, mask
/// and dispositions in this thread's turn) is a function of the replayed
/// schedule. When no such signal is pending, the call returns as before.
async fn end_replayed_pselect6_under_its_temporary_mask<G, T>(
    guest: &mut G,
    temporary_mask: KernelSigset,
) where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let Ok(state) = read_thread_signal_state(guest.pid(), guest.tid()) else {
        return;
    };
    if state.pending
        & state.blocked
        & state.interrupting(kernel_installed_signal_mask(temporary_mask))
        == 0
    {
        return;
    }
    // The cells go in the stack scratch, or at the top of the red zone when
    // the thread runs on a stack it placed near the start of a page, which is
    // valid. The guest's red-zone bytes go back once the injected call has
    // returned. The kernel builds a signal frame below the red zone, and the
    // guest runs no code before the bytes are back, so it never sees them
    // change. Nothing is mapped, so the replayed address space stays the
    // recording's. No window is excluded: the call has returned, and Linux
    // touches only the cells. The 24 bytes fit wherever recording staged its
    // own 24-byte mask copy (Claude re-review of
    // https://github.com/rrnewton/hermit/pull/4087).
    let Ok(Some(staged)) =
        stage_private_cells(guest, PrivateCells::MaskAndZeroTimeout(temporary_mask), &[]).await
    else {
        return;
    };
    inject_signal_delivery(guest, staged.mask(), staged.after_mask()).await;
    staged.release(guest);
}

/// Injects the zero-timeout ppoll with no descriptors that delivers a
/// replayed call's signal under its temporary mask (`mask`), as recording ran
/// the call itself: the kernel installs the mask, finds the signal pending,
/// returns `ERESTARTNOHAND` and delivers the signal as the injected call
/// returns, as it did after the recorded call, and the guest then gets the
/// recorded result. ppoll takes the mask pointer directly, so the delivery
/// needs no `{ sigmask, sigsetsize }` wrapper, which a pselect6 would.
async fn inject_signal_delivery<G, T>(guest: &mut G, mask: usize, zero_timeout: usize)
where
    G: Guest<Detcore<T>>,
    T: RecordOrReplay,
{
    let _ = guest
        .inject(
            syscalls::Ppoll::new()
                .with_fds(None)
                .with_nfds(0)
                .with_timeout(AddrMut::from_raw(zero_timeout))
                .with_sigmask(Addr::from_raw(mask))
                .with_sigsetsize(KERNEL_SIGSET_SIZE),
        )
        .await;
}

pub(super) fn pselect6_fd_set_len(nfds: i32) -> Result<usize, Errno> {
    let nfds = usize::try_from(nfds).map_err(|_| Errno::EINVAL)?;
    let bits_per_word = std::mem::size_of::<libc::c_ulong>() * 8;
    Ok(nfds.div_ceil(bits_per_word) * std::mem::size_of::<libc::c_ulong>())
}

fn pselect6_probe_result(result: Result<i64, Errno>) -> Result<i64, Errno> {
    match result {
        // The injected syscall runs outside the guest's original restart frame.
        // Do not expose this kernel-internal restart instruction at the
        // rewritten pselect6 call site.
        Err(Errno::ERESTARTSYS) => Err(Errno::EINTR),
        result => result,
    }
}

pub(super) fn read_pselect6_fd_set<T, G>(
    guest: &mut G,
    address: Option<AddrMut<'_, libc::fd_set>>,
    len: usize,
) -> Result<Option<Vec<u8>>, Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let Some(address) = address else {
        return Ok(None);
    };
    let mut bytes = vec![0; len];
    if len != 0 {
        guest
            .memory()
            .read_exact(address.cast(), &mut bytes)
            .map_err(|_| Errno::EFAULT)?;
    }
    Ok(Some(bytes))
}

pub(super) fn write_pselect6_fd_set<T, G>(
    guest: &mut G,
    address: Option<AddrMut<'_, libc::fd_set>>,
    bytes: &Option<Vec<u8>>,
) -> Result<(), Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    if let (Some(address), Some(bytes)) = (address, bytes)
        && !bytes.is_empty()
    {
        guest
            .memory()
            .write_exact(address.cast(), bytes)
            .map_err(|_| Errno::EFAULT)?;
    }
    Ok(())
}

fn copy_pselect6_fd_set<T, G>(
    guest: &mut G,
    source: Option<AddrMut<'_, libc::fd_set>>,
    destination: Option<AddrMut<'_, libc::fd_set>>,
    len: usize,
) -> Result<(), Error>
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    if let (Some(source), Some(destination)) = (source, destination)
        && len != 0
    {
        let mut bytes = vec![0; len];
        guest
            .memory()
            .read_exact(source.cast(), &mut bytes)
            .map_err(|_| Errno::EFAULT)?;
        guest
            .memory()
            .write_exact(destination.cast(), &bytes)
            .map_err(|_| Errno::EFAULT)?;
    }
    Ok(())
}

pub(super) fn ppoll_timeout_duration(timeout: Timespec) -> Result<Duration, Errno> {
    let seconds = u64::try_from(timeout.tv_sec).map_err(|_| Errno::EINVAL)?;
    let nanoseconds = u32::try_from(timeout.tv_nsec).map_err(|_| Errno::EINVAL)?;
    if nanoseconds >= 1_000_000_000 {
        return Err(Errno::EINVAL);
    }
    Ok(Duration::new(seconds, nanoseconds))
}

/// `select`'s timeout as Linux reads it. In Linux v7.1 `kern_select`
/// (`fs/select.c`) first refuses a negative `tv_sec` or `tv_usec` with
/// `EINVAL`, then passes `tv_sec + tv_usec / USEC_PER_SEC` seconds and
/// `(tv_usec % USEC_PER_SEC) * NSEC_PER_USEC` nanoseconds to
/// `poll_select_set_timeout`, which refuses only what `timespec64_valid`
/// (`include/linux/time64.h`) refuses: so microseconds of a second or more
/// fold into the seconds. (v6.12 has no separate sign check, so there a
/// mixed-sign timeval such as `{2, -1000000}` normalizes to 1 s; v7.1 added
/// the check because the signed sum could wrap a negative timeout into a
/// valid one. This follows v7.1, the measured host behaviour.) A sum that
/// would overflow the signed seconds is `EINVAL` here.
pub(super) fn select_timeout_duration(timeout: libc::timeval) -> Result<Duration, Errno> {
    if timeout.tv_sec < 0 || timeout.tv_usec < 0 {
        return Err(Errno::EINVAL);
    }
    let seconds = timeout
        .tv_sec
        .checked_add(timeout.tv_usec / 1_000_000)
        .ok_or(Errno::EINVAL)?;
    let nanoseconds = (timeout.tv_usec % 1_000_000) * 1_000;
    Ok(Duration::new(seconds as u64, nanoseconds as u32))
}

fn timespec_from_duration(duration: Duration) -> Timespec {
    Timespec {
        tv_sec: duration.as_secs() as libc::time_t,
        tv_nsec: duration.subsec_nanos() as libc::c_long,
    }
}

const SCM_TIMESTAMP_OLD: libc::c_int = 29;
const SCM_TIMESTAMPNS_OLD: libc::c_int = 35;
const SCM_TIMESTAMPING_OLD: libc::c_int = 37;
const SCM_TIMESTAMP_NEW: libc::c_int = 63;
const SCM_TIMESTAMPNS_NEW: libc::c_int = 64;
const SCM_TIMESTAMPING_NEW: libc::c_int = 65;
const MAX_CONTROL_BYTES: usize = 64 * 1024;

#[derive(Clone, Copy)]
enum SocketTimestampKind {
    Timeval,
    Timespec,
    Timestamping,
}

#[derive(Clone, Copy)]
struct SocketTimestampMessage {
    data_offset: usize,
    available_data_len: usize,
    kind: SocketTimestampKind,
}

fn cmsg_align(length: usize) -> usize {
    let alignment = std::mem::size_of::<usize>();
    (length + alignment - 1) & !(alignment - 1)
}

fn read_control_value<T: Copy>(bytes: &[u8]) -> Option<T> {
    if bytes.len() < std::mem::size_of::<T>() {
        return None;
    }
    // SAFETY: the length check guarantees a complete T, and read_unaligned
    // permits the control buffer's byte alignment.
    Some(unsafe { bytes.as_ptr().cast::<T>().read_unaligned() })
}

fn write_control_value<T: Copy>(bytes: &mut [u8], value: T) -> bool {
    if bytes.len() < std::mem::size_of::<T>() {
        return false;
    }
    // SAFETY: the length check guarantees room for T, and write_unaligned
    // permits the control buffer's byte alignment.
    unsafe { bytes.as_mut_ptr().cast::<T>().write_unaligned(value) };
    true
}

fn write_control_prefix<T: Copy>(bytes: &mut [u8], value: T) -> usize {
    let value_len = std::mem::size_of::<T>();
    let write_len = bytes.len().min(value_len);
    // SAFETY: `value` is alive for this copy and the resulting byte view has
    // exactly its initialized object representation.
    let value_bytes =
        unsafe { std::slice::from_raw_parts((&value as *const T).cast::<u8>(), value_len) };
    bytes[..write_len].copy_from_slice(&value_bytes[..write_len]);
    write_len
}

fn socket_timestamp_messages(control: &[u8]) -> Vec<SocketTimestampMessage> {
    let header_len = cmsg_align(std::mem::size_of::<libc::cmsghdr>());
    let mut messages = Vec::new();
    let mut offset = 0usize;

    while let Some(header_bytes) = control.get(offset..) {
        let Some(header) = read_control_value::<libc::cmsghdr>(header_bytes) else {
            break;
        };
        if header.cmsg_len < header_len {
            break;
        }
        let Some(end) = offset.checked_add(header.cmsg_len) else {
            break;
        };

        if header.cmsg_level == libc::SOL_SOCKET {
            let data_offset = offset + header_len;
            let declared_data_len = header.cmsg_len - header_len;
            let available_data_len = control
                .len()
                .saturating_sub(data_offset)
                .min(declared_data_len);
            let kind = match header.cmsg_type {
                SCM_TIMESTAMP_OLD | SCM_TIMESTAMP_NEW => Some(SocketTimestampKind::Timeval),
                SCM_TIMESTAMPNS_OLD | SCM_TIMESTAMPNS_NEW => Some(SocketTimestampKind::Timespec),
                SCM_TIMESTAMPING_OLD | SCM_TIMESTAMPING_NEW => {
                    Some(SocketTimestampKind::Timestamping)
                }
                _ => None,
            };
            if let Some(kind) = kind {
                messages.push(SocketTimestampMessage {
                    data_offset,
                    available_data_len,
                    kind,
                });
            }
        }

        if end > control.len() {
            break;
        }

        let step = cmsg_align(header.cmsg_len);
        let Some(next) = offset.checked_add(step) else {
            break;
        };
        if next <= offset {
            break;
        }
        offset = next;
    }
    messages
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-901)
fn canonicalize_socket_timestamps(control: &mut [u8], now: LogicalTime) -> usize {
    let messages = socket_timestamp_messages(control);
    let timespec = libc::timespec {
        tv_sec: now.as_secs() as libc::time_t,
        tv_nsec: now.subsec_nanos() as libc::c_long,
    };
    let timeval = libc::timeval {
        tv_sec: timespec.tv_sec,
        tv_usec: (timespec.tv_nsec / 1_000) as libc::suseconds_t,
    };

    for message in &messages {
        let available_end = message
            .data_offset
            .saturating_add(message.available_data_len)
            .min(control.len());
        let data = &mut control[message.data_offset..available_end];
        match message.kind {
            SocketTimestampKind::Timeval => {
                write_control_prefix(data, timeval);
            }
            SocketTimestampKind::Timespec => {
                write_control_prefix(data, timespec);
            }
            SocketTimestampKind::Timestamping => {
                let size = std::mem::size_of::<libc::timespec>();
                let zero = libc::timespec {
                    tv_sec: 0,
                    tv_nsec: 0,
                };
                for slot in 0..3 {
                    let start = slot * size;
                    if start >= data.len() {
                        break;
                    }
                    let end = (start + size).min(data.len());
                    let slot_bytes = &mut data[start..end];
                    if slot_bytes.len() != size {
                        slot_bytes.fill(0);
                        continue;
                    }
                    let original = read_control_value::<libc::timespec>(slot_bytes)
                        .expect("complete timestamping slot");
                    let replacement = if original.tv_sec == 0 && original.tv_nsec == 0 {
                        zero
                    } else {
                        timespec
                    };
                    let _ = write_control_value(slot_bytes, replacement);
                }
            }
        }
    }
    messages.len()
}

fn sanitize_ppoll_signal_mask(mask: u64) -> u64 {
    let signal_bit = (reverie::PERF_EVENT_SIGNAL as usize) - 1;
    mask & !(1_u64 << signal_bit)
}

/// The pollfd array a ppoll reads and writes, if it has one.
fn ppoll_fds_range(call: syscalls::Ppoll) -> Option<std::ops::Range<usize>> {
    call.fds().map(|fds| {
        let start = fds.as_raw();
        let len = usize::try_from(call.nfds())
            .unwrap_or(usize::MAX)
            .saturating_mul(std::mem::size_of::<libc::pollfd>());
        start..start.saturating_add(len)
    })
}

/// Whether the kernel owns a ppoll's blocking wait: when threads are not
/// sequentialized, and for every record/replay call, masked or not, as for a
/// masked pselect6 (https://github.com/rrnewton/hermit/issues/4042). A masked
/// sequentialized run-mode wait still takes the probe path.
fn ppoll_uses_kernel_wait(sequentialize_threads: bool, recordreplay_modes: bool) -> bool {
    !sequentialize_threads || recordreplay_modes
}

impl<T: RecordOrReplay> Detcore<T> {
    /// poll syscall (MAYHANG)
    // TODO-HUMAN-REVIEW(PR-1023): Review zero-timeout poll scheduling across backends.
    pub async fn handle_poll<G: Guest<Self>>(
        &self,
        guest: &mut G,

        call: syscalls::Poll,
    ) -> Result<i64, Error> {
        if self.cfg.sequentialize_threads && call.timeout() == 0 {
            // This cannot block, but still yield a scheduler turn so a polling thread cannot
            // monopolize the guest between preemptions.
            let yield_to_peer = self.cfg.backend.loopback_pollers_yield_to_peers
                && guest.thread_state().has_loopback_peer();
            resource_request(
                guest,
                zero_timeout_poll_request(guest.thread_state().dettid, yield_to_peer),
            )
            .await;
            if self.cfg.recordreplay_modes {
                Ok(self.record_or_replay(guest, call).await?)
            } else {
                // A virtual timerfd among the fds is readable exactly when its
                // deadline has passed (`crate::syscalls::timerfd`).
                crate::syscalls::timerfd::publish_expired(guest).await;
                Ok(guest.inject(call).await?)
            }
        } else if !self.cfg.sequentialize_threads || self.cfg.recordreplay_modes {
            // In replay mode, we cannot assume the existence of FILES during replay.
            // Thus we must record the poll and replay it from the trace.
            Ok(self.handle_external_poll(guest, call).await?)
        } else {
            // TODO:
            // if is-external-poll { self.handle_external_poll(guest, call) }
            self.handle_internal_poll(guest, call).await
        }
    }

    /// Record or replay a raw `select` or `pselect6` the way `poll` is: a zero
    /// timeout cannot block and takes an ordinary scheduler turn; any other call
    /// may wait in the kernel, so it runs as a blocking external operation. The
    /// recorder captures the descriptor sets and remaining time, so replay never
    /// asks the kernel about descriptors it did not recreate.
    async fn record_or_replay_select_family<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
        zero_timeout: bool,
    ) -> Result<i64, Error> {
        if zero_timeout {
            resource_request(guest, Resources::new(guest.thread_state().dettid)).await;
            Ok(self.record_or_replay(guest, call).await?)
        } else {
            self.record_or_replay_blocking(guest, call).await
        }
    }

    /// Refuses a large-nfds pselect6 with a temporary mask, or a record/replay
    /// one that may wait (`record_or_replay_pselect6_under_a_private_mask`),
    /// whose private copy has nowhere to go: the stack has no writable scratch below its red zone,
    /// and every red-zone window overlaps one of the call's own buffers
    /// (`stage_private_cells`). Forwarding the guest's wrapper instead
    /// would let a sibling change the mask the kernel installs after the
    /// scheduler has recorded the one it waits for, so the call could time out
    /// with a wrong answer (Codex re-check #4 of
    /// https://github.com/rrnewton/hermit/pull/4053). A fail-closed run stops
    /// with the policy refusal and this message. A run started with
    /// `--allow-unsupported-syscalls` runs on: the call keeps the guest's
    /// wrapper, whose mask a sibling may still change, so the scheduler records
    /// its sleeping mask as unknown (`Resources::blocked_signal_mask_unknown`),
    /// not the thread's own, and arms no entry barrier for it (Codex re-check
    /// #5).
    async fn refuse_unstageable_pselect6_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Pselect6,
    ) -> Result<i64, Error> {
        use std::io::Write as _;
        let message = "unsupported: pselect6 with a temporary signal mask, on a stack with \
            no room below its red zone whose red zone the call's own buffers fill: Hermit \
            has nowhere to put the private copy of the mask the kernel must install \
            (https://github.com/rrnewton/hermit/pull/4053)";
        tracing::error!("[tid {}] {message}", guest.tid());
        if self.cfg.panic_on_unsupported_syscalls {
            let _ = writeln!(crate::util::RetryingStderr, "{message}");
            return self
                .refuse_unserviceable_operation(guest, Sysno::pselect6, Errno::EFAULT)
                .await;
        }
        self.record_or_replay_blocking_with_unknown_mask(guest, Syscall::Pselect6(call))
            .await
    }

    /// Refuses a record/replay pselect6 with a temporary mask on a stack less
    /// than 24 bytes above unwritable memory, where replay could not deliver a
    /// signal that interrupts it (`replayed_pselect6_delivery_fits`). Recording
    /// and replay refuse it alike. A fail-closed run stops with the policy
    /// refusal and this message, whose result this returns. A run started
    /// with `--allow-unsupported-syscalls` records the call as before, and a
    /// replay of an interrupted one stops at the next syscall; this returns
    /// `None` for it.
    async fn refuse_undeliverable_pselect6_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
    ) -> Option<Result<i64, Error>> {
        use std::io::Write as _;
        let message = "unsupported: pselect6 with a temporary signal mask on a stack less than \
            24 bytes above unwritable memory: replay has nowhere to deliver a signal that \
            interrupts it (https://github.com/rrnewton/hermit/issues/4112)";
        tracing::error!("[tid {}] {message}", guest.tid());
        if !self.cfg.panic_on_unsupported_syscalls {
            return None;
        }
        let _ = writeln!(crate::util::RetryingStderr, "{message}");
        Some(
            self.refuse_unserviceable_operation(guest, Sysno::pselect6, Errno::EFAULT)
                .await,
        )
    }

    /// Records or replays a masked pselect6 that may wait, under a private
    /// copy of the mask read at its entry (`mask`), staged clear of the fd
    /// sets and timeout the kernel reads and writes (`stage_private_cells`).
    /// The outer `Err` is a staging failure; a call with no room for the copy
    /// is refused (`refuse_unstageable_pselect6_mask`).
    async fn record_or_replay_pselect6_under_a_private_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Pselect6,
        mask: KernelSigset,
    ) -> Result<Result<i64, Error>, Error> {
        let set_len = pselect6_fd_set_len(call.nfds()).unwrap_or(0);
        let live: Vec<std::ops::Range<usize>> = [
            call.readfds().map(|set| (set.as_raw(), set_len)),
            call.writefds().map(|set| (set.as_raw(), set_len)),
            call.exceptfds().map(|set| (set.as_raw(), set_len)),
            call.timeout()
                .map(|timeout| (timeout.as_raw(), std::mem::size_of::<Timespec>())),
        ]
        .into_iter()
        .flatten()
        .map(|(start, len)| start..start.saturating_add(len))
        .collect();
        let Some(staged) =
            stage_private_cells(guest, PrivateCells::Pselect6Mask(mask), &live).await?
        else {
            return Ok(self.refuse_unstageable_pselect6_mask(guest, call).await);
        };
        let result = self
            .record_or_replay_blocking_with_unknown_mask(
                guest,
                Syscall::Pselect6(call.with_sigmask(Addr::from_raw(staged.after_mask()))),
            )
            .await;
        staged.release(guest);
        Ok(result)
    }

    /// pselect6 syscall (MAYHANG).
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#686): Review scratch fd sets and scheduler polling.
    pub async fn handle_pselect6<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Pselect6,
    ) -> Result<i64, Error> {
        if self.cfg.recordreplay_modes {
            let zero_timeout = call.timeout().is_some_and(|timeout| {
                guest
                    .memory()
                    .read_value(timeout)
                    .is_ok_and(|timeout: Timespec| timeout.tv_sec == 0 && timeout.tv_nsec == 0)
            });
            // A call with a `{ sigmask, sigsetsize }` wrapper may sleep under a
            // temporary mask, and the scheduler must not take the thread's
            // ordinary mask for it: it would arm the release barrier for a
            // signal the call blocks and wait for a wake Linux never delivers
            // (https://github.com/rrnewton/hermit/issues/3963). Nor can this turn
            // read the mask the kernel will install: the wrapper may sit in
            // memory a tracer read cannot reach though the kernel can, and a
            // sibling may rewrite the mask before the kernel copies it. So the
            // scheduler records no mask for such a call
            // (`Resources::blocked_signal_mask_unknown`). Without a wrapper the
            // call sleeps under the thread's own mask, which the scheduler reads.
            // A call that may wait sleeps under a private copy of its mask,
            // read here, at its entry, where Linux copies it. Handing the kernel
            // the guest's wrapper let a sibling turn that ran after the grant
            // change the mask the kernel copied, at a point host timing chose,
            // and replay could not tell (https://github.com/rrnewton/hermit/issues/4095).
            // Replay reads the mask at the same point and delivers a recorded
            // interruption under it. A zero-timeout call holds its turn until
            // it returns, so no sibling runs before the kernel copies the mask,
            // and replay reads it once the call is granted (review of
            // https://github.com/rrnewton/hermit/pull/4084). A zero timeout still
            // installs the mask and ends the call with ERESTARTNOHAND for a
            // pending signal it unblocks
            // (https://github.com/rrnewton/hermit/issues/4054).
            if !replayed_pselect6_delivery_fits(guest, call).await
                && let Some(refused) = self.refuse_undeliverable_pselect6_mask(guest).await
            {
                return refused;
            }
            let read_mask = self.cfg.replaying && call.sigmask().is_some();
            let mut temporary_mask = None;
            let result = if call.sigmask().is_some() && !zero_timeout {
                // `None` when the call names no mask, or one the kernel rejects
                // or the tracer cannot read, though the kernel can: such a call
                // keeps the guest's own wrapper.
                let entry_mask = entry_pselect6_temporary_mask(guest, call).await;
                if read_mask {
                    temporary_mask = entry_mask;
                }
                match entry_mask {
                    Some(mask) => {
                        self.record_or_replay_pselect6_under_a_private_mask(guest, call, mask)
                            .await?
                    }
                    None => {
                        self.record_or_replay_blocking_with_unknown_mask(
                            guest,
                            Syscall::Pselect6(call),
                        )
                        .await
                    }
                }
            } else if zero_timeout {
                resource_request(guest, Resources::new(guest.thread_state().dettid)).await;
                if read_mask {
                    temporary_mask = replayed_pselect6_temporary_mask(guest, call);
                }
                self.record_or_replay(guest, Syscall::Pselect6(call))
                    .await
                    .map_err(Into::into)
            } else {
                self.record_or_replay_blocking(guest, Syscall::Pselect6(call))
                    .await
            };
            if let Some(mask) = temporary_mask
                && matches!(result, Err(Error::Errno(Errno::ERESTARTNOHAND)))
            {
                end_replayed_pselect6_under_its_temporary_mask(guest, mask).await;
            }
            return result;
        }
        if !self.cfg.sequentialize_threads {
            return Ok(guest.inject(call).await?);
        }

        if call.nfds() < 0 {
            return Ok(guest.inject(call).await?);
        }

        // Linux copies pselect6's outer { sigmask, sigsetsize } wrapper before
        // validating the timeout. Copy only the wrapper here; validation of the
        // pointed-to signal mask remains below, after timeout validation.
        let sigmask_argument = match call.sigmask() {
            Some(argument) => {
                // A split read can fall back to PTRACE_PEEKDATA for the final
                // word, bypassing PROT_NONE or reporting EIO for an unmapped
                // page. Have Linux validate both wrapper words first. It copies
                // this wrapper before rejecting a malformed timeout, without
                // reading the inner mask, changing it, waiting, or writing output.
                let mut stack = guest.stack().await;
                let validation_timeout = stack.reserve::<Timespec>();
                let _guard = stack.commit()?;
                guest.memory().write_value(
                    validation_timeout,
                    &Timespec {
                        tv_sec: 0,
                        tv_nsec: 1_000_000_000,
                    },
                )?;
                let validation = syscalls::Pselect6::new()
                    .with_nfds(0)
                    .with_readfds(None)
                    .with_writefds(None)
                    .with_exceptfds(None)
                    .with_timeout(Some(validation_timeout))
                    .with_sigmask(Some(argument));
                match guest.inject(validation).await {
                    Err(Errno::EINVAL) => {}
                    // The call's first injection
                    // (`restart_if_first_probe_not_run`).
                    Err(errno) => {
                        return crate::syscalls::signal::restart_if_first_probe_not_run(Err(
                            errno.into()
                        ));
                    }
                    // Success would mean the backend did not validate the probe.
                    Ok(_) => return Err(Errno::EIO.into()),
                }
                let argument: Pselect6SigmaskArg = guest.memory().read_value(argument.cast())?;
                Some(argument)
            }
            None => None,
        };
        let raw_timeout = match call.timeout() {
            Some(timeout) => {
                let timeout: Timespec = guest.memory().read_value(timeout)?;
                Some(timeout)
            }
            None => None,
        };
        let timeout = raw_timeout.map(ppoll_timeout_duration).transpose()?;
        if let Some(argument) = sigmask_argument
            && argument.sigmask != 0
        {
            self.observe_under_temporary_mask(
                guest,
                Addr::<libc::sigset_t>::from_raw(argument.sigmask),
                argument.sigsetsize,
            )
            .await?;
        }
        if timeout == Some(Duration::ZERO) {
            // A virtual timerfd among the sets is readable exactly when its
            // deadline has passed (`crate::syscalls::timerfd`).
            crate::syscalls::timerfd::publish_expired(guest).await;
            return Ok(guest.inject(call).await?);
        }

        // Linux clamps raw fd-set copies to the process fd table's current max_fds.
        // Its initial table holds one machine word; larger nfds values can therefore
        // require fewer bytes than a userspace calculation predicts. Keep those calls
        // under kernel ownership rather than over-reading the guest bitmap.
        if call.nfds() > PSELECT6_INTERNAL_MAX_NFDS {
            // The kernel sleeps under the inner mask, so record it for the
            // scheduler's SIGCHLD target choice (`Resources::blocked_signal_mask`).
            // A mask the kernel would reject (wrong size, or a pointer it cannot
            // read) fails the real call before it sleeps or installs anything, so
            // the thread's own mask is the right record there (`None`).
            let snapshot = match sigmask_argument {
                Some(argument)
                    if argument.sigmask != 0 && argument.sigsetsize == KERNEL_SIGSET_SIZE =>
                {
                    match Addr::<libc::sigset_t>::from_raw(argument.sigmask) {
                        Some(mask_addr) => {
                            let mask = read_kernel_sigset(guest, mask_addr).await.ok();
                            if let Some(mask) = mask {
                                super::signal_shadow::note_wait_mask(guest, mask);
                            }
                            mask
                        }
                        None => None,
                    }
                }
                _ => None,
            };
            let Some(mask) = snapshot else {
                return self
                    .record_or_replay_blocking_with_mask(guest, Syscall::Pselect6(call), None)
                    .await;
            };
            // The call sleeps under a private copy of the mask read here, not the
            // guest's wrapper: a sibling may rewrite the guest's buffer before the
            // released call reaches the kernel, and the scheduler waits for the
            // kernel to install exactly this mask (`background_entry_barriers`,
            // Codex review of https://github.com/rrnewton/hermit/pull/4053). The
            // wrapper and the mask were validated above, in Linux's order. The
            // copy stays readable until the call has rejoined.
            // The buffers Linux reads and writes, which the copy must not
            // overlap. The fd sets span at most their nominal length; Linux
            // may copy less.
            let set_len = pselect6_fd_set_len(call.nfds())?;
            let live: Vec<std::ops::Range<usize>> = [
                call.readfds().map(|set| (set.as_raw(), set_len)),
                call.writefds().map(|set| (set.as_raw(), set_len)),
                call.exceptfds().map(|set| (set.as_raw(), set_len)),
                call.timeout()
                    .map(|timeout| (timeout.as_raw(), std::mem::size_of::<Timespec>())),
            ]
            .into_iter()
            .flatten()
            .map(|(start, len)| start..start.saturating_add(len))
            .collect();
            let Some(staged) =
                stage_private_cells(guest, PrivateCells::Pselect6Mask(mask), &live).await?
            else {
                return self.refuse_unstageable_pselect6_mask(guest, call).await;
            };
            let result = self
                .record_or_replay_blocking_with_mask(
                    guest,
                    Syscall::Pselect6(call.with_sigmask(Addr::from_raw(staged.after_mask()))),
                    Some(kernel_installed_signal_mask(mask)),
                )
                .await;
            staged.release(guest);
            return result;
        }

        // Linux wraps pselect6's temporary mask in { pointer, size }. Glibc supplies
        // the wrapper even when the inner pointer is null. A real mask must stay in
        // effect for the whole wait so an unblocked signal (make's jobserver unblocks
        // SIGCHLD) can interrupt it. Previously that forced the external-blocking path,
        // whose completion timing is host-decided and is a source of `make -jN`
        // execution-log divergence. With SIGCHLD admission now deterministic (scheduler
        // `sigchld_deferred`/`sigchld_ready`), honor the mask on each deterministic poll
        // probe instead: a pending unblocked signal is observed at a scheduler-decided
        // probe point rather than at host signal-arrival time.
        let sigmask = if let Some(argument) = sigmask_argument {
            if argument.sigmask != 0 {
                if argument.sigsetsize != KERNEL_SIGSET_SIZE {
                    return Err(Errno::EINVAL.into());
                }
                let mask_addr =
                    Addr::<libc::sigset_t>::from_raw(argument.sigmask).ok_or(Errno::EFAULT)?;
                let mask = read_kernel_sigset(guest, mask_addr).await?;
                super::signal_shadow::note_wait_mask(guest, mask);
                Some(sanitize_ppoll_signal_mask(mask))
            } else {
                None
            }
        } else {
            None
        };
        // The inner mask was snapshotted above. Do not let later guest mutations of the
        // outer wrapper change the meaning of a retry probe.
        let call = call.with_sigmask(None);

        self.handle_internal_pselect6(guest, call, timeout, sigmask)
            .await
    }

    async fn handle_internal_pselect6<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Pselect6,
        timeout: Option<Duration>,
        sigmask: Option<u64>,
    ) -> Result<i64, Error> {
        let len = pselect6_fd_set_len(call.nfds())?;
        let deadline = match timeout {
            Some(timeout) => Some(thread_observe_time(guest).await + timeout),
            None => None,
        };
        let original_readfds = match read_pselect6_fd_set(guest, call.readfds(), len) {
            Ok(value) => value,
            Err(error) => {
                self.write_pselect6_remaining(guest, call, deadline).await?;
                return Err(error);
            }
        };
        let original_writefds = match read_pselect6_fd_set(guest, call.writefds(), len) {
            Ok(value) => value,
            Err(error) => {
                self.write_pselect6_remaining(guest, call, deadline).await?;
                return Err(error);
            }
        };
        let original_exceptfds = match read_pselect6_fd_set(guest, call.exceptfds(), len) {
            Ok(value) => value,
            Err(error) => {
                self.write_pselect6_remaining(guest, call, deadline).await?;
                return Err(error);
            }
        };

        let mut stack = guest.stack().await;
        let readfds = call.readfds().map(|_| stack.reserve::<libc::fd_set>());
        let writefds = call.writefds().map(|_| stack.reserve::<libc::fd_set>());
        let exceptfds = call.exceptfds().map(|_| stack.reserve::<libc::fd_set>());
        // pselect6's timeout is a writable in-out kernel timespec, so the probe
        // needs a mutable scratch cell (re-zeroed each iteration below to keep
        // every probe a non-blocking poll).
        let probe_timeout = stack.reserve::<Timespec>();
        // Carry the temporary signal mask on every zero-timeout probe so the kernel
        // applies it atomically: a pending, mask-unblocked signal makes the probe return
        // EINTR at a deterministic scheduler point. The probe's wrapper points at scratch
        // memory the guard keeps alive across each injection.
        // On a backend whose kernel reports the guest's signal state, a
        // `KernelSignalWait` below decides interruption against the temporary
        // mask instead (`with_wait_mask`), and the probes carry no mask.
        let kernel_signal_wait = self.cfg.backend.supports_blocked_wait_signal_interruption;
        let probe_sigmask = sigmask.filter(|_| !kernel_signal_wait).map(|mask| {
            let sigset = stack.push(mask);
            stack
                .push(Pselect6SigmaskArg {
                    sigmask: sigset.as_raw(),
                    sigsetsize: KERNEL_SIGSET_SIZE,
                })
                .cast()
        });
        // On a backend whose kernel reports the guest's signal state, only a
        // signal that would end the wait natively does; the mask cell lets the
        // wait block signals under the one scratch-stack guard
        // (https://github.com/rrnewton/hermit/issues/3146). A temporary mask is
        // the mask the call sleeps under, as Linux installs it for the whole
        // call: a signal it blocks never ends the call, and one it unblocks ends
        // it although the guest's own mask blocks it (`with_wait_mask`,
        // https://github.com/rrnewton/hermit/issues/3991). A restart resumes from
        // the remaining time written back below, so a default stop ends the wait
        // as on Linux. Unlike the other waits, it holds no signal until the call
        // returns, so a pending caught `SIGCHLD` ends it as on Linux, whoever
        // sent it, at a turn that host timing can choose: a known gap that this
        // wait had before (`KernelSignalWait::for_select`).
        let mut signals = kernel_signal_wait.then(|| {
            KernelSignalWait::for_select(guest)
                .with_wait_mask(sigmask.map(crate::syscalls::signal::kernel_installed_signal_mask))
        });
        // Set when a signal the temporary mask unblocks, pending in the kernel,
        // ends the call: the kernel must then deliver it under that mask (see
        // below).
        let mut deliver_under_temporary_mask = false;
        let mask_cell = signals.as_ref().map(|_| stack.reserve::<KernelSigset>());
        // This frame holds the scratch stack across every probe, so the
        // memory that makes an expired timerfd readable is reserved here
        // (`crate::syscalls::timerfd::publish_expired_in`).
        let timerfd_scratch = crate::syscalls::timerfd::reserve_scratch(&mut stack);
        let _guard = stack.commit()?;
        // The scheduler reads the effective mask while the wait parks
        // (`publish_wait_mask`).
        let mut published = PublishedWaitMask::default();
        if let Some(signals) = signals.as_ref() {
            publish_wait_mask(guest, signals.effective_mask(), &mut published).await;
        }
        let probe = call
            .with_readfds(readfds)
            .with_writefds(writefds)
            .with_exceptfds(exceptfds)
            .with_timeout(Some(probe_timeout))
            .with_sigmask(probe_sigmask);

        let mut resources = Resources::new(guest.thread_state().dettid);
        resources.insert(ResourceID::InternalIOPolling, Permission::W);
        resources.fyi("pselect6");
        resources.poll_deadline = poll_deadline_for(guest, deadline);
        // Keep the request metadata accurate, but do not make it eligible for
        // the scheduler's ERESTARTSYS wakeup. A cross-task signal must first be
        // checked against pselect6's snapshotted temporary mask and disposition.
        resources.set_signal_interrupt_errno(Errno::EINTR);

        // Every exit, including a `?` on a guest-memory write, must reach the mask
        // restore below, so the loop runs in its own async block.
        let result: Result<i64, Error> = {
            // Moved reborrows: a future holding a shared reference to a scratch
            // address would not be `Send`.
            let guest = &mut *guest;
            let signals = &mut signals;
            let resources = &mut resources;
            let published = &mut published;
            let deliver_under_temporary_mask = &mut deliver_under_temporary_mask;
            async move {
                loop {
                    let signaled = matches!(
                        resource_request(guest, resources.clone()).await,
                        ResumeStatus::Signaled(_)
                    );
                    // Virtual timerfds first (`crate::syscalls::timerfd`).
                    let next_timerfd =
                        crate::syscalls::timerfd::publish_expired_in(guest, Some(timerfd_scratch))
                            .await;
                    resources.poll_deadline =
                        crate::syscalls::timerfd::poll_deadline_with(guest, deadline, next_timerfd);
                    // A pending real-time timer expiration that this wait would
                    // be ended by, under the guest's mask or the call's temporary
                    // one (https://github.com/rrnewton/hermit/issues/4089).
                    let pending = pending_real_time_among(guest, REAL_TIME_SIGNALS).await;
                    if pending != 0 {
                        let (observable, discarded) = match (signals.as_ref(), sigmask) {
                            (Some(signals), _) => (signals.observable_now()?, 0),
                            (None, Some(mask)) => unblocked_by(
                                &read_thread_wait_signal_state(guest.pid(), guest.tid())?,
                                mask,
                            ),
                            (None, None) => (0, 0),
                        };
                        act_on_pending_real_time(guest, pending, observable, discarded).await;
                    }
                    // Every turn probes before it looks at pending signals, as Linux
                    // does: do_select polls every descriptor on each pass and returns
                    // the count of ready ones before it tests signal_pending, and at
                    // the start max_select_fd returns EBADF for a descriptor that is
                    // not open (`core_sys_select`). So a descriptor that becomes ready
                    // in a later turn in which a signal is also pending is reported
                    // (round-12 Medium on https://github.com/rrnewton/hermit/pull/3361).
                    // Each probe is a new select call, so a descriptor closed during
                    // the wait gets EBADF, where Linux reports it ready. Without the
                    // kernel's signal state, a scheduler `Signaled` answer still ends
                    // the call before the probe.
                    if signals.is_none() && signaled {
                        self.write_pselect6_remaining(guest, call, deadline).await?;
                        return Err(Errno::EINTR.into());
                    }
                    guest.memory().write_value(
                        probe_timeout,
                        &Timespec {
                            tv_sec: 0,
                            tv_nsec: 0,
                        },
                    )?;
                    write_pselect6_fd_set(guest, probe.readfds(), &original_readfds)?;
                    write_pselect6_fd_set(guest, probe.writefds(), &original_writefds)?;
                    write_pselect6_fd_set(guest, probe.exceptfds(), &original_exceptfds)?;

                    let raw_result = match signals.as_mut() {
                        // A signal stop that does not end the wait is absorbed (see
                        // `KernelSignalWait`). Every probe runs before the turn's check
                        // (`inject_first_probe`): a signal that would end the wait and
                        // stops the probe is held, the probe runs again, and the signal
                        // is delivered as the call returns. Once `block` has blocked
                        // every blockable signal, a pending one cannot stop the probe.
                        Some(signals) => match signals.inject_first_probe(guest, probe).await {
                            Ok(result) => result,
                            Err(error) => {
                                self.write_pselect6_remaining(guest, call, deadline).await?;
                                break Err(error);
                            }
                        },
                        None => guest.inject(probe).await,
                    };
                    let result = pselect6_probe_result(raw_result);
                    if result != Ok(0) {
                        let copy_result = if result.is_ok() {
                            self.copy_pselect6_results(guest, probe, call, len)
                        } else {
                            Ok(())
                        };
                        self.write_pselect6_remaining(guest, call, deadline).await?;
                        copy_result?;
                        break result.map_err(Into::into);
                    }
                    // The turn's check, after its probe found no descriptor ready and no
                    // error: only then does a pending signal end the call. A scheduler
                    // `Signaled` answer is only a hint here; the kernel's state decides.
                    // pselect6 returns ERESTARTNOHAND: EINTR after a handler, a restart
                    // with the remaining timeout after a stop.
                    if let Some(signals) = signals.as_ref() {
                        match signals.interrupted_with_state() {
                            Ok((false, _)) => {}
                            Ok((true, state)) => {
                                self.write_pselect6_remaining(guest, call, deadline).await?;
                                // A signal the backend holds, or one the guest's own
                                // mask leaves unblocked, is delivered as the call
                                // returns. One that only the temporary mask unblocks
                                // stays pending under the guest's own mask, and must be
                                // delivered under the temporary mask.
                                *deliver_under_temporary_mask = !signals.holds_signal()
                                    && signals.interrupts_only_under_wait_mask(&state);
                                break Err(Errno::ERESTARTNOHAND.into());
                            }
                            Err(error) => break Err(error),
                        }
                    }
                    if let Some(signals) = signals.as_mut()
                        && signals.needs_block()
                        && let Err(error) = signals.block(guest, mask_cell).await
                    {
                        self.write_pselect6_remaining(guest, call, deadline).await?;
                        break Err(error);
                    }
                    if let Some(signals) = signals.as_ref() {
                        publish_wait_mask(guest, signals.effective_mask(), published).await;
                    }

                    resources.poll_attempt += 1;
                    if let Some(deadline) = deadline
                        && thread_observe_time(guest).await >= deadline
                    {
                        let copy_result = self.copy_pselect6_results(guest, probe, call, len);
                        self.write_pselect6_remaining(guest, call, Some(deadline))
                            .await?;
                        copy_result?;
                        break Ok(0);
                    }
                    trace!(
                        "Retry #{} for syscall due to result Ok(0): {}",
                        resources.poll_attempt,
                        probe.display(&guest.memory())
                    );
                    record_retry_event(guest, probe).await;
                }
            }
            .await
        };
        let result = match &mut signals {
            Some(signals) => {
                let restored = signals.restore(guest, mask_cell).await;
                publish_wait_mask(guest, None, &mut published).await;
                result_after_restore(restored, result)?
            }
            None => result,
        };
        // Restoring the guest's mask cannot deliver that signal, which the mask
        // blocks, but check again that the backend holds nothing.
        if deliver_under_temporary_mask
            && !signals.as_ref().is_some_and(KernelSignalWait::holds_signal)
            && let Some(mask) = sigmask
            && matches!(result, Err(Error::Errno(Errno::ERESTARTNOHAND)))
        {
            // Linux delivers such a signal before it puts the guest's own mask
            // back, which may block it, and the handler's frame restores the
            // guest's mask when it returns. Only the kernel can do both, so the
            // thread makes one more call, a ppoll with no descriptors, a zero
            // timeout and the call's temporary mask, as replay delivers a
            // recorded call's signal (`inject_signal_delivery`): the kernel
            // installs the mask, finds the signal pending, returns
            // ERESTARTNOHAND and delivers it as the call returns, EINTR after a
            // handler (https://github.com/rrnewton/hermit/issues/3991).
            // - The mask is a private copy of the one this call read at entry
            //   (`sigmask`), as Linux copies it once at entry; the guest's
            //   buffer, which a sibling may have rewritten since, is not read
            //   again (review of https://github.com/rrnewton/hermit/pull/4051).
            // - A zero timeout never sleeps, and Linux writes no remaining time
            //   back for it; the guest's timeout already holds the remaining
            //   time written above.
            // - A plain injection, not a tail injection, so the rest of this
            //   handler and Detcore's post-syscall work still run.
            drop(_guard);
            // No room for the cells cannot happen in practice: this call's
            // probe cells, at least as large, were committed in the same stack
            // scratch above. Should it happen, the signal stays pending under
            // the guest's own mask and the call returns EINTR without its
            // handler run, as before this delivery existed.
            match stage_private_cells(guest, PrivateCells::MaskAndZeroTimeout(mask), &[]).await? {
                Some(staged) => {
                    inject_signal_delivery(guest, staged.mask(), staged.after_mask()).await;
                    staged.release(guest);
                }
                None => tracing::warn!(
                    "[tid {}] no room to deliver a pselect6 signal under its temporary mask; \
                     its handler runs only once the guest unblocks it",
                    guest.tid()
                ),
            }
        }
        refuse_held_signal_loss(guest, Sysno::pselect6, result).await
    }

    fn copy_pselect6_results<G: Guest<Self>>(
        &self,
        guest: &mut G,
        probe: syscalls::Pselect6,
        call: syscalls::Pselect6,
        len: usize,
    ) -> Result<(), Error> {
        copy_pselect6_fd_set(guest, probe.readfds(), call.readfds(), len)?;
        copy_pselect6_fd_set(guest, probe.writefds(), call.writefds(), len)?;
        copy_pselect6_fd_set(guest, probe.exceptfds(), call.exceptfds(), len)
    }

    pub(super) async fn write_pselect6_remaining<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Pselect6,
        deadline: Option<LogicalTime>,
    ) -> Result<(), Error> {
        if let (Some(timeout), Some(deadline)) = (call.timeout(), deadline) {
            let now = thread_observe_time(guest).await;
            let remaining = deadline.as_nanos().saturating_sub(now.as_nanos());
            let remaining = Timespec {
                tv_sec: (remaining / 1_000_000_000) as libc::time_t,
                tv_nsec: (remaining % 1_000_000_000) as libc::c_long,
            };
            // pselect6's timeout is a writable in-out kernel timespec; reverie-syscalls
            // now types it as `AddrMut<Timespec>`, so the remaining time can be written
            // back directly without an unsafe pointer cast.
            if let Err(error) = guest.memory().write_value(timeout, &remaining) {
                // Linux preserves the pselect6 result when remaining-time copyout faults.
                trace!(?error, "ignoring pselect6 timeout writeback failure");
            }
        }
        Ok(())
    }

    /// select syscall (MAYHANG).
    ///
    /// `select` is the classic `timeval` sibling of `pselect6` (which is already
    /// Determinized). It reuses the pselect6 fd-set scratch machinery, but takes
    /// a `struct timeval` timeout (which Linux updates in place with the time not
    /// slept) and carries no signal mask.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#800): Review select determinization mirroring pselect6.
    pub async fn handle_select<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Select,
    ) -> Result<i64, Error> {
        if self.cfg.recordreplay_modes {
            let zero_timeout = call.timeout().is_some_and(|timeout| {
                guest
                    .memory()
                    .read_value(timeout)
                    .is_ok_and(|timeout: libc::timeval| timeout.tv_sec == 0 && timeout.tv_usec == 0)
            });
            return self
                .record_or_replay_select_family(guest, Syscall::Select(call), zero_timeout)
                .await;
        }
        if !self.cfg.sequentialize_threads {
            return Ok(guest.inject(call).await?);
        }

        if call.nfds() < 0 {
            return Ok(guest.inject(call).await?);
        }

        let raw_timeout = match call.timeout() {
            Some(timeout) => {
                let timeout: libc::timeval = guest.memory().read_value(timeout)?;
                Some(timeout)
            }
            None => None,
        };
        if matches!(raw_timeout, Some(timeout) if timeout.tv_sec == 0 && timeout.tv_usec == 0) {
            // A zero timeout is a pure non-blocking poll; the kernel can service it directly,
            // once a virtual timerfd among the sets is readable exactly when its deadline
            // has passed (`crate::syscalls::timerfd`).
            crate::syscalls::timerfd::publish_expired(guest).await;
            return Ok(guest.inject(call).await?);
        }

        // Mirror pselect6: keep large fd tables under kernel ownership rather than
        // over-reading the guest bitmap (Linux clamps raw fd-set copies to max_fds).
        if call.nfds() > PSELECT6_INTERNAL_MAX_NFDS {
            return self
                .record_or_replay_blocking(guest, Syscall::Select(call))
                .await;
        }

        let timeout = raw_timeout.map(select_timeout_duration).transpose()?;
        self.handle_internal_select(guest, call, timeout).await
    }

    async fn handle_internal_select<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Select,
        timeout: Option<Duration>,
    ) -> Result<i64, Error> {
        let len = pselect6_fd_set_len(call.nfds())?;
        let deadline = match timeout {
            Some(timeout) => Some(thread_observe_time(guest).await + timeout),
            None => None,
        };
        let original_readfds = match read_pselect6_fd_set(guest, call.readfds(), len) {
            Ok(value) => value,
            Err(error) => {
                self.write_select_remaining(guest, call, deadline).await?;
                return Err(error);
            }
        };
        let original_writefds = match read_pselect6_fd_set(guest, call.writefds(), len) {
            Ok(value) => value,
            Err(error) => {
                self.write_select_remaining(guest, call, deadline).await?;
                return Err(error);
            }
        };
        let original_exceptfds = match read_pselect6_fd_set(guest, call.exceptfds(), len) {
            Ok(value) => value,
            Err(error) => {
                self.write_select_remaining(guest, call, deadline).await?;
                return Err(error);
            }
        };

        let mut stack = guest.stack().await;
        let readfds = call.readfds().map(|_| stack.reserve::<libc::fd_set>());
        let writefds = call.writefds().map(|_| stack.reserve::<libc::fd_set>());
        let exceptfds = call.exceptfds().map(|_| stack.reserve::<libc::fd_set>());
        // select modifies its timeout in place, so the probe timeout must be a
        // writable scratch cell. It is re-zeroed each iteration to keep every
        // probe a non-blocking poll (a NULL timeout would block indefinitely).
        let probe_timeout = stack.reserve::<libc::timeval>();
        // On a backend whose kernel reports the guest's signal state, only a signal
        // that would end the wait natively does; the mask cell lets the wait block
        // signals under the one scratch-stack guard
        // (https://github.com/rrnewton/hermit/issues/3146). A restart resumes from
        // the remaining time written back below, so a default stop ends the wait
        // as on Linux. Unlike the other waits, it holds no signal until the call
        // returns, so a pending caught `SIGCHLD` ends it as on Linux, whoever
        // sent it, at a turn that host timing can choose: a known gap that this
        // wait had before (`KernelSignalWait::for_select`).
        let mut signals = self
            .cfg
            .backend
            .supports_blocked_wait_signal_interruption
            .then(|| KernelSignalWait::for_select(guest));
        let mask_cell = signals.as_ref().map(|_| stack.reserve::<KernelSigset>());
        // This frame holds the scratch stack across every probe, so the
        // memory that makes an expired timerfd readable is reserved here
        // (`crate::syscalls::timerfd::publish_expired_in`).
        let timerfd_scratch = crate::syscalls::timerfd::reserve_scratch(&mut stack);
        let _guard = stack.commit()?;
        // The scheduler reads the effective mask while the wait parks
        // (`publish_wait_mask`).
        let mut published = PublishedWaitMask::default();
        if let Some(signals) = signals.as_ref() {
            publish_wait_mask(guest, signals.effective_mask(), &mut published).await;
        }
        let probe = call
            .with_readfds(readfds)
            .with_writefds(writefds)
            .with_exceptfds(exceptfds)
            .with_timeout(Some(probe_timeout));

        let mut resources = Resources::new(guest.thread_state().dettid);
        resources.insert(ResourceID::InternalIOPolling, Permission::W);
        resources.fyi("select");
        resources.poll_deadline = poll_deadline_for(guest, deadline);
        // EINTR records select's interruption result. The scheduler only wakes
        // ERESTARTSYS requests: blocked and ignored signals still need a target-
        // side disposition check before this Signaled path can be used.
        resources.set_signal_interrupt_errno(Errno::EINTR);

        // Every exit, including a `?` on a guest-memory write, must reach the mask
        // restore below, so the loop runs in its own async block.
        let result: Result<i64, Error> = {
            // Moved reborrows: a future holding a shared reference to a scratch
            // address would not be `Send`.
            let guest = &mut *guest;
            let signals = &mut signals;
            let resources = &mut resources;
            let published = &mut published;
            async move {
                loop {
                    let signaled = matches!(
                        resource_request(guest, resources.clone()).await,
                        ResumeStatus::Signaled(_)
                    );
                    // Virtual timerfds first (`crate::syscalls::timerfd`).
                    let next_timerfd =
                        crate::syscalls::timerfd::publish_expired_in(guest, Some(timerfd_scratch))
                            .await;
                    resources.poll_deadline =
                        crate::syscalls::timerfd::poll_deadline_with(guest, deadline, next_timerfd);
                    // A pending real-time timer expiration that this wait would
                    // be ended by (https://github.com/rrnewton/hermit/issues/4089).
                    let pending = pending_real_time_among(guest, REAL_TIME_SIGNALS).await;
                    if pending != 0
                        && let Some(signals) = signals.as_ref()
                    {
                        let observable = signals.observable_now()?;
                        act_on_pending_real_time(guest, pending, observable, 0).await;
                    }
                    // Every turn probes before it looks at pending signals, as Linux
                    // does: do_select polls every descriptor on each pass and returns
                    // the count of ready ones before it tests signal_pending, and at
                    // the start max_select_fd returns EBADF for a descriptor that is
                    // not open (`core_sys_select`). So a descriptor that becomes ready
                    // in a later turn in which a signal is also pending is reported
                    // (round-12 Medium on https://github.com/rrnewton/hermit/pull/3361).
                    // Each probe is a new select call, so a descriptor closed during
                    // the wait gets EBADF, where Linux reports it ready. Without the
                    // kernel's signal state, a scheduler `Signaled` answer still ends
                    // the call before the probe.
                    if signals.is_none() && signaled {
                        self.write_select_remaining(guest, call, deadline).await?;
                        return Err(Errno::EINTR.into());
                    }
                    guest.memory().write_value(
                        probe_timeout,
                        &libc::timeval {
                            tv_sec: 0,
                            tv_usec: 0,
                        },
                    )?;
                    write_pselect6_fd_set(guest, probe.readfds(), &original_readfds)?;
                    write_pselect6_fd_set(guest, probe.writefds(), &original_writefds)?;
                    write_pselect6_fd_set(guest, probe.exceptfds(), &original_exceptfds)?;

                    let result = match signals.as_mut() {
                        // A signal stop that does not end the wait is absorbed (see
                        // `KernelSignalWait`). Every probe runs before the turn's check
                        // (`inject_first_probe`): a signal that would end the wait and
                        // stops the probe is held, the probe runs again, and the signal
                        // is delivered as the call returns. Once `block` has blocked
                        // every blockable signal, a pending one cannot stop the probe.
                        Some(signals) => match signals.inject_first_probe(guest, probe).await {
                            Ok(result) => result,
                            Err(error) => {
                                self.write_select_remaining(guest, call, deadline).await?;
                                break Err(error);
                            }
                        },
                        None => guest.inject(probe).await,
                    };
                    if result != Ok(0) {
                        let copy_result = if result.is_ok() {
                            self.copy_select_results(guest, probe, call, len)
                        } else {
                            Ok(())
                        };
                        self.write_select_remaining(guest, call, deadline).await?;
                        copy_result?;
                        break result.map_err(Into::into);
                    }
                    // The turn's check, after its probe found no descriptor ready and no
                    // error: only then does a pending signal end the call. A scheduler
                    // `Signaled` answer is only a hint here; the kernel's state decides.
                    // select returns ERESTARTNOHAND: EINTR after a handler, a restart
                    // with the remaining timeout after a stop.
                    if let Some(signals) = signals.as_ref() {
                        match signals.interrupted_with_state() {
                            Ok((false, _)) => {}
                            Ok((true, _)) => {
                                self.write_select_remaining(guest, call, deadline).await?;
                                break Err(Errno::ERESTARTNOHAND.into());
                            }
                            Err(error) => break Err(error),
                        }
                    }
                    if let Some(signals) = signals.as_mut()
                        && signals.needs_block()
                        && let Err(error) = signals.block(guest, mask_cell).await
                    {
                        self.write_select_remaining(guest, call, deadline).await?;
                        break Err(error);
                    }
                    if let Some(signals) = signals.as_ref() {
                        publish_wait_mask(guest, signals.effective_mask(), published).await;
                    }

                    resources.poll_attempt += 1;
                    if let Some(deadline) = deadline
                        && thread_observe_time(guest).await >= deadline
                    {
                        let copy_result = self.copy_select_results(guest, probe, call, len);
                        self.write_select_remaining(guest, call, Some(deadline))
                            .await?;
                        copy_result?;
                        break Ok(0);
                    }
                    trace!(
                        "Retry #{} for syscall due to result Ok(0): {}",
                        resources.poll_attempt,
                        probe.display(&guest.memory())
                    );
                    record_retry_event(guest, probe).await;
                }
            }
            .await
        };
        let result = match &mut signals {
            Some(signals) => {
                let restored = signals.restore(guest, mask_cell).await;
                publish_wait_mask(guest, None, &mut published).await;
                result_after_restore(restored, result)?
            }
            None => result,
        };
        refuse_held_signal_loss(guest, Sysno::select, result).await
    }

    fn copy_select_results<G: Guest<Self>>(
        &self,
        guest: &mut G,
        probe: syscalls::Select,
        call: syscalls::Select,
        len: usize,
    ) -> Result<(), Error> {
        copy_pselect6_fd_set(guest, probe.readfds(), call.readfds(), len)?;
        copy_pselect6_fd_set(guest, probe.writefds(), call.writefds(), len)?;
        copy_pselect6_fd_set(guest, probe.exceptfds(), call.exceptfds(), len)
    }

    pub(super) async fn write_select_remaining<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Select,
        deadline: Option<LogicalTime>,
    ) -> Result<(), Error> {
        if let (Some(timeout), Some(deadline)) = (call.timeout(), deadline) {
            let now = thread_observe_time(guest).await;
            let remaining = deadline.as_nanos().saturating_sub(now.as_nanos());
            let remaining = libc::timeval {
                tv_sec: (remaining / 1_000_000_000) as libc::time_t,
                tv_usec: ((remaining % 1_000_000_000) / 1_000) as libc::suseconds_t,
            };
            // select's timeout is a writable in-out kernel timeval reporting the
            // time not slept; derive it from deterministic virtual time.
            if let Err(error) = guest.memory().write_value(timeout, &remaining) {
                // Linux preserves the select result when remaining-time copyout faults.
                trace!(?error, "ignoring select timeout writeback failure");
            }
        }
        Ok(())
    }

    /// ppoll syscall (MAYHANG)
    // TODO-HUMAN-REVIEW(PR-273)
    pub async fn handle_ppoll<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Ppoll,
    ) -> Result<i64, Error> {
        let timeout_address = call.timeout();
        let timeout = match timeout_address {
            Some(timeout) => Some(ppoll_timeout_duration(guest.memory().read_value(timeout)?)?),
            None => None,
        };

        let result: Result<i64, Error> = if timeout == Some(Duration::ZERO) {
            if self.cfg.recordreplay_modes {
                resource_request(guest, Resources::new(guest.thread_state().dettid)).await;
            } else {
                // A virtual timerfd among the fds is readable exactly when its
                // deadline has passed (`crate::syscalls::timerfd`); before the
                // probe's copies take the scratch stack.
                crate::syscalls::timerfd::publish_expired(guest).await;
            }
            // A virtual timerfd's expiration is published first, so the
            // mask's read is not the call's first step.
            let (probe, staged) = match self.prepare_ppoll_probe(guest, call, false).await? {
                Some((probe, staged)) => (probe, Some(staged)),
                None => {
                    if let Some(refused) = self.refuse_unstageable_ppoll_copy(guest).await {
                        return refused;
                    }
                    // Runs on with the guest's own zero timeout and mask.
                    (call, None)
                }
            };
            // The mask the probe installs, read at entry, for replay to deliver
            // under (https://github.com/rrnewton/hermit/issues/4054).
            let temporary_mask = if self.cfg.replaying {
                match call.sigmask() {
                    Some(signal_mask) => read_kernel_sigset(guest, signal_mask)
                        .await
                        .ok()
                        .map(sanitize_ppoll_signal_mask),
                    None => None,
                }
            } else {
                None
            };
            if let Some(mask) = temporary_mask {
                super::signal_shadow::note_wait_mask(guest, mask);
            }
            // No `?` on the call's own result: an interruption must reach the
            // replay delivery below.
            let result: Result<i64, Error> = if self.cfg.recordreplay_modes {
                self.record_or_replay(guest, probe)
                    .await
                    .map_err(Into::into)
            } else {
                guest.inject_with_retry(probe).await.map_err(Into::into)
            };
            if let Some(staged) = staged {
                staged.release(guest);
            }
            // Replay serves the probe from the recording, so the kernel never
            // installs its mask; deliver a recorded interruption under it, as
            // for pselect6.
            if let Some(mask) = temporary_mask
                && matches!(result, Err(Error::Errno(Errno::ERESTARTNOHAND)))
            {
                end_replayed_pselect6_under_its_temporary_mask(guest, mask).await;
            }
            // Linux does not write back an initially zero timeout. Besides matching the
            // kernel, omitting this write matters when the timeout aliases the pollfd array:
            // the injected probe may have just stored revents in those same bytes.
            result
        } else if ppoll_uses_kernel_wait(
            self.cfg.sequentialize_threads,
            self.cfg.recordreplay_modes,
        ) {
            // The kernel owns the blocking wait (`ppoll_uses_kernel_wait`). Use
            // a private copy only for the signal mask so raw ppoll can still
            // update the guest timeout.
            let mut staged = None;
            // The mask the real call sleeps under, recorded for the scheduler's
            // SIGCHLD target choice (`Resources::blocked_signal_mask`).
            let mut blocked_signal_mask = None;
            // The mask read at entry, for replay to deliver under.
            let mut temporary_mask = None;
            let call = if let Some(signal_mask) = call.sigmask() {
                if call.sigsetsize() != KERNEL_SIGSET_SIZE {
                    return Err(Errno::EINVAL.into());
                }
                let signal_mask = crate::syscalls::signal::restart_if_first_probe_not_run(
                    read_kernel_sigset(guest, signal_mask).await,
                )?;
                super::signal_shadow::note_wait_mask(guest, signal_mask);
                let signal_mask = sanitize_ppoll_signal_mask(signal_mask);
                blocked_signal_mask = Some(kernel_installed_signal_mask(signal_mask));
                temporary_mask = Some(signal_mask);
                // Linux writes revents into the pollfd array and the time left
                // into the timeout, so the copy overlaps neither.
                let live: Vec<std::ops::Range<usize>> = ppoll_fds_range(call)
                    .into_iter()
                    .chain(call.timeout().map(|timeout| {
                        let start = timeout.as_raw();
                        start..start.saturating_add(std::mem::size_of::<Timespec>())
                    }))
                    .collect();
                match stage_private_cells(guest, PrivateCells::Mask(signal_mask), &live).await? {
                    Some(cells) => {
                        let mask = cells.mask();
                        staged = Some(cells);
                        call.with_sigmask(Addr::from_raw(mask))
                    }
                    None => {
                        if let Some(refused) = self.refuse_unstageable_ppoll_copy(guest).await {
                            return refused;
                        }
                        // Runs on with the guest's own mask. A sibling may
                        // change it while the call waits, which a masked
                        // record/replay wait already allows for: its sleeping
                        // mask is recorded as unknown.
                        call
                    }
                }
            } else {
                call
            };
            // A masked record/replay wait sleeps under a temporary mask the
            // scheduler records as unknown, as a masked pselect6's does
            // (`Resources::blocked_signal_mask_unknown`), although Hermit knows
            // it exactly. A known mask would let the scheduler wait for the
            // kernel to install it, and replay serves the call from the
            // recording without the kernel ever doing so; recording and replay
            // must schedule the call alike. The result has no `?`: an
            // interruption must reach the replay delivery below.
            let result: Result<i64, Error> =
                if self.cfg.recordreplay_modes && temporary_mask.is_some() {
                    self.record_or_replay_blocking_with_unknown_mask(guest, Syscall::Ppoll(call))
                        .await
                } else {
                    self.record_or_replay_blocking_with_mask(
                        guest,
                        Syscall::Ppoll(call),
                        blocked_signal_mask,
                    )
                    .await
                };
            if let Some(staged) = staged {
                staged.release(guest);
            }
            // Replay serves the call from the recording, so the kernel never
            // installs its mask; deliver a recorded interruption under it
            // (https://github.com/rrnewton/hermit/issues/3992).
            if self.cfg.replaying
                && let Some(mask) = temporary_mask
                && matches!(result, Err(Error::Errno(Errno::ERESTARTNOHAND)))
            {
                end_replayed_pselect6_under_its_temporary_mask(guest, mask).await;
            }
            result
        } else {
            self.handle_internal_ppoll(guest, call, timeout).await
        };

        result
    }

    /// The zero-timeout probe for `call`: it reads a private zero timeout,
    /// and a private, sanitized copy of the call's mask, staged by
    /// `stage_private_cells` clear of the pollfd array that Linux writes. So
    /// neither a sibling's write nor the guest's own mask can make the probe
    /// sleep or block Hermit's timer signal. `None` when the copies have
    /// nowhere to go (`refuse_unstageable_ppoll_copy`). `first` says the
    /// mask's read is the call's first injection, so a signal that stops it
    /// restarts the call (`restart_if_first_probe_not_run`).
    async fn prepare_ppoll_probe<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Ppoll,
        first: bool,
    ) -> Result<
        Option<(
            syscalls::Ppoll,
            StagedCells<<G::Stack as Stack>::StackGuard>,
        )>,
        Error,
    > {
        let signal_mask = match call.sigmask() {
            Some(signal_mask) => {
                if call.sigsetsize() != KERNEL_SIGSET_SIZE {
                    return Err(Errno::EINVAL.into());
                }
                let read = read_kernel_sigset(guest, signal_mask).await;
                let signal_mask = if first {
                    crate::syscalls::signal::restart_if_first_probe_not_run(read)?
                } else {
                    read?
                };
                super::signal_shadow::note_wait_mask(guest, signal_mask);
                // The probe's temporary mask lets the kernel take a pending
                // real-time timer expiration that every thread blocked when it
                // expired (https://github.com/rrnewton/hermit/issues/4089).
                observe_under_mask(guest, signal_mask).await?;
                Some(sanitize_ppoll_signal_mask(signal_mask))
            }
            None => None,
        };
        let cells = match signal_mask {
            Some(signal_mask) => PrivateCells::MaskAndZeroTimeout(signal_mask),
            None => PrivateCells::ZeroTimeout,
        };
        let live: Vec<std::ops::Range<usize>> = ppoll_fds_range(call).into_iter().collect();
        let Some(staged) = stage_private_cells(guest, cells, &live).await? else {
            return Ok(None);
        };
        let probe = match signal_mask {
            Some(_) => call
                .with_timeout(AddrMut::from_raw(staged.after_mask()))
                .with_sigmask(Addr::from_raw(staged.mask())),
            None => call.with_timeout(AddrMut::from_raw(staged.at)),
        };
        Ok(Some((probe, staged)))
    }

    /// Refuses a ppoll whose private copies have nowhere to go
    /// (`stage_private_cells`): a zero-timeout probe's zero timeout and mask,
    /// or the mask of a call the kernel waits in. The stack has no writable
    /// scratch below its red zone, and every red-zone window overlaps the
    /// pollfd array or, for a kernel wait, the timeout
    /// (https://github.com/rrnewton/hermit/issues/4113). A fail-closed run
    /// stops with the policy refusal and this message, whose result this
    /// returns. A run started with `--allow-unsupported-syscalls` runs on with
    /// the guest's own timeout and mask, and this returns `None`.
    async fn refuse_unstageable_ppoll_copy<G: Guest<Self>>(
        &self,
        guest: &mut G,
    ) -> Option<Result<i64, Error>> {
        use std::io::Write as _;
        let message = "unsupported: ppoll on a stack with no room below its red zone whose \
            red zone the call's pollfd array and timeout fill: Hermit has nowhere to put its \
            private copy of the timeout and the signal mask \
            (https://github.com/rrnewton/hermit/issues/4113)";
        tracing::error!("[tid {}] {message}", guest.tid());
        if !self.cfg.panic_on_unsupported_syscalls {
            return None;
        }
        let _ = writeln!(crate::util::RetryingStderr, "{message}");
        Some(
            self.refuse_unserviceable_operation(guest, Sysno::ppoll, Errno::EFAULT)
                .await,
        )
    }

    /// Handle a guest-internal `ppoll` using zero-time kernel probes.
    async fn handle_internal_ppoll<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Ppoll,
        timeout: Option<Duration>,
    ) -> Result<i64, Error> {
        debug_assert_ne!(timeout, Some(Duration::ZERO));
        let timeout_address = call.timeout();
        let started_at = if timeout.is_some() {
            Some(thread_observe_time(guest).await)
        } else {
            None
        };
        let deadline = match (timeout, started_at) {
            (Some(duration), Some(started_at)) => Some(started_at + duration),
            (None, None) => None,
            _ => unreachable!(),
        };

        // Every zero-timeout probe carries a private copy of the call's temporary
        // mask, so the kernel applies it atomically, as pselect6's probes do
        // (`handle_internal_pselect6`). A pending signal that the mask lets in
        // ends a probe with ERESTARTNOHAND at that probe's scheduler point, and
        // the kernel delivers it under the temporary mask and only then puts the
        // thread's own mask back, as Linux does for a ppoll that a signal
        // interrupts. That probe is the last injection before the guest
        // resumes, so nothing disturbs the pending restore
        // (https://github.com/rrnewton/reverie/issues/1003). A ppoll that would
        // block used to return ENOSYS here
        // (https://github.com/rrnewton/hermit/issues/4042).
        if call.sigmask().is_some() {
            // Only the virtual clock was read before: the mask's read is the
            // call's first injection.
            let Some((probe, staged)) = self.prepare_ppoll_probe(guest, call, true).await? else {
                // The guest's own mask could block Hermit's timer signal while
                // the call is parked, so a run that allows unsupported
                // operations gets the same answer as a mask it cannot honor.
                return match self.refuse_unstageable_ppoll_copy(guest).await {
                    Some(refused) => refused,
                    None => Err(Errno::ENOSYS.into()),
                };
            };
            let result = if self.cfg.backend.supports_blocked_wait_signal_interruption {
                self.wait_on_masked_ppoll_probe(
                    guest,
                    probe,
                    staged.mask(),
                    staged.timerfd_scratch,
                    deadline,
                )
                .await
            } else {
                // Without the kernel's signal state, Hermit cannot tell whether
                // the backend holds a signal that should end the wait, so a
                // ppoll that would block still fails closed. An expired
                // timerfd on virtual time is made readable first, in the
                // staged cells' scratch (`crate::syscalls::timerfd`).
                crate::syscalls::timerfd::publish_expired_in(guest, staged.timerfd_scratch).await;
                let result = guest.inject_with_retry(probe).await;
                if probe.syscall_would_have_blocked(result) {
                    Err(Errno::ENOSYS.into())
                } else {
                    result.map_err(Into::into)
                }
            };
            // Before the time left is written: the copies' red-zone window may
            // cover the guest's timeout, which Linux never sees.
            staged.release(guest);
            if let (Some(timeout_address), Some(timeout), Some(started_at)) =
                (timeout_address, timeout, started_at)
            {
                self.write_ppoll_remaining(guest, timeout_address, timeout, started_at)
                    .await?;
            }
            return result;
        }

        let mut rsrc = Resources::new(guest.thread_state().dettid);
        rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
        rsrc.fyi("ppoll");
        let result = retry_nonblocking_syscall_with_timeout(guest, call, rsrc, deadline).await;
        if let (Some(timeout_address), Some(timeout), Some(started_at)) =
            (timeout_address, timeout, started_at)
        {
            self.write_ppoll_remaining(guest, timeout_address, timeout, started_at)
                .await?;
        }
        result
    }

    /// The wait of a ppoll with a temporary mask under sequentialized threads:
    /// `probe`, a zero-timeout copy of the call that reads the mask staged at
    /// `mask` (`prepare_ppoll_probe`), runs once per scheduler turn until it
    /// reports a ready descriptor or an error, until a signal that the mask
    /// lets in ends the wait, or until `deadline` passes, which returns 0.
    ///
    /// The probe carries the mask, so the kernel ends a probe with
    /// ERESTARTNOHAND for a pending signal the mask lets in, and delivers it
    /// under the mask as the call returns. A signal that the guest's own mask
    /// lets in can instead stop a probe before the kernel installs the mask;
    /// the backend then holds it, and `KernelSignalWait` decides against the
    /// call's mask whether it ends the wait (`with_wait_mask`). Without that,
    /// the wait ran on to its deadline, or forever, with the signal held. The
    /// backend delivers a held signal under the guest's own mask
    /// (https://github.com/rrnewton/reverie/issues/1011). A signal that only
    /// the mask lets in can be pending at a turn's check without having ended
    /// that turn's probe only if it arrived from outside the guest between
    /// the two; the next probe, which carries the mask, ends the call for it
    /// and the kernel delivers it under the mask. The wait never blocks
    /// signals (`KernelSignalWait::block`): a probe that a signal ended would
    /// then leave the all-blocked mask to be restored.
    ///
    /// Every probe runs before the turn's check, as Linux's do_poll polls every
    /// descriptor before it tests for a pending signal. The wait holds no
    /// signal until the call returns (`KernelSignalWait::for_select`): the
    /// kernel ends a probe for any pending signal the mask lets in, `SIGCHLD`
    /// included, so a `SIGCHLD` the kernel posts for a child ends it at a turn
    /// that host timing chooses, the known gap that select and pselect6 waits
    /// share (https://github.com/rrnewton/hermit/issues/3146).
    async fn wait_on_masked_ppoll_probe<G: Guest<Self>>(
        &self,
        guest: &mut G,
        probe: syscalls::Ppoll,
        mask: usize,
        timerfd_scratch: Option<crate::syscalls::timerfd::Scratch>,
        deadline: Option<LogicalTime>,
    ) -> Result<i64, Error> {
        let temporary_mask: KernelSigset = guest
            .memory()
            .read_value(Addr::<KernelSigset>::from_raw(mask).ok_or(Errno::EFAULT)?)?;
        let mut signals = KernelSignalWait::for_select(guest)
            .with_wait_mask(Some(crate::syscalls::signal::kernel_installed_signal_mask(
                temporary_mask,
            )))
            .with_deadline(deadline.is_some());
        let mut rsrc = Resources::new(guest.thread_state().dettid);
        rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
        rsrc.fyi("ppoll");
        rsrc.poll_deadline = poll_deadline_for(guest, deadline);
        // As for pselect6: a scheduler `Signaled` answer wakes the wait, and
        // the probe and the kernel's state decide whether a signal ends it.
        rsrc.set_signal_interrupt_errno(Errno::EINTR);
        // Between probes the kernel holds the guest's own mask, while Linux's
        // `blocked` is the temporary one for the whole call.
        let mut published = PublishedWaitMask::default();
        publish_wait_mask(guest, signals.effective_mask(), &mut published).await;
        let result = loop {
            resource_request(guest, rsrc.clone()).await;
            // Virtual timerfds first (`crate::syscalls::timerfd`), in the
            // staged cells' scratch while they hold the stack.
            let next_timerfd =
                crate::syscalls::timerfd::publish_expired_in(guest, timerfd_scratch).await;
            rsrc.poll_deadline =
                crate::syscalls::timerfd::poll_deadline_with(guest, deadline, next_timerfd);
            // A pending real-time timer expiration that the temporary mask lets
            // the kernel deliver or discard
            // (https://github.com/rrnewton/hermit/issues/4089).
            if let Err(error) = observe_under_mask(guest, temporary_mask).await {
                break Err(error);
            }
            let injected = match signals.inject_first_probe(guest, probe).await {
                Ok(result) => result,
                Err(error) => break Err(error),
            };
            if !probe.syscall_would_have_blocked(injected) {
                break injected.map_err(Into::into);
            }
            match signals.interrupted_with_state() {
                Ok((false, _)) => {}
                // The next probe ends the call for it (see above).
                Ok((true, state))
                    if !signals.holds_signal()
                        && signals.interrupts_only_under_wait_mask(&state) =>
                {
                    continue;
                }
                Ok((true, _)) => break Err(Errno::ERESTARTNOHAND.into()),
                Err(error) => break Err(error),
            }
            rsrc.poll_attempt += 1;
            if let Some(deadline) = deadline
                && thread_observe_time(guest).await >= deadline
            {
                break Ok(0);
            }
            record_retry_event(guest, probe).await;
        };
        publish_wait_mask(guest, None, &mut published).await;
        refuse_held_signal_loss(guest, Sysno::ppoll, result).await
    }

    pub(super) async fn write_ppoll_remaining<G: Guest<Self>>(
        &self,
        guest: &mut G,
        timeout_address: AddrMut<'_, Timespec>,
        timeout: Duration,
        started_at: LogicalTime,
    ) -> Result<(), Error> {
        let now = thread_observe_time(guest).await;
        let elapsed = Duration::from_nanos(now.as_nanos().saturating_sub(started_at.as_nanos()));
        let remaining = timeout.saturating_sub(elapsed);
        if let Err(error) = guest
            .memory()
            .write_value(timeout_address, &timespec_from_duration(remaining))
        {
            // Linux preserves the ppoll result when remaining-time copyout faults.
            trace!(?error, "ignoring ppoll timeout writeback failure");
        }
        Ok(())
    }

    /// Handle a guest-internal poll call that can be fully determinized.
    // TODO-HUMAN-REVIEW(PR-1052): Review scheduler fairness for zero-timeout poll.
    pub async fn handle_internal_poll<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Poll,
    ) -> Result<i64, Error> {
        let timeout_millis = call.timeout();
        if timeout_millis == 0 {
            // A nonblocking poll can still be the synchronization point in a
            // userspace polling loop. Yield once before probing so backends
            // without PMU preemption cannot let that loop starve its producer.
            let yield_to_peer = self.cfg.backend.loopback_pollers_yield_to_peers
                && guest.thread_state().has_loopback_peer();
            resource_request(
                guest,
                zero_timeout_poll_request(guest.thread_state().dettid, yield_to_peer),
            )
            .await;
            crate::syscalls::timerfd::publish_expired(guest).await;
            Ok(guest.inject(call).await?) // Already non-blocking.
        } else {
            let maybe_timeout_ns = millis_duration_to_absolute_timeout(guest, timeout_millis).await;
            self.handle_internal_poll_until(guest, call, maybe_timeout_ns)
                .await
        }
    }

    /// The blocking half of `handle_internal_poll`, waiting until `deadline`. A
    /// `restart_syscall` resumes an interrupted `poll` here with the deadline the
    /// wait kept, as Linux's `do_restart_poll` does
    /// (`Detcore::handle_restart_syscall`).
    pub(crate) async fn handle_internal_poll_until<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Poll,
        deadline: Option<LogicalTime>,
    ) -> Result<i64, Error> {
        let mut rsrc = Resources::new(guest.thread_state().dettid);
        rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
        rsrc.fyi("poll");
        let result = retry_nonblocking_syscall_with_timeout(guest, call, rsrc, deadline).await;
        keep_restart_block(guest, &result, RestartCall::Poll(call), deadline).await;
        result
    }

    /// Handle a poll syscall that deponds on external, nondeterminstic IO.
    pub async fn handle_external_poll<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Poll,
    ) -> Result<i64, Error> {
        let len = call.nfds();
        let time_delta = Duration::from_millis(call.timeout() as u64);

        if len == 0 && time_delta.is_zero() {
            let request = Self::sleep_request(guest, time_delta).await;
            resource_request(guest, request).await;
            Ok(0)
        } else {
            print_poll(&call);
            Ok(self
                .record_or_replay_blocking(guest, Syscall::Poll(call))
                .await?)
        }
    }

    /// epoll_create1 syscall
    pub async fn handle_epoll_create1<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollCreate1,
    ) -> Result<i64, Error> {
        let dettid = guest.thread_state().dettid;
        resource_request(guest, Resources::new(dettid)).await; // empty request
        let fd = self.record_or_replay(guest, call).await? as RawFd;
        // Register the epoll fd in the DetFd table like every other
        // fd-creating syscall (openat, eventfd2, pipe2, socket, ...). Without
        // this, later operations that consult the table via `with_detfd` /
        // `dup_fd` (F_GETFL, F_SETFD, F_DUPFD[_CLOEXEC], dup, ...) would fail
        // with EBADF even though the underlying kernel fd is valid. This broke,
        // for example, running rustup proxies (cargo/rustc) under hermit, whose
        // tokio runtime dups its epoll fd at startup.
        //
        // EPOLL_CLOEXEC shares the same bit value as O_CLOEXEC, so we can carry
        // the cloexec flag straight across.
        self.add_fd(
            guest,
            fd,
            OFlag::from_bits_truncate(call.flags().bits()),
            FdType::Epoll,
        )
        .await?;
        Ok(fd as i64)
    }

    /// Apply an ADD, MOD, or DEL mutation to an epoll interest list.
    ///
    /// Determinism: strict execution serializes this mutation, so its result depends only on the
    /// operation, event payload, and deterministically reconstructed epoll/file-descriptor state.
    /// Record/replay records each answer and replays it; replay reapplies only the recorded
    /// successes, so its epoll sets hold what the recording's held
    /// (<https://github.com/rrnewton/hermit/issues/31>).
    pub async fn handle_epoll_ctl<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollCtl,
    ) -> Result<i64, Error> {
        let dettid = guest.thread_state().dettid;
        resource_request(guest, Resources::new(dettid)).await; // empty request
        Ok(self.record_or_replay(guest, call).await?)
    }

    /// epoll_pwait syscall (MAYHANG)
    pub async fn handle_epoll_pwait<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollPwait,
    ) -> Result<i64, Error> {
        // This used to unconditionally inject the raw
        // call and wait for it to return. With an infinite timeout under
        // `--sequentialize-threads` that DEADLOCKS the whole guest: the calling
        // task holds the scheduler turn while blocked in the kernel, and the
        // only task that could ever satisfy the wait is sitting in the run queue
        // waiting for a turn that never comes. Observed as `cmake` configure
        // hanging forever at zero CPU with a grandchild frozen mid-`openat`;
        // hermit's own scheduler log ends at `COMMIT turn N, dettid <parent>`
        // injecting `epoll_pwait(..., -1, NULL, 8)` with `queue len 2`.
        //
        // WHO ACTUALLY REACHES THIS, measured rather than assumed. It is NOT
        // glibc's `epoll_wait(2)` on this architecture: glibc calls
        // `SYS_epoll_pwait` only where `__NR_epoll_wait` does not exist (arm64
        // and friends). x86_64 has it, and `strace` on glibc 2.34/x86_64 shows
        // a plain `epoll_wait` syscall, which `handle_epoll_wait` has always
        // handled correctly. The callers that land here are programs issuing
        // `epoll_pwait` DIRECTLY -- libuv does, which is how the original
        // `cmake` hang was found. With a NULL sigmask the two calls are
        // semantically identical, so route them together.
        //
        // A NON-NULL sigmask swaps the signal mask atomically for the duration
        // of the wait. Under record and replay a masked call that may wait is
        // a kernel wait outside the turn (`record_or_replay_masked_epoll_pwait`).
        // A sequentialized run on a backend whose kernel reports the guest's
        // signal state polls it under that mask
        // (`handle_internal_masked_epoll_pwait`). Otherwise, and for a zero
        // timeout, it keeps its turn: on another backend, or without
        // sequentialized threads, such a call can still block the scheduler
        // (https://github.com/rrnewton/hermit/issues/4041), and because it
        // never sleeps outside the runnable set the scheduler needs no record
        // of its temporary mask (`Resources::blocked_signal_mask`).
        if call.sigmask().is_some() && self.cfg.recordreplay_modes && call.timeout() != 0 {
            return self.record_or_replay_masked_epoll_pwait(guest, call).await;
        }
        if call.sigmask().is_some()
            && call.timeout() != 0
            && self.cfg.sequentialize_threads
            && self.cfg.backend.supports_blocked_wait_signal_interruption
        {
            return self.handle_internal_masked_epoll_pwait(guest, call).await;
        }
        if call.sigmask().is_some() {
            self.observe_under_temporary_mask(guest, call.sigmask(), call.sigsetsize())
                .await?;
            let dettid = guest.thread_state().dettid;
            resource_request(guest, Resources::new(dettid)).await; // empty request
            // The raw call runs in this turn. A zero timeout only probes, once a
            // virtual timerfd is readable exactly when its deadline has passed;
            // any other waits in the host (`crate::syscalls::timerfd`).
            if call.timeout() == 0 {
                crate::syscalls::timerfd::publish_expired(guest).await;
            } else {
                crate::syscalls::timerfd::refuse_host_wait(guest, "epoll_pwait with a signal mask");
            }
            return Ok(self.record_or_replay(guest, call).await?);
        }
        if self.cfg.recordreplay_modes && call.timeout() == 0 {
            // Cannot block, but still yield a scheduler turn so a polling thread
            // cannot monopolize the guest between preemptions.
            resource_request(guest, Resources::new(guest.thread_state().dettid)).await;
            Ok(self.record_or_replay(guest, call).await?)
        } else if !self.cfg.sequentialize_threads || self.cfg.recordreplay_modes {
            Ok(self
                .record_or_replay_blocking(guest, Syscall::EpollPwait(call))
                .await?)
        } else {
            self.handle_internal_epoll_pwait(guest, call).await
        }
    }

    /// A masked `epoll_pwait` that may wait, under sequentialized threads on a
    /// backend whose kernel reports the guest's signal state. It used to run
    /// the real call in the thread's turn, so nothing else ran while it waited
    /// and it returned 0 at once or at its timeout
    /// (https://github.com/rrnewton/hermit/issues/4041). It now polls, as an
    /// unmasked `epoll_pwait` does, with probes that carry no mask, and decides
    /// interruption against the temporary mask read at entry
    /// (`retry_blocking_wait_under_mask`): a signal the mask blocks never ends
    /// the call and is delivered after it, and one the mask lets in ends it
    /// with EINTR although the guest's own mask may block it. A zero-timeout
    /// `epoll_pwait` never looks at pending signals, so no probe could be ended
    /// by one in any case. A signal that only the temporary mask lets in stays
    /// pending under the guest's own mask once it is put back, and is delivered
    /// under the temporary mask (`inject_signal_delivery`), as for a masked
    /// pselect6. Linux checks the mask's size, then reads it, before it looks
    /// at the epoll descriptor.
    async fn handle_internal_masked_epoll_pwait<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollPwait,
    ) -> Result<i64, Error> {
        if call.sigsetsize() != KERNEL_SIGSET_SIZE {
            return Err(Errno::EINVAL.into());
        }
        let mask_address = call.sigmask().ok_or(Errno::EFAULT)?;
        let mask = crate::syscalls::signal::restart_if_first_probe_not_run(
            read_kernel_sigset(guest, mask_address.cast()).await,
        )?;
        super::signal_shadow::note_wait_mask(guest, mask);
        // A pending real-time timer expiration that the mask lets the kernel
        // deliver or discard (https://github.com/rrnewton/hermit/issues/4089).
        observe_under_mask(guest, mask).await?;
        let mask = sanitize_ppoll_signal_mask(mask);
        let deadline = millis_duration_to_absolute_timeout(guest, call.timeout()).await;
        self.wait_on_masked_epoll(
            guest,
            call.with_sigmask(None),
            mask,
            deadline,
            "epoll_pwait",
        )
        .await
    }

    /// The wait of a masked epoll_pwait or epoll_pwait2 under sequentialized
    /// threads, once the call's temporary `mask` (sanitized) has been read at
    /// entry: `probe`, an unmasked epoll_pwait, polls until an event, an
    /// error, a signal that `mask` lets in (EINTR), or `deadline`, which
    /// returns 0 (`handle_internal_masked_epoll_pwait`).
    async fn wait_on_masked_epoll<G: Guest<Self>>(
        &self,
        guest: &mut G,
        probe: syscalls::EpollPwait,
        mask: KernelSigset,
        deadline: Option<LogicalTime>,
        fyi: &'static str,
    ) -> Result<i64, Error> {
        let mut rsrc = Resources::new(guest.thread_state().dettid);
        rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
        rsrc.fyi(fyi);
        let (result, deliver) = retry_blocking_wait_under_mask(
            guest,
            probe,
            rsrc,
            // Both calls return 0 at their timeout.
            deadline.map(|deadline| (deadline, Ok(0))),
            Some(crate::syscalls::signal::kernel_installed_signal_mask(mask)),
        )
        .await;
        if deliver && matches!(result, Err(Error::Errno(Errno::EINTR))) {
            // The probes staged nothing, so they say nothing about room for
            // these 24 bytes. `stage_private_cells`, which tests a window by
            // reading it, finds none only on a stack whose pointer is less
            // than 24 bytes above unreadable memory and whose red zone is
            // unreadable too
            // (https://github.com/rrnewton/hermit/issues/4113). Then the signal
            // stays pending under the guest's own mask, and the call returns
            // EINTR without its handler run and logs this warning.
            match stage_private_cells(guest, PrivateCells::MaskAndZeroTimeout(mask), &[]).await? {
                Some(staged) => {
                    inject_signal_delivery(guest, staged.mask(), staged.after_mask()).await;
                    staged.release(guest);
                }
                None => tracing::warn!(
                    "[tid {}] no room to deliver an {fyi} signal under its temporary mask; its \
                     handler runs only once the guest unblocks it",
                    guest.tid()
                ),
            }
        }
        result
    }

    /// A masked `epoll_pwait` that may wait, under record and replay. As for a
    /// masked pselect6 or ppoll there, the kernel owns the wait outside the
    /// scheduler turn, so the timer's signal can end it as on Linux; it kept
    /// its turn before, so nothing else ran and the call returned at its
    /// timeout (https://github.com/rrnewton/hermit/issues/4041). The call sleeps
    /// under a private copy of the mask read at entry, which a sibling cannot
    /// rewrite, and the scheduler records its sleeping mask as unknown
    /// (`Resources::blocked_signal_mask_unknown`). Replay serves the call from
    /// the recording, so for a recorded EINTR it delivers the signal under that
    /// mask (`end_replayed_pselect6_under_its_temporary_mask`).
    async fn record_or_replay_masked_epoll_pwait<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollPwait,
    ) -> Result<i64, Error> {
        if call.sigsetsize() != KERNEL_SIGSET_SIZE {
            return Err(Errno::EINVAL.into());
        }
        let mask_address = call.sigmask().ok_or(Errno::EFAULT)?;
        let mask = crate::syscalls::signal::restart_if_first_probe_not_run(
            read_kernel_sigset(guest, mask_address.cast()).await,
        )?;
        super::signal_shadow::note_wait_mask(guest, mask);
        // The copy may go in the red zone on a stack with no scratch below it,
        // clear of the events buffer the kernel writes (`stage_private_cells`;
        // re-review of https://github.com/rrnewton/hermit/pull/4087).
        let live: Vec<std::ops::Range<usize>> = call
            .events()
            .map(|events| {
                let start = events.as_raw();
                let len = usize::try_from(call.maxevents().max(0)).unwrap_or(0)
                    * std::mem::size_of::<libc::epoll_event>();
                start..start.saturating_add(len)
            })
            .into_iter()
            .collect();
        let Some(staged) = stage_private_cells(guest, PrivateCells::Mask(mask), &live).await?
        else {
            return self.refuse_unstageable_epoll_pwait_mask(guest, call).await;
        };
        let result = self
            .record_or_replay_blocking_with_unknown_mask(
                guest,
                Syscall::EpollPwait(call.with_sigmask(Addr::from_raw(staged.mask()))),
            )
            .await;
        staged.release(guest);
        if self.cfg.replaying && matches!(result, Err(Error::Errno(Errno::EINTR))) {
            end_replayed_pselect6_under_its_temporary_mask(guest, mask).await;
        }
        result
    }

    /// Refuses a masked record/replay `epoll_pwait` whose private mask copy has
    /// nowhere to go: no stack scratch below the red zone, and every red-zone
    /// window overlaps the events buffer (`refuse_unstageable_pselect6_mask`
    /// for pselect6). A run that allows unsupported operations forwards the
    /// guest's own mask.
    async fn refuse_unstageable_epoll_pwait_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollPwait,
    ) -> Result<i64, Error> {
        use std::io::Write as _;
        let message = "unsupported: epoll_pwait with a temporary signal mask, on a stack \
            with no room below its red zone whose red zone the call's events buffer fills: \
            Hermit has nowhere to put the private copy of the mask \
            (https://github.com/rrnewton/hermit/pull/4087)";
        tracing::error!("[tid {}] {message}", guest.tid());
        if self.cfg.panic_on_unsupported_syscalls {
            let _ = writeln!(crate::util::RetryingStderr, "{message}");
            return self
                .refuse_unserviceable_operation(guest, Sysno::epoll_pwait, Errno::EFAULT)
                .await;
        }
        self.record_or_replay_blocking_with_unknown_mask(guest, Syscall::EpollPwait(call))
            .await
    }

    /// Handle a guest-internal `epoll_pwait` (NULL sigmask) that can be fully
    /// determinized. Mirrors `handle_internal_epoll_wait`.
    pub async fn handle_internal_epoll_pwait<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollPwait,
    ) -> Result<i64, Error> {
        let timeout_millis = call.timeout();
        if timeout_millis == 0 {
            // Cannot block, but must still yield a scheduler turn: a
            // zero-timeout polling loop that never requests a resource can
            // monopolize the guest between preemptions and starve the producer
            // it is polling for. `handle_poll` takes a turn for every
            // sequential mode, and the record/replay arm of `handle_epoll_pwait`
            // does the same; before this PR routed NULL-sigmask `epoll_pwait`
            // here, the old handler always made an empty request. Omitting it
            // only on the plain-strict path would be a scheduling regression,
            // not a refactor.
            if self.cfg.sequentialize_threads {
                let yield_to_peer = self.cfg.backend.loopback_pollers_yield_to_peers
                    && guest.thread_state().has_loopback_peer();
                resource_request(
                    guest,
                    zero_timeout_poll_request(guest.thread_state().dettid, yield_to_peer),
                )
                .await;
            }
            crate::syscalls::timerfd::publish_expired(guest).await;
            Ok(guest.inject(call).await?) // Already non-blocking.
        } else {
            let maybe_timeout_ns = millis_duration_to_absolute_timeout(guest, timeout_millis).await;
            let mut rsrc = Resources::new(guest.thread_state().dettid);
            rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
            rsrc.fyi("epoll_pwait");
            retry_nonblocking_syscall_with_timeout(guest, call, rsrc, maybe_timeout_ns).await
        }
    }

    /// epoll_pwait2 syscall (MAYHANG).
    ///
    /// epoll_pwait2 is epoll_pwait with a `struct timespec *` timeout instead of
    /// an int-milliseconds timeout; recent glibc implements epoll_wait/
    /// epoll_pwait via epoll_pwait2 when the kernel supports it. The pinned
    /// Reverie revision has no typed variant, so it arrives as a raw
    /// `Syscall::Other` and is dispatched here by Sysno. Detcore treats it
    /// like epoll_pwait: under record and replay a call that may wait waits
    /// outside the turn; under sequentialized threads it polls
    /// (`handle_internal_epoll_pwait2`); otherwise, and for a zero timeout, a
    /// scheduler yield point is followed by the raw call.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(#773)
    pub async fn handle_epoll_pwait2<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: Syscall,
    ) -> Result<i64, Error> {
        // epoll_pwait2(epfd, events, maxevents, timeout, sigmask, sigsetsize)
        let (_, args) = call.into_parts();
        self.observe_under_temporary_mask(
            guest,
            Addr::<libc::sigset_t>::from_raw(args.arg4),
            args.arg5,
        )
        .await?;
        // A NULL timeout waits forever; a zero timespec cannot wait and keeps
        // the turn.
        let zero_timeout = args.arg3 != 0
            && Addr::<Timespec>::from_raw(args.arg3)
                .and_then(|timeout| guest.memory().read_value(timeout).ok())
                .is_some_and(|timeout: Timespec| timeout.tv_sec == 0 && timeout.tv_nsec == 0);
        // Under record and replay, a call that may wait does so outside the
        // scheduler turn, as epoll_pwait's does: unmasked through the logged
        // background path, masked as a kernel wait under a private copy of its
        // mask (`record_or_replay_masked_epoll_pwait2`).
        if self.cfg.recordreplay_modes {
            if !zero_timeout {
                return if args.arg4 != 0 {
                    self.record_or_replay_masked_epoll_pwait2(guest, args).await
                } else {
                    self.record_or_replay_blocking(guest, call).await
                };
            }
        } else if self.cfg.sequentialize_threads
            && !zero_timeout
            && (args.arg4 == 0 || self.cfg.backend.supports_blocked_wait_signal_interruption)
        {
            // With threads sequentialized, a call that may wait polls, as
            // epoll_pwait does, rather than waiting in the kernel inside the
            // turn, where no other guest thread could run
            // (https://github.com/rrnewton/hermit/issues/4228). A masked call
            // on a backend whose kernel does not report the guest's signal
            // state keeps the turn, as a masked epoll_pwait does there.
            return self.handle_internal_epoll_pwait2(guest, args).await;
        }
        let dettid = guest.thread_state().dettid;
        resource_request(guest, Resources::new(dettid)).await; // empty request
        // The raw call runs in this turn. A zero timeout only probes, once a
        // virtual timerfd is readable exactly when its deadline has passed;
        // any other wait here is in the host (`crate::syscalls::timerfd`).
        if zero_timeout {
            crate::syscalls::timerfd::publish_expired(guest).await;
        } else {
            crate::syscalls::timerfd::refuse_host_wait(guest, "epoll_pwait2 with a signal mask");
        }
        Ok(self.record_or_replay(guest, call).await?)
    }

    /// An epoll_pwait2 that may wait, under sequentialized threads: it polls
    /// with zero-timeout probes, as epoll_pwait does. A probe is an
    /// epoll_pwait with no mask, whose answer for a zero timeout is the same
    /// as epoll_pwait2's. Linux copies and checks the timespec before the
    /// mask (`record_or_replay_masked_epoll_pwait2`), then the mask's size
    /// and the mask, before it looks at the epoll descriptor. An unmasked
    /// call waits as an unmasked epoll_pwait does
    /// (`handle_internal_epoll_pwait`), a masked one as a masked epoll_pwait
    /// does (`wait_on_masked_epoll`). It used to make the real call in the
    /// thread's turn, so it returned 0 at its timeout, or never, when only
    /// another guest thread could make an event ready
    /// (https://github.com/rrnewton/hermit/issues/4228).
    async fn handle_internal_epoll_pwait2<G: Guest<Self>>(
        &self,
        guest: &mut G,
        args: SyscallArgs,
    ) -> Result<i64, Error> {
        let deadline = match Addr::<Timespec>::from_raw(args.arg3) {
            Some(timeout) => {
                let timeout: Timespec = guest.memory().read_value(timeout)?;
                let timeout = ppoll_timeout_duration(timeout)?;
                // As for ppoll: `LogicalTime + Duration` saturates, where a
                // nanosecond count cast to u64 would wrap a 2^64 ns timeout to
                // none at all (Claude review of
                // https://github.com/rrnewton/hermit/pull/4235).
                Some(thread_observe_time(guest).await + timeout)
            }
            None => None,
        };
        let probe = syscalls::EpollPwait::new()
            .with_epfd(args.arg0 as i32)
            .with_events(AddrMut::from_raw(args.arg1))
            .with_maxevents(args.arg2 as i32)
            .with_timeout(0)
            .with_sigmask(None)
            .with_sigsetsize(args.arg5);
        if args.arg4 == 0 {
            let mut rsrc = Resources::new(guest.thread_state().dettid);
            rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
            rsrc.fyi("epoll_pwait2");
            return retry_nonblocking_syscall_with_timeout(guest, probe, rsrc, deadline).await;
        }
        if args.arg5 != KERNEL_SIGSET_SIZE {
            return Err(Errno::EINVAL.into());
        }
        let mask_address = Addr::<libc::sigset_t>::from_raw(args.arg4).ok_or(Errno::EFAULT)?;
        let mask = read_kernel_sigset(guest, mask_address).await?;
        super::signal_shadow::note_wait_mask(guest, mask);
        // A pending real-time timer expiration that the mask lets the kernel
        // deliver or discard (https://github.com/rrnewton/hermit/issues/4089).
        observe_under_mask(guest, mask).await?;
        let mask = sanitize_ppoll_signal_mask(mask);
        self.wait_on_masked_epoll(guest, probe, mask, deadline, "epoll_pwait2")
            .await
    }

    /// Before a call that sleeps or probes under the temporary mask at `mask`
    /// (`sigsetsize` bytes), refuse the run by name if the mask lets the kernel
    /// take a pending real-time timer expiration that every thread blocked when
    /// it expired, and discard the ones it lets the kernel discard
    /// (https://github.com/rrnewton/hermit/issues/4089). A mask the kernel would
    /// reject fails the call itself, which then observes nothing.
    async fn observe_under_temporary_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
        mask: Option<Addr<'_, libc::sigset_t>>,
        sigsetsize: usize,
    ) -> Result<(), Error> {
        let Some(mask) = mask else {
            return Ok(());
        };
        if sigsetsize != KERNEL_SIGSET_SIZE
            || !crate::scheduler::real_time_expiration_kept_pending()
        {
            return Ok(());
        }
        let Ok(mask) = read_kernel_sigset(guest, mask).await else {
            return Ok(());
        };
        observe_under_mask(guest, mask).await
    }

    /// `record_or_replay_masked_epoll_pwait` for `epoll_pwait2`, whose mask
    /// pointer and size are its fifth and sixth arguments.
    async fn record_or_replay_masked_epoll_pwait2<G: Guest<Self>>(
        &self,
        guest: &mut G,
        args: SyscallArgs,
    ) -> Result<i64, Error> {
        // Linux copies and checks the timespec before the mask, so an
        // unreadable timeout is EFAULT and an invalid one EINVAL whatever the
        // mask and its size (Claude review of
        // https://github.com/rrnewton/hermit/pull/4128).
        if let Some(timeout) = Addr::<Timespec>::from_raw(args.arg3) {
            let timeout: Timespec = guest.memory().read_value(timeout)?;
            ppoll_timeout_duration(timeout)?;
        }
        if args.arg5 != KERNEL_SIGSET_SIZE {
            return Err(Errno::EINVAL.into());
        }
        let mask_address = Addr::<libc::sigset_t>::from_raw(args.arg4).ok_or(Errno::EFAULT)?;
        let mask = read_kernel_sigset(guest, mask_address).await?;
        super::signal_shadow::note_wait_mask(guest, mask);
        // The copy may go in the red zone on a stack with no scratch below it,
        // clear of the events buffer the kernel writes and of the timeout it
        // reads, as for epoll_pwait (`stage_private_cells`).
        let events_len = usize::try_from((args.arg2 as i32).max(0)).unwrap_or(0)
            * std::mem::size_of::<libc::epoll_event>();
        let live: Vec<std::ops::Range<usize>> = [
            (args.arg1, events_len),
            (args.arg3, std::mem::size_of::<Timespec>()),
        ]
        .into_iter()
        .filter(|&(start, _)| start != 0)
        .map(|(start, len)| start..start.saturating_add(len))
        .collect();
        let Some(staged) = stage_private_cells(guest, PrivateCells::Mask(mask), &live).await?
        else {
            return self.refuse_unstageable_epoll_pwait2_mask(guest, args).await;
        };
        let mut private = args;
        private.arg4 = staged.mask();
        let result = self
            .record_or_replay_blocking_with_unknown_mask(
                guest,
                Syscall::Other(Sysno::epoll_pwait2, private),
            )
            .await;
        staged.release(guest);
        if self.cfg.replaying && matches!(result, Err(Error::Errno(Errno::EINTR))) {
            end_replayed_pselect6_under_its_temporary_mask(guest, mask).await;
        }
        result
    }

    /// `refuse_unstageable_epoll_pwait_mask` for `epoll_pwait2`, whose red
    /// zone the events buffer and the timeout fill.
    async fn refuse_unstageable_epoll_pwait2_mask<G: Guest<Self>>(
        &self,
        guest: &mut G,
        args: SyscallArgs,
    ) -> Result<i64, Error> {
        use std::io::Write as _;
        let message = "unsupported: epoll_pwait2 with a temporary signal mask, on a stack \
            with no room below its red zone whose red zone the call's events buffer and \
            timeout fill: Hermit has nowhere to put the private copy of the mask \
            (https://github.com/rrnewton/hermit/issues/4088)";
        tracing::error!("[tid {}] {message}", guest.tid());
        if self.cfg.panic_on_unsupported_syscalls {
            let _ = writeln!(crate::util::RetryingStderr, "{message}");
            return self
                .refuse_unserviceable_operation(guest, Sysno::epoll_pwait2, Errno::EFAULT)
                .await;
        }
        self.record_or_replay_blocking_with_unknown_mask(
            guest,
            Syscall::Other(Sysno::epoll_pwait2, args),
        )
        .await
    }

    /// epoll_wait syscall (MAYHANG)
    pub async fn handle_epoll_wait<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollWait,
    ) -> Result<i64, Error> {
        if self.cfg.recordreplay_modes && call.timeout() == 0 {
            // This cannot block, but still yield a scheduler turn so a polling thread cannot
            // monopolize the guest between preemptions.
            resource_request(guest, Resources::new(guest.thread_state().dettid)).await;
            Ok(self.record_or_replay(guest, call).await?)
        } else if !self.cfg.sequentialize_threads || self.cfg.recordreplay_modes {
            Ok(self
                .record_or_replay_blocking(guest, Syscall::EpollWait(call))
                .await?)
        } else {
            self.handle_internal_epoll_wait(guest, call).await
        }
    }

    /// Handle a guest-internal `epoll_wait` call that can be fully determinized.
    pub async fn handle_internal_epoll_wait<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::EpollWait,
    ) -> Result<i64, Error> {
        let timeout_millis = call.timeout();
        if timeout_millis == 0 {
            crate::syscalls::timerfd::publish_expired(guest).await;
            Ok(guest.inject(call).await?) // Already non-blocking.
        } else {
            let maybe_timeout_ns = millis_duration_to_absolute_timeout(guest, timeout_millis).await;
            let mut rsrc = Resources::new(guest.thread_state().dettid);
            rsrc.insert(ResourceID::InternalIOPolling, Permission::W);
            rsrc.fyi("epoll_wait");
            retry_nonblocking_syscall_with_timeout(guest, call, rsrc, maybe_timeout_ns).await
        }
    }

    /// Connect system call (MAYHANG)
    /// Note that connect waits until a TCP handshake but does not wait for accept() on the other end.
    /// Nevertheless, it can block for a long time while waiting for connection, unless the socket
    /// is already nonblocking.
    pub async fn handle_connect<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Connect,
    ) -> Result<i64, Error> {
        let fd = call.fd();
        let uservaddr = call.uservaddr();
        let addrlen = call.addrlen();

        if guest.config().sched_heuristic == SchedHeuristic::ConnectBind {
            trace!("Scheduling heuristic: reprioritizing connect");
            let resource = ResourceID::PriorityChangePoint(
                FIRST_PRIORITY,
                guest.thread_state().thread_logical_time.as_nanos(),
                guest.thread_state().committed_clock_value,
                Vec::new(),
            );
            let req = guest.thread_state().mk_request(resource, Permission::W);
            resource_request(guest, req).await;
        }

        let result = self.execute_nonblockable_fd_syscall(guest, call).await;
        if self.cfg.backend.loopback_pollers_yield_to_peers
            && connect_result_allows_peer_classification(&result)
        {
            // This metadata is a loopback-poll scheduling hint, not part of connect's semantics.
            // Let the kernel establish the authoritative result first, then classify the peer
            // best-effort so an invalid guest pointer or an untracked fd can never replace the
            // kernel's errno.
            let loopback_peer = (|| -> Result<Option<bool>, Error> {
                let Some(address) = uservaddr else {
                    return Ok(None);
                };
                let addrlen = usize::try_from(addrlen).unwrap_or(0);
                if addrlen < std::mem::size_of::<u16>() {
                    return Ok(None);
                }
                let family: u16 = guest.memory().read_value(address.cast())?;
                if family == libc::AF_INET as u16 {
                    if addrlen < std::mem::size_of::<libc::sockaddr_in>() {
                        return Ok(None);
                    }
                    let address: libc::sockaddr_in = guest.memory().read_value(address.cast())?;
                    Ok(Some(
                        Ipv4Addr::from(address.sin_addr.s_addr.to_ne_bytes()).is_loopback(),
                    ))
                } else if family == libc::AF_INET6 as u16 {
                    if addrlen < std::mem::size_of::<libc::sockaddr_in6>() {
                        return Ok(None);
                    }
                    let address: libc::sockaddr_in6 = guest.memory().read_value(address.cast())?;
                    Ok(Some(
                        Ipv6Addr::from(address.sin6_addr.s6_addr).is_loopback(),
                    ))
                } else {
                    // This includes a successful AF_UNSPEC disconnect and successful connects
                    // to non-IP families, neither of which has a loopback IP peer.
                    Ok(Some(false))
                }
            })()
            .ok()
            .flatten();
            if let Some(loopback_peer) = loopback_peer {
                let _ = guest
                    .thread_state()
                    .with_detfd(fd, |detfd| detfd.set_loopback_peer(loopback_peer));
            }
        }

        result
    }

    /// Handles sendto, sendmsg, and sendmmsg syscalls (MAYHANG).
    pub async fn handle_sendrecv<
        G: Guest<Self>,
        C: SyscallInfo + NonblockableSyscall + Into<Syscall>,
    >(
        &self,
        guest: &mut G,
        call: C,
    ) -> Result<i64, Error> {
        self.execute_nonblockable_fd_syscall(guest, call).await
    }

    /// Sends one message and invalidates process-wide flock knowledge after success.
    ///
    /// The guest can mutate shared message and control memory while this helper
    /// deschedules. Parsing before the syscall would not prove which descriptors the
    /// kernel later transferred, so a successful unbound send conservatively makes
    /// every cached open-file-description lock mode unknown.
    pub async fn handle_sendmsg<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Sendmsg,
    ) -> Result<i64, Error> {
        if self.accept_in_turn_enabled() {
            let headers: Vec<usize> = call
                .msg()
                .map(|header| header.as_raw())
                .into_iter()
                .collect();
            self.mark_exported_listeners(guest, &headers);
        }
        let headers: Vec<usize> = call
            .msg()
            .map(|header| header.as_raw())
            .into_iter()
            .collect();
        self.refuse_sending_virtual_timerfds(guest, &headers);
        let result = self.execute_nonblockable_fd_syscall(guest, call).await?;
        guest.thread_state().forget_flock_modes();
        Ok(result)
    }

    /// Mark every accept-in-turn listener (`DetFd::is_accept_in_turn_eligible`)
    /// these messages name in `SCM_RIGHTS` as exported, before they are sent
    /// and whatever the send returns: Linux may have queued the descriptor
    /// even when the call reports an error, and both phases must decide alike.
    /// `headers` are the guest addresses of the `msghdr`s: one for `sendmsg`,
    /// one per entry Linux could send for `sendmmsg`.
    ///
    /// An exported listener may be held by a process outside Detcore's view,
    /// so its accepts never take the temporary `O_NONBLOCK` again and keep the
    /// backgrounded path. An accept already waiting on it refuses at its next
    /// attempt (`ACCEPT_EXPORT_REFUSAL`). Both phases read the same guest
    /// memory here. When a header or control buffer cannot be read in full
    /// (Linux reads a write-only page, which Detcore cannot, and takes up to
    /// `optmem_max` bytes of control data, more than `MAX_CONTROL_BYTES`),
    /// every eligible listener in the table counts as exported. A thread that
    /// rewrites the control buffer while this one is descheduled can send a
    /// listener this did not see.
    // TODO-HUMAN-REVIEW(PR-3908)
    fn mark_exported_listeners<G: Guest<Self>>(&self, guest: &mut G, headers: &[usize]) {
        let (sent, unread) = sent_descriptors(guest, headers);
        if unread {
            // Linux can read memory Detcore cannot (a write-only page) and
            // accepts more control data than Detcore reads, so a message it
            // could not read may carry any listener.
            guest
                .thread_state()
                .mark_accept_in_turn_listeners_exported();
            return;
        }
        for fd in sent {
            let _ = guest.thread_state().with_detfd(fd, |detfd| {
                if detfd.is_accept_in_turn_eligible() {
                    detfd.set_exported();
                }
            });
        }
    }

    /// Refuse the run, by name, before messages at `headers` pass a timerfd
    /// over `SCM_RIGHTS` while timerfds run on Detcore's virtual clock, armed
    /// or not (one armed after it is sent would be modelled only by the
    /// sender), or an epoll descriptor while this process holds an armed one
    /// (its set may include it). The receiving process keeps no model of the
    /// timer, so its probes would never make it readable and it would wait
    /// where Linux wakes (`crate::syscalls::timerfd`). A message whose control
    /// data Detcore cannot read refuses too while this process holds an armed
    /// timerfd.
    fn refuse_sending_virtual_timerfds<G: Guest<Self>>(&self, guest: &mut G, headers: &[usize]) {
        if !crate::syscalls::timerfd::enabled(guest) {
            return;
        }
        let holds_armed = crate::syscalls::timerfd::has_virtual_timerfds(guest);
        let (sent, unread) = sent_descriptors(guest, headers);
        if unread && holds_armed {
            crate::syscalls::timerfd::refuse(format_args!(
                "a message whose SCM_RIGHTS control data Detcore cannot read may pass a timerfd to another process"
            ));
        }
        for fd in sent {
            match guest.thread_state().with_detfd(fd, |detfd| detfd.ty()) {
                Ok(FdType::Timerfd) => crate::syscalls::timerfd::refuse(format_args!(
                    "a message passes timerfd {fd} over SCM_RIGHTS, and its receiver keeps no model of the timer"
                )),
                Ok(FdType::Epoll) if holds_armed => crate::syscalls::timerfd::refuse(format_args!(
                    "a message passes epoll descriptor {fd} over SCM_RIGHTS while this process holds a timerfd its set may include"
                )),
                _ => {}
            }
        }
    }
}

/// The descriptors the messages at `headers` name in `SCM_RIGHTS`, and
/// whether a header or control buffer could not be read in full, in which case
/// the list may be incomplete (`mark_exported_listeners`).
fn sent_descriptors<T, G>(guest: &G, headers: &[usize]) -> (Vec<RawFd>, bool)
where
    T: RecordOrReplay,
    G: Guest<Detcore<T>>,
{
    let mut sent: Vec<RawFd> = Vec::new();
    let mut unread = false;
    for header in headers {
        // A null header or control buffer fails the call with EFAULT
        // before Linux reads any descriptor.
        let Some(header) = Addr::<libc::msghdr>::from_raw(*header) else {
            continue;
        };
        let Ok(header) = guest.memory().read_value::<_, libc::msghdr>(header) else {
            unread = true;
            continue;
        };
        let Some(control_address) = Addr::<u8>::from_raw(header.msg_control as usize) else {
            continue;
        };
        if header.msg_controllen > MAX_CONTROL_BYTES {
            unread = true;
            continue;
        }
        let mut control = vec![0; header.msg_controllen];
        if guest
            .memory()
            .read_exact(control_address, &mut control)
            .is_err()
        {
            unread = true;
            continue;
        }
        sent.extend(received_descriptors(&control));
    }
    (sent, unread)
}

impl<T: RecordOrReplay> Detcore<T> {
    /// Sends a message batch and invalidates process-wide flock knowledge when the
    /// kernel reports at least one message sent. This intentionally includes
    /// descriptors named only by an unsent tail message: the mutable guest array is
    /// not stable across a possible deschedule, so narrower attribution is unsafe.
    pub async fn handle_sendmmsg<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Sendmmsg,
    ) -> Result<i64, Error> {
        if self.cfg.recordreplay_modes && is_accepted_connection(guest, call.sockfd()) {
            return self.refuse_log_served_mmsg(guest, Sysno::sendmmsg).await;
        }
        if self.accept_in_turn_enabled() {
            // Every entry Linux could send, sent or not: see `mark_exported_listeners`.
            // Reverie types the vector as `msghdr`s, but its entries are
            // `mmsghdr`s, each a `msghdr` followed by `msg_len`, so step by
            // the larger size.
            let headers: Vec<usize> = call.msgvec().map_or_else(Vec::new, |vector| {
                (0..call.vlen().min(libc::UIO_MAXIOV as u32) as usize)
                    .map(|index| vector.as_raw() + index * std::mem::size_of::<libc::mmsghdr>())
                    .collect()
            });
            self.mark_exported_listeners(guest, &headers);
        }
        // As for `sendmsg`: every entry Linux could send.
        let headers: Vec<usize> = call.msgvec().map_or_else(Vec::new, |vector| {
            (0..call.vlen().min(libc::UIO_MAXIOV as u32) as usize)
                .map(|index| vector.as_raw() + index * std::mem::size_of::<libc::mmsghdr>())
                .collect()
        });
        self.refuse_sending_virtual_timerfds(guest, &headers);
        let result = self.execute_nonblockable_fd_syscall(guest, call).await?;
        if result > 0 {
            guest.thread_state().forget_flock_modes();
        }
        Ok(result)
    }

    /// Record and replay keep an accepted connection only in the log, and
    /// replay stands in an eventfd for it. Neither recorder nor replayer
    /// records the batched `sendmmsg` and `recvmmsg`, so replay would run them
    /// live against that eventfd and return something other than what the
    /// recording saw. Refuse them in both modes instead. On other sockets
    /// they still run live
    /// (https://github.com/rrnewton/hermit/issues/3883).
    // TODO-HUMAN-REVIEW(PR-3871): Refuse unrecorded batched I/O on accepted connections.
    async fn refuse_log_served_mmsg<G: Guest<Self>>(
        &self,
        guest: &mut G,
        sysno: Sysno,
    ) -> Result<i64, Error> {
        self.refuse_unserviceable_operation(guest, sysno, Errno::EOPNOTSUPP)
            .await
    }

    // TODO-HUMAN-REVIEW(PR-912): Review receive-time capture across socket aliases.
    async fn observe_socket_receive<G: Guest<Self>>(
        &self,
        guest: &mut G,
        fd: i32,
    ) -> Result<LogicalTime, Error> {
        let timestamp = thread_observe_time(guest).await;
        guest.thread_state().with_detfd(fd, |detfd| {
            detfd.set_socket_receive_timestamp(timestamp);
        })?;
        Ok(timestamp)
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-901)
    /// Receive one message and replace host socket timestamps with logical time.
    pub async fn handle_recvmsg<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Recvmsg,
    ) -> Result<i64, Error> {
        // NETLINK_SOCK_DIAG replies carry host-assigned socket identities in
        // their msg_iov payload (not msg_control). Canonicalize the supported
        // fields before the binary reply reaches the guest.
        // The predicate is shared with the read/readv/recvfrom/recvmmsg paths so
        // the five receive syscalls cannot drift apart again.
        if self.sock_diag_reply_fd(guest, call.sockfd()) {
            return self.handle_sock_diag_recvmsg(guest, call).await;
        }

        if !self.cfg.virtualize_time {
            return self.execute_nonblockable_fd_syscall(guest, call).await;
        }

        let Some(message_address) = call.msg() else {
            return self
                .handle_socket_receive(guest, call, call.sockfd(), true)
                .await;
        };
        // Snapshot every input field before the receive. Linux permits the
        // control buffer to overlap this header, so rereading it afterward can
        // turn a successful consuming receive into an artificial EFAULT.
        let message: libc::msghdr = guest.memory().read_value(message_address)?;
        if message.msg_control.is_null() || message.msg_controllen == 0 {
            return self
                .handle_socket_receive(guest, call, call.sockfd(), true)
                .await;
        }
        let control_len = message.msg_controllen.min(MAX_CONTROL_BYTES);
        let control_address: AddrMut<'_, u8> =
            AddrMut::from_raw(message.msg_control as usize).ok_or(Errno::EFAULT)?;
        let mut control = vec![0; control_len];
        // Validate the output region before consuming a datagram.
        guest.memory().read_exact(control_address, &mut control)?;

        let result = self.execute_nonblockable_fd_syscall(guest, call).await?;
        let now = self.observe_socket_receive(guest, call.sockfd()).await?;
        let mut control = vec![0; control_len];
        guest.memory().read_exact(control_address, &mut control)?;
        // A descriptor received over `SCM_RIGHTS` that Detcore models (an
        // in-guest backend discovers it as it is used) joins this process's
        // table in `procfs::locks::LockLedger` with whatever OFD or flock
        // locks its description holds. One Detcore keeps no model of (ptrace)
        // is left out, so its locks are hidden once no modelled table names
        // the description (a named residual).
        for fd in received_descriptors(&control) {
            self.refresh_description_locks(guest, fd, super::files::LockCall::Recvmsg)
                .await?;
        }
        if socket_timestamp_messages(&control).is_empty() {
            return Ok(result);
        }

        canonicalize_socket_timestamps(&mut control, now);
        guest.memory().write_exact(control_address, &control)?;
        Ok(result)
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1064)
    /// Whether replies received on `fd` must have their supported socket
    /// identities canonicalized.
    ///
    /// This is the single predicate every receive path consults. It exists as
    /// one function because the flag used to be tested inline in `recvmsg`
    /// alone, and four other receive syscalls reached the same dump without it
    /// (see [`Self::sanitize_sock_diag_segments`]).
    pub(crate) fn sock_diag_reply_fd<G: Guest<Self>>(&self, guest: &mut G, fd: RawFd) -> bool {
        self.cfg.virtualize_metadata
            && guest
                .thread_state()
                .with_detfd(fd, |detfd| detfd.is_sock_diag() || detfd.is_netlink_route())
                .unwrap_or(false)
    }

    /// Whether this descriptor is specifically a `NETLINK_ROUTE` socket, which
    /// needs the link-counter sanitizer rather than the sock-diag one.
    fn netlink_route_reply_fd<G: Guest<Self>>(&self, guest: &mut G, fd: RawFd) -> bool {
        guest
            .thread_state()
            .with_detfd(fd, |detfd| detfd.is_netlink_route())
            .unwrap_or(false)
    }

    /// Read an `iovec` array out of guest memory as plain `(address, capacity)`
    /// scalars, so no non-`Send` raw pointer is held across an await.
    fn read_iov_segments<G: Guest<Self>>(
        guest: &mut G,
        iov: usize,
        iovlen: usize,
    ) -> Result<Vec<(usize, usize)>, Error> {
        if iov == 0 || iovlen == 0 {
            return Ok(Vec::new());
        }
        let count = iovlen.min(libc::UIO_MAXIOV as usize);
        let address: AddrMut<'_, libc::iovec> = AddrMut::from_raw(iov).ok_or(Errno::EFAULT)?;
        // SAFETY: `iovec` is a plain C record; an all-zero value is a valid
        // staging value immediately overwritten by `read_values`.
        let mut iovecs: Vec<libc::iovec> =
            (0..count).map(|_| unsafe { std::mem::zeroed() }).collect();
        guest.memory().read_values(address.into(), &mut iovecs)?;
        Ok(iovecs
            .iter()
            .map(|iov| (iov.iov_base as usize, iov.iov_len))
            .collect())
    }

    /// Canonicalize host-assigned identities in a `NETLINK_SOCK_DIAG` reply that
    /// the kernel has already written into guest memory.
    ///
    /// `segments` describes the destination buffers as `(address, capacity)` in
    /// the order the kernel filled them; `received` is the syscall's return
    /// value. The reply is gathered contiguously (it may be scattered across
    /// several buffers), sanitized via `crate::sock_diag` (fail-open,
    /// zero-only, never resizes), and written back preserving the original
    /// boundaries. Bounded by both each buffer's capacity and `received`, which
    /// under `MSG_TRUNC` can exceed the total capacity.
    ///
    /// Synchronous on purpose: every caller has already completed its receive,
    /// so nothing here awaits and no guest address outlives the borrow.
    fn sanitize_sock_diag_segments<G: Guest<Self>>(
        &self,
        guest: &mut G,
        fd: RawFd,
        segments: &[(usize, usize)],
        received: usize,
    ) -> Result<(), Error> {
        if received == 0 || segments.is_empty() {
            return Ok(());
        }
        let mut filled: Vec<(AddrMut<'_, u8>, usize)> = Vec::new();
        let mut buffer: Vec<u8> = Vec::with_capacity(received);
        let mut remaining = received;
        for &(base, capacity) in segments {
            if remaining == 0 {
                break;
            }
            if base == 0 || capacity == 0 {
                continue;
            }
            let take = capacity.min(remaining);
            let address: AddrMut<'_, u8> = AddrMut::from_raw(base).ok_or(Errno::EFAULT)?;
            let mut segment = vec![0u8; take];
            guest.memory().read_exact(address, &mut segment)?;
            buffer.extend_from_slice(&segment);
            filled.push((address, take));
            remaining -= take;
        }

        // NETLINK_ROUTE and NETLINK_SOCK_DIAG replies need different
        // sanitizers: one zeroes live interface counters, the other determinizes
        // supported socket identities. The descriptor decides which, so a guest
        // holding both kinds of socket gets each handled correctly.
        let modified = if self.netlink_route_reply_fd(guest, fd) {
            crate::netlink_route::sanitize_route_link_stats(&mut buffer)
        } else {
            crate::sock_diag::sanitize_sock_diag_identities(&mut buffer)
        };
        if !modified {
            return Ok(());
        }

        let mut offset = 0;
        for (address, len) in filled {
            guest
                .memory()
                .write_exact(address, &buffer[offset..offset + len])?;
            offset += len;
        }
        Ok(())
    }

    /// `recvfrom` on a socket-diag descriptor: one destination buffer.
    ///
    /// `recv(2)` has no syscall of its own on x86_64 — glibc lowers it to
    /// `recvfrom` with a null address — so this covers `recv` as well, which is
    /// what Python's `socket.recv()` reaches.
    pub async fn handle_sock_diag_recvfrom<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Recvfrom,
    ) -> Result<i64, Error> {
        let fd = call.fd();
        let base = call.buf().map(|address| address.as_raw()).unwrap_or(0);
        let len = call.len();
        let result = self.handle_socket_receive(guest, call, fd, true).await?;
        let received = usize::try_from(result).unwrap_or(0);
        self.sanitize_sock_diag_segments(guest, fd, &[(base, len)], received)?;
        Ok(result)
    }

    /// `read` on a socket-diag descriptor: one destination buffer.
    pub async fn handle_sock_diag_read<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Read,
    ) -> Result<i64, Error> {
        let fd = call.fd();
        let base = call.buf().map(|address| address.as_raw()).unwrap_or(0);
        let len = call.len();
        let result = self.handle_read(guest, call).await?;
        let received = usize::try_from(result).unwrap_or(0);
        self.sanitize_sock_diag_segments(guest, fd, &[(base, len)], received)?;
        Ok(result)
    }

    /// Receive a `NETLINK_SOCK_DIAG` dump and zero the host-assigned socket
    /// inode numbers in the reply so `ss`-style enumeration is deterministic.
    ///
    /// The dump lands in `msg_iov` (netlink diag sockets carry no ancillary
    /// data), possibly scattered across several iovecs.
    async fn handle_sock_diag_recvmsg<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Recvmsg,
    ) -> Result<i64, Error> {
        let fd = call.sockfd();
        let Some(message_address) = call.msg() else {
            return self.handle_socket_receive(guest, call, fd, true).await;
        };
        // Snapshot the header's iovec pointer/count before the receive (they are
        // stable across it; the kernel fills the pointed-to buffers, not the
        // array). Scoped so the `msghdr`'s raw pointers are dropped before the
        // await below: holding one would make this future non-`Send`.
        let segments = {
            let message: libc::msghdr = guest.memory().read_value(message_address)?;
            Self::read_iov_segments(guest, message.msg_iov as usize, message.msg_iovlen)?
        };

        let result = self.handle_socket_receive(guest, call, fd, true).await?;
        let received = usize::try_from(result).unwrap_or(0);
        self.sanitize_sock_diag_segments(guest, fd, &segments, received)?;
        Ok(result)
    }

    /// `readv` on a socket-diag descriptor: an `iovec` array, no message header.
    pub async fn handle_sock_diag_readv<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Readv,
    ) -> Result<i64, Error> {
        let fd = call.fd();
        let iov = call.iov().map(|address| address.as_raw()).unwrap_or(0);
        let segments = Self::read_iov_segments(guest, iov, call.len())?;
        let result = self.handle_readv(guest, call).await?;
        let received = usize::try_from(result).unwrap_or(0);
        self.sanitize_sock_diag_segments(guest, fd, &segments, received)?;
        Ok(result)
    }

    /// `recvmmsg` on a socket-diag descriptor.
    ///
    /// Each delivered `mmsghdr` is a separate datagram with its own byte count
    /// in `msg_len`, so each is gathered and sanitized independently; treating
    /// the batch as one buffer would let one message's length run into the
    /// next message's memory.
    pub async fn handle_sock_diag_recvmmsg<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Recvmmsg,
    ) -> Result<i64, Error> {
        let fd = call.fd();
        let Some(messages_address) = call.mmsg() else {
            return self.handle_recvmmsg(guest, call).await;
        };
        let vlen = call.vlen();
        if vlen == 0 || vlen > libc::UIO_MAXIOV as u32 {
            return self.handle_recvmmsg(guest, call).await;
        }
        let base = messages_address.as_raw();

        // Snapshot each message's iovec geometry BEFORE the receive: the kernel
        // fills the pointed-to buffers and writes msg_len, but does not move the
        // iovec arrays themselves. Scoped, and reduced to plain scalars, so the
        // `mmsghdr` raw pointers are dropped before the await below: holding one
        // would make this future non-`Send`.
        let geometry: Vec<Vec<(usize, usize)>> = {
            // SAFETY: `mmsghdr` is a plain C record; an all-zero value is a
            // valid staging value immediately overwritten by `read_values`.
            let mut headers: Vec<libc::mmsghdr> = (0..vlen as usize)
                .map(|_| unsafe { std::mem::zeroed() })
                .collect();
            guest
                .memory()
                .read_values(messages_address.into(), &mut headers)?;
            let mut geometry = Vec::with_capacity(headers.len());
            for header in &headers {
                geometry.push(Self::read_iov_segments(
                    guest,
                    header.msg_hdr.msg_iov as usize,
                    header.msg_hdr.msg_iovlen,
                )?);
            }
            geometry
        };

        let result = self.handle_recvmmsg(guest, call).await?;
        let delivered = usize::try_from(result).unwrap_or(0).min(geometry.len());
        if delivered == 0 {
            return Ok(result);
        }

        // Re-read the array for the per-message byte counts the kernel just
        // wrote. Also scoped: nothing awaits past this point, but keeping the
        // raw pointers contained keeps the rule visible.
        let counts: Vec<usize> = {
            let address: AddrMut<'_, libc::mmsghdr> =
                AddrMut::from_raw(base).ok_or(Errno::EFAULT)?;
            // SAFETY: as above.
            let mut headers: Vec<libc::mmsghdr> = (0..vlen as usize)
                .map(|_| unsafe { std::mem::zeroed() })
                .collect();
            guest.memory().read_values(address.into(), &mut headers)?;
            headers.iter().map(|h| h.msg_len as usize).collect()
        };
        for (index, segments) in geometry.iter().enumerate().take(delivered) {
            self.sanitize_sock_diag_segments(guest, fd, segments, counts[index])?;
        }
        Ok(result)
    }

    // TODO-HUMAN-REVIEW(PR-912): Review receive-time capture across socket aliases.
    /// Handle a socket receive and retain one timestamp for every alias of its open file.
    pub async fn handle_socket_receive<
        G: Guest<Self>,
        C: SyscallInfo + NonblockableSyscall + Into<Syscall>,
    >(
        &self,
        guest: &mut G,
        call: C,
        fd: i32,
        zero_delivers_packet: bool,
    ) -> Result<i64, Error> {
        let result = self.execute_nonblockable_fd_syscall(guest, call).await?;
        if self.cfg.virtualize_time && (result > 0 || (result == 0 && zero_delivers_packet)) {
            self.observe_socket_receive(guest, fd).await?;
        }
        Ok(result)
    }

    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-901)
    /// Receive a message batch and replace every host socket timestamp with logical time.
    pub async fn handle_recvmmsg<G: Guest<Self>>(
        &self,
        guest: &mut G,
        call: syscalls::Recvmmsg,
    ) -> Result<i64, Error> {
        if self.cfg.recordreplay_modes && is_accepted_connection(guest, call.fd()) {
            return self.refuse_log_served_mmsg(guest, Sysno::recvmmsg).await;
        }
        if !self.cfg.virtualize_time || call.vlen() > libc::UIO_MAXIOV as u32 {
            return self.execute_nonblockable_fd_syscall(guest, call).await;
        }

        let Some(messages_address) = call.mmsg() else {
            return self.execute_nonblockable_fd_syscall(guest, call).await;
        };
        let controls = {
            // SAFETY: `mmsghdr` is a plain C record and an all-zero value is a valid
            // initialized staging value that is immediately overwritten by `read_values`.
            let mut messages: Vec<libc::mmsghdr> = (0..call.vlen())
                .map(|_| unsafe { std::mem::zeroed() })
                .collect();
            guest
                .memory()
                .read_values(messages_address.into(), &mut messages)?;

            let mut controls = Vec::with_capacity(messages.len());
            for message in &messages {
                let header = &message.msg_hdr;
                if header.msg_control.is_null() || header.msg_controllen == 0 {
                    controls.push(None);
                    continue;
                }
                controls.push(Some((
                    header.msg_control as usize,
                    header.msg_controllen.min(MAX_CONTROL_BYTES),
                )));
            }
            controls
        };

        let result = self.execute_nonblockable_fd_syscall(guest, call).await?;
        let delivered = usize::try_from(result).unwrap_or(0).min(controls.len());
        if delivered == 0 {
            return Ok(result);
        }
        let now = self.observe_socket_receive(guest, call.fd()).await?;
        let mut timestamped = Vec::new();
        for (address, length) in controls.into_iter().take(delivered).flatten() {
            let address: AddrMut<'_, u8> = AddrMut::from_raw(address).ok_or(Errno::EFAULT)?;
            let mut bytes = vec![0; length];
            guest.memory().read_exact(address, &mut bytes)?;
            if !socket_timestamp_messages(&bytes).is_empty() {
                timestamped.push((address, bytes));
            }
        }
        if timestamped.is_empty() {
            return Ok(result);
        }

        for (address, mut bytes) in timestamped {
            canonicalize_socket_timestamps(&mut bytes, now);
            guest.memory().write_exact(address, &bytes)?;
        }
        Ok(result)
    }
}

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

    #[test]
    fn zero_timeout_socket_poll_requests_a_strong_one_turn_yield() {
        let request = zero_timeout_poll_request(DetTid::from_raw(17), true);

        assert_eq!(request.resources.len(), 1);
        assert_eq!(
            request.resources.get(&ResourceID::SchedYield),
            Some(&Permission::W)
        );
        assert_eq!(request.fyi, LOOPBACK_POLL_YIELD_FYI);
    }

    #[test]
    fn zero_timeout_non_socket_poll_keeps_the_existing_empty_turn() {
        let request = zero_timeout_poll_request(DetTid::from_raw(17), false);

        assert!(request.resources.is_empty());
        assert!(request.fyi.is_empty());
    }

    #[test]
    fn connect_peer_classification_never_overrides_kernel_errors() {
        assert!(connect_result_allows_peer_classification(&Ok(0)));
        assert!(connect_result_allows_peer_classification(&Err(
            Error::Errno(Errno::EINPROGRESS)
        )));
        assert!(!connect_result_allows_peer_classification(&Err(
            Error::Errno(Errno::EALREADY)
        )));
        assert!(!connect_result_allows_peer_classification(&Err(
            Error::Errno(Errno::EBADF)
        )));
        assert!(!connect_result_allows_peer_classification(&Err(
            Error::Errno(Errno::EFAULT)
        )));
    }

    #[test]
    fn ppoll_timeout_uses_timespec_units() {
        assert_eq!(
            ppoll_timeout_duration(Timespec {
                tv_sec: 2,
                tv_nsec: 345_678_901,
            }),
            Ok(Duration::new(2, 345_678_901))
        );
        assert_eq!(
            timespec_from_duration(Duration::new(2, 345_678_901)),
            Timespec {
                tv_sec: 2,
                tv_nsec: 345_678_901,
            }
        );
    }

    #[test]
    fn pselect6_fd_set_lengths_follow_the_raw_linux_abi() {
        assert_eq!(PSELECT6_INTERNAL_MAX_NFDS, 64);
        assert_eq!(pselect6_fd_set_len(-1), Err(Errno::EINVAL));
        assert_eq!(pselect6_fd_set_len(0), Ok(0));
        assert_eq!(pselect6_fd_set_len(1), Ok(8));
        assert_eq!(pselect6_fd_set_len(65), Ok(16));
        assert_eq!(pselect6_fd_set_len(libc::FD_SETSIZE as i32), Ok(128));
    }

    #[test]
    fn pselect6_probe_result_maps_only_erestartsys_to_eintr() {
        assert_eq!(
            pselect6_probe_result(Err(Errno::ERESTARTSYS)),
            Err(Errno::EINTR)
        );

        assert_eq!(pselect6_probe_result(Ok(0)), Ok(0));
        assert_eq!(pselect6_probe_result(Ok(1)), Ok(1));
        assert_eq!(pselect6_probe_result(Err(Errno::EBADF)), Err(Errno::EBADF));
        assert_eq!(
            pselect6_probe_result(Err(Errno::EINVAL)),
            Err(Errno::EINVAL)
        );
        assert_eq!(pselect6_probe_result(Err(Errno::EINTR)), Err(Errno::EINTR));
    }

    #[test]
    fn select_timeout_folds_whole_seconds_out_of_microseconds() {
        let timeval = |tv_sec, tv_usec| libc::timeval { tv_sec, tv_usec };
        assert_eq!(
            select_timeout_duration(timeval(0, 1_500_000)),
            Ok(Duration::new(1, 500_000_000))
        );
        assert_eq!(
            select_timeout_duration(timeval(2, 3_000_001)),
            Ok(Duration::new(5, 1_000))
        );
        assert_eq!(
            select_timeout_duration(timeval(0, 999_999)),
            Ok(Duration::new(0, 999_999_000))
        );
        assert_eq!(select_timeout_duration(timeval(0, -1)), Err(Errno::EINVAL));
        assert_eq!(select_timeout_duration(timeval(-1, 0)), Err(Errno::EINVAL));
        // v7.1 refuses a negative field before normalizing, so neither mixed
        // sign is read as the 1 s it would sum to.
        assert_eq!(
            select_timeout_duration(timeval(2, -1_000_000)),
            Err(Errno::EINVAL)
        );
        assert_eq!(
            select_timeout_duration(timeval(-1, 2_000_000)),
            Err(Errno::EINVAL)
        );
        assert_eq!(select_timeout_duration(timeval(1, -1)), Err(Errno::EINVAL));
        assert_eq!(
            select_timeout_duration(timeval(libc::time_t::MAX, 1_000_000)),
            Err(Errno::EINVAL)
        );
    }

    #[test]
    fn ppoll_signal_mask_keeps_reverie_preemption_unblocked() {
        let preemption_bit = 1_u64 << ((reverie::PERF_EVENT_SIGNAL as usize) - 1);
        assert_eq!(sanitize_ppoll_signal_mask(u64::MAX), !preemption_bit);
    }

    #[test]
    fn ppoll_record_replay_waits_are_kernel_waits_and_run_mode_masked_waits_probe() {
        // Record/replay: the kernel owns the wait (issue 4042).
        assert!(ppoll_uses_kernel_wait(true, true));
        // A sequentialized run keeps the probe path.
        assert!(!ppoll_uses_kernel_wait(true, false));
        // Without sequentialization the kernel always owns it.
        assert!(ppoll_uses_kernel_wait(false, false));
        assert!(ppoll_uses_kernel_wait(false, true));
    }

    #[test]
    fn ppoll_timeout_rejects_invalid_timespecs() {
        assert_eq!(
            ppoll_timeout_duration(Timespec {
                tv_sec: -1,
                tv_nsec: 0,
            }),
            Err(Errno::EINVAL)
        );
        assert_eq!(
            ppoll_timeout_duration(Timespec {
                tv_sec: 0,
                tv_nsec: 1_000_000_000,
            }),
            Err(Errno::EINVAL)
        );
    }

    #[test]
    fn socket_timestamp_control_messages_use_logical_time() {
        let header_len = cmsg_align(std::mem::size_of::<libc::cmsghdr>());
        let timeval_len = std::mem::size_of::<libc::timeval>();
        let timespec_len = std::mem::size_of::<libc::timespec>();
        let first_len = header_len + timeval_len;
        let second_offset = cmsg_align(first_len);
        let second_len = header_len + timespec_len;
        let third_offset = second_offset + cmsg_align(second_len);
        let third_len = header_len + std::mem::size_of::<i32>();
        let mut control = vec![0; third_offset + cmsg_align(third_len)];

        assert!(write_control_value(
            &mut control,
            libc::cmsghdr {
                cmsg_len: first_len,
                cmsg_level: libc::SOL_SOCKET,
                cmsg_type: SCM_TIMESTAMP_OLD,
            }
        ));
        assert!(write_control_value(
            &mut control[header_len..],
            libc::timeval {
                tv_sec: 99,
                tv_usec: 88,
            }
        ));
        assert!(write_control_value(
            &mut control[second_offset..],
            libc::cmsghdr {
                cmsg_len: second_len,
                cmsg_level: libc::SOL_SOCKET,
                cmsg_type: SCM_TIMESTAMPNS_OLD,
            }
        ));
        assert!(write_control_value(
            &mut control[second_offset + header_len..],
            libc::timespec {
                tv_sec: 77,
                tv_nsec: 66,
            }
        ));
        assert!(write_control_value(
            &mut control[third_offset..],
            libc::cmsghdr {
                cmsg_len: third_len,
                cmsg_level: libc::SOL_SOCKET,
                cmsg_type: libc::SCM_RIGHTS,
            }
        ));
        assert!(write_control_value(
            &mut control[third_offset + header_len..],
            42_i32
        ));
        let unrelated_message = control[third_offset..].to_vec();

        assert_eq!(
            canonicalize_socket_timestamps(&mut control, LogicalTime::from_nanos(2_345_678_901)),
            2
        );
        let timeval = read_control_value::<libc::timeval>(&control[header_len..]).unwrap();
        assert_eq!(timeval.tv_sec, 2);
        assert_eq!(timeval.tv_usec, 345_678);
        let timespec =
            read_control_value::<libc::timespec>(&control[second_offset + header_len..]).unwrap();
        assert_eq!(timespec.tv_sec, 2);
        assert_eq!(timespec.tv_nsec, 345_678_901);
        assert_eq!(control[third_offset..], unrelated_message);
    }

    #[test]
    fn truncated_timestamp_payload_prefix_is_rewritten() {
        let header_len = cmsg_align(std::mem::size_of::<libc::cmsghdr>());
        let full_len = header_len + std::mem::size_of::<libc::timeval>();
        let mut control = vec![0xaa; header_len + std::mem::size_of::<i32>()];
        assert!(write_control_value(
            &mut control,
            libc::cmsghdr {
                cmsg_len: full_len,
                cmsg_level: libc::SOL_SOCKET,
                cmsg_type: SCM_TIMESTAMP_OLD,
            }
        ));

        assert_eq!(
            canonicalize_socket_timestamps(&mut control, LogicalTime::from_nanos(2_345_678_901)),
            1
        );
        assert_eq!(
            read_control_value::<i32>(&control[header_len..]),
            Some(2),
            "the visible timeval prefix must not retain host seconds"
        );
    }

    #[test]
    fn timestamping_preserves_populated_source_slots() {
        let header_len = cmsg_align(std::mem::size_of::<libc::cmsghdr>());
        let timespec_len = std::mem::size_of::<libc::timespec>();
        let message_len = header_len + 3 * timespec_len;
        let mut control = vec![0; cmsg_align(message_len)];
        assert!(write_control_value(
            &mut control,
            libc::cmsghdr {
                cmsg_len: message_len,
                cmsg_level: libc::SOL_SOCKET,
                cmsg_type: SCM_TIMESTAMPING_OLD,
            }
        ));
        let zero = libc::timespec {
            tv_sec: 0,
            tv_nsec: 0,
        };
        let populated = libc::timespec {
            tv_sec: 99,
            tv_nsec: 88,
        };
        assert!(write_control_value(&mut control[header_len..], zero));
        assert!(write_control_value(
            &mut control[header_len + timespec_len..],
            populated
        ));
        assert!(write_control_value(
            &mut control[header_len + 2 * timespec_len..],
            populated
        ));

        assert_eq!(
            canonicalize_socket_timestamps(&mut control, LogicalTime::from_nanos(2_345_678_901)),
            1
        );
        let first = read_control_value::<libc::timespec>(&control[header_len..]).unwrap();
        let second =
            read_control_value::<libc::timespec>(&control[header_len + timespec_len..]).unwrap();
        let third = read_control_value::<libc::timespec>(&control[header_len + 2 * timespec_len..])
            .unwrap();
        assert_eq!((first.tv_sec, first.tv_nsec), (0, 0));
        assert_eq!((second.tv_sec, second.tv_nsec), (2, 345_678_901));
        assert_eq!((third.tv_sec, third.tv_nsec), (2, 345_678_901));
    }
}