onelf-format 0.6.1

Binary format definitions for onelf
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
//! Per-library choice between the bundle and the host, made at launch.
//!
//! A bundle that reaches a host GPU driver has to load that driver's
//! dependencies too, and the host built them against its own glibc and
//! libstdc++. Whether they can share a process with the bundled copies
//! depends on which side is newer, and that differs per library, so the
//! answer cannot be a directory order. Each soname present on both sides
//! is compared by the versions it defines and the superset wins.
//!
//! The loader and libc are a unit: a libc only runs under the loader it
//! shipped with. They are compared first, and when the host's pair wins
//! the whole glibc family comes from the host so nothing bundled is loaded
//! beside a foreign loader. The host's loader never runs as installed:
//! it would still search its compiled-in default directories, where any
//! host library is found. A private copy closed to those directories by
//! [`crate::ldso::scrub_glibc_loader`] runs instead, and a host loader
//! that cannot be closed leaves the bundled pair in place.
//!
//! Winners from the host are materialized as symlinks in a link farm, and
//! the runtime shows each one the bundle lacks in the bundle's library
//! directory. No host directory is ever searched, so a soname the bundle
//! lacks and the resolver did not choose fails by name instead of being
//! satisfied by whatever the host has.
//!
//! A launch that activates only part of the package's dependency sets
//! records its decision under the canonical identity of the pins it
//! activated, so two selections never serve each other's decisions while
//! lists that differ only in order share one.

use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque};
use std::ffi::OsString;
use std::fs;
use std::io::{self, Write};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};

use crate::drivers::{self, DRIVER_FAMILIES};
use crate::footer::HostLibsPolicy;
use crate::ldso::{self, MAX_LOADER_BYTES};
use crate::plugindirs::PluginDir;
use crate::verdef::{self, Choice, ElfFacts, LibcFamily, VersionSet};

/// What a launch takes from the host.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Resolution {
    /// Directory of symlinks to the chosen host libraries, when any were
    /// chosen.
    pub farm: Option<PathBuf>,
    /// The closed copy of the host loader to run the entrypoint under,
    /// when the host's libc won. The bundled loader is used otherwise.
    pub host_interp: Option<HostLoader>,
    /// Sonames whose two copies could not be ordered. The bundled copy is
    /// used; these are reported so the publisher can see them.
    pub incomparable: Vec<String>,
    /// The host modules the counterpart directories hold that the
    /// decision admitted, as their host paths, sorted. A launch is
    /// meant to present them beside the bundled members of their plugin
    /// directory; their host dependencies come through the farm.
    pub admitted: Vec<PathBuf>,
    /// The merged view of each plugin directory with admitted modules,
    /// sorted by the directory's package path. A view links the bundled
    /// members first and the admitted host members whose names the
    /// package does not carry after them, so wherever the directory is
    /// redirected the launch presents that order. Built only by
    /// [`resolve_selected`]; a view that cannot be built is left out and
    /// its directory keeps the bundled members alone.
    pub plugin_views: Vec<PluginView>,
}

/// One plugin directory's merged view, built in the decision's store.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginView {
    /// The plugin directory's package path, relative to the package root.
    pub relative: String,
    /// The view directory. Every entry is a symlink: one per bundled
    /// member, aimed at the package's copy, then one per admitted host
    /// member whose name no bundled member holds, aimed at the host's.
    pub dir: PathBuf,
}

/// The private copy of the host loader a launch runs, closed to the
/// loader's default search directories, and the digest it was written
/// with.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HostLoader {
    /// The copy, under the uid's cache root.
    pub path: PathBuf,
    /// BLAKE3 of the copy's bytes.
    pub hash: blake3::Hash,
}

impl HostLoader {
    /// Check the copy against its digest. A recorded decision is served
    /// and a kept copy reused only after this passes, so a copy damaged or
    /// replaced since it was written is never handed to a launch.
    pub fn verify(&self) -> io::Result<()> {
        let image = read_loader(&self.path)?;
        if blake3::hash(&image) == self.hash {
            Ok(())
        } else {
            Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "the copy does not match the digest it was written with",
            ))
        }
    }
}

/// A host loader the resolver chose, closed in memory: where it came
/// from, where its copy goes, and the closed image with its digest. The
/// image is the one the copy is written from, so the host's loader is
/// read once per decision.
#[derive(Clone, PartialEq, Eq)]
pub struct ClosedLoader {
    /// The host's loader.
    pub source: PathBuf,
    /// Where the closed copy is kept.
    pub copy: PathBuf,
    /// BLAKE3 of the closed image.
    pub hash: blake3::Hash,
    /// The closed image.
    pub image: Vec<u8>,
}

impl std::fmt::Debug for ClosedLoader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClosedLoader")
            .field("source", &self.source)
            .field("copy", &self.copy)
            .field("hash", &self.hash)
            .field("image_len", &self.image.len())
            .finish()
    }
}

/// One launch's inputs.
pub struct Request<'a> {
    /// The package root as the entrypoint will see it.
    pub pkg_root: &'a Path,
    /// Library directories relative to `pkg_root`.
    pub lib_dirs: &'a [&'a str],
    /// The plugin directories the package carries, from its
    /// `.onelf/plugin-dirs` record. Their host counterparts join the
    /// decision fingerprint, so installing or removing a host module
    /// recomputes the decision. A package without the record has none.
    pub plugin_dirs: &'a [PluginDir],
    pub policy: HostLibsPolicy,
    /// Where the link farm and the recorded decision live. Created on
    /// demand; reused across launches of the same package on the same
    /// host.
    pub store: &'a Path,
    /// The loader cache image describing the host.
    pub ld_cache: &'a Path,
    /// Directories of Vulkan ICD and EGL vendor files describing the
    /// host's drivers. [`ICD_DIRS`] outside of tests.
    pub icd_dirs: &'a [&'a str],
    /// A fetched GL build's extracted root, indexed ahead of the host:
    /// its [`GL_BUILD_LIB_DIRS`] and [`GL_BUILD_ICD_DIRS`] supply what
    /// the host could not.
    pub extra_root: Option<&'a Path>,
    /// Extracted roots of the dependency sets the package pins. Their
    /// libraries are the ones the bundle left out, so they are compared
    /// against the host exactly as a bundled copy would be.
    pub set_roots: &'a [PathBuf],
    /// Whether the launch can place host files over their bundled copies.
    /// Without that the host's glibc is never chosen: the bundled
    /// executables' run path would still find the bundled libc, and it
    /// cannot run under the host loader.
    pub host_libc: bool,
    /// Where a closed host loader's copy is kept, as the runtime judged
    /// it. Without a usable one the host's glibc is never chosen.
    pub loader_store: LoaderStore<'a>,
    /// The host's name service configuration. A host libc opens the
    /// modules it names by soname. [`nsswitch_file`] outside of tests.
    pub nsswitch: &'a Path,
    /// The user the launch runs as. A host module, and every directory
    /// above it, must belong to this user or to root and be writable by
    /// no one else, or another user could plant what the launch loads.
    pub uid: u32,
    /// Who owns the host modules, as [`judge_host_modules`] found it
    /// before the launch entered a user namespace, where every owner but
    /// the launching user reads as the same overflow id and nothing can
    /// be told apart. `None` judges on the spot, outside a namespace.
    pub trust: Option<&'a HostTrust>,
}

/// The ownership verdict on each host counterpart directory and module:
/// `None` when only root or the launching user could have put it there,
/// else why someone else could have.
pub type HostTrust = BTreeMap<PathBuf, Option<String>>;

/// Judge who could have planted each counterpart directory of
/// `plugin_dirs` and each module in it, for a launch that will decide
/// inside a user namespace. A directory past the module bound is judged
/// alone; [`decide`] refuses it whole anyway.
pub fn judge_host_modules(plugin_dirs: &[PluginDir], ld_cache: &Path, uid: u32) -> HostTrust {
    let mut out = HostTrust::new();
    for dir in counterpart_dirs(plugin_dirs, ld_cache) {
        let verdict = untrusted(&dir, uid);
        for module in modules_in(&dir).unwrap_or_default() {
            let module_verdict = verdict.clone().or_else(|| untrusted(&module, uid));
            out.insert(module, module_verdict);
        }
        out.insert(dir, verdict);
    }
    out
}

/// Where hosts keep their name service configuration, in the order the
/// host libc looks: an administrator's file, then the distribution
/// default openSUSE ships under `/usr/etc`.
const NSSWITCH_FILES: &[&str] = &["/etc/nsswitch.conf", "/usr/etc/nsswitch.conf"];

/// The host's name service configuration file, the first of
/// [`NSSWITCH_FILES`] that exists.
pub fn nsswitch_file() -> &'static Path {
    NSSWITCH_FILES
        .iter()
        .map(Path::new)
        .find(|path| path.exists())
        .unwrap_or(Path::new(NSSWITCH_FILES[0]))
}

/// Where closed host loader copies go, or why none can be kept.
///
/// A copy is executed, so it has to live on a filesystem that allows it.
/// The runtime checks that before it builds a request, since it alone can
/// ask the kernel how a filesystem is mounted.
#[derive(Debug, Clone, Copy)]
pub enum LoaderStore<'a> {
    /// The uid's cache root. Copies go under its `loaders/`, content
    /// addressed, so one copy serves every package and decision.
    Root {
        /// The cache root.
        root: &'a Path,
        /// The uid that has to own every directory on the way.
        uid: u32,
    },
    /// No copy can be kept, and why.
    Refused(&'a str),
}

/// Library directories of a GL build, relative to its root.
pub const GL_BUILD_LIB_DIRS: &[&str] = &["lib", "lib64"];

/// Driver description directories of a GL build, relative to its root.
pub const GL_BUILD_ICD_DIRS: &[&str] = &["share/vulkan/icd.d", "share/glvnd/egl_vendor.d"];

/// Libraries a GL build carries that nothing names in `DT_NEEDED`: the
/// glvnd vendor libraries, Mesa's gallium and DRI megadrivers, Vulkan
/// ICDs and layers are all opened by name at runtime. They seed the
/// closure walk so they and their dependencies reach the farm, since
/// the build's directories are never put on the search path themselves.
const GL_BUILD_SEEDS: &[&str] = &[
    "libGLX_",
    "libEGL_",
    "libgallium",
    "libdril",
    "libvulkan_",
    "libVkLayer_",
];

/// Host libraries that glvnd opens by a name built from the X server's
/// vendor string, so neither `DT_NEEDED` nor any driver description file
/// names them. They seed the host's closure walk like the driver
/// families, or a package reaching GL through GLX cannot load the
/// vendor the host's display names.
const HOST_SEEDS: &[&str] = &["libGLX_"];

/// Driver-family names that do not start the host's closure walk.
/// nvidia-settings' interface libraries ship with the driver and only
/// nvidia-settings loads them, so a packed nvidia-settings loses its GTK
/// interface. Otherwise their GTK closure would put libdbus, libsystemd
/// and the like up for a host-or-bundle choice in every package.
const NOT_DRIVER_SEEDS: &[&str] = &["libnvidia-gtk"];

/// Where a launch gets its GL stack from, if anywhere.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Gl {
    Bundled,
    Host,
    /// The host lacks an entry point the bundle links, so the bundle
    /// fails without one.
    Missing,
    /// Nothing links an entry point and the host has none. A library may
    /// still open one by name, as libepoxy and SDL do, but only when the
    /// application draws with GL.
    Absent,
}

impl Gl {
    /// Whether neither the bundle nor the host has a GL stack, so a
    /// pinned build is worth fetching.
    pub fn lacking(self) -> bool {
        matches!(self, Gl::Missing | Gl::Absent)
    }
}

/// The entry points of a GL or Vulkan stack. One of them present, on
/// either side, is a stack the application can load.
const GL_ENTRY: &[&str] = &["libGL.so", "libEGL.so", "libGLESv2.so", "libvulkan.so"];

fn has_gl_entry(libs: &BTreeMap<String, PathBuf>) -> bool {
    libs.keys()
        .any(|name| GL_ENTRY.iter().any(|p| name.starts_with(p)))
}

/// The GL entry points `carried` objects and `executable` name and
/// `carried` does not itself hold. This is what the launch will ask the
/// host, or a pinned build, to supply.
fn gl_entries_wanted(
    carried: &BTreeMap<String, PathBuf>,
    executable: Option<&Path>,
) -> BTreeSet<String> {
    let mut wanted = BTreeSet::new();
    for path in carried.values().map(PathBuf::as_path).chain(executable) {
        for needed in verdef::read_needed(path).unwrap_or_default() {
            if GL_ENTRY.iter().any(|p| needed.starts_with(p)) && !carried.contains_key(&needed) {
                wanted.insert(needed);
            }
        }
    }
    wanted
}

/// Whether the bundle or the host carries a GL stack. Decided before a
/// pinned build is fetched, so the host is indexed without one.
///
/// `extra_dirs` are library directories beside the package, which is
/// where a shared dependency set puts a toolkit, and a toolkit is
/// usually what names a GL entry point. `executable` is the binary the
/// launch runs, which links GL itself as often as its libraries do.
///
/// A host counts only when it has every entry point the package asks
/// for. Some hosts have part of a stack and not the rest: an image with
/// `libGL.so.1` and no `libEGL.so.1` is one an X server pulls in, and
/// treating that as a stack leaves the application to fail on the
/// missing half.
pub fn gl_situation(
    pkg_root: &Path,
    lib_dirs: &[&str],
    extra_dirs: &[PathBuf],
    executable: Option<&Path>,
    ld_cache: &Path,
    icd_dirs: &[&str],
) -> Gl {
    let mut carried = bundled_libs(pkg_root, lib_dirs);
    for dir in extra_dirs {
        for (name, path) in libs_in(dir) {
            carried.entry(name).or_insert(path);
        }
    }
    if has_gl_entry(&carried) {
        return Gl::Bundled;
    }
    let host = HostIndex::load(ld_cache, icd_dirs, None).libs;
    let wanted = gl_entries_wanted(&carried, executable);
    // Nothing names one outright, so a plugin may still ask for one by
    // dlopen and any entry point is worth having.
    if wanted.is_empty() {
        return if has_gl_entry(&host) {
            Gl::Host
        } else {
            Gl::Absent
        };
    }
    if wanted.iter().all(|name| host.contains_key(name)) {
        Gl::Host
    } else {
        Gl::Missing
    }
}

const LIBC: &str = "libc.so.6";

/// Everything that must come from the same build as the loader, matched
/// as soname prefixes. The loader itself is recognized by
/// [`ldso::is_glibc_loader_name`].
const LIBC_FAMILY: &[&str] = &[
    "libc.so",
    "libm.so",
    "libpthread.so",
    "libdl.so",
    "librt.so",
    "libresolv.so",
    "libutil.so",
    "libanl.so",
    "libnss_",
    "libBrokenLocale.so",
    "libmvec.so",
    "libc_malloc_debug.so",
    "libthread_db.so",
];

/// Where the host records the graphics drivers it loads by absolute path.
/// Those never appear as `DT_NEEDED`, so the walk has to be seeded from
/// here as well as from the driver families.
pub const ICD_DIRS: &[&str] = &[
    "/run/opengl-driver/share/vulkan/icd.d",
    "/etc/vulkan/icd.d",
    "/usr/share/vulkan/icd.d",
    "/run/opengl-driver/share/glvnd/egl_vendor.d",
    "/etc/glvnd/egl_vendor.d",
    "/usr/share/glvnd/egl_vendor.d",
];

/// Whether `soname` belongs to the glibc family, which a package always
/// carries as a unit with its loader.
pub fn is_libc_family(soname: &str) -> bool {
    LIBC_FAMILY.iter().any(|p| soname.starts_with(p)) || ldso::is_glibc_loader_name(soname)
}

fn is_driver_family(soname: &str) -> bool {
    DRIVER_FAMILIES.iter().any(|p| soname.starts_with(p))
}

/// Decide what this launch takes from the host, for a launch whose
/// active dependency sets are `pins`, empty for a launch that activates
/// none.
///
/// The decision is read and written under the launch's
/// [`SelectionIdentity`] as well as the host: a changed subset or pin
/// misses and is recomputed against only its own library universe, an
/// empty selection never reads a toolkit selection's record, and an
/// equivalent list order hits and is served as it stands. A list outside
/// the identity's bounds decides afresh every launch rather than being
/// cached under a guessed key. A host change, a winner swapped in place
/// and a vanished winner each void a record, as does a record left by a
/// release that keyed decisions on the host alone.
pub fn resolve_selected(req: &Request, pins: &[ActivePin]) -> Resolution {
    if req.policy == HostLibsPolicy::Never {
        return Resolution::default();
    }
    let host = HostIndex::load(req.ld_cache, req.icd_dirs, req.extra_root);
    if host.libs.is_empty() {
        return Resolution::default();
    }
    let fingerprint = fingerprint(
        req.ld_cache,
        req.plugin_dirs,
        req.icd_dirs,
        req.policy,
        req.extra_root,
        req.host_libc,
        req.nsswitch,
        req.loader_store,
    );
    let Some(identity) = SelectionIdentity::new(pins) else {
        return with_plugin_views(fresh(req, &fingerprint, &host).1, req, &fingerprint);
    };
    let Some(dir) = selection_dir(identity.key()).map(|name| req.store.join(name)) else {
        return with_plugin_views(fresh(req, &fingerprint, &host).1, req, &fingerprint);
    };
    if let Some(recorded) =
        load_selection(&dir, &identity, &fingerprint, req.store, req.loader_store)
    {
        return with_plugin_views(recorded, req, &fingerprint);
    }
    let (decision, resolution, recordable) = fresh(req, &fingerprint, &host);
    if recordable
        && let Err(e) = record_selection(
            &dir,
            &identity,
            &fingerprint,
            &resolution,
            &decision.winners,
        )
    {
        eprintln!(
            "onelf-rt: resolver: cannot record {}: {e}",
            req.store.display()
        );
    }
    with_plugin_views(resolution, req, &fingerprint)
}

/// A fresh decision for `req` with its farm and loader copy built, and
/// whether it may be recorded.
///
/// When the closed loader copy cannot be kept after all, a race or a full
/// disk past what the runtime could judge up front, the request is
/// decided again with the store refused. The bundled pair runs and the
/// rest of the decision stands, the driver closure with it. That fallback
/// is not recorded, so a later launch tries the copy again. A resolution
/// whose farm could not be built is likewise not recorded, so a later
/// launch tries the farm again.
fn fresh(req: &Request, fingerprint: &str, host: &HostIndex) -> (Decision, Resolution, bool) {
    let decision = decide_on(req, host);
    let Some(loader) = &decision.host_interp else {
        let (resolution, farm_built) = materialized(req, fingerprint, &decision);
        return (decision, resolution, farm_built);
    };
    match keep_loader_copy(loader, req.loader_store) {
        Ok(()) => {
            let (resolution, farm_built) = materialized(req, fingerprint, &decision);
            (decision, resolution, farm_built)
        }
        Err(why) => {
            eprintln!(
                "onelf-rt: resolver: the host loader {} is not used, {why}; the bundled loader \
                 and libc run",
                loader.source.display()
            );
            let refused = Request {
                loader_store: LoaderStore::Refused("the loader copy cannot be kept"),
                ..*req
            };
            let decision = decide_on(&refused, host);
            let (resolution, _) = materialized(&refused, fingerprint, &decision);
            (decision, resolution, false)
        }
    }
}

/// The resolution of a decision, with its farm built, and whether the
/// farm was built. A farm that cannot be written leaves nothing taken
/// from the host, and the caller leaves the record alone so a later
/// launch tries again.
fn materialized(req: &Request, fingerprint: &str, decision: &Decision) -> (Resolution, bool) {
    let copy = decision.host_interp.as_ref().map(|l| l.copy.as_path());
    let farm = match materialize(req.store, fingerprint, &decision.winners, copy) {
        Ok(farm) => farm,
        Err(e) => {
            eprintln!(
                "onelf-rt: resolver: cannot write {}: {e}",
                req.store.display()
            );
            return (Resolution::default(), false);
        }
    };
    let resolution = Resolution {
        farm,
        host_interp: decision.host_interp.as_ref().map(|loader| HostLoader {
            path: loader.copy.clone(),
            hash: loader.hash,
        }),
        incomparable: decision.incomparable.clone(),
        admitted: decision.admitted.clone(),
        plugin_views: Vec::new(),
    };
    (resolution, true)
}

/// A host module the resolver refused, with the first object of its
/// closure that failed a check and why it failed.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ModuleRefusal {
    /// The module refused.
    pub module: PathBuf,
    /// The first object of the module's closure that failed a check,
    /// the module itself when its own facts fail.
    pub object: PathBuf,
    /// Why that object failed.
    pub reason: String,
}

/// What the resolver decides for a request, worked out afresh and with
/// nothing written.
#[derive(Debug, Default)]
pub struct Decision {
    /// Every soname weighed, in order, with the copy that won: `None` for
    /// one the bundle lacks, which only the host can supply.
    pub compared: Vec<(String, Option<Choice>)>,
    /// The host libraries chosen, by soname. A loader soname names the
    /// interpreter the pair runs, not the cache's entry for it.
    pub winners: BTreeMap<String, PathBuf>,
    /// The host loader the pair runs under, when the host's libc won and
    /// its loader could be closed.
    pub host_interp: Option<ClosedLoader>,
    /// The host loader of a winning host libc that could not be closed,
    /// and why. The bundled pair runs instead.
    pub host_interp_refused: Option<(PathBuf, String)>,
    pub incomparable: Vec<String>,
    /// The host modules the counterpart directories hold that the
    /// decision admitted, sorted by path. Their host dependencies are
    /// among the winners.
    pub admitted: Vec<PathBuf>,
    /// The host modules the decision refused, sorted by module path.
    pub refused: Vec<ModuleRefusal>,
    /// Where in the store the link farm for these winners lives.
    pub farm: Option<PathBuf>,
}

/// [`resolve_selected`] without the store: neither the recorded decision
/// is read nor a farm or record written. For reporting what a launch would do.
pub fn decide(req: &Request) -> Decision {
    if req.policy == HostLibsPolicy::Never {
        return Decision::default();
    }
    let host = HostIndex::load(req.ld_cache, req.icd_dirs, req.extra_root);
    if host.libs.is_empty() {
        // No glibc library to weigh anything against, as on a musl host:
        // every counterpart module is refused, and named.
        let mut decision = Decision::default();
        for dir in counterpart_dirs(req.plugin_dirs, req.ld_cache) {
            for module in modules_in(&dir).unwrap_or_default() {
                decision.refused.push(ModuleRefusal {
                    object: module.clone(),
                    module,
                    reason: "the host offers no glibc libraries to load it against".to_string(),
                });
            }
        }
        decision.refused.sort();
        return decision;
    }
    let mut decision = decide_on(req, &host);
    if !decision.winners.is_empty() {
        let fingerprint = fingerprint(
            req.ld_cache,
            req.plugin_dirs,
            req.icd_dirs,
            req.policy,
            req.extra_root,
            req.host_libc,
            req.nsswitch,
            req.loader_store,
        );
        let copy = decision.host_interp.as_ref().map(|l| l.copy.as_path());
        decision.farm = Some(farm_dir(req.store, &fingerprint, &decision.winners, copy));
    }
    decision
}

fn decide_on(req: &Request, host: &HostIndex) -> Decision {
    let mut bundled = bundled_libs(req.pkg_root, req.lib_dirs);
    // A name the bundle carries itself wins: the set supplies what was
    // left out, not a second copy of what was kept.
    for root in req.set_roots {
        for (name, path) in bundled_libs(root, GL_BUILD_LIB_DIRS) {
            bundled.entry(name).or_insert(path);
        }
    }
    let mut decision = Decision::default();

    if let (true, Some(bundled_libc), Some(host_libc)) =
        (req.host_libc, bundled.get(LIBC), host.libs.get(LIBC))
    {
        let choice = verdef::compare(&versions(bundled_libc), &versions(host_libc));
        decision.compared.push((LIBC.to_string(), Some(choice)));
        match choice {
            Choice::Host => {
                if let Some(interp) = host.loader_for(host_libc) {
                    let closed = match req.loader_store {
                        LoaderStore::Root { root, .. } => close_loader(&interp).map(|image| {
                            let hash = blake3::hash(&image);
                            (loader_copy(root, &hash, &loader_name(&interp)), hash, image)
                        }),
                        LoaderStore::Refused(why) => Err(why.to_string()),
                    };
                    match closed {
                        Ok((copy, hash, image)) => {
                            for name in bundled.keys().filter(|n| is_libc_family(n)) {
                                let path = if ldso::is_glibc_loader_name(name) {
                                    Some(interp.clone())
                                } else {
                                    host.libs.get(name).cloned()
                                };
                                if let Some(path) = path {
                                    decision.winners.insert(name.clone(), path);
                                }
                            }
                            decision.host_interp = Some(ClosedLoader {
                                source: interp,
                                copy,
                                hash,
                                image,
                            });
                        }
                        Err(why) => decision.host_interp_refused = Some((interp, why)),
                    }
                }
            }
            Choice::Incomparable => decision.incomparable.push(LIBC.to_string()),
            Choice::Bundle => {}
        }
    }

    // What the host is expected to supply is chosen first, so the
    // counterpart modules below weigh themselves against the copies the
    // decision actually installs. A member of the driver closure the
    // bundle also carries is compared; one it does not carry can only
    // come from the host. Under `always` every bundled soname is
    // compared as well.
    let mut candidates: Vec<String> = host.driver_closure().into_iter().collect();
    if decision.host_interp.is_some() {
        candidates.extend(host.closure(host_libc_opens(req.nsswitch)));
    }
    if req.policy == HostLibsPolicy::Always {
        candidates.extend(bundled.keys().cloned());
    }
    candidates.sort();
    candidates.dedup();
    let mut chosen: BTreeSet<String> = BTreeSet::new();
    let mut choose = |decision: &mut Decision, name: &str| {
        if !chosen.insert(name.to_string()) {
            return;
        }
        if is_libc_family(name) {
            // A closed host loader finds nothing on its own, so a member
            // of its family that the host libc or a host driver opens and
            // the bundle lacks has to be chosen like any other library.
            // A glibc loader soname instead names the interpreter the
            // pair runs, whatever the cache's entry carries: the farm
            // links the closed copy under the demanded name.
            if let Some(interp) = decision.host_interp.as_ref() {
                if ldso::is_glibc_loader_name(name) {
                    decision
                        .winners
                        .entry(name.to_string())
                        .or_insert(interp.source.clone());
                } else if !bundled.contains_key(name)
                    && let Some(host_path) = host.libs.get(name)
                {
                    decision.compared.push((name.to_string(), None));
                    decision.winners.insert(name.to_string(), host_path.clone());
                }
            }
            return;
        }
        let Some(host_path) = host.libs.get(name) else {
            return;
        };
        let choice = bundled
            .get(name)
            .map(|bundled_path| verdef::compare(&versions(bundled_path), &versions(host_path)));
        decision.compared.push((name.to_string(), choice));
        match choice.unwrap_or(Choice::Host) {
            Choice::Host => {
                decision.winners.insert(name.to_string(), host_path.clone());
            }
            Choice::Incomparable => decision.incomparable.push(name.to_string()),
            Choice::Bundle => {}
        }
    };
    for name in &candidates {
        choose(&mut decision, name);
    }

    // The package's counterpart modules lean on the copies the launch
    // would load, winners included; the refusals ride the decision for
    // the doctor to report. The sonames the admitted closures reached
    // are then chosen like any other, so they and their needs reach the
    // farm.
    let counterpart = counterpart_dirs(req.plugin_dirs, req.ld_cache);
    if !counterpart.is_empty() {
        let loader = decision.host_interp.as_ref().map(|l| l.source.as_path());
        let (admitted, refused, reached) = admit_modules(
            host,
            &bundled,
            &decision.winners,
            &counterpart,
            bundled.get(LIBC).map(|p| p.as_path()),
            loader,
            req.uid,
            req.trust,
        );
        decision.admitted = admitted;
        decision.refused = refused;
        for name in &reached {
            choose(&mut decision, name);
        }
    }
    decision
}

/// One active dependency set of a launch: the set's canonical
/// declaration name and the full hash its build is pinned by.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ActivePin {
    /// The declaration name, as the package's set table names it.
    pub label: String,
    /// The pinned build's BLAKE3 digest in hex.
    pub hash: String,
}

/// The canonical identity of a launch's active pin set.
///
/// Built by sorting the pins by declaration name and hash, collapsing
/// exact duplicates and normalizing every hash to lowercase, so lists
/// that differ only in order or in hash case give one identity and any
/// difference in membership, name or pin gives two. The identity is
/// bounded and total: a name over the label bound, a name or hash
/// carrying a control character, a hash that is not one digest, or more
/// than the pin-count bound refuses to build, and a launch it cannot
/// identify is decided afresh rather than cached under a guessed key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SelectionIdentity {
    pins: Vec<ActivePin>,
    canonical: String,
    key: String,
}

impl SelectionIdentity {
    /// The identity of `pins`, or `None` when a pin is outside the
    /// bounds.
    pub fn new(pins: &[ActivePin]) -> Option<Self> {
        if pins.len() > MAX_ACTIVE_PINS {
            return None;
        }
        let mut pins: Vec<ActivePin> = pins.to_vec();
        for pin in &mut pins {
            pin.hash = pin.hash.to_ascii_lowercase();
        }
        pins.sort();
        pins.dedup();
        let mut canonical = String::new();
        for pin in &pins {
            pin_in_bounds(pin)?;
            if !canonical.is_empty() {
                canonical.push('\n');
            }
            canonical.push_str(&pin_line(pin));
        }
        let digest = blake3::hash(canonical.as_bytes());
        let key = hex_prefix(&digest.as_bytes()[..SELECTION_KEY_BYTES]);
        Some(Self {
            pins,
            canonical,
            key,
        })
    }

    /// The pins in their canonical order.
    pub fn pins(&self) -> &[ActivePin] {
        &self.pins
    }

    /// The canonical text the identity is: each pin's record line, in
    /// canonical order, newline separated, and empty for an empty
    /// selection. This is the exact text a record stores and the key
    /// digests.
    pub fn canonical(&self) -> &str {
        &self.canonical
    }

    /// The bounded store name the identity is keyed by: lowercase hex
    /// over a digest of [`Self::canonical`]. Equal identities share it.
    /// A record still names the full canonical pins and a reader
    /// compares them whole, so the key only has to find the record,
    /// never to vouch for it.
    pub fn key(&self) -> &str {
        &self.key
    }

    /// The record lines the canonical text is made of.
    fn identity_lines(&self) -> Vec<&str> {
        self.canonical.lines().collect()
    }
}

/// Whether `pin` is inside the bounds a record can carry: a nonempty
/// name of at most the label bound, with no control character, and
/// exactly one BLAKE3 digest in lowercase hex.
fn pin_in_bounds(pin: &ActivePin) -> Option<()> {
    let label = pin.label.as_bytes();
    if label.is_empty() || label.len() > MAX_PIN_LABEL_BYTES {
        return None;
    }
    if label.iter().any(|b| *b < 0x20 || *b == 0x7f) {
        return None;
    }
    if pin.hash.len() != 64
        || !pin
            .hash
            .bytes()
            .all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
    {
        return None;
    }
    Some(())
}

/// One pin's canonical record line, `pin <name length>:<name>\t<hash>`.
/// The length prefix and the admitted names make the line injective, so
/// two pins never render one line and a record can be compared as whole
/// lines.
fn pin_line(pin: &ActivePin) -> String {
    format!("pin {}:{}\t{}", pin.label.len(), pin.label, pin.hash)
}

/// Lowercase hex of `bytes`.
fn hex_prefix(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

/// The store subdirectory for one selection identity key, `sel-<key>`,
/// or `None` when `key` is not a key [`SelectionIdentity`] mints: the
/// name is always one bounded lowercase hex component, so nothing a
/// caller passes can name anything outside the store.
pub fn selection_dir(key: &str) -> Option<String> {
    let hex = !key.is_empty()
        && key.len() <= SELECTION_KEY_BYTES * 2
        && key.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'));
    hex.then(|| format!("{SELECTION_DIR_PREFIX}{key}"))
}

/// A library that cannot be read is treated as defining nothing: an
/// unreadable host library loses to the bundled copy, and an unreadable
/// bundled library loses to the host copy.
fn versions(path: &Path) -> VersionSet {
    verdef::read(path).unwrap_or_default()
}

/// The host's libraries by soname, from its loader cache and the drivers
/// its ICD files name by path.
struct HostIndex {
    libs: BTreeMap<String, PathBuf>,
    /// Drivers reached through ICD files rather than the cache, and a GL
    /// build's dlopen-only libraries. They seed the closure walk alongside
    /// the driver families.
    icd_libs: Vec<PathBuf>,
    /// What the host's driver modules need. Mesa and libva open those
    /// modules by absolute path, so the modules load without the farm but
    /// their dependencies are looked up by soname and must be in it.
    module_needs: BTreeSet<String>,
}

impl HostIndex {
    /// The extra root goes in first, so a soname it carries shadows the
    /// host's copy and its drivers seed the closure walk.
    fn load(ld_cache: &Path, icd_dirs: &[&str], extra_root: Option<&Path>) -> Self {
        let mut libs = BTreeMap::new();
        let mut icd_libs = Vec::new();
        if let Some(root) = extra_root {
            libs.extend(bundled_libs(root, GL_BUILD_LIB_DIRS));
            icd_libs.extend(
                libs.iter()
                    .filter(|(name, _)| GL_BUILD_SEEDS.iter().any(|p| name.starts_with(p)))
                    .map(|(_, path)| path.clone()),
            );
            let dirs: Vec<String> = GL_BUILD_ICD_DIRS
                .iter()
                .map(|d| root.join(d).to_string_lossy().into_owned())
                .collect();
            let dirs: Vec<&str> = dirs.iter().map(String::as_str).collect();
            // The build's ICD files name their drivers by bare soname, so
            // they relocate; those resolve against the build's own
            // directories rather than a cache.
            icd_libs.extend(icd_library_paths_in(&dirs, Some(&libs)));
        }
        for (soname, path) in drivers::cache_paths_in(ld_cache) {
            libs.entry(soname).or_insert_with(|| PathBuf::from(path));
        }
        // After the cache, so an ICD file naming its driver by bare soname
        // (NVIDIA's `libGLX_nvidia.so.0`, which no driver family names)
        // resolves through it and seeds the walk like one named by path.
        icd_libs.extend(icd_library_paths_in(icd_dirs, Some(&libs)));
        for path in &icd_libs {
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                libs.entry(name.to_string()).or_insert_with(|| path.clone());
            }
        }
        let mut module_needs = BTreeSet::new();
        for dir in module_dirs(ld_cache) {
            let Ok(entries) = fs::read_dir(&dir) else {
                continue;
            };
            for entry in entries.flatten() {
                let path = entry.path();
                if path.extension().is_some_and(|e| e == "so") {
                    module_needs.extend(verdef::read_needed(&path).unwrap_or_default());
                }
            }
        }
        HostIndex {
            libs,
            icd_libs,
            module_needs,
        }
    }

    /// The interpreter the host pair runs: its libc's `PT_INTERP` when
    /// it names a path that exists here, else the cache's own loader
    /// soname. An existing path comes back whatever it is, so closing it
    /// records why it cannot run instead of silently falling back to the
    /// bundled pair.
    fn loader_for(&self, host_libc: &Path) -> Option<PathBuf> {
        if let Some(interp) = crate::elf::read_interp(host_libc) {
            let interp = PathBuf::from(interp);
            if interp.exists() {
                return Some(interp);
            }
        }
        self.libs
            .iter()
            .find(|(name, path)| ldso::is_glibc_loader_name(name) && path.is_file())
            .map(|(_, path)| path.clone())
    }

    /// Every soname reachable through `DT_NEEDED` from the driver
    /// families, the host's dlopen-only seeds, the ICD-named drivers and
    /// what the host's driver modules need.
    fn driver_closure(&self) -> HashSet<String> {
        let mut queue: VecDeque<String> = self
            .libs
            .keys()
            .filter(|name| is_driver_family(name) || HOST_SEEDS.iter().any(|p| name.starts_with(p)))
            .filter(|name| !NOT_DRIVER_SEEDS.iter().any(|p| name.starts_with(p)))
            .cloned()
            .collect();
        for path in &self.icd_libs {
            if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                queue.push_back(name.to_string());
            }
        }
        queue.extend(self.module_needs.iter().cloned());
        self.closure(queue)
    }

    /// Every soname reachable through `DT_NEEDED` from `seeds`, the seeds
    /// included.
    fn closure(&self, seeds: impl IntoIterator<Item = String>) -> HashSet<String> {
        let mut seen: HashSet<String> = HashSet::new();
        let mut queue: VecDeque<String> = seeds.into_iter().collect();
        while let Some(name) = queue.pop_front() {
            if !seen.insert(name.clone()) {
                continue;
            }
            let Some(path) = self.libs.get(&name) else {
                continue;
            };
            for needed in verdef::read_needed(path).unwrap_or_default() {
                if !seen.contains(&needed) {
                    queue.push_back(needed);
                }
            }
        }
        seen
    }
}

/// The largest name service configuration read. A real one is a few
/// hundred bytes.
const NSSWITCH_MAX_BYTES: u64 = 64 * 1024;

/// The most lines of the name service configuration looked at. A real one
/// is a few dozen.
const NSSWITCH_MAX_LINES: usize = 1024;

/// The longest service name taken from the name service configuration.
const NSS_SERVICE_MAX_LEN: usize = 64;

/// What a host libc opens by name rather than through `DT_NEEDED`: the
/// module of every service the name service configuration at `nsswitch`
/// names, and `libgcc_s`, which it opens to unwind a thread on cancellation.
/// A configuration that cannot be read, or is larger than any real one,
/// names no service.
fn host_libc_opens(nsswitch: &Path) -> Vec<String> {
    let mut opens: BTreeSet<String> = BTreeSet::new();
    opens.insert("libgcc_s.so.1".to_string());
    let text = read_nsswitch(nsswitch).unwrap_or_default();
    for line in text.lines().take(NSSWITCH_MAX_LINES) {
        let line = line.split('#').next().unwrap_or_default();
        let Some((_, sources)) = line.split_once(':') else {
            continue;
        };
        let mut in_action = false;
        for word in sources.split_whitespace() {
            if word.starts_with('[') {
                in_action = true;
            }
            let service = !in_action
                && word.len() <= NSS_SERVICE_MAX_LEN
                && word
                    .bytes()
                    .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'_');
            if service && !word.is_empty() {
                opens.insert(format!("libnss_{word}.so.2"));
            }
            if word.ends_with(']') {
                in_action = false;
            }
        }
    }
    opens.into_iter().collect()
}

/// The name service configuration's text, when it is a regular file no
/// larger than [`NSSWITCH_MAX_BYTES`] holding UTF-8.
fn read_nsswitch(path: &Path) -> Option<String> {
    use std::io::Read;
    let file = fs::File::open(path).ok()?;
    let md = file.metadata().ok()?;
    if !md.is_file() || md.len() > NSSWITCH_MAX_BYTES {
        return None;
    }
    let mut text = String::new();
    file.take(NSSWITCH_MAX_BYTES)
        .read_to_string(&mut text)
        .ok()?;
    Some(text)
}

/// The `dri` directories beside the host's library directories, where
/// Mesa keeps its DRI drivers and libva its VA-API drivers. Each sits next
/// to the library directory Mesa was installed into, so the loader cache
/// finds them on any distribution layout.
fn module_dirs(ld_cache: &Path) -> Vec<PathBuf> {
    drivers::cache_dirs_in(ld_cache)
        .into_iter()
        .map(|dir| Path::new(&dir).join("dri"))
        .filter(|dir| dir.is_dir())
        .collect()
}

/// The host counterpart directories of the package's carried plugin
/// directories: each compiled-in absolute path, and the directory's
/// path under every host library directory [`counterpart_lib_dirs`]
/// names, which covers a multiarch host where `/usr/lib/alsa-lib` has
/// `/usr/lib/x86_64-linux-gnu/alsa-lib` beside it. Only those that
/// exist are returned, like the driver directories: a directory that
/// is not there holds no modules for the decision to weigh, and its
/// absence is the host's business alone.
fn counterpart_dirs(plugin_dirs: &[PluginDir], ld_cache: &Path) -> Vec<PathBuf> {
    if plugin_dirs.is_empty() {
        return Vec::new();
    }
    let libs = counterpart_lib_dirs(ld_cache);
    let out: BTreeSet<PathBuf> = plugin_dirs
        .iter()
        .flat_map(|dir| counterpart_spellings(dir, &libs))
        .collect();
    // Two spellings of one directory, `/usr/lib/x` and `/lib/x` on a
    // merged host say, hold the same modules: the first in sorted order
    // stands for both, so each module is weighed and named once.
    let mut seen: BTreeSet<(u64, u64)> = BTreeSet::new();
    out.into_iter()
        .filter(|dir| {
            fs::metadata(dir).is_ok_and(|md| md.is_dir() && seen.insert((md.dev(), md.ino())))
        })
        .collect()
}

/// The library directories a host without a loader cache searches, as
/// musl's loader does by default.
#[cfg(not(test))]
const DEFAULT_LIB_DIRS: &[&str] = &["/lib", "/usr/local/lib", "/usr/lib"];
/// None in the unit tests, whose synthetic hosts the real directories
/// would leak into.
#[cfg(test)]
const DEFAULT_LIB_DIRS: &[&str] = &[];

/// The host library directories a carried plugin directory's
/// counterparts sit under: the ones the loader cache at `ld_cache`
/// names, or, on a host with no cache, such as a musl one, the
/// default directories that exist.
fn counterpart_lib_dirs(ld_cache: &Path) -> Vec<String> {
    let dirs = drivers::cache_dirs_in(ld_cache);
    if !dirs.is_empty() {
        return dirs;
    }
    DEFAULT_LIB_DIRS
        .iter()
        .filter(|dir| Path::new(dir).is_dir())
        .map(|dir| dir.to_string())
        .collect()
}

/// The most modules one counterpart directory may hold. A directory
/// over it is refused whole: weighing it would cost every launch time
/// and memory the host could inflate at will.
pub const MAX_COUNTERPART_MODULES: usize = 4096;

/// The module files a directory holds: regular files whose name ends in
/// `.so`, sorted, or `None` when there are more than
/// [`MAX_COUNTERPART_MODULES`]. A directory that cannot be read holds
/// none.
fn modules_in(dir: &Path) -> Option<Vec<PathBuf>> {
    let Ok(entries) = fs::read_dir(dir) else {
        return Some(Vec::new());
    };
    let mut out: Vec<PathBuf> = Vec::new();
    for path in entries.flatten().map(|entry| entry.path()) {
        if path.extension().is_some_and(|e| e == "so")
            && fs::metadata(&path).is_ok_and(|md| md.is_file())
        {
            if out.len() == MAX_COUNTERPART_MODULES {
                return None;
            }
            out.push(path);
        }
    }
    out.sort();
    Some(out)
}

/// Why `path` could be replaced by someone other than root and `uid`, or
/// `None` when it cannot: the path resolved, it and every directory above
/// it belong to root or `uid`, and none of them is writable by its group
/// or by others unless it is sticky, as `/tmp` is, where no one may
/// replace an entry they do not own and every entry below is checked in
/// turn. A module there was planted by one of the two.
fn untrusted(path: &Path, uid: u32) -> Option<String> {
    let Ok(resolved) = fs::canonicalize(path) else {
        return Some(format!("{} cannot be resolved", path.display()));
    };
    for step in resolved.ancestors() {
        let Ok(md) = fs::symlink_metadata(step) else {
            return Some(format!("{} cannot be read", step.display()));
        };
        if md.uid() != 0 && md.uid() != uid {
            return Some(format!("{} belongs to another user", step.display()));
        }
        if md.mode() & 0o022 != 0 && (!md.is_dir() || md.mode() & 0o1000 == 0) {
            return Some(format!("{} can be written by another user", step.display()));
        }
    }
    None
}

/// The facts of one object, read once per decision. A file whose facts
/// cannot be read is remembered as unreadable.
fn facts_of(path: &Path, cache: &mut BTreeMap<PathBuf, Option<ElfFacts>>) -> Option<ElfFacts> {
    cache
        .entry(path.to_path_buf())
        .or_insert_with(|| verdef::read_facts(path).ok())
        .clone()
}

/// The versions the copy of `dependency` this launch would load
/// defines, or `None` when nothing supplies it.
///
/// The copy that loads is the one the decision chose: its winner, when
/// there is one, which for a glibc loader soname is the loader the
/// pair runs (the interpreter when the host's pair won, the bundle's
/// own loader copy under the bundled pair) rather than whatever entry
/// the cache carries; the host's family copy under its pair whenever
/// the host has one; the bundled copy otherwise. `loader` is the
/// interpreter the host's pair runs, when that pair won, and `None` is
/// the bundled pair. A copy whose facts cannot be read supplies
/// nothing: the launch would keep it or link it, and nothing could
/// vouch for its versions. Without a winner the bundle keeps a soname
/// it carries, and the bundled copy's versions are the conservative
/// measure, because a host copy only wins by defining a superset.
fn supplier_versions(
    dependency: &str,
    bundled: &BTreeMap<String, PathBuf>,
    host: &HostIndex,
    winners: &BTreeMap<String, PathBuf>,
    loader: Option<&Path>,
    facts: &mut BTreeMap<PathBuf, Option<ElfFacts>>,
) -> Option<VersionSet> {
    if ldso::is_glibc_loader_name(dependency) {
        let path = if loader.is_some() {
            loader
        } else {
            bundled.get(dependency).map(|p| p.as_path())
        };
        return path
            .and_then(|path| facts_of(path, facts))
            .filter(|f| f.mappable)
            .map(|f| f.defined_versions);
    }
    if let Some(path) = winners.get(dependency) {
        return facts_of(path, facts)
            .filter(|f| f.mappable)
            .map(|f| f.defined_versions);
    }
    if is_libc_family(dependency) {
        if loader.is_some()
            && let Some(path) = host.libs.get(dependency)
        {
            return facts_of(path, facts)
                .filter(|f| f.mappable)
                .map(|f| f.defined_versions);
        }
        return bundled
            .get(dependency)
            .and_then(|path| facts_of(path, facts))
            .filter(|f| f.mappable)
            .map(|f| f.defined_versions);
    }
    if !bundled.contains_key(dependency) {
        return host
            .libs
            .get(dependency)
            .and_then(|path| facts_of(path, facts))
            .filter(|f| f.mappable)
            .map(|f| f.defined_versions);
    }
    bundled
        .get(dependency)
        .and_then(|path| facts_of(path, facts))
        .filter(|f| f.mappable)
        .map(|f| f.defined_versions)
}

/// Admit or refuse each module the counterpart directories hold.
///
/// A module is admitted when every object of its closure through the
/// host's libraries has the bundled pair's class and machine, is of no
/// foreign libc family, carries no old-style search path the loader
/// searches ahead of the farm, names no filter object outside the
/// closure, demands only versions the copy that would load defines,
/// and needs only sonames something supplies: a decision
/// winner, a bundled copy, a glibc loader soname, which loads the
/// loader the pair runs when a host pair won, or a host copy, which is
/// then chosen like any other winner. Under the bundled pair (`loader`
/// `None`) the exception is a family member the bundle lacks, which
/// nothing can supply: the family is not farmed piecemeal. A refusal
/// names the first object that failed and why. The host sonames the
/// admitted closures reach come back, to join the decision's
/// candidates.
///
/// Without a bundled pair to check against there is no admission:
/// every module is refused. `loader` is the interpreter the host's
/// pair runs, when that pair won.
#[allow(clippy::too_many_arguments)]
fn admit_modules(
    host: &HostIndex,
    bundled: &BTreeMap<String, PathBuf>,
    winners: &BTreeMap<String, PathBuf>,
    dirs: &[PathBuf],
    reference: Option<&Path>,
    loader: Option<&Path>,
    uid: u32,
    trust: Option<&HostTrust>,
) -> (Vec<PathBuf>, Vec<ModuleRefusal>, BTreeSet<String>) {
    let untrusted = |path: &Path| match trust {
        Some(judged) => match judged.get(path) {
            Some(verdict) => verdict.clone(),
            None => Some(format!(
                "{} appeared after the launch judged its owners",
                path.display()
            )),
        },
        None => untrusted(path, uid),
    };
    let mut admitted: Vec<PathBuf> = Vec::new();
    let mut refused: Vec<ModuleRefusal> = Vec::new();
    let mut candidates: BTreeSet<String> = BTreeSet::new();
    let refuse_all = |refused: &mut Vec<ModuleRefusal>, modules: Vec<PathBuf>, reason: &str| {
        for module in modules {
            refused.push(ModuleRefusal {
                object: module.clone(),
                module,
                reason: reason.to_string(),
            });
        }
    };
    let pair = reference.and_then(|path| verdef::read_facts(path).ok());
    let mut facts: BTreeMap<PathBuf, Option<ElfFacts>> = BTreeMap::new();
    for dir in dirs {
        let Some(modules) = modules_in(dir) else {
            refused.push(ModuleRefusal {
                object: dir.clone(),
                module: dir.clone(),
                reason: format!("the directory holds over {MAX_COUNTERPART_MODULES} modules"),
            });
            continue;
        };
        let Some(pair) = &pair else {
            refuse_all(&mut refused, modules, "no bundled pair to check against");
            continue;
        };
        if let Some(why) = untrusted(dir) {
            refuse_all(&mut refused, modules, &why);
            continue;
        }
        for module in modules {
            if let Some(why) = untrusted(&module) {
                refused.push(ModuleRefusal {
                    object: module.clone(),
                    module,
                    reason: why,
                });
                continue;
            }
            match closure_verdict(&module, host, bundled, winners, pair, loader, &mut facts) {
                Ok(reached) => {
                    candidates.extend(reached);
                    admitted.push(module);
                }
                Err(refusal) => refused.push(refusal),
            }
        }
    }
    admitted.sort();
    refused.sort();
    (admitted, refused, candidates)
}

/// Whether `module`'s closure through the host's libraries passes the
/// checks: `Ok` with the host sonames the closure reaches, to join the
/// decision's candidates, or `Err` with the first failing object and
/// its reason. A soname the decision already chose a winner for loads
/// from that winner, the farm linking it over the bundle's own copy,
/// so its closure is walked here. A soname without a winner that the
/// bundle carries loads from the bundle, whose closure is the bundle's
/// business, validated at launch. `loader` is the interpreter the
/// host's pair runs, when that pair won: only then can a family member
/// the bundle lacks be supplied at all, and only then does a glibc
/// loader soname have a copy to name. `None` is the bundled pair.
fn closure_verdict(
    module: &Path,
    host: &HostIndex,
    bundled: &BTreeMap<String, PathBuf>,
    winners: &BTreeMap<String, PathBuf>,
    pair: &ElfFacts,
    loader: Option<&Path>,
    facts: &mut BTreeMap<PathBuf, Option<ElfFacts>>,
) -> Result<BTreeSet<String>, ModuleRefusal> {
    let refuse = |object: PathBuf, reason: String| ModuleRefusal {
        module: module.to_path_buf(),
        object,
        reason,
    };
    let mut seen: BTreeSet<PathBuf> = BTreeSet::new();
    let mut reached: BTreeSet<String> = BTreeSet::new();
    let mut queue: VecDeque<PathBuf> = VecDeque::new();
    queue.push_back(module.to_path_buf());
    while let Some(path) = queue.pop_front() {
        if !seen.insert(path.clone()) {
            continue;
        }
        let Some(object) = facts_of(&path, facts) else {
            return Err(refuse(path, "cannot be read".to_string()));
        };
        if object.class != pair.class || object.machine != pair.machine {
            return Err(refuse(
                path,
                "is not built for the package's architecture".to_string(),
            ));
        }
        match object.libc_family {
            LibcFamily::Glibc | LibcFamily::Unknown => {}
            LibcFamily::Musl => return Err(refuse(path, "is not of the glibc family".to_string())),
            LibcFamily::Conflicting => return Err(refuse(path, "mixes libc families".to_string())),
        }
        // An object without a load segment cannot be opened as a shared
        // object at all: admitting it would present a module that fails
        // at launch instead of a refusal here.
        if !object.mappable {
            return Err(refuse(
                path,
                "has no load segment, so the loader cannot map it".to_string(),
            ));
        }
        // The loader searches an object's old-style `DT_RPATH` ahead of
        // the farm, unless the object also carries a runpath, which
        // supersedes it. A filter object loads its objects outside this
        // closure: either way would bring in copies the checks never
        // saw.
        if object.has_rpath && !object.has_runpath {
            return Err(refuse(
                path,
                "has an old-style search path, which is searched ahead of the farm".to_string(),
            ));
        }
        if object.filtered {
            return Err(refuse(
                path,
                "has a filter object, which loads dependencies the closure does not name"
                    .to_string(),
            ));
        }
        for (dependency, required) in &object.required_versions {
            let Some(defined) =
                supplier_versions(dependency, bundled, host, winners, loader, facts)
            else {
                return Err(refuse(
                    path,
                    format!("needs {dependency}, which nothing supplies"),
                ));
            };
            let missing = required
                .iter()
                .filter(|r| !r.is_weak())
                .find(|r| !defined.contains(&r.name));
            if let Some(missing) = missing {
                return Err(refuse(
                    path,
                    format!(
                        "needs {dependency} version {}, which nothing here defines",
                        missing.name
                    ),
                ));
            }
        }
        for name in &object.needed {
            // A needed name holding a separator is opened as a path, not
            // looked up by soname: nothing the decision chooses answers
            // it, and it must never become a farm link name.
            if name.is_empty() || name.contains('/') || name == "." || name == ".." {
                return Err(refuse(
                    path,
                    format!("needs {name:?}, which is a path rather than a soname"),
                ));
            }
            // The copy that loads is the decision's winner when there
            // is one: the farm links it over the bundle's own copy, so
            // its closure is walked here, whatever the bundle carries.
            // Without a winner a soname the bundle carries loads from
            // the bundle, and its closure is the bundle's business,
            // validated at launch.
            if let Some(winner) = winners.get(name) {
                queue.push_back(winner.clone());
                continue;
            }
            if bundled.contains_key(name) {
                continue;
            }
            // A glibc loader soname loads the loader the pair runs,
            // whatever the cache's entry for the name carries: the
            // closure walks the interpreter, and the soname is reached
            // so the farm links the closed copy under it. With only
            // the bundled pair the bundle's loader copy supplied it
            // above, or nothing does.
            if ldso::is_glibc_loader_name(name) {
                if let Some(loader) = loader {
                    reached.insert(name.clone());
                    queue.push_back(loader.to_path_buf());
                    continue;
                }
                // With only the bundled pair the demand is answered by
                // the loader the process itself runs under, and only by
                // it: the mapped loader carries its own soname, so a
                // foreign glibc loader name would still have to load
                // from disk. Version demands of it stay refused: the
                // mapped loader's definitions are not on disk to read.
                let own = pair
                    .interpreter
                    .as_deref()
                    .and_then(|interp| Path::new(interp).file_name())
                    .and_then(|name| name.to_str());
                if own == Some(name.as_str()) {
                    continue;
                }
                return Err(refuse(
                    path,
                    format!("needs {name}, which nothing supplies"),
                ));
            }
            // A family member rides with the pair that runs. Under the
            // bundled pair nothing outside the farm can be found, so a
            // family soname the bundle lacks cannot be supplied at all.
            if is_libc_family(name) && loader.is_none() {
                return Err(refuse(
                    path,
                    format!("needs {name}, which nothing supplies"),
                ));
            }
            match host.libs.get(name) {
                Some(host_path) => {
                    reached.insert(name.clone());
                    queue.push_back(host_path.clone());
                }
                None => {
                    return Err(refuse(
                        path,
                        format!("needs {name}, which nothing supplies"),
                    ));
                }
            }
        }
    }
    Ok(reached)
}

/// `library_path` values from Vulkan ICD and EGL vendor files under
/// `icd_dirs`. A value with a `/` names a file, absolute or relative to
/// the file; a bare soname is looked up in `bare_names` and dropped when
/// that is `None` or lacks it.
fn icd_library_paths_in(
    icd_dirs: &[&str],
    bare_names: Option<&BTreeMap<String, PathBuf>>,
) -> Vec<PathBuf> {
    let mut out = Vec::new();
    for dir in icd_dirs {
        let Ok(entries) = fs::read_dir(dir) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().is_none_or(|e| e != "json") {
                continue;
            }
            let Ok(text) = fs::read_to_string(&path) else {
                continue;
            };
            let Some(value) = json_library_path(&text) else {
                continue;
            };
            if !value.contains('/') {
                if let Some(lib) = bare_names.and_then(|names| names.get(value)) {
                    out.push(lib.clone());
                }
                continue;
            }
            let lib = if value.starts_with('/') {
                PathBuf::from(value)
            } else {
                path.parent().unwrap_or(Path::new("/")).join(value)
            };
            if lib.is_file() {
                out.push(lib);
            }
        }
    }
    out
}

/// The string value of the first `"library_path"` key in `text`. The files
/// are small and regular enough that a scan is safer than a parser the
/// runtime would otherwise not need.
pub fn json_library_path(text: &str) -> Option<&str> {
    let at = text.find("\"library_path\"")?;
    let rest = &text[at + "\"library_path\"".len()..];
    let rest = rest.trim_start().strip_prefix(':')?.trim_start();
    let rest = rest.strip_prefix('"')?;
    let end = rest.find('"')?;
    Some(&rest[..end])
}

/// The shared objects the bundle carries in its library directories, by
/// soname.
fn bundled_libs(pkg_root: &Path, lib_dirs: &[&str]) -> BTreeMap<String, PathBuf> {
    let mut out = BTreeMap::new();
    for dir in lib_dirs {
        for (name, path) in libs_in(&pkg_root.join(dir)) {
            out.entry(name).or_insert(path);
        }
    }
    out
}

/// Every shared object directly in `dir`, by file name.
///
/// A symlink to an absolute path is not one of them. A package's own links
/// stay inside it, and an absolute one is a link the runtime made to a host
/// or set library, which a mount or a cache tree still shows when a later
/// launch looks.
fn libs_in(dir: &Path) -> Vec<(String, PathBuf)> {
    let Ok(entries) = fs::read_dir(dir) else {
        return Vec::new();
    };
    entries
        .flatten()
        .filter_map(|entry| {
            let name = entry.file_name().to_str().map(String::from)?;
            let path = entry.path();
            let placed = fs::read_link(&path).is_ok_and(|t| t.is_absolute());
            (name.contains(".so") && !placed && path.is_file()).then_some((name, path))
        })
        .collect()
}

/// A description of everything on the host the decision depends on: the
/// cache image, the name service configuration, the directories the
/// cache names, the driver description files and the counterparts of the
/// package's carried plugin directories with their module files, each
/// with its size and modification time, and where loader copies can be
/// kept. Compared whole, so no hash is needed. The stamps carry the
/// change time as well, which a rewrite always moves and a tool cannot
/// pin back, so a rewrite beneath a restored modification time still
/// recomputes. A refusal's evidence in the host's own libraries is not
/// stamped: it re-examines when any watched host state changes, and
/// until then the record keeps refusing, which is the safe direction.
/// Every argument is distinct and a parameter object would be built at
/// each call site and read once.
#[allow(clippy::too_many_arguments)]
fn fingerprint(
    ld_cache: &Path,
    plugin_dirs: &[PluginDir],
    icd_dirs: &[&str],
    policy: HostLibsPolicy,
    extra_root: Option<&Path>,
    host_libc: bool,
    nsswitch: &Path,
    loader_store: LoaderStore,
) -> String {
    let mut out = format!("onelf-resolve-1 {}", policy.as_str());
    if !host_libc {
        out.push_str(" bundled-libc");
    }
    match loader_store {
        LoaderStore::Root { root, .. } => {
            out.push_str(&format!(" loader-store={}", root.display()));
        }
        LoaderStore::Refused(_) => out.push_str(" loader-store=refused"),
    }
    let mut stamp = |path: &Path| {
        out.push('\x1f');
        out.push_str(&path.to_string_lossy());
        match fs::metadata(path) {
            Ok(md) => {
                let mtime = md
                    .modified()
                    .ok()
                    .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                    .map(|d| d.as_nanos())
                    .unwrap_or(0);
                out.push_str(&format!(
                    "={}:{mtime}:{}.{}",
                    md.len(),
                    md.ctime(),
                    md.ctime_nsec()
                ));
            }
            Err(_) => out.push_str("=missing"),
        }
    };
    stamp(ld_cache);
    stamp(nsswitch);
    for dir in drivers::cache_dirs_in(ld_cache) {
        stamp(Path::new(&dir));
    }
    for dir in module_dirs(ld_cache) {
        stamp(&dir);
    }
    for dir in counterpart_dirs(plugin_dirs, ld_cache) {
        stamp(&dir);
        // The module files themselves, so an in-place rewrite at the
        // same size, invisible to the directory stamp, still recomputes
        // the decision.
        for module in modules_in(&dir).unwrap_or_default() {
            stamp(&module);
        }
    }
    for dir in icd_dirs {
        stamp(Path::new(dir));
    }
    if let Some(root) = extra_root {
        for dir in GL_BUILD_LIB_DIRS.iter().chain(GL_BUILD_ICD_DIRS) {
            stamp(&root.join(dir));
        }
    }
    out
}

const DECISION_FILE: &str = "decision";

/// The record format [`resolve_selected`] reads and writes, keyed on the
/// host and the launch's selection identity. The version in the header
/// steps up whenever the layout changes, so a record naming another
/// version is a miss, never a misread.
const SELECTION_RECORD_HEADER: &str = "onelf-resolve-selection 3";

/// The store subdirectory a selection's record lives in, named by the
/// identity's key. Beside the farms, outside the `onelf-` mountpoint
/// namespace the sweep reclaims.
const SELECTION_DIR_PREFIX: &str = "sel-";

/// How many active pins an identity may carry, matching the per-selection
/// count the package metadata enforces on declared and selected sets.
const MAX_ACTIVE_PINS: usize = 4096;

/// How many bytes an active pin's name may carry, matching the name
/// bound the package metadata enforces.
const MAX_PIN_LABEL_BYTES: usize = 128;

/// How many digest bytes a selection's directory key keeps.
const SELECTION_KEY_BYTES: usize = 16;

/// The largest record a read accepts, so hostile state is a miss rather
/// than a large read.
const RECORD_MAX_BYTES: u64 = 1024 * 1024;

/// The link farm for one host state. Keyed by the fingerprint and the
/// winners themselves: two decisions can share a fingerprint (an
/// in-place library swap leaves the directory stamps alone) yet pick
/// different winners, and a farm reused across them would serve the
/// previous decision's stale links. A launch after the host changed
/// builds a new one beside the old rather than replacing it: an
/// instance started earlier may still be loading through the old farm,
/// and two instances starting together build the same one and race for
/// nothing. The closed host loader copy its loader link names keys it
/// too, so a farm only ever names the copy it was built for.
fn farm_dir(
    store: &Path,
    fingerprint: &str,
    winners: &BTreeMap<String, PathBuf>,
    loader_copy: Option<&Path>,
) -> PathBuf {
    let mut h = Fnv::new();
    h.write(fingerprint.as_bytes());
    let mut w = Fnv::new();
    for (name, path) in winners {
        w.write(name.as_bytes());
        w.write(path.to_string_lossy().as_bytes());
        w.write(&[0x1f]);
    }
    w.write(
        loader_copy
            .map(|p| p.as_os_str().as_bytes())
            .unwrap_or_default(),
    );
    store.join(format!("farm-{:016x}-{:016x}", h.0, w.0))
}

/// FNV-1a, so the store's names are stable across runs without a hash
/// dependency.
struct Fnv(u64);

impl Fnv {
    fn new() -> Self {
        Fnv(0xcbf2_9ce4_8422_2325)
    }

    fn write(&mut self, bytes: &[u8]) {
        for &b in bytes {
            self.0 ^= u64::from(b);
            self.0 = self.0.wrapping_mul(0x0100_0000_01b3);
        }
    }
}

/// The cache root subdirectory closed host loader copies are kept in,
/// one directory per closed image's digest.
const LOADERS_DIR: &str = "loaders";

/// Where under the cache `root` the closed image with digest `hash` of a
/// loader named `name` is kept.
fn loader_copy(root: &Path, hash: &blake3::Hash, name: &str) -> PathBuf {
    root.join(LOADERS_DIR)
        .join(hash.to_hex().as_str())
        .join(name)
}

/// The file name a host loader's copy keeps, so the process's mappings
/// still name the loader they came from.
fn loader_name(source: &Path) -> String {
    source
        .file_name()
        .and_then(|n| n.to_str())
        .filter(|n| crate::manifest::is_safe_component(n))
        .unwrap_or("ld.so")
        .to_string()
}

/// Where the farm link for winner `name` points: the closed copy for the
/// loader when one runs, the chosen host file for everything else. The
/// copy is what a bundled loader's path shows once it is shadowed, so a
/// process that re-execs through that path gets the closed loader too.
fn link_target<'p>(name: &str, host: &'p Path, copy: Option<&'p Path>) -> &'p Path {
    match copy {
        Some(copy) if ldso::is_glibc_loader_name(name) => copy,
        _ => host,
    }
}

/// A loader image read whole, refused when it is not a regular file or
/// is larger than any loader.
fn read_loader(path: &Path) -> io::Result<Vec<u8>> {
    use std::io::Read;
    use std::os::unix::fs::OpenOptionsExt;
    // Opened nonblocking first, so a named pipe or device a hostile
    // interpreter names cannot hang the open, then refused unless it is
    // a regular file.
    let file = fs::File::options()
        .read(true)
        .custom_flags(0o4000)
        .open(path)?;
    let md = file.metadata()?;
    if !md.is_file() || md.len() > MAX_LOADER_BYTES as u64 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "not a regular file of a loader's size",
        ));
    }
    let mut image = Vec::with_capacity(md.len() as usize);
    file.take(MAX_LOADER_BYTES as u64 + 1)
        .read_to_end(&mut image)?;
    if image.len() > MAX_LOADER_BYTES {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "grew past a loader's size while read",
        ));
    }
    Ok(image)
}

/// The image of the host loader at `source` closed to its default search
/// directories, or why it cannot be. An image in which no default
/// directory is recognized is refused: nothing then shows that its search
/// is closed.
fn close_loader(source: &Path) -> Result<Vec<u8>, String> {
    let mut image = read_loader(source).map_err(|e| format!("it cannot be read: {e}"))?;
    let scrub = ldso::scrub_glibc_loader(&mut image).map_err(|e| e.to_string())?;
    if scrub.default_dirs == 0 {
        return Err("its image names no default search directory the runtime can close".into());
    }
    Ok(image)
}

/// Write `image` to `path` with execute permission, through a temporary
/// name beside it, so a reader never sees a partial copy.
fn write_loader(path: &Path, image: &[u8]) -> io::Result<()> {
    use std::os::unix::fs::OpenOptionsExt;
    static WRITES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let dir = path.parent().unwrap_or(Path::new("."));
    let n = WRITES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let tmp = dir.join(format!(".loader.{}.{n}", std::process::id()));
    let _ = fs::remove_file(&tmp);
    let mut file = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .mode(0o700)
        .open(&tmp)?;
    let written = file.write_all(image).and_then(|()| {
        drop(file);
        fs::rename(&tmp, path)
    });
    if written.is_err() {
        let _ = fs::remove_file(&tmp);
    }
    written
}

/// The link farm for `fingerprint` holding `winners`, built if it does not
/// exist yet, with its loader links naming `copy` when the host's pair
/// won. An empty set leaves no farm.
///
/// Built in a scratch directory and renamed into place. A rename that finds
/// the farm already there lost to another instance building the same
/// thing, which is fine. Nothing is ever removed.
fn materialize(
    store: &Path,
    fingerprint: &str,
    winners: &BTreeMap<String, PathBuf>,
    copy: Option<&Path>,
) -> io::Result<Option<PathBuf>> {
    if winners.is_empty() {
        return Ok(None);
    }
    let farm = farm_dir(store, fingerprint, winners, copy);
    // A farm left by an earlier launch is reused only when every link
    // still names exactly the chosen copy, the way a served record's
    // farm is checked: the launch searches whatever the directory holds.
    if farm_checked(store, fingerprint, winners, copy).is_some() {
        return Ok(Some(farm));
    }
    static SCRATCHES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let n = SCRATCHES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let scratch = store.join(format!(
        ".{}.{}.{}",
        farm.file_name().unwrap_or_default().to_string_lossy(),
        std::process::id(),
        n
    ));
    let _ = fs::remove_dir_all(&scratch);
    fs::create_dir_all(&scratch)?;
    for (name, target) in winners {
        std::os::unix::fs::symlink(link_target(name, target, copy), scratch.join(name))?;
    }
    match fs::rename(&scratch, &farm) {
        Ok(()) => {}
        Err(_) => {
            // The rename lost to a farm that appeared meanwhile. When
            // that farm checks out it wins; otherwise it is the damaged
            // one this build exists to replace, so rebuild over it.
            if farm_checked(store, fingerprint, winners, copy).is_some() {
                let _ = fs::remove_dir_all(&scratch);
            } else {
                // The farm path may be held by a single stray file,
                // which remove_dir_all cannot clear.
                match fs::symlink_metadata(&farm) {
                    Ok(md) if !md.is_dir() => {
                        let _ = fs::remove_file(&farm);
                    }
                    _ => {
                        let _ = fs::remove_dir_all(&farm);
                    }
                }
                if let Err(e) = fs::rename(&scratch, &farm) {
                    let _ = fs::remove_dir_all(&scratch);
                    return Err(e);
                }
            }
        }
    }
    Ok(Some(farm))
}

/// The merged plugin views of one host state, keyed by the fingerprint,
/// the admitted modules themselves and the package tree the bundled
/// members aim at, the way [`farm_dir`] keys a farm: a changed decision
/// builds beside the old rather than replacing it, an older instance may
/// still be reading through the one it was given, and a decision whose
/// admitted set or package tree changed never shares one. The package
/// tree is part of the key because a view's bundled members aim into
/// it, and a launch tree belongs to its launch alone.
fn views_dir(store: &Path, fingerprint: &str, admitted: &[PathBuf], pkg_root: &Path) -> PathBuf {
    let mut h = Fnv::new();
    h.write(fingerprint.as_bytes());
    let mut w = Fnv::new();
    for path in admitted {
        w.write(path.to_string_lossy().as_bytes());
        w.write(&[0x1f]);
    }
    w.write(pkg_root.to_string_lossy().as_bytes());
    store.join(format!("view-{:016x}-{:016x}", h.0, w.0))
}

/// Whether `dir` links exactly `expected`, names and targets, and
/// nothing else.
fn view_checked(dir: &Path, expected: &[(OsString, PathBuf)]) -> bool {
    let Ok(entries) = fs::read_dir(dir) else {
        return false;
    };
    let got: BTreeMap<OsString, PathBuf> = entries
        .flatten()
        .map(|entry| (entry.file_name(), entry.path()))
        .collect();
    got.len() == expected.len()
        && expected.iter().all(|(name, target)| {
            got.get(name)
                .is_some_and(|link| fs::read_link(link).is_ok_and(|aimed| &aimed == target))
        })
}

/// One record's pending view: its directory name under the decision's
/// view root, the record's package path, and the entries the view
/// links.
struct WantedView {
    name: String,
    relative: String,
    entries: Vec<(OsString, PathBuf)>,
}

/// The view root for the wanted views under `root`: verified and reused
/// when every view's directory links exactly its entries, built beside
/// and renamed over the standing tree otherwise. Each view's directory
/// name is unique inside the root.
fn views_materialized(root: &Path, wanted: &[WantedView]) -> io::Result<()> {
    let stands = |root: &Path| {
        root.is_dir()
            && wanted
                .iter()
                .all(|w| view_checked(&root.join(&w.name), &w.entries))
    };
    if stands(root) {
        return Ok(());
    }
    static SCRATCHES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let n = SCRATCHES.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let scratch = root.parent().unwrap_or(root).join(format!(
        ".{}.{}.{}",
        root.file_name().unwrap_or_default().to_string_lossy(),
        std::process::id(),
        n
    ));
    let _ = fs::remove_dir_all(&scratch);
    fs::create_dir_all(&scratch)?;
    for w in wanted {
        let dir = scratch.join(&w.name);
        fs::create_dir(&dir)?;
        for (member, target) in &w.entries {
            std::os::unix::fs::symlink(target, dir.join(member))?;
        }
    }
    match fs::rename(&scratch, root) {
        Ok(()) => {}
        Err(first) => {
            // The rename lost to a root that appeared meanwhile. When
            // that root checks out it wins; otherwise it is the damaged
            // one this build exists to replace, so rebuild over it.
            // Siblings can hold the root mid-rebuild for a few rounds,
            // so the look, the clear and the rename retry a bounded
            // number of times before this build gives up.
            let mut last = first;
            for _ in 0..3 {
                if stands(root) {
                    let _ = fs::remove_dir_all(&scratch);
                    return Ok(());
                }
                // The root path may be held by a single stray file,
                // which remove_dir_all cannot clear.
                match fs::symlink_metadata(root) {
                    Ok(md) if !md.is_dir() => {
                        let _ = fs::remove_file(root);
                    }
                    _ => {
                        let _ = fs::remove_dir_all(root);
                    }
                }
                match fs::rename(&scratch, root) {
                    Ok(()) => return Ok(()),
                    Err(e) => last = e,
                }
            }
            // A sibling launch's rebuild may have landed in between:
            // when the standing root checks out, it wins and this
            // build's scratch is dropped.
            let _ = fs::remove_dir_all(&scratch);
            if !stands(root) {
                return Err(last);
            }
        }
    }
    Ok(())
}

/// Every place a host may keep `dir`'s counterpart: its compiled-in
/// absolute paths, and its library-relative path under every
/// loader-cache library directory in `libs`.
fn counterpart_spellings<'a>(
    dir: &'a PluginDir,
    libs: &'a [String],
) -> impl Iterator<Item = PathBuf> + 'a {
    let below = (!dir.lib_relative.is_empty())
        .then(|| {
            libs.iter()
                .map(|lib| Path::new(lib).join(&dir.lib_relative))
        })
        .into_iter()
        .flatten();
    dir.absolutes.iter().map(PathBuf::from).chain(below)
}

/// The counterpart directories of one record that exist on this host.
fn record_counterpart_dirs(dir: &PluginDir, libs: &[String]) -> Vec<PathBuf> {
    let out: BTreeSet<PathBuf> = counterpart_spellings(dir, libs).collect();
    out.into_iter().filter(|dir| dir.is_dir()).collect()
}

/// The entries a view of `relative` holds, sorted by member name: every
/// bundled member aimed at the package's copy, then every admitted host
/// member whose name no bundled member holds, aimed at the host's file.
/// `None` when the package does not carry the directory, in which case
/// no redirection would reach the view either.
fn view_entries(
    pkg_root: &Path,
    relative: &str,
    members: &[&PathBuf],
) -> Option<Vec<(OsString, PathBuf)>> {
    let bundled = pkg_root.join(relative);
    let read = fs::read_dir(&bundled).ok()?;
    let mut out: Vec<(OsString, PathBuf)> = Vec::new();
    let mut names: BTreeSet<OsString> = BTreeSet::new();
    for entry in read.flatten() {
        let name = entry.file_name();
        names.insert(name.clone());
        out.push((name, bundled.join(entry.file_name())));
    }
    for member in members {
        let Some(name) = member.file_name() else {
            continue;
        };
        if names.insert(name.to_os_string()) {
            out.push((name.to_os_string(), (*member).clone()));
        }
    }
    out.sort_by(|a, b| a.0.cmp(&b.0));
    Some(out)
}

/// The merged plugin views of one decision: one per plugin directory
/// whose counterpart directories hold admitted modules, under one
/// decision-keyed root in the store. A record whose bundled directory
/// cannot be read is left out: nothing would reach its view. A root
/// that cannot be written leaves every directory's bundled members
/// alone, which loses the host modules of the launch, never the
/// package's.
fn plugin_views(req: &Request, fingerprint: &str, admitted: &[PathBuf]) -> Vec<PluginView> {
    if admitted.is_empty() || req.plugin_dirs.is_empty() {
        return Vec::new();
    }
    let libs = counterpart_lib_dirs(req.ld_cache);
    let mut wanted: Vec<WantedView> = Vec::new();
    for (index, dir) in req.plugin_dirs.iter().enumerate() {
        let dirs = record_counterpart_dirs(dir, &libs);
        let members: Vec<&PathBuf> = admitted
            .iter()
            .filter(|module| {
                module
                    .parent()
                    .is_some_and(|parent| dirs.contains(&parent.to_path_buf()))
            })
            .collect();
        if members.is_empty() {
            continue;
        }
        let Some(entries) = view_entries(req.pkg_root, &dir.relative, &members) else {
            continue;
        };
        wanted.push(WantedView {
            name: format!("dir-{index}"),
            relative: dir.relative.clone(),
            entries,
        });
    }
    if wanted.is_empty() {
        return Vec::new();
    }
    let root = views_dir(req.store, fingerprint, admitted, req.pkg_root);
    if let Err(e) = views_materialized(&root, &wanted) {
        eprintln!(
            "onelf-rt: resolver: cannot build the merged plugin views in {}: {e}",
            root.display()
        );
        return Vec::new();
    }
    wanted
        .into_iter()
        .map(|w| PluginView {
            relative: w.relative,
            dir: root.join(w.name),
        })
        .collect()
}

/// Attach a decision's merged plugin views to its resolution.
fn with_plugin_views(mut resolution: Resolution, req: &Request, fingerprint: &str) -> Resolution {
    resolution.plugin_views = plugin_views(req, fingerprint, &resolution.admitted);
    resolution
}

/// Make sure `loader`'s copy holds its closed image, writing it when it is
/// absent or does not match. Every directory from the cache root down to the
/// copy's has to be a `0700` one the store's uid owns, as the cache's own
/// are.
fn keep_loader_copy(loader: &ClosedLoader, store: LoaderStore) -> io::Result<()> {
    let LoaderStore::Root { root, uid } = store else {
        return Err(io::Error::other("no loader store"));
    };
    let dir = loader.copy.parent().unwrap_or(root);
    let loaders = dir.parent().unwrap_or(root);
    for d in [root, loaders, dir] {
        if !crate::cache_layout::ensure_safe_dir(d, uid) {
            return Err(io::Error::other(format!(
                "{} is not a private directory",
                d.display()
            )));
        }
    }
    let copy = HostLoader {
        path: loader.copy.clone(),
        hash: loader.hash,
    };
    if copy_is_owned_file(&copy.path, uid) && copy.verify().is_ok() {
        return Ok(());
    }
    write_loader(&loader.copy, &loader.image)
        .map_err(|e| io::Error::new(e.kind(), format!("cannot write {}: {e}", dir.display())))
}

/// Whether `copy`, kept under the cache `root`, can be trusted by its
/// digest. Its contents are a function of the world-readable host loader,
/// so anyone can produce a file that hashes right; what makes it this
/// uid's is where it sits. Every directory from the root down to the
/// copy's has to be a `0700` one `uid` owns, so nobody else can swap the
/// file once it is checked, and the copy has to be a regular file `uid`
/// owns, not a link to one.
fn copy_is_private(root: &Path, copy: &HostLoader, uid: u32) -> bool {
    let loaders = root.join(LOADERS_DIR);
    let dir = loaders.join(copy.hash.to_hex().as_str());
    [root, &loaders, &dir]
        .iter()
        .all(|d| crate::cache_layout::is_safe_owned_dir(d, uid))
        && copy.path.parent() == Some(dir.as_path())
        && copy_is_owned_file(&copy.path, uid)
}

/// Whether `path` is a regular file `uid` owns, not a link to one, that
/// nobody else may write and that no other name shares, so nothing outside
/// its private directory can reach it.
fn copy_is_owned_file(path: &Path, uid: u32) -> bool {
    use std::os::unix::fs::MetadataExt;
    fs::symlink_metadata(path).is_ok_and(|md| {
        md.is_file() && md.uid() == uid && md.mode() & 0o022 == 0 && md.nlink() == 1
    })
}

/// Write `resolution` and the winners it was decided from to `dir`'s
/// record, replacing any earlier one atomically. The identity lines come
/// first, then the host state the decision was made about.
fn write_decision(
    dir: &Path,
    header: &str,
    identity_text: &str,
    fingerprint: &str,
    resolution: &Resolution,
    winners: &BTreeMap<String, PathBuf>,
) -> io::Result<()> {
    fs::create_dir_all(dir)?;
    let mut text = String::new();
    text.push_str(header);
    text.push('\n');
    if !identity_text.is_empty() {
        text.push_str(identity_text);
        text.push('\n');
    }
    text.push_str(&format!("fingerprint {fingerprint}\n"));
    if let Some(loader) = &resolution.host_interp {
        let name = loader
            .path
            .file_name()
            .map(|n| n.to_string_lossy())
            .unwrap_or_default();
        text.push_str(&format!("interp {}\t{name}\n", loader.hash.to_hex()));
    }
    for (name, path) in winners {
        text.push_str(&format!("host {name}\t{}\n", path.display()));
    }
    // The identity of every winner, so the next launch can tell an
    // in-place same-size swap (invisible to the directory stamps) from
    // the host the decision was made about.
    for (name, path) in winners {
        text.push_str(&format!("stat {name}\t{}\n", stat_line(path)));
    }
    // The count of admissions, so a record truncated before its module
    // section is a miss instead of a launch that silently presents
    // nothing.
    text.push_str(&format!("admitted {}\n", resolution.admitted.len()));
    for module in &resolution.admitted {
        text.push_str(&format!("module {}\n", module.display()));
    }
    // The identity of every admitted module, for the same reason the
    // winners carry one: an in-place same-size swap leaves the
    // counterpart directory stamps alone.
    for module in &resolution.admitted {
        text.push_str(&format!(
            "module-stat {}\t{}\n",
            module.display(),
            stat_line(module)
        ));
    }
    for name in &resolution.incomparable {
        text.push_str(&format!("incomparable {name}\n"));
    }
    let tmp = dir.join(format!(".{DECISION_FILE}.{}.{}", std::process::id(), {
        static WRITES: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        WRITES.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
    }));
    let _ = fs::remove_file(&tmp);
    fs::File::create(&tmp)?.write_all(text.as_bytes())?;
    fs::rename(&tmp, dir.join(DECISION_FILE))
}

/// Record a selection's decision under its identity.
fn record_selection(
    dir: &Path,
    identity: &SelectionIdentity,
    fingerprint: &str,
    resolution: &Resolution,
    winners: &BTreeMap<String, PathBuf>,
) -> io::Result<()> {
    write_decision(
        dir,
        SELECTION_RECORD_HEADER,
        identity.canonical(),
        fingerprint,
        resolution,
        winners,
    )
}

/// The identity of one file: size, inode, and both times, separated by
/// colons. `missing` when the file cannot be stat'ed at all.
fn stat_line(path: &Path) -> String {
    match fs::metadata(path) {
        Ok(md) => {
            use std::os::unix::fs::MetadataExt;
            format!(
                "{}:{}:{}:{}:{}:{}",
                md.len(),
                md.ino(),
                md.mtime(),
                md.mtime_nsec(),
                md.ctime(),
                md.ctime_nsec()
            )
        }
        Err(_) => "missing".to_string(),
    }
}

/// A selection's recorded decision, when it was made for this exact
/// identity and host state, everything it names still exists, and a
/// closed loader copy it names still matches its digest.
/// `farm_base` is the store the decision's farm lives in, and
/// `loader_store` where loader copies are kept.
fn load_selection(
    dir: &Path,
    identity: &SelectionIdentity,
    fingerprint: &str,
    farm_base: &Path,
    loader_store: LoaderStore,
) -> Option<Resolution> {
    let text = read_record_bounded(&dir.join(DECISION_FILE))?;
    let lines = identity.identity_lines();
    let (mut resolution, winners, loader) =
        parse_decision(&text, SELECTION_RECORD_HEADER, &lines, fingerprint)?;
    if winners.is_empty() {
        return loader.is_none().then_some(resolution);
    }
    let copy = match loader {
        Some((hash, name)) => {
            let LoaderStore::Root { root, uid } = loader_store else {
                return None;
            };
            let copy = HostLoader {
                path: loader_copy(root, &hash, &name),
                hash,
            };
            if !copy_is_private(root, &copy, uid) {
                return None;
            }
            copy.verify().ok()?;
            Some(copy)
        }
        None => None,
    };
    let copy_path = copy.as_ref().map(|c| c.path.as_path());
    resolution.farm = Some(farm_checked(farm_base, fingerprint, &winners, copy_path)?);
    resolution.host_interp = copy;
    Some(resolution)
}

/// The record's text, read only when it is a small regular file holding
/// valid UTF-8. Hostile or damaged state is a miss with a clean reason,
/// never a large read or a partial adoption.
fn read_record_bounded(path: &Path) -> Option<String> {
    let md = fs::metadata(path).ok()?;
    if !md.is_file() || md.len() > RECORD_MAX_BYTES {
        return None;
    }
    fs::read_to_string(path).ok()
}

/// A record's closed loader: the copy's digest and file name.
type RecordedLoader = (blake3::Hash, String);

/// The decision a record carries, when its version, identity lines and
/// host state all check out, together with the winners it was decided
/// from and the closed loader it runs, if any. Each admitted module is
/// named once, the record declares how many it admits, and every
/// module and every winner carries exactly one `stat` line that still
/// matches it, so a truncated, damaged, padded or stale record is a
/// miss and nothing is adopted partially. Each winner is named once on
/// a `host` line: a repeat would move the winner after its `stat` line
/// verified a different path.
fn parse_decision(
    text: &str,
    header: &str,
    identity_lines: &[&str],
    fingerprint: &str,
) -> Option<(
    Resolution,
    BTreeMap<String, PathBuf>,
    Option<RecordedLoader>,
)> {
    let mut lines = text.lines();
    if lines.next()? != header {
        return None;
    }
    for expected in identity_lines {
        if lines.next()? != *expected {
            return None;
        }
    }
    if lines.next()?.strip_prefix("fingerprint ")? != fingerprint {
        return None;
    }
    let mut resolution = Resolution::default();
    let mut winners: BTreeMap<String, PathBuf> = BTreeMap::new();
    let mut loader = None;
    let mut admitted_count: Option<usize> = None;
    let mut winner_stats: BTreeSet<String> = BTreeSet::new();
    let mut modules: BTreeSet<PathBuf> = BTreeSet::new();
    let mut verified: BTreeSet<PathBuf> = BTreeSet::new();
    for line in lines {
        if let Some(rest) = line.strip_prefix("interp ") {
            let (hash, name) = rest.split_once('\t')?;
            if loader.is_some() || !crate::manifest::is_safe_component(name) {
                return None;
            }
            loader = Some((blake3::Hash::from_hex(hash).ok()?, name.to_string()));
        } else if let Some(rest) = line.strip_prefix("admitted ") {
            let count = rest.parse::<usize>().ok()?;
            if admitted_count.is_some() {
                return None;
            }
            admitted_count = Some(count);
        } else if let Some(rest) = line.strip_prefix("host ") {
            let (name, path) = rest.split_once('\t')?;
            if winners.contains_key(name) {
                return None;
            }
            winners.insert(name.to_string(), PathBuf::from(path));
        } else if let Some(rest) = line.strip_prefix("stat ") {
            let (name, recorded) = rest.split_once('\t')?;
            let path = winners.get(name)?;
            if !winner_stats.insert(name.to_string()) || stat_line(path) != recorded {
                return None;
            }
        } else if let Some(rest) = line.strip_prefix("module ") {
            if !modules.insert(PathBuf::from(rest)) {
                return None;
            }
        } else if let Some(rest) = line.strip_prefix("module-stat ") {
            let (path, recorded) = rest.split_once('\t')?;
            let path = Path::new(path);
            if recorded == "missing"
                || !modules.contains(path)
                || !verified.insert(path.to_path_buf())
                || stat_line(path) != recorded
            {
                return None;
            }
        } else {
            let name = line.strip_prefix("incomparable ")?;
            resolution.incomparable.push(name.to_string());
        }
    }
    if admitted_count != Some(modules.len())
        || winner_stats.len() != winners.len()
        || verified.len() != modules.len()
    {
        return None;
    }
    resolution.admitted = modules.into_iter().collect();
    Some((resolution, winners, loader))
}

/// The farm for `fingerprint` holding `winners` and naming the loader
/// `copy`, when every link names what the decision chose and the chosen
/// file still exists.
fn farm_checked(
    base: &Path,
    fingerprint: &str,
    winners: &BTreeMap<String, PathBuf>,
    copy: Option<&Path>,
) -> Option<PathBuf> {
    let farm = farm_dir(base, fingerprint, winners, copy);
    for (name, path) in winners {
        let expected = link_target(name, path, copy);
        match fs::read_link(farm.join(name)) {
            Ok(target) if target == expected && path.is_file() => {}
            _ => return None,
        }
    }
    // The launch searches the whole farm, so any entry beyond the
    // winners' own links would put an unchecked copy on the search path.
    let entries = match fs::read_dir(&farm) {
        Ok(entries) => entries,
        Err(_) => return None,
    };
    let mut linked = 0usize;
    for entry in entries.flatten() {
        let name = entry.file_name();
        let name = name.to_string_lossy();
        if !winners.contains_key(name.as_ref()) {
            return None;
        }
        linked += 1;
    }
    if linked != winners.len() {
        return None;
    }
    Some(farm)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::verdef::fixture::{self, shared_object, shared_object_with_interp, temp_dir};
    use std::process::Command;

    /// A host with a library directory and a loader cache naming its
    /// contents, and a package root with one `lib/` directory.
    struct World {
        host_lib: PathBuf,
        cache: PathBuf,
        pkg: PathBuf,
        store: PathBuf,
        /// Where the host's ICD files go; empty unless a test writes one.
        icd_dir: PathBuf,
        /// The farm the last resolution produced, where `farm_link` looks.
        last_farm: std::cell::RefCell<Option<PathBuf>>,
        /// The cache root closed loader copies go under.
        loader_root: PathBuf,
        /// The host's name service configuration; absent unless a test
        /// writes one.
        nsswitch: PathBuf,
        /// The plugin directories the package carries, empty unless a
        /// test carries some with [`World::carry`].
        plugin_dirs: std::cell::RefCell<Vec<PluginDir>>,
        uid: u32,
    }

    impl World {
        fn new() -> Self {
            let root = temp_dir("resolve");
            let host_lib = root.join("host");
            let pkg = root.join("pkg");
            fs::create_dir_all(&host_lib).unwrap();
            fs::create_dir_all(pkg.join("lib")).unwrap();
            let icd_dir = root.join("icd.d");
            fs::create_dir_all(&icd_dir).unwrap();
            World {
                cache: root.join("ld.so.cache"),
                store: root.join("store"),
                host_lib,
                pkg,
                icd_dir,
                last_farm: std::cell::RefCell::new(None),
                loader_root: root.join("cache"),
                nsswitch: root.join("nsswitch.conf"),
                plugin_dirs: std::cell::RefCell::new(Vec::new()),
                uid: {
                    use std::os::unix::fs::MetadataExt;
                    fs::metadata(&root).unwrap().uid()
                },
            }
        }

        fn loader_store(&self) -> LoaderStore<'_> {
            LoaderStore::Root {
                root: &self.loader_root,
                uid: self.uid,
            }
        }

        /// Have the package carry plugin directories, whose host
        /// counterparts join the decision.
        fn carry(&self, dirs: Vec<PluginDir>) {
            *self.plugin_dirs.borrow_mut() = dirs;
        }

        fn host(&self, name: &str, bytes: &[u8]) -> PathBuf {
            let path = self.host_lib.join(name);
            fs::write(&path, bytes).unwrap();
            self.write_cache();
            path
        }

        /// Put a driver module in the `dri` directory beside the host's
        /// libraries, where Mesa opens it by path.
        fn host_module(&self, name: &str, bytes: &[u8]) -> PathBuf {
            let dir = self.host_lib.join("dri");
            fs::create_dir_all(&dir).unwrap();
            let path = dir.join(name);
            fs::write(&path, bytes).unwrap();
            path
        }

        fn bundle(&self, name: &str, bytes: &[u8]) -> PathBuf {
            let path = self.pkg.join("lib").join(name);
            fs::write(&path, bytes).unwrap();
            path
        }

        /// Rewrite the cache image from the host directory's contents.
        fn write_cache(&self) {
            let mut names: Vec<String> = fs::read_dir(&self.host_lib)
                .unwrap()
                .flatten()
                .filter(|e| e.file_type().is_ok_and(|t| t.is_file()))
                .map(|e| e.file_name().to_string_lossy().into_owned())
                .collect();
            names.sort();
            let paths: Vec<String> = names
                .iter()
                .map(|n| self.host_lib.join(n).to_string_lossy().into_owned())
                .collect();
            let refs: Vec<&str> = paths.iter().map(String::as_str).collect();
            fs::write(&self.cache, new_cache(&refs)).unwrap();
        }

        fn resolve(&self, policy: HostLibsPolicy) -> Resolution {
            self.resolve_with(policy, None)
        }

        fn resolve_with(&self, policy: HostLibsPolicy, extra_root: Option<&Path>) -> Resolution {
            self.resolve_full(policy, extra_root, true)
        }

        /// [`decide`] for this world, with launches able to shadow.
        fn decide(&self, policy: HostLibsPolicy) -> Decision {
            let icd_dir = self.icd_dir.to_string_lossy().into_owned();
            decide(&Request {
                pkg_root: &self.pkg,
                lib_dirs: &["lib"],
                plugin_dirs: &self.plugin_dirs.borrow(),
                policy,
                store: &self.store,
                ld_cache: &self.cache,
                icd_dirs: &[icd_dir.as_str()],
                extra_root: None,
                set_roots: &[],
                host_libc: true,
                loader_store: self.loader_store(),
                nsswitch: &self.nsswitch,
                uid: self.uid,
                trust: None,
            })
        }

        fn resolve_full(
            &self,
            policy: HostLibsPolicy,
            extra_root: Option<&Path>,
            host_libc: bool,
        ) -> Resolution {
            let icd_dir = self.icd_dir.to_string_lossy().into_owned();
            let set_roots = self.set_roots();
            let r = resolve_selected(
                &Request {
                    pkg_root: &self.pkg,
                    lib_dirs: &["lib"],
                    plugin_dirs: &self.plugin_dirs.borrow(),
                    policy,
                    store: &self.store,
                    ld_cache: &self.cache,
                    icd_dirs: &[icd_dir.as_str()],
                    extra_root,
                    set_roots: &set_roots,
                    host_libc,
                    loader_store: self.loader_store(),
                    nsswitch: &self.nsswitch,
                    uid: self.uid,
                    trust: None,
                },
                &[],
            );
            *self.last_farm.borrow_mut() = r.farm.clone();
            r
        }

        /// [`resolve_selected`] against the given set roots and active
        /// pins, with the farm the launch produced remembered for
        /// [`World::farm_link`].
        fn resolve_selection(
            &self,
            policy: HostLibsPolicy,
            set_roots: &[PathBuf],
            pins: &[ActivePin],
        ) -> Resolution {
            let icd_dir = self.icd_dir.to_string_lossy().into_owned();
            let r = resolve_selected(
                &Request {
                    pkg_root: &self.pkg,
                    lib_dirs: &["lib"],
                    plugin_dirs: &self.plugin_dirs.borrow(),
                    policy,
                    store: &self.store,
                    ld_cache: &self.cache,
                    icd_dirs: &[icd_dir.as_str()],
                    extra_root: None,
                    set_roots,
                    host_libc: true,
                    loader_store: self.loader_store(),
                    nsswitch: &self.nsswitch,
                    uid: self.uid,
                    trust: None,
                },
                pins,
            );
            *self.last_farm.borrow_mut() = r.farm.clone();
            r
        }

        fn set_roots(&self) -> Vec<PathBuf> {
            Vec::new()
        }

        fn gl(&self) -> Gl {
            gl_situation(&self.pkg, &["lib"], &[], None, &self.cache, &[])
        }

        fn farm_link(&self, name: &str) -> Option<PathBuf> {
            let farm = self.last_farm.borrow().clone()?;
            fs::read_link(farm.join(name)).ok()
        }
    }

    /// A new-format loader cache image naming `paths`.
    fn new_cache(paths: &[&str]) -> Vec<u8> {
        let mut out = Vec::from(&b"glibc-ld.so.cache1.1"[..]);
        out.extend_from_slice(&(paths.len() as u32).to_le_bytes());
        out.extend_from_slice(&0u32.to_le_bytes());
        out.push(0);
        out.extend_from_slice(&[0; 3]);
        out.extend_from_slice(&0u32.to_le_bytes());
        out.extend_from_slice(&[0; 12]);
        let strings_at = 48 + paths.len() * 24;
        let mut strings: Vec<u8> = Vec::new();
        for p in paths {
            let off = (strings_at + strings.len()) as u32;
            out.extend_from_slice(&drivers::native_entry_flags().to_le_bytes());
            out.extend_from_slice(&0u32.to_le_bytes());
            out.extend_from_slice(&off.to_le_bytes());
            out.extend_from_slice(&0u32.to_le_bytes());
            out.extend_from_slice(&0u64.to_le_bytes());
            strings.extend_from_slice(p.as_bytes());
            strings.push(0);
        }
        out.extend_from_slice(&strings);
        out
    }

    fn versioned(names: &[&str]) -> Vec<u8> {
        let defs: Vec<(u16, &str)> = names.iter().map(|n| (0, *n)).collect();
        shared_object(&defs, &[])
    }

    /// A pin named `label` whose build is pinned by the digest `n`
    /// names.
    fn pin(label: &str, n: u8) -> ActivePin {
        ActivePin {
            label: label.to_string(),
            hash: format!("{n:02x}").repeat(32),
        }
    }

    /// A dependency set root carrying one library in its `lib`
    /// directory, laid out like a mounted or extracted build.
    fn set_lib(dir: &Path, name: &str, bytes: &[u8]) -> PathBuf {
        fs::create_dir_all(dir.join("lib")).unwrap();
        fs::write(dir.join("lib").join(name), bytes).unwrap();
        dir.to_path_buf()
    }

    /// The store directory the identity of `pins` records under.
    fn sel_dir(store: &Path, pins: &[ActivePin]) -> PathBuf {
        store.join(selection_dir(SelectionIdentity::new(pins).unwrap().key()).unwrap())
    }

    /// Pin a file's mtime to a fixed past instant, so a later write of
    /// the same bytes still reads as one.
    fn backdate(path: &Path) -> std::time::SystemTime {
        let times = std::fs::FileTimes::new()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000));
        std::fs::File::options()
            .write(true)
            .open(path)
            .unwrap()
            .set_times(times)
            .unwrap();
        std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000)
    }

    fn driver_needing(needed: &[&str]) -> Vec<u8> {
        shared_object(&[], needed)
    }

    #[test]
    fn a_newer_host_dependency_of_a_driver_wins() {
        let w = World::new();
        let host_gl = w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));

        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(r.farm.as_ref().is_some_and(|f| f.starts_with(&w.store)));
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo));
        // The driver itself is not bundled, so it can only come from the
        // host, and it must be reachable without a host directory on the
        // search path.
        assert_eq!(w.farm_link("libGL.so.1"), Some(host_gl));
        assert!(r.host_interp.is_none());
        assert!(r.incomparable.is_empty());
    }

    /// A report has to show each verdict and where the farm would be,
    /// and must not leave a farm or a recorded decision behind.
    #[test]
    fn deciding_names_every_verdict_and_writes_nothing() {
        let w = World::new();
        let host_gl = w.host(
            "libGL.so.1",
            &driver_needing(&["libfoo.so.1", "libbar.so.1"]),
        );
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.host("libbar.so.1", &versioned(&["BAR_1.0"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        w.bundle("libbar.so.1", &versioned(&["BAR_1.0", "BAR_1.1"]));
        let icd_dir = w.icd_dir.to_string_lossy().into_owned();
        let req = Request {
            pkg_root: &w.pkg,
            lib_dirs: &["lib"],
            plugin_dirs: &[],
            policy: HostLibsPolicy::Auto,
            store: &w.store,
            ld_cache: &w.cache,
            icd_dirs: &[icd_dir.as_str()],
            extra_root: None,
            set_roots: &[],
            host_libc: true,
            loader_store: w.loader_store(),
            nsswitch: &w.nsswitch,
            uid: w.uid,
            trust: None,
        };

        let d = decide(&req);
        assert_eq!(
            d.compared,
            vec![
                ("libGL.so.1".to_string(), None),
                ("libbar.so.1".to_string(), Some(Choice::Bundle)),
                ("libfoo.so.1".to_string(), Some(Choice::Host)),
            ]
        );
        assert_eq!(d.winners.get("libfoo.so.1"), Some(&host_foo));
        assert_eq!(d.winners.get("libGL.so.1"), Some(&host_gl));
        assert!(!w.store.exists(), "deciding wrote the store");

        let r = resolve_selected(&req, &[]);
        assert_eq!(
            r.farm, d.farm,
            "the farm a launch builds is the one reported"
        );
    }

    #[test]
    fn a_driver_dependency_the_bundle_lacks_comes_from_the_host() {
        let w = World::new();
        w.host("libvulkan.so.1", &driver_needing(&["libdrm.so.2"]));
        let host_drm = w.host("libdrm.so.2", &versioned(&[]));
        w.host("libunrelated.so.1", &versioned(&[]));
        w.bundle("libapp.so.1", &versioned(&[]));

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libdrm.so.2"), Some(host_drm));
        assert!(w.farm_link("libunrelated.so.1").is_none());
        assert!(w.farm_link("libapp.so.1").is_none());
    }

    #[test]
    fn auto_ignores_libraries_outside_the_driver_closure() {
        let w = World::new();
        let host_bar = w.host("libbar.so.1", &versioned(&["BAR_1.0", "BAR_2.0"]));
        w.bundle("libbar.so.1", &versioned(&["BAR_1.0"]));

        assert_eq!(w.resolve(HostLibsPolicy::Auto), Resolution::default());
        assert!(w.farm_link("libbar.so.1").is_none());

        let r = w.resolve(HostLibsPolicy::Always);
        assert!(r.farm.is_some());
        assert_eq!(w.farm_link("libbar.so.1"), Some(host_bar));
    }

    #[test]
    fn a_newer_bundled_copy_stays() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        w.host("libfoo.so.1", &versioned(&["FOO_1.0"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libfoo.so.1").is_none());
        assert!(w.farm_link("libGL.so.1").is_some());
    }

    #[test]
    fn unversioned_and_unreadable_host_copies_stay_bundled() {
        let w = World::new();
        w.host(
            "libGL.so.1",
            &driver_needing(&["libfoo.so.1", "libbaz.so.1"]),
        );
        w.host("libfoo.so.1", &versioned(&[]));
        w.bundle("libfoo.so.1", &versioned(&[]));
        let broken = versioned(&["BAZ_9.0"]);
        w.host("libbaz.so.1", &broken[..broken.len() / 2]);
        w.bundle("libbaz.so.1", &versioned(&["BAZ_1.0"]));
        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libfoo.so.1").is_none());
        assert!(w.farm_link("libbaz.so.1").is_none());
        assert!(r.incomparable.is_empty());
    }

    #[test]
    fn incomparable_copies_stay_bundled_and_are_named() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        w.host("libfoo.so.1", &versioned(&["FOO_1.0", "VENDOR_1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libfoo.so.1").is_none());
        assert_eq!(r.incomparable, ["libfoo.so.1"]);
    }

    #[test]
    fn never_takes_nothing() {
        let w = World::new();
        w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        assert_eq!(w.resolve(HostLibsPolicy::Never), Resolution::default());
        assert!(!w.store.exists());
    }

    #[test]
    fn closure_reaches_transitive_dependencies_and_stops_at_missing_ones() {
        let w = World::new();
        w.host(
            "libvulkan.so.1",
            &driver_needing(&["libdrm.so.2", "libgone.so.0"]),
        );
        w.host("libdrm.so.2", &driver_needing(&["libz.so.1"]));
        w.host("libz.so.1", &versioned(&[]));
        w.host("libunrelated.so.1", &versioned(&[]));
        let closure = HostIndex::load(&w.cache, &[], None).driver_closure();
        for name in ["libvulkan.so.1", "libdrm.so.2", "libz.so.1", "libgone.so.0"] {
            assert!(closure.contains(name), "{name} missing from {closure:?}");
        }
        assert!(!closure.contains("libunrelated.so.1"));
    }

    #[test]
    fn nvidia_settings_interface_libraries_do_not_seed_the_closure() {
        let w = World::new();
        w.host(
            "libnvidia-gtk3.so.580.196.07",
            &driver_needing(&["libgtk-3.so.0"]),
        );
        w.host("libgtk-3.so.0", &driver_needing(&["libdbus-1.so.3"]));
        w.host("libdbus-1.so.3", &versioned(&["LIBDBUS_PRIVATE_1.16.2"]));
        w.host("libnvidia-glcore.so.580.196.07", &versioned(&[]));
        let closure = HostIndex::load(&w.cache, &[], None).driver_closure();
        assert!(closure.contains("libnvidia-glcore.so.580.196.07"));
        for name in [
            "libnvidia-gtk3.so.580.196.07",
            "libgtk-3.so.0",
            "libdbus-1.so.3",
        ] {
            assert!(!closure.contains(name), "{name} in {closure:?}");
        }
    }

    /// A synthetic host loader carrying the default directory table and
    /// cache path a real one has, so it can be closed.
    fn closable_loader() -> Vec<u8> {
        let mut image = versioned(&["GLIBC_2.2.5"]);
        image.extend_from_slice(b"\0/etc/ld.so.cache\0/lib64/\0/usr/lib64/\0");
        image
    }

    /// A host libc newer than the bundled one, built for the loader at
    /// `host_ld`, with the bundle carrying the older pair.
    fn newer_host_glibc(w: &World, host_ld: &Path) -> PathBuf {
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.2.5"]));
        w.bundle("ld-linux-x86-64.so.2", &versioned(&["GLIBC_2.2.5"]));
        w.host(
            "libc.so.6",
            &shared_object_with_interp(
                &[(0, "GLIBC_2.2.5"), (0, "GLIBC_2.34")],
                &[],
                Some(host_ld.to_str().unwrap()),
            ),
        )
    }

    #[test]
    fn a_newer_host_glibc_brings_its_loader_and_family() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        let host_libc = newer_host_glibc(&w, &host_ld);
        let host_libm = w.host("libm.so.6", &versioned(&["GLIBC_2.2.5", "GLIBC_2.34"]));
        w.bundle("libm.so.6", &versioned(&["GLIBC_2.2.5"]));
        w.bundle("libother.so.1", &versioned(&["OTHER_1"]));

        let r = w.resolve(HostLibsPolicy::Auto);
        let loader = r.host_interp.clone().expect("the host's loader runs");
        assert_eq!(
            loader.path,
            w.loader_root
                .join("loaders")
                .join(loader.hash.to_hex().as_str())
                .join("ld-linux-x86-64.so.2")
        );
        for dir in [&w.loader_root, &w.loader_root.join("loaders")] {
            assert!(crate::cache_layout::is_safe_owned_dir(dir, w.uid));
        }
        assert_eq!(w.farm_link("libc.so.6"), Some(host_libc));
        assert_eq!(w.farm_link("libm.so.6"), Some(host_libm));
        assert_eq!(
            w.farm_link("ld-linux-x86-64.so.2"),
            Some(loader.path.clone()),
            "the shadowed bundled loader path shows the closed copy"
        );
        assert!(w.farm_link("libother.so.1").is_none());

        let mut closed = closable_loader();
        ldso::scrub_glibc_loader(&mut closed).unwrap();
        assert_eq!(fs::read(&loader.path).unwrap(), closed);
        assert_eq!(loader.hash, blake3::hash(&closed));
        loader.verify().unwrap();
        assert_eq!(
            fs::read(&host_ld).unwrap(),
            closable_loader(),
            "the host's loader is never written"
        );

        let decided = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decided.farm, r.farm, "the report names the launch's farm");
        assert_eq!(decided.host_interp.unwrap().source, host_ld);
    }

    /// A counterpart module that demands a version of the loader
    /// soname is weighed against the interpreter the pair runs, not
    /// the cache's entry for that soname: the interpreter is the copy
    /// the farm links for the module to load.
    #[test]
    fn a_module_demands_versions_of_the_interpreter_not_the_cache_entry() {
        let w = World::new();
        // The cache names a loader whose build defines no 2.34; the
        // interpreter the host libc names does.
        w.host("ld-linux-x86-64.so.2", &versioned(&["GLIBC_2.2.5"]));
        let interp_dir = w.host_lib.parent().unwrap().join("interp");
        fs::create_dir_all(&interp_dir).unwrap();
        let interp = interp_dir.join("ld-linux-x86-64.so.2");
        let mut image = versioned(&["GLIBC_2.2.5", "GLIBC_2.34"]);
        image.extend_from_slice(b"\0/etc/ld.so.cache\0/lib64/\0/usr/lib64/\0");
        fs::write(&interp, &image).unwrap();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.2.5"]));
        w.host(
            "libc.so.6",
            &shared_object_with_interp(
                &[(0, "GLIBC_2.2.5"), (0, "GLIBC_2.34")],
                &[],
                Some(interp.to_str().unwrap()),
            ),
        );
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(
                0,
                &["ld-linux-x86-64.so.2"],
                &[("ld-linux-x86-64.so.2", &[(0, "GLIBC_2.34")])],
            ),
        )
        .unwrap();

        let decided = w.decide(HostLibsPolicy::Auto);
        assert_eq!(
            decided.winners.get("ld-linux-x86-64.so.2"),
            Some(&interp),
            "the interpreter, not the cache's entry, supplies the soname"
        );
        assert_eq!(decided.admitted, vec![module.clone()]);

        let r = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(r.admitted, vec![module]);
        let loader = r.host_interp.expect("the host's loader runs");
        assert_eq!(w.farm_link("ld-linux-x86-64.so.2"), Some(loader.path));
    }

    /// A counterpart module that demands a loader soname other than
    /// the interpreter's own file name is still weighed against the
    /// interpreter the pair runs, and the farm links the closed copy
    /// under the demanded name: the cache's distinct entry for that
    /// soname supplies neither the versions nor the launch.
    #[test]
    fn a_module_demands_a_loader_soname_the_interpreter_supplies() {
        let w = World::new();
        // The cache's entry for the demanded soname defines no 2.34;
        // the interpreter the host libc names, under another name, does.
        w.host("ld-linux-x86-64.so.2", &versioned(&["GLIBC_2.2.5"]));
        let interp_dir = w.host_lib.parent().unwrap().join("interp");
        fs::create_dir_all(&interp_dir).unwrap();
        let interp = interp_dir.join("ld64.so.2");
        let mut image = versioned(&["GLIBC_2.2.5", "GLIBC_2.34"]);
        image.extend_from_slice(b"\0/etc/ld.so.cache\0/lib64/\0/usr/lib64/\0");
        fs::write(&interp, &image).unwrap();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.2.5"]));
        w.host(
            "libc.so.6",
            &shared_object_with_interp(
                &[(0, "GLIBC_2.2.5"), (0, "GLIBC_2.34")],
                &[],
                Some(interp.to_str().unwrap()),
            ),
        );
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(
                0,
                &["ld-linux-x86-64.so.2"],
                &[("ld-linux-x86-64.so.2", &[(0, "GLIBC_2.34")])],
            ),
        )
        .unwrap();

        let decided = w.decide(HostLibsPolicy::Auto);
        assert_eq!(
            decided.winners.get("ld-linux-x86-64.so.2"),
            Some(&interp),
            "the demanded soname names the interpreter the pair runs"
        );
        assert_eq!(decided.admitted, vec![module.clone()]);

        let r = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(r.admitted, vec![module]);
        let loader = r.host_interp.expect("the host's loader runs");
        assert_eq!(w.farm_link("ld-linux-x86-64.so.2"), Some(loader.path));
    }

    /// A loader whose image names no default directory the scrub knows
    /// cannot be shown closed, so the bundled pair runs and the reason
    /// is there for the report.
    #[test]
    fn a_host_loader_that_cannot_be_closed_keeps_the_bundled_pair() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &versioned(&["GLIBC_2.2.5"]));
        newer_host_glibc(&w, &host_ld);

        let r = w.resolve(HostLibsPolicy::Always);
        assert!(r.host_interp.is_none());
        assert!(w.farm_link("libc.so.6").is_none());
        assert!(w.farm_link("ld-linux-x86-64.so.2").is_none());

        let decided = w.decide(HostLibsPolicy::Always);
        assert!(decided.host_interp.is_none());
        let (source, why) = decided.host_interp_refused.unwrap();
        assert_eq!(source, host_ld);
        assert!(why.contains("no default search directory"), "{why}");

        fs::write(&host_ld, b"not an ELF image").unwrap();
        let (_, why) = w
            .decide(HostLibsPolicy::Always)
            .host_interp_refused
            .unwrap();
        assert!(why.contains("not an ELF object"), "{why}");
    }

    /// Without a place a loader copy can run from, the host's pair is not
    /// taken, and a record made while there was one is not served.
    #[test]
    fn a_refused_loader_store_keeps_the_bundled_pair() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        assert!(w.resolve(HostLibsPolicy::Always).host_interp.is_some());

        let icd_dir = w.icd_dir.to_string_lossy().into_owned();
        let req = Request {
            pkg_root: &w.pkg,
            lib_dirs: &["lib"],
            plugin_dirs: &[],
            policy: HostLibsPolicy::Always,
            store: &w.store,
            ld_cache: &w.cache,
            icd_dirs: &[icd_dir.as_str()],
            extra_root: None,
            set_roots: &[],
            host_libc: true,
            loader_store: LoaderStore::Refused("the loader copy's filesystem is mounted noexec"),
            nsswitch: &w.nsswitch,
            uid: w.uid,
            trust: None,
        };
        let r = resolve_selected(&req, &[]);
        assert!(r.host_interp.is_none());
        assert!(r.farm.is_none_or(|farm| !farm.join("libc.so.6").exists()));
        let (source, why) = decide(&req).host_interp_refused.unwrap();
        assert_eq!(source, host_ld);
        assert_eq!(why, "the loader copy's filesystem is mounted noexec");
    }

    /// A copy that cannot be written after all takes the host's pair away
    /// but nothing else: the driver still comes from the host, and since
    /// the fallback is not recorded, a later launch tries the copy again.
    #[test]
    fn a_copy_that_cannot_be_written_keeps_the_rest_of_the_decision() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let host_gl = w.host("libGL.so.1", &driver_needing(&[]));
        fs::create_dir_all(&w.loader_root).unwrap();
        fs::write(w.loader_root.join("loaders"), b"in the way").unwrap();

        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(r.host_interp.is_none());
        assert_eq!(w.farm_link("libGL.so.1"), Some(host_gl));
        assert!(w.farm_link("libc.so.6").is_none());
        assert!(w.farm_link("ld-linux-x86-64.so.2").is_none());
        assert!(!sel_dir(&w.store, &[]).join(DECISION_FILE).exists());

        fs::remove_file(w.loader_root.join("loaders")).unwrap();
        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(r.host_interp.is_some());
        assert!(w.farm_link("libc.so.6").is_some());
    }

    /// Two decisions closing the same loader share one copy.
    #[test]
    fn one_copy_serves_every_decision_of_a_loader() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let first = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();
        let second = w.resolve(HostLibsPolicy::Always).host_interp.unwrap();
        assert_eq!(first, second);
        let copies = fs::read_dir(w.loader_root.join("loaders")).unwrap().count();
        assert_eq!(copies, 1);
    }

    /// The host libc opens its name service modules and `libgcc_s` by
    /// name, and the closed loader finds them only in the farm. Each
    /// service the configuration names is chosen when the bundle lacks
    /// it, with what it needs, and none is chosen beside the bundled libc.
    #[test]
    fn the_host_libc_gets_the_modules_it_opens_by_name() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let sss = w.host("libnss_sss.so.2", &driver_needing(&["libsssdep.so.1"]));
        let dep = w.host("libsssdep.so.1", &versioned(&[]));
        let gcc = w.host("libgcc_s.so.1", &versioned(&["GCC_3.0"]));
        w.host("libnss_unnamed.so.2", &versioned(&[]));
        fs::write(
            &w.nsswitch,
            "# comment: unnamed\npasswd: files sss [NOTFOUND=return] systemd\n\
             group:    sss\nbogus line\nhosts: Bad.Name dns\n",
        )
        .unwrap();

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libnss_sss.so.2"), Some(sss));
        assert_eq!(w.farm_link("libsssdep.so.1"), Some(dep));
        assert_eq!(w.farm_link("libgcc_s.so.1"), Some(gcc));
        assert!(w.farm_link("libnss_unnamed.so.2").is_none());
        assert_eq!(
            host_libc_opens(&w.nsswitch),
            [
                "libgcc_s.so.1",
                "libnss_dns.so.2",
                "libnss_files.so.2",
                "libnss_sss.so.2",
                "libnss_systemd.so.2",
            ]
        );

        fs::write(&host_ld, versioned(&["GLIBC_2.2.5"])).unwrap();
        w.write_cache();
        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libnss_sss.so.2").is_none());
        assert!(w.farm_link("libgcc_s.so.1").is_none());
    }

    /// A configuration larger than any real one names nothing, and only
    /// its first lines are read.
    #[test]
    fn the_name_service_configuration_is_read_bounded() {
        let w = World::new();
        assert_eq!(host_libc_opens(&w.nsswitch), ["libgcc_s.so.1"]);
        let mut text = "passwd: files\n".repeat(NSSWITCH_MAX_LINES);
        text.push_str("group: late\n");
        fs::write(&w.nsswitch, &text).unwrap();
        assert!(!host_libc_opens(&w.nsswitch).contains(&"libnss_late.so.2".to_string()));
        let huge = "passwd: big\n".repeat(NSSWITCH_MAX_BYTES as usize / 4);
        fs::write(&w.nsswitch, huge).unwrap();
        assert_eq!(host_libc_opens(&w.nsswitch), ["libgcc_s.so.1"]);
    }

    /// Where loader copies can be kept is part of the host: a refusal
    /// recorded while the store was unusable is not served once it is.
    #[test]
    fn the_loader_store_keys_the_decision() {
        let w = World::new();
        let stamp = |store| {
            fingerprint(
                &w.cache,
                &[],
                &[],
                HostLibsPolicy::Auto,
                None,
                true,
                &w.nsswitch,
                store,
            )
        };
        let refused = stamp(LoaderStore::Refused("noexec"));
        assert_ne!(refused, stamp(w.loader_store()));
        assert_eq!(refused, stamp(LoaderStore::Refused("another reason")));
        let elsewhere = w.loader_root.join("elsewhere");
        let moved = stamp(LoaderStore::Root {
            root: &elsewhere,
            uid: w.uid,
        });
        assert_ne!(moved, stamp(w.loader_store()));
    }

    /// A changed name service configuration is a changed host.
    #[test]
    fn the_name_service_configuration_keys_the_decision() {
        let w = World::new();
        let before = fingerprint(
            &w.cache,
            &[],
            &[],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        fs::write(&w.nsswitch, "passwd: files\n").unwrap();
        let after = fingerprint(
            &w.cache,
            &[],
            &[],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        assert_ne!(before, after);
    }

    /// The counterpart directories of the plugin directories the package
    /// carries join the decision: the compiled-in absolute path itself,
    /// and the directory's path under every library directory the cache
    /// names, a multiarch host's included.
    #[test]
    fn a_plugin_directory_keys_the_decision_on_its_counterparts() {
        let w = World::new();
        let multiarch = w.host_lib.join("x86_64-linux-gnu");
        let alsa = multiarch.join("alsa-lib");
        fs::create_dir_all(&alsa).unwrap();
        let named = multiarch.join("libasound.so.2");
        fs::write(&named, b"elf").unwrap();
        let refs: Vec<String> = vec![named.to_string_lossy().into_owned()];
        let str_refs: Vec<&str> = refs.iter().map(String::as_str).collect();
        fs::write(&w.cache, new_cache(&str_refs)).unwrap();
        let absolute = w.host_lib.join("alsa-lib");
        fs::create_dir_all(&absolute).unwrap();
        let carried = vec![PluginDir {
            relative: "lib/alsa-lib".to_owned(),
            lib_relative: "alsa-lib".to_owned(),
            absolutes: vec![absolute.to_string_lossy().into_owned()],
        }];
        let with = fingerprint(
            &w.cache,
            &carried,
            &[],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        assert!(
            with.contains(&format!("\x1f{}", alsa.display())),
            "the multiarch counterpart is stamped: {with}"
        );
        assert!(
            with.contains(&format!("\x1f{}", absolute.display())),
            "the same absolute path is stamped: {with}"
        );
        let plain = fingerprint(
            &w.cache,
            &[],
            &[],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        assert!(!plain.contains(alsa.to_string_lossy().as_ref()));
        assert_ne!(plain, with);
    }

    /// A counterpart that does not exist is stamped nowhere: neither a
    /// compiled-in absolute that is missing, nor a library-directory
    /// join the host has no such directory under.
    #[test]
    fn an_absent_counterpart_is_not_stamped() {
        let w = World::new();
        w.host("libasound.so.2", &versioned(&[]));
        let carried = vec![PluginDir {
            relative: "lib/alsa-lib".to_owned(),
            lib_relative: "alsa-lib".to_owned(),
            absolutes: vec![w.host_lib.join("nowhere").to_string_lossy().into_owned()],
        }];
        let stamped = fingerprint(
            &w.cache,
            &carried,
            &[],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        assert!(!stamped.contains("nowhere"));
        assert!(!stamped.contains("alsa-lib"));
    }

    /// A module arriving in, or leaving, a counterpart directory changes
    /// the fingerprint, so the decision it keyed recomputes.
    #[test]
    fn a_module_added_to_or_removed_from_a_counterpart_rekeys_the_decision() {
        let w = World::new();
        w.host("libasound.so.2", &versioned(&[]));
        let alsa = w.host_lib.join("alsa-lib");
        fs::create_dir_all(&alsa).unwrap();
        let carried = vec![PluginDir {
            relative: "lib/alsa-lib".to_owned(),
            lib_relative: "alsa-lib".to_owned(),
            absolutes: Vec::new(),
        }];
        let stamp = || {
            fingerprint(
                &w.cache,
                &carried,
                &[],
                HostLibsPolicy::Auto,
                None,
                true,
                &w.nsswitch,
                w.loader_store(),
            )
        };
        let before = stamp();
        let module = alsa.join("libasound_module_pcm_pipe.so");
        fs::write(&module, b"elf").unwrap();
        // Set explicitly: a directory's mtime can be coarser than the
        // time between the two writes.
        fs::File::open(&alsa)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000))
            .unwrap();
        let added = stamp();
        assert_ne!(before, added);
        fs::remove_file(&module).unwrap();
        fs::File::open(&alsa)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(2_000_000))
            .unwrap();
        let removed = stamp();
        assert_ne!(added, removed);
    }

    /// A launch after a host module arrives in a counterpart directory
    /// recomputes the decision, and its farm is a new one.
    #[test]
    fn a_new_module_in_a_counterpart_resolves_afresh() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let alsa = w.host_lib.join("alsa-lib");
        fs::create_dir_all(&alsa).unwrap();
        w.carry(vec![PluginDir {
            relative: "lib/alsa-lib".to_owned(),
            lib_relative: "alsa-lib".to_owned(),
            absolutes: Vec::new(),
        }]);
        let first = w.resolve_full(HostLibsPolicy::Always, None, true);
        let farm1 = first.farm.clone().expect("a farm the launch built");
        fs::write(alsa.join("libasound_module_pcm_pipe.so"), b"elf").unwrap();
        fs::File::open(&alsa)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000))
            .unwrap();
        let second = w.resolve_full(HostLibsPolicy::Always, None, true);
        let farm2 = second.farm.clone().expect("a farm the launch built");
        assert_ne!(farm1, farm2);
        fs::remove_file(alsa.join("libasound_module_pcm_pipe.so")).unwrap();
        fs::File::open(&alsa)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(2_000_000))
            .unwrap();
        let third = w.resolve_full(HostLibsPolicy::Always, None, true);
        assert_ne!(farm2, third.farm.unwrap());
    }

    /// A recorded decision is served only while its loader copy matches
    /// the digest it recorded. A damaged copy is a miss, and deciding
    /// again writes the copy back rather than running what is there.
    #[test]
    fn a_damaged_loader_copy_is_never_served() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let first = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();

        fs::write(&first.path, b"\x7fELF tampered").unwrap();
        assert!(first.verify().is_err());
        let again = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();
        assert_eq!(again, first);
        again.verify().unwrap();

        fs::remove_file(&first.path).unwrap();
        let again = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();
        again.verify().unwrap();
    }

    /// A copy is trusted by its digest only where nobody else could have
    /// put it or could swap it: a copy behind a loosened directory, one
    /// owned by another uid, or a link to an identical file is never
    /// served, and deciding again writes a private copy back.
    #[test]
    fn a_copy_outside_private_directories_is_never_served() {
        use std::os::unix::fs::PermissionsExt;
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let copy = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();
        let identity = SelectionIdentity::new(&[]).unwrap();
        let fingerprint = fingerprint(
            &w.cache,
            &[],
            &[w.icd_dir.to_str().unwrap()],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        let dir = sel_dir(&w.store, &[]);
        let load = || load_selection(&dir, &identity, &fingerprint, &w.store, w.loader_store());
        assert!(load().is_some());
        assert!(!copy_is_private(
            &w.loader_root,
            &copy,
            w.uid.wrapping_add(1)
        ));

        let copy_dir = copy.path.parent().unwrap();
        fs::set_permissions(copy_dir, fs::Permissions::from_mode(0o770)).unwrap();
        assert!(load().is_none(), "a group-writable directory");
        let again = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();
        assert!(copy_is_private(&w.loader_root, &again, w.uid));

        let elsewhere = w.loader_root.join("planted");
        fs::copy(&copy.path, &elsewhere).unwrap();
        fs::remove_file(&copy.path).unwrap();
        std::os::unix::fs::symlink(&elsewhere, &copy.path).unwrap();
        copy.verify().unwrap();
        assert!(load().is_none(), "a link to an identical file");
        let again = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();
        assert!(!fs::symlink_metadata(&again.path).unwrap().is_symlink());
        assert!(load().is_some());
    }

    /// A record naming a digest other than its copy's, or a copy name
    /// that leaves the farm, is a miss decided afresh.
    #[test]
    fn a_record_with_a_foreign_loader_is_a_miss() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let first = w.resolve(HostLibsPolicy::Auto).host_interp.unwrap();
        let record = sel_dir(&w.store, &[]).join(DECISION_FILE);
        let text = fs::read_to_string(&record).unwrap();
        let line = format!("interp {}\tld-linux-x86-64.so.2", first.hash.to_hex());
        assert!(text.contains(&line), "{text}");

        let identity = SelectionIdentity::new(&[]).unwrap();
        let fingerprint = fingerprint(
            &w.cache,
            &[],
            &[w.icd_dir.to_str().unwrap()],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        let dir = sel_dir(&w.store, &[]);
        assert!(
            load_selection(&dir, &identity, &fingerprint, &w.store, w.loader_store()).is_some()
        );
        for forged in [
            format!(
                "interp {}\tld-linux-x86-64.so.2",
                blake3::hash(b"other").to_hex()
            ),
            format!("interp {}\t..", first.hash.to_hex()),
            format!("interp {}", first.hash.to_hex()),
            "interp nothex\tld-linux-x86-64.so.2".to_string(),
        ] {
            fs::write(&record, text.replace(&line, &forged)).unwrap();
            assert!(
                load_selection(&dir, &identity, &fingerprint, &w.store, w.loader_store()).is_none(),
                "{forged}"
            );
        }
        fs::write(&record, text.replace(&line, &format!("{line}\n{line}"))).unwrap();
        assert!(
            load_selection(&dir, &identity, &fingerprint, &w.store, w.loader_store()).is_none()
        );
    }

    /// Under the closed host loader nothing is found outside the farm, so
    /// a glibc family member a host driver needs and the bundle lacks is
    /// chosen with the pair.
    #[test]
    fn a_driver_gets_the_family_member_the_bundle_lacks() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let host_dl = w.host("libdl.so.2", &versioned(&["GLIBC_2.2.5"]));
        w.host("libGL.so.1", &driver_needing(&["libdl.so.2"]));

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libdl.so.2"), Some(host_dl));

        fs::write(&host_ld, versioned(&["GLIBC_2.2.5"])).unwrap();
        w.write_cache();
        w.resolve(HostLibsPolicy::Auto);
        assert!(
            w.farm_link("libdl.so.2").is_none(),
            "beside the bundled libc no host family member is taken"
        );
    }

    /// Under the host loader the bundled executables' run path still finds
    /// the bundled libc, so a launch that cannot put the host's copy in its
    /// place must keep the bundled pair rather than crash at start.
    #[test]
    fn a_launch_that_cannot_shadow_keeps_the_bundled_glibc() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);

        let r = w.resolve_full(HostLibsPolicy::Auto, None, false);
        assert!(r.host_interp.is_none());
        assert!(w.farm_link("libc.so.6").is_none());

        // The decision recorded for it is not the one a launch that can
        // shadow gets.
        let r = w.resolve_full(HostLibsPolicy::Auto, None, true);
        assert!(r.host_interp.is_some());
    }

    #[test]
    fn an_older_or_equal_host_glibc_keeps_the_bundled_pair() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &versioned(&[]));
        w.host(
            "libc.so.6",
            &shared_object_with_interp(&[(0, "GLIBC_2.2.5")], &[], Some(host_ld.to_str().unwrap())),
        );
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.2.5", "GLIBC_2.34"]));
        assert_eq!(w.resolve(HostLibsPolicy::Always), Resolution::default());

        w.bundle("libc.so.6", &versioned(&["GLIBC_2.2.5"]));
        assert_eq!(w.resolve(HostLibsPolicy::Always), Resolution::default());
    }

    #[test]
    fn a_host_without_glibc_takes_nothing_for_the_pair() {
        let w = World::new();
        w.host("libc.musl-x86_64.so.1", &versioned(&[]));
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.2.5"]));
        let r = w.resolve(HostLibsPolicy::Always);
        assert!(r.host_interp.is_none());
        assert!(r.farm.is_none());
    }

    /// The 64-bit bytes of a counterpart module of `machine` that
    /// needs `needed` and demands `requirements`, matching the
    /// fixture-built bundled pair's machine.
    fn module_bytes(
        machine: u16,
        needed: &[&str],
        requirements: &[(&str, &[(u16, &str)])],
    ) -> Vec<u8> {
        fixture::facts_elf(true, machine, None, needed, requirements).bytes
    }

    /// A named pipe at `path`, for tests that must not block on it.
    fn make_fifo(path: &Path) {
        let ok = Command::new("mkfifo")
            .arg(path)
            .status()
            .expect("mkfifo(1) must exist")
            .success();
        assert!(ok, "mkfifo failed for {}", path.display());
    }

    /// A package carrying `lib/alsa-lib`, whose host counterpart is the
    /// `alsa-lib` directory beside the host's libraries.
    fn carry_counterpart(w: &World) -> PathBuf {
        let dir = w.host_lib.join("alsa-lib");
        fs::create_dir_all(&dir).unwrap();
        w.carry(vec![PluginDir {
            relative: "lib/alsa-lib".to_string(),
            lib_relative: "alsa-lib".to_string(),
            absolutes: Vec::new(),
        }]);
        dir
    }

    /// The refusal recorded for `module`.
    fn refusal_for<'a>(refused: &'a [ModuleRefusal], module: &Path) -> &'a ModuleRefusal {
        refused
            .iter()
            .find(|r| r.module == module)
            .expect("the module is refused")
    }

    /// A counterpart module the checks admit brings its host
    /// dependencies into the decision, as chosen host libraries.
    #[test]
    fn a_counterpart_module_admits_with_its_host_dependencies() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert_eq!(decision.admitted, vec![module]);
        assert!(decision.refused.is_empty());
        assert_eq!(
            decision.winners.get("libdep.so.2"),
            Some(&w.host_lib.join("libdep.so.2"))
        );
        assert!(
            decision
                .compared
                .iter()
                .any(|(n, c)| n == "libdep.so.2" && c.is_none()),
            "a soname the bundle lacks is named as host-only"
        );
    }

    /// A module built for another machine is refused on itself and
    /// nothing it needs is offered.
    #[test]
    fn a_module_for_another_architecture_is_refused_with_its_dependencies() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(183, &["libdep.so.2"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        assert_eq!(decision.refused.len(), 1);
        assert_eq!(decision.refused[0].module, module);
        assert_eq!(decision.refused[0].object, module);
        assert!(decision.refused[0].reason.contains("architecture"));
        assert!(!decision.winners.contains_key("libdep.so.2"));
        assert!(!decision.compared.iter().any(|(n, _)| n == "libdep.so.2"));
    }

    /// A module of the package's machine but the other ELF class is
    /// refused, as is one linking two libc families at once.
    #[test]
    fn a_module_of_the_other_class_or_of_mixed_libcs_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let narrow = dir.join("libnarrow.so");
        fs::write(
            &narrow,
            fixture::facts_elf(false, 0, None, &["libc.so.6"], &[]).bytes,
        )
        .unwrap();
        let mixed = dir.join("libmixed.so");
        fs::write(
            &mixed,
            module_bytes(0, &["libc.so.6", "libc.musl-x86_64.so.1"], &[]),
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        assert!(
            refusal_for(&decision.refused, &narrow)
                .reason
                .contains("architecture")
        );
        assert!(
            refusal_for(&decision.refused, &mixed)
                .reason
                .contains("mixes libc families")
        );
    }

    /// A dependency of a module that demands a glibc version nothing here
    /// defines refuses the module, naming the dependency and the version,
    /// and the dependency is never offered to the farm.
    #[test]
    fn a_dependency_demanding_an_undefined_version_refuses_its_module() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        let needy = w.host(
            "libneedy.so.1",
            &module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.99")])]),
        );
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libneedy.so.1"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert_eq!(refusal.object, needy);
        assert!(refusal.reason.contains("GLIBC_2.99"), "{}", refusal.reason);
        assert!(!decision.winners.contains_key("libneedy.so.1"));
    }

    /// A needed name holding a separator is a path the loader opens
    /// relative to the working directory, not a soname: the module is
    /// refused, and the name never becomes a farm link.
    #[test]
    fn a_module_needing_a_path_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(0, &["ld-linux-evil/../../../x.so"], &[]),
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        assert!(
            refusal_for(&decision.refused, &module)
                .reason
                .contains("a path rather than a soname")
        );
    }

    /// A host without glibc libraries, a musl host say, admits no
    /// module but still names each one it refused.
    #[test]
    fn a_host_without_glibc_names_every_module_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        let dir = w.host_lib.join("alsa-lib");
        fs::create_dir_all(&dir).unwrap();
        w.carry(vec![PluginDir {
            relative: "lib/alsa-lib".to_string(),
            lib_relative: "alsa-lib".to_string(),
            absolutes: vec![dir.display().to_string()],
        }]);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libc.musl-x86_64.so.1"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        assert_eq!(decision.refused.len(), 1);
        assert_eq!(decision.refused[0].module, module);
        assert!(decision.farm.is_none());
    }

    /// A module someone else could have planted is refused: one in a
    /// directory others can write, and one reached through a link into
    /// such a directory. A sticky directory such as `/tmp` above a
    /// directory of the launching user's own is not a hole.
    #[test]
    fn a_module_another_user_could_plant_is_refused() {
        use std::os::unix::fs::PermissionsExt;
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let plain = || module_bytes(0, &["libc.so.6"], &[]);
        fs::write(dir.join("libplug.so"), plain()).unwrap();
        let open = w.host_lib.parent().unwrap().join("open");
        fs::create_dir_all(&open).unwrap();
        fs::write(open.join("libelsewhere.so"), plain()).unwrap();
        fs::set_permissions(&open, fs::Permissions::from_mode(0o777)).unwrap();
        std::os::unix::fs::symlink(open.join("libelsewhere.so"), dir.join("liblinked.so")).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decision.admitted, vec![dir.join("libplug.so")]);
        assert_eq!(decision.refused.len(), 1);
        assert!(
            decision.refused[0]
                .reason
                .contains("can be written by another user"),
            "{:?}",
            decision.refused
        );

        fs::set_permissions(&dir, fs::Permissions::from_mode(0o777)).unwrap();
        let decision = w.decide(HostLibsPolicy::Auto);
        fs::set_permissions(&dir, fs::Permissions::from_mode(0o755)).unwrap();
        fs::set_permissions(&open, fs::Permissions::from_mode(0o755)).unwrap();
        assert!(decision.admitted.is_empty(), "{:?}", decision.admitted);
        assert!(
            decision
                .refused
                .iter()
                .all(|r| r.reason.contains("can be written by another user")),
            "{:?}",
            decision.refused
        );
    }

    /// A directory holding more modules than the bound is refused whole,
    /// naming the bound, instead of costing every launch.
    #[test]
    fn a_counterpart_past_the_module_bound_is_refused_whole() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        for i in 0..=MAX_COUNTERPART_MODULES {
            fs::write(dir.join(format!("libm{i:05}.so")), b"").unwrap();
        }
        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        assert_eq!(decision.refused.len(), 1);
        assert!(decision.refused[0].reason.contains("holds over"));
    }

    /// A musl-linked module is refused: it must not pull its libc into
    /// the process.
    #[test]
    fn a_musl_module_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libc.musl-x86_64.so.1"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        assert_eq!(decision.refused.len(), 1);
        assert!(decision.refused[0].reason.contains("glibc family"));
    }

    /// A module is weighed by the versions it demands: one whose demand
    /// the copies here define is admitted beside one whose demand
    /// nothing defines, which is refused naming the version.
    #[test]
    fn a_module_is_admitted_or_refused_by_the_versions_it_demands() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let fitting = dir.join("libfits.so");
        fs::write(
            &fitting,
            module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.32")])]),
        )
        .unwrap();
        let demanding = dir.join("libgreedy.so");
        fs::write(
            &demanding,
            module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.99")])]),
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert_eq!(decision.admitted, vec![fitting]);
        assert_eq!(decision.refused.len(), 1);
        assert_eq!(decision.refused[0].module, demanding);
        assert!(decision.refused[0].reason.contains("GLIBC_2.99"));
    }

    /// A module whose dependency nothing here supplies is refused, and
    /// an unreadable file that names itself a module is refused too,
    /// with a readable neighbour still admitted.
    #[test]
    fn an_unsupplied_dependency_or_unreadable_module_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let missing = dir.join("libwants.so");
        fs::write(&missing, module_bytes(0, &["libnothere.so.9"], &[])).unwrap();
        let junk = dir.join("libjunk.so");
        fs::write(&junk, b"not elf").unwrap();
        let plain = dir.join("libplain.so");
        fs::write(&plain, module_bytes(0, &[], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert_eq!(decision.admitted, vec![plain]);
        assert_eq!(decision.refused.len(), 2);
        let reasons: Vec<(PathBuf, String)> = decision
            .refused
            .iter()
            .map(|r| (r.module.clone(), r.reason.clone()))
            .collect();
        assert!(
            reasons
                .iter()
                .any(|(m, r)| m == &missing && r.contains("nothing supplies")),
            "{reasons:?}"
        );
        assert!(
            reasons
                .iter()
                .any(|(m, r)| m == &junk && r.contains("read")),
            "{reasons:?}"
        );
    }

    /// A module carrying an old-style `DT_RPATH` is refused: the
    /// loader searches that path ahead of the farm, so an unchecked
    /// copy could answer a soname the decision chose.
    #[test]
    fn a_module_with_an_old_search_path_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        // DT_RPATH is tag 15.
        fs::write(
            &module,
            fixture::facts_elf_with(true, 0, None, &[], &[], &[(15, 0)]).bytes,
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert_eq!(refusal.object, module);
        assert!(refusal.reason.contains("search path"), "{refusal:?}");
    }

    /// A runpath supersedes an old-style search path: the loader ignores
    /// the rpath of an object that also carries a runpath, so the module
    /// is admitted like one with a runpath alone.
    #[test]
    fn a_runpath_supersedes_an_old_search_path() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        // DT_RPATH is tag 15, DT_RUNPATH is 29.
        fs::write(
            &module,
            fixture::facts_elf_with(true, 0, None, &[], &[], &[(15, 0), (29, 0)]).bytes,
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decision.admitted, vec![module]);
    }

    /// A module whose need names the loader the bundled pair runs is
    /// admitted without any copy on disk: the process runs that loader
    /// already, and glibc answers the demand from the mapped copy.
    #[test]
    fn a_module_naming_the_loader_is_admitted_without_a_copy() {
        let w = World::new();
        w.bundle(
            "libc.so.6",
            &shared_object_with_interp(&[(0, "GLIBC_2.32")], &[], Some("ld-linux-x86-64.so.2")),
        );
        w.host("libextra.so.1", &versioned(&["EXTRA_1"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["ld-linux-x86-64.so.2"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decision.admitted, vec![module]);
        assert!(!decision.winners.contains_key("ld-linux-x86-64.so.2"));
    }

    /// A module naming a glibc loader the pair does not run is refused:
    /// the mapped loader answers only its own soname, and a foreign
    /// loader name would have to load from disk.
    #[test]
    fn a_foreign_loader_name_is_refused_under_the_bundled_pair() {
        let w = World::new();
        w.bundle(
            "libc.so.6",
            &shared_object_with_interp(&[(0, "GLIBC_2.32")], &[], Some("ld-linux-x86-64.so.2")),
        );
        w.host("libextra.so.1", &versioned(&["EXTRA_1"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["ld.so.1"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(refusal.reason.contains("nothing supplies"), "{refusal:?}");
    }

    /// A stray file holding the farm's path is replaced: the launch
    /// after it serves a real farm again instead of wedging forever.
    #[test]
    fn a_stray_file_at_the_farm_path_is_replaced() {
        let w = World::new();
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        fs::create_dir_all(&w.store).unwrap();

        w.resolve(HostLibsPolicy::Auto);
        let farm = w.last_farm.borrow().clone().expect("a farm");
        fs::remove_dir_all(&farm).unwrap();
        fs::write(&farm, b"stray").unwrap();

        let second = w.resolve(HostLibsPolicy::Auto);
        let rebuilt = second
            .farm
            .expect("the farm is rebuilt over the stray file");
        assert_eq!(rebuilt, farm);
        assert!(rebuilt.join("libdep.so.2").exists());
    }

    /// A module naming a filter object is refused: a filter loads its
    /// objects outside the closure, so nothing vouches for them.
    #[test]
    fn a_module_with_a_filter_object_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        // DT_FILTER is 0x7fffffff.
        fs::write(
            &module,
            fixture::facts_elf_with(true, 0, None, &[], &[], &[(0x7fffffff, 0)]).bytes,
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(refusal.reason.contains("filter"), "{refusal:?}");
    }

    /// A module whose requirement hash does not match its name cannot
    /// be read: the loader would reject the demand at launch, so
    /// admission refuses instead of presenting a module that fails.
    #[test]
    fn a_module_whose_requirement_hash_lies_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        let mut bytes = module_bytes(0, &["libdep.so.2"], &[("libdep.so.2", &[(0, "DEP_1.0")])]);
        let good = verdef::elf_hash("DEP_1.0").to_le_bytes();
        let at = bytes
            .windows(4)
            .position(|window| window == good)
            .expect("the fixture carries the requirement hash");
        bytes[at..at + 4].copy_from_slice(&(verdef::elf_hash("DEP_1.0") ^ 0x10).to_le_bytes());
        fs::write(&module, bytes).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(refusal.reason.contains("read"), "{refusal:?}");
        assert!(!decision.winners.contains_key("libdep.so.2"));
    }

    /// A cache entry that is a named pipe cannot hang admission: the
    /// walk finds nothing that supplies the dependency and refuses.
    #[test]
    fn a_fifo_cache_entry_is_refused_without_hanging() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        let dep = w.host_lib.join("libdep.so.2");
        fs::remove_file(&dep).unwrap();
        make_fifo(&dep);

        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(refusal.reason.contains("read"), "{refusal:?}");
        assert_eq!(refusal.object, dep);
        assert!(!decision.winners.contains_key("libdep.so.2"));
    }

    /// A loader source that is a named pipe cannot hang the close: the
    /// host pair is refused as unreadable and the bundled pair runs.
    #[test]
    fn a_loader_that_is_a_fifo_is_refused() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        fs::remove_file(&host_ld).unwrap();
        make_fifo(&host_ld);

        let decided = w.decide(HostLibsPolicy::Always);
        assert!(decided.host_interp.is_none());
        let (source, why) = decided.host_interp_refused.unwrap();
        assert_eq!(source, host_ld);
        assert!(why.contains("read"), "{why}");
    }

    /// A module needing a glibc family member the bundle lacks takes it
    /// from the host when the host's pair won: it joins the farm like
    /// the rest of its family, or the closed loader could not find it.
    #[test]
    fn a_module_takes_an_unbundled_family_soname_through_the_host_pair() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        w.host("libm.so.6", &versioned(&["GLIBC_2.2.5", "GLIBC_2.34"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libm.so.6"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decision.admitted, vec![module.clone()]);
        assert!(decision.refused.is_empty());
        assert_eq!(
            decision.winners.get("libm.so.6"),
            Some(&w.host_lib.join("libm.so.6"))
        );

        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(r.host_interp.is_some(), "the host's pair runs");
        assert_eq!(w.farm_link("libm.so.6"), Some(w.host_lib.join("libm.so.6")));
    }

    /// Under the bundled pair a module needing a family member the
    /// bundle lacks is refused, even though the host carries one:
    /// nothing outside the farm can be found, and the family is not
    /// farmed piecemeal.
    #[test]
    fn an_unbundled_family_soname_refuses_a_module_under_the_bundled_pair() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libm.so.6", &versioned(&["GLIBC_2.2.5"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libm.so.6"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert_eq!(refusal.object, module);
        assert!(refusal.reason.contains("libm.so.6"));
        assert!(!decision.winners.contains_key("libm.so.6"));
    }

    /// A soname the bundle carries satisfies a module and stays out of
    /// the decision, unlike a family member only the host's pair can
    /// supply, which joins the farm: the bundle keeps what it has.
    #[test]
    fn a_bundled_soname_satisfies_a_module_without_a_new_candidate() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.bundle("libown.so.1", &versioned(&["OWN_1.0"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libown.so.1"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert_eq!(decision.admitted, vec![module]);
        assert!(!decision.winners.contains_key("libown.so.1"));
        assert!(!decision.compared.iter().any(|(n, _)| n == "libown.so.1"));
    }

    /// A package without a bundled pair to check against admits
    /// nothing: every module is refused, sorted by path.
    #[test]
    fn a_package_without_a_bundled_pair_refuses_every_module() {
        let w = World::new();
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        fs::create_dir_all(w.host_lib.join("alsa-lib/xsub")).unwrap();
        let z = w.host_lib.join("alsa-lib/z.so");
        let a = w.host_lib.join("alsa-lib/xsub/a.so");
        fs::write(&z, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        fs::write(&a, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        w.carry(vec![
            PluginDir {
                relative: "lib/alsa-lib".to_string(),
                lib_relative: "alsa-lib".to_string(),
                absolutes: Vec::new(),
            },
            PluginDir {
                relative: "lib/alsa-lib/xsub".to_string(),
                lib_relative: "alsa-lib/xsub".to_string(),
                absolutes: Vec::new(),
            },
        ]);

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        let refused: Vec<PathBuf> = decision.refused.iter().map(|r| r.module.clone()).collect();
        assert_eq!(refused, vec![a, z]);
    }

    /// A module added to a counterpart directory between two decisions
    /// is noticed: the second decision considers it.
    #[test]
    fn a_module_added_between_launches_is_considered() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);

        assert!(w.decide(HostLibsPolicy::Auto).admitted.is_empty());
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();

        assert_eq!(w.decide(HostLibsPolicy::Auto).admitted, vec![module]);
    }

    /// A `never` policy considers nothing: no module is admitted,
    /// refused, or weighed, whatever the counterpart directories hold.
    #[test]
    fn a_never_policy_considers_no_module() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        fs::write(
            dir.join("libplug.so"),
            module_bytes(0, &["libdep.so.2"], &[]),
        )
        .unwrap();

        let d = w.decide(HostLibsPolicy::Never);
        assert!(d.compared.is_empty());
        assert!(d.winners.is_empty());
        assert!(d.admitted.is_empty());
        assert!(d.refused.is_empty());
    }

    /// A supplied family dependency is walked like any host library:
    /// what it needs in turn joins the farm, so the closed loader can
    /// load the whole chain.
    #[test]
    fn a_family_dependency_s_own_needs_reach_the_farm_too() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        w.host("libm.so.6", &versioned(&["GLIBC_2.2.5", "GLIBC_2.34"]));
        w.host("libmvec.so.1", &driver_needing(&["libm.so.6"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libmvec.so.1"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decision.admitted, vec![module]);
        assert!(decision.refused.is_empty());

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(
            w.farm_link("libmvec.so.1"),
            Some(w.host_lib.join("libmvec.so.1"))
        );
        assert_eq!(w.farm_link("libm.so.6"), Some(w.host_lib.join("libm.so.6")));
    }

    /// A module is weighed against the copy that would load: under the
    /// host's pair its newer libc defines a demand the bundled copy
    /// cannot, and the module is admitted.
    #[test]
    fn a_module_demands_what_the_pair_in_use_defines() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.34")])]),
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert_eq!(decision.admitted, vec![module]);
        assert!(decision.refused.is_empty());
    }

    /// A refusal names the object of the closure that failed, not just
    /// the module: a dependency whose facts cannot be read is named
    /// itself.
    #[test]
    fn a_refusal_names_the_dependency_that_failed() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", b"not elf");
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert_eq!(refusal.object, w.host_lib.join("libdep.so.2"));
        assert!(refusal.reason.contains("read"), "{}", refusal.reason);
    }

    /// Under the host's pair a family demand is weighed against the
    /// host's copy, the one that loads: a demand only the bundled copy
    /// defines is refused.
    #[test]
    fn a_family_demand_only_the_bundled_copy_defines_is_refused() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        w.host("libm.so.6", &versioned(&["GLIBC_2.2.5", "GLIBC_2.34"]));
        w.bundle("libm.so.6", &versioned(&["GLIBC_2.2.5", "GLIBC_2.99"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(0, &["libm.so.6"], &[("libm.so.6", &[(0, "GLIBC_2.99")])]),
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(refusal.reason.contains("GLIBC_2.99"), "{}", refusal.reason);
    }

    /// The farm links the host's family copy under its pair, so a host
    /// copy whose facts cannot be read supplies nothing, whatever the
    /// bundled copy would define.
    #[test]
    fn an_unreadable_host_family_copy_supplies_nothing() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        w.bundle("libm.so.6", &versioned(&["GLIBC_2.2.5", "GLIBC_2.99"]));
        w.host("libm.so.6", b"not elf");
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(0, &["libm.so.6"], &[("libm.so.6", &[(0, "GLIBC_2.99")])]),
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);

        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(
            refusal.reason.contains("nothing supplies"),
            "{}",
            refusal.reason
        );
    }

    /// Under the host's pair a bundled family soname loads from the
    /// host's copy, so that copy's closure is walked: what it needs
    /// reaches the farm, and a copy that fails a check refuses the
    /// module.
    #[test]
    fn a_bundled_family_soname_is_walked_through_its_host_copy() {
        let w = World::new();
        let host_ld = w.host("ld-linux-x86-64.so.2", &closable_loader());
        newer_host_glibc(&w, &host_ld);
        w.bundle("libm.so.6", &versioned(&["GLIBC_2.2.5"]));
        w.host("libm.so.6", &driver_needing(&["libhidden.so.1"]));
        let hidden = w.host("libhidden.so.1", &versioned(&["HID_1"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libm.so.6"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decision.admitted, vec![module.clone()]);
        assert!(decision.refused.is_empty());

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libhidden.so.1"), Some(hidden));

        // A selected copy that fails a check refuses the module.
        fs::write(w.host_lib.join("libm.so.6"), module_bytes(183, &[], &[])).unwrap();
        w.write_cache();
        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(
            refusal.reason.contains("architecture"),
            "{}",
            refusal.reason
        );
    }

    /// A refused module repaired in place, at the same size, is weighed
    /// again: the fingerprint stamps the counterpart files themselves,
    /// so the record cannot serve a stale refusal.
    #[test]
    fn a_repaired_module_is_weighed_again() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.99")])]),
        )
        .unwrap();

        let first = w.resolve(HostLibsPolicy::Auto);
        assert!(first.admitted.is_empty());

        // Same size, new demand: the repair is invisible to the
        // directory stamps.
        fs::write(
            &module,
            module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.32")])]),
        )
        .unwrap();
        std::fs::File::options()
            .write(true)
            .open(&module)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_001))
            .unwrap();

        assert_eq!(w.resolve(HostLibsPolicy::Auto).admitted, vec![module]);
    }

    /// Under `always` the decision compares the bundle's own sonames,
    /// so a module needing one walks the copy the decision won: what
    /// the winning copy demands reaches the farm.
    #[test]
    fn a_module_walks_the_copy_the_decision_won() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        w.host(
            "libfoo.so.1",
            &shared_object(&[(0, "FOO_1.0"), (0, "FOO_2.0")], &["libhelp.so.1"]),
        );
        let help = w.host("libhelp.so.1", &versioned(&["HELP_1"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libfoo.so.1"], &[])).unwrap();

        let decision = w.decide(HostLibsPolicy::Always);
        assert_eq!(decision.admitted, vec![module]);
        assert!(decision.refused.is_empty());

        w.resolve(HostLibsPolicy::Always);
        assert_eq!(w.farm_link("libhelp.so.1"), Some(help));
    }

    /// The same rule for versions: a demand is weighed against the copy
    /// the decision won, so a version only the winning host copy
    /// defines admits the module.
    #[test]
    fn a_module_weighs_the_copy_the_decision_won() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        w.host(
            "libfoo.so.1",
            &shared_object(&[(0, "FOO_1.0"), (0, "FOO_2.0")], &[]),
        );
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(0, &["libfoo.so.1"], &[("libfoo.so.1", &[(0, "FOO_2.0")])]),
        )
        .unwrap();

        let decision = w.decide(HostLibsPolicy::Always);

        assert_eq!(decision.admitted, vec![module]);
        assert!(decision.refused.is_empty());
    }

    /// Admissions and refusals come back sorted by path even when the
    /// counterpart directories' scan order differs, so the launch's
    /// view and the record's canonical order agree. A nested directory
    /// sorts before its parent's own modules under path ordering.
    #[test]
    fn a_decision_sorts_modules_across_counterparts() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        fs::create_dir_all(w.host_lib.join("alsa-lib/xsub")).unwrap();
        let z = w.host_lib.join("alsa-lib/z.so");
        let a = w.host_lib.join("alsa-lib/xsub/a.so");
        w.carry(vec![
            PluginDir {
                relative: "lib/alsa-lib".to_string(),
                lib_relative: "alsa-lib".to_string(),
                absolutes: Vec::new(),
            },
            PluginDir {
                relative: "lib/alsa-lib/xsub".to_string(),
                lib_relative: "alsa-lib/xsub".to_string(),
                absolutes: Vec::new(),
            },
        ]);

        fs::write(&z, module_bytes(0, &[], &[])).unwrap();
        fs::write(&a, module_bytes(0, &[], &[])).unwrap();
        let decision = w.decide(HostLibsPolicy::Auto);
        assert_eq!(decision.admitted, vec![a.clone(), z.clone()]);

        fs::write(&z, module_bytes(183, &[], &[])).unwrap();
        fs::write(&a, module_bytes(183, &[], &[])).unwrap();
        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refused: Vec<PathBuf> = decision.refused.iter().map(|r| r.module.clone()).collect();
        assert_eq!(refused, vec![a, z]);
    }

    /// A counterpart module rewritten beneath a restored modification
    /// time is weighed again: the stamp carries the change time, which
    /// a rewrite always moves and a tool cannot pin back.
    #[test]
    fn a_module_rewritten_beneath_a_restored_mtime_is_weighed_again() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(
            &module,
            module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.32")])]),
        )
        .unwrap();
        std::fs::File::options()
            .write(true)
            .open(&module)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_002))
            .unwrap();

        let first = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(first.admitted, vec![module.clone()]);

        // Same size, the modification time pinned back to the same
        // instant, a new demand: only the change time tells the rewrite
        // apart. The sleep keeps the two writes in different kernel
        // timestamp ticks, which coarse inode clocks otherwise share.
        std::thread::sleep(std::time::Duration::from_millis(20));
        fs::write(
            &module,
            module_bytes(0, &["libc.so.6"], &[("libc.so.6", &[(0, "GLIBC_2.99")])]),
        )
        .unwrap();
        std::fs::File::options()
            .write(true)
            .open(&module)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_002))
            .unwrap();

        let second = w.resolve(HostLibsPolicy::Auto);
        assert!(second.admitted.is_empty());
    }

    /// A record must also carry exactly one surviving stat for every
    /// winner it chose: a winner without its stat, a duplicated winner
    /// stat and a stat naming an undeclared winner are each a miss, or
    /// an in-place swap of an unvouched winner would be served.
    #[test]
    fn a_record_without_one_stat_per_winner_is_never_served() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        let host_dep = w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        let pins = [pin("gtk", 1)];
        let identity = SelectionIdentity::new(&pins).unwrap();
        let icd_dir = w.icd_dir.to_string_lossy().into_owned();
        let plugin_dirs = w.plugin_dirs.borrow();
        let fingerprint = fingerprint(
            &w.cache,
            &plugin_dirs,
            &[icd_dir.as_str()],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        w.resolve_selection(HostLibsPolicy::Auto, &[], &pins);
        let record = sel_dir(&w.store, &pins).join(DECISION_FILE);
        let good = fs::read_to_string(&record).unwrap();
        let winner_entry = format!("stat libdep.so.2\t{}", stat_line(&host_dep));
        let served = |text: &str| {
            fs::write(&record, text).unwrap();
            load_selection(
                sel_dir(&w.store, &pins).as_path(),
                &identity,
                &fingerprint,
                &w.store,
                w.loader_store(),
            )
        };
        let miss = |text: String, why: &str| {
            assert_eq!(served(&text), None, "{why}");
        };

        assert!(served(&good).is_some(), "the intact record is served");

        miss(
            good.replace(&format!("{winner_entry}\n"), ""),
            "a winner without its stat",
        );
        miss(
            good.replace(&winner_entry, &format!("{winner_entry}\n{winner_entry}")),
            "a duplicated winner stat",
        );
        miss(
            good.replace(
                &winner_entry,
                &format!("stat libown.so.9\t{}", stat_line(&host_dep)),
            ),
            "a stat naming an undeclared winner",
        );

        // A repeated `host` line, even one placed after the winner's
        // stat verified the first path, must not adopt the repeat: its
        // path was never statted, so the record would vouch a winner
        // nobody checked.
        let alt = w.store.join("alt-copy.so.2");
        fs::write(&alt, b"not the copy the host state vouches").unwrap();
        let forged = good.replace(
            &winner_entry,
            &format!("{winner_entry}\nhost libdep.so.2\t{}", alt.display()),
        );
        assert!(
            parse_decision(
                &forged,
                SELECTION_RECORD_HEADER,
                &identity.identity_lines(),
                &fingerprint
            )
            .is_none(),
            "a repeated host line moves the winner past its stat"
        );
    }

    /// A recorded decision carries its admissions: a launch served
    /// from the store sees the same modules a fresh decision admitted.
    #[test]
    fn the_record_carries_the_admitted_modules() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        fs::create_dir_all(&w.store).unwrap();

        let first = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(first.admitted, vec![module.clone()]);
        let served = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(served.admitted, vec![module], "served from the record");
        assert_eq!(served.farm, first.farm);
        assert_eq!(served.host_interp, first.host_interp);
    }

    /// A record with no winners at all serves too: a module admitted
    /// on bundled sonames alone round trips without a farm or a
    /// loader.
    #[test]
    fn a_record_without_winners_still_serves_its_admissions() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libextra.so.1", &versioned(&["EXTRA_1"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libc.so.6"], &[])).unwrap();
        fs::create_dir_all(&w.store).unwrap();

        let first = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(first.admitted, vec![module.clone()]);
        assert!(first.farm.is_none());
        let served = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(served.admitted, vec![module], "served from the record");
        assert_eq!(served.farm, None);
        assert!(served.host_interp.is_none());
    }

    /// A record must carry exactly one surviving stat for every module
    /// it admits: a duplicated module line, a module without its stat, a
    /// stat naming an undeclared module, a duplicated stat, a stat
    /// forged to `missing`, a record truncated before its admissions
    /// and a module that has since vanished are each a miss, while the
    /// intact record is served.
    #[test]
    fn a_record_without_one_stat_per_module_is_never_served() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        let pins = [pin("gtk", 1)];
        let identity = SelectionIdentity::new(&pins).unwrap();
        let icd_dir = w.icd_dir.to_string_lossy().into_owned();
        let plugin_dirs = w.plugin_dirs.borrow();
        let fingerprint = fingerprint(
            &w.cache,
            &plugin_dirs,
            &[icd_dir.as_str()],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        w.resolve_selection(HostLibsPolicy::Auto, &[], &pins);
        let record = sel_dir(&w.store, &pins).join(DECISION_FILE);
        let good = fs::read_to_string(&record).unwrap();
        let module_entry = format!("module {}", module.display());
        let stat_prefix = format!("module-stat {}\t", module.display());
        let served = |text: &str| {
            fs::write(&record, text).unwrap();
            load_selection(
                sel_dir(&w.store, &pins).as_path(),
                &identity,
                &fingerprint,
                &w.store,
                w.loader_store(),
            )
        };
        let miss = |text: String, why: &str| {
            assert_eq!(served(&text), None, "{why}");
        };

        assert!(served(&good).is_some(), "the intact record is served");

        let stat_at = good
            .find(&stat_prefix)
            .expect("the record stats the module");
        let stat_end = good[stat_at..].find('\n').unwrap() + stat_at;
        let recorded_stat = good[stat_at + stat_prefix.len()..stat_end].to_string();
        let stat_entry = format!("{stat_prefix}{recorded_stat}");

        miss(
            good.replace(&module_entry, &format!("{module_entry}\n{module_entry}")),
            "a duplicated module line",
        );
        miss(
            good.replace(&format!("{stat_entry}\n"), ""),
            "a module without its stat",
        );
        miss(
            good.replacen(
                &stat_entry,
                &format!(
                    "module-stat {}\t{}",
                    w.host_lib.join("libdep.so.2").display(),
                    stat_line(&w.host_lib.join("libdep.so.2"))
                ),
                1,
            ),
            "a stat naming an undeclared module",
        );
        miss(
            good.replace(&stat_entry, &format!("{stat_entry}\n{stat_entry}")),
            "a duplicated module stat",
        );
        miss(
            good.replace(&stat_entry, &format!("{stat_prefix}missing")),
            "a stat forged to missing",
        );
        miss(
            good[..good
                .find("admitted ")
                .expect("the record declares its count")]
                .to_string(),
            "a record truncated before its admissions",
        );

        fs::remove_file(&module).unwrap();
        miss(good.clone(), "a module that has since vanished");
    }

    #[test]
    fn the_decision_is_recorded_and_reused_until_the_host_changes() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        let first = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo.clone()));

        // Nothing changed: the recorded decision is reused as it stands.
        assert_eq!(w.resolve(HostLibsPolicy::Auto), first);
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo.clone()));

        // Rewriting the file in place changes neither the cache nor the
        // directory, but the decision records the winner's own stat, so
        // this is re-judged. The rewritten copy now matches the bundle's
        // version exactly, and the bundle keeps its own.
        fs::write(&host_foo, versioned(&["FOO_1.0"])).unwrap();
        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libfoo.so.1").is_none());

        // A new entry in the directory is a host change; the fresh
        // comparison agrees with what the re-judgement found.
        fs::write(w.host_lib.join("libnew.so.1"), versioned(&[])).unwrap();
        bump_mtime(&w.host_lib);
        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libfoo.so.1").is_none());
    }

    #[test]
    fn an_in_place_same_size_swap_is_not_served_stale() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo.clone()));

        // Same byte count, different versions: neither side's version set
        // contains the other's, so the recorded decision is void. The
        // directory stamps see nothing here. The mtime is pinned to a
        // distinct instant because two writes in one kernel timestamp
        // tick share ctime; a real swap always lands later.
        fs::write(&host_foo, versioned(&["FOO_0.9", "FOO_1.1"])).unwrap();
        std::fs::File::options()
            .write(true)
            .open(&host_foo)
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_000_000))
            .unwrap();
        w.resolve(HostLibsPolicy::Auto);
        assert!(
            w.farm_link("libfoo.so.1").is_none(),
            "a swapped winner must be re-judged, not served from the record"
        );
    }

    #[test]
    fn a_vanished_winner_recomputes_instead_of_failing() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        w.resolve(HostLibsPolicy::Auto);

        let before = fs::metadata(&w.host_lib).unwrap().modified().unwrap();
        fs::remove_file(&host_foo).unwrap();
        fs::File::open(&w.host_lib)
            .unwrap()
            .set_modified(before)
            .unwrap();
        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libfoo.so.1").is_none());
        assert!(w.farm_link("libGL.so.1").is_some());
    }

    #[test]
    fn library_path_is_read_from_icd_json() {
        assert_eq!(
            json_library_path(
                "{\n  \"file_format_version\": \"1.0.0\",\n  \"ICD\": {\n    \"library_path\": \"/usr/lib64/libvulkan_radeon.so\",\n    \"api_version\": \"1.3\"\n  }\n}"
            ),
            Some("/usr/lib64/libvulkan_radeon.so")
        );
        assert_eq!(
            json_library_path("{\"ICD\":{\"library_path\":\"libEGL_nvidia.so.0\"}}"),
            Some("libEGL_nvidia.so.0")
        );
        assert_eq!(json_library_path("{}"), None);
    }

    /// Move a directory's mtime well past "now", so a change made within
    /// the same filesystem timestamp granularity still reads as a change.
    fn bump_mtime(dir: &Path) {
        let later = std::time::SystemTime::now() + std::time::Duration::from_secs(5);
        fs::File::open(dir).unwrap().set_modified(later).unwrap();
    }

    /// An image that has `libGL.so.1` and not `libEGL.so.1`, which is
    /// what an X server pulls in, is not a stack for a package whose
    /// toolkit links `libEGL`. Counting it as one leaves the launch to
    /// fail on the missing half instead of fetching the pinned build.
    #[test]
    fn a_partial_host_stack_does_not_count() {
        let w = World::new();
        w.host("libGL.so.1", &shared_object(&[(1, "GL_1")], &[]));
        w.bundle(
            "libQtGui.so.6",
            &shared_object(&[(1, "Qt_6")], &["libEGL.so.1"]),
        );
        assert_eq!(
            w.gl(),
            Gl::Missing,
            "the host has no libEGL, which the bundle asks for"
        );
        w.host("libEGL.so.1", &shared_object(&[(1, "EGL_1")], &[]));
        assert_eq!(w.gl(), Gl::Host, "both halves present");
    }

    #[test]
    fn the_gl_situation_is_bundled_host_missing_or_absent() {
        let w = World::new();
        w.host("libz.so.1", &shared_object(&[(1, "Z_1")], &[]));
        assert_eq!(w.gl(), Gl::Absent, "no GL entry point anywhere");
        w.bundle(
            "libgtk-3.so.0",
            &shared_object(&[(1, "Gtk_3")], &["libepoxy.so.0"]),
        );
        assert_eq!(
            w.gl(),
            Gl::Absent,
            "a toolkit that opens GL by name links none"
        );
        w.bundle(
            "libQtGui.so.6",
            &shared_object(&[(1, "Qt_6")], &["libEGL.so.1"]),
        );
        assert_eq!(w.gl(), Gl::Missing, "the bundle links libEGL");
        w.host("libEGL.so.1", &shared_object(&[(1, "EGL_1")], &[]));
        assert_eq!(w.gl(), Gl::Host);
        w.bundle("libGL.so.1", &shared_object(&[(1, "GL_1")], &[]));
        assert_eq!(w.gl(), Gl::Bundled, "the bundle's own stack comes first");
    }

    #[test]
    fn the_launched_binary_names_gl_like_a_library() {
        let w = World::new();
        w.host("libz.so.1", &shared_object(&[(1, "Z_1")], &[]));
        let app = w.pkg.join("bin/app");
        fs::create_dir_all(app.parent().unwrap()).unwrap();
        fs::write(&app, shared_object(&[(1, "App_1")], &["libGL.so.1"])).unwrap();
        assert_eq!(
            gl_situation(&w.pkg, &["lib"], &[], None, &w.cache, &[]),
            Gl::Absent,
            "a binary the launch does not run asks for nothing"
        );
        assert_eq!(
            gl_situation(&w.pkg, &["lib"], &[], Some(&app), &w.cache, &[]),
            Gl::Missing,
            "the launched binary links libGL"
        );
    }

    #[test]
    fn an_extra_root_supplies_what_the_host_lacks() {
        let w = World::new();
        w.host("libz.so.1", &shared_object(&[(1, "Z_1")], &[]));
        w.bundle("libc.so.6", &shared_object(&[(2, "GLIBC_2.30")], &[]));
        let root = temp_dir("glbuild");
        fs::create_dir_all(root.join("lib")).unwrap();
        fs::create_dir_all(root.join("share/vulkan/icd.d")).unwrap();
        let gl = root.join("lib/libGL.so.1");
        fs::write(&gl, shared_object(&[(1, "GL_1")], &["libdrm.so.2"])).unwrap();
        let drm = root.join("lib/libdrm.so.2");
        fs::write(&drm, shared_object(&[(1, "DRM_1")], &[])).unwrap();
        let icd = root.join("lib/libvulkan_fake.so");
        fs::write(&icd, shared_object(&[(1, "ICD_1")], &[])).unwrap();
        let vendor = root.join("lib/libEGL_fake.so.0");
        fs::write(&vendor, shared_object(&[(1, "VENDOR_1")], &["libdrm.so.2"])).unwrap();
        fs::write(
            root.join("share/vulkan/icd.d/fake.json"),
            format!("{{\"ICD\": {{\"library_path\": \"{}\"}}}}", icd.display()),
        )
        .unwrap();

        let without = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(without.farm, None, "nothing to take from a host without GL");

        let with = w.resolve_with(HostLibsPolicy::Auto, Some(&root));
        assert_eq!(w.farm_link("libGL.so.1"), Some(gl));
        assert_eq!(
            w.farm_link("libdrm.so.2"),
            Some(drm),
            "the build's closure follows"
        );
        assert_eq!(
            w.farm_link("libvulkan_fake.so"),
            Some(icd),
            "ICD files seed the walk"
        );
        assert_eq!(
            w.farm_link("libEGL_fake.so.0"),
            Some(vendor),
            "a vendor library nothing names is farmed by its name"
        );
        assert!(with.farm.is_some());
        assert_eq!(
            with.host_interp, None,
            "the extra root never supplies a loader"
        );

        let _ = fs::remove_dir_all(&root);
    }

    #[test]
    fn a_host_change_leaves_the_old_farm_for_running_instances() {
        let w = World::new();
        let host_gl = w.host("libGL.so.1", &versioned(&["GL_1.0", "GL_2.0"]));
        w.bundle("libGL.so.1", &versioned(&["GL_1.0"]));
        w.resolve(HostLibsPolicy::Auto);
        let old_farm = w.last_farm.borrow().clone().expect("a farm");
        assert_eq!(
            fs::read_link(old_farm.join("libGL.so.1")).ok(),
            Some(host_gl)
        );

        // The host changes, so the next launch decides afresh. An instance
        // started before is still loading through the old farm.
        w.host("libnew.so.1", &versioned(&[]));
        w.resolve(HostLibsPolicy::Auto);
        let new_farm = w.last_farm.borrow().clone().expect("a farm");
        assert_ne!(old_farm, new_farm);
        assert!(
            old_farm.join("libGL.so.1").exists(),
            "the old farm was torn down under a running instance"
        );
        assert!(new_farm.join("libGL.so.1").exists());
    }

    /// A farm left behind by an earlier launch is reused only when its
    /// links still name exactly the chosen copies: a relinked or extra
    /// entry in it is rebuilt over, never handed to a launch.
    #[test]
    fn a_damaged_or_grown_farm_is_rebuilt_for_the_next_launch() {
        let w = World::new();
        let host_dep = w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        fs::create_dir_all(&w.store).unwrap();

        w.resolve(HostLibsPolicy::Auto);
        let farm = w.last_farm.borrow().clone().expect("a farm");
        let planted = w.store.join("planted.so.1");
        fs::write(&planted, b"planted").unwrap();

        // A winner link bent to another file is rebuilt.
        let link = farm.join("libdep.so.2");
        fs::remove_file(&link).unwrap();
        std::os::unix::fs::symlink(&planted, &link).unwrap();
        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(
            fs::read_link(&link).ok(),
            Some(host_dep),
            "the tampered link is re-aimed at the chosen copy"
        );

        // An entry beyond the winners is dropped with the rebuild.
        std::os::unix::fs::symlink(&planted, farm.join("extra.so.9")).unwrap();
        w.resolve(HostLibsPolicy::Auto);
        assert!(
            farm.join("extra.so.9").symlink_metadata().is_err(),
            "the extra farm entry does not survive the rebuild"
        );
    }

    #[test]
    fn a_failed_materialize_is_not_recorded() {
        use std::os::unix::fs::PermissionsExt;

        let w = World::new();
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        fs::create_dir_all(&w.store).unwrap();

        w.resolve(HostLibsPolicy::Auto);
        let farm = w.last_farm.borrow().clone().expect("a farm");
        let record = sel_dir(&w.store, &[]).join("decision");
        let before = fs::metadata(&record).unwrap().modified().unwrap();

        // A farm directory nobody can empty wedges the rebuild, and the
        // failed materialize must not overwrite the served record.
        fs::remove_dir_all(&farm).unwrap();
        fs::create_dir(&farm).unwrap();
        fs::write(farm.join("stuck.so"), b"stuck").unwrap();
        fs::set_permissions(&farm, fs::Permissions::from_mode(0o500)).unwrap();

        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(r.farm.is_none(), "the wedged farm is not served");
        let after = fs::metadata(&record).unwrap().modified().unwrap();
        assert_eq!(
            before, after,
            "the failed materialize leaves the recorded decision alone"
        );
        assert_eq!(
            fs::read_link(farm.join("libdep.so.2")).ok(),
            None,
            "the wedged occupant is left for the next launch to retry"
        );
    }

    #[test]
    fn a_view_links_bundled_members_then_admitted_host_ones() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let counterpart = carry_counterpart(&w);
        let module = counterpart.join("libtype.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        let shadowed = counterpart.join("libcarried.so");
        fs::write(&shadowed, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        let bundled = w.pkg.join("lib/alsa-lib");
        fs::create_dir_all(&bundled).unwrap();
        let carried = bundled.join("libcarried.so");
        fs::write(&carried, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        fs::write(bundled.join("plugin.conf"), b"config").unwrap();
        fs::create_dir_all(&w.store).unwrap();

        let r = w.resolve(HostLibsPolicy::Auto);

        assert_eq!(r.plugin_views.len(), 1, "one view, for the carried record");
        let view = &r.plugin_views[0];
        assert_eq!(view.relative, "lib/alsa-lib");
        let entries: BTreeMap<String, PathBuf> = fs::read_dir(&view.dir)
            .unwrap()
            .flatten()
            .map(|e| (e.file_name().to_string_lossy().into_owned(), e.path()))
            .collect();
        assert_eq!(
            entries.len(),
            3,
            "two bundled members and the one host member the package lacks"
        );
        assert_eq!(
            fs::read_link(view.dir.join("libcarried.so")).ok(),
            Some(carried),
            "a name the package carries links the package's own copy"
        );
        assert_eq!(
            fs::read_link(view.dir.join("plugin.conf")).ok(),
            Some(bundled.join("plugin.conf")),
            "bundled data members ride along"
        );
        assert_eq!(
            fs::read_link(view.dir.join("libtype.so")).ok(),
            Some(module),
            "the host member the package lacks links the host's file"
        );
    }

    #[test]
    fn a_damaged_view_is_rebuilt_for_the_next_launch() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let counterpart = carry_counterpart(&w);
        let module = counterpart.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        let bundled = w.pkg.join("lib/alsa-lib");
        fs::create_dir_all(&bundled).unwrap();
        fs::write(bundled.join("libcarried.so"), b"bundled").unwrap();
        fs::create_dir_all(&w.store).unwrap();

        let r = w.resolve(HostLibsPolicy::Auto);
        let view = r.plugin_views[0].dir.clone();
        let planted = w.store.join("planted.so");
        fs::write(&planted, b"planted").unwrap();
        let link = view.join("libplug.so");
        fs::remove_file(&link).unwrap();
        std::os::unix::fs::symlink(&planted, &link).unwrap();
        std::os::unix::fs::symlink(&planted, view.join("extra.so")).unwrap();

        let r = w.resolve(HostLibsPolicy::Auto);
        assert_eq!(r.plugin_views.len(), 1);
        let view = r.plugin_views[0].dir.clone();
        let mut names: Vec<String> = fs::read_dir(&view)
            .unwrap()
            .flatten()
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .collect();
        names.sort();
        assert_eq!(names, vec!["libcarried.so", "libplug.so"]);
        assert_eq!(
            fs::read_link(view.join("libplug.so")).ok(),
            Some(module),
            "the bent link is re-aimed at the host's module"
        );
        assert_eq!(
            fs::read_link(view.join("libcarried.so")).ok(),
            Some(bundled.join("libcarried.so"))
        );
    }

    #[test]
    fn a_served_record_also_presents_its_views() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let counterpart = carry_counterpart(&w);
        let module = counterpart.join("libplug.so");
        fs::write(&module, module_bytes(0, &["libdep.so.2"], &[])).unwrap();
        let bundled = w.pkg.join("lib/alsa-lib");
        fs::create_dir_all(&bundled).unwrap();
        fs::write(bundled.join("libcarried.so"), b"bundled").unwrap();
        fs::create_dir_all(&w.store).unwrap();

        let first = w.resolve(HostLibsPolicy::Auto);
        let record = sel_dir(&w.store, &[]).join("decision");
        let before = fs::metadata(&record).unwrap().modified().unwrap();

        let second = w.resolve(HostLibsPolicy::Auto);
        let after = fs::metadata(&record).unwrap().modified().unwrap();
        assert_eq!(before, after, "the second launch is served by the record");
        assert_eq!(second.plugin_views, first.plugin_views);
        assert_eq!(
            fs::read_link(second.plugin_views[0].dir.join("libplug.so")).ok(),
            Some(module)
        );
    }

    #[test]
    fn a_launch_without_admitted_modules_builds_no_view() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        carry_counterpart(&w);
        let bundled = w.pkg.join("lib/alsa-lib");
        fs::create_dir_all(&bundled).unwrap();
        fs::write(bundled.join("libcarried.so"), b"bundled").unwrap();
        fs::create_dir_all(&w.store).unwrap();

        let r = w.resolve(HostLibsPolicy::Auto);
        assert!(r.plugin_views.is_empty());
        let strangers: Vec<String> = fs::read_dir(&w.store)
            .unwrap()
            .flatten()
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|name| name.starts_with("view-"))
            .collect();
        assert!(
            strangers.is_empty(),
            "no view root is built without admitted members"
        );
    }

    /// A file that is only an ELF header has nothing the loader can
    /// map, so admission refuses it instead of presenting a module that
    /// fails at launch.
    #[test]
    fn a_header_only_module_is_refused() {
        let w = World::new();
        w.bundle("libc.so.6", &versioned(&["GLIBC_2.32"]));
        w.host("libdep.so.2", &versioned(&["DEP_1.0"]));
        let dir = carry_counterpart(&w);
        let module = dir.join("libhead.so");
        let mut head = vec![0u8; 64];
        head[0..4].copy_from_slice(b"\x7fELF");
        head[4] = 2;
        head[5] = 1;
        head[16..18].copy_from_slice(&3u16.to_le_bytes());
        fs::write(&module, head).unwrap();

        let decision = w.decide(HostLibsPolicy::Auto);
        assert!(decision.admitted.is_empty());
        let refusal = refusal_for(&decision.refused, &module);
        assert!(refusal.reason.contains("map"), "{refusal:?}");
    }

    #[test]
    fn an_icd_naming_its_driver_by_bare_soname_seeds_the_walk() {
        let w = World::new();
        // NVIDIA's files name the vendor library by soname alone, and no
        // driver family prefix matches `libGLX_nvidia`.
        w.host(
            "libGLX_nvidia.so.0",
            &driver_needing(&["libnvidia-glcore.so.1"]),
        );
        let host_glcore = w.host("libnvidia-glcore.so.1", &versioned(&[]));
        fs::write(
            w.icd_dir.join("nvidia_icd.json"),
            r#"{ "ICD": { "library_path": "libGLX_nvidia.so.0" } }"#,
        )
        .unwrap();
        w.bundle("libapp.so.1", &versioned(&[]));

        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libGLX_nvidia.so.0").is_some());
        assert_eq!(w.farm_link("libnvidia-glcore.so.1"), Some(host_glcore));
    }

    #[test]
    fn a_glx_vendor_library_nothing_names_is_farmed_with_its_needs() {
        let w = World::new();
        // glvnd builds the vendor's soname from the X server's vendor
        // string, so no ICD or EGL vendor file names Mesa's copy and no
        // driver family prefix matches it.
        let host_mesa = w.host("libGLX_mesa.so.0", &driver_needing(&["libxcb-glx.so.0"]));
        let host_xcb_glx = w.host("libxcb-glx.so.0", &versioned(&[]));
        w.bundle("libapp.so.1", &versioned(&[]));

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libGLX_mesa.so.0"), Some(host_mesa));
        assert_eq!(w.farm_link("libxcb-glx.so.0"), Some(host_xcb_glx));
    }

    #[test]
    fn the_glx_indirect_fallback_is_farmed() {
        let w = World::new();
        let host_indirect = w.host("libGLX_indirect.so.0", &versioned(&[]));
        w.bundle("libapp.so.1", &versioned(&[]));

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libGLX_indirect.so.0"), Some(host_indirect));
    }

    #[test]
    fn a_bundled_glx_vendor_stays_bundled() {
        let w = World::new();
        // Neither copy defines symbol versions, so the sets are equal and
        // the package ships its own driver choice.
        w.host("libGLX_mesa.so.0", &versioned(&[]));
        w.bundle("libGLX_mesa.so.0", &versioned(&[]));

        let icd_dir = w.icd_dir.to_string_lossy().into_owned();
        let req = Request {
            pkg_root: &w.pkg,
            lib_dirs: &["lib"],
            plugin_dirs: &[],
            policy: HostLibsPolicy::Auto,
            store: &w.store,
            ld_cache: &w.cache,
            icd_dirs: &[icd_dir.as_str()],
            extra_root: None,
            set_roots: &[],
            host_libc: true,
            loader_store: w.loader_store(),
            nsswitch: &w.nsswitch,
            uid: w.uid,
            trust: None,
        };
        let d = decide(&req);
        assert_eq!(
            d.compared,
            vec![("libGLX_mesa.so.0".to_string(), Some(Choice::Bundle))]
        );
        assert!(d.winners.is_empty());

        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libGLX_mesa.so.0").is_none());
    }

    #[test]
    fn a_host_without_glx_vendors_is_unaffected() {
        let w = World::new();
        let host_gl = w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));

        let closure = HostIndex::load(&w.cache, &[], None).driver_closure();
        assert!(!closure.iter().any(|n| n.starts_with("libGLX_")));

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libGL.so.1"), Some(host_gl));
        assert!(w.farm_link("libfoo.so.1").is_some());
    }

    #[test]
    fn a_dri_module_brings_what_it_needs() {
        let w = World::new();
        let host_llvm = w.host("libLLVM-15.so.1", &versioned(&[]));
        w.host_module("swrast_dri.so", &driver_needing(&["libLLVM-15.so.1"]));
        w.bundle("libapp.so.1", &versioned(&[]));

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libLLVM-15.so.1"), Some(host_llvm));
        assert!(w.farm_link("swrast_dri.so").is_none());
    }

    #[test]
    fn an_unreadable_module_is_ignored() {
        let w = World::new();
        let host_llvm = w.host("libLLVM-15.so.1", &versioned(&[]));
        w.host_module("broken_dri.so", b"not an ELF object");
        w.host_module("swrast_dri.so", &driver_needing(&["libLLVM-15.so.1"]));
        w.bundle("libapp.so.1", &versioned(&[]));

        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libLLVM-15.so.1"), Some(host_llvm));
    }

    #[test]
    fn a_new_module_resolves_afresh() {
        let w = World::new();
        w.host("libLLVM-15.so.1", &versioned(&[]));
        let host_sensors = w.host("libsensors.so.5", &versioned(&[]));
        w.host_module("swrast_dri.so", &driver_needing(&["libLLVM-15.so.1"]));
        w.bundle("libapp.so.1", &versioned(&[]));
        w.resolve(HostLibsPolicy::Auto);
        assert!(w.farm_link("libsensors.so.5").is_none());

        w.host_module("radeonsi_dri.so", &driver_needing(&["libsensors.so.5"]));
        // Set explicitly: a directory's mtime can be coarser than the
        // time between the two writes.
        fs::File::open(w.host_lib.join("dri"))
            .unwrap()
            .set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(1))
            .unwrap();
        w.resolve(HostLibsPolicy::Auto);
        assert_eq!(w.farm_link("libsensors.so.5"), Some(host_sensors));
    }

    #[test]
    fn equivalent_selection_lists_share_one_identity() {
        let (gtk, qt) = (pin("gtk", 1), pin("qt", 2));
        let one = SelectionIdentity::new(&[gtk.clone(), qt.clone()]).unwrap();
        let two = SelectionIdentity::new(&[qt.clone(), gtk.clone()]).unwrap();
        assert_eq!(one, two);
        assert_eq!(one.canonical(), two.canonical());
        assert_eq!(one.key(), two.key());

        // A hash written in the other case names the same build.
        let mut upper = pin("qt", 2);
        upper.hash = upper.hash.to_ascii_uppercase();
        assert_eq!(one, SelectionIdentity::new(&[gtk, upper]).unwrap());
    }

    #[test]
    fn a_changed_subset_or_pin_changes_the_identity() {
        let gtk = pin("gtk", 1);
        let qt = pin("qt", 2);
        let gtk_repinned = pin("gtk", 3);
        let gtk_renamed = pin("toolkit", 1);

        let full = SelectionIdentity::new(&[gtk.clone(), qt.clone()]).unwrap();
        let subset = SelectionIdentity::new(std::slice::from_ref(&gtk)).unwrap();
        let empty = SelectionIdentity::new(&[]).unwrap();
        let repinned = SelectionIdentity::new(&[gtk_repinned]).unwrap();
        assert_ne!(full, subset, "a dropped set is another selection");
        assert_ne!(subset, empty, "an empty selection is its own");
        assert_ne!(subset, repinned, "a republished pin is another selection");
        assert_ne!(
            subset,
            SelectionIdentity::new(&[gtk_renamed]).unwrap(),
            "a renamed declaration is another selection"
        );
        assert_ne!(full.key(), subset.key());
        assert_ne!(subset.key(), empty.key());

        // The key is a bounded lowercase hex name.
        let key = full.key();
        assert_eq!(key.len(), 32);
        assert!(key.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')));
    }

    #[test]
    fn duplicate_pins_collapse_and_conflicting_ones_do_not() {
        let (gtk_a, gtk_b) = (pin("gtk", 1), pin("gtk", 2));
        let twice = SelectionIdentity::new(&[gtk_a.clone(), gtk_a.clone()]).unwrap();
        assert_eq!(
            twice,
            SelectionIdentity::new(std::slice::from_ref(&gtk_a)).unwrap()
        );

        // The same name pinned two ways stays both pins, in a stable
        // order, and never shares an identity with either alone.
        let both = SelectionIdentity::new(&[gtk_a.clone(), gtk_b.clone()]).unwrap();
        assert_ne!(both, twice);
        assert_eq!(
            both,
            SelectionIdentity::new(&[gtk_b.clone(), gtk_a.clone()]).unwrap()
        );
        assert_eq!(both.pins(), &[gtk_a, gtk_b]);
    }

    #[test]
    fn a_pin_outside_the_bounds_refuses_an_identity() {
        let ok = pin("gtk", 1);
        let refused = |pins: &[ActivePin]| SelectionIdentity::new(pins).is_none();
        let with_label = |label: &str| ActivePin {
            label: label.to_string(),
            hash: ok.hash.clone(),
        };
        let with_hash = |hash: String| ActivePin {
            label: "gtk".to_string(),
            hash,
        };

        assert!(refused(&[ActivePin {
            label: String::new(),
            hash: ok.hash.clone()
        }]));
        assert!(refused(&[with_label(&"g".repeat(129))]));
        for label in ["a\tb", "a\nb", "a\rb", "a\u{1}b", "a\u{7f}b", "a\u{0}b"] {
            assert!(refused(&[with_label(label)]), "name {label:?} is refused");
        }
        for hash in [
            String::new(),
            "ab".repeat(31),
            "ab".repeat(33),
            "zz".repeat(32),
        ] {
            assert!(refused(&[with_hash(hash)]), "hash is refused");
        }

        // Exactly the pin-count bound is fine, one past it is not.
        let at_bound: Vec<ActivePin> = (0..MAX_ACTIVE_PINS as u32)
            .map(|i| pin(&format!("s{i}"), 1))
            .collect();
        assert!(SelectionIdentity::new(&at_bound).is_some());
        let mut past_bound = at_bound.clone();
        past_bound.push(pin("extra", 1));
        assert!(refused(&past_bound));
    }

    #[test]
    fn a_selection_directory_refuses_what_it_did_not_mint() {
        assert_eq!(selection_dir(""), None);
        assert_eq!(selection_dir("../escape"), None);
        assert_eq!(selection_dir("a/b"), None);
        assert_eq!(selection_dir("ABC"), None);
        assert_eq!(selection_dir("gg"), None);
        assert_eq!(selection_dir(&"a".repeat(65)), None);
        assert_eq!(
            selection_dir(&"a".repeat(32)),
            Some(format!("sel-{}", "a".repeat(32)))
        );
    }

    /// Two launches naming the same active declarations in opposite list
    /// orders share one record: the second is served from it rather than
    /// recomputing, seen in the record going untouched while the farm it
    /// names still serves both.
    #[test]
    fn selection_list_order_does_not_invalidate_the_record() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        let (gtk, qt) = (pin("gtk", 1), pin("qt", 2));

        let first = w.resolve_selection(HostLibsPolicy::Auto, &[], &[gtk.clone(), qt.clone()]);
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo.clone()));
        let record = sel_dir(&w.store, &[gtk.clone(), qt.clone()]).join(DECISION_FILE);
        assert!(record.is_file());

        let stamped = backdate(&record);
        let second = w.resolve_selection(HostLibsPolicy::Auto, &[], &[qt, gtk]);
        assert_eq!(second, first);
        assert_eq!(
            fs::metadata(&record).unwrap().modified().unwrap(),
            stamped,
            "an equivalent launch reuses the record instead of rewriting it"
        );
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo));
    }

    /// A changed subset or a republished pin is another library universe:
    /// its record is separate, and the launch decides against only its
    /// own roots instead of being served the other selection's decision.
    #[test]
    fn a_changed_subset_or_pin_recomputes_in_its_own_universe() {
        let w = World::new();
        w.host(
            "libGL.so.1",
            &driver_needing(&["libfoo.so.1", "libbar.so.1"]),
        );
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        let host_bar = w.host("libbar.so.1", &versioned(&["BAR_1.0"]));
        w.bundle("libapp.so.1", &versioned(&[]));

        let root = temp_dir("sel-universe");
        let one = set_lib(&root.join("one"), "libfoo.so.1", &versioned(&["FOO_1.0"]));
        let two = set_lib(
            &root.join("two"),
            "libbar.so.1",
            &versioned(&["BAR_1.0", "BAR_2.0"]),
        );
        let (gtk, qt) = (pin("gtk", 1), pin("qt", 2));

        w.resolve_selection(
            HostLibsPolicy::Auto,
            &[one.clone(), two],
            &[gtk.clone(), qt.clone()],
        );
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo.clone()));
        assert!(
            w.farm_link("libbar.so.1").is_none(),
            "the set's newer bar stays bundled"
        );
        let toolkit_record = sel_dir(&w.store, &[gtk.clone(), qt.clone()]).join(DECISION_FILE);
        assert!(toolkit_record.is_file());

        // Dropping the set whose library won is another selection: the
        // host's older bar is now the only bar, and the toolkit's
        // decision must not stand in for it.
        let stamped = backdate(&toolkit_record);
        w.resolve_selection(HostLibsPolicy::Auto, &[one], std::slice::from_ref(&gtk));
        assert_eq!(w.farm_link("libbar.so.1"), Some(host_bar.clone()));
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo.clone()));
        let gtk_record = sel_dir(&w.store, std::slice::from_ref(&gtk)).join(DECISION_FILE);
        assert!(gtk_record.is_file());
        assert_ne!(
            gtk_record.parent(),
            toolkit_record.parent(),
            "another selection records beside, not over"
        );
        assert_eq!(
            fs::metadata(&toolkit_record).unwrap().modified().unwrap(),
            stamped,
            "the other selection's record is kept, not edited"
        );

        // The same declaration republished under a new pin is another
        // selection again: this build's foo matches the host's, so it
        // stays bundled where the older pin's foo was farmed.
        let gtk2 = pin("gtk", 3);
        let one2 = set_lib(
            &root.join("one-2"),
            "libfoo.so.1",
            &versioned(&["FOO_1.0", "FOO_1.1"]),
        );
        w.resolve_selection(HostLibsPolicy::Auto, &[one2], std::slice::from_ref(&gtk2));
        assert!(
            w.farm_link("libfoo.so.1").is_none(),
            "the repinned set's foo stays bundled"
        );
        assert!(sel_dir(&w.store, &[gtk2]).join(DECISION_FILE).is_file());
        let _ = fs::remove_dir_all(&root);
    }

    /// A launch with no sets active shares the host with a toolkit launch
    /// but not its universe: the host's own bar is the only bar, where
    /// the toolkit's decision kept the set's newer one.
    #[test]
    fn an_empty_selection_is_its_own_universe() {
        let w = World::new();
        w.host(
            "libGL.so.1",
            &driver_needing(&["libfoo.so.1", "libbar.so.1"]),
        );
        let host_bar = w.host("libbar.so.1", &versioned(&["BAR_1.0"]));
        w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libapp.so.1", &versioned(&[]));

        let root = temp_dir("sel-empty");
        let two = set_lib(
            &root.join("two"),
            "libbar.so.1",
            &versioned(&["BAR_1.0", "BAR_2.0"]),
        );

        let toolkit = w.resolve_selection(HostLibsPolicy::Auto, &[two], &[pin("qt", 2)]);
        assert!(toolkit.farm.is_some());
        assert!(w.farm_link("libbar.so.1").is_none());

        let bare = w.resolve_selection(HostLibsPolicy::Auto, &[], &[]);
        assert!(bare.farm.is_some());
        assert_eq!(w.farm_link("libbar.so.1"), Some(host_bar));
        assert!(sel_dir(&w.store, &[]).join(DECISION_FILE).is_file());
        let _ = fs::remove_dir_all(&root);
    }

    /// The record names its identity in full, so a reader pointed at one
    /// selection's directory with another selection's identity, or with
    /// another host state, still refuses it.
    #[test]
    fn a_selection_record_is_bound_to_its_identity_and_host() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        let pins = [pin("gtk", 1)];
        let identity = SelectionIdentity::new(&pins).unwrap();
        let icd_dir = w.icd_dir.to_string_lossy().into_owned();
        let set_roots: Vec<PathBuf> = Vec::new();
        let fingerprint = fingerprint(
            &w.cache,
            &[],
            &[icd_dir.as_str()],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );

        let decided = w.resolve_selection(HostLibsPolicy::Auto, &set_roots, &pins);
        assert_eq!(w.farm_link("libfoo.so.1"), Some(host_foo.clone()));
        let dir = sel_dir(&w.store, &pins);

        let again =
            load_selection(&dir, &identity, &fingerprint, &w.store, w.loader_store()).unwrap();
        assert_eq!(again.farm.as_ref(), decided.farm.as_ref());

        let other = SelectionIdentity::new(&[pin("qt", 2)]).unwrap();
        assert_eq!(
            load_selection(&dir, &other, &fingerprint, &w.store, w.loader_store()),
            None
        );
        assert_eq!(
            load_selection(&dir, &identity, "another host", &w.store, w.loader_store()),
            None
        );
    }

    /// Hostile or damaged state is a miss with nothing adopted: another
    /// version's header, truncation, bytes that are not a record, a
    /// tampered identity line and an oversized file all recompute.
    #[test]
    fn hostile_selection_records_are_misses() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        let pins = [pin("gtk", 1)];
        let identity = SelectionIdentity::new(&pins).unwrap();
        let icd_dir = w.icd_dir.to_string_lossy().into_owned();
        let set_roots: Vec<PathBuf> = Vec::new();
        let fingerprint = fingerprint(
            &w.cache,
            &[],
            &[icd_dir.as_str()],
            HostLibsPolicy::Auto,
            None,
            true,
            &w.nsswitch,
            w.loader_store(),
        );
        w.resolve_selection(HostLibsPolicy::Auto, &set_roots, &pins);
        let dir = sel_dir(&w.store, &pins);
        let record = dir.join(DECISION_FILE);
        let good = fs::read_to_string(&record).unwrap();
        let miss = |bytes: &[u8]| {
            fs::write(&record, bytes).unwrap();
            assert_eq!(
                load_selection(&dir, &identity, &fingerprint, &w.store, w.loader_store()),
                None
            );
        };

        miss(b"");
        miss(b"\xff\xfe not utf-8");
        miss(b"onelf-resolve 2\nfingerprint x\n");
        miss(b"onelf-resolve-selection 2\nfingerprint x\n");
        miss(b"onelf-resolve-selection 1\n");
        miss(&good.as_bytes()[..good.len() / 2]);
        miss(
            good.replacen(identity.canonical(), "pin 3:gtk\tzz", 1)
                .as_bytes(),
        );
        let mut junk = good.clone();
        junk.push_str("not-a-record-line\n");
        miss(junk.as_bytes());
        let oversized = vec![b'x'; (RECORD_MAX_BYTES + 1) as usize];
        miss(&oversized);

        fs::write(&record, &good).unwrap();
        assert!(
            load_selection(&dir, &identity, &fingerprint, &w.store, w.loader_store()).is_some(),
            "the intact record is still a hit"
        );
    }

    /// The record replaces any earlier one through a temp file and a
    /// rename, so a directory never holds a half-written decision and no
    /// scratch file outlives the write.
    #[test]
    fn recording_replaces_the_record_atomically() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        w.host("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0"]));
        let pins = [pin("gtk", 1)];
        let names = |dir: &Path| {
            let mut out: Vec<String> = fs::read_dir(dir)
                .unwrap()
                .flatten()
                .map(|e| e.file_name().to_string_lossy().into_owned())
                .collect();
            out.sort();
            out
        };

        w.resolve_selection(HostLibsPolicy::Auto, &[], &pins);
        let dir = sel_dir(&w.store, &pins);
        assert_eq!(names(&dir), ["decision"]);

        // A record that fails its checks is rewritten in place, and the
        // rewrite again leaves only the decision behind.
        fs::write(dir.join(DECISION_FILE), b"").unwrap();
        w.resolve_selection(HostLibsPolicy::Auto, &[], &pins);
        assert_eq!(names(&dir), ["decision"]);
    }

    /// A record a release keyed on the host alone left at the store's top
    /// is never read: its winner is not served, and the launch decides
    /// for its own selection.
    #[test]
    fn a_host_only_record_is_never_served() {
        let w = World::new();
        w.host("libGL.so.1", &driver_needing(&["libfoo.so.1"]));
        let host_foo = w.host("libfoo.so.1", &versioned(&["FOO_1.0"]));
        w.bundle("libfoo.so.1", &versioned(&["FOO_1.0", "FOO_1.1"]));
        fs::create_dir_all(&w.store).unwrap();
        fs::write(
            w.store.join(DECISION_FILE),
            format!(
                "onelf-resolve 2\nfingerprint x\nhost libfoo.so.1\t{}\n",
                host_foo.display()
            ),
        )
        .unwrap();

        w.resolve_selection(HostLibsPolicy::Auto, &[], &[pin("gtk", 1)]);
        assert!(w.farm_link("libfoo.so.1").is_none());
        assert!(w.farm_link("libGL.so.1").is_some());
    }
}