1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
//! Detcore is a Reverie tool that determinizes the execution of a process.
//!
//! # Backend-abstraction commandment
//!
//! Detcore is a *tool* written against Reverie's **abstract** instrumentation
//! interface (the `reverie` crate). It depends only on those traits and types
//! and is deliberately ignorant of how a guest is actually traced.
//!
//! Detcore MUST NEVER depend on or import a concrete Reverie backend or support
//! crate -- any `reverie-*` crate other than the abstract `reverie-core`
//! interface. Choosing and instantiating a backend, and running a detcore tool
//! against it, is the sole responsibility of the `hermit-cli` package. There
//! are no backend-specific hacks in detcore: any tracing-mechanism-specific
//! behavior belongs behind the Reverie abstraction, not here.
//!
//! Why: Hermit follows Reverie's abstract model. A backend dependency in
//! detcore would couple the determinism engine to one tracing mechanism and
//! break the clean abstraction boundary that lets the same tool run over any
//! backend.
//!
//! The one allowed exception is test-only: detcore's own integration tests
//! (under `detcore/tests/`, wired via the `reverie-ptrace` **dev-dependency**)
//! drive a real tracer to exercise the tool. That coupling never reaches the
//! shipped library. This invariant is enforced in CI by
//! `scripts/check-detcore-backend-abstraction.sh`.
#![deny(clippy::all)]
#![deny(missing_docs)]
#![allow(clippy::uninlined_format_args)]
mod config;
mod consts;
mod cpuid;
mod digest;
mod dirents;
#[cfg(feature = "dispatch-observer")]
pub mod dispatch_observer;
/// Schedule-alignment and edit-distance algorithms shared by Hermit tools.
#[allow(missing_docs)]
pub mod edit_distance;
pub mod exec_record;
pub mod exit_dependencies;
mod fatal_core;
mod fd;
mod fifo_peer;
pub mod guest_log;
mod host_inputs;
pub mod in_guest_launch;
pub mod in_guest_site_patching;
mod io_buffers;
mod iovecs;
#[allow(unused)]
mod ivar;
pub mod logdiff;
pub mod machine;
mod memory;
pub mod native_exec;
pub mod netlink_route;
pub mod nonwaiting_write;
mod pipe_wake;
mod procfs;
mod procfs_inode;
mod procmaps;
pub mod race_probe;
pub mod random;
mod record_or_replay;
pub mod refused_signal_controls;
mod resources;
mod scheduler;
mod shared_open_files;
mod sigalrm_phase1;
mod signal_state;
mod sock_diag;
mod stat;
mod syscall_classification;
mod syscall_time;
mod syscalls;
#[cfg(test)]
mod test_pages;
mod tool_global;
mod tool_local;
pub mod util;
pub mod detlog;
pub mod preemptions;
pub mod types;
use std::fs::File;
use std::io::Write;
use std::os::unix::io::RawFd;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
pub use config::BlockingMode;
pub use config::CONFIG_FINGERPRINT_ENV;
pub use config::Config;
pub use config::FatalCoreCapture;
pub use config::InheritedSeccomp;
pub use config::LITEINST_CONFIG_FINGERPRINT_ENV;
pub use config::LaunchImage;
pub use config::RunsPostFork;
pub use config::SchedHeuristic;
pub use config::config_fingerprint_mismatch;
pub use config::config_wire_fingerprint;
pub use config::decode_backend_config_json;
pub use config::encode_backend_config_json;
pub use config::from_legacy_backend_json;
pub use config::to_legacy_backend_json;
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1120): Review the public canonical Detcore root identity.
pub use consts::ROOT_DETPID;
use detcore_model::bundled_signal_facts;
pub use digest::Digest;
#[cfg(test)]
use rand::RngExt as _;
use raw_cpuid::CpuIdResult;
use raw_cpuid::cpuid;
pub use record_or_replay::RecordOrReplay;
use reverie::Error;
use reverie::ExitStatus;
use reverie::GlobalRPC;
use reverie::Guest;
use reverie::Pid;
use reverie::Rdtsc;
use reverie::RdtscResult;
use reverie::RegDisplay;
use reverie::Signal;
use reverie::Subscription;
use reverie::Tid;
use reverie::TimerSchedule;
use reverie::Tool;
pub use reverie::process::Namespace;
use reverie::syscalls::CloneFlags;
use reverie::syscalls::Displayable;
use reverie::syscalls::EpollCreate1;
use reverie::syscalls::Errno;
use reverie::syscalls::InotifyInit1;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::Syscall;
use reverie::syscalls::SyscallInfo;
use reverie::syscalls::Sysno;
pub use scheduler::Priority;
pub use scheduler::TerminalDeadlockHook;
pub use scheduler::runqueue::DEFAULT_PRIORITY;
pub use scheduler::runqueue::FIRST_PRIORITY;
pub use scheduler::runqueue::LAST_PRIORITY;
pub use scheduler::set_terminal_deadlock_hook;
pub use tool_global::BackendFailureCleanup;
pub use tool_global::GlobalState;
use tool_global::ThreadDeregistration;
use tool_global::acknowledge_robust_list_exit_time;
pub use tool_global::cancel_exec;
use tool_global::create_child_thread;
use tool_global::create_vfork_child_thread;
use tool_global::deregister_thread;
pub use tool_global::format_unsupported_syscall_warning;
pub use tool_global::native_exec as native_exec_control;
pub use tool_global::prepare_exec;
use tool_global::report_unsupported_syscall;
use tool_global::robust_list_wakes_after_exec;
use tool_global::robust_list_wakes_after_exit;
pub use tool_global::shared_open_file_reply;
pub use tool_global::shared_open_file_request;
pub use tool_local::initial_resource_limit;
fn select_thread_exit_detpid(
thread_detpid: Option<DetPid>,
process_detpid: DetPid,
) -> (DetPid, bool) {
match thread_detpid {
Some(detpid) => (detpid, false),
None => (process_detpid, true),
}
}
fn select_thread_start_detpid(thread_detpid: Option<DetPid>, guest_pid: Pid) -> DetPid {
thread_detpid.unwrap_or_else(|| DetPid::from_raw(guest_pid.into()))
}
/// What a stop of thread `tid` of `pid` did with a copy of the standard
/// `signal` (`kept`: resumed into the thread, or else dropped), for the
/// scheduler's record of copies Linux discarded when they were sent
/// (`scheduler::discarded_at_send`, <https://github.com/rrnewton/hermit/issues/4293>).
/// Reads, after the stop, the thread's queues and its mask, which decides
/// whether a kept copy is requeued at the resume (`ptrace_signal`). `None`
/// for a signal the record does not track, or a state that cannot be read,
/// which changes nothing.
fn consumed_signal_control(
pid: Pid,
tid: Pid,
signal: i32,
kept: bool,
) -> Option<scheduler::discarded_at_send::DiscardedSignalControl> {
use scheduler::discarded_at_send::Consumption;
use scheduler::discarded_at_send::DiscardedSignalControl;
if !(1..32).contains(&signal) {
return None;
}
let state = syscalls::read_kernel_signal_state(pid, tid).ok()?;
let bit = scheduler::kernel_signal_bit(signal);
let how = if kept {
Consumption::KeptResume {
blocked: state.blocked & bit != 0,
}
} else {
Consumption::Dropped
};
Some(DiscardedSignalControl::Consumed {
signal,
how,
thread_holds: state.thread_pending & bit != 0,
process_holds: state.shared_pending & bit != 0,
})
}
fn thread_start_needs_registration(
is_root_process: bool,
past_first_execve: bool,
dettid: DetTid,
detpid: DetPid,
) -> bool {
dettid == detpid && (is_root_process || !past_first_execve)
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-644): Review the typed fail-closed backend signal.
/// Identifies an unsupported syscall that a backend must terminate without unwinding.
#[derive(Debug)]
pub struct UnsupportedSyscallError(pub Sysno);
impl std::fmt::Display for UnsupportedSyscallError {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(formatter, "unsupported syscall: {:?}", self.0)
}
}
impl std::error::Error for UnsupportedSyscallError {}
pub use fd::OpenFileLease;
pub use fd::OpenFileModel;
pub use fd::SharedOpenFileChannel;
pub use fd::SharedOpenFileError;
pub use fd::install_shared_open_file_channel;
pub use shared_open_files::ChunkedOpenFileChannel;
pub use shared_open_files::OpenFileControl;
pub use shared_open_files::OpenFileControlReply;
pub use shared_open_files::OpenFileControlTransport;
pub use tool_local::Detcore;
pub use tool_local::FileMetadata;
/// Returns whether the audited runtime policy classifies `sysno` as unsupported.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-644): Review the copied-DBT-child classification surface.
pub fn is_unsupported_syscall(sysno: Sysno) -> bool {
matches!(
syscall_classification::classify_syscall(sysno),
syscall_classification::SyscallClassification::Unsupported
)
}
/// Every syscall in the pinned x86_64 table, including the final entry.
///
/// Backends that sweep the classification table must use this rather than
/// `Sysno::iter()`, which stops one short and silently drops the last row.
pub fn all_pinned_syscalls() -> impl Iterator<Item = Sysno> {
syscall_classification::all_pinned_syscalls()
}
/// Returns whether the audited runtime policy classifies `sysno` as
/// `Determinized` — that is, Detcore either models the syscall with a handler
/// or applies an explicit deterministic refusal policy to it.
///
/// This is the complement of the refusal boundary below. A backend that
/// executes guest syscalls outside `handle_syscall_event` needs BOTH: the
/// refusal set tells it which syscalls to answer with a fixed errno, and this
/// predicate tells it which syscalls Detcore claims to determinize at all.
/// Running a `Determinized` syscall natively is a determinism hole even when it
/// is not in the refusal set, because the modelling that makes it deterministic
/// lives in a handler the backend never entered.
pub fn is_determinized_syscall(sysno: Sysno) -> bool {
matches!(
syscall_classification::classify_syscall(sysno),
syscall_classification::SyscallClassification::Determinized
)
}
/// Returns whether `sysno` is a kernel-keyring syscall (`add_key`,
/// `request_key`, `keyctl`) that Detcore hides behind a deterministic
/// `CONFIG_KEYS`-absent boundary under the default fail-closed policy.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-916): Exposed so the copied-DBT-child policy can preserve
// the same keyring isolation boundary that the reclassification (PR-848) moved
// out of the Unsupported set.
pub fn is_kernel_keyring_syscall(sysno: Sysno) -> bool {
syscall_classification::is_kernel_keyring_syscall(sysno)
}
/// Returns whether Detcore deterministically refuses `sysno` with a fixed
/// errno when the fail-closed policy is active, without consulting the host.
///
/// A backend that lets a guest execute a syscall outside Detcore's
/// `handle_syscall_event` dispatcher consults this to enforce the same fixed
/// refusal the ptrace path enforces. The only production consumer is the DBT
/// copied pre-exec child policy in `detcore-dbt`, which runs without a Detcore
/// tool; the KVM backend, like ptrace, delivers these syscalls to
/// `handle_syscall_event`. It deliberately excludes emulated / no-op /
/// host-forwarding families (credential no-ops, `timer_create`, AF_UNIX
/// autobind), because fail-closing a copied child for those would diverge from
/// the ptrace path rather than match it.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-978): Review the copied-DBT-child deterministic-refusal surface.
pub fn is_deterministically_refused_syscall(sysno: Sysno) -> bool {
syscall_classification::is_deterministically_refused_syscall(sysno)
}
/// Returns whether `sysno` is refused by the default fail-closed policy but
/// forwarded under the explicit compatibility opt-out. The legacy
/// `strict_only` name is retained for API compatibility.
pub fn is_strict_only_deterministic_refusal_syscall(sysno: Sysno) -> bool {
syscall_classification::is_strict_only_deterministic_refusal_syscall(sysno)
}
use tool_local::PosixTimers;
use tool_local::ProcessCpuTime;
pub use tool_local::ThreadState;
pub use tool_local::ThreadStats;
pub use tool_local::thread_rng_from_parent;
use tracing::debug;
use tracing::error;
use tracing::info;
use tracing::trace;
use tracing::warn;
pub use types::DetTid;
use types::*;
pub use util::punch_out_print;
use crate::resources::Permission;
use crate::resources::ResourceID;
use crate::scheduler::itimer_coalesce::GuestSigalrmTarget;
use crate::syscall_classification::SyscallClassification;
use crate::syscall_classification::classify_syscall;
use crate::syscall_classification::is_credential_identity_noop_syscall;
use crate::syscall_classification::is_futex2_enosys_syscall;
use crate::syscall_classification::is_host_kernel_probe_syscall;
use crate::syscall_classification::is_host_security_identity_probe_syscall;
use crate::syscall_classification::is_landlock_sandbox_syscall;
use crate::syscall_classification::is_mount_introspection_enosys_syscall;
use crate::syscall_classification::is_mount_ns_admin_refused_syscall;
use crate::syscall_classification::is_optional_memory_feature_syscall;
use crate::syscall_classification::is_ownership_change_noop_syscall;
use crate::syscall_classification::is_perf_event_enosys_syscall;
use crate::syscall_classification::is_privileged_admin_refused_syscall;
use crate::syscall_classification::is_privileged_observation_refused_syscall;
use crate::syscall_classification::is_process_isolation_refused_syscall;
use crate::syscall_classification::is_remap_file_pages_enosys_syscall;
use crate::syscall_classification::is_unimplemented_enosys_syscall;
use crate::syscall_classification::is_unsupported_async_ipc_syscall;
use crate::syscall_classification::is_zero_copy_pipe_syscall;
use crate::syscalls::helpers::with_guest_rip;
use crate::syscalls::helpers::with_guest_time;
use crate::syscalls::time::guest_clock_time;
use crate::tool_global::resource_request;
use crate::tool_global::trace_schedevent;
use crate::tool_global::unrecoverable_shutdown;
use crate::types::SigWrapper;
#[macro_use]
extern crate bitflags;
#[cold]
fn report_rcb_overshoot(
panic_on_rcb_overshoot: bool,
clock_value: u64,
delta_rcbs: u64,
last_timer: u64,
) {
let message = format!(
"{} prehook: PMU RCB overshoot! Clock_value: {}. Stepped forward {} RCBs, but should have trapped at {}",
reverie::SKID_OVERSHOOT_MARKER,
clock_value,
delta_rcbs,
last_timer
);
if panic_on_rcb_overshoot {
panic!("{}", message);
}
reverie::record_skid_overshoot();
error!("{}", message);
}
fn rcb_timer_overshot(delta_rcbs: u64, last_timer: u64) -> bool {
delta_rcbs > last_timer
}
/// The branches from `now` to the point the last handler armed the thread's
/// timer for: `armed` branches past `committed`, that handler's clock value
/// (`keep_timer_across_dropped_signal`). At least one: a stop at or past the
/// point fires the event at once, where the backend records any overshoot.
fn kept_timer_rcbs(committed: u64, armed: u64, now: u64) -> u64 {
committed.saturating_add(armed).saturating_sub(now).max(1)
}
fn choose_rcb_timer(
max_rcbs_remaining: u64,
current_rcbs: u64,
next_interrupt: Option<u64>,
) -> (u64, bool) {
if let Some(next_interrupt) = next_interrupt {
let interrupt_rcbs = next_interrupt - current_rcbs;
if interrupt_rcbs < max_rcbs_remaining {
return (interrupt_rcbs, false);
}
}
(max_rcbs_remaining, true)
}
/// The registers a syscall's completion leaves, computed from `entry`, the
/// context at its entry (`rip` at the `syscall` instruction): `rax` holds the
/// result, `rip` follows the instruction, and the instruction's own clobbers
/// hold `rcx` = that `rip` and `r11` = `rflags` (signal phase 1's completed
/// projection, design section 4). Only read, never written back.
fn completed_syscall_projection(
entry: libc::user_regs_struct,
result: i64,
) -> libc::user_regs_struct {
const SYSCALL_INSTRUCTION_LENGTH: u64 = 2;
let mut completed = entry;
completed.rax = result as u64;
completed.rip = entry.rip.wrapping_add(SYSCALL_INSTRUCTION_LENGTH);
completed.rcx = completed.rip;
completed.r11 = entry.eflags;
completed
}
/// What returned control to Detcore, as far as ending a timeslice is concerned.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CheckIn {
/// A syscall, signal, timer, exec or thread start: any expired slice ends.
Event,
/// A trapped `rdtsc` or `cpuid` in the middle of user code: see
/// [`ThreadState::instruction_trap_ends_timeslice`].
InstructionTrap,
}
/// The feature words the x86-64-v3 table advertises, as `(leaf, subleaf,
/// register, bits)` with registers 0-3 for EAX-EDX, for the CLI's check that
/// the host has every one of them natively
/// (<https://github.com/rrnewton/hermit/issues/4243>). Leaf 0xD, which comes
/// from the host, is not among them.
pub fn v3_feature_words() -> Vec<(u32, u32, usize, u32)> {
cpuid::v3_feature_words()
}
/// Why a v3 configuration cannot run at all, or `None`: Detcore answers a v3
/// guest from the qualified host's facts, so a configuration without them is
/// refused rather than given a guessed leaf 0xD. Whether those facts came from
/// a real qualification is Hermit's to check, at its launch functions
/// (`cpu_profile_admission::refuse_unadmitted_launch`), before any backend
/// starts (<https://github.com/rrnewton/hermit/issues/4243>).
pub fn cpu_profile_admission_refusal(cfg: &Config) -> Option<&'static str> {
(!cfg.cpu_profile.is_default() && cfg.cpu_profile_host.is_none())
.then_some("the configuration carries no qualified host facts")
}
/// The digest of the CPUID table and bound host xstate facts `cfg` selects,
/// which a recording carries so that replay can refuse a different table
/// (<https://github.com/rrnewton/hermit/issues/4243>).
pub fn virtual_cpuid_digest(cfg: &Config) -> [u8; 32] {
cpuid::InterceptedCpuid::for_config(cfg).digest(cfg.cpu_profile_host.as_ref())
}
impl<T: RecordOrReplay> Detcore<T> {
/// Whether `signal`, passed on to a guest thread without a report, could
/// have entered a handler the thread's signal-state copy never saw: where
/// the copy is kept against the kernel's own state, for any signal but
/// Reverie's preemption signal, SIGKILL and SIGSTOP, which never reach a
/// guest handler.
fn copy_misses_unreported(&self, signal: i32) -> bool {
self.cfg.signal_state_copy
&& self.cfg.sequentialize_threads
&& bundled_signal_facts::kernel_signal_state_is_guests(&self.cfg.backend)
&& signal != reverie::PERF_EVENT_SIGNAL as i32
&& signal != libc::SIGKILL
&& signal != libc::SIGSTOP
}
/// Rule 4 for the coalescing ITIMER_REAL: tell the scheduler about a
/// committed guest SIGALRM send (https://github.com/rrnewton/hermit/issues/4139).
async fn note_guest_sigalrm<G: Guest<Self>>(&self, guest: &mut G, target: GuestSigalrmTarget) {
if guest.config().sequentialize_threads {
crate::tool_global::note_sigalrm_event(
guest,
crate::tool_global::GlobalRequest::NoteGuestSigalrm(target),
)
.await;
}
}
/// The table Detcore answers a guest's CPUID instruction from. Both the
/// instruction (`handle_cpuid_event`) and `/proc/cpuinfo` take it from
/// here, so a CPU profile such as
/// <https://github.com/rrnewton/hermit/issues/4243> proposes cannot give
/// the two views different CPUs.
pub(crate) fn virtual_cpuid(&self) -> cpuid::InterceptedCpuid {
cpuid::InterceptedCpuid::for_config(&self.cfg)
}
/// The procfs files this session renders (`procfs::RenderedFiles`):
/// `/proc/cpuinfo` among them where the guest's CPUID instruction is
/// answered from [`Self::virtual_cpuid`], which is where CPUID is
/// virtualized and the backend delivers the instruction to Detcore.
///
/// Recomputed at each lookup from this run's configuration and the
/// guest's backend: one capability read, no injected call, and no state
/// shared with another session in the process. The answer is the same at
/// every lookup of a run: Reverie arms CPUID interception before the
/// guest's first callback (ptrace in its post-spawn preinit, before
/// `handle_thread_start`, and again at each `exec`; KVM from the Tool's
/// subscription before the first entry; DBT always intercepts), and a new
/// task takes its parent's.
pub(crate) fn rendered_files<G: Guest<Self>>(&self, guest: &G) -> procfs::RenderedFiles {
procfs::RenderedFiles::of(
self.cfg.virtualize_cpuid,
guest.has_cpuid_interception(),
self.cfg.container_mount_table.is_some(),
)
}
/// Give this image's descriptor table the session's answer to whether it
/// renders `/proc/cpuinfo` ([`Self::rendered_files`]), at the image's
/// first guest callback and before guest code runs, so that a placeholder
/// the table rediscovered without a guest at hand (an in-guest backend's
/// image after `exec`) is trusted exactly where the session renders the
/// file (`FileMetadata::decide_cpuinfo_placeholders`).
fn decide_cpuinfo_placeholders<G: Guest<Self>>(&self, guest: &G) -> Result<(), Errno> {
let rendered = self.rendered_files(guest).cpuinfo;
guest
.thread_state()
.file_metadata
.lock()
.unwrap()
.decide_cpuinfo_placeholders(rendered)
}
/// Signal phase 1, step I4 (design section 4, "The commit"): when the
/// caller's process holds a SIGALRM entry that is due for it, run the
/// signal pipeline ptrace runs at a signal-delivery stop (step 2), take
/// the entry inside that pipeline's `InboundSignal` turn (step 3), and
/// hand the delivery to the backend (steps 4 to 8). An entry taken that
/// the backend cannot deliver is a determinism loss.
///
/// `before_syscall` is the I4 addendum's point c: the delivery happens at
/// the current syscall's entry and the syscall, not yet run, starts again
/// after the handler. Returns whether a delivery was handed to the
/// backend; at a syscall's entry the caller then must not run the syscall.
///
/// `completed` is the syscall's result at its completion (None at its
/// entry): the signal pipeline then reads the completed register
/// projection (design section 4).
async fn deliver_due_sigalrm<G: Guest<Self>>(
&self,
guest: &mut G,
before_syscall: bool,
completed: Option<i64>,
) -> bool {
if !tool_global::sigalrm_refuses(guest, tool_global::SigalrmControl::DueEntry).await {
return false;
}
if let Some(result) = completed {
let entry = guest.regs().await;
guest.thread_state_mut().signal_register_projection =
Some(completed_syscall_projection(entry, result));
}
let pipeline = self.handle_signal_event(guest, Signal::SIGALRM).await;
guest.thread_state_mut().signal_register_projection = None;
let delivering = matches!(pipeline, Ok(Some(_)));
// A suppressed signal's entry is dropped and nothing is delivered.
if !tool_global::sigalrm_refuses(guest, tool_global::SigalrmControl::TakeEntry).await
|| !delivering
{
return false;
}
let info = scheduler::discarded_at_send::itimer_real_siginfo();
let target = reverie::SignalTarget::Process { pid: guest.pid() };
let delivered = match reverie::SignalEvent::new(libc::SIGALRM, info, target) {
Ok(event) if before_syscall => guest
.defer_signal_delivery_before_syscall(event)
.await
.is_ok(),
Ok(event) => guest.defer_signal_delivery(event).await.is_ok(),
Err(_) => false,
};
if !delivered {
self.stop_after_sigalrm_loss(guest).await;
}
if before_syscall {
// Faithful in its position relative to the guest's syscalls, but
// not in the interrupted registers the handler sees: recorded as a
// determinism loss, so verification refuses the run rather than
// compare it (round 4 of the I4 review; the faithful alternative
// is a delivery from the trampoline's return, before the syscall
// instruction runs).
tool_global::sigalrm_refuses(
guest,
tool_global::SigalrmControl::EntryDeliveryUnfaithful,
)
.await;
}
delivered
}
/// A SIGALRM entry this thread took cannot be delivered (or its virtual
/// mask cannot be read): record the determinism loss in the coordinator,
/// where the verifier reads it, and stop the process before any guest
/// instruction runs. Linux would have delivered the signal; continuing
/// would run the guest as if it never fired.
///
/// The stop is the backend's `exit_group`, not `std::process::exit`: under
/// in-guest LiteInst the latter is the guest's own `exit`, which runs the
/// guest's `atexit` handlers and ELF finalizers.
async fn stop_after_sigalrm_loss<G: Guest<Self>>(&self, guest: &mut G) -> ! {
tool_global::sigalrm_refuses(guest, tool_global::SigalrmControl::DeliveryLost).await;
guest_log::flush_unsent();
match guest
.tail_inject(
reverie::syscalls::ExitGroup::new()
.with_status(detcore_model::HERMIT_POLICY_REFUSAL_EXIT),
)
.await {}
}
/// Signal phase 1: before a callback can file a scheduler request, make
/// the scheduler's SIGALRM-blocked bit match the runtime's virtual mask. A
/// handler's start and return change that mask with no syscall, so the bit
/// may be stale at any callback (a syscall, or a trapped instruction whose
/// hooks may end a timeslice). Reads the runtime's virtual bit (one query
/// it answers from its virtual state) and publishes it when it differs from
/// the bit this thread last published, inside the thread's turn (no
/// firing commits while it holds it). Returns the bit.
async fn sync_sigalrm_eligibility<G: Guest<Self>>(&self, guest: &mut G) -> bool {
let Ok(blocked) = crate::syscalls::virtual_sigalrm_blocked(guest).await else {
self.stop_after_sigalrm_loss(guest).await
};
if blocked != guest.thread_state().sigalrm_blocked_published {
tool_global::sigalrm_refuses(
guest,
tool_global::SigalrmControl::PublishBlocked(blocked),
)
.await;
guest.thread_state_mut().sigalrm_blocked_published = blocked;
}
blocked
}
/// Publish the posthook happens-before anchors the current syscall entry
/// reached at its prehook, now that `res` is the result the guest will
/// get: after the last observer that can replace it, on every path that
/// returns a guest result, including an observer's own final errno (the
/// Codex review of https://github.com/rrnewton/hermit/pull/4048). Which
/// results are a completion is decided by [`posthook_checkpoint`]; the
/// pending names are consumed either way.
async fn publish_posthook_checkpoint<G: Guest<Self>>(
&self,
guest: &mut G,
count: u64,
res: &Result<i64, Error>,
) {
let posthook = std::mem::take(&mut guest.thread_state_mut().stats.hb_pending_posthook);
if let Some(checkpoint) = posthook_checkpoint(count, posthook, res) {
let request = guest.thread_state().mk_request(checkpoint, Permission::R);
resource_request(guest, request).await;
}
}
/// The completion of a syscall that can resume the guest (a result or an
/// errno, not a backend failure): in a process that handles SIGALRM, a due
/// entry is delivered here, where Linux would deliver a signal that became
/// pending while the call ran (design section 4, point a).
async fn complete_syscall<G: Guest<Self>>(
&self,
guest: &mut G,
res: Result<i64, Error>,
) -> Result<i64, Error> {
if self.cfg.backend.virtualizes_guest_sigalrm && guest.thread_state().sigalrm_handled {
let completed = match &res {
Ok(value) => Some(*value),
Err(Error::Errno(errno)) => Some(-i64::from(errno.into_raw())),
Err(_) => None,
};
if completed.is_some() {
self.deliver_due_sigalrm(guest, false, completed).await;
}
}
res
}
/// Signal phase 1 at a syscall's entry, before its first scheduler
/// request, in a process that handles SIGALRM: sync the scheduler's
/// blocked bit ([`Self::sync_sigalrm_eligibility`]), and while SIGALRM is
/// unblocked deliver an entry that is already due before this syscall (the
/// I4 addendum's point c). An entry is due here when it became deliverable
/// with no syscall completing (a handler returned and unblocked SIGALRM),
/// or committed during a scheduler yield inside a trapped instruction's
/// hooks; Linux delivers it before any further guest instruction. Returns
/// whether a delivery was prepared; the syscall then must not run.
async fn sigalrm_at_syscall_entry<G: Guest<Self>>(&self, guest: &mut G) -> bool {
let blocked = self.sync_sigalrm_eligibility(guest).await;
!blocked && self.deliver_due_sigalrm(guest, true, None).await
}
/// Registers a child whose native backend executed the clone syscall.
///
/// The caller must initialize the child's local thread state from the same
/// parent state and clone flags before the child enters its start hook.
/// No backend that reaches this keeps a signal-state copy; one that does
/// must call `signal_shadow::before_clone` before its native clone, as
/// `handle_clone_family` does.
// TODO-HUMAN-REVIEW(PR-743): Review the backend-neutral native child registration API.
pub async fn register_external_child<G: Guest<Self>>(
&self,
guest: &mut G,
child_tid: Tid,
child_tid_addr: usize,
flags: CloneFlags,
exit_signal: libc::c_int,
physical_ids: Option<(i32, i32)>,
) {
let child_dettid = DetTid::from_raw(child_tid.into());
guest.thread_state_mut().clone_flags = Some(flags);
if !flags.contains(CloneFlags::CLONE_THREAD) {
guest
.thread_state()
.prepare_child_process_cpu_time(child_dettid);
}
let parent_dettid = guest.thread_state().dettid;
let parent_pedigree = &mut guest.thread_state_mut().pedigree;
let child_pedigree = parent_pedigree.fork_mut();
debug!(
"[dtid {}] after registering external child (tid {}, pedigree {}) parent's pedigree becomes {}",
parent_dettid, child_dettid, child_pedigree, parent_pedigree,
);
// The kernel clone has already succeeded before an external backend
// calls this method, so these parent updates are not speculative. If
// registration observes a retired parent, create_child_thread exits
// that parent by tail injection; no continuing caller can retry or
// roll the successful clone back.
tool_global::create_child_thread(
guest,
child_dettid,
tool_global::child_tid_clear_address(flags, child_tid_addr),
Some(flags),
exit_signal,
physical_ids,
)
.await;
guest.thread_state_mut().clone_flags = None;
}
async fn passthrough<G: Guest<Self>>(
&self,
guest: &mut G,
call: Syscall,
) -> Result<i64, Error> {
self.record_or_replay_preserving_tool_errors(guest, call)
.await
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-643): Review unsupported-syscall reporting and fail-fast behavior.
/// Applies the legacy policy to an explicitly listed but unsupported syscall.
async fn handle_unsupported_syscall<G: Guest<Self>>(
&self,
guest: &mut G,
call: Syscall,
dettid: DetTid,
panic_on_unsupported_syscalls: bool,
) -> Result<i64, Error> {
if panic_on_unsupported_syscalls {
error!(
"[detcore, dtid {}] unsupported syscall: {} = ?",
dettid,
call.display(&guest.memory()),
);
if guest.config().shutdown_on_unsupported_syscall {
// A fail-closed policy decision: the run named a syscall hermit
// cannot service and `shutdown_on_unsupported_syscall` says stop.
unrecoverable_shutdown(guest, detcore_model::HERMIT_POLICY_REFUSAL_EXIT).await;
}
if guest.config().exit_on_unsupported_syscall {
return Err(Error::Tool(anyhow::Error::new(UnsupportedSyscallError(
call.number(),
))));
}
panic!("unsupported syscall: {:?}", call);
}
report_unsupported_syscall(guest, call.number()).await;
self.passthrough(guest, call).await
}
/// Linux's `restart_syscall`. When no handler runs, the kernel restarts a wait
/// that returned `ERESTART_RESTARTBLOCK` by running this call at the
/// interrupted call's address, and the wait resumes with its original deadline
/// (https://github.com/rrnewton/hermit/issues/3358). Detcore emulates those
/// waits, so it resumes them from the record the wait kept
/// (`syscalls::helpers::RestartBlock`). Any other `restart_syscall` keeps the
/// unsupported-syscall policy.
// TODO-HUMAN-REVIEW(https://github.com/rrnewton/hermit/pull/3361): Review the
// emulated `restart_syscall` resumption and the record it consumes.
async fn handle_restart_syscall<G: Guest<Self>>(
&self,
guest: &mut G,
call: Syscall,
restart_block: Option<syscalls::helpers::RestartBlock>,
dettid: DetTid,
panic_on_unsupported_syscalls: bool,
) -> Result<i64, Error> {
let rip = guest.regs().await.rip;
let Some(block) = restart_block.and_then(|block| block.resumed_at(rip)) else {
trace!(
"[detcore, dtid {}] restart_syscall at {:#x} restarts no wait Detcore emulated ({:?})",
dettid, rip, restart_block
);
return self
.handle_unsupported_syscall(guest, call, dettid, panic_on_unsupported_syscalls)
.await;
};
trace!(
"[detcore, dtid {}] restart_syscall resumes {:?}",
dettid, block
);
match block.call {
syscalls::helpers::RestartCall::Poll(poll) => {
self.handle_internal_poll_until(guest, poll, block.deadline)
.await
}
syscalls::helpers::RestartCall::Futex(futex) => {
self.handle_futex_from(
guest,
futex,
syscalls::FutexDeadline::Restart(block.deadline),
)
.await
}
syscalls::helpers::RestartCall::Nanosleep(call) => {
let deadline = block
.deadline
.expect("a claimed nanosleep keeps its absolute deadline");
self.resume_nanosleep(guest, call, deadline).await
}
}
}
/// Defense-in-depth determinism for the registers the syscall instruction
/// clobbers.
///
/// On x86-64 the `syscall` instruction destroys `%rcx` (which the CPU loads
/// with the return instruction pointer) and `%r11` (the saved `RFLAGS`).
/// After a syscall these are architecturally "undefined", so hermit must not
/// assume a well-behaved guest ignores them: a misbehaving guest that reads
/// `%rcx`/`%r11` must still observe deterministic values. Reverie's
/// injected-syscall path can otherwise leave its *private trampoline page's*
/// RIP/RFLAGS in these registers, which is both nondeterministic and an
/// information leak of tracer internals.
///
/// This forces both registers to the guest's own (deterministic) RIP and
/// RFLAGS, which is exactly what a faithful `SYSRET` would leave there. It is
/// a no-op when they already hold the canonical values (the common path), so
/// it only writes registers when something diverged. Register-preserved
/// arguments (`%rdi`..`%r9`, callee-saved) are deliberately left untouched:
/// the Linux ABI preserves them, so zeroing them would break faithful,
/// well-behaved programs.
#[cfg(target_arch = "x86_64")]
async fn canonicalize_syscall_clobbers<G: Guest<Self>>(&self, guest: &mut G) {
let mut regs = guest.regs().await;
// A faithful SYSRET leaves the return RIP in %rcx and RFLAGS in %r11.
if regs.rcx != regs.rip || regs.r11 != regs.eflags {
regs.rcx = regs.rip;
regs.r11 = regs.eflags;
if let Err(err) = guest.set_regs(regs).await {
// Best-effort: some backends cannot write registers. Do not fail
// the syscall over a defense-in-depth hardening step.
debug!(
"canonicalize_syscall_clobbers: set_regs unsupported/failed: {}",
err
);
}
}
}
/// No-op on architectures without the x86-64 `%rcx`/`%r11` syscall clobber.
#[cfg(not(target_arch = "x86_64"))]
async fn canonicalize_syscall_clobbers<G: Guest<Self>>(&self, _guest: &mut G) {}
/// Update logical thread time with any outstanding ticks of the Reverie clock. Returns a list
/// of corresponding Branch/OtherInstructions events if schedule recording is enabled.
///
/// # Arguments
///
/// * `precise_branch`: if true, there were no non-branch instructions since the last recorded branch instruction.
async fn update_logical_time_rcbs<G: Guest<Self>>(
&self,
guest: &mut G,
precise_branch: bool,
) -> Option<Vec<SchedEvent>> {
if self.cfg.max_timeslice.is_some() {
let precise_timers = !guest.config().imprecise_timers;
// TODO(T86591083): we might need to not always increment as a hack fix
// for deterministic virtual time without sequentialize threads.
let clock_value = guest.read_clock().expect("Couldn't read clock");
// N.B. clock_value does not yet include any updates for the inbound
// syscall/instruction because this function is the very first thing that
// happens in each type of handler.
let thread_state = guest.thread_state_mut();
let dettid = thread_state.dettid;
assert!(thread_state.committed_clock_value <= clock_value);
let delta_rcbs: u64 = clock_value - thread_state.committed_clock_value;
if self.cfg.use_rcb_time() {
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1151)
if thread_state.chaos_slowdown_active {
let factor = thread_state.rcb_time_multiplier();
thread_state
.thread_logical_time
.add_rcbs_with_multiplier(delta_rcbs, factor);
} else {
thread_state.thread_logical_time.add_rcbs(delta_rcbs);
}
}
thread_state.account_process_cpu_time();
thread_state.committed_clock_value = clock_value;
if thread_state.end_of_timeslice.is_some() {
if let Some(last_timer) = thread_state.last_rcb_timer
&& rcb_timer_overshot(delta_rcbs, last_timer)
&& precise_timers
{
report_rcb_overshoot(
self.cfg.panic_on_rcb_overshoot,
clock_value,
delta_rcbs,
last_timer,
);
// Preserve timer state. `pre_handler_hook` will yield through the normal
// scheduler path if the slice expired; `post_handler_hook` will otherwise
// re-arm an overshot `interrupt_at` timer.
}
// Otherwise we're very early, at the prehook of handle_thread_start.
} else {
panic!(
"Invariant violation: end_of_timeslice is None during update_logical_time_rcbs..."
)
}
trace!(
"[dtid {}] updated rcb clock, new logical time: {:?}, i.e. {}, timeslice end: {}, local rcb clock_value {:?}",
dettid,
&thread_state.thread_logical_time,
thread_state.thread_logical_time.as_nanos(),
thread_state
.end_of_timeslice
.map_or_else(|| "".to_string(), |x| format!("{}", x)),
clock_value,
);
if self.cfg.use_rcb_time() && self.cfg.should_trace_schedevent() {
let mut vec = Vec::new();
let ev = with_guest_time(
guest,
SchedEvent::branches(
dettid,
delta_rcbs
.try_into()
.expect("should not have more than 2^32 branches at once"),
),
);
let ev = if precise_branch {
with_guest_rip(guest, ev).await
} else {
ev
};
if delta_rcbs > 0 {
// We don't fill the end_rip here, because the current rip is NOT precisely the
// end of this block of branch events. Other instructions may have occured
// since the last branch.
vec.push(ev)
} else {
trace!(
"[detcore, dtid {}] Refusing to record zero-braches event: {:?}",
&ev.dettid, ev
);
}
if !precise_branch {
// This will ALWAYS record, even if the branches above are zero.
let ev2 = with_guest_time(
guest,
SchedEvent {
dettid,
op: Op::OtherInstructions,
count: 1,
start_rip: None,
end_rip: None,
end_time: None,
},
);
// Fill in end_rip because current rip represents the end of this event.
let ev2 = with_guest_rip(guest, ev2).await;
vec.push(ev2);
}
Some(vec)
} else {
None
}
} else {
None
}
}
/// A common hook called at the start of *every* handler, just after we receive
/// control from the guest.
/// The guest's registers as Detcore's hooks report them: the completed
/// projection while a SIGALRM delivery's signal pipeline runs at a
/// syscall's completion, otherwise the backend's.
async fn observed_regs<G: Guest<Self>>(&self, guest: &mut G) -> libc::user_regs_struct {
match guest.thread_state().signal_register_projection {
Some(projection) => projection,
None => guest.regs().await,
}
}
/// Detcore's handling of an inbound signal, for both of Reverie's signal
/// hooks (`Tool::handle_signal_event`, and `Tool::handle_raw_signal_event`
/// for a realtime signal): the signal takes its turn in the schedule
/// before it is delivered. Returns the signal to deliver, or `None` to
/// suppress it.
async fn handle_inbound_signal<G: Guest<Self>>(
&self,
guest: &mut G,
signal: SigWrapper,
) -> Result<Option<SigWrapper>, Errno> {
// Whether this stop comes straight from the thread's own
// `rt_sigreturn`, taken before any hook of this event clears it.
let from_sigreturn = std::mem::take(&mut guest.thread_state_mut().returned_from_sigreturn);
if signal.signal() == Some(Signal::SIGINT) && self.cfg.sigint_instakill {
warn!("Fatal: Exiting hermit container immediately upon SIGINT");
// ⚠️ NOT A REFUSAL. The operator interrupted the run; hermit examined
// nothing and decided nothing. Reporting `128 + SIGINT` is what every
// other tool reports for this, and it keeps 122 meaning one thing.
unrecoverable_shutdown(guest, detcore_model::HERMIT_SIGINT_DEATH_EXIT).await
} else {
if signal.signal() == Some(Signal::SIGALRM) {
self.normalize_reported_itimer_real(guest).await;
}
if signal.signal() == Some(Signal::SIGCHLD)
&& self.cfg.backend.reports_kernel_child_exit_signals
&& !self.child_exit_sigchld_delivers(guest).await
{
// A claimed copy that is dropped has no delivery to place, but
// the claim may have held this duplicate while the claimed copy
// is still pending (`syscalls::signal::claim_still_pending`).
let (pid, tid) = (guest.pid(), guest.tid());
let claimed = crate::syscalls::claim_still_pending(
guest.thread_state().claimed_sigchld,
|| crate::syscalls::read_thread_signal_state(pid, tid),
);
guest.thread_state_mut().claimed_sigchld = claimed;
self.keep_timer_across_dropped_signal(guest);
return Ok(None);
}
// The test-only claim fault drops the claimed copy here, as the
// ledger drops a duplicate above (`syscalls::signal::take_claim_fault`).
if signal.signal() == Some(Signal::SIGCHLD)
&& guest.thread_state().claimed_sigchld
&& crate::syscalls::take_claim_fault(crate::syscalls::ClaimFault::DropClaimed)
{
warn!(
"[dtid {}] claim fault hook: dropped the claimed SIGCHLD",
guest.thread_state().dettid
);
guest.thread_state_mut().claimed_sigchld = false;
self.keep_timer_across_dropped_signal(guest);
return Ok(None);
}
// A POSIX timer's entry: delivered with its own siginfo, or, if its
// timer was set again since it was queued, skipped as Linux skips
// it inside the dequeue, with no turn
// (<https://github.com/rrnewton/hermit/issues/4262>).
let mut timer_entry = false;
let stop_time = guest.thread_state().thread_logical_time.as_nanos();
if scheduler::discarded_at_send::records(&self.cfg) && (1..32).contains(&signal.raw()) {
use scheduler::discarded_at_send::DiscardedSignalAnswer;
use scheduler::discarded_at_send::DiscardedSignalControl;
use scheduler::posix_timer_coalesce::DeliveryAnswer;
let control = DiscardedSignalControl::Dequeue {
signal: signal.raw(),
at: stop_time,
};
let answer = tool_global::discarded_signal(guest, control).await;
timer_entry = answer == DiscardedSignalAnswer::Delivery(DeliveryAnswer::TimerEntry);
match answer {
DiscardedSignalAnswer::Delivery(DeliveryAnswer::Suppress) => {
info!(
"[dtid {}] signal {} is a stale POSIX timer entry; skipped",
guest.thread_state().dettid,
signal
);
self.keep_timer_across_dropped_signal(guest);
return Ok(None);
}
DiscardedSignalAnswer::Delivery(DeliveryAnswer::Rewrite(info)) => {
if let Err(errno) = guest.set_signal_info(info.to_user()) {
warn!(
"[dtid {}] signal {} keeps its physical siginfo: {}",
guest.thread_state().dettid,
signal,
errno
);
}
}
_ => {}
}
}
// The kernel dequeued `signal` before this stop: a hold for it ends
// here, and this stop's hooks may end the slice
// (`own_signal_turn_hold`).
if guest.thread_state().own_signal_turn_hold == Some(signal.raw()) {
guest.thread_state_mut().own_signal_turn_hold = None;
}
let timeslice = guest.thread_state().stats.timeslice_count;
self.pre_handler_hook(guest, false, CheckIn::Event).await;
// The delivery continues the thread's turn only while the slice
// that ran its `rt_sigreturn` is still open. A stop that ends the
// slice is Linux's TIF_NEED_RESCHED case: other threads run
// (`schedule()`) before this one resumes and delivers, so the
// delivery waits its place behind them instead of being taken
// right after the yield. And at most
// `MAX_SIGRETURN_CONTINUATIONS` in a row
// (`consecutive_continuations`): in every mode, that cap is what
// stops a handler that raises its own signal from holding the
// CPU for ever.
let continues_sigreturn = from_sigreturn
&& guest.thread_state().consecutive_continuations
< crate::tool_local::MAX_SIGRETURN_CONTINUATIONS
&& signal.signal() != Some(Signal::SIGCHLD)
&& guest.thread_state().stats.timeslice_count == timeslice;
// For `hermit run --verify`: a guest that faulted on an illegal
// instruction may have run code that made syscalls Detcore never
// saw, so the run reports `/`, the ancestor of every path, and
// names no host input change. A false alarm only withholds a name.
// The case this was written for no longer reaches here: on ptrace,
// a syscall issued from the untraced stub of Reverie's private page
// is followed by a `ud2`, and Reverie now ends the run at that
// SIGILL (`UntracedStubSyscall`, a policy refusal) before any Tool
// sees it. A stub syscall that never returns to the `ud2` (execve,
// exit, rt_sigreturn) raises no SIGILL at all
// (https://github.com/rrnewton/reverie/issues/1064). This stays as
// the conservative answer for any other illegal instruction. A
// guest that first rewrites or replaces the page's code is reported
// when it does so (`record_untraced_code_change`).
if signal.signal() == Some(Signal::SIGILL) && self.cfg.record_host_inputs {
tool_global::record_host_mutation(guest, String::from("/")).await;
}
let dettid = guest.thread_state().dettid;
let mycount = guest.thread_state().stats.signal_count;
info!(
"[dtid {}] handling inbound signal (#{}) {}",
dettid, mycount, signal
);
guest.thread_state_mut().stats.count_signal();
let time = &guest.thread_state().thread_logical_time;
let nanos = time.as_nanos();
if self.cfg.sequentialize_threads && self.cfg.should_trace_schedevent() {
trace_schedevent(
guest,
SchedEvent {
dettid,
op: Op::SignalReceived(signal),
count: 1,
start_rip: None,
end_rip: None,
end_time: Some(nanos),
},
true,
)
.await;
}
// The delivery of a `SIGCHLD` this thread claimed is placed, so its
// turn is not deferred behind runnable work
// (`ThreadState::claimed_sigchld`).
if signal.signal() == Some(Signal::SIGCHLD)
&& std::mem::take(&mut guest.thread_state_mut().claimed_sigchld)
{
tool_global::child_exit_sigchld(
guest,
scheduler::child_exit_sigchld::ChildExitSigchldControl::ClaimedDelivery,
)
.await;
}
// Read-write marks this as the delivery itself, which the
// scheduler's write-only wakes of a signaled sleeper are not
// (`Scheduler::thread_timer_copies`).
let mut request = guest
.thread_state_mut()
.mk_request(ResourceID::InboundSignal(signal), Permission::RW);
request.continues_sigreturn = continues_sigreturn;
resource_request(guest, request).await;
// Under `--pipe-wake-latency`, a delivery that ends the process
// dooms its threads at this committed turn, before their
// host-timed removal (`scheduler::pipe_wake`).
if self.cfg.pipe_wake_latency_in_effect().is_some()
&& match crate::syscalls::signal_shadow::copied_dispositions_for(
guest,
signal.raw(),
) {
Some(copied) => crate::syscalls::default_action_ends_process(
copied.ignored,
copied.caught,
signal.raw(),
),
None => crate::syscalls::read_kernel_signal_state(guest.pid(), guest.tid())
.is_ok_and(|state| state.default_action_ends_process(signal.raw())),
}
{
tool_global::record_fatal_signal(guest).await;
}
// A timer entry's siginfo, with its overrun counted now that the
// delivery's turn is granted, where the handler's time begins.
if timer_entry {
use scheduler::discarded_at_send::DiscardedSignalAnswer;
use scheduler::posix_timer_coalesce::DeliveryAnswer;
let control = scheduler::discarded_at_send::DiscardedSignalControl::DeliveryInfo {
signal: signal.raw(),
at: guest.thread_state().thread_logical_time.as_nanos(),
};
if let DiscardedSignalAnswer::Delivery(DeliveryAnswer::Rewrite(info)) =
tool_global::discarded_signal(guest, control).await
&& let Err(errno) = guest.set_signal_info(info.to_user())
{
warn!(
"[dtid {}] signal {} keeps its physical siginfo: {}",
guest.thread_state().dettid,
signal,
errno
);
}
}
if self
.end_thread_group_before_fatal_delivery(guest, signal)
.await
{
// As an exit_group does after its grant, file nothing more:
// the group's turns are held until it has ended, which needs
// this thread to die (Claude review of
// https://github.com/rrnewton/hermit/pull/4387, finding 4).
info!(
"[dtid {}] finish delivering signal (#{}) {}",
dettid, mycount, signal
);
return Ok(Some(signal));
}
// A delivered signal may be caught, ignored, or terminate the
// thread group. Reading the lists is harmless; the collected
// wakes remain inert unless the ptrace exit callback later reports
// that this exact signal caused physical exit.
self.stage_thread_group_robust_list_wakes(
guest,
tool_local::RobustListExit::Signal(signal.raw()),
)
.await;
info!(
"[dtid {}] finish delivering signal (#{}) {}",
dettid, mycount, signal
);
self.record_handler_restore_mask(guest, signal).await;
self.post_handler_hook(guest, CheckIn::Event).await;
Ok(Some(signal))
}
}
/// Take back the POSIX timer copies a successful exec flushes
/// (`Scheduler::take_exec_timer_flush`), one zero-timeout
/// `rt_sigtimedwait` each, before the new image runs. Linux discards a
/// queued timer signal at exec (`flush_itimer_signals`) and keeps every
/// other pending signal; Hermit sends a timer's expiration as an ordinary
/// signal, which exec would keep
/// (<https://github.com/rrnewton/hermit/issues/4184>). A copy that cannot
/// be singled out refuses the run by name.
async fn flush_exec_timer_copies<G: Guest<Self>>(&self, guest: &mut G) {
let dettid = guest.thread_state().dettid;
let refusal = match tool_global::exec_timer_flush(guest).await {
Err(reason) => Some(reason),
Ok(flush) => {
let mut refusal = None;
// The thread's own queue is dequeued first, as Linux does.
for set in [flush.own, flush.shared] {
for signo in
(1..=64).filter(|signo| set & scheduler::kernel_signal_bit(*signo) != 0)
{
match syscalls::take_pending_signal(guest, signo).await {
Ok(taken) if taken == signo as i64 => info!(
"[detcore, dtid {}] exec flushed a POSIX timer's pending signal {}",
dettid, signo
),
other => {
refusal = Some(format!(
"taking back a POSIX timer's pending signal {signo} at exec answered {other:?}"
));
break;
}
}
}
}
refusal
}
};
if let Some(reason) = refusal {
let message = format!(
"hermit: thread {dettid} exec'd with a POSIX timer's signal possibly pending: {reason}; Linux discards a queued timer signal at exec, and Detcore cannot single this one out, so the run is refused (https://github.com/rrnewton/hermit/issues/4184)"
);
error!("{message}");
let _ = writeln!(crate::util::RetryingStderr, "{message}");
tool_global::unrecoverable_shutdown(guest, detcore_model::HERMIT_POLICY_REFUSAL_EXIT)
.await;
}
}
/// After a dropped signal's delivery stop, which made no other hook run:
/// request the thread's timer event again for the same point of its
/// branch clock as the last handler armed it (`post_handler_hook`). The
/// stop is a Tool callback, which cancels the event, and nothing else
/// re-arms it, so a thread spinning in user code would run on unpreempted
/// until its next system call: measured, a chaos schedule whose sibling
/// took a dropped kernel `SIGCHLD` spun about 4e10 branches past its
/// 2e7-branch slice (https://github.com/rrnewton/hermit/issues/4304). The
/// point is fixed by the clock value of that handler, not by when the
/// host-timed stop came, and no logical time is charged here, so the stop
/// stays invisible to the schedule.
fn keep_timer_across_dropped_signal<G: Guest<Self>>(&self, guest: &mut G) {
if !self.cfg.use_preemption_timer() {
return;
}
let thread_state = guest.thread_state();
let Some(armed) = thread_state.last_rcb_timer else {
return;
};
let thread_state_committed = thread_state.committed_clock_value;
// The stop cancelled the event: an unreadable clock leaves the thread
// unpreempted, so it fails closed, as every handler's clock read does.
let now = guest.read_clock().expect("Couldn't read clock");
assert!(thread_state_committed <= now);
let schedule = TimerSchedule::Rcbs(kept_timer_rcbs(thread_state_committed, armed, now));
let armed = if guest.config().imprecise_timers {
guest.set_timer(schedule)
} else {
guest.set_timer_precise(schedule)
};
armed.expect("Failed to set timer");
}
/// Install Linux timer metadata only for the exact first kept expiry
/// claimed before this callback advances logical time.
async fn normalize_reported_itimer_real<G: Guest<Self>>(&self, guest: &mut G) {
use scheduler::discarded_at_send::DiscardedSignalAnswer;
use scheduler::discarded_at_send::DiscardedSignalControl;
if !scheduler::discarded_at_send::records(&self.cfg) {
return;
}
let Some(info) = guest.signal_info() else {
return;
};
if info == scheduler::discarded_at_send::itimer_real_siginfo() {
// A claimed copy may have requeued after a blocked resume. Its
// canonical payload is already installed; do not claim a newer
// publication that arrived while this copy was in flight.
return;
}
if tool_global::discarded_signal(guest, DiscardedSignalControl::ClaimItimerReal).await
!= DiscardedSignalAnswer::ItimerReal(true)
{
return;
}
if let Err(errno) =
guest.set_signal_info(scheduler::discarded_at_send::itimer_real_siginfo())
{
tool_global::report_determinism_loss(
guest,
format!("ITIMER_REAL_SIGINFO_UNAVAILABLE: owned timer delivery cannot install Linux siginfo: {errno}"),
).await;
guest_log::flush_unsent();
match guest
.tail_inject(
reverie::syscalls::ExitGroup::new()
.with_status(detcore_model::HERMIT_POLICY_REFUSAL_EXIT),
)
.await {}
}
}
/// Whether a `SIGCHLD` that stopped this thread is delivered, on a backend
/// that reports child-exit publication, where both the kernel and the
/// scheduler send a child's exit `SIGCHLD` (`scheduler::child_exit_sigchld`).
/// The first one delivered for a child's exit stands for it, and a copy
/// of either kind that arrives after it is not delivered. The scheduler's
/// copy (`SI_USER` from this tracer) is delivered with the siginfo Linux
/// gives the parent. Any other `SIGCHLD` is delivered as it is.
async fn child_exit_sigchld_delivers<G: Guest<Self>>(&self, guest: &mut G) -> bool {
use scheduler::child_exit_sigchld::ChildExitSigchldAnswer;
use scheduler::child_exit_sigchld::OwnCopy;
let Some(info) = guest.signal_info() else {
return true;
};
// A delivery stop: a kept copy's `InboundSignal` request follows.
let control = scheduler::child_exit_sigchld::taken_control(&info, true);
match tool_global::child_exit_sigchld(guest, control).await {
ChildExitSigchldAnswer::Claimed(dropped) => !dropped,
ChildExitSigchldAnswer::OwnCopy(OwnCopy::Duplicate) => false,
ChildExitSigchldAnswer::OwnCopy(OwnCopy::Deliver(own))
| ChildExitSigchldAnswer::ClaimedFor(own) => {
if let Err(errno) = guest.set_signal_info(own.to_bytes()) {
warn!(
"[dtid {}] the SIGCHLD for child {} keeps its siginfo: {}",
guest.thread_state().dettid,
own.pid,
errno
);
}
true
}
_ => true,
}
}
async fn pre_handler_hook<G: Guest<Self>>(
&self,
guest: &mut G,
precise_branch: bool,
boundary: CheckIn,
) {
// A handler that left early (an error return) may not have cleared
// these; no request made by this new handler belongs to that syscall,
// and a wake that syscall made no longer ends the slice.
guest.thread_state_mut().in_uncharged_bootstrap_syscall = false;
guest.thread_state_mut().yield_after_futex_wake = false;
// Any event ends the window right after an `rt_sigreturn`; a signal
// stop reads the flag before calling this hook.
guest.thread_state_mut().returned_from_sigreturn = false;
let dettid = guest.thread_state().dettid;
let evs = self.update_logical_time_rcbs(guest, precise_branch).await;
if guest.thread_state().guest_past_first_execve() {
detlog_debug!(
"(pre) registers [dtid {}][rcbs {}]. {}",
dettid,
guest.thread_state().thread_logical_time.rcbs(),
self.observed_regs(guest).await.display()
);
}
trace!(
"prehook [dtid {}] Updating rcbs and checking time remaining.",
dettid
);
if let Some(vec) = evs {
for ev in vec {
trace_schedevent(guest, ev, false).await;
}
}
self.pipe_hand_off_if_due(guest).await;
self.end_timeslice_if_needed(guest, boundary).await;
}
// AUTONOMOUS-BOT-IMPLEMENTED
/// Yield when accumulated logical time reaches the syscall-boundary target deadline.
async fn end_timeslice_if_needed<G: Guest<Self>>(&self, guest: &mut G, boundary: CheckIn) {
let thread_state = guest.thread_state();
// A thread that sent a signal to its own process keeps its turn until
// it dequeues that signal (`own_signal_turn_hold`).
if let Some(signal) = thread_state.own_signal_turn_hold {
trace!(
"[dtid {}] keeps its turn until it takes signal {}",
thread_state.dettid, signal
);
return;
}
let Some(slice_end) = thread_state.end_of_timeslice else {
return;
};
let ends = match boundary {
CheckIn::Event => thread_state.timeslice_expired(),
CheckIn::InstructionTrap => thread_state.instruction_trap_ends_timeslice(&self.cfg),
};
if !ends {
return;
}
trace!(
"[dtid {}] logical time {} reached timeslice target {}",
thread_state.dettid,
thread_state.thread_logical_time.as_nanos(),
slice_end
);
self.end_timeslice(guest).await;
}
/// A common hook called at the end of *every* handler, just before returning control
/// to the guest. This enforces the logical target and resets the PMU maximum timer.
///
/// However, note that the thread's timeslice (turn) may have expired DURING this handler.
/// Therefore the timeslice can end in the posthook as well as in the prehook.
async fn post_handler_hook<G: Guest<Self>>(&self, guest: &mut G, boundary: CheckIn) {
// Only `kill` and `rt_sigqueueinfo` set the hold, and they wake no
// futex waiter (https://github.com/rrnewton/hermit/issues/4034).
assert!(
guest.thread_state().own_signal_turn_hold.is_none()
|| !guest.thread_state().yield_after_futex_wake,
"a futex wake must not end the turn a signal to the sender's own process holds"
);
if std::mem::take(&mut guest.thread_state_mut().yield_after_futex_wake) {
// A turn of its own, outside the timeslice bookkeeping: the slice
// keeps its deadlines, draws nothing from the thread's chaos PRNG
// and consumes no recorded preemption point, so a replay that
// makes the same decision stays aligned with its record. (Under
// a random `--sched-heuristic` the turn's selection draws from the
// run queue's generator, as every turn does, identically in record
// and replay.)
trace!(
"posthook [dtid {}] FUTEX_WAKE woke a waiter; yielding to it",
guest.thread_state().dettid
);
let request = Self::yield_request(guest);
resource_request(guest, request).await;
}
self.pipe_hand_off_if_due(guest).await;
self.end_timeslice_if_needed(guest, boundary).await;
let dettid = guest.thread_state().dettid;
let mut current_time = guest.thread_state().thread_logical_time.as_nanos();
if let Some(mut max_timeslice_end) = guest.thread_state().max_timeslice_end {
assert!(guest.config().max_timeslice.is_some());
let mut replay_rcb_end = guest.thread_state().replay_rcb_end;
// TODO: get rid of fractional NANOS_PER_RCB so it's clear that this does not lose precision:
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1151)
let clock_multiplier = guest.config().clock_multiplier.unwrap_or(1.0)
* guest.thread_state().rcb_time_multiplier().as_f64();
let epsilon = Duration::from_nanos((NANOS_PER_RCB * clock_multiplier).ceil() as u64);
if replay_rcb_end.is_none() && current_time + epsilon > max_timeslice_end {
if guest.thread_state().own_signal_turn_hold.is_some() {
// No guest instruction runs before the delivery stop that
// ends the hold, and that stop's post-hook ends this slice
// and sets the next timer (`own_signal_turn_hold`).
trace!(
"posthook [dtid {}] past PMU maximum {} while it keeps its turn",
dettid, max_timeslice_end
);
return;
}
trace!(
"posthook [dtid {}] less than one RCB remains before PMU maximum {}; ending slice",
dettid, max_timeslice_end
);
self.end_timeslice(guest).await;
max_timeslice_end = guest
.thread_state()
.max_timeslice_end
.expect("ending a PMU-backed timeslice must install a new maximum");
current_time = guest.thread_state().thread_logical_time.as_nanos();
replay_rcb_end = guest.thread_state().replay_rcb_end;
}
if replay_rcb_end.is_none() && current_time + epsilon > max_timeslice_end {
panic!(
"Ended time slice, but current time {} is still beyond PMU maximum {}",
current_time, max_timeslice_end
);
}
let current_rcbs = guest.thread_state().thread_logical_time.rcbs();
let current_pmu_rcbs = guest.thread_state().committed_clock_value;
let (ns_remaining, max_rcbs_remaining) = if let Some(replay_rcb_end) = replay_rcb_end {
assert!(
replay_rcb_end > current_pmu_rcbs,
"recorded PMU RCB deadline {} is not ahead of current {}",
replay_rcb_end,
current_pmu_rcbs
);
let logical_remaining = if max_timeslice_end > current_time {
max_timeslice_end - current_time
} else {
LogicalTime::ZERO
};
(logical_remaining, replay_rcb_end - current_pmu_rcbs)
} else {
let logical_remaining = max_timeslice_end - current_time;
(
logical_remaining,
logical_remaining.into_rcbs_with_multiplier(clock_multiplier),
)
};
let next_interrupt = self
.cfg
.use_rcb_time()
.then(|| {
guest
.thread_state()
.interrupt_at
.range((current_rcbs + 1)..)
.next()
.copied()
})
.flatten();
let (rcbs_remaining, timer_is_max) =
choose_rcb_timer(max_rcbs_remaining, current_rcbs, next_interrupt);
if let Some(next_interrupt) = next_interrupt {
debug!(
"[dtid: {}] current rcbs: {}, next interrupt_at: {}",
dettid, current_rcbs, next_interrupt
)
}
trace!(
"posthook [dtid {}] {} remaining before PMU maximum ({} rcbs).",
dettid, ns_remaining, rcbs_remaining,
);
if replay_rcb_end.is_none() && ns_remaining.is_zero() {
panic!(
"Timer invariant broken: we should not exit a handler with 0 timeslice remaining."
);
}
assert!(rcbs_remaining > 0);
// The maximum also governs logical expiry and instruction traps.
// Delivery capability gates only the physical arm and its record.
if guest.config().use_preemption_timer() {
trace!(
"posthook [dtid {}] Resetting timer to {:?} RCBs in the future (current {})",
dettid,
rcbs_remaining,
guest.thread_state().thread_logical_time.rcbs()
);
{
let thread_state = guest.thread_state_mut();
thread_state.last_rcb_timer = Some(rcbs_remaining);
thread_state.last_rcb_timer_is_max = timer_is_max;
}
if guest.config().imprecise_timers {
guest
.set_timer(TimerSchedule::Rcbs(rcbs_remaining))
.expect("Failed to set timer");
} else {
guest
.set_timer_precise(TimerSchedule::Rcbs(rcbs_remaining))
.expect("Failed to set timer");
}
} else {
guest.thread_state_mut().last_rcb_timer = None;
guest.thread_state_mut().last_rcb_timer_is_max = false;
}
} else {
assert!(guest.config().max_timeslice.is_none());
guest.thread_state_mut().last_rcb_timer = None;
guest.thread_state_mut().last_rcb_timer_is_max = false;
}
if guest.thread_state().guest_past_first_execve() {
detlog_debug!(
"(post) registers [dtid {}][rcbs {}]. {}",
dettid,
guest.thread_state().thread_logical_time.rcbs(),
self.observed_regs(guest).await.display(),
);
}
}
/// End this logical timeslice and talk to the scheduler before continuing.
///
/// Effects
/// - ends timeslice (mutating thread stats and both deadlines)
/// - priority change / yield RPC
async fn end_timeslice<G: Guest<Self>>(&self, guest: &mut G) {
self.end_timeslice_with_sched_yield(guest, false).await;
}
async fn end_timeslice_for_sched_yield<G: Guest<Self>>(&self, guest: &mut G) {
self.end_timeslice_with_sched_yield(guest, true).await;
}
async fn end_timeslice_with_sched_yield<G: Guest<Self>>(
&self,
guest: &mut G,
mut explicit_sched_yield: bool,
) {
let chaos = guest.config().chaos;
loop {
let thread_state = guest.thread_state();
let dettid = thread_state.dettid;
let end_time = thread_state.thread_logical_time.as_nanos();
info!(
"[detcore, dtid {}] ending timeslice T{}. {} syscalls and {} signals this timeslice.",
dettid,
thread_state.stats.timeslice_count,
thread_state.stats.timeslice_syscall_count,
thread_state.stats.timeslice_signal_count,
);
let maybe_prio = guest.thread_state_mut().next_timeslice(&self.cfg);
// Depending on chaos mode, a received timer event is either a preemption or a changepoint
let req = if let Some(prio) = maybe_prio {
Self::priority_changepoint_request(guest, end_time, prio)
} else if chaos {
Self::random_priority_changepoint_request(guest, end_time)
} else if explicit_sched_yield && self.cfg.replay_schedule_from.is_none() {
Self::sched_yield_request(guest)
} else {
Self::yield_request(guest)
};
resource_request(guest, req).await;
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1151)
// Multiple scheduler commits can occur without an intervening
// conditional branch. Exact-RCB replay represents those as
// adjacent zero-RCB slices, which must be consumed before the
// guest resumes.
if !guest.thread_state().timeslice_expired() {
break;
}
explicit_sched_yield = false;
}
}
/// Hash the guest REGISTER FILE and log it.
///
/// # The sampling boundary: GUEST-LOGICAL CONTROL, never handler interior
///
/// This is called from exactly one place -- immediately after a syscall has finished and its
/// result has been written back, before the guest resumes. At that instant the guest
/// LOGICALLY HAS CONTROL: the architectural state is what the guest itself would observe at
/// its own RIP, and it is the same instant at which stack and heap are already hashed.
///
/// It is deliberately NOT sampled anywhere inside a tool handler. A backend that runs its
/// handler IN-GUEST (sabre, liteinst, e9patch) executes instructions the ptrace reference
/// never executes, using guest registers as scratch while it does. Registers there
/// legitimately differ across backends, so comparing them would report correct behaviour as a
/// divergence and burn the prefix-depth ratchet on artifacts. Handler-interior state is out of
/// the domain, not excluded from it by a filter -- the same "define the domain" rule the heap
/// definition follows.
///
/// # What is in the hash, and what is deliberately not
///
/// Included: the general-purpose registers the guest can observe, `rip`, `rsp`, `rflags`,
/// `orig_rax`, and the TLS bases `fs_base`/`gs_base`.
///
/// EXCLUDED, with reasons rather than by convenience:
/// * `rcx` and `r11` -- architecturally clobbered by the `SYSCALL` instruction, which stores
/// the return RIP and RFLAGS in them. They carry no information beyond `rip`/`eflags`, which
/// ARE hashed, and a patching backend that reaches the kernel by some route other than a
/// bare `SYSCALL` will leave different values there for a reason that is not a determinism
/// defect.
/// * The segment selectors `cs`/`ss`/`ds`/`es`/`fs`/`gs` -- constant for a 64-bit userspace
/// guest, so they add no signal; the TLS BASES are what a guest actually observes and those
/// are hashed.
fn detlog_registers<G: Guest<Self>>(
&self,
guest: &mut G,
regs: &libc::user_regs_struct,
seq: u64,
) {
if !self.cfg.detlog_regs {
return;
}
// COST TIER: cadence 1 == full (every control point); N > 1 == spot-check every Nth.
// The cadence index is a PER-THREAD counter, NOT a shared one: a global atomic would be
// incremented in whatever order threads happen to reach it, so the cadence -- and
// therefore which points got sampled -- would itself be nondeterministic. A determinism
// instrument must not have a nondeterministic sampling schedule.
//
// It is `stats.syscall_count`, which starts at ZERO, rather than the syscall ORDINAL used
// in the log (which starts at 2). With the ordinal, a guest whose control points never
// land on a multiple of the cadence emitted NOTHING and the run still reported PASS -- a
// spot-tier green backed by zero samples. Indexing from zero makes the first control point
// of every thread always sampled, so a spot-tier run can never be silently empty.
let cadence = self.cfg.detlog_regs_cadence.max(1);
let index = {
let stats = &mut guest.thread_state_mut().stats;
let i = stats.regs_sample_index;
stats.regs_sample_index = i.saturating_add(1);
i
};
let _ = seq;
if !index.is_multiple_of(cadence) {
return;
}
let tier = if cadence == 1 {
"full".to_string()
} else {
format!("spot-1/{cadence}")
};
let mut bytes = Vec::with_capacity(19 * 8);
for v in [
regs.rax,
regs.rbx,
regs.rdx,
regs.rsi,
regs.rdi,
regs.rbp,
regs.rsp,
regs.r8,
regs.r9,
regs.r10,
regs.r12,
regs.r13,
regs.r14,
regs.r15,
regs.rip,
regs.eflags,
regs.orig_rax,
regs.fs_base,
regs.gs_base,
] {
bytes.extend_from_slice(&v.to_le_bytes());
}
detlog!(
"[registers][dtid {}] control_point=syscall-exit tier={} {}",
guest.thread_state().dettid,
tier,
Digest::new(&bytes)
);
}
fn detlog_memory_maps<G: Guest<Self>>(&self, guest: &mut G) -> Result<(), reverie::Error> {
if !(self.cfg.detlog_stack || self.cfg.detlog_heap) {
// Don't incur the *significant* performance penalty for reading
// /proc/maps unless one of these flags is enabled.
return Ok(());
}
// ...and don't incur it when nothing would observe the record either.
//
// The hash below is an argument to `detlog!`, so `tracing` already skips
// it when INFO is disabled. Enumerating the maps is NOT: it happens
// before the macro, on every syscall, whether or not a record is ever
// written. Measured on a QEMU/Linux boot with `RUST_LOG` unset, where
// each run emitted 123 bytes of log in total: no flag 43.71s,
// `--detlog-stack` 190.37s (4.36x), `--detlog-heap` 207.90s (4.76x).
// `--detlog-regs` was already inert because everything it does before
// its own `detlog!` is trivial; this restores the same property here.
//
// Skipping is invisible to the guest: enumerating maps and hashing guest
// memory are host-side observations of the tracee that neither issue a
// guest syscall nor advance virtual time, so a run that skips them
// executes the same guest instruction stream as one that does not.
if !detlog_observed!() {
return Ok(());
}
// Out-of-process backends (e.g. KVM) report their guest memory regions
// directly, because `guest.pid()` is the host VMM process there and its
// `/proc/<pid>/maps` describes the VMM, not the guest address space.
// Reading those host addresses through `guest.memory()` (guest-address
// space) would fault and abort the syscall. When the backend supplies
// regions, hash those guest ranges; otherwise fall back to the ptrace
// path of parsing `/proc/<pid>/maps`.
if let Some(regions) = guest.detlog_memory_regions() {
for region in regions {
let want = match region.kind {
reverie::DetlogRegionKind::Stack => self.cfg.detlog_stack,
reverie::DetlogRegionKind::Heap => self.cfg.detlog_heap,
};
if !want {
continue;
}
let dettid = guest.thread_state().dettid;
detlog!(
"[memory][dtid {}] {:?} {:#x}-{:#x}->{}",
dettid,
region.kind,
region.start,
region.end,
procmaps::compute_hash_range(guest, region.start, region.end)?
)
}
return Ok(());
}
// Where Detcore answers brk from its model (a launcher run), the
// kernel's `[heap]` is the launcher's, not the guest's.
let modeled_brk = guest
.thread_state()
.memory_metadata
.lock()
.expect("memory metadata mutex poisoned")
.brk_model
.is_some();
let mut labelled_heap = false;
for mmap in procmaps::from_pid(guest.pid(), |map| match map.pathname {
procmaps::MMapPath::Stack if self.cfg.detlog_stack => true,
procmaps::MMapPath::Heap if self.cfg.detlog_heap && !modeled_brk => true,
_ => false,
})? {
labelled_heap |= matches!(mmap.pathname, procmaps::MMapPath::Heap);
let dettid = guest.thread_state().dettid;
detlog!(
"[memory][dtid {}] {}->{}",
dettid,
procmaps::display(&mmap),
procmaps::compute_hash(guest, &mmap)?
)
}
// The kernel labels `[heap]` only for `[mm->start_brk, mm->brk)`. Under a
// backend that loads the guest itself (DynamoRIO) that break belongs to
// the loader, so the guest's heap is an unlabelled anonymous mapping and
// the filter above matches nothing. Emitting no record there is worse
// than a wrong one: downstream a zero-record heap comparison reads as
// "compared and matched" rather than "never measured". Fall back to the
// brk range Detcore observed, which identifies the heap on every backend.
// Where Detcore models the break, the observed range is the model's.
if self.cfg.detlog_heap && !labelled_heap {
self.detlog_brk_heap(guest)?;
}
Ok(())
}
/// Emit the `[heap]` record from the observed program break, for backends
/// where the kernel does not label the guest's heap.
///
/// Reports `[start_brk, brk)` -- the range Detcore observed -- taking the
/// non-address columns from the enclosing anonymous mapping, so the record
/// is textually comparable with the labelled record another backend
/// produces for the same guest.
///
/// ⚠️ THE EXTENT IS THE OBSERVED BREAK, NOT THE MAPPING THAT CONTAINS IT,
/// and the two are not the same region. The selector below admits any
/// anonymous mapping with `address.0 <= start && end <= address.1`, which
/// is a SUPERSET by construction. Reporting that mapping instead would make
/// the comparability claim above true only when the arena happens to
/// coincide with the break, and would fold non-heap bytes into the digest.
/// On the backend this path exists for, the premise is that the LOADER owns
/// the break, so those extra bytes are loader arena -- the least
/// reproducible region in the process. A silently empty record would become
/// a loudly divergent one, for a reason that is not the guest's heap.
///
/// The labelled path above hashes its mapping directly, which is correct
/// there because the kernel defines `[heap]` as exactly `[start_brk, brk)`.
/// Both paths therefore report the same quantity.
fn detlog_brk_heap<G: Guest<Self>>(&self, guest: &mut G) -> Result<(), reverie::Error> {
let Some((start, end)) = guest
.thread_state()
.memory_metadata
.lock()
.expect("memory metadata mutex poisoned")
.brk_heap_range()
else {
return Ok(());
};
let modeled = guest
.thread_state()
.memory_metadata
.lock()
.expect("memory metadata mutex poisoned")
.brk_model
.is_some();
if modeled {
return self.detlog_modeled_brk_heap(guest, start, end);
}
let enclosing = procmaps::from_pid(guest.pid(), |map| {
matches!(map.pathname, procmaps::MMapPath::Anonymous)
&& map.address.0 <= start
&& end <= map.address.1
})?;
let Some(mmap) = enclosing.into_iter().next() else {
return Ok(());
};
let dettid = guest.thread_state().dettid;
detlog!(
"[memory][dtid {}] {}->{}",
dettid,
procmaps::display_range_as(&mmap, start, end, "[heap]"),
procmaps::compute_hash_range(guest, start, end)?
);
Ok(())
}
/// The heap records where Detcore models the break (a launcher run): one
/// per anonymous mapping that holds part of the modeled `[start, end)`,
/// clipped to it, as Linux's `[heap]` is one mapping per piece once an
/// mprotect splits it, and up to the break's page-aligned end, where the
/// kernel's `[heap]` mapping ends. So the records are the ones the
/// kernel's `[heap]` mappings give where the kernel's break is the
/// guest's, and a heap whose pages the guest unmapped gives none, as
/// Linux's label goes with its last page.
fn detlog_modeled_brk_heap<G: Guest<Self>>(
&self,
guest: &mut G,
start: u64,
end: u64,
) -> Result<(), reverie::Error> {
// The page brk(2) rounds the break up to.
let end = end.checked_add(4095).map_or(end, |end| end & !4095);
let pieces = procmaps::from_pid(guest.pid(), |map| {
matches!(map.pathname, procmaps::MMapPath::Anonymous)
&& map.address.0 < end
&& start < map.address.1
})?;
let dettid = guest.thread_state().dettid;
for piece in pieces {
let (from, to) = (piece.address.0.max(start), piece.address.1.min(end));
detlog!(
"[memory][dtid {}] {}->{}",
dettid,
procmaps::display_range_as(&piece, from, to, "[heap]"),
procmaps::compute_hash_range(guest, from, to)?
);
}
Ok(())
}
}
/// Render a finished syscall for the `finish syscall` DETLOG line.
///
/// Output pointers are dereferenced only when the kernel can have written
/// them. Linux leaves the output buffer of the syscalls listed in
/// [`failure_leaves_outputs_unwritten`] untouched when they fail with any errno
/// other than EFAULT, so rendering it would publish whatever the guest
/// happened to have there -- typically uninitialized stack -- as if it were a
/// result, and two otherwise identical runs would diverge on it
/// (<https://github.com/rrnewton/hermit/issues/3153>). For those syscalls such
/// a failure renders the pointer arguments without their pointees; the errno
/// is printed next to this rendering from the result itself.
///
/// EFAULT keeps the outputs rendered, because it is also the error of a copy
/// that faulted part way: `copy_to_user()` may already have stored a prefix of
/// the struct, which the guest can read and DETLOG must show. A tool error is
/// not a guest errno and proves nothing about the buffer, so it keeps them
/// rendered too.
///
/// Every other syscall keeps rendering its outputs on failure, because some
/// Linux syscalls do write an output on an error return (for example
/// `gettimeofday` stores `tv` before it faults on a bad `tz`). Hiding such an
/// output would remove real evidence from DETLOG.
fn display_syscall_finished<'a, M: MemoryAccess>(
syscall: &'a Syscall,
memory: &'a M,
result: &Result<i64, Error>,
) -> reverie::syscalls::Display<'a, M, Syscall> {
match syscall {
Syscall::Fstat(_) => syscall.display(memory), //FIXME: T136880615 - fstat structure isn't fully deterministic yet
_ if failure_proves_outputs_unwritten(result)
&& failure_leaves_outputs_unwritten(syscall) =>
{
syscall.display(memory)
}
_ => syscall.display_with_outputs(memory),
}
}
/// Syscalls whose output buffer Linux does not write when the syscall fails
/// with an errno other than EFAULT, restricted to those whose output the pinned Reverie formatter dereferences.
///
/// This is deliberately a list of syscalls PROVEN not to write on failure,
/// rather than a list of exceptions that do: a syscall missing from it keeps
/// its outputs rendered, which can at worst show stale guest memory, whereas a
/// syscall wrongly missing from an exception list would silently hide a value
/// the kernel really wrote.
///
/// Kernel behaviour (Linux `fs/stat.c`, `kernel/time/posix-timers.c`):
///
/// - `stat`, `lstat`, `fstat`, `newfstatat`: `vfs_stat()`, `vfs_lstat()`,
/// `vfs_fstat()` and `vfs_fstatat()` return their error before
/// `cp_new_stat()` is called, so the `struct stat` is never copied out.
/// Hermit's `handle_stat_family` likewise returns the injected syscall's
/// error before it rewrites the buffer.
/// - `statx`: `do_statx()` returns the `vfs_statx()` error before
/// `cp_statx()` copies the `struct statx` out.
/// - `clock_gettime`: `put_timespec64()` runs only when
/// `kc->clock_get_timespec()` succeeded (`if (!error && put_timespec64(..))`),
/// and an unknown clock returns `-EINVAL` before that. Hermit's
/// `handle_clock_gettime` writes `tp` only on its success path.
///
/// In every case the one failure that follows a copy attempt is the `-EFAULT`
/// from the copy itself, where `copy_to_user()` may have stored a prefix of
/// the struct before faulting. [`failure_proves_outputs_unwritten`] therefore
/// keeps rendering EFAULT failures, so that partial prefix stays visible.
///
/// `gettimeofday` is intentionally absent: `SYSCALL_DEFINE2(gettimeofday)` in
/// `kernel/time/time.c` stores `tv` and then returns `-EFAULT` if copying
/// `tz` faults, so its rendered output can be kernel-written on failure.
fn failure_leaves_outputs_unwritten(syscall: &Syscall) -> bool {
matches!(
syscall,
Syscall::Stat(_)
| Syscall::Lstat(_)
| Syscall::Fstat(_)
| Syscall::Newfstatat(_)
| Syscall::Statx(_)
| Syscall::ClockGettime(_)
)
}
/// Whether `result` is a failure that, for a syscall listed in
/// [`failure_leaves_outputs_unwritten`], proves Linux stored no output: a
/// guest errno other than EFAULT. EFAULT can follow a partial copy, and a
/// tool error is not the syscall's result.
fn failure_proves_outputs_unwritten(result: &Result<i64, Error>) -> bool {
matches!(result, Err(Error::Errno(errno)) if *errno != Errno::EFAULT)
}
/// The happens-before request a syscall entry makes once its result is
/// decided, for the posthook anchors it reached at its prehook (`anchors`), or
/// `None` (<https://github.com/rrnewton/hermit/issues/3929>):
/// - a value or a guest errno other than an internal restart code is a
/// completion: Linux returns it unchanged, so the anchors fire
/// ([`ResourceID::HappensBeforeSyscallCheckpoint`]);
/// - an internal restart code (`ERESTARTSYS`, `ERESTARTNOINTR`,
/// `ERESTARTNOHAND`, `ERESTART_RESTARTBLOCK`) is refused
/// ([`ResourceID::HappensBeforePosthookInterrupted`]): the kernel decides
/// only at signal delivery, after this point, whether the guest sees `EINTR`
/// or the entry is restarted and never returns, so neither firing nor not
/// firing would keep the anchor's promise;
/// - a tool or backend failure is not a result the guest receives; nothing
/// fires, and the failure ends the run on its own path.
fn posthook_checkpoint(
count: u64,
anchors: Vec<String>,
result: &Result<i64, Error>,
) -> Option<ResourceID> {
if anchors.is_empty() {
return None;
}
match result {
Err(Error::Errno(
errno @ (Errno::ERESTARTSYS
| Errno::ERESTARTNOINTR
| Errno::ERESTARTNOHAND
| Errno::ERESTART_RESTARTBLOCK),
)) => Some(ResourceID::HappensBeforePosthookInterrupted {
count,
anchors,
code: errno
.name()
.map_or_else(|| errno.to_string(), str::to_owned),
}),
Ok(_) | Err(Error::Errno(_)) => {
Some(ResourceID::HappensBeforeSyscallCheckpoint { count, anchors })
}
Err(_) => None,
}
}
#[reverie::tool]
impl<T: RecordOrReplay> Tool for Detcore<T> {
type GlobalState = GlobalState;
type ThreadState = ThreadState<T::ThreadState>;
/// Only a sequentialized run on a backend with process signal control
/// can install that control. Whether this run observes dequeues is
/// decided after installation by [`GlobalState`]'s
/// `GlobalTool::observe_signal_dequeues`.
fn may_observe_signal_dequeues(config: &Config) -> bool {
config.backend.provides_process_signal_control && config.sequentialize_threads
}
/// The same runs require the control: real-timer signals on such a run
/// travel only through acknowledged shared dequeues. Reverie refuses it
/// before any guest code, with ENOSYS on a runner that offers no control,
/// as Detcore's own installation did before this check moved into
/// Reverie, and with EINVAL on a runner whose offer was not taken. So
/// Detcore's scheduler needs no refusal of its own for a capable backend
/// without a control.
fn requires_signal_control(config: &Config) -> bool {
config.backend.provides_process_signal_control && config.sequentialize_threads
}
/// A sequentialized run lets one guest thread run guest code at a time,
/// and Detcore refuses every way to install a seccomp filter: `seccomp(2)`
/// (`handle_seccomp`, EOPNOTSUPP or an earlier argument error) and
/// `prctl(PR_SET_SECCOMP)` (not a supported option, ENOSYS). Both
/// refusals need the call intercepted, so the declaration also requires
/// both in the subscriptions. The ptrace backend then keeps the guest's
/// SIGTRAP action and mask across its own single-steps
/// (<https://github.com/rrnewton/hermit/issues/4409>).
///
/// One exception to "one at a time": a call the scheduler runs in the
/// background (`BlockingExternalIO`, `BlockingRtSigsuspend`) is injected
/// with a returning `inject`, so its thread runs only that syscall, in the
/// kernel, and stops before any guest instruction. It can install no
/// filter. Its injection is single-stepped, though, and that step's trap,
/// at a host-timed moment, can reset the process's shared SIGTRAP action
/// while another thread's injection is kept. An ignored, or caught and
/// blocked, SIGTRAP can then end at `SIG_DFL`, where an undeclared run
/// always leaves it; the blocked mask is per thread and is not affected.
fn enforces_serialized_execution(config: &Config) -> bool {
let subscriptions = Self::subscriptions(config);
config.sequentialize_threads
&& subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::seccomp)
&& subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::prctl)
}
async fn handle_signal_dequeue<G: Guest<Self>>(
&self,
guest: &mut G,
dequeue: reverie::SignalDequeue,
) -> Result<(), Errno> {
tool_global::signal_dequeued(guest, dequeue).await
}
/// Constructor for Detcore process-local state.
fn new(pid: Pid, cfg: &Config) -> Self {
let detpid = DetPid::from_raw(pid.into()); // TODO(T78538674): virtualize pid.
cfg.validate_invariants();
if let Some(policy) = &cfg.in_guest_detlog_forward_policy {
// This image's forwarder was set up before the Tool (SaBRe's
// connect, the LiteInst constructor); without one, or with another
// policy, refuse.
detlog::require_forwarding(policy);
}
if cfg.in_guest_site_patching_off {
// Record the host's request; the preload constructor compares it
// with the settings the runtime captured once installation
// returns (see in_guest_site_patching).
in_guest_site_patching::require_site_patching_off();
}
// Read before any guest syscall, so a readlink of another process's
// pipe or socket never depends on the tracer's descriptors then. The
// outcome is kept; a failure is reported only if a guest needs it.
let _ = syscalls::anonymous_object_devices();
Self {
detpid,
cfg: cfg.clone(),
record_or_replay: T::new(pid, cfg),
}
}
/// The signal-state copy follows handler returns from the backend's
/// reports where it has them (`syscalls::signal_shadow`).
fn observes_sigreturns(config: &Config) -> bool {
crate::syscalls::signal_shadow::observes_sigreturns(config)
}
fn handle_sigreturn(
&self,
thread_state: &mut Self::ThreadState,
report: reverie::SigreturnReport,
) {
crate::syscalls::signal_shadow::record_sigreturn_report(thread_state, report);
// Where the backend reports the call instead of passing it to
// `handle_syscall_event` (ptrace), this is the thread's last event
// before a signal its restored mask unblocks is delivered
// (`returned_from_sigreturn`).
thread_state.returned_from_sigreturn = true;
}
/// Counts each signal the backend passes on without reporting it, which
/// could reach a guest handler, so the delivery counters show what no
/// delivery check saw (A5 step 3.2b).
fn note_unreported_signal(
&self,
thread_state: &mut Self::ThreadState,
signal: &reverie::UnreportedSignal,
kept: bool,
) {
if kept && self.copy_misses_unreported(signal.number()) {
let dettid = thread_state.dettid;
thread_state
.delivery_counters
.count_unreported(dettid, signal.reason());
}
}
/// NOTE: these subscriptions are used ONLY for hermit run mode. Hermit record has its own
/// subscriptions specified in recorder/mod.rs.
fn subscriptions(config: &Config) -> Subscription {
let do_sched =
config.sched_heuristic != SchedHeuristic::None || config.sequentialize_threads;
if !config.passthru_opt {
// Fail closed by default in every build profile. Besides allowing syscall-specific
// handlers to run, interception is what charges generic syscall logical time.
//
// `cpuid` is the one exception. With CPUID virtualization off the handler only
// re-executes the host instruction, so trapping does not change what the guest
// sees, but the subscription makes Reverie probe and enable CPUID faulting and log
// an ERROR for each exec on hosts that lack it
// (https://github.com/rrnewton/hermit/issues/3460). No backend installs a CPUID
// table of its own, so an untrapped guest CPUID is the host's on every backend.
let mut subscription = Subscription::all_syscalls();
subscription.rdtsc();
if config.virtualize_cpuid {
subscription.cpuid();
}
subscription
} else {
// Explicit performance opt-in: unlisted syscalls bypass Detcore entirely. Keep this
// path separate so its allow-list can be tightened without weakening the default.
let mut subscription = Subscription::none();
subscription.syscalls([
Sysno::write,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#547)
Sysno::writev,
// Timer-slack procfs virtualization must see every scalar and
// vectored read/write form that Linux accepts for the file.
Sysno::readv,
Sysno::preadv,
Sysno::preadv2,
Sysno::pwritev,
Sysno::pwritev2,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#683)
Sysno::pwrite64,
Sysno::openat,
Sysno::open,
Sysno::creat,
// Detcore answers access and extended-attribute requests on the
// procfs files it renders
// (https://github.com/rrnewton/hermit/issues/4213); a backend
// keeps no copy of them, so these must not bypass Detcore.
Sysno::access,
Sysno::faccessat,
Sysno::faccessat2,
Sysno::getxattr,
Sysno::lgetxattr,
Sysno::setxattr,
Sysno::lsetxattr,
Sysno::removexattr,
Sysno::lremovexattr,
Sysno::listxattr,
Sysno::llistxattr,
Sysno::close,
Sysno::read,
Sysno::pread64,
Sysno::lseek,
Sysno::fadvise64,
Sysno::mmap,
Sysno::madvise,
Sysno::munmap,
Sysno::mremap,
Sysno::fcntl,
Sysno::arch_prctl,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-2150): Timer-slack prctl state is
// virtual and must not bypass Detcore under passthru_opt.
Sysno::prctl,
Sysno::ioctl,
Sysno::futex,
Sysno::clone,
Sysno::clone3,
Sysno::fork,
Sysno::vfork,
Sysno::wait4,
Sysno::waitid,
Sysno::setsid,
Sysno::uname,
Sysno::exit_group,
Sysno::exit,
// AUTONOMOUS-BOT-IMPLEMENTED
// The scheduler must observe changes to the address used for
// its modeled CHILD_CLEARTID wake, including record/replay's
// passthrough optimization.
Sysno::set_tid_address,
// AUTONOMOUS-BOT-IMPLEMENTED
// Rare (once per thread) but load-bearing: without it the exit
// hook cannot replay `exit_robust_list()` and robust-mutex
// waiters are never woken.
Sysno::set_robust_list,
Sysno::dup,
Sysno::dup2,
Sysno::dup3,
Sysno::pipe,
Sysno::pipe2,
Sysno::getrandom,
Sysno::utime,
Sysno::utimes,
Sysno::utimensat,
Sysno::futimesat,
Sysno::socket,
Sysno::socketpair,
Sysno::eventfd,
Sysno::eventfd2,
Sysno::sched_getaffinity,
Sysno::sched_setaffinity,
Sysno::signalfd,
Sysno::signalfd4,
Sysno::timerfd_create,
Sysno::timerfd_settime,
Sysno::timerfd_gettime,
Sysno::inotify_init,
Sysno::inotify_init1,
Sysno::inotify_add_watch,
Sysno::inotify_rm_watch,
Sysno::memfd_create,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-862): Keep modeled pidfd creation intercepted.
Sysno::pidfd_open,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1175): Keep pidfd signal/get-fd
// determinization from being bypassed under the passthru opt-in.
Sysno::pidfd_send_signal,
Sysno::pidfd_getfd,
Sysno::userfaultfd,
Sysno::io_uring_setup,
Sysno::io_uring_enter,
Sysno::io_uring_register,
Sysno::accept,
Sysno::accept4,
Sysno::nanosleep,
Sysno::clock_nanosleep,
Sysno::sched_yield,
Sysno::poll,
Sysno::ppoll,
Sysno::prlimit64,
Sysno::epoll_create,
Sysno::epoll_create1,
Sysno::epoll_ctl,
Sysno::epoll_pwait,
Sysno::epoll_wait,
Sysno::epoll_wait_old,
Sysno::epoll_ctl_old,
Sysno::recvfrom,
Sysno::rt_sigsuspend,
Sysno::rt_sigtimedwait,
Sysno::execve,
Sysno::execveat,
Sysno::rseq,
Sysno::getpid,
Sysno::gettid,
Sysno::getcpu,
Sysno::rt_sigprocmask,
Sysno::rt_sigaction,
Sysno::getrusage,
Sysno::sysinfo,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#686): Review scratch fd sets and scheduler polling.
Sysno::pselect6,
// `select` is included by the Determinized sweep below;
// T137258824 tracks improving its implementation.
]);
if do_sched {
subscription.syscalls([
// TODO: some of the above could probably move to this bucket.
Sysno::alarm,
Sysno::pause,
]);
}
if config.virtualize_metadata {
subscription.syscalls([
Sysno::getdents,
Sysno::getdents64,
Sysno::stat,
Sysno::lstat,
Sysno::fstat,
Sysno::newfstatat,
Sysno::statx,
]);
}
if true
// TODO: could introduce a flag for this:
/* config.virtualize_keys */
{
subscription.syscalls([Sysno::add_key, Sysno::request_key, Sysno::keyctl]);
}
if do_sched {
subscription.syscall(Sysno::connect);
}
if do_sched || config.warn_non_zero_binds {
subscription.syscall(Sysno::bind);
}
if config.warn_non_zero_binds {
subscription.syscall(Sysno::bind);
}
if config.virtualize_time {
subscription.rdtsc();
subscription.syscalls([
Sysno::gettimeofday,
Sysno::time,
Sysno::clock_gettime,
Sysno::clock_getres,
]);
}
if config.virtualize_cpuid {
subscription.cpuid();
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-978): Keep the passthru-opt allow-list in sync
// with the complete Determinized classification AND with the
// Unsupported set. Even under the performance opt-in, Detcore must
// see every syscall that its audited policy says it models or
// deterministically refuses. Otherwise record/replay can bypass a
// Detcore handler and execute the syscall natively against the host.
//
// `Determinized` and `Unsupported` are disjoint classifications, so
// the determinized filter alone leaves the Unsupported set
// unsubscribed: record/replay would silently execute an unsupported
// syscall on the live host instead of invalidating its determinism
// claim. Both halves are required; neither implies the other.
// NOTE: `all_pinned_syscalls()`, not `Sysno::iter()`. The latter
// stops one short of the end of the table, which silently dropped
// `lsm_list_modules` out of this sweep.
subscription.syscalls(crate::all_pinned_syscalls().filter(|sysno| {
crate::is_determinized_syscall(*sysno) || crate::is_unsupported_syscall(*sysno)
}));
// Make sure we also intercept everything that the record-or-replay tool
// wants.
subscription | T::subscriptions(config)
}
}
async fn handle_cpuid_event<G: Guest<Self>>(
&self,
guest: &mut G,
eax: u32,
ecx: u32,
) -> Result<CpuIdResult, Errno> {
trace!("handle_cpuid_event: eax: {}, ecx: {}", eax, ecx);
// A trapped instruction stops the guest in user code (A5 step 3.2).
guest.thread_state_mut().delivery_interruption = Some(reverie::Interruption::NotInSyscall);
if self.cfg.backend.virtualizes_guest_sigalrm && guest.thread_state().sigalrm_handled {
self.sync_sigalrm_eligibility(guest).await;
}
self.pre_handler_hook(guest, false, CheckIn::InstructionTrap)
.await;
let res = if self.cfg.virtualize_cpuid {
let dettid = guest.thread_state().dettid;
let time = &mut guest.thread_state_mut().thread_logical_time;
let intercepted = self.virtual_cpuid();
time.add_cpuid();
let nanos = time.as_nanos();
trace!(
"[dtid {}] inbound cpuid, new logical time: {:?}",
dettid, time
);
if self.cfg.should_trace_schedevent() {
trace_schedevent(
guest,
SchedEvent {
dettid,
op: Op::Cpuid,
count: 1,
start_rip: None,
end_rip: None,
end_time: Some(nanos),
},
true,
)
.await;
}
intercepted.cpuid(eax, ecx).unwrap_or_else(|| {
warn!(
"[dtid {}] cpuid leaf 0x{:x} subleaf 0x{:x} not in deterministic table; returning zero result",
dettid, eax, ecx
);
CpuIdResult {
eax: 0,
ebx: 0,
ecx: 0,
edx: 0,
}
})
} else {
cpuid!(eax, ecx)
};
self.post_handler_hook(guest, CheckIn::InstructionTrap)
.await;
Ok(res)
}
async fn handle_rdtsc_event<G: Guest<Self>>(
&self,
guest: &mut G,
request: Rdtsc,
) -> Result<RdtscResult, Errno> {
trace!("handle_rdtsc_event: {:?}", request);
// A trapped instruction stops the guest in user code (A5 step 3.2).
guest.thread_state_mut().delivery_interruption = Some(reverie::Interruption::NotInSyscall);
if self.cfg.backend.virtualizes_guest_sigalrm && guest.thread_state().sigalrm_handled {
self.sync_sigalrm_eligibility(guest).await;
}
self.pre_handler_hook(guest, false, CheckIn::InstructionTrap)
.await;
let result = if guest.config().virtualize_time {
let dettid = guest.thread_state().dettid;
guest.thread_state_mut().thread_logical_time.add_rdtsc();
info!(
"[dtid {}] inbound rdtsc, new logical time: {:?}",
dettid,
guest.thread_state().thread_logical_time
);
if self.cfg.should_trace_schedevent() {
let ev = with_guest_time(
guest,
SchedEvent {
dettid,
op: Op::Rdtsc,
count: 1,
start_rip: None,
end_rip: None,
end_time: None,
},
);
trace_schedevent(guest, ev, true).await;
}
// The guest TSC must name the same instant as `clock_gettime`. Both
// now read the coordinator's clock through the shared per-process
// floor, so a guest comparing the two -- a clocksource watchdog, a
// delay loop calibrated against a device timer, a second vCPU
// reading the TSC -- sees one time base instead of two.
//
// The `add_rdtsc()` charge above is folded into global time by the
// same RPC that reads it back (`send_and_update_time` updates the
// coordinator before dispatching the request), so consecutive reads
// from one thread still advance.
let tsc = guest_clock_time(guest).await;
Ok(RdtscResult {
// We treat virtual cycles as equivalent to virtual nanoseconds.
tsc: tsc.as_nanos(),
aux: None,
})
} else {
self.record_or_replay
.handle_rdtsc_event(&mut guest.into_guest(), request)
.await
};
self.post_handler_hook(guest, CheckIn::InstructionTrap)
.await;
result
}
// Note: we will not see SIGSTKFLT used for timers.
async fn handle_signal_event<G: Guest<Self>>(
&self,
guest: &mut G,
signal: Signal,
) -> Result<Option<Signal>, Errno> {
let delivered = self
.handle_inbound_signal(guest, SigWrapper::from(signal))
.await?;
if scheduler::discarded_at_send::records(&self.cfg)
&& let Some(control) = consumed_signal_control(
guest.pid(),
guest.tid(),
signal as i32,
delivered.is_some(),
)
{
tool_global::discarded_signal(guest, control).await;
}
if delivered.is_some() {
crate::syscalls::signal_shadow::check_and_record_delivery(guest, signal as i32).await;
}
Ok(delivered.map(|_| signal))
}
/// A realtime signal (32 to 64), which `Signal` cannot name: handled as
/// any other (`handle_inbound_signal`), so it takes its turn in the
/// schedule (<https://github.com/rrnewton/reverie/issues/207>).
async fn handle_raw_signal_event<G: Guest<Self>>(
&self,
guest: &mut G,
signal: i32,
) -> Result<Option<i32>, Errno> {
let delivered = self
.handle_inbound_signal(guest, SigWrapper(signal))
.await?;
if delivered.is_some() {
crate::syscalls::signal_shadow::check_and_record_delivery(guest, signal).await;
}
Ok(delivered.map(|signal| signal.raw()))
}
/// A `SIGCHLD` the backend passes on without reporting it is dropped when
/// it is the kernel's copy of a child-exit notification the scheduler
/// sends itself (`scheduler::child_exit_sigchld`). A copy the scheduler
/// sent is kept, with the siginfo the tracer gave it: this hook cannot
/// replace it.
///
/// Either way, the scheduler's record of copies Linux discarded when they
/// were sent learns what became of the copy (`consumed_signal_control`,
/// <https://github.com/rrnewton/hermit/issues/4293>).
async fn filter_unreported_signal<R: GlobalRPC<Self::GlobalState>>(
&self,
rpc: &R,
unreported: reverie::UnreportedSignal,
) -> bool {
if unreported.signal() == Some(Signal::SIGALRM)
&& scheduler::discarded_at_send::records(&self.cfg)
{
let tid = DetTid::from_raw(unreported.tid().as_raw());
rpc.send_rpc((
DetTime::default(),
MmId::initial(tid),
tool_global::GlobalRequest::DiscardedSignal(
scheduler::discarded_at_send::DiscardedSignalControl::UnreportedAlarm,
),
))
.await;
}
// A POSIX timer's stale entry is skipped, as Linux skips it inside the
// dequeue (<https://github.com/rrnewton/hermit/issues/4262>).
let stale_timer_entry = scheduler::discarded_at_send::records(&self.cfg)
&& (1..32).contains(&unreported.number())
&& {
let tid = DetTid::from_raw(unreported.tid().as_raw());
let control = scheduler::discarded_at_send::DiscardedSignalControl::Unreported {
signal: unreported.number(),
};
// Carries no logical time; answered before any clock accounting.
let (_, response) = rpc
.send_rpc((
DetTime::default(),
MmId::initial(tid),
tool_global::GlobalRequest::DiscardedSignal(control),
))
.await;
matches!(
response,
tool_global::GlobalResponse::DiscardedSignal(
scheduler::discarded_at_send::DiscardedSignalAnswer::Delivery(
scheduler::posix_timer_coalesce::DeliveryAnswer::Suppress
)
)
)
};
let kept = !stale_timer_entry && async {
// The test-only claim fault drops the claimed copy here too, where a
// relative `nanosleep`'s restart code makes the backend pass it on
// unreported (`syscalls::signal::take_claim_fault`).
if unreported.signal() == Some(Signal::SIGCHLD)
&& crate::syscalls::take_claim_fault(crate::syscalls::ClaimFault::DropClaimed)
{
warn!(
"[tid {}] claim fault hook: dropped the claimed SIGCHLD",
unreported.tid()
);
return false;
}
if unreported.signal() != Some(Signal::SIGCHLD)
|| !self.cfg.backend.reports_kernel_child_exit_signals
{
return true;
}
let Some(info) = unreported.siginfo() else {
return true;
};
let tid = DetTid::from_raw(unreported.tid().as_raw());
// Passed on unreported, a kept copy makes no `InboundSignal` request.
match scheduler::child_exit_sigchld::control_for(&info, std::process::id(), false) {
None => true,
Some(control) => {
use scheduler::child_exit_sigchld::ChildExitSigchldAnswer;
use scheduler::child_exit_sigchld::OwnCopy;
// The control message carries no logical time; the scheduler answers
// it before any clock accounting.
let (_, response) = rpc
.send_rpc((
DetTime::default(),
MmId::initial(tid),
tool_global::GlobalRequest::ChildExitSigchld(control),
))
.await;
match response {
tool_global::GlobalResponse::ChildExitSigchld(
ChildExitSigchldAnswer::Claimed(dropped),
) => !dropped,
tool_global::GlobalResponse::ChildExitSigchld(
ChildExitSigchldAnswer::OwnCopy(OwnCopy::Duplicate),
) => false,
tool_global::GlobalResponse::ChildExitSigchld(
ChildExitSigchldAnswer::OwnCopy(OwnCopy::Deliver(own))
| ChildExitSigchldAnswer::ClaimedFor(own),
) => {
warn!(
"[dtid {}] the SIGCHLD for child {} passed on unreported keeps its delivered siginfo",
tid, own.pid
);
true
}
_ => true,
}
}
}
}
.await;
if scheduler::discarded_at_send::records(&self.cfg)
&& let Some(control) = consumed_signal_control(
unreported.pid(),
unreported.tid(),
unreported.number(),
kept,
)
{
let tid = DetTid::from_raw(unreported.tid().as_raw());
// Carries no logical time; answered before any clock accounting.
rpc.send_rpc((
DetTime::default(),
MmId::initial(tid),
tool_global::GlobalRequest::DiscardedSignal(control),
))
.await;
}
// The signal-state copy lives in this process wherever it is kept
// (`syscalls::signal_shadow`); a signal that reaches a handler
// unreported is one it never saw.
let signal = unreported.number();
if kept && self.copy_misses_unreported(signal) {
crate::syscalls::signal_shadow::note_unreported(
unreported.pid().as_raw(),
unreported.tid().as_raw(),
signal,
);
}
kept
}
fn init_thread_state(
&self,
tid: Tid,
parent: Option<(Tid, &Self::ThreadState)>,
) -> Self::ThreadState {
trace!("[tid {}] detcore init new thread state", tid);
let record_or_replay = self
.record_or_replay
.init_thread_state(tid, parent.map(|(ptid, ts)| (ptid, ts.as_ref())));
// TODO(T78538674): virtualize tid, extend tid<=>dettid mapping here.
match parent {
None => ThreadState::new(DetPid::from_raw(tid.into()), &self.cfg, record_or_replay),
Some(pts) => {
let clone_flags = pts
.1
.clone_flags
.expect("clone_flags must be set by parent");
let dettid = DetPid::from_raw(tid.into());
// If we had mutable access to the parent state, we could update it here, but
// instead we leave that to the clone/fork handling.
let (_next_parent_pedigree, child_pedigree) = pts.1.pedigree.fork();
let child_logical_time = pts.1.thread_logical_time.clone_for_child();
let last_accounted_user_time = child_logical_time.user_cpu_time();
let last_accounted_system_time = child_logical_time.system_cpu_time();
if !clone_flags.contains(CloneFlags::CLONE_THREAD) {
pts.1.prepare_child_process_cpu_time(dettid);
}
let guest_clock = Arc::clone(&pts.1.guest_clock);
// A pending unreported signal may already have changed the
// parent's actions unseen: the child gets no copy then.
let child_signal_shadow = pts
.1
.signal_shadow
.as_ref()
.filter(|_| !crate::syscalls::signal_shadow::unreported_pending())
.and_then(|shadow| shadow.for_clone(clone_flags.bits()));
// A child sharing the actions without a copy could change them
// unrecorded: they become unknown to every sharer.
if child_signal_shadow.is_none()
&& clone_flags.contains(CloneFlags::CLONE_SIGHAND)
&& let Some(shadow) = pts.1.signal_shadow.as_ref()
{
shadow.forget_dispositions();
}
if child_signal_shadow.is_some() {
trace!(
"[dtid {}] signal-state copy: child {} gets a copy",
pts.1.dettid, dettid
);
} else if pts.1.signal_shadow.is_some() {
debug!(
"[dtid {}] signal-state copy: child {} gets none (created inside a \
handler, an unreported signal pending, or the actions unknown)",
pts.1.dettid, dettid
);
} else if pts.1.signal_shadow_lost {
debug!(
"[dtid {}] signal-state copy: child {} gets none (its creator's was lost)",
pts.1.dettid, dettid
);
}
debug!(
"[init_thread-state, parent dtid = {}] child thread {}, clone_flags = {:x?}",
pts.0, tid, clone_flags
);
let file_metadata = if clone_flags.contains(CloneFlags::CLONE_FILES) {
pts.1.file_metadata.clone()
} else {
Arc::new(Mutex::new(
pts.1.file_metadata.lock().unwrap().fork_for(dettid),
))
};
// A thread is of its creator's process. A new process leads a
// thread group of its own, whose table is the one it starts
// with: its creator's under CLONE_FILES, a copy otherwise.
let process_files = if clone_flags.contains(CloneFlags::CLONE_THREAD) {
pts.1.process_files
} else {
Some(file_metadata.lock().unwrap().files_id)
};
ThreadState {
dettid,
detpid: None, // Initialized later.
thread_start_entered: false,
physical_tid: None,
signal_task_identity: None,
open_file_creator: None,
mm_id: MmId::for_clone(
pts.1.mm_id,
dettid,
clone_flags.contains(CloneFlags::CLONE_VM),
),
memory_metadata: if clone_flags.contains(CloneFlags::CLONE_VM) {
Arc::clone(&pts.1.memory_metadata)
} else {
Arc::new(Mutex::new(
pts.1
.memory_metadata
.lock()
.expect("memory metadata mutex poisoned")
.clone(),
))
},
pedigree: child_pedigree.clone(),
initialized_random_auxv: None,
stats: ThreadStats::new(),
file_metadata,
process_files,
tool_shares_guest_descriptor_table: pts.1.tool_shares_guest_descriptor_table,
// Linux copies the creating thread's current timer slack
// into both fields of every new task (thread or process).
timer_slack_ns: pts.1.timer_slack_ns,
default_timer_slack_ns: pts.1.timer_slack_ns,
// A new process created with CLONE_NEWPID is the init of
// a namespace below its creator's; everything else lives
// in its creator's.
nested_pid_namespace: pts.1.nested_pid_namespace
|| (clone_flags.contains(CloneFlags::CLONE_NEWPID)
&& !clone_flags.contains(CloneFlags::CLONE_THREAD)),
// A new task starts in its creator's root, and with
// CLONE_FS keeps sharing it.
changed_root: if clone_flags.contains(CloneFlags::CLONE_FS) {
Arc::clone(&pts.1.changed_root)
} else {
Arc::new(std::sync::atomic::AtomicBool::new(
pts.1.changed_root.load(std::sync::atomic::Ordering::SeqCst),
))
},
// A new task runs its creator's image.
exe: pts.1.exe.clone(),
// POSIX timers are shared among threads of a process but are
// NOT inherited across fork(2). Share the table for a new
// thread (CLONE_THREAD); give a new process a fresh, empty
// one.
posix_timers: if clone_flags.contains(CloneFlags::CLONE_THREAD) {
Arc::clone(&pts.1.posix_timers)
} else {
Arc::new(Mutex::new(PosixTimers::default()))
},
// Resource limits are process state: threads share them,
// while a forked process inherits a snapshot.
resource_limits: if clone_flags.contains(CloneFlags::CLONE_THREAD) {
Arc::clone(&pts.1.resource_limits)
} else {
Arc::new(Mutex::new(
pts.1
.resource_limits
.lock()
.expect("resource limits mutex poisoned")
.clone(),
))
},
process_cpu_time: if clone_flags.contains(CloneFlags::CLONE_THREAD) {
Arc::clone(&pts.1.process_cpu_time)
} else {
Arc::new(Mutex::new(ProcessCpuTime::default()))
},
// Wall time belongs to the traced process tree, not to an
// individual process. Forked processes and cloned threads
// therefore retain one monotonic view of raw logical time.
guest_clock,
parent_process_cpu_time: if clone_flags.contains(CloneFlags::CLONE_THREAD) {
pts.1.parent_process_cpu_time.clone()
} else {
Some(Arc::clone(&pts.1.process_cpu_time))
},
parent_cpu_publication: if clone_flags.contains(CloneFlags::CLONE_THREAD) {
pts.1.parent_cpu_publication.clone()
} else if self.cfg.backend.emulates_child_waits
&& self.cfg.sequentialize_threads
{
Some(pts.1.prepare_child_cpu_publication(dettid))
} else {
None
},
// A new thread has made no syscall yet.
restart_block: None,
// A fork child inherits its parent's signal dispositions.
sigalrm_handled: pts.1.sigalrm_handled,
signal_shadow: child_signal_shadow.clone(),
// A parent whose shared actions were unknown gives its
// child no copy, and that child is never seeded either.
// Only a divergence marks a lineage as never to be seeded
// again; a child denied a copy for a quiet reason is
// seeded at its exec.
signal_shadow_lost: pts.1.signal_shadow_lost,
// A new thread's first stop is at the exit of the call
// that made it, which Detcore did not finish for it.
delivery_interruption: None,
delivery_frame_handler: 0,
delivery_counters: Default::default(),
// The threads of a process share the record of what the
// scheduler holds for it; a new process has nothing
// published.
// The scheduler holds no mask for a new thread.
published_mask: Some(None),
published_dispositions: if clone_flags.contains(CloneFlags::CLONE_THREAD) {
Arc::clone(&pts.1.published_dispositions)
} else {
Arc::default()
},
sigalrm_blocked_published: pts.1.sigalrm_blocked_published,
handler_restore_rt_reported: pts.1.handler_restore_rt_reported,
signal_register_projection: None,
last_accounted_user_time,
last_accounted_system_time,
thread_cpu_start_user_time: last_accounted_user_time,
thread_cpu_start_system_time: last_accounted_system_time,
clone_flags: None,
pending_vfork: pts.1.pending_vfork.clone(),
// Child RNG identity follows the deterministic creation
// pedigree, never the backend/host Tid. Guest-visible IDs
// and scheduler targeting continue to use `dettid`.
prng: tool_local::thread_rng_from_parent_pedigree(
"USER RAND",
&pts.1.prng,
&child_pedigree,
tool_local::ChildRngStream::User,
),
chaos_prng: tool_local::thread_rng_from_parent_pedigree(
"CHAOSRAND",
&pts.1.chaos_prng,
&child_pedigree,
tool_local::ChildRngStream::Chaos,
),
// For comparing progress to other threads, it is important that our
// child thread start at a sensible place, rather than starting back
// at zero:
thread_logical_time: child_logical_time,
// A new thread gets a new clock, so we've committed 0 ticks
committed_clock_value: 0,
// A new thread or process is never the backend runtime's
// bootstrapping thread, so it starts outside any window.
uncharged_bootstrap_syscalls: 0,
in_uncharged_bootstrap_syscall: false,
yield_after_futex_wake: false,
pipe_handoff_due: None,
own_signal_turn_hold: None,
returned_from_sigreturn: false,
consecutive_continuations: 0,
end_of_timeslice: None,
replay_rcb_end: None,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1151)
chaos_epoch: tool_local::chaos_epoch_sentinel(),
chaos_slowdown_factor: RcbTimeMultiplier::ONE,
chaos_slowdown_active: false,
claimed_sigchld: false,
pending_chaos_epochs: Vec::new(),
max_timeslice_end: None,
last_grant_deadline: None,
published_runtime_private_memory: None,
last_rcb_timer: None,
last_rcb_timer_is_max: false,
record_or_replay,
preemption_points: None,
// We only get to the point of creating child threads if we're past the first execve.
past_global_first_execve: true,
interrupt_at: self.cfg.interrupts_for_thread(dettid),
// `copy_process()` sets `p->robust_list = NULL` for every
// new task, thread or process alike. The child re-registers
// its own head before it can own a robust futex.
robust_list_head: None,
robust_list_process: if clone_flags.contains(CloneFlags::CLONE_THREAD) {
Arc::clone(&pts.1.robust_list_process)
} else {
Arc::new(Mutex::new(tool_local::RobustListProcessState::default()))
},
}
}
}
}
async fn handle_thread_start<G: Guest<Self>>(&self, guest: &mut G) -> Result<(), Error> {
guest.thread_state_mut().thread_start_entered = true;
self.decide_cpuinfo_placeholders(guest)?;
let new_dettid = DetTid::from_raw(guest.tid().into()); // TODO(T78538674): virtualize pid/tid:
assert_eq!(new_dettid, guest.thread_state().dettid);
let detpid = select_thread_start_detpid(guest.thread_state().detpid, guest.pid());
let needs_registration = thread_start_needs_registration(
guest.is_root_process(),
guest.thread_state().past_global_first_execve,
new_dettid,
detpid,
);
trace!(
"[tid {}] detcore handle_thread_start, pid={}",
guest.tid(),
detpid
);
// Delayed initialization of thread_state for this new thread:
let thread_state = guest.thread_state_mut();
thread_state.detpid = Some(detpid);
if thread_state.recover_process_mm_id(detpid) {
debug!(
"[detcore, dtid {}] recovered process memory identity {} for unparented thread state",
new_dettid, detpid
);
}
if let Some(vfork) = guest.thread_state_mut().pending_vfork.take() {
create_vfork_child_thread(guest, new_dettid, vfork).await;
} else if needs_registration {
// A root has no parent registration. A fresh runtime after exec
// also needs this handshake, even when the Guest has an in-tree
// parent: the global owner consumes its prepared exec and returns
// the replacement MmId before any ordinary image request. State
// inherited from a parent is already past the first exec and does
// not register a second birth here.
debug!(
"[detcore, dtid {}] thread start registration, scheduling.. full config:\n {:?}",
&new_dettid,
guest.config()
);
let physical_ids = if guest
.config()
.backend
.requires_thread_directed_process_signals
{
Some((
guest.pid().as_raw(),
guest
.thread_state()
.physical_tid
.expect("backend requires a host thread ID before registration"),
))
} else {
None
};
if let Some(post_exec_mm) =
create_child_thread(guest, new_dettid, 0, None, libc::SIGCHLD, physical_ids).await
{
guest.thread_state_mut().mm_id = post_exec_mm;
}
}
if new_dettid.as_raw() == detpid.as_raw() {
// A process's first thread, with its final address space: a forked
// child's copy of its parent's unsent warning count is not its own.
guest_log::starts_new_process(guest.thread_state().mm_id);
}
// Except for the root task, let's block until it's our turn to go:
let th = tool_global::thread_start_request(&self.cfg, guest, detpid).await;
// Finish the delayed initialization of the full threadstate:
{
let ts = guest.thread_state_mut();
ts.preemption_points = th.map(|x| x.into_iter());
ts.next_timeslice(&self.cfg); // Must be after preemption_points is set.
}
// The start grant's deadline hint arrived before the first slice
// existed; cap the slice now.
let deadline = guest.thread_state_mut().last_grant_deadline.take();
tool_global::cap_timeslice_at_deadline(guest, deadline);
// The prehook is a noop for a thread just starting. Can't end the timeslice. There's no
// RCB progress to record. However, we call it for consistency with all the other handlers.
self.pre_handler_hook(guest, true, CheckIn::Event).await;
// ^ precise_branch=true: There should have been ZERO prior instructions before this,
// because the thread hasn't done anything yet.
self.record_or_replay
.handle_thread_start(&mut guest.into_guest())
.await?;
self.post_handler_hook(guest, CheckIn::Event).await;
Ok(())
}
async fn handle_post_exec<G: Guest<Self>>(&self, guest: &mut G) -> Result<(), Errno> {
// The new image's first stop is at the exit of the exec Detcore did not
// finish (A5 step 3.2). The old image's delivery counts are logged
// under the identity they were taken under, and the new image counts
// afresh.
guest.thread_state_mut().delivery_interruption = None;
let dettid = guest.thread_state().dettid;
let counters = std::mem::take(&mut guest.thread_state_mut().delivery_counters);
counters.log(dettid);
// The new image's table may hold placeholders it rediscovered before
// this, the image's first guest callback.
self.decide_cpuinfo_placeholders(guest)?;
// Exec resets caught signals to their defaults, on a private copy of
// the actions; a thread without a copy (the first image, or one given
// up quietly) is seeded instead. Before any request: a nonleader
// exec's reconnect asks for a turn, and every request first publishes
// the process's dispositions, which must be the new image's.
crate::syscalls::signal_shadow::at_exec(guest);
// A nonleader exec preserves the survivor's state and PMU counter, but
// Linux changes its TID. Bind that state before any image callback RPC.
match tool_global::reconnect_exec(guest).await {
Err(Errno::EOPNOTSUPP) => {
let message = "unsupported: preemption recording and replay across nonleader exec";
error!("{message}");
// Report even when logging is disabled or redirected. The
// ordinary refusal helper preserves the policy exit class;
// reconnect already bound identity, so its diagnostic RPC is
// authenticated and cannot silently retire an unbound owner.
let _ = writeln!(crate::util::RetryingStderr, "{message}");
tool_global::unrecoverable_shutdown(
guest,
detcore_model::HERMIT_POLICY_REFUSAL_EXIT,
)
.await;
}
result => result?,
}
// Under x86-64-v3 the guest's CPUID must come from Detcore's table: a
// native CPUID would show the host's features and leaf 0xD. Reverie
// re-arms CPUID faulting for each new image before this callback and
// only warns when that fails, so a v3 run ends here, by name, instead
// of running on with the host's CPUID
// (https://github.com/rrnewton/hermit/issues/4243).
if self.cfg.cpu_profile == detcore_model::cpu_profile::CpuProfile::X86_64V3
&& !guest.has_cpuid_interception()
{
let message = "--cpu-profile x86-64-v3 refused: CPUID faulting is not active for \
this image, so the guest would see the host's CPUID";
error!("{message}");
let _ = writeln!(crate::util::RetryingStderr, "{message}");
tool_global::unrecoverable_shutdown(guest, detcore_model::HERMIT_POLICY_REFUSAL_EXIT)
.await;
}
// A v3 configuration without host facts ends here, by name, before the
// guest's first instruction, instead of reaching the CPUID table.
if let Some(why) = cpu_profile_admission_refusal(&self.cfg) {
let message = format!("--cpu-profile x86-64-v3 refused: {why}");
error!("{message}");
let _ = writeln!(crate::util::RetryingStderr, "{message}");
tool_global::unrecoverable_shutdown(guest, detcore_model::HERMIT_POLICY_REFUSAL_EXIT)
.await;
}
// Exec gives the task filesystem information of its own
// (`unshare_fs_struct`): a root it shared through CLONE_FS is now its
// own, as it stands.
let changed_root = guest
.thread_state()
.changed_root
.load(std::sync::atomic::Ordering::SeqCst);
guest.thread_state_mut().changed_root =
Arc::new(std::sync::atomic::AtomicBool::new(changed_root));
// The exec'ing thread is its process's only thread now, and leads
// it, so the table it holds is the process's. (Where Detcore handles
// the execve, `handle_execveat` already installed it; a backend that
// runs execve natively, as DBT does, reaches only this callback.)
guest.thread_state_mut().adopt_process_files();
// The guest's first image is entered by an execve. A backend that traces
// the guest from before it (ptrace) intercepts that execve and counts it
// as the guest's first syscall. A backend that starts the guest already
// executing its image (DBT under DynamoRIO, SaBRe) reaches this callback
// with nothing counted, so under DBT, which then sees every later
// syscall, numbers and virtual time ran exactly one syscall behind
// ptrace's. Count that execve here, at its own cost. (SaBRe also misses
// the dynamic loader's syscalls before its plugin starts; this closes
// only the execve's share of that gap.)
let thread = guest.thread_state_mut();
if !thread.past_global_first_execve && thread.stats.syscall_count == 0 {
thread.stats.count_syscall();
thread
.thread_logical_time
.add_syscall_with_cost(syscall_time::cost_ns(Sysno::execve));
}
guest.thread_state_mut().past_global_first_execve = true;
// exec resets a handled signal to its default action.
guest.thread_state_mut().sigalrm_handled = false;
guest.thread_state_mut().sigalrm_blocked_published = false;
// Only a successful exec reaches this callback. Delete the old image's
// POSIX timer IDs while exec still owns its scheduler turn; the global
// notification below cancels their deadlines before the pre-handler
// can yield. alarm()/ITIMER_REAL and the continuous clock survive exec.
guest
.thread_state()
.posix_timers
.lock()
.unwrap()
.clear_for_exec();
// A successful exec clears the kernel's clear_child_tid registration.
// Mirror that reset before any replacement-image syscall can run.
if guest.config().sequentialize_threads {
tool_global::set_child_tid_address(guest, 0).await;
}
// The exec walked the old thread group's robust lists; wake one waiter
// per owner word it marked FUTEX_OWNER_DIED.
let robust_wakes = guest.thread_state().take_robust_list_wakes_for_exec();
let identities: Vec<_> = robust_wakes
.iter()
.map(|(owner, wake)| (*owner, wake.futex))
.collect();
let counts = robust_list_wakes_after_exec(guest, robust_wakes).await;
for ((owner, futex), count) in identities.into_iter().zip(counts) {
info!(
"[detcore, dtid {}] robust-list owner death woke {} waiter(s) on futex {:?} at exec",
owner, count, futex,
);
}
// In a run that declares a launcher, an exec Detcore did not prepare
// leaves the new image without a record: resolve it here from the
// path the exec was given.
let unprepared_exe = if self.declares_launcher() {
self.exec_image_from_execfn(guest)
} else {
None
};
tool_global::mark_past_first_execve(guest, unprepared_exe).await;
// That request also took the process out of its old descriptor table
// in `procfs::locks::LockLedger`; name the descriptions whose locks
// Detcore follows that survived the exec (its close-on-exec slots are
// gone from this table). A failed exec reaches neither.
let surviving = guest.thread_state().lock_tracked_slots();
if !surviving.is_empty() {
tool_global::note_table_exec(guest, surviving).await;
}
// The new image's auxiliary vector, as it finds it on its initial
// stack, for its `/proc/<pid>/auxv` (package K slice K2 of
// https://github.com/rrnewton/hermit/issues/4213). Not under record
// and replay, where Detcore renders no procfs file.
// Boxed, so its injected calls do not grow the post-exec future,
// which a backend may poll on a small stack.
if !self.cfg.recordreplay_modes {
Box::pin(self.record_process_auxv(guest)).await;
}
// The new image's program-break model, checked against the kernel's
// once its exec record is in place.
self.check_brk_model(guest).await;
// Linux discards every queued POSIX timer signal at exec
// (`flush_itimer_signals`), and Hermit's are ordinary signals it sent:
// take them back before the new image runs
// (https://github.com/rrnewton/hermit/issues/4184).
if scheduler::timer_copies_recorded() || scheduler::real_time_expiration_kept_pending() {
self.flush_exec_timer_copies(guest).await;
}
self.pre_handler_hook(guest, false, CheckIn::Event).await;
let auxv = guest.auxv();
let initialized = guest
.thread_state_mut()
.complete_initial_random_auxv(auxv.at_random().map(|p| p.as_raw()))
.expect("authenticated initial random handoff no longer matches this image");
// A backend that wrote AT_RANDOM before this callback (the loader
// handoff) emitted no record: emit it here from the bytes it wrote, with
// no second draw or write, so the record sits where every backend puts
// it, after the root thread's seeding records. The loader's getrandom
// fills (glibc's early initialization makes one) and resource-limit
// reads (glibc's startup reads RLIMIT_STACK) emitted none either: their
// records follow, in order, as when this thread's own handlers serve
// them after this callback.
if let Some((bytes, early_requests)) = initialized {
let dettid = guest.thread_state().dettid;
random::record_initial_auxv(dettid, &bytes);
for request in early_requests {
match request {
random::EarlyRequest::Getrandom(fill) => {
random::record_early_getrandom(dettid, fill)
}
random::EarlyRequest::LimitRead {
call,
resource,
current,
maximum,
} => {
syscalls::sysinfo::record_early_limit_read(call, resource, current, maximum)
}
}
}
} else if let Some(ptr) = auxv.at_random() {
// It is safe to mutate this address since libc has not yet had a
// chance to modify or copy the auxv table.
let memory = guest.memory();
let dettid = guest.thread_state().dettid;
let ptr = unsafe { ptr.into_mut() };
random::initialize_auxv(
guest.thread_state_mut().thread_prng(),
memory,
ptr.cast(),
dettid,
)?;
}
// Successful exec never returns through handle_syscall_event, so the
// nested recorder/replayer needs this callback to commit or retire its
// pending exec state before the replacement image issues another exec.
self.record_or_replay
.handle_post_exec(&mut guest.into_guest())
.await?;
self.post_handler_hook(guest, CheckIn::Event).await;
Ok(())
}
/// A timer fires to preempt the guest and give other threads a turn.
async fn handle_timer_event<G: Guest<Self>>(&self, guest: &mut G) {
// A preemption stops the guest in user code (A5 step 3.2).
guest.thread_state_mut().delivery_interruption = Some(reverie::Interruption::NotInSyscall);
info!(
"[detcore, dtid {}] inbound timer preemption event",
guest.thread_state().dettid
);
if guest.config().preemption_stacktrace {
let mut file_writer: Box<dyn Write> =
match &guest.config().preemption_stacktrace_log_file {
Some(path) => Box::new(
File::create(path).expect("Failed to open preemption stacktrace log file"),
),
// Under --max-log-bytes a full stderr must not stop the
// run before the cap; see `util::DroppingStderr`.
None if crate::nonwaiting_write::diagnostics_must_not_wait() => {
Box::new(crate::util::DroppingStderr)
}
None => Box::new(std::io::stderr()),
};
let ts = guest.thread_state();
writeln!(
file_writer,
"\n>>> Guest tid {} preempted at thread time {} with stack trace:",
ts.dettid,
ts.thread_logical_time.as_nanos(),
)
.unwrap();
if let Some(backtrace) = guest.backtrace() {
if let Ok(pbt) = backtrace.pretty() {
writeln!(file_writer, "{}", pbt).unwrap();
} else {
writeln!(file_writer, "{}", backtrace).unwrap();
}
} else {
warn!("Could not read backtrace!");
}
}
// This may LOOK like a noop, but actually all of the logic for ending the timeslice is in
// the prehook. All the timer has to do is interrupt the guest and generate an extra call
// to this prehook.
self.pre_handler_hook(guest, true, CheckIn::Event).await;
if guest.config().no_rcb_time && guest.thread_state().last_rcb_timer_is_max {
let max_timeslice_end = guest
.thread_state()
.max_timeslice_end
.expect("PMU maximum timer requires a deadline");
guest
.thread_state_mut()
.thread_logical_time
.advance_to(max_timeslice_end);
if self.cfg.should_trace_schedevent() {
let dettid = guest.thread_state().dettid;
let ev = with_guest_time(
guest,
SchedEvent {
dettid,
op: Op::OtherInstructions,
count: 1,
start_rip: None,
end_rip: None,
end_time: None,
},
);
let ev = with_guest_rip(guest, ev).await;
trace_schedevent(guest, ev, false).await;
}
if self.cfg.replay_schedule_from.is_some() {
let fallback_deadline = max_timeslice_end
+ Duration::from_nanos(u64::from(
self.cfg
.max_timeslice
.expect("PMU maximum must be configured"),
));
let thread_state = guest.thread_state_mut();
let replay_deadline = thread_state
.end_of_timeslice
.filter(|deadline| *deadline > max_timeslice_end)
.unwrap_or(fallback_deadline);
thread_state.end_of_timeslice = Some(replay_deadline);
thread_state.max_timeslice_end = Some(replay_deadline);
thread_state.last_rcb_timer = None;
thread_state.last_rcb_timer_is_max = false;
thread_state.stats.reset_timeslice();
} else if guest.thread_state().own_signal_turn_hold.is_none() {
self.end_timeslice(guest).await;
}
}
self.post_handler_hook(guest, CheckIn::Event).await;
}
async fn handle_syscall_event<G: Guest<Self>>(
&self,
guest: &mut G,
call: Syscall,
) -> Result<i64, Error> {
#[cfg(feature = "dispatch-observer")]
if let Some(observer) = dispatch_observer::installed() {
// This is the genuine incoming event. An entry record does not
// establish that subsequent signal gates or handling completed.
let registers = guest.regs().await;
let (number, arguments) = call.into_parts();
observer(dispatch_observer::Entry {
pid: guest.pid().as_raw(),
tid: guest.tid().as_raw(),
dettid: guest.thread_state().dettid.as_raw(),
number: number as u64,
instruction_pointer: registers.rip,
arguments: [
arguments.arg0 as u64,
arguments.arg1 as u64,
arguments.arg2 as u64,
arguments.arg3 as u64,
arguments.arg4 as u64,
arguments.arg5 as u64,
],
});
}
// A FIFO open's path page that a killed task of this address space
// left mapped goes before this call runs (and, below, before a call
// that was waiting returns).
Self::unmap_orphaned_staging_pages(guest).await;
crate::syscalls::signal_shadow::at_syscall_entry(guest);
// Until Detcore finishes it, the kernel may run it (A5 step 3.2).
guest.thread_state_mut().delivery_interruption = Some(reverie::Interruption::KernelRun {
syscall: call.number() as i64,
});
// Signal controls an in-guest runtime refused since this process's
// last report, which Detcore did not see as calls.
crate::refused_signal_controls::report(guest).await;
// Signal phase 1: before this syscall's first scheduler request. A
// delivery prepared here runs the guest's handler before the syscall,
// which then starts again; what this returns is discarded.
if self.cfg.backend.virtualizes_guest_sigalrm
&& guest.thread_state().sigalrm_handled
&& self.sigalrm_at_syscall_entry(guest).await
{
return Ok(0);
}
// A thread that keeps its turn for a signal it sent its own process
// takes that signal before it can make another syscall.
if let Some(signal) = guest.thread_state_mut().own_signal_turn_hold.take() {
tracing::error!(
"[detcore, dtid {}] reached a syscall without taking signal {} it sent to its own \
process (https://github.com/rrnewton/hermit/issues/4034)",
guest.thread_state().dettid,
signal
);
}
// A claimed `SIGCHLD` stays claimed only while it is still pending for
// this thread (`syscalls::signal::claim_still_pending`).
let (pid, tid) = (guest.pid(), guest.tid());
let claimed =
crate::syscalls::claim_still_pending(guest.thread_state().claimed_sigchld, || {
crate::syscalls::read_thread_signal_state(pid, tid)
});
guest.thread_state_mut().claimed_sigchld = claimed;
self.pre_handler_hook(guest, false, CheckIn::Event).await;
// Linux's restart of an interrupted wait is the thread's very next syscall,
// so the record a wait kept for it lives only until then
// (`handle_restart_syscall`).
let restart_block = guest.thread_state_mut().restart_block.take();
let dettid = guest.thread_state().dettid;
if guest.thread_state().guest_past_first_execve() {
detlog!(
event = crate::detlog::DetLogEvent::Syscall;
"[syscall][detcore, dtid {}] inbound syscall: {} = ?",
dettid,
call.display(&guest.memory())
);
}
// The hot syscall path only reads a few Copy flags from the config, so copy
// just those out instead of cloning the entire Config on every intercepted
// syscall (previously flagged inline as an unnecessary copy). guest.config()
// borrows guest immutably; bind the flags in a tight scope so the borrow ends
// before the later thread_state_mut()/&mut guest borrows below.
let (sequentialize_threads, virtualize_time, panic_on_unsupported_syscalls) = {
let config = guest.config();
(
config.sequentialize_threads,
config.virtualize_time,
config.panic_on_unsupported_syscalls,
)
};
if sequentialize_threads && self.cfg.should_trace_schedevent() {
trace_schedevent(
guest,
with_guest_time(
guest,
SchedEvent::syscall(dettid, call.number(), SyscallPhase::Prehook),
),
true,
)
.await;
}
let syscall_cost_ns = syscall_time::cost_ns(call.number());
// The backend-runtime bootstrap window. A backend-resident runtime (the
// LiteInst preload constructor) issues hundreds to thousands of syscalls
// on one thread (315 for a small C program, 6,970 for emacs; see
// `syscall_time::MAX_UNCHARGED_BOOTSTRAP_SYSCALLS`) between its validated
// begin trap and the trap that ends its bootstrap: the ready report, or
// the report that preparation failed.
// Reverie reports that window only for the bootstrapping thread; every
// other thread and every forked process sees no window.
//
// What is withheld: the per-syscall cost below, for the bootstrapping
// thread only, for each syscall it makes inside the window that does not
// observe virtual time, up to
// `syscall_time::MAX_UNCHARGED_BOOTSTRAP_SYSCALLS` per window. That
// includes syscalls made by guest code the runtime calls into, such as
// an interposed malloc or open64. Charging the runtime's own syscalls
// would make guest-visible virtual time (uptime, CLOCK_MONOTONIC, CPU
// time) depend on which backend ran the program. Every withheld syscall
// is still counted and fully handled below, so Detcore keeps tracking
// the fds, mappings and inodes it creates.
//
// What is always charged, inside the window too: syscalls that observe
// virtual time (`syscall_time::observes_virtual_time`), so two clock
// reads on the bootstrapping thread differ by at least one syscall cost
// exactly as outside the window; and every syscall past the cap, so a
// loop inside the window, or a runtime that never reports ready, again
// advances the clock at each syscall.
//
// When it matters: with a syscall-driven clock (no PMU, or
// --max-timeslice=disabled, which Hermit selects when perf counters are
// unavailable, or --no-rcb-time) these charges are the thread's only
// per-syscall progress, and with sequentialized threads the 500x no-RCB
// multiplier makes each one large. With RCB time on a PMU host, retired
// branches still advance the thread's clock inside the window and only
// the syscall costs are withheld.
//
// The decision depends only on the window the backend reports (opened
// and closed by trap instructions the runtime executes), the syscall
// number and the count of earlier uncharged syscalls in the same
// window: all functions of the guest's own execution. Record/replay and
// both --verify runs therefore make the same decisions. The clock is
// never reset: the first syscall after the window continues from the
// value the window left. See https://github.com/rrnewton/hermit/issues/3338
// and https://github.com/rrnewton/hermit/pull/3430#issuecomment-5928691696.
//
// The scheduler turns such a syscall needs are withheld the same way.
// Handling an uncharged syscall can still commit scheduler turns (for
// example the file resources of a read of /proc/self/maps), and each
// committed turn normally advances global time by the per-turn
// scheduler cost. While the syscall is being handled the thread is
// marked `in_uncharged_bootstrap_syscall`, `tool_global::resource_request`
// copies that mark into the request, and the scheduler does not
// advance global time for a marked turn unless it is an IO-polling
// retry, whose time enforces timeouts. Without this, a LiteInst run of
// the clock-trajectory fixture reached its first clock read 11.5 ms of
// virtual time later than with it, and each exec added 12.0 ms more
// (measured locally), which was enough for its sysinfo uptime to
// differ from the ptrace backend's on the hosted runner
// (https://github.com/rrnewton/hermit/issues/3517). A charged syscall
// (one that observes virtual time, or any past the cap) is not marked,
// so its turns advance the clock as everywhere else.
let in_backend_runtime_bootstrap = guest.is_backend_runtime_bootstrap();
let new_count = {
// which results from not being able to borrow guest twice.
let thread_state = guest.thread_state_mut();
thread_state.stats.count_syscall();
// Every intercepted syscall advances logical time, including configurations that do
// not serialize threads. This keeps virtual clocks productive during syscall loops.
// The one exception is the bootstrap window described above: on the bootstrapping
// thread, up to MAX_UNCHARGED_BOOTSTRAP_SYSCALLS syscalls per window that do not
// observe virtual time are left uncharged.
let charged =
thread_state.charge_syscall_time(in_backend_runtime_bootstrap, call.number());
if charged {
thread_state
.thread_logical_time
.add_syscall_with_cost(syscall_cost_ns);
}
thread_state.in_uncharged_bootstrap_syscall = !charged;
// This only folds the thread's new user and system time into the process total.
// An uncharged syscall added none, so it needs no guard.
thread_state.account_process_cpu_time();
thread_state.stats.syscall_count
};
// Happens-before enforcement checkpoint. When the run carries a
// happens-before program and an anchor at exactly
// `Position::SyscallCount(new_count)` names this thread (by dettid, or by
// a spawn ordinal only the scheduler can resolve), this syscall checks in
// with the scheduler at its prehook, carrying this thread's running
// syscall count.
// The scheduler fires any anchor at that count on this thread and parks
// the thread (out of the run queue) when that anchor is the AFTER
// endpoint of a Hard edge whose BEFORE endpoint has not fired yet. For
// any other (thread, count) the scheduler would find no anchor and do
// nothing, so the request is skipped: one scheduler round trip per anchor
// the thread reaches rather than per syscall
// (https://github.com/rrnewton/hermit/issues/3877), and threads the spec
// does not name keep their schedule.
// This is the gate that makes an authored partial order reproduce a known
// race deterministically (see detcore-model `happens_before`). It
// requires sequentialized threads (enforced by the CLI) so the scheduler
// owns ordering.
//
// A syscall-occurrence anchor ("the thread's Nth writev to fd 9") is
// counted here, in this thread's own counters, once per syscall entry;
// the thread checks in only when a call is some such anchor's nth
// occurrence, naming the anchors it reached.
let hb_checkpoint = if guest.config().happens_before.is_some() {
let dettid = guest.thread_state().dettid;
let mut counters = std::mem::take(&mut guest.thread_state_mut().stats.hb_occurrences);
let program = guest
.config()
.happens_before
.as_ref()
.expect("checked above");
// A posthook anchor this entry reaches checks in when the call
// completes, below, not here
// (https://github.com/rrnewton/hermit/issues/3929).
let mut reached = if program.has_syscall_occurrence_anchors() {
let (sysno, args) = call.into_parts();
program.count_syscall_occurrences(
&mut counters,
dettid,
sysno,
[args.arg0, args.arg1],
new_count,
)
} else {
Vec::new()
};
let posthook: Vec<String> = reached
.extract_if(.., |name| {
program
.anchors
.get(name)
.is_some_and(|a| a.fires_at_posthook())
})
.collect();
let checkpoint = if !reached.is_empty() {
Some(ResourceID::HappensBeforeSyscallCheckpoint {
count: new_count,
anchors: reached,
})
} else if program.may_have_syscall_count_anchor_at(dettid, new_count) {
Some(ResourceID::HappensBeforeCheckpoint(new_count))
} else {
None
};
guest.thread_state_mut().stats.hb_occurrences = counters;
guest.thread_state_mut().stats.hb_pending_posthook = posthook;
checkpoint
} else {
None
};
if let Some(checkpoint) = hb_checkpoint {
let request = guest.thread_state().mk_request(checkpoint, Permission::R);
resource_request(guest, request).await;
}
// For `hermit run --verify`: a name the guest itself rebinds is not a
// host input change (see `detcore_model::host_input`). Reported before
// the call runs, whatever its outcome, so that a change the kernel
// commits is never missing from the record, even if the run ends
// before this handler returns.
if self.cfg.record_host_inputs {
self.record_host_namespace_change(guest, &call).await;
self.record_untraced_code_change(guest, &call).await;
}
// The inodes whose last name this call removes, stat'ed before it runs
// and retired once it succeeds, so that a host reusing one cannot hand
// a new file the old file's number
// (https://github.com/rrnewton/hermit/issues/3840).
let removed_inodes = self.last_names_removed_by(guest, &call).await;
// The guest-made FIFOs whose name this call removes, forgotten once it
// succeeds (https://github.com/rrnewton/hermit/issues/2203).
let removed_guest_fifos = self.guest_made_fifos_removed_by(guest, &call).await;
// Only an emulated RNG readv supplies authoritative imported geometry.
// A generic pre-dispatch snapshot would become stale across pipe waits.
let mut rng_readv_output = None;
// Signal phase 1: a process that handles SIGALRM runs only the calls
// its table places (`sigalrm_phase1`).
let sigalrm_refusal = if guest.thread_state().sigalrm_handled {
self.sigalrm_phase1_gate(guest, call)
} else {
None
};
// A `/proc` name outside the run's `--proc` level does not exist. A
// procfs file Detcore renders answers, as Linux does, the calls that
// would reach the placeholder standing in for it.
// A rendered procfs descriptor that reached this table over
// SCM_RIGHTS is adopted as the file it is before anything answers a
// call through it.
let adoption = if sigalrm_refusal.is_none() {
self.adopt_received_placeholders(guest, &call).await
} else {
Ok(())
};
let rendered_procfs_answer = if let Err(error) = adoption {
Some(Err(error))
} else if sigalrm_refusal.is_none() {
match self
.refuse_procfs_name_outside_level(guest, &call)
.await
.transpose()
{
Some(refusal) => Some(refusal),
None => match self.answer_rendered_procfs_operation(guest, &call).await {
Some(answer) => Some(answer),
None => match self.answer_rendered_procfs_mapping(guest, &call).await {
Some(answer) => Some(answer),
None => self
.answer_rendered_procfs_path_call(guest, &call)
.await
.transpose(),
},
},
}
} else {
None
};
let res = match classify_syscall(call.number()) {
_ if sigalrm_refusal.is_some() => Err(sigalrm_refusal.expect("checked").into()),
_ if rendered_procfs_answer.is_some() => rendered_procfs_answer.expect("checked"),
SyscallClassification::PassThrough if call.number() == Sysno::chroot => {
let result = self.passthrough(guest, call).await;
if result.is_ok() {
guest
.thread_state()
.changed_root
.store(true, std::sync::atomic::Ordering::SeqCst);
// Another process's view of this one's mount table
// follows its root (`Detcore::rendered_procfs_at`).
crate::tool_global::mark_changed_root(guest).await;
}
result
}
// Outbound TCP channels under a network trace mode; never with no mode.
_ if self.network_trace_owns(guest, &call) => {
self.handle_network_trace_syscall(guest, call).await
}
// Rseq is not type-safe in the pinned Reverie revision. Dispatch by Sysno so a
// future typed representation preserves this explicit policy.
SyscallClassification::Determinized if call.number() == Sysno::rseq => {
if panic_on_unsupported_syscalls {
Err(Error::Errno(Errno::ENOSYS))
} else {
self.passthrough(guest, call).await
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
// The pinned Reverie revision exposes process_madvise only as a raw call.
SyscallClassification::Determinized if call.number() == Sysno::process_madvise => {
match call {
Syscall::Other(_, args) => Self::handle_process_madvise(args.arg0, args.arg4),
_ => unreachable!("process_madvise unexpectedly gained a typed variant"),
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1175): The pinned Reverie revision exposes
// pidfd_send_signal/pidfd_getfd only as raw calls, so dispatch on the
// Sysno. See the handlers in syscalls/files.rs for the determinism
// argument.
SyscallClassification::Determinized if call.number() == Sysno::pidfd_send_signal => {
match call {
Syscall::Other(_, args) => {
let result = self
.handle_pidfd_send_signal(
guest,
call,
args.arg0 as RawFd,
args.arg1 as i32,
args.arg3 as u32,
)
.await;
if args.arg1 as i32 == libc::SIGALRM && result.is_ok() {
self.note_guest_sigalrm(guest, GuestSigalrmTarget::Unclassified)
.await;
}
result
}
_ => unreachable!("pidfd_send_signal unexpectedly gained a typed variant"),
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-1175): pidfd_getfd, likewise untyped.
SyscallClassification::Determinized if call.number() == Sysno::pidfd_getfd => {
match call {
Syscall::Other(_, args) => {
self.handle_pidfd_getfd(
guest,
call,
args.arg0 as RawFd,
args.arg1 as RawFd,
args.arg2 as u32,
)
.await
}
_ => unreachable!("pidfd_getfd unexpectedly gained a typed variant"),
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#715): Deterministic ENOSYS for syscalls the pinned
// x86_64 kernel leaves unimplemented (sys_ni_syscall). A fixed -ENOSYS is
// deterministic by construction and identical to the modern kernel's own
// return, so no guest-visible behavior changes versus the legacy
// pass-through. These are untyped (Syscall::Other) in the pinned Reverie,
// so dispatch on the Sysno before the typed match below.
SyscallClassification::Determinized
if is_unimplemented_enosys_syscall(call.number()) =>
{
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-852): Review the futex2 fallback contract.
// Detcore models legacy futex but not the newer vector/sized futex2
// ABI. Match a kernel without futex2 so runtimes take their
// established legacy-futex fallback without consulting the host.
SyscallClassification::Determinized if is_futex2_enosys_syscall(call.number()) => {
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-836): Host filesystem and mount
// introspection are outside the deterministic model. Return the
// portable feature-absence errno so callers use /proc fallbacks.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-859): Extend this boundary to obsolete ustat
// host-filesystem capacity counters.
SyscallClassification::Determinized
if is_mount_introspection_enosys_syscall(call.number()) =>
{
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-848): Hide unmodeled shared keyrings and
// request-key upcalls behind the portable CONFIG_KEYS-absent errno.
// TODO-HUMAN-REVIEW(PR-916): Fail closed whenever the
// panic-on-unsupported policy is active; ordinary runs select that
// policy by default. The explicit compatibility opt-out keeps the
// pre-848 host pass-through so the guest observes a real working
// keyring, restoring the enabled rr `keyctl` compatibility test.
// Under fail-closed execution the deterministic ENOSYS boundary is
// preserved.
SyscallClassification::Determinized if is_kernel_keyring_syscall(call.number()) => {
if panic_on_unsupported_syscalls {
Err(Error::Errno(Errno::ENOSYS))
} else {
self.passthrough(guest, call).await
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-855): Fail-closed runs cannot expose
// unmodeled pipe-buffer ownership or vmsplice page pinning, but
// preserve host pass-through under the explicit compatibility
// opt-out, `hermit run --allow-unsupported-syscalls`. splice and tee
// refuse with EINVAL, the errno Linux returns when a descriptor
// pair cannot be spliced and the one callers fall back to
// read/write on: GNU grep 3.12 drains a pipe into /dev/null with
// splice and falls back on EINVAL, but on ENOSYS it reports a read
// error and exits 2. vmsplice keeps ENOSYS.
SyscallClassification::Determinized if is_zero_copy_pipe_syscall(call.number()) => {
if panic_on_unsupported_syscalls {
if call.number() == Sysno::vmsplice {
Err(Error::Errno(Errno::ENOSYS))
} else {
Err(Error::Errno(Errno::EINVAL))
}
} else {
self.passthrough(guest, call).await
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-860): Host LSM attributes are outside
// Detcore's model. Present a stable feature-absence boundary
// instead of forwarding probes.
SyscallClassification::Determinized
if is_host_security_identity_probe_syscall(call.number()) =>
{
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#722): Deterministic EPERM for privileged
// system-administration syscalls (module load/unload, kexec, reboot,
// swap, raw I/O ports, root-mount pivot, host/domain name, tty
// hangup, disk quotas). The deterministic guest does not hold the
// capabilities these require against the host kernel, so a fixed
// -EPERM matches the unprivileged errno, never perturbs global host
// state, and is identical across --verify and record/replay. These
// are untyped (Syscall::Other) in the pinned Reverie, so dispatch on
// the Sysno before the typed match below.
SyscallClassification::Determinized
if is_privileged_admin_refused_syscall(call.number()) =>
{
Err(Error::Errno(Errno::EPERM))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-844): Enforce a deterministic boundary
// around host-global process accounting and cross-process memory.
SyscallClassification::Determinized
if is_process_isolation_refused_syscall(call.number()) =>
{
Err(Error::Errno(Errno::EPERM))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-876): Guest performance events
// expose host PMU availability, policy, and asynchronous counter
// state that Detcore does not model. A fixed ENOSYS preserves the
// portable feature-probe fallback without creating an untracked fd.
SyscallClassification::Determinized if is_perf_event_enosys_syscall(call.number()) => {
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-853): Refuse nested tracing, host-object
// comparison at the deterministic boundary.
SyscallClassification::Determinized
if is_privileged_observation_refused_syscall(call.number()) =>
{
Err(Error::Errno(Errno::EPERM))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#720): set_mempolicy_home_node is untyped in the
// pinned Reverie revision. Hermit exposes a single virtual NUMA node,
// so setting a memory range's home node has no observable effect: a
// deterministic no-op.
SyscallClassification::Determinized
if call.number() == Sysno::set_mempolicy_home_node =>
{
Ok(0)
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#724): Deterministic EPERM for privileged mount
// and namespace administration syscalls (mount/umount2/mount_setattr/
// move_mount/open_tree/fsopen/fsmount/fsconfig/fspick, unshare, setns,
// open_by_handle_at, fanotify_init/fanotify_mark, settimeofday). A
// deterministic container pins the guest's namespaces, mount
// hierarchy, and virtual clock for the whole run, so these are
// refused with a fixed -EPERM: the unprivileged errno for the
// capability-gated operations and a deliberate deterministic refusal
// otherwise. Never forwarded to the host; identical across --verify
// and record/replay. Untyped (Syscall::Other) in the pinned Reverie,
// so dispatch on the Sysno before the typed match below.
SyscallClassification::Determinized
if is_mount_ns_admin_refused_syscall(call.number()) =>
{
Err(Error::Errno(Errno::EPERM))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#731): Deterministic ENOSYS for the
// asynchronous and message-passing I/O and IPC interfaces Detcore
// does not model: Linux native AIO (io_setup/io_destroy/io_submit/
// io_cancel/io_getevents/io_pgetevents), POSIX message queues
// (mq_*), and System V message queues (msg*). AIO completion is
// kernel-driven and lives outside logical time; the message-queue
// families operate on global, key/name-addressed kernel objects
// shared with the whole host. A fixed -ENOSYS is the errno a kernel
// built without AIO/CONFIG_POSIX_MQUEUE/CONFIG_SYSVIPC returns, is
// never forwarded to the host, and is identical across --verify and
// record/replay (mirrors the io_uring refusal). Untyped
// (Syscall::Other) in the pinned Reverie, so dispatch on the Sysno
// before the typed match below.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-859): Include System V semaphore and shared-
// memory objects in the existing CONFIG_SYSVIPC refusal boundary.
SyscallClassification::Determinized
if is_unsupported_async_ipc_syscall(call.number()) =>
{
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-882): Legacy nonlinear page
// remapping has host-dependent kernel support and VMA behavior that
// Detcore does not model. Preserve the documented mmap fallback.
SyscallClassification::Determinized
if is_remap_file_pages_enosys_syscall(call.number()) =>
{
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#787): BATCH 38. openat2 is untyped (Syscall::Other)
// in the pinned Reverie revision. It is a superset of openat whose
// callers must fall back to openat when it returns ENOSYS (kernels
// before 5.6 lack openat2), so a fixed -ENOSYS routes them onto the
// already-determinized openat path with no host dependency and behavior
// identical across --verify and record/replay.
SyscallClassification::Determinized if call.number() == Sysno::openat2 => {
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#787): BATCH 38. The credential-setting family
// (setuid/setgid and their re-/res-/fs- variants, and setgroups) is
// untyped (Syscall::Other) in the pinned Reverie. Detcore presents a
// fixed virtual-root identity (getuid/geteuid/getgid/getegid are
// virtualized to 0) and never tracks a credential change, so these
// succeed as deterministic no-ops returning 0 -- the value a real root
// process gets for a permitted credential change (and the previous
// fs-id, virtual 0, for setfsuid/setfsgid). That lets privilege-
// dropping programs proceed instead of fail-closing and is identical
// across --verify and record/replay.
SyscallClassification::Determinized
if is_credential_identity_noop_syscall(call.number()) =>
{
Ok(0)
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#1851): The file-ownership mutation family
// (chown/fchown/fchownat/lchown) completes the fixed virtual-root
// identity that the credential query (#1549) and credential set
// (#787) families already implement. A real root process's chown
// succeeds for any uid, so 0 is the value the virtual identity must
// observe; forwarding instead returned the errno of whatever host
// identity the backend happened to run under (EPERM with no user
// namespace, EINVAL for an unmapped uid inside a one-uid map, and
// backend-dependent for in-process backends).
//
// The emulation covers the IDENTITY half only. Root privilege
// waives the ownership permission check; it does not waive pathname,
// descriptor, or flag errors, so handle_ownership_change_noop
// translates the target arguments into a side-effect-free metadata
// lookup and returns 0 only if that validation succeeds. ENOENT,
// EBADF, EFAULT, ENOTDIR and the fchownat flag EINVAL therefore
// still reach the guest; the host-identity-dependent EPERM/EINVAL
// cannot be produced at all. No setattr is attempted, so host
// ownership, mode bits, and timestamps are never modified, and
// Detcore does not model per-file ownership, so the success is not
// observable through a later stat -- see
// is_ownership_change_noop_syscall and handle_ownership_change_noop
// for the full boundary, and
// hermit-cli/tests/chown_virtual_root_identity.rs for the bracket
// that fails if this arm's RESULT regresses.
SyscallClassification::Determinized
if is_ownership_change_noop_syscall(call.number()) =>
{
self.handle_ownership_change_noop(guest, call).await
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#827): Deterministic ENOSYS for the Landlock
// unprivileged-sandbox syscalls (landlock_create_ruleset,
// landlock_add_rule, landlock_restrict_self). Landlock availability
// and ABI version depend on the host kernel build
// (CONFIG_SECURITY_LANDLOCK) and runtime LSM stacking, so forwarding
// them (the legacy pass-through) is host-dependent and, because a
// ruleset restricts the whole thread tree, a global-state isolation
// hole. A fixed -ENOSYS is the errno a kernel built without Landlock
// returns, so the guest sees a consistent "sandbox unavailable"
// answer regardless of host; never forwarded to the host and
// bitwise-identical across --verify and record/replay. Untyped
// (Syscall::Other) in the pinned Reverie, so dispatch on the Sysno
// before the typed match below.
SyscallClassification::Determinized if is_landlock_sandbox_syscall(call.number()) => {
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-847): Refuse unmodeled host-kernel probes
// with a fixed ENOSYS so guest behavior does not depend on BPF/LSM
// configuration or mutable page-cache state.
SyscallClassification::Determinized if is_host_kernel_probe_syscall(call.number()) => {
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-838): Review close_range descriptor-table
// synchronization. The pinned Reverie exposes close_range as a raw
// call, so dispatch by Sysno before the typed match.
SyscallClassification::Determinized if call.number() == Sysno::close_range => {
self.handle_close_range(guest, call).await
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-839): Optional modern memory APIs vary with
// host kernel configuration, CET support, and pidfd lifecycle.
// Present the portable feature-absence result instead.
SyscallClassification::Determinized
if is_optional_memory_feature_syscall(call.number()) =>
{
Err(Error::Errno(Errno::ENOSYS))
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#773): epoll_pwait2 is untyped (Syscall::Other)
// in the pinned Reverie revision. It is epoll_pwait with a
// `struct timespec *` timeout; recent glibc routes epoll_wait/
// epoll_pwait through it. Handled identically to epoll_pwait
// (scheduler yield + record/replay forwarding).
SyscallClassification::Determinized if call.number() == Sysno::epoll_pwait2 => {
self.handle_epoll_pwait2(guest, call).await
}
SyscallClassification::Determinized => match call {
Syscall::Getpid(_) | Syscall::Gettid(_) => {
// Keep the inner Tool's syscall observation/injection and
// all errors, then project a successful own ID from shared
// state. Do not return early past the normal posthooks.
self.passthrough(guest, call)
.await
.map(|result| self.project_own_getter_result(guest, call, result))
}
Syscall::Write(w) => self.handle_write(guest, w).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#547)
Syscall::Writev(w) => self.handle_writev(guest, w).await,
Syscall::Openat(o) => self.handle_openat(guest, o).await,
Syscall::Open(o) => self.handle_openat(guest, o.into()).await,
Syscall::Creat(o) => self.handle_openat(guest, o.into()).await,
Syscall::Close(s) => self.handle_close(guest, s).await,
Syscall::Read(s) if self.sock_diag_reply_fd(guest, s.fd()) => {
self.handle_sock_diag_read(guest, s).await
}
Syscall::Read(s) => self.handle_read(guest, s).await,
Syscall::Pread64(s) => self.handle_pread64(guest, s).await,
Syscall::Lseek(s) => self.handle_lseek(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-838): Review regular-file sendfile mediation.
Syscall::Sendfile(s) => self.handle_sendfile(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-887): Present a stable pre-4.5-kernel
// boundary so callers use determinized read/write copying.
Syscall::CopyFileRange(_) => Err(Error::Errno(Errno::ENOSYS)),
// TODO-HUMAN-REVIEW(#794): vectored scatter/gather I/O, mirroring
// read/pread64/pwrite64/writev.
Syscall::Readv(s) if self.sock_diag_reply_fd(guest, s.fd()) => {
self.handle_sock_diag_readv(guest, s).await
}
Syscall::Readv(s) => {
self.handle_readv_with_output(guest, s, &mut rng_readv_output)
.await
}
Syscall::Preadv(s) => {
self.handle_preadv_with_output(guest, s, &mut rng_readv_output)
.await
}
Syscall::Preadv2(s) => {
self.handle_preadv2_with_output(guest, s, &mut rng_readv_output)
.await
}
Syscall::Pwritev(s) => self.handle_pwritev(guest, s).await,
Syscall::Pwritev2(s) => self.handle_pwritev2(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#683)
Syscall::Pwrite64(s) => self.handle_pwrite64(guest, s).await,
// This syscall is advisory; fixed success preserves its API contract.
Syscall::Fadvise64(_) => Ok(0),
Syscall::Mmap(s) => self.handle_mmap(guest, s).await,
Syscall::Madvise(s) => self.handle_madvise(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#775)
Syscall::Mincore(s) => self.handle_mincore(guest, s).await,
Syscall::Munmap(s) => self.handle_munmap(guest, s).await,
Syscall::Mremap(s) => self.handle_mremap(guest, s).await,
Syscall::Stat(s) => self.handle_stat_family(guest, s.into()).await,
Syscall::Lstat(s) => self.handle_stat_family(guest, s.into()).await,
Syscall::Fstat(s) => self.handle_stat_family(guest, s.into()).await,
Syscall::Newfstatat(s) => self.handle_stat_family(guest, s.into()).await,
Syscall::Statx(s) => self.handle_statx(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#877)
Syscall::Readlink(s) => self.handle_readlink(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
Syscall::Readlinkat(s) => self.handle_readlinkat(guest, s).await,
Syscall::Fcntl(s) => self.handle_fcntl(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-912)
Syscall::Ioctl(s)
if syscalls::socket_timestamp_ioctl::is_socket_timestamp_ioctl(s) =>
{
self.handle_socket_timestamp_ioctl(guest, s).await
}
Syscall::Ioctl(s) => self.handle_ioctl(guest, s).await,
Syscall::Futex(s) => self.handle_futex(guest, s).await,
Syscall::Clone(s) => self.handle_clone_family(guest, s.into()).await,
Syscall::Clone3(s) => self.handle_clone_family(guest, s.into()).await,
Syscall::Fork(s) => self.handle_clone_family(guest, s.into()).await,
// Forward vfork as vfork (rather than rewriting to fork) so the
// kernel enforces the CLONE_VFORK parent-blocking contract while the
// child registers itself and runs to exec/exit.
Syscall::Vfork(s) => self.handle_clone_family(guest, s.into()).await,
Syscall::Wait4(s) => self.handle_wait4(guest, s).await,
Syscall::Waitid(s) => self.handle_waitid(guest, s).await,
Syscall::Setpgid(s) => self.handle_setpgid(guest, s).await,
Syscall::Setsid(s) => self.handle_setsid(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
Syscall::Getpgrp(_) | Syscall::Getpgid(_) | Syscall::Getsid(_) => {
self.handle_group_or_session_getter(guest, call).await
}
// Without virtual time the guest reads the host clock. Each of these
// must have a record/replay handler: a recording captures the value
// the guest observed, and replay returns it without reading the host.
Syscall::Gettimeofday(s) => {
if virtualize_time {
self.handle_gettimeofday(guest, s).await
} else {
self.passthrough(guest, call).await
}
}
Syscall::Time(s) => {
if virtualize_time {
self.handle_time(guest, s).await
} else {
self.passthrough(guest, call).await
}
}
Syscall::ClockGettime(s) => {
if virtualize_time {
self.handle_clock_gettime(guest, s).await
} else {
self.passthrough(guest, call).await
}
}
Syscall::ClockGetres(s) => {
if virtualize_time {
self.handle_clock_getres(guest, s).await
} else {
self.passthrough(guest, call).await
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::ClockSettime(_) => Err(Error::Errno(Errno::EPERM)),
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-892)
Syscall::Getitimer(s) => self.handle_getitimer(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Setitimer(s) => self.handle_setitimer(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-857): Virtual NTP query and fixed mutation refusal.
Syscall::Adjtimex(s) => {
if virtualize_time {
self.handle_adjtimex(guest, s).await
} else {
self.handle_unsupported_syscall(
guest,
call,
dettid,
panic_on_unsupported_syscalls,
)
.await
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-857): Clock-id form of virtual NTP query.
Syscall::ClockAdjtime(s) => {
if virtualize_time {
self.handle_clock_adjtime(guest, s).await
} else {
self.handle_unsupported_syscall(
guest,
call,
dettid,
panic_on_unsupported_syscalls,
)
.await
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-857): Empty virtual kernel ring buffer.
Syscall::Syslog(s) => self.handle_syslog(guest, s).await,
Syscall::ArchPrctl(s) => self.handle_arch_prctl(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
Syscall::Seccomp(s) => self.handle_seccomp(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Prctl(s) => self.handle_prctl(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Getpriority(s) => self.handle_getpriority(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Setpriority(s) => self.handle_setpriority(guest, s).await,
Syscall::Uname(s) => self.handle_uname(guest, s).await,
Syscall::ExitGroup(s) => self.handle_exit_group(guest, s).await,
Syscall::Exit(s) => self.handle_exit(guest, s).await,
Syscall::Dup(w) => self.handle_dup(guest, w).await.map_err(Into::into),
Syscall::Dup2(w) => self.handle_dup2(guest, w).await,
Syscall::Dup3(w) => self.handle_dup3(guest, w).await,
Syscall::Pipe(w) => self.handle_pipe2(guest, w.into()).await,
Syscall::Pipe2(w) => self.handle_pipe2(guest, w).await,
Syscall::Getrandom(s) => self.handle_getrandom(guest, s).await,
Syscall::Utime(s) => self.handle_utime(guest, s).await.map_err(Into::into),
Syscall::Utimes(s) => self.handle_utimes(guest, s).await.map_err(Into::into),
// NB: lutimes is a libc function not a syscall
Syscall::Utimensat(s) => self.handle_utimensat(guest, s).await.map_err(Into::into),
// NB: futimes/futimens are libc functions not a syscall,
// futimesat is obsolete, return -ENOSYS for simplicity.
Syscall::Futimesat(_s) => Err(Error::Errno(Errno::ENOSYS)),
// io_uring completion and memory-sharing semantics are not deterministic.
Syscall::IoUringSetup(_)
| Syscall::IoUringEnter(_)
| Syscall::IoUringRegister(_) => Err(Error::Errno(Errno::ENOSYS)),
Syscall::Socket(s) => self.handle_socket(guest, s).await,
Syscall::Socketpair(s) => self.handle_socketpair(guest, s).await,
Syscall::Connect(s) => self.handle_connect(guest, s).await,
Syscall::Bind(s) => self.handle_bind(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Setsockopt(s) => self.handle_setsockopt(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Listen(s) => self.handle_listen(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Getsockname(s) => self.handle_getsockname(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Getpeername(s) => self.handle_getpeername(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Getsockopt(s) => self.handle_getsockopt(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#818): shutdown is the lone remaining
// socket-family syscall; half-closes a tracked socket and
// forwards via record_or_replay (KVM ratchet round 12).
Syscall::Shutdown(s) => self.handle_shutdown(guest, s).await,
Syscall::Eventfd(s) => self.handle_eventfd2(guest, s.into()).await,
Syscall::Eventfd2(s) => self.handle_eventfd2(guest, s).await,
Syscall::Signalfd(s) => self.handle_signalfd4(guest, s.into()).await,
Syscall::Signalfd4(s) => self.handle_signalfd4(guest, s).await,
Syscall::TimerfdCreate(s) => self.handle_timerfd_create(guest, s).await,
Syscall::TimerfdSettime(s) => self.handle_timerfd_settime(guest, s).await,
Syscall::TimerfdGettime(s) => self.handle_timerfd_gettime(guest, s).await,
Syscall::InotifyInit(s) => {
self.handle_inotify_init1(guest, InotifyInit1::from(s))
.await
}
Syscall::InotifyInit1(s) => self.handle_inotify_init1(guest, s).await,
Syscall::InotifyAddWatch(s) => self.handle_inotify_add_watch(guest, s).await,
Syscall::InotifyRmWatch(s) => self.handle_inotify_rm_watch(guest, s).await,
Syscall::MemfdCreate(s) => self.handle_memfd_create(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-862): Record/replay and register pidfds.
Syscall::PidfdOpen(s) => self.handle_pidfd_open(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-899): Host object handles and mount IDs
// are outside Detcore's filesystem identity model.
Syscall::NameToHandleAt(_) => Err(Error::Errno(Errno::EOPNOTSUPP)),
Syscall::Userfaultfd(s) => self.handle_userfaultfd(guest, s).await,
Syscall::Accept(s) => self.handle_accept4(guest, s.into()).await,
Syscall::Accept4(s) => self.handle_accept4(guest, s).await,
Syscall::Nanosleep(s) => self.handle_nanosleep_family(guest, s.into()).await,
Syscall::ClockNanosleep(s) => self.handle_nanosleep_family(guest, s.into()).await,
Syscall::SchedYield(s) => self.handle_sched_yield(guest, s).await,
// NB: getdents is not recommended, (g)libc should call getdents64 only
// see: sysdeps/unix/sysv/linux/getdents.c.
Syscall::Getdents(s) => self.handle_getdents(guest, s).await,
Syscall::Getdents64(s) => self.handle_getdents64(guest, s).await,
Syscall::Poll(s) => self.handle_poll(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#686): Review scratch fd sets and scheduler polling.
Syscall::Pselect6(s) => self.handle_pselect6(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#800): select is the timeval sibling of pselect6.
Syscall::Select(s) => self.handle_select(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
Syscall::Ppoll(s) => self.handle_ppoll(guest, s).await,
Syscall::EpollCreate(s) => {
self.handle_epoll_create1(guest, EpollCreate1::from(s))
.await
}
Syscall::EpollCreate1(s) => self.handle_epoll_create1(guest, s).await,
Syscall::EpollCtl(s) => self.handle_epoll_ctl(guest, s).await,
Syscall::EpollPwait(s) => self.handle_epoll_pwait(guest, s).await,
Syscall::EpollWait(s) => self.handle_epoll_wait(guest, s).await,
Syscall::EpollWaitOld(s) => panic!(
"Not handling deprecated syscall: {}",
s.display(&guest.memory())
),
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#549)
// The obsolete x86_64 entry point is absent from modern Linux kernels.
Syscall::EpollCtlOld(_) => Err(Error::Errno(Errno::ENOSYS)),
Syscall::SchedGetaffinity(s) => self.handle_sched_getaffinity(guest, s).await,
Syscall::SchedSetaffinity(s) => self.handle_sched_setaffinity(guest, s).await,
// ===== BATCH 3: NUMA memory-placement and Linux CPU-scheduling
// policy. Hermit exposes a single virtual NUMA node and replaces
// the Linux scheduler with Detcore, so these are inoperative and
// are virtualized to fixed, host-independent results (see the
// determinism argument in syscall_classification.rs). Setters and
// count-returning calls are no-ops; getters emulate a default
// single-node / SCHED_OTHER answer.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#720)
Syscall::Mbind(_) => Ok(0),
Syscall::SetMempolicy(_) => Ok(0),
Syscall::GetMempolicy(s) => self.handle_get_mempolicy(guest, s).await,
Syscall::MigratePages(_) => Ok(0),
Syscall::MovePages(s) => self.handle_move_pages(guest, s).await,
Syscall::SchedSetscheduler(_) => Ok(0),
Syscall::SchedSetparam(_) => Ok(0),
// Report the fixed default policy SCHED_OTHER (0).
Syscall::SchedGetscheduler(_) => Ok(0),
Syscall::SchedGetparam(s) => self.handle_sched_getparam(guest, s).await,
Syscall::SchedRrGetInterval(s) => self.handle_sched_rr_get_interval(guest, s).await,
// ===== BATCH 51: fail-closed utility syscalls, re-enabling chrt,
// ionice, and flock under --strict. Detcore replaces the Linux
// scheduler, exposes a single virtual CPU, and serializes guest
// threads, so a thread's Linux scheduling attributes (sched_getattr)
// and I/O priority (ioprio_set) are inert: those two have no
// deterministic effect and are emulated to fixed, host-independent
// results (see syscall_classification.rs).
//
// flock is NOT in that inert group and is not emulated. This comment
// used to claim it "is never contended inside the serialized
// container"; that was measured false -- two open file descriptions
// in ONE process both held the same LOCK_EX under the old no-op,
// where native Linux excluded the second -- and the no-op was
// removed. Serializing THREADS does not make a whole-file lock
// uncontended, because flock conflicts are between OPEN FILE
// DESCRIPTIONS. flock is now forwarded to the kernel, like fcntl's
// POSIX record locks; handle_flock carries the determinism argument
// and the one case Detcore refuses (a contended BLOCKING request,
// which it cannot park a thread on deterministically).
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#791)
Syscall::SchedGetattr(s) => self.handle_sched_getattr(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-841): Review virtual sched_setattr no-op policy.
Syscall::SchedSetattr(s) => self.handle_sched_setattr(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#791)
Syscall::IoprioSet(s) => self.handle_ioprio_set(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-881): Review virtual ioprio_get defaults.
Syscall::IoprioGet(s) => self.handle_ioprio_get(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#2373)
Syscall::Flock(s) => self.handle_flock(guest, s).await,
// TODO-HUMAN-REVIEW(PR-1064): recvfrom/read/readv/recvmmsg reach
// a NETLINK_SOCK_DIAG dump exactly as recvmsg does. Until they
// were routed through the same sanitizer, four of the five
// usable receive syscalls returned raw host socket inode
// numbers, which made the determinization optional from the
// guest's point of view: `socket.recv()` alone was enough to
// skip it. Non-socket-diag descriptors take the same path as
// before; the predicate is checked inside.
Syscall::Recvfrom(s) if self.sock_diag_reply_fd(guest, s.fd()) => {
self.handle_sock_diag_recvfrom(guest, s).await
}
Syscall::Recvfrom(s) => self.handle_socket_receive(guest, s, s.fd(), true).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-901)
Syscall::Recvmsg(s) => self.handle_recvmsg(guest, s).await,
Syscall::Sendto(s) => self.handle_sendrecv(guest, s).await,
Syscall::Sendmsg(s) => self.handle_sendmsg(guest, s).await,
Syscall::Sendmmsg(s) => self.handle_sendmmsg(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#788): recvmmsg is the multi-message form of
// recvmsg and shares its NonblockableSyscall impl. The fd is made
// temporarily nonblocking, the kernel fills the mmsghdr array
// atomically, and the Detcore scheduler owns any blocking, so the
// timeout argument (deliberately ignored, see helpers.rs) does not
// introduce nondeterminism.
// TODO-HUMAN-REVIEW(PR-901): Review batched ancillary timestamp rewriting.
Syscall::Recvmmsg(s) if self.sock_diag_reply_fd(guest, s.fd()) => {
self.handle_sock_diag_recvmmsg(guest, s).await
}
Syscall::Recvmmsg(s) => self.handle_recvmmsg(guest, s).await,
Syscall::RtSigtimedwait(s) => self.handle_rt_sigtimedwait(guest, s).await,
Syscall::RtSigsuspend(s) => self.handle_rt_sigsuspend(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::RtSigpending(s) => self.handle_rt_sigpending(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Kill(s) => {
let result = self.handle_kill(guest, s).await;
let result = self
.report_backend_signal_refusal(guest, Sysno::kill, result)
.await;
if s.sig() == libc::SIGALRM && result.is_ok() {
let target = if guest.thread_state().nested_pid_namespace {
Some(GuestSigalrmTarget::Unclassified)
} else {
match s.pid() {
pid if pid > 0 => {
Some(GuestSigalrmTarget::Process(DetPid::from_raw(pid)))
}
0 => Some(GuestSigalrmTarget::Group(None)),
-1 => Some(GuestSigalrmTarget::All),
libc::pid_t::MIN => None,
pid => {
Some(GuestSigalrmTarget::Group(Some(DetPid::from_raw(-pid))))
}
}
};
if let Some(target) = target {
self.note_guest_sigalrm(guest, target).await;
}
}
result
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Tgkill(s) => {
let result = self.handle_tgkill(guest, s).await;
self.report_backend_signal_refusal(guest, Sysno::tgkill, result)
.await
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#812)
Syscall::Tkill(s) => {
let result = self.handle_tkill(guest, s).await;
self.report_backend_signal_refusal(guest, Sysno::tkill, result)
.await
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#812)
Syscall::RtSigqueueinfo(s) => {
let result = self.handle_rt_sigqueueinfo(guest, s).await;
let result = self
.report_backend_signal_refusal(guest, Sysno::rt_sigqueueinfo, result)
.await;
if s.sig() == libc::SIGALRM && result.is_ok() {
let target = if guest.thread_state().nested_pid_namespace {
GuestSigalrmTarget::Unclassified
} else {
GuestSigalrmTarget::Process(DetPid::from_raw(s.tgid()))
};
self.note_guest_sigalrm(guest, target).await;
}
result
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#812)
Syscall::RtTgsigqueueinfo(s) => {
let result = self.handle_rt_tgsigqueueinfo(guest, s).await;
self.report_backend_signal_refusal(guest, Sysno::rt_tgsigqueueinfo, result)
.await
}
Syscall::Execve(s) => self.handle_execveat(guest, s.into()).await,
Syscall::Execveat(s) => self.handle_execveat(guest, s).await,
Syscall::Getcpu(s) => self.handle_getcpu(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#1549): Credential-query
// family emulated to the fixed virtual-root identity (0). See
// syscall_classification.rs for the determinism rationale.
// getuid/geteuid/getgid/getegid return the constant directly;
// getresuid/getresgid write the constant to each provided result
// pointer. Never forwarded to the host, so the answer no longer
// depends on whether the backend runs the guest in a
// CLONE_NEWUSER namespace.
Syscall::Getuid(_)
| Syscall::Geteuid(_)
| Syscall::Getgid(_)
| Syscall::Getegid(_) => Ok(0),
Syscall::Getresuid(s) => self.handle_getresuid(guest, s).await,
Syscall::Getresgid(s) => self.handle_getresgid(guest, s).await,
Syscall::RtSigprocmask(s) => self.handle_rt_sigprocmask(guest, s).await,
Syscall::RtSigaction(s) => {
let result = self.handle_rt_sigaction(guest, s).await;
// Rule 5, decided from the installed disposition: an
// old-action fault after the install still installed it
// (https://github.com/rrnewton/hermit/issues/4139).
if s.signum() == libc::SIGALRM
&& s.action().is_some()
&& guest.config().sequentialize_threads
{
crate::tool_global::note_sigalrm_event(
guest,
crate::tool_global::GlobalRequest::NoteSigalrmDisposition,
)
.await;
}
result
}
Syscall::Alarm(s) => self.handle_alarm(guest, s).await,
Syscall::Pause(s) => self.handle_pause(guest, s).await,
Syscall::Getrusage(s) => self.handle_getrusage(guest, s).await,
Syscall::Sysinfo(s) => self.handle_sysinfo(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
Syscall::Times(s) => self.handle_times(guest, s).await,
Syscall::Prlimit64(s) => self.handle_prlimit64(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Getrlimit(s) => self.handle_getrlimit(guest, s).await,
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#663)
Syscall::Setrlimit(s) => self.handle_setrlimit(guest, s).await,
// POSIX per-process timers use the virtual clock and scheduler
// for deterministic arming and supported signal delivery.
Syscall::TimerCreate(s) => self.handle_timer_create(guest, s).await,
Syscall::TimerSettime(s) => self.handle_timer_settime(guest, s).await,
Syscall::TimerGettime(s) => self.handle_timer_gettime(guest, s).await,
Syscall::TimerGetoverrun(s) => self.handle_timer_getoverrun(guest, s).await,
Syscall::TimerDelete(s) => self.handle_timer_delete(guest, s).await,
// Serialized threads share a total memory order, so process-wide
// memory barriers are trivially satisfied and can be no-ops.
Syscall::Membarrier(s) => self.handle_membarrier(guest, s).await,
// Filesystem statistics: passthrough is record/replay-aware so the
// (otherwise host-dependent) result is captured and reproduced.
// statfs/fstatfs run the real syscall, then canonicalize the
// host-varying fields (free blocks/inodes, fsid) so the result is
// deterministic under --verify (a bare passthrough diverged, e.g.
// for tar).
Syscall::Statfs(s) => self.handle_statfs(guest, s).await,
Syscall::Fstatfs(s) => self.handle_fstatfs(guest, s).await,
unexpected => {
self.handle_unsupported_syscall(
guest,
unexpected,
dettid,
panic_on_unsupported_syscalls,
)
.await
}
},
// Where the run declares a launcher, the kernel's program break
// is the launcher's: Detcore answers brk from its model of the
// guest's (`Detcore::handle_brk`).
SyscallClassification::PassThrough
if call.number() == Sysno::brk && self.declares_launcher() =>
{
self.handle_brk(guest, call).await
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-2985): Review scheduler tracking of set_tid_address.
// Linux and the backend own the pass-through call. Detcore also
// mirrors its successful registration into the scheduler because
// the scheduler supplies the logical CHILD_CLEARTID wake.
SyscallClassification::PassThrough if call.number() == Sysno::set_tid_address => {
match call {
Syscall::SetTidAddress(s) => self.handle_set_tid_address(guest, s).await,
_ => unreachable!("set_tid_address unexpectedly lost its typed variant"),
}
}
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-2223): Review observing
// the robust-list registration without changing its pass-through
// classification. This is still a pass-through — Linux owns the
// registration and supplies its result — but Detcore remembers the
// head address so thread exit can replay `exit_robust_list()`
// against its own futex waiter pool.
SyscallClassification::PassThrough if call.number() == Sysno::set_robust_list => {
match call {
Syscall::SetRobustList(s) => self.handle_set_robust_list(guest, s).await,
_ => self.passthrough(guest, call).await,
}
}
// faccessat2 and fchmodat2 are untyped in the pinned Reverie revision; the
// reviewed classification table routes them, and every other reviewed
// PassThrough syscall, through the blanket arm below.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-644): Keep dispatch aligned with the reviewed classification.
// A pass-through whose restored mask may deliver a pending real-time
// timer expiration (https://github.com/rrnewton/hermit/issues/4089).
SyscallClassification::PassThrough if call.number() == Sysno::rt_sigreturn => {
crate::syscalls::signal_shadow::record_rt_sigreturn(guest).await;
self.observe_sigreturn_mask(guest).await;
// A signal the restored mask unblocks is delivered on this
// same return to user space (`returned_from_sigreturn`).
guest.thread_state_mut().returned_from_sigreturn = true;
self.passthrough(guest, call).await
}
SyscallClassification::PassThrough => self.passthrough(guest, call).await,
// The kernel's restart of a wait Detcore emulates resumes it; any other
// `restart_syscall` keeps the unsupported-syscall policy below.
// AUTONOMOUS-BOT-IMPLEMENTED
SyscallClassification::Unsupported if call.number() == Sysno::restart_syscall => {
self.handle_restart_syscall(
guest,
call,
restart_block,
dettid,
panic_on_unsupported_syscalls,
)
.await
}
SyscallClassification::Unsupported => {
self.handle_unsupported_syscall(guest, call, dettid, panic_on_unsupported_syscalls)
.await
}
};
// A copy may already have changed guest memory before reporting a
// terminal backend error. Do not perform even the syscall-result
// display's memory reads, or later observers/posthooks/timer effects.
// Random-device reads have completed their release RPC before returning;
// getrandom acquired no file resource. Physical cleanup belongs to the
// backend failure owner, not to this observer fence.
if res.as_ref().is_err_and(crate::random::is_copy_failure) {
return res;
}
if res.is_ok() && !removed_inodes.is_empty() {
self.retire_removed_inodes(guest, removed_inodes).await;
}
if res.is_ok() {
for fifo in removed_guest_fifos {
crate::fifo_peer::forget_guest_made(fifo);
}
self.note_guest_made_fifo(guest, &call).await;
}
// A read or write that moved bytes through a pipe or FIFO readies the
// threads polling its other direction (`--pipe-wake-latency`).
if let Ok(moved) = res
&& moved > 0
&& let Some((fd, wants)) = crate::pipe_wake::progress_of(&call)
{
self.note_fd_readied(guest, fd, wants).await;
}
Self::unmap_orphaned_staging_pages(guest).await;
// With asynchronous exit completion, a descriptor that would make this
// guest serve another guest's exit ends the run before anything
// observes the result (crate::exit_dependencies).
if let Ok(ret) = res {
self.refuse_exit_dependencies(guest, &call, ret).await;
}
// The guest resumes with this result in its return register (A5 step
// 3.2). An error that is not an errno names no register value.
let finished = match &res {
Ok(value) => Some(*value),
Err(Error::Errno(errno)) => Some(-(errno.into_raw() as i64)),
Err(_) => None,
};
if let Some(result) = finished {
guest.thread_state_mut().delivery_interruption = Some(reverie::Interruption::Return {
syscall: call.number() as i64,
result,
});
}
detlog!(
event = crate::detlog::DetLogEvent::SyscallResult {
finished_syscall_number: new_count,
};
"[syscall][detcore, dtid {}] finish syscall #{}: {} = {:?}",
dettid,
new_count,
display_syscall_finished(&call, &guest.memory(), &res),
res
);
// Same guest-logical-control point that already anchors the stack/heap hashes: the
// syscall is complete and its result written back, so the guest logically has control.
// Reading registers is itself backend work, so keep the disabled path inert. In
// particular, a run that does not request register evidence must not be perturbed by
// collecting data that will immediately be discarded.
if self.cfg.detlog_regs {
let control_point_regs = guest.regs().await;
let regs_seq = guest.thread_state().stats.syscall_count;
self.detlog_registers(guest, &control_point_regs, regs_seq);
}
// brk is PassThrough, so nothing else records where the guest's heap is.
// Both brk(NULL) and brk(addr) return the break in effect afterwards.
if let Syscall::Brk(_) = &call
&& let Ok(brk) = res
&& brk > 0
{
guest
.thread_state()
.memory_metadata
.lock()
.expect("memory metadata mutex poisoned")
.observe_brk(brk as u64);
}
if let Err(error) = self.detlog_memory_maps(guest) {
let res = Err(error);
self.publish_posthook_checkpoint(guest, new_count, &res)
.await;
return self.complete_syscall(guest, res).await;
}
// Same control point again, for the bytes this syscall moved through a guest buffer.
// Unlike the two mapping hashes above, the extent comes from the syscall's OWN
// arguments, so it does not matter whether the buffer lives on the stack, in the brk
// heap, in BSS or in an anonymous mmap -- the last two of which neither mapping hash
// can see. Only successful calls moved anything.
if let Ok(ret) = &res
&& self.cfg.detlog_io_buffers
&& let Err(error) = io_buffers::detlog_io_buffers(
guest,
&call,
*ret,
dettid,
rng_readv_output.as_deref(),
)
{
let res = Err(error);
self.publish_posthook_checkpoint(guest, new_count, &res)
.await;
return self.complete_syscall(guest, res).await;
}
if sequentialize_threads && self.cfg.should_trace_schedevent() {
trace_schedevent(
guest,
with_guest_time(
guest,
SchedEvent::syscall(dettid, call.number(), SyscallPhase::Posthook),
),
true,
)
.await;
}
// Posthook happens-before anchors this call reached fire now that its
// result is decided and before the guest resumes
// (https://github.com/rrnewton/hermit/issues/3929).
self.publish_posthook_checkpoint(guest, new_count, &res)
.await;
// The syscall is finished; a turn the post-hook takes (a timeslice
// end) is the thread's own and advances global time as usual.
guest.thread_state_mut().in_uncharged_bootstrap_syscall = false;
self.post_handler_hook(guest, CheckIn::Event).await;
// Defense-in-depth: unless the backend already owns this guarantee,
// force the syscall-clobbered registers (%rcx/%r11 on x86-64) to
// deterministic values before returning to the guest.
if !self.cfg.backend.virtualizes_syscall_clobbers {
self.canonicalize_syscall_clobbers(guest).await;
}
// Signal phase 1, step I4: a due SIGALRM entry is delivered here.
self.complete_syscall(guest, res).await
}
async fn on_exit_thread<G: GlobalRPC<Self::GlobalState>>(
&self,
tid: Tid,
global_state: &G,
mut thread_state: Self::ThreadState,
exit_status: ExitStatus,
) -> Result<(), Error> {
let dettid = thread_state.dettid;
thread_state.delivery_counters.log(dettid);
// Cancellation can consume the backend's transferred state before the
// successful-exec callback has rebound its logical identity.
let current = DetTid::from_raw(tid.as_raw());
let transferred_exec = current != dettid;
debug!(
"[detcore, dtid {}] thread exit hook, deregistering from scheduler.",
dettid
);
// Close the final in-progress timeslice so this thread contributes its
// last (partial) slice to the run report, even if it never exhausted a
// full slice.
let now = thread_state.thread_logical_time.as_nanos();
thread_state.stats.close_final_timeslice(now);
// Ptrace keeps its exit event stopped while this hook runs. KVM runs
// this owner hook after worker joins and child waitability publication.
// Publish final CPU in this synchronous prefix: serial KVM waitid waits
// for that owned notification before consuming, not for the hook tail.
// DETERMINISTIC RECOVERY (TODO-HUMAN-REVIEW(PR-1147)). `ThreadState::detpid`
// is `Option` and starts as `None` ("Initialized later" at the clone site),
// so a thread that reaches the exit hook before its per-thread identity is
// populated used to `.expect()` here. That panic fires inside a Reverie
// teardown callback, while the backend still owns the exit event, which is
// the worst place to abort: it can wedge the supervisor rather than fail one
// thread. Fall back to the PROCESS-level `self.detpid`, which is
// non-optional and preserves the identity source selected for this
// backend: virtual for the current DBT ABI, physical for ABI v1. Warn so
// the exceptional window is observable instead of silently papered over.
//
// CORRECTED BY #2348, and stated rather than quietly dropped: the normal
// current-ABI DBT path pre-populates `thread_state.detpid` with the
// client-published virtual process identity, so thread start and exit
// agree on that value. This recovery is only for a thread that exits
// before that initialization. In that exceptional window thread start
// would fall back to `guest.pid()` (the physical host pid for `DbtGuest`),
// while this exit path uses `self.detpid`. ABI v1 also intentionally keeps
// physical callback identities. The warning makes either compatibility
// or recovery path observable; this comment does not claim those fallback
// identities are virtual or independently deterministic.
let (detpid, used_process_detpid) =
select_thread_exit_detpid(thread_state.detpid, self.detpid);
if used_process_detpid {
tracing::warn!(
"[detcore, dtid {}] thread exited before its per-thread detpid was \
initialized; falling back to the process detpid {}",
dettid,
self.detpid
);
}
// The transferred survivor is the final leader even if cancellation
// precedes local rebinding. Its snapshot includes the worker's tail;
// the earlier displaced-leader snapshot is only a prefix.
if current == detpid {
if !thread_state.record_exited_child_process_cpu_time(detpid) {
return Err(Error::Tool(anyhow::anyhow!(
"child CPU publication ownership changed before final exit"
)));
}
} else {
thread_state.account_process_cpu_time();
}
let mm_id = thread_state.mm_id;
let exit_signal = match &exit_status {
ExitStatus::Signaled(signal, _) => Some(*signal as i32),
ExitStatus::SignaledRaw(signal, _) => Some(*signal),
ExitStatus::Exited(_) => None,
};
// Publish each owner's final clock under its own RPC identity before
// counting that owner in the complete physical-exit barrier. Otherwise
// the group's maximum could be charged to whichever callback finishes
// last, followed by a backwards update from its real deregistration.
// Exec preparation already cleared the old image's robust list. An
// unbound transfer must authenticate its consuming cleanup before any
// ordinary RPC can publish the survivor's clock under the leader TID.
let exit_time_accounted = !transferred_exec
&& (!thread_state.has_matching_robust_list_exit(exit_signal)
|| acknowledge_robust_list_exit_time(
thread_state.thread_logical_time.clone(),
global_state,
mm_id,
)
.await);
if !exit_time_accounted {
// Preserve benign cleanup for a retired incarnation without
// acknowledging an unaccounted owner or releasing its staged wakes.
thread_state.drop_robust_list_head_at_exit();
}
if exit_time_accounted
&& let Some((_group_time, ready)) = thread_state.take_robust_list_wakes_after_exit(
exit_signal,
thread_state.thread_logical_time.clone(),
)
{
let identities: Vec<_> = ready
.iter()
.map(|(owner, wake)| (*owner, wake.futex))
.collect();
let counts = robust_list_wakes_after_exit(
thread_state.thread_logical_time.clone(),
global_state,
mm_id,
ready,
)
.await;
for ((owner, futex), count) in identities.into_iter().zip(counts) {
info!(
"[detcore, dtid {}] robust-list owner death woke {} waiter(s) on futex {:?} after physical exit",
owner, count, futex,
);
}
}
// Warnings this address space could not send, this hook's own among
// them, go out while the hook's connection still serves it
// (`crate::guest_log`).
guest_log::flush_unsent_through(global_state).await;
let pending_chaos_epochs = thread_state.take_pending_chaos_epochs();
let deregistration = ThreadDeregistration {
dettid,
detpid,
mm: mm_id,
thread_start_entered: thread_state.thread_start_entered,
timeslice_stats: thread_state.stats.timeslice_stats,
syscall_count: thread_state.stats.syscall_count,
chaos_epochs: pending_chaos_epochs,
};
if transferred_exec {
tool_global::retire_exec(
thread_state.thread_logical_time.clone(),
global_state,
deregistration,
exit_status.signal().is_some(),
)
.await?;
} else {
deregister_thread(
thread_state.thread_logical_time.clone(),
&self.cfg,
global_state,
deregistration,
)
.await;
}
self.record_or_replay
.on_exit_thread(
tid,
global_state,
thread_state.record_or_replay,
exit_status,
)
.await?;
Ok(())
}
}
#[cfg(test)]
mod subscription_tests {
use super::*;
fn strict_config(passthru_opt: bool) -> Config {
Config {
sequentialize_threads: true,
deterministic_io: true,
passthru_opt,
..Default::default()
}
}
/// The last row of the pinned table is the one a `Sysno::iter()` sweep
/// drops. `lsm_list_modules` is Determinized AND deterministically refused,
/// so before this was fixed it executed natively against the host under
/// `--passthru-opt` — which is the default for `hermit record` and
/// `hermit replay` — instead of receiving its fixed refusal.
#[test]
fn passthru_opt_covers_the_final_row_of_the_pinned_table() {
let last = Sysno::last();
// Guard the premise: if the table endpoint moves, this test must be
// re-derived rather than silently passing on a different syscall.
assert_eq!(last, Sysno::lsm_list_modules);
assert!(crate::is_determinized_syscall(last));
assert!(crate::is_deterministically_refused_syscall(last));
// The bug this pins: the final row is absent from `Sysno::iter()`.
assert!(!Sysno::iter().any(|sysno| sysno == last));
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(true));
assert!(
subscriptions.iter_syscalls().any(|sysno| sysno == last),
"{last} must be intercepted under passthru_opt; it is deterministically refused"
);
}
#[test]
fn passthru_opt_intercepts_every_unsupported_syscall() {
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(true));
let unsupported: Vec<Sysno> = crate::all_pinned_syscalls()
.filter(|sysno| crate::is_unsupported_syscall(*sysno))
.collect();
assert_eq!(unsupported, [Sysno::restart_syscall]);
for syscall in unsupported {
assert!(
subscriptions
.iter_syscalls()
.any(|subscribed| subscribed == syscall),
"passthru_opt allowed unsupported {syscall} to bypass Detcore"
);
}
}
/// `passthru_opt` is not a niche flag: `record_or_replay_config` turns it on
/// for every `hermit record` / `hermit replay`, so this covers the record
/// and replay subscription too.
#[test]
fn passthru_opt_subscribes_every_determinized_syscall() {
let determinized: Vec<Sysno> = crate::all_pinned_syscalls()
.filter(|sysno| crate::is_determinized_syscall(*sysno))
.collect();
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(true));
let delivered: Vec<Sysno> = subscriptions.iter_syscalls().collect();
let missing = determinized
.iter()
.filter(|sysno| !delivered.contains(sysno))
.copied()
.collect::<Vec<_>>();
assert!(
missing.is_empty(),
"passthru_opt let Determinized syscalls bypass Detcore: {}",
missing
.iter()
.map(|sysno| sysno.to_string())
.collect::<Vec<_>>()
.join(" ")
);
assert!(
delivered.contains(&Sysno::syslog),
"syslog must reach its deterministic Detcore handler"
);
for sysno in [
Sysno::access,
Sysno::faccessat,
Sysno::faccessat2,
Sysno::getxattr,
Sysno::lgetxattr,
Sysno::setxattr,
Sysno::lsetxattr,
Sysno::removexattr,
Sysno::lremovexattr,
Sysno::listxattr,
Sysno::llistxattr,
] {
assert!(
delivered.contains(&sysno),
"{sysno} must reach Detcore, which answers it for the procfs files it renders"
);
}
}
#[test]
fn passthru_opt_intercepts_thread_exit_registrations() {
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(true));
for syscall in [Sysno::set_tid_address, Sysno::set_robust_list] {
assert!(
subscriptions
.iter_syscalls()
.any(|subscribed| subscribed == syscall),
"passthru_opt allowed {syscall} to bypass Detcore's thread-exit state"
);
}
}
#[test]
fn passthru_opt_leaves_unlisted_passthrough_syscalls_unsubscribed() {
assert_eq!(
syscall_classification::classify_syscall(Sysno::chdir),
syscall_classification::SyscallClassification::PassThrough
);
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(true));
assert!(
!subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::chdir),
"chdir is PassThrough and must remain outside the partial subscription"
);
}
/// Io-buffer hashing can only hash a syscall Detcore is subscribed to.
/// Under `--passthru-opt` the subscription narrows, so the check's coverage
/// narrows with it -- silently, because a syscall that never reaches
/// Detcore produces no record and no record is indistinguishable from a
/// syscall that moved no bytes.
///
/// Measured 2026-08-21 on `f05bf04e4f`, one probe calling `getcwd`,
/// `recvmsg`, `readv` and `readlink`:
///
/// ```text
/// hermit --log=info run --detlog-io-buffers -- ./probe 12 [iobuf] records
/// hermit --log=info run --passthru-opt --detlog-io-buffers -- ./probe 11 [iobuf] records
/// ```
///
/// The single lost syscall is `getcwd`, and the reason is structural: 9 of
/// the 20 are in the literal allow-list and 10 more arrive via the
/// unconditional Determinized sweep at the end of `subscriptions`, but
/// `getcwd` is classified `PassThrough`, so neither path picks it up.
///
/// WHY THIS IS A TEST AND NOT A RUNTIME WARNING. The set is stable, so a
/// warning would print the same sentence on every `--passthru-opt` run
/// forever and be tuned out within a week. The exposure is not today's
/// one-syscall gap; it is that 19 of 20 holds only because two
/// INDEPENDENTLY MAINTAINED lists happen to agree -- the classification
/// table in `syscall_classification.rs` and the match arms in
/// `io_buffers.rs`. Reclassifying any one of those ten from `Determinized`
/// to `PassThrough` would drop it out of the sweep and out of io-buffers'
/// reach with nothing failing. This asserts the relationship so that edit
/// cannot land quietly.
///
/// It is deliberately an EQUALITY, not a subset check, so it fails in both
/// directions: a nineteenth syscall going missing, and `getcwd` becoming
/// covered while this expectation still claims it is not.
#[test]
fn passthru_opt_leaves_io_buffer_hashing_blind_only_for_getcwd() {
let subscribed: Vec<Sysno> = <Detcore as Tool>::subscriptions(&strict_config(true))
.iter_syscalls()
.collect();
let unreachable: Vec<Sysno> = crate::io_buffers::HASHED_SYSCALLS
.iter()
.copied()
.filter(|sysno| !subscribed.contains(sysno))
.collect();
assert_eq!(
unreachable,
vec![Sysno::getcwd],
"--passthru-opt changes which syscalls io-buffer hashing can reach, and the set \
moved. {} of {} reachable. If a syscall was RECLASSIFIED out of Determinized, that \
silently shrank an enabled determinism check -- re-derive rather than editing this \
expectation to match.",
crate::io_buffers::HASHED_SYSCALLS.len() - unreachable.len(),
crate::io_buffers::HASHED_SYSCALLS.len()
);
}
/// The other direction, and the reason the check above is worth having:
/// with the default subscription every buffer-carrying syscall is
/// reachable, so there is nothing to report on an ordinary run.
#[test]
fn the_default_subscription_reaches_every_io_buffer_syscall() {
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(false));
let unreachable: Vec<Sysno> = crate::io_buffers::HASHED_SYSCALLS
.iter()
.copied()
.filter(|sysno| !subscriptions.iter_syscalls().any(|s| s == *sysno))
.collect();
assert!(
unreachable.is_empty(),
"without --passthru-opt every io-buffer syscall must be reachable; missing {unreachable:?}"
);
}
#[test]
fn strict_subscriptions_intercept_every_event_by_default() {
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(false));
assert_eq!(subscriptions, Subscription::all());
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::ppoll)
);
// On a backend without its own CPUID table (ptrace), `--no-virtualize-cpuid`
// must not subscribe to `cpuid` in either mode. The
// subscription is what makes Reverie probe and enable CPUID faulting, and
// a host without faulting then logs an ERROR for each traced exec
// (https://github.com/rrnewton/hermit/issues/3460). Everything else stays
// as intercepted as it is with CPUID virtualization on.
for passthru_opt in [false, true] {
let virtualized = <Detcore as Tool>::subscriptions(&strict_config(passthru_opt));
let config = Config {
virtualize_cpuid: false,
..strict_config(passthru_opt)
};
let subscriptions = <Detcore as Tool>::subscriptions(&config);
assert!(virtualized.has_cpuid(), "passthru_opt={passthru_opt}");
assert!(!subscriptions.has_cpuid(), "passthru_opt={passthru_opt}");
assert!(subscriptions.has_rdtsc(), "passthru_opt={passthru_opt}");
assert!(
subscriptions
.iter_syscalls()
.eq(virtualized.iter_syscalls()),
"passthru_opt={passthru_opt}: the syscall set must not depend on CPUID virtualization"
);
}
}
/// Detcore declares the serialized-execution contract only where it holds:
/// threads sequentialized, and both seccomp-filter routes intercepted and
/// refused.
#[test]
fn serialized_execution_is_declared_only_where_detcore_enforces_it() {
let enforces = <Detcore as Tool>::enforces_serialized_execution;
for passthru_opt in [false, true] {
let config = strict_config(passthru_opt);
let subscriptions = <Detcore as Tool>::subscriptions(&config);
for sysno in [Sysno::seccomp, Sysno::prctl] {
assert!(
subscriptions.iter_syscalls().any(|s| s == sysno),
"passthru_opt={passthru_opt}: {sysno} must reach its refusal"
);
}
assert!(enforces(&config), "passthru_opt={passthru_opt}");
}
assert!(!enforces(&Config {
sequentialize_threads: false,
..strict_config(false)
}));
// The refusals themselves are pinned in `syscalls::misc`'s tests.
}
#[test]
fn passthru_opt_uses_the_partial_subscription_set() {
let subscriptions = <Detcore as Tool>::subscriptions(&strict_config(true));
assert_ne!(subscriptions, Subscription::all());
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::clock_gettime)
);
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::rt_sigsuspend)
);
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::ppoll)
);
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::madvise)
);
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::arch_prctl)
);
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::writev)
);
for sysno in [
Sysno::read,
Sysno::write,
Sysno::pread64,
Sysno::pwrite64,
Sysno::readv,
Sysno::writev,
Sysno::preadv,
Sysno::preadv2,
Sysno::pwritev,
Sysno::pwritev2,
Sysno::prctl,
] {
assert!(
subscriptions
.iter_syscalls()
.any(|subscribed| subscribed == sysno),
"timer-slack mediation requires {sysno:?}"
);
}
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::pwrite64)
);
assert!(
subscriptions
.iter_syscalls()
.any(|sysno| sysno == Sysno::pidfd_open)
);
}
}
#[cfg(test)]
mod rcb_overshoot_tests {
use std::fmt::Write;
use std::sync::Arc;
use std::sync::Mutex;
use tracing::Event;
use tracing::Id;
use tracing::Level;
use tracing::Metadata;
use tracing::Subscriber;
use tracing::field::Field;
use tracing::field::Visit;
use tracing::span::Attributes;
use tracing::span::Record;
use tracing::subscriber::with_default;
use super::rcb_timer_overshot;
use super::report_rcb_overshoot;
struct ErrorSubscriber(Arc<Mutex<Option<String>>>);
struct EventVisitor(String);
impl Visit for EventVisitor {
fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) {
let _ = write!(self.0, "{}={:?}", field.name(), value);
}
}
impl Subscriber for ErrorSubscriber {
fn enabled(&self, metadata: &Metadata<'_>) -> bool {
*metadata.level() == Level::ERROR
}
fn new_span(&self, _span: &Attributes<'_>) -> Id {
Id::from_u64(1)
}
fn record(&self, _span: &Id, _values: &Record<'_>) {}
fn record_follows_from(&self, _span: &Id, _follows: &Id) {}
fn event(&self, event: &Event<'_>) {
if *event.metadata().level() == Level::ERROR {
let mut visitor = EventVisitor(String::new());
event.record(&mut visitor);
*self.0.lock().unwrap() = Some(visitor.0);
}
}
fn enter(&self, _span: &Id) {}
fn exit(&self, _span: &Id) {}
}
#[test]
fn default_overshoot_policy_emits_error_and_returns() {
let _ = reverie::take_skid_overshoot_count();
let error = Arc::new(Mutex::new(None));
with_default(ErrorSubscriber(error.clone()), || {
report_rcb_overshoot(false, 16_249, 139, 100);
});
let error = error.lock().unwrap().take().expect("missing ERROR event");
assert!(error.contains(reverie::SKID_OVERSHOOT_MARKER), "{error}");
assert!(error.contains("PMU RCB overshoot"), "{error}");
assert!(error.contains("16249"), "{error}");
assert!(error.contains("139"), "{error}");
assert!(error.contains("100"), "{error}");
assert_eq!(
reverie::take_skid_overshoot_count(),
1,
"the log-and-continue path must feed the supervisor's structural count"
);
}
#[test]
fn exact_rcb_timer_hit_is_not_an_overshoot() {
assert!(!rcb_timer_overshot(100, 100));
assert!(!rcb_timer_overshot(99, 100));
assert!(rcb_timer_overshot(101, 100));
}
#[test]
#[should_panic(expected = "PMU RCB overshoot")]
fn opt_in_overshoot_policy_panics() {
report_rcb_overshoot(true, 16_249, 139, 100);
}
}
#[cfg(test)]
mod timeslice_timer_tests {
use super::*;
/// A dropped signal's stop re-requests the timer event for the point the
/// last handler armed, whenever the host-timed stop came: before it, the
/// remaining branches; at or past it, at once
/// (https://github.com/rrnewton/hermit/issues/4304).
#[test]
fn a_kept_timer_aims_at_the_armed_point() {
assert_eq!(kept_timer_rcbs(1_000, 500, 1_000), 500);
assert_eq!(kept_timer_rcbs(1_000, 500, 1_200), 300);
assert_eq!(kept_timer_rcbs(1_000, 500, 1_499), 1);
assert_eq!(kept_timer_rcbs(1_000, 500, 1_500), 1);
assert_eq!(kept_timer_rcbs(1_000, 500, 40_000_000_000), 1);
assert_eq!(kept_timer_rcbs(u64::MAX - 10, 500, u64::MAX - 5), 5);
}
#[test]
fn manual_interrupts_can_shorten_but_not_extend_maximum() {
assert_eq!(choose_rcb_timer(100, 100, Some(150)), (50, false));
assert_eq!(choose_rcb_timer(100, 100, Some(250)), (100, true));
assert_eq!(choose_rcb_timer(100, 100, None), (100, true));
}
#[test]
fn pmu_duration_conversion_applies_clock_multiplier() {
let duration = crate::types::LogicalTime::from_nanos(100);
assert_eq!(duration.into_rcbs_with_multiplier(2.0), 5);
assert_eq!(duration.into_rcbs_with_multiplier(0.5), 20);
assert_eq!(
crate::types::LogicalTime::from_nanos(101).into_rcbs_with_multiplier(2.0),
5
);
}
#[test]
#[should_panic(expected = "max_timeslice must be at least one RCB")]
fn detcore_constructor_validates_programmatic_config() {
let config = Config {
max_timeslice: std::num::NonZeroU64::new(1),
..Default::default()
};
let _ = <Detcore as Tool>::new(Pid::from_raw(1), &config);
}
}
#[cfg(test)]
mod process_tree_guest_clock_tests {
use super::*;
#[test]
fn forked_process_shares_guest_clock_domain() {
let config = Config::default();
let tool = <Detcore as Tool>::new(Pid::from_raw(1), &config);
let mut parent = ThreadState::new(DetPid::from_raw(1), &config, ());
parent.clone_flags = Some(CloneFlags::empty());
let child = <Detcore as Tool>::init_thread_state(
&tool,
Tid::from_raw(2),
Some((Tid::from_raw(1), &parent)),
);
assert!(Arc::ptr_eq(&parent.guest_clock, &child.guest_clock));
}
}
#[cfg(test)]
mod process_files_tests {
use super::*;
fn child(
tool: &Detcore,
parent: &mut ThreadState<()>,
tid: i32,
clone_flags: CloneFlags,
) -> ThreadState<()> {
parent.clone_flags = Some(clone_flags);
let mut child = <Detcore as Tool>::init_thread_state(
tool,
Tid::from_raw(tid),
Some((Tid::from_raw(parent.dettid.as_raw()), parent)),
);
child.detpid = Some(if clone_flags.contains(CloneFlags::CLONE_THREAD) {
parent.detpid.unwrap()
} else {
DetPid::from_raw(tid)
});
child
}
/// The table a thread group's leader holds, which procfs reads for the
/// process's descriptor links, is known to every thread of the group,
/// through every way a table is made: the root's, a thread's shared or
/// own, a forked process's, a process made with `CLONE_FILES` (whose
/// table was made for another process) and its threads, and the table an
/// exec gives a thread that did not lead its process.
#[test]
fn every_thread_knows_the_table_its_process_holds() {
let config = Config::default();
let tool = <Detcore as Tool>::new(Pid::from_raw(1), &config);
let thread = CloneFlags::CLONE_VM
| CloneFlags::CLONE_FS
| CloneFlags::CLONE_FILES
| CloneFlags::CLONE_SIGHAND
| CloneFlags::CLONE_THREAD;
let mut root = ThreadState::new(DetPid::from_raw(3), &config, ());
root.detpid = Some(DetPid::from_raw(3));
assert!(root.holds_process_files());
// A thread that shares the leader's table holds it; one cloned
// without CLONE_FILES holds its own.
let shared = child(&tool, &mut root, 4, thread);
assert_eq!(shared.process_files, root.process_files);
assert!(shared.holds_process_files());
let split = child(&tool, &mut root, 5, thread - CloneFlags::CLONE_FILES);
assert_eq!(split.process_files, root.process_files);
assert!(!split.holds_process_files());
// A forked process holds its copy; its creator's group is unchanged.
let mut forked = child(&tool, &mut root, 6, CloneFlags::empty());
assert_ne!(forked.process_files, root.process_files);
assert!(forked.holds_process_files());
assert_eq!(root.process_files, Some(root.files_id()));
// A process made with CLONE_FILES shares its creator's table, made
// for that creator, and leads a group of its own: its thread holds
// the process's table although the table's creator is neither the
// thread nor its leader.
let mut shares_files = child(&tool, &mut forked, 7, CloneFlags::CLONE_FILES);
assert_eq!(shares_files.files_id(), forked.files_id());
assert_eq!(shares_files.process_files, Some(forked.files_id()));
let its_thread = child(&tool, &mut shares_files, 8, thread);
assert_ne!(its_thread.files_id().creator(), DetPid::from_raw(7));
assert!(its_thread.holds_process_files());
// A thread that did not lead its process execs, as handle_execveat
// gives it its new table: the new table is the process's, and a
// thread the new image creates holds it. A failed exec restores
// both.
let mut worker = child(&tool, &mut root, 9, thread);
let old_metadata = Arc::clone(&worker.file_metadata);
let old_process_files = worker.process_files;
let new_metadata = worker.file_metadata.lock().unwrap().for_exec(worker.dettid);
worker.file_metadata = Arc::new(Mutex::new(new_metadata));
worker.adopt_process_files();
assert_eq!(worker.files_id().creator(), DetPid::from_raw(9));
assert!(worker.holds_process_files());
assert_ne!(worker.process_files, root.process_files);
assert_eq!(root.process_files, Some(root.files_id()));
let after_exec = child(&tool, &mut worker, 10, thread);
assert!(after_exec.holds_process_files());
worker.file_metadata = old_metadata;
worker.process_files = old_process_files;
assert_eq!(worker.process_files, root.process_files);
assert!(worker.holds_process_files());
}
}
#[cfg(test)]
mod child_rng_identity_tests {
use super::*;
fn child_state(
tool: &Detcore,
parent: &mut ThreadState<()>,
host_tid: i32,
clone_flags: CloneFlags,
) -> ThreadState<()> {
parent.clone_flags = Some(clone_flags);
<Detcore as Tool>::init_thread_state(
tool,
Tid::from_raw(host_tid),
Some((Tid::from_raw(parent.dettid.as_raw()), parent)),
)
}
fn rng_sample(child: &ThreadState<()>) -> [u64; 4] {
let mut rng = child.prng.clone();
std::array::from_fn(|_| rng.random())
}
fn chaos_rng_sample(child: &ThreadState<()>) -> [u64; 4] {
let mut rng = child.chaos_prng.clone();
std::array::from_fn(|_| rng.random())
}
#[test]
fn common_child_rng_identity_ignores_backend_tid_for_every_clone_shape() {
let config = Config::default();
let tool = <Detcore as Tool>::new(Pid::from_raw(1), &config);
for (label, clone_flags) in [
("fork", CloneFlags::empty()),
("vfork", CloneFlags::CLONE_VM | CloneFlags::CLONE_VFORK),
("clone-process", CloneFlags::CLONE_VM),
(
"clone-thread",
CloneFlags::CLONE_VM | CloneFlags::CLONE_THREAD,
),
] {
let mut low_tid_parent = ThreadState::new(DetPid::from_raw(1), &config, ());
let mut high_tid_parent = ThreadState::new(DetPid::from_raw(1), &config, ());
let low_tid_child = child_state(&tool, &mut low_tid_parent, 2, clone_flags);
let high_tid_child = child_state(&tool, &mut high_tid_parent, 42_002, clone_flags);
assert_eq!(format!("{}", low_tid_child.pedigree), "C", "{label}");
assert_eq!(low_tid_child.dettid.as_raw(), 2, "{label}");
assert_eq!(high_tid_child.dettid.as_raw(), 42_002, "{label}");
assert_eq!(
rng_sample(&low_tid_child),
rng_sample(&high_tid_child),
"{label} child RNG depended on the backend Tid"
);
assert_eq!(
chaos_rng_sample(&low_tid_child),
chaos_rng_sample(&high_tid_child),
"{label} child chaos RNG depended on the backend Tid"
);
assert_ne!(
rng_sample(&low_tid_child),
chaos_rng_sample(&low_tid_child),
"{label} child guest and chaos RNG streams were coupled"
);
}
}
#[test]
fn serialized_siblings_receive_distinct_child_rng_streams() {
let config = Config::default();
let tool = <Detcore as Tool>::new(Pid::from_raw(1), &config);
let mut parent = ThreadState::new(DetPid::from_raw(1), &config, ());
let first = child_state(&tool, &mut parent, 2, CloneFlags::empty());
let first_pedigree = parent.pedigree.fork_mut();
assert_eq!(format!("{}", first.pedigree), format!("{first_pedigree}"));
let second = child_state(&tool, &mut parent, 3, CloneFlags::empty());
assert_eq!(format!("{}", second.pedigree), "PC");
assert_ne!(rng_sample(&first), rng_sample(&second));
}
}
#[cfg(test)]
mod thread_exit_identity_tests {
use super::*;
#[test]
fn initialized_thread_exit_identity_is_preserved() {
let thread_detpid = DetPid::from_raw(41);
let process_detpid = DetPid::from_raw(7);
assert_eq!(
select_thread_exit_detpid(Some(thread_detpid), process_detpid),
(thread_detpid, false)
);
}
#[test]
fn missing_thread_exit_identity_uses_process_identity() {
let process_detpid = DetPid::from_raw(7);
assert_eq!(
select_thread_exit_detpid(None, process_detpid),
(process_detpid, true)
);
}
}
#[cfg(test)]
mod completed_syscall_projection_tests {
use super::*;
/// Signal phase 1: the signal pipeline at a syscall's completion reads
/// the result in rax, rip after the two-byte syscall instruction, and the
/// instruction's clobbers; every other register is the entry's.
#[test]
fn a_completed_syscall_shows_its_result_and_return_address() {
let mut entry: libc::user_regs_struct = unsafe { std::mem::zeroed() };
entry.rip = 0x40_1000;
entry.rax = libc::SYS_pause as u64;
entry.rcx = 0x1111;
entry.r11 = 0x2222;
entry.eflags = 0x246;
entry.rdi = 7;
entry.rsp = 0x7ffd_0000;
let completed = completed_syscall_projection(entry, -i64::from(libc::EINTR));
assert_eq!(completed.rax, (-i64::from(libc::EINTR)) as u64);
assert_eq!(completed.rip, 0x40_1002);
assert_eq!(completed.rcx, 0x40_1002);
assert_eq!(completed.r11, 0x246);
assert_eq!(completed.rdi, 7);
assert_eq!(completed.rsp, 0x7ffd_0000);
assert_eq!(completed.eflags, 0x246);
}
}
#[cfg(test)]
mod thread_start_identity_tests {
use super::*;
#[test]
fn backend_process_identity_is_preserved() {
let backend_detpid = DetPid::from_raw(3);
assert_eq!(
select_thread_start_detpid(Some(backend_detpid), Pid::from_raw(42_001)),
backend_detpid
);
assert_eq!(
select_thread_start_detpid(None, Pid::from_raw(42_001)),
DetPid::from_raw(42_001)
);
}
#[test]
fn root_thread_uses_deterministic_process_identity() {
let detpid = DetPid::from_raw(3);
for past_first_execve in [false, true] {
assert!(thread_start_needs_registration(
true,
past_first_execve,
DetTid::from_raw(3),
detpid
));
for is_root_process in [false, true] {
assert!(!thread_start_needs_registration(
is_root_process,
past_first_execve,
DetTid::from_raw(4),
detpid
));
}
}
assert!(thread_start_needs_registration(
false,
false,
DetTid::from_raw(3),
detpid
));
assert!(!thread_start_needs_registration(
false,
true,
DetTid::from_raw(3),
detpid
));
}
}
#[cfg(test)]
mod thread_cpu_time_tests {
use super::*;
#[test]
fn cloned_threads_and_processes_keep_absolute_time_without_inheriting_work() {
for clone_flags in [
CloneFlags::CLONE_THREAD,
CloneFlags::empty(),
CloneFlags::CLONE_VFORK | CloneFlags::CLONE_VM,
] {
let config = Config::default();
let tool = <Detcore as Tool>::new(Pid::from_raw(1), &config);
let mut parent = ThreadState::new(DetPid::from_raw(1), &config, ());
parent.thread_logical_time.add_rcbs(7);
parent.thread_logical_time.add_syscall_with_cost(101);
parent.clone_flags = Some(clone_flags);
let child = <Detcore as Tool>::init_thread_state(
&tool,
Tid::from_raw(2),
Some((Tid::from_raw(1), &parent)),
);
assert_eq!(
child.thread_logical_time.as_nanos(),
parent.thread_logical_time.as_nanos()
);
assert_eq!(
child.thread_logical_time.inherited_nanos(),
LogicalTime::from_nanos(171)
);
let mut global = GlobalTime::new(&config);
let epoch = global.as_nanos();
for thread in [&parent, &child] {
global.update_global_time(
thread.dettid,
thread.thread_logical_time.as_nanos(),
thread.thread_logical_time.inherited_nanos(),
);
}
assert_eq!(global.as_nanos(), epoch + LogicalTime::from_nanos(171));
}
}
#[test]
fn cloned_thread_and_fork_child_start_with_zero_thread_cpu() {
for clone_flags in [CloneFlags::CLONE_THREAD, CloneFlags::empty()] {
let config = Config::default();
let tool = <Detcore as Tool>::new(Pid::from_raw(1), &config);
let mut parent = ThreadState::new(DetPid::from_raw(1), &config, ());
// Give the parent nonzero CPU before the child exists. The child's
// absolute logical clock inherits this position for scheduler
// ordering, but Linux per-thread CPU accounting must not.
parent.thread_logical_time.add_rcbs(200);
parent.thread_logical_time.add_syscall();
parent.clone_flags = Some(clone_flags);
let mut child = <Detcore as Tool>::init_thread_state(
&tool,
Tid::from_raw(2),
Some((Tid::from_raw(1), &parent)),
);
assert_eq!(
child.thread_cpu_time(),
(LogicalTime::ZERO, LogicalTime::ZERO),
"clone flags {clone_flags:?} inherited pre-creation CPU"
);
child.thread_logical_time.add_rcbs(4);
child.thread_logical_time.add_syscall();
let (user, system) = child.thread_cpu_time();
assert!(user > LogicalTime::ZERO);
assert!(system > LogicalTime::ZERO);
assert!(user < parent.thread_logical_time.user_cpu_time());
assert!(system <= parent.thread_logical_time.system_cpu_time());
}
}
}
/// Regression tests for <https://github.com/rrnewton/hermit/issues/3153>: a
/// failed syscall must not render an output buffer the kernel never wrote.
///
/// Each buffer is pre-filled with sentinel values standing in for the
/// uninitialized guest stack the issue observed, and the rendering is checked
/// through the same `finish syscall` format the DETLOG line uses.
#[cfg(test)]
mod finished_syscall_display_tests {
use std::ffi::CString;
use reverie::syscalls::AddrMut;
use reverie::syscalls::AtFlags;
use reverie::syscalls::ClockGettime;
use reverie::syscalls::ClockId;
use reverie::syscalls::Gettimeofday;
use reverie::syscalls::LocalMemory;
use reverie::syscalls::Newfstatat;
use reverie::syscalls::PathPtr;
use reverie::syscalls::StatPtr;
use reverie::syscalls::Statx;
use reverie::syscalls::StatxMask;
use reverie::syscalls::StatxPtr;
use reverie::syscalls::Timespec;
use reverie::syscalls::TimespecMutPtr;
use reverie::syscalls::Timeval;
use reverie::syscalls::TimevalMutPtr;
use super::*;
/// A value no real `st_size` or timestamp in these tests can take.
const SENTINEL: i64 = 0x5EED_0BAD_F00D;
/// Render exactly what the `finish syscall` DETLOG line renders.
fn finish_line(syscall: &Syscall, result: Result<i64, Error>) -> String {
let memory = LocalMemory::new();
format!(
"{} = {:?}",
display_syscall_finished(syscall, &memory, &result),
result
)
}
fn sentinel_stat() -> libc::stat {
// SAFETY: all-zero is a valid `struct stat`.
let mut stat: libc::stat = unsafe { std::mem::zeroed() };
stat.st_mode = libc::S_IFREG | 0o644;
stat.st_size = SENTINEL;
stat
}
fn sentinel_statx() -> libc::statx {
// SAFETY: all-zero is a valid `struct statx`.
let mut statx: libc::statx = unsafe { std::mem::zeroed() };
statx.stx_mode = (libc::S_IFREG | 0o644) as u16;
statx.stx_size = SENTINEL as u64;
statx
}
fn newfstatat(path: &CString, stat: &libc::stat) -> Syscall {
Syscall::Newfstatat(
Newfstatat::new()
.with_dirfd(libc::AT_FDCWD)
.with_path(PathPtr::from_ptr(path.as_ptr()))
.with_stat(StatPtr::from_ptr(stat as *const libc::stat))
.with_flags(AtFlags::empty()),
)
}
fn statx(path: &CString, statx: &libc::statx) -> Syscall {
Syscall::Statx(
Statx::new()
.with_dirfd(libc::AT_FDCWD)
.with_path(PathPtr::from_ptr(path.as_ptr()))
.with_flags(AtFlags::empty())
.with_mask(StatxMask::STATX_BASIC_STATS)
.with_statx(StatxPtr::from_ptr(statx as *const libc::statx)),
)
}
fn assert_no_struct_rendered(line: &str) {
assert!(!line.contains("st_mode"), "rendered st_mode: {line}");
assert!(!line.contains("st_size"), "rendered st_size: {line}");
assert!(
!line.contains(&SENTINEL.to_string()),
"rendered the unwritten sentinel: {line}"
);
}
#[test]
fn failed_newfstatat_renders_pointer_and_errno_but_not_the_buffer() {
let path = CString::new("/nonexistent/issue-3153").unwrap();
let stat = sentinel_stat();
let line = finish_line(&newfstatat(&path, &stat), Err(Errno::ENOENT.into()));
assert_no_struct_rendered(&line);
assert!(
line.contains(&format!("{:p}", &stat as *const libc::stat)),
"stat pointer missing: {line}"
);
assert!(
line.contains("\"/nonexistent/issue-3153\""),
"path missing: {line}"
);
assert!(line.contains("ENOENT"), "errno missing: {line}");
}
#[test]
fn successful_newfstatat_still_renders_the_buffer() {
let path = CString::new("/dev/null").unwrap();
let stat = sentinel_stat();
let line = finish_line(&newfstatat(&path, &stat), Ok(0));
assert!(
line.contains(&format!(
"{:p} -> {{st_mode=SFlag(S_IFREG) | 0644, st_size={}, ...}}",
&stat as *const libc::stat, SENTINEL
)),
"stat struct missing: {line}"
);
}
#[test]
fn failed_statx_renders_pointer_and_errno_but_not_the_buffer() {
let path = CString::new("/nonexistent/issue-3153").unwrap();
let buf = sentinel_statx();
let line = finish_line(&statx(&path, &buf), Err(Errno::ENOENT.into()));
assert_no_struct_rendered(&line);
assert!(
line.contains(&format!("{:p}", &buf as *const libc::statx)),
"statx pointer missing: {line}"
);
assert!(line.contains("ENOENT"), "errno missing: {line}");
}
#[test]
fn successful_statx_still_renders_the_buffer() {
let path = CString::new("/dev/null").unwrap();
let buf = sentinel_statx();
let line = finish_line(&statx(&path, &buf), Ok(0));
assert!(
line.contains(&format!(
"{:p} -> {{st_mode=SFlag(S_IFREG) | 0644, st_size={}, ...}}",
&buf as *const libc::statx, SENTINEL
)),
"statx struct missing: {line}"
);
}
#[test]
fn failed_clock_gettime_does_not_render_the_timespec() {
let tp = Timespec {
tv_sec: SENTINEL,
tv_nsec: 0,
};
let call = Syscall::ClockGettime(
ClockGettime::new()
.with_clockid(ClockId::CLOCK_MONOTONIC)
.with_tp(Some(TimespecMutPtr(
AddrMut::from_ptr(&tp as *const Timespec).unwrap(),
))),
);
let failed = finish_line(&call, Err(Errno::EINVAL.into()));
assert!(!failed.contains("tv_sec"), "rendered tv_sec: {failed}");
assert!(
failed.contains(&format!("{:p}", &tp as *const Timespec)),
"tp pointer missing: {failed}"
);
assert!(failed.contains("EINVAL"), "errno missing: {failed}");
let succeeded = finish_line(&call, Ok(0));
assert!(
succeeded.contains(&format!("tv_sec: {SENTINEL}")),
"timespec missing on success: {succeeded}"
);
}
/// EFAULT is also the error of a copy-out that faulted part way, after
/// `copy_to_user()` stored a prefix the guest can read, so it must keep
/// the buffer rendered for every allowlisted syscall.
#[test]
fn efault_failures_still_render_a_possible_partial_copy() {
let path = CString::new("/dev/null").unwrap();
let stat = sentinel_stat();
let line = finish_line(&newfstatat(&path, &stat), Err(Errno::EFAULT.into()));
assert!(
line.contains(&format!(
"{:p} -> {{st_mode=SFlag(S_IFREG) | 0644, st_size={}, ...}}",
&stat as *const libc::stat, SENTINEL
)),
"partial stat copy hidden on EFAULT: {line}"
);
assert!(line.contains("EFAULT"), "errno missing: {line}");
let buf = sentinel_statx();
let line = finish_line(&statx(&path, &buf), Err(Errno::EFAULT.into()));
assert!(
line.contains(&format!(
"{:p} -> {{st_mode=SFlag(S_IFREG) | 0644, st_size={}, ...}}",
&buf as *const libc::statx, SENTINEL
)),
"partial statx copy hidden on EFAULT: {line}"
);
let tp = Timespec {
tv_sec: SENTINEL,
tv_nsec: 0,
};
let call = Syscall::ClockGettime(
ClockGettime::new()
.with_clockid(ClockId::CLOCK_MONOTONIC)
.with_tp(Some(TimespecMutPtr(
AddrMut::from_ptr(&tp as *const Timespec).unwrap(),
))),
);
let line = finish_line(&call, Err(Errno::EFAULT.into()));
assert!(
line.contains(&format!("tv_sec: {SENTINEL}")),
"partial timespec copy hidden on EFAULT: {line}"
);
}
/// A posthook anchor fires on a value or a final guest errno, `EINTR`
/// included; an internal restart code is refused; a tool error and an
/// entry with no posthook anchor make no request
/// (https://github.com/rrnewton/hermit/issues/3929).
#[test]
fn a_posthook_checkpoint_fires_only_on_a_final_result() {
let anchors = || vec!["p".to_owned()];
let fires = |result: Result<i64, Error>| {
matches!(
posthook_checkpoint(9, anchors(), &result),
Some(ResourceID::HappensBeforeSyscallCheckpoint { count: 9, anchors })
if anchors == ["p"]
)
};
assert!(fires(Ok(0)));
assert!(fires(Ok(6)));
for errno in [Errno::EINTR, Errno::EAGAIN, Errno::EBADF, Errno::EFAULT] {
assert!(fires(Err(errno.into())), "{errno}");
}
for errno in [
Errno::ERESTARTSYS,
Errno::ERESTARTNOINTR,
Errno::ERESTARTNOHAND,
Errno::ERESTART_RESTARTBLOCK,
] {
match posthook_checkpoint(9, anchors(), &Err(errno.into())) {
Some(ResourceID::HappensBeforePosthookInterrupted {
count: 9,
anchors,
code,
}) => {
assert_eq!(anchors, ["p"]);
assert_eq!(Some(code.as_str()), errno.name());
}
other => panic!("{errno}: {other:?}"),
}
}
assert!(
posthook_checkpoint(9, anchors(), &Err(Error::Tool(anyhow::anyhow!("tool")))).is_none()
);
assert!(posthook_checkpoint(9, Vec::new(), &Ok(0)).is_none());
assert!(posthook_checkpoint(9, Vec::new(), &Err(Errno::ERESTARTSYS.into())).is_none());
}
/// A tool error is not the syscall's result and proves nothing about the
/// buffer, so only a guest errno other than EFAULT hides it.
#[test]
fn only_non_efault_guest_errnos_prove_the_buffer_unwritten() {
for errno in [Errno::ENOENT, Errno::EINVAL, Errno::EBADF, Errno::ENOMEM] {
assert!(failure_proves_outputs_unwritten(&Err(errno.into())));
}
assert!(!failure_proves_outputs_unwritten(
&Err(Errno::EFAULT.into())
));
assert!(!failure_proves_outputs_unwritten(&Ok(0)));
assert!(!failure_proves_outputs_unwritten(&Err(Error::Tool(
anyhow::anyhow!("tool failure")
))));
let path = CString::new("/dev/null").unwrap();
let stat = sentinel_stat();
let line = finish_line(
&newfstatat(&path, &stat),
Err(Error::Tool(anyhow::anyhow!("tool failure"))),
);
assert!(
line.contains(&format!("st_size={SENTINEL}")),
"buffer hidden on a tool error: {line}"
);
}
/// `gettimeofday` stores `tv` before it can fail with `EFAULT` on a bad
/// `tz`, so a failed call can carry a kernel-written output. It must stay
/// rendered: suppressing it would hide real evidence from DETLOG.
#[test]
fn failed_gettimeofday_still_renders_the_timeval() {
let tv = Timeval {
tv_sec: SENTINEL,
tv_usec: 0,
};
let call = Syscall::Gettimeofday(
Gettimeofday::new()
.with_tv(Some(TimevalMutPtr(
AddrMut::from_ptr(&tv as *const Timeval).unwrap(),
)))
.with_tz(None),
);
let line = finish_line(&call, Err(Errno::EFAULT.into()));
assert!(
line.contains(&format!("tv_sec: {SENTINEL}")),
"kernel-written timeval hidden on failure: {line}"
);
assert!(line.contains("EFAULT"), "errno missing: {line}");
}
/// `fstat` never renders its buffer (T136880615); the result must not
/// change that in either direction.
#[test]
fn fstat_never_renders_the_buffer() {
let stat = sentinel_stat();
let call = Syscall::Fstat(
reverie::syscalls::Fstat::new()
.with_fd(3)
.with_stat(StatPtr::from_ptr(&stat as *const libc::stat)),
);
for result in [Ok(0), Err(Errno::EBADF.into())] {
assert_no_struct_rendered(&finish_line(&call, result));
}
}
}