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

//! Authenticated initial-image random ingress at existing SaBRe ptrace stops.
//! No RPC readiness, scheduling operation or additional stop is introduced.

use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::ffi::CString;
use std::ffi::OsStr;
use std::fs::File;
use std::io::IoSlice;
use std::io::IoSliceMut;
use std::io::Read;
use std::io::Write;
use std::ops::BitAnd;
use std::ops::Deref;
use std::os::fd::AsRawFd;
use std::os::fd::FromRawFd;
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::FileExt;
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use std::path::PathBuf;

use anyhow::Result;
use anyhow::anyhow;
use anyhow::ensure;
use detcore::random::EarlyRequest;
use detcore::random::InitialImage;
use detcore::random::LimitReadCall;
use detcore::random::LoaderState;
use detcore::random::MAX_EARLY_GETRANDOM;
use detcore::random::MAX_EARLY_LIMIT_READS;
use detcore::random::encode_continuation;
use detcore::random::encode_initial_state;
use detcore::random::getrandom_unrecorded;
use detcore::random::root_prng;
use detcore::random::write_initial_auxv;
use nix::unistd::Pid;
use object::Object;
use object::ObjectSegment;
use object::ObjectSymbol;
use reverie::syscalls::AddrMut;
use reverie::syscalls::Errno;
use reverie::syscalls::MemoryAccess;
use reverie::syscalls::Syscall;
use reverie::syscalls::SyscallArgs;
use reverie::syscalls::Sysno;
// Versioned loader wire protocol. No plugin crate is linked into the CLI:
// that crate owns a global allocator and guest-local runtime state.
mod bootstrap {
    pub const PRCTL_OPTION: u64 = 0x53425242;
    pub const VERSION: u64 = 1;
    pub const IMAGE: u64 = 1;
    pub const GETRANDOM: u64 = 2;
    pub const TAKE_STATE: u64 = 3;
    pub const PRLIMIT: u64 = 4;
    pub const GETRLIMIT: u64 = 5;
    pub const MAX_STATE_BYTES: usize = 4096;
    pub const SYSCALL_SYMBOL: &str = "sbr_bootstrap_syscall_v1";
}
pub(super) const ENVIRONMENT: &str = "REVERIE_SABRE_BOOTSTRAP_V1";
const LAYOUT_SYMBOL: &str = "sbr_bootstrap_frame_layout_v1";
/// Version 2 of the descriptor also gives r10, the fourth syscall argument,
/// which a forwarded prlimit64's output pointer occupies. A loader that answers
/// pre-plugin resource-limit reads exports it; one that does not exports only
/// version 1, and such reads are then not offered.
const LAYOUT_SYMBOL_V2: &str = "sbr_bootstrap_frame_layout_v2";

const IN_MEMORY_SNAPSHOT_LIMIT: usize = 128 * 1024 * 1024;
const MAX_MAPS: usize = 1024 * 1024;
const MAX_OBJECTS: usize = 16;
const PAGE: usize = 4096;

fn bounded_file(mut file: &File, maximum: usize) -> Result<Vec<u8>> {
    let mut bytes = Vec::new();
    Read::by_ref(&mut file)
        .take(maximum as u64 + 1)
        .read_to_end(&mut bytes)?;
    ensure!(
        bytes.len() <= maximum,
        "bootstrap input exceeds declared bound"
    );
    Ok(bytes)
}

fn read_path(path: impl AsRef<Path>, maximum: usize) -> Result<Vec<u8>> {
    bounded_file(&File::open(path)?, maximum)
}

fn word(bytes: &[u8], offset: usize) -> Result<usize> {
    let end = offset
        .checked_add(8)
        .ok_or_else(|| anyhow!("word offset overflow"))?;
    Ok(u64::from_le_bytes(
        bytes
            .get(offset..end)
            .ok_or_else(|| anyhow!("short word"))?
            .try_into()?,
    ) as usize)
}

/// Whether ELF segment flags include `flag`. object 0.36 reports `p_flags` as
/// `u32` and later releases as `object::elf::ProgramFlags`; comparing against
/// the flag itself compiles with both.
fn has_segment_flag<F>(p_flags: F, flag: F) -> bool
where
    F: Copy + BitAnd<Output = F> + PartialEq,
{
    p_flags & flag == flag
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct Map {
    start: usize,
    end: usize,
    offset: usize,
    device: String,
    inode: u64,
    path: Vec<u8>,
    permissions: String,
}

impl Map {
    fn contains(&self, address: usize, size: usize) -> bool {
        address >= self.start && address.checked_add(size).is_some_and(|end| end <= self.end)
    }

    fn displays_path(&self, path: &Path) -> bool {
        // Linux maps escapes newline as \\012 but leaves a literal backslash
        // unchanged. Never decode this ambiguous display into an object path.
        let mut displayed = Vec::new();
        for byte in path.as_os_str().as_bytes() {
            if *byte == b'\n' {
                displayed.extend_from_slice(b"\\012");
            } else {
                displayed.push(*byte);
            }
        }
        self.path == displayed
    }

    fn mapped_path(&self, pid: Pid) -> Result<PathBuf> {
        ensure!(
            self.path.starts_with(b"/") && self.inode != 0,
            "unsupported anonymous early getrandom source"
        );
        // Ordinary path displays are already raw bytes. Only ambiguous kernel
        // displays need map_files; do not add that proc capability requirement
        // to an otherwise unambiguous legacy path.
        if !self.path.windows(4).any(|part| part == b"\\012") && !self.path.ends_with(b" (deleted)")
        {
            return Ok(PathBuf::from(OsStr::from_bytes(&self.path)));
        }
        // The magic link returns actual pathname bytes. Authenticate its
        // display and then the opened FD/device/inode/root-relative contents;
        // a newline and a literal \\012 alone cannot select the same object.
        let path = std::fs::read_link(format!(
            "/proc/{pid}/map_files/{:x}-{:x}",
            self.start, self.end
        ))?;
        ensure!(
            self.displays_path(&path),
            "mapped object path display changed"
        );
        Ok(path)
    }
}

fn maps(pid: Pid) -> Result<Vec<Map>> {
    let bytes = read_path(format!("/proc/{pid}/maps"), MAX_MAPS)?;
    bytes
        .strip_suffix(b"\n")
        .unwrap_or(&bytes)
        .split(|byte| *byte == b'\n')
        .map(|line| {
            // Only the five structural fields are whitespace-separated text.
            // The remaining pathname may contain spaces, kernel escapes or
            // non-UTF8 bytes in an unrelated mapping. Retain those bytes and
            // authenticate pathname bytes only when selecting that object.
            let mut rest = line;
            let mut fields = Vec::with_capacity(5);
            for _ in 0..5 {
                rest = rest.trim_ascii_start();
                let end = rest
                    .iter()
                    .position(u8::is_ascii_whitespace)
                    .unwrap_or(rest.len());
                ensure!(end > 0, "missing bootstrap mapping field");
                fields.push(std::str::from_utf8(&rest[..end])?);
                rest = &rest[end..];
            }
            let (start, end) = fields[0]
                .split_once('-')
                .ok_or_else(|| anyhow!("bad map range"))?;
            let row = Map {
                start: usize::from_str_radix(start, 16)?,
                end: usize::from_str_radix(end, 16)?,
                offset: usize::from_str_radix(fields[2], 16)?,
                device: fields[3].to_owned(),
                inode: fields[4].parse()?,
                path: rest.trim_ascii_start().to_vec(),
                permissions: fields[1].to_owned(),
            };
            ensure!(
                row.start < row.end && row.permissions.len() == 4,
                "invalid bootstrap map"
            );
            Ok(row)
        })
        .collect()
}

fn containing(rows: &[Map], address: usize, size: usize) -> Result<&Map> {
    let mut found = rows.iter().filter(|row| row.contains(address, size));
    let first = found
        .next()
        .ok_or_else(|| anyhow!("bootstrap extent is not in one mapping"))?;
    ensure!(found.next().is_none(), "ambiguous bootstrap extent");
    Ok(first)
}

fn open_under_root(pid: Pid, path: &Path) -> Result<File> {
    let path = path.as_os_str().as_bytes();
    ensure!(path.starts_with(b"/"), "nonabsolute bootstrap object path");
    let components: Vec<_> = path[1..].split(|byte| *byte == b'/').collect();
    ensure!(
        !components.is_empty()
            && components
                .iter()
                .all(|c| !c.is_empty() && *c != b"." && *c != b".."),
        "noncanonical bootstrap object path"
    );
    // Follow only the proc magic link to this owned task's root. Every actual
    // path component thereafter is no-follow and relative to a held directory.
    let mut file = File::open(format!("/proc/{pid}/root"))?;
    for (i, part) in components.iter().enumerate() {
        let name = CString::new(*part)?;
        let flags = libc::O_CLOEXEC
            | libc::O_NOFOLLOW
            | if i + 1 < components.len() {
                libc::O_DIRECTORY
            } else {
                0
            };
        let fd = unsafe { libc::openat(file.as_raw_fd(), name.as_ptr(), flags, 0) };
        if fd < 0 {
            return Err(std::io::Error::last_os_error().into());
        }
        file = unsafe { File::from_raw_fd(fd) };
    }
    Ok(file)
}

struct DiskSnapshot {
    address: usize,
    length: usize,
}

fn require_supported_snapshot_filesystem(file: &File) -> Result<()> {
    let mut fs = std::mem::MaybeUninit::<libc::statfs>::uninit();
    if unsafe { libc::fstatfs(file.as_raw_fd(), fs.as_mut_ptr()) } != 0 {
        return Err(std::io::Error::last_os_error().into());
    }
    let fs = unsafe { fs.assume_init() };
    // XFS_SUPER_MAGIC from linux/magic.h is not exported by this libc version.
    // Limit this storage path to qualified regular filesystems; this is not a
    // claim about their physical block-device backing. The mapped snapshot
    // pages must be reclaimable independently of the supervisor's heap.
    const XFS_SUPER_MAGIC: libc::c_long = 0x58465342;
    ensure!(
        matches!(
            fs.f_type,
            libc::BTRFS_SUPER_MAGIC | libc::EXT4_SUPER_MAGIC | XFS_SUPER_MAGIC
        ),
        "large bootstrap snapshot requires a supported cache filesystem (unqualified filesystem {:#x})",
        fs.f_type
    );
    Ok(())
}

impl DiskSnapshot {
    fn new(file: &File, length: usize) -> Result<Self> {
        ensure!(
            length <= isize::MAX as usize,
            "bootstrap snapshot exceeds address space"
        );
        // Reuse the existing Hermit cache placement, never cwd or a new
        // hardcoded scratch directory. Small objects do not need this cache.
        let data = super::HermitData::new();
        ensure!(
            data.data_dir().is_absolute(),
            "bootstrap snapshot cache must be absolute"
        );
        let parent = data.data_dir().join("tmp");
        std::fs::create_dir_all(&parent)?;
        let directory = tempfile::Builder::new()
            .prefix("sabre-snapshot-")
            .permissions(std::fs::Permissions::from_mode(0o700))
            .tempdir_in(parent)?;
        let mut output = tempfile::tempfile_in(directory.path())?;
        require_supported_snapshot_filesystem(&output)?;
        let mut buffer = [0; 64 * 1024];
        let mut offset = 0;
        while offset < length {
            let size = buffer.len().min(length - offset);
            file.read_exact_at(&mut buffer[..size], offset as u64)?;
            output.write_all(&buffer[..size])?;
            offset += size;
        }
        ensure!(
            file.metadata()?.len() == length as u64,
            "bootstrap object size changed during snapshot"
        );
        // Reopen read-only, then discard the writer. After mmap discard even
        // the read-only FD: the guest shares this supervisor's PID namespace
        // and must not obtain a snapshot alias through /proc/<parent>/fd.
        let readonly = File::open(format!("/proc/self/fd/{}", output.as_raw_fd()))?;
        drop(output);
        let address = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                length,
                libc::PROT_READ,
                libc::MAP_PRIVATE,
                readonly.as_raw_fd(),
                0,
            )
        };
        if address == libc::MAP_FAILED {
            return Err(std::io::Error::last_os_error().into());
        }
        let snapshot = Self {
            address: address as usize,
            length,
        };
        drop(readonly);
        directory.close()?;
        Ok(snapshot)
    }

    fn as_slice(&self) -> &[u8] {
        // The anonymous copied backing has no writable handle or exposed
        // pathname. It is owned solely by this read-only mapping until Drop,
        // under the same trusted-supervisor assumption as the former Vec.
        unsafe { std::slice::from_raw_parts(self.address as *const u8, self.length) }
    }
}

impl Drop for DiskSnapshot {
    fn drop(&mut self) {
        unsafe { libc::munmap(self.address as *mut libc::c_void, self.length) };
    }
}

enum OriginalBytes {
    Memory(Vec<u8>),
    Disk(DiskSnapshot),
}

impl OriginalBytes {
    fn new(file: &File, length: usize) -> Result<Self> {
        if length <= IN_MEMORY_SNAPSHOT_LIMIT {
            Ok(Self::Memory(bounded_file(file, IN_MEMORY_SNAPSHOT_LIMIT)?))
        } else {
            Ok(Self::Disk(DiskSnapshot::new(file, length)?))
        }
    }

    fn as_slice(&self) -> &[u8] {
        match self {
            Self::Memory(bytes) => bytes,
            Self::Disk(snapshot) => snapshot.as_slice(),
        }
    }

    fn matches_file(&self, file: &File) -> Result<bool> {
        if file.metadata()?.len() != self.len() as u64 {
            return Ok(false);
        }
        let mut buffer = [0; 64 * 1024];
        for (index, expected) in self.chunks(buffer.len()).enumerate() {
            let actual = &mut buffer[..expected.len()];
            file.read_exact_at(actual, (index * 64 * 1024) as u64)?;
            if actual != expected {
                return Ok(false);
            }
        }
        Ok(file.metadata()?.len() == self.len() as u64)
    }
}

impl Deref for OriginalBytes {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        self.as_slice()
    }
}

struct HeldObject {
    path: PathBuf,
    file: File,
    bytes: OriginalBytes,
    device: String,
    inode: u64,
}

impl HeldObject {
    fn open(path: &Path) -> Result<Self> {
        let held = Self::open_file(path)?;
        let elf = object::File::parse(held.bytes.as_slice())?;
        ensure!(
            elf.format() == object::BinaryFormat::Elf
                && elf.architecture() == object::Architecture::X86_64
                && elf.is_little_endian(),
            "bootstrap requires x86-64 ELF"
        );
        Ok(held)
    }

    fn open_file(path: &Path) -> Result<Self> {
        let path = std::fs::canonicalize(path)?;
        let file = File::open(&path)?;
        let meta = file.metadata()?;
        ensure!(
            meta.is_file() && meta.len() > 0,
            "unsupported bootstrap ELF object size"
        );
        let length = usize::try_from(meta.len())?;
        let bytes = OriginalBytes::new(&file, length)?;
        ensure!(
            bytes.matches_file(&file)?,
            "bootstrap object changed during snapshot"
        );
        // Compare maps-device to maps-device for this exact held FD. Btrfs's
        // maps superblock device need not equal its subvolume st_dev.
        let address = unsafe {
            libc::mmap(
                std::ptr::null_mut(),
                PAGE,
                libc::PROT_READ,
                libc::MAP_PRIVATE,
                file.as_raw_fd(),
                0,
            )
        };
        ensure!(
            address != libc::MAP_FAILED,
            "cannot map held bootstrap object"
        );
        let observed = (|| {
            let own = maps(Pid::this())?;
            let row = containing(&own, address as usize, 1)?;
            ensure!(
                row.displays_path(&path) && row.inode == meta.ino() && row.offset == 0,
                "held bootstrap FD mapping mismatch"
            );
            Ok::<_, anyhow::Error>(row.device.clone())
        })();
        let unmapped = unsafe { libc::munmap(address, PAGE) };
        ensure!(unmapped == 0, "failed to unmap bootstrap identity mapping");
        Ok(Self {
            path,
            file,
            bytes,
            device: observed?,
            inode: meta.ino(),
        })
    }

    fn matches_mapping(&self, row: &Map) -> bool {
        row.displays_path(&self.path) && row.inode == self.inode && row.device == self.device
    }

    fn authenticate(&self, pid: Pid, row: &Map) -> Result<()> {
        ensure!(
            self.matches_mapping(row),
            "bootstrap mapped object identity mismatch: map={:#x}..{:#x} offset={:#x} \
             device={} inode={} permissions={} expected_device={} expected_inode={}",
            row.start,
            row.end,
            row.offset,
            row.device,
            row.inode,
            row.permissions,
            self.device,
            self.inode
        );
        self.authenticate_path(pid)
    }

    fn authenticate_path(&self, pid: Pid) -> Result<()> {
        let other = open_under_root(pid, &self.path)?;
        let a = self.file.metadata()?;
        let b = other.metadata()?;
        ensure!(
            (a.dev(), a.ino(), a.len()) == (b.dev(), b.ino(), b.len()),
            "bootstrap root-relative object changed"
        );
        ensure!(
            self.bytes.matches_file(&other)?,
            "bootstrap mapped pathname content changed"
        );
        Ok(())
    }

    fn bias(&self, pid: Pid, rows: &[Map]) -> Result<usize> {
        let elf = object::File::parse(self.bytes.as_slice())?;
        let mut common: Option<BTreeSet<usize>> = None;
        for row in rows.iter().filter(|r| self.matches_mapping(r)) {
            self.authenticate(pid, row)?;
            if !row.permissions.contains('x') {
                continue;
            }
            let mut candidates = BTreeSet::new();
            for segment in elf.segments() {
                let object::SegmentFlags::Elf { p_flags, .. } = segment.flags() else {
                    continue;
                };
                if !has_segment_flag(p_flags, object::elf::PF_X) {
                    continue;
                }
                let (offset, size) = segment.file_range();
                if size == 0 {
                    continue;
                }
                let lo = offset as usize & !(PAGE - 1);
                let end = (offset as usize)
                    .checked_add(size as usize)
                    .and_then(|n| n.checked_add(PAGE - 1))
                    .ok_or_else(|| anyhow!("ELF segment overflow"))?
                    & !(PAGE - 1);
                if row.offset >= lo && row.offset < end {
                    let virtual_start = (segment.address() as usize & !(PAGE - 1))
                        .checked_add(row.offset - lo)
                        .ok_or_else(|| anyhow!("ELF bias overflow"))?;
                    if let Some(bias) = row.start.checked_sub(virtual_start) {
                        candidates.insert(bias);
                    }
                }
            }
            ensure!(
                !candidates.is_empty(),
                "executable mapping is not backed by an executable ELF load segment"
            );
            common = Some(match common {
                None => candidates,
                Some(old) => old.intersection(&candidates).copied().collect(),
            });
        }
        let biases =
            common.ok_or_else(|| anyhow!("missing executable bootstrap object mapping"))?;
        ensure!(biases.len() == 1, "ambiguous bootstrap ELF load bias");
        Ok(*biases.first().unwrap())
    }

    /// Select the instance containing an observed executable address. The
    /// loader and guest may map the same PT_INTERP file at different bases;
    /// unrelated instances cannot contribute constraints to this address.
    fn bias_at(&self, pid: Pid, rows: &[Map], anchor: usize, role: &str) -> Result<usize> {
        let result = (|| {
            let row = containing(rows, anchor, 1)?;
            self.authenticate(pid, row)?;
            ensure!(
                row.permissions.contains('x'),
                "anchor mapping is not executable"
            );
            let elf = object::File::parse(self.bytes.as_slice())?;
            let mut candidates = BTreeSet::new();
            for segment in elf.segments() {
                let object::SegmentFlags::Elf { p_flags, .. } = segment.flags() else {
                    continue;
                };
                if !has_segment_flag(p_flags, object::elf::PF_X) {
                    continue;
                }
                let (offset, size) = segment.file_range();
                let offset = usize::try_from(offset)?;
                let size = usize::try_from(size)?;
                let virtual_start = usize::try_from(segment.address())?;
                let file_anchor = row
                    .offset
                    .checked_add(anchor - row.start)
                    .ok_or_else(|| anyhow!("anchor file offset overflow"))?;
                if file_anchor < offset || file_anchor - offset >= size {
                    continue;
                }
                let relative = virtual_start
                    .checked_add(file_anchor - offset)
                    .ok_or_else(|| anyhow!("anchor virtual address overflow"))?;
                if let Some(bias) = anchor.checked_sub(relative)
                    && bias.is_multiple_of(PAGE)
                {
                    candidates.insert(bias);
                }
            }
            ensure!(
                candidates.len() == 1,
                "missing/ambiguous anchored ELF load bias"
            );
            Ok(*candidates.first().unwrap())
        })();
        result.map_err(|error: anyhow::Error| anyhow!("{role} anchor={anchor:#x}: {error:#}"))
    }

    fn program_headers(
        &self,
        pid: Pid,
        rows: &[Map],
        bias: usize,
        address: usize,
        count: usize,
        size: usize,
    ) -> Result<()> {
        let result = (|| {
            let phoff = word(&self.bytes, 32)?;
            let phnum = u16::from_le_bytes(self.bytes[56..58].try_into()?) as usize;
            ensure!(
                count == phnum && size == 56,
                "final program header count/size changed"
            );
            let length = phnum
                .checked_mul(56)
                .ok_or_else(|| anyhow!("program header extent overflow"))?;
            let elf = object::File::parse(self.bytes.as_slice())?;
            let first = elf
                .segments()
                .next()
                .ok_or_else(|| anyhow!("missing first ELF load"))?;
            let (offset, _) = first.file_range();
            let virtual_address = usize::try_from(first.address())?
                .checked_add(
                    phoff
                        .checked_sub(usize::try_from(offset)?)
                        .ok_or_else(|| anyhow!("program headers precede first ELF load"))?,
                )
                .ok_or_else(|| anyhow!("program header virtual address overflow"))?;
            ensure!(
                bias.checked_add(virtual_address) == Some(address),
                "final program header address differs from anchored ELF"
            );
            let row = containing(rows, address, length)?;
            if self.matches_mapping(row) {
                self.authenticate(pid, row)?;
            } else {
                // library_buf_get_original replaces precisely the first file
                // page after saving the original image. Authenticate only the
                // PHDR bytes there, anchored by the still-file-backed entry.
                // This is not authority for anonymous instructions or frames.
                let page = bias
                    .checked_add(usize::try_from(first.address())? & !(PAGE - 1))
                    .ok_or_else(|| anyhow!("copied ELF page address overflow"))?;
                let object::SegmentFlags::Elf { p_flags, .. } = first.flags() else {
                    return Err(anyhow!("missing first ELF load flags"));
                };
                let permissions = format!(
                    "{}{}{}p",
                    if has_segment_flag(p_flags, object::elf::PF_R) {
                        'r'
                    } else {
                        '-'
                    },
                    if has_segment_flag(p_flags, object::elf::PF_W) {
                        'w'
                    } else {
                        '-'
                    },
                    if has_segment_flag(p_flags, object::elf::PF_X) {
                        'x'
                    } else {
                        '-'
                    }
                );
                ensure!(
                    offset == 0
                        && row.start == page
                        && page.checked_add(PAGE) == Some(row.end)
                        && row.offset == 0
                        && row.inode == 0
                        && row.device == "00:00"
                        && row.path.is_empty()
                        && row.permissions == permissions,
                    "unrecognized copied PHDR mapping: map={:#x}..{:#x} offset={:#x} \
                     device={} inode={} permissions={} expected_page={page:#x} expected_permissions={permissions}",
                    row.start,
                    row.end,
                    row.offset,
                    row.device,
                    row.inode,
                    row.permissions
                );
                self.authenticate_path(pid)?;
            }
            let expected = self
                .bytes
                .get(
                    phoff
                        ..phoff
                            .checked_add(length)
                            .ok_or_else(|| anyhow!("program header range overflow"))?,
                )
                .ok_or_else(|| anyhow!("short program headers"))?;
            ensure!(
                self.at_virtual(virtual_address, length, false)? == expected
                    && remote_bytes(pid, address, length)? == expected,
                "final program headers differ from held ELF"
            );
            Ok(())
        })();
        result.map_err(|error: anyhow::Error| {
            anyhow!("program PHDR address={address:#x} load_bias={bias:#x}: {error:#}")
        })
    }

    fn at_virtual(&self, address: usize, length: usize, readonly: bool) -> Result<&[u8]> {
        let elf = object::File::parse(self.bytes.as_slice())?;
        let end = address
            .checked_add(length)
            .ok_or_else(|| anyhow!("ELF extent overflow"))?;
        let mut found = None;
        for segment in elf.segments() {
            let (offset, size) = segment.file_range();
            let lo = segment.address() as usize;
            let hi = lo
                .checked_add(size as usize)
                .ok_or_else(|| anyhow!("ELF segment overflow"))?;
            if address >= lo && end <= hi {
                let object::SegmentFlags::Elf { p_flags, .. } = segment.flags() else {
                    continue;
                };
                ensure!(
                    !readonly
                        || (has_segment_flag(p_flags, object::elf::PF_R)
                            && !has_segment_flag(p_flags, object::elf::PF_W)),
                    "bootstrap descriptor is in a writable ELF load segment"
                );
                ensure!(found.is_none(), "ambiguous ELF file extent");
                let start = (offset as usize)
                    .checked_add(address - lo)
                    .ok_or_else(|| anyhow!("ELF file offset overflow"))?;
                found = self.bytes.get(
                    start
                        ..start
                            .checked_add(length)
                            .ok_or_else(|| anyhow!("ELF file range overflow"))?,
                );
            }
        }
        found.ok_or_else(|| anyhow!("bootstrap ELF extent is not fully file-backed"))
    }

    fn layout(&self) -> Result<FrameLayout> {
        let elf = object::File::parse(self.bytes.as_slice())?;
        let definitions = |name: &str| -> BTreeSet<_> {
            elf.symbols()
                .chain(elf.dynamic_symbols())
                .filter(|s| s.name().ok() == Some(name) && !s.is_undefined())
                .map(|s| {
                    (
                        s.address() as usize,
                        s.size() as usize,
                        s.kind() == object::SymbolKind::Data,
                    )
                })
                .collect()
        };
        let version2 = definitions(LAYOUT_SYMBOL_V2);
        let (symbol, definitions) = if version2.is_empty() {
            (LAYOUT_SYMBOL, definitions(LAYOUT_SYMBOL))
        } else {
            (LAYOUT_SYMBOL_V2, version2)
        };
        ensure!(
            definitions.len() == 1,
            "missing/ambiguous bootstrap frame descriptor"
        );
        let (address, size, data) = *definitions.first().unwrap();
        let expected = if symbol == LAYOUT_SYMBOL_V2 { 10 } else { 9 } * 8;
        ensure!(
            data && size == expected,
            "wrong bootstrap frame descriptor type/extent"
        );
        let mut layout = FrameLayout::decode(self.at_virtual(address, size, true)?)?;
        layout.descriptor = symbol;
        Ok(layout)
    }

    fn symbol(&self, name: &str) -> Result<usize> {
        let elf = object::File::parse(self.bytes.as_slice())?;
        let symbols: BTreeSet<_> = elf
            .symbols()
            .chain(elf.dynamic_symbols())
            .filter(|s| s.name().ok() == Some(name) && !s.is_undefined())
            .map(|s| s.address() as usize)
            .collect();
        ensure!(
            symbols.len() == 1,
            "missing/ambiguous bootstrap ELF symbol {name}"
        );
        Ok(*symbols.first().unwrap())
    }
}

#[derive(Clone, Copy)]
struct RemoteMemory(Pid);
impl MemoryAccess for RemoteMemory {
    fn write_with_user_access(
        &mut self,
        address: AddrMut<u8>,
        bytes: &[u8],
    ) -> std::result::Result<usize, Errno> {
        address
            .as_raw()
            .checked_add(bytes.len())
            .ok_or(Errno::EFAULT)?;
        if bytes.is_empty() {
            return Ok(0);
        }
        // process_vm_writev obeys the tracee's user page permissions. There is
        // no ptrace/debugger fallback, including for an eight-byte request.
        let local = libc::iovec {
            iov_base: bytes.as_ptr().cast_mut().cast(),
            iov_len: bytes.len(),
        };
        let remote = libc::iovec {
            iov_base: address.as_raw() as *mut libc::c_void,
            iov_len: bytes.len(),
        };
        let n = unsafe { libc::process_vm_writev(self.0.as_raw(), &local, 1, &remote, 1, 0) };
        if n < 0 {
            Err(Errno::last())
        } else {
            Ok(n as usize)
        }
    }

    fn read_vectored(
        &self,
        remote: &[IoSlice],
        local: &mut [IoSliceMut],
    ) -> std::result::Result<usize, Errno> {
        let n = unsafe {
            libc::process_vm_readv(
                self.0.as_raw(),
                local.as_ptr().cast(),
                local.len() as _,
                remote.as_ptr().cast(),
                remote.len() as _,
                0,
            )
        };
        if n < 0 {
            Err(Errno::last())
        } else {
            Ok(n as usize)
        }
    }
    fn write_vectored(
        &mut self,
        local: &[IoSlice],
        remote: &mut [IoSliceMut],
    ) -> std::result::Result<usize, Errno> {
        let n = unsafe {
            libc::process_vm_writev(
                self.0.as_raw(),
                local.as_ptr().cast(),
                local.len() as _,
                remote.as_ptr().cast(),
                remote.len() as _,
                0,
            )
        };
        if n < 0 {
            Err(Errno::last())
        } else {
            Ok(n as usize)
        }
    }
}

fn random_response(result: std::result::Result<i64, reverie::Error>) -> Result<i64> {
    match result {
        Ok(n) => Ok(n),
        Err(reverie::Error::Errno(error)) => Ok(-(error.into_raw() as i64)),
        // The caller must see this error before rewriting registers or
        // resuming the held bootstrap request. Do not fabricate a guest errno.
        Err(error) => Err(error.into()),
    }
}

fn remote_bytes(pid: Pid, address: usize, length: usize) -> Result<Vec<u8>> {
    ensure!(
        length <= MAX_MAPS && address.checked_add(length).is_some(),
        "bootstrap remote read exceeds bound"
    );
    let pointer =
        reverie::syscalls::Addr::from_raw(address).ok_or_else(|| anyhow!("null bootstrap read"))?;
    let mut bytes = vec![0; length];
    RemoteMemory(pid).read_exact(pointer, &mut bytes)?;
    Ok(bytes)
}

fn generation(pid: Pid) -> Result<u64> {
    let bytes = read_path(format!("/proc/{pid}/stat"), 4096)?;
    stat_generation(pid, &bytes)
}

fn stat_generation(pid: Pid, bytes: &[u8]) -> Result<u64> {
    // Linux comm is raw bytes and may itself contain ") ". Only the PID
    // prefix and the numeric fields following its final delimiter are text.
    let end = bytes
        .windows(2)
        .rposition(|pair| pair == b") ")
        .ok_or_else(|| anyhow!("malformed bootstrap process stat"))?;
    let prefix = &bytes[..end];
    let owner_end = prefix
        .windows(2)
        .position(|pair| pair == b" (")
        .ok_or_else(|| anyhow!("missing process stat owner"))?;
    ensure!(
        std::str::from_utf8(&prefix[..owner_end])?.parse::<i32>()? == pid.as_raw(),
        "bootstrap stat owner mismatch"
    );
    let start = bytes[end + 2..]
        .split(u8::is_ascii_whitespace)
        .filter(|field| !field.is_empty())
        .nth(19)
        .ok_or_else(|| anyhow!("missing process generation"))?;
    Ok(std::str::from_utf8(start)?.parse()?)
}

fn script_interpreter(bytes: &[u8]) -> Result<Option<&Path>> {
    Ok(script_words(bytes)?
        .first()
        .map(|&path| Path::new(OsStr::from_bytes(path))))
}

fn script_words(bytes: &[u8]) -> Result<Vec<&[u8]>> {
    // Match the loader's single BINPRM_BUF_SIZE/fgets/strtok resolution.
    if !bytes.starts_with(b"#!") {
        return Ok(Vec::new());
    }
    let line = &bytes[..bytes.len().min(255)];
    let line = &line[..line.iter().position(|b| *b == b'\n').unwrap_or(line.len())];
    let mut tokens = line[2..]
        .split(|b| matches!(b, b' ' | b'\n' | b'\r' | b'\t'))
        .filter(|part| !part.is_empty());
    let path = tokens
        .next()
        .ok_or_else(|| anyhow!("missing SaBRe script interpreter"))?;
    // Preserve the existing interpreter-path NUL refusal. C strtok ends the
    // optional argument at its first NUL and never considers later tokens.
    ensure!(!path.contains(&0), "NUL in SaBRe script interpreter");
    let mut words = vec![path];
    if let Some(token) = tokens.next() {
        let token = &token[..token
            .iter()
            .position(|byte| *byte == 0)
            .unwrap_or(token.len())];
        if !token.is_empty() {
            words.push(token);
        }
    }
    Ok(words)
}

fn elf_interpreter(bytes: &[u8]) -> Result<Option<PathBuf>> {
    // PT_INTERP is a program header, not a PT_LOAD segment.
    let phoff = word(bytes, 32)?;
    let phnum = u16::from_le_bytes(
        bytes
            .get(56..58)
            .ok_or_else(|| anyhow!("short ELF header"))?
            .try_into()?,
    ) as usize;
    ensure!(
        phnum <= 128 && bytes.get(54..56) == Some(&56u16.to_le_bytes()),
        "unsupported ELF program headers"
    );
    ensure!(
        phnum > 0
            && phoff
                .checked_add(phnum * 56)
                .is_some_and(|end| end <= bytes.len()),
        "program headers outside held ELF"
    );
    let mut path = None;
    for i in 0..phnum {
        let p = phoff
            .checked_add(i * 56)
            .ok_or_else(|| anyhow!("program header overflow"))?;
        if bytes.get(p..p + 4) == Some(&3u32.to_le_bytes()) {
            ensure!(path.is_none(), "duplicate guest interpreter");
            let offset = word(bytes, p + 8)?;
            let size = word(bytes, p + 32)?;
            ensure!((2..=4096).contains(&size), "invalid guest interpreter path");
            let bytes = bytes
                .get(
                    offset
                        ..offset
                            .checked_add(size)
                            .ok_or_else(|| anyhow!("interpreter extent overflow"))?,
                )
                .ok_or_else(|| anyhow!("short interpreter path"))?;
            ensure!(
                bytes.last() == Some(&0) && !bytes[..size - 1].contains(&0),
                "invalid interpreter terminator"
            );
            path = Some(PathBuf::from(OsStr::from_bytes(&bytes[..size - 1])));
        }
    }
    Ok(path)
}

// The initial exec stop precedes the loader's first instruction. Bound string
// capture by the actual kernel-created stack mapping, with one non-overlapping
// extent per argument. No smaller per-string limit is imposed. Only argv is
// retained; environment values are not retained or included in diagnostics.
fn initial_arguments(pid: Pid, rows: &[Map], stack: usize) -> Result<Vec<Vec<u8>>> {
    let row = containing(rows, stack, 8)?;
    ensure!(
        row.path == b"[stack]" && row.permissions.starts_with("rw"),
        "initial argv is not on the owned writable stack"
    );
    let argc = word(&remote_bytes(pid, stack, 8)?, 0)?;
    // run_sabre execs SaBRe with the guest's own argv (see
    // reverie_sabre_stats::launch), within the supported guest argc (4096).
    ensure!(
        (1..=4096).contains(&argc),
        "unsupported initial loader argc"
    );
    let vector_size = (argc + 2) * 8;
    ensure!(
        row.contains(stack, vector_size),
        "initial argv vector leaves stack"
    );
    let vector = remote_bytes(pid, stack, vector_size)?;
    ensure!(
        word(&vector, (argc + 1) * 8)? == 0,
        "missing initial argv terminator"
    );
    let mut previous_end = stack + vector_size;
    let mut arguments = Vec::with_capacity(argc);
    for index in 0..argc {
        let pointer = word(&vector, (index + 1) * 8)?;
        ensure!(
            pointer >= previous_end && row.contains(pointer, 1),
            "initial argv string overlaps or leaves kernel stack: index={index}"
        );
        let mut value = Vec::new();
        let mut at = pointer;
        loop {
            ensure!(
                at < row.end,
                "unterminated initial argv string: index={index}"
            );
            let chunk = remote_bytes(pid, at, (row.end - at).min(PAGE))?;
            if let Some(end) = chunk.iter().position(|byte| *byte == 0) {
                value.extend_from_slice(&chunk[..=end]);
                previous_end = at + end + 1;
                break;
            }
            value.extend_from_slice(&chunk);
            at += chunk.len();
        }
        arguments.push(value);
    }
    Ok(arguments)
}

/// The argv the guest's image must start with: SaBRe's own initial argv,
/// unchanged. run_sabre execs SaBRe with the guest's argv, a script's already
/// resolved to its interpreter as the kernel would, and passes the plugin and
/// image in a launch specification, so the loader leaves argv as the kernel
/// built it.
fn guest_arguments(initial: Vec<Vec<u8>>) -> Result<Vec<Vec<u8>>> {
    ensure!(
        !initial.is_empty() && initial.len() <= 4096,
        "unsupported bootstrap argc"
    );
    Ok(initial)
}

fn authenticate_arguments(
    pid: Pid,
    rows: &[Map],
    vector: &[u8],
    expected: &[Vec<u8>],
) -> Result<usize> {
    let argc = word(vector, 0)?;
    ensure!(
        argc > 0 && argc <= 4096 && argc == expected.len(),
        "final argv count changed"
    );
    for (index, value) in expected.iter().enumerate() {
        let pointer = word(vector, (index + 1) * 8)?;
        let row = containing(rows, pointer, value.len()).map_err(|error| {
            anyhow!("final argv mapping index={index} address={pointer:#x}: {error:#}")
        })?;
        ensure!(
            row.permissions.starts_with('r'),
            "final argv mapping is not readable: index={index}"
        );
        // Values include the terminating NUL. Comparing the full retained
        // extent rejects truncation, extension, reordered arguments and an
        // unrelated readable pointer without assuming strings live on stack.
        let mut offset = 0;
        while offset < value.len() {
            let length = (value.len() - offset).min(PAGE);
            ensure!(
                remote_bytes(pid, pointer + offset, length)? == value[offset..offset + length],
                "final argv bytes/order changed: index={index}"
            );
            offset += length;
        }
    }
    let at = (argc + 1) * 8;
    ensure!(word(vector, at)? == 0, "missing final argv terminator");
    Ok(at + 8)
}

/// Launch inputs are retained before the owned child can execute the loader.
pub(super) struct Launch {
    loader: HeldObject,
    program: HeldObject,
    interpreter: Option<HeldObject>,
    script: Option<HeldObject>,
    config: detcore::Config,
    layout: FrameLayout,
}
impl Launch {
    pub(super) fn new(loader: &Path, program: &Path, config: &detcore::Config) -> Result<Self> {
        let loader = HeldObject::open(loader)?;
        loader.symbol(bootstrap::SYSCALL_SYMBOL)?;
        let layout = loader.layout()?;
        let input = HeldObject::open_file(program)?;
        let (program, script) = if let Some(path) = script_interpreter(&input.bytes)? {
            (HeldObject::open(path)?, Some(input))
        } else {
            let elf = object::File::parse(input.bytes.as_slice())?;
            ensure!(
                elf.format() == object::BinaryFormat::Elf
                    && elf.architecture() == object::Architecture::X86_64
                    && elf.is_little_endian(),
                "bootstrap requires x86-64 ELF"
            );
            (input, None)
        };
        let path = elf_interpreter(&program.bytes)?;
        let interpreter = path.as_deref().map(HeldObject::open).transpose()?;
        Ok(Self {
            loader,
            program,
            interpreter,
            script,
            config: config.clone(),
            layout,
        })
    }

    pub(super) fn initializes_random(&self) -> bool {
        self.interpreter.is_some()
    }

    fn authenticate_script(&self, pid: Pid) -> Result<()> {
        if let Some(script) = &self.script {
            script.authenticate_path(pid)?;
        }
        Ok(())
    }
}

struct SigillOrigin {
    generation: u64,
    registers: libc::user_regs_struct,
    mapping: Map,
}

struct VdsoSnapshot {
    mapping: Map,
    bytes: Vec<u8>,
}

/// State is single-root/single-image until TAKE. Ordinary post-handoff process
/// and robust-exit accounting remains in the existing supervisor.
pub(super) struct Bootstrap {
    launch: Launch,
    root: Pid,
    generation: u64,
    image: Option<InitialImage>,
    prng: rand_pcg::Pcg64Mcg,
    /// The bytes written at AT_RANDOM, for the handoff's post-exec record.
    at_random_value: Option<[u8; 16]>,
    /// The loader's getrandom fills, in order, for the handoff's post-exec
    /// records: the root thread's seeding records must precede them.
    /// The getrandom fills and resource-limit reads served before post-exec,
    /// in order, for post-exec to record.
    early_requests: Vec<EarlyRequest>,
    taken: bool,
    sigill: Option<SigillOrigin>,
    initial_random: usize,
    expected_arguments: Vec<Vec<u8>>,
    vdso: Option<VdsoSnapshot>,
    other_objects: Vec<HeldObject>,
    // Only real kernel EXEC events create these entries. They are removed on
    // successful TAKE or final physical exit, never copied across fork.
    continuations: BTreeMap<Pid, InitialImage>,
    static_reexec_observed: bool,
}

#[derive(Clone, Copy, Debug)]
struct FrameLayout {
    size: usize,
    rdi: usize,
    rsi: usize,
    rdx: usize,
    /// Present in a version 2 descriptor.
    r10: Option<usize>,
    architectural_return: usize,
    scratch_return: usize,
    /// The loader symbol the descriptor came from.
    descriptor: &'static str,
}
impl FrameLayout {
    /// The descriptor's size in bytes: 9 words in version 1, 10 in version 2.
    fn extent(&self) -> usize {
        if self.r10.is_some() { 80 } else { 72 }
    }

    fn decode(bytes: &[u8]) -> Result<Self> {
        let version = if bytes.len() >= 8 { word(bytes, 0)? } else { 0 };
        ensure!(
            ((bytes.len() == 72 && version == 1 && word(bytes, 64)? == 9)
                || (bytes.len() == 80 && version == 2 && word(bytes, 64)? == 10))
                && word(bytes, 56)? == 8,
            "unsupported bootstrap frame descriptor"
        );
        let size = word(bytes, 8)?;
        ensure!(
            (8..=4096).contains(&size) && size.is_multiple_of(8),
            "invalid bootstrap full frame size"
        );
        let mut offsets = vec![
            word(bytes, 16)?,
            word(bytes, 24)?,
            word(bytes, 32)?,
            word(bytes, 40)?,
            word(bytes, 48)?,
        ];
        if version == 2 {
            offsets.push(word(bytes, 72)?);
        }
        ensure!(
            offsets
                .iter()
                .all(|o| o.is_multiple_of(8) && o.checked_add(8).is_some_and(|end| end <= size)),
            "bootstrap frame field outside extent"
        );
        ensure!(
            offsets.iter().copied().collect::<BTreeSet<_>>().len() == offsets.len(),
            "overlapping bootstrap frame fields"
        );
        Ok(Self {
            size,
            rdi: offsets[0],
            rsi: offsets[1],
            rdx: offsets[2],
            r10: offsets.get(5).copied(),
            architectural_return: offsets[3],
            scratch_return: offsets[4],
            descriptor: if version == 2 {
                LAYOUT_SYMBOL_V2
            } else {
                LAYOUT_SYMBOL
            },
        })
    }
}

fn auxv_random(bytes: &[u8]) -> Result<usize> {
    ensure!(bytes.len().is_multiple_of(16), "short initial kernel auxv");
    let mut random = None;
    for pair in bytes.as_chunks::<16>().0 {
        let kind = word(pair, 0)?;
        let value = word(pair, 8)?;
        if kind == libc::AT_NULL as usize {
            ensure!(value == 0, "invalid auxv terminator");
            return random.ok_or_else(|| anyhow!("initial kernel auxv has no AT_RANDOM"));
        }
        if kind == libc::AT_RANDOM as usize {
            ensure!(
                value != 0 && random.replace(value).is_none(),
                "ambiguous initial kernel AT_RANDOM"
            );
        }
    }
    Err(anyhow!("unterminated initial kernel auxv"))
}

fn initial_vdso<'a>(rows: &'a [Map], auxv: &[u8]) -> Result<Option<&'a Map>> {
    ensure!(auxv.len().is_multiple_of(16), "short initial kernel auxv");
    let mut address = None;
    let mut terminated = false;
    for pair in auxv.as_chunks::<16>().0 {
        let kind = word(pair, 0)?;
        let value = word(pair, 8)?;
        if kind == libc::AT_NULL as usize {
            ensure!(value == 0, "invalid auxv terminator");
            terminated = true;
            break;
        }
        if kind == libc::AT_SYSINFO_EHDR as usize {
            ensure!(
                address.replace(value).is_none(),
                "ambiguous initial vDSO auxv"
            );
        }
    }
    ensure!(terminated, "unterminated initial kernel auxv");
    let mut candidates = rows.iter().filter(|row| row.path == b"[vdso]");
    let mapping = candidates.next();
    ensure!(candidates.next().is_none(), "ambiguous initial kernel vDSO");
    match (address.filter(|value| *value != 0), mapping) {
        (None, None) => Ok(None),
        (Some(address), Some(row)) => {
            ensure!(address == row.start, "initial vDSO auxv/mapping mismatch");
            ensure!(
                row.permissions == "r-xp" && row.inode == 0 && row.offset == 0,
                "unsupported initial vDSO mapping"
            );
            Ok(Some(row))
        }
        _ => Err(anyhow!("initial vDSO auxv/mapping mismatch")),
    }
}

impl Bootstrap {
    pub(super) fn new(root: Pid, launch: Launch) -> Result<Self> {
        let generation = generation(root)?;
        ensure!(generation != 0, "invalid initial process generation");
        let rows = maps(root)?;
        let expected_arguments = if launch.initializes_random() {
            let registers = nix::sys::ptrace::getregs(root)?;
            guest_arguments(initial_arguments(root, &rows, registers.rsp as usize)?)?
        } else {
            // Static legacy TAKE has no IMAGE/argv proof. Preserve its
            // existing admission instead of imposing the dynamic argc bound.
            Vec::new()
        };
        let auxv = read_path(format!("/proc/{root}/auxv"), 4096)?;
        // The caller still owns the initial attach stop. Capture pristine
        // bytes now; absence requires agreement between maps and kernel auxv,
        // and cannot stand in for an unreadable or already rewritten vDSO.
        let vdso = initial_vdso(&rows, &auxv)?
            .map(|mapping| {
                let bytes = remote_bytes(root, mapping.start, mapping.end - mapping.start)?;
                let elf = object::File::parse(bytes.as_slice())?;
                ensure!(
                    elf.architecture() == object::Architecture::X86_64 && elf.is_little_endian(),
                    "invalid initial kernel vDSO ELF"
                );
                Ok::<_, anyhow::Error>(VdsoSnapshot {
                    mapping: mapping.clone(),
                    bytes,
                })
            })
            .transpose()?;
        let initial_random = auxv_random(&auxv)?;
        ensure!(
            self::generation(root)? == generation,
            "initial process generation changed"
        );
        let prng = root_prng(launch.config.rng_seed());
        Ok(Self {
            launch,
            root,
            generation,
            image: None,
            prng,
            at_random_value: None,
            early_requests: Vec::new(),
            taken: false,
            sigill: None,
            initial_random,
            expected_arguments,
            vdso,
            other_objects: Vec::new(),
            continuations: BTreeMap::new(),
            static_reexec_observed: false,
        })
    }

    fn same_owner(&self, pid: Pid) -> Result<()> {
        ensure!(
            pid == self.root && generation(pid)? == self.generation,
            "bootstrap owner or process generation changed"
        );
        Ok(())
    }

    fn syscall_site(&self, pid: Pid, rows: &[Map], site: usize) -> Result<usize> {
        let loader = &self.launch.loader;
        let bias = loader.bias_at(pid, rows, site, "loader private syscall")?;
        let symbol = loader.symbol(bootstrap::SYSCALL_SYMBOL)?;
        ensure!(
            site == bias
                .checked_add(symbol)
                .ok_or_else(|| anyhow!("loader syscall address overflow"))?,
            "private bootstrap request is not at the retained loader instruction"
        );
        let row = containing(rows, site, 2)?;
        ensure!(
            row.permissions.contains('x'),
            "private bootstrap instruction is not executable"
        );
        loader.authenticate(pid, row)?;
        ensure!(
            loader.at_virtual(symbol, 2, false)? == [0x0f, 0x05]
                && remote_bytes(pid, site, 2)? == [0x0f, 0x05],
            "private bootstrap instruction bytes changed"
        );
        // Recheck the complete immutable descriptor in its live readonly load
        // mapping as well as the pre-launch held-file extraction.
        let descriptor = loader.symbol(self.launch.layout.descriptor)?;
        let extent = self.launch.layout.extent();
        let address = bias
            .checked_add(descriptor)
            .ok_or_else(|| anyhow!("descriptor address overflow"))?;
        let mapping = containing(rows, address, extent)?;
        ensure!(
            mapping.permissions.starts_with("r-") && !mapping.permissions.contains('w'),
            "live bootstrap descriptor is writable"
        );
        loader.authenticate(pid, mapping)?;
        ensure!(
            remote_bytes(pid, address, extent)? == loader.at_virtual(descriptor, extent, true)?,
            "live bootstrap descriptor differs from held object"
        );
        Ok(bias)
    }

    fn image(&mut self, pid: Pid, rows: &[Map], stack: usize, entry: usize) -> Result<i64> {
        ensure!(
            self.image.is_none() && !self.taken,
            "duplicate bootstrap IMAGE"
        );
        ensure!(stack.is_multiple_of(16), "unaligned final guest stack");
        let stack_map = containing(rows, stack, 8)?;
        ensure!(
            stack_map.permissions.starts_with("rw") && stack_map.path == b"[stack]",
            "final guest stack is not the owned writable stack"
        );
        let length = (stack_map.end - stack).min(64 * 1024);
        let bytes = remote_bytes(pid, stack, length)?;
        let mut at = authenticate_arguments(pid, rows, &bytes, &self.expected_arguments)?;
        let mut env_count = 0;
        loop {
            let pointer = word(&bytes, at)?;
            at += 8;
            if pointer == 0 {
                break;
            }
            env_count += 1;
            ensure!(
                env_count <= 4096 && stack_map.contains(pointer, 1),
                "unsupported final environment extent"
            );
        }
        let mut aux = std::collections::BTreeMap::new();
        let mut terminated = false;
        for _ in 0..128 {
            let kind = word(&bytes, at)?;
            let value = word(&bytes, at + 8)?;
            at += 16;
            if kind == libc::AT_NULL as usize {
                ensure!(value == 0, "bad final auxv terminator");
                terminated = true;
                break;
            }
            ensure!(
                aux.insert(kind, value).is_none(),
                "duplicate final auxv entry"
            );
        }
        ensure!(terminated, "unterminated final auxv");
        let get = |key: u64| {
            aux.get(&(key as usize))
                .copied()
                .ok_or_else(|| anyhow!("missing final auxv field {key}"))
        };
        let random = get(libc::AT_RANDOM)?;
        ensure!(
            random == self.initial_random && stack_map.contains(random, 16),
            "final AT_RANDOM is not the original owned writable target"
        );
        let program = &self.launch.program;
        let bias = program.bias_at(pid, rows, get(libc::AT_ENTRY)?, "program entry")?;
        let elf = object::File::parse(program.bytes.as_slice())?;
        let expected_entry = bias
            .checked_add(elf.entry() as usize)
            .ok_or_else(|| anyhow!("guest entry overflow"))?;
        ensure!(
            get(libc::AT_ENTRY)? == expected_entry,
            "final guest entry differs from held ELF"
        );
        let entry_map = containing(rows, expected_entry, 1)?;
        program.authenticate(pid, entry_map)?;
        ensure!(
            entry_map.permissions.contains('x'),
            "guest entry not executable"
        );
        program.program_headers(
            pid,
            rows,
            bias,
            get(libc::AT_PHDR)?,
            get(libc::AT_PHNUM)?,
            get(libc::AT_PHENT)?,
        )?;
        self.launch.authenticate_script(pid)?;
        let interpreter = self
            .launch
            .interpreter
            .as_ref()
            .ok_or_else(|| anyhow!("initial static image must not request random IMAGE"))?;
        let interp_bias = interpreter.bias_at(pid, rows, entry, "guest interpreter entry")?;
        let interp = object::File::parse(interpreter.bytes.as_slice())?;
        ensure!(
            entry
                == interp_bias
                    .checked_add(interp.entry() as usize)
                    .ok_or_else(|| anyhow!("interpreter entry overflow"))?,
            "loader final entry is not held guest interpreter"
        );
        let row = containing(rows, entry, 1)?;
        interpreter.authenticate(pid, row)?;
        ensure!(
            row.permissions.contains('x'),
            "interpreter entry not executable"
        );
        // No record here: the guest's post-exec emits it from the handoff.
        self.at_random_value = Some(write_initial_auxv(
            &mut self.prng,
            RemoteMemory(pid),
            AddrMut::from_raw(random).ok_or_else(|| anyhow!("null final AT_RANDOM"))?,
        )?);
        self.image = Some(InitialImage {
            pid: pid.as_raw(),
            start_time_ticks: self.generation,
            at_random: random,
        });
        Ok(0)
    }

    fn original_bytes(
        &mut self,
        pid: Pid,
        rows: &[Map],
        site: usize,
        length: usize,
    ) -> Result<Vec<u8>> {
        ensure!(length <= 128, "original instruction read exceeds bound");
        let row = containing(rows, site, length)?.clone();
        ensure!(
            row.permissions.contains('x'),
            "early getrandom source is not executable"
        );
        if row.path == b"[vdso]" {
            let vdso = self
                .vdso
                .as_ref()
                .ok_or_else(|| anyhow!("vDSO origin lacks an initial snapshot"))?;
            ensure!(row == vdso.mapping, "initial kernel vDSO mapping changed");
            let offset = site - vdso.mapping.start;
            Ok(vdso
                .bytes
                .get(offset..offset + length)
                .ok_or_else(|| anyhow!("short original vDSO instruction range"))?
                .to_vec())
        } else {
            let object = if self.launch.program.matches_mapping(&row) {
                &self.launch.program
            } else if let Some(interpreter) = self.launch.interpreter.as_ref()
                && interpreter.matches_mapping(&row)
            {
                interpreter
            } else {
                if !self
                    .other_objects
                    .iter()
                    .any(|object| object.matches_mapping(&row))
                {
                    ensure!(
                        self.other_objects.len() < MAX_OBJECTS,
                        "too many early bootstrap source objects"
                    );
                    let object = HeldObject::open(&row.mapped_path(pid)?)?;
                    object.authenticate(pid, &row)?;
                    self.other_objects.push(object);
                }
                self.other_objects
                    .iter()
                    .find(|object| object.matches_mapping(&row))
                    .ok_or_else(|| anyhow!("bootstrap source object identity changed"))?
            };
            object.authenticate(pid, &row)?;
            let bias = object.bias_at(pid, rows, site, "original instruction")?;
            let relative = site
                .checked_sub(bias)
                .ok_or_else(|| anyhow!("source bias underflow"))?;
            Ok(object.at_virtual(relative, length, false)?.to_vec())
        }
    }

    fn original_syscall(&mut self, pid: Pid, rows: &[Map], site: usize) -> Result<Map> {
        ensure!(
            self.original_bytes(pid, rows, site, 2)? == [0x0f, 0x05],
            "early source was not a syscall in the authenticated image"
        );
        Ok(containing(rows, site, 2)?.clone())
    }

    /// Save only a real kernel signal-delivery observation. A later private
    /// request's argument values or supplied stack pointer cannot create it.
    pub(super) fn signal(&mut self, pid: Pid, signal: nix::sys::signal::Signal) -> Result<()> {
        ensure!(
            self.sigill.take().is_none(),
            "intervening signal invalidated bootstrap SIGILL provenance"
        );
        if self.taken
            || !self.launch.initializes_random()
            || signal != nix::sys::signal::Signal::SIGILL
        {
            return Ok(());
        }
        self.same_owner(pid)?;
        let regs = nix::sys::ptrace::getregs(pid)?;
        let forwarded = [
            libc::SYS_getrandom,
            libc::SYS_prlimit64,
            libc::SYS_getrlimit,
        ];
        if !forwarded.iter().any(|sysno| regs.rax == *sysno as u64)
            || remote_bytes(pid, regs.rip as usize, 2)? != [0x0f, 0xff]
        {
            return Ok(());
        }
        ensure!(
            self.image.is_some(),
            "bootstrap SIGILL before authenticated IMAGE"
        );
        let info = nix::sys::ptrace::getsiginfo(pid)?;
        ensure!(
            info.si_code > 0 && unsafe { info.si_addr() } as usize == regs.rip as usize,
            "SIGILL is not a kernel fault at the rewritten source"
        );
        let rows = maps(pid)?;
        let mapping = self.original_syscall(pid, &rows, regs.rip as usize)?;
        self.sigill = Some(SigillOrigin {
            generation: self.generation,
            registers: regs,
            mapping,
        });
        Ok(())
    }

    /// Authenticates a forwarded request against the syscall it came from:
    /// `sysno` is the syscall the operation stands for, and `arguments` its
    /// leading arguments (rdi, rsi, rdx, r10 order), two to four of them.
    ///
    /// On the ordinary path the loader chose the operation from the real
    /// syscall number. On the SIGILL path the saved registers carry it, and it
    /// must match: otherwise a guest's own SIGILL handler could present one
    /// trapped syscall's arguments as another operation's.
    fn origin(
        &mut self,
        pid: Pid,
        rows: &[Map],
        sysno: libc::c_long,
        arguments: &[usize],
        wrapper: usize,
        loader_bias: usize,
    ) -> Result<()> {
        ensure!(
            (2..=4).contains(&arguments.len()),
            "bootstrap request authenticates 2 to 4 syscall arguments"
        );
        let layout = self.launch.layout;
        ensure!(
            arguments.len() < 4 || layout.r10.is_some(),
            "bootstrap frame descriptor has no r10"
        );
        if let Some(saved) = self.sigill.take() {
            ensure!(
                saved.generation == self.generation,
                "stale bootstrap SIGILL generation"
            );
            let regs = saved.registers;
            ensure!(
                regs.rax == sysno as u64,
                "SIGILL syscall differs from forwarded operation"
            );
            let original = [
                regs.rdi as usize,
                regs.rsi as usize,
                regs.rdx as usize,
                regs.r10 as usize,
            ];
            ensure!(
                original[..arguments.len()] == *arguments,
                "SIGILL original arguments differ from forwarded request"
            );
            ensure!(
                self.original_syscall(pid, rows, regs.rip as usize)? == saved.mapping
                    && remote_bytes(pid, regs.rip as usize, 2)? == [0x0f, 0xff],
                "SIGILL source changed before forwarding"
            );
            let address = wrapper
                .checked_add(layout.scratch_return)
                .ok_or_else(|| anyhow!("SIGILL return pointer overflow"))?;
            let row = containing(rows, address, 8)?;
            ensure!(
                row.path == b"[stack]" && row.permissions.starts_with("rw"),
                "SIGILL return word is not on owned stack"
            );
            let returned = word(&remote_bytes(pid, address, 8)?, 0)?;
            ensure!(
                returned == regs.rip as usize + 2,
                "SIGILL return word differs from saved kernel continuation"
            );
            return Ok(());
        }
        let row = containing(rows, wrapper, layout.size)?;
        ensure!(
            row.path == b"[stack]"
                && row.permissions.starts_with("rw")
                && wrapper.is_multiple_of(8),
            "ordinary bootstrap frame is not on owned stack"
        );
        let frame = remote_bytes(pid, wrapper, layout.size)?;
        let mut original = vec![
            word(&frame, layout.rdi)?,
            word(&frame, layout.rsi)?,
            word(&frame, layout.rdx)?,
        ];
        if arguments.len() == 4 {
            let r10 = layout
                .r10
                .ok_or_else(|| anyhow!("bootstrap frame descriptor has no r10"))?;
            original.push(word(&frame, r10)?);
        }
        ensure!(
            original[..arguments.len()] == *arguments,
            "assembly frame arguments differ from forwarded request"
        );
        let returned = word(&frame, layout.architectural_return)?;
        let site = returned
            .checked_sub(2)
            .ok_or_else(|| anyhow!("architectural return underflow"))?;
        self.original_syscall(pid, rows, site)?;
        let scratch = word(&frame, layout.scratch_return)?;
        // Real rewriter.c's syscall trampoline: saved return points at the
        // red-zone-restoring LEA, 33 bytes after its PUSH/LEA/body begins.
        let start = scratch
            .checked_sub(33)
            .ok_or_else(|| anyhow!("scratch return underflow"))?;
        let mapping = containing(rows, start, 41)?;
        ensure!(
            mapping.permissions.contains('x'),
            "scratch continuation is not executable"
        );
        let code = remote_bytes(pid, start, 41)?;
        ensure!(
            code[..4] == [0x50, 0x48, 0x8d, 0x05]
                && code[8..11] == [0x50, 0x48, 0xb8]
                && code[19..33]
                    == [
                        0x50, 0x48, 0x8d, 0x05, 0x06, 0, 0, 0, 0x48, 0x87, 0x44, 0x24, 0x10, 0xc3
                    ]
                && code[33..] == [0x48, 0x8d, 0xa4, 0x24, 0x80, 0, 0, 0],
            "unrecognized full-frame scratch trampoline"
        );
        let displacement = i32::from_le_bytes(code[4..8].try_into()?) as i64;
        ensure!(
            (start as i64)
                .checked_add(8)
                .and_then(|n| n.checked_add(displacement))
                == Some(returned as i64),
            "scratch trampoline names a different syscall continuation"
        );
        let handler = word(&code, 11)?;
        let bias = loader_bias;
        let mut handlers = BTreeSet::new();
        for symbol in ["handle_syscall", "handle_syscall_loader"] {
            handlers.insert(
                bias.checked_add(self.launch.loader.symbol(symbol)?)
                    .ok_or_else(|| anyhow!("handler address overflow"))?,
            );
        }
        ensure!(
            handlers.contains(&handler),
            "scratch trampoline does not call the held loader wrapper"
        );
        // Bind the source jump to this exact scratch body, including the real
        // SYSCALL clobber/red-zone prefix and both relocated byte sequences.
        // The production rewriter keeps five instructions in its ring; four
        // preceding/following x86 instructions occupy at most 4 * 15 bytes.
        // A matching frame and an unrelated executable byte pattern alone are
        // not evidence that this original instruction enters that trampoline.
        let prefix = start
            .checked_sub(17)
            .ok_or_else(|| anyhow!("scratch prefix underflow"))?;
        let prefix_bytes = remote_bytes(pid, prefix, 17)?;
        ensure!(
            prefix_bytes[..13]
                == [
                    0x48, 0x8d, 0x64, 0x24, 0x80, 0x90, 0x90, 0x9c, 0x41, 0x5b, 0x48, 0x8d, 0x0d,
                ]
                && (start as i64)
                    .checked_add(i32::from_le_bytes(prefix_bytes[13..17].try_into()?) as i64)
                    == Some(returned as i64),
            "scratch prefix does not preserve the original SYSCALL continuation"
        );
        let source_map = containing(rows, site, 2)?;
        let mut matched = 0;
        for pre in 0..=60 {
            let Some(jump) = site.checked_sub(pre) else {
                continue;
            };
            let Some(destination) = prefix.checked_sub(pre) else {
                continue;
            };
            if !source_map.contains(jump, 5) {
                continue;
            }
            let entry = remote_bytes(pid, jump, 5)?;
            if entry[0] != 0xe9
                || (jump as i64 + 5).checked_add(i32::from_le_bytes(entry[1..5].try_into()?) as i64)
                    != Some(destination as i64)
            {
                continue;
            }
            ensure!(
                containing(rows, destination, pre + 17)?
                    .permissions
                    .contains('x'),
                "scratch entry is not executable"
            );
            if pre != 0
                && self.original_bytes(pid, rows, jump, pre)?
                    != remote_bytes(pid, destination, pre)?
            {
                continue;
            }
            for post in 0..=60 {
                if pre + 2 + post < 5 || !source_map.contains(jump, pre + 2 + post) {
                    continue;
                }
                let after = start + 41 + post;
                if !mapping.contains(after, 5) {
                    continue;
                }
                let exit = remote_bytes(pid, after, 5)?;
                if exit[0] != 0xe9
                    || (after as i64 + 5)
                        .checked_add(i32::from_le_bytes(exit[1..5].try_into()?) as i64)
                        != Some((returned + post) as i64)
                {
                    continue;
                }
                if post != 0
                    && self.original_bytes(pid, rows, returned, post)?
                        != remote_bytes(pid, start + 41, post)?
                {
                    continue;
                }
                if pre + 2 + post > 5
                    && remote_bytes(pid, jump + 5, pre + 2 + post - 5)?
                        .iter()
                        .any(|byte| *byte != 0x90)
                {
                    continue;
                }
                matched += 1;
            }
        }
        ensure!(
            matched == 1,
            "missing/ambiguous rewritten source-to-scratch linkage"
        );
        ensure!(
            remote_bytes(pid, site, 2)? != [0x0f, 0x05]
                && remote_bytes(pid, site, 2)? != [0x0f, 0xff],
            "ordinary assembly path lacks a rewritten source site"
        );
        Ok(())
    }

    /// None leaves the existing supervisor path untouched. Protocol ownership,
    /// phase and shape errors abort the run; syscall errors remain Linux results.
    pub(super) fn request(
        &mut self,
        pid: Pid,
        regs: &libc::user_regs_struct,
    ) -> Result<Option<i64>> {
        let private =
            regs.orig_rax == libc::SYS_prctl as u64 && regs.rdi == bootstrap::PRCTL_OPTION;
        if !private {
            ensure!(
                self.sigill.take().is_none(),
                "intervening syscall invalidated bootstrap SIGILL provenance"
            );
            return Ok(None);
        }
        let continuation = self.continuations.get(&pid).copied();
        if let Some(image) = continuation {
            ensure!(
                generation(pid)? == image.start_time_ticks,
                "continuation owner or process generation changed"
            );
        } else {
            self.same_owner(pid)?;
        }
        let rows = maps(pid)?;
        let site = (regs.rip as usize)
            .checked_sub(2)
            .ok_or_else(|| anyhow!("private syscall RIP underflow"))?;
        let loader_bias = self.syscall_site(pid, &rows, site)?;
        let initial_static = !self.taken && !self.launch.initializes_random();
        if continuation.is_some() || initial_static {
            ensure!(
                self.sigill.is_none()
                    && regs.rsi == bootstrap::TAKE_STATE
                    && regs.r8 == bootstrap::VERSION
                    && regs.r9 == 0,
                "legacy continuation requires the exact TAKE protocol"
            );
            let image = if let Some(image) = continuation {
                image
            } else {
                // Static clients initialize their plugin before final-stack
                // compaction. Authenticate the actual mapped held program,
                // while leaving their ordinary auxv/ThreadState path alone.
                self.launch.authenticate_script(pid)?;
                let program = &self.launch.program;
                let bias = program.bias(pid, &rows)?;
                let elf = object::File::parse(program.bytes.as_slice())?;
                let entry = bias
                    .checked_add(elf.entry() as usize)
                    .ok_or_else(|| anyhow!("static entry overflow"))?;
                let row = containing(&rows, entry, 1)?;
                ensure!(
                    row.permissions.contains('x'),
                    "held static entry not executable"
                );
                program.authenticate(pid, row)?;
                InitialImage {
                    pid: pid.as_raw(),
                    start_time_ticks: self.generation,
                    at_random: self.initial_random,
                }
            };
            self.check_current_auxv(pid, &rows, image)?;
            let state = if initial_static {
                LoaderState::InitialStaticLegacy
            } else {
                LoaderState::ObservedExecContinuation
            };
            let bytes = encode_continuation(&self.launch.config, image, state)?;
            let result = Self::write_take(pid, regs, &bytes)?;
            if result > 0 {
                if initial_static {
                    self.taken = true;
                }
                self.continuations.remove(&pid);
            }
            return Ok(Some(result));
        }
        ensure!(!self.taken, "bootstrap state already handed off");
        let result = match regs.rsi {
            bootstrap::IMAGE => {
                ensure!(
                    self.sigill.is_none() && regs.r8 == bootstrap::VERSION && regs.r9 == 0,
                    "invalid IMAGE protocol shape"
                );
                self.image(pid, &rows, regs.rdx as usize, regs.r10 as usize)
                    .map_err(|error| anyhow!("bootstrap IMAGE authentication: {error:#}"))?
            }
            bootstrap::GETRANDOM => {
                ensure!(self.image.is_some(), "early getrandom before IMAGE");
                let args = [regs.rdx as usize, regs.r10 as usize, regs.r8 as usize];
                self.origin(
                    pid,
                    &rows,
                    libc::SYS_getrandom,
                    &args,
                    regs.r9 as usize,
                    loader_bias,
                )?;
                let call = Syscall::from_raw(
                    Sysno::getrandom,
                    SyscallArgs::new(args[0], args[1], args[2], 0, 0, 0),
                );
                let Syscall::Getrandom(call) = call else {
                    unreachable!()
                };
                ensure!(
                    self.early_requests
                        .iter()
                        .filter(|request| matches!(request, EarlyRequest::Getrandom(_)))
                        .count()
                        < MAX_EARLY_GETRANDOM,
                    "more than {MAX_EARLY_GETRANDOM} early getrandom requests"
                );
                // Detcore's root thread does not exist yet, so its seeding
                // records are still to come: emit no record now. Post-exec
                // emits this fill's record from the handoff, after them.
                random_response(
                    getrandom_unrecorded(&mut self.prng, RemoteMemory(pid), call).map(
                        |(result, fill)| {
                            self.early_requests
                                .extend(fill.map(EarlyRequest::Getrandom));
                            result
                        },
                    ),
                )?
            }
            bootstrap::TAKE_STATE => {
                ensure!(
                    self.sigill.is_none() && regs.r8 == bootstrap::VERSION && regs.r9 == 0,
                    "invalid TAKE protocol shape"
                );
                let image = self.image.ok_or_else(|| anyhow!("TAKE before IMAGE"))?;
                self.check_current_auxv(pid, &rows, image)?;
                let at_random_value = self
                    .at_random_value
                    .ok_or_else(|| anyhow!("TAKE before the AT_RANDOM write"))?;
                let bytes = encode_initial_state(
                    &self.launch.config,
                    image,
                    &self.prng,
                    at_random_value,
                    &self.early_requests,
                )?;
                let result = Self::write_take(pid, regs, &bytes)?;
                if result > 0 {
                    self.taken = true;
                }
                result
            }
            bootstrap::PRLIMIT => {
                ensure!(self.image.is_some(), "early prlimit64 before IMAGE");
                // prlimit64(pid, resource, NULL, old): the loader forwards only
                // reads, so the new-limit argument must be NULL.
                let (target, resource, old) =
                    (regs.rdx as usize, regs.r10 as usize, regs.r8 as usize);
                self.origin(
                    pid,
                    &rows,
                    libc::SYS_prlimit64,
                    &[target, resource, 0, old],
                    regs.r9 as usize,
                    loader_bias,
                )?;
                // pid_t is a C int: the upper half of the register is not
                // part of the argument.
                let target = target as i32;
                self.early_limit_read(pid, resource, old, LimitReadCall::Prlimit64 { pid: target })?
            }
            bootstrap::GETRLIMIT => {
                ensure!(self.image.is_some(), "early getrlimit before IMAGE");
                ensure!(regs.r8 == 0, "invalid GETRLIMIT protocol shape");
                let (resource, limit) = (regs.rdx as usize, regs.r10 as usize);
                self.origin(
                    pid,
                    &rows,
                    libc::SYS_getrlimit,
                    &[resource, limit],
                    regs.r9 as usize,
                    loader_bias,
                )?;
                self.early_limit_read(pid, resource, limit, LimitReadCall::Getrlimit)?
            }
            _ => return Err(anyhow!("unknown loader bootstrap operation")),
        };
        Ok(Some(result))
    }

    /// Answers a resource-limit read the loader forwarded before the plugin
    /// exists, with Detcore's initial limit, as Detcore's own handlers answer it
    /// once running: an invalid resource is EINVAL, a prlimit64 aimed at
    /// another process is EPERM, a getrlimit without a buffer is EFAULT. A
    /// successful read joins the early requests post-exec records.
    fn early_limit_read(
        &mut self,
        pid: Pid,
        resource: usize,
        destination: usize,
        call: LimitReadCall,
    ) -> Result<i64> {
        let reads = self
            .early_requests
            .iter()
            .filter(|request| matches!(request, EarlyRequest::LimitRead { .. }))
            .count();
        ensure!(
            reads < MAX_EARLY_LIMIT_READS,
            "more than {MAX_EARLY_LIMIT_READS} early resource-limit reads"
        );
        // The resource is a C int: the upper half of the register is not part
        // of the argument, for Linux or for Detcore's handlers.
        let resource = resource as u32;
        let destination = AddrMut::<u8>::from_raw(destination);
        if call == LimitReadCall::Getrlimit {
            // Detcore's getrlimit, in order: a negative resource is EINVAL
            // (Reverie decodes it as an int), then a missing buffer is EFAULT,
            // then an unknown resource is EINVAL. Its prlimit64 accepts a
            // missing old pointer.
            if (resource as i32) < 0 {
                return Ok(-libc::EINVAL as i64);
            }
            if destination.is_none() {
                return Ok(-libc::EFAULT as i64);
            }
        }
        let Some((current, maximum)) = detcore::initial_resource_limit(resource) else {
            return Ok(-libc::EINVAL as i64);
        };
        if let LimitReadCall::Prlimit64 { pid: target } = call
            && target != 0
            && target != pid.as_raw()
        {
            return Ok(-libc::EPERM as i64);
        }
        if let Some(destination) = destination {
            let mut bytes = [0u8; 16];
            bytes[..8].copy_from_slice(&current.to_ne_bytes());
            bytes[8..].copy_from_slice(&maximum.to_ne_bytes());
            if let Err(error) = RemoteMemory(pid).write_exact(destination, &bytes) {
                return Ok(-(error.into_raw() as i64));
            }
        }
        self.early_requests.push(EarlyRequest::LimitRead {
            call,
            resource,
            current,
            maximum,
        });
        Ok(0)
    }

    fn write_take(pid: Pid, regs: &libc::user_regs_struct, bytes: &[u8]) -> Result<i64> {
        let capacity = regs.r10 as usize;
        let destination = AddrMut::from_raw(regs.rdx as usize);
        if capacity == 0 || capacity > bootstrap::MAX_STATE_BYTES || destination.is_none() {
            return Ok(-libc::EINVAL as i64);
        }
        if bytes.len() > capacity {
            return Ok(-libc::EMSGSIZE as i64);
        }
        Ok(
            match RemoteMemory(pid).write_exact(destination.unwrap(), bytes) {
                Ok(()) => bytes.len() as i64,
                Err(e) => -(e.into_raw() as i64),
            },
        )
    }

    fn check_current_auxv(&self, pid: Pid, rows: &[Map], image: InitialImage) -> Result<()> {
        ensure!(
            generation(pid)? == image.start_time_ticks
                && pid.as_raw() == image.pid
                && auxv_random(&read_path(format!("/proc/{pid}/auxv"), 4096)?)? == image.at_random,
            "loader handoff image or kernel auxv changed"
        );
        let row = containing(rows, image.at_random, 16)?;
        ensure!(
            row.path == b"[stack]" && row.permissions.starts_with("rw"),
            "loader handoff AT_RANDOM is not the owned writable stack"
        );
        Ok(())
    }

    pub(super) fn forget(&mut self, pid: Pid) {
        self.continuations.remove(&pid);
    }

    /// Called only for a real kernel event on the supervisor's owned lineage,
    /// before it resumes that image. An exec replaces any unconsumed earlier
    /// image proof; a mere matching pid/start or user-supplied payload cannot
    /// create one. Entries live no longer than the existing owned tracee set.
    pub(super) fn event(&mut self, pid: Pid, event: libc::c_int) -> Result<()> {
        ensure!(
            self.sigill.take().is_none(),
            "intervening ptrace event invalidated bootstrap SIGILL provenance"
        );
        if event == libc::PTRACE_EVENT_EXEC {
            let image = InitialImage {
                pid: pid.as_raw(),
                start_time_ticks: generation(pid)?,
                at_random: auxv_random(&read_path(format!("/proc/{pid}/auxv"), 4096)?)?,
            };
            self.check_current_auxv(pid, &maps(pid)?, image)?;
            if self.taken {
                self.continuations.insert(pid, image);
                return Ok(());
            }
            if !self.launch.initializes_random() && !self.static_reexec_observed {
                self.same_owner(pid)?;
                // The static loader may exec its dynamic linker once to
                // preload the plugin. This does not authorize arbitrary initial
                // dynamic execution: TAKE must still prove the original held
                // static program and exact loader mapping before succeeding.
                self.static_reexec_observed = true;
                self.initial_random = image.at_random;
                return Ok(());
            }
        }
        ensure!(
            self.taken
                || !matches!(
                    event,
                    libc::PTRACE_EVENT_CLONE
                        | libc::PTRACE_EVENT_FORK
                        | libc::PTRACE_EVENT_VFORK
                        | libc::PTRACE_EVENT_EXEC
                ),
            "clone/fork/exec before initial random handoff is unsupported"
        );
        Ok(())
    }
}

#[cfg(test)]
#[path = "sabre_bootstrap_user_access_tests.rs"]
mod user_access_tests;

#[cfg(test)]
mod tests {
    use std::io::Seek;
    use std::io::SeekFrom;
    use std::io::Write;

    use super::*;

    #[test]
    fn generation_accepts_raw_worker_comm_and_rejects_invalid_identity() {
        struct RestoreComm {
            tid: i32,
            name: [u8; 16],
        }
        impl Drop for RestoreComm {
            fn drop(&mut self) {
                assert_eq!(unsafe { libc::syscall(libc::SYS_gettid) } as i32, self.tid);
                assert_eq!(
                    unsafe { libc::prctl(libc::PR_SET_NAME, self.name.as_ptr()) },
                    0
                );
            }
        }
        // Change only this libtest worker's name, never the process leader or
        // another test's thread. This is a native reader control, not a guest.
        let tid = unsafe { libc::syscall(libc::SYS_gettid) } as i32;
        let pid = Pid::from_raw(tid);
        let expected = generation(pid).unwrap();
        let mut restore = RestoreComm { tid, name: [0; 16] };
        assert_eq!(
            unsafe { libc::prctl(libc::PR_GET_NAME, restore.name.as_mut_ptr()) },
            0
        );
        assert_eq!(
            unsafe { libc::prctl(libc::PR_SET_NAME, c"raw) \xff) task".as_ptr()) },
            0
        );
        let bytes = read_path(format!("/proc/{pid}/stat"), 4096).unwrap();
        // These are actual kernel bytes rejected by the former whole-file
        // UTF-8 conversion, including an embedded closing delimiter.
        assert!(std::str::from_utf8(&bytes).is_err());
        assert!(bytes.windows(12).any(|row| row == b"raw) \xff) task"));
        assert_eq!(generation(pid).unwrap(), expected);
        assert_eq!(stat_generation(pid, &bytes).unwrap(), expected);
        expect_error(
            stat_generation(Pid::from_raw(tid + 1), &bytes),
            "owner mismatch",
        );
        expect_error(stat_generation(pid, b"missing delimiter"), "malformed");
        let end = bytes.windows(2).rposition(|row| row == b") ").unwrap();
        let mut fields: Vec<&[u8]> = bytes[end + 2..]
            .split(u8::is_ascii_whitespace)
            .filter(|field| !field.is_empty())
            .collect();
        let mut malformed = bytes[..end + 2].to_vec();
        fields[19] = b"not-a-number";
        malformed.extend(fields.join(&b' '));
        assert!(stat_generation(pid, &malformed).is_err());
        expect_error(
            stat_generation(pid, &bytes[..end + 2]),
            "missing process generation",
        );
        let owner_end = bytes.windows(2).position(|row| row == b" (").unwrap();
        let mut malformed_owner = b"not-a-pid".to_vec();
        malformed_owner.extend_from_slice(&bytes[owner_end..]);
        assert!(stat_generation(pid, &malformed_owner).is_err());
        drop(restore);
    }

    struct Mapping {
        address: *mut libc::c_void,
        _file: File,
    }

    impl Mapping {
        fn new(path: &Path) -> Self {
            let file = File::open(path).unwrap();
            let address = unsafe {
                libc::mmap(
                    std::ptr::null_mut(),
                    PAGE,
                    libc::PROT_READ,
                    libc::MAP_PRIVATE,
                    file.as_raw_fd(),
                    0,
                )
            };
            assert_ne!(address, libc::MAP_FAILED);
            Self {
                address,
                _file: file,
            }
        }
    }

    impl Drop for Mapping {
        fn drop(&mut self) {
            assert_eq!(unsafe { libc::munmap(self.address, PAGE) }, 0);
        }
    }

    fn fixture_file(directory: &Path, name: &[u8]) -> PathBuf {
        let path = directory.join(std::ffi::OsStr::from_bytes(name));
        std::fs::write(&path, [0x5a; PAGE]).unwrap();
        path
    }

    fn expect_error<T>(result: Result<T>, expected: &str) {
        let error = result.err().expect("invalid bootstrap input was accepted");
        assert!(
            error.to_string().contains(expected),
            "expected {expected:?}, got {error:#}"
        );
    }

    fn unrelated_mapping(name: &[u8], deleted: bool) {
        let directory = tempfile::tempdir().unwrap();
        let clean = fixture_file(directory.path(), b"selected");
        let unrelated = fixture_file(directory.path(), name);
        let _mapping = Mapping::new(&unrelated);
        if deleted {
            std::fs::remove_file(&unrelated).unwrap();
        }
        // Exercise the actual /proc reader while the unrelated VMA is live.
        let held = HeldObject::open_file(&clean).unwrap();
        held.authenticate_path(Pid::this()).unwrap();
    }

    #[test]
    fn unrelated_space_mapping_preserves_selected_object() {
        unrelated_mapping(b"unrelated space", false);
    }

    #[test]
    fn unrelated_deleted_mapping_preserves_selected_object() {
        unrelated_mapping(b"unrelated-deleted", true);
    }

    #[test]
    fn unrelated_non_utf8_mapping_preserves_selected_object() {
        unrelated_mapping(b"unrelated-\xff", false);
    }

    #[test]
    fn unrelated_newline_mapping_preserves_selected_object() {
        unrelated_mapping(b"unrelated-\n-name", false);
    }

    #[test]
    fn selected_mapping_authenticates_identity_and_content() {
        let directory = tempfile::tempdir().unwrap();
        let path = fixture_file(directory.path(), b"selected");
        let held = HeldObject::open_file(&path).unwrap();
        let mapping = Mapping::new(&path);
        let rows = maps(Pid::this()).unwrap();
        held.authenticate(
            Pid::this(),
            containing(&rows, mapping.address as usize, 1).unwrap(),
        )
        .unwrap();
    }

    #[test]
    fn selected_mapping_rejects_same_bytes_in_another_object() {
        let directory = tempfile::tempdir().unwrap();
        let path = fixture_file(directory.path(), b"selected");
        let held = HeldObject::open_file(&path).unwrap();
        let other = fixture_file(directory.path(), b"same-bytes-distinct-object");
        let mapping = Mapping::new(&other);
        let rows = maps(Pid::this()).unwrap();
        expect_error(
            held.authenticate(
                Pid::this(),
                containing(&rows, mapping.address as usize, 1).unwrap(),
            ),
            "bootstrap mapped object identity mismatch",
        );
    }

    #[test]
    fn selected_mapping_rejects_changed_content() {
        let directory = tempfile::tempdir().unwrap();
        let path = fixture_file(directory.path(), b"selected");
        let held = HeldObject::open_file(&path).unwrap();
        let mut file = std::fs::OpenOptions::new().write(true).open(&path).unwrap();
        file.seek(SeekFrom::Start(128)).unwrap();
        file.write_all(&[held.bytes[128] ^ 1]).unwrap();
        expect_error(
            held.authenticate_path(Pid::this()),
            "bootstrap mapped pathname content changed",
        );
    }

    fn raw_selected_mismatch(name: &[u8]) {
        let directory = tempfile::tempdir().unwrap();
        let path = fixture_file(directory.path(), name);
        let held = HeldObject::open_file(&path).unwrap();
        let other = fixture_file(directory.path(), b"different-object");
        let mapping = Mapping::new(&other);
        let rows = maps(Pid::this()).unwrap();
        expect_error(
            held.authenticate(
                Pid::this(),
                containing(&rows, mapping.address as usize, 1).unwrap(),
            ),
            "bootstrap mapped object identity mismatch",
        );
    }

    #[test]
    fn selected_non_utf8_mapping_rejects_another_object() {
        raw_selected_mismatch(b"selected-\xff");
    }

    #[test]
    fn selected_newline_mapping_rejects_another_object() {
        raw_selected_mismatch(b"selected-\n-name");
    }

    #[test]
    fn selected_deleted_mapping_is_refused() {
        let directory = tempfile::tempdir().unwrap();
        let path = fixture_file(directory.path(), b"selected");
        let held = HeldObject::open_file(&path).unwrap();
        let mapping = Mapping::new(&path);
        std::fs::remove_file(&path).unwrap();
        let rows = maps(Pid::this()).unwrap();
        expect_error(
            held.authenticate(
                Pid::this(),
                containing(&rows, mapping.address as usize, 1).unwrap(),
            ),
            "bootstrap mapped object identity mismatch",
        );
    }

    fn words(values: &[u64]) -> Vec<u8> {
        values
            .iter()
            .flat_map(|value| value.to_le_bytes())
            .collect()
    }

    // The descriptor exported by the landed x86-64 SaBRe C frame definition.
    const FRAME_WORDS: [u64; 9] = [1, 144, 72, 80, 88, 128, 136, 8, 9];

    #[test]
    fn frame_descriptor_decodes_the_exported_layout() {
        let layout = FrameLayout::decode(&words(&FRAME_WORDS)).unwrap();
        assert_eq!(
            (
                layout.size,
                layout.rdi,
                layout.rsi,
                layout.rdx,
                layout.architectural_return,
                layout.scratch_return
            ),
            (144, 72, 80, 88, 128, 136)
        );
    }

    // Version 2 appends r10's offset; struct syscall_stackframe puts it sixth.
    const FRAME_WORDS_V2: [u64; 10] = [2, 144, 72, 80, 88, 128, 136, 8, 10, 48];

    #[test]
    fn frame_descriptor_v2_decodes_r10() {
        let layout = FrameLayout::decode(&words(&FRAME_WORDS_V2)).unwrap();
        assert_eq!(layout.r10, Some(48));
        assert_eq!(layout.extent(), 80);
        assert_eq!(layout.descriptor, LAYOUT_SYMBOL_V2);
        let v1 = FrameLayout::decode(&words(&FRAME_WORDS)).unwrap();
        assert_eq!(
            (v1.r10, v1.extent(), v1.descriptor),
            (None, 72, LAYOUT_SYMBOL)
        );
        // A version 2 descriptor that claims 9 words, or a version 1 one padded
        // to 10, is neither.
        let mut nine = FRAME_WORDS_V2;
        nine[8] = 9;
        expect_error(
            FrameLayout::decode(&words(&nine)),
            "unsupported bootstrap frame descriptor",
        );
        let mut padded_v1 = FRAME_WORDS_V2;
        padded_v1[0] = 1;
        expect_error(
            FrameLayout::decode(&words(&padded_v1)),
            "unsupported bootstrap frame descriptor",
        );
        // r10 must lie inside the frame like every other field.
        let mut outside = FRAME_WORDS_V2;
        outside[9] = 144;
        expect_error(
            FrameLayout::decode(&words(&outside)),
            "bootstrap frame field outside extent",
        );
    }

    #[test]
    fn frame_descriptor_rejects_invalid_protocol() {
        let bytes = words(&FRAME_WORDS);
        expect_error(
            FrameLayout::decode(&bytes[..71]),
            "unsupported bootstrap frame descriptor",
        );
        let mut extended = bytes;
        extended.extend_from_slice(&0u64.to_le_bytes());
        expect_error(
            FrameLayout::decode(&extended),
            "unsupported bootstrap frame descriptor",
        );
        for (field, value) in [(0, 2), (7, 4), (8, 8)] {
            let mut descriptor = FRAME_WORDS;
            descriptor[field] = value;
            expect_error(
                FrameLayout::decode(&words(&descriptor)),
                "unsupported bootstrap frame descriptor",
            );
        }
    }

    #[test]
    fn frame_descriptor_rejects_invalid_size() {
        for size in [0, 7, 145, 4104] {
            let mut descriptor = FRAME_WORDS;
            descriptor[1] = size;
            expect_error(
                FrameLayout::decode(&words(&descriptor)),
                "invalid bootstrap full frame size",
            );
        }
    }

    #[test]
    fn frame_descriptor_rejects_invalid_offsets() {
        for offset in [73, 144, u64::MAX - 7] {
            let mut descriptor = FRAME_WORDS;
            descriptor[2] = offset;
            expect_error(
                FrameLayout::decode(&words(&descriptor)),
                "bootstrap frame field outside extent",
            );
        }
        let mut descriptor = FRAME_WORDS;
        descriptor[3] = descriptor[2];
        expect_error(
            FrameLayout::decode(&words(&descriptor)),
            "overlapping bootstrap frame fields",
        );
    }

    #[test]
    fn auxv_random_accepts_a_terminated_vector() {
        let bytes = words(&[
            libc::AT_PAGESZ,
            4096,
            libc::AT_RANDOM,
            0x1234,
            libc::AT_NULL,
            0,
        ]);
        assert_eq!(auxv_random(&bytes).unwrap(), 0x1234);
    }

    #[test]
    fn auxv_random_requires_a_valid_terminator_and_random_entry() {
        expect_error(
            auxv_random(&words(&[libc::AT_RANDOM, 0x1234])),
            "unterminated initial kernel auxv",
        );
        expect_error(
            auxv_random(&words(&[libc::AT_RANDOM, 0x1234, libc::AT_NULL, 1])),
            "invalid auxv terminator",
        );
        expect_error(
            auxv_random(&words(&[libc::AT_PAGESZ, 4096, libc::AT_NULL, 0])),
            "initial kernel auxv has no AT_RANDOM",
        );
    }

    #[test]
    fn auxv_random_rejects_duplicate_or_zero_random_entries() {
        expect_error(
            auxv_random(&words(&[
                libc::AT_RANDOM,
                0x1234,
                libc::AT_RANDOM,
                0x5678,
                libc::AT_NULL,
                0,
            ])),
            "ambiguous initial kernel AT_RANDOM",
        );
        expect_error(
            auxv_random(&words(&[libc::AT_RANDOM, 0, libc::AT_NULL, 0])),
            "ambiguous initial kernel AT_RANDOM",
        );
    }

    #[test]
    fn auxv_random_rejects_partial_entries() {
        let bytes = words(&[libc::AT_RANDOM, 0x1234, libc::AT_NULL, 0]);
        for length in [1, 8, 15, 17, 31] {
            expect_error(auxv_random(&bytes[..length]), "short initial kernel auxv");
        }
    }

    #[test]
    fn selected_raw_paths_authenticate_the_held_object() {
        for name in [
            b"space name".as_slice(),
            b"non-utf8-\xff",
            b"newline-\n",
            b"literal-\\012",
            b"back\\slash",
            b"literal (deleted)",
        ] {
            let directory = tempfile::tempdir().unwrap();
            let path = fixture_file(directory.path(), name);
            let held = HeldObject::open_file(&path).unwrap();
            let mapping = Mapping::new(&path);
            let rows = maps(Pid::this()).unwrap();
            let row = containing(&rows, mapping.address as usize, 1).unwrap();
            held.authenticate(Pid::this(), row).unwrap();
            assert_eq!(row.mapped_path(Pid::this()).unwrap(), held.path);
        }
    }

    #[test]
    fn ambiguous_kernel_path_display_does_not_select_another_inode() {
        let directory = tempfile::tempdir().unwrap();
        let newline = fixture_file(directory.path(), b"same-\n");
        let literal = fixture_file(directory.path(), b"same-\\012");
        let a = HeldObject::open_file(&newline).unwrap();
        let b = HeldObject::open_file(&literal).unwrap();
        let ma = Mapping::new(&newline);
        let mb = Mapping::new(&literal);
        let rows = maps(Pid::this()).unwrap();
        let ra = containing(&rows, ma.address as usize, 1).unwrap();
        let rb = containing(&rows, mb.address as usize, 1).unwrap();
        assert_eq!(
            ra.path, rb.path,
            "exercise the real kernel's ambiguous display"
        );
        assert_ne!(ra.inode, rb.inode);
        assert_eq!(ra.mapped_path(Pid::this()).unwrap(), a.path);
        assert_eq!(rb.mapped_path(Pid::this()).unwrap(), b.path);
        a.authenticate(Pid::this(), ra).unwrap();
        b.authenticate(Pid::this(), rb).unwrap();
        expect_error(
            a.authenticate(Pid::this(), rb),
            "bootstrap mapped object identity mismatch",
        );
        expect_error(
            b.authenticate(Pid::this(), ra),
            "bootstrap mapped object identity mismatch",
        );
    }

    #[test]
    fn raw_root_relative_open_preserves_no_follow_and_canonical_guards() {
        let directory = tempfile::tempdir().unwrap();
        let path = fixture_file(directory.path(), b"selected-\xff");
        let link = directory.path().join("alias");
        std::os::unix::fs::symlink(&path, &link).unwrap();
        assert!(open_under_root(Pid::this(), &link).is_err());
        expect_error(
            open_under_root(Pid::this(), Path::new("relative")),
            "nonabsolute bootstrap object path",
        );
        expect_error(
            open_under_root(Pid::this(), &directory.path().join("../outside")),
            "noncanonical bootstrap object path",
        );
        assert_eq!(
            open_under_root(Pid::this(), &path)
                .unwrap()
                .metadata()
                .unwrap()
                .ino(),
            path.metadata().unwrap().ino()
        );
    }

    fn executable_fixture(interpreter: Option<&[u8]>) -> Vec<u8> {
        // A real ELF64 ET_EXEC with one executable PT_LOAD, a Linux exit(0)
        // entry, and an optional PT_INTERP. No native execution is needed to
        // test object parsing, pathname identity or trailing file data.
        let mut bytes = vec![0; PAGE];
        bytes[..7].copy_from_slice(b"\x7fELF\x02\x01\x01");
        bytes[16..18].copy_from_slice(&2u16.to_le_bytes());
        bytes[18..20].copy_from_slice(&62u16.to_le_bytes());
        bytes[20..24].copy_from_slice(&1u32.to_le_bytes());
        bytes[24..32].copy_from_slice(&0x400100u64.to_le_bytes());
        bytes[32..40].copy_from_slice(&64u64.to_le_bytes());
        bytes[52..54].copy_from_slice(&64u16.to_le_bytes());
        bytes[54..56].copy_from_slice(&56u16.to_le_bytes());
        bytes[56..58]
            .copy_from_slice(&(if interpreter.is_some() { 2u16 } else { 1 }).to_le_bytes());
        bytes[64..68].copy_from_slice(&1u32.to_le_bytes());
        bytes[68..72].copy_from_slice(&5u32.to_le_bytes());
        bytes[80..88].copy_from_slice(&0x400000u64.to_le_bytes());
        bytes[96..104].copy_from_slice(&(PAGE as u64).to_le_bytes());
        bytes[104..112].copy_from_slice(&(PAGE as u64).to_le_bytes());
        bytes[112..120].copy_from_slice(&(PAGE as u64).to_le_bytes());
        bytes[256..265].copy_from_slice(&[0xb8, 60, 0, 0, 0, 0x31, 0xff, 0x0f, 0x05]);
        if let Some(path) = interpreter {
            bytes[120..124].copy_from_slice(&3u32.to_le_bytes());
            bytes[128..136].copy_from_slice(&512u64.to_le_bytes());
            bytes[152..160].copy_from_slice(&((path.len() + 1) as u64).to_le_bytes());
            bytes[512..512 + path.len()].copy_from_slice(path);
        }
        bytes
    }

    #[test]
    fn initial_exec_arguments_preserve_kernel_bytes_and_client_delimiters() {
        use std::os::unix::process::CommandExt;

        use nix::sys::signal::Signal;
        use nix::sys::wait::WaitStatus;
        use nix::sys::wait::waitpid;
        struct OwnedChild(std::process::Child);
        impl Drop for OwnedChild {
            fn drop(&mut self) {
                // This test remains the sole waiter; the child has not been
                // reaped and cannot denote a reused PID at this point.
                let _ = self.0.kill();
                self.0.wait().expect("reap owned native exec-stop fixture");
            }
        }
        let long = vec![b'a'; 64 * 1024];
        let raw = b"raw-\xff\\012";
        let mut command = std::process::Command::new("/bin/true");
        command.args([
            OsStr::new("plugin"),
            OsStr::new("--"),
            OsStr::new("client"),
            OsStr::new("--"),
            OsStr::from_bytes(&long),
            OsStr::from_bytes(raw),
        ]);
        unsafe {
            command.pre_exec(|| {
                if libc::ptrace(libc::PTRACE_TRACEME, 0, 0, 0) == -1 {
                    return Err(std::io::Error::last_os_error());
                }
                Ok(())
            });
        }
        let child = OwnedChild(command.spawn().unwrap());
        let pid = Pid::from_raw(child.0.id() as i32);
        assert_eq!(
            waitpid(pid, None).unwrap(),
            WaitStatus::Stopped(pid, Signal::SIGTRAP)
        );
        let registers = nix::sys::ptrace::getregs(pid).unwrap();
        let rows = maps(pid).unwrap();
        let captured = initial_arguments(pid, &rows, registers.rsp as usize).unwrap();
        // The guest's argv is SaBRe's initial argv, kernel bytes and literal
        // `--` arguments included, unchanged.
        let direct = guest_arguments(captured.clone()).unwrap();
        assert_eq!(direct, captured);
        assert_eq!(direct[0], b"/bin/true\0");
        assert_eq!(direct[1], b"plugin\0");
        assert_eq!(direct[2], b"--\0");
        assert_eq!(&direct[5][..long.len()], long);
        assert_eq!(direct[5].last(), Some(&0));
        assert_eq!(&direct[6][..raw.len()], raw);
        expect_error(guest_arguments(Vec::new()), "unsupported bootstrap argc");
    }

    #[test]
    fn relocated_arguments_require_exact_bytes_order_and_terminators() {
        let expected = vec![
            b"/interpreter-\xff\0".to_vec(),
            b"--option\0".to_vec(),
            b"script\0".to_vec(),
        ];
        let mut actual = expected.clone();
        let vector = |values: &[Vec<u8>]| {
            let mut pointers = vec![values.len() as u64];
            pointers.extend(values.iter().map(|value| value.as_ptr() as u64));
            pointers.push(0);
            words(&pointers)
        };
        let rows = maps(Pid::this()).unwrap();
        assert_ne!(
            containing(&rows, actual[0].as_ptr() as usize, actual[0].len())
                .unwrap()
                .path,
            b"[stack]"
        );
        assert_eq!(
            authenticate_arguments(Pid::this(), &rows, &vector(&actual), &expected).unwrap(),
            40
        );
        actual.swap(0, 1);
        expect_error(
            authenticate_arguments(Pid::this(), &rows, &vector(&actual), &expected),
            "bytes/order changed",
        );
        actual.swap(0, 1);
        actual[0][0] ^= 1;
        expect_error(
            authenticate_arguments(Pid::this(), &rows, &vector(&actual), &expected),
            "bytes/order changed",
        );
        actual[0][0] ^= 1;
        *actual[1].last_mut().unwrap() = b'x';
        expect_error(
            authenticate_arguments(Pid::this(), &rows, &vector(&actual), &expected),
            "bytes/order changed",
        );
        *actual[1].last_mut().unwrap() = 0;
        let mut bad = vector(&actual);
        bad[8..16].copy_from_slice(&1u64.to_le_bytes());
        expect_error(
            authenticate_arguments(Pid::this(), &rows, &bad, &expected),
            "final argv mapping",
        );
        bad = vector(&actual);
        bad[0..8].copy_from_slice(&2u64.to_le_bytes());
        expect_error(
            authenticate_arguments(Pid::this(), &rows, &bad, &expected),
            "count changed",
        );
        bad = vector(&actual);
        bad[32..40].copy_from_slice(&1u64.to_le_bytes());
        expect_error(
            authenticate_arguments(Pid::this(), &rows, &bad, &expected),
            "missing final argv terminator",
        );
    }

    struct ElfMapping {
        address: *mut libc::c_void,
        _file: File,
    }
    impl ElfMapping {
        fn new(path: &Path) -> Self {
            let file = File::open(path).unwrap();
            let address = unsafe {
                libc::mmap(
                    std::ptr::null_mut(),
                    2 * PAGE,
                    libc::PROT_READ,
                    libc::MAP_PRIVATE,
                    file.as_raw_fd(),
                    0,
                )
            };
            assert_ne!(address, libc::MAP_FAILED);
            assert_eq!(
                unsafe {
                    libc::mprotect(
                        address.byte_add(PAGE),
                        PAGE,
                        libc::PROT_READ | libc::PROT_EXEC,
                    )
                },
                0
            );
            Self {
                address,
                _file: file,
            }
        }
        fn entry(&self) -> usize {
            self.address as usize + PAGE + 256
        }
        fn bias(&self) -> usize {
            self.address as usize - 0x400000
        }
        fn copy_phdr_page(&self, bytes: &[u8]) {
            assert_eq!(
                unsafe {
                    libc::mmap(
                        self.address,
                        PAGE,
                        libc::PROT_READ | libc::PROT_WRITE,
                        libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_FIXED,
                        -1,
                        0,
                    )
                },
                self.address
            );
            // Match the real rewriter's --i loop: word zero is not copied.
            unsafe {
                std::ptr::copy_nonoverlapping(
                    bytes.as_ptr().add(8),
                    self.address.cast::<u8>().add(8),
                    PAGE - 8,
                );
            }
            assert_eq!(
                unsafe { libc::mprotect(self.address, PAGE, libc::PROT_READ) },
                0
            );
        }
    }
    impl Drop for ElfMapping {
        fn drop(&mut self) {
            assert_eq!(unsafe { libc::munmap(self.address, 2 * PAGE) }, 0);
        }
    }
    fn two_page_elf() -> Vec<u8> {
        let mut bytes = executable_fixture(None);
        bytes.resize(2 * PAGE, 0);
        bytes[24..32].copy_from_slice(&0x401100u64.to_le_bytes());
        bytes[56..58].copy_from_slice(&2u16.to_le_bytes());
        bytes[68..72].copy_from_slice(&4u32.to_le_bytes());
        let header = bytes[64..120].to_vec();
        bytes[120..176].copy_from_slice(&header);
        bytes[124..128].copy_from_slice(&5u32.to_le_bytes());
        bytes[128..136].copy_from_slice(&(PAGE as u64).to_le_bytes());
        bytes[136..144].copy_from_slice(&0x401000u64.to_le_bytes());
        let code = bytes[256..265].to_vec();
        bytes[PAGE + 256..PAGE + 265].copy_from_slice(&code);
        bytes
    }

    #[test]
    fn executable_anchor_selects_real_duplicate_instances_and_rejects_other_objects() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("held-elf");
        let other = directory.path().join("different-inode");
        let bytes = two_page_elf();
        std::fs::write(&path, &bytes).unwrap();
        std::fs::write(&other, &bytes).unwrap();
        let held = HeldObject::open(&path).unwrap();
        let first = ElfMapping::new(&path);
        let second = ElfMapping::new(&path);
        let wrong = ElfMapping::new(&other);
        let rows = maps(Pid::this()).unwrap();
        // The old unanchored intersection still refuses two distinct bases.
        expect_error(
            held.bias(Pid::this(), &rows),
            "ambiguous bootstrap ELF load bias",
        );
        for mapping in [&first, &second] {
            assert_eq!(
                held.bias_at(Pid::this(), &rows, mapping.entry(), "test entry")
                    .unwrap(),
                mapping.bias()
            );
        }
        expect_error(
            held.bias_at(Pid::this(), &rows, wrong.entry(), "test entry"),
            "identity mismatch",
        );
        expect_error(
            held.bias_at(
                Pid::this(),
                &rows,
                first.address as usize + 64,
                "test entry",
            ),
            "not executable",
        );
        expect_error(
            held.bias_at(Pid::this(), &rows, 1, "test entry"),
            "test entry anchor=0x1",
        );
        // One real mapping can still have two candidate virtual addresses.
        // Deliberately overlapping file extents must not become first-match
        // selection merely because the observation names a single VMA.
        let mut ambiguous = bytes.clone();
        ambiguous[56..58].copy_from_slice(&3u16.to_le_bytes());
        ambiguous[176..232].copy_from_slice(&bytes[120..176]);
        ambiguous[192..200].copy_from_slice(&0x402000u64.to_le_bytes());
        let ambiguous_path = directory.path().join("ambiguous-loads");
        std::fs::write(&ambiguous_path, ambiguous).unwrap();
        let ambiguous_held = HeldObject::open(&ambiguous_path).unwrap();
        let ambiguous_mapping = ElfMapping::new(&ambiguous_path);
        expect_error(
            ambiguous_held.bias_at(
                Pid::this(),
                &maps(Pid::this()).unwrap(),
                ambiguous_mapping.entry(),
                "ambiguous entry",
            ),
            "missing/ambiguous anchored ELF load bias",
        );
        // Executable VMA permissions cannot extend the ELF's actual file
        // extent: the observed anchor is beyond p_filesz in this object.
        let mut short = bytes.clone();
        short[152..160].copy_from_slice(&128u64.to_le_bytes());
        let short_path = directory.path().join("short-executable-load");
        std::fs::write(&short_path, short).unwrap();
        let short_held = HeldObject::open(&short_path).unwrap();
        let short_mapping = ElfMapping::new(&short_path);
        expect_error(
            short_held.bias_at(
                Pid::this(),
                &maps(Pid::this()).unwrap(),
                short_mapping.entry(),
                "beyond file extent",
            ),
            "missing/ambiguous anchored ELF load bias",
        );
    }

    #[test]
    fn copied_phdr_requires_anchored_page_geometry_and_immutable_header_bytes() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("held-elf");
        let bytes = two_page_elf();
        std::fs::write(&path, &bytes).unwrap();
        let held = HeldObject::open(&path).unwrap();
        let mapping = ElfMapping::new(&path);
        let other = ElfMapping::new(&path);
        let phdr = mapping.address as usize + 64;
        let rows = maps(Pid::this()).unwrap();
        let bias = held
            .bias_at(Pid::this(), &rows, mapping.entry(), "test entry")
            .unwrap();
        held.program_headers(Pid::this(), &rows, bias, phdr, 2, 56)
            .unwrap();
        mapping.copy_phdr_page(&bytes);
        let rows = maps(Pid::this()).unwrap();
        expect_error(
            held.authenticate(Pid::this(), containing(&rows, phdr, 112).unwrap()),
            "identity mismatch",
        );
        assert_eq!(
            held.bias_at(Pid::this(), &rows, mapping.entry(), "test entry")
                .unwrap(),
            bias
        );
        held.program_headers(Pid::this(), &rows, bias, phdr, 2, 56)
            .unwrap();
        expect_error(
            held.program_headers(Pid::this(), &rows, other.bias(), phdr, 2, 56),
            "address differs",
        );
        expect_error(
            held.program_headers(Pid::this(), &rows, bias, phdr + 8, 2, 56),
            "address differs",
        );
        expect_error(
            held.program_headers(Pid::this(), &rows, bias, phdr, 3, 56),
            "count/size changed",
        );
        expect_error(
            held.program_headers(Pid::this(), &rows, bias, phdr, 2, 64),
            "count/size changed",
        );
        assert_eq!(
            unsafe { libc::mprotect(mapping.address, PAGE, libc::PROT_READ | libc::PROT_WRITE) },
            0
        );
        expect_error(
            held.program_headers(Pid::this(), &maps(Pid::this()).unwrap(), bias, phdr, 2, 56),
            "unrecognized copied PHDR mapping",
        );
        unsafe {
            *mapping.address.cast::<u8>().add(72) ^= 1;
        }
        assert_eq!(
            unsafe { libc::mprotect(mapping.address, PAGE, libc::PROT_READ) },
            0
        );
        expect_error(
            held.program_headers(Pid::this(), &maps(Pid::this()).unwrap(), bias, phdr, 2, 56),
            "headers differ",
        );
    }

    #[test]
    fn interpreter_parsers_preserve_raw_unix_paths() {
        for (script, expected) in [
            (
                b"#!/interpreter --op\0ignored later\n".as_slice(),
                vec![b"/interpreter".as_slice(), b"--op".as_slice()],
            ),
            (
                b"#!/interpreter \0ignored later\n".as_slice(),
                vec![b"/interpreter".as_slice()],
            ),
        ] {
            assert_eq!(script_words(script).unwrap(), expected);
        }
        expect_error(
            script_words(b"#!/interpreter\0bad --option\n"),
            "NUL in SaBRe script interpreter",
        );
        let script = b"#! /interpreter-\xff\\012 --option\nbody\n";
        assert_eq!(
            script_interpreter(script)
                .unwrap()
                .unwrap()
                .as_os_str()
                .as_bytes(),
            b"/interpreter-\xff\\012"
        );
        let path = b"/interpreter-\xff\n\\012";
        let bytes = executable_fixture(Some(path));
        object::File::parse(bytes.as_slice()).unwrap();
        assert_eq!(
            elf_interpreter(&bytes)
                .unwrap()
                .unwrap()
                .as_os_str()
                .as_bytes(),
            path
        );
        assert!(
            elf_interpreter(&executable_fixture(None))
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn interpreter_parsers_preserve_terminator_and_header_refusals() {
        expect_error(
            script_interpreter(b"#! \n"),
            "missing SaBRe script interpreter",
        );
        expect_error(
            script_interpreter(b"#!/bad\0path\n"),
            "NUL in SaBRe script interpreter",
        );
        let mut bytes = executable_fixture(Some(b"/interpreter"));
        bytes[524] = b'x';
        expect_error(elf_interpreter(&bytes), "invalid interpreter terminator");
        let mut bytes = executable_fixture(Some(b"/bad\0path"));
        expect_error(elf_interpreter(&bytes), "invalid interpreter terminator");
        bytes[56..58].copy_from_slice(&129u16.to_le_bytes());
        expect_error(elf_interpreter(&bytes), "unsupported ELF program headers");
        let mut bytes = executable_fixture(None);
        bytes[32..40].copy_from_slice(&u64::MAX.to_le_bytes());
        expect_error(elf_interpreter(&bytes), "program headers outside held ELF");
    }

    #[test]
    fn large_sparse_elf_snapshot_preserves_bytes_and_closes_aliases() {
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("large-elf");
        let file = std::fs::OpenOptions::new()
            .write(true)
            .read(true)
            .create_new(true)
            .open(&path)
            .unwrap();
        file.write_all_at(&executable_fixture(None), 0).unwrap();
        let length = (IN_MEMORY_SNAPSHOT_LIMIT + PAGE) as u64;
        file.set_len(length).unwrap();
        file.write_all_at(b"X", length - 1).unwrap();
        let held = HeldObject::open(&path).unwrap();
        let OriginalBytes::Disk(snapshot) = &held.bytes else {
            panic!("large object must use disk backing")
        };
        let rows = maps(Pid::this()).unwrap();
        let row = containing(&rows, snapshot.address, snapshot.length)
            .unwrap()
            .clone();
        assert_eq!(row.permissions, "r--p");
        let raw = row.mapped_path(Pid::this()).unwrap();
        assert!(
            !raw.parent().unwrap().exists(),
            "private snapshot directory survived construction"
        );
        let cache_device = super::super::HermitData::new()
            .data_dir()
            .metadata()
            .unwrap()
            .dev();
        for entry in std::fs::read_dir("/proc/self/fd").unwrap() {
            if let Ok(metadata) = entry.unwrap().path().metadata() {
                assert_ne!(
                    (metadata.dev(), metadata.ino()),
                    (cache_device, row.inode),
                    "snapshot FD alias survived construction"
                );
            }
        }
        assert_eq!(held.bytes.len() as u64, length);
        assert_eq!(held.bytes.last(), Some(&b'X'));
        held.authenticate_path(Pid::this()).unwrap();
        file.write_all_at(b"Y", length - 1).unwrap();
        expect_error(
            held.authenticate_path(Pid::this()),
            "bootstrap mapped pathname content changed",
        );
        assert_eq!(held.bytes.last(), Some(&b'X'));
        drop(held);
        assert!(
            !maps(Pid::this())
                .unwrap()
                .iter()
                .any(|current| current.start == row.start
                    && current.end == row.end
                    && current.inode == row.inode
                    && current.device == row.device),
            "snapshot mapping survived Drop"
        );
    }

    #[test]
    fn small_snapshot_preserves_memory_storage_and_content_authentication() {
        let directory = tempfile::tempdir().unwrap();
        let path = fixture_file(directory.path(), b"small");
        let held = HeldObject::open_file(&path).unwrap();
        assert!(matches!(held.bytes, OriginalBytes::Memory(_)));
        held.authenticate_path(Pid::this()).unwrap();
    }

    #[test]
    fn disk_snapshot_refuses_a_real_memory_backed_file() {
        let fd =
            unsafe { libc::memfd_create(c"bootstrap-backing-control".as_ptr(), libc::MFD_CLOEXEC) };
        assert!(fd >= 0);
        let file = unsafe { File::from_raw_fd(fd) };
        expect_error(
            require_supported_snapshot_filesystem(&file),
            "large bootstrap snapshot requires a supported cache filesystem",
        );
    }

    fn vdso_row() -> Map {
        Map {
            start: 0x1000,
            end: 0x2000,
            offset: 0,
            device: "00:00".into(),
            inode: 0,
            path: b"[vdso]".to_vec(),
            permissions: "r-xp".into(),
        }
    }

    #[test]
    fn vdso_absence_requires_both_maps_and_kernel_auxv_absence() {
        assert!(
            initial_vdso(&[], &words(&[libc::AT_NULL, 0]))
                .unwrap()
                .is_none()
        );
        assert!(
            initial_vdso(&[], &words(&[libc::AT_SYSINFO_EHDR, 0, libc::AT_NULL, 0]))
                .unwrap()
                .is_none()
        );
        expect_error(
            initial_vdso(
                &[],
                &words(&[libc::AT_SYSINFO_EHDR, 0x1000, libc::AT_NULL, 0]),
            ),
            "initial vDSO auxv/mapping mismatch",
        );
        expect_error(
            initial_vdso(&[vdso_row()], &words(&[libc::AT_NULL, 0])),
            "initial vDSO auxv/mapping mismatch",
        );
    }

    #[test]
    fn vdso_selection_preserves_identity_and_ambiguity_refusals() {
        let rows = [vdso_row()];
        let auxv = words(&[libc::AT_SYSINFO_EHDR, 0x1000, libc::AT_NULL, 0]);
        assert_eq!(initial_vdso(&rows, &auxv).unwrap(), Some(&rows[0]));
        expect_error(
            initial_vdso(&[vdso_row(), vdso_row()], &auxv),
            "ambiguous initial kernel vDSO",
        );
        expect_error(
            initial_vdso(
                &rows,
                &words(&[libc::AT_SYSINFO_EHDR, 0x2000, libc::AT_NULL, 0]),
            ),
            "initial vDSO auxv/mapping mismatch",
        );
        let mut writable = vdso_row();
        writable.permissions = "rwxp".into();
        expect_error(
            initial_vdso(&[writable], &auxv),
            "unsupported initial vDSO mapping",
        );
        expect_error(
            initial_vdso(
                &rows,
                &words(&[
                    libc::AT_SYSINFO_EHDR,
                    0x1000,
                    libc::AT_SYSINFO_EHDR,
                    0x1000,
                    libc::AT_NULL,
                    0,
                ]),
            ),
            "ambiguous initial vDSO auxv",
        );
        expect_error(
            initial_vdso(&rows, &auxv[..24]),
            "short initial kernel auxv",
        );
    }

    #[test]
    fn vdso_selection_matches_actual_proc_inputs() {
        let rows = maps(Pid::this()).unwrap();
        let auxv = read_path("/proc/self/auxv", 4096).unwrap();
        let selected = initial_vdso(&rows, &auxv).unwrap();
        assert_eq!(
            selected.is_some(),
            rows.iter().any(|row| row.path == b"[vdso]")
        );
    }
}