hermit-run 0.5.0

Hermit: a deterministic Linux execution engine for reproducible runs, record and replay, and concurrency-race search.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
/*
 * 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.
 */

// Treat all Clippy warnings as errors.
#![doc = include_str!("../README.md")]
#![deny(clippy::all)]
#![allow(clippy::uninlined_format_args)]

// This host library retains LiteInst code without becoming an in-guest Tool
// image. Declare its historical backing explicitly and keep the host allocator;
// LiteInst's allocation preflight still refuses non-Tool allocations.
#[cfg(feature = "liteinst")]
reverie_liteinst::declare_legacy_tool_backing!();

mod backend_stats;
pub mod build_info;
pub mod canonical_verdict;
mod chroot;
use hermit_record_format::consts;
pub mod logdiff_report;
// TODO-HUMAN-REVIEW(PR-594): Review the public e9patch preprocessing API.
pub mod cpu_profile_admission;
pub mod e9patch;
mod error;
use hermit_record_format::event;
use hermit_record_format::event_stream;
pub mod happens_before;
pub mod host_input_change;
mod id;
#[cfg(feature = "liteinst")]
pub mod in_guest_exits;
pub mod instruction_map;
#[cfg(feature = "liteinst")]
use hermit_record_format::interp;
pub mod liteinst_bootstrap;
mod liteinst_constructor;
pub mod liteinst_record;
mod metadata;
pub mod xstate_probe;
/// Diagnostic writes that never wait for a reader; they live in detcore, whose
/// own stderr writer reads the same flag.
pub use detcore::nonwaiting_write;
pub mod physical_exit_watch;
pub mod preemption_admission;
pub mod proc_mount;
mod ptrace_completion;

pub use canonical_verdict::Verdict;

/// Whether record/replay verification hashes syscall output buffers, and
/// therefore whether its log comparison can see buffer CONTENT at all.
///
/// ONE CONSTANT BECAUSE TWO INDEPENDENT SWITCHES HAD TO AGREE AND SILENTLY DID
/// NOT MATTER. Record/replay carried this decision twice: `metadata.rs`'s
/// `record_or_replay_config` decided whether the recorder EMITS the hash, and
/// `record_start.rs`'s `ComparisonOptions` decided whether the comparator READS
/// it. Both were hard-coded `false`, neither was reachable from the command
/// line, and nothing related them. Setting either alone achieves nothing: hash
/// without compare is dead weight, compare without hash finds nothing. Naming
/// the decision once makes the pair impossible to desynchronize, and makes
/// changing it a one-line edit in a place a reader can find.
pub const RECORD_REPLAY_HASHES_IO_BUFFERS: bool = true;

/// Whether record/replay virtualizes time. FALSE, DELIBERATELY, AND THAT IS WHY
/// A GREEN RECORD/REPLAY VERDICT IS NOT A DETERMINISM RESULT.
///
/// Named for the same reason as [`RECORD_REPLAY_HASHES_IO_BUFFERS`] directly
/// above: the decision was about to be carried twice. `metadata.rs`'s
/// `record_or_replay_config` sets the config the recorder and replayer run
/// under, and `record_start.rs`'s `ComparisonOptions` now discloses that setting
/// in the verdict. If those two disagreed, the report would describe a time
/// policy the run did not use — which is precisely the class of defect the
/// disclosure exists to close, reintroduced one layer up.
///
/// `hermit run --verify` reads its value from the live `det_config` instead,
/// because `--no-virtualize-time` makes it a genuine runtime choice there. Only
/// the record/replay path is a fixed decision, so only it gets a constant.
pub const RECORD_REPLAY_VIRTUALIZES_TIME: bool = false;

/// Exit status for a completed comparison that found a divergence.
///
/// This is the conventional `diff` result: zero means equal and one means
/// different. The verification JSON distinguishes it from a guest that exits
/// one of its own accord.
pub const HERMIT_VERIFICATION_DIVERGENCE_EXIT: i32 = 1;

/// Exit status for a failure of HERMIT ITSELF, as distinct from the guest's.
///
/// ⚠️ EVERY CLI ERROR USED TO BE `Exited(1)`, WHICH IS THE SINGLE MOST COMMON
/// GUEST FAILURE CODE. A tracer panic and a guest that exited 1 of its own
/// accord were therefore INDISTINGUISHABLE from `$?` alone — and every harness,
/// gate and script on this project decides pass/fail from exactly that value.
/// The information was not missing, only discarded: reverie's container reports
/// the child's real status as a typed `ExitStatus`, and hermit collapsed it to
/// prose and then to `1`.
///
/// WHY THIS DOES NOT BREAK THE "hermit's exit IS the guest's exit" CONTRACT.
/// That contract describes the case where the guest RAN AND EXITED, and it is
/// untouched: a guest status still flows through `raise_or_exit` unchanged, and
/// only the `Err` arm — where there is NO guest exit code, because hermit failed
/// before or instead of producing one — uses this value. The contract did not
/// cover that case; it does now.
///
/// WHY 125. It is the established convention for "the wrapper tool itself
/// failed" — GNU `env`, `chroot` and `timeout` all reserve 125 for exactly this,
/// leaving 126/127 for exec-level problems. It is not a value hermit emits
/// anywhere else.
///
/// ⚠️ THIS REDUCES A COLLISION, IT DOES NOT REMOVE ONE. A guest is free to exit
/// 125 deliberately, and every value in 0..=255 is a legal guest status, so no
/// code can be reserved outright. What it removes is the GUARANTEED collision
/// with the most common failure code. For an unambiguous signal, read the
/// `HERMIT_TASK_PANIC` marker on stderr or the verification JSON; this makes the
/// cheap check — `$?` — stop actively lying.
///
/// ⚠️ IT LIVES IN THE LIBRARY, NOT IN `main.rs`, AND THAT PLACEMENT IS THE FIX
/// FOR HOW IT WENT STALE. It was a private `const` inside the binary, so
/// `tests/cli.rs` — the only thing that asserts on it — could not name it and
/// wrote the number out by hand instead. When hermit#2558 moved the value from
/// `1` to `125`, the definition moved and the sixteen copies did not: eight cli
/// tests went red on main and stayed red, each failing an assertion about the
/// exit code rather than about the behaviour it exists to check. Same shape as
/// [`RECORD_REPLAY_HASHES_IO_BUFFERS`] above — a decision carried in two places
/// that had to agree and had no way to. Exported here, the tests move with it.
///
/// ⚠️ DO NOT FLIP EVERY `Some(1)` YOU FIND. `tests/stress_suite.rs`
/// asserts `Some(1)` as THE GUEST'S OWN SIGNAL, not as a stale copy of this
/// constant: there `Some(1) => GuestOutcome::Exposed`, and
/// `status.code() == Some(1)` IS the `exposes_bug` predicate. It is
/// pattern-identical to the tests that DID go stale, and it is the OPPOSITE
/// case -- the assertion is CORRECT and must not move with this constant.
///
/// ⚠️ ITS FAILURE MODE IS SILENT SUCCESS. Changing it to 125 would not
/// turn the stress suite red; it would make `exposes_bug` NEVER FIRE, so the
/// suite would report no bug exposed forever and pass while blind. A red is
/// recoverable; a green that cannot fail is not.
///
/// REVIEWER RULE: a change to `tests/stress_suite.rs` inside an exit-code head
/// SHOULD BE REFUSED ON SIGHT AND QUESTIONED.
pub const HERMIT_INTERNAL_FAILURE_EXIT: i32 = 125;

/// A deadline hermit itself was asked to enforce expired: **124**.
///
/// The status the container init exits with when a hermit-owned bound ends the
/// run -- `run --timeout`'s SIGALRM fallback here, and `record --record-timeout`'s
/// handler in `record_start.rs`, which has always used this value. 124 is the
/// established code for "a deadline fired" across GNU `timeout`, `safehermit`'s
/// wall bound and both hermit spellings, so this names an existing convention
/// rather than claiming a new number.
///
/// ⚠️ IT MUST BE READ AT THE CONTAINER BOUNDARY OR IT IS SILENTLY REWRITTEN TO
/// 125. Found only by forcing the fallback to fire, 2026-08-26: the init exited
/// 124, `classify_container_result` had no arm for it, and it fell through to
/// `ContainerChildExit` -- so the run reported `exit 125` and
/// `class=container-child-exit`, "the child died with a status it did not
/// pick", for a deadline hermit chose to enforce. Identical in shape to the
/// refusal that arrives as 122 and needs its own arm for the same reason.
pub const HERMIT_DEADLINE_EXIT: i32 = 124;

/// Hermit's own log output exceeded `--max-log-bytes`: **123**.
///
/// The status hermit exits with when the byte budget on its tracing output
/// (stderr, or `--log-file`) is exhausted and it deliberately tears the run
/// down. 123 was already reserved below for "a LOG BYTE CAP fired" on behalf of
/// dev-hermit's `bin/safehermit`; this is the same event enforced one layer
/// lower, exactly as 124 is shared by hermit's `--timeout` and safehermit's wall
/// deadline. It is not 125 because the cap working is not hermit breaking, and
/// not 122 because no fail-closed determinism policy was involved: the remedy
/// is "lower the log level or raise the cap", not "change the program".
///
/// Like 124, it must be read at the container boundary
/// (`classify_container_result`) or it is rewritten to 125.
pub const HERMIT_LOG_CAP_EXIT: i32 = 123;

/// The guest program could not be found at all: **127**.
///
/// Exported for the same reason as [`HERMIT_INTERNAL_FAILURE_EXIT`] and with the
/// same history one step behind it. `GuestProgramFault::exit_code` wrote `127`
/// and `126` as bare literals, and `tests/cli.rs` had no way to name either, so
/// a test meaning "not found" and a test meaning "hermit refused" were asserted
/// with the same helper and the same number until the values diverged.
///
/// ⚠️ THIS IS NOT A SUBDIVISION OF HERMIT'S OWN FAILURE, AND THAT IS THE WHOLE
/// DISTINCTION. 125 says *hermit* broke; 127 and 126 say *the guest program* was
/// unusable and hermit correctly declined to start it. A caller that cannot tell
/// those apart cannot tell "fix my tooling" from "fix my command line".
pub const GUEST_PROGRAM_NOT_FOUND_EXIT: i32 = 127;

/// The guest program exists but cannot be executed as given: **126**.
///
/// See [`GUEST_PROGRAM_NOT_FOUND_EXIT`]. 127/126 are the GNU `env`/`chroot`/
/// `timeout` convention that 125 itself came from, so the three sit in one
/// scheme rather than being three unrelated numbers.
pub const GUEST_PROGRAM_NOT_EXECUTABLE_EXIT: i32 = 126;

// ⚠️ THE VALUES THEMSELVES ARE PINNED, NOT ONLY THEIR NAMES.
//
// Naming a constant makes every consumer agree with the definition; it does not
// make the definition right. Once `tests/cli.rs` asserts `Some(THE_CONSTANT)`
// everywhere, the whole suite is self-consistent and completely unanchored -- a
// one-character edit here would move all sixteen assertions with it and nothing
// would fail. That is the exact shape of the defect this scheme exists to close,
// displaced one level up, and it is why these are compile-time assertions rather
// than a comment: a deliberate edit "would be noticed" is the argument that
// already failed once, when hermit#2558 moved 1 -> 125 and sixteen copies went
// stale.
//
// WHY EACH VALUE AND NOT ANOTHER. GNU `env`, `chroot` and `timeout` reserve 125
// for "the wrapper itself failed", 127 for "command not found" and 126 for
// "found but not executable". Borrowing the whole scheme rather than one number
// is what keeps the three answers distinguishable from each other.
//
// ⚠️ THE EXIT-CODE ALLOCATION FOR THE `hermit` BINARY. ONE TABLE, HERE.
//
// Five separate exit-code defects were found in one evening and every one of
// them came from a value being chosen, copied or rewritten somewhere that could
// not see the others. This is the single place the space is allocated; anything
// that emits or rewrites a hermit exit status is a consumer OF this table and
// must not invent its own reading of it.
//
//   0          success.
//   1          HERMIT_VERIFICATION_DIVERGENCE_EXIT, matching `diff`; also a
//              legal guest status. Read the verification JSON to distinguish.
//   2 ..= 121  THE GUEST'S OWN STATUS, passed through untouched. (The range
//              stops at 121 because 122 is reserved below; a guest may still
//              CHOOSE any value, see the caveat at the end.)
//   122        HERMIT_POLICY_REFUSAL_EXIT -- hermit REFUSED the run under a
//              fail-closed policy. Hermit WORKED; the run was stopped on
//              purpose and the reason was printed. Distinct from 125 because
//              "hermit refused" and "hermit broke" demand opposite responses:
//              read the refusal and change your program or flags, versus file a
//              bug. Defined in `detcore-model` because `detcore` emits it and
//              this crate recognises it, and that is the only crate both
//              depend on.
//   123        HERMIT_LOG_CAP_EXIT -- a LOG BYTE CAP fired. Hermit's own
//              `--max-log-bytes` budget on its tracing output, and the value
//              reserved for dev-hermit's `bin/safehermit` log-cap kill (which,
//              as of 2026-09-26, still exits 125 -- see the 125 note below).
//              Hermit's spelling prints `HERMIT_LOG_CAP class=log-cap`.
//   124        DO NOT USE. GNU `timeout`'s deadline, and dev-hermit's
//              `bin/safehermit` WALL DEADLINE kill. `tests/cli.rs` asserts
//              `assert_ne!(code, Some(124))` on the awk-mincore probe.
//   125        HERMIT_INTERNAL_FAILURE_EXIT -- hermit itself failed, no guest
//              ran. NOT YET THE SOLE MEANING through dev-hermit's
//              `bin/safehermit`: that wrapper has exited 125 for its
//              log-byte-cap kill since it was created on 2026-08-20 and still
//              did on 2026-10-05, so a run through it produces one number for
//              two faults with opposite remedies (see "125 ⚠️ NOT FREE" below).
//              An earlier version of this table said safehermit had already
//              moved off 125; it never did. The intended fix is that SAFEHERMIT
//              MOVES -- to the 123 reserved for it above -- NOT HERMIT, because
//              that wrapper is the only layer that knows which happened (it
//              relays hermit's status otherwise) and because 125/126/127 is one
//              GNU scheme that cannot be broken up. Tracked as
//              `hermit_s_125_collides`.
//   126        GUEST_PROGRAM_NOT_EXECUTABLE_EXIT.
//   127        GUEST_PROGRAM_NOT_FOUND_EXIT -- the ABSOLUTE-PATH form. The
//              bare-name-on-guest-PATH form currently exits 125 instead, which
//              is an inconsistency tracked as `hermit_reports_a_missing`, not a
//              second meaning for 127.
//   128 + N    killed by signal N (shell convention). Hermit does not emit these
//              deliberately; a wrapper reporting a signal death must use them
//              rather than borrow a code that already means something.
//
// ⚠️ NO CODE IS EXCLUSIVELY HERMIT'S. Every value in 0..=255 is a legal guest
// status, so a guest may return 125 or 127 of its own accord and this table
// cannot stop it. The table removes GUARANTEED collisions, not possible ones.
// The only unambiguous discriminator is the `HERMIT_INTERNAL_FAILURE` marker on
// stderr, present exactly when hermit itself failed. Any consumer deciding on
// `$?` alone is guessing, and should say so.
//
// ⚠️ THE SAME ARGUMENT APPLIES TO 124, AND MORE SHARPLY, BECAUSE FIVE
// MECHANISMS PRODUCE IT. `hermit run --timeout`, its unwind fallback, `hermit
// record --record-timeout`, the `timeout(1)` wrapped around a manifest cell, and
// `safehermit`'s wall deadline all exit 124, and GNU `timeout` uses it for the
// same event. They demand different responses -- a slow guest, a wedged
// teardown inside hermit, a cell bound set too low, a run that escaped every
// inner bound -- so `$?` cannot route the reader to any of them.
//
// The discriminator is again the marker on stderr, and it is a CONTRACT rather
// than a diagnostic nicety: `class=run-timeout` for hermit's own bound (which
// also states the bound in seconds), `HERMIT_RUN_TIMEOUT_FALLBACK` for the
// unwind failing to complete, `safehermit: bound.wall=` for the cgroup reap.
// EXIT 124 WITH NO MARKER AT ALL MEANS NO INNER BOUND FIRED -- something outside
// hermit killed the run -- which is a configuration error and not a slow guest.
// The full picture, including which rung bounds which quantity and the
// strict-inequality invariant between them, is docs/TIMEOUT_LADDER.md.
//
// ⚠️ DO NOT CLAMP INTO THIS RANGE. `scripts/hermit-code-coverage.rs` used
// `clamp(1, 125)` and so rewrote 127 and 126 into 125 -- turning "your program is
// missing" into "hermit broke" and pointing the reader at the wrong project.
// Truncating a status does not lose information here, it MANUFACTURES a false
// attribution, because the ceiling is itself a meaning.
//
// ⚠️ WHAT EACH VALUE MUST NOT COLLIDE WITH -- AND 125 ALREADY DOES.
//
//   0    success. Never a failure code; see the nonzero pin below.
//   1    the commonest guest exit status. Sharing it is the collision
//        hermit#2558 introduced 125 to escape, so 125 must never drift back.
//   124  GNU `timeout`'s "deadline fired", and dev-hermit's `bin/safehermit`
//        uses it for its WALL DEADLINE kill. `tests/cli.rs` asserts
//        `assert_ne!(code, Some(124))` on the awk-mincore probe for that reason.
//   126  hermit's own GUEST_PROGRAM_NOT_EXECUTABLE_EXIT.
//   127  hermit's own GUEST_PROGRAM_NOT_FOUND_EXIT -- already in use for the
//        ABSOLUTE-PATH form of a missing guest program. Note the bare-name form
//        resolved against the guest PATH exits 125 instead, which is an
//        inconsistency filed separately, not a licence to reuse 127.
//
//   125  ⚠️ NOT FREE. dev-hermit's `bin/safehermit` exits 125 to mean "the LOG
//        BYTE CAP fired: safehermit killed the run through its cgroup"
//        (`demos/lib/demo_common.py`'s SAFEHERMIT_EXIT_REASON). Any hermit run
//        launched through that wrapper -- which is how the demos and the repeat
//        harness run it -- can therefore produce 125 for two unrelated reasons:
//        hermit refused, or the wrapper killed it for log volume. They demand
//        opposite responses (fix the invocation vs raise the cap), and `$?`
//        cannot tell them apart. This is a LIVE ambiguity, not a hypothetical:
//        demo 5 has been observed reporting `exited with status 125` when the
//        cap fired. It is recorded here rather than resolved because changing
//        either value is a cross-repository decision.
//
//        The discriminator meanwhile is the same one that separates hermit's 125
//        from a guest's own 125: the `HERMIT_INTERNAL_FAILURE` marker on stderr,
//        present only when hermit itself failed. safehermit writes its verdict
//        to its `--sh-report` file instead. Read one of those, never the number
//        alone.
//
// ⚠️ 0 IS THE DANGEROUS DRIFT, NOT 126 OR 127. Every one of these is asserted as
// `assert_eq!(code, Some(CONST))`. At 0 those assertions would stop demanding a
// FAILURE and start demanding a SUCCESS, and would still pass -- sixteen tests
// silently inverted. `tests/cli.rs` also asserts `!status.success()` beside the
// code for that reason; this pin is the second of the two locks.
const _: () = assert!(
    HERMIT_VERIFICATION_DIVERGENCE_EXIT == 1,
    "1 is the established diff-style result for a completed comparison that diverged"
);
const _: () = assert!(
    HERMIT_INTERNAL_FAILURE_EXIT == 125,
    "125 is the GNU wrapper-failed convention; 1 is the commonest guest status and 0 is success"
);
const _: () = assert!(
    GUEST_PROGRAM_NOT_FOUND_EXIT == 127,
    "127 is the GNU command-not-found convention and must stay distinct from 125 and 126"
);
const _: () = assert!(
    GUEST_PROGRAM_NOT_EXECUTABLE_EXIT == 126,
    "126 is the GNU found-but-not-executable convention and must stay distinct from 125 and 127"
);
const _: () = assert!(
    HERMIT_LOG_CAP_EXIT == 123,
    "123 is Hermit's log-cap code, reserved for bin/safehermit's log-cap kill as well; see the allocation table"
);
const _: () = assert!(
    HERMIT_LOG_CAP_EXIT != 0
        && HERMIT_VERIFICATION_DIVERGENCE_EXIT != 0
        && HERMIT_INTERNAL_FAILURE_EXIT != 0
        && GUEST_PROGRAM_NOT_FOUND_EXIT != 0
        && GUEST_PROGRAM_NOT_EXECUTABLE_EXIT != 0,
    "a failure code of 0 would invert every assert_eq!(code, Some(CONST)) into demanding success"
);
mod record;
use hermit_record_format::record_replay_path;
mod replay;
#[cfg(feature = "sabre")]
mod sabre_bootstrap;
#[cfg(feature = "sabre")]
mod sabre_ptrace;
use hermit_record_format::script;
mod tracer_thread;

#[cfg(feature = "sabre")]
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::io::Seek;
use std::io::SeekFrom;
use std::io::Write;
#[cfg(feature = "dbt")]
use std::os::fd::AsFd;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
#[cfg(any(feature = "sabre", all(test, feature = "liteinst")))]
use std::os::unix::ffi::OsStrExt;
#[cfg(feature = "sabre")]
use std::os::unix::fs::OpenOptionsExt;
#[cfg(any(feature = "sabre", feature = "liteinst"))]
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::path::PathBuf;
#[cfg(feature = "sabre")]
use std::sync::Arc;
use std::sync::Mutex;
#[cfg(feature = "sabre")]
use std::sync::atomic::AtomicBool;
use std::time::Duration;
use std::time::Instant;

use anyhow::anyhow;
use clap::ValueEnum;
pub use consts::LAST;
pub use consts::LAST_RUN;
use consts::MANIFEST_NAME as RUN_MANIFEST_NAME;
pub use consts::MANIFEST_NAME;
use consts::METADATA_NAME;
pub use consts::METADATA_NAME as RECORDING_METADATA_NAME;
pub use detcore::Config as DetConfig;
pub use detcore::Detcore;
pub use detcore::RecordOrReplay;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_background_init_v2;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_background_init_v3;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_name;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_pre_syscall;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_ready;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_thread_exit;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_thread_init;
#[doc(hidden)]
#[cfg(feature = "dbt")]
pub use detcore_dbt::reverie_dbt_runtime_totals;
use detcore_model::bundled_signal_facts;
pub use error::Context;
pub use error::Error;
pub use error::FailureKind;
pub use error::SerializableError;
use goblin::elf::Elf;
use goblin::elf::header;
use goblin::elf::section_header;
pub use id::Id;
use metadata::Metadata;
pub use ptrace_completion::HermitCleanupStage;
pub use ptrace_completion::HermitCleanupUnconfirmed;
pub use ptrace_completion::HermitPtraceFailure;
pub use ptrace_completion::RecoveryRefusal;
use record::Record;
use replay::Replay;
pub use reverie::ExitStatus;
#[cfg(feature = "sabre")]
use reverie::GlobalTool;
pub use reverie::process;
pub use reverie::process::Command;
pub use reverie::process::Mount;
pub use reverie::process::Namespace;
pub use reverie::process::Output;
pub use reverie::process::Stdio;
pub use script::Shebang;
use serde::Deserialize;
use serde::Serialize;
pub use tracer_thread::launch_on_calling_thread;

enum KvmStdinReservation {
    Open(fs::File),
    Closed,
}

static KVM_STDIN_RESERVATION: Mutex<Option<KvmStdinReservation>> = Mutex::new(None);

/// Saves stdin captured before Rust's process startup can reuse a closed fd 0.
pub fn reserve_kvm_stdin(stdin: Option<fs::File>) -> io::Result<()> {
    let mut reservation = KVM_STDIN_RESERVATION
        .lock()
        .map_err(|_| io::Error::other("KVM stdin reservation lock is poisoned"))?;
    if reservation.is_some() {
        return Err(io::Error::new(
            io::ErrorKind::AlreadyExists,
            "KVM stdin is already reserved",
        ));
    }
    *reservation = Some(match stdin {
        Some(file) => KvmStdinReservation::Open(file),
        None => KvmStdinReservation::Closed,
    });
    Ok(())
}

fn duplicate_current_stdin() -> io::Result<Option<fs::File>> {
    // SAFETY: F_DUPFD_CLOEXEC duplicates fd 0 without taking ownership of it.
    let duplicate = unsafe { libc::fcntl(libc::STDIN_FILENO, libc::F_DUPFD_CLOEXEC, 3) };
    if duplicate >= 0 {
        // SAFETY: F_DUPFD_CLOEXEC returned a new owned descriptor.
        return Ok(Some(unsafe { fs::File::from_raw_fd(duplicate) }));
    }
    let error = io::Error::last_os_error();
    if error.raw_os_error() == Some(libc::EBADF) {
        Ok(None)
    } else {
        Err(error)
    }
}

fn ensure_kvm_stdin_reserved() -> io::Result<()> {
    let mut reservation = KVM_STDIN_RESERVATION
        .lock()
        .map_err(|_| io::Error::other("KVM stdin reservation lock is poisoned"))?;
    if reservation.is_none() {
        *reservation = Some(match duplicate_current_stdin()? {
            Some(file) => KvmStdinReservation::Open(file),
            None => KvmStdinReservation::Closed,
        });
    }
    Ok(())
}

fn reserved_kvm_stdin() -> Result<Option<fs::File>, Error> {
    ensure_kvm_stdin_reserved()?;
    let reservation = KVM_STDIN_RESERVATION
        .lock()
        .map_err(|_| io::Error::other("KVM stdin reservation lock is poisoned"))?;
    match reservation.as_ref() {
        Some(KvmStdinReservation::Open(file)) => Ok(Some(file.try_clone()?)),
        Some(KvmStdinReservation::Closed) => Ok(None),
        None => unreachable!("stdin reservation was initialized above"),
    }
}

/// A replayable snapshot of the guest's stdin for the output-capturing backends.
///
/// `hermit run --verify` executes the guest twice and compares the two runs. The
/// output-capturing backends historically fed the guest `Stdio::null()`, so any
/// data piped into hermit (`echo prog | hermit run --strict --verify -- ...`)
/// was silently dropped. Both runs then saw identical *empty* input and hermit
/// reported a false "deterministic" success even though the guest never received
/// its input. `Seekable` holds a read-only, rewindable file so both runs receive
/// the exact same bytes without being able to modify the caller's input; `None`
/// means stdin should be `/dev/null` (nothing to replay, or a terminal that
/// cannot be replayed identically to two runs).
enum StdinSnapshot {
    Seekable(fs::File),
    None,
}

static OUTPUT_STDIN_SNAPSHOT: Mutex<Option<StdinSnapshot>> = Mutex::new(None);

/// Records a rewindable snapshot of the process stdin so the output-capturing
/// backends can replay identical input to each run of `hermit run --verify`.
///
/// `stdin` is the descriptor captured before Rust startup could reuse a closed
/// fd 0 (see the binary's `startup_stdin`). A regular-file redirect is reopened
/// read-only; a pipe/fifo/socket is drained once into a seekable temporary file
/// and that file is reopened read-only. A terminal (or absent stdin) is treated
/// as `/dev/null` because a live terminal cannot be replayed identically to two
/// runs.
pub fn reserve_output_stdin_snapshot(stdin: Option<fs::File>) -> io::Result<()> {
    let snapshot = match stdin {
        None => StdinSnapshot::None,
        Some(mut file) => {
            // SAFETY: as_raw_fd borrows the descriptor without taking ownership.
            let is_tty = unsafe { libc::isatty(file.as_raw_fd()) } == 1;
            if is_tty {
                StdinSnapshot::None
            } else {
                let replay = if file.stream_position().is_ok() {
                    // Already seekable (e.g. `< file`): retain it below through
                    // a read-only descriptor with an independent file offset.
                    file
                } else {
                    // Non-seekable (pipe/fifo/socket): buffer once into a
                    // seekable temporary file so both --verify runs receive
                    // identical input.
                    //
                    // NAME THE WAIT BEFORE ENTERING IT. This `io::copy` reads to
                    // EOF, so a stdin that never reaches EOF -- an inherited socket,
                    // a fifo whose writer stays open -- blocks here FOREVER, before
                    // the guest has started. Measured: the process wedges with ZERO
                    // bytes on stdout and stderr and is only cleared by killing it,
                    // which is indistinguishable from a slow run. That
                    // indistinguishability is the whole harm; an unbounded wait that
                    // says what it is waiting for is merely slow, and a reader can
                    // act on it.
                    //
                    // Deliberately UNCONDITIONAL rather than emitted after a delay:
                    // a message that appears only once N seconds have passed makes
                    // hermit's own stderr depend on timing, and this project does
                    // not accept timing-dependent output. Deliberately NOT a
                    // deadline either -- a 12s slow producer is legitimate and
                    // delivers, and nothing can separate "slow" from "never" except
                    // by waiting. This changes no control flow: every input that
                    // worked before still works, byte for byte, on stdout.
                    const BUFFERING_NOTE: &str = "hermit: --verify is buffering stdin from a non-seekable stream so both \
                         runs receive identical input. If this appears to hang, stdin has not \
                         reached end-of-file; pass `< /dev/null` when the guest needs no input.";
                    if nonwaiting_write::diagnostics_must_not_wait() {
                        // Under --max-log-bytes, termination must not depend
                        // on this write; see `nonwaiting_write`.
                        nonwaiting_write::write_without_waiting(
                            libc::STDERR_FILENO,
                            format!("{BUFFERING_NOTE}\n").as_bytes(),
                        );
                    } else {
                        eprintln!("{BUFFERING_NOTE}");
                    }
                    let mut buffered = tempfile::tempfile()?;
                    io::copy(&mut file, &mut buffered)?;
                    buffered.seek(SeekFrom::Start(0))?;
                    buffered
                };

                // A duplicate would keep the original access mode. Reopen the
                // reserved descriptor read-only so neither verification run
                // can modify the caller's stdin or the next run's input.
                let replay_path = format!("/proc/self/fd/{}", replay.as_raw_fd());
                StdinSnapshot::Seekable(fs::File::open(replay_path)?)
            }
        }
    };
    let mut reservation = OUTPUT_STDIN_SNAPSHOT
        .lock()
        .map_err(|_| io::Error::other("output stdin snapshot lock is poisoned"))?;
    if reservation.is_some() {
        return Err(io::Error::new(
            io::ErrorKind::AlreadyExists,
            "output stdin snapshot is already reserved",
        ));
    }
    *reservation = Some(snapshot);
    Ok(())
}

/// Returns a rewound descriptor for the reserved stdin snapshot, if any.
///
/// Each call rewinds the snapshot and hands back a fresh descriptor, so
/// repeated `--verify` runs read identical input from the start. Returns `None`
/// when no snapshot was reserved or the reserved stdin is a terminal/absent
/// (nothing to replay). Runs are sequential (each executes in its own forked
/// container child), so resetting the shared open file description's offset here
/// is safe.
fn output_backend_stdin_reservation() -> Result<(bool, Option<fs::File>), Error> {
    let mut reservation = OUTPUT_STDIN_SNAPSHOT
        .lock()
        .map_err(|_| io::Error::other("output stdin snapshot lock is poisoned"))?;
    match reservation.as_mut() {
        Some(StdinSnapshot::Seekable(file)) => {
            file.seek(SeekFrom::Start(0))?;
            Ok((true, Some(file.try_clone()?)))
        }
        Some(StdinSnapshot::None) => Ok((true, None)),
        None => Ok((false, None)),
    }
}

fn output_backend_stdin_file() -> Result<Option<fs::File>, Error> {
    Ok(output_backend_stdin_reservation()?.1)
}

/// Returns the stdin to hand a guest run in an output-capturing backend.
///
/// When [`reserve_output_stdin_snapshot`] has stored a replayable snapshot the
/// file is rewound and a fresh descriptor handed to the guest, so each
/// `--verify` run reads identical input. Otherwise (no snapshot reserved, or a
/// terminal/absent stdin) the guest gets `/dev/null`, matching the previous
/// behavior for callers that do not reserve a snapshot.
pub fn output_backend_stdin() -> Result<Stdio, Error> {
    match output_backend_stdin_file()? {
        Some(file) => Ok(Stdio::from(file)),
        None => Ok(Stdio::null()),
    }
}

/// The result of recording a command.
#[derive(Debug, Serialize, Deserialize)]
pub struct Recording {
    /// The unique ID of the recording.
    pub id: Id,

    /// The exit code of the command.
    pub exit_status: ExitStatus,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CapabilityProbe {
    Namespaces,
    Ptrace,
    Seccomp,
}

fn run_capability_probe(probe: CapabilityProbe) -> Result<bool, Error> {
    // SAFETY: The child calls only async-signal-safe syscalls and exits immediately.
    let pid = unsafe { libc::fork() };
    if pid == -1 {
        return Err(std::io::Error::last_os_error()).context("Failed to fork capability probe");
    }
    if pid == 0 {
        let supported = match probe {
            CapabilityProbe::Namespaces => unsafe {
                libc::unshare(libc::CLONE_NEWUSER | libc::CLONE_NEWPID) == 0
            },
            CapabilityProbe::Ptrace => {
                // SAFETY: PTRACE_TRACEME ignores the pid and address arguments.
                unsafe {
                    libc::ptrace(
                        libc::PTRACE_TRACEME,
                        0,
                        std::ptr::null_mut::<libc::c_void>(),
                        std::ptr::null_mut::<libc::c_void>(),
                    ) != -1
                }
            }
            CapabilityProbe::Seccomp => {
                let mut filter = libc::sock_filter {
                    code: 0x06, // BPF_RET | BPF_K
                    jt: 0,
                    jf: 0,
                    k: 0x7fff0000, // SECCOMP_RET_ALLOW
                };
                let program = libc::sock_fprog {
                    len: 1,
                    filter: &mut filter,
                };
                // SAFETY: The filter is an allow-all program with a valid one-element lifetime.
                unsafe {
                    libc::prctl(libc::PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == 0
                        && libc::syscall(
                            libc::SYS_seccomp,
                            1, // SECCOMP_SET_MODE_FILTER
                            0,
                            &program,
                        ) == 0
                }
            }
        };
        // SAFETY: Avoid running Rust destructors after fork.
        unsafe { libc::_exit(i32::from(!supported)) }
    }

    let mut status = 0;
    loop {
        // SAFETY: pid is the child created above and status points to valid storage.
        let result = unsafe { libc::waitpid(pid, &mut status, 0) };
        if result == pid {
            return Ok(libc::WIFEXITED(status) && libc::WEXITSTATUS(status) == 0);
        }
        if result == -1 {
            let error = std::io::Error::last_os_error();
            if error.kind() == std::io::ErrorKind::Interrupted {
                continue;
            }
            return Err(error).context("Failed to wait for capability probe");
        }
    }
}

/// Whether a run creates Hermit's user and PID namespaces. A `--no-namespace`
/// run starts the guest as a plain child and never calls `unshare`, so the
/// namespace probe says nothing about whether it can run
/// (<https://github.com/rrnewton/hermit/issues/4515>).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NamespaceUse {
    /// The run unshares its user and PID namespaces (the default).
    Creates,
    /// The run creates no namespace (`--no-namespace`).
    None,
}

fn validate_tracing_environment(namespaces: NamespaceUse) -> Result<(), Error> {
    check_tracing_environment(namespaces, run_capability_probe)
}

/// Refuses a ptrace run that `probe` says this environment cannot host: the
/// namespace probe for a run that creates namespaces, then the ptrace and
/// seccomp probes for every run, which a `--no-namespace` run needs too.
fn check_tracing_environment(
    namespaces: NamespaceUse,
    mut probe: impl FnMut(CapabilityProbe) -> Result<bool, Error>,
) -> Result<(), Error> {
    if namespaces == NamespaceUse::Creates && !probe(CapabilityProbe::Namespaces)? {
        anyhow::bail!(
            "Hermit cannot create its required user and PID namespaces: \
             unshare(CLONE_NEWUSER | CLONE_NEWPID) was denied. Allow unprivileged user namespaces \
             and the unshare syscall in the host/container policy."
        );
    }
    if !probe(CapabilityProbe::Ptrace)? {
        anyhow::bail!(
            "Hermit cannot use ptrace in this environment: a child PTRACE_TRACEME probe was \
             denied. Allow same-UID parent-child ptrace in the container seccomp and host \
             Yama/LSM policy; CAP_SYS_PTRACE is normally not required. Use --namespace-only for \
             a sandbox smoke test without ptrace (it intercepts only kill, through a seccomp \
             filter)."
        );
    }
    if !probe(CapabilityProbe::Seccomp)? {
        anyhow::bail!(
            "Hermit cannot install its tracee seccomp filter: \
             seccomp(SECCOMP_SET_MODE_FILTER) was denied. Allow seccomp and \
             prctl(PR_SET_NO_NEW_PRIVS) in the container policy. --namespace-only also \
             installs a seccomp filter (on kill), so it cannot run here either."
        );
    }
    Ok(())
}

#[cfg(feature = "dbt")]
fn is_dynamorio_sdk(path: &Path) -> bool {
    path.join("include/dr_api.h").is_file()
        || path.join("DynamoRIOConfig.cmake").is_file()
        || path.join("cmake/DynamoRIOConfig.cmake").is_file()
}

#[cfg(feature = "dbt")]
fn dynamorio_sdk_available() -> bool {
    if hermit_resources::resource("dynamorio/bin64/drrun")
        .is_ok_and(|path| path.is_some_and(|path| path.is_file()))
    {
        return true;
    }
    if reverie_dbt::bundled_drrun_path().is_file() {
        return true;
    }
    const DEFAULT_ROOTS: [&str; 3] = [
        "/usr/lib/cmake/DynamoRIO",
        "/usr/local/lib/cmake/DynamoRIO",
        "/opt/dynamorio",
    ];

    ["DYNAMORIO_HOME", "DynamoRIO_DIR"]
        .into_iter()
        .filter_map(std::env::var_os)
        .map(PathBuf::from)
        .chain(DEFAULT_ROOTS.into_iter().map(PathBuf::from))
        .any(|path| is_dynamorio_sdk(&path))
}

#[cfg(feature = "dbt")]
fn dbt_runtime_unavailable_reason() -> Option<String> {
    dbt_runtime_unavailable_reason_from(detcore_dbt::runtime_library_path())
}

#[cfg(feature = "dbt")]
fn dbt_runtime_unavailable_reason_from(runtime: io::Result<PathBuf>) -> Option<String> {
    runtime.err().map(|error| {
        format!(
            "the Detcore DBT runtime is unavailable: {error}; build the hermit binary and \
             cdylib in the same target directory"
        )
    })
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(#688): Review LiteInst runtime discovery.
/// Checks that `path` is an x86-64 shared object that exports every name in
/// `required` and registers `initializer_name` in its constructor array, so
/// that preloading it runs that initializer. `label` names the library in every
/// error message. Returns the canonical path.
/// Detcore requires its protected root and sole local RELATIVE registration;
/// this is minimal discovery, separate from complete artifact qualification.
fn validate_preload_constructor_library(
    path: &Path,
    label: &str,
    required: &[&str],
    initializer_name: &str,
) -> io::Result<PathBuf> {
    let bytes = fs::read(path).map_err(|error| {
        io::Error::new(
            error.kind(),
            format!("failed to read {label} {}: {error}", path.display()),
        )
    })?;
    let elf = Elf::parse(&bytes).map_err(|error| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            format!("{label} {} is not an ELF DSO: {error}", path.display()),
        )
    })?;
    if elf.header.e_type != header::ET_DYN || elf.header.e_machine != header::EM_X86_64 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("{label} {} is not an x86-64 shared object", path.display()),
        ));
    }

    for &name in required {
        if !elf
            .dynsyms
            .iter()
            .any(|symbol| elf.dynstrtab.get_at(symbol.st_name) == Some(name))
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!(
                    "{label} {} is missing required export {name}",
                    path.display()
                ),
            ));
        }
    }
    if initializer_name == liteinst_constructor::ROOT {
        liteinst_constructor::validate(&bytes, &elf).map_err(|error| {
            io::Error::new(
                error.kind(),
                format!(
                    "{label} {} has invalid Detcore constructor binding: {error}",
                    path.display()
                ),
            )
        })?;
        return path.canonicalize();
    }
    let (initializer_index, initializer) = elf
        .dynsyms
        .iter()
        .enumerate()
        .find(|(_, symbol)| elf.dynstrtab.get_at(symbol.st_name) == Some(initializer_name))
        .ok_or_else(|| {
            io::Error::other(format!(
                "checked {label} initializer {initializer_name} disappeared"
            ))
        })?;
    let init_array = elf
        .section_headers
        .iter()
        .find(|section| section.sh_type == section_header::SHT_INIT_ARRAY)
        .ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                format!("{label} {} has no constructor array", path.display()),
            )
        })?;
    let init_start = init_array.sh_addr;
    let init_end = init_start.saturating_add(init_array.sh_size);
    let relocated_initializer = elf
        .dynrelas
        .iter()
        .chain(elf.dynrels.iter())
        .any(|relocation| {
            (init_start..init_end).contains(&relocation.r_offset)
                && relocation.r_sym == initializer_index
        });
    let init_bytes = usize::try_from(init_array.sh_offset)
        .ok()
        .and_then(|start| {
            usize::try_from(init_array.sh_size)
                .ok()
                .and_then(|size| bytes.get(start..start.checked_add(size)?))
        })
        .unwrap_or_default();
    let direct_initializer = init_bytes
        .as_chunks::<8>()
        .0
        .iter()
        .any(|entry| u64::from_le_bytes(*entry) == initializer.st_value);
    if !relocated_initializer && !direct_initializer {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "{label} {} does not register {initializer_name} as a preload constructor",
                path.display()
            ),
        ));
    }
    path.canonicalize()
}

/// Environment variable naming the in-guest Detcore preload
/// (`libdetcore_liteinst.so`) explicitly.
pub const LITEINST_TOOL_RUNTIME_ENV: &str = "HERMIT_LITEINST_TOOL_RUNTIME";

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-3635): Review in-guest Detcore runtime discovery.
/// Returns the in-guest Detcore preload: the one [`LITEINST_TOOL_RUNTIME_ENV`]
/// names; otherwise the packaged `rsrcs/libdetcore_liteinst.so` of the selected
/// Hermit installation (`hermit-install` stages it, as it stages the DBT and
/// SaBRe Detcore runtimes, and a standalone installation or published bundle
/// carries it). A selected installation missing that resource is incomplete
/// and is refused. When no installation is selected, the development fallback
/// is the one `cargo build -p detcore-liteinst` built beside the Hermit binary
/// or in its `deps/` directory.
///
/// A runtime built from another tree is refused when it starts: Hermit passes
/// it [`detcore::config_wire_fingerprint`], which it compares with its own
/// before decoding anything (<https://github.com/rrnewton/hermit/issues/3986>).
#[doc(hidden)]
pub fn liteinst_tool_runtime_library_path() -> io::Result<PathBuf> {
    if let Some(path) = std::env::var_os(LITEINST_TOOL_RUNTIME_ENV) {
        let path = PathBuf::from(path);
        if !path.is_file() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("{LITEINST_TOOL_RUNTIME_ENV} does not name a regular file"),
            ));
        }
        return validate_liteinst_tool_runtime_library(&path).map_err(|error| {
            io::Error::new(
                error.kind(),
                format!("{LITEINST_TOOL_RUNTIME_ENV} is invalid: {error}"),
            )
        });
    }

    if let Some(path) = hermit_resources::resource("libdetcore_liteinst.so")? {
        if !path.is_file() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                "selected Hermit installation has no finalized in-guest Detcore runtime; \
                 run `make install-deps` to build and stage the qualified LiteInst runtime",
            ));
        }
        return validate_liteinst_tool_runtime_library(&path);
    }

    let executable = std::env::current_exe()?;
    let directory = executable.parent().ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::NotFound,
            "Hermit executable has no parent directory",
        )
    })?;
    if let Some(path) = [
        directory.join("libdetcore_liteinst.so"),
        directory.join("deps/libdetcore_liteinst.so"),
    ]
    .into_iter()
    .find(|path| path.is_file())
    {
        return validate_liteinst_tool_runtime_library(&path);
    }
    Err(io::Error::new(
        io::ErrorKind::NotFound,
        format!(
            "libdetcore_liteinst.so was not built beside {} or staged as an installed resource",
            executable.display()
        ),
    ))
}

fn validate_liteinst_tool_runtime_library(path: &Path) -> io::Result<PathBuf> {
    validate_preload_constructor_library(
        path,
        "in-guest Detcore runtime",
        &["detcore_liteinst_initialize"],
        "detcore_liteinst_initialize",
    )
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-3635): Review the in-guest LiteInst library refusals.
/// In-guest LiteInst refused a run before starting its guest, because the
/// runtime cannot honour the request or could not monitor the program.
///
/// A type rather than a message, because it crosses the container error
/// boundary as [`error::FailureKind::PolicyRefusal`] (exit 122).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LiteinstInGuestRefusal {
    backend: Backend,
    reason: String,
}

impl LiteinstInGuestRefusal {
    /// A refusal of a `--backend=liteinst` run.
    pub fn new(reason: impl Into<String>) -> Self {
        Self::for_backend(Backend::Liteinst, reason)
    }

    /// A refusal of a run of `backend`, one of the in-guest LiteInst backends
    /// ([`Backend::is_in_guest_liteinst`]), so the message names the backend
    /// the caller selected.
    pub fn for_backend(backend: Backend, reason: impl Into<String>) -> Self {
        debug_assert!(backend.is_in_guest_liteinst(), "{backend:?}");
        Self {
            backend,
            reason: reason.into(),
        }
    }
}

impl std::fmt::Display for LiteinstInGuestRefusal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let runtime = match self.backend {
            Backend::InGuestTrap => "in-guest LiteInst, site patching off",
            _ => "in-guest LiteInst",
        };
        write!(
            f,
            "--backend={} ({runtime}) refuses this run: {}",
            self.backend.as_str(),
            self.reason
        )
    }
}

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

/// Refuses, before the guest starts, an in-guest LiteInst run that the runtime
/// cannot honour or could not monitor.
///
/// - A maximum timeslice. Reverie's in-guest Tool host has no preemption timer:
///   its `set_timer` refuses with `ENOSYS`, and Detcore treats a failed
///   `set_timer` as fatal (`.expect("Failed to set timer")`), so the guest
///   would panic at its first scheduling point. The CLI refuses
///   `--max-timeslice` earlier; this covers library callers.
/// - A kernel older than Linux 6.5 ([`in_guest_liteinst_kernel_gap`]).
/// - A guest program that would run code before, or without, the runtime's
///   constructor ([`in_guest_liteinst_program_gap`]).
/// - For `in-guest-trap`, a guest environment that sets
///   `REVERIE_LITEINST_SITE_PATCHING` to anything but `0`
///   ([`in_guest_trap_site_patching_conflict`]).
///
/// This runs where the guest is spawned (inside the container for the CLI),
/// so it resolves the program in the filesystem the guest sees. It pins the
/// resolved program on `command` ([`pin_spawn_program`]), so the launch
/// executes the same path this check inspected. For `in-guest-trap` it then
/// sets `REVERIE_LITEINST_SITE_PATCHING=0` on `command`
/// ([`force_in_guest_site_patching`]).
#[cfg(feature = "liteinst")]
fn refuse_in_guest_liteinst_run(
    command: &mut Command,
    config: &DetConfig,
    backend: Backend,
) -> Result<(), Error> {
    let reason = if let Some(conflict) = in_guest_trap_site_patching_conflict(command, backend) {
        Some(conflict)
    } else if config.max_timeslice.is_some() {
        Some(
            "it cannot deliver Detcore's preemption timer yet (the in-guest Tool host refuses \
             set_timer with ENOSYS), so the run would fail at its first timeslice; pass \
             --max-timeslice=disabled"
                .to_owned(),
        )
    } else if let Some(gap) = in_guest_liteinst_kernel_gap(&running_kernel_release()) {
        Some(gap)
    } else {
        in_guest_liteinst_program_gap(command)?
    };
    match reason {
        Some(reason) => Err(Error::new(LiteinstInGuestRefusal::for_backend(
            backend, reason,
        ))),
        None => {
            force_in_guest_site_patching(command, backend);
            Ok(())
        }
    }
}

/// The value of `REVERIE_LITEINST_SITE_PATCHING` (`reverie_liteinst::SITE_PATCHING_ENV`)
/// that the guest sees under `backend`, when Hermit decides it: `0` for
/// `in-guest-trap`, which exists to run with no syscall site patched, and
/// `None` for `liteinst`, which leaves the variable to the guest environment
/// (unset means patching on).
pub fn forced_site_patching_value(backend: Backend) -> Option<&'static str> {
    match backend {
        Backend::InGuestTrap => Some("0"),
        _ => None,
    }
}

/// Why an `in-guest-trap` run cannot start with the guest environment of
/// `command`: the environment (explicit or inherited) sets
/// `REVERIE_LITEINST_SITE_PATCHING` to a value other than the one Hermit
/// forces. Hermit refuses such a run rather than overriding the value
/// silently, because the caller asked for two contradictory things: site
/// patching (or a value the runtime would reject) and the backend that exists
/// to run without it. A value equal to the forced one is accepted. `None`
/// for every other backend.
#[cfg(feature = "liteinst")]
fn in_guest_trap_site_patching_conflict(command: &Command, backend: Backend) -> Option<String> {
    let forced = forced_site_patching_value(backend)?;
    let name = reverie_liteinst::SITE_PATCHING_ENV;
    let value = command.get_env(name)?;
    (*value != *std::ffi::OsStr::new(forced)).then(|| {
        format!(
            "the guest environment sets {name}={}, but --backend={} runs with syscall site \
             patching off and sets {name}={forced} itself; drop the variable, or select \
             --backend=liteinst to patch syscall sites",
            value.to_string_lossy(),
            backend.as_str()
        )
    })
}

/// Sets the guest's `REVERIE_LITEINST_SITE_PATCHING` to the value Hermit
/// forces for `backend` ([`forced_site_patching_value`]), leaving the
/// environment unchanged for a backend that forces none. The guest's
/// descendants inherit it unless the guest changes its own environment; a
/// descendant that turns patching back on shows in the run's dispatch record
/// as patched sites.
#[cfg(feature = "liteinst")]
fn force_in_guest_site_patching(command: &mut Command, backend: Backend) {
    if let Some(value) = forced_site_patching_value(backend) {
        command.env(reverie_liteinst::SITE_PATCHING_ENV, value);
    }
}

/// Whether every in-guest Tool image of a `backend` run checks that its runtime
/// captured site patching off and statistics on
/// (`detcore::Config::in_guest_site_patching_off`): true for `in-guest-trap`.
#[cfg(feature = "liteinst")]
fn in_guest_site_patching_off(backend: Backend) -> bool {
    forced_site_patching_value(backend) == Some("0")
}

/// Whether an in-guest run of `backend` collects LiteInst's statistics: when
/// something reads them ([`backend_stats::request`]), and on every
/// `in-guest-trap` run, whose result is checked against them
/// ([`in_guest_trap_patching_evidence`]).
#[cfg(feature = "liteinst")]
fn in_guest_stats_request(
    backend: Backend,
    summary_json: &Option<PathBuf>,
) -> reverie::BackendStatsRequest {
    if backend == Backend::InGuestTrap {
        reverie::BackendStatsRequest::ENABLED
    } else {
        backend_stats::request(summary_json)
    }
}

/// Reports a refusal the fail-closed policy decided inside the guest as
/// Hermit's own refusal. The in-guest Detcore that refuses tells the
/// coordinator why (`GlobalState::policy_refusal`) and exits with
/// `HERMIT_POLICY_REFUSAL_EXIT`, which ends only the guest process. Without
/// this the run ended with that as the guest's exit status and no reason, or
/// with whatever postcondition the early exit then tripped (an unregistered
/// exit, or in-guest-trap's missing statistics). Checked before those.
#[cfg(feature = "liteinst")]
fn refuse_in_guest_policy(backend: Backend, refusal: Option<String>) -> Result<(), Error> {
    match refusal {
        Some(reason) => Err(Error::new(LiteinstInGuestRefusal::for_backend(
            backend,
            format!("{reason}; --backend=ptrace runs this program"),
        ))),
        None => Ok(()),
    }
}

/// Why a finished `in-guest-trap` run did not show that it ran with syscall
/// site patching off, from its dispatch record and the number of guest
/// processes that reported LiteInst statistics: a site the runtime patched or
/// considered patching (with patching off it considers none), no record, or no
/// process report at all, in which case the record's zeros are not
/// measurements (a run whose guests collected no statistics still yields an
/// empty record). `None` for every other backend.
///
/// This is the coordinator's postcondition. Each Tool image also refuses to
/// run its program when the runtime started it with patching on
/// (`detcore::in_guest_site_patching`), which holds for every image whatever
/// it reports; this one sees only the images that report, because an image
/// reports its statistics when it exits (`exit` or `exit_group`), so an image
/// that `execve`s, or is killed by a signal, sends none, and the coordinator
/// cannot tell that from a report that was suppressed. It refuses a run in
/// which no process reported; it does not detect a run in which some did and
/// others did not. Each image's own check covers that: an image whose runtime
/// is not collecting statistics refuses to run its program.
#[cfg(feature = "liteinst")]
fn in_guest_trap_patching_evidence(
    backend: Backend,
    dispatch: Option<&reverie::DispatchStats>,
    process_reports: u64,
) -> Option<String> {
    if backend != Backend::InGuestTrap {
        return None;
    }
    let Some(record) = dispatch else {
        return Some(
            "the run produced no LiteInst dispatch record, so it cannot show that no syscall \
             site was patched"
                .to_owned(),
        );
    };
    if process_reports == 0 {
        return Some(
            "no guest process reported LiteInst statistics, so the run's zero patched sites \
             are not a measurement and it cannot show that no syscall site was patched"
                .to_owned(),
        );
    }
    let shown =
        |count: Option<u64>| count.map_or_else(|| "unmeasured".to_owned(), |n| n.to_string());
    (record.sites.patched != Some(0) || record.sites.candidates != Some(0)).then(|| {
        format!(
            "its guests' LiteInst statistics report {} patched and {} candidate syscall sites, \
             so it did not run with syscall site patching off and its result is not an \
             in-guest-trap result",
            shown(record.sites.patched),
            shown(record.sites.candidates)
        )
    })
}

/// Refuses a finished `in-guest-trap` run whose dispatch record shows site
/// patching ([`in_guest_trap_patching_evidence`]): records a determinism loss,
/// so verification refuses to compare it, and returns the typed in-guest
/// refusal (exit 122).
#[cfg(feature = "liteinst")]
fn refuse_in_guest_trap_patching(backend: Backend, evidence: Option<String>) -> Result<(), Error> {
    match evidence {
        Some(reason) => {
            detcore::detlog::record_determinism_loss(&reason);
            Err(Error::new(LiteinstInGuestRefusal::for_backend(
                backend, reason,
            )))
        }
        None => Ok(()),
    }
}

/// Use the existing actual subscriber-derived forwarding policy for the
/// shared native path. Call inside the same verification tracing scope.
#[cfg(feature = "native-exec")]
#[doc(hidden)]
pub fn in_guest_detlog_forward_policy_for_native() -> detcore::detlog::ForwardPolicy {
    in_guest_detlog_forward_policy()
}

/// Report the shared native trap runtime through the existing statistics
/// comparator and require a real guest report with site patching disabled.
/// This never converts a patching-off execution into LiteInst evidence.
#[cfg(feature = "liteinst")]
#[doc(hidden)]
pub fn native_trap_dispatch_stats(
    source: &reverie_liteinst::LiteinstBackendStatsSource,
) -> Result<Option<reverie::DispatchStats>, Error> {
    let backend = Backend::InGuestTrap;
    let record = backend_stats::report(backend, reverie::BackendStatsRequest::ENABLED, source);
    let evidence = in_guest_trap_patching_evidence(
        backend,
        record.as_ref(),
        source.snapshot().process_reports(),
    );
    refuse_in_guest_trap_patching(backend, evidence)?;
    Ok(record)
}

/// The oldest kernel in-guest LiteInst runs on, as (major, minor). Linux 6.5
/// added `SO_PEERPIDFD`: the coordinator admits each guest process through the
/// pidfd of the socket's peer and watches that pidfd for the process's exit
/// (`in_guest_exits`). On an older kernel every admission would fail.
#[cfg(feature = "liteinst")]
const IN_GUEST_LITEINST_KERNEL_FLOOR: (u32, u32) = (6, 5);

/// The running kernel's release string (`uname -r`), or empty if unreadable.
#[cfg(feature = "liteinst")]
fn running_kernel_release() -> String {
    let mut name: libc::utsname = unsafe { std::mem::zeroed() };
    // SAFETY: uname fills the struct it is given.
    if unsafe { libc::uname(&mut name) } != 0 {
        return String::new();
    }
    // SAFETY: uname NUL-terminates each field.
    unsafe { std::ffi::CStr::from_ptr(name.release.as_ptr()) }
        .to_string_lossy()
        .into_owned()
}

/// Why kernel `release` cannot run in-guest LiteInst, or `None` when it is at
/// least [`IN_GUEST_LITEINST_KERNEL_FLOOR`]. A release whose version cannot be
/// read is refused.
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_kernel_gap(release: &str) -> Option<String> {
    let mut numbers = release
        .split(|c: char| !c.is_ascii_digit())
        .map(|part| part.parse::<u32>().ok());
    let (floor_major, floor_minor) = IN_GUEST_LITEINST_KERNEL_FLOOR;
    match (numbers.next().flatten(), numbers.next().flatten()) {
        (Some(major), Some(minor)) if (major, minor) >= IN_GUEST_LITEINST_KERNEL_FLOOR => None,
        (Some(_), Some(_)) => Some(format!(
            "it needs Linux {floor_major}.{floor_minor} or later (SO_PEERPIDFD, which it uses to \
             admit each guest process and observe its exit); this kernel is {release}"
        )),
        _ => Some(format!(
            "it needs Linux {floor_major}.{floor_minor} or later (SO_PEERPIDFD), and the kernel \
             release {release:?} does not say which version this is"
        )),
    }
}

/// How many `#!` interpreters [`in_guest_liteinst_program_gap`] follows before
/// it refuses the program. The kernel has its own small limit (`exec_binprm` in
/// `fs/exec.c`); a chain this check stops following is refused, not run, so the
/// check fails closed whichever limit is lower.
#[cfg(feature = "liteinst")]
const MAX_INTERPRETER_DEPTH: usize = 4;

/// Why the guest program would run code before, or without, the in-guest
/// runtime, or `None` when it would not.
///
/// The runtime is an `LD_PRELOAD` library whose constructor installs Detcore.
/// The dynamic loader never loads it into a statically linked image or an image
/// of another architecture, and ignores `LD_PRELOAD` in secure-execution mode
/// (set-user-ID, set-group-ID, or file capabilities). An executable's
/// `DT_PREINIT_ARRAY` runs before every library constructor. Each of those
/// would run guest code unmonitored, so each is refused. `#!` scripts are
/// checked through their interpreter, read as the kernel's script handler
/// reads it ([`script::KernelScript`]). A program or interpreter that is not a
/// regular file is refused before it is opened: the kernel executes only
/// regular files, opening a device can act on it, opening a socket fails, and
/// reading a FIFO that has no writer would block the check.
///
/// Every fact about one file (its type, header, ELF headers, mode bits and
/// capabilities) is read through one open descriptor, so they all describe
/// the same file. The check is still advisory, not containment: the `execve`
/// opens the path again, and a file replaced at that path in between is not
/// checked again.
///
/// Not covered, and recorded on
/// <https://github.com/rrnewton/hermit/issues/3520>:
/// - The loader itself, the C library's initialization, the executable's
///   `IFUNC` resolvers and the constructors of the executable's own shared
///   libraries all run before the runtime's constructor in every guest.
/// - Secure-execution mode that a security module or a parent with differing
///   real and effective IDs imposes; only the file's own mode bits and
///   capabilities are checked.
/// - Whether the `PT_INTERP` loader honours `LD_PRELOAD` at all.
/// - A `binfmt_misc` rule, which the kernel consults before its ELF and script
///   handlers, and which can hand a file this check accepted to another
///   interpreter (for example an emulator).
/// - A runtime library the loader fails to load: the loader continues without
///   it, and only the launch's check that the guest connected before it exited
///   reports the failure, after the guest ran.
/// - A `pre_exec` callback on `command` that changes the working directory or
///   the root directory: the check resolves paths against the working
///   directory `command` is configured with, in Hermit's root, before any
///   such callback runs.
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_program_gap(command: &mut Command) -> Result<Option<String>, Error> {
    use std::io::Read;
    use std::os::unix::fs::OpenOptionsExt;

    use script::KernelScript;

    let mut path = pin_spawn_program(command)?;
    for _ in 0..=MAX_INTERPRETER_DEPTH {
        let shown = path.display().to_string();
        let not_regular = || {
            Ok(Some(format!(
                "{shown} is not a regular file, and the kernel executes only regular files"
            )))
        };
        if !fs::metadata(&path)
            .with_context(|| format!("cannot read guest program {shown}"))?
            .is_file()
        {
            return not_regular();
        }
        // The path can change type after the check above. O_NONBLOCK then keeps
        // the open of a FIFO that has no writer from blocking, and O_NOCTTY
        // keeps a terminal from becoming Hermit's controlling terminal; neither
        // changes how a regular file is read. The descriptor's own type is
        // checked again below.
        let mut file = fs::OpenOptions::new()
            .read(true)
            .custom_flags(libc::O_NONBLOCK | libc::O_NOCTTY)
            .open(&path)
            .with_context(|| format!("cannot read guest program {shown}"))?;
        if !file
            .metadata()
            .with_context(|| format!("cannot read guest program {shown}"))?
            .is_file()
        {
            return not_regular();
        }
        let mut header = Vec::new();
        (&file)
            .take(256)
            .read_to_end(&mut header)
            .with_context(|| format!("cannot read guest program {shown}"))?;
        match KernelScript::from_buf(&header) {
            KernelScript::Interpreter(interpreter) => {
                // The kernel opens a relative interpreter against the working
                // directory of the process calling execve.
                path = match command.get_current_dir() {
                    Some(directory) => directory.join(interpreter),
                    None => interpreter,
                };
                continue;
            }
            KernelScript::Declined => {
                return Ok(Some(format!(
                    "{shown} starts with #! but the kernel's script handler declines it (it \
                     names no interpreter, or the interpreter path fills the first 256 bytes), \
                     so the kernel would fail the execve or hand the file to another handler \
                     that need not load the runtime"
                )));
            }
            KernelScript::NotScript => {}
        }
        let Some(startup) = interp::elf_startup(&mut file)
            .with_context(|| format!("cannot read the ELF headers of guest program {shown}"))?
        else {
            return Ok(Some(format!(
                "{shown} is neither an ELF executable nor a #! script, so the kernel would hand \
                 it to a handler that need not load the runtime"
            )));
        };
        let reason = if !startup.x86_64 {
            "is not a 64-bit x86-64 ELF image, so the dynamic loader cannot load the 64-bit \
             runtime into it"
        } else if !startup.has_interp {
            "is statically linked (it has no PT_INTERP), so no dynamic loader runs to load the \
             runtime"
        } else if is_secure_execution_image(&file, &shown)? {
            "is set-user-ID, set-group-ID or has file capabilities, so the kernel runs it in \
             secure-execution mode, where the dynamic loader ignores LD_PRELOAD"
        } else if startup.has_preinit_array {
            "has a DT_PREINIT_ARRAY, whose functions run before the runtime's constructor"
        } else {
            return Ok(None);
        };
        return Ok(Some(format!(
            "guest program {shown} {reason}, so it would run unmonitored if Hermit launched it"
        )));
    }
    Ok(Some(format!(
        "the #! interpreters of guest program {} nest more than {MAX_INTERPRETER_DEPTH} deep",
        Path::new(command.get_program()).display()
    )))
}

/// Resolves the program `command` executes with Reverie's
/// [`Command::find_program`], the call the in-guest launch makes, and pins the
/// result on `command`, keeping `argv[0]`.
///
/// The result is absolute, so the launch's own `find_program` returns it
/// unchanged and the launch executes the same path the check inspected,
/// whatever `PATH` or working directory either side would otherwise use. The
/// path, not its contents: a file replaced at that path in between is not
/// checked again (see [`in_guest_liteinst_program_gap`]). A program the launch
/// could not find is an error here, as it would be at launch.
#[cfg(feature = "liteinst")]
fn pin_spawn_program(command: &mut Command) -> Result<PathBuf, Error> {
    let arg0 = command.get_arg0().to_owned();
    let program = command.find_program().with_context(|| {
        format!(
            "cannot find guest program {}",
            Path::new(command.get_program()).display()
        )
    })?;
    command.program(&program).arg0(arg0);
    Ok(program)
}

/// Whether executing `file` (shown as `shown`) puts the kernel in
/// secure-execution mode (`AT_SECURE`): set-user-ID, set-group-ID (with group
/// execute, as the kernel requires), or a `security.capability` attribute.
#[cfg(feature = "liteinst")]
fn is_secure_execution_image(file: &fs::File, shown: &str) -> Result<bool, Error> {
    let mode = file
        .metadata()
        .with_context(|| format!("cannot stat guest program {shown}"))?
        .permissions()
        .mode();
    if mode & 0o4000 != 0 || mode & 0o2010 == 0o2010 {
        return Ok(true);
    }
    // SAFETY: the descriptor is open for the call, the name is NUL-terminated,
    // and a zero-sized query writes no value.
    let size = unsafe {
        libc::fgetxattr(
            file.as_raw_fd(),
            c"security.capability".as_ptr(),
            std::ptr::null_mut(),
            0,
        )
    };
    if size >= 0 {
        return Ok(true);
    }
    match io::Error::last_os_error().raw_os_error() {
        Some(libc::ENODATA) | Some(libc::ENOTSUP) => Ok(false),
        _ => Err(Error::new(io::Error::last_os_error()).context(format!(
            "cannot read the file capabilities of guest program {shown}"
        ))),
    }
}

/// Refuses a wall-clock bound on an in-guest LiteInst run. The bound drops the
/// run's future, and dropping it neither kills the guest nor stops the
/// blocking task that waits for it.
#[cfg(feature = "liteinst")]
fn refuse_in_guest_liteinst_timeout(
    backend: Backend,
    timeout: Option<Duration>,
) -> Result<(), Error> {
    if backend.is_in_guest_liteinst() && timeout.is_some() {
        return Err(Error::new(LiteinstInGuestRefusal::for_backend(
            backend,
            "a wall-clock timeout has not been qualified for the in-guest runtime, whose run \
             future does not kill the guest when it is dropped",
        )));
    }
    Ok(())
}

#[cfg(not(feature = "liteinst"))]
fn refuse_in_guest_liteinst_timeout(
    _backend: Backend,
    _timeout: Option<Duration>,
) -> Result<(), Error> {
    Ok(())
}

#[cfg(feature = "liteinst")]
fn liteinst_unavailable_reason() -> Option<String> {
    liteinst_runtime_unavailable_reason()
}

// LiteInst is an experimental backend whose architecture is not settled, so the
// published crate leaves it out: `reverie-liteinst` (and through it `liteinst2`)
// is an optional dependency behind the `liteinst` feature, which
// `third-party-backends` enables for every validation build.
#[cfg(not(feature = "liteinst"))]
fn liteinst_unavailable_reason() -> Option<String> {
    Some(if option_env!("CARGO_PKG_NAME") == Some("hermit-run") {
        "this build was compiled without the liteinst backend, which is not included in this registry release; its source-development implementation is on https://github.com/rrnewton/hermit/tree/main. This release has no `liteinst` Cargo feature, and `third-party-backends` does not include LiteInst. LiteInst availability on this machine has not been checked".to_owned()
    } else {
        "this build was compiled without the liteinst backend: the `liteinst` feature is not enabled; rebuild with `--features liteinst` (or `--features third-party-backends`). This says nothing about whether LiteInst works on this machine -- it has not been checked".to_owned()
    })
}

#[cfg(feature = "liteinst")]
fn liteinst_runtime_unavailable_reason() -> Option<String> {
    liteinst_tool_runtime_library_path().err().map(|error| {
        format!(
            "the in-guest Detcore runtime is unavailable: {error}; build it with `cargo build -p detcore-liteinst` in the target directory of this hermit binary, install it as rsrcs/libdetcore_liteinst.so, or name it with {LITEINST_TOOL_RUNTIME_ENV}"
        )
    })
}

fn kvm_device_unavailable_reason(path: &Path) -> Option<String> {
    fs::OpenOptions::new()
        .read(true)
        .write(true)
        .open(path)
        .err()
        .map(|error| {
            format!(
                "cannot open {} read-write: {error}; grant access through the device owner/group \
                 or root",
                path.display()
            )
        })
}

/// Process instrumentation backend used to run a Hermit guest.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, ValueEnum)]
pub enum Backend {
    /// Use Reverie's ptrace backend.
    #[default]
    Ptrace,
    /// Use the DynamoRIO backend.
    Dbt,
    /// Use LiteInst, with Detcore's Tool running inside the guest.
    Liteinst,
    /// Use in-guest LiteInst with syscall site patching off: the same
    /// in-guest Detcore host as `liteinst`, but every trapped syscall reaches
    /// the Tool through the seccomp `SIGSYS` fallback and no syscall site is
    /// rewritten. Hermit sets `REVERIE_LITEINST_SITE_PATCHING=0` in the
    /// guest's environment and refuses a run whose environment asks for any
    /// other value.
    InGuestTrap,
    /// Use the SaBRe static binary rewriting backend.
    Sabre,
    /// Use the KVM backend.
    Kvm,
    /// Preprocess the main ELF with e9patch, then use the ptrace runtime.
    // TODO-HUMAN-REVIEW(PR-594): Review the CLI-only hybrid backend selection.
    E9patch,
}

/// A requested backend could not start because its required runtime support is absent.
///
/// Keep this as a typed error until the top-level command records its failure class.
/// The human-readable message is for the operator; consumers must use the class line
/// emitted by the command rather than matching this prose.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct BackendUnavailable {
    backend: Backend,
    reason: String,
}

impl BackendUnavailable {
    pub fn new(backend: Backend, reason: impl Into<String>) -> Self {
        Self {
            backend,
            reason: reason.into(),
        }
    }

    pub fn backend(&self) -> Backend {
        self.backend
    }

    pub fn reason(&self) -> &str {
        &self.reason
    }
}

impl std::fmt::Display for BackendUnavailable {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            formatter,
            "backend `{}` is unavailable: {}",
            self.backend.as_str(),
            self.reason
        )
    }
}

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

impl Backend {
    const ALL: [Self; 7] = [
        Self::Ptrace,
        Self::Dbt,
        Self::Liteinst,
        Self::InGuestTrap,
        Self::Sabre,
        Self::Kvm,
        Self::E9patch,
    ];

    /// Returns the command-line spelling for this backend.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Ptrace => "ptrace",
            Self::Dbt => "dbt",
            Self::Liteinst => "liteinst",
            Self::InGuestTrap => "in-guest-trap",
            Self::Sabre => "sabre",
            Self::Kvm => "kvm",
            Self::E9patch => "e9patch",
        }
    }

    /// Whether this backend hosts Detcore's Tool inside each guest process
    /// through the in-guest LiteInst preload runtime: `liteinst` and
    /// `in-guest-trap`, which differ only in whether syscall sites are patched.
    /// Every refusal, capability and launch step of in-guest LiteInst applies to
    /// both.
    pub const fn is_in_guest_liteinst(self) -> bool {
        matches!(self, Self::Liteinst | Self::InGuestTrap)
    }

    /// The name of the backend runtime that executes this backend's guest, as
    /// its statistics source names itself (`BackendStatsSnapshot::BACKEND_NAME`).
    /// `in-guest-trap` runs the in-guest LiteInst runtime, so its statistics come
    /// from that runtime's source, which is named `liteinst`.
    pub const fn runtime_name(self) -> &'static str {
        match self {
            Self::InGuestTrap => Self::Liteinst.as_str(),
            other => other.as_str(),
        }
    }

    fn uses_ptrace_pmu_timers(self) -> bool {
        match self {
            Self::Ptrace | Self::E9patch => true,
            // In-guest Detcore has no ptrace tracer to arm the PMU timer from.
            Self::Liteinst | Self::InGuestTrap | Self::Dbt | Self::Sabre | Self::Kvm => false,
        }
    }

    /// Returns backends whose Hermit integration prerequisites are met.
    ///
    /// Some integrations use CLI launch adapters rather than direct
    /// [`run_with_backend`] dispatch.
    pub fn available() -> impl Iterator<Item = Self> {
        Self::ALL
            .into_iter()
            .filter(|backend| backend.is_available())
    }

    /// Returns whether this backend's integration prerequisites are met.
    pub fn is_available(self) -> bool {
        self.unavailable_reason().is_none()
    }

    /// Returns an actionable error when this backend's prerequisites are not met.
    pub fn ensure_available(self) -> Result<(), Error> {
        self.ensure_available_for(NamespaceUse::Creates)
    }

    /// [`Backend::ensure_available`] for a run that creates `namespaces`: a
    /// `--no-namespace` ptrace run is not refused where only `unshare` of a
    /// user namespace is denied, since it never makes that call; the ptrace
    /// and seccomp probes still apply.
    pub fn ensure_available_for(self, namespaces: NamespaceUse) -> Result<(), Error> {
        if let Some(reason) = self.unavailable_reason_for(namespaces) {
            Err(Error::new(BackendUnavailable::new(self, reason)))
        } else {
            Ok(())
        }
    }

    fn unavailable_reason(self) -> Option<String> {
        self.unavailable_reason_for(NamespaceUse::Creates)
    }

    fn unavailable_reason_for(self, namespaces: NamespaceUse) -> Option<String> {
        match self {
            Self::Ptrace => validate_tracing_environment(namespaces)
                .err()
                .map(|error| error.to_string()),
            Self::Dbt => dbt_unavailable_reason(),
            // The same runtime, so the same prerequisites and the same reason.
            Self::Liteinst | Self::InGuestTrap => liteinst_unavailable_reason(),
            // TODO-HUMAN-REVIEW(#589): Review SaBRe backend availability reporting.
            Self::Sabre => sabre_unavailable_reason(),
            Self::Kvm => kvm_device_unavailable_reason(Path::new("/dev/kvm")),
            Self::E9patch => e9patch_unavailable_reason(),
        }
    }
}

struct SkidOvershootReport {
    enabled: bool,
}

/// The backend failed while running the guest, after Hermit launched it: the
/// DBT evidence check, a KVM execution failure, a SaBRe guest that never
/// reached Detcore. `run --verify` records it as `run_failed` with
/// `backend_failure` instead of leaving the `not_run` stamp.
///
/// It sits in the chain below the message that names the failure, so the
/// first `Error:` line is unchanged; it crosses the container error boundary
/// as [`error::FailureKind::BackendRunFailure`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BackendRunFailure;

impl std::fmt::Display for BackendRunFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("the backend failed while running the guest")
    }
}

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

/// A ptrace-backed run observed late precise-timer delivery, so Hermit refuses
/// to treat the completed execution as deterministic evidence.
///
/// This is a type rather than a message match because it crosses the container
/// error boundary as [`error::FailureKind::PolicyRefusal`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SkidOvershootError {
    count: u64,
}

impl SkidOvershootError {
    pub fn new(count: u64) -> Self {
        assert!(
            count > 0,
            "a skid-overshoot refusal requires a positive count"
        );
        Self { count }
    }

    pub fn count(&self) -> u64 {
        self.count
    }
}

impl std::fmt::Display for SkidOvershootError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "observed {} {} report(s); refusing the result because precise PMU timer \
             delivery passed its target and deterministic execution was not established",
            self.count,
            reverie::SKID_OVERSHOOT_MARKER
        )
    }
}

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

fn take_skid_overshoot_error() -> Option<SkidOvershootError> {
    let count = reverie::take_skid_overshoot_count();
    (count > 0).then(|| SkidOvershootError::new(count))
}

impl SkidOvershootReport {
    fn begin(enabled: bool) -> Self {
        if enabled {
            // A previous invocation in this process must not qualify this one.
            let _ = reverie::take_skid_overshoot_count();
        }
        Self { enabled }
    }

    fn finish<T>(self, result: Result<T, Error>) -> Result<T, Error> {
        if !self.enabled {
            return result;
        }

        let Some(overshoot) = take_skid_overshoot_error() else {
            return result;
        };
        match result {
            Ok(_) => Err(Error::new(overshoot)),
            Err(error) => Err(error.context(overshoot)),
        }
    }

    fn finish_with_count<T>(self, result: Result<T, Error>) -> Result<(T, u64), Error> {
        if !self.enabled {
            return result.map(|value| (value, 0));
        }

        let count = reverie::take_skid_overshoot_count();
        match result {
            Ok(value) => Ok((value, count)),
            Err(error) if count > 0 => Err(error.context(SkidOvershootError::new(count))),
            Err(error) => Err(error),
        }
    }
}

// SaBRe's statistics and RPC support are selected only by `sabre`; the external
// loader and Detcore plugin must also be supplied. e9patch preprocessing requires
// external `e9tool`/`e9patch` artifacts. Neither path is enabled by default.
#[cfg(feature = "sabre")]
fn sabre_unavailable_reason() -> Option<String> {
    sabre_runtime_unavailable_reason()
}

#[cfg(not(feature = "sabre"))]
fn sabre_unavailable_reason() -> Option<String> {
    Some("the `sabre` feature is not enabled in this build; rebuild with `--features sabre` (or `--features third-party-backends`). This says nothing about whether SaBRe works on this machine -- it has not been checked".to_owned())
}

#[cfg(feature = "e9patch")]
fn e9patch_unavailable_reason() -> Option<String> {
    // e9patch overlays the rewritten ELF in a mount namespace, so it always
    // creates namespaces (run refuses `--backend=e9patch --no-namespace`).
    validate_tracing_environment(NamespaceUse::Creates)
        .err()
        .map(|error| error.to_string())
        .or_else(e9patch::unavailable_reason)
}

#[cfg(not(feature = "e9patch"))]
fn e9patch_unavailable_reason() -> Option<String> {
    Some("the `e9patch` feature is not enabled in this build; rebuild with `--features e9patch` (or `--features third-party-backends`). This says nothing about whether e9patch works on this machine -- it has not been checked".to_owned())
}

#[cfg(feature = "dbt")]
fn dbt_unavailable_reason() -> Option<String> {
    if !dynamorio_sdk_available() {
        return Some(
            "the DynamoRIO runtime was not found; build target/install_pkg, set HERMIT_INSTALL_DIR, or set DYNAMORIO_HOME/DynamoRIO_DIR to a valid SDK"
                .to_owned(),
        );
    }
    dbt_runtime_unavailable_reason()
}

#[cfg(not(feature = "dbt"))]
// TODO-HUMAN-REVIEW(PR-1150): Review the default-on DBT compile-time feature boundary.
fn dbt_unavailable_reason() -> Option<String> {
    Some(dbt_disabled_build_reason(
        option_env!("CARGO_PKG_NAME").unwrap_or("hermit"),
    ))
}

#[cfg(not(feature = "dbt"))]
fn dbt_disabled_build_reason(package_name: &str) -> String {
    if package_name == "hermit-run" {
        "the dbt backend is not included in this registry release; use a source build from https://github.com/rrnewton/hermit with `--features dbt`. The registry's `third-party-backends` feature does not include DBT. DynamoRIO availability on this machine has not been checked".to_owned()
    } else {
        "the `dbt` feature is not enabled in this source build; rebuild with `--features dbt` (or `--features third-party-backends`). DynamoRIO availability on this machine has not been checked".to_owned()
    }
}

#[cfg(feature = "sabre")]
const SABRE_BINARY_ENV: &str = "HERMIT_SABRE_BINARY";

#[cfg(feature = "sabre")]
fn is_executable_file(path: &Path) -> bool {
    fs::metadata(path)
        .is_ok_and(|metadata| metadata.is_file() && metadata.permissions().mode() & 0o111 != 0)
}

// TODO-HUMAN-REVIEW(PR-739): Review SaBRe loader discovery and executable validation.
#[cfg(feature = "sabre")]
fn resolve_sabre_binary_from(
    override_path: Option<&OsStr>,
    packaged_path: Option<&Path>,
    executable: &Path,
    path_env: &OsStr,
) -> Result<PathBuf, Error> {
    if let Some(requested) = override_path {
        if requested.is_empty() {
            return Err(anyhow!("{SABRE_BINARY_ENV} is empty"));
        }
        let path = PathBuf::from(requested);
        return is_executable_file(&path)
            .then_some(path.clone())
            .ok_or_else(|| {
                anyhow!(
                    "{SABRE_BINARY_ENV}={} is not an executable file",
                    path.display()
                )
            });
    }

    if let Some(path) = packaged_path
        && is_executable_file(path)
    {
        return Ok(path.to_path_buf());
    }

    let directory = executable
        .parent()
        .ok_or_else(|| anyhow!("Hermit executable has no parent directory"))?;
    let sibling = directory.join("sabre");
    let target_build = directory.parent().map(|target| target.join("sabre/sabre"));

    if is_executable_file(&sibling) {
        return Ok(sibling);
    }
    if let Some(candidate) = &target_build
        && is_executable_file(candidate)
    {
        return Ok(candidate.clone());
    }
    if !path_env.is_empty()
        && let Some(candidate) = std::env::split_paths(path_env)
            .map(|directory| directory.join("sabre"))
            .find(|candidate| is_executable_file(candidate))
    {
        return Ok(candidate);
    }

    Err(anyhow!(
        "SaBRe executable was not found in the Hermit installation, beside {}, or in PATH; set {} or {SABRE_BINARY_ENV}",
        executable.display(),
        hermit_resources::INSTALL_DIR_ENV
    ))
}

#[cfg(feature = "sabre")]
fn resolve_sabre_binary() -> Result<PathBuf, Error> {
    let executable =
        std::env::current_exe().context("failed to locate running Hermit executable")?;
    let override_path = std::env::var_os(SABRE_BINARY_ENV);
    let packaged_path = hermit_resources::resource("sabre")?;
    let path_env = std::env::var_os("PATH").unwrap_or_default();
    resolve_sabre_binary_from(
        override_path.as_deref(),
        packaged_path.as_deref(),
        &executable,
        &path_env,
    )
}

#[cfg(feature = "sabre")]
const SABRE_RPC_SOCKET_ENV: &str = "REVERIE_SABRE_HERMIT_RPC_SOCKET";
#[cfg(feature = "sabre")]
const SABRE_DETLOG_FORWARD_ENV: &str = "REVERIE_SABRE_HERMIT_FORWARD_DETLOG";
/// `reverie_sabre::TOOL_OUTPUT_ENV`: the descriptor reverie-sabre protects
/// for the in-guest tool's own output. Hermit does not link reverie-sabre.
#[cfg(feature = "sabre")]
const SABRE_TOOL_OUTPUT_ENV: &str = "REVERIE_SABRE_TOOL_OUTPUT_FD";
/// `detcore_liteinst::DETLOG_FORWARD_ENV`; that crate is a preload `cdylib`, so
/// Hermit cannot import the constant.
#[cfg(feature = "liteinst")]
const LITEINST_DETLOG_FORWARD_ENV: &str = "HERMIT_LITEINST_FORWARD_DETLOG";
/// `detcore_liteinst::DETLOG_FORWARD_POLICY_ENV`, for the same reason.
#[cfg(feature = "liteinst")]
const LITEINST_DETLOG_FORWARD_POLICY_ENV: &str = "HERMIT_LITEINST_FORWARD_DETLOG_POLICY";

/// Where in-guest LiteInst and SaBRe runs in this process forward Detcore's
/// DETLOG records; see [`forward_in_guest_detlogs_to`].
static IN_GUEST_DETLOG_SINK: Mutex<Option<std::os::fd::OwnedFd>> = Mutex::new(None);

/// Makes the in-guest LiteInst and SaBRe runs this process starts forward the
/// DETLOG records Detcore logs inside the guest to `socket`, the sending end of a Unix
/// `SOCK_SEQPACKET` pair. `hermit run --verify` gives each run its own pair and
/// reads the records from the other end into that run's log. Each record is one
/// message, one line (`detcore::detlog::forwarded_line`).
/// A record is forwarded when the CLI's filter logs INFO for its module (see
/// [`in_guest_detlog_forward_policy`]); without a socket none are forwarded.
#[doc(hidden)]
pub fn forward_in_guest_detlogs_to(socket: std::os::fd::OwnedFd) {
    *IN_GUEST_DETLOG_SINK.lock().unwrap() = Some(socket);
}

/// What Hermit tells the in-guest LiteInst runtime, which travels in the
/// sealed preload bootstrap rather than the guest's environment
/// ([`detcore::in_guest_launch`]), so none of it lands on the guest's initial
/// stack or in `/proc/self/environ`:
/// - this build's [`detcore::config_wire_fingerprint`], which the runtime
///   compares with its own before it decodes the configuration the
///   coordinator sends. A runtime built from another tree then fails naming
///   both fingerprints and its own path, not with a bare decoding error
///   (<https://github.com/rrnewton/hermit/issues/3986>);
/// - when `policy` forwards any record, the socket chosen with
///   [`forward_in_guest_detlogs_to`] (an inheritable duplicate, which the
///   runtime moves to a descriptor Reverie reserves and protects from the
///   guest) and the encoded policy, which the guest's Detcore applies to each
///   record by its module as each in-process `detlog!` callsite asks tracing.
///
/// The caller keeps the returned duplicate open until the guest has started.
/// Hermit's own variable names are removed from `command`, so a stale value
/// cannot reach a runtime that falls back to the environment.
#[cfg(feature = "liteinst")]
fn in_guest_liteinst_launch(
    command: &mut Command,
    policy: &detcore::detlog::ForwardPolicy,
) -> Result<
    (
        detcore::in_guest_launch::InGuestLaunch,
        Option<std::os::fd::OwnedFd>,
    ),
    Error,
> {
    command.env_remove(LITEINST_DETLOG_FORWARD_ENV);
    command.env_remove(LITEINST_DETLOG_FORWARD_POLICY_ENV);
    command.env_remove(detcore::LITEINST_CONFIG_FINGERPRINT_ENV);
    let mut launch = detcore::in_guest_launch::InGuestLaunch {
        coordinator_fingerprint: Some(detcore::config_wire_fingerprint()),
        ..Default::default()
    };
    // Asking only whether INFO is on for `detcore`, and then forwarding
    // everything, kept records a target-scoped filter drops under ptrace
    // (info,detcore::random=warn) and could forward nothing where it keeps
    // some (warn,detcore::random=info).
    if !policy.forwards_any() {
        return Ok((launch, None));
    }
    let Some(duplicate) = inheritable_in_guest_detlog_socket()? else {
        return Ok((launch, None));
    };
    launch.forward_request = Some(detcore::detlog::tool_output_env_value(
        duplicate.as_raw_fd(),
    )?);
    launch.forward_policy = Some(policy.encode());
    Ok((launch, Some(duplicate)))
}

/// An inheritable duplicate of the socket chosen with
/// [`forward_in_guest_detlogs_to`], or `None` when none was chosen. The
/// caller passes its number to the guest and keeps it open until the guest
/// has started.
#[cfg(any(feature = "sabre", feature = "liteinst"))]
fn inheritable_in_guest_detlog_socket() -> Result<Option<std::os::fd::OwnedFd>, Error> {
    let sink = IN_GUEST_DETLOG_SINK.lock().unwrap();
    let Some(socket) = sink.as_ref() else {
        return Ok(None);
    };
    // SAFETY: F_DUPFD on an open descriptor returns a new descriptor or -1.
    let duplicate = unsafe { libc::fcntl(socket.as_raw_fd(), libc::F_DUPFD, 3) };
    if duplicate < 0 {
        return Err(Error::new(std::io::Error::last_os_error())
            .context("duplicating the in-guest DETLOG forwarding socket"));
    }
    // SAFETY: fcntl returned a new descriptor this function now owns.
    Ok(Some(unsafe {
        std::os::fd::OwnedFd::from_raw_fd(duplicate)
    }))
}
#[cfg(feature = "sabre")]
const SABRE_PATH_EVIDENCE_ENV: &str = "HERMIT_SABRE_PATH_EVIDENCE";

/// The INFO policy for DETLOG records an in-guest Detcore (SaBRe's plugin,
/// in-guest LiteInst) forwards: the current subscriber's answer for every
/// target the CLI's `RUST_LOG` names (the text
/// `EffectiveFilter::from_default_env` builds every CLI subscriber from) and
/// for any other target.
#[cfg(any(feature = "sabre", feature = "liteinst"))]
fn in_guest_detlog_forward_policy() -> detcore::detlog::ForwardPolicy {
    let directives = std::env::var(tracing_subscriber::EnvFilter::DEFAULT_ENV).unwrap_or_default();
    detcore::detlog::ForwardPolicy::from_current_subscriber(&directives)
}
#[cfg(feature = "sabre")]
const SABRE_STAGING_DIRECTORY: &str = "/dev/shm";

#[cfg(feature = "sabre")]
struct StagedSabreProgram {
    path: PathBuf,
}

#[cfg(feature = "sabre")]
impl Drop for StagedSabreProgram {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.path);
    }
}

#[cfg(feature = "sabre")]
fn sabre_program_needs_neutral_name(program: &Path) -> bool {
    program
        .file_name()
        .is_some_and(|name| name.as_bytes().starts_with(b"ld"))
}

// TODO-HUMAN-REVIEW(PR-845): Review the neutral-name workaround for SaBRe's
// dynamic-loader prefix collision.
#[cfg(feature = "sabre")]
fn stage_sabre_program_in(
    program: &Path,
    staging_directory: &Path,
) -> Result<Option<StagedSabreProgram>, Error> {
    if !sabre_program_needs_neutral_name(program) {
        return Ok(None);
    }

    let path = staging_directory.join(format!("hermit-sabre-program-{}", std::process::id()));
    let mut source = fs::File::open(program).map_err(|error| {
        anyhow!(
            "failed to open SaBRe guest executable {} for neutral-name staging: {error}",
            program.display()
        )
    })?;
    let mut staged = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .mode(0o700)
        .open(&path)
        .map_err(|error| {
            anyhow!(
                "failed to stage SaBRe guest executable {} as {}: {error}",
                program.display(),
                path.display()
            )
        })?;
    if let Err(error) = io::copy(&mut source, &mut staged) {
        let _ = fs::remove_file(&path);
        return Err(anyhow!(
            "failed to stage SaBRe guest executable {} as {}: {error}",
            program.display(),
            path.display()
        ));
    }
    drop(staged);

    Ok(Some(StagedSabreProgram { path }))
}

// TODO-HUMAN-REVIEW(PR-738): Review controller/plugin artifact separation.
#[cfg(feature = "sabre")]
fn sabre_runtime_library_path() -> io::Result<PathBuf> {
    if let Some(path) = hermit_resources::resource("libdetcore_sabre.so")?
        && path.is_file()
    {
        return Ok(path);
    }

    let executable = std::env::current_exe()?;
    let directory = executable.parent().ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::NotFound,
            "Hermit executable has no parent directory",
        )
    })?;
    [
        directory.join("libdetcore_sabre.so"),
        directory.join("deps/libdetcore_sabre.so"),
    ]
    .into_iter()
    .find(|path| path.is_file())
    .ok_or_else(|| {
        io::Error::new(
            io::ErrorKind::NotFound,
            format!(
                "libdetcore_sabre.so was not built beside {}",
                executable.display()
            ),
        )
    })
}

#[cfg(feature = "sabre")]
fn sabre_runtime_unavailable_reason() -> Option<String> {
    if let Err(error) = resolve_sabre_binary() {
        return Some(error.to_string());
    }
    sabre_runtime_library_path().err().map(|error| {
        format!(
            "the Detcore SaBRe plugin is unavailable: {error}; build detcore-sabre and hermit in the same target directory"
        )
    })
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-774): Review the bounded SaBRe RPC disconnect drain.
#[cfg(feature = "sabre")]
const SABRE_RPC_DISCONNECT_TIMEOUT: Duration = Duration::from_secs(1);

#[cfg(feature = "sabre")]
async fn wait_for_sabre_rpc_disconnects<T>(
    global: &Arc<T>,
    timeout: Duration,
) -> Result<(), usize> {
    let disconnected = tokio::time::timeout(timeout, async {
        while Arc::strong_count(global) > 1 {
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
    })
    .await;

    if disconnected.is_ok() {
        return Ok(());
    }

    let live_references = Arc::strong_count(global).saturating_sub(1);
    if live_references == 0 {
        Ok(())
    } else {
        Err(live_references)
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-782): Review SaBRe RPC server shutdown errors.
#[cfg(feature = "sabre")]
async fn stop_sabre_rpc_server<E>(
    server_task: tokio::task::JoinHandle<Result<(), E>>,
) -> Result<(), Error>
where
    E: std::fmt::Display,
{
    server_task.abort();
    match server_task.await {
        Err(error) if error.is_cancelled() => Ok(()),
        Err(error) => Err(anyhow!("SaBRe coordinator task failed: {error}")),
        Ok(Err(error)) => Err(anyhow!("SaBRe coordinator server failed: {error}")),
        Ok(Ok(())) => Err(anyhow!("SaBRe coordinator server stopped unexpectedly")),
    }
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-789): Review complete SaBRe RPC shutdown ordering.
#[cfg(feature = "sabre")]
async fn shutdown_sabre_rpc<T, E>(
    server_task: tokio::task::JoinHandle<Result<(), E>>,
    global: &Arc<T>,
    timeout: Duration,
) -> Result<(), Error>
where
    E: std::fmt::Display,
{
    let server_result = stop_sabre_rpc_server(server_task).await;
    let disconnect_result = wait_for_sabre_rpc_disconnects(global, timeout).await;

    server_result?;
    disconnect_result.map_err(|live_references| {
        anyhow!("SaBRe coordinator stopped with {live_references} live RPC reference(s)")
    })
}

fn ensure_backend_dispatch(backend: Backend) -> Result<(), Error> {
    // The CLI probes ptrace readiness before entering its container; repeating
    // the namespace probe here would test nested namespaces instead of the host.
    if backend == Backend::Ptrace {
        return Ok(());
    }
    if backend == Backend::E9patch {
        return Err(anyhow!(
            "backend `e9patch` requires CLI preprocessing; library callers must use \
             e9patch::prepare and then select `ptrace`"
        ));
    }
    // KVM and DBT have dedicated dispatches (`run_kvm` and `run_dbt`); neither
    // must reach this generic rejection path.
    backend.ensure_available()?;
    Err(anyhow!(
        "backend `{}` has no Hermit dispatch implementation",
        backend.as_str()
    ))
}

/// Run one command with the Detcore tool executing inside a SaBRe plugin and
/// the single GlobalState held by this Hermit coordinator process.
// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-738): Review SaBRe coordinator lifetime and artifact loading.
#[cfg(feature = "sabre")]
async fn run_sabre(
    mut command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    capture_output: bool,
) -> Result<Output, Error> {
    let path_evidence_file = std::env::var_os(SABRE_PATH_EVIDENCE_ENV).map(PathBuf::from);
    // Whether this run passes the plugin a DETLOG socket (below), in which case
    // every Tool image must forward on it; see
    // detcore::Config::in_guest_detlog_forward_policy.
    let mut config = config;
    let policy = in_guest_detlog_forward_policy();
    config.in_guest_detlog_forward_policy = (policy.forwards_any()
        && IN_GUEST_DETLOG_SINK.lock().unwrap().is_some())
    .then(|| policy.encode());
    let sabre = resolve_sabre_binary()?;
    // The kernel execs SaBRe's loader, which loads the guest itself, so the
    // kernel's `exe` link names the loader (`detcore::LaunchImage::launcher`).
    // Declared even where the first image could not be resolved, so that a
    // link Detcore cannot answer is refused rather than named the loader.
    config
        .launch_image
        .get_or_insert(detcore::LaunchImage {
            exe: None,
            launcher: None,
        })
        .launcher = Some(PathBuf::from(&sabre));
    let plugin = sabre_runtime_library_path()
        .map_err(|error| anyhow!("failed to locate the Detcore SaBRe plugin: {error}"))?;
    let program = command.find_program().map_err(|error| {
        anyhow!(
            "failed to resolve SaBRe guest executable {:?}: {error}",
            command.get_program()
        )
    })?;
    let staged_program = stage_sabre_program_in(&program, Path::new(SABRE_STAGING_DIRECTORY))?;
    let launch_program = staged_program
        .as_ref()
        .map_or(program.as_path(), |staged| staged.path.as_path());

    let bootstrap_launch =
        sabre_bootstrap::Launch::new(Path::new(&sabre), launch_program, &config)?;

    // This runs after Hermit enters the guest mount namespace, where `/tmp`
    // names the container-visible temporary filesystem. Do not inherit a
    // host-side nested TMPDIR: validation commonly sets TMPDIR=/tmp/<run>/tmp,
    // but that parent path is hidden once the private `/tmp` mount is active.
    // The SaBRe plugin and coordinator must name the socket through the same
    // namespace-visible path.
    let socket_dir = tempfile::Builder::new()
        .prefix("hermit-sabre-rpc-")
        .tempdir_in(Path::new("/tmp"))?;
    let socket_path = socket_dir.path().join("coordinator.sock");
    let fallback_ready = Arc::new(AtomicBool::new(false));
    let global = Arc::new(detcore::GlobalState::init_global_state(&config).await);
    let server = reverie_rpc_transport::RpcServer::bind_with_readiness(
        &socket_path,
        global.clone(),
        config.clone(),
        fallback_ready.clone(),
    )
    .map_err(|error| anyhow!("failed to start SaBRe coordinator RPC: {error}"))?;
    let server_task = tokio::spawn(async move { server.serve().await });

    // SaBRe runs with the guest's own argv and environment, so the kernel
    // builds the guest's initial stack, /proc/self/cmdline and
    // /proc/self/environ from them, as for an exec of the guest itself. The
    // plugin and image paths and the plugin's settings go in a sealed launch
    // specification instead (reverie_sabre_stats::launch), which SaBRe's loader
    // reads and closes before the guest starts. A script runs as its
    // interpreter, with the argv the kernel would give it.
    let mut first_bytes = vec![0u8; 256];
    let read = std::fs::File::open(launch_program)
        .and_then(|mut file| std::io::Read::read(&mut file, &mut first_bytes))
        .map_err(|error| {
            anyhow!(
                "failed to read SaBRe guest executable {}: {error}",
                launch_program.display()
            )
        })?;
    first_bytes.truncate(read);
    let arg0 = command.get_arg0().to_owned();
    let client = match reverie_sabre_stats::launch::shebang(&first_bytes) {
        Some((interpreter, argument)) => {
            command.program(&sabre);
            command.arg0(OsStr::from_bytes(&interpreter));
            let mut words: Vec<&OsStr> = argument
                .iter()
                .map(|word| OsStr::from_bytes(word))
                .collect();
            words.push(launch_program.as_os_str());
            command.prepend_args(words);
            PathBuf::from(OsStr::from_bytes(&interpreter))
        }
        None => {
            command.program(&sabre);
            command.arg0(&arg0);
            launch_program.to_path_buf()
        }
    };
    let mut launch_settings: Vec<Vec<u8>> = Vec::new();
    let mut setting = |name: &str, value: &OsStr| {
        let mut entry = name.as_bytes().to_vec();
        entry.push(b'=');
        entry.extend_from_slice(value.as_bytes());
        launch_settings.push(entry);
    };
    setting(SABRE_RPC_SOCKET_ENV, socket_path.as_os_str());
    command.env_remove(SABRE_RPC_SOCKET_ENV);
    command.env_remove(sabre_bootstrap::ENVIRONMENT);
    if bootstrap_launch.initializes_random() {
        setting(sabre_bootstrap::ENVIRONMENT, OsStr::new("1"));
    }
    // Publish the configuration and clock RPC fingerprint for this coordinator.
    // The plugin is a separate artifact in the same target directory, so it can
    // be stale without looking it. Its check before connecting names the mismatch
    // instead of failing to decode Config or a later DetTime request field.
    // See detcore_model::config_wire_fingerprint.
    setting(
        detcore::CONFIG_FINGERPRINT_ENV,
        OsStr::new(&detcore::config_wire_fingerprint()),
    );
    command.env_remove(detcore::CONFIG_FINGERPRINT_ENV);
    command.env_remove(SABRE_PATH_EVIDENCE_ENV);
    command.env_remove(SABRE_DETLOG_FORWARD_ENV);
    // Per emitting module, as each in-process `detlog!` callsite asks tracing:
    // asking only about `detcore` dropped every record a target-scoped
    // RUST_LOG (warn,detcore::random=info) keeps under ptrace.
    command.env_remove(SABRE_TOOL_OUTPUT_ENV);
    let detlog_policy = in_guest_detlog_forward_policy();
    // Kept open until the guest has started (it inherits its own copy).
    let mut _detlog_descriptor = None;
    if detlog_policy.forwards_any() {
        setting(
            SABRE_DETLOG_FORWARD_ENV,
            OsStr::new(&detlog_policy.encode()),
        );
        // With a socket (`hermit run --verify`), the plugin sends each record
        // on it from a descriptor reverie-sabre protects from the guest, and
        // the coordinator writes each thread's records into the log at that
        // thread's scheduler turns, as for in-guest LiteInst. Without one it
        // writes them to the guest's standard error.
        if let Some(socket) = inheritable_in_guest_detlog_socket()? {
            setting(
                SABRE_TOOL_OUTPUT_ENV,
                OsStr::new(&detcore::detlog::tool_output_env_value(socket.as_raw_fd())?),
            );
            _detlog_descriptor = Some(socket);
        }
    }
    command.env_remove("SABRE_BINARY");
    command.env_remove("SABRE_PLUGIN");
    // Always collect: the statistics page is a descriptor the guest can see,
    // so creating it only when a summary or DEBUG log is requested would make
    // those flags change the guest's view. Its number travels in the launch
    // specification, and the descriptor sits above the plugin's fixed RPC
    // socket.
    let stats = reverie_sabre_stats::SabreStats::create(reverie::BackendStatsRequest::ENABLED)
        .map_err(|error| anyhow!("failed to create the SaBRe statistics page: {error}"))?
        .expect("an enabled request creates the statistics page");
    setting(
        reverie_sabre_stats::BACKEND_STATS_ENV,
        OsStr::new(&stats.raw_fd().to_string()),
    );
    command.env_remove(reverie_sabre_stats::BACKEND_STATS_ENV);
    let launch_settings: Vec<&[u8]> = launch_settings.iter().map(Vec::as_slice).collect();
    let launch_spec = reverie_sabre_stats::launch::spec_bytes(
        plugin.as_os_str().as_bytes(),
        client.as_os_str().as_bytes(),
        &launch_settings,
    )
    .ok_or_else(|| anyhow!("the SaBRe launch specification is malformed or too large"))?;
    // Kept open until the guest has started (it inherits its own copy, which
    // SaBRe's loader closes).
    let _launch_spec = reverie_sabre_stats::launch::spec_fd(&launch_spec)
        .map_err(|error| anyhow!("failed to create the SaBRe launch specification: {error}"))?;

    // THE SOCKET PATH IS DELIBERATELY RANDOM AND MUST NOT BE COMPARED.
    //
    // `tempfile` appends a random 6-character suffix to the directory above, so
    // the absolute path differs on every launch by design -- that is what keeps
    // concurrent hermit invocations on a shared host from binding the same
    // coordinator socket. Emitting it at INFO put per-launch randomness into the
    // INFO stream, which `--verify-strict` compares:
    //
    //   Mismatch at log messages 2 (run 1) and 2 (run 2):
    //   < socket=/tmp/hermit-sabre-rpc-Gw6tUi/coordinator.sock
    //   > socket=/tmp/hermit-sabre-rpc-7SZ94p/coordinator.sock
    //
    // That fails EVERY SaBRe verify cell before the guest executes an
    // instruction, for a host-side value with no guest-visible content. The
    // defect was in the oracle, not the backend.
    //
    // WHY THE VALUE IS DROPPED FROM INFO RATHER THAN MADE DETERMINISTIC: the two
    // `--verify` runs execute in SEPARATE CHILD PROCESSES, so any path stable
    // across both would have to be derived from state shared between them. That
    // trades `tempfile`'s guaranteed-fresh directory for a reused one, and a
    // run1 that dies holding the socket would then make run2 fail to bind -- a
    // real new failure mode in exchange for a cosmetic field. The path keeps its
    // randomness; only the compared stream stops carrying it.
    //
    // This is NOT a comparator relaxation. No normalization envelope is widened,
    // so any future randomness leaking into this banner still fails the
    // comparison. The full path remains available one level down.
    tracing::debug!(
        target: "hermit::sabre",
        socket = %socket_path.display(),
        "SaBRe coordinator RPC socket (per-launch temp path, deliberately not at INFO)",
    );
    tracing::info!(
        target: "hermit::sabre",
        guest = %program.display(),
        plugin = %plugin.display(),
        "launching Detcore guest through SaBRe with coordinator RPC",
    );

    let supervised = match sabre_ptrace::run(
        command.into_std_lossy(),
        PathBuf::from(&sabre),
        plugin.clone(),
        fallback_ready,
        global.clone(),
        bootstrap_launch,
        stats.clone(),
        capture_output,
    )
    .await
    {
        Ok(supervised) => supervised,
        Err(error) => {
            global.release_all_physical_process_exits();
            global.force_shutdown_with_error();
            let _ = shutdown_sabre_rpc(server_task, &global, SABRE_RPC_DISCONNECT_TIMEOUT).await;
            return Err(error);
        }
    };
    // The supervisor returns only after every tracee reached a final kernel wait status. Release
    // the root process's barrier (and any intentionally unreaped child barriers) before scheduler
    // shutdown; no guest thread remains that could race timer fast-forward here.
    global.release_all_physical_process_exits();
    // A SaBRe execution in which no guest thread ever reached the coordinator
    // is an execution in which Detcore was never loaded as a Reverie tool: the
    // guest ran on bare Linux. Treat it exactly like a failed run so the
    // scheduler is torn down here instead of blocking forever in `clean_up`
    // below, waiting for a guest thread that will never register.
    let detcore_never_engaged = !supervised.path_evidence.guest_rpc_observed;
    let requires_forced_shutdown = !supervised.status.success() || detcore_never_engaged;
    if requires_forced_shutdown {
        global.force_shutdown_with_error();
    }
    // Emit one unconditional, machine-readable per-run fact instead of a
    // free-form warning. SaBRe loads the guest interpreter before the Detcore
    // plugin, so pre-plugin loader syscalls are structurally absent from its
    // observation envelope. Versioning that fact lets scorecard consumers
    // reject old or incomplete records rather than infer coverage from silence.
    //
    // Keep this in the controller-diagnostic stream rather than writing
    // directly to the process stderr that also carries captured guest stderr.
    // WARN is Hermit's default tracing level, and `--log-file` can therefore
    // separate the unconditional controller fact from guest output without a
    // comparator exception. A missing record remains distinct from a record
    // whose counters are zero.
    let backend_evidence = sabre_backend_evidence_line(&supervised.path_evidence);
    tracing::warn!(target: "hermit::sabre", "{backend_evidence}");
    if let Some(path) = path_evidence_file {
        let mut file = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .mode(0o600)
            .open(&path)
            .map_err(|error| {
                anyhow!(
                    "failed to open SaBRe path evidence {}: {error}",
                    path.display()
                )
            })?;
        serde_json::to_writer(&mut file, &supervised.path_evidence)?;
        writeln!(file)?;
    }
    let output = Output {
        status: supervised.status.into(),
        stdout: supervised.stdout,
        stderr: supervised.stderr,
    };

    shutdown_sabre_rpc(server_task, &global, SABRE_RPC_DISCONNECT_TIMEOUT).await?;
    let mut global = Arc::try_unwrap(global).map_err(|global| {
        anyhow!(
            "SaBRe coordinator stopped with {} live RPC reference(s)",
            Arc::strong_count(&global) - 1
        )
    })?;
    if requires_forced_shutdown {
        global.cancel_internal_scheduler().await;
    }
    let dispatch_stats = backend_stats::report(
        Backend::Sabre,
        backend_stats::request(print_summary_to_json_file),
        &stats,
    );
    global
        .clean_up_with_dispatch_stats(print_summary, print_summary_to_json_file, dispatch_stats)
        .await;
    tracing::info!(
        target: "hermit::sabre::fallback",
        ptrace_fallback_sites = supervised.path_evidence.ptrace_fallback_sites,
        trusted_shared_object_sites = supervised.path_evidence.trusted_shared_object_sites,
        guest_rpc_observed = supervised.path_evidence.guest_rpc_observed,
        "SaBRe ptrace fallback completed",
    );
    if detcore_never_engaged {
        return Err(Error::new(BackendRunFailure)
            .context(sabre_uninstrumented_guest_message(&output.status)));
    }
    Ok(output)
}

#[cfg(not(feature = "sabre"))]
async fn run_sabre(
    _command: Command,
    _config: DetConfig,
    _print_summary: bool,
    _print_summary_to_json_file: &Option<PathBuf>,
    _capture_output: bool,
) -> Result<Output, Error> {
    Backend::Sabre.ensure_available()?;
    unreachable!("SaBRe availability must fail when the feature is disabled");
}

/// Classify the existing SaBRe reach evidence without collapsing "never
/// engaged" into the same zero-valued state as a fully exercised run.
#[cfg(feature = "sabre")]
fn sabre_reach_state(guest_rpc_observed: bool, ptrace_fallback_sites: usize) -> &'static str {
    match (guest_rpc_observed, ptrace_fallback_sites) {
        (false, _) => "no-detcore-reached",
        (true, 1..) => "degraded-ptrace-fallback",
        (true, 0) => "sabre-exercised",
    }
}

/// Produce the versioned SaBRe backend fact consumed by compatibility reports.
///
/// `preplugin_coverage=absent` is a property of SaBRe's launch order: the guest
/// interpreter runs before the Detcore plugin is loaded. It is recorded rather
/// than inferred from counters because the ptrace fallback cannot observe that
/// interval either.
///
/// Preserve the originating #1725 measurement as provenance for that field:
/// on its measurement host, `/bin/true` produced 3 COMMITs under SaBRe versus
/// 14 under ptrace; the 11 absent records were the loader's libc.so.6 path
/// resolution. The launch-order fact, not that host-specific count, is the
/// runtime contract.
#[cfg(feature = "sabre")]
fn sabre_backend_evidence_line(evidence: &sabre_ptrace::PathEvidence) -> String {
    format!(
        ":: Backend: sabre static rewriting + ptrace runtime; run_mode=run; \
         evidence_schema={}; preplugin_coverage=absent; ptrace_fallback_sites={}; \
         trusted_shared_object_sites={}; guest_rpc_observed={}; reach_state={}",
        evidence.schema,
        evidence.ptrace_fallback_sites,
        evidence.trusted_shared_object_sites,
        evidence.guest_rpc_observed,
        sabre_reach_state(evidence.guest_rpc_observed, evidence.ptrace_fallback_sites),
    )
}

/// Explain a SaBRe run whose guest never reached the Detcore coordinator.
///
/// `guest_rpc_observed` is set by the coordinator RPC listener the first time a
/// SaBRe-loaded guest connects. When it stays false the guest completed without
/// Detcore intercepting anything, so the run carries no determinism guarantee
/// whatsoever -- its timing, scheduling, PIDs, and clock reads all came from the
/// host. Reporting that as a successful `hermit run` would be a fail-open
/// determinism hole, so the caller turns it into a hard error and this function
/// supplies the diagnosis.
///
/// The dominant cause is a guest whose syscall sites SaBRe never rewrote. A
/// statically linked ELF is the sharp edge: it has no dynamic loader and no
/// shared library through which SaBRe could regain control, so an unrewritten
/// static client runs entirely on bare Linux with no second chance.
#[cfg(feature = "sabre")]
fn sabre_uninstrumented_guest_message(status: &ExitStatus) -> String {
    format!(
        "the SaBRe backend finished ({status:?}) without the guest ever reaching the Detcore \
         coordinator: no syscall was intercepted, so this run applied no determinization at all \
         and its result is not a Hermit guarantee. This means SaBRe rewrote no syscall site in \
         the guest -- most often a statically linked ELF, whose syscall sites SaBRe must patch \
         in the client image itself because there is no dynamic loader to intercept."
    )
}

/// Guest-physical memory available to the single-process KVM personality.
// The KVM personality is a sparse MAP_NORESERVE address space. QEMU needs room
// for its own ELF mappings in addition to the nested machine's RAM mapping.
// reverie-kvm places every guest mapping in this window, even a PROT_NONE
// reservation, so a runtime that reserves address space up front needs room
// here: node's V8 reserves 256, 128 and then 512 MiB twice, which 1 GiB could
// not hold (https://github.com/rrnewton/hermit/issues/4366). 3 GiB is the
// largest window reverie-kvm's bootstrap identity-maps. The window costs the
// host only the pages a guest touches, and mmap and munmap discard ranges of
// 2 MiB or more rather than writing zeros over them
// (https://github.com/rrnewton/reverie/pull/1059). madvise(MADV_DONTNEED)
// still writes zeros over every page, and node calls it over its 512 MiB
// reservation, so a node run peaks near 880 MB of host memory until that is
// made lazy too (https://github.com/rrnewton/hermit/issues/4366).
const KVM_GUEST_MEMORY_BYTES: usize = 3 * 1024 * 1024 * 1024;

/// Maximum `#!` interpreter indirection levels, matching the Linux kernel's
/// `BINPRM_MAX_RECURSION` limit for chained script interpreters.
const MAX_SHEBANG_DEPTH: usize = 4;

/// Resolve `#!` interpreter scripts before the reverie-kvm ELF loader runs.
///
/// The KVM ELF loader can only map ELF images, so a guest program that is
/// actually a `#!`-script (for example `/usr/local/bin/file` -> `#!/bin/bash`,
/// or `/usr/bin/pkg-config` -> `#!/usr/bin/sh`) must be rewritten to launch its
/// interpreter, exactly as the kernel's `execve(2)` `binfmt_script` handler
/// does. On success the returned image is an ELF and `argv` has the interpreter,
/// its shebang arguments, and the script path prepended in kernel order:
/// `[interp, shebang_args.., script_path, <original argv[1..]>]`.
///
/// The interpreter line is parsed with hermit's shared [`Shebang`] so the KVM
/// backend matches how the ptrace backend and recorder treat `#!`-scripts.
fn resolve_kvm_shebang(
    resolved_program: &Path,
    mut argv: Vec<String>,
) -> Result<(PathBuf, Vec<String>, Vec<u8>), Error> {
    let mut load_path = resolved_program.to_path_buf();
    let mut image = fs::read(&load_path)
        .map_err(|error| anyhow!("failed to read KVM guest executable {load_path:?}: {error}"))?;

    let mut depth = 0;
    while image.starts_with(b"#!") {
        depth += 1;
        if depth > MAX_SHEBANG_DEPTH {
            return Err(anyhow!(
                "too many levels of `#!` interpreter indirection loading {resolved_program:?}"
            ));
        }
        let (interpreter, shebang_args) = Shebang::from_buf(&image)
            .ok_or_else(|| anyhow!("malformed `#!` interpreter line in {load_path:?}"))?
            .into_parts();
        let interpreter_str = interpreter
            .to_str()
            .ok_or_else(|| anyhow!("non-UTF-8 `#!` interpreter path in {load_path:?}"))?
            .to_owned();

        // Rewrite argv in kernel order. The prior argv[0] (the script's own
        // name) is dropped on the first level; on deeper levels the previous
        // interpreter path is preserved as a positional argument, matching
        // `binfmt_script`.
        let mut rewritten = Vec::with_capacity(argv.len() + shebang_args.len() + 2);
        rewritten.push(interpreter_str);
        for arg in &shebang_args {
            rewritten.push(
                arg.to_str()
                    .ok_or_else(|| anyhow!("non-UTF-8 `#!` interpreter argument in {load_path:?}"))?
                    .to_owned(),
            );
        }
        rewritten.push(load_path.to_string_lossy().into_owned());
        rewritten.extend_from_slice(&argv[1..]);
        argv = rewritten;

        load_path = interpreter;
        image = fs::read(&load_path).map_err(|error| {
            anyhow!(
                "failed to read `#!` interpreter {load_path:?} for {resolved_program:?}: {error}"
            )
        })?;
    }

    Ok((load_path, argv, image))
}

/// The container replaces this host directory with a private, empty tmpfs, so
/// nothing beneath it is visible to the guest. Kept next to the diagnostic that
/// depends on it; `run.rs` has its own `TMP_DIR` for the mount itself.
const CONTAINER_REPLACED_TMP: &str = "/tmp";

/// Explain why a working directory that plainly exists on the host could not be
/// resolved for the guest.
///
/// ⚠️ THIS FUNCTION EXISTS BECAUSE THE BARE ERROR CAUSED A MISDIAGNOSIS, not to
/// be tidy. `fs::canonicalize` runs after the container's mounts are applied, and
/// the container bind-mounts a fresh empty tmpfs over `/tmp`
/// (`bin/hermit/container.rs`). So a cwd beneath `/tmp` is genuinely absent from
/// the guest's mount namespace and `canonicalize` returns a perfectly correct
/// `NotFound` -- for a directory the user can `ls`.
///
/// Reported 2026-08-25 as "run_kvm_ cli tests fail from a git worktree". They do
/// not. Measured across five working directories with one binary: a PLAIN
/// directory under `/tmp` with no git in it FAILS, and a linked git worktree
/// under `/home` WORKS. The only property that tracks the failure is "the cwd is
/// under /tmp"; `/tmp` itself resolves because the mount POINT exists. The git
/// hypothesis cost real investigation time, and it came from this message saying
/// only "No such file or directory".
///
/// It mattered more than a stray report because the historical landing recipe
/// placed detached worktrees below `/tmp`, exactly where this fires. Current
/// dev-hermit worktrees are created through wrkslots below `worktrees/`, but the
/// diagnostic remains necessary for arbitrary callers and older checkouts.
///
/// The message names the mechanism and both ways out. It deliberately does NOT
/// silently fall back to `/`: changing the guest's working directory out from
/// under a caller who asked for a specific one would trade a loud, correct
/// failure for a quiet, wrong success.
fn kvm_cwd_resolution_error(requested_cwd: &Path, error: &std::io::Error) -> Error {
    let under_replaced_tmp = requested_cwd.starts_with(CONTAINER_REPLACED_TMP)
        && requested_cwd != Path::new(CONTAINER_REPLACED_TMP);
    if error.kind() == std::io::ErrorKind::NotFound && under_replaced_tmp {
        anyhow!(
            "failed to resolve KVM guest working directory {:?}: {error}\n\
             \n\
             This directory may well exist on the host. The container replaces \
             {CONTAINER_REPLACED_TMP} with a private, empty tmpfs, so nothing beneath \
             {CONTAINER_REPLACED_TMP} is visible to the guest, and resolving the working \
             directory happens inside that mount namespace.\n\
             Either run from a directory outside {CONTAINER_REPLACED_TMP}, or pass \
             --workdir with a path that exists inside the guest.",
            requested_cwd
        )
    } else {
        anyhow!(
            "failed to resolve KVM guest working directory {:?}: {error}",
            requested_cwd
        )
    }
}

/// Bind KVM's raw mountinfo and fdinfo to the same filesystem namespace.
///
/// The CLI supplies IdentityGuard provenance from the completed container: the
/// namespace's row/parent order and the namespace it was captured in
/// ([`detcore_model::config::MountNamespaceIdentity`], whose kernel ID Linux
/// never reuses during a boot). The backend requires its own mount namespace
/// to be that one and keeps the producer's order, the same snapshot the
/// container mount table was built from; it does not take a second one, and
/// it refuses provenance that is not well formed (a duplicate ID, an empty
/// order, an assignment Detcore's pool would refuse, a root rewrite or table
/// row outside the order).
///
/// Two captures of one private namespace need not agree. When a host process
/// removes the directory a host mount was on, Linux detaches the mounts on that
/// dentry in every mount namespace (`vfs_rmdir`, `vfs_unlink` and rename call
/// `detach_mounts`, and `__detach_mounts` in fs/namespace.c unmounts each
/// mount on the dentry with `umount_tree`), so a copied host row can leave the
/// container between two reads although nothing propagates into it. The
/// snapshot keeps naming that row. A detached mount stays usable through what
/// still holds it (an open descriptor, a working directory, a directory
/// descriptor), and fdinfo `mnt_id` and STATX_MNT_ID still number it through
/// the order; later mountinfo views lack it and stay ordered subsets of the
/// snapshot, which Detcore's validation accepts. A kernel automount (an autofs
/// trigger reached by an ordinary path lookup) can add a row after the
/// snapshot despite the private propagation: fdinfo and statx number its new
/// ID on first observation, and a later kernel-view mountinfo that shows it is
/// not an ordered subset and is refused. The container mount table never shows
/// it.
/// A library caller that built no container and supplies no provenance gets
/// this capture; `--no-namespace` runs supply it from Hermit's own namespace
/// (`complete_container_config`). Either way Detcore renders the guest's mount
/// files from the container mount table. KVM does not permit guest namespace
/// changes.
fn prepare_kvm_mountinfo_config(
    config: &mut DetConfig,
    namespace: impl FnOnce() -> Result<detcore_model::config::MountNamespaceIdentity, Error>,
    capture: impl FnOnce() -> Result<Vec<u64>, Error>,
) -> Result<(), Error> {
    if !config.mountinfo_mount_ids_captured
        && (!config.mountinfo_mount_ids.is_empty()
            || !config.mount_id_assignment_order.is_empty()
            || config.mountinfo_mount_namespace.is_some())
    {
        anyhow::bail!("KVM mount identities have no producer-captured namespace");
    }
    if config.mountinfo_mount_ids_captured {
        let Some(producer) = config.mountinfo_mount_namespace else {
            anyhow::bail!(
                "KVM producer-captured mount identities do not name their mount namespace"
            );
        };
        // Every mount namespace has its root, so a real one never yields an
        // empty order; and Detcore's pool refuses a malformed one.
        if config.mountinfo_mount_ids.is_empty()
            || !detcore_model::procfs::mount_identity_provenance_is_well_formed(
                &config.mountinfo_mount_ids,
                &config.mount_id_assignment_order,
            )
        {
            anyhow::bail!(
                "KVM producer-captured mount identities are malformed ({} listed, {} assigned)",
                config.mountinfo_mount_ids.len(),
                config.mount_id_assignment_order.len()
            );
        }
        if let Some(missing) = config
            .mountinfo_root_rewrites
            .iter()
            .map(|rewrite| rewrite.raw_mount_id)
            .find(|id| !config.mountinfo_mount_ids.contains(id))
        {
            anyhow::bail!(
                "KVM mount root rewrite names mount {missing}, which the producer's snapshot does \
                 not have"
            );
        }
        let backend = namespace().context("failed to identify the KVM backend mount namespace")?;
        if producer != backend {
            anyhow::bail!(
                "KVM producer-captured mount identities name another mount namespace \
                 (captured in {producer:?}, backend in {backend:?})"
            );
        }
    } else {
        let order = capture().context("failed to capture the KVM backend mount namespace")?;
        config.mountinfo_mount_ids = order;
        config.mountinfo_mount_ids_captured = true;
    }
    // The container mount table Detcore renders on KVM too names mounts of
    // this same namespace: refuse one built in another.
    if let Some(table) = &config.container_mount_table
        && let Some(missing) = table
            .mounts
            .iter()
            .map(|row| row.raw_mount_id)
            .find(|id| !config.mountinfo_mount_ids.contains(id))
    {
        anyhow::bail!(
            "KVM container mount table names mount {missing}, which the backend namespace does \
             not have"
        );
    }
    // The backend now exposes the actual namespace's devices. There is no
    // synthetic 0:1 root device to translate; stat and mountinfo use the same
    // raw identities before entering Detcore's shared DevicePool.
    config.mountinfo_device_rewrites.clear();
    Ok(())
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(https://github.com/rrnewton/hermit/issues/3695): Review
// host metadata timestamps for KVM runs.
/// Whether a KVM run asks the backend for the timestamps the kernel stored
/// rather than its fixed value.
///
/// With virtual metadata, Detcore rewrites every timestamp that a stat call
/// returns to the guest, and it reads the stored host values for its own
/// checks: `set_file_times` confirms that `utimensat` stored the requested
/// mtime. KVM replaces those timestamps with a fixed value unless the run asks
/// for the stored ones. The backend refuses this setting unless Detcore owns
/// every guest thread, which `run_kvm` never opts out of. The stored values sit
/// in the guest's buffer until Detcore rewrites them, so the run must also
/// sequentialize threads: then no other guest thread runs in that interval.
fn kvm_reports_stored_metadata_timestamps(config: &DetConfig) -> bool {
    config.virtualize_metadata && config.sequentialize_threads
}

/// Dispatch a command onto the real reverie-kvm Tool runtime.
async fn run_kvm(
    command: &Command,
    mut config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    capture_output: bool,
) -> Result<Output, Error> {
    let dispatch_started = Instant::now();
    // A caller that built no container (a library caller): the KVM guest's
    // host file operations run in this process's namespace, whatever its
    // command asks (the backend applies no chroot or mount of the command),
    // so its table is this namespace's. A caller with no provenance at all
    // takes the order from the same read; malformed provenance is still
    // refused below.
    if config.container_mount_table.is_none() {
        let launch_cwd = command
            .get_current_dir()
            .map_or_else(|| Path::new(".").to_owned(), Path::to_owned);
        let (table, order) = unplanned_container_mount_table(&launch_cwd)?;
        if !config.mountinfo_mount_ids_captured
            && config.mountinfo_mount_ids.is_empty()
            && config.mount_id_assignment_order.is_empty()
            && config.mountinfo_mount_namespace.is_none()
        {
            config.mountinfo_mount_namespace = Some(current_mount_namespace()?);
            config.mountinfo_mount_ids = order;
            config.mountinfo_mount_ids_captured = true;
        }
        config.container_mount_table = Some(table);
    }
    let keep = config
        .container_mount_table
        .as_ref()
        .map(detcore_model::container_mounts::ContainerMountTable::shown_mount_ids)
        .unwrap_or_default();
    prepare_kvm_mountinfo_config(&mut config, current_mount_namespace, || {
        capture_mountinfo_identity_order_keeping(&keep)
    })?;
    let stdin = if capture_output {
        let (snapshot_reserved, snapshot) = output_backend_stdin_reservation()?;
        if snapshot_reserved {
            snapshot
        } else {
            // Public KVM output-capture callers do not pass through the CLI's
            // verify setup. Preserve their existing stdin behavior instead of
            // silently replacing it with /dev/null merely because output is
            // captured.
            reserved_kvm_stdin()?
        }
    } else {
        reserved_kvm_stdin()?
    };
    let requested_cwd = command
        .get_current_dir()
        .map(Path::to_owned)
        .unwrap_or(std::env::current_dir()?);
    let cwd = fs::canonicalize(&requested_cwd)
        .map_err(|error| kvm_cwd_resolution_error(&requested_cwd, &error))?;
    let program = command
        .get_program()
        .to_str()
        .ok_or_else(|| anyhow!("KVM guest executable path is not valid UTF-8"))?
        .to_owned();
    if !cwd.is_dir() {
        return Err(anyhow!(
            "KVM guest working directory is not a directory: {:?}",
            cwd
        ));
    }
    let resolved_program = command.find_program().map_err(|error| {
        anyhow!("failed to resolve KVM guest executable {program:?} in the guest PATH: {error}")
    })?;
    let mut argv = Vec::with_capacity(1 + command.get_args().count());
    argv.push(program.clone());
    for argument in command.get_args() {
        argv.push(
            argument
                .to_str()
                .ok_or_else(|| anyhow!("KVM guest argument is not valid UTF-8"))?
                .to_owned(),
        );
    }

    // Rewrite `#!`-scripts to their interpreter before the ELF loader sees them.
    let (_interpreter_path, argv, image) = resolve_kvm_shebang(&resolved_program, argv)?;
    // After shebang resolution the executable is the interpreter (argv[0]).
    let program = argv.first().cloned().unwrap_or(program);
    let envp = command
        .get_captured_envs()
        .into_iter()
        .map(|(key, value)| {
            let key = key
                .to_str()
                .ok_or_else(|| anyhow!("KVM guest environment key is not valid UTF-8"))?;
            let value = value
                .to_str()
                .ok_or_else(|| anyhow!("KVM guest environment value is not valid UTF-8"))?;
            Ok(format!("{key}={value}"))
        })
        .collect::<Result<Vec<_>, Error>>()?;
    tracing::info!(
        target: "hermit::kvm",
        program = %program,
        argv = ?argv,
        cwd = %cwd.display(),
        env_count = envp.len(),
        "launching guest through reverie-kvm",
    );
    let argv = argv.iter().map(String::as_str).collect::<Vec<_>>();
    let envp = envp.iter().map(String::as_str).collect::<Vec<_>>();

    let setup_started = Instant::now();
    // `prepare_backend_config` already copied `KvmBackend::capabilities()`,
    // which reports the backend's own CPUID table and that, of the madvise
    // advice with guest-visible semantics, it implements only MADV_DONTNEED.
    // KVM does not enter Hermit's UTS namespace, so Detcore must provide the
    // same synthetic identity that the namespace-backed ptrace path exposes.
    // TODO-HUMAN-REVIEW(PR-998): Review KVM UTS namespace parity.
    config.has_uts_namespace = false;
    let random_seed = config.rng_seed();
    let mut backend = reverie_kvm::KvmBackend::new_with_stdin(KVM_GUEST_MEMORY_BYTES, stdin)
        .map_err(|error| anyhow!("failed to initialize reverie-kvm: {error}"))?;
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1120): Review KVM's canonical Detcore root identity.
    backend
        .set_root_pid(detcore::ROOT_DETPID.as_raw())
        .map_err(|error| anyhow!("failed to configure KVM root PID: {error}"))?;
    // Guests see their task IDs, so number them as the ptrace reference does
    // (one extra ID per task for its tracer thread; see reverie::task_ids).
    backend
        .set_ids_per_task(reverie::task_ids::HERMIT_PTRACE_IDS_PER_TASK)
        .map_err(|error| anyhow!("failed to configure KVM task IDs: {error}"))?;
    // The ptrace launcher execs the path `find_program` resolved, so that is
    // the filename Linux records in AT_EXECFN and copies to the top of the
    // stack, including for a `#!` script whose argv now starts with the
    // interpreter.
    backend
        .install_static_elf_with_exec_context(
            &image,
            resolved_program.as_os_str(),
            &argv,
            &envp,
            &cwd,
        )
        .map_err(|error| anyhow!("failed to load KVM guest executable {program:?}: {error}"))?;
    // The loader has copied the image's segments into guest memory, so the
    // file's bytes are no longer needed. Holding them until the run ends kept
    // a second copy of the program in host memory for the whole run: 75 MB
    // for node (https://github.com/rrnewton/reverie/issues/1063).
    drop(image);
    backend
        .set_random_seed(random_seed)
        .map_err(|error| anyhow!("failed to configure KVM guest random seed: {error}"))?;
    // The KVM backend defaults to Tool-owned guest threads: CLONE_THREAD
    // workers are driven through the Detcore tool loop and their futex/CLEARTID
    // synchronization routes to Detcore, matching the golden ptrace model
    // ("follow children"). Detcore registers every cloned thread as a tool
    // thread (detcore/src/syscalls/threads.rs), so the backend must stay in
    // that mode: host-owned threads would split worker execution and futex
    // ownership across two synchronization domains, which deadlocks
    // pthread_join.
    if kvm_reports_stored_metadata_timestamps(&config) {
        backend
            .set_host_metadata_timestamps(true)
            .map_err(|error| anyhow!("failed to configure KVM metadata timestamps: {error}"))?;
    }

    let execution_started = Instant::now();
    let completion = hermit_run_kvm::run_detcore(&mut backend, config, capture_output)
        .await
        .map_err(|error| kvm_execution_error(error, None))?;
    let cleanup_started = Instant::now();
    let result =
        finish_kvm_tool_completion(completion, print_summary, print_summary_to_json_file).await;
    let teardown_started = Instant::now();
    // Drop explicitly so the host's KVM VM teardown cost remains observable.
    drop(backend);
    let teardown_finished = Instant::now();
    let (status, stdout, stderr) = result?;

    // Every field below is a host wall-clock duration. Keep this diagnostic at
    // DEBUG so it remains available for backend profiling without entering the
    // INFO stream exposed to canonical comparison. KVM's built-in verification
    // is still output/status-only; this classification does not claim KVM L2.
    tracing::debug!(
        target: "hermit::kvm",
        prepare_us = setup_started.duration_since(dispatch_started).as_micros() as u64,
        setup_us = execution_started.duration_since(setup_started).as_micros() as u64,
        execution_us = cleanup_started.duration_since(execution_started).as_micros() as u64,
        cleanup_us = teardown_started.duration_since(cleanup_started).as_micros() as u64,
        teardown_us = teardown_finished.duration_since(teardown_started).as_micros() as u64,
        lifecycle_us = teardown_finished.duration_since(dispatch_started).as_micros() as u64,
        "reverie-kvm lifecycle phase timings",
    );

    if !capture_output {
        std::io::stdout().write_all(&stdout)?;
        std::io::stderr().write_all(&stderr)?;
    }

    Ok(Output {
        status,
        stdout,
        stderr,
    })
}

// A runtime error still owns GlobalState. Finish its terminal daemon before
// returning the typed cause to the CLI's error classifier.
async fn finish_kvm_tool_completion(
    completion: reverie_kvm::ToolRunCompletion<detcore::GlobalState>,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
) -> Result<(ExitStatus, Vec<u8>, Vec<u8>), Error> {
    match completion.result {
        Ok(output) => {
            completion
                .global_state
                .clean_up(print_summary, print_summary_to_json_file)
                .await;
            Ok(output)
        }
        Err(error) => {
            let cleanup = completion
                .global_state
                .clean_up_after_backend_failure()
                .await;
            Err(kvm_execution_error(error, Some(cleanup)))
        }
    }
}

fn kvm_execution_error(
    primary: reverie_kvm::Error,
    cleanup: Option<detcore::BackendFailureCleanup>,
) -> Error {
    // The manifest runner retains the first Error line. Keep the cause there,
    // as well as the typed backend and optional scheduler errors in the chain.
    let message = format!("KVM guest execution failed: {primary}");
    let mut error = Error::new(primary).context(BackendRunFailure);
    if let Some(cleanup) = cleanup {
        if let Err(recording) = cleanup.preemption_recording {
            error = error.context(format!("partial preemption recording failed: {recording}"));
        }
        if let Err(scheduler) = cleanup.scheduler {
            error = error.context(scheduler);
        }
    }
    error.context(message)
}

#[cfg(test)]
mod kvm_failure_tests;

#[cfg(all(test, feature = "kvm-execution-tests"))]
mod kvm_execution_tests;

// TODO-HUMAN-REVIEW(PR-743): Review bounded relaunch before DBT guest execution.
#[cfg(feature = "dbt")]
fn dbt_client_thread_start_failed(status: &std::process::ExitStatus) -> bool {
    status.code() == Some(reverie_dbt::CLIENT_THREAD_START_FAILURE_EXIT_CODE)
}

/// Keys that `HERMIT_DBT_DETCONFIG` still carries although [`DetConfig`] no
/// longer has the switches they named. Each entry is the key, its fixed value,
/// and the key it preceded in the old field order.
///
/// These are serialization constants. Nothing reads them back: the admitted
/// DBT standalone path decodes the variable with
/// [`detcore::decode_backend_config_json`], whose legacy reader ignores these
/// keys. The values are the only ones the deleted switches ever took on any
/// backend.
const LEGACY_DBT_DETCONFIG_KEYS: [(&str, bool, &str); 3] = [
    (
        "use_thread_local_clock_reads",
        false,
        "detect_host_clock_futex_timeouts",
    ),
    (
        "backend_serializes_fork_children",
        false,
        "backend_tracks_process_children",
    ),
    (
        "backend_dispatches_thread_tools",
        true,
        "backend_tracks_process_children",
    ),
];

/// Serialize `config` for the DBT runtime's `HERMIT_DBT_DETCONFIG` variable.
///
/// This is not plain `serde_json::to_string` because the variable sits in the
/// guest's own environment. A guest can read it through `getenv`, `printenv`
/// or `/proc/self/environ`, so its bytes are guest-visible. Two later changes
/// to [`DetConfig`] would otherwise have changed what every DBT guest
/// observes, and each is undone here:
///
/// - The backend facts moved into [`DetConfig::backend`].
///   [`detcore::to_legacy_backend_json`] writes them back out as the fifteen
///   separate keys they were, at the position the object carried them.
/// - Three always-constant switches were deleted, which would have shortened
///   the variable by 117 bytes. The keys in `LEGACY_DBT_DETCONFIG_KEYS` are
///   re-inserted at their old positions.
///
/// When the legacy keys express every backend capability exactly, the output
/// is byte-identical to what the same config produced before either change.
/// That is pinned by golden bytes in the tests below. Otherwise a versioned
/// envelope carries the exact capabilities alongside the legacy config.
///
/// Coordinated DBT startup receives the authoritative typed Config through the
/// RPC handshake. This JSON remains guest-visible and supplies the admitted
/// standalone fallback, so its representable bytes still need preservation.
/// Remove this shim only when that variable no longer reaches the guest.
#[doc(hidden)]
pub fn dbt_detconfig_json(config: &DetConfig) -> serde_json::Result<String> {
    let legacy = insert_legacy_dbt_detconfig_keys(&detcore::to_legacy_backend_json(config)?)?;
    detcore::encode_backend_config_json(legacy, config.backend)
}

/// Re-insert [`LEGACY_DBT_DETCONFIG_KEYS`] into a serialized [`DetConfig`].
///
/// The insertion works on the parsed top-level object, never on substrings,
/// so string values that happen to contain a key name cannot move it. It
/// refuses, rather than guessing, when an anchor key is missing or repeated or
/// when a legacy key is already present.
fn insert_legacy_dbt_detconfig_keys(serialized: &str) -> serde_json::Result<String> {
    use serde::de::MapAccess;
    use serde::ser::Error as _;
    use serde::ser::SerializeMap;
    use serde_json::value::RawValue;

    /// The top-level members of a JSON object in document order, each value
    /// kept as its exact serialized bytes.
    struct Members<'a>(Vec<(&'a str, &'a RawValue)>);

    impl<'de> serde::Deserialize<'de> for Members<'de> {
        fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
            struct MembersVisitor;
            impl<'de> serde::de::Visitor<'de> for MembersVisitor {
                type Value = Members<'de>;
                fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    formatter.write_str("a serialized Detcore config object")
                }
                fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
                    let mut members = Vec::new();
                    while let Some(member) = map.next_entry()? {
                        members.push(member);
                    }
                    Ok(Members(members))
                }
            }
            deserializer.deserialize_map(MembersVisitor)
        }
    }

    impl serde::Serialize for Members<'_> {
        fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            let mut map = serializer.serialize_map(None)?;
            for (key, value) in &self.0 {
                for (legacy_key, fixed_value, anchor) in LEGACY_DBT_DETCONFIG_KEYS {
                    if anchor == *key {
                        map.serialize_entry(legacy_key, &fixed_value)?;
                    }
                }
                map.serialize_entry(key, value)?;
            }
            map.end()
        }
    }

    let members: Members<'_> = serde_json::from_str(serialized)?;
    for (legacy_key, _, anchor) in LEGACY_DBT_DETCONFIG_KEYS {
        let anchors = members.0.iter().filter(|(key, _)| *key == anchor).count();
        if anchors != 1 {
            return Err(serde_json::Error::custom(format!(
                "cannot place legacy DBT config key {legacy_key:?}: its anchor key {anchor:?} \
                 appears {anchors} times, expected exactly once"
            )));
        }
        if members.0.iter().any(|(key, _)| *key == legacy_key) {
            return Err(serde_json::Error::custom(format!(
                "legacy DBT config key {legacy_key:?} is already present; it must come only \
                 from the fixed legacy table"
            )));
        }
    }
    serde_json::to_string(&members)
}

#[cfg(test)]
mod dbt_detconfig_tests {
    use std::path::PathBuf;

    use clap::Parser;
    use detcore_model::config::MountInfoRootRewrite;

    use super::Backend;
    use super::DetConfig;
    use super::dbt_detconfig_json;
    use super::insert_legacy_dbt_detconfig_keys;
    use super::prepare_backend_config;

    /// `HERMIT_DBT_DETCONFIG` bytes for each config below, captured with
    /// `serde_json::to_string` from a build of
    /// 806cf2fa38d13b806195d54aeba22d282d79f33d, which still had the legacy
    /// switches. Any later change to `DetConfig`'s serialized fields also
    /// changes this guest-visible variable, so this test is meant to fail on
    /// it and make that change a deliberate one.
    ///
    /// Two deliberate changes since that capture: the empty
    /// `fdinfo_unlisted_mount_ids` key was renamed `mount_id_assignment_order`
    /// when Detcore began numbering mount IDs in first-observation order
    /// (RECORD_VERSION 0x122), and the `proc_level` key was added by the
    /// `/proc` levels (https://github.com/rrnewton/hermit/issues/4266), whose
    /// default is `Minimal`. Every other byte is the 806cf2fa output.
    const GOLDEN: [(&str, &str); 3] = [
        (
            "default",
            include_str!("../tests/fixtures/dbt/detconfig-806cf2fa-default.json"),
        ),
        (
            "dbt",
            include_str!("../tests/fixtures/dbt/detconfig-806cf2fa-dbt.json"),
        ),
        (
            "rich_dbt",
            include_str!("../tests/fixtures/dbt/detconfig-806cf2fa-rich_dbt.json"),
        ),
    ];

    /// `Config::default()` with its three environment-backed fields
    /// (`HERMIT_EPOCH`, `HERMIT_PRNG`, `HERMIT_SCHED_SEED`) pinned, so the
    /// result does not depend on the test process's environment.
    fn env_free(epoch: &str, seed: &str) -> DetConfig {
        let mut config = DetConfig::parse_from(["hermit", epoch, seed, "--sched-seed=0"]);
        config.sched_seed = None;
        config
    }

    fn default_config() -> DetConfig {
        env_free("--epoch=2026-01-01T00:00:00Z", "--seed=0")
    }

    /// Non-default values in fields that serialize as strings, floats,
    /// options, and nested arrays. The path holds legacy anchor key names, so
    /// a substring-based insertion would put a key inside a string value.
    fn rich_config() -> DetConfig {
        let mut config = env_free("--epoch=2000-12-31T23:59:59Z", "--seed=7");
        config.rng_seed = Some(13);
        config.sched_seed = Some(11);
        config.clock_multiplier = Some(1.5);
        config.chaos = true;
        config.record_preemptions_to = Some(PathBuf::from(
            "/tmp/a,\"detect_host_clock_futex_timeouts\":true,\"backend_tracks_process_children\":{",
        ));
        config.mountinfo_root_rewrites = vec![MountInfoRootRewrite {
            raw_mount_id: 41,
            deterministic_root: b"/".to_vec(),
            raw_root_prefix: Some(b"/tmp/x".to_vec()),
            deterministic_root_prefix: Some(b"/tmp".to_vec()),
            raw_mountpoint_prefix: None,
            deterministic_mountpoint_prefix: None,
        }];
        config.stacktrace_event = vec![(3, Some(PathBuf::from("/s")))];
        config
    }

    fn cases() -> [(&'static str, DetConfig); 3] {
        [
            ("default", default_config()),
            (
                "dbt",
                prepare_backend_config(default_config(), Backend::Dbt),
            ),
            (
                "rich_dbt",
                prepare_backend_config(rich_config(), Backend::Dbt),
            ),
        ]
    }

    #[test]
    fn default_case_is_config_default_with_pinned_environment_fields() {
        let pinned = default_config();
        let from_default = DetConfig {
            epoch: pinned.epoch,
            seed: pinned.seed,
            sched_seed: pinned.sched_seed,
            ..DetConfig::default()
        };
        assert_eq!(
            serde_json::to_string(&from_default).unwrap(),
            serde_json::to_string(&pinned).unwrap()
        );
    }

    #[test]
    fn dbt_detconfig_matches_806cf2fa_bytes() {
        for ((name, mut config), (golden_name, golden)) in cases().into_iter().zip(GOLDEN) {
            // Keep the historical capability value as well as its byte oracle.
            // Current DBT descriptor facts need the versioned transport below.
            config.backend.tool_shares_guest_descriptor_table = false;
            config.backend.rediscovers_descriptors_after_exec = false;
            // These three were `Config` fields, false unless the host set them
            // for the backend, until they became stated capabilities.
            config.backend.supports_blocked_wait_signal_interruption = false;
            config.backend.delivers_preemption_timer_events = false;
            config.backend.runs_guest_code_between_injections = false;
            assert_eq!(name, golden_name);
            let encoded = dbt_detconfig_json(&config).unwrap();
            assert_eq!(
                encoded, golden,
                "HERMIT_DBT_DETCONFIG for the {name} config differs from the 806cf2fa bytes"
            );
        }
    }

    /// The DBT standalone fallback decodes the variable with
    /// `detcore::decode_backend_config_json` and falls back to its default
    /// config on a parse error. The three deleted switches' keys must be
    /// accepted and ignored, and the fifteen backend keys must decode back to
    /// the backend facts, so the runtime sees exactly the config that was
    /// encoded.
    #[test]
    fn dbt_detconfig_decodes_to_the_encoded_config() {
        for (name, config) in cases() {
            let encoded = dbt_detconfig_json(&config).unwrap();
            let decoded = detcore::decode_backend_config_json(&encoded)
                .unwrap_or_else(|error| panic!("{name}: DBT config does not decode: {error}"));
            assert_eq!(
                serde_json::to_string(&decoded).unwrap(),
                serde_json::to_string(&config).unwrap(),
                "{name}: decoded config differs from the encoded one"
            );
        }
    }

    #[test]
    fn current_dbt_uses_exact_descriptor_facts_without_host_timed_turns() {
        let config = prepare_backend_config(default_config(), Backend::Dbt);
        assert!(config.backend.tool_shares_guest_descriptor_table);
        assert!(config.backend.rediscovers_descriptors_after_exec);
        assert!(!config.backend.internal_pipe_turns_are_host_timed);
        assert!(!config.backend.loopback_pollers_yield_to_peers);
        let encoded = dbt_detconfig_json(&config).unwrap();
        assert!(detcore::from_legacy_backend_json(&encoded).is_err());
        let decoded = detcore::decode_backend_config_json(&encoded).unwrap();
        assert_eq!(decoded.backend, reverie::BackendCapabilities::DBT);
        assert_eq!(decoded.backend, config.backend);
    }

    #[test]
    fn dbt_detconfig_keeps_descriptor_facts_independent() {
        // Exercise the real CLI encoder and the admitted standalone JSON
        // reader. In particular, descriptor discovery must not silently
        // opt into host-timed turns or loopback poller yielding.
        for mask in 0_u8..16 {
            let mut config = prepare_backend_config(default_config(), Backend::Dbt);
            config.backend.tool_shares_guest_descriptor_table = mask & 1 != 0;
            config.backend.rediscovers_descriptors_after_exec = mask & 2 != 0;
            config.backend.internal_pipe_turns_are_host_timed = mask & 4 != 0;
            config.backend.loopback_pollers_yield_to_peers = mask & 8 != 0;
            let encoded = dbt_detconfig_json(&config).unwrap();
            assert_eq!(
                detcore::from_legacy_backend_json(&encoded).is_ok(),
                mask == 0 || mask == 15,
                "only coupled facts fit the legacy form: mask {mask}"
            );
            let decoded = detcore::decode_backend_config_json(&encoded).unwrap();
            assert_eq!(decoded.backend, config.backend, "capability mask {mask}");
            assert_eq!(
                serde_json::to_string(&decoded).unwrap(),
                serde_json::to_string(&config).unwrap(),
                "configuration mask {mask}"
            );
        }
    }

    #[test]
    fn representable_descriptor_facts_keep_the_current_config_bytes() {
        for discover in [false, true] {
            let mut config = prepare_backend_config(rich_config(), Backend::Dbt);
            config.backend.tool_shares_guest_descriptor_table = discover;
            config.backend.rediscovers_descriptors_after_exec = discover;
            config.backend.internal_pipe_turns_are_host_timed = discover;
            config.backend.loopback_pollers_yield_to_peers = discover;
            let legacy = detcore::to_legacy_backend_json(&config).unwrap();
            assert_eq!(
                dbt_detconfig_json(&config).unwrap(),
                insert_legacy_dbt_detconfig_keys(&legacy).unwrap()
            );
        }
    }

    #[test]
    fn versioned_dbt_config_rejects_unknown_or_malformed_envelopes() {
        let config = prepare_backend_config(default_config(), Backend::Dbt);
        let legacy =
            insert_legacy_dbt_detconfig_keys(&detcore::to_legacy_backend_json(&config).unwrap())
                .unwrap();
        let legacy_backend = detcore::from_legacy_backend_json(&legacy).unwrap().backend;
        let backend = serde_json::to_string(&config.backend).unwrap();
        let envelope = |version: &str| {
            format!(
                "{{\"dbt_config_version\":{version},\"config\":{legacy},\"backend\":{backend}}}"
            )
        };
        for version in ["0", "2", "-1", "null", "\"1\""] {
            assert!(
                detcore::decode_backend_config_json(&envelope(version)).is_err(),
                "version {version} must be refused"
            );
            let tagged_legacy =
                legacy.replacen('{', &format!("{{\"dbt_config_version\":{version},"), 1);
            assert_eq!(
                detcore::decode_backend_config_json(&tagged_legacy)
                    .unwrap()
                    .backend,
                legacy_backend,
                "an unknown legacy key must remain ignored"
            );
        }
        let duplicate = format!(
            "{{\"dbt_config_version\":1,\"dbt_config_version\":1,\"config\":{legacy},\"backend\":{backend}}}"
        );
        assert!(detcore::decode_backend_config_json(&duplicate).is_err());
        let missing_backend = format!("{{\"dbt_config_version\":1,\"config\":{legacy}}}");
        assert!(detcore::decode_backend_config_json(&missing_backend).is_err());
        let missing_config = format!("{{\"dbt_config_version\":1,\"backend\":{backend}}}");
        assert!(detcore::decode_backend_config_json(&missing_config).is_err());
        for duplicate in [
            format!("\"config\":{legacy}"),
            format!("\"backend\":{backend}"),
            "\"unknown\":true".to_owned(),
        ] {
            let json = envelope("1").replacen('{', &format!("{{{duplicate},"), 1);
            assert!(detcore::decode_backend_config_json(&json).is_err());
        }
    }

    #[test]
    fn legacy_key_insertion_refuses_a_missing_repeated_or_present_key() {
        let complete = r#"{"a":1,"detect_host_clock_futex_timeouts":false,"backend_tracks_process_children":true}"#;
        assert_eq!(
            insert_legacy_dbt_detconfig_keys(complete).unwrap(),
            concat!(
                r#"{"a":1,"use_thread_local_clock_reads":false,"#,
                r#""detect_host_clock_futex_timeouts":false,"#,
                r#""backend_serializes_fork_children":false,"#,
                r#""backend_dispatches_thread_tools":true,"#,
                r#""backend_tracks_process_children":true}"#,
            )
        );

        let missing = r#"{"detect_host_clock_futex_timeouts":false}"#;
        let error = insert_legacy_dbt_detconfig_keys(missing).unwrap_err();
        assert!(
            error.to_string().contains("appears 0 times"),
            "unexpected error: {error}"
        );

        let repeated = r#"{"detect_host_clock_futex_timeouts":false,"detect_host_clock_futex_timeouts":false,"backend_tracks_process_children":true}"#;
        let error = insert_legacy_dbt_detconfig_keys(repeated).unwrap_err();
        assert!(
            error.to_string().contains("appears 2 times"),
            "unexpected error: {error}"
        );

        let present = r#"{"use_thread_local_clock_reads":true,"detect_host_clock_futex_timeouts":false,"backend_tracks_process_children":true}"#;
        let error = insert_legacy_dbt_detconfig_keys(present).unwrap_err();
        assert!(
            error.to_string().contains("already present"),
            "unexpected error: {error}"
        );
    }
}

/// The program a DBT launch runs and the working directory it runs from, as
/// reverie-dbt's launcher finds them: a name without a `/` through `path`,
/// whose relative entries are taken from the guest's working directory;
/// `cwd` relative to `current` (the launcher's own directory) when it is
/// relative, and `current` when there is none. `None` where the search finds
/// no executable file.
#[cfg(feature = "dbt")]
fn dbt_launched_program(
    program: &std::path::Path,
    cwd: Option<&std::path::Path>,
    path: Option<&std::ffi::OsStr>,
    current: &std::path::Path,
) -> Option<(PathBuf, PathBuf)> {
    use std::os::unix::ffi::OsStrExt;
    use std::os::unix::fs::PermissionsExt;
    let cwd = cwd.map_or_else(|| current.to_owned(), |cwd| current.join(cwd));
    let spelled = if program.as_os_str().as_bytes().contains(&b'/') {
        program.to_owned()
    } else {
        std::env::split_paths(path?)
            .map(|directory| cwd.join(directory).join(program))
            .find(|candidate| {
                candidate.metadata().is_ok_and(|metadata| {
                    metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
                })
            })?
    };
    Some((spelled, cwd))
}

#[cfg(all(test, feature = "dbt"))]
mod dbt_launched_program_tests {
    use std::os::unix::fs::PermissionsExt;

    use super::*;

    fn executable(path: &std::path::Path) {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, b"").unwrap();
        std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
    }

    #[test]
    fn a_relative_path_entry_is_taken_from_the_guests_directory_not_the_launchers() {
        let root = tempfile::tempdir().unwrap();
        let launcher_dir = root.path().join("launcher");
        // A decoy where the launcher's own directory would find it, and the
        // program where the guest's directory does.
        executable(&launcher_dir.join("bin/prog"));
        executable(&root.path().join("guest/bin/prog"));
        std::fs::write(root.path().join("guest/plain"), b"").unwrap();
        let path = std::ffi::OsStr::new("bin");
        let found = |program: &str, cwd: Option<&str>| {
            dbt_launched_program(
                std::path::Path::new(program),
                cwd.map(std::path::Path::new),
                Some(path),
                &launcher_dir,
            )
        };
        let guest = root.path().join("guest");
        // An absolute and a relative working directory find the same file.
        let expected = Some((guest.join("bin/prog"), guest.clone()));
        assert_eq!(found("prog", Some(guest.to_str().unwrap())), expected);
        assert_eq!(
            found("prog", Some("../guest")),
            Some((
                launcher_dir.join("../guest/bin/prog"),
                launcher_dir.join("../guest")
            ))
        );
        // Without a working directory, the launcher's own is the guest's.
        assert_eq!(
            found("prog", None),
            Some((launcher_dir.join("bin/prog"), launcher_dir.clone()))
        );
        // A name with a `/` is not searched for.
        assert_eq!(
            found("./plain", Some(guest.to_str().unwrap())),
            Some((std::path::PathBuf::from("./plain"), guest.clone()))
        );
        // A file that is not executable is not found.
        assert_eq!(found("plain", Some(guest.to_str().unwrap())), None);
    }
}

/// Records, for a DBT run, the guest's first image: `program`, found and
/// run from `cwd` with the guest's `PATH`, `path`, as the launch finds it
/// (`dbt_launched_program`), then resolved as the kernel resolves an
/// `execve`. Both DBT launch paths call it in the container, whose root is
/// the guest's, in place of `launched_image`'s search, which takes a
/// relative `PATH` entry from the coordinator's directory rather than the
/// guest's. Where nothing is found the record stays unset, and the image's
/// own `AT_EXECFN` gives it after the exec.
#[cfg(feature = "dbt")]
pub fn record_dbt_launched_image(
    config: &mut DetConfig,
    program: &std::path::Path,
    cwd: Option<&std::path::Path>,
    path: Option<&std::ffi::OsStr>,
) {
    let exe = std::env::current_dir()
        .ok()
        .and_then(|current| dbt_launched_program(program, cwd, path, &current))
        .and_then(|(spelled, cwd)| {
            detcore::exec_record::ExecRecord::resolve(std::path::Path::new("/"), &cwd, &spelled)
        })
        .map(|record| record.exe);
    config
        .launch_image
        .get_or_insert(detcore::LaunchImage {
            exe: None,
            launcher: None,
        })
        .exe = exe;
}

/// Declares, for a DBT run through `drrun`, the image the kernel execs in the
/// guest's place: DynamoRIO's library (`lib64/release/libdynamorio.so` beside
/// `drrun`'s `bin64`), which loads the guest itself. So the kernel's `exe`
/// link and program break are DynamoRIO's (`detcore::LaunchImage::launcher`),
/// and Detcore answers the guest's brk from its own model; the runner tells
/// DynamoRIO to reserve nothing for the break
/// (`reverie_dbt::DbtRunner::tool_answers_brk`).
/// An error where that library is missing: the run would otherwise
/// declare a launcher the kernel never execs, and refuse every brk for want
/// of a record the launcher check can accept.
#[cfg(feature = "dbt")]
pub fn declare_dbt_launcher(config: &mut DetConfig, drrun: &std::path::Path) -> Result<(), Error> {
    let library = drrun
        .parent()
        .and_then(std::path::Path::parent)
        .unwrap_or(std::path::Path::new("/"))
        .join("lib64/release/libdynamorio.so");
    if !library.is_file() {
        return Err(anyhow!(
            "DynamoRIO's library is not at {} beside drrun {}",
            library.display(),
            drrun.display()
        ));
    }
    config
        .launch_image
        .get_or_insert(detcore::LaunchImage {
            exe: None,
            launcher: None,
        })
        .launcher = Some(library);
    Ok(())
}

// AUTONOMOUS-BOT-IMPLEMENTED
// TODO-HUMAN-REVIEW(PR-737): Review public DBT dispatch and child environment ownership.
/// Dispatch a command onto the Detcore-linked reverie-dbt runtime.
#[cfg(feature = "dbt")]
async fn run_dbt(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    capture_output: bool,
) -> Result<Output, Error> {
    if !config.sequentialize_threads {
        return Err(anyhow!(
            "the dbt backend requires sequentialized threads; remove \
             --no-sequentialize-threads (or --strace-only) to run under --backend dbt"
        ));
    }

    // The guest sees this string in its environment, so it keeps the encoding
    // it had before the backend facts moved into `Config::backend` and before
    // three constant switches were deleted; see `dbt_detconfig_json`. The DBT
    // runtime decodes the backend facts back out of it (here DBT's own); when
    // the string is absent or does not parse, it uses the strict default's
    // facts, which are ptrace's.
    let (drrun, client) = detcore_dbt::prepare_native_client()
        .map_err(|error| anyhow!("failed to prepare the Detcore DynamoRIO client: {error}"))?;
    let mut config = config;
    declare_dbt_launcher(&mut config, &drrun)?;
    record_dbt_launched_image(
        &mut config,
        std::path::Path::new(command.get_program()),
        command.get_current_dir(),
        command
            .get_captured_envs()
            .get(std::ffi::OsStr::new("PATH"))
            .map(std::ffi::OsString::as_os_str),
    );
    let config_json = dbt_detconfig_json(&config)
        .map_err(|error| anyhow!("failed to serialize the Detcore config for DBT: {error}"))?;
    let panic_on_unsupported_syscalls = config.panic_on_unsupported_syscalls;
    // Bind native diagnostics to the controller before guest output capture.
    let diagnostics = std::fs::File::from(
        std::io::stderr()
            .as_fd()
            .try_clone_to_owned()
            .map_err(|error| anyhow!("failed to retain DBT diagnostic stderr: {error}"))?,
    );
    let mut runner = reverie_dbt::DbtRunner::new(&drrun, &client)
        .map_err(|error| {
            anyhow!(
                "failed to configure the DynamoRIO DBT runner (drrun={}, client={}): {error}",
                drrun.display(),
                client.display()
            )
        })?
        .summary(print_summary)
        .isolated_process_group(panic_on_unsupported_syscalls)
        .tool_answers_brk(true)
        .diagnostic_file(&diagnostics)
        .map_err(|error| anyhow!("failed to configure DBT diagnostic stderr: {error}"))?;
    drop(diagnostics);
    if panic_on_unsupported_syscalls {
        runner = runner.client_argument("-panic-on-unsupported-syscalls");
    }

    let program = command.get_program().to_owned();
    let mut environment = command.get_captured_envs();
    environment.insert(detcore_dbt::DETCONFIG_ENV.into(), config_json.into());
    let guest = command.into_std_lossy();
    tracing::info!(
        target: "hermit::dbt",
        program = ?program,
        drrun = %drrun.display(),
        client = %client.display(),
        "launching guest through reverie-dbt with Detcore<DbtGuest>",
    );

    let (status, stdout, stderr, mut global) = if capture_output {
        let launch = || {
            runner.output_with_environment_and_global::<detcore::GlobalState>(
                &guest,
                &environment,
                config.clone(),
            )
        };
        let (mut output, mut global) = launch()
            .await
            .map_err(|error| anyhow!("failed to launch drrun ({}): {error}", drrun.display()))?;
        if dbt_client_thread_start_failed(&output.status) {
            tracing::warn!(
                target: "hermit::dbt",
                "DynamoRIO client thread failed before guest start; retrying once",
            );
            global.force_shutdown_with_error();
            global.cancel_internal_scheduler().await;
            global.clean_up(false, &None).await;
            (output, global) = launch().await.map_err(|error| {
                anyhow!("failed to launch drrun ({}): {error}", drrun.display())
            })?;
        }
        (output.status, output.stdout, output.stderr, global)
    } else {
        let launch = || {
            runner.status_with_environment_and_global::<detcore::GlobalState>(
                &guest,
                &environment,
                config.clone(),
            )
        };
        let (mut status, mut global) = launch()
            .await
            .map_err(|error| anyhow!("failed to launch drrun ({}): {error}", drrun.display()))?;
        if dbt_client_thread_start_failed(&status) {
            tracing::warn!(
                target: "hermit::dbt",
                "DynamoRIO client thread failed before guest start; retrying once",
            );
            global.force_shutdown_with_error();
            global.cancel_internal_scheduler().await;
            global.clean_up(false, &None).await;
            (status, global) = launch().await.map_err(|error| {
                anyhow!("failed to launch drrun ({}): {error}", drrun.display())
            })?;
        }
        (status, Vec::new(), Vec::new(), global)
    };

    if !status.success() {
        global.force_shutdown_with_error();
        global.cancel_internal_scheduler().await;
    }
    global.clean_up(print_summary, &None).await;
    Ok(Output {
        status: status.into(),
        stdout,
        stderr,
    })
}

// NOTE: A single-threaded executor is used here so that the tokio threads
// themselves wouldn't contribute non-determinism to the PID namespace. This
// could also be changed to a specific number of threads and that would be
// deterministic, but it shouldn't be based on the number of cores. When the
// thread count is based off of the number of cores in the machine, then two
// runs on different machines with a different number of cores will not be the
// same.
/// Run the given command as deterministically as possible.
///
/// # Stack size
///
/// On the ptrace backend this and the other launch functions (`run*`,
/// `record*`, `replay*`, [`HermitData::record`]) run the whole ptrace session
/// on a tracer thread of their own, with an 8 MiB stack (the main thread's
/// usual size, the default `ulimit -s`), so the calling thread needs no
/// particular stack: a recording through the library returned on a 95 KiB
/// caller thread. One tracer thread serves each calling thread for the
/// caller's lifetime, so an operation kept at a [`HermitCleanupUnconfirmed`]
/// checkpoint is resumed from the same caller. That thread stays, idle, until
/// the caller exits, and a tracing subscriber set for the calling thread only
/// does not see the session's events (a global subscriber does). The session itself, in a debug
/// build, needs about 2 MiB, mostly Detcore's syscall handler, which at
/// opt-level 0 keeps every syscall arm's temporaries in one frame
/// (<https://github.com/rrnewton/hermit/issues/4389>).
///
/// The other backends still run on the calling thread, which then needs that
/// stack itself: launching from a thread spawned with 4 MiB
/// (`std::thread::Builder::stack_size`) is recommended for them, and Rust's
/// default 2 MiB for a spawned thread or a test is not enough in a debug build
/// with the `third-party-backends` feature. Optimised builds need much less:
/// in the `validate` profile a 512 KiB thread was enough.
pub fn run(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
) -> Result<ExitStatus, Error> {
    run_with_backend(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        Backend::Ptrace,
    )
}

/// Run the given command using the selected instrumentation backend.
///
/// On ptrace it needs no particular stack from the calling thread; see [`run`].
pub fn run_with_backend(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
) -> Result<ExitStatus, Error> {
    run_with_backend_timeout(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        backend,
        None,
    )
}

/// [`run_with_backend`] with a hermit-enforced wall-clock bound on the guest.
///
/// On ptrace it needs no particular stack from the calling thread; see [`run`].
///
/// This library never installs a process alarm. Ordinary ptrace keeps the
/// original runtime, operation, failed GlobalState and backing guards when a
/// finite cleanup attempt is unconfirmed; downcast [`HermitCleanupUnconfirmed`]
/// and recover it on the original process/thread. A timeout is measured before
/// startup and is not a certificate of cleanup. `None` leaves execution
/// unbounded, but fatal cleanup attempts remain finite. Nonordinary backends
/// retain their existing cooperative timeout/drop semantics. Synchronous stalls
/// require caller-owned supervision; only the CLI owns its hard alarm fallback.
///
/// ⚠️ NOT PART OF `DetConfig`, DELIBERATELY. A wall-clock bound is host state,
/// and `DetConfig` is the determinism configuration that is serialized to disk
/// and reasoned about as guest-visible. Threading a real-time deadline through
/// it would put a nondeterministic quantity inside the structure whose entire
/// job is to exclude them.
pub fn run_with_backend_timeout(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
    timeout: Option<Duration>,
) -> Result<ExitStatus, Error> {
    if backend == Backend::Ptrace {
        let summary_path = print_summary_to_json_file.clone();
        let command = tracer_thread::SendCommand::new(command);
        return ptrace_completion::run_on_tracer_thread(timeout, move |control| async move {
            let command = command.into_inner();
            let report = SkidOvershootReport::begin(true);
            let config = prepare_backend_config(config, backend);
            let result = dispatch_backend(
                command,
                config,
                print_summary,
                &summary_path,
                backend,
                Some(control),
            )
            .await;
            report.finish(result)
        });
    }
    let skid_overshoot_report = SkidOvershootReport::begin(backend.uses_ptrace_pmu_timers());
    if backend == Backend::Kvm {
        ensure_kvm_stdin_reserved()?;
    }
    let config = prepare_backend_config(config, backend);
    let result = run_with_backend_inner(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        backend,
        timeout,
    );
    skid_overshoot_report.finish(result)
}

// TODO-HUMAN-REVIEW(PR-749): Review LiteInst backend configuration normalization.
#[doc(hidden)]
pub fn prepare_backend_config(mut config: DetConfig, backend: Backend) -> DetConfig {
    // Detcore reads what the selected backend can do, never which backend it is.
    // Each backend states its own facts in `Backend::capabilities()`.
    //
    // Three properties hold on every backend, so they are not capabilities:
    // - Guest-visible wall and monotonic clocks stay in the same global
    //   virtual-time domain as timers, sleeps, and timeout deadlines. SaBRe's
    //   per-thread execution clock does not advance when the scheduler skips
    //   global time while a thread is blocked.
    // - A forked process child runs concurrently with its parent. Detcore
    //   blocks the parent only for CLONE_VFORK.
    // - Detcore dispatches every cloned thread's syscalls through its tool.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-845): Review backend-local guest clock observations.
    // TODO-HUMAN-REVIEW(PR-1013): Review backend child process execution ordering.
    // TODO-HUMAN-REVIEW(PR-1013): Review backend thread callback coverage.
    // TODO-HUMAN-REVIEW(PR-1122): Review concurrent KVM process-child scheduling.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-3635): Review in-guest LiteInst killed-thread RPC cancellation.
    // AUTONOMOUS-BOT-IMPLEMENTED
    // TODO-HUMAN-REVIEW(PR-1152): KVM defers the vfork child spawn, so the child
    // registers its vfork barrier only after the parent posts BlockedExternalContinue. ptrace keeps
    // the parent kernel-blocked until the child registers.
    config.backend = backend_capabilities(backend);
    // Whether the run's process signals are tool-controlled is not decided
    // here: Detcore installs the backend's process signal control when the
    // backend offers it and threads are sequentialized, and reads the installed
    // control from then on.
    // Whether blocked waits decide signal interruption from the kernel's
    // signal state (https://github.com/rrnewton/hermit/issues/3146), whether a
    // `--max-timeslice` really preempts (https://github.com/rrnewton/hermit/issues/4068)
    // and whether guest code can run between two injected calls are the
    // backend's own stated facts in `config.backend`
    // (`supports_blocked_wait_signal_interruption`,
    // `delivers_preemption_timer_events`, `runs_guest_code_between_injections`),
    // not derived from its name here (audit step A9 of
    // https://github.com/rrnewton/hermit/issues/4485).
    // Only backends where Detcore treats a terminal's signals as reaching the
    // guest from the host read it (`Config::guest_may_inherit_a_terminal`, the
    // legacy bundled condition
    // `bundled_signal_facts::host_timed_signal_sources_are_physical`), so the
    // host is asked only for them.
    config.guest_may_inherit_a_terminal =
        bundled_signal_facts::host_timed_signal_sources_are_physical(&config.backend)
            && launcher_may_pass_a_terminal();
    config
}

/// Whether the guest may start with a terminal, which can make Linux send the
/// guest signals at moments the host sets, such as SIGHUP when the terminal
/// hangs up and SIGWINCH when it is resized
/// (`Config::guest_may_inherit_a_terminal`): one of this process's standard
/// descriptors, which the guest inherits, is a terminal, or this process has a
/// controlling terminal, which the guest inherits with its session because
/// neither Hermit nor Reverie starts a new session for it. When the
/// controlling terminal cannot be read, the answer is yes.
fn launcher_may_pass_a_terminal() -> bool {
    // SAFETY: isatty only inspects the descriptor.
    let standard_terminal = (0..=2).any(|fd| unsafe { libc::isatty(fd) } == 1);
    may_pass_a_terminal(standard_terminal, controlling_terminal_device())
}

/// The rule of `launcher_may_pass_a_terminal`: a terminal standard descriptor,
/// a controlling terminal (a nonzero device number), or an unknown one.
fn may_pass_a_terminal(standard_terminal: bool, controlling_terminal: Option<i64>) -> bool {
    standard_terminal || controlling_terminal != Some(0)
}

/// This process's controlling terminal as `/proc/self/stat` reports it: field
/// 7, `tty_nr`, the terminal's device number, or 0 when there is none. `None`
/// when it cannot be read.
fn controlling_terminal_device() -> Option<i64> {
    parse_controlling_terminal_device(&std::fs::read_to_string("/proc/self/stat").ok()?)
}

/// `tty_nr` from the text of a `/proc/<pid>/stat` file. The command name in
/// field 2 is parenthesized and may hold spaces and parentheses, so the fields
/// after it are counted from its last `)`: state, ppid, pgrp, session, tty_nr.
fn parse_controlling_terminal_device(stat: &str) -> Option<i64> {
    let after_command = stat.get(stat.rfind(')')? + 1..)?;
    after_command.split_ascii_whitespace().nth(4)?.parse().ok()
}

/// What the selected backend can do, as Detcore consumes it.
///
/// Backends whose crate is always linked are asked directly. The LiteInst and
/// DBT constants are checked against their crates by tests built with those
/// features; the SaBRe value has no such check. e9patch takes ptrace's.
#[doc(hidden)]
pub fn backend_capabilities(backend: Backend) -> reverie::BackendCapabilities {
    match backend {
        // e9patch is binary-rewriting preprocessing whose guest runs on the
        // ptrace backend (`hermit run --backend=e9patch` configures its run as
        // `Backend::Ptrace`), so it has ptrace's capabilities. A direct caller
        // used to get two of them turned off by this name.
        Backend::Ptrace | Backend::E9patch => {
            <reverie_ptrace::PtraceBackend as reverie::Backend>::capabilities()
        }
        Backend::Kvm => reverie_kvm::KvmBackend::capabilities(),
        // Site patching changes how a syscall reaches the Tool, not what the
        // Tool host can do, so in-guest-trap has in-guest LiteInst's capabilities.
        Backend::Liteinst | Backend::InGuestTrap => reverie::BackendCapabilities::LITEINST_IN_GUEST,
        Backend::Sabre => reverie::BackendCapabilities::SABRE,
        Backend::Dbt => reverie::BackendCapabilities::DBT,
    }
}

/// Whether `backend`'s runtime arms Reverie ptrace's PMU timer and delivers its
/// events, so `--max-timeslice` really preempts a thread that makes no syscall:
/// the backend's stated `delivers_preemption_timer_events`. Only ptrace does
/// (e9patch preprocessing runs on it). SaBRe and KVM accept a timer request and
/// deliver nothing; DBT and in-guest LiteInst refuse it.
pub fn backend_delivers_preemption_timer(backend: Backend) -> bool {
    backend_capabilities(backend).delivers_preemption_timer_events
}

/// After an in-guest LiteInst run, waits (bounded) until the watcher has
/// reported every admitted process's physical exit, then releases any exit
/// barrier still recorded. Release comes only after that drain: an exit still
/// in flight is reported first.
///
/// Every report is applied again to the run's returned state. The backend
/// lets go of its own reference to that state when the run ends, and a report
/// that arrived afterwards reached nothing; applying a report twice consumes
/// nothing the second time.
///
/// A failed drain, a fired watchdog or a refused admission fails the run:
/// it records a determinism loss, so verification refuses to compare it, and
/// returns the failure, after the barriers are released.
#[cfg(feature = "liteinst")]
async fn settle_in_guest_exits(
    exits: &in_guest_exits::InGuestExitAdmission,
    global_state: &detcore::GlobalState,
) -> Result<(), Error> {
    let drained = exits.drain(std::time::Duration::from_secs(10)).await;
    for pid in exits.reported_exits() {
        global_state.complete_physical_process_exit(pid);
    }
    global_state.release_all_physical_process_exits();
    let failure = exits.failure().or_else(|| {
        drained
            .err()
            .map(|error| format!("not every in-guest LiteInst guest exit was reported: {error:?}"))
    });
    match failure {
        Some(reason) => {
            detcore::detlog::record_determinism_loss(&reason);
            Err(Error::msg(reason))
        }
        None => Ok(()),
    }
}

// TODO-HUMAN-REVIEW(PR-736): Review reserved LiteInst runtime failure statuses.
//
// ⚠️ THIS PREDICATE AND THE CLASSIFIER MUST AGREE ABOUT THE SIGNAL BAND, AND A
// HARD-CODED RANGE CANNOT. They do NOT cover the same statuses and are not meant
// to -- see the table below for what each covers. It is the SIGNAL half that has
// to move together, because both readers derive it from the same fact. When the reserved set grew to include the signal
// band, `Exited(130)` fell outside `122..=127` and is NOT `Signaled(_, _)` --
// it is a status hermit CHOSE, not an actual signal death -- so LiteInst
// skipped the forced shutdown and `clean_up` could wait forever on
// `handle.await` (detcore/src/tool_global.rs). The reserved set had grown past
// the range this hard-coded, and nothing connected the two.
//
// Keying the SIGNAL HALF on `signal_from_exit_status` -- the same predicate
// `classify_container_result` uses -- is what stops that half drifting again: a
// change to the band moves both readers or neither.
//
// ⚠️ THE TWO PREDICATES ARE NOT THE SAME SET, AND AN EARLIER VERSION OF THIS
// COMMENT SAID THEY WERE. agent(hermit-007)'s codex lane caught the overclaim.
// They deliberately differ:
//
//   this predicate            122..=127        +  signal band  +  real Signaled
//   classify_container_result 122 + 123 + 124  +  signal band
//
// 123..=127 force a LiteInst shutdown and are classified as NEITHER a refusal
// nor a signal death. The classifier gives 123 and 124 arms of their own -- 123
// is the log byte cap (hermit's `--max-log-bytes`, and the value reserved for
// safehermit's log-cap kill, which still exits 125), 124 a deadline hermit
// enforced -- while 125 (hermit itself) and 126/127 (exec-level) have none, and
// each has a different producer. Only the signal half is shared, and only the
// signal half is protected from drift. Do not "simplify" this by assuming the
// two agree; the `122..=127` bound here is still hard-coded and still needs a
// human to widen it if the reserved set grows again.
#[cfg(feature = "liteinst")]
fn liteinst_requires_forced_shutdown(status: ExitStatus) -> bool {
    match status {
        // A real signal death: the process never chose a status at all.
        ExitStatus::Signaled(_, _) | ExitStatus::SignaledRaw(_, _) => true,
        ExitStatus::Exited(code) => {
            // The 122..=127 reserved band, plus any status hermit emits to mean
            // "killed by signal N".
            (122..=127).contains(&code) || detcore_model::signal_from_exit_status(code).is_some()
        }
    }
}

/// The guest outlived the wall-clock bound the caller asked hermit to enforce.
///
/// A DISTINCT TYPE, NOT A STRING, because `failure_exit_code` and
/// `classify_failure` both have to recognise it and a prose match would break
/// silently the first time the wording changed.
#[derive(Debug)]
pub struct GuestTimedOut {
    /// The bound that was exceeded, as the caller spelled it.
    pub limit: Duration,
}

impl std::fmt::Display for GuestTimedOut {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Guest exceeded the --timeout bound of {} seconds",
            self.limit.as_secs()
        )
    }
}

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

/// Nonordinary backends retain their existing cooperative timeout behavior.
/// There is no process-wide alarm in this library. A caller needing a bound on
/// synchronous stalls must own external supervision; the CLI arms its own
/// separate init-process fallback before invoking the library. Ordinary ptrace
/// uses the retained whole-operation driver instead of cancelling this future.
async fn with_run_deadline<F>(timeout: Option<Duration>, guest: F) -> Result<ExitStatus, Error>
where
    F: std::future::Future<Output = Result<ExitStatus, Error>>,
{
    match timeout {
        None => guest.await,
        Some(limit) => match tokio::time::timeout(limit, guest).await {
            Ok(result) => result,
            Err(_) => Err(Error::new(GuestTimedOut { limit })),
        },
    }
}

#[tokio::main(flavor = "current_thread")]
async fn run_with_backend_inner(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
    timeout: Option<Duration>,
) -> Result<ExitStatus, Error> {
    refuse_in_guest_liteinst_timeout(backend, timeout)?;
    // Keep the large backend future off the container supervisor's stack
    // before the deadline and Tokio wrappers capture it. Polling and dropping
    // stay inline, including the backend teardown on timeout.
    let guest = Box::pin(dispatch_backend(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        backend,
        None,
    ));
    with_run_deadline(timeout, guest).await
}

/// The guest's first image, as its `/proc/self/exe` link names it
/// (`detcore::Config::launch_image`): the program the launch executes, found
/// as the launch finds it, then resolved as the kernel resolves an `execve`
/// (`detcore::exec_record::ExecRecord::resolve`). Read in the container,
/// whose root is the guest's. A launch through a launcher names it itself
/// (`run_sabre`). `None` when it cannot be found, which leaves the link to the
/// kernel, and the launch reports the missing program itself.
fn launched_image(command: &Command) -> Option<detcore::LaunchImage> {
    let program = command.find_program().ok()?;
    let cwd = match command.get_current_dir() {
        Some(cwd) => cwd.to_owned(),
        None => std::env::current_dir().ok()?,
    };
    let record = detcore::exec_record::ExecRecord::resolve(Path::new("/"), &cwd, &program)?;
    Some(detcore::LaunchImage {
        exe: Some(record.exe),
        launcher: None,
    })
}

async fn dispatch_backend(
    command: Command,
    mut config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
    control: Option<std::rc::Rc<ptrace_completion::Control>>,
) -> Result<ExitStatus, Error> {
    // Every launch path, ptrace's included, reaches the guest through here.
    crate::cpu_profile_admission::refuse_unadmitted_launch(&config, backend == Backend::Ptrace)?;
    config.launch_image = launched_image(&command);
    if backend == Backend::Kvm {
        return Ok(run_kvm(
            &command,
            config,
            print_summary,
            print_summary_to_json_file,
            false,
        )
        .await?
        .status);
    }
    if backend == Backend::Dbt {
        #[cfg(feature = "dbt")]
        {
            return Ok(run_dbt(command, config, print_summary, false).await?.status);
        }
        #[cfg(not(feature = "dbt"))]
        {
            backend.ensure_available()?;
            unreachable!("DBT availability must fail when the feature is disabled");
        }
    }
    if backend == Backend::Sabre {
        return Ok(run_sabre(
            command,
            config,
            print_summary,
            print_summary_to_json_file,
            false,
        )
        .await?
        .status);
    }
    if backend.is_in_guest_liteinst() {
        #[cfg(feature = "liteinst")]
        {
            let stats_request = in_guest_stats_request(backend, print_summary_to_json_file);
            let mut command = command;
            refuse_in_guest_liteinst_run(&mut command, &config, backend)?;
            let (launch, _detlog_descriptor) =
                in_guest_liteinst_launch(&mut command, &in_guest_detlog_forward_policy())?;
            let tool_data = launch.encode();
            // Every Tool image must forward on that socket, by that policy;
            // see detcore::Config::in_guest_detlog_forward_policy.
            let mut config = config;
            config.in_guest_detlog_forward_policy = _detlog_descriptor
                .is_some()
                .then(|| in_guest_detlog_forward_policy().encode());
            config.in_guest_site_patching_off = in_guest_site_patching_off(backend);
            let preload = liteinst_tool_runtime_library_path()?;
            let exits = std::sync::Arc::new(in_guest_exits::InGuestExitAdmission::default());
            let (exit_status, mut global_state, dispatch_stats, process_reports) =
                reverie_liteinst::LiteinstBackend::with_connection_admission(
                    exits.clone(),
                    async {
                        if stats_request.is_enabled() {
                            let (exit_status, global_state, source) =
                                reverie_liteinst::LiteinstBackend::run_with_preload_data_and_stats::<
                                    Detcore,
                                >(command, config, preload, tool_data)
                                .await?;
                            let dispatch_stats =
                                backend_stats::report(backend, stats_request, &source);
                            let process_reports = source.snapshot().process_reports();
                            Ok::<_, reverie::Error>((
                                exit_status,
                                global_state,
                                dispatch_stats,
                                process_reports,
                            ))
                        } else {
                            let (exit_status, global_state) =
                                reverie_liteinst::LiteinstBackend::run_with_preload_data::<Detcore>(
                                    command, config, preload, tool_data,
                                )
                                .await?;
                            Ok((exit_status, global_state, None, 0))
                        }
                    },
                )
                .await
                .inspect_err(|error| exits.fail_launch(error))?;
            let patched =
                in_guest_trap_patching_evidence(backend, dispatch_stats.as_ref(), process_reports);
            let settled = settle_in_guest_exits(&exits, &global_state).await;
            let policy_refusal = global_state.policy_refusal();
            if settled.is_err() || liteinst_requires_forced_shutdown(exit_status) {
                global_state.force_shutdown_with_error();
                global_state.cancel_internal_scheduler().await;
            }
            global_state
                .clean_up_with_dispatch_stats(
                    print_summary,
                    print_summary_to_json_file,
                    dispatch_stats,
                )
                .await;
            refuse_in_guest_policy(backend, policy_refusal)?;
            settled?;
            refuse_in_guest_trap_patching(backend, patched)?;
            return Ok(exit_status);
        }
        #[cfg(not(feature = "liteinst"))]
        {
            backend.ensure_available()?;
            unreachable!("LiteInst availability must fail when the feature is disabled");
        }
    }
    ensure_backend_dispatch(backend)?;

    let stats_request = backend_stats::request(print_summary_to_json_file);
    let control = control
        .ok_or_else(|| Error::msg("ordinary ptrace requires its retained operation owner"))?;
    // A sequentialized guest tells gdbserver not to serialize its threads;
    // see hermit_run_ptrace::spawn_tracer.
    let tracer = hermit_run_ptrace::spawn_tracer(
        command,
        config.clone(),
        config.gdbserver.then_some(config.gdbserver_port),
        config.sequentialize_threads,
        stats_request,
    )
    .await?;
    let stats_source = tracer.backend_stats();
    let (exit_status, global_state) = ptrace_completion::wait(tracer, control).await?;
    let dispatch_stats =
        backend_stats::report_if_collected(backend, stats_request, stats_source.as_ref());
    global_state
        .clean_up_with_dispatch_stats(print_summary, print_summary_to_json_file, dispatch_stats)
        .await; // Before it's dropped by this function.
    Ok(exit_status)
}

/// Variant of `run` that also captures stdout/stderr.
///
/// On ptrace it needs no particular stack from the calling thread; see [`run`].
pub fn run_with_output(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
) -> Result<Output, Error> {
    run_with_output_backend(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        Backend::Ptrace,
    )
}

/// Variant of [`run_with_backend`] that also captures stdout/stderr.
///
/// On ptrace it needs no particular stack from the calling thread; see [`run`].
pub fn run_with_output_backend(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
) -> Result<Output, Error> {
    if backend == Backend::Kvm {
        ensure_kvm_stdin_reserved()?;
    }
    run_with_output_backend_timeout(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        backend,
        None,
    )
}

/// [`run_with_output_backend`] with a hermit-enforced wall-clock bound.
///
/// See [`run_with_backend_timeout`]; the teardown semantics are identical. On
/// ptrace it needs no particular stack from the calling thread; see [`run`].
pub fn run_with_output_backend_timeout(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
    timeout: Option<Duration>,
) -> Result<Output, Error> {
    if backend == Backend::Ptrace {
        let summary_path = print_summary_to_json_file.clone();
        let command = tracer_thread::SendCommand::new(command);
        return ptrace_completion::run_on_tracer_thread(timeout, move |control| async move {
            let command = command.into_inner();
            let report = SkidOvershootReport::begin(true);
            // Box before the retained operation captures the backend state;
            // constructing and polling it still happen after admission here.
            let result = Box::pin(dispatch_output_backend(
                command,
                prepare_backend_config(config, backend),
                print_summary,
                &summary_path,
                backend,
                Some(control),
            ))
            .await;
            report.finish(result)
        });
    }
    let (output, skid_overshoots) = run_with_output_backend_timeout_and_skid_overshoots(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        backend,
        timeout,
    )?;
    if skid_overshoots > 0 {
        return Err(Error::new(SkidOvershootError::new(skid_overshoots)));
    }
    Ok(output)
}

/// Run with captured output and return the number of precise PMU timer
/// overshoots alongside it instead of refusing before a caller can inspect the
/// completed output. The `--verify` path uses this to finish and publish its
/// comparison before it refuses the overshoot-tainted result.
#[doc(hidden)]
pub fn run_with_output_backend_timeout_and_skid_overshoots(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
    timeout: Option<Duration>,
) -> Result<(Output, u64), Error> {
    if backend == Backend::Ptrace {
        let summary_path = print_summary_to_json_file.clone();
        let command = tracer_thread::SendCommand::new(command);
        return ptrace_completion::run_on_tracer_thread(timeout, move |control| async move {
            let command = command.into_inner();
            let report = SkidOvershootReport::begin(true);
            let result = Box::pin(dispatch_output_backend(
                command,
                prepare_backend_config(config, backend),
                print_summary,
                &summary_path,
                backend,
                Some(control),
            ))
            .await;
            report.finish_with_count(result)
        });
    }
    let skid_overshoot_report = SkidOvershootReport::begin(backend.uses_ptrace_pmu_timers());
    if backend == Backend::Kvm {
        // Reserve before the Tokio runtime can reuse a closed fd 0. KVM
        // verification consumes the explicit output snapshot in `run_kvm`;
        // public output-capture callers without one retain this reservation.
        ensure_kvm_stdin_reserved()?;
    }
    let config = prepare_backend_config(config, backend);
    let result = run_with_output_backend_inner(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        backend,
        timeout,
    );
    skid_overshoot_report.finish_with_count(result)
}

#[tokio::main(flavor = "current_thread")]
async fn run_with_output_backend_inner(
    command: Command,
    config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
    timeout: Option<Duration>,
) -> Result<Output, Error> {
    refuse_in_guest_liteinst_timeout(backend, timeout)?;
    // Keep the backend state off the caller's stack before Tokio and the
    // deadline future capture it. Polling and teardown stay on this thread.
    let guest = Box::pin(dispatch_output_backend(
        command,
        config,
        print_summary,
        print_summary_to_json_file,
        backend,
        None,
    ));
    match timeout {
        None => guest.await,
        Some(limit) => match tokio::time::timeout(limit, guest).await {
            Ok(result) => result,
            Err(_elapsed) => Err(Error::new(GuestTimedOut { limit })),
        },
    }
}

async fn dispatch_output_backend(
    mut command: Command,
    mut config: DetConfig,
    print_summary: bool,
    print_summary_to_json_file: &Option<PathBuf>,
    backend: Backend,
    control: Option<std::rc::Rc<ptrace_completion::Control>>,
) -> Result<Output, Error> {
    // Every launch path, ptrace's included, reaches the guest through here.
    crate::cpu_profile_admission::refuse_unadmitted_launch(&config, backend == Backend::Ptrace)?;
    config.launch_image = launched_image(&command);
    if backend == Backend::Kvm {
        return run_kvm(
            &command,
            config,
            print_summary,
            print_summary_to_json_file,
            true,
        )
        .await;
    }
    if backend == Backend::Dbt {
        #[cfg(feature = "dbt")]
        {
            return run_dbt(command, config, print_summary, true).await;
        }
        #[cfg(not(feature = "dbt"))]
        {
            backend.ensure_available()?;
            unreachable!("DBT availability must fail when the feature is disabled");
        }
    }
    if backend == Backend::Sabre {
        #[cfg(not(feature = "sabre"))]
        backend.ensure_available()?;
        command.stdin(output_backend_stdin()?);
        command.stdout(Stdio::piped());
        command.stderr(Stdio::piped());
        return run_sabre(
            command,
            config,
            print_summary,
            print_summary_to_json_file,
            true,
        )
        .await;
    }
    if backend.is_in_guest_liteinst() {
        #[cfg(feature = "liteinst")]
        {
            command.stdin(output_backend_stdin()?);
            let stats_request = in_guest_stats_request(backend, print_summary_to_json_file);
            refuse_in_guest_liteinst_run(&mut command, &config, backend)?;
            let (launch, _detlog_descriptor) =
                in_guest_liteinst_launch(&mut command, &in_guest_detlog_forward_policy())?;
            let tool_data = launch.encode();
            // Every Tool image must forward on that socket, by that policy;
            // see detcore::Config::in_guest_detlog_forward_policy.
            let mut config = config;
            config.in_guest_detlog_forward_policy = _detlog_descriptor
                .is_some()
                .then(|| in_guest_detlog_forward_policy().encode());
            config.in_guest_site_patching_off = in_guest_site_patching_off(backend);
            let preload = liteinst_tool_runtime_library_path()?;
            let exits = std::sync::Arc::new(in_guest_exits::InGuestExitAdmission::default());
            let (output, mut global_state, dispatch_stats, process_reports) =
                reverie_liteinst::LiteinstBackend::with_connection_admission(exits.clone(), async {
                    if stats_request.is_enabled() {
                        let (output, global_state, source) =
                            reverie_liteinst::LiteinstBackend::run_with_output_and_preload_data_and_stats::<
                                Detcore,
                            >(command, config, preload, tool_data)
                            .await?;
                        let dispatch_stats = backend_stats::report(backend, stats_request, &source);
                        let process_reports = source.snapshot().process_reports();
                        Ok::<_, reverie::Error>((output, global_state, dispatch_stats, process_reports))
                    } else {
                        let (output, global_state) =
                            reverie_liteinst::LiteinstBackend::run_with_output_and_preload_data::<
                                Detcore,
                            >(command, config, preload, tool_data)
                            .await?;
                        Ok((output, global_state, None, 0))
                    }
                })
                .await
                .inspect_err(|error| exits.fail_launch(error))?;
            let patched =
                in_guest_trap_patching_evidence(backend, dispatch_stats.as_ref(), process_reports);
            let settled = settle_in_guest_exits(&exits, &global_state).await;
            let policy_refusal = global_state.policy_refusal();
            let output = Output {
                status: output.status.into(),
                stdout: output.stdout,
                stderr: output.stderr,
            };
            let status = output.status;
            if settled.is_err() || liteinst_requires_forced_shutdown(status) {
                global_state.force_shutdown_with_error();
                global_state.cancel_internal_scheduler().await;
            }
            global_state
                .clean_up_with_dispatch_stats(
                    print_summary,
                    print_summary_to_json_file,
                    dispatch_stats,
                )
                .await;
            refuse_in_guest_policy(backend, policy_refusal)?;
            settled?;
            refuse_in_guest_trap_patching(backend, patched)?;
            return Ok(Output {
                status,
                stdout: output.stdout,
                stderr: output.stderr,
            });
        }
        #[cfg(not(feature = "liteinst"))]
        {
            backend.ensure_available()?;
            unreachable!("LiteInst availability must fail when the feature is disabled");
        }
    }
    ensure_backend_dispatch(backend)?;

    let stats_request = backend_stats::request(print_summary_to_json_file);
    command.stdin(output_backend_stdin()?);
    command.stdout(Stdio::piped());
    command.stderr(Stdio::piped());
    let control = control
        .ok_or_else(|| Error::msg("ordinary ptrace requires its retained operation owner"))?;
    // A sequentialized guest tells gdbserver not to serialize its threads;
    // see hermit_run_ptrace::spawn_tracer.
    let tracer = hermit_run_ptrace::spawn_tracer(
        command,
        config.clone(),
        config.gdbserver.then_some(config.gdbserver_port),
        config.sequentialize_threads,
        stats_request,
    )
    .await?;
    let stats_source = tracer.backend_stats();
    let (output, global_state) = ptrace_completion::wait_with_output(tracer, control).await?;
    let dispatch_stats =
        backend_stats::report_if_collected(backend, stats_request, stats_source.as_ref());
    global_state
        .clean_up_with_dispatch_stats(print_summary, print_summary_to_json_file, dispatch_stats)
        .await;
    Ok(output)
}

/// Holds the context necessary to run high-level hermit functions.
pub struct HermitData {
    // The data directory. Defaults to `~/.cache/hermit`. Note that we shouldn't
    // expect this to exist in any of the functions that are called.
    data_dir: PathBuf,
}

fn collect_recording_ids(
    entries: impl IntoIterator<Item = io::Result<fs::DirEntry>>,
    data_dir: &Path,
    mut file_type: impl FnMut(&fs::DirEntry) -> io::Result<fs::FileType>,
) -> Result<Vec<Id>, Error> {
    let mut recordings = Vec::new();
    for entry in entries {
        let entry = entry.with_context(|| {
            format!(
                "Failed to read an entry in recording inventory {}",
                data_dir.display()
            )
        })?;
        let path = entry.path();
        if file_type(&entry)
            .with_context(|| {
                format!(
                    "Failed to inspect recording inventory entry {}",
                    path.display()
                )
            })?
            .is_dir()
            && let Some(id) = entry
                .file_name()
                .to_str()
                .and_then(|name| name.parse::<Id>().ok())
        {
            recordings.push(id);
        }
    }
    Ok(recordings)
}

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

impl HermitData {
    /// Creates an instance of `HermitData` using `~/.cache/hermit` as the data
    /// directory.
    pub fn new() -> Self {
        Self::with_dir(
            dirs::cache_dir()
                .map_or_else(|| PathBuf::from("/tmp/hermit"), |dir| dir.join("hermit")),
        )
    }

    /// Creates a `HermitData` using the given directory as the base path for
    /// storing recording data.
    pub fn with_dir<P>(data_dir: P) -> Self
    where
        P: Into<PathBuf>,
    {
        Self {
            data_dir: data_dir.into(),
        }
    }

    /// Returns the path to the data directory where recordings are stored.
    pub fn data_dir(&self) -> &Path {
        &self.data_dir
    }

    /// Records the execution of the given command, returning its `Recording`.
    ///
    /// If recording failed, then an error is returned. Note that if the command
    /// itself failed, then we still return a successful recording, but its exit
    /// status will be non-zero.
    ///
    /// This low-level API does not create a Hermit container, so it has no
    /// authenticated private-mount provenance to supply. Detcore derives mount
    /// identities from the completed guest namespace. CLI container callers
    /// with producer-owned root provenance must use
    /// [`Self::record_with_mountinfo`].
    ///
    /// The ptrace session runs on a tracer thread, so the calling thread needs no
    /// particular stack; see [`run`].
    pub fn record(&self, command: Command) -> Result<Recording, Error> {
        let store = Self {
            data_dir: self.data_dir.clone(),
        };
        let command = tracer_thread::SendCommand::new(command);
        ptrace_completion::run_on_tracer_thread(None, move |control| async move {
            let command = command.into_inner();
            let data = store.create_recording_dir()?;
            let exit_status = record_to_async(
                command,
                data.path(),
                MountProvenance::default(),
                None,
                None,
                None,
                detcore_model::cpu_profile::CpuProfile::default(),
                control,
            )
            .await?;
            store.commit_recording(data, exit_status)
        })
    }

    /// Records with mountinfo provenance captured from the active container.
    ///
    /// The ptrace session runs on a tracer thread, so the calling thread needs no
    /// particular stack; see [`run`].
    pub fn record_with_mountinfo(
        &self,
        command: Command,
        mountinfo_root_rewrites: Vec<detcore_model::config::MountInfoRootRewrite>,
    ) -> Result<Recording, Error> {
        let store = Self {
            data_dir: self.data_dir.clone(),
        };
        let command = tracer_thread::SendCommand::new(command);
        ptrace_completion::run_on_tracer_thread(None, move |control| async move {
            let command = command.into_inner();
            let data = store.create_recording_dir()?;
            let mount_ids = capture_mountinfo_identity_order()?;
            let exit_status = record_to_async(
                command,
                data.path(),
                MountProvenance {
                    root_rewrites: mountinfo_root_rewrites,
                    container_mount_table: None,
                    mount_ids: None,
                },
                Some(mount_ids),
                None,
                None,
                detcore_model::cpu_profile::CpuProfile::default(),
                control,
            )
            .await?;
            store.commit_recording(data, exit_status)
        })
    }

    /// Creates a temporary directory for a recording that has not been committed yet.
    pub fn create_recording_dir(&self) -> Result<tempfile::TempDir, Error> {
        let tmp_data_dir = self.data_dir.join("tmp");

        fs::create_dir_all(&tmp_data_dir).with_context(|| {
            format!(
                "Failed to create recording directory: {}",
                self.data_dir.display()
            )
        })?;

        Ok(tempfile::TempDir::new_in(tmp_data_dir)?)
    }

    /// Commits a completed temporary recording to the recording store.
    pub fn commit_recording(
        &self,
        data: tempfile::TempDir,
        exit_status: ExitStatus,
    ) -> Result<Recording, Error> {
        let id = Id::unique();

        // Atomically move the temporary recording to its final location.
        fs::rename(data.keep(), self.data_dir.join(id.to_string()))?;

        self.note_latest(&id, true)
            .with_context(|| format!("Failed to update {:?}", self.data_dir.join(LAST)))?;

        Ok(Recording { id, exit_status })
    }

    /// Replays the given recording ID.
    ///
    /// The ptrace session runs on a tracer thread, so the calling thread needs no
    /// particular stack; see [`run`].
    pub fn replay(&self, id: Id) -> Result<ExitStatus, Error> {
        let data = self.data_dir.join(id.to_string());
        replay_from(&data)
    }

    /// Replays the given recording ID with a gdbserver available to attach to.
    ///
    /// The ptrace session runs on a tracer thread, so the calling thread needs no
    /// particular stack; see [`run`].
    pub fn replay_with_gdbserver(&self, id: Id, port: u16) -> Result<ExitStatus, Error> {
        let data = self.data_dir.join(id.to_string());
        replay_with_gdbserver(&data, port)
    }

    /// Returns an iterator over the recordings.
    ///
    /// Use [`Self::recording_metadata`] to get more information about a recording.
    pub fn recordings(&self) -> impl Iterator<Item = Id> + use<> {
        fs::read_dir(&self.data_dir)
            .ok()
            .into_iter()
            .flatten()
            .filter_map(|entry| {
                let entry = entry.ok()?;

                if entry.file_type().ok()?.is_dir() {
                    Some(entry.file_name().to_str()?.parse::<Id>().ok()?)
                } else {
                    None
                }
            })
    }

    /// Returns all recordings, or an error if the data directory cannot be
    /// enumerated completely.
    ///
    /// A missing data directory has no recordings. Other filesystem errors are
    /// returned rather than being mistaken for an empty or partial inventory.
    pub fn try_recordings(&self) -> Result<Vec<Id>, Error> {
        let entries = match fs::read_dir(&self.data_dir) {
            Ok(entries) => entries,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
            Err(error) => {
                return Err(error).with_context(|| {
                    format!(
                        "Failed to read recording inventory in {}",
                        self.data_dir.display()
                    )
                });
            }
        };

        collect_recording_ids(entries, &self.data_dir, fs::DirEntry::file_type)
    }

    /// Returns the metadata of a recording.
    pub fn recording_metadata(&self, id: Id) -> Result<Metadata, Error> {
        Metadata::load(&self.data_dir.join(id.to_string()))
    }

    /// Deletes a recording.
    pub fn remove(&self, id: Id) -> Result<(), Error> {
        let path = self.data_dir.join(id.to_string());

        // Before deleting anything, make sure this file exists. This may not be a
        // recording if this file does not exist. A run manifest marks a
        // recording too: a run that drew an input from the host leaves only
        // that (https://github.com/rrnewton/hermit/issues/4271).
        let metadata_path = [METADATA_NAME, RUN_MANIFEST_NAME]
            .into_iter()
            .map(|name| path.join(name))
            .find(|marker| marker.exists())
            .unwrap_or_else(|| path.join(METADATA_NAME));
        let metadata = fs::metadata(&metadata_path)
            .with_context(|| format!("Failed to find {:?}", metadata_path))?;

        if !metadata.is_file() {
            return Err(anyhow!("{:?} is not a file", metadata_path));
        }

        // Do a recursive delete on the directory. Note that this does not follow
        // symlinks.
        fs::remove_dir_all(path)?;

        Ok(())
    }

    /// Returns the last recorded ID.
    pub fn last_id(&self) -> Result<Id, Error> {
        Ok(fs::read_to_string(self.data_dir.join(LAST))?.parse()?)
    }

    /// Atomically names `id` as the newest recording: `last-run` always,
    /// and `last`, which a bare `hermit replay` replays, when the user
    /// `asked` for the recording (`hermit record`, `hermit run
    /// --record-networking`) rather than a run leaving its manifest; see
    /// <https://github.com/rrnewton/hermit/issues/4271>.
    pub fn note_latest(&self, id: &Id, asked: bool) -> Result<(), Error> {
        let tmp = self.data_dir.join("tmp");
        fs::create_dir_all(&tmp)?;
        let names = if asked {
            &[LAST_RUN, LAST][..]
        } else {
            &[LAST_RUN][..]
        };
        for name in names {
            let mut file = tempfile::NamedTempFile::new_in(&tmp)?;
            write!(file, "{}", id)?;
            file.persist(self.data_dir.join(name))?;
        }
        Ok(())
    }
}

/// What a recording takes from the completed recording container's mount
/// namespace, which a caller without a Hermit container leaves empty.
#[derive(Clone, Debug, Default)]
pub struct MountProvenance {
    /// The proven roots of Hermit's own mounts
    /// (`detcore::Config::mountinfo_root_rewrites`).
    pub root_rewrites: Vec<detcore_model::config::MountInfoRootRewrite>,
    /// The guest's container mount table
    /// (`detcore::Config::container_mount_table`), which the recording carries
    /// for its replay.
    pub container_mount_table: Option<detcore_model::container_mounts::ContainerMountTable>,
    /// The namespace's identity order, read with the table
    /// (`detcore::Config::mountinfo_mount_ids`). `None` asks the recorder to
    /// capture it. A table and an order from two reads need not agree: a host
    /// process that removes a host mount's directory detaches the copy of that
    /// mount from the container too, so a row the table shows can be gone
    /// from a later read.
    pub mount_ids: Option<Vec<u64>>,
}

/// `NS_GET_MNTNS_ID`, `_IOR(NSIO, 5, __u64)` in Linux's uapi/linux/nsfs.h.
const NS_GET_MNTNS_ID: libc::c_ulong = (2 << 30) | (8 << 16) | (0xb7 << 8) | 5;

/// The calling thread's mount namespace (`DetConfig::mountinfo_mount_namespace`):
/// the kernel's namespace ID from `ioctl(NS_GET_MNTNS_ID)` on
/// `/proc/thread-self/ns/mnt`, and the descriptor's device and inode numbers.
/// The calling thread's, not the thread group leader's: a library caller's
/// thread can have a mount namespace of its own, and every capture here
/// (`/proc/thread-self/mountinfo` and `fdinfo`) reads that same thread's. A
/// kernel without the ioctl (ENOTTY or EINVAL) yields no ID; any other failure
/// is an error.
pub fn current_mount_namespace() -> Result<detcore_model::config::MountNamespaceIdentity, Error> {
    use std::os::fd::AsRawFd;
    use std::os::unix::fs::MetadataExt;
    let file = fs::File::open("/proc/thread-self/ns/mnt")
        .context("failed to open this thread's mount namespace")?;
    let metadata = file
        .metadata()
        .context("failed to read this process's mount namespace")?;
    let mut id: u64 = 0;
    // SAFETY: NS_GET_MNTNS_ID writes one u64 through the pointer, which
    // points at `id` for the duration of the call.
    let id =
        if unsafe { libc::ioctl(file.as_raw_fd(), NS_GET_MNTNS_ID, &mut id as *mut u64) } == 0 {
            Some(id)
        } else {
            let error = std::io::Error::last_os_error();
            match error.raw_os_error() {
                Some(libc::ENOTTY | libc::EINVAL) => None,
                _ => {
                    return Err(Error::new(error)
                        .context("failed to read this process's mount namespace ID"));
                }
            }
        };
    Ok(detcore_model::config::MountNamespaceIdentity {
        id,
        device: metadata.dev(),
        inode: metadata.ino(),
    })
}

/// The calling thread's mount table (`/proc/thread-self/mountinfo`): the rows
/// of the namespace [`current_mount_namespace`] names.
pub fn thread_mountinfo() -> Result<Vec<u8>, Error> {
    fs::read("/proc/thread-self/mountinfo")
        .context("Failed to read this thread's mount table (/proc/thread-self/mountinfo)")
}

/// The raw mount ID of the mount `path` reaches in the calling thread's
/// namespace, from the `fdinfo` of an `O_PATH` descriptor held on it; `None`
/// where it cannot be opened. O_PATH pins the object without read
/// permission.
fn thread_mount_id_of(path: &Path) -> Option<u64> {
    held_mount_id(&hold_path(path, false).ok()?)
}

/// An `O_PATH` descriptor held on `path` (a directory, with `directory`),
/// resolved as `chdir` and `open` resolve it: following symbolic links, and
/// a relative path from the calling thread's working directory, whose name is
/// never asked for. Holding it pins the mount it reaches, and the lookup has
/// already triggered any automount on the way.
fn hold_path(path: &Path, directory: bool) -> nix::Result<std::os::fd::OwnedFd> {
    use nix::fcntl::OFlag;
    let flags = OFlag::O_PATH
        | OFlag::O_CLOEXEC
        | if directory {
            OFlag::O_DIRECTORY
        } else {
            OFlag::empty()
        };
    nix::fcntl::open(path, flags, nix::sys::stat::Mode::empty())
}

/// The raw mount ID of the mount `held` reaches, from its
/// `/proc/thread-self/fdinfo`.
fn held_mount_id(held: &std::os::fd::OwnedFd) -> Option<u64> {
    let fdinfo = fs::read(format!("/proc/thread-self/fdinfo/{}", held.as_raw_fd())).ok()?;
    detcore_model::procfs::parse_fdinfo_mount_id(&fdinfo)
}

/// Capture the calling thread's mount namespace's raw IDs in the exact
/// row/parent order used by Detcore's canonical mountinfo mapping.
pub fn capture_mountinfo_identity_order() -> Result<Vec<u64>, Error> {
    mountinfo_identity_order_from_raw(&thread_mountinfo()?)
}

/// [`capture_mountinfo_identity_order`], keeping every row whose mount ID is
/// in `keep` (the rows a container mount table shows) whatever its name: a
/// mount Hermit installed is never dropped by the seed filter, so a table
/// showing it validates against the captured order.
pub fn capture_mountinfo_identity_order_keeping(
    keep: &std::collections::BTreeSet<u64>,
) -> Result<Vec<u64>, Error> {
    mountinfo_identity_order_keeping_from_raw(&thread_mountinfo()?, keep)
}

/// Each of [`BASE_MOUNTPOINTS`](detcore_model::container_mounts::BASE_MOUNTPOINTS)
/// that the calling thread can open, as the kernel escapes it in `mountinfo`,
/// with the mount ID of a descriptor held on it: rule 2's proof of
/// `ContainerMountTable::select`. The base mountpoints hold no byte the kernel
/// escapes (space, tab, newline or backslash), so each is its own escaped form.
pub fn base_mountpoint_mount_ids() -> Vec<(Vec<u8>, u64)> {
    detcore_model::container_mounts::BASE_MOUNTPOINTS
        .iter()
        .filter_map(|mountpoint| {
            let id = thread_mount_id_of(Path::new(mountpoint))?;
            Some((mountpoint.as_bytes().to_vec(), id))
        })
        .collect()
}

/// A container mount table and the raw identity order Detcore validates it
/// against ([`unplanned_container_mount_table`]).
pub type UnplannedMountTable = (
    detcore_model::container_mounts::ContainerMountTable,
    Vec<u64>,
);

// TODO-HUMAN-REVIEW(https://github.com/rrnewton/hermit/issues/4213): Review
// the container mount table of a run in a namespace Hermit did not make: the
// membership rule of K3a with the launch mounts in place of Hermit's
// installations.
/// The container mount table of a run whose guest runs in the calling
/// thread's own mount namespace, in which Hermit installed no mount
/// (`--no-namespace`, or a library caller that built no container, on KVM),
/// and the raw identity order Detcore validates it against, both from one
/// read of the namespace's rows. Shown are:
/// - the launch mounts: the mount holding `launch_cwd`, the guest's working
///   directory at launch, and the mount holding the host's `/tmp`, which the
///   guest shares, with their ancestors (rules 1 and 3 of
///   `ContainerMountTable::select`, as for Hermit's installations), so that
///   `df .` and `findmnt -T .` from a checkout find their row;
/// - the base mountpoints mounted here (rule 2).
///
/// Both are facts of the launch, fixed before the guest starts, as the base
/// mountpoints are. No other host mount is shown; a file on one reports its
/// nearest shown ancestor. A shown row's parent is its nearest shown
/// ancestor, as for every table.
///
/// `launch_cwd` may be relative (`.` for the inherited working directory):
/// it is resolved from the calling thread's working directory as the guest's
/// `chdir` will resolve it, without asking that directory for its name, so a
/// removed working directory does not stop a launch with an absolute
/// `--workdir`.
pub fn unplanned_container_mount_table(launch_cwd: &Path) -> Result<UnplannedMountTable, Error> {
    unplanned_container_mount_table_with(launch_cwd, thread_mountinfo)
}

/// [`unplanned_container_mount_table`], reading the mount table with `read`.
/// The launch directories and the base mountpoints are resolved and held
/// first, then the table is read, then the mount IDs are taken from the held
/// descriptors: a mount that resolving a launch directory creates (an
/// automount on its path) is already in the rows read, and one that leaves
/// afterwards is still named by them. A launch mount the read does not list
/// is refused, never dropped.
fn unplanned_container_mount_table_with(
    launch_cwd: &Path,
    read: impl FnOnce() -> Result<Vec<u8>, Error>,
) -> Result<UnplannedMountTable, Error> {
    let working = hold_path(launch_cwd, true).with_context(|| {
        format!(
            "the guest's working directory {} cannot be opened for its mount table",
            launch_cwd.display()
        )
    })?;
    // The host /tmp, which the guest shares, if there is one.
    let tmp = hold_path(Path::new("/tmp"), true).ok();
    let base = detcore_model::container_mounts::BASE_MOUNTPOINTS
        .iter()
        .filter_map(|mountpoint| Some((*mountpoint, hold_path(Path::new(mountpoint), false).ok()?)))
        .collect::<Vec<_>>();
    let raw = read()?;
    let launch = std::iter::once(&working)
        .chain(tmp.as_ref())
        .map(|held| {
            held_mount_id(held)
                .ok_or_else(|| anyhow!("a launch directory's mount ID cannot be read"))
        })
        .collect::<Result<std::collections::BTreeSet<_>, Error>>()?;
    let base = base
        .iter()
        .filter_map(|(mountpoint, held)| {
            Some((mountpoint.as_bytes().to_vec(), held_mount_id(held)?))
        })
        .collect::<Vec<_>>();
    unplanned_container_mount_table_from(&raw, &launch, &base)
}

fn unplanned_container_mount_table_from(
    raw: &[u8],
    launch: &std::collections::BTreeSet<u64>,
    base: &[(Vec<u8>, u64)],
) -> Result<UnplannedMountTable, Error> {
    use detcore_model::container_mounts::ContainerMountTable;
    let rows = detcore_model::container_mounts::parse_container_mounts(raw)
        .ok_or_else(|| anyhow!("this thread's mount table has a malformed row"))?;
    if let Some(missing) = launch
        .iter()
        .find(|id| !rows.iter().any(|row| row.raw_mount_id == **id))
    {
        anyhow::bail!(
            "the launch mount {missing} (holding the guest's working directory or /tmp) is not in \
             this thread's mount table, read after it was resolved; refusing a table that would \
             omit the mount the guest starts on"
        );
    }
    let never = rows
        .iter()
        .filter(|row| row.is_ephemeral_host_seed())
        .map(|row| row.raw_mount_id)
        .collect();
    let table = ContainerMountTable::select(&rows, launch, base, &never);
    let order = mountinfo_identity_order_keeping_from_raw(raw, &table.shown_mount_ids())?;
    Ok((table, order))
}

/// Producer provenance follows the guest mount model: ephemeral host seed rows
/// (other processes' squashfuse mounts) are not namespace members and must not
/// enter the captured identity order. Detcore drops the same rows with the same
/// filter when it captures the guest's mountinfo.
fn mountinfo_identity_order_from_raw(raw: &[u8]) -> Result<Vec<u64>, Error> {
    mountinfo_identity_order_keeping_from_raw(raw, &std::collections::BTreeSet::new())
}

/// The identity order of the mountinfo contents `raw` (seed rows dropped, as
/// `mountinfo_identity_order_from_raw` drops them), keeping a seed row whose
/// mount ID is in `keep`.
pub fn mountinfo_identity_order_keeping_from_raw(
    raw: &[u8],
    keep: &std::collections::BTreeSet<u64>,
) -> Result<Vec<u64>, Error> {
    let mut kept = Vec::with_capacity(raw.len());
    for line in raw.split_inclusive(|byte| *byte == b'\n') {
        let body = line.strip_suffix(b"\n").unwrap_or(line);
        let id = body
            .split(|byte| *byte == b' ')
            .next()
            .and_then(|field| std::str::from_utf8(field).ok()?.parse::<u64>().ok());
        if !detcore_model::procfs::is_ephemeral_host_seed_mount(body)
            || id.is_some_and(|id| keep.contains(&id))
        {
            kept.extend_from_slice(line);
        }
    }
    mountinfo_identity_order_from(&kept)
}

fn mountinfo_identity_order_from(contents: &[u8]) -> Result<Vec<u64>, Error> {
    use std::collections::BTreeSet;

    let rows = detcore_model::procfs::parse_mountinfo(contents)
        .ok_or_else(|| anyhow!("malformed mountinfo"))?;
    let mut visible = Vec::new();
    let mut parents = Vec::new();
    let mut seen = BTreeSet::new();
    for row in rows {
        seen.insert(row.raw_mount_id);
        visible.push(row.raw_mount_id);
        parents.push(row.raw_parent_id);
    }
    for parent in parents {
        if seen.insert(parent) {
            visible.push(parent);
        }
    }
    Ok(visible)
}

impl<'a> From<Option<&'a PathBuf>> for HermitData {
    fn from(data_dir: Option<&'a PathBuf>) -> Self {
        data_dir.map_or_else(Self::new, Self::with_dir)
    }
}

/// Records to an existing directory. The caller must keep its backing resources
/// alive through any [`HermitCleanupUnconfirmed`] recovery. [`HermitData::record`]
/// owns its temporary recording directory inside the retained operation.
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn record_to(command: Command, dir: &Path) -> Result<ExitStatus, Error> {
    let dir = dir.to_owned();
    let command = tracer_thread::SendCommand::new(command);
    ptrace_completion::run_on_tracer_thread(None, move |control| async move {
        let command = command.into_inner();
        record_to_async(
            command,
            &dir,
            MountProvenance::default(),
            None,
            None,
            None,
            detcore_model::cpu_profile::CpuProfile::default(),
            control,
        )
        .await
    })
}

/// Records with producer-owned mountinfo provenance. The directory-lifetime
/// obligation is the same as [`record_to`]. `local_networking` and
/// `proc_level` are stored in the recording's `metadata.json`, and a replay
/// uses that network and that `/proc` level.
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn record_to_with_mountinfo(
    command: Command,
    dir: &Path,
    mountinfo: MountProvenance,
    local_networking: bool,
    proc_level: detcore_model::config::ProcLevel,
    cpu_profile: detcore_model::cpu_profile::CpuProfile,
) -> Result<ExitStatus, Error> {
    let dir = dir.to_owned();
    let command = tracer_thread::SendCommand::new(command);
    ptrace_completion::run_on_tracer_thread(None, move |control| async move {
        let command = command.into_inner();
        let mut mountinfo = mountinfo;
        let mount_ids = match mountinfo.mount_ids.take() {
            Some(mount_ids) => mount_ids,
            None => capture_mountinfo_identity_order_keeping(
                &mountinfo
                    .container_mount_table
                    .as_ref()
                    .map(detcore_model::container_mounts::ContainerMountTable::shown_mount_ids)
                    .unwrap_or_default(),
            )?,
        };
        record_to_async(
            command,
            &dir,
            mountinfo,
            Some(mount_ids),
            Some(local_networking),
            Some(proc_level),
            cpu_profile,
            control,
        )
        .await
    })
}

// Private plumbing: Record::spawn_with_mountinfo's parameters plus the
// ptrace completion control.
#[allow(clippy::too_many_arguments)]
async fn record_to_async(
    command: Command,
    dir: &Path,
    mountinfo: MountProvenance,
    mountinfo_mount_ids: Option<Vec<u64>>,
    local_networking: Option<bool>,
    proc_level: Option<detcore_model::config::ProcLevel>,
    cpu_profile: detcore_model::cpu_profile::CpuProfile,
    control: std::rc::Rc<ptrace_completion::Control>,
) -> Result<ExitStatus, Error> {
    let report = SkidOvershootReport::begin(true);
    let result = async {
        Record::spawn_with_mountinfo(
            command,
            dir,
            mountinfo,
            mountinfo_mount_ids,
            local_networking,
            proc_level,
            cpu_profile,
        )
        .await?
        .wait(control)
        .await
    }
    .await;
    report.finish(result)
}

/// Records and captures output. Keep the directory's backing resources alive
/// through any cleanup checkpoint, as for [`record_to`].
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn record_with_output(command: Command, dir: &Path) -> Result<Output, Error> {
    let dir = dir.to_owned();
    let command = tracer_thread::SendCommand::new(command);
    ptrace_completion::run_on_tracer_thread(None, move |control| async move {
        let command = command.into_inner();
        record_with_output_async(
            command,
            &dir,
            MountProvenance::default(),
            None,
            None,
            None,
            detcore_model::cpu_profile::CpuProfile::default(),
            control,
        )
        .await
    })
}

/// Records with captured output and exact mountinfo provenance.
/// `local_networking` and `proc_level` are as for [`record_to_with_mountinfo`].
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn record_with_output_with_mountinfo(
    command: Command,
    dir: &Path,
    mountinfo: MountProvenance,
    local_networking: bool,
    proc_level: detcore_model::config::ProcLevel,
    cpu_profile: detcore_model::cpu_profile::CpuProfile,
) -> Result<Output, Error> {
    let dir = dir.to_owned();
    let command = tracer_thread::SendCommand::new(command);
    ptrace_completion::run_on_tracer_thread(None, move |control| async move {
        let command = command.into_inner();
        let mut mountinfo = mountinfo;
        let mount_ids = match mountinfo.mount_ids.take() {
            Some(mount_ids) => mount_ids,
            None => capture_mountinfo_identity_order_keeping(
                &mountinfo
                    .container_mount_table
                    .as_ref()
                    .map(detcore_model::container_mounts::ContainerMountTable::shown_mount_ids)
                    .unwrap_or_default(),
            )?,
        };
        record_with_output_async(
            command,
            &dir,
            mountinfo,
            Some(mount_ids),
            Some(local_networking),
            Some(proc_level),
            cpu_profile,
            control,
        )
        .await
    })
}

// Private plumbing: Record::spawn_with_mountinfo's parameters plus the
// ptrace completion control.
#[allow(clippy::too_many_arguments)]
async fn record_with_output_async(
    mut command: Command,
    dir: &Path,
    mountinfo: MountProvenance,
    mountinfo_mount_ids: Option<Vec<u64>>,
    local_networking: Option<bool>,
    proc_level: Option<detcore_model::config::ProcLevel>,
    cpu_profile: detcore_model::cpu_profile::CpuProfile,
    control: std::rc::Rc<ptrace_completion::Control>,
) -> Result<Output, Error> {
    let report = SkidOvershootReport::begin(true);
    command.stdin(Stdio::null());
    command.stdout(Stdio::piped());
    command.stderr(Stdio::piped());
    let result = async {
        Record::spawn_with_mountinfo(
            command,
            dir,
            mountinfo,
            mountinfo_mount_ids,
            local_networking,
            proc_level,
            cpu_profile,
        )
        .await?
        .wait_with_output(control)
        .await
    }
    .await;
    report.finish(result)
}

/// Replays an existing recording. Materialization and chroot guards remain with
/// the original runtime through any cleanup checkpoint.
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn replay_from(dir: &Path) -> Result<ExitStatus, Error> {
    replay_plain(dir, None, &[])
}

/// Replays with a gdb server.
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn replay_with_gdbserver(dir: &Path, port: u16) -> Result<ExitStatus, Error> {
    replay_plain(dir, Some(port), &[])
}

/// Replays with a gdb server and mounts inside the replay chroot.
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn replay_with_gdbserver_and_mounts(
    dir: &Path,
    port: u16,
    mounts: &[Mount],
) -> Result<ExitStatus, Error> {
    replay_plain(dir, Some(port), mounts)
}

fn replay_plain(dir: &Path, port: Option<u16>, mounts: &[Mount]) -> Result<ExitStatus, Error> {
    let dir = dir.to_owned();
    let mounts = mounts.to_vec();
    ptrace_completion::run_on_tracer_thread(None, move |control| async move {
        let report = SkidOvershootReport::begin(true);
        let result = async {
            Replay::spawn(&dir, false, port, &mounts)
                .await?
                .wait(control)
                .await
        }
        .await;
        report.finish(result)
    })
}

/// Replays with captured output.
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn replay_with_output(dir: &Path) -> Result<Output, Error> {
    replay_with_output_and_mounts(dir, &[])
}

/// Replays with captured output and mounts inside the replay chroot.
///
/// The ptrace session runs on a tracer thread, so the calling thread needs no
/// particular stack; see [`run`].
pub fn replay_with_output_and_mounts(dir: &Path, mounts: &[Mount]) -> Result<Output, Error> {
    let dir = dir.to_owned();
    let mounts = mounts.to_vec();
    ptrace_completion::run_on_tracer_thread(None, move |control| async move {
        let report = SkidOvershootReport::begin(true);
        let result = async {
            Replay::spawn(&dir, true, None, &mounts)
                .await?
                .wait_with_output(control)
                .await
        }
        .await;
        report.finish(result)
    })
}

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

    static SKID_OVERSHOOT_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    /// Runs `check_tracing_environment` against probes that report `denied`
    /// as unavailable and every other probe as available; returns the refusal,
    /// if any, and the probes in the order they ran.
    fn check_with_denied(
        namespaces: NamespaceUse,
        denied: &[CapabilityProbe],
    ) -> (Option<String>, Vec<CapabilityProbe>) {
        let mut ran = Vec::new();
        let result = check_tracing_environment(namespaces, |probe| {
            ran.push(probe);
            Ok(!denied.contains(&probe))
        });
        (result.err().map(|error| error.to_string()), ran)
    }

    /// Where unprivileged user namespaces are denied, a run that creates them
    /// is refused at the namespace probe, and a `--no-namespace` run, which
    /// never calls unshare, is not: it still passes the ptrace and seccomp
    /// probes (https://github.com/rrnewton/hermit/issues/4515).
    #[test]
    fn denied_namespaces_refuse_only_a_run_that_creates_them() {
        let denied = [CapabilityProbe::Namespaces];
        let (refusal, ran) = check_with_denied(NamespaceUse::Creates, &denied);
        assert!(
            refusal
                .as_deref()
                .is_some_and(|refusal| refusal.contains("unshare(CLONE_NEWUSER | CLONE_NEWPID)")),
            "{refusal:?}"
        );
        assert_eq!(ran, [CapabilityProbe::Namespaces]);
        let (refusal, ran) = check_with_denied(NamespaceUse::None, &denied);
        assert_eq!(refusal, None);
        assert_eq!(ran, [CapabilityProbe::Ptrace, CapabilityProbe::Seccomp]);
    }

    /// A `--no-namespace` run is still refused where ptrace or seccomp is
    /// denied: skipping the namespace probe skips nothing else.
    #[test]
    fn a_run_without_namespaces_still_needs_ptrace_and_seccomp() {
        for denied in [CapabilityProbe::Ptrace, CapabilityProbe::Seccomp] {
            let (refusal, _) = check_with_denied(NamespaceUse::None, &[denied]);
            let expected = match denied {
                CapabilityProbe::Ptrace => "PTRACE_TRACEME",
                _ => "SECCOMP_SET_MODE_FILTER",
            };
            assert!(
                refusal
                    .as_deref()
                    .is_some_and(|refusal| refusal.contains(expected)),
                "{denied:?}: {refusal:?}"
            );
        }
        let (refusal, ran) = check_with_denied(NamespaceUse::Creates, &[]);
        assert_eq!(refusal, None);
        assert_eq!(
            ran,
            [
                CapabilityProbe::Namespaces,
                CapabilityProbe::Ptrace,
                CapabilityProbe::Seccomp
            ]
        );
    }

    /// A run that drew an input from the host leaves a recording directory
    /// holding only `manifest.json`; `record rm` and `record clean` remove it
    /// as they remove a syscall recording, and still refuse a directory with
    /// neither marker (https://github.com/rrnewton/hermit/issues/4271).
    #[test]
    fn a_manifest_only_recording_can_be_removed() {
        let directory = tempfile::tempdir().unwrap();
        let data = HermitData::with_dir(directory.path());
        let id = Id::unique();
        let recording = directory.path().join(id.to_string());
        fs::create_dir_all(&recording).unwrap();
        assert!(data.remove(id).is_err());
        fs::write(recording.join(MANIFEST_NAME), "{}").unwrap();
        data.remove(id).unwrap();
        assert!(!recording.exists());
    }

    /// The launch-time capture drops ephemeral host seed rows before ordering,
    /// so a seed's raw ID never enters the provenance Detcore checks guest
    /// views against, and every retained row keeps its place.
    #[test]
    fn identity_capture_excludes_host_seed_mounts_and_keeps_order() {
        let raw = b"10 1 8:1 / / rw - ext4 /dev/root rw\n\
                    76 10 0:50 / /mnt/xarfuse/uid-1/e62a203d-seed-nspid4026531836_cgpid16161-ns-4026531832 rw - fuse.squashfuse_ll squashfuse_ll rw\n\
                    20 10 0:7 / /test rw - tmpfs tmpfs rw\n\
                    30 10 0:51 / /mnt/xarfuse/stable-release rw - fuse.squashfuse_ll squashfuse_ll rw\n";
        let order = mountinfo_identity_order_from_raw(raw).unwrap();
        assert!(!order.contains(&76), "seed mount ID captured: {order:?}");
        assert_eq!(order, [10, 20, 30, 1]);
        assert_eq!(
            mountinfo_identity_order_from(raw).unwrap(),
            [10, 76, 20, 30, 1],
            "control: without the filter the seed ID is captured"
        );
    }

    /// The namespace a test's captured provenance names.
    const NAMESPACE: detcore_model::config::MountNamespaceIdentity =
        detcore_model::config::MountNamespaceIdentity {
            id: Some(9001),
            device: 4,
            inode: 4026531840,
        };

    #[test]
    fn kvm_mountinfo_config_preserves_captured_provenance() {
        let rewrite = detcore_model::config::MountInfoRootRewrite {
            raw_mount_id: 20,
            deterministic_root: b"/".to_vec(),
            raw_root_prefix: Some(b"/private-source".to_vec()),
            deterministic_root_prefix: Some(b"/tmp".to_vec()),
            raw_mountpoint_prefix: Some(b"/private-source".to_vec()),
            deterministic_mountpoint_prefix: Some(b"/tmp".to_vec()),
        };
        let mut config = DetConfig {
            mountinfo_mount_ids: vec![10, 20, 30],
            mountinfo_mount_ids_captured: true,
            mountinfo_mount_namespace: Some(NAMESPACE),
            mount_id_assignment_order: vec![700, 701],
            mountinfo_root_rewrites: vec![rewrite],
            mountinfo_device_rewrites: vec![(libc::makedev(0, 1), libc::makedev(8, 1))],
            ..DetConfig::default()
        };
        let mut expected = config.clone();
        expected.mountinfo_device_rewrites.clear();
        let (mut namespaces, mut captures) = (0, 0);
        prepare_kvm_mountinfo_config(
            &mut config,
            || {
                namespaces += 1;
                Ok(NAMESPACE)
            },
            || {
                captures += 1;
                Ok(vec![10, 30])
            },
        )
        .unwrap();
        assert_eq!(namespaces, 1, "the backend's namespace must be checked");
        assert_eq!(
            captures, 0,
            "the producer's snapshot is kept; the namespace is not read again"
        );
        assert_eq!(
            serde_json::to_value(&config).unwrap(),
            serde_json::to_value(&expected).unwrap(),
            "root rewrites, listed and assignment orders and every other setting must remain intact"
        );
    }

    /// Provenance the backend keeps without reading the namespace again must
    /// be well formed by itself: these are the refusals a second, live capture
    /// used to supply (Codex post-facto review of dh-dt0n).
    #[test]
    fn kvm_mountinfo_config_refuses_malformed_captured_provenance() {
        let rewrite = |raw_mount_id| detcore_model::config::MountInfoRootRewrite {
            raw_mount_id,
            deterministic_root: b"/".to_vec(),
            raw_root_prefix: None,
            deterministic_root_prefix: None,
            raw_mountpoint_prefix: None,
            deterministic_mountpoint_prefix: None,
        };
        let valid = DetConfig {
            mountinfo_mount_ids: vec![10, 20, 1],
            mountinfo_mount_ids_captured: true,
            mountinfo_mount_namespace: Some(NAMESPACE),
            mount_id_assignment_order: vec![20],
            mountinfo_root_rewrites: vec![rewrite(20)],
            ..DetConfig::default()
        };
        prepare_kvm_mountinfo_config(
            &mut valid.clone(),
            || Ok(NAMESPACE),
            || panic!("captured provenance is not read again"),
        )
        .unwrap();
        let cases = [
            (
                "duplicate",
                DetConfig {
                    mountinfo_mount_ids: vec![10, 10],
                    ..valid.clone()
                },
                "malformed",
            ),
            (
                "empty",
                DetConfig {
                    mountinfo_mount_ids: vec![],
                    ..valid.clone()
                },
                "malformed",
            ),
            (
                "assigned twice",
                DetConfig {
                    mount_id_assignment_order: vec![20, 20],
                    ..valid.clone()
                },
                "malformed",
            ),
            (
                "zero assigned, unlisted",
                DetConfig {
                    mount_id_assignment_order: vec![0],
                    ..valid.clone()
                },
                "malformed",
            ),
            (
                "rewrite outside",
                DetConfig {
                    mountinfo_root_rewrites: vec![rewrite(30)],
                    ..valid.clone()
                },
                "root rewrite names mount 30",
            ),
        ];
        for (name, mut config, expected) in cases {
            let before = serde_json::to_value(&config).unwrap();
            let error = prepare_kvm_mountinfo_config(
                &mut config,
                || panic!("{name}: malformed provenance is refused before any read"),
                || panic!("{name}: captured provenance is not read again"),
            )
            .unwrap_err();
            assert!(error.to_string().contains(expected), "{name}: {error:#}");
            assert_eq!(serde_json::to_value(&config).unwrap(), before, "{name}");
        }
    }

    /// Two reads of this process's namespace name it identically, and a
    /// kernel that reports a namespace ID reports a nonzero one.
    #[test]
    fn current_mount_namespace_is_stable_and_names_a_kernel_id_where_reported() {
        let first = current_mount_namespace().unwrap();
        assert_eq!(current_mount_namespace().unwrap(), first);
        assert_ne!(first.inode, 0);
        assert_ne!(first.id, Some(0));
    }

    /// A host mount copied into the container can leave it between the
    /// producer's capture and the backend's start: Linux detaches the mounts on
    /// a dentry in every mount namespace when a host process removes that
    /// directory (`__detach_mounts`). The backend keeps the producer's order,
    /// which still names the row, and does not refuse; the order Detcore later
    /// validates guest views against is the producer's
    /// (minibeads dh-dt0n).
    #[test]
    fn kvm_mountinfo_config_keeps_a_snapshot_whose_host_row_left_the_namespace() {
        let producer = b"10 1 8:1 / / rw - ext4 /dev/root rw\n\
                         20 10 0:22 / /proc rw - proc proc rw\n\
                         30 10 0:50 / /data/a rw - tmpfs tmpfs rw\n";
        let now = b"10 1 8:1 / / rw - ext4 /dev/root rw\n\
                    20 10 0:22 / /proc rw - proc proc rw\n";
        let order = mountinfo_identity_order_from(producer).unwrap();
        let mut config = DetConfig {
            mountinfo_mount_ids: order.clone(),
            mountinfo_mount_ids_captured: true,
            mountinfo_mount_namespace: Some(NAMESPACE),
            ..DetConfig::default()
        };
        prepare_kvm_mountinfo_config(
            &mut config,
            || Ok(NAMESPACE),
            || mountinfo_identity_order_from(now),
        )
        .unwrap();
        assert_eq!(config.mountinfo_mount_ids, order);
        assert_eq!(config.mountinfo_mount_ids, [10, 20, 30, 1]);
        assert!(detcore_model::procfs::mount_ids_are_ordered_subset(
            &mountinfo_identity_order_from(now).unwrap(),
            &config.mountinfo_mount_ids
        ));
    }

    #[test]
    fn kvm_mountinfo_config_refuses_provenance_of_another_namespace() {
        let config = DetConfig {
            mountinfo_mount_ids: vec![10, 20, 1],
            mountinfo_mount_ids_captured: true,
            mountinfo_mount_namespace: Some(NAMESPACE),
            mountinfo_device_rewrites: vec![(libc::makedev(0, 1), libc::makedev(8, 1))],
            ..DetConfig::default()
        };
        // The first is a recycled nsfs inode: device and inode match, the
        // kernel's never-reused namespace ID does not.
        for backend in [
            detcore_model::config::MountNamespaceIdentity {
                id: Some(9002),
                ..NAMESPACE
            },
            detcore_model::config::MountNamespaceIdentity {
                inode: NAMESPACE.inode + 1,
                ..NAMESPACE
            },
            detcore_model::config::MountNamespaceIdentity {
                device: NAMESPACE.device + 1,
                ..NAMESPACE
            },
            detcore_model::config::MountNamespaceIdentity {
                id: None,
                ..NAMESPACE
            },
        ] {
            let mut stale = config.clone();
            let before = serde_json::to_value(&stale).unwrap();
            let error = prepare_kvm_mountinfo_config(
                &mut stale,
                || Ok(backend),
                || panic!("captured provenance is not read again"),
            )
            .unwrap_err();
            assert!(
                error.to_string().contains("name another mount namespace"),
                "{error:#}"
            );
            assert_eq!(serde_json::to_value(&stale).unwrap(), before);
        }
        let mut unnamed = DetConfig {
            mountinfo_mount_namespace: None,
            ..config.clone()
        };
        let before = serde_json::to_value(&unnamed).unwrap();
        let error = prepare_kvm_mountinfo_config(
            &mut unnamed,
            || panic!("provenance without a namespace is refused before any read"),
            || panic!("provenance without a namespace is refused before any read"),
        )
        .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("do not name their mount namespace"),
            "{error:#}"
        );
        assert_eq!(serde_json::to_value(&unnamed).unwrap(), before);
        let mut unreadable = config;
        let before = serde_json::to_value(&unreadable).unwrap();
        let error = prepare_kvm_mountinfo_config(
            &mut unreadable,
            || Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied).into()),
            || panic!("captured provenance is not read again"),
        )
        .unwrap_err();
        assert!(
            error
                .downcast_ref::<std::io::Error>()
                .is_some_and(|error| error.kind() == std::io::ErrorKind::PermissionDenied)
        );
        assert_eq!(serde_json::to_value(&unreadable).unwrap(), before);
    }

    #[test]
    fn kvm_mountinfo_config_captures_validated_ambient_namespace() {
        let contents = b"80 20 8:1 / / rw - ext4 /dev/root rw\n\
                         10 80 0:7 / /test rw - tmpfs tmpfs rw\n";
        let mut config = DetConfig::default();
        let mut expected = config.clone();
        expected.mountinfo_mount_ids = vec![80, 10, 20];
        expected.mountinfo_mount_ids_captured = true;
        prepare_kvm_mountinfo_config(
            &mut config,
            || panic!("an ambient capture has no producer namespace to compare"),
            || mountinfo_identity_order_from(contents),
        )
        .unwrap();
        assert_eq!(
            serde_json::to_value(&config).unwrap(),
            serde_json::to_value(&expected).unwrap(),
            "visible rows precede parent-only IDs and no guest setting changes"
        );
        assert!(detcore_model::procfs::mount_ids_are_ordered_subset(
            &[80, 10],
            &config.mountinfo_mount_ids
        ));
        assert!(!detcore_model::procfs::mount_ids_are_ordered_subset(
            &[10, 80],
            &config.mountinfo_mount_ids
        ));
        assert!(!detcore_model::procfs::mount_ids_are_ordered_subset(
            &[80, 99],
            &config.mountinfo_mount_ids
        ));
        assert!(!detcore_model::procfs::mount_ids_are_ordered_subset(
            &[80, 80],
            &config.mountinfo_mount_ids
        ));

        let mut empty = DetConfig::default();
        prepare_kvm_mountinfo_config(
            &mut empty,
            || panic!("an ambient capture has no producer namespace to compare"),
            || mountinfo_identity_order_from(b""),
        )
        .unwrap();
        assert!(empty.mountinfo_mount_ids.is_empty());
        assert!(empty.mountinfo_mount_ids_captured);
    }

    #[test]
    fn kvm_mountinfo_config_refuses_missing_or_malformed_capture() {
        let valid = b"10 20 8:1 / / rw - ext4 /dev/root rw\n";
        let config = DetConfig {
            mountinfo_device_rewrites: vec![(libc::makedev(0, 1), libc::makedev(8, 1))],
            ..DetConfig::default()
        };
        for contents in [
            b"malformed".as_slice(),
            b"10 20 8:1 / / rw - ext4 /dev/root rw\n10 20 8:1 / / rw - ext4 /dev/root rw\n",
            b"10 20 8:1 / / rw unknown:7 - ext4 /dev/root rw\n",
        ] {
            let mut candidate = config.clone();
            let before = serde_json::to_value(&candidate).unwrap();
            let error = prepare_kvm_mountinfo_config(
                &mut candidate,
                || panic!("an ambient capture has no producer namespace to compare"),
                || mountinfo_identity_order_from(contents),
            )
            .unwrap_err();
            assert!(format!("{error:#}").contains("malformed mountinfo"));
            assert_eq!(serde_json::to_value(&candidate).unwrap(), before);
        }
        let mut candidate = config;
        let before = serde_json::to_value(&candidate).unwrap();
        let error = prepare_kvm_mountinfo_config(
            &mut candidate,
            || panic!("an ambient capture has no producer namespace to compare"),
            || Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied).into()),
        )
        .unwrap_err();
        assert!(
            error
                .downcast_ref::<std::io::Error>()
                .is_some_and(|error| error.kind() == std::io::ErrorKind::PermissionDenied)
        );
        assert_eq!(serde_json::to_value(&candidate).unwrap(), before);
        assert_eq!(mountinfo_identity_order_from(valid).unwrap(), [10, 20]);
    }

    #[test]
    fn kvm_mountinfo_config_refuses_uncaptured_provenance_vectors() {
        for (listed, assigned, namespace) in [
            (vec![10], vec![], None),
            (vec![], vec![700], None),
            (vec![], vec![], Some(NAMESPACE)),
        ] {
            let mut config = DetConfig {
                mountinfo_mount_ids: listed,
                mount_id_assignment_order: assigned,
                mountinfo_mount_namespace: namespace,
                ..DetConfig::default()
            };
            let before = serde_json::to_value(&config).unwrap();
            let error = prepare_kvm_mountinfo_config(
                &mut config,
                || panic!("must not overwrite incomplete provenance"),
                || panic!("must not overwrite incomplete provenance"),
            )
            .unwrap_err();
            assert!(error.to_string().contains("no producer-captured namespace"));
            assert_eq!(serde_json::to_value(&config).unwrap(), before);
        }
    }

    /// The container mount table Detcore renders on KVM must name mounts of
    /// the namespace the backend captured
    /// (https://github.com/rrnewton/hermit/issues/4213, K3a).
    #[test]
    fn kvm_mountinfo_config_refuses_a_mount_table_of_another_namespace() {
        let namespace = b"10 1 8:1 / / rw - ext4 /dev/root rw\n\
                          20 10 0:22 / /proc rw - proc proc rw\n\
                          40 10 0:23 / /elsewhere rw - tmpfs tmpfs rw\n";
        let rows = detcore_model::container_mounts::parse_container_mounts(namespace).unwrap();
        let table = |installed: &[u64]| {
            detcore_model::container_mounts::ContainerMountTable::select(
                &rows,
                &installed.iter().copied().collect(),
                &[],
                &std::collections::BTreeSet::new(),
            )
        };
        let config = |table| DetConfig {
            mountinfo_mount_ids: vec![10, 20, 1],
            mountinfo_mount_ids_captured: true,
            mountinfo_mount_namespace: Some(NAMESPACE),
            container_mount_table: Some(table),
            ..DetConfig::default()
        };
        let snapshot = || -> Result<Vec<u64>, Error> { panic!("the snapshot is kept") };
        let mut matching = config(table(&[20]));
        prepare_kvm_mountinfo_config(&mut matching, || Ok(NAMESPACE), snapshot).unwrap();
        let mut stale = config(table(&[40]));
        let error =
            prepare_kvm_mountinfo_config(&mut stale, || Ok(NAMESPACE), snapshot).unwrap_err();
        assert!(error.to_string().contains("names mount 40"), "{error:#}");
    }

    /// A run in a namespace Hermit made no mount in shows only the base
    /// mountpoints mounted at their very paths, maps a file on any other
    /// mount to its nearest shown ancestor, and validates against an order
    /// read from the same bytes (https://github.com/rrnewton/hermit/issues/4213).
    #[test]
    fn an_unplanned_table_shows_only_the_base_mountpoints() {
        let namespace = b"10 1 8:1 / / rw - ext4 /dev/root rw\n\
                          20 10 0:22 / /proc rw - proc proc rw\n\
                          30 10 0:23 / /dev rw - devtmpfs dev rw\n\
                          40 10 0:24 / /data/eden rw - fuse.edenfs eden rw\n\
                          41 30 0:25 / /dev/shm rw - tmpfs tmpfs rw\n\
                          50 10 0:26 / /run/user/1000 rw - tmpfs tmpfs rw\n";
        // /run is no mount here: its descriptor reaches the root mount.
        let base = [
            (b"/".to_vec(), 10),
            (b"/proc".to_vec(), 20),
            (b"/dev".to_vec(), 30),
            (b"/dev/shm".to_vec(), 41),
            (b"/run".to_vec(), 10),
        ];
        for mountpoint in detcore_model::container_mounts::BASE_MOUNTPOINTS {
            assert!(
                !mountpoint.bytes().any(|byte| b" \t\n\\".contains(&byte)),
                "{mountpoint} needs mountinfo escaping"
            );
        }
        let no_launch = std::collections::BTreeSet::new();
        let (table, order) =
            unplanned_container_mount_table_from(namespace, &no_launch, &base).unwrap();
        assert_eq!(table.shown_mount_ids(), [10, 20, 30, 41].into());
        assert_eq!(table.shown_mount_id(40), 10);
        assert_eq!(table.shown_mount_id(50), 10);
        assert_eq!(order, [10, 20, 30, 40, 41, 50, 1]);
        let rendered = String::from_utf8(table.mountinfo()).unwrap();
        assert!(!rendered.contains("/data/eden") && !rendered.contains("/run/user"));
        // A launch from a checkout on /data/eden shows that mount too; a
        // file on the still hidden /run/user/1000 reports the root.
        let launch = [40].into();
        let (table, order) =
            unplanned_container_mount_table_from(namespace, &launch, &base).unwrap();
        assert_eq!(table.shown_mount_ids(), [10, 20, 30, 40, 41].into());
        assert_eq!(table.shown_mount_id(40), 40);
        assert_eq!(table.shown_mount_id(50), 10);
        assert_eq!(order, [10, 20, 30, 40, 41, 50, 1]);
    }

    /// Runs test `name` again under `unshare --user --map-root-user
    /// --mount`, where it may mount, and returns true in this process, which
    /// then has nothing more to check; returns false in that child, which runs
    /// the test's body. `unshare(CLONE_NEWUSER)` refuses a multithreaded
    /// process, so the test cannot enter one itself. Where user namespaces
    /// are unavailable the test is skipped, saying why.
    fn rerun_in_user_namespace(name: &str) -> bool {
        const CHILD: &str = "HERMIT_TEST_USER_NAMESPACE_CHILD";
        if std::env::var_os(CHILD).is_some() {
            return false;
        }
        let probe = std::process::Command::new("unshare")
            .args(["--user", "--map-root-user", "--mount", "true"])
            .output();
        if !probe.as_ref().is_ok_and(|probe| probe.status.success()) {
            eprintln!("SKIPPED {name}: user namespaces are unavailable here: {probe:?}");
            return true;
        }
        let output = std::process::Command::new("unshare")
            .args(["--user", "--map-root-user", "--mount", "--"])
            .arg(std::env::current_exe().unwrap())
            .args(["--exact", name, "--nocapture", "--test-threads=1"])
            .env(CHILD, "1")
            .output()
            .expect("run the test under unshare");
        let stdout = String::from_utf8_lossy(&output.stdout);
        assert!(
            output.status.success() && stdout.contains("1 passed"),
            "the child failed or ran no test:\n{stdout}\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        true
    }

    /// A library caller's thread with a mount namespace of its own captures
    /// its own namespace's rows, identity and mount IDs (all through
    /// `/proc/thread-self`), not the thread group leader's: otherwise the
    /// leader's rows and the thread's descriptor mount IDs never meet, and
    /// the table shows nothing, not even `/`. A thread may unshare its mount
    /// namespace only with CAP_SYS_ADMIN, which an unprivileged test gets in
    /// a user namespace of its own (`rerun_in_user_namespace`); there the test
    /// thread, not the leader, unshares the mount namespace.
    #[test]
    fn a_thread_with_its_own_mount_namespace_captures_its_own_view() {
        use std::os::unix::fs::MetadataExt;
        if rerun_in_user_namespace(
            "tests::a_thread_with_its_own_mount_namespace_captures_its_own_view",
        ) {
            return;
        }
        let checked = std::thread::spawn(|| {
            // SAFETY: unshare takes no pointer.
            assert_eq!(
                unsafe { libc::unshare(libc::CLONE_NEWNS) },
                0,
                "unshare(CLONE_NEWNS): {}",
                io::Error::last_os_error()
            );
            let own = current_mount_namespace().unwrap();
            let leader = fs::metadata("/proc/self/ns/mnt").unwrap();
            assert_ne!(
                own.inode,
                leader.ino(),
                "the thread kept the leader's namespace"
            );
            let (table, order) = unplanned_container_mount_table(Path::new(".")).unwrap();
            assert!(
                table
                    .mounts
                    .iter()
                    .any(|row| row.mountpoint.as_slice() == b"/"),
                "the thread's table shows no /: {table:?}"
            );
            let root = thread_mount_id_of(Path::new("/")).unwrap();
            assert!(table.shown_mount_ids().contains(&root));
            assert!(table.shown_mount_ids().iter().all(|id| order.contains(id)));
        })
        .join();
        assert!(checked.is_ok(), "the thread's checks failed");
    }

    /// The launch directory is resolved and held before the mount table is
    /// read: a mount made on it between the two (here a tmpfs over it, in a
    /// user and mount namespace of the test's own) is not the launch mount,
    /// because the held directory is below it; the mount the directory was
    /// on is, and it is in the rows read. Had the directory been resolved
    /// after the read, it would have reached the new tmpfs. The same order
    /// puts a mount that resolving the directory triggers (an automount on
    /// its path) in the rows read.
    #[test]
    fn launch_directories_are_held_before_the_mount_table_is_read() {
        if rerun_in_user_namespace(
            "tests::launch_directories_are_held_before_the_mount_table_is_read",
        ) {
            return;
        }
        let directory = tempfile::tempdir().unwrap();
        let launch = directory.path().join("launch");
        fs::create_dir(&launch).unwrap();
        let before = thread_mount_id_of(&launch).unwrap();
        let target = launch.clone();
        let (table, _) = unplanned_container_mount_table_with(&launch, move || {
            let status = std::process::Command::new("mount")
                .args(["-t", "tmpfs", "none"])
                .arg(&target)
                .status()
                .expect("run mount");
            assert!(status.success(), "mount a tmpfs over the launch directory");
            thread_mountinfo()
        })
        .unwrap();
        let over = thread_mount_id_of(&launch).unwrap();
        assert_ne!(over, before, "the tmpfs did not cover the launch directory");
        assert!(table.shown_mount_ids().contains(&before));
        assert!(!table.shown_mount_ids().contains(&over));
        let _ = std::process::Command::new("umount").arg(&launch).status();
    }

    /// A launch mount the mount table read does not list is refused, not
    /// dropped from the table.
    #[test]
    fn a_launch_mount_missing_from_the_read_is_refused() {
        let namespace = b"10 1 8:1 / / rw - ext4 /dev/root rw\n";
        let error = unplanned_container_mount_table_from(namespace, &[99].into(), &[])
            .unwrap_err()
            .to_string();
        assert!(error.contains("launch mount 99"), "{error}");
    }

    #[test]
    fn skid_overshoot_report_covers_success_error_and_disabled_backends() {
        let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
        let _ = reverie::take_skid_overshoot_count();

        reverie::record_skid_overshoot();
        let empty_run = SkidOvershootReport::begin(true);
        assert_eq!(
            reverie::take_skid_overshoot_count(),
            0,
            "begin must clear evidence from a previous invocation"
        );
        empty_run.finish(Ok::<_, Error>(())).unwrap();

        let success = SkidOvershootReport::begin(true);
        reverie::record_skid_overshoot();
        reverie::record_skid_overshoot();
        let error = success.finish(Ok::<_, Error>(7)).unwrap_err();
        assert_eq!(
            error
                .downcast_ref::<SkidOvershootError>()
                .expect("skid refusal must remain typed")
                .count(),
            2
        );
        assert!(
            error
                .to_string()
                .contains("observed 2 HERMIT_SKID_OVERSHOOT report(s)"),
            "{error:#}"
        );
        assert!(
            error
                .to_string()
                .contains("deterministic execution was not established"),
            "{error:#}"
        );
        assert_eq!(reverie::take_skid_overshoot_count(), 0);

        let error = SkidOvershootReport::begin(true);
        reverie::record_skid_overshoot();
        let error = error
            .finish(Err::<(), _>(anyhow!("guest failed")))
            .unwrap_err();
        assert!(error.downcast_ref::<SkidOvershootError>().is_some());
        let error = format!("{error:#}");
        assert!(error.contains("guest failed"), "{error}");
        assert!(
            error.contains("observed 1 HERMIT_SKID_OVERSHOOT report(s)"),
            "{error}"
        );
        assert_eq!(reverie::take_skid_overshoot_count(), 0);

        reverie::record_skid_overshoot();
        let disabled = SkidOvershootReport::begin(false);
        disabled.finish(Ok::<_, Error>(())).unwrap();
        assert_eq!(
            reverie::take_skid_overshoot_count(),
            1,
            "a backend that cannot produce ptrace PMU overshoots must not consume another run's evidence"
        );
    }

    #[test]
    fn only_ptrace_hosted_backends_consume_skid_overshoot_reports() {
        for backend in [Backend::Ptrace, Backend::E9patch] {
            assert!(backend.uses_ptrace_pmu_timers(), "{backend:?}");
        }
        for backend in [Backend::Dbt, Backend::Sabre, Backend::Kvm] {
            assert!(!backend.uses_ptrace_pmu_timers(), "{backend:?}");
        }
        // In-guest Detcore has no ptrace tracer to arm the PMU timer from.
        assert!(!Backend::Liteinst.uses_ptrace_pmu_timers());
        assert!(!Backend::InGuestTrap.uses_ptrace_pmu_timers());
    }

    #[cfg(feature = "liteinst")]
    fn write_guest_program(directory: &Path, name: &str, bytes: &[u8], mode: u32) -> PathBuf {
        let path = directory.join(name);
        fs::write(&path, bytes).unwrap();
        fs::set_permissions(&path, fs::Permissions::from_mode(mode)).unwrap();
        // The kernel may clear a set-group-ID bit that this user cannot set;
        // a test that silently lost its bit would test nothing.
        assert_eq!(
            fs::metadata(&path).unwrap().permissions().mode() & 0o7777,
            mode,
            "{}",
            path.display()
        );
        path
    }

    #[cfg(feature = "liteinst")]
    fn shebang_to(interpreter: &Path) -> Vec<u8> {
        let mut script = b"#!".to_vec();
        script.extend_from_slice(interpreter.as_os_str().as_bytes());
        script.extend_from_slice(b" -e\necho script\n");
        script
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_liteinst_runs_a_dynamically_linked_x86_64_program() {
        use interp::startup_fixtures::dynamic_executable;

        let directory = tempfile::tempdir().unwrap();
        let program = write_guest_program(
            directory.path(),
            "dynamic",
            &dynamic_executable(false),
            0o755,
        );
        assert_eq!(
            in_guest_liteinst_program_gap(&mut Command::new(&program)).unwrap(),
            None
        );
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_liteinst_refuses_programs_that_start_before_the_runtime() {
        use interp::startup_fixtures::*;

        let directory = tempfile::tempdir().unwrap();
        let cases: [(&str, Vec<u8>, &str); 4] = [
            (
                "static",
                elf(EM_X86_64, &[(PT_LOAD, b"code")]),
                "is statically linked",
            ),
            (
                "preinit",
                dynamic_executable(true),
                "has a DT_PREINIT_ARRAY",
            ),
            (
                "aarch64",
                elf(EM_AARCH64, &[(PT_INTERP, INTERP)]),
                "is not a 64-bit x86-64 ELF image",
            ),
            (
                "text",
                b"plain text, no #! line\n".to_vec(),
                "is neither an ELF executable nor a #! script",
            ),
        ];
        for (name, bytes, reason) in cases {
            let program = write_guest_program(directory.path(), name, &bytes, 0o755);
            let gap = in_guest_liteinst_program_gap(&mut Command::new(&program))
                .unwrap()
                .unwrap_or_else(|| panic!("{name} was not refused"));
            assert!(
                gap.contains(reason) && gap.contains(&program.display().to_string()),
                "{name}: {gap}"
            );
        }
    }

    /// A failure recorded during an in-guest run fails the settle step and
    /// latches a determinism loss, so verification refuses the run (Codex
    /// implementation review, HIGH 3 and 4).
    #[tokio::test]
    #[cfg(feature = "liteinst")]
    async fn a_recorded_in_guest_failure_fails_the_run_and_latches_a_loss() {
        use std::os::fd::FromRawFd;

        use reverie::GlobalTool;
        use reverie_rpc_transport::ConnectionAdmission;

        let config = DetConfig::default().with_backend(|backend| {
            backend.process_exits_complete_asynchronously = true;
        });
        let global_state = detcore::GlobalState::init_global_state(&config).await;
        let exits = in_guest_exits::InGuestExitAdmission::default();
        // Admitting before any reporter is attached is refused and recorded.
        let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, std::process::id() as i32, 0) };
        assert!(fd >= 0);
        let pidfd = unsafe { std::os::fd::OwnedFd::from_raw_fd(fd as i32) };
        assert!(exits.admit(pidfd).is_err());
        let error = settle_in_guest_exits(&exits, &global_state)
            .await
            .expect_err("a recorded failure must fail the run");
        assert!(
            error
                .to_string()
                .starts_with("in-guest LiteInst refused a guest connection:"),
            "{error}"
        );
        assert!(detcore::detlog::determinism_loss().is_some());
    }

    #[test]
    fn in_guest_trap_is_in_guest_liteinst_and_forces_site_patching_off() {
        assert!(Backend::Liteinst.is_in_guest_liteinst());
        assert!(Backend::InGuestTrap.is_in_guest_liteinst());
        for backend in [
            Backend::Ptrace,
            Backend::Dbt,
            Backend::Sabre,
            Backend::Kvm,
            Backend::E9patch,
        ] {
            assert!(!backend.is_in_guest_liteinst(), "{backend:?}");
            assert_eq!(backend.runtime_name(), backend.as_str(), "{backend:?}");
            assert_eq!(forced_site_patching_value(backend), None, "{backend:?}");
        }
        assert_eq!(Backend::InGuestTrap.as_str(), "in-guest-trap");
        assert_eq!(Backend::InGuestTrap.runtime_name(), "liteinst");
        assert_eq!(Backend::Liteinst.runtime_name(), "liteinst");
        // liteinst leaves the variable to the guest environment.
        assert_eq!(forced_site_patching_value(Backend::Liteinst), None);
        assert_eq!(forced_site_patching_value(Backend::InGuestTrap), Some("0"));
        // The CLI spelling parses to the variant and back.
        assert_eq!(
            <Backend as ValueEnum>::from_str("in-guest-trap", false),
            Ok(Backend::InGuestTrap)
        );
        assert!(Backend::ALL.contains(&Backend::InGuestTrap));
        for backend in Backend::ALL {
            let name = backend
                .to_possible_value()
                .expect("every backend is a visible CLI value");
            assert_eq!(name.get_name(), backend.as_str(), "{backend:?}");
        }
    }

    /// in-guest-trap sets `REVERIE_LITEINST_SITE_PATCHING=0` on the guest
    /// command when the guest environment leaves it unset or already `0`, and
    /// refuses, before anything starts, an environment that sets any other
    /// value; the refusal names the value and both ways out. liteinst neither
    /// refuses nor changes the variable.
    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_trap_refuses_a_guest_environment_that_turns_site_patching_on() {
        use std::ffi::OsStr;

        let name = reverie_liteinst::SITE_PATCHING_ENV;
        let mut unset = Command::new("/bin/true");
        unset.env_remove(name);
        assert_eq!(
            in_guest_trap_site_patching_conflict(&unset, Backend::InGuestTrap),
            None
        );
        force_in_guest_site_patching(&mut unset, Backend::InGuestTrap);
        assert_eq!(unset.get_env(name).as_deref(), Some(OsStr::new("0")));

        let mut zero = Command::new("/bin/true");
        zero.env(name, "0");
        assert_eq!(
            in_guest_trap_site_patching_conflict(&zero, Backend::InGuestTrap),
            None
        );

        for value in ["1", "", "yes", " 0"] {
            let mut command = Command::new("/bin/true");
            command.env(name, value);
            let conflict = in_guest_trap_site_patching_conflict(&command, Backend::InGuestTrap)
                .unwrap_or_else(|| panic!("{value:?} was accepted"));
            assert!(conflict.contains(&format!("{name}={value},")), "{conflict}");
            assert!(conflict.contains("--backend=liteinst"), "{conflict}");
            assert!(conflict.contains("drop the variable"), "{conflict}");
            // The whole refusal path stops on it first, before the program,
            // kernel or timeslice checks, and leaves the value untouched.
            let error = refuse_in_guest_liteinst_run(
                &mut command,
                &DetConfig::default(),
                Backend::InGuestTrap,
            )
            .unwrap_err();
            let refusal = error
                .downcast_ref::<LiteinstInGuestRefusal>()
                .expect("a typed in-guest refusal, which the CLI reports as exit 122");
            assert!(
                refusal.to_string().starts_with(
                    "--backend=in-guest-trap (in-guest LiteInst, site patching off) refuses \
                     this run: the guest environment sets"
                ),
                "{refusal}"
            );
            assert_eq!(command.get_env(name).as_deref(), Some(OsStr::new(value)));

            // liteinst takes the variable from the guest environment as it is.
            let mut liteinst = Command::new("/bin/true");
            liteinst.env(name, value);
            assert_eq!(
                in_guest_trap_site_patching_conflict(&liteinst, Backend::Liteinst),
                None
            );
            force_in_guest_site_patching(&mut liteinst, Backend::Liteinst);
            assert_eq!(liteinst.get_env(name).as_deref(), Some(OsStr::new(value)));
        }
    }

    /// The coordinator's postcondition for an in-guest-trap run: a record
    /// with no patched and no candidate site passes; a patched site, a
    /// candidate, an unmeasured count or a missing record is refused, with a
    /// typed in-guest refusal and a determinism loss. Other backends are not
    /// held to it.
    #[test]
    #[cfg(feature = "liteinst")]
    fn an_in_guest_trap_run_that_patched_a_site_is_refused_after_it_ends() {
        let record = |candidates: Option<u64>, patched: Option<u64>| {
            reverie::DispatchStats::new(
                "in-guest-trap",
                reverie::DispatchCounters::ZERO,
                reverie::SiteCounters {
                    candidates,
                    patched,
                    fell_back: Some(0),
                },
            )
        };
        let clean = record(Some(0), Some(0));
        assert_eq!(
            in_guest_trap_patching_evidence(Backend::InGuestTrap, Some(&clean), 1),
            None
        );
        for (candidates, patched, shown) in [
            (Some(3), Some(3), "3 patched and 3 candidate"),
            (Some(1), Some(0), "0 patched and 1 candidate"),
            (Some(0), None, "unmeasured patched and 0 candidate"),
        ] {
            let reason = in_guest_trap_patching_evidence(
                Backend::InGuestTrap,
                Some(&record(candidates, patched)),
                1,
            )
            .unwrap_or_else(|| panic!("{candidates:?} {patched:?} was accepted"));
            assert!(reason.contains(shown), "{reason}");
        }
        assert!(
            in_guest_trap_patching_evidence(Backend::InGuestTrap, None, 1)
                .is_some_and(|reason| reason.contains("no LiteInst dispatch record"))
        );
        // An empty record from guests that collected nothing is not zero.
        assert!(
            in_guest_trap_patching_evidence(Backend::InGuestTrap, Some(&clean), 0)
                .is_some_and(|reason| reason.contains("no guest process reported"))
        );
        let patched = record(Some(3), Some(3));
        assert_eq!(
            in_guest_trap_patching_evidence(Backend::Liteinst, Some(&patched), 1),
            None
        );
        assert_eq!(
            in_guest_trap_patching_evidence(Backend::Liteinst, None, 0),
            None
        );

        refuse_in_guest_trap_patching(Backend::InGuestTrap, None).unwrap();
        let error = refuse_in_guest_trap_patching(
            Backend::InGuestTrap,
            in_guest_trap_patching_evidence(Backend::InGuestTrap, Some(&patched), 1),
        )
        .unwrap_err();
        assert!(
            error.downcast_ref::<LiteinstInGuestRefusal>().is_some(),
            "{error:#}"
        );
        assert!(
            error
                .to_string()
                .starts_with("--backend=in-guest-trap (in-guest LiteInst, site patching off)"),
            "{error:#}"
        );
        assert!(
            detcore::detlog::determinism_loss().is_some_and(|loss| loss.contains("3 patched")),
            "the refusal must record a determinism loss"
        );
    }

    /// Only an in-guest-trap run tells every Tool image to check the settings
    /// its runtime captured, and that run always collects the statistics the
    /// check requires.
    #[test]
    #[cfg(feature = "liteinst")]
    fn only_in_guest_trap_asks_tool_images_to_check_their_runtime() {
        for backend in Backend::ALL {
            let checks = in_guest_site_patching_off(backend);
            assert_eq!(checks, backend == Backend::InGuestTrap, "{backend:?}");
            if checks {
                assert!(in_guest_stats_request(backend, &None).is_enabled());
            }
        }
        assert!(!in_guest_stats_request(Backend::Liteinst, &None).is_enabled());
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_liteinst_refuses_a_kernel_older_than_6_5() {
        for release in [
            "6.5.0",
            "6.5",
            "6.10.3-200.fc40.x86_64",
            "7.1.3-0_fbk0_rc18",
            "10.0",
        ] {
            assert_eq!(in_guest_liteinst_kernel_gap(release), None, "{release}");
        }
        for release in ["6.4.16", "5.15.0-91-generic", "4.19"] {
            let gap = in_guest_liteinst_kernel_gap(release)
                .unwrap_or_else(|| panic!("{release} was not refused"));
            assert!(gap.contains("Linux 6.5 or later"), "{gap}");
            assert!(gap.contains(release), "{gap}");
        }
        for release in ["", "unknown", "6"] {
            assert!(
                in_guest_liteinst_kernel_gap(release).is_some(),
                "{release:?}"
            );
        }
        // No assertion about the kernel running the tests: that would make
        // the test's result depend on the host, and the releases above
        // already test both sides of the floor.
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_liteinst_refuses_secure_execution_images() {
        use interp::startup_fixtures::dynamic_executable;

        let directory = tempfile::tempdir().unwrap();
        let bytes = dynamic_executable(false);
        let gap = |name: &str, mode: u32| {
            let program = write_guest_program(directory.path(), name, &bytes, mode);
            in_guest_liteinst_program_gap(&mut Command::new(&program)).unwrap()
        };
        for (name, mode) in [("setuid", 0o4755), ("setgid", 0o2755)] {
            let refusal = gap(name, mode).unwrap_or_else(|| panic!("{name} was not refused"));
            assert!(refusal.contains("secure-execution mode"), "{refusal}");
        }
        // Without group execute the kernel treats set-group-ID as mandatory
        // locking, not a credential change, so the loader honours LD_PRELOAD.
        assert_eq!(gap("setgid-no-group-exec", 0o2744), None);
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_liteinst_checks_scripts_through_their_interpreter() {
        use interp::startup_fixtures::*;

        let directory = tempfile::tempdir().unwrap();
        let dynamic = write_guest_program(
            directory.path(),
            "dynamic",
            &dynamic_executable(false),
            0o755,
        );
        let static_elf = write_guest_program(
            directory.path(),
            "static",
            &elf(EM_X86_64, &[(PT_LOAD, b"code")]),
            0o755,
        );

        let script = write_guest_program(
            directory.path(),
            "to-static",
            &shebang_to(&static_elf),
            0o755,
        );
        let gap = in_guest_liteinst_program_gap(&mut Command::new(&script))
            .unwrap()
            .expect("a script run by a static interpreter was not refused");
        assert!(
            gap.contains(&static_elf.display().to_string()) && gap.contains("statically linked"),
            "{gap}"
        );

        // A relative interpreter resolves against the guest's working directory.
        let relative = write_guest_program(directory.path(), "relative", b"#!static\n", 0o755);
        let gap =
            in_guest_liteinst_program_gap(Command::new(&relative).current_dir(directory.path()))
                .unwrap()
                .expect("a relative static interpreter was not refused");
        assert!(gap.contains("statically linked"), "{gap}");

        // The kernel keeps a carriage return in the interpreter path, so a
        // script with CRLF line endings runs `interp\r`, not `interp`.
        write_guest_program(
            directory.path(),
            "interp",
            &dynamic_executable(false),
            0o755,
        );
        let interp_cr = write_guest_program(
            directory.path(),
            "interp\r",
            &elf(EM_X86_64, &[(PT_LOAD, b"code")]),
            0o755,
        );
        let mut crlf = b"#!".to_vec();
        crlf.extend_from_slice(directory.path().join("interp").as_os_str().as_bytes());
        crlf.extend_from_slice(b"\r\necho script\r\n");
        let crlf = write_guest_program(directory.path(), "crlf", &crlf, 0o755);
        let gap = in_guest_liteinst_program_gap(&mut Command::new(&crlf))
            .unwrap()
            .expect("a CRLF script run by a static interp\\r was not refused");
        assert!(
            gap.contains(&interp_cr.display().to_string()) && gap.contains("statically linked"),
            "{gap}"
        );

        // A #! line the kernel's script handler declines runs no interpreter:
        // the kernel fails the execve or tries another handler, so it is
        // refused.
        let mut truncated = b"#!/".to_vec();
        truncated.resize(256, b'a');
        for (name, bytes) in [
            ("empty", b"#! \t\nbody\n".to_vec()),
            ("truncated", truncated),
        ] {
            let program = write_guest_program(directory.path(), name, &bytes, 0o755);
            let gap = in_guest_liteinst_program_gap(&mut Command::new(&program))
                .unwrap()
                .unwrap_or_else(|| panic!("{name} was not refused"));
            assert!(
                gap.contains("the kernel's script handler declines it"),
                "{name}: {gap}"
            );
        }

        // Four nested scripts are followed to the ELF image; a fifth is refused.
        let mut target = dynamic;
        for level in 1..=5 {
            target = write_guest_program(
                directory.path(),
                &format!("level-{level}"),
                &shebang_to(&target),
                0o755,
            );
            let gap = in_guest_liteinst_program_gap(&mut Command::new(&target)).unwrap();
            if level <= MAX_INTERPRETER_DEPTH {
                assert_eq!(gap, None, "level {level}");
            } else {
                let gap = gap.expect("five nested scripts were not refused");
                assert!(gap.contains("nest more than 4 deep"), "{gap}");
            }
        }
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_liteinst_refuses_a_fifo_without_blocking() {
        let directory = tempfile::tempdir().unwrap();
        let fifo = directory.path().join("fifo");
        let fifo_c = std::ffi::CString::new(fifo.as_os_str().as_bytes()).unwrap();
        assert_eq!(unsafe { libc::mkfifo(fifo_c.as_ptr(), 0o700) }, 0);
        let script = write_guest_program(directory.path(), "to-fifo", &shebang_to(&fifo), 0o755);
        // A socket cannot be opened at all (ENXIO), and opening a device can
        // act on it; both must be refused, not reported as unreadable.
        let socket = directory.path().join("socket");
        let _listener = std::os::unix::net::UnixListener::bind(&socket).unwrap();
        let to_socket =
            write_guest_program(directory.path(), "to-socket", &shebang_to(&socket), 0o755);
        let device = PathBuf::from("/dev/null");
        let to_device =
            write_guest_program(directory.path(), "to-device", &shebang_to(&device), 0o755);
        // `./fifo` reaches the check through find_program's working-directory
        // branch, which does not require a regular file; the scripts reach the
        // FIFO, the socket and the device as their interpreters. Nothing opens
        // the FIFO for writing, so a blocking open or read would never return:
        // each check runs on its own thread and must answer within 10 s.
        let cases = [
            (
                "program",
                PathBuf::from("./fifo"),
                Some(directory.path().to_owned()),
                fs::canonicalize(&fifo).unwrap(),
            ),
            ("interpreter", script, None, fifo.clone()),
            ("socket interpreter", to_socket, None, socket.clone()),
            ("device interpreter", to_device, None, device),
        ];
        for (name, program, current_dir, shown) in cases {
            let (sender, receiver) = std::sync::mpsc::channel();
            std::thread::spawn(move || {
                let mut command = Command::new(&program);
                if let Some(directory) = current_dir {
                    command.current_dir(directory);
                }
                let gap = in_guest_liteinst_program_gap(&mut command)
                    .map_err(|error| format!("{error:#}"));
                let _ = sender.send(gap);
            });
            let gap = receiver
                .recv_timeout(std::time::Duration::from_secs(10))
                .unwrap_or_else(|_| panic!("{name}: the check blocked on the FIFO"))
                .unwrap()
                .unwrap_or_else(|| panic!("{name}: the FIFO was not refused"));
            assert!(
                gap.contains(&format!("{} is not a regular file", shown.display())),
                "{name}: {gap}"
            );
        }
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn in_guest_liteinst_pins_the_program_the_launch_resolves() {
        use interp::startup_fixtures::*;

        let directory = tempfile::tempdir().unwrap();
        // A name that no host PATH directory holds.
        let name = "hermit-3520-static";
        let static_elf = write_guest_program(
            directory.path(),
            name,
            &elf(EM_X86_64, &[(PT_LOAD, b"code")]),
            0o755,
        );
        let search = format!("/nonexistent-directory:{}", directory.path().display());
        // The launch resolves the program with `find_program` on the command it
        // is given. Builds the command twice: once for the launch's own
        // resolution, once for the check, which must agree with it.
        let check = |build: &dyn Fn() -> Command| {
            let launch = build().find_program();
            let mut command = build();
            let arg0 = command.get_arg0().to_owned();
            let gap = in_guest_liteinst_program_gap(&mut command);
            match launch {
                Ok(program) => {
                    let gap = gap.unwrap().expect("the static image was not refused");
                    assert!(gap.contains("is statically linked"), "{gap}");
                    assert_eq!(Path::new(command.get_program()), program);
                    assert_eq!(command.get_arg0(), arg0);
                    // Pinned: the launch's own resolution returns it unchanged.
                    assert_eq!(command.find_program().unwrap(), program);
                    assert_eq!(
                        fs::canonicalize(&program).unwrap(),
                        fs::canonicalize(&static_elf).unwrap()
                    );
                    true
                }
                Err(_) => {
                    let error = gap.expect_err("the check found what the launch cannot");
                    assert!(
                        format!("{error:#}").contains("cannot find guest program"),
                        "{error:#}"
                    );
                    false
                }
            }
        };

        // A path with a slash resolves against the guest's working directory.
        assert!(check(&|| {
            let mut command = Command::new(format!("./{name}"));
            command.current_dir(directory.path());
            command
        }));
        // A bare name is searched for in the command's PATH once the base
        // environment is cleared, skipping entries without such a file.
        assert!(check(&|| {
            let mut command = Command::new(name);
            command.env_clear().env("PATH", &search);
            command
        }));
        // Without a cleared environment the launch searches Hermit's own PATH,
        // not the command's, so the static image is not found by either.
        assert!(!check(&|| {
            let mut command = Command::new(name);
            command.env("PATH", &search);
            command
        }));
        // A cleared environment without PATH (`--base-env=empty`) searches
        // Hermit's working directory, not the guest's and not /bin:/usr/bin.
        assert!(!check(&|| {
            let mut command = Command::new(name);
            command.env_clear().current_dir(directory.path());
            command
        }));
        // Not executable, or missing: the launch cannot run it, and neither
        // resolution finds it.
        fs::set_permissions(&static_elf, fs::Permissions::from_mode(0o644)).unwrap();
        assert!(!check(&|| {
            let mut command = Command::new(name);
            command.env_clear().env("PATH", &search);
            command
        }));
        assert!(!check(&|| Command::new(&static_elf)));
        assert!(!check(&|| Command::new("hermit-no-such-program-3520")));
    }

    #[test]
    fn skid_overshoot_error_refuses_a_successful_result() {
        let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
        let _ = reverie::take_skid_overshoot_count();
        assert!(take_skid_overshoot_error().is_none());

        reverie::record_skid_overshoot();
        let error = take_skid_overshoot_error().expect("recorded overshoot must be reported");
        assert!(
            error
                .to_string()
                .contains("observed 1 HERMIT_SKID_OVERSHOOT report(s)")
        );
        assert!(
            error
                .to_string()
                .contains("deterministic execution was not established")
        );
        assert_eq!(reverie::take_skid_overshoot_count(), 0);
    }

    #[test]
    fn skid_overshoot_report_preserves_success_without_an_overshoot() {
        let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
        let _ = reverie::take_skid_overshoot_count();

        let report = SkidOvershootReport::begin(true);
        assert_eq!(report.finish(Ok::<_, Error>(7)).unwrap(), 7);
        assert_eq!(reverie::take_skid_overshoot_count(), 0);
    }

    #[test]
    fn skid_overshoot_count_can_cross_the_verify_boundary_with_the_output() {
        let _lock = SKID_OVERSHOOT_TEST_LOCK.lock().unwrap();
        let _ = reverie::take_skid_overshoot_count();

        let report = SkidOvershootReport::begin(true);
        reverie::record_skid_overshoot();
        let (value, count) = report.finish_with_count(Ok::<_, Error>(7)).unwrap();
        assert_eq!(value, 7);
        assert_eq!(count, 1);
        assert_eq!(reverie::take_skid_overshoot_count(), 0);
    }

    /// ⚠️ THE REGRESSION agent(hermit-007)'s CODEX LANE CAUGHT, PINNED FOR THE
    /// SIGNAL BAND. This test fails if the signal half stops being covered; it
    /// does NOT prove the predicate can never drift again, because the
    /// `122..=127` half is still a hard-coded literal that a future reserved
    /// value would fall outside exactly as 130 did.
    /// `liteinst_requires_forced_shutdown` hard-coded
    /// `Exited(122..=127)`. When the reserved set grew to include the signal
    /// band, `Exited(130)` fell outside it and is not `Signaled(_, _)` -- it is a
    /// status hermit CHOSE -- so LiteInst skipped the forced shutdown and
    /// `clean_up` could wait forever on `handle.await`. A reachable hang,
    /// introduced by widening the reserved set in one place and not the other.
    ///
    /// The 130 and 143 rows are the ones with teeth: revert the predicate to the
    /// bare range and they fail.
    #[test]
    #[cfg(feature = "liteinst")]
    fn liteinst_forced_shutdown_covers_every_status_hermit_reserves() {
        // The signal band. Not `Signaled`: hermit CHOSE these codes, because a
        // namespace init cannot produce a genuine signalled wait status.
        assert!(
            liteinst_requires_forced_shutdown(ExitStatus::Exited(130)),
            "128+SIGINT"
        );
        assert!(
            liteinst_requires_forced_shutdown(ExitStatus::Exited(143)),
            "128+SIGTERM"
        );
        assert!(
            liteinst_requires_forced_shutdown(ExitStatus::Exited(129)),
            "128+SIGHUP"
        );

        // The pre-existing reserved band, unchanged.
        assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(122)));
        assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(125)));
        assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(127)));

        // A real signal death still forces shutdown.
        assert!(liteinst_requires_forced_shutdown(ExitStatus::Signaled(
            nix::sys::signal::Signal::SIGKILL,
            false
        )));

        // ⚠️ CONTROLS, so a predicate that simply returned `true` would fail
        // this test rather than pass it. An ordinary guest exit must NOT force a
        // shutdown; 128 is excluded because there is no signal 0, and 200 is
        // above SIGRTMAX so it is a guest's own number.
        assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(0)));
        assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(1)));
        assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(121)));
        assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(128)));
        assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(200)));
        // ⚠️ THE BAND EDGES, WHICH THIS TEST STOPPED SHORT OF. It went up to 143
        // and then jumped to 200, so `MAX_SIGNO` could shrink from 64 to 15 with
        // every row still green -- proved vacuous by agent(hermit-005)'s codex
        // lane. 192 is `128 + SIGRTMAX`, the last status in the band; 193 is the
        // first outside it. Testing a range in the middle pins nothing at either end.
        assert!(
            liteinst_requires_forced_shutdown(ExitStatus::Exited(192)),
            "128 + SIGRTMAX is still a signal death and must force shutdown"
        );
        assert!(
            !liteinst_requires_forced_shutdown(ExitStatus::Exited(193)),
            "above SIGRTMAX the status is the guest's own and must NOT force shutdown"
        );
    }

    /// ⚠️ The bare `NotFound` here was read as a git-worktree bug and cost real
    /// investigation time. The message must name the mount namespace, because
    /// the directory it is complaining about usually exists on the host.
    #[test]
    fn a_cwd_under_tmp_is_explained_by_the_container_tmpfs_not_by_a_missing_directory() {
        let error = std::io::Error::from(std::io::ErrorKind::NotFound);
        let rendered = format!(
            "{}",
            super::kvm_cwd_resolution_error(std::path::Path::new("/tmp/agent-checkout"), &error)
        );
        assert!(
            rendered.contains("private, empty tmpfs"),
            "must name the mechanism: {rendered}"
        );
        assert!(
            rendered.contains("--workdir"),
            "must name a way out: {rendered}"
        );
        assert!(
            rendered.contains("/tmp/agent-checkout"),
            "must still name the path asked for: {rendered}"
        );
    }

    /// The explanation must NOT be attached to failures it does not explain --
    /// an error that volunteers the wrong cause is what produced the original
    /// misdiagnosis, in the other direction.
    #[test]
    fn the_tmpfs_explanation_is_withheld_where_it_does_not_apply() {
        let not_found = std::io::Error::from(std::io::ErrorKind::NotFound);
        // ⚠️ A CONSTANT ABSOLUTE PATH, NOT ONE READ FROM THE ENVIRONMENT. The only
        // property this arm needs is "outside /tmp", and a literal that is outside
        // /tmp by construction supplies it without asking the runner anything.
        //
        // The earlier form wrote a developer-specific home path, which made
        // `scripts/check-portable-paths.sh` red on main -- correctly: that gate
        // rejects such a path anywhere in a tracked build or run file, tests and
        // COMMENTS included, so spelling one out even to explain this would trip
        // it. Replacing it by reading $HOME then made the test depend on the
        // runner instead: measured by `agent(codex-rev-2641)`, HOME unset, empty,
        // relative and /tmp-rooted gave three different fixtures and one failure.
        // A unit test that consults the environment is answering a question about
        // the environment.
        //
        // ⚠️ AND NO GUARD IS NEEDED HERE, WHICH IS THE POINT OF USING A CONSTANT.
        // The $HOME form carried `assert!(!outside_path.starts_with("/tmp"))` to
        // stop a /tmp-rooted HOME making this vacuous. That assertion could never
        // fire: the `!outside.contains("tmpfs")` check below sits ABOVE it in
        // source order and panics first in exactly that case. It was decorative,
        // and an assertion over a compile-time constant would be too, so there is
        // none. The fixture cannot drift into /tmp because nothing computes it.
        let outside_path = std::path::Path::new("/nonexistent-home/wt");
        let outside = format!(
            "{}",
            super::kvm_cwd_resolution_error(outside_path, &not_found)
        );
        assert!(!outside.contains("tmpfs"), "not a /tmp path: {outside}");

        // `/tmp` itself resolves in the guest -- the mount POINT exists -- so a
        // failure there is something else and must not be mislabelled.
        let tmp_itself = format!(
            "{}",
            super::kvm_cwd_resolution_error(std::path::Path::new("/tmp"), &not_found)
        );
        assert!(!tmp_itself.contains("tmpfs"), "/tmp itself: {tmp_itself}");

        // A different errno under /tmp is a different fault (permissions, say).
        let denied = std::io::Error::from(std::io::ErrorKind::PermissionDenied);
        let other = format!(
            "{}",
            super::kvm_cwd_resolution_error(std::path::Path::new("/tmp/x"), &denied)
        );
        assert!(!other.contains("tmpfs"), "not NotFound: {other}");
    }

    /// A build-flag message must not assert a machine condition it never tested.
    ///
    /// ⚠️ THIS IS THE FOURTH DIAGNOSTIC TONIGHT THAT NAMED AN UNESTABLISHED CAUSE,
    /// after the sigpipe checker, the manifest gate and the LiteInst pidfd error.
    /// "DBT support was not included in this build" reads as "this machine cannot
    /// run DBT", and cost four or five unnecessary abstentions across the fleet --
    /// agents declined to make DBT claims on a box where DynamoRIO is installed
    /// and `--features dbt` builds and runs. The absence is a build flag, and the
    /// message must say which flag and admit what it did not check.
    #[test]
    fn an_unbuilt_backend_names_the_flag_and_claims_nothing_about_the_machine() {
        let feature_disabled_backends: &[(Backend, &str)] = &[
            #[cfg(not(feature = "dbt"))]
            (Backend::Dbt, "dbt"),
            #[cfg(not(feature = "sabre"))]
            (Backend::Sabre, "sabre"),
            #[cfg(not(feature = "e9patch"))]
            (Backend::E9patch, "e9patch"),
            #[cfg(not(feature = "liteinst"))]
            (Backend::Liteinst, "liteinst"),
            // in-guest-trap is the same runtime behind the same feature.
            #[cfg(not(feature = "liteinst"))]
            (Backend::InGuestTrap, "liteinst"),
        ];

        for &(backend, feature) in feature_disabled_backends {
            let reason = backend
                .unavailable_reason()
                .expect("a feature-disabled backend must report why it is unavailable");
            assert!(
                reason.contains(feature),
                "{backend:?}: a build-flag message must name the flag to rebuild with, got: {reason}"
            );
            assert!(
                reason.contains("has not been checked"),
                "{backend:?}: a build-flag message must not imply the machine was tested, got: {reason}"
            );
        }
    }

    #[cfg(not(feature = "dbt"))]
    #[test]
    fn dbt_registry_diagnostic_redirects_to_a_source_build() {
        let reason = super::dbt_disabled_build_reason("hermit-run");
        assert!(reason.contains("not included in this registry release"));
        assert!(reason.contains("source build from https://github.com/rrnewton/hermit"));
        assert!(reason.contains("`third-party-backends` feature does not include DBT"));
        assert!(reason.contains("has not been checked"));

        let source_reason = super::dbt_disabled_build_reason("hermit");
        assert!(source_reason.contains("rebuild with `--features dbt`"));
        assert!(!source_reason.contains("not included in this registry release"));
    }

    #[cfg(not(feature = "sabre"))]
    #[tokio::test]
    async fn disabled_sabre_dispatch_refuses_before_resolving_the_program() {
        let program = "/hermit-missing-disabled-sabre-program";
        let error = super::dispatch_backend(
            super::Command::new(program),
            super::DetConfig::default(),
            false,
            &None,
            Backend::Sabre,
            None,
        )
        .await
        .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("the `sabre` feature is not enabled")
        );
        let error = super::dispatch_output_backend(
            super::Command::new(program),
            super::DetConfig::default(),
            false,
            &None,
            Backend::Sabre,
            None,
        )
        .await
        .unwrap_err();
        assert!(
            error
                .to_string()
                .contains("the `sabre` feature is not enabled")
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn a_cold_dbt_runtime_diagnostic_is_not_feature_absence() {
        let reason = dbt_runtime_unavailable_reason_from(Err(io::Error::new(
            io::ErrorKind::NotFound,
            "Hermit DBT runtime was not built beside /cold-target/hermit or in its deps directory",
        )))
        .expect("a missing cold-target runtime must remain unavailable");

        assert!(reason.contains("was not built beside"));
        assert!(reason.contains("build the hermit binary and cdylib"));
        assert!(!reason.contains("not enabled in this build"));
        assert!(!reason.contains("has not been checked"));
    }
    #[cfg(feature = "sabre")]
    use std::ffi::OsStr;
    use std::fs;
    #[cfg(feature = "sabre")]
    use std::os::unix::fs::PermissionsExt;
    use std::path::PathBuf;
    #[cfg(feature = "sabre")]
    use std::sync::Arc;
    #[cfg(feature = "sabre")]
    use std::time::Duration;

    use super::Backend;
    #[cfg(any(feature = "sabre", feature = "liteinst"))]
    use super::ExitStatus;
    use super::HermitData;
    use super::Id;
    #[cfg(feature = "sabre")]
    use super::SABRE_RPC_SOCKET_ENV;
    use super::collect_recording_ids;
    #[cfg(feature = "dbt")]
    use super::dbt_runtime_unavailable_reason;
    #[cfg(feature = "dbt")]
    use super::dbt_runtime_unavailable_reason_from;
    #[cfg(feature = "dbt")]
    use super::dynamorio_sdk_available;
    use super::ensure_backend_dispatch;
    #[cfg(feature = "dbt")]
    use super::is_dynamorio_sdk;
    use super::kvm_device_unavailable_reason;
    #[cfg(feature = "liteinst")]
    use super::liteinst_requires_forced_shutdown;
    #[cfg(feature = "dbt")]
    use super::liteinst_unavailable_reason;
    use super::output_backend_stdin_file;
    use super::prepare_backend_config;
    use super::reserve_output_stdin_snapshot;
    use super::resolve_kvm_shebang;
    #[cfg(feature = "sabre")]
    use super::resolve_sabre_binary_from;
    #[cfg(feature = "sabre")]
    use super::sabre_backend_evidence_line;
    #[cfg(feature = "sabre")]
    use super::sabre_program_needs_neutral_name;
    #[cfg(feature = "sabre")]
    use super::sabre_reach_state;
    #[cfg(feature = "sabre")]
    use super::sabre_uninstrumented_guest_message;
    #[cfg(feature = "sabre")]
    use super::shutdown_sabre_rpc;
    #[cfg(feature = "sabre")]
    use super::stage_sabre_program_in;
    #[cfg(feature = "sabre")]
    use super::stop_sabre_rpc_server;
    #[cfg(feature = "sabre")]
    use super::wait_for_sabre_rpc_disconnects;

    #[test]
    fn recording_inventory_preserves_missing_and_non_recording_cases() {
        let parent = tempfile::tempdir().expect("failed to create recording inventory parent");
        let missing = parent.path().join("missing");
        assert!(
            HermitData::with_dir(&missing)
                .try_recordings()
                .expect("a missing data directory should be empty")
                .is_empty()
        );

        let inventory = parent.path().join("inventory");
        fs::create_dir(&inventory).expect("failed to create recording inventory");
        let recording: Id = "0123456789abcdef0123456789abcdef"
            .parse()
            .expect("valid recording ID");
        fs::create_dir(inventory.join(recording.to_string()))
            .expect("failed to create recording directory");
        fs::create_dir(inventory.join("not-a-recording"))
            .expect("failed to create invalid-name directory");
        fs::write(
            inventory.join("fedcba9876543210fedcba9876543210"),
            b"not a directory",
        )
        .expect("failed to create valid-looking non-directory");

        assert_eq!(
            HermitData::with_dir(&inventory)
                .try_recordings()
                .expect("recording inventory should be readable"),
            vec![recording]
        );
        assert_eq!(
            HermitData::with_dir(&inventory)
                .recordings()
                .collect::<Vec<_>>(),
            vec![recording]
        );
    }

    #[test]
    fn recording_inventory_rejects_an_error_after_a_valid_entry() {
        let inventory = tempfile::tempdir().expect("failed to create recording inventory");
        let recording: Id = "0123456789abcdef0123456789abcdef"
            .parse()
            .expect("valid recording ID");
        fs::create_dir(inventory.path().join(recording.to_string()))
            .expect("failed to create recording directory");
        let valid_entry = fs::read_dir(inventory.path())
            .expect("failed to open recording inventory")
            .next()
            .expect("missing recording entry")
            .expect("failed to read recording entry");

        let entries = vec![
            Ok(valid_entry),
            Err(std::io::Error::other("injected entry failure")),
        ];
        let error = collect_recording_ids(entries, inventory.path(), fs::DirEntry::file_type)
            .expect_err("a partial inventory must not be returned");
        assert!(
            error
                .to_string()
                .contains("Failed to read an entry in recording inventory"),
            "unexpected error: {error:#}"
        );
    }

    #[test]
    fn recording_inventory_rejects_an_entry_inspection_error() {
        let inventory = tempfile::tempdir().expect("failed to create recording inventory");
        let recording = inventory.path().join("0123456789abcdef0123456789abcdef");
        fs::create_dir(&recording).expect("failed to create recording directory");
        let entry = fs::read_dir(inventory.path())
            .expect("failed to open recording inventory")
            .next()
            .expect("missing recording entry")
            .expect("failed to read recording entry");

        let error = collect_recording_ids([Ok(entry)], inventory.path(), |_| {
            Err(std::io::Error::other("injected inspection failure"))
        })
        .expect_err("an uninspectable entry must fail the inventory");
        assert!(
            error
                .to_string()
                .contains("Failed to inspect recording inventory entry"),
            "unexpected error: {error:#}"
        );
    }

    /// Regression test for the `hermit run --verify` empty-stdin bug: a pipe
    /// (non-seekable) fed to hermit must be buffered and replayed *identically*
    /// to every run of `--verify`. Before the fix the output-capturing backend
    /// used `Stdio::null()`, so piped input was silently dropped and both runs
    /// saw empty input (a false "deterministic" pass, e.g. `gcc -x c -`
    /// compiling nothing). This asserts the reserved snapshot replays the exact
    /// bytes twice.
    #[test]
    fn output_stdin_snapshot_replays_pipe_to_repeated_runs() {
        use std::io::Read;
        use std::io::Write;
        use std::os::fd::FromRawFd;

        // A pipe is non-seekable, exercising the buffer-into-tempfile path that
        // matches the real `echo prog | hermit run --verify` scenario.
        let mut fds = [0i32; 2];
        assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
        // SAFETY: pipe(2) just returned two fresh owned descriptors.
        let read_end = unsafe { fs::File::from_raw_fd(fds[0]) };
        let mut write_end = unsafe { fs::File::from_raw_fd(fds[1]) };

        let payload = b"int main(){return 0;}\n";
        write_end.write_all(payload).unwrap();
        // Close the write end so draining the pipe hits EOF.
        drop(write_end);

        reserve_output_stdin_snapshot(Some(read_end)).unwrap();

        // Two successive runs (as `--verify` performs) must each read the full
        // payload from the start.
        for run in 0..2 {
            let mut file = output_backend_stdin_file()
                .unwrap()
                .unwrap_or_else(|| panic!("run {run}: expected a replayable stdin snapshot"));
            let mut got = Vec::new();
            file.read_to_end(&mut got).unwrap();
            assert_eq!(got, payload, "run {run} stdin replay mismatch");
        }

        let mut file = output_backend_stdin_file().unwrap().unwrap();
        let error = file
            .write_all(b"guest-must-not-change-the-snapshot")
            .expect_err("the replayed stdin must be read-only");
        assert_eq!(error.raw_os_error(), Some(libc::EBADF));
    }

    #[test]
    #[cfg(feature = "liteinst")]
    fn liteinst_reserved_failures_require_scheduler_cancellation() {
        for status in 122..=127 {
            assert!(liteinst_requires_forced_shutdown(ExitStatus::Exited(
                status
            )));
        }
        assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(121)));
        assert!(!liteinst_requires_forced_shutdown(ExitStatus::Exited(128)));
    }

    /// A SaBRe guest that never reaches the coordinator ran with no Detcore in
    /// the loop at all. The diagnosis must say so in those terms -- a
    /// zero-syscall SaBRe run is not a weak result, it is *no* result -- and it
    /// must name the statically linked ELF case, which is the shape that
    /// reaches this path in practice because a static client has no dynamic
    /// loader through which SaBRe could regain control.
    #[test]
    #[cfg(feature = "sabre")]
    fn uninstrumented_sabre_guest_is_reported_as_no_determinization() {
        let message = sabre_uninstrumented_guest_message(&ExitStatus::Exited(0));
        assert!(
            message.contains("no determinization at all"),
            "must not present an uninstrumented run as a weaker guarantee: {message}"
        );
        assert!(
            message.contains("Detcore coordinator"),
            "must name the authority that was never reached: {message}"
        );
        assert!(
            message.contains("statically linked ELF"),
            "must name the dominant cause so the reader can act: {message}"
        );
        // The exit status is carried because a successful-looking status is
        // exactly what makes this failure mode dangerous.
        assert!(
            message.contains("Exited(0)"),
            "must carry the observed status: {message}"
        );
        assert!(
            sabre_uninstrumented_guest_message(&ExitStatus::Exited(139)).contains("Exited(139)"),
            "must carry a failing status too"
        );
    }

    /// The old two-way banner treated both `guest_rpc_observed=false` and an
    /// engaged zero-fallback run as `sabre-exercised`. Assert the three states
    /// together so no pair can collapse back onto one value.
    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_reach_states_are_pairwise_distinct() {
        let no_detcore = sabre_reach_state(false, 0);
        let degraded = sabre_reach_state(true, 1);
        let exercised = sabre_reach_state(true, 0);

        assert_eq!(no_detcore, "no-detcore-reached");
        assert_eq!(degraded, "degraded-ptrace-fallback");
        assert_eq!(exercised, "sabre-exercised");
        assert_ne!(no_detcore, degraded);
        assert_ne!(no_detcore, exercised);
        assert_ne!(degraded, exercised);
        assert_eq!(
            sabre_reach_state(false, 9),
            "no-detcore-reached",
            "absence of a guest RPC must dominate any fallback count"
        );
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_backend_fact_is_versioned_and_names_preplugin_coverage() {
        let exercised = super::sabre_ptrace::PathEvidence {
            schema: 1,
            guest_rpc_observed: true,
            ptrace_fallback_sites: 0,
            trusted_shared_object_sites: 2,
            trusted_shared_objects: vec!["/usr/lib/libc.so.6".to_owned()],
        };
        assert_eq!(
            sabre_backend_evidence_line(&exercised),
            ":: Backend: sabre static rewriting + ptrace runtime; run_mode=run; \
             evidence_schema=1; preplugin_coverage=absent; ptrace_fallback_sites=0; \
             trusted_shared_object_sites=2; guest_rpc_observed=true; \
             reach_state=sabre-exercised"
        );

        let unengaged = super::sabre_ptrace::PathEvidence {
            guest_rpc_observed: false,
            ..exercised
        };
        let fact = sabre_backend_evidence_line(&unengaged);
        assert!(fact.contains("preplugin_coverage=absent"));
        assert!(fact.contains("reach_state=no-detcore-reached"));
        assert!(!fact.contains("reach_state=sabre-exercised"));
    }

    #[test]
    fn liteinst_backend_config_preserves_the_requested_timeslice() {
        // Configuration must not silently clear a requested timeslice: the
        // in-guest LiteInst refusal relies on seeing it.
        let config = super::DetConfig::default();
        assert!(config.max_timeslice.is_some());
        for backend in [Backend::Liteinst, Backend::InGuestTrap] {
            assert!(
                prepare_backend_config(config.clone(), backend)
                    .max_timeslice
                    .is_some(),
                "{backend:?}"
            );
        }
        assert!(
            prepare_backend_config(config, Backend::Ptrace)
                .max_timeslice
                .is_some()
        );
    }

    #[test]
    fn in_guest_liteinst_cancels_killed_thread_rpcs() {
        let config = super::DetConfig::default();
        for backend in [Backend::Liteinst, Backend::InGuestTrap] {
            assert!(
                prepare_backend_config(config.clone(), backend)
                    .backend
                    .needs_killed_thread_rpc_cancellation,
                "{backend:?}"
            );
        }
        // LiteInst's cancellation changes no other backend.
        assert!(
            !prepare_backend_config(config, Backend::Ptrace)
                .backend
                .needs_killed_thread_rpc_cancellation
        );
    }

    #[test]
    fn sabre_backend_configures_process_local_capabilities() {
        let config = super::DetConfig::default();
        let sabre = prepare_backend_config(config.clone(), Backend::Sabre).backend;
        assert!(sabre.tool_shares_guest_descriptor_table);
        assert!(sabre.rediscovers_descriptors_after_exec);
        assert!(sabre.internal_pipe_turns_are_host_timed);
        assert!(sabre.loopback_pollers_yield_to_peers);
        assert!(sabre.guest_clock_reads_bypass_backend);
        assert!(sabre.virtualizes_syscall_clobbers);
        assert!(sabre.needs_killed_thread_rpc_cancellation);
        assert!(sabre.reports_physical_process_exits);
        assert!(sabre.signal_interrupts_external_syscalls);
        assert!(sabre.tracks_process_children);
        assert!(!sabre.runs_exit_robust_list);
        assert!(!sabre.requires_thread_directed_process_signals);
        assert!(!sabre.defers_vfork_child_registration);
        let ptrace = prepare_backend_config(config, Backend::Ptrace).backend;
        assert!(!ptrace.tool_shares_guest_descriptor_table);
        assert!(!ptrace.rediscovers_descriptors_after_exec);
        assert!(!ptrace.internal_pipe_turns_are_host_timed);
        assert!(!ptrace.loopback_pollers_yield_to_peers);
        assert!(!ptrace.guest_clock_reads_bypass_backend);
        assert!(!ptrace.virtualizes_syscall_clobbers);
        assert!(!ptrace.needs_killed_thread_rpc_cancellation);
        assert!(!ptrace.reports_physical_process_exits);
        assert!(!ptrace.signal_interrupts_external_syscalls);
        assert!(ptrace.tracks_process_children);
        assert!(ptrace.runs_exit_robust_list);
        assert!(!ptrace.requires_thread_directed_process_signals);
        assert!(!ptrace.defers_vfork_child_registration);
    }

    #[test]
    fn kvm_reports_stored_metadata_timestamps_only_for_sequential_tool_threads() {
        let sequential = prepare_backend_config(
            super::DetConfig {
                sequentialize_threads: true,
                virtualize_metadata: true,
                ..super::DetConfig::default()
            },
            Backend::Kvm,
        );
        assert!(super::kvm_reports_stored_metadata_timestamps(&sequential));

        // Another guest thread could read the stored values before Detcore
        // rewrites them.
        let concurrent = super::DetConfig {
            sequentialize_threads: false,
            ..sequential.clone()
        };
        assert!(!super::kvm_reports_stored_metadata_timestamps(&concurrent));

        // Nothing rewrites the stored values.
        let host_metadata = super::DetConfig {
            virtualize_metadata: false,
            ..sequential
        };
        assert!(!super::kvm_reports_stored_metadata_timestamps(
            &host_metadata
        ));
    }

    #[test]
    fn kvm_backend_config_marks_concurrent_process_children() {
        let defaults = super::DetConfig::default();
        assert!(!defaults.sequentialize_threads);
        let nonsequential = prepare_backend_config(defaults, Backend::Kvm);
        assert!(nonsequential.backend.provides_process_signal_control);
        assert!(nonsequential.backend.emulates_child_waits);
        assert!(!may_observe_signal_dequeues(&nonsequential));
        assert!(!requires_signal_control(&nonsequential));

        let config = super::DetConfig {
            sequentialize_threads: true,
            ..super::DetConfig::default()
        };
        let kvm = prepare_backend_config(config, Backend::Kvm);
        assert!(may_observe_signal_dequeues(&kvm));
        assert!(requires_signal_control(&kvm));
        assert!(kvm.backend.needs_killed_thread_rpc_cancellation);
        assert!(kvm.backend.tracks_process_children);
        assert!(!kvm.backend.runs_exit_robust_list);
        assert!(!kvm.backend.requires_thread_directed_process_signals);
        assert!(kvm.backend.defers_vfork_child_registration);

        // A controlled run needs both the backend's process-signal control and
        // sequentialized threads; neither alone admits one.
        for backend in [
            Backend::Ptrace,
            Backend::Dbt,
            Backend::Sabre,
            Backend::Liteinst,
            Backend::InGuestTrap,
            Backend::E9patch,
        ] {
            let config = super::DetConfig {
                sequentialize_threads: true,
                ..super::DetConfig::default()
            };
            let prepared = prepare_backend_config(config, backend);
            assert!(!may_observe_signal_dequeues(&prepared), "{backend:?}");
            assert!(!requires_signal_control(&prepared), "{backend:?}");
        }
    }

    /// Whether Detcore asks the backend to install process signal control for
    /// `config`: the bound Reverie checks before it offers the control.
    fn may_observe_signal_dequeues(config: &DetConfig) -> bool {
        <super::Detcore as reverie::Tool>::may_observe_signal_dequeues(config)
    }

    /// Whether Reverie must refuse `config` on a runner that offers no
    /// process signal control, as Detcore's installation did before.
    fn requires_signal_control(config: &DetConfig) -> bool {
        <super::Detcore as reverie::Tool>::requires_signal_control(config)
    }

    /// The values the hand-written assignments in `prepare_backend_config` and
    /// `run_kvm` gave each backend before Detcore read
    /// [`reverie::BackendCapabilities`], transcribed field by field. A backend
    /// identity check became the capability it selected:
    /// `discover_live_file_metadata` split into the first four fields, the old
    /// `backend_reports_physical_process_exits` became two, and
    /// `backend_is_kvm` became five, of which the last two remain here:
    /// `refuses_nonleader_exec_with_enosys` became the backend's typed exec
    /// refusal (Reverie d78f770f), `failed_gettimeofday_may_store_host_time`
    /// the guest's storable-memory report (Reverie 8bfb450e) and
    /// `user_address_limit` the guest's user address limit query (Reverie
    /// 6f75cc4c). Four fields are newer than that transcription:
    /// `process_exits_complete_asynchronously` (Reverie 056c8cac), true only
    /// for in-guest LiteInst, whose process exits complete some time after the
    /// tool grants them, `supports_madv_dontneed` (Reverie 8d8697aa), true for
    /// every backend, and `reports_child_exit_publication` (Reverie 9cb6f7b7),
    /// true for ptrace and e9patch, whose tracer reports when the kernel
    /// publishes a process's exit to its parent, and
    /// `virtualizes_guest_sigalrm` (Reverie 7142ff8c), true only for in-guest
    /// LiteInst, whose runtime keeps a guest SIGALRM handler virtual (signal
    /// phase 1). `in-guest-trap` is in-guest LiteInst with site patching off,
    /// which changes how a syscall reaches the Tool and nothing the Tool host
    /// can do, so it has exactly in-guest LiteInst's row.
    /// DBT now also shares the guest descriptor table and rediscovers it after
    /// exec (Reverie 15374907); its host-timed-turn and poller-yield
    /// capabilities remain false. `reports_sigreturns` (A5 step 2) is true
    /// for ptrace, and so for e9patch preprocessing, whose guest Hermit runs
    /// on ptrace. `reports_kernel_child_exit_signals` is true for ptrace,
    /// e9patch and SaBRe, whose signal callbacks see the kernel's child-exit
    /// SIGCHLD with its siginfo and can suppress or rewrite it.
    fn golden_backend_capabilities(backend: Backend) -> serde_json::Value {
        let sabre = backend == Backend::Sabre;
        let kvm = backend == Backend::Kvm;
        let dbt = backend == Backend::Dbt;
        // LiteInst runs only through its in-guest runtime.
        let in_guest_liteinst = matches!(backend, Backend::Liteinst | Backend::InGuestTrap);
        serde_json::json!({
            "tool_shares_guest_descriptor_table": sabre || dbt,
            "rediscovers_descriptors_after_exec": sabre || dbt,
            "internal_pipe_turns_are_host_timed": sabre,
            "loopback_pollers_yield_to_peers": sabre,
            "guest_clock_reads_bypass_backend": sabre,
            "virtualizes_syscall_clobbers": sabre,
            "needs_killed_thread_rpc_cancellation": in_guest_liteinst || sabre || dbt || kvm,
            "reports_physical_process_exits": sabre,
            "process_exits_complete_asynchronously": in_guest_liteinst,
            "virtualizes_guest_sigalrm": in_guest_liteinst,
            "signal_interrupts_external_syscalls": sabre,
            "tracks_process_children": !dbt,
            "runs_exit_robust_list": matches!(backend, Backend::Ptrace | Backend::E9patch),
            "requires_thread_directed_process_signals": dbt,
            "supports_parked_write_signal_interruption":
                matches!(backend, Backend::Ptrace | Backend::E9patch | Backend::Kvm),
            "defers_vfork_child_registration": kvm,
            "supports_madvise": !kvm,
            "supports_madv_dontneed": true,
            "reports_child_exit_publication":
                matches!(backend, Backend::Ptrace | Backend::E9patch),
            "reports_kernel_child_exit_signals":
                matches!(backend, Backend::Ptrace | Backend::E9patch | Backend::Sabre),
            "provides_process_signal_control": kvm,
            "emulates_child_waits": kvm,
            // A Config field Hermit set from the backend's name before it was
            // a stated capability. e9patch runs on ptrace and has its value.
            "supports_blocked_wait_signal_interruption":
                matches!(backend, Backend::Ptrace | Backend::E9patch),
            "delivers_preemption_timer_events":
                matches!(backend, Backend::Ptrace | Backend::E9patch),
            "runs_guest_code_between_injections": sabre || in_guest_liteinst,
            "promotes_open_files_before_native_lifecycle": dbt || in_guest_liteinst,
            // Hermit runs e9patch preprocessing as a ptrace run (empty
            // trampolines keep the original syscall instruction), whose
            // rt_sigreturns reach the seccomp stop that reports them.
            "reports_sigreturns": matches!(backend, Backend::Ptrace | Backend::E9patch),
        })
    }

    #[test]
    fn backend_capabilities_match_the_golden_table() {
        for backend in [
            Backend::Ptrace,
            Backend::Kvm,
            Backend::E9patch,
            Backend::Liteinst,
            Backend::InGuestTrap,
            Backend::Sabre,
            Backend::Dbt,
        ] {
            let config = prepare_backend_config(super::DetConfig::default(), backend);
            assert_eq!(
                serde_json::to_value(config.backend).unwrap(),
                golden_backend_capabilities(backend),
                "{backend:?}"
            );
        }
    }

    #[test]
    fn backend_capabilities_are_the_ones_each_backend_reports() {
        assert_eq!(
            super::backend_capabilities(Backend::Ptrace),
            <reverie_ptrace::PtraceBackend as reverie::Backend>::capabilities()
        );
        assert_eq!(
            super::backend_capabilities(Backend::Kvm),
            reverie_kvm::KvmBackend::capabilities()
        );
        #[cfg(feature = "liteinst")]
        {
            // SAFETY: the strong immutable declaration above has the public
            // repr(C), two-u64 descriptor ABI. This reads the real linked
            // object, so changing the host declaration to Fixed fails here.
            let backing = unsafe {
                std::ptr::addr_of!(super::reverie_liteinst_allocator_backing_v1)
                    .cast::<[u64; 2]>()
                    .read_volatile()
            };
            assert_eq!(backing, [1, 1], "host Hermit must declare Legacy backing");
            assert_eq!(
                super::backend_capabilities(Backend::Liteinst),
                <reverie_liteinst::LiteinstBackend as reverie::Backend>::capabilities()
            );
            // in-guest-trap runs the same Reverie backend.
            assert_eq!(
                super::backend_capabilities(Backend::InGuestTrap),
                <reverie_liteinst::LiteinstBackend as reverie::Backend>::capabilities()
            );
        }
        #[cfg(feature = "dbt")]
        {
            assert_eq!(
                super::backend_capabilities(Backend::Dbt),
                reverie_dbt::DbtRunner::capabilities()
            );
            // The DBT runtime re-asserts the same value when it loads the
            // configuration this crate serializes for it.
            assert_eq!(
                super::backend_capabilities(Backend::Dbt),
                detcore_dbt::backend_capabilities()
            );
        }
    }

    /// `Config` with its three environment-backed fields (`HERMIT_EPOCH`,
    /// `HERMIT_PRNG`, `HERMIT_SCHED_SEED`) pinned, so the encoding does not
    /// depend on the test process's environment.
    fn env_free_detconfig(epoch: &str, seed: &str) -> DetConfig {
        use clap::Parser;
        let mut config = DetConfig::parse_from(["hermit", epoch, seed, "--sched-seed=0"]);
        config.sched_seed = None;
        config
    }

    /// Non-default values in fields that serialize as strings, floats,
    /// options and nested arrays. The path contains legacy key names, so an
    /// encoder that edited text would put a key inside a string value.
    fn rich_detconfig() -> DetConfig {
        let mut config = env_free_detconfig("--epoch=2000-12-31T23:59:59Z", "--seed=7");
        config.rng_seed = Some(13);
        config.sched_seed = Some(11);
        config.clock_multiplier = Some(1.5);
        config.chaos = true;
        config.record_preemptions_to = Some(std::path::PathBuf::from(
            "/tmp/a,\"detect_host_clock_futex_timeouts\":true,\"backend_tracks_process_children\":{",
        ));
        config.mountinfo_root_rewrites = vec![detcore_model::config::MountInfoRootRewrite {
            raw_mount_id: 41,
            deterministic_root: b"/".to_vec(),
            raw_root_prefix: Some(b"/tmp/x".to_vec()),
            deterministic_root_prefix: Some(b"/tmp".to_vec()),
            raw_mountpoint_prefix: None,
            deterministic_mountpoint_prefix: None,
        }];
        config.stacktrace_event = vec![(3, Some(std::path::PathBuf::from("/s")))];
        config
    }

    /// `HERMIT_DBT_DETCONFIG` is in the guest's environment, so a guest can
    /// read it with `getenv` or `/proc/self/environ`. These fixtures are the
    /// output of `serde_json::to_string` on the same configs, captured from a
    /// build of the commit before Detcore read [`reverie::BackendCapabilities`],
    /// when `Config` still had one boolean field per backend switch. They are
    /// the variable's bytes before `dbt_detconfig_json` re-inserts the three
    /// deleted switches' keys; `dbt_detconfig_matches_806cf2fa_bytes` pins the
    /// complete variable. Every DBT run sequentializes threads, so the DBT
    /// cases do too. The fixtures carry one deliberate later change, the
    /// `fdinfo_unlisted_mount_ids` -> `mount_id_assignment_order` key rename
    /// described on `dbt_detconfig_tests::GOLDEN`. Preserve the historical DBT
    /// descriptor capabilities here as well as the historical bytes. Current
    /// DBT's independent descriptor facts require the typed envelope, whose
    /// exact round trip and all sixteen coupled-flag combinations are checked
    /// by `dbt_detconfig_tests`.
    #[test]
    fn historical_dbt_capabilities_preserve_legacy_detconfig_bytes() {
        let sequential = |mut config: DetConfig| {
            config.sequentialize_threads = true;
            config
        };
        let default_config = || env_free_detconfig("--epoch=2026-01-01T00:00:00Z", "--seed=0");
        for (name, mut config, before) in [
            (
                "default",
                default_config(),
                include_str!("../tests/fixtures/dbt/detconfig-before-capabilities-default.json"),
            ),
            (
                "dbt",
                prepare_backend_config(sequential(default_config()), Backend::Dbt),
                include_str!("../tests/fixtures/dbt/detconfig-before-capabilities-dbt.json"),
            ),
            (
                "rich_dbt",
                prepare_backend_config(sequential(rich_detconfig()), Backend::Dbt),
                include_str!("../tests/fixtures/dbt/detconfig-before-capabilities-rich_dbt.json"),
            ),
        ] {
            config.backend.tool_shares_guest_descriptor_table = false;
            config.backend.rediscovers_descriptors_after_exec = false;
            // Read back as false, as the `Config` fields they replace were.
            config.backend.supports_blocked_wait_signal_interruption = false;
            config.backend.delivers_preemption_timer_events = false;
            config.backend.runs_guest_code_between_injections = false;
            assert_eq!(
                detcore::to_legacy_backend_json(&config).unwrap(),
                before,
                "HERMIT_DBT_DETCONFIG for the {name} config changed"
            );
            // A reader of the earlier bytes recovers the capabilities.
            let decoded = detcore::from_legacy_backend_json(before).unwrap();
            assert_eq!(decoded.backend, config.backend, "{name}");
            assert_eq!(
                decoded.sequentialize_threads, config.sequentialize_threads,
                "{name}"
            );
            assert_eq!(
                detcore::to_legacy_backend_json(&decoded).unwrap(),
                before,
                "{name}"
            );
        }
    }

    /// The fifteen per-backend keys, in the order the encoding has always
    /// carried them.
    const LEGACY_BACKEND_KEYS: [&str; 15] = [
        "cpuid_virtualized_by_backend",
        "backend_supports_madvise",
        "discover_live_file_metadata",
        "detect_host_clock_futex_timeouts",
        "syscall_clobbers_virtualized_by_backend",
        "cancel_killed_thread_rpcs",
        "backend_reports_physical_process_exits",
        "backend_tracks_process_children",
        "backend_runs_exit_robust_list",
        "backend_requires_thread_directed_process_signals",
        "backend_is_kvm",
        "kvm_shared_dequeue_timers",
        "backend_supports_parked_write_signal_interruption",
        "backend_virtualizes_capability_prctls",
        "backend_defers_vfork_child_registration",
    ];

    /// Pin the current projection onto all fifteen legacy backend keys, once
    /// without and once with sequentialized threads. DBT's two descriptor
    /// facts project to `discover_live_file_metadata=true`; that legacy bit
    /// cannot retain their independence from the host-timed capabilities.
    /// Current DBT therefore uses the separately checked typed envelope, not
    /// this lossy projection. LiteInst's row is its in-guest runtime. KVM's
    /// first two digits are the capabilities `run_kvm` formerly set itself; the
    /// first, `cpuid_virtualized_by_backend`, is written false for every
    /// backend now that no backend installs a CPUID profile of its own, and so
    /// is the fourteenth, `backend_virtualizes_capability_prctls`, now that
    /// the capability prctls reach the kernel on every backend.
    #[test]
    fn every_backend_encodes_its_current_legacy_projection() {
        for (backend, unsequenced, sequential) in [
            (Backend::Ptrace, "010000011000100", "010000011000100"),
            (Backend::E9patch, "010000011000100", "010000011000100"),
            (Backend::Liteinst, "010001010000000", "010001010000000"),
            (Backend::InGuestTrap, "010001010000000", "010001010000000"),
            (Backend::Sabre, "011111110000000", "011111110000000"),
            (Backend::Dbt, "011001000100000", "011001000100000"),
            (Backend::Kvm, "000001010010101", "000001010011101"),
        ] {
            for (sequentialize_threads, expected) in [(false, unsequenced), (true, sequential)] {
                let config = prepare_backend_config(
                    DetConfig {
                        sequentialize_threads,
                        ..DetConfig::default()
                    },
                    backend,
                );
                let encoded: serde_json::Map<String, serde_json::Value> =
                    serde_json::from_str(&detcore::to_legacy_backend_json(&config).unwrap())
                        .unwrap();
                let digits: String = LEGACY_BACKEND_KEYS
                    .iter()
                    .map(|key| match encoded.get(*key) {
                        Some(serde_json::Value::Bool(true)) => '1',
                        Some(serde_json::Value::Bool(false)) => '0',
                        other => panic!("{key} encoded as {other:?}"),
                    })
                    .collect();
                assert_eq!(
                    digits, expected,
                    "{backend:?}, sequentialize_threads={sequentialize_threads}"
                );
            }
        }
    }

    #[test]
    fn dbt_backend_config_translates_process_signals_to_host_threads() {
        let config = prepare_backend_config(super::DetConfig::default(), Backend::Dbt);
        assert!(config.backend.needs_killed_thread_rpc_cancellation);
        assert!(!config.backend.tracks_process_children);
        assert!(!config.backend.runs_exit_robust_list);
        assert!(config.backend.requires_thread_directed_process_signals);
        assert!(!config.backend.defers_vfork_child_registration);
    }

    #[test]
    fn backend_write_signal_contract_is_explicit() {
        for (backend, supports_interruption) in [
            (Backend::Ptrace, true),
            (Backend::Dbt, false),
            (Backend::Kvm, true),
            (Backend::Sabre, false),
            (Backend::Liteinst, false),
            (Backend::InGuestTrap, false),
            (Backend::E9patch, true),
        ] {
            let config = prepare_backend_config(super::DetConfig::default(), backend);
            assert_eq!(
                config.backend.supports_parked_write_signal_interruption, supports_interruption,
                "unexpected parked-write signal support for {backend:?}"
            );
        }
    }

    /// Only ptrace delivers the preemption timer, so only it lets a chaos
    /// `sched_yield` hand its turn to a thread that may never make a syscall
    /// (https://github.com/rrnewton/hermit/issues/4068).
    #[test]
    fn backend_timer_delivery_is_explicit() {
        for (backend, delivers) in [
            (Backend::Ptrace, true),
            (Backend::Dbt, false),
            (Backend::Kvm, false),
            (Backend::Sabre, false),
            (Backend::Liteinst, false),
            (Backend::InGuestTrap, false),
            (Backend::E9patch, true),
        ] {
            let config = prepare_backend_config(super::DetConfig::default(), backend);
            assert_eq!(
                config.backend.delivers_preemption_timer_events, delivers,
                "unexpected timer delivery for {backend:?}"
            );
        }
    }

    /// Only SaBRe and in-guest LiteInst run guest code inside a Tool handler,
    /// between two injected calls.
    #[test]
    fn backend_guest_code_between_injections_is_explicit() {
        for (backend, runs) in [
            (Backend::Ptrace, false),
            (Backend::Dbt, false),
            (Backend::Kvm, false),
            (Backend::Sabre, true),
            (Backend::Liteinst, true),
            (Backend::InGuestTrap, true),
            (Backend::E9patch, false),
        ] {
            let config = prepare_backend_config(super::DetConfig::default(), backend);
            assert_eq!(
                config.backend.runs_guest_code_between_injections, runs,
                "unexpected guest code between injections for {backend:?}"
            );
        }
    }

    #[test]
    fn backend_blocked_wait_signal_contract_is_explicit() {
        for (backend, supports_interruption) in [
            (Backend::Ptrace, true),
            (Backend::Dbt, false),
            (Backend::Kvm, false),
            (Backend::Sabre, false),
            (Backend::Liteinst, false),
            (Backend::InGuestTrap, false),
            (Backend::E9patch, true),
        ] {
            let config = prepare_backend_config(super::DetConfig::default(), backend);
            assert_eq!(
                config.backend.supports_blocked_wait_signal_interruption, supports_interruption,
                "unexpected blocked-wait signal support for {backend:?}"
            );
        }
    }

    /// A launch may pass the guest a terminal when a standard descriptor is
    /// one, when the launcher has a controlling terminal, or when that cannot
    /// be read; only a launch with neither is known not to. The device number
    /// is read past a command name that holds spaces and parentheses.
    #[test]
    fn a_launch_may_pass_a_terminal_unless_it_proves_it_has_none() {
        assert!(!super::may_pass_a_terminal(false, Some(0)));
        assert!(super::may_pass_a_terminal(true, Some(0)));
        assert!(super::may_pass_a_terminal(false, Some(34817)));
        assert!(super::may_pass_a_terminal(false, None));

        let stat = "4242 (a) b (c)) S 1 4242 4242 34817 4242 4194560 0 0 0 0";
        assert_eq!(super::parse_controlling_terminal_device(stat), Some(34817));
        let stat = "4242 (hermit) S 1 4242 4242 0 -1 4194560 0 0 0 0";
        assert_eq!(super::parse_controlling_terminal_device(stat), Some(0));
        assert_eq!(
            super::parse_controlling_terminal_device("4242 (hermit"),
            None
        );
        assert_eq!(
            super::parse_controlling_terminal_device("4242 (hermit) S 1"),
            None
        );
        assert!(super::controlling_terminal_device().is_some());
    }

    /// Only a backend whose blocked waits decide interruption from the kernel's
    /// signal state asks whether the launch may pass the guest a terminal.
    #[test]
    fn only_gated_backends_record_an_inherited_terminal() {
        for backend in [
            Backend::Dbt,
            Backend::Kvm,
            Backend::Sabre,
            Backend::Liteinst,
            Backend::InGuestTrap,
            Backend::E9patch,
        ] {
            let config = prepare_backend_config(super::DetConfig::default(), backend);
            assert!(
                !config.guest_may_inherit_a_terminal,
                "unexpected terminal record for {backend:?}"
            );
        }
        let config = prepare_backend_config(super::DetConfig::default(), Backend::Ptrace);
        assert_eq!(
            config.guest_may_inherit_a_terminal,
            super::launcher_may_pass_a_terminal()
        );
    }

    #[test]
    fn liteinst_public_dispatch_runs_in_guest_detcore() {
        if Backend::Liteinst.ensure_available().is_err() {
            return;
        }

        // In-guest LiteInst has no preemption timer and refuses a maximum
        // timeslice, so this run disables preemption; preemption is not what
        // this test checks.
        let config = super::DetConfig {
            max_timeslice: None,
            ..super::DetConfig::default()
        };
        let mut command = super::Command::new("/bin/echo");
        command.arg("hello");
        let output = super::run_with_output_backend(
            command,
            config.clone(),
            false,
            &None,
            Backend::Liteinst,
        )
        .expect("run /bin/echo through in-guest LiteInst");
        // Include captured output so a failed backend launch reports its diagnostics.
        assert_eq!(
            output.status,
            super::ExitStatus::Exited(0),
            "guest did not exit 0.\n--- guest stderr ---\n{}\n--- guest stdout ---\n{}",
            String::from_utf8_lossy(&output.stderr),
            String::from_utf8_lossy(&output.stdout),
        );
        assert_eq!(output.stdout, b"hello\n");

        let status = super::run_with_backend(
            super::Command::new("/bin/true"),
            config,
            false,
            &None,
            Backend::Liteinst,
        )
        .expect("run /bin/true through in-guest LiteInst");
        assert_eq!(status, super::ExitStatus::Exited(0));
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn default_and_available_backends_reflect_host_probes() {
        assert_eq!(Backend::default(), Backend::Ptrace);
        let available = Backend::available().collect::<Vec<_>>();
        assert_eq!(
            available.contains(&Backend::Ptrace),
            Backend::Ptrace.is_available()
        );
        assert_eq!(
            available.contains(&Backend::Dbt),
            dynamorio_sdk_available() && dbt_runtime_unavailable_reason().is_none()
        );
        assert_eq!(
            available.contains(&Backend::Liteinst),
            liteinst_unavailable_reason().is_none()
        );
        // The same runtime, so available exactly when liteinst is.
        assert_eq!(
            available.contains(&Backend::InGuestTrap),
            available.contains(&Backend::Liteinst)
        );
        assert_eq!(
            available.contains(&Backend::Sabre),
            Backend::Sabre.is_available()
        );
        assert_eq!(
            available.contains(&Backend::Kvm),
            kvm_device_unavailable_reason(std::path::Path::new("/dev/kvm")).is_none(),
        );
        assert_eq!(
            available.contains(&Backend::E9patch),
            Backend::E9patch.is_available()
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dependency_probes_require_usable_paths() {
        let temp = tempfile::tempdir().unwrap();
        assert!(!is_dynamorio_sdk(temp.path()));
        fs::create_dir(temp.path().join("include")).unwrap();
        fs::write(temp.path().join("include/dr_api.h"), b"/* marker */").unwrap();
        assert!(is_dynamorio_sdk(temp.path()));

        let reason = kvm_device_unavailable_reason(temp.path())
            .expect("a directory must not pass the read-write KVM device probe");
        assert!(reason.contains("read-write"));
    }

    #[cfg(feature = "sabre")]
    fn write_test_executable(path: &std::path::Path) {
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, b"test loader").unwrap();
        let mut permissions = fs::metadata(path).unwrap().permissions();
        permissions.set_mode(0o700);
        fs::set_permissions(path, permissions).unwrap();
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_rpc_socket_uses_private_exec_environment() {
        assert!(SABRE_RPC_SOCKET_ENV.starts_with("REVERIE_SABRE_"));
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_stages_program_names_that_collide_with_loader_prefix() {
        assert!(sabre_program_needs_neutral_name(
            PathBuf::from("/usr/bin/ld").as_path()
        ));
        assert!(sabre_program_needs_neutral_name(
            PathBuf::from("/usr/bin/ld.bfd").as_path()
        ));
        assert!(!sabre_program_needs_neutral_name(
            PathBuf::from("/usr/bin/gold").as_path()
        ));
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_neutral_name_staging_preserves_program_bytes_and_cleans_up() {
        let temp = tempfile::tempdir().unwrap();
        let program = temp.path().join("ld.test");
        fs::write(&program, b"linker image").unwrap();

        let staged = stage_sabre_program_in(&program, temp.path())
            .unwrap()
            .expect("ld-prefixed program should be staged");
        let staged_path = staged.path.clone();
        assert_eq!(fs::read(&staged_path).unwrap(), b"linker image");
        assert!(
            staged_path
                .file_name()
                .unwrap()
                .as_encoded_bytes()
                .starts_with(b"hermit-sabre-program-")
        );

        drop(staged);
        assert!(!staged_path.exists());
    }

    #[tokio::test(start_paused = true)]
    #[cfg(feature = "sabre")]
    async fn sabre_rpc_disconnect_wait_observes_delayed_release() {
        let global = Arc::new(());
        let connection = global.clone();
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(10)).await;
            drop(connection);
        });

        assert_eq!(
            wait_for_sabre_rpc_disconnects(&global, Duration::from_millis(50)).await,
            Ok(())
        );
    }

    #[tokio::test(start_paused = true)]
    #[cfg(feature = "sabre")]
    async fn sabre_rpc_disconnect_wait_reports_stuck_connection() {
        let global = Arc::new(());
        let _connection = global.clone();

        assert_eq!(
            wait_for_sabre_rpc_disconnects(&global, Duration::from_millis(10)).await,
            Err(1)
        );
    }

    #[tokio::test]
    #[cfg(feature = "sabre")]
    async fn sabre_rpc_server_intentional_abort_is_clean() {
        let server_task = tokio::spawn(std::future::pending::<Result<(), &'static str>>());

        assert!(stop_sabre_rpc_server(server_task).await.is_ok());
    }

    #[tokio::test]
    #[cfg(feature = "sabre")]
    async fn sabre_rpc_server_failure_is_reported() {
        let server_task = tokio::spawn(async { Err::<(), _>("accept failed") });
        while !server_task.is_finished() {
            tokio::task::yield_now().await;
        }

        let error = stop_sabre_rpc_server(server_task).await.unwrap_err();
        assert!(error.to_string().contains("accept failed"));
    }

    #[tokio::test(start_paused = true)]
    #[cfg(feature = "sabre")]
    async fn sabre_rpc_shutdown_drains_connections_after_server_failure() {
        let global = Arc::new(());
        let connection = global.clone();
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(10)).await;
            drop(connection);
        });
        let server_task = tokio::spawn(async { Err::<(), _>("accept failed") });
        while !server_task.is_finished() {
            tokio::task::yield_now().await;
        }

        let error = shutdown_sabre_rpc(server_task, &global, Duration::from_millis(50))
            .await
            .unwrap_err();
        assert!(error.to_string().contains("accept failed"));
        assert_eq!(Arc::strong_count(&global), 1);
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_binary_resolver_finds_cargo_target_build() {
        let temp = tempfile::tempdir().unwrap();
        let executable = temp.path().join("target/release/hermit");
        let loader = temp.path().join("target/sabre/sabre");
        write_test_executable(&loader);

        assert_eq!(
            resolve_sabre_binary_from(None, None, &executable, OsStr::new("")).unwrap(),
            loader
        );
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_binary_resolver_uses_packaged_loader() {
        let temp = tempfile::tempdir().unwrap();
        let executable = temp.path().join("target/release/hermit");
        let packaged = temp.path().join("install_pkg/rsrcs/sabre");
        write_test_executable(&packaged);

        assert_eq!(
            resolve_sabre_binary_from(None, Some(&packaged), &executable, OsStr::new("")).unwrap(),
            packaged
        );
    }

    #[test]
    #[cfg(feature = "sabre")]
    fn sabre_binary_resolver_prefers_and_validates_override() {
        let temp = tempfile::tempdir().unwrap();
        let executable = temp.path().join("target/release/hermit");
        let discovered = temp.path().join("target/sabre/sabre");
        let requested = temp.path().join("requested-sabre");
        write_test_executable(&discovered);
        write_test_executable(&requested);

        assert_eq!(
            resolve_sabre_binary_from(
                Some(requested.as_os_str()),
                Some(&discovered),
                &executable,
                OsStr::new(""),
            )
            .unwrap(),
            requested
        );

        let mut permissions = fs::metadata(&requested).unwrap().permissions();
        permissions.set_mode(0o600);
        fs::set_permissions(&requested, permissions).unwrap();
        let error = resolve_sabre_binary_from(
            Some(requested.as_os_str()),
            Some(&discovered),
            &executable,
            OsStr::new(""),
        )
        .unwrap_err();
        assert!(error.to_string().contains("is not an executable file"));
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn optional_backends_report_accurate_availability() {
        match Backend::Dbt.ensure_available() {
            Ok(()) => assert!(
                dynamorio_sdk_available() && dbt_runtime_unavailable_reason().is_none(),
                "DBT reported available without its SDK and runtime"
            ),
            Err(dbt_error) => {
                let message = dbt_error.to_string();
                assert!(
                    message.contains("DynamoRIO runtime")
                        || message.contains("Detcore DBT runtime"),
                    "unexpected DBT availability error: {message}"
                );
            }
        }
        assert_eq!(
            Backend::Liteinst.ensure_available().is_ok(),
            liteinst_unavailable_reason().is_none()
        );
        assert_eq!(
            Backend::InGuestTrap.ensure_available().is_ok(),
            liteinst_unavailable_reason().is_none()
        );

        match Backend::Kvm.ensure_available() {
            Ok(()) => assert!(
                kvm_device_unavailable_reason(std::path::Path::new("/dev/kvm")).is_none(),
                "KVM reported available without a usable /dev/kvm",
            ),
            Err(kvm_error) => {
                let message = kvm_error.to_string();
                assert!(
                    message.contains("/dev/kvm"),
                    "unexpected KVM availability error: {message}",
                );
                assert!(!message.contains("requires root privileges"));
            }
        }
    }

    #[test]
    fn public_backend_dispatch_rejects_unprepared_e9patch() {
        let error = ensure_backend_dispatch(Backend::E9patch).unwrap_err();
        assert!(
            error.to_string().contains("requires CLI preprocessing"),
            "unexpected error: {error}"
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_retries_only_the_pre_guest_bootstrap_failure() {
        use std::os::unix::process::ExitStatusExt as _;

        let failure = std::process::ExitStatus::from_raw(
            reverie_dbt::CLIENT_THREAD_START_FAILURE_EXIT_CODE << 8,
        );
        assert!(super::dbt_client_thread_start_failed(&failure));
        assert!(!super::dbt_client_thread_start_failed(
            &std::process::ExitStatus::from_raw(1 << 8)
        ));
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_public_dispatch_requires_sequentialized_threads() {
        let command = super::Command::new("/bin/true");
        let config = super::DetConfig {
            sequentialize_threads: false,
            ..Default::default()
        };

        let error = super::run_with_output_backend(command, config, false, &None, Backend::Dbt)
            .expect_err("DBT must reject non-sequentialized execution");
        assert!(
            error
                .to_string()
                .contains("dbt backend requires sequentialized threads"),
            "unexpected error: {error}"
        );
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_public_dispatch_runs_echo_through_detcore() {
        use clap::Parser;

        if Backend::Dbt.ensure_available().is_err() {
            return;
        }

        // Isolate the controller's fd 2 in a subprocess, without redirecting a
        // descriptor shared by other tests in this process.
        const CHILD: &str = "HERMIT_TEST_DBT_LIBRARY_DIAGNOSTICS_CHILD";
        if std::env::var_os(CHILD).is_none() {
            let output = std::process::Command::new(std::env::current_exe().unwrap())
                .args([
                    "--exact",
                    "tests::dbt_public_dispatch_runs_echo_through_detcore",
                    "--nocapture",
                ])
                .env(CHILD, "1")
                .output()
                .expect("run isolated DBT library output checks");
            let diagnostics = String::from_utf8_lossy(&output.stderr);
            assert!(output.status.success(), "{output:?}");
            assert!(
                diagnostics
                    .lines()
                    .filter(|line| line.starts_with("reverie-dbt: tool=Detcore "))
                    .count()
                    >= 2,
                "DBT native summaries did not prove both Detcore dispatches: {diagnostics}"
            );
            assert!(!diagnostics.contains("guest-stderr"), "{diagnostics}");
            return;
        }

        let mut command = super::Command::new("/bin/echo");
        command.arg("hello");
        let mut config = super::DetConfig::parse_from(["hermit-dbt-test"]);
        config.sequentialize_threads = true;
        config.validate();
        let output = super::run_with_output_backend(command, config, true, &None, Backend::Dbt)
            .expect("run /bin/echo through DbtGuest<Detcore>");

        // Include captured output so a failed backend launch reports its diagnostics.
        assert_eq!(
            output.status,
            super::ExitStatus::Exited(0),
            "guest did not exit 0.\n--- guest stderr ---\n{}\n--- guest stdout ---\n{}",
            String::from_utf8_lossy(&output.stderr),
            String::from_utf8_lossy(&output.stdout),
        );
        assert_eq!(output.stdout, b"hello\n");
        assert_eq!(output.stderr, b"");

        let mut command = super::Command::new("/bin/sh");
        command.args(["-c", "printf 'guest-stderr\\n' >&2"]);
        let mut config = super::DetConfig::parse_from(["hermit-dbt-test"]);
        config.sequentialize_threads = true;
        config.validate();
        let output = super::run_with_output_backend(command, config, true, &None, Backend::Dbt)
            .expect("capture nonempty guest stderr through DbtGuest<Detcore>");
        assert_eq!(output.status, super::ExitStatus::Exited(0), "{output:?}");
        assert_eq!(output.stdout, b"");
        assert_eq!(output.stderr, b"guest-stderr\n");
    }

    #[test]
    #[cfg(feature = "dbt")]
    fn dbt_public_status_dispatch_runs_true_through_detcore() {
        use clap::Parser;

        if Backend::Dbt.ensure_available().is_err() {
            return;
        }

        let command = super::Command::new("/bin/true");
        let mut config = super::DetConfig::parse_from(["hermit-dbt-test"]);
        config.sequentialize_threads = true;
        config.validate();
        let status = super::run_with_backend(command, config, true, &None, Backend::Dbt)
            .expect("run /bin/true through DbtGuest<Detcore>");

        assert_eq!(status, super::ExitStatus::Exited(0));
    }

    #[test]
    fn kvm_runs_dynamic_echo_through_detcore() {
        use clap::Parser;

        if kvm_device_unavailable_reason(std::path::Path::new("/dev/kvm")).is_some() {
            return;
        }

        let mut command = super::Command::new("/bin/echo");
        command.arg("hello");
        let mut config = super::DetConfig::parse_from(["hermit-kvm-test"]);
        config.validate();
        let output = super::run_with_output_backend(command, config, false, &None, Backend::Kvm)
            .expect("run dynamic /bin/echo through KvmGuest<Detcore>");

        // Include captured output so a failed backend launch reports its diagnostics.
        assert_eq!(
            output.status,
            super::ExitStatus::Exited(0),
            "guest did not exit 0.\n--- guest stderr ---\n{}\n--- guest stdout ---\n{}",
            String::from_utf8_lossy(&output.stderr),
            String::from_utf8_lossy(&output.stdout),
        );
        assert_eq!(output.stdout, b"hello\n");
        assert!(output.stderr.is_empty());
    }

    // Keep the low-level vmcall transport covered independently from the ELF
    // process personality. Requires /dev/kvm; skips cleanly otherwise.
    #[test]
    fn detcore_drives_kvm_guest_for_synthetic_syscall() {
        use clap::Parser;

        const MEMORY_SIZE: usize = 0x10_000;
        const ENTRY_POINT: u64 = 0x1000;
        const FRAME_ADDRESS: u64 = 0x2000;

        let mut backend = match reverie_kvm::KvmBackend::new(MEMORY_SIZE) {
            Ok(backend) => backend,
            Err(error) => {
                eprintln!("skipping KVM Detcore experiment: cannot init VM: {error}");
                return;
            }
        };

        // A guest that issues one `getpid` through the vmcall transport, then HLTs.
        backend
            .install_syscall(
                ENTRY_POINT,
                FRAME_ADDRESS,
                reverie_kvm::SyscallRequest::new(libc::SYS_getpid as u64, [0; 6]),
            )
            .expect("install synthetic getpid guest");

        // Minimal deterministic Detcore config with RCB preemption disabled.
        let mut config =
            super::DetConfig::parse_from(["hermit-kvm-test", "--max-timeslice=disabled"]);
        config.validate();

        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("build tokio runtime");

        let outcome = runtime.block_on(async {
            backend
                .run_with_tool::<super::Detcore, _>(
                    config,
                    // Executor: forward anything Detcore injects to the host.
                    |request: &reverie_kvm::SyscallRequest, _memory: &reverie_kvm::GuestMemory| {
                        // SAFETY: forwarding a register-only syscall (getpid) to the
                        // host; no guest pointers are dereferenced by the kernel.
                        unsafe {
                            libc::syscall(
                                request.number() as libc::c_long,
                                request.args()[0],
                                request.args()[1],
                                request.args()[2],
                                request.args()[3],
                                request.args()[4],
                                request.args()[5],
                            ) as i64
                        }
                    },
                )
                .await
        });

        // The point of the experiment is to observe whether Detcore can be driven
        // to completion over KvmGuest at all; assert it did not error.
        outcome.expect("Detcore drove the synthetic KVM guest to completion");
    }

    // Minimal fake ELF payload: the loader only needs the image to NOT start
    // with `#!`, and a real ELF magic makes the intent obvious.
    const FAKE_ELF: &[u8] = b"\x7fELF\x02\x01\x01\x00 fake elf body";

    fn shebang_tmpdir(tag: &str) -> PathBuf {
        let dir = std::env::temp_dir().join(format!(
            "hermit-shebang-test-{}-{}",
            std::process::id(),
            tag
        ));
        let _ = fs::remove_dir_all(&dir);
        fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn resolve_shebang_plain_elf_is_unchanged() {
        let dir = shebang_tmpdir("plain");
        let prog = dir.join("prog");
        fs::write(&prog, FAKE_ELF).unwrap();

        let argv = vec!["prog".to_owned(), "-a".to_owned()];
        let (path, out_argv, image) = resolve_kvm_shebang(&prog, argv).unwrap();
        assert_eq!(path, prog);
        assert_eq!(out_argv, vec!["prog".to_owned(), "-a".to_owned()]);
        assert_eq!(image, FAKE_ELF);
        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn resolve_shebang_single_level_kernel_order() {
        let dir = shebang_tmpdir("single");
        let interp = dir.join("fakebash");
        fs::write(&interp, FAKE_ELF).unwrap();
        let script = dir.join("script");
        // Interpreter with a single optional argument.
        fs::write(&script, format!("#!{} -x\necho hi\n", interp.display())).unwrap();

        let argv = vec!["script".to_owned(), "arg1".to_owned()];
        let (path, out_argv, image) = resolve_kvm_shebang(&script, argv).unwrap();
        assert_eq!(path, interp);
        // Kernel order: [interp, optarg, script_path, original args after argv[0]].
        assert_eq!(
            out_argv,
            vec![
                interp.to_string_lossy().into_owned(),
                "-x".to_owned(),
                script.to_string_lossy().into_owned(),
                "arg1".to_owned(),
            ]
        );
        assert_eq!(image, FAKE_ELF);
        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn resolve_shebang_nested_accumulates_like_binfmt_script() {
        let dir = shebang_tmpdir("nested");
        let interp = dir.join("fakebash");
        fs::write(&interp, FAKE_ELF).unwrap();
        let mid = dir.join("mid"); // a #!-interpreter that is itself a script
        fs::write(&mid, format!("#!{}\n", interp.display())).unwrap();
        let script = dir.join("script");
        fs::write(&script, format!("#!{} -e\n", mid.display())).unwrap();

        let argv = vec!["script".to_owned(), "arg1".to_owned()];
        let (path, out_argv, image) = resolve_kvm_shebang(&script, argv).unwrap();
        assert_eq!(path, interp);
        // Level 1: [mid, -e, script, arg1]; level 2 prepends [interp, mid].
        assert_eq!(
            out_argv,
            vec![
                interp.to_string_lossy().into_owned(),
                mid.to_string_lossy().into_owned(),
                "-e".to_owned(),
                script.to_string_lossy().into_owned(),
                "arg1".to_owned(),
            ]
        );
        assert_eq!(image, FAKE_ELF);
        fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn resolve_shebang_rejects_infinite_recursion() {
        let dir = shebang_tmpdir("loop");
        let a = dir.join("a");
        let b = dir.join("b");
        fs::write(&a, format!("#!{}\n", b.display())).unwrap();
        fs::write(&b, format!("#!{}\n", a.display())).unwrap();

        let argv = vec!["a".to_owned()];
        assert!(resolve_kvm_shebang(&a, argv).is_err());
        fs::remove_dir_all(&dir).unwrap();
    }

    /// The build scripts' pin reader, bracketed against the inputs that
    /// first-match-wins got wrong. Included rather than imported because the
    /// reader is build-script code with no library home.
    mod reverie_pin_reader {
        include!("../reverie_pin.rs");

        const A: &str = "1111111111111111111111111111111111111111";
        const B: &str = "2222222222222222222222222222222222222222";

        fn dep(rev: &str) -> String {
            format!(
                "reverie = {{ git = \"https://github.com/rrnewton/reverie.git\", rev = \"{rev}\" }}\n"
            )
        }

        #[test]
        fn a_single_revision_is_read() {
            assert_eq!(parse_reverie_pin(&dep(A)).as_deref(), Some(A));
        }

        #[test]
        fn agreeing_revisions_are_read_once() {
            let text = format!("{}{}", dep(A), dep(A));
            assert_eq!(parse_reverie_pin(&text).as_deref(), Some(A));
        }

        /// A manifest halfway through a bump names two revisions. First-match-wins
        /// silently returned the first; the canonical rule refuses to resolve it.
        #[test]
        fn disagreeing_revisions_are_refused() {
            let text = format!("{}{}", dep(A), dep(B));
            assert_eq!(parse_reverie_pin(&text), None);
        }

        /// The reported reproduction: ONE commented line above the live pin made
        /// the binary embed the comment's revision, so a correctly staged runtime
        /// would be refused by a guard naming a revision that was never the pin.
        #[test]
        fn a_commented_out_dependency_is_not_the_pin() {
            let text = format!("# {}{}", dep(B), dep(A));
            assert_eq!(
                parse_reverie_pin(&text).as_deref(),
                Some(A),
                "a commented-out revision must not win"
            );
        }

        #[test]
        fn a_manifest_naming_no_revision_yields_none() {
            assert_eq!(parse_reverie_pin("[dependencies]\nlibc = \"0.2\"\n"), None);
        }

        /// An unparseable Reverie line could be hiding a disagreement, so it makes
        /// the manifest ambiguous rather than being skipped as absent.
        #[test]
        fn an_unparseable_revision_is_ambiguous() {
            let text = format!(
                "{}reverie-x = {{ git = \"https://github.com/rrnewton/reverie.git\", rev = \"beef\" }}\n",
                dep(A)
            );
            assert_eq!(parse_reverie_pin(&text), None);
        }
    }

    /// SaBRe's guest plugin has no subscriber, so it forwards DETLOG records by
    /// the policy the CLI hands it. For each filter the CLI builds, that policy
    /// must answer every emitting module as the CLI's own subscriber does,
    /// which is what each in-process `detlog!` callsite asks under ptrace.
    /// `warn,detcore::random=info` once yielded no forwarding at all, because
    /// only the generic `detcore` target was asked, so SaBRe dropped the
    /// AT_RANDOM and early getrandom records ptrace prints.
    #[cfg(feature = "sabre")]
    #[test]
    fn sabre_detlog_forwarding_answers_each_target_as_the_cli_filter_does() {
        use detcore::detlog::ForwardPolicy;
        use detcore::detlog::info_enabled_for_target;
        use tracing::level_filters::LevelFilter;
        use tracing_subscriber::layer::SubscriberExt;

        use crate::liteinst_bootstrap::EffectiveFilter;

        let targets = [
            "",
            "detcore",
            "detcore::random",
            "detcore::randomness",
            "detcore::tool_local",
            "detcore::scheduler",
            "detcore::scheduler::runqueue",
            "detcore::syscalls::files",
            "hermit",
            "warn",
        ];
        let policy_under = |raw: &str, level: LevelFilter| {
            let subscriber = tracing_subscriber::registry()
                .with(EffectiveFilter::from_directives_lossy(raw, level).into_filter());
            tracing::subscriber::with_default(subscriber, || {
                let policy = ForwardPolicy::from_current_subscriber(raw);
                let subscriber_says: Vec<bool> = targets
                    .iter()
                    .map(|target| info_enabled_for_target(target))
                    .collect();
                (policy, subscriber_says)
            })
        };
        for (raw, level) in [
            ("warn,detcore::random=info", LevelFilter::WARN),
            ("detcore::random=info", LevelFilter::WARN),
            ("detcore::random=warn", LevelFilter::INFO),
            (
                "warn,detcore=info,detcore::scheduler=off",
                LevelFilter::WARN,
            ),
            (
                "warn,detcore[work]=info,detcore::random=info",
                LevelFilter::WARN,
            ),
            ("", LevelFilter::INFO),
            ("", LevelFilter::WARN),
        ] {
            let (policy, subscriber_says) = policy_under(raw, level);
            for (target, enabled) in targets.iter().zip(subscriber_says) {
                assert_eq!(
                    policy.forwards(target),
                    enabled,
                    "RUST_LOG={raw:?} --log={level}: policy {policy:?} disagrees on {target:?}"
                );
            }
        }

        let (scoped, subscriber_says) =
            policy_under("warn,detcore::random=info", LevelFilter::WARN);
        assert!(!subscriber_says[1], "the generic detcore target is off");
        assert!(scoped.forwards_any());
        assert!(scoped.forwards("detcore::random"));
        assert!(!scoped.forwards("detcore::tool_local"));
        assert_eq!(policy_under("", LevelFilter::INFO).0, ForwardPolicy::all());
        assert!(!policy_under("", LevelFilter::WARN).0.forwards_any());
    }

    /// In-guest LiteInst gets the per-target policy it is given next to the
    /// forwarding descriptor, and this build's fingerprint, in the launch
    /// record, with none of Hermit's variables left on the command; a policy
    /// that forwards nothing requests no forwarding.
    #[cfg(feature = "liteinst")]
    #[test]
    fn liteinst_launch_hands_the_guest_its_per_target_policy() {
        use std::os::fd::AsRawFd;

        use detcore::detlog::ForwardPolicy;
        use detcore::in_guest_launch::InGuestLaunch;
        use reverie::process::Command;

        use super::LITEINST_DETLOG_FORWARD_ENV;
        use super::LITEINST_DETLOG_FORWARD_POLICY_ENV;
        use super::forward_in_guest_detlogs_to;
        use super::in_guest_liteinst_launch;

        let (sink, _peer) = std::os::unix::net::UnixDatagram::pair().unwrap();
        forward_in_guest_detlogs_to(sink.into());
        let removed = |command: &Command| {
            [
                LITEINST_DETLOG_FORWARD_ENV,
                LITEINST_DETLOG_FORWARD_POLICY_ENV,
                detcore::LITEINST_CONFIG_FINGERPRINT_ENV,
            ]
            .into_iter()
            .all(|key| {
                command
                    .get_envs()
                    .any(|(name, value)| name == std::ffi::OsStr::new(key) && value.is_none())
            })
        };

        let scoped = ForwardPolicy::decode("0,detcore::random=1").unwrap();
        let mut command = Command::new("/bin/true");
        let (launch, duplicate) = in_guest_liteinst_launch(&mut command, &scoped).unwrap();
        let duplicate = duplicate.expect("a policy that forwards a target requests forwarding");
        assert!(removed(&command));
        let launch = InGuestLaunch::decode(&launch.encode()).unwrap();
        let passed = launch.forward_request.expect("the descriptor is passed");
        assert_eq!(
            detcore::detlog::tool_output_env_target(&passed),
            Some(duplicate.as_raw_fd()),
            "{passed}"
        );
        let sent =
            ForwardPolicy::decode(&launch.forward_policy.expect("the policy goes too")).unwrap();
        assert_eq!(sent, scoped);
        assert!(sent.forwards("detcore::random"));
        assert!(!sent.forwards("detcore::tool_local"));
        assert_eq!(
            launch.coordinator_fingerprint,
            Some(detcore::config_wire_fingerprint())
        );

        let nothing = ForwardPolicy::decode("0").unwrap();
        let mut command = Command::new("/bin/true");
        command
            .env(LITEINST_DETLOG_FORWARD_ENV, "9")
            .env(LITEINST_DETLOG_FORWARD_POLICY_ENV, "1")
            .env(detcore::LITEINST_CONFIG_FINGERPRINT_ENV, "0000000000000000");
        let (launch, duplicate) = in_guest_liteinst_launch(&mut command, &nothing).unwrap();
        assert!(duplicate.is_none());
        assert!(removed(&command));
        assert_eq!(launch.forward_request, None);
        assert_eq!(launch.forward_policy, None);
    }

    /// The forwarding policy answers as a real in-process `detlog!` record
    /// behaves under the CLI's filter: a record is logged only when both its
    /// `tracing::enabled!` hint and its event pass, and a directive with a
    /// field filter applies to the hint whatever its fields but to the event
    /// only when the event has that field. `info,<module>[{unused}]=warn`
    /// suppresses the record; the policy once forwarded it, because it probed
    /// only the event.
    #[test]
    fn detlog_forward_policy_matches_real_detlog_emission_under_field_filters() {
        use std::sync::Arc;
        use std::sync::atomic::AtomicUsize;
        use std::sync::atomic::Ordering;

        use detcore::detlog::ForwardPolicy;
        use tracing::level_filters::LevelFilter;
        use tracing_subscriber::layer::SubscriberExt;

        use crate::liteinst_bootstrap::EffectiveFilter;

        struct Count(Arc<AtomicUsize>);
        impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for Count {
            fn on_event(
                &self,
                _event: &tracing::Event<'_>,
                _context: tracing_subscriber::layer::Context<'_, S>,
            ) {
                self.0.fetch_add(1, Ordering::Relaxed);
            }
        }

        let module = module_path!();
        for (raw, level) in [
            (format!("info,{module}[{{unused}}]=warn"), LevelFilter::INFO),
            (
                format!("warn,{module}[{{unused}}]=trace"),
                LevelFilter::WARN,
            ),
            (
                format!("info,{module}[{{message}}]=warn"),
                LevelFilter::INFO,
            ),
            (format!("info,{module}=warn"), LevelFilter::INFO),
            (format!("warn,{module}=info"), LevelFilter::WARN),
            ("info".to_owned(), LevelFilter::INFO),
            ("warn".to_owned(), LevelFilter::WARN),
        ] {
            let emitted = Arc::new(AtomicUsize::new(0));
            let subscriber = tracing_subscriber::registry()
                .with(EffectiveFilter::from_directives_lossy(&raw, level).into_filter())
                .with(Count(emitted.clone()));
            let forwards = tracing::subscriber::with_default(subscriber, || {
                detcore::detlog!("probe record");
                ForwardPolicy::from_current_subscriber(&raw).forwards(module)
            });
            assert_eq!(
                forwards,
                emitted.load(Ordering::Relaxed) == 1,
                "RUST_LOG={raw:?} --log={level}: the policy and a real detlog! disagree"
            );
        }
    }
}