ktstr 0.4.12

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

use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::Context;
use serde::{Deserialize, Serialize};

/// Filename prefix that marks an in-progress atomic-store directory
/// under the cache root. Format: `{TMP_DIR_PREFIX}{cache_key}-{pid}`.
/// Emitted by [`CacheDir::store`] when composing the tempdir path,
/// recognized by the list/lookup scanners and by the orphan-sweep
/// path in [`clean_orphaned_tmp_dirs`] so a scanner that sees this
/// prefix on a directory name knows to skip (in-progress), and the
/// validator in [`validate_cache_key`] rejects keys starting with it
/// so user input cannot shadow a real tempdir. Centralized here so
/// the three roles — emitter, scanner, validator — cannot drift.
const TMP_DIR_PREFIX: &str = ".tmp-";

// Subdirectory under the cache root that holds per-entry
// coordination lockfiles. Files inside are named
// `{cache_key}.lock` — full path
// `{cache_root}/.locks/{cache_key}.lock`. Sourced from
// [`crate::flock::LOCK_DIR_NAME`] so the cache module and the
// run-dir flock surface in [`crate::test_support::sidecar`] share
// one source of truth — a future relocation updates one place.
//
// # Why a subdirectory, not a sibling next to the entry
//
// [`CacheDir::store`] installs new entries via
// `renameat2(RENAME_EXCHANGE)` on the entry directory itself. Any
// lockfile living INSIDE the entry directory would be swapped
// along with the entry — reader processes holding the lockfile's
// flock keep a stale inode, and the newly installed entry has a
// different lockfile inode that fresh readers target instead. The
// two sides stop coordinating.
//
// Parking the lockfile outside every entry keeps its inode stable
// across swaps. We pick a dedicated subdirectory rather than
// sibling-by-prefix (`.lock-{key}`) so the lockfile namespace is
// cleanly separated from the entry namespace. Cache-enumeration
// code ([`CacheDir::list`], [`clean_orphaned_tmp_dirs`],
// [`CacheDir::clean_all`]) walks first-level children looking for
// entries — the `.locks/` dotfile subdirectory is skipped by the
// dotfile filter in [`CacheDir::list`] and does not match
// [`TMP_DIR_PREFIX`] so it bypasses the orphan sweep too. User
// cache keys likewise cannot shadow lockfile paths: lockfiles are
// children of `.locks/` rather than first-level entries, so no
// [`validate_cache_key`] prefix rejection is needed.
//
// The subdirectory is created lazily on first acquire and reused;
// `CacheDir` never removes it. Lockfiles inside persist as empty
// sentinels between runs — the flock itself releases on process
// death, but the file stays for the next acquirer to reuse.
use crate::flock::LOCK_DIR_NAME;

// serde(default) is stripped from cache types so an old metadata.json
// missing a required non-Option field (e.g. has_vmlinux) fails at
// deserialize time → ListedEntry::Corrupt. Option fields tolerate
// absent keys via serde_json's native handling. Other modules use
// serde(default, skip_serializing_if) for producer-omitted fields
// (JSON minification, not compat shims); do not import that pattern here.

/// How a cached kernel's source was acquired, with per-variant
/// payload (git details for `Git`, source-tree path and git hash for
/// `Local`).
///
/// Serialized as `{"type": "tarball"}`, `{"type": "git", "git_hash": ..., "ref": ...}`,
/// or `{"type": "local", "source_tree_path": ..., "git_hash": ...}`.
/// Every per-variant payload field is emitted explicitly — `Option`
/// fields serialize as `null` when `None` rather than being skipped,
/// so JSON consumers see stable keys across every variant regardless
/// of which optional payload values are set.
///
/// Only the serialization side is pinned here. On deserialize,
/// serde_json treats absent `Option` keys as `None`, so an old
/// `metadata.json` that drops `git_hash`, `ref`, or
/// `source_tree_path` still round-trips — see
/// [`tests::kernel_source_absent_option_keys_deserialize_as_none`].
/// Cache-integrity enforcement (truncated `metadata.json` surfacing
/// as [`ListedEntry::Corrupt`] via [`CacheDir::list`]) rides on the
/// required non-`Option` fields of [`KernelMetadata`], not on the
/// optional payloads here.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase", tag = "type")]
#[non_exhaustive]
pub enum KernelSource {
    /// Downloaded tarball from kernel.org (version / prefix / EOL
    /// probe paths).
    Tarball,
    /// Shallow clone of a git URL at a caller-specified ref.
    Git {
        /// Git commit hash of the kernel source (short form).
        git_hash: Option<String>,
        /// Git ref used for checkout (branch, tag, or ref spec).
        #[serde(rename = "ref")]
        git_ref: Option<String>,
    },
    /// Build of a local on-disk kernel source tree.
    Local {
        /// Path to the source tree on disk. `None` when the tree has
        /// been sanitized for remote cache transport or is otherwise
        /// unavailable.
        source_tree_path: Option<PathBuf>,
        /// Git commit hash of the source tree at build time (short
        /// form). `None` when the tree is not a git repository, the
        /// hash could not be read, or the worktree is dirty — a
        /// HEAD hash does not describe a tree with uncommitted
        /// changes, so identifying it by that hash would mislead a
        /// reproducer.
        git_hash: Option<String>,
    },
}

impl fmt::Display for KernelSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            KernelSource::Tarball => f.write_str("tarball"),
            KernelSource::Git { .. } => f.write_str("git"),
            KernelSource::Local { .. } => f.write_str("local"),
        }
    }
}

impl KernelSource {
    /// Borrow the `git_hash` field on a [`KernelSource::Local`]
    /// variant. Returns `None` for any other variant or when the
    /// Local variant carries `git_hash: None` (dirty / non-git
    /// tree at acquire time).
    ///
    /// Mainly used by [`crate::cli::kernel_build_pipeline`]'s
    /// post-build dirty re-check, which compares the post-build
    /// HEAD hash against the acquire-time hash to detect mid-build
    /// commits or branch flips. Borrows rather than clones so the
    /// caller does not pay an allocation when only comparing.
    pub fn as_local_git_hash(&self) -> Option<&str> {
        match self {
            KernelSource::Local { git_hash, .. } => git_hash.as_deref(),
            _ => None,
        }
    }
}

/// Metadata stored alongside a cached kernel image.
///
/// Required fields (`source`, `arch`, `image_name`, `built_at`,
/// `has_vmlinux`, `vmlinux_stripped`) must be present in
/// `metadata.json` during deserialization; a truncated file that
/// drops any of them surfaces the entry as [`ListedEntry::Corrupt`]
/// via [`CacheDir::list`] rather than silently defaulting. Optional
/// fields (`version`, `config_hash`, `ktstr_kconfig_hash`) and the
/// `Option`-typed payloads inside [`KernelSource`] variants tolerate
/// absent keys as `None` — they participate in the on-disk shape but
/// do not gate cache integrity.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct KernelMetadata {
    /// Kernel version string (e.g. "6.14.2", "6.15-rc3").
    /// `None` for local builds without a version tag.
    pub version: Option<String>,
    /// How the kernel source was acquired, with per-source payload.
    pub source: KernelSource,
    /// Target architecture (e.g. "x86_64", "aarch64").
    pub arch: String,
    /// Boot image filename (e.g. "bzImage", "Image").
    pub image_name: String,
    /// CRC32 of the final .config used for the build.
    pub config_hash: Option<String>,
    /// ISO 8601 timestamp of when the image was built.
    pub built_at: String,
    /// CRC32 of ktstr.kconfig at build time.
    pub ktstr_kconfig_hash: Option<String>,
    /// Whether a vmlinux ELF was cached alongside the image. When
    /// true, the entry directory contains a `vmlinux` file; see
    /// [`strip_vmlinux_debug`] for the strip policy and
    /// [`KernelMetadata::vmlinux_stripped`] for whether the cached
    /// bytes came from the strip pipeline or the raw fallback.
    ///
    /// Required in metadata.json — plain `bool` without
    /// `#[serde(default)]` must be present during deserialization, so
    /// entries that predate this field surface as Corrupt rather than
    /// defaulting to `false`.
    ///
    /// Visibility: private field, [`KernelMetadata::has_vmlinux`]
    /// reader is public and [`KernelMetadata::set_has_vmlinux`]
    /// mutator is `pub(crate)`. The authoritative writer is
    /// [`CacheDir::store`]; making the field crate-writable-only
    /// enforces that ownership statically instead of leaving the
    /// "don't set this directly" guidance purely advisory (see the
    /// note on `new()` below).
    has_vmlinux: bool,
    /// Whether the cached vmlinux ELF came from a successful strip
    /// pass (`true`) or the raw-fallback path (`false`) where both
    /// the keep-list strip and the debug-prefix strip failed and
    /// [`CacheDir::store`] copied the unstripped bytes.
    ///
    /// Meaningful only when [`has_vmlinux`](Self::has_vmlinux)
    /// returns `true`. When `has_vmlinux` is `false`, this field is
    /// always `false` and has no consumer (there is no vmlinux to
    /// describe).
    ///
    /// Required in metadata.json — plain `bool` without
    /// `#[serde(default)]` must be present during deserialization.
    /// Old entries without this field surface as Corrupt and must be
    /// regenerated (pre-1.0 policy: no serde compatibility shims —
    /// re-running the test that populates the cache rebuilds the
    /// entry with the new field).
    ///
    /// Visibility: private field with the same ownership rules as
    /// [`has_vmlinux`](Self::has_vmlinux) — the authoritative writer
    /// is [`CacheDir::store`].
    vmlinux_stripped: bool,
    /// Size in bytes of the SOURCE-TREE vmlinux at cache-store time.
    /// Used by [`prefer_source_tree_for_dwarf`] to validate that a
    /// later DWARF read against the source-tree vmlinux is reading
    /// the same ELF the cache entry was built from. A user rebuild
    /// between the cache store and the read changes the source-tree
    /// vmlinux size; mismatching sizes signal that the source-tree
    /// vmlinux no longer matches the cache, and the helper falls
    /// back to the cached (stripped) vmlinux.
    ///
    /// `None` for non-Local entries (Tarball / Git — no source tree
    /// to validate against), and for Local entries whose vmlinux was
    /// not found at store time (the cache entry has no vmlinux to
    /// route to source for either, so validation is moot).
    pub source_vmlinux_size: Option<u64>,
    /// Modification time (seconds since UNIX epoch) of the
    /// SOURCE-TREE vmlinux at cache-store time. Same purpose and
    /// ownership as [`source_vmlinux_size`]; both fields are checked
    /// together by [`prefer_source_tree_for_dwarf`]. `None` semantics
    /// match `source_vmlinux_size`.
    ///
    /// `i64` so a pre-1970 mtime (impossible in practice but
    /// representable on disk) round-trips without panicking on
    /// duration-since-epoch. Truncates to seconds — sub-second
    /// precision provides no incremental signal for "did the file
    /// change between two builds" and varies across filesystems
    /// (ext4 supports nanoseconds, vfat does not).
    pub source_vmlinux_mtime_secs: Option<i64>,
}

impl KernelMetadata {
    /// Create a new KernelMetadata with required fields.
    ///
    /// Optional fields default to `None` / `false`. Use setter methods
    /// to populate them.
    ///
    /// Note on `has_vmlinux`: builders leave this `false` on
    /// construction and the setter should not be called directly.
    /// The authoritative writer is [`CacheDir::store`], which
    /// inspects the artifacts it's asked to persist and sets
    /// `has_vmlinux = true` iff a stripped `vmlinux` ELF was
    /// actually written into the entry directory. Setting
    /// `has_vmlinux` anywhere else risks drift between the field
    /// and the on-disk contents; that drift stays hidden until a
    /// caller of [`vmlinux_path()`](CacheEntry::vmlinux_path)
    /// consumes a path that doesn't exist, or silently skips a
    /// vmlinux that's there but marked absent. `list()` does not
    /// verify vmlinux presence, so the field alone is the source
    /// of truth for lookups.
    pub fn new(source: KernelSource, arch: String, image_name: String, built_at: String) -> Self {
        KernelMetadata {
            version: None,
            source,
            arch,
            image_name,
            config_hash: None,
            built_at,
            ktstr_kconfig_hash: None,
            has_vmlinux: false,
            vmlinux_stripped: false,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        }
    }

    /// Set the source-tree vmlinux size and mtime captured at cache
    /// store time. Used by [`crate::cli::kernel_build_pipeline`]
    /// before [`CacheDir::store`] so
    /// [`prefer_source_tree_for_dwarf`] can later validate that the
    /// on-disk source-tree vmlinux still matches the build the cache
    /// entry was produced from. Both fields move as a pair — partial
    /// values would weaken the validation invariant — so the setter
    /// takes both.
    pub fn with_source_vmlinux_stat(mut self, size: u64, mtime_secs: i64) -> Self {
        self.source_vmlinux_size = Some(size);
        self.source_vmlinux_mtime_secs = Some(mtime_secs);
        self
    }

    /// Set the kernel version.
    pub fn with_version(mut self, version: Option<String>) -> Self {
        self.version = version;
        self
    }

    /// Set the .config CRC32 hash.
    pub fn with_config_hash(mut self, hash: Option<String>) -> Self {
        self.config_hash = hash;
        self
    }

    /// Set the ktstr.kconfig CRC32 hash.
    pub fn with_ktstr_kconfig_hash(mut self, hash: Option<String>) -> Self {
        self.ktstr_kconfig_hash = hash;
        self
    }

    /// Whether a vmlinux ELF was cached alongside the image.
    ///
    /// Returns `true` whether the cached vmlinux came from a
    /// successful strip pass OR from the raw-fallback path (both
    /// produce an entry-dir `vmlinux` file). Use
    /// [`vmlinux_stripped`](Self::vmlinux_stripped) to distinguish
    /// the two shapes.
    ///
    /// Public reader for the private `has_vmlinux` field; consumers
    /// outside this crate must use this method to observe the field
    /// (the field itself is no longer directly accessible). Lookups
    /// that consume the bool to decide whether to resolve
    /// `vmlinux_path()` should read through this accessor.
    pub fn has_vmlinux(&self) -> bool {
        self.has_vmlinux
    }

    /// Crate-only mutator for `has_vmlinux`.
    ///
    /// Exists to allow [`CacheDir::store`] (the authoritative
    /// writer) to record whether the stripped vmlinux was actually
    /// persisted into the entry directory. Not public: external
    /// callers mutating this bit would risk drift between the
    /// field and the on-disk contents, which is exactly what
    /// `store()` ownership is supposed to prevent.
    pub(crate) fn set_has_vmlinux(&mut self, value: bool) {
        self.has_vmlinux = value;
    }

    /// Whether the cached vmlinux came from a successful strip pass.
    ///
    /// Returns `true` when the cached bytes were produced by
    /// [`strip_vmlinux_debug`] (either the keep-list strip or the
    /// debug-prefix fallback inside it). Returns `false` when
    /// [`strip_vmlinux_debug`] itself errored and [`CacheDir::store`]
    /// fell back to copying the unstripped source — a much larger
    /// on-disk payload that still exposes symbols / BTF but indicates
    /// the strip pipeline hit a parse failure on this kernel.
    ///
    /// Meaningful only when [`has_vmlinux`](Self::has_vmlinux) is
    /// `true`; returns `false` otherwise.
    pub fn vmlinux_stripped(&self) -> bool {
        self.vmlinux_stripped
    }

    /// Crate-only mutator for `vmlinux_stripped`.
    ///
    /// Authoritative writer is [`CacheDir::store`]; same ownership
    /// discipline as [`set_has_vmlinux`](Self::set_has_vmlinux).
    pub(crate) fn set_vmlinux_stripped(&mut self, value: bool) {
        self.vmlinux_stripped = value;
    }
}

/// Bundle of cache artifacts for [`CacheDir::store`].
///
/// Groups the image plus optional sidecars so the store signature
/// stays legible as new artifacts are added. Callers pass by
/// reference; the bundle is copy-out only — the cache reads paths
/// and copies contents.
///
/// The vmlinux path points at the raw (unstripped) ELF. `store()`
/// strips it internally via [`strip_vmlinux_debug`] and writes the
/// result, so callers do not need to run the strip pipeline
/// themselves.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct CacheArtifacts<'a> {
    /// Path to the kernel boot image (bzImage or Image).
    pub image: &'a Path,
    /// Optional path to the raw (unstripped) vmlinux ELF. `store()`
    /// strips it internally before caching — see
    /// [`strip_vmlinux_debug`].
    pub vmlinux: Option<&'a Path>,
}

impl<'a> CacheArtifacts<'a> {
    /// Create an artifact bundle with only the required image.
    pub fn new(image: &'a Path) -> Self {
        CacheArtifacts {
            image,
            vmlinux: None,
        }
    }

    /// Attach the raw (unstripped) vmlinux ELF. [`CacheDir::store`]
    /// runs [`strip_vmlinux_debug`] on it before caching.
    pub fn with_vmlinux(mut self, vmlinux: &'a Path) -> Self {
        self.vmlinux = Some(vmlinux);
        self
    }
}

/// Comparison between a cache entry's kconfig hash and a current
/// reference hash. Returned by [`CacheEntry::kconfig_status`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum KconfigStatus {
    /// Entry was built with the current kconfig — nothing to do.
    Matches,
    /// Entry was built with a different kconfig. `cached` and
    /// `current` name the two hashes for diagnostics.
    Stale {
        /// Hash recorded in the cache entry.
        cached: String,
        /// Hash the caller compared against.
        current: String,
    },
    /// Entry has no kconfig hash recorded (pre-tracking cache
    /// format). Treat as unknown; do not assume stale.
    Untracked,
}

impl KconfigStatus {
    /// Single source of truth for the "is the entry stale against
    /// the current kconfig?" predicate. Callers that previously
    /// open-coded `matches!(status, KconfigStatus::Stale { .. })`
    /// should use this method so future variants that also mean
    /// "stale" (e.g. a hypothetical version-mismatch variant) are
    /// picked up in one place.
    pub fn is_stale(&self) -> bool {
        matches!(self, Self::Stale { .. })
    }

    /// Single source of truth for the "does the entry lack a
    /// recorded kconfig hash?" predicate. Parallels [`is_stale`] so
    /// `kernel list`'s tag-aggregation loop can track the two
    /// non-Matches variants with one predicate each.
    pub fn is_untracked(&self) -> bool {
        matches!(self, Self::Untracked)
    }
}

impl fmt::Display for KconfigStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            KconfigStatus::Matches => f.write_str("matches"),
            KconfigStatus::Stale { .. } => f.write_str("stale"),
            KconfigStatus::Untracked => f.write_str("untracked"),
        }
    }
}

// Re-export KernelId from kernel_path (canonical definition, std-only).
pub use crate::kernel_path::KernelId;

/// A cached kernel entry returned by [`CacheDir::lookup`] and
/// [`CacheDir::store`]. Metadata is always present — entries with
/// missing or corrupt metadata surface as [`ListedEntry::Corrupt`]
/// via [`CacheDir::list`] instead.
#[derive(Debug)]
#[non_exhaustive]
pub struct CacheEntry {
    /// Cache key (directory name).
    pub key: String,
    /// Path to the cache entry directory.
    pub path: PathBuf,
    /// Deserialized metadata. Always present on a [`CacheEntry`].
    pub metadata: KernelMetadata,
}

impl CacheEntry {
    /// Absolute path to the cached boot image
    /// (`<entry>/<image_name>`).
    pub fn image_path(&self) -> PathBuf {
        self.path.join(&self.metadata.image_name)
    }

    /// Absolute path to the cached stripped vmlinux ELF, when one
    /// was stored alongside the image. Returns `None` for entries
    /// that were cached without a vmlinux (e.g. the source vmlinux
    /// could not be read at build time).
    pub fn vmlinux_path(&self) -> Option<PathBuf> {
        self.metadata.has_vmlinux.then(|| self.path.join("vmlinux"))
    }

    /// Compare this entry's kconfig hash against `current_hash`.
    pub fn kconfig_status(&self, current_hash: &str) -> KconfigStatus {
        match self.metadata.ktstr_kconfig_hash.as_deref() {
            None => KconfigStatus::Untracked,
            Some(h) if h == current_hash => KconfigStatus::Matches,
            Some(h) => KconfigStatus::Stale {
                cached: h.to_string(),
                current: current_hash.to_string(),
            },
        }
    }
}

/// Entry yielded by [`CacheDir::list`]. Distinguishes valid entries
/// (with parsed metadata AND a present image file) from corrupt ones
/// (unreadable metadata, unparseable metadata, or metadata that
/// references an image file that no longer exists) so callers don't
/// have to re-check `Option` or re-stat the image path.
#[derive(Debug)]
#[non_exhaustive]
pub enum ListedEntry {
    /// Valid cache entry with parsed metadata and an image file
    /// present on disk at the metadata-declared path.
    ///
    /// Boxed so the enum's `Valid` variant doesn't dwarf `Corrupt`
    /// — `CacheEntry` carries a 280-byte `KernelMetadata` plus
    /// `PathBuf`s, while `Corrupt` is a flat 72-byte triple.
    /// Without the indirection clippy's `large_enum_variant` flags
    /// every `ListedEntry` value as paying for the `Valid` shape
    /// even when it lands in `Corrupt`.
    Valid(Box<CacheEntry>),
    /// Entry directory exists but is unusable. Common reasons:
    /// metadata.json missing, metadata.json unparseable, or metadata
    /// parsed cleanly but the declared image file is absent (partial
    /// download, manual deletion, failed strip+rename).
    Corrupt {
        /// Cache key (directory name).
        key: String,
        /// Path to the (corrupt) entry directory.
        path: PathBuf,
        /// Human-readable explanation of why the entry is classified
        /// as corrupt. Rendered by CLI consumers for the user.
        reason: String,
    },
}

impl ListedEntry {
    /// Cache key (directory name) for either variant.
    pub fn key(&self) -> &str {
        match self {
            ListedEntry::Valid(e) => &e.key,
            ListedEntry::Corrupt { key, .. } => key,
        }
    }

    /// Path to the entry directory for either variant.
    pub fn path(&self) -> &Path {
        match self {
            ListedEntry::Valid(e) => &e.path,
            ListedEntry::Corrupt { path, .. } => path,
        }
    }

    /// Borrow the valid [`CacheEntry`] payload, or `None` for
    /// [`ListedEntry::Corrupt`].
    pub fn as_valid(&self) -> Option<&CacheEntry> {
        match self {
            ListedEntry::Valid(e) => Some(e.as_ref()),
            ListedEntry::Corrupt { .. } => None,
        }
    }

    /// Machine-readable classification of a corrupt entry's failure
    /// mode, for consumers that need to branch without parsing the
    /// free-form `reason`. Returns `None` on a `Valid` entry.
    ///
    /// Classifier keys the prefix the [`read_metadata`] and
    /// [`CacheDir::list`] producers actually emit, so a downstream
    /// consumer can switch on the returned `&str` instead of matching
    /// human text. Values are stable identifiers (snake_case) the
    /// `kernel list --json` schema surfaces as the `error_kind` field:
    ///
    /// | `error_kind`    | Producing prefix                        |
    /// |-----------------|-----------------------------------------|
    /// | `"missing"`     | `"metadata.json missing"`               |
    /// | `"unreadable"`  | `"metadata.json unreadable: ..."`       |
    /// | `"schema_drift"`| `"metadata.json schema drift: ..."`     |
    /// | `"malformed"`   | `"metadata.json malformed: ..."`        |
    /// | `"truncated"`   | `"metadata.json truncated: ..."`        |
    /// | `"parse_error"` | `"metadata.json parse error: ..."` (Io fallback) |
    /// | `"image_missing"` | `"image file <name> missing from entry directory"` |
    /// | `"unknown"`     | Any reason that does not match the above prefixes. Surfaces here instead of panicking so a future prefix addition degrades to "unclassified" rather than bailing the whole list. |
    ///
    /// A regression that introduces a new prefix in the producer
    /// without updating this classifier surfaces as `"unknown"` in
    /// the JSON output — consumer-side alerts on that value give
    /// maintainers a clear signal to extend the table.
    pub fn error_kind(&self) -> Option<&'static str> {
        match self {
            ListedEntry::Valid(_) => None,
            ListedEntry::Corrupt { reason, .. } => Some(classify_corrupt_reason(reason)),
        }
    }
}

/// Shared prefix → `error_kind` classifier. Public to the module so
/// tests can pin each prefix routes to its documented value without
/// constructing a whole `ListedEntry::Corrupt`.
fn classify_corrupt_reason(reason: &str) -> &'static str {
    if reason == "metadata.json missing" {
        "missing"
    } else if reason.starts_with("metadata.json unreadable: ") {
        "unreadable"
    } else if reason.starts_with("metadata.json schema drift: ") {
        "schema_drift"
    } else if reason.starts_with("metadata.json malformed: ") {
        "malformed"
    } else if reason.starts_with("metadata.json truncated: ") {
        "truncated"
    } else if reason.starts_with("metadata.json parse error: ") {
        "parse_error"
    } else if reason.starts_with("image file ") && reason.contains("missing") {
        "image_missing"
    } else {
        "unknown"
    }
}

/// Handle to the kernel image cache directory.
///
/// All operations are local filesystem operations via `std::fs`.
/// Thread safety: individual operations are atomic (rename-based
/// writes), but concurrent callers must coordinate externally.
///
/// `#[non_exhaustive]` matches the sibling pub types in this module
/// ([`KernelMetadata`], [`KernelSource`], [`CacheArtifacts`],
/// [`KconfigStatus`], [`CacheEntry`], [`ListedEntry`]). Every field
/// today is private, so external struct-literal construction is
/// already impossible; the attribute is kept for consistency and to
/// pin the "no cross-crate struct-literal" contract against a future
/// change that promotes a field to `pub`. Use [`Self::new`] or
/// [`Self::with_root`] to construct.
#[derive(Debug)]
#[non_exhaustive]
pub struct CacheDir {
    root: PathBuf,
}

/// Emit a per-lookup warning when a cache entry was created with an
/// unstripped vmlinux — i.e. a prior `CacheDir::store` call took the
/// strip-fallback path. The entry is still usable (monitor and probe
/// symbol lookup works on the raw ELF) but the on-disk payload is
/// much larger than a successfully-stripped entry. Firing every
/// lookup gives the operator a persistent reminder until the cache
/// is rebuilt — complementing the one-shot eprintln at store time.
fn warn_if_unstripped_vmlinux(entry: &CacheEntry) {
    if should_warn_unstripped(entry) {
        eprintln!(
            "cache: using unstripped vmlinux for {} (strip failed on a prior build; \
             re-run with a clean cache to retry)",
            entry.key,
        );
    }
}

/// Pure decision logic for [`warn_if_unstripped_vmlinux`]: `true`
/// iff the entry has a cached vmlinux AND that vmlinux came from
/// the raw-fallback path (strip failure). Returns `false` when
/// `has_vmlinux` is false (no vmlinux was cached; the
/// `vmlinux_stripped` bit is meaningless in that shape and emitting
/// a warning would be noise).
///
/// Separated from the eprintln so the `!has_vmlinux → no warning`
/// and `has_vmlinux && vmlinux_stripped → no warning` branches can
/// be unit-tested without capturing stderr.
fn should_warn_unstripped(entry: &CacheEntry) -> bool {
    entry.metadata.has_vmlinux() && !entry.metadata.vmlinux_stripped()
}

impl CacheDir {
    /// Open a cache directory at the resolved root path.
    ///
    /// Resolution order:
    /// 1. `KTSTR_CACHE_DIR` environment variable
    /// 2. `$XDG_CACHE_HOME/ktstr/kernels/`
    /// 3. `$HOME/.cache/ktstr/kernels/`
    ///
    /// Only resolves the path; does not create the directory.
    /// [`store`](Self::store) creates the tree lazily on first write.
    /// [`list`](Self::list) and [`lookup`](Self::lookup) handle a
    /// nonexistent root as "no entries".
    pub fn new() -> anyhow::Result<Self> {
        let root = resolve_cache_root()?;
        Ok(CacheDir { root })
    }

    /// Open a cache directory at a specific path.
    ///
    /// Only sets the root path; does not create the directory — the
    /// constructor is infallible. Used by tests and callers that need
    /// an explicit cache location.
    pub fn with_root(root: PathBuf) -> Self {
        CacheDir { root }
    }

    /// Resolve the default cache root path without side effects.
    ///
    /// Returns the path that [`new`](Self::new) would use, without
    /// constructing a [`CacheDir`] or touching the filesystem.
    /// Renamed from `resolve_root` to `default_root` so the name
    /// describes what the returned path represents (the default
    /// the constructor *would* pick), while
    /// [`root`](Self::root) on an instance returns the root a
    /// particular `CacheDir` actually points at — the two names
    /// no longer share a noun.
    pub fn default_root() -> anyhow::Result<PathBuf> {
        resolve_cache_root()
    }

    /// Root directory this `CacheDir` is anchored at. Distinct from
    /// [`default_root`](Self::default_root) (static): `default_root`
    /// says "the default constructor would build here";
    /// `root` says "this specific instance lives here".
    pub fn root(&self) -> &Path {
        &self.root
    }

    /// Look up a cached kernel by cache key.
    ///
    /// Returns the cache entry if it exists, has valid metadata, and
    /// contains the expected kernel image file. Returns `None` if the
    /// key is invalid, the entry does not exist, or is corrupted.
    ///
    /// On a successful lookup of an entry whose vmlinux came from the
    /// raw-fallback path (`vmlinux_stripped == false` alongside
    /// `has_vmlinux == true`), emits a per-lookup eprintln warning so
    /// operators running with the default (no-tracing) subscriber see
    /// the signal every time they hit a bloated entry — not just once
    /// at store time when a prior build's strip pipeline failed.
    pub fn lookup(&self, cache_key: &str) -> Option<CacheEntry> {
        if let Err(e) = validate_cache_key(cache_key) {
            tracing::warn!("invalid cache key: {e}");
            return None;
        }
        let entry_dir = self.root.join(cache_key);
        if !entry_dir.is_dir() {
            return None;
        }
        let metadata = read_metadata(&entry_dir).ok()?;
        // Entry must have a kernel image file.
        if !entry_dir.join(&metadata.image_name).exists() {
            return None;
        }
        let entry = CacheEntry {
            key: cache_key.to_string(),
            path: entry_dir,
            metadata,
        };
        warn_if_unstripped_vmlinux(&entry);
        Some(entry)
    }

    /// List all cached kernel entries, sorted by build time (newest
    /// first). Entries with missing metadata, unparseable metadata,
    /// or a missing image file surface as [`ListedEntry::Corrupt`] at
    /// the end of the Vec. Valid entries are observed to have an
    /// image file present at scan time — the presence check runs
    /// inside this function before classification, so callers do not
    /// need to re-stat for steady-state reads. The guarantee is
    /// best-effort against TOCTOU: concurrent cache mutation
    /// (another process calling `store`/`clean`, or manual rmdir)
    /// can invalidate the invariant between `list()` return and a
    /// subsequent [`CacheEntry::image_path`] open, so callers in
    /// multi-process contexts must still handle ENOENT gracefully.
    pub fn list(&self) -> anyhow::Result<Vec<ListedEntry>> {
        let mut entries: Vec<ListedEntry> = Vec::new();
        let read_dir = match fs::read_dir(&self.root) {
            Ok(rd) => rd,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(entries),
            Err(e) => return Err(e.into()),
        };
        for dir_entry in read_dir {
            let dir_entry = dir_entry?;
            let path = dir_entry.path();
            let file_name = dir_entry.file_name();
            let name_hint = file_name.to_string_lossy();
            // Skip dotfile children BEFORE the is_dir check. The
            // cache root's dotfile-namespace is reserved for ktstr's
            // own bookkeeping: `.locks/` (per-entry coordination
            // lockfiles, see [`LOCK_DIR_NAME`]), `.tmp-*` (in-progress
            // store tempdirs, see [`TMP_DIR_PREFIX`]), and any future
            // bookkeeping directory. Cache keys validated by
            // [`validate_cache_key`] cannot begin with `.` — that
            // validator rejects only reserved prefixes, but POSIX
            // convention keeps application-visible data out of
            // dotfile names, and every bookkeeping surface in this
            // module follows the convention. Skipping at the first
            // filter stops `clean_all` / `clean_keep` (which remove
            // every ListedEntry this function returns) from ever
            // touching `.locks/` or any future bookkeeping
            // subdirectory even if `is_dir` would otherwise let it
            // through.
            if name_hint.starts_with('.') {
                continue;
            }
            if !path.is_dir() {
                continue;
            }
            // Skip temp directories from in-progress stores.
            let name = match dir_entry.file_name().into_string() {
                Ok(n) => n,
                Err(_) => continue,
            };
            if name.starts_with(TMP_DIR_PREFIX) {
                continue;
            }
            match read_metadata(&path) {
                Ok(metadata) => {
                    let image_path = path.join(&metadata.image_name);
                    if image_path.exists() {
                        entries.push(ListedEntry::Valid(Box::new(CacheEntry {
                            key: name,
                            path,
                            metadata,
                        })));
                    } else {
                        // Metadata parsed but the image file declared
                        // inside it is gone — treat as corrupt so
                        // callers don't dispatch to image_path() and
                        // get a path that 404s downstream.
                        entries.push(ListedEntry::Corrupt {
                            key: name,
                            path,
                            reason: format!(
                                "image file {} missing from entry directory",
                                metadata.image_name
                            ),
                        });
                    }
                }
                Err(reason) => {
                    // Emit a `tracing::info` so operators reviewing
                    // logs can see per-entry corruption without
                    // scraping the CLI table. `reason` carries a
                    // self-classified prefix from `read_metadata` —
                    // `"metadata.json missing"`, `"... unreadable: "`,
                    // `"... schema drift: "`, `"... malformed: "`, or
                    // `"... truncated: "`. The log message itself
                    // stays neutral; downstream log parsers key on
                    // the `reason` prefix rather than the message.
                    tracing::info!(
                        entry = %name,
                        path = %path.display(),
                        %reason,
                        "cache entry corrupt at list-time",
                    );
                    entries.push(ListedEntry::Corrupt {
                        key: name,
                        path,
                        reason,
                    });
                }
            }
        }
        // Sort by built_at descending (newest first). Corrupt entries
        // have no timestamp and sort last.
        entries.sort_by(|a, b| {
            let a_time = a.as_valid().map(|e| e.metadata.built_at.as_str());
            let b_time = b.as_valid().map(|e| e.metadata.built_at.as_str());
            b_time.cmp(&a_time)
        });
        Ok(entries)
    }

    /// Store a kernel image in the cache.
    ///
    /// `cache_key`: directory name for the entry (e.g.
    /// `6.14.2-tarball-x86_64-kc{kconfig_hash}`).
    ///
    /// `artifacts`: required image plus optional raw vmlinux. When
    /// `artifacts.vmlinux` is set, `store()` runs
    /// [`strip_vmlinux_debug`] internally; on strip failure it falls
    /// back to caching the unstripped vmlinux (logged via
    /// `tracing::warn!`). See [`CacheArtifacts`].
    ///
    /// `metadata`: descriptor to serialize as `metadata.json`. The
    /// `has_vmlinux` and `vmlinux_stripped` fields are overwritten
    /// based on what `store()` actually persisted (whether a vmlinux
    /// was given, and whether the strip pipeline succeeded), so
    /// callers do not need to pre-populate either.
    ///
    /// Files are copied (not moved) so the caller retains the
    /// originals. Writes atomically via a temporary directory that is
    /// renamed into place on success. Creates the cache root lazily
    /// if it does not yet exist.
    pub fn store(
        &self,
        cache_key: &str,
        artifacts: &CacheArtifacts<'_>,
        metadata: &KernelMetadata,
    ) -> anyhow::Result<CacheEntry> {
        validate_cache_key(cache_key)?;
        validate_filename(&metadata.image_name)?;

        // Serialize atomic-swap installs against every reader (test
        // VM holding LOCK_SH on this entry). The guard drops at the
        // end of this function, after `renameat2` has run and
        // `_guard` (TmpDirGuard) has cleaned the displaced content.
        // A 60 s blocking timeout catches pathologically stuck peers
        // while tolerating a single healthy test run draining. See
        // [`LOCK_DIR_NAME`] for the `.locks/` subdirectory placement
        // rationale.
        let _store_lock =
            self.acquire_exclusive_lock_blocking(cache_key, STORE_EXCLUSIVE_LOCK_TIMEOUT)?;

        let final_dir = self.root.join(cache_key);
        let tmp_dir = self.root.join(format!(
            "{TMP_DIR_PREFIX}{}-{}",
            cache_key,
            std::process::id(),
        ));

        // Clean up any stale temp dir from a prior crash. create_dir_all
        // on tmp_dir also creates self.root lazily on first store.
        if tmp_dir.exists() {
            fs::remove_dir_all(&tmp_dir)?;
        }
        // Scan-and-clean orphaned `.tmp-*` siblings from prior
        // panic/abort debris across OTHER PIDs. The same-PID cleanup
        // above only fires for the current PID's tmp dir; a prior
        // run that died with a different PID leaves debris behind,
        // which accumulates across crashes and takes disk space.
        // `clean_orphaned_tmp_dirs` reads the cache root, inspects
        // each `.tmp-{key}-{pid}` name, and removes entries whose
        // `{pid}` is no longer a live pid. Errors are logged
        // and swallowed — a failure to clean orphans must not block
        // a successful store.
        if let Err(e) = clean_orphaned_tmp_dirs(&self.root) {
            tracing::warn!(err = %format!("{e:#}"), "clean_orphaned_tmp_dirs failed; continuing store");
        }
        fs::create_dir_all(&tmp_dir)?;

        // TmpGuard ensures tmp_dir is cleaned up on any error path
        // (including serde serialization failures) and on the
        // success path — where tmp_dir either no longer exists
        // (plain rename moved it to final_dir) or now holds the
        // displaced old cache content (atomic swap rotated it in).
        let _guard = TmpDirGuard(&tmp_dir);

        // Copy boot image.
        let image_dest = tmp_dir.join(&metadata.image_name);
        fs::copy(artifacts.image, &image_dest)
            .map_err(|e| anyhow::anyhow!("copy kernel image to cache: {e}"))?;

        // Strip the raw vmlinux and copy the stripped result into the
        // cache entry. If the strip pipeline errors (e.g. ELF too
        // exotic for object::build::Builder), fall back to caching
        // the unstripped vmlinux so downstream callers still get
        // symbols and BTF — the size penalty is preferable to losing
        // the vmlinux entirely.
        //
        // `vmlinux_stripped` distinguishes the two success shapes:
        // `true` on the strip-ok path, `false` on the raw-fallback
        // path. Consumers (e.g. `ktstr cache list --json`) surface
        // this so operators notice when a newly-cached kernel hit
        // the fallback — a silent size regression that used to only
        // appear in tracing logs.
        let (has_vmlinux, vmlinux_stripped) = if let Some(vmlinux) = artifacts.vmlinux {
            let vmlinux_dest = tmp_dir.join("vmlinux");
            match strip_vmlinux_debug(vmlinux) {
                Ok(stripped) => {
                    fs::copy(stripped.path(), &vmlinux_dest)
                        .map_err(|e| anyhow::anyhow!("copy stripped vmlinux to cache: {e}"))?;
                    (true, true)
                }
                Err(e) => {
                    // eprintln in addition to tracing::warn so the
                    // failure is visible to operators running with
                    // the default (no-tracing) subscriber. Without
                    // this, the only signal was a tracing::warn
                    // swallowed by the default writer — the cached
                    // entry grew unboundedly (the uncompressed
                    // source vmlinux's full size) with no on-screen
                    // explanation.
                    eprintln!(
                        "cache: vmlinux strip failed for {cache_key} ({e:#}); \
                         caching unstripped (larger on-disk payload). \
                         See `ktstr cache list --json` vmlinux_stripped field.",
                    );
                    tracing::warn!(
                        cache_key = cache_key,
                        err = %format!("{e:#}"),
                        "vmlinux strip failed, caching unstripped",
                    );
                    fs::copy(vmlinux, &vmlinux_dest)
                        .map_err(|e| anyhow::anyhow!("copy vmlinux to cache: {e}"))?;
                    (true, false)
                }
            }
        } else {
            (false, false)
        };

        // Write metadata. has_vmlinux and vmlinux_stripped reflect
        // what we actually stored, overriding whatever the caller set.
        let mut meta = metadata.clone();
        meta.set_has_vmlinux(has_vmlinux);
        meta.set_vmlinux_stripped(vmlinux_stripped);
        let meta_json = serde_json::to_string_pretty(&meta)?;
        fs::write(tmp_dir.join("metadata.json"), meta_json)
            .map_err(|e| anyhow::anyhow!("write cache metadata: {e}"))?;

        // Atomic install. Two cases:
        //
        // 1. final_dir does not yet exist → plain rename(tmp_dir,
        //    final_dir). The first such rename to win races gets to
        //    install; a concurrent store that also thought final_dir
        //    was absent retries via the swap branch when its rename
        //    trips ENOTEMPTY/EEXIST against the just-installed dir.
        //
        // 2. final_dir already exists → renameat2 with
        //    RENAME_EXCHANGE atomically swaps tmp_dir and final_dir
        //    in a single syscall. Readers calling lookup() at any
        //    instant see either the old entry or the new one,
        //    never a missing final_dir. After the swap, the old
        //    content lives in tmp_dir and is cleaned by the guard.
        //
        // RENAME_EXCHANGE is Linux-specific (kernel ≥ 3.15) and
        // requires that both paths exist at swap time. POSIX
        // rename(old, new) on a non-empty directory returns ENOTEMPTY
        // and would otherwise force a two-syscall dance with an
        // observable gap where final_dir does not exist — breaking
        // reader atomicity.
        match fs::rename(&tmp_dir, &final_dir) {
            Ok(()) => {}
            Err(e)
                if e.raw_os_error() == Some(libc::ENOTEMPTY)
                    || e.raw_os_error() == Some(libc::EEXIST) =>
            {
                atomic_swap_dirs(&tmp_dir, &final_dir)?;
                // After the swap, tmp_dir points at the old cache
                // entry content; drop it so the guard's
                // remove_dir_all cleans the right tree.
            }
            Err(e) => {
                return Err(anyhow::anyhow!("atomic rename cache entry: {e}"));
            }
        }

        // Guard drops at end of scope, cleaning tmp_dir in both
        // cases: a plain rename left it nonexistent (remove_dir_all
        // no-ops on ENOENT); an atomic swap placed the displaced
        // old entry there for removal.
        Ok(CacheEntry {
            key: cache_key.to_string(),
            path: final_dir,
            metadata: meta,
        })
    }

    /// Remove every cached entry. Returns the number of entries
    /// removed.
    pub fn clean_all(&self) -> anyhow::Result<usize> {
        self.remove_entries(self.list()?)
    }

    /// Remove every cached entry except the `keep` most recent ones
    /// (by `built_at` timestamp). `keep == 0` is equivalent to
    /// [`clean_all`](Self::clean_all). Returns the number of entries
    /// removed.
    pub fn clean_keep(&self, keep: usize) -> anyhow::Result<usize> {
        self.remove_entries(self.list()?.into_iter().skip(keep))
    }

    fn remove_entries<I: IntoIterator<Item = ListedEntry>>(
        &self,
        iter: I,
    ) -> anyhow::Result<usize> {
        let to_remove: Vec<_> = iter.into_iter().collect();
        let count = to_remove.len();
        for entry in &to_remove {
            fs::remove_dir_all(entry.path())?;
        }
        Ok(count)
    }

    // ------------------------------------------------------------------
    // Per-entry coordination locks
    // ------------------------------------------------------------------
    //
    // See [`LOCK_DIR_NAME`] for the on-disk shape rationale. A
    // shared (reader) lock is held by each test-VM run against the
    // cache entry it resolved its kernel image from; an exclusive
    // (writer) lock is held by `CacheDir::store` for the window
    // spanning temp-dir composition → atomic rename. flock(2) is
    // per-open-file-description; RAII guards drop the OwnedFd which
    // releases the lock.

    /// Absolute path to the coordination lockfile for `cache_key`.
    /// The lockfile lives at `{cache_root}/.locks/{cache_key}.lock`
    /// (see [`LOCK_DIR_NAME`] for the subdirectory placement
    /// rationale). Does not touch the filesystem — use
    /// [`CacheDir::ensure_lock_dir`] before opening the file to make
    /// sure the parent `.locks/` subdirectory exists.
    pub(crate) fn lock_path(&self, cache_key: &str) -> PathBuf {
        self.root
            .join(LOCK_DIR_NAME)
            .join(format!("{cache_key}.lock"))
    }

    /// Create the `{cache_root}/.locks/` subdirectory if absent.
    /// Callers invoke this before [`open_lockfile`] so the parent
    /// exists; `fs::create_dir_all` is idempotent (Ok on existing
    /// directory), mirroring the lazy-create semantics
    /// [`CacheDir::store`] uses for the cache root itself.
    pub(crate) fn ensure_lock_dir(&self) -> anyhow::Result<()> {
        let dir = self.root.join(LOCK_DIR_NAME);
        fs::create_dir_all(&dir)
            .with_context(|| format!("create lock subdirectory {}", dir.display()))
    }

    /// Acquire `LOCK_SH` on the cache-entry lockfile. Blocks until the
    /// lock is available or the default shared-lock timeout elapses.
    ///
    /// Test-VM runs call this on the entry they resolved their kernel
    /// image from. A concurrent `CacheDir::store` that holds `LOCK_EX`
    /// for an atomic-swap install is serialized behind every reader.
    /// Multiple readers coexist.
    ///
    /// The underlying lockfile is created on-demand when it does not
    /// exist (mode 0o666); the cache root is created if missing
    /// (matching [`CacheDir::store`]'s lazy root creation). Returns
    /// `Err` on filesystem errors or timeout.
    pub fn acquire_shared_lock(&self, cache_key: &str) -> anyhow::Result<SharedLockGuard> {
        validate_cache_key(cache_key)?;
        let path = self.lock_path(cache_key);
        let fd = crate::flock::acquire_flock_with_timeout(
            &path,
            FlockMode::Shared,
            SHARED_LOCK_DEFAULT_TIMEOUT,
            &format!("cache entry {cache_key:?}"),
            None,
        )?;
        Ok(SharedLockGuard { fd })
    }

    /// Acquire `LOCK_EX` on the cache-entry lockfile. Blocks until the
    /// lock is available or `timeout` elapses.
    ///
    /// Called internally by [`CacheDir::store`] to serialize atomic-swap
    /// installs against concurrent readers (test runs holding
    /// `LOCK_SH`). On timeout, the error message lists the PIDs that
    /// are currently holding the lock (parsed from `/proc/locks`) so
    /// the operator can kill or wait on them deliberately.
    pub fn acquire_exclusive_lock_blocking(
        &self,
        cache_key: &str,
        timeout: std::time::Duration,
    ) -> anyhow::Result<ExclusiveLockGuard> {
        validate_cache_key(cache_key)?;
        let path = self.lock_path(cache_key);
        let fd = crate::flock::acquire_flock_with_timeout(
            &path,
            FlockMode::Exclusive,
            timeout,
            &format!("cache entry {cache_key:?}"),
            None,
        )?;
        Ok(ExclusiveLockGuard { fd })
    }

    /// Non-blocking `LOCK_EX` attempt on the cache-entry lockfile. Used
    /// as a pre-check by `--force` operator-driven rebuilds: fail fast
    /// with a PID list if tests are actively using the entry, instead
    /// of silently stomping on an in-flight run.
    ///
    /// Returns `Err` immediately when any reader or writer holds the
    /// lock; the error surfaces the holder PIDs parsed from
    /// `/proc/locks`.
    pub fn try_acquire_exclusive_lock(
        &self,
        cache_key: &str,
    ) -> anyhow::Result<ExclusiveLockGuard> {
        validate_cache_key(cache_key)?;
        self.ensure_lock_dir()?;
        let path = self.lock_path(cache_key);
        // `try_flock` folds `open(O_CLOEXEC) + flock(LOCK_NB)` into a
        // single call; on `EWOULDBLOCK` it returns `Ok(None)` with the
        // fd already dropped, so the /proc/locks scan below cannot
        // confuse our own new fd (just-opened, never flocked) with a
        // real holder.
        match crate::flock::try_flock(&path, crate::flock::FlockMode::Exclusive)? {
            Some(fd) => Ok(ExclusiveLockGuard { fd }),
            None => {
                let holders = crate::flock::read_holders(&path).unwrap_or_default();
                anyhow::bail!(
                    "cache entry {cache_key:?} is locked by active test runs \
                     (lockfile {lockfile}, holders: {holders}). Wait for \
                     those tests to finish, or kill them, then retry.",
                    lockfile = path.display(),
                    holders = crate::flock::format_holder_list(&holders),
                );
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Cache-lock timeouts — reader 10s, writer 60s (INTENTIONAL asymmetry)
// ---------------------------------------------------------------------------
//
// Readers (test VMs) wait 10 s for a writer to finish. Writers
// (`CacheDir::store`) wait 60 s for all readers to drain. The
// asymmetry is deliberate, not a tuning artifact:
//
// - A writer's critical section is SHORT — temp-dir copy + metadata
//   write + `renameat2(RENAME_EXCHANGE)` — measured in low seconds
//   even for multi-GB vmlinux images. A reader waiting 10 s must
//   have caught a genuinely stuck writer, and surfacing that as an
//   error beats hanging the test run indefinitely.
// - A writer's contention is LONG — it must outlast every reader
//   currently running a test on this entry. A single test run takes
//   tens of seconds (VM boot + scenario + teardown); several parallel
//   runs serialize through their own nextest scheduling. 60 s is
//   empirically enough for the natural drain without rewarding
//   pathological test loops.
// - Pathological peers in either direction surface as actionable
//   errors (holder PIDs + cmdlines in the error text) instead of
//   silent hangs.

/// Default wall-clock timeout for [`CacheDir::acquire_shared_lock`].
/// See module-level rationale block above.
const SHARED_LOCK_DEFAULT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

/// Timeout for [`CacheDir::store`]'s internal `LOCK_EX` acquire.
/// See module-level rationale block above for the reader/writer
/// asymmetry (10 s vs 60 s).
const STORE_EXCLUSIVE_LOCK_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);

// Flock primitive lives in [`crate::flock`], shared with
// `crate::vmm::host_topology`'s LLC / per-CPU acquires. LlcLockMode
// remains separate at the scheduler-intent layer (perf-mode vs
// no-perf-mode request, not a flock operation).
use crate::flock::FlockMode;

/// RAII guard for a `LOCK_SH` hold on a cache-entry lockfile. Dropping
/// the guard releases the advisory lock via `OwnedFd::drop` — no
/// explicit `unlock()` call needed. The `fd` field carries the
/// `OwnedFd` whose Drop releases the kernel-side flock; nothing reads
/// it after construction, but it must stay named (not `_fd`) so
/// grep-based audits and struct-literal tools can find the holder
/// field without a leading-underscore filter.
#[derive(Debug)]
pub struct SharedLockGuard {
    #[allow(dead_code)]
    fd: std::os::fd::OwnedFd,
}

/// RAII guard for a `LOCK_EX` hold on a cache-entry lockfile. Dropping
/// the guard releases the advisory lock via `OwnedFd::drop`.
#[derive(Debug)]
pub struct ExclusiveLockGuard {
    #[allow(dead_code)]
    fd: std::os::fd::OwnedFd,
}

/// Scan `cache_root` for `.tmp-{key}-{pid}` directories whose `{pid}`
/// is no longer a live process and remove them.
///
/// Same-PID orphan cleanup lives in `store()`; this helper handles
/// the cross-PID case — debris left by a prior process that died
/// with a different PID. Without periodic cleanup, crashes
/// accumulate directories forever. Called from the start of every
/// `store()` so ordinary fetch traffic cleans up without a
/// dedicated CLI knob.
///
/// Discrimination: parse the trailing `-{digits}` suffix as a pid
/// and probe liveness via `kill(pid, None)` (the standard
/// signal-zero probe). `Err(ESRCH)` is the only outcome that
/// justifies removal; `Ok(())` and `Err(EPERM)` both indicate a
/// live pid whose debris we leave alone. A pid recycled between
/// orphan creation and this scan is treated as alive — false
/// negative, preserves debris — but never false positive.
///
/// # TOCTOU: pid reuse between the `kill(pid, None)` probe and
///   `remove_dir_all(path)`
///
/// There is an inherent race between the liveness probe above and
/// the filesystem unlink below: the kernel is free to reap the
/// embedded pid (actually dead → probe returns `ESRCH`), allocate
/// it to a fresh unrelated process, and schedule that process
/// before `remove_dir_all` starts. The cleanup then proceeds
/// because the probe snapshot said "dead" — it cannot observe the
/// subsequent reuse. The consequence is bounded and harmless: the
/// new pid-holder does NOT own the `.tmp-{key}-{pid}` directory
/// (it was created by the prior dead process), so
/// `remove_dir_all` operates on paths that no live process is
/// reading or writing. The reuse does not cross into the cleanup
/// target's data. The pid-reuse wraparound distance is the
/// runtime `/proc/sys/kernel/pid_max` sysctl (the default on
/// modern Linux is `2^22` == `4_194_304`, matching the
/// compile-time ceiling `include/linux/threads.h::PID_MAX_LIMIT`;
/// administrators can lower it via `sysctl kernel.pid_max=<N>`
/// to shrink the wraparound surface, or — less commonly — raise
/// it on 64-bit hosts where the 32-bit pid_t carries the higher
/// bound). So reuse is rare on a host with moderate fork rate
/// but not vanishingly so on CI runners that recycle pids
/// quickly, especially if the runner's base image has lowered
/// `pid_max` to fit a container namespace.
///
/// # TOCTOU: concurrent ktstr processes cannot collide on the
///   same `.tmp-` directory
///
/// Two ktstr processes running against the same cache root
/// cannot create conflicting `.tmp-{key}-{pid}` entries because
/// the path embeds the CREATOR's pid. Process A with pid=100
/// creates `.tmp-foo-100`; process B with pid=200 creates
/// `.tmp-foo-200`. The scan here classifies both entries
/// independently via the `kill(pid, None)` probe on the EMBEDDED
/// pid, so a live sibling's debris is never misclassified as
/// the scanner's own orphan — the scanner never writes a path
/// embedding its own pid AND holding liveness for a foreign pid
/// at the same time. A live ktstr whose pid happens to be
/// reused after death falls under the pid-reuse case above, not
/// this concurrent-process case, because the embedded pid
/// refers to the DEAD creator, not the current live process
/// occupying the same pid slot.
///
/// We accept the race rather than serialize behind an additional
/// lock because (a) the damage model is "delete a dead process's
/// leftover tempdir"— exactly the scan's intent — regardless of
/// what happens to the pid slot AFTER the probe, and (b) the only
/// alternative that closes the race (an exclusive `flock` on each
/// tempdir before remove) would compound its own failure modes in
/// exchange for eliminating an effectively-benign race. The
/// reverse race (pid reuse BEFORE the probe, producing a false
/// "alive" verdict) is already covered by the "false negative,
/// preserves debris" bullet above — debris staying one extra cycle
/// is acceptable; deleting a live process's state would not be.
///
/// Errors during individual entry walks are swallowed and logged
/// (each one would prevent a store that otherwise has nothing to
/// do with the unreadable entry). Filesystem-level read errors on
/// the cache root itself are propagated.
fn clean_orphaned_tmp_dirs(cache_root: &Path) -> anyhow::Result<()> {
    if !cache_root.is_dir() {
        return Ok(());
    }
    let read_dir = match fs::read_dir(cache_root) {
        Ok(rd) => rd,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(e) => anyhow::bail!("read cache root {}: {e}", cache_root.display()),
    };
    for dir_entry in read_dir {
        let dir_entry = match dir_entry {
            Ok(d) => d,
            Err(e) => {
                tracing::warn!(err = %format!("{e:#}"), "skip unreadable cache root entry");
                continue;
            }
        };
        let name = match dir_entry.file_name().into_string() {
            Ok(n) => n,
            Err(_) => continue, // non-UTF-8, not a `.tmp-` we created
        };
        if !name.starts_with(TMP_DIR_PREFIX) {
            // Lockfile subdirectory (`.locks/`) and any future
            // dotfile bookkeeping sibling fall through here. They
            // don't start with `.tmp-`, so this prefix filter
            // excludes them from the tmp-dir sweep — exactly the
            // namespace-separation contract advertised in the
            // `LOCK_DIR_NAME` comment. No extra check needed.
            continue;
        }
        // Suffix parse: `.tmp-{key}-{pid}`. Key may itself contain
        // `-`; the PID is the last `-`-separated token.
        let pid_str = match name.rsplit_once('-') {
            Some((_, suffix)) if !suffix.is_empty() => suffix,
            _ => continue, // malformed — not our format
        };
        let pid: i32 = match pid_str.parse() {
            Ok(p) => p,
            Err(_) => continue, // non-numeric suffix — not our format
        };
        // Reject non-positive pids defensively: `kill(0, None)` probes
        // the caller's own process group and `kill(-N, None)` probes
        // process group N — both have broadcast semantics that would
        // misclassify a live unrelated group as our own dead orphan.
        // The current parser builds the suffix via `rsplit_once('-')`,
        // which strips the dash — `parse::<i32>()` cannot produce a
        // negative — but a future refactor that emitted `-N` (regex,
        // explicit splitn(3, '-'), …) would land in the broadcast
        // probe and silently delete unrelated tempdirs.
        if pid <= 0 {
            continue;
        }
        // Portable liveness probe via `kill(pid, signal=None)`:
        // `Ok(())` — signal could have been delivered, pid is alive
        // (same uid or we hold CAP_KILL). `Err(ESRCH)` — pid is
        // dead; the only case where removal is safe. `Err(EPERM)` —
        // pid is alive but owned by another uid; its debris is not
        // ours to touch. Any other errno treats the pid as alive and
        // preserves the debris (false negatives are recoverable;
        // false positives delete live state).
        let dead = matches!(
            nix::sys::signal::kill(nix::unistd::Pid::from_raw(pid), None),
            Err(nix::errno::Errno::ESRCH),
        );
        if !dead {
            continue;
        }
        let path = dir_entry.path();
        match fs::remove_dir_all(&path) {
            Ok(()) => {
                tracing::info!(
                    path = %path.display(),
                    orphan_pid = pid,
                    "cleaned orphaned .tmp- dir from prior crashed process",
                );
            }
            Err(e) => {
                tracing::warn!(
                    err = %format!("{e:#}"),
                    path = %path.display(),
                    "failed to remove orphaned .tmp- dir; leaving in place",
                );
            }
        }
    }
    Ok(())
}

/// Rejects empty keys, whitespace-only keys, keys starting with
/// `.tmp-` (reserved for in-progress stores), and keys containing
/// path separators (`/`, `\`), parent-directory traversal (`..`),
/// or null bytes. Returns `Ok(())` on valid keys.
fn validate_cache_key(key: &str) -> anyhow::Result<()> {
    if key.is_empty() || key.trim().is_empty() {
        anyhow::bail!("cache key must not be empty or whitespace-only");
    }
    if key.contains('/') || key.contains('\\') {
        anyhow::bail!("cache key must not contain path separators: {key:?}");
    }
    if key == "." || key == ".." {
        anyhow::bail!("cache key must not be a directory reference: {key:?}");
    }
    if key.contains("..") {
        anyhow::bail!("cache key must not contain path traversal: {key:?}");
    }
    if key.contains('\0') {
        anyhow::bail!("cache key must not contain null bytes");
    }
    if key.starts_with(TMP_DIR_PREFIX) {
        anyhow::bail!("cache key must not start with {TMP_DIR_PREFIX} (reserved): {key:?}",);
    }
    Ok(())
}

/// Validate a filename (e.g. image_name in metadata).
///
/// Rejects empty names, path separators (`/`, `\`), parent-directory
/// traversal (`..`), and null bytes to prevent path traversal when
/// joining the filename to a directory path.
fn validate_filename(name: &str) -> anyhow::Result<()> {
    if name.is_empty() {
        anyhow::bail!("image name must not be empty");
    }
    if name.contains('/') || name.contains('\\') {
        anyhow::bail!("image name must not contain path separators: {name:?}");
    }
    if name.contains("..") {
        anyhow::bail!("image name must not contain path traversal: {name:?}");
    }
    if name.contains('\0') {
        anyhow::bail!("image name must not contain null bytes");
    }
    Ok(())
}

/// RAII guard that removes a temporary directory on drop. Used by
/// [`CacheDir::store`] to clean up both serialization-failure
/// remnants and post-swap old cache content.
struct TmpDirGuard<'a>(&'a Path);

impl Drop for TmpDirGuard<'_> {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(self.0);
    }
}

/// Atomically swap two filesystem paths.
///
/// Wraps Linux `renameat2(AT_FDCWD, src, AT_FDCWD, dst,
/// RENAME_EXCHANGE)` via [`rustix::fs::renameat_with`]. Both paths
/// must already exist. On success the inodes they name are
/// exchanged in a single syscall — observers traversing either path
/// see the old target or the new one, never a missing or partial
/// entry.
///
/// Fails if the kernel does not support `RENAME_EXCHANGE`
/// (pre-3.15, `ENOSYS`), if either path is missing (`ENOENT`), or
/// if the two paths live on different filesystems (`EXDEV`). The
/// cache keeps both tmp_dir and final_dir under the same root, so
/// `EXDEV` would only fire on exotic bind mounts.
fn atomic_swap_dirs(src: &Path, dst: &Path) -> anyhow::Result<()> {
    rustix::fs::renameat_with(
        rustix::fs::CWD,
        src,
        rustix::fs::CWD,
        dst,
        rustix::fs::RenameFlags::EXCHANGE,
    )
    .map_err(|e| {
        anyhow::anyhow!(
            "renameat2(RENAME_EXCHANGE) {} <-> {}: {e}",
            src.display(),
            dst.display(),
        )
    })
}

/// Read and deserialize metadata.json from a cache entry directory.
///
/// On failure returns a human-readable reason suitable for surfacing
/// through [`ListedEntry::Corrupt::reason`]. The reason carries a
/// distinct prefix per failure mode so `kernel list` output and CI
/// logs can point the user at the specific cause:
/// - `"metadata.json missing"` — the file is absent (ENOENT), which
///   typically means the directory is not a cache entry at all
///   (scanner stumbled onto an unrelated dir).
/// - `"metadata.json unreadable: {e}"` — other I/O error from
///   `fs::read_to_string` (permissions, dangling symlink, etc.).
/// - `"metadata.json schema drift: {e}"` — serde_json's
///   `Category::Data` branch: JSON parsed cleanly but does not match
///   the [`KernelMetadata`] shape (missing required field, wrong
///   type on a present field). Signals a cache written by a ktstr
///   version whose `KernelMetadata` schema has since changed.
/// - `"metadata.json malformed: {e}"` — serde_json's
///   `Category::Syntax` branch: the file is not valid JSON at all
///   (stray characters, unbalanced braces, etc.).
/// - `"metadata.json truncated: {e}"` — serde_json's `Category::Eof`
///   branch: the file ends mid-value. Typical cause is a
///   partially-written metadata from a crashed store.
///
/// `Category::Io` is not reachable from `from_str`, which only sees
/// an in-memory `&str` — the I/O branch exists for `from_reader`
/// callers. If a future serde_json promotes `Category::Io` onto the
/// `from_str` path, the fallback arm in the match below keeps the
/// entry classified (generic "parse error") rather than panicking.
fn read_metadata(dir: &Path) -> Result<KernelMetadata, String> {
    let meta_path = dir.join("metadata.json");
    let contents = match fs::read_to_string(&meta_path) {
        Ok(c) => c,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
            return Err("metadata.json missing".to_string());
        }
        Err(e) => return Err(format!("metadata.json unreadable: {e}")),
    };
    serde_json::from_str(&contents).map_err(|e| match e.classify() {
        serde_json::error::Category::Data => format!("metadata.json schema drift: {e}"),
        serde_json::error::Category::Syntax => format!("metadata.json malformed: {e}"),
        serde_json::error::Category::Eof => format!("metadata.json truncated: {e}"),
        serde_json::error::Category::Io => {
            tracing::error!(
                err = %e,
                "serde_json::from_str returned Category::Io — unexpected for in-memory input",
            );
            format!("metadata.json parse error: {e}")
        }
    })
}

/// Re-route a cache-entry directory to its original source tree when
/// blazesym DWARF access is required.
///
/// Cache entries carry vmlinux stripped of `.debug_*` sections
/// (see [`strip_vmlinux_debug`]), so pointing blazesym at the cache
/// directory gives no file:line. [`KernelSource::Local`] entries record
/// the original build tree in the variant's `source_tree_path` field;
/// when it is populated and the tree still has an unstripped vmlinux
/// on disk, this helper returns that path so callers can route
/// blazesym there instead.
///
/// Returns `None` when:
/// - `dir` has no `metadata.json` (not a cache entry, e.g. a build-tree root).
/// - Metadata parse fails.
/// - `metadata.source` is not [`KernelSource::Local`] (tarball/git entries
///   — no original source tree to route to).
/// - `source_tree_path` is `None` on a Local entry (tree location not recorded).
/// - `source_tree_path` is set but has no `vmlinux` file (tree deleted
///   or rebuilt without saving vmlinux).
/// - The source-tree vmlinux's current size or mtime does NOT match
///   the values captured at cache-store time (see
///   [`KernelMetadata::source_vmlinux_size`] /
///   [`source_vmlinux_mtime_secs`](KernelMetadata::source_vmlinux_mtime_secs)).
///   A user rebuild between cache store and this lookup changes the
///   on-disk vmlinux; routing DWARF reads to a mismatched ELF would
///   silently surface line numbers from the wrong build. The cache
///   entry's stripped vmlinux always matches the cached BTF and is
///   the safe fallback.
/// - The cache entry's metadata did not record the source-tree
///   vmlinux size and mtime at store time (vmlinux not present in
///   the source tree when the cache entry was written). Without the
///   pair, the validation gate cannot prove the on-disk vmlinux
///   matches the cache; fall back to the cached stripped vmlinux.
///
/// In any of those cases callers should fall back to the cache
/// directory for symbol/BTF lookup — file:line is genuinely
/// unrecoverable without re-downloading sources.
pub fn prefer_source_tree_for_dwarf(dir: &Path) -> Option<PathBuf> {
    let metadata = read_metadata(dir).ok()?;
    let want_size = metadata.source_vmlinux_size?;
    let want_mtime = metadata.source_vmlinux_mtime_secs?;
    let KernelSource::Local {
        source_tree_path, ..
    } = metadata.source
    else {
        return None;
    };
    let src_path = source_tree_path?;
    let vmlinux = src_path.join("vmlinux");
    let stat = std::fs::metadata(&vmlinux).ok()?;
    if !stat.is_file() {
        return None;
    }
    if stat.len() != want_size {
        return None;
    }
    let cur_mtime = stat.modified().ok().and_then(|t| {
        t.duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .ok()
            .or_else(|| {
                // Pre-1970 mtime — represent the negative offset
                // from epoch via the symmetric earlier-than branch.
                std::time::UNIX_EPOCH
                    .duration_since(t)
                    .ok()
                    .map(|d| -(d.as_secs() as i64))
            })
    })?;
    if cur_mtime != want_mtime {
        return None;
    }
    Some(src_path)
}

/// Read `dir/metadata.json` and return the persisted source-tree
/// path when the entry was built from a local source tree
/// ([`KernelSource::Local`] with `source_tree_path: Some(_)`).
///
/// Used by callers that have a path which may be either a
/// kernel-cache entry directory (`<cache>/local-{hash7}-...`) or
/// a kernel source tree, and need the source-tree path either
/// way:
///
/// - When `dir` is a cache entry produced by `kernel build` /
///   `cargo ktstr test --kernel /path/to/linux`, the metadata
///   carries the original `source_tree_path` and this helper
///   returns it.
/// - When `dir` IS the source tree (no `metadata.json`, or
///   metadata that isn't `KernelSource::Local`, or a Local
///   entry with no recorded `source_tree_path`), this helper
///   returns `None` and the caller falls back to using `dir`
///   itself.
///
/// The two callers (cargo-ktstr.rs `gix::open(KTSTR_KERNEL)` site
/// for `cargo ktstr stats compare`, and
/// [`crate::test_support::sidecar::resolve_kernel_source_dir`]'s
/// Path arm for sidecar `kernel_commit` resolution) need the
/// same logic; centralising here avoids drift.
///
/// Distinct from [`prefer_source_tree_for_dwarf`]: that helper
/// gates on a present `vmlinux` file inside the source tree (DWARF
/// access requires the on-disk ELF). This helper does NOT — git
/// repo / source-tree probes (commit detection, tree opens) only
/// need the directory path, and the tree may be present without a
/// rebuilt vmlinux.
pub fn recover_local_source_tree(dir: &Path) -> Option<PathBuf> {
    let metadata = read_metadata(dir).ok()?;
    if let KernelSource::Local {
        source_tree_path: Some(p),
        ..
    } = metadata.source
    {
        return Some(p);
    }
    None
}

/// Structural ELF sections that must survive any cache-time strip
/// so that downstream readers can parse the result. Not tied to any
/// specific consumer — independent of monitor or probe code.
const STRUCTURAL_KEEP_SECTIONS: &[&[u8]] = &[
    b"",          // null section (index 0) — required by ELF spec
    b".shstrtab", // section header string table
];

/// Data sections retained as SHT_NOBITS headers with no current
/// consumer. Kept defensively so that symbols future kernels might
/// place here survive `Builder::delete_orphans`; remove an entry
/// only if no in-tree or upstream kernel version places monitored
/// symbols in it.
const SPECULATIVE_ZERO_DATA_SECTIONS: &[&[u8]] = &[b".init.data"];

/// Union of consumer-declared keep-lists plus structural sections.
///
/// Each consumer module owns the list of ELF sections it reads —
/// see [`crate::monitor::symbols::VMLINUX_KEEP_SECTIONS`],
/// [`crate::monitor::VMLINUX_KEEP_SECTIONS`], and
/// [`crate::probe::btf::VMLINUX_KEEP_SECTIONS`]. `is_keep_section`
/// unions them with [`STRUCTURAL_KEEP_SECTIONS`] at strip time.
///
/// Keep-list (vs remove-list) is safer: new debug or data sections
/// added by future compiler / kernel versions are stripped
/// automatically. Sections not matched by `is_keep_section` or
/// `is_zero_data_section` are deleted outright (non-code) or have
/// their bytes dropped via SHT_NOBITS (code — see [`strip_keep_list`]).
fn is_keep_section(name: &[u8]) -> bool {
    STRUCTURAL_KEEP_SECTIONS.contains(&name)
        || crate::monitor::symbols::VMLINUX_KEEP_SECTIONS.contains(&name)
        || crate::monitor::VMLINUX_KEEP_SECTIONS.contains(&name)
        || crate::probe::btf::VMLINUX_KEEP_SECTIONS.contains(&name)
}

/// Union of consumer-declared zero-data lists plus the speculative
/// retention set.
///
/// Data sections whose bytes are dropped (converted to SHT_NOBITS +
/// zero-length data) while their headers and addresses are
/// preserved. Monitor code reads only symbol addresses (`st_value`)
/// from these, never the backing bytes. Keeping the header lets
/// symbols with `st_shndx` pointing at them survive
/// `Builder::delete_orphans`.
///
/// See [`crate::monitor::symbols::VMLINUX_ZERO_DATA_SECTIONS`] for
/// the current consumer list and [`SPECULATIVE_ZERO_DATA_SECTIONS`]
/// for retained-without-consumer entries.
fn is_zero_data_section(name: &[u8]) -> bool {
    SPECULATIVE_ZERO_DATA_SECTIONS.contains(&name)
        || crate::monitor::symbols::VMLINUX_ZERO_DATA_SECTIONS.contains(&name)
}

/// Stripped vmlinux written to a temporary file.
///
/// Owns the backing [`tempfile::TempDir`] so the file stays alive
/// until the caller drops the [`StrippedVmlinux`]. Callers read the
/// stripped-vmlinux path via [`path`](Self::path) to pass into
/// [`CacheDir::store`] (or equivalent consumer) before the handle
/// goes out of scope.
#[derive(Debug)]
pub(crate) struct StrippedVmlinux {
    _tmp: tempfile::TempDir,
    path: PathBuf,
}

impl StrippedVmlinux {
    /// Path to the stripped vmlinux file inside the owned temp dir.
    pub fn path(&self) -> &Path {
        &self.path
    }
}

/// Strip vmlinux for caching. Sections fall into three groups:
/// bytes preserved (matched by [`is_keep_section`] — symbol tables,
/// BTF, `.rodata` for IKCONFIG, `.bss`, `.shstrtab`), header-only
/// via SHT_NOBITS (matched by [`is_zero_data_section`] plus all
/// code sections, so symbols with `st_shndx` pointing at them
/// survive), and deleted (everything else — DWARF `.debug_*`,
/// relocations, etc.). See [`strip_keep_list`] for the dispatch
/// detail.
///
/// The cached vmlinux is consumed by monitor and probe code for
/// symbol addresses (`.symtab`) and BTF type info (`.BTF`). DWARF
/// from the build tree (not the cache) is used by blazesym for probe
/// source locations — stripping the cached copy does not affect that
/// path.
///
/// If the keep-list strip fails (e.g. `Builder::read` encounters
/// an unsupported ELF feature), falls back to removing only
/// `.debug_*` sections, which preserves all other sections
/// including those the symbol table references.
///
/// Returns a [`StrippedVmlinux`] handle that owns the backing temp
/// directory; the caller must keep it alive until consumers (e.g.
/// [`CacheDir::store`]) have copied the file.
pub(crate) fn strip_vmlinux_debug(vmlinux_path: &Path) -> anyhow::Result<StrippedVmlinux> {
    let raw =
        fs::read(vmlinux_path).map_err(|e| anyhow::anyhow!("read vmlinux for stripping: {e}"))?;
    let original_size = raw.len();
    let data =
        neutralize_relocs(&raw).map_err(|e| anyhow::anyhow!("preprocess vmlinux ELF: {e}"))?;

    let out = match strip_keep_list(&data) {
        Ok(buf) => buf,
        Err(e) => {
            tracing::warn!("keep-list strip failed ({e:#}), falling back to debug-only strip");
            strip_debug_prefix(&data)?
        }
    };

    let stripped_size = out.len();
    let saved_mb = (original_size - stripped_size) as f64 / (1024.0 * 1024.0);
    tracing::debug!(
        original = original_size,
        stripped = stripped_size,
        saved_mb = format!("{saved_mb:.0}"),
        "strip_vmlinux_debug",
    );

    let tmp_dir = tempfile::TempDir::new()
        .map_err(|e| anyhow::anyhow!("create temp dir for stripped vmlinux: {e}"))?;
    let stripped_path = tmp_dir.path().join("vmlinux");
    fs::write(&stripped_path, &out).map_err(|e| anyhow::anyhow!("write stripped vmlinux: {e}"))?;
    Ok(StrippedVmlinux {
        _tmp: tmp_dir,
        path: stripped_path,
    })
}

/// Rewrite every relocation section (`SHT_REL`, `SHT_RELA`, `SHT_RELR`,
/// `SHT_CREL`) to `SHT_PROGBITS` with `sh_size = 0`, regardless of the
/// `SHF_ALLOC` flag, returning a modified copy of the bytes.
///
/// Workaround for `object::build::elf::Builder::read`. The Builder
/// routes sections by `sh_type` through two mechanisms that each pose
/// failure modes on malformed reloc inputs.
///
/// **`SHT_REL` / `SHT_RELA`** go through `section.rel()` /
/// `section.rela()` (object-0.37.3/src/build/elf.rs:187,198) which
/// call `data_as_array::<Rel|Rela<Endianness>>` against the raw
/// bytes. Four independent failure modes trip that call:
///
/// - `sh_offset + sh_size` byte range exceeds the file (arm64 kernel
///   7.0's observed failure mode, matches `"Invalid ELF relocation
///   section offset or size"`).
/// - `sh_size` not divisible by the entry size (24 bytes for `Rela64`,
///   16 for `Rel64`) — `slice_from_all_bytes` rejects the leftover
///   tail.
/// - Under ktstr's feature set (`default-features = false,
///   features = ["build"]`) the runtime-endian `Rela<Endianness>`
///   uses the non-`unaligned` `aligned` module in
///   object-0.37.3/src/endian.rs with `align_of == 8`. Even with
///   `sh_size == 0`, `read_bytes_at` returns a literal `Ok(&[])`
///   whose dangling pointer (`0x1` on x86_64) is NOT 8-aligned, so
///   `slice_from_all_bytes::<Rela64>` fails the alignment check.
///   Zeroing `sh_size` alone is insufficient under this feature
///   configuration.
/// - For allocated relocs with `sh_link == 0`,
///   `read_relocations_impl` bounds-checks each entry's `r_info`
///   symbol index against an empty dynamic symtab
///   (`symtab_len == 0`); any non-null index trips "Invalid symbol
///   index N in relocation section at index M".
///
/// **`SHT_RELR` / `SHT_CREL`** short-circuit both `section.rel()`
/// (returns `Ok(None)` because `sh_type != SHT_REL`) and
/// `section.rela()` (`sh_type != SHT_RELA`) in the same
/// `Builder::read` loop, then reach the sh_type match at
/// build/elf.rs:221 which dispatches to `section.data()`
/// (line 225-232) — the opaque-data path. Failure mode there is
/// `sh_offset + sh_size` past the file end, surfacing as
/// `"Invalid ELF section size or offset"`. Empty-slice alignment
/// does not apply (opaque byte reads have no entry alignment
/// requirement).
///
/// Rewriting `sh_type` to `SHT_PROGBITS` routes every matching section
/// through the same opaque-data path regardless of its original type:
/// `section.rel()` / `section.rela()` / `section.relr()` /
/// `section.crel()` all return `Ok(None)` immediately at their
/// respective sh_type mismatch guards (section.rs:829, 849, 867, 886),
/// the sh_type match at build/elf.rs:221 falls through to the
/// `SHT_PROGBITS` arm, and `section.data(endian, data)` returns
/// `Ok(&[])` via the zero-size short-circuit in `read_bytes_at` (line
/// 132) without any alignment check on the empty slice's pointer.
/// Builder::read succeeds; Builder::write reserves 0 bytes for the
/// opaque data and emits a well-formed section header.
///
/// Zeroing `sh_size` in the same pass ensures that if a downstream
/// reader re-parses the output with a strict "sh_offset + sh_size <=
/// file_len" check, the zero-length section passes trivially. The
/// section still carries its original name and file offset so the
/// keep-list or debug-prefix strip that runs next can delete it by
/// name.
///
/// The output is indistinguishable from a file that never had the
/// offending relocation sections: their bytes (pre-existing entries)
/// are left in place but orphaned, and the strip passes that follow
/// remove their headers by name. Neither monitor nor probe walks
/// relocation entries — the output is semantically equivalent.
///
/// No-op for ELFs that have no matching relocation sections (returns
/// the original bytes copied into a new `Vec`).
fn neutralize_relocs(data: &[u8]) -> anyhow::Result<Vec<u8>> {
    // SHT_RELR and SHT_CREL are not exposed by goblin; use the
    // object-crate constants (19 and 0x40000014 respectively, matching
    // object-0.37.3/src/elf.rs).
    const SHT_RELR: u32 = object::elf::SHT_RELR;
    const SHT_CREL: u32 = object::elf::SHT_CREL;
    // SHT_PROGBITS (value 1) is the "program data" section type —
    // opaque bytes that the Builder reads as `SectionData::Data` and
    // writes back verbatim. Rewriting reloc sections to this type
    // routes them through the opaque path, avoiding the rel/rela/relr/
    // crel parse branches that the empty-slice alignment pathology
    // breaks.
    const SHT_PROGBITS: u32 = goblin::elf::section_header::SHT_PROGBITS;

    let elf = goblin::elf::Elf::parse(data)
        .map_err(|e| anyhow::anyhow!("parse vmlinux ELF for preprocess: {e}"))?;
    let mut out = data.to_vec();
    let shoff = elf.header.e_shoff as usize;
    let shentsize = elf.header.e_shentsize as usize;
    // Section header field offsets (same order in ELF32 and ELF64, width
    // differs on the 64-bit `sh_flags`/`sh_addr`/`sh_offset` path).
    // ELF64: sh_name(4) sh_type(4) sh_flags(8) sh_addr(8) sh_offset(8)
    //        sh_size(8) ... -> sh_size at offset 32.
    // ELF32: sh_name(4) sh_type(4) sh_flags(4) sh_addr(4) sh_offset(4)
    //        sh_size(4) ... -> sh_size at offset 20.
    // sh_type is at offset 4 with width 4 in both layouts (the ELF
    // spec fixes it at `Elf{32,64}_Word = u32`).
    let (sh_size_offset, sh_size_width) = if elf.is_64 { (32, 8) } else { (20, 4) };
    let sh_type_offset: usize = 4;
    let sh_type_width: usize = 4;
    // sh_type is stored little- or big-endian per the ELF header's
    // e_ident[EI_DATA]. goblin::elf::Elf's `little_endian` field
    // reports the observed endianness.
    let le = elf.little_endian;
    use goblin::elf::section_header::{SHT_REL, SHT_RELA};
    for (i, sh) in elf.section_headers.iter().enumerate() {
        let is_reloc = matches!(sh.sh_type, SHT_REL | SHT_RELA | SHT_RELR | SHT_CREL);
        if !is_reloc {
            continue;
        }
        let entry_offset = shoff
            .checked_add(
                i.checked_mul(shentsize)
                    .ok_or_else(|| anyhow::anyhow!("section header table overflow at index {i}"))?,
            )
            .ok_or_else(|| anyhow::anyhow!("section header offset overflow at index {i}"))?;
        let type_offset = entry_offset
            .checked_add(sh_type_offset)
            .ok_or_else(|| anyhow::anyhow!("sh_type offset overflow at index {i}"))?;
        let type_end = type_offset
            .checked_add(sh_type_width)
            .ok_or_else(|| anyhow::anyhow!("sh_type end overflow at index {i}"))?;
        let size_offset = entry_offset
            .checked_add(sh_size_offset)
            .ok_or_else(|| anyhow::anyhow!("sh_size offset overflow at index {i}"))?;
        let size_end = size_offset
            .checked_add(sh_size_width)
            .ok_or_else(|| anyhow::anyhow!("sh_size end overflow at index {i}"))?;
        if type_end > out.len() || size_end > out.len() {
            anyhow::bail!("section header {i} sh_type or sh_size field extends past file end");
        }
        // Write sh_type = SHT_PROGBITS (u32, per the ELF spec for
        // Elf{32,64}_Word) in the file's endianness.
        let type_bytes: [u8; 4] = if le {
            SHT_PROGBITS.to_le_bytes()
        } else {
            SHT_PROGBITS.to_be_bytes()
        };
        out[type_offset..type_end].copy_from_slice(&type_bytes);
        // Zero sh_size (endian-agnostic: all bytes are zero).
        out[size_offset..size_end].fill(0);
    }
    Ok(out)
}

/// Keep-list strip: three-way partition of ELF sections.
///
/// Sections matched by [`is_keep_section`] keep their bytes (symbol
/// tables, BTF, `.shstrtab`, `.rodata` for IKCONFIG, `.bss` already
/// SHT_NOBITS).
///
/// Sections matched by [`is_zero_data_section`] have their headers
/// preserved but bytes dropped via `SHT_NOBITS` + zero-length data.
/// Monitor code reads symbol addresses (`st_value`) from these, never
/// the backing bytes.
///
/// Code sections (`SHF_EXECINSTR`: `.text`, `.init.text`,
/// `.exit.text`, `.text.hot`, `.altinstr_replacement`, etc.) receive
/// the same SHT_NOBITS treatment so that ~115k function symbols
/// pointing into them (`schedule`, `__schedule`, etc.) survive
/// `Builder::delete_orphans` — the auto-pass at the top of
/// `Builder::write` that drops any symbol whose section was deleted.
/// Without this, `resolve_addrs_from_elf` (probe/output.rs) returns
/// an empty vec for any kernel function lookup.
///
/// Everything else is deleted outright (DWARF `.debug_*`, relocation
/// sections, etc.).
///
/// After stripping, checks the result has a non-empty symbol table.
/// Returns an error to trigger the fallback to `strip_debug_prefix`
/// if the symbol table is empty.
fn strip_keep_list(data: &[u8]) -> anyhow::Result<Vec<u8>> {
    let mut builder = object::build::elf::Builder::read(data)
        .map_err(|e| anyhow::anyhow!("parse vmlinux ELF: {e}"))?;
    for section in builder.sections.iter_mut() {
        let name = section.name.as_slice();
        if is_keep_section(name) {
            continue;
        }
        if is_zero_data_section(name) {
            section.sh_type = object::elf::SHT_NOBITS;
            section.data = object::build::elf::SectionData::UninitializedData(0);
            continue;
        }
        let is_code = section.sh_flags & u64::from(object::elf::SHF_EXECINSTR) != 0;
        if is_code {
            section.sh_type = object::elf::SHT_NOBITS;
            section.data = object::build::elf::SectionData::UninitializedData(0);
        } else {
            section.delete = true;
        }
    }

    // Check symtab survivors BEFORE writing. The `Builder` state
    // already holds every parsed symbol; counting named, non-deleted
    // entries here lets us fail fast without the post-write
    // `goblin::elf::Elf::parse(&out)` we used to run. The null symbol
    // (index 0) is filtered via the empty-name check, matching the
    // semantics of the old check pass.
    let named_syms = builder
        .symbols
        .iter()
        .filter(|s| !s.delete && !s.name.as_slice().is_empty())
        .count();
    if named_syms == 0 {
        anyhow::bail!("keep-list strip emptied symbol table (0 named symbols)");
    }

    let mut out = Vec::new();
    builder
        .write(&mut out)
        .map_err(|e| anyhow::anyhow!("rewrite stripped vmlinux: {e}"))?;
    Ok(out)
}

/// Fallback strip: remove `.debug_*`, `.comment`, and neutralized
/// relocation sections (`.rela.*`, `.rel.*`, `.relr.*`, `.crel.*`).
/// Uses the shared [`crate::elf_strip::rewrite`] primitive.
///
/// The reloc-prefix arms delete the header entries that
/// [`neutralize_relocs`] left behind with `sh_size = 0`. Without this
/// pass the fallback output carries one zero-size ghost section
/// header per reloc section — parseable but wasted section-header
/// bytes that consumers never walk (neither monitor nor probe reads
/// relocation entries).
fn strip_debug_prefix(data: &[u8]) -> anyhow::Result<Vec<u8>> {
    crate::elf_strip::rewrite(data, |name| {
        name.starts_with(b".debug_")
            || name == b".comment"
            || name.starts_with(b".rela.")
            || name.starts_with(b".rel.")
            || name.starts_with(b".relr.")
            || name.starts_with(b".crel.")
    })
    .map_err(|e| anyhow::anyhow!("rewrite stripped vmlinux (fallback): {e}"))
}

/// Resolve the cache root directory path with a per-cache `suffix`
/// (`"kernels"` for the kernel cache, `"models"` for the model cache).
///
/// Single source of truth for env-variable handling and HOME
/// validation across both ktstr cache flavors. Both
/// [`resolve_cache_root`] (kernel cache) and
/// [`crate::test_support::model::resolve_cache_root`] (model cache)
/// route through here so a future change to environment-variable
/// semantics or HOME validation lands once.
///
/// Resolution cascade:
/// 1. `KTSTR_CACHE_DIR` (with non-UTF-8 bail). The override returns
///    the path verbatim — no `suffix` is appended, since the
///    operator who sets `KTSTR_CACHE_DIR` is naming the literal
///    cache root they want. The kernel cache and the model cache
///    co-locate under the same root in this case but never collide
///    on filesystem paths because cache keys (kernel build hashes)
///    and GGUF filenames live in disjoint name spaces under the
///    root.
/// 2. `XDG_CACHE_HOME/ktstr/{suffix}` when set and non-empty.
/// 3. `$HOME/.cache/ktstr/{suffix}` after HOME validation (3 arms:
///    unset/empty, literal `/`, non-absolute path — see body
///    comments for the per-shape rationale).
///
/// Does not create the directory — the caller is responsible for
/// ensuring it exists.
///
/// Non-UTF-8 handling: `KTSTR_CACHE_DIR` is the explicit operator
/// override and a silent fall-through on a non-UTF-8 value would
/// leave the operator debugging why their override "didn't work"
/// when the fallback `$HOME/.cache/ktstr` took over instead.
/// [`crate::test_support::test_helpers::EnvVarGuard`] accepts
/// arbitrary `OsStr`, so a test fixture or a real-world
/// locale-encoded path can legally put non-UTF-8 bytes in this
/// variable. A non-UTF-8 `KTSTR_CACHE_DIR` surfaces as an
/// actionable error naming the variable and pointing at a UTF-8
/// replacement path — the override never fails silently.
pub(crate) fn resolve_cache_root_with_suffix(suffix: &str) -> anyhow::Result<PathBuf> {
    // 1. Explicit override.
    match std::env::var("KTSTR_CACHE_DIR") {
        Ok(dir) if !dir.is_empty() => return Ok(PathBuf::from(dir)),
        Ok(_) => { /* empty string -> fall through to fallbacks */ }
        Err(std::env::VarError::NotPresent) => { /* unset -> fall through */ }
        Err(std::env::VarError::NotUnicode(raw)) => {
            anyhow::bail!(
                "KTSTR_CACHE_DIR contains non-UTF-8 bytes ({} bytes): {:?}. \
                 ktstr requires a UTF-8 cache path — set KTSTR_CACHE_DIR \
                 to an ASCII/UTF-8 directory (e.g. `/tmp/ktstr-cache`) or \
                 unset it to fall back to $XDG_CACHE_HOME/$HOME.",
                raw.len(),
                raw,
            );
        }
    }
    // 2. XDG_CACHE_HOME/ktstr/{suffix}.
    if let Ok(xdg) = std::env::var("XDG_CACHE_HOME")
        && !xdg.is_empty()
    {
        return Ok(PathBuf::from(xdg).join("ktstr").join(suffix));
    }
    // 3. $HOME/.cache/ktstr/{suffix}.
    //
    // HOME shape validation lives in [`validate_home_for_cache`]
    // below — it reads `HOME` and rejects unset/empty, literal
    // `/`, and non-absolute values, returning the validated
    // `PathBuf` for the caller to extend with the cache-flavor
    // suffix. The gate is INTENTIONALLY NARROW: it catches only
    // shapes where the resulting cache path is structurally wrong
    // before the filesystem has had a chance to surface its own
    // diagnostic. Other malformed HOME values (non-existent
    // directory, path pointing at a non-directory, kernel pseudo-
    // filesystem like /proc) pass through; downstream
    // open()/statvfs() surface the real error with the offending
    // path embedded so the operator sees what HOME actually
    // expanded to.
    //
    // Cases this gate does NOT catch (intentional):
    //
    // - `HOME=/nonexistent`: ENOENT at use-time, with the offending
    //   path in the error message. Pre-validating with `Path::exists`
    //   would race against an operator who creates the directory
    //   between our check and the use site, OR mask a stale-NFS
    //   timeout that would otherwise surface as a real error.
    // - `HOME=/dev/null`: ENOTDIR at use-time. Same race rationale
    //   as `/nonexistent`; pre-flighting with metadata() adds a
    //   syscall on every cache lookup for a vanishingly rare case.
    // - `HOME=/proc`, `HOME=/sys`, `HOME=/`-rooted pseudo-fs paths:
    //   these surface as EROFS or EACCES on write attempts. The
    //   error path is well-described by the OS-level diagnostic;
    //   adding a special case here would be churn without payoff.
    // - `HOME=//`, `HOME=/./`, `HOME=/.`: these expand to root via
    //   POSIX path normalization. PathBuf does not normalize at
    //   `from`, so the literal junk-path lands on the filesystem
    //   layer. We could canonicalize() here to catch them, but
    //   canonicalize is a syscall on every cache lookup and these
    //   shapes are rare enough (operator typo or shell quoting
    //   accident) that the OS-level "operation not permitted on
    //   /.cache" suffices.
    let home = validate_home_for_cache()?;
    Ok(home.join(".cache").join("ktstr").join(suffix))
}

/// Read `HOME` from the environment, reject values that produce a
/// guaranteed-junk cache path, and return the validated `PathBuf`.
///
/// On `Ok` the returned PathBuf is `PathBuf::from(<HOME value>)` —
/// the caller appends the cache-flavor suffix (e.g. `.cache/ktstr/kernels`).
/// On `Err` the diagnostic names the specific rejection case so the
/// operator can fix HOME or set `KTSTR_CACHE_DIR` / `XDG_CACHE_HOME`
/// instead.
///
/// Four rejected shapes (full rationale in the body comments
/// above [`resolve_cache_root_with_suffix`]'s HOME-fallback site,
/// repeated near the matching check below):
/// 1. **Unset** (`Err(NotPresent)` — HOME never assigned in the
///    process environment). The body distinguishes this from the
///    set-but-empty case with a dedicated diagnostic so the operator
///    can tell whether the variable is missing entirely or assigned
///    to the empty string by an inherited shell.
/// 2. **Set-but-empty** (`Ok("")` — HOME assigned to the empty
///    string, often by a shell rc file that did `export HOME=` to
///    "unset" it). Same downstream effect as unset but a different
///    operator-facing root cause.
/// 3. **Literal `/`** (root user / container init with no home).
/// 4. **Non-absolute** (CWD-relative cache state — silently
///    relocates as the operator changes directories).
///
/// Cases this gate INTENTIONALLY does not catch (`HOME=/nonexistent`,
/// `HOME=/dev/null`, `HOME=/proc`, `HOME=//`, `HOME=/.`, etc.) are
/// deferred to filesystem-level errors at use time — see the call
/// site's comment block for the per-shape rationale.
///
/// `pub(crate)` because both the kernel cache (this module) and
/// the model cache ([`crate::test_support::model::resolve_cache_root`])
/// reach this helper transitively through
/// [`resolve_cache_root_with_suffix`]. There is now exactly one
/// place that defines what "valid HOME for ktstr cache derivation"
/// means; a future change to the validation policy lands once and
/// flows through both cache flavors.
pub(crate) fn validate_home_for_cache() -> anyhow::Result<PathBuf> {
    // Distinguish unset (`Err(NotPresent)` — HOME never assigned in
    // the process environment) from empty (`Ok("")` — HOME assigned
    // but to the empty string) so the operator's diagnostic names
    // the actual misconfiguration shape. The two failure modes
    // arise from different bugs in practice:
    //   - Unset: container init dropped HOME (`docker run` without
    //     `-e HOME=...`, systemd unit without `Environment=HOME=...`).
    //   - Empty: a Dockerfile `ENV HOME=` line, a shell rc that
    //     accidentally `unset`s HOME via assignment-expansion (e.g.
    //     `export HOME=$HOME_OVERRIDE` when `HOME_OVERRIDE` is unset
    //     under `set -u` semantics inverted).
    // Non-UTF-8 HOME (`Err(NotUnicode)`) is treated as unset for
    // diagnostic purposes — the operator's remediation is the same
    // (set KTSTR_CACHE_DIR / XDG_CACHE_HOME), and naming the raw
    // bytes here would surface environment-leakage in the error.
    let home = match std::env::var("HOME") {
        Ok(v) if !v.is_empty() => v,
        Ok(_) => {
            // Empty assignment.
            anyhow::bail!(
                "HOME is set to the empty string; cannot resolve cache directory. \
                 An empty HOME usually means a Dockerfile or shell rc has \
                 `export HOME=` or `ENV HOME=` with no value. Either set HOME \
                 to a real absolute path, or set KTSTR_CACHE_DIR to an absolute \
                 path (e.g. /tmp/ktstr-cache) or XDG_CACHE_HOME to specify a \
                 cache location explicitly."
            );
        }
        Err(_) => {
            // Unset (NotPresent) or non-UTF-8 (NotUnicode) — both
            // surface as "no usable HOME for derivation" with the
            // same remediation.
            anyhow::bail!(
                "HOME is unset; cannot resolve cache directory. \
                 The container init or login shell did not assign HOME — set \
                 it to an absolute path, or set KTSTR_CACHE_DIR to an absolute \
                 path (e.g. /tmp/ktstr-cache) or XDG_CACHE_HOME to specify a \
                 cache location explicitly."
            );
        }
    };
    // 2. Literal `/`: a process-environment artifact (root user with
    //    no home dir, container init that didn't set HOME).
    //    `PathBuf::from("/").join(".cache")` yields `/.cache` —
    //    statvfs reports the root filesystem's free space, not a
    //    usable user-cache filesystem; cache writes also escalate
    //    to root-fs writes.
    if home == "/" {
        anyhow::bail!(
            "HOME is `/`; the resulting cache path /.cache/ktstr would alias the \
             root filesystem rather than naming a user cache. This usually means \
             the process inherited HOME from a container init or root login that \
             did not set a real home. Set KTSTR_CACHE_DIR to an absolute path \
             (e.g. /tmp/ktstr-cache) or XDG_CACHE_HOME to bypass HOME entirely."
        );
    }
    // 3. Relative path: `PathBuf::from("relative").join(".cache")`
    //    yields `relative/.cache` resolved against CWD at every
    //    call. Cache state would silently relocate as the operator
    //    moves between directories — a usability nightmare worse
    //    than the deferred-error case. POSIX HOME is documented as
    //    an absolute pathname.
    if !home.starts_with('/') {
        anyhow::bail!(
            "HOME={home:?} is not an absolute path; ktstr requires HOME to start \
             with `/` so the cache root resolves consistently regardless of the \
             current working directory. Set HOME to an absolute path, or set \
             KTSTR_CACHE_DIR / XDG_CACHE_HOME to a specific cache location."
        );
    }
    Ok(PathBuf::from(home))
}

/// Resolve the kernel cache root directory path.
///
/// Thin wrapper over [`resolve_cache_root_with_suffix`] with the
/// `"kernels"` suffix. The model cache uses the same helper with
/// the `"models"` suffix from
/// [`crate::test_support::model::resolve_cache_root`]; both share the
/// `KTSTR_CACHE_DIR` / `XDG_CACHE_HOME` / `HOME` cascade verbatim.
///
/// Does not create the directory -- the caller is responsible for
/// ensuring it exists.
fn resolve_cache_root() -> anyhow::Result<PathBuf> {
    resolve_cache_root_with_suffix("kernels")
}

/// Is `p` (a file path) located inside the kernel cache root?
///
/// Used by callers that derive sibling artifacts next to a vmlinux on
/// disk (e.g. [`crate::monitor::btf_offsets::load_btf_from_path`]'s
/// `<vmlinux>.btf` sidecar) to avoid writing into directories the
/// cache does not own — most importantly, kernel source trees and
/// distro-installed debug paths supplied via [`crate::vmm::find_vmlinux`]
/// fallbacks. A bare textual prefix would misclassify paths whose
/// parent directory contains a symlink component, or paths spelled
/// with `..`/`.` segments, so this canonicalizes both the cache
/// root and `p`'s parent directory before comparing them with
/// [`Path::starts_with`] (component-based, robust against trailing
/// slash differences).
///
/// `p` ITSELF is NOT canonicalized. Only `p.parent()` is resolved
/// through the symlink layer. Callers that need symlink-following
/// semantics on `p` itself — e.g. `p` is a symlink whose target
/// lives in a different membership bucket than its lexical parent —
/// MUST canonicalize `p` before calling this helper, as
/// [`crate::monitor::btf_offsets::load_btf_from_path`] does.
///
/// Both canonicalizations must succeed — any error (cache root
/// unset, HOME unresolvable, parent missing, EACCES, etc.) returns
/// `false` so that downstream sidecar writes are suppressed rather
/// than landing in an unintended directory. The skip is logged at
/// `tracing::debug` so an operator debugging "why is BTF
/// re-extracted every load?" sees the cause without surfacing
/// noise on the steady-state path.
///
/// Resolution is performed fresh on every call — `KTSTR_CACHE_DIR`
/// can change between test invocations and during process lifetime,
/// so caching the resolved root would surface stale answers.
pub(crate) fn path_inside_cache_root(p: &Path) -> bool {
    let root = match resolve_cache_root() {
        Ok(r) => r,
        Err(e) => {
            tracing::debug!(
                err = %e,
                "cache root unresolvable; treating path as outside cache",
            );
            return false;
        }
    };
    let canon_root = match fs::canonicalize(&root) {
        Ok(r) => r,
        Err(e) => {
            tracing::debug!(
                root = %root.display(),
                err = %e,
                "cache root canonicalize failed; treating path as outside cache",
            );
            return false;
        }
    };
    // Canonicalize the parent directory and compare against the
    // canonical cache root. The PARENT's canonical form is what the
    // membership decision is based on — a symlinked parent that
    // resolves into the cache classifies as in-cache, and vice
    // versa.
    //
    // Note: this helper does NOT canonicalize `p` itself. If `p`
    // is a regular file under a real (non-symlink) parent, the
    // parent's canonical form fully determines the answer. If `p`
    // IS a symlink (e.g. `/source-tree/vmlinux` ->
    // `/cache/entry/vmlinux`), the helper would classify based on
    // the lexical parent (`/source-tree`), which is NOT what
    // callers usually want. Callers that need symlink-following
    // semantics on `p` itself must canonicalize `p` before
    // calling — see [`crate::monitor::btf_offsets::load_btf_from_path`]
    // for an example.
    let parent = match p.parent() {
        Some(p) if !p.as_os_str().is_empty() => p,
        // Bare filename or filesystem root — neither can sit "inside"
        // the cache by construction.
        _ => return false,
    };
    let canon_parent = match fs::canonicalize(parent) {
        Ok(p) => p,
        Err(e) => {
            tracing::debug!(
                parent = %parent.display(),
                err = %e,
                "input path parent canonicalize failed; treating as outside cache",
            );
            return false;
        }
    };
    canon_parent.starts_with(&canon_root)
}

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

    /// Decode an ELF section `sh_type` integer to its `SHT_*` constant
    /// name. Strip-helper assertions embed the decoded name alongside
    /// the raw integer so a failing diagnostic like "left: 8 right: 1"
    /// reads as "sh_type=8 (SHT_NOBITS)" / "sh_type=1 (SHT_PROGBITS)"
    /// — immediately actionable instead of requiring the reader to
    /// look up the ELF spec table.
    fn sh_type_name(t: u32) -> &'static str {
        use goblin::elf::section_header::{
            SHT_DYNAMIC, SHT_DYNSYM, SHT_HASH, SHT_NOBITS, SHT_NOTE, SHT_NULL, SHT_PROGBITS,
            SHT_REL, SHT_RELA, SHT_SHLIB, SHT_STRTAB, SHT_SYMTAB,
        };
        match t {
            SHT_NULL => "SHT_NULL",
            SHT_PROGBITS => "SHT_PROGBITS",
            SHT_SYMTAB => "SHT_SYMTAB",
            SHT_STRTAB => "SHT_STRTAB",
            SHT_RELA => "SHT_RELA",
            SHT_HASH => "SHT_HASH",
            SHT_DYNAMIC => "SHT_DYNAMIC",
            SHT_NOTE => "SHT_NOTE",
            SHT_NOBITS => "SHT_NOBITS",
            SHT_REL => "SHT_REL",
            SHT_SHLIB => "SHT_SHLIB",
            SHT_DYNSYM => "SHT_DYNSYM",
            _ => "SHT_UNKNOWN",
        }
    }

    fn test_metadata(version: &str) -> KernelMetadata {
        KernelMetadata {
            version: Some(version.to_string()),
            source: KernelSource::Tarball,
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: Some("abc123".to_string()),
            built_at: "2026-04-12T10:00:00Z".to_string(),
            ktstr_kconfig_hash: Some("def456".to_string()),
            has_vmlinux: false,
            vmlinux_stripped: false,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        }
    }

    fn create_fake_image(dir: &Path) -> PathBuf {
        let image = dir.join("bzImage");
        fs::write(&image, b"fake kernel image").unwrap();
        image
    }

    /// Build a minimal ELF object with a single `.text` section (64
    /// bytes of 0xCC) anchored by one symbol. The anchor symbol is
    /// what drives `object::write` to emit `.symtab`/`.strtab`, and
    /// every `neutralize_relocs` test shares this base shape.
    /// Callers that need relocation sections (with or without
    /// `SHF_ALLOC`) add them on top of the returned object before
    /// calling `.write()`.
    ///
    /// `arch` selects the ELF class: `Architecture::X86_64` yields
    /// ELF64 (8-byte anchor symbol), `Architecture::I386` yields
    /// ELF32 (4-byte anchor symbol). The anchor-symbol size is the
    /// only shape difference between the two classes at this
    /// fixture level; everything downstream (section headers, the
    /// `is_reloc` predicate under test) is driven by the
    /// ELF32/ELF64 split `object::write` performs based on `arch`.
    fn build_base_elf_with_text_symbol(
        arch: object::Architecture,
    ) -> object::write::Object<'static> {
        use object::write;
        let sym_size = match arch {
            object::Architecture::X86_64 => 8,
            object::Architecture::I386 => 4,
            other => panic!(
                "build_base_elf_with_text_symbol: unsupported arch {other:?}; supported: X86_64, I386",
            ),
        };
        let mut obj =
            write::Object::new(object::BinaryFormat::Elf, arch, object::Endianness::Little);
        let text_id = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
        obj.append_section_data(text_id, &[0xCC; 64], 1);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_text_symbol".to_vec(),
            value: 0x0,
            size: sym_size,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(text_id),
            flags: object::SymbolFlags::None,
        });
        obj
    }

    /// Regression pin for the explicit `other =>` arm in
    /// [`build_base_elf_with_text_symbol`]. Before the guard, an
    /// unsupported architecture silently fell through to `sym_size = 8`
    /// which is wrong for any future 32-bit arch (or any arch whose
    /// address width isn't 8 bytes). `Aarch64` is a supported object
    /// crate architecture that isn't on the helper's allow-list, so
    /// passing it triggers the panic and the `#[should_panic]`
    /// assertion confirms the guard fires.
    #[test]
    #[should_panic(expected = "unsupported arch")]
    fn build_base_elf_with_text_symbol_panics_on_unsupported_arch() {
        let _ = build_base_elf_with_text_symbol(object::Architecture::Aarch64);
    }

    // -- keep-list source disjointness --

    /// Every entry in `is_keep_section` comes from one of four source
    /// lists owned by independent modules. Overlap is harmless at
    /// strip time but masks drift: if two modules add `.foo` and one
    /// later removes it, the strip still preserves `.foo` via the
    /// other module — the "dead" reference outlives its reader.
    ///
    /// This test locks the four lists as disjoint sets so a removal
    /// in one module immediately drops `.foo` from
    /// `is_keep_section`, and the downstream consumer's loss of its
    /// declared section becomes a visible test break rather than a
    /// silent ALL-tests-pass-but-data-is-missing runtime surprise.
    #[test]
    fn keep_section_sources_are_disjoint() {
        use std::collections::HashMap;
        let mut origins: HashMap<&[u8], Vec<&str>> = HashMap::new();
        let sources: &[(&str, &[&[u8]])] = &[
            ("cache::STRUCTURAL_KEEP_SECTIONS", STRUCTURAL_KEEP_SECTIONS),
            (
                "monitor::symbols::VMLINUX_KEEP_SECTIONS",
                crate::monitor::symbols::VMLINUX_KEEP_SECTIONS,
            ),
            (
                "monitor::VMLINUX_KEEP_SECTIONS",
                crate::monitor::VMLINUX_KEEP_SECTIONS,
            ),
            (
                "probe::btf::VMLINUX_KEEP_SECTIONS",
                crate::probe::btf::VMLINUX_KEEP_SECTIONS,
            ),
        ];
        for (label, list) in sources {
            for name in *list {
                origins.entry(*name).or_default().push(label);
            }
        }
        let dupes: Vec<_> = origins
            .iter()
            .filter(|(_, lists)| lists.len() > 1)
            .collect();
        assert!(
            dupes.is_empty(),
            "keep-list entries declared by multiple source modules (drift hazard): {dupes:?}",
        );
    }

    /// Same disjointness contract for the two zero-data lists.
    /// Retained sections here keep symbols but drop bytes — duplicate
    /// declarations would mask the same drift the keep-list test
    /// guards against.
    #[test]
    fn zero_data_section_sources_are_disjoint() {
        use std::collections::HashSet;
        let speculative: HashSet<&[u8]> = SPECULATIVE_ZERO_DATA_SECTIONS.iter().copied().collect();
        let declared: HashSet<&[u8]> = crate::monitor::symbols::VMLINUX_ZERO_DATA_SECTIONS
            .iter()
            .copied()
            .collect();
        let overlap: Vec<_> = speculative.intersection(&declared).collect();
        assert!(
            overlap.is_empty(),
            "zero-data section declared by both SPECULATIVE and a consumer (drift hazard): {overlap:?}",
        );
    }

    // -- KernelMetadata serde --

    #[test]
    fn cache_metadata_serde_roundtrip() {
        let meta = test_metadata("6.14.2");
        let json = serde_json::to_string_pretty(&meta).unwrap();
        let parsed: KernelMetadata = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.version.as_deref(), Some("6.14.2"));
        assert_eq!(parsed.source, KernelSource::Tarball);
        assert_eq!(parsed.arch, "x86_64");
        assert_eq!(parsed.image_name, "bzImage");
        assert_eq!(parsed.config_hash.as_deref(), Some("abc123"));
        assert_eq!(parsed.built_at, "2026-04-12T10:00:00Z");
        assert_eq!(parsed.ktstr_kconfig_hash.as_deref(), Some("def456"));
        assert!(!parsed.has_vmlinux);
        assert!(!parsed.vmlinux_stripped);
    }

    #[test]
    fn cache_metadata_serde_git_with_payload() {
        let meta = KernelMetadata {
            version: Some("6.15-rc3".to_string()),
            source: KernelSource::Git {
                git_hash: Some("a1b2c3d".to_string()),
                git_ref: Some("v6.15-rc3".to_string()),
            },
            arch: "aarch64".to_string(),
            image_name: "Image".to_string(),
            config_hash: None,
            built_at: "2026-04-12T12:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: false,
            vmlinux_stripped: false,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        };
        let json = serde_json::to_string(&meta).unwrap();
        let parsed: KernelMetadata = serde_json::from_str(&json).unwrap();
        assert!(matches!(
            parsed.source,
            KernelSource::Git {
                git_hash: Some(ref h),
                git_ref: Some(ref r),
            }
            if h == "a1b2c3d" && r == "v6.15-rc3"
        ));
    }

    #[test]
    fn cache_metadata_serde_local_with_source_tree() {
        let meta = KernelMetadata {
            version: Some("6.14.0".to_string()),
            source: KernelSource::Local {
                source_tree_path: Some(PathBuf::from("/tmp/linux")),
                git_hash: Some("deadbee".to_string()),
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: Some("fff000".to_string()),
            built_at: "2026-04-12T14:00:00Z".to_string(),
            ktstr_kconfig_hash: Some("aaa111".to_string()),
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        };
        let json = serde_json::to_string(&meta).unwrap();
        let parsed: KernelMetadata = serde_json::from_str(&json).unwrap();
        assert!(matches!(
            parsed.source,
            KernelSource::Local {
                source_tree_path: Some(ref p),
                git_hash: Some(ref h),
            }
            if p == &PathBuf::from("/tmp/linux") && h == "deadbee"
        ));
        assert!(parsed.has_vmlinux);
        assert!(parsed.vmlinux_stripped);
    }

    /// git_hash on KernelSource::Local is a plain Option<String> with
    /// no serde attributes — the compat shims (serde(default) +
    /// skip_serializing_if) were removed for pre-1.0, so `None`
    /// serializes as an explicit `null` key and deserialization
    /// accepts `null` back as `None`. This test pins only the
    /// None → null → None round trip; the absent-key branch is
    /// exercised separately by
    /// [`kernel_source_absent_option_keys_deserialize_as_none`].
    #[test]
    fn kernel_source_local_git_hash_serde_round_trip_none() {
        let src = KernelSource::Local {
            source_tree_path: Some(PathBuf::from("/tmp/linux")),
            git_hash: None,
        };
        let json = serde_json::to_string(&src).unwrap();
        assert!(
            json.contains(r#""git_hash":null"#),
            "git_hash=None must round-trip as explicit null, got {json}"
        );
        let parsed: KernelSource = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, KernelSource::Local { git_hash: None, .. }));
    }

    /// Pins the post-shim wire format: `Option` payload fields inside
    /// every [`KernelSource`] variant serialize as explicit `null`
    /// rather than being omitted. The `serde(default)` /
    /// `skip_serializing_if` compat shims were removed for pre-1.0;
    /// [`kernel_source_local_git_hash_serde_round_trip_none`] already
    /// covers the round-trip statement for the single Local.git_hash
    /// slot. This test extends that guarantee across every Option
    /// payload on both Git and Local so `cargo ktstr kernel list
    /// --json` consumers see stable key presence regardless of which
    /// optional values are set — absent keys would mean the emitted
    /// schema has silently regressed.
    #[test]
    fn kernel_source_option_fields_serialize_as_explicit_null() {
        let local = KernelSource::Local {
            source_tree_path: None,
            git_hash: None,
        };
        let local_json = serde_json::to_string(&local).unwrap();
        assert!(
            local_json.contains(r#""source_tree_path":null"#),
            "Local.source_tree_path=None must serialize as explicit null, got {local_json}"
        );
        assert!(
            local_json.contains(r#""git_hash":null"#),
            "Local.git_hash=None must serialize as explicit null, got {local_json}"
        );

        let git = KernelSource::Git {
            git_hash: None,
            git_ref: None,
        };
        let git_json = serde_json::to_string(&git).unwrap();
        assert!(
            git_json.contains(r#""git_hash":null"#),
            "Git.git_hash=None must serialize as explicit null, got {git_json}"
        );
        // The struct field is `git_ref` but `#[serde(rename = "ref")]`
        // renames the JSON key — check the renamed key, not the field.
        assert!(
            git_json.contains(r#""ref":null"#),
            "Git.git_ref=None must serialize as explicit null under the `ref` key, got {git_json}"
        );
    }

    /// Older `metadata.json` files written before `Option` fields
    /// were emitted as explicit `null` simply omit the keys. The
    /// [`KernelSource`] doc states absent `Option` keys must
    /// deserialize as `None` — cache-integrity enforcement rides on
    /// the required non-`Option` fields of [`KernelMetadata`], not
    /// on the optional payloads inside variants. Feed each variant
    /// a minimal JSON with every `Option` key omitted, deserialize,
    /// and assert the result carries `None` in every payload slot.
    #[test]
    fn kernel_source_absent_option_keys_deserialize_as_none() {
        // Git with both git_hash and ref omitted.
        let git_bare: KernelSource = serde_json::from_str(r#"{"type":"git"}"#)
            .expect("Git with absent Option keys must deserialize");
        assert!(matches!(
            git_bare,
            KernelSource::Git {
                git_hash: None,
                git_ref: None,
            }
        ));

        // Git with only git_hash present.
        let git_hash_only: KernelSource =
            serde_json::from_str(r#"{"type":"git","git_hash":"abc"}"#)
                .expect("Git with only git_hash must deserialize");
        assert!(matches!(
            git_hash_only,
            KernelSource::Git {
                git_hash: Some(ref h),
                git_ref: None,
            } if h == "abc"
        ));

        // Git with only ref present.
        let git_ref_only: KernelSource = serde_json::from_str(r#"{"type":"git","ref":"main"}"#)
            .expect("Git with only ref must deserialize");
        assert!(matches!(
            git_ref_only,
            KernelSource::Git {
                git_hash: None,
                git_ref: Some(ref r),
            } if r == "main"
        ));

        // Local with both source_tree_path and git_hash omitted.
        let local_bare: KernelSource = serde_json::from_str(r#"{"type":"local"}"#)
            .expect("Local with absent Option keys must deserialize");
        assert!(matches!(
            local_bare,
            KernelSource::Local {
                source_tree_path: None,
                git_hash: None,
            }
        ));

        // Local with only source_tree_path present.
        let local_path_only: KernelSource =
            serde_json::from_str(r#"{"type":"local","source_tree_path":"/tmp/linux"}"#)
                .expect("Local with only source_tree_path must deserialize");
        assert!(matches!(
            local_path_only,
            KernelSource::Local {
                source_tree_path: Some(ref p),
                git_hash: None,
            } if p.to_str() == Some("/tmp/linux")
        ));

        // Local with only git_hash present.
        let local_hash_only: KernelSource =
            serde_json::from_str(r#"{"type":"local","git_hash":"deadbeef"}"#)
                .expect("Local with only git_hash must deserialize");
        assert!(matches!(
            local_hash_only,
            KernelSource::Local {
                source_tree_path: None,
                git_hash: Some(ref h),
            } if h == "deadbeef"
        ));
    }

    #[test]
    fn kernel_source_serde_tagged_representation() {
        // Check the tagged JSON shape on each variant.
        let t = serde_json::to_string(&KernelSource::Tarball).unwrap();
        assert_eq!(t, r#"{"type":"tarball"}"#);
        let g = serde_json::to_string(&KernelSource::Git {
            git_hash: Some("abc".to_string()),
            git_ref: Some("main".to_string()),
        })
        .unwrap();
        assert!(g.contains(r#""type":"git""#));
        assert!(g.contains(r#""git_hash":"abc""#));
        assert!(g.contains(r#""ref":"main""#));
        let l = serde_json::to_string(&KernelSource::Local {
            source_tree_path: Some(PathBuf::from("/tmp/linux")),
            git_hash: Some("a1b2c3d".to_string()),
        })
        .unwrap();
        assert!(l.contains(r#""type":"local""#));
        assert!(l.contains(r#""source_tree_path":"/tmp/linux""#));
        assert!(l.contains(r#""git_hash":"a1b2c3d""#));
    }

    // -- CacheDir --

    #[test]
    fn cache_dir_with_root_does_not_create_dir() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path().join("kernels");
        assert!(!root.exists());
        let cache = CacheDir::with_root(root.clone());
        // Resolution must not create the directory — store() does it
        // lazily on first write.
        assert!(!root.exists());
        assert_eq!(cache.root(), root);
    }

    #[test]
    fn cache_dir_list_returns_empty_for_nonexistent_root() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path().join("never-created");
        assert!(!root.exists());
        let cache = CacheDir::with_root(root);
        let entries = cache.list().unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn cache_dir_store_creates_root_lazily() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path().join("lazy-root");
        assert!(!root.exists());
        let cache = CacheDir::with_root(root.clone());
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");
        cache
            .store("key", &CacheArtifacts::new(&image), &meta)
            .unwrap();
        assert!(root.exists(), "store() must create the cache root");
    }

    #[test]
    fn cache_dir_default_root_returns_path() {
        // `lock_env()` serializes against every other env-touching
        // test in the crate (test_support/model.rs, test_helpers
        // siblings, the cache_resolve_root_* tests below). nextest
        // runs unit tests concurrently within a binary and
        // `std::env::set_var` is process-wide, so a sibling test
        // that mutates HOME / XDG_CACHE_HOME / KTSTR_CACHE_DIR
        // without the lock can race the save / mutate / restore
        // window of an `EnvVarGuard` here. Tester finding T1.
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", tmp.path());
        let resolved = CacheDir::default_root().unwrap();
        assert_eq!(resolved, tmp.path());
        // Side-effect-free: calling default_root() must not create
        // any directories beyond what the env var already pointed at.
    }

    #[test]
    fn cache_dir_list_empty() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let entries = cache.list().unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn cache_dir_store_and_lookup() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));

        // Create a fake kernel image.
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        // Store.
        let entry = cache
            .store("6.14.2-tarball-x86_64", &CacheArtifacts::new(&image), &meta)
            .unwrap();
        assert_eq!(entry.key, "6.14.2-tarball-x86_64");
        assert!(entry.path.join("bzImage").exists());
        assert!(entry.path.join("metadata.json").exists());

        // Lookup.
        let found = cache.lookup("6.14.2-tarball-x86_64");
        assert!(found.is_some());
        let found = found.unwrap();
        assert_eq!(found.key, "6.14.2-tarball-x86_64");
        assert_eq!(found.metadata.version.as_deref(), Some("6.14.2"));
    }

    #[test]
    fn cache_dir_lookup_missing() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        assert!(cache.lookup("nonexistent").is_none());
    }

    #[test]
    fn cache_dir_lookup_corrupt_metadata() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());

        // Create entry dir with image but corrupt metadata.
        let entry_dir = tmp.path().join("bad-entry");
        fs::create_dir_all(&entry_dir).unwrap();
        fs::write(entry_dir.join("bzImage"), b"fake").unwrap();
        fs::write(entry_dir.join("metadata.json"), b"not json").unwrap();

        // lookup returns None because metadata is corrupt and we
        // cannot determine the image_name field.
        let found = cache.lookup("bad-entry");
        assert!(found.is_none());
    }

    #[test]
    fn cache_dir_lookup_missing_image() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());

        // Create entry dir with valid metadata but no image file.
        let entry_dir = tmp.path().join("no-image");
        fs::create_dir_all(&entry_dir).unwrap();
        let meta = test_metadata("6.14.2");
        let json = serde_json::to_string(&meta).unwrap();
        fs::write(entry_dir.join("metadata.json"), json).unwrap();

        let found = cache.lookup("no-image");
        assert!(found.is_none());
    }

    #[test]
    fn cache_dir_store_overwrites_existing() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());

        let meta1 = KernelMetadata {
            built_at: "2026-04-12T10:00:00Z".to_string(),
            ..test_metadata("6.14.2")
        };
        cache
            .store(
                "6.14.2-tarball-x86_64",
                &CacheArtifacts::new(&image),
                &meta1,
            )
            .unwrap();

        let meta2 = KernelMetadata {
            built_at: "2026-04-12T11:00:00Z".to_string(),
            ..test_metadata("6.14.2")
        };
        cache
            .store(
                "6.14.2-tarball-x86_64",
                &CacheArtifacts::new(&image),
                &meta2,
            )
            .unwrap();

        let found = cache.lookup("6.14.2-tarball-x86_64").unwrap();
        assert_eq!(found.metadata.built_at, "2026-04-12T11:00:00Z");
    }

    #[test]
    fn cache_dir_list_sorted_newest_first() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());

        let meta_old = KernelMetadata {
            built_at: "2026-04-10T10:00:00Z".to_string(),
            ..test_metadata("6.13.0")
        };
        let meta_new = KernelMetadata {
            built_at: "2026-04-12T10:00:00Z".to_string(),
            ..test_metadata("6.14.2")
        };
        let meta_mid = KernelMetadata {
            built_at: "2026-04-11T10:00:00Z".to_string(),
            ..test_metadata("6.14.0")
        };

        // Store in non-chronological order.
        cache
            .store("old", &CacheArtifacts::new(&image), &meta_old)
            .unwrap();
        cache
            .store("new", &CacheArtifacts::new(&image), &meta_new)
            .unwrap();
        cache
            .store("mid", &CacheArtifacts::new(&image), &meta_mid)
            .unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 3);
        assert_eq!(entries[0].key(), "new");
        assert_eq!(entries[1].key(), "mid");
        assert_eq!(entries[2].key(), "old");
    }

    #[test]
    fn cache_dir_list_includes_corrupt_entries() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());

        // Create a valid entry.
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");
        cache
            .store("valid", &CacheArtifacts::new(&image), &meta)
            .unwrap();

        // Create a corrupt entry (no metadata).
        let bad_dir = tmp.path().join("corrupt");
        fs::create_dir_all(&bad_dir).unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 2);
        // Valid entry surfaces as ListedEntry::Valid.
        let valid = entries.iter().find(|e| e.key() == "valid").unwrap();
        assert!(valid.as_valid().is_some());
        // Corrupt entry surfaces as ListedEntry::Corrupt.
        let corrupt = entries.iter().find(|e| e.key() == "corrupt").unwrap();
        assert!(corrupt.as_valid().is_none());
        let ListedEntry::Corrupt { reason, .. } = corrupt else {
            panic!("expected Corrupt variant");
        };
        assert_eq!(
            reason, "metadata.json missing",
            "missing-metadata reason should be the exact missing-file label, got: {reason}",
        );
    }

    #[test]
    fn cache_dir_list_classifies_missing_image_as_corrupt() {
        // Metadata parses cleanly but the image file it references
        // has been deleted (partial download / manual cleanup).
        // list() must surface the entry as ListedEntry::Corrupt with
        // an image-missing reason, so callers don't dispatch to
        // image_path() and get a stale path.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");
        let entry = cache
            .store("missing-image", &CacheArtifacts::new(&image), &meta)
            .unwrap();

        // Delete only the image file; leave metadata.json in place.
        fs::remove_file(entry.image_path()).unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 1);
        let listed = &entries[0];
        assert_eq!(listed.key(), "missing-image");
        assert!(
            listed.as_valid().is_none(),
            "entry with missing image must not surface as Valid",
        );
        let ListedEntry::Corrupt { reason, .. } = listed else {
            panic!("expected Corrupt variant for missing-image entry");
        };
        assert!(
            reason.contains("image file") && reason.contains("missing"),
            "reason should cite missing image file, got: {reason}",
        );
        assert!(
            reason.contains(&meta.image_name),
            "reason should name the specific image file, got: {reason}",
        );
    }

    #[test]
    fn cache_dir_list_classifies_unreadable_metadata_as_corrupt() {
        // The `missing` and parse-family branches (schema drift,
        // malformed, truncated) of `read_metadata` are covered
        // elsewhere; the I/O-error branch — any `fs::read_to_string`
        // failure that is NOT `ErrorKind::NotFound` — is exercised
        // here. Forcing a non-ENOENT error without relying on
        // filesystem permissions (which vary across rootless
        // containers and CI sandboxes) is awkward, so we make
        // `metadata.json` a DIRECTORY: `read_to_string` then fails
        // with `EISDIR`, which `read_metadata` must map to the
        // `"metadata.json unreadable: "` prefix rather than the
        // missing or any parse-family label. This pins the
        // distinction so a future refactor that collapses the arms
        // back into a single generic "corrupt" reason breaks this
        // test before it ships a less-actionable diagnostic.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let entry_dir = tmp.path().join("unreadable-metadata");
        fs::create_dir_all(entry_dir.join("metadata.json")).unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 1);
        let listed = &entries[0];
        assert_eq!(listed.key(), "unreadable-metadata");
        assert!(listed.as_valid().is_none());
        let ListedEntry::Corrupt { reason, .. } = listed else {
            panic!("expected Corrupt variant for entry with unreadable metadata");
        };
        assert!(
            reason.starts_with("metadata.json unreadable: "),
            "unreadable-metadata reason should carry the unreadable prefix distinct from the \
             missing / schema-drift / malformed / truncated prefixes, got: {reason}",
        );
    }

    #[test]
    fn cache_dir_list_classifies_malformed_json_as_corrupt() {
        // metadata.json exists but is not valid JSON at all (unbalanced
        // punctuation / stray characters). `read_metadata` must route
        // this through `serde_json::Error::classify() ==
        // Category::Syntax` to produce the
        // `"metadata.json malformed: {e}"` prefix — distinct from the
        // schema-drift prefix that fires when JSON parses but does
        // not match the `KernelMetadata` shape.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let entry_dir = tmp.path().join("malformed-json");
        fs::create_dir_all(&entry_dir).unwrap();
        fs::write(entry_dir.join("metadata.json"), b"not valid json {[").unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 1);
        let listed = &entries[0];
        assert_eq!(listed.key(), "malformed-json");
        assert!(listed.as_valid().is_none());
        let ListedEntry::Corrupt { reason, .. } = listed else {
            panic!("expected Corrupt variant for malformed-json entry");
        };
        assert!(
            reason.starts_with("metadata.json malformed: "),
            "malformed-JSON reason should carry the malformed prefix \
             (Category::Syntax route), got: {reason}",
        );
    }

    #[test]
    fn cache_dir_list_classifies_incomplete_metadata_as_corrupt() {
        // metadata.json is valid JSON but omits fields the current
        // `KernelMetadata` schema requires: `source`, `arch`,
        // `image_name`, `built_at`, `has_vmlinux`, and
        // `vmlinux_stripped`. These are non-`Option`,
        // non-`#[serde(default)]` fields, so `serde_json::from_str`
        // fails with `Category::Data` when they are absent. Note
        // `has_vmlinux: bool` and `vmlinux_stripped: bool` are
        // required even though they are not wrapped in `Option` — a
        // plain `bool` with no `#[serde(default)]` attribute must
        // still be present in the JSON payload. serde_json reports
        // the first missing required field in declaration order
        // (`source`), and `read_metadata` wraps it under the
        // schema-drift prefix so the user sees both the
        // classification ("schema drift") and
        // the specific missing field.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let entry_dir = tmp.path().join("incomplete-metadata");
        fs::create_dir_all(&entry_dir).unwrap();
        fs::write(entry_dir.join("metadata.json"), br#"{"version": "6.14"}"#).unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 1);
        let listed = &entries[0];
        assert_eq!(listed.key(), "incomplete-metadata");
        assert!(
            listed.as_valid().is_none(),
            "incomplete-metadata missing required fields must not deserialize as Valid",
        );
        let ListedEntry::Corrupt { reason, .. } = listed else {
            panic!("expected Corrupt variant for entry with incomplete metadata");
        };
        assert!(
            reason.starts_with("metadata.json schema drift: "),
            "incomplete-metadata reason should carry the schema-drift \
             prefix (Category::Data route), got: {reason}",
        );
        assert!(
            reason.contains("missing field `source`"),
            "incomplete-metadata reason should name the first missing required field, got: {reason}",
        );
    }

    #[test]
    fn cache_dir_list_classifies_truncated_json_as_corrupt() {
        // metadata.json ends mid-value: `{"source":` stops after the
        // colon with no value byte. serde_json surfaces this as
        // `Category::Eof`, which `read_metadata` wraps under the
        // `"metadata.json truncated: {e}"` prefix. Covers the Eof
        // branch of the classify() match — distinct from the schema-
        // drift (Data) and malformed (Syntax) branches exercised by
        // the sibling tests above.
        //
        // Typical real-world cause: a crashed `store()` whose atomic
        // rename never completed, leaving a partially-written
        // metadata.json in a surviving entry directory.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let entry_dir = tmp.path().join("truncated-json");
        fs::create_dir_all(&entry_dir).unwrap();
        fs::write(entry_dir.join("metadata.json"), br#"{"source":"#).unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 1);
        let listed = &entries[0];
        assert_eq!(listed.key(), "truncated-json");
        assert!(listed.as_valid().is_none());
        let ListedEntry::Corrupt { reason, .. } = listed else {
            panic!("expected Corrupt variant for truncated-json entry");
        };
        assert!(
            reason.starts_with("metadata.json truncated: "),
            "truncated-JSON reason should carry the truncated prefix \
             (Category::Eof route), got: {reason}",
        );
    }

    /// Table-drive every prefix → `error_kind` classifier mapping.
    /// Pins each documented value independently so a regression in
    /// one arm surfaces with the specific prefix cited in the
    /// failure message, not as a blanket "classifier broken". The
    /// "unknown" fallback row is the safety net: a future producer
    /// prefix that falls through this table must surface as
    /// `"unknown"` to consumers rather than panic.
    #[test]
    fn classify_corrupt_reason_covers_every_documented_prefix() {
        let cases: &[(&str, &str)] = &[
            ("metadata.json missing", "missing"),
            (
                "metadata.json unreadable: Is a directory (os error 21)",
                "unreadable",
            ),
            (
                "metadata.json schema drift: missing field `source` at line 1 column 21",
                "schema_drift",
            ),
            (
                "metadata.json malformed: expected value at line 1 column 1",
                "malformed",
            ),
            (
                "metadata.json truncated: EOF while parsing a value at line 1 column 10",
                "truncated",
            ),
            (
                "metadata.json parse error: something unexpected",
                "parse_error",
            ),
            (
                "image file bzImage missing from entry directory",
                "image_missing",
            ),
            ("some future prefix nobody wrote yet", "unknown"),
        ];
        for (reason, expected) in cases {
            assert_eq!(
                classify_corrupt_reason(reason),
                *expected,
                "reason `{reason}` should classify as `{expected}`",
            );
        }
    }

    /// `ListedEntry::error_kind()` returns `None` on a Valid entry
    /// and the classifier result on a Corrupt entry. Pins the
    /// Valid → None contract so a consumer that dispatches on
    /// `error_kind().is_some()` can safely gate on the corrupt
    /// path.
    #[test]
    fn listed_entry_error_kind_dispatches_on_variant() {
        // Construct a Valid entry via the normal store path.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");
        cache
            .store("valid-ek", &CacheArtifacts::new(&image), &meta)
            .unwrap();

        // And a Corrupt entry via a missing-metadata directory.
        let bad_dir = tmp.path().join("cache").join("corrupt-ek");
        fs::create_dir_all(&bad_dir).unwrap();

        let entries = cache.list().unwrap();
        assert_eq!(entries.len(), 2);
        let valid = entries
            .iter()
            .find(|e| e.key() == "valid-ek")
            .expect("valid entry must be listed");
        let corrupt = entries
            .iter()
            .find(|e| e.key() == "corrupt-ek")
            .expect("corrupt entry must be listed");
        assert_eq!(
            valid.error_kind(),
            None,
            "Valid entries must report no error_kind",
        );
        assert_eq!(
            corrupt.error_kind(),
            Some("missing"),
            "missing-metadata Corrupt entry must classify as `missing`",
        );
    }

    #[test]
    fn cache_dir_list_skips_tmp_dirs() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());

        // Create a .tmp- directory (in-progress store).
        let tmp_dir = tmp.path().join(".tmp-in-progress-12345");
        fs::create_dir_all(&tmp_dir).unwrap();

        let entries = cache.list().unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn cache_dir_list_skips_regular_files() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());

        // Create a regular file in the cache root.
        fs::write(tmp.path().join("stray-file.txt"), b"stray").unwrap();

        let entries = cache.list().unwrap();
        assert!(entries.is_empty());
    }

    #[test]
    fn cache_dir_clean_all() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());

        cache
            .store("a", &CacheArtifacts::new(&image), &test_metadata("6.14.0"))
            .unwrap();
        cache
            .store("b", &CacheArtifacts::new(&image), &test_metadata("6.14.1"))
            .unwrap();
        cache
            .store("c", &CacheArtifacts::new(&image), &test_metadata("6.14.2"))
            .unwrap();

        let removed = cache.clean_all().unwrap();
        assert_eq!(removed, 3);
        assert!(cache.list().unwrap().is_empty());
    }

    #[test]
    fn cache_dir_clean_keep_n() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());

        let meta_old = KernelMetadata {
            built_at: "2026-04-10T10:00:00Z".to_string(),
            ..test_metadata("6.13.0")
        };
        let meta_new = KernelMetadata {
            built_at: "2026-04-12T10:00:00Z".to_string(),
            ..test_metadata("6.14.2")
        };
        let meta_mid = KernelMetadata {
            built_at: "2026-04-11T10:00:00Z".to_string(),
            ..test_metadata("6.14.0")
        };

        cache
            .store("old", &CacheArtifacts::new(&image), &meta_old)
            .unwrap();
        cache
            .store("new", &CacheArtifacts::new(&image), &meta_new)
            .unwrap();
        cache
            .store("mid", &CacheArtifacts::new(&image), &meta_mid)
            .unwrap();

        let removed = cache.clean_keep(1).unwrap();
        assert_eq!(removed, 2);

        let remaining = cache.list().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].key(), "new");
    }

    #[test]
    fn cache_dir_clean_keep_more_than_exist() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());

        cache
            .store(
                "only",
                &CacheArtifacts::new(&image),
                &test_metadata("6.14.2"),
            )
            .unwrap();

        let removed = cache.clean_keep(5).unwrap();
        assert_eq!(removed, 0);
        assert_eq!(cache.list().unwrap().len(), 1);
    }

    #[test]
    fn cache_dir_clean_empty_cache() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let removed = cache.clean_all().unwrap();
        assert_eq!(removed, 0);
    }

    // -- resolve_cache_root --

    #[test]
    fn cache_resolve_root_ktstr_cache_dir() {
        // `lock_env()` serializes against sibling env-touching tests
        // in test_support/model.rs and the cache_resolve_root_* group
        // below. See `cache_dir_default_root_returns_path` for the
        // long-form rationale (Tester finding T1).
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path().join("custom-cache");
        // Temporarily set env var for this test.
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", &dir);
        let root = resolve_cache_root().unwrap();
        assert_eq!(root, dir);
    }

    #[test]
    fn cache_resolve_root_xdg_cache_home() {
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::set("XDG_CACHE_HOME", tmp.path());
        let root = resolve_cache_root().unwrap();
        assert_eq!(root, tmp.path().join("ktstr").join("kernels"));
    }

    #[test]
    fn cache_resolve_root_empty_ktstr_cache_dir_falls_through() {
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _guard1 = EnvVarGuard::set("KTSTR_CACHE_DIR", "");
        let _guard2 = EnvVarGuard::set("XDG_CACHE_HOME", tmp.path());
        let root = resolve_cache_root().unwrap();
        assert_eq!(root, tmp.path().join("ktstr").join("kernels"));
    }

    #[test]
    fn cache_resolve_root_empty_xdg_falls_to_home() {
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::set("XDG_CACHE_HOME", "");
        let _guard3 = EnvVarGuard::set("HOME", tmp.path());
        let root = resolve_cache_root().unwrap();
        assert_eq!(
            root,
            tmp.path().join(".cache").join("ktstr").join("kernels")
        );
    }

    // -- resolve_cache_root error paths --

    #[test]
    fn cache_resolve_root_home_unset_error() {
        let _lock = lock_env();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::remove("XDG_CACHE_HOME");
        let _guard3 = EnvVarGuard::remove("HOME");
        let err = resolve_cache_root().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("HOME is unset"),
            "expected HOME-unset error, got: {msg}"
        );
        assert!(
            !msg.contains("HOME is set to the empty string"),
            "unset HOME must NOT use the empty-string diagnostic — the two \
             cases are distinct now (NotPresent vs Ok(\"\")), got: {msg}",
        );
        assert!(
            msg.contains("KTSTR_CACHE_DIR"),
            "error should suggest KTSTR_CACHE_DIR, got: {msg}"
        );
    }

    /// A HOME literal of `"/"` (legacy root convention, container
    /// init that forgot to override HOME) must NOT silently produce
    /// `/.cache/ktstr/kernels` — that path's statvfs reports the
    /// root filesystem's free space, which is typically a small
    /// constrained mount and never the user's intended cache
    /// location. Bail with a diagnostic that names the resulting
    /// junk path and points the operator at a remediation.
    #[test]
    fn cache_resolve_root_home_root_slash_error() {
        let _lock = lock_env();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::remove("XDG_CACHE_HOME");
        let _guard3 = EnvVarGuard::set("HOME", "/");
        let err = resolve_cache_root().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("HOME is `/`"),
            "expected HOME=/ specific error, got: {msg}"
        );
        assert!(
            msg.contains("/.cache/ktstr"),
            "diagnostic must cite the offending cache path, got: {msg}"
        );
        assert!(
            msg.contains("KTSTR_CACHE_DIR"),
            "error should suggest KTSTR_CACHE_DIR, got: {msg}"
        );
    }

    /// A HOME literal of `""` (empty string) is just as broken as
    /// unset (`PathBuf::from("").join(".cache")` produces a
    /// relative `.cache` rooted at the process CWD instead of the
    /// user's home), but the diagnostic now distinguishes the two
    /// shapes: empty-string assignment hits the `Ok("")` arm of
    /// `validate_home_for_cache`, surfacing "HOME is set to the
    /// empty string" so an operator can identify a Dockerfile
    /// `ENV HOME=` or shell-rc `export HOME=` typo as the cause
    /// rather than a missing init-time assignment.
    #[test]
    fn cache_resolve_root_home_empty_error() {
        let _lock = lock_env();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::remove("XDG_CACHE_HOME");
        let _guard3 = EnvVarGuard::set("HOME", "");
        let err = resolve_cache_root().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("HOME is set to the empty string"),
            "empty-HOME bail must use the empty-string diagnostic, got: {msg}",
        );
        assert!(
            !msg.contains("HOME is unset"),
            "empty-HOME must NOT use the unset diagnostic — the two \
             cases are distinct now, got: {msg}",
        );
    }

    /// A relative-path HOME (e.g. `HOME=relative/dir`) silently
    /// resolves the cache against CWD, which silently relocates
    /// the cache as the operator changes directories — a usability
    /// nightmare worse than a deferred error. Pin the explicit
    /// rejection so a regression that drops the absolute-path
    /// check surfaces here instead of as a hard-to-diagnose
    /// "cache contents disappeared" report from the operator.
    #[test]
    fn cache_resolve_root_home_relative_path_error() {
        let _lock = lock_env();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::remove("XDG_CACHE_HOME");
        let _guard3 = EnvVarGuard::set("HOME", "relative/dir");
        let err = resolve_cache_root().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("not an absolute path"),
            "expected relative-path-specific error, got: {msg}"
        );
        assert!(
            msg.contains("relative/dir"),
            "diagnostic must cite the offending HOME value, got: {msg}"
        );
        assert!(
            msg.contains("KTSTR_CACHE_DIR"),
            "error should suggest KTSTR_CACHE_DIR, got: {msg}"
        );
    }

    /// A bare-name HOME (no path separators at all, e.g. `HOME=tmp`)
    /// is also relative — `PathBuf::from("tmp").join(".cache")`
    /// yields `tmp/.cache` against CWD. Pin separately from the
    /// `relative/dir` case to confirm the absolute-path check
    /// isn't accidentally permissive on shapes that lack a `/`.
    #[test]
    fn cache_resolve_root_home_bare_name_relative_error() {
        let _lock = lock_env();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::remove("XDG_CACHE_HOME");
        let _guard3 = EnvVarGuard::set("HOME", "tmp");
        let err = resolve_cache_root().unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("not an absolute path"),
            "expected relative-path-specific error, got: {msg}"
        );
        assert!(
            msg.contains("\"tmp\""),
            "diagnostic must cite the offending HOME value via its Debug \
             representation, got: {msg}"
        );
    }

    /// Sanity check the happy path: an absolute HOME pointing at a
    /// real directory must resolve through the gate to the expected
    /// `$HOME/.cache/ktstr/kernels` path. Pins that the new
    /// validation does not over-reject — a regression that hardens
    /// the gate further (e.g. requires HOME to exist via metadata)
    /// would break this.
    #[test]
    fn cache_resolve_root_home_absolute_passes() {
        let _lock = lock_env();
        let _guard1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _guard2 = EnvVarGuard::remove("XDG_CACHE_HOME");
        let tmp = TempDir::new().expect("tempdir");
        let _guard3 = EnvVarGuard::set("HOME", tmp.path());
        let resolved = resolve_cache_root().expect("absolute HOME must resolve");
        let expected = tmp.path().join(".cache").join("ktstr").join("kernels");
        assert_eq!(
            resolved, expected,
            "absolute HOME must produce $HOME/.cache/ktstr/kernels",
        );
    }

    /// A non-UTF-8 `KTSTR_CACHE_DIR` must fail fast with an
    /// actionable diagnostic rather than silently falling through to
    /// `$XDG_CACHE_HOME` / `$HOME`. Before the `NotUnicode` branch
    /// existed, `std::env::var` returned `Err` and the old `if let
    /// Ok(..)` guard dropped the override without a trace — an
    /// operator who set the variable would see ktstr write to a
    /// surprising directory under `$HOME` and have no clue why the
    /// override was ignored.
    ///
    /// `EnvVarGuard::set` accepts arbitrary `OsStr`, so the test can
    /// plant a lone 0xFF byte (valid on Unix filesystems, invalid as
    /// UTF-8) and observe the bail.
    #[test]
    #[cfg(unix)]
    fn cache_resolve_root_non_utf8_ktstr_cache_dir_bails() {
        // `lock_env()` for the same reason every other env-touching
        // cache.rs test holds it (Tester finding T1).
        let _lock = lock_env();
        use std::ffi::OsStr;
        use std::os::unix::ffi::OsStrExt;
        let bytes: &[u8] = b"/tmp/ktstr-\xFFcache";
        let value = OsStr::from_bytes(bytes);
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", value);
        let err = resolve_cache_root()
            .expect_err("non-UTF-8 KTSTR_CACHE_DIR must bail, not silently fall through");
        let msg = err.to_string();
        assert!(
            msg.contains("KTSTR_CACHE_DIR"),
            "error must name the offending variable, got: {msg}",
        );
        assert!(
            msg.contains("non-UTF-8"),
            "error must mention non-UTF-8 so the operator knows the encoding, \
             got: {msg}",
        );
        assert!(
            msg.contains("UTF-8") || msg.contains("unset") || msg.contains("ASCII"),
            "error must name a remediation (UTF-8 replacement or unset), \
             got: {msg}",
        );
    }

    // -- path_inside_cache_root direct unit tests --
    //
    // `path_inside_cache_root` is the gate that prevents
    // `<vmlinux>.btf` sidecar writes from polluting kernel source
    // trees and other directories the cache does not own (see
    // `monitor::btf_offsets::load_btf_from_path`). The tests below
    // exercise the helper directly so a regression in the
    // membership logic surfaces against this dedicated entry point
    // as well as the integration paths in btf_offsets.rs.
    //
    // Every test holds `lock_env()` because the helper reads
    // KTSTR_CACHE_DIR / XDG_CACHE_HOME / HOME from the process
    // environment.

    /// Sibling path that lives directly under the cache root resolves
    /// as in-cache. The canonical case the gate must accept.
    #[test]
    fn path_inside_cache_root_accepts_path_inside() {
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", tmp.path());
        let entry = tmp.path().join("kentry");
        std::fs::create_dir_all(&entry).unwrap();
        let vmlinux = entry.join("vmlinux");
        std::fs::write(&vmlinux, b"placeholder").unwrap();
        assert!(
            path_inside_cache_root(&vmlinux),
            "vmlinux directly under cache root must be classified as in-cache",
        );
    }

    /// Path that lives in a sibling tempdir (modeling a kernel source
    /// tree) must NOT classify as in-cache — the source-tree pollution
    /// scenario this helper exists to prevent.
    #[test]
    fn path_inside_cache_root_rejects_path_outside() {
        let _lock = lock_env();
        let cache_root = TempDir::new().unwrap();
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", cache_root.path());
        let source_tree = TempDir::new().unwrap();
        let vmlinux = source_tree.path().join("vmlinux");
        std::fs::write(&vmlinux, b"placeholder").unwrap();
        assert!(
            !path_inside_cache_root(&vmlinux),
            "vmlinux in a sibling tempdir must NOT be classified as in-cache",
        );
    }

    /// Bare filename has no parent component (`Path::parent` returns
    /// `Some("")`). Treating an empty parent as "in cache" would let
    /// any process invoking `path_inside_cache_root("vmlinux")` pass
    /// when its CWD happens to be the cache root — surprising
    /// semantics for what is otherwise a structural check. The
    /// helper short-circuits to false.
    #[test]
    fn path_inside_cache_root_rejects_bare_filename() {
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", tmp.path());
        let bare = std::path::Path::new("vmlinux");
        assert!(
            !path_inside_cache_root(bare),
            "bare filename (no parent) must short-circuit to false",
        );
    }

    /// With every cascade variable removed (`KTSTR_CACHE_DIR`,
    /// `XDG_CACHE_HOME`, `HOME`), `resolve_cache_root` errors out and
    /// `path_inside_cache_root` falls through to `false`. A real
    /// vmlinux on disk under those conditions is "outside the cache"
    /// because no cache root exists.
    #[test]
    fn path_inside_cache_root_false_when_unresolvable() {
        let _lock = lock_env();
        let _g1 = EnvVarGuard::remove("KTSTR_CACHE_DIR");
        let _g2 = EnvVarGuard::remove("XDG_CACHE_HOME");
        let _g3 = EnvVarGuard::remove("HOME");
        // Use a real file so `Path::parent` is non-empty; the
        // unresolvable cache root is the part being tested.
        let dir = TempDir::new().unwrap();
        let f = dir.path().join("vmlinux");
        std::fs::write(&f, b"x").unwrap();
        assert!(
            !path_inside_cache_root(&f),
            "unresolvable cache root must classify as outside-cache (false)",
        );
    }

    /// Parent that does not exist on disk: `fs::canonicalize(parent)`
    /// fails with ENOENT, and the helper safely returns false.
    /// Models a caller passing a path whose ancestor was rmdir'd
    /// between path construction and the membership check.
    #[test]
    fn path_inside_cache_root_false_when_parent_canonicalize_fails() {
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", tmp.path());
        // Construct a path whose parent does not exist on disk.
        let nonexistent = std::path::Path::new("/this/parent/should/not/exist/vmlinux");
        assert!(
            !nonexistent.parent().unwrap().exists(),
            "precondition: parent must not exist for the canonicalize \
             failure path to be exercised",
        );
        assert!(
            !path_inside_cache_root(nonexistent),
            "nonexistent parent must surface as outside-cache, not panic",
        );
    }

    /// Symlink whose parent's canonical form lands INSIDE the cache
    /// root: classified as in-cache, even though the symlink itself
    /// lives outside. This is the desired semantic for callers that
    /// follow `find_vmlinux`-style fallbacks where the resolved
    /// target is what matters.
    #[test]
    #[cfg(unix)]
    fn path_inside_cache_root_follows_symlink_into_cache() {
        let _lock = lock_env();
        let cache_root = TempDir::new().unwrap();
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", cache_root.path());
        // Real vmlinux inside the cache.
        let entry = cache_root.path().join("kentry");
        std::fs::create_dir_all(&entry).unwrap();
        let real = entry.join("vmlinux");
        std::fs::write(&real, b"placeholder").unwrap();
        // Symlink whose PARENT (an outside-cache tempdir) holds a
        // link to the in-cache real file. `path.parent()` is the
        // outside-cache dir, but `fs::canonicalize(parent)`
        // resolves... the parent itself, not the symlink target.
        // To exercise the "symlink resolving INTO cache" semantics
        // we need a parent that IS itself a symlink into the
        // cache. Use a directory symlink for that.
        let outside = TempDir::new().unwrap();
        let alias_parent = outside.path().join("alias");
        std::os::unix::fs::symlink(&entry, &alias_parent).unwrap();
        let through_alias = alias_parent.join("vmlinux");
        assert!(
            through_alias.exists(),
            "precondition: path through symlinked parent must be reachable",
        );
        assert!(
            path_inside_cache_root(&through_alias),
            "path whose parent symlink resolves into cache must classify as in-cache",
        );
    }

    /// Symlink whose parent's canonical form lands OUTSIDE the cache
    /// root: classified as outside-cache. The opposite of
    /// `path_inside_cache_root_follows_symlink_into_cache` — together
    /// the two pin the canonicalize-then-compare contract.
    #[test]
    #[cfg(unix)]
    fn path_inside_cache_root_follows_symlink_out_of_cache() {
        let _lock = lock_env();
        let cache_root = TempDir::new().unwrap();
        let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", cache_root.path());
        // Real vmlinux outside the cache.
        let outside = TempDir::new().unwrap();
        let real = outside.path().join("vmlinux");
        std::fs::write(&real, b"placeholder").unwrap();
        // Symlink directory IN cache pointing at the outside parent.
        let alias_parent = cache_root.path().join("alias");
        std::os::unix::fs::symlink(outside.path(), &alias_parent).unwrap();
        let through_alias = alias_parent.join("vmlinux");
        assert!(
            through_alias.exists(),
            "precondition: path through symlinked parent must be reachable",
        );
        assert!(
            !path_inside_cache_root(&through_alias),
            "path whose parent symlink resolves OUT of cache must classify as outside-cache",
        );
    }

    /// Empty `KTSTR_CACHE_DIR` falls through the cascade per
    /// `resolve_cache_root_with_suffix`'s `Ok(_) => fall through`
    /// arm; the helper then resolves through XDG/HOME like any
    /// other call. With XDG and HOME pointed at a tempdir, a path
    /// inside that tempdir's `ktstr/kernels/` subtree must classify
    /// as in-cache; a sibling outside it must not.
    #[test]
    fn path_inside_cache_root_empty_ktstr_cache_dir_falls_through() {
        let _lock = lock_env();
        let tmp = TempDir::new().unwrap();
        let _g1 = EnvVarGuard::set("KTSTR_CACHE_DIR", "");
        let _g2 = EnvVarGuard::set("XDG_CACHE_HOME", tmp.path());
        // resolve_cache_root with empty KTSTR_CACHE_DIR + XDG set →
        // root = <tmp>/ktstr/kernels. Stage a vmlinux inside that
        // resolved root.
        let resolved = tmp.path().join("ktstr").join("kernels");
        let entry = resolved.join("kentry");
        std::fs::create_dir_all(&entry).unwrap();
        let vmlinux = entry.join("vmlinux");
        std::fs::write(&vmlinux, b"placeholder").unwrap();
        assert!(
            path_inside_cache_root(&vmlinux),
            "with empty KTSTR_CACHE_DIR, the cascade must resolve via \
             XDG_CACHE_HOME and accept paths inside that resolved root",
        );
    }

    /// Resolution is performed fresh on every call: changing
    /// `KTSTR_CACHE_DIR` between two invocations must yield
    /// different membership decisions for the same input path.
    /// Memoization would surface here as a stale `true` after the
    /// pointer moves away from the path's parent.
    #[test]
    fn path_inside_cache_root_fresh_resolution_per_call() {
        let _lock = lock_env();
        let cache_a = TempDir::new().unwrap();
        let cache_b = TempDir::new().unwrap();
        // Stage a vmlinux inside cache_a.
        let entry_a = cache_a.path().join("kentry");
        std::fs::create_dir_all(&entry_a).unwrap();
        let vmlinux_a = entry_a.join("vmlinux");
        std::fs::write(&vmlinux_a, b"placeholder").unwrap();
        // First call: KTSTR_CACHE_DIR points at cache_a → in-cache.
        {
            let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", cache_a.path());
            assert!(
                path_inside_cache_root(&vmlinux_a),
                "first call: vmlinux is inside cache_a (the active root)",
            );
        }
        // Second call: KTSTR_CACHE_DIR moved to cache_b. Same input
        // path is now outside the active root.
        {
            let _guard = EnvVarGuard::set("KTSTR_CACHE_DIR", cache_b.path());
            assert!(
                !path_inside_cache_root(&vmlinux_a),
                "second call: KTSTR_CACHE_DIR has moved to cache_b, so the \
                 vmlinux (still under cache_a) must be classified outside",
            );
        }
    }

    // -- clean_orphaned_tmp_dirs unit tests --
    //
    // Parser/dispatcher coverage: the scan must remove directories
    // under `.tmp-{key}-{pid}` whose `{pid}` is verifiably dead,
    // must LEAVE malformed entries and non-`.tmp-` entries alone,
    // and must tolerate a nonexistent cache root.

    /// A `.tmp-{key}-{pid}` directory whose pid refers to a dead
    /// process is removed. Uses `libc::pid_t::MAX` — above
    /// `PID_MAX_LIMIT` (2^22), so no live process can ever claim it
    /// (same technique as `process_alive_nonexistent_pid` in
    /// scenario tests, removes the pid-reuse race from the test).
    #[test]
    fn clean_orphaned_tmp_dirs_removes_dead_pid_tempdir() {
        let tmp = TempDir::new().unwrap();
        let dead_pid = libc::pid_t::MAX;
        let orphan = tmp
            .path()
            .join(format!("{TMP_DIR_PREFIX}somekey-{dead_pid}"));
        std::fs::create_dir_all(&orphan).unwrap();
        // Plant a nested file so a regression that hand-rolled
        // `remove_dir` (non-recursive) instead of `remove_dir_all`
        // would fail with ENOTEMPTY and the dir would survive.
        std::fs::write(orphan.join("inner.txt"), b"data").unwrap();

        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(
            !orphan.exists(),
            "dead-pid tempdir must be removed by clean_orphaned_tmp_dirs",
        );
    }

    /// A `.tmp-{key}-{pid}` directory whose pid is LIVE (the test
    /// process itself) must be preserved. `kill(getpid(), None)`
    /// returns `Ok(())` inside `clean_orphaned_tmp_dirs`'s liveness
    /// probe, which routes to the `!dead` continue branch.
    #[test]
    fn clean_orphaned_tmp_dirs_preserves_live_pid_tempdir() {
        let tmp = TempDir::new().unwrap();
        let live_pid = unsafe { libc::getpid() };
        let keeper = tmp
            .path()
            .join(format!("{TMP_DIR_PREFIX}somekey-{live_pid}"));
        std::fs::create_dir_all(&keeper).unwrap();

        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(
            keeper.exists(),
            "live-pid tempdir must NOT be removed — its owner is still running",
        );
    }

    /// Entries whose suffix cannot be parsed as a pid (non-numeric
    /// or empty after the trailing `-`) must be left alone — they
    /// do not match our format and may belong to an unrelated
    /// tool. Covers the `rsplit_once` / `parse::<i32>` continue
    /// branches.
    #[test]
    fn clean_orphaned_tmp_dirs_leaves_malformed_suffix_alone() {
        let tmp = TempDir::new().unwrap();
        // Case A: non-numeric suffix.
        let nonnum = tmp.path().join(format!("{TMP_DIR_PREFIX}somekey-notapid"));
        std::fs::create_dir_all(&nonnum).unwrap();
        // Case B: empty suffix (name ends with `-`).
        let empty_suf = tmp.path().join(format!("{TMP_DIR_PREFIX}somekey-"));
        std::fs::create_dir_all(&empty_suf).unwrap();
        // Case C: no `-` at all after the prefix (rsplit_once
        // still finds the `-` inside the prefix itself, but
        // `.tmp` parses as non-numeric → continue).
        let no_dash = tmp.path().join(format!("{TMP_DIR_PREFIX}nokeyhere"));
        std::fs::create_dir_all(&no_dash).unwrap();

        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(nonnum.exists(), "non-numeric pid suffix must be left alone");
        assert!(empty_suf.exists(), "empty pid suffix must be left alone");
        assert!(no_dash.exists(), "no-pid-suffix entry must be left alone");
    }

    /// Directories that do not begin with [`TMP_DIR_PREFIX`] must
    /// never be touched. The cache root also holds real cache
    /// entries (hash-keyed directories), and an overbroad scan
    /// would wipe them out.
    #[test]
    fn clean_orphaned_tmp_dirs_leaves_unrelated_entries_alone() {
        let tmp = TempDir::new().unwrap();
        let real_entry = tmp.path().join("real-cache-entry");
        std::fs::create_dir_all(&real_entry).unwrap();
        let other = tmp.path().join("not-a-tempdir");
        std::fs::create_dir_all(&other).unwrap();

        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(
            real_entry.exists(),
            "unrelated cache entry must be preserved"
        );
        assert!(other.exists(), "unrelated directory must be preserved");
    }

    /// Non-UTF-8 filenames in the cache root must be skipped
    /// silently — they cannot be a `.tmp-{key}-{pid}` directory
    /// this module created (all our names are ASCII), and bailing
    /// on every stray non-UTF-8 entry would fail the whole cleanup
    /// pass.
    ///
    /// Unix-only because the byte-level name construction uses
    /// `OsStr::from_bytes`, which is Unix-only. Other platforms
    /// cannot produce a non-UTF-8 filesystem name from this test
    /// code.
    #[test]
    #[cfg(unix)]
    fn clean_orphaned_tmp_dirs_skips_non_utf8_names() {
        use std::ffi::OsStr;
        use std::os::unix::ffi::OsStrExt;
        let tmp = TempDir::new().unwrap();
        // Name that looks like a tempdir prefix but has a non-UTF-8
        // byte after. `into_string()` in the scan returns Err(_)
        // and the continue branch skips it.
        let mut bytes: Vec<u8> = b".tmp-".to_vec();
        bytes.push(0xFF);
        bytes.extend_from_slice(b"-123");
        let bad_name = OsStr::from_bytes(&bytes);
        let bad_path = tmp.path().join(bad_name);
        std::fs::create_dir(&bad_path).unwrap();

        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(
            bad_path.exists(),
            "non-UTF-8 entry must be left alone — the scan cannot \
             confirm it matches our format, so safe-default is skip",
        );
    }

    /// A nonexistent cache root returns `Ok(())` without error —
    /// called from the `store()` prologue, which may execute
    /// before any cache operation has created the directory.
    #[test]
    fn clean_orphaned_tmp_dirs_handles_missing_cache_root() {
        let tmp = TempDir::new().unwrap();
        let never_created = tmp.path().join("never-created");
        // `is_dir()` short-circuits to Ok(()) without read_dir.
        clean_orphaned_tmp_dirs(&never_created).unwrap();
    }

    /// Multi-entry mix: a DEAD-pid orphan and a LIVE-pid tempdir
    /// side by side — only the dead one is removed. Pins the
    /// per-entry classification logic against a regression that
    /// bailed on the first entry's liveness-probe error or
    /// short-circuited after the first successful remove.
    #[test]
    fn clean_orphaned_tmp_dirs_mixed_entries() {
        let tmp = TempDir::new().unwrap();
        let dead_pid = libc::pid_t::MAX;
        let live_pid = unsafe { libc::getpid() };
        let dead = tmp.path().join(format!("{TMP_DIR_PREFIX}a-{dead_pid}"));
        let live = tmp.path().join(format!("{TMP_DIR_PREFIX}b-{live_pid}"));
        let unrelated = tmp.path().join("c-regular-entry");
        std::fs::create_dir_all(&dead).unwrap();
        std::fs::create_dir_all(&live).unwrap();
        std::fs::create_dir_all(&unrelated).unwrap();

        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(!dead.exists(), "dead orphan must be removed");
        assert!(live.exists(), "live-pid entry must survive");
        assert!(unrelated.exists(), "unrelated entry must survive");
    }

    /// `pid == 0` suffix: the scan rejects non-positive pids before
    /// the liveness probe runs. `kill(0, None)` has process-group
    /// broadcast semantics (POSIX: signal pid 0 = self's pgrp) and
    /// could not authoritatively classify a `.tmp-key-0` directory
    /// as belonging to a dead orphan even on `Err(ESRCH)`, so the
    /// `pid <= 0` filter forces the safe default (preserve) without
    /// invoking the probe at all.
    #[test]
    fn clean_orphaned_tmp_dirs_preserves_pid_zero_suffix() {
        let tmp = TempDir::new().unwrap();
        let entry = tmp.path().join(format!("{TMP_DIR_PREFIX}somekey-0"));
        std::fs::create_dir_all(&entry).unwrap();
        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(
            entry.exists(),
            "pid=0 suffix must be preserved — `pid <= 0` filter \
             skips the entry before kill(0, None)'s pgrp-broadcast \
             ambiguity can reach the liveness probe",
        );
    }

    /// "Negative pid suffix" unreachability pin. The parser uses
    /// `rsplit_once('-')` to extract the suffix AFTER the last `-`,
    /// which by construction never contains a `-` — so
    /// `parse::<i32>()` on the suffix can only produce a
    /// non-negative integer (or fail to parse). A filename like
    /// `.tmp-key--12345` rsplits into `(".tmp-key-", "12345")`:
    /// the suffix `"12345"` parses to pid 12345 (POSITIVE).
    ///
    /// This test documents the invariant and pins the observable
    /// behavior: under `.tmp-key--12345`, the pid parses as a
    /// real positive integer (12345), `kill(12345, None)` likely
    /// returns `Err(ESRCH)` on a fresh pid space, and the entry
    /// is REMOVED (not preserved). The test verifies the REMOVAL
    /// path under this input so a future refactor that changed
    /// `rsplit_once('-')` to `splitn(3, '-')` or a regex — which
    /// COULD emit a `-12345` suffix and open the negative-pid
    /// door — would change the observable behavior and trip this
    /// test's "entry must be gone" assertion.
    ///
    /// Note: the test's observable outcome depends on pid 12345
    /// NOT being alive on the host. If a coincidental live
    /// process happens to hold pid 12345, the entry would be
    /// preserved instead; accept the ≈1-in-N-pids risk
    /// (empirically negligible in CI / dev environments) rather
    /// than contort the test to force a guaranteed-dead pid (the
    /// existing `dead_pid = libc::pid_t::MAX` technique produces
    /// a suffix too large to demonstrate the `--` splitting
    /// behavior).
    #[test]
    fn clean_orphaned_tmp_dirs_double_dash_parses_as_positive_pid() {
        let tmp = TempDir::new().unwrap();
        // Name with a double-dash so the rsplit-once path produces
        // the suffix "12345" (no leading dash — rsplit_once
        // guarantees no delimiter in the suffix). A future regex
        // that emitted "-12345" would behave differently here.
        let entry = tmp.path().join(format!("{TMP_DIR_PREFIX}somekey--12345"));
        std::fs::create_dir_all(&entry).unwrap();
        clean_orphaned_tmp_dirs(tmp.path()).unwrap();

        // Whether the entry is removed depends on whether pid
        // 12345 is alive at test time. The invariant being pinned
        // is the parse direction (positive, not negative), which
        // is a prerequisite for either the remove or preserve
        // branch — a refactor to a negative-suffix parser would
        // land in the `kill(-12345, None)` broadcast probe
        // instead, which returns `Ok(())` and preserves
        // unconditionally. Testing both "parses as positive" AND
        // "either removed or preserved based on liveness" together
        // requires nothing stronger than a liveness check here.
        //
        // The TEST IS PRIMARILY A DOC — the comment above explains
        // the negative-pid unreachability. The assertion below
        // guards against the most likely concrete regression: a
        // regex that emits a `-N` suffix and thereby lands in the
        // broadcast-probe branch. Under that regression, `kill(-N,
        // None)` returns Ok and the entry is ALWAYS preserved;
        // this assertion is satisfied only if the current parse
        // direction (positive pid, real liveness probe) holds.
        //
        // Use `kill(12345, None)` here to decide what we expect:
        // if the pid is live, the entry is preserved; if dead,
        // removed. Either result confirms positive-pid parse.
        let pid_alive = matches!(
            nix::sys::signal::kill(nix::unistd::Pid::from_raw(12345), None),
            Ok(()),
        );
        if pid_alive {
            assert!(
                entry.exists(),
                "pid 12345 was alive at probe time → entry must be \
                 preserved; got: entry removed (regression?)",
            );
        } else {
            assert!(
                !entry.exists(),
                "pid 12345 was dead at probe time → entry must be \
                 removed (proves positive-pid parse). A regression to \
                 negative-pid parse would preserve unconditionally; \
                 entry still exists.",
            );
        }
    }

    /// Regular file entry (not a directory) whose name MATCHES the
    /// `.tmp-{key}-{pid}` pattern with a dead pid. `fs::remove_dir_all`
    /// on a regular file returns `ENOTDIR` / `NotADirectory`; the
    /// scan catches the error in its match arm, logs + continues,
    /// and the file stays in place. Pins that the scan does NOT
    /// fall through to `fs::remove_file` on type mismatch —
    /// quietly removing a file with a tempdir-shaped name could
    /// destroy state belonging to an unrelated tool that happened
    /// to pick a colliding name.
    #[test]
    fn clean_orphaned_tmp_dirs_leaves_regular_file_entry() {
        let tmp = TempDir::new().unwrap();
        let dead_pid = libc::pid_t::MAX;
        let file_entry = tmp
            .path()
            .join(format!("{TMP_DIR_PREFIX}fileshaped-{dead_pid}"));
        std::fs::write(&file_entry, b"not a directory").unwrap();
        clean_orphaned_tmp_dirs(tmp.path()).unwrap();
        assert!(
            file_entry.exists(),
            "regular file with tempdir-shaped name + dead pid must \
             NOT be removed — `remove_dir_all` errors on a file, \
             and the scan's error-tolerance contract leaves it",
        );
    }

    /// Symlink entry whose NAME matches the tempdir pattern but
    /// whose TARGET is an unrelated path outside the cache. The
    /// scan must not follow the symlink — following would risk
    /// `remove_dir_all` deleting the target's contents (the very
    /// bug that a production cache cleaner must never commit).
    ///
    /// Rust's `std::fs::remove_dir_all` on modern platforms uses
    /// `openat` + symlink-aware checks to refuse to follow
    /// symlinks; this test pins that guarantee against a regression
    /// that reached for `fs::remove_dir` (which follows) or hand-
    /// rolled a recursive walk that followed links.
    #[test]
    #[cfg(unix)]
    fn clean_orphaned_tmp_dirs_leaves_symlink_entry() {
        let tmp = TempDir::new().unwrap();

        // Create the real target directory OUTSIDE the cache root
        // — the test asserts the target's contents survive even
        // though the symlink shares the tempdir-like name + dead
        // pid.
        let target_root = TempDir::new().unwrap();
        let target_file = target_root.path().join("sentinel.txt");
        std::fs::write(&target_file, b"must-not-be-deleted").unwrap();

        let dead_pid = libc::pid_t::MAX;
        let symlink = tmp
            .path()
            .join(format!("{TMP_DIR_PREFIX}symkey-{dead_pid}"));
        std::os::unix::fs::symlink(target_root.path(), &symlink).unwrap();

        clean_orphaned_tmp_dirs(tmp.path()).unwrap();

        // Either the symlink itself was removed (modern
        // `remove_dir_all` removes the link without following) OR
        // the symlink stayed (older `remove_dir_all` that errored
        // on symlinks). The LOAD-BEARING invariant is that the
        // TARGET's contents survive — the test's safety guarantee
        // is "data outside the cache root is untouched", not
        // "the symlink entry itself must survive".
        assert!(
            target_file.exists(),
            "symlink target's contents must survive the scan — \
             following symlinks would delete unrelated state \
             outside the cache root, a critical security / data- \
             safety regression",
        );
        assert_eq!(
            std::fs::read(&target_file).unwrap(),
            b"must-not-be-deleted",
            "target file content must be unchanged",
        );
    }

    // -- validate_cache_key unit tests --

    #[test]
    fn cache_validate_key_rejects_empty() {
        let err = validate_cache_key("").unwrap_err();
        assert!(err.to_string().contains("empty"));
    }

    #[test]
    fn cache_validate_key_rejects_whitespace_only() {
        let err = validate_cache_key("   ").unwrap_err();
        assert!(err.to_string().contains("empty"));
    }

    #[test]
    fn cache_validate_key_rejects_forward_slash() {
        let err = validate_cache_key("a/b").unwrap_err();
        assert!(err.to_string().contains("path separator"));
    }

    #[test]
    fn cache_validate_key_rejects_backslash() {
        let err = validate_cache_key("a\\b").unwrap_err();
        assert!(err.to_string().contains("path separator"));
    }

    #[test]
    fn cache_validate_key_rejects_dotdot() {
        // Use a key without slashes to specifically hit the ".." check
        // (slashes are rejected first by the separator check).
        let err = validate_cache_key("foo..bar").unwrap_err();
        assert!(err.to_string().contains("path traversal"));
    }

    #[test]
    fn cache_validate_key_rejects_null_byte() {
        let err = validate_cache_key("key\0evil").unwrap_err();
        assert!(err.to_string().contains("null"));
    }

    #[test]
    fn cache_validate_key_rejects_tmp_prefix() {
        let err = validate_cache_key(".tmp-in-progress").unwrap_err();
        assert!(
            err.to_string().contains(".tmp-"),
            "expected .tmp- rejection, got: {err}"
        );
    }

    #[test]
    fn cache_validate_key_rejects_dot() {
        let err = validate_cache_key(".").unwrap_err();
        assert!(
            err.to_string().contains("directory reference"),
            "expected dot rejection, got: {err}"
        );
    }

    #[test]
    fn cache_validate_key_rejects_dotdot_bare() {
        let err = validate_cache_key("..").unwrap_err();
        assert!(
            err.to_string().contains("directory reference"),
            "expected dotdot rejection, got: {err}"
        );
    }

    #[test]
    fn cache_validate_key_accepts_valid() {
        assert!(validate_cache_key("6.14.2-tarball-x86_64").is_ok());
        assert!(validate_cache_key("local-deadbeef-x86_64").is_ok());
        assert!(validate_cache_key("v6.14-git-a1b2c3d-aarch64").is_ok());
    }

    // -- validate_filename --

    #[test]
    fn cache_validate_filename_rejects_traversal() {
        assert!(validate_filename("../etc/passwd").is_err());
        assert!(validate_filename("foo/../bar").is_err());
    }

    #[test]
    fn cache_validate_filename_rejects_empty() {
        assert!(validate_filename("").is_err());
    }

    #[test]
    fn cache_validate_filename_accepts_valid() {
        assert!(validate_filename("bzImage").is_ok());
        assert!(validate_filename("Image").is_ok());
    }

    // -- image_name traversal via store --

    #[test]
    fn cache_dir_store_rejects_image_name_traversal() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let mut meta = test_metadata("6.14.2");
        meta.image_name = "../escape".to_string();

        let err = cache
            .store("valid-key", &CacheArtifacts::new(&image), &meta)
            .unwrap_err();
        assert!(
            err.to_string().contains("image name"),
            "expected image_name rejection, got: {err}"
        );
    }

    // -- .tmp- prefix via store/lookup --

    #[test]
    fn cache_dir_store_tmp_prefix_key_rejected() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        let err = cache
            .store(".tmp-sneaky", &CacheArtifacts::new(&image), &meta)
            .unwrap_err();
        assert!(
            err.to_string().contains(".tmp-"),
            "expected .tmp- rejection, got: {err}"
        );
    }

    #[test]
    fn cache_dir_lookup_tmp_prefix_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        assert!(cache.lookup(".tmp-sneaky").is_none());
    }

    // -- cache key validation via store/lookup --

    #[test]
    fn cache_dir_store_empty_key_rejected() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        let err = cache
            .store("", &CacheArtifacts::new(&image), &meta)
            .unwrap_err();
        assert!(
            err.to_string().contains("empty"),
            "expected empty-key error, got: {err}"
        );
    }

    #[test]
    fn cache_dir_lookup_empty_key_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        assert!(cache.lookup("").is_none());
    }

    #[test]
    fn cache_dir_store_path_traversal_rejected() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        let err = cache
            .store("../escape", &CacheArtifacts::new(&image), &meta)
            .unwrap_err();
        assert!(
            err.to_string().contains("path"),
            "expected path-traversal error, got: {err}"
        );
    }

    #[test]
    fn cache_dir_lookup_path_traversal_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        assert!(cache.lookup("../escape").is_none());
        assert!(cache.lookup("foo/../bar").is_none());
    }

    #[test]
    fn cache_dir_store_slash_in_key_rejected() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        let err = cache
            .store("a/b", &CacheArtifacts::new(&image), &meta)
            .unwrap_err();
        assert!(
            err.to_string().contains("path separator"),
            "expected path-separator error, got: {err}"
        );
    }

    #[test]
    fn cache_dir_store_whitespace_only_key_rejected() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        let err = cache
            .store("   ", &CacheArtifacts::new(&image), &meta)
            .unwrap_err();
        assert!(
            err.to_string().contains("empty"),
            "expected empty/whitespace error, got: {err}"
        );
    }

    // -- clean with mixed valid + corrupt entries --

    #[test]
    fn cache_dir_clean_keep_n_with_mixed_entries() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());

        // Two valid entries with different timestamps.
        let meta_new = KernelMetadata {
            built_at: "2026-04-12T10:00:00Z".to_string(),
            ..test_metadata("6.14.2")
        };
        let meta_old = KernelMetadata {
            built_at: "2026-04-10T10:00:00Z".to_string(),
            ..test_metadata("6.13.0")
        };
        cache
            .store("new", &CacheArtifacts::new(&image), &meta_new)
            .unwrap();
        cache
            .store("old", &CacheArtifacts::new(&image), &meta_old)
            .unwrap();

        // One corrupt entry (no metadata).
        let corrupt_dir = tmp.path().join("cache").join("corrupt");
        fs::create_dir_all(&corrupt_dir).unwrap();

        // list() returns 3 entries. Corrupt entries (no built_at) sort
        // last. keep=1 should keep the newest valid entry and remove
        // the old valid + corrupt entries.
        let removed = cache.clean_keep(1).unwrap();
        assert_eq!(removed, 2);

        let remaining = cache.list().unwrap();
        assert_eq!(remaining.len(), 1);
        assert_eq!(remaining[0].key(), "new");
    }

    // -- atomic write safety --

    /// Regression for the rename-TOCTOU fix: a second `store` with
    /// the same key must atomically replace the previous entry's
    /// content without leaving half-installed state — even though
    /// the underlying code path exercises the
    /// `final_dir-exists → swap` branch rather than the plain
    /// rename. The new content wins, the old content is gone, and
    /// no `.evict-*` staging dir lingers under cache_root.
    #[test]
    fn cache_dir_store_overwrites_existing_key_atomically() {
        let tmp = TempDir::new().unwrap();
        let cache_root = tmp.path().join("cache");
        let cache = CacheDir::with_root(cache_root.clone());

        // First install.
        let src_a = TempDir::new().unwrap();
        let image_a = create_fake_image(src_a.path());
        fs::write(&image_a, b"version-a").unwrap();
        let mut meta_a = test_metadata("6.14.2");
        meta_a.built_at = "2026-04-10T00:00:00Z".to_string();
        let entry_a = cache
            .store("collide", &CacheArtifacts::new(&image_a), &meta_a)
            .unwrap();
        assert_eq!(
            fs::read(entry_a.path.join("bzImage")).unwrap(),
            b"version-a"
        );

        // Second install with the same key — exercises the rename-
        // to-staging branch. Different built_at so we can tell
        // which metadata won.
        let src_b = TempDir::new().unwrap();
        let image_b = create_fake_image(src_b.path());
        fs::write(&image_b, b"version-b").unwrap();
        let mut meta_b = test_metadata("6.14.2");
        meta_b.built_at = "2026-04-18T00:00:00Z".to_string();
        let entry_b = cache
            .store("collide", &CacheArtifacts::new(&image_b), &meta_b)
            .unwrap();

        // New content wins.
        assert_eq!(
            fs::read(entry_b.path.join("bzImage")).unwrap(),
            b"version-b",
            "new content must replace old content atomically"
        );
        let installed_meta = read_metadata(&entry_b.path).expect("metadata.json");
        assert_eq!(installed_meta.built_at, "2026-04-18T00:00:00Z");

        // No staging or tmp residue.
        for dirent in fs::read_dir(&cache_root).unwrap() {
            let name = dirent.unwrap().file_name().to_string_lossy().into_owned();
            assert!(
                !name.starts_with(".evict-") && !name.starts_with(".tmp-"),
                "unexpected leftover directory under cache_root: {name}"
            );
        }
    }

    #[test]
    fn cache_dir_store_cleans_stale_tmp() {
        let tmp = TempDir::new().unwrap();
        let cache_root = tmp.path().join("cache");
        let cache = CacheDir::with_root(cache_root.clone());

        // Create a stale .tmp- directory simulating a prior crash.
        let stale_tmp = cache_root.join(format!(".tmp-mykey-{}", std::process::id()));
        fs::create_dir_all(&stale_tmp).unwrap();
        fs::write(stale_tmp.join("junk"), b"leftover").unwrap();

        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        // Store should succeed despite stale tmp dir.
        let entry = cache
            .store("mykey", &CacheArtifacts::new(&image), &meta)
            .unwrap();
        assert!(entry.path.join("bzImage").exists());
        // Stale tmp dir should be gone.
        assert!(!stale_tmp.exists());
    }

    /// Concurrent readers calling `lookup()` while a writer is
    /// rapidly overwriting the same cache key must never observe a
    /// half-installed entry. The atomic rename-to-staging swap in
    /// `store()` should make every successful lookup return an entry
    /// whose `image_path()` exists and whose contents match one of
    /// the writer's complete versions — never a missing file, never a
    /// truncated image.
    ///
    /// Pinning this behavior catches regressions where the swap
    /// sequence is reordered (e.g. removing `final_dir` before
    /// renaming the tmp dir into place) or replaced with a non-atomic
    /// copy. Such regressions would let a reader observe a cache
    /// entry with valid metadata but a missing `bzImage`, or a
    /// partially-written image with bytes from two generations.
    #[test]
    fn cache_dir_store_atomic_under_concurrent_readers() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
        use std::thread;

        let tmp = TempDir::new().unwrap();
        let cache_root = tmp.path().join("cache");
        let cache = Arc::new(CacheDir::with_root(cache_root.clone()));

        // Writer sources: two distinct full versions with
        // recognizable, long-ish content so a torn read would be
        // detectable by byte comparison, not just length.
        let src_a = TempDir::new().unwrap();
        let image_a = src_a.path().join("bzImage");
        let content_a = b"AAAAAAAA-image-version-a-AAAAAAAA".repeat(64);
        fs::write(&image_a, &content_a).unwrap();

        let src_b = TempDir::new().unwrap();
        let image_b = src_b.path().join("bzImage");
        let content_b = b"BBBBBBBB-image-version-b-BBBBBBBB".repeat(64);
        fs::write(&image_b, &content_b).unwrap();

        // Prime the cache so lookup() has something to find from
        // iteration one onwards. Without priming, early readers would
        // legitimately see None until the writer lands the first
        // store — and we want to assert "never missing once present,"
        // which requires an initial present state.
        let meta_prime = test_metadata("6.14.2");
        cache
            .store("atomic-key", &CacheArtifacts::new(&image_a), &meta_prime)
            .unwrap();

        const WRITE_ITERATIONS: usize = 40;
        let stop = Arc::new(AtomicBool::new(false));
        let lookups_observed = Arc::new(AtomicUsize::new(0));
        let atomicity_violations = Arc::new(AtomicUsize::new(0));

        // Spawn reader threads. Each reader loops until `stop` is
        // set, calling lookup() and checking that when Some(entry)
        // comes back, the image file exists and matches one of the
        // two known writer contents byte-for-byte.
        let reader_count = 4;
        let mut readers = Vec::with_capacity(reader_count);
        for _ in 0..reader_count {
            let cache = Arc::clone(&cache);
            let stop = Arc::clone(&stop);
            let lookups_observed = Arc::clone(&lookups_observed);
            let violations = Arc::clone(&atomicity_violations);
            let expected_a = content_a.clone();
            let expected_b = content_b.clone();
            readers.push(thread::spawn(move || {
                while !stop.load(Ordering::Relaxed) {
                    let Some(entry) = cache.lookup("atomic-key") else {
                        // Once primed, lookup must always see an
                        // entry. A None here is a real atomicity
                        // violation: the writer briefly removed the
                        // final_dir without immediately replacing it.
                        violations.fetch_add(1, Ordering::Relaxed);
                        continue;
                    };
                    let image_path = entry.image_path();
                    let Ok(bytes) = fs::read(&image_path) else {
                        // Entry directory + metadata visible, but
                        // image file missing → non-atomic install.
                        violations.fetch_add(1, Ordering::Relaxed);
                        continue;
                    };
                    if bytes != expected_a && bytes != expected_b {
                        // Torn read: bytes don't match either
                        // complete version. Would indicate the image
                        // was observed mid-copy.
                        violations.fetch_add(1, Ordering::Relaxed);
                    }
                    lookups_observed.fetch_add(1, Ordering::Relaxed);
                }
            }));
        }

        // Writer: alternate between version A and version B,
        // exercising the rename-to-staging branch on every iteration
        // after the first (final_dir already exists from priming).
        for i in 0..WRITE_ITERATIONS {
            let (image, label) = if i % 2 == 0 {
                (&image_a, "a")
            } else {
                (&image_b, "b")
            };
            let mut meta = test_metadata("6.14.2");
            meta.built_at = format!("2026-04-18T00:00:{:02}Z", i % 60);
            meta.config_hash = Some(format!("iter-{i}-{label}"));
            cache
                .store("atomic-key", &CacheArtifacts::new(image), &meta)
                .expect("store under concurrent readers must not fail");
        }

        stop.store(true, Ordering::Relaxed);
        for r in readers {
            r.join().expect("reader thread panicked");
        }

        assert_eq!(
            atomicity_violations.load(Ordering::Relaxed),
            0,
            "lookup observed a missing or torn cache entry during concurrent store; \
             rename-to-staging swap is not atomic",
        );
        assert!(
            lookups_observed.load(Ordering::Relaxed) > 0,
            "readers never observed a successful lookup — test did not \
             actually exercise the concurrency window",
        );

        // Post-condition: final state is intact and no staging or
        // tmp residue leaked out of the write loop.
        let final_entry = cache.lookup("atomic-key").expect("entry must exist");
        let final_bytes = fs::read(final_entry.image_path()).unwrap();
        assert!(
            final_bytes == content_a || final_bytes == content_b,
            "final image must match one of the writer's versions",
        );
        for dirent in fs::read_dir(&cache_root).unwrap() {
            let name = dirent.unwrap().file_name().to_string_lossy().into_owned();
            assert!(
                !name.starts_with(".evict-") && !name.starts_with(".tmp-"),
                "unexpected leftover directory under cache_root: {name}",
            );
        }
    }

    #[test]
    fn cache_dir_store_with_vmlinux() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let vmlinux = src_dir.path().join("vmlinux");
        fs::write(&vmlinux, b"fake vmlinux ELF").unwrap();
        let meta = test_metadata("6.14.2");

        let entry = cache
            .store(
                "with-vmlinux",
                &CacheArtifacts::new(&image).with_vmlinux(&vmlinux),
                &meta,
            )
            .unwrap();
        assert!(entry.path.join("bzImage").exists());
        assert!(entry.path.join("vmlinux").exists());
        assert!(entry.path.join("metadata.json").exists());
        // Metadata records has_vmlinux.
        assert!(entry.metadata.has_vmlinux);
        // Original files still exist (copy, not move).
        assert!(image.exists());
        assert!(vmlinux.exists());
    }

    #[test]
    fn cache_dir_store_without_vmlinux() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        let entry = cache
            .store("no-vmlinux", &CacheArtifacts::new(&image), &meta)
            .unwrap();
        assert!(entry.path.join("bzImage").exists());
        assert!(!entry.path.join("vmlinux").exists());
        assert!(entry.path.join("metadata.json").exists());
        // Metadata records absence of vmlinux; vmlinux_stripped is
        // meaningless without a vmlinux but must still be false (the
        // strip pipeline never ran).
        assert!(!entry.metadata.has_vmlinux);
        assert!(!entry.metadata.vmlinux_stripped);
    }

    #[test]
    fn cache_dir_store_strips_vmlinux_internally() {
        // Real ELF fixture: store() must run strip_vmlinux_debug and
        // the stored vmlinux must reflect the strip (smaller than
        // source, no .debug_* sections).
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let vmlinux = create_strip_test_fixture(src_dir.path());
        let source_size = fs::metadata(&vmlinux).unwrap().len();
        let meta = test_metadata("6.14.2");

        let entry = cache
            .store(
                "strip-in-store",
                &CacheArtifacts::new(&image).with_vmlinux(&vmlinux),
                &meta,
            )
            .unwrap();
        let cached_vmlinux = entry.path.join("vmlinux");
        let cached_size = fs::metadata(&cached_vmlinux).unwrap().len();
        assert!(
            cached_size < source_size,
            "stored vmlinux ({cached_size} bytes) should be smaller \
             than source ({source_size}) after internal strip"
        );
        let data = fs::read(&cached_vmlinux).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();
        let section_names: Vec<&str> = elf
            .section_headers
            .iter()
            .filter_map(|s| elf.shdr_strtab.get_at(s.sh_name))
            .collect();
        assert!(
            !section_names.contains(&".debug_info"),
            "internal strip should have removed .debug_info"
        );
        assert!(entry.metadata.has_vmlinux);
        assert!(
            entry.metadata.vmlinux_stripped,
            "strip-succeeds path must set vmlinux_stripped = true"
        );
    }

    #[test]
    fn cache_dir_store_falls_back_when_strip_fails() {
        // Unparseable vmlinux: strip errors, store() falls back to
        // copying the raw bytes. has_vmlinux stays true (so consumers
        // still see the sidecar) but vmlinux_stripped is false (so
        // consumers can tell the raw-fallback path ran).
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let vmlinux = src_dir.path().join("vmlinux");
        let raw = b"not an ELF file";
        fs::write(&vmlinux, raw).unwrap();
        let meta = test_metadata("6.14.2");

        let entry = cache
            .store(
                "strip-fallback",
                &CacheArtifacts::new(&image).with_vmlinux(&vmlinux),
                &meta,
            )
            .unwrap();
        let cached = fs::read(entry.path.join("vmlinux")).unwrap();
        assert_eq!(cached, raw, "fallback must copy raw bytes verbatim");
        assert!(entry.metadata.has_vmlinux);
        assert!(
            !entry.metadata.vmlinux_stripped,
            "raw-fallback path must set vmlinux_stripped = false so \
             `ktstr cache list --json` surfaces the strip failure"
        );
    }

    // -- should_warn_unstripped (pure decision logic driving
    //    `CacheDir::lookup`'s per-lookup "unstripped vmlinux" warning).

    /// Helper for the three `should_warn_unstripped` tests below:
    /// construct a synthetic [`CacheEntry`] with explicit
    /// `has_vmlinux` / `vmlinux_stripped` bits and the rest of the
    /// metadata filled in from [`KernelMetadata::new`]. The entry-dir
    /// path is never touched (the decision logic only reads the
    /// metadata bools), so a synthetic PathBuf is enough.
    fn make_warn_test_entry(has_vmlinux: bool, vmlinux_stripped: bool) -> CacheEntry {
        let mut meta = KernelMetadata::new(
            KernelSource::Tarball,
            "x86_64".to_string(),
            "bzImage".to_string(),
            "2026-04-24T12:00:00Z".to_string(),
        );
        meta.set_has_vmlinux(has_vmlinux);
        meta.set_vmlinux_stripped(vmlinux_stripped);
        CacheEntry {
            key: "test-key".to_string(),
            path: PathBuf::from("/nonexistent/entry"),
            metadata: meta,
        }
    }

    /// An entry with a vmlinux that came from the raw-fallback path
    /// (strip failed at store time) MUST trigger the warning. The
    /// per-lookup eprintln is the operator's persistent signal to
    /// rebuild the cache.
    #[test]
    fn should_warn_unstripped_fires_when_vmlinux_present_and_unstripped() {
        let entry = make_warn_test_entry(true, false);
        assert!(
            should_warn_unstripped(&entry),
            "has_vmlinux=true + vmlinux_stripped=false must warn"
        );
    }

    /// An entry with a successfully-stripped vmlinux MUST NOT warn.
    /// This is the common case; warning here would be noise that
    /// operators learn to ignore, defeating the signal on the
    /// genuine failure case above.
    #[test]
    fn should_warn_unstripped_silent_when_vmlinux_stripped() {
        let entry = make_warn_test_entry(true, true);
        assert!(
            !should_warn_unstripped(&entry),
            "has_vmlinux=true + vmlinux_stripped=true must not warn"
        );
    }

    /// An entry with no vmlinux at all MUST NOT warn. The
    /// `vmlinux_stripped` bit is meaningless in that shape (always
    /// `false` by construction in [`CacheDir::store`]'s no-vmlinux
    /// branch) and warning would fire on every cache hit that simply
    /// did not cache a vmlinux — pure noise.
    #[test]
    fn should_warn_unstripped_silent_when_no_vmlinux() {
        let entry = make_warn_test_entry(false, false);
        assert!(
            !should_warn_unstripped(&entry),
            "has_vmlinux=false must not warn (no vmlinux to worry about)"
        );
    }

    #[test]
    fn cache_dir_store_preserves_original_vmlinux() {
        // strip_vmlinux_debug reads the source path; check the
        // source file is still there after store() (no move, no
        // truncate).
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let vmlinux = create_strip_test_fixture(src_dir.path());
        let source_size = fs::metadata(&vmlinux).unwrap().len();
        let meta = test_metadata("6.14.2");

        cache
            .store(
                "preserve-src",
                &CacheArtifacts::new(&image).with_vmlinux(&vmlinux),
                &meta,
            )
            .unwrap();
        assert!(vmlinux.exists(), "source vmlinux must survive store()");
        assert_eq!(
            fs::metadata(&vmlinux).unwrap().len(),
            source_size,
            "source vmlinux size must not change"
        );
    }

    #[test]
    fn cache_dir_store_preserves_original_image() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");

        cache
            .store("key", &CacheArtifacts::new(&image), &meta)
            .unwrap();

        // Original image must still exist (copy, not move).
        assert!(image.exists());
    }

    // -- CacheEntry accessors --

    #[test]
    fn cache_entry_image_path_joins_key_with_image_name() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let entry = cache
            .store(
                "key",
                &CacheArtifacts::new(&image),
                &test_metadata("6.14.2"),
            )
            .unwrap();
        assert_eq!(entry.image_path(), entry.path.join("bzImage"));
        assert!(entry.image_path().exists());
    }

    #[test]
    fn cache_entry_vmlinux_path_some_when_stored() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let vmlinux = create_strip_test_fixture(src_dir.path());
        let entry = cache
            .store(
                "with-vml",
                &CacheArtifacts::new(&image).with_vmlinux(&vmlinux),
                &test_metadata("6.14.2"),
            )
            .unwrap();
        let vml_path = entry.vmlinux_path().expect("vmlinux_path() should be Some");
        assert_eq!(vml_path, entry.path.join("vmlinux"));
        assert!(vml_path.exists());
    }

    #[test]
    fn cache_entry_vmlinux_path_none_when_not_stored() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let entry = cache
            .store(
                "no-vml",
                &CacheArtifacts::new(&image),
                &test_metadata("6.14.2"),
            )
            .unwrap();
        assert!(entry.vmlinux_path().is_none());
    }

    // -- KconfigStatus variants --

    #[test]
    fn kconfig_status_matches_when_hash_equal() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2").with_ktstr_kconfig_hash(Some("deadbeef".to_string()));
        let entry = cache
            .store("kc-match", &CacheArtifacts::new(&image), &meta)
            .unwrap();
        assert_eq!(entry.kconfig_status("deadbeef"), KconfigStatus::Matches);
    }

    #[test]
    fn kconfig_status_untracked_when_no_hash_in_entry() {
        // Pre-kconfig-tracking cache entries (ktstr_kconfig_hash == None)
        // must surface as `Untracked`, not `Stale`. `find_kernel`'s
        // stale-filter at lib.rs treats `Untracked` as "keep" so legacy
        // entries remain usable — checked here so a regression that
        // conflates "no recorded hash" with "different hash" surfaces
        // at unit-test time.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        // test_metadata seeds ktstr_kconfig_hash = Some("def456"); strip
        // it here to hit the None branch in kconfig_status.
        let meta = KernelMetadata {
            ktstr_kconfig_hash: None,
            ..test_metadata("6.14.2")
        };
        let entry = cache
            .store("kc-untracked", &CacheArtifacts::new(&image), &meta)
            .unwrap();
        assert_eq!(entry.kconfig_status("anything"), KconfigStatus::Untracked);
    }

    #[test]
    fn kconfig_status_stale_pins_cached_and_current_field_order() {
        // `KconfigStatus::Stale { cached, current }` names the two
        // hashes for diagnostics: `cached` is what the entry recorded
        // at build time, `current` is what the caller is comparing
        // against. A swap would invert the "was / is" story in every
        // diagnostic message consuming these fields (e.g. `kernel list`
        // tags, future error-formatting code). Pin the mapping so a
        // refactor that swaps the two construction args breaks this
        // test before it ships a misleading diagnostic.
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2").with_ktstr_kconfig_hash(Some("old_cached".to_string()));
        let entry = cache
            .store("kc-stale", &CacheArtifacts::new(&image), &meta)
            .unwrap();
        match entry.kconfig_status("new_current") {
            KconfigStatus::Stale { cached, current } => {
                assert_eq!(
                    cached, "old_cached",
                    "`cached` must hold the hash recorded in the entry"
                );
                assert_eq!(
                    current, "new_current",
                    "`current` must hold the hash the caller passed in"
                );
            }
            other => panic!("expected KconfigStatus::Stale, got {other:?}"),
        }
    }

    // -- prefer_source_tree_for_dwarf --

    #[test]
    fn prefer_source_tree_local_with_vmlinux() {
        // Local-source cache entry whose source tree is still on disk
        // and has a vmlinux whose size + mtime match the recorded
        // stat pair: helper returns the source tree path.
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        let src_tree = tmp.path().join("src");
        fs::create_dir_all(&cache_entry).unwrap();
        fs::create_dir_all(&src_tree).unwrap();
        let vmlinux = src_tree.join("vmlinux");
        fs::write(&vmlinux, b"fake-elf").unwrap();
        let stat = fs::metadata(&vmlinux).unwrap();
        let mtime_secs = stat
            .modified()
            .unwrap()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        let meta = KernelMetadata {
            version: Some("6.14.2".to_string()),
            source: KernelSource::Local {
                source_tree_path: Some(src_tree.clone()),
                git_hash: None,
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: Some(stat.len()),
            source_vmlinux_mtime_secs: Some(mtime_secs),
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(prefer_source_tree_for_dwarf(&cache_entry), Some(src_tree));
    }

    #[test]
    fn prefer_source_tree_local_without_vmlinux_in_tree() {
        // Local-source cache entry but source tree lacks vmlinux:
        // fall back to None so caller keeps the cache-entry path.
        // The entry still records the stat pair (cache-store time
        // may have written the pair, then the user later removed the
        // source-tree vmlinux); the helper bails on the absent
        // `vmlinux` file before reaching the size/mtime check.
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        let src_tree = tmp.path().join("src");
        fs::create_dir_all(&cache_entry).unwrap();
        fs::create_dir_all(&src_tree).unwrap();
        // No vmlinux in src_tree.

        let meta = KernelMetadata {
            version: None,
            source: KernelSource::Local {
                source_tree_path: Some(src_tree),
                git_hash: None,
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: false,
            vmlinux_stripped: false,
            source_vmlinux_size: Some(42),
            source_vmlinux_mtime_secs: Some(1_700_000_000),
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(prefer_source_tree_for_dwarf(&cache_entry), None);
    }

    #[test]
    fn prefer_source_tree_tarball_source_returns_none() {
        // Tarball source entry has no source_tree_path — return None
        // so caller uses the cache-entry directory (symbol lookup only,
        // no file:line).
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        fs::create_dir_all(&cache_entry).unwrap();

        let meta = KernelMetadata {
            version: Some("6.14.2".to_string()),
            source: KernelSource::Tarball,
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(prefer_source_tree_for_dwarf(&cache_entry), None);
    }

    #[test]
    fn prefer_source_tree_no_metadata_returns_none() {
        // Directory without metadata.json (e.g. a build-tree root, not
        // a cache entry): return None, caller keeps its existing path.
        let tmp = TempDir::new().unwrap();
        assert_eq!(prefer_source_tree_for_dwarf(tmp.path()), None);
    }

    /// Malformed `metadata.json` — present on disk but not valid
    /// [`KernelMetadata`] — must short-circuit to `None`. The
    /// `read_metadata(..).ok()?` guard in
    /// [`prefer_source_tree_for_dwarf`] converts the parse failure
    /// into `None` without bailing, so callers fall back to the
    /// cache directory for symbol-only lookup rather than having
    /// the DWARF path blow up on a corrupted entry.
    ///
    /// A regression that replaced the `.ok()?` with `.unwrap()`,
    /// `.expect(..)`, or an `anyhow::Result` propagation would
    /// break this test — preserving the "silent fallback" contract
    /// documented on the function.
    #[test]
    fn prefer_source_tree_metadata_parse_failure_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        fs::create_dir_all(&cache_entry).unwrap();
        // Valid JSON shape but NOT a `KernelMetadata` — missing
        // every required field. serde_json::from_str errors with
        // "missing field", which `read_metadata` maps to
        // `Err(String)`, which `prefer_source_tree_for_dwarf`'s
        // `.ok()?` turns into `None`.
        fs::write(
            cache_entry.join("metadata.json"),
            br#"{"not_kernel_metadata": true}"#,
        )
        .unwrap();

        assert_eq!(
            prefer_source_tree_for_dwarf(&cache_entry),
            None,
            "malformed metadata.json must short-circuit to None, not bail",
        );

        // Completely invalid JSON (not parseable at the token level)
        // must also short-circuit. Covers serde's two distinct
        // error classes — tokenizer failure vs shape mismatch —
        // both of which map to `Err(String)` inside `read_metadata`.
        let other_entry = tmp.path().join("other");
        fs::create_dir_all(&other_entry).unwrap();
        fs::write(other_entry.join("metadata.json"), b"not json at all {{{").unwrap();
        assert_eq!(
            prefer_source_tree_for_dwarf(&other_entry),
            None,
            "unparseable metadata.json must short-circuit to None, not bail",
        );
    }

    /// Local-source cache entry whose `source_tree_path` is
    /// explicitly `None` short-circuits at the `let src_path =
    /// source_tree_path?;` line — no filesystem probe runs for the
    /// missing path. Pins the "tree location not recorded" branch
    /// documented on [`prefer_source_tree_for_dwarf`].
    ///
    /// Distinct from `prefer_source_tree_local_without_vmlinux_in_tree`:
    /// that test has `source_tree_path = Some(...)` but the
    /// filesystem lacks `vmlinux`, so the function reaches the
    /// `src_path.join("vmlinux").is_file()` check before returning
    /// None. This test short-circuits earlier, before any filesystem
    /// inspection — a regression that replaced the `?` with a
    /// `.unwrap_or_else(|| default_path)` or a fallback would break
    /// it.
    #[test]
    fn prefer_source_tree_local_with_none_source_tree_path_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        fs::create_dir_all(&cache_entry).unwrap();

        let meta = KernelMetadata {
            version: Some("6.14.2".to_string()),
            source: KernelSource::Local {
                source_tree_path: None,
                git_hash: Some("abc123".to_string()),
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: Some(42),
            source_vmlinux_mtime_secs: Some(1_700_000_000),
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(
            prefer_source_tree_for_dwarf(&cache_entry),
            None,
            "Local entry with source_tree_path=None must short-circuit \
             to None at the `let src_path = source_tree_path?;` line \
             — no filesystem probe must run",
        );
    }

    /// When `source_vmlinux_size` and `source_vmlinux_mtime_secs`
    /// match the on-disk vmlinux's current stat, the helper returns
    /// the source-tree path. Pins the validate-and-pass branch
    /// added for the user-rebuild-detection gate.
    #[test]
    fn prefer_source_tree_validates_matching_vmlinux_stat_and_returns_path() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        let src_tree = tmp.path().join("src");
        fs::create_dir_all(&cache_entry).unwrap();
        fs::create_dir_all(&src_tree).unwrap();
        let vmlinux = src_tree.join("vmlinux");
        fs::write(&vmlinux, b"fake-elf-bytes").unwrap();
        let stat = fs::metadata(&vmlinux).unwrap();
        let mtime_secs = stat
            .modified()
            .unwrap()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        let meta = KernelMetadata {
            version: None,
            source: KernelSource::Local {
                source_tree_path: Some(src_tree.clone()),
                git_hash: None,
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: Some(stat.len()),
            source_vmlinux_mtime_secs: Some(mtime_secs),
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(
            prefer_source_tree_for_dwarf(&cache_entry),
            Some(src_tree),
            "matching size + mtime must pass the validation gate"
        );
    }

    /// A size mismatch (user rebuild changed the vmlinux) drops the
    /// helper to None so callers fall back to the cached stripped
    /// vmlinux. Without the gate, DWARF reads would silently route
    /// to a vmlinux whose line numbers no longer correspond to the
    /// cache's BTF.
    #[test]
    fn prefer_source_tree_size_mismatch_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        let src_tree = tmp.path().join("src");
        fs::create_dir_all(&cache_entry).unwrap();
        fs::create_dir_all(&src_tree).unwrap();
        let vmlinux = src_tree.join("vmlinux");
        fs::write(&vmlinux, b"fake-elf-bytes").unwrap();
        let stat = fs::metadata(&vmlinux).unwrap();
        let mtime_secs = stat
            .modified()
            .unwrap()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        // Stored size deliberately wrong by one byte.
        let meta = KernelMetadata {
            version: None,
            source: KernelSource::Local {
                source_tree_path: Some(src_tree),
                git_hash: None,
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: Some(stat.len() + 1),
            source_vmlinux_mtime_secs: Some(mtime_secs),
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(
            prefer_source_tree_for_dwarf(&cache_entry),
            None,
            "size mismatch must drop validation and return None"
        );
    }

    /// An mtime mismatch (typical user-rebuild signal — `make`
    /// touches `vmlinux` even when the resulting bytes happen to be
    /// the same length) drops to None on the same fallback path.
    #[test]
    fn prefer_source_tree_mtime_mismatch_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        let src_tree = tmp.path().join("src");
        fs::create_dir_all(&cache_entry).unwrap();
        fs::create_dir_all(&src_tree).unwrap();
        let vmlinux = src_tree.join("vmlinux");
        fs::write(&vmlinux, b"fake-elf-bytes").unwrap();
        let stat = fs::metadata(&vmlinux).unwrap();
        let mtime_secs = stat
            .modified()
            .unwrap()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        // Stored mtime deliberately offset by an hour.
        let meta = KernelMetadata {
            version: None,
            source: KernelSource::Local {
                source_tree_path: Some(src_tree),
                git_hash: None,
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: Some(stat.len()),
            source_vmlinux_mtime_secs: Some(mtime_secs - 3600),
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(
            prefer_source_tree_for_dwarf(&cache_entry),
            None,
            "mtime mismatch must drop validation and return None"
        );
    }

    // -- recover_local_source_tree --
    //
    // Sibling helper to `prefer_source_tree_for_dwarf` but with
    // distinct semantics: NO `vmlinux` presence check, NO arm for
    // tarball/git entries (returns None when metadata.source isn't
    // `KernelSource::Local`). Used by callers that just need the
    // source tree directory (git open, commit detection); DWARF
    // callers stay on `prefer_source_tree_for_dwarf` for the
    // additional vmlinux gate.

    /// Cache entry built from a local tree: helper returns the
    /// recorded `source_tree_path` regardless of whether the
    /// source tree still has a `vmlinux` on disk. The vmlinux gate
    /// is `prefer_source_tree_for_dwarf`'s concern, NOT this
    /// helper's — git tree opens and commit detection only need
    /// the directory path.
    #[test]
    fn recover_local_source_tree_local_with_path_returns_source_tree() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        let src_tree = tmp.path().join("src");
        fs::create_dir_all(&cache_entry).unwrap();
        fs::create_dir_all(&src_tree).unwrap();
        // Deliberately omit vmlinux from the source tree so this
        // test pins the "no vmlinux gate" contract — a regression
        // that copied prefer_source_tree_for_dwarf's vmlinux check
        // would surface here as a None result.

        let meta = KernelMetadata {
            version: Some("6.14.2".to_string()),
            source: KernelSource::Local {
                source_tree_path: Some(src_tree.clone()),
                git_hash: Some("abc1234".to_string()),
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: false,
            vmlinux_stripped: false,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(recover_local_source_tree(&cache_entry), Some(src_tree));
    }

    /// No `metadata.json` on disk: helper returns `None` and the
    /// caller falls back to using the input dir verbatim
    /// (cargo-ktstr stats compare's `gix::open` site treats the
    /// env value as the source tree itself; sidecar's
    /// `resolve_kernel_source_dir` Path arm does the same).
    #[test]
    fn recover_local_source_tree_no_metadata_returns_none() {
        let tmp = TempDir::new().unwrap();
        // No metadata.json planted — `tmp.path()` is a bare empty
        // tempdir, modeling the dirty-source-tree path that
        // skipped cache store.
        assert_eq!(recover_local_source_tree(tmp.path()), None);
    }

    /// `metadata.json` present but `source` is `Tarball`: helper
    /// returns `None`. Tarball entries never carry a
    /// `source_tree_path` (the extraction dir is transient);
    /// callers should not probe a tarball entry as a git repo.
    #[test]
    fn recover_local_source_tree_tarball_source_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        fs::create_dir_all(&cache_entry).unwrap();

        let meta = KernelMetadata {
            version: Some("6.14.2".to_string()),
            source: KernelSource::Tarball,
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(recover_local_source_tree(&cache_entry), None);
    }

    /// `Local` source with `source_tree_path: None` (the
    /// dirty-tree case where the source-tree path was not
    /// recorded): helper returns `None`. Distinct from the
    /// no-metadata case but produces the same outcome.
    #[test]
    fn recover_local_source_tree_local_with_none_path_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        fs::create_dir_all(&cache_entry).unwrap();

        let meta = KernelMetadata {
            version: Some("6.14.2".to_string()),
            source: KernelSource::Local {
                source_tree_path: None,
                git_hash: Some("abc1234".to_string()),
            },
            arch: "x86_64".to_string(),
            image_name: "bzImage".to_string(),
            config_hash: None,
            built_at: "2026-04-18T10:00:00Z".to_string(),
            ktstr_kconfig_hash: None,
            has_vmlinux: true,
            vmlinux_stripped: true,
            source_vmlinux_size: None,
            source_vmlinux_mtime_secs: None,
        };
        fs::write(
            cache_entry.join("metadata.json"),
            serde_json::to_string(&meta).unwrap(),
        )
        .unwrap();

        assert_eq!(recover_local_source_tree(&cache_entry), None);
    }

    /// Malformed `metadata.json`: helper short-circuits to `None`
    /// silently. Mirrors `prefer_source_tree_for_dwarf`'s contract
    /// — a corrupt cache entry must not blow up callers; they fall
    /// back to using the input dir verbatim.
    #[test]
    fn recover_local_source_tree_malformed_metadata_returns_none() {
        let tmp = TempDir::new().unwrap();
        let cache_entry = tmp.path().join("cache");
        fs::create_dir_all(&cache_entry).unwrap();
        fs::write(
            cache_entry.join("metadata.json"),
            br#"{"not_kernel_metadata": true}"#,
        )
        .unwrap();
        assert_eq!(recover_local_source_tree(&cache_entry), None);
    }

    // -- strip_vmlinux_debug --

    /// Check whether `elf` has a defined symbol with the given name.
    /// Mirrors the `sym_addr` closure inside `KernelSymbols::from_vmlinux`
    /// by requiring `st_value != 0` to reject undefined/absent symbols.
    fn has_symbol(elf: &goblin::elf::Elf, name: &str) -> bool {
        elf.syms
            .iter()
            .any(|s| s.st_value != 0 && elf.strtab.get_at(s.st_name) == Some(name))
    }

    /// Build a minimal ELF covering every strip dispatch branch:
    /// `.text` (code, bytes dropped via SHT_NOBITS), `.BTF` and
    /// `.rodata` (kept whole via the keep-list predicate), `.bss`
    /// (keep-list, already SHT_NOBITS), `.BTF.ext` + `.debug_*`
    /// (deleted), and the zero-data sections (`.data`,
    /// `.data..percpu`, `.init.data`; bytes dropped via SHT_NOBITS).
    /// Each bytes-dropped section has a symbol pointing at an
    /// in-bounds offset so tests can assert the symbols survive
    /// `Builder::delete_orphans`.
    fn create_strip_test_fixture(dir: &Path) -> PathBuf {
        use object::write;
        let mut obj = write::Object::new(
            object::BinaryFormat::Elf,
            object::Architecture::X86_64,
            object::Endianness::Little,
        );
        // .text — loadable code (not in keep-list, bytes dropped by keep-list path).
        let text_id = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
        obj.append_section_data(text_id, &[0xCC; 64], 1);
        // Symbol so .symtab and .strtab are generated.
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_text_symbol".to_vec(),
            value: 0x10,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(text_id),
            flags: object::SymbolFlags::None,
        });
        // .BTF — kept by both keep-list and fallback.
        let btf_id = obj.add_section(Vec::new(), b".BTF".to_vec(), object::SectionKind::Metadata);
        obj.append_section_data(btf_id, &[0xEB; 256], 1);
        // .rodata — kept by keep-list (IKCONFIG gzip blob at runtime).
        // Bytes are preserved verbatim so read_hz_from_ikconfig can scan
        // for the IKCFG_ST marker; fixture stores an opaque payload.
        let rodata_id = obj.add_section(
            Vec::new(),
            b".rodata".to_vec(),
            object::SectionKind::ReadOnlyData,
        );
        obj.append_section_data(rodata_id, &[0xCA; 512], 1);
        // .bss — kept by keep-list; already SHT_NOBITS on any real
        // kernel build. object::write emits it without backing bytes
        // when `kind = UninitializedData`, matching real-vmlinux layout.
        let bss_id = obj.add_section(
            Vec::new(),
            b".bss".to_vec(),
            object::SectionKind::UninitializedData,
        );
        obj.append_section_bss(bss_id, 256, 8);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_bss_symbol".to_vec(),
            value: 0x50,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(bss_id),
            flags: object::SymbolFlags::None,
        });
        // .BTF.ext — deleted by keep-list (no consumer).
        let btf_ext_id = obj.add_section(
            Vec::new(),
            b".BTF.ext".to_vec(),
            object::SectionKind::Metadata,
        );
        obj.append_section_data(btf_ext_id, &[0xE1; 128], 1);
        // .debug_info — always stripped.
        let debug_id = obj.add_section(
            Vec::new(),
            b".debug_info".to_vec(),
            object::SectionKind::Debug,
        );
        obj.append_section_data(debug_id, &[0xAA; 4096], 1);
        // .debug_str — always stripped.
        let debug_str_id = obj.add_section(
            Vec::new(),
            b".debug_str".to_vec(),
            object::SectionKind::Debug,
        );
        obj.append_section_data(debug_str_id, &[0xBB; 2048], 1);
        // .data — bytes dropped via SHT_NOBITS; symbol must survive.
        let data_id = obj.add_section(Vec::new(), b".data".to_vec(), object::SectionKind::Data);
        obj.append_section_data(data_id, &[0xDD; 512], 8);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_data_symbol".to_vec(),
            value: 0x20,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(data_id),
            flags: object::SymbolFlags::None,
        });
        // .data..percpu — bytes dropped via SHT_NOBITS; symbol must survive.
        let percpu_id = obj.add_section(
            Vec::new(),
            b".data..percpu".to_vec(),
            object::SectionKind::Data,
        );
        obj.append_section_data(percpu_id, &[0xCC; 256], 8);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_percpu_symbol".to_vec(),
            value: 0x30,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(percpu_id),
            flags: object::SymbolFlags::None,
        });
        // .init.data — bytes dropped via SHT_NOBITS; symbol must survive.
        let initdata_id = obj.add_section(
            Vec::new(),
            b".init.data".to_vec(),
            object::SectionKind::Data,
        );
        obj.append_section_data(initdata_id, &[0x11; 1024], 8);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_initdata_symbol".to_vec(),
            value: 0x40,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(initdata_id),
            flags: object::SymbolFlags::None,
        });

        let data = obj.write().unwrap();
        let path = dir.join("vmlinux");
        fs::write(&path, &data).unwrap();
        path
    }

    #[test]
    fn strip_vmlinux_debug_applies_keep_list() {
        let src = TempDir::new().unwrap();
        let vmlinux = create_strip_test_fixture(src.path());
        let original_size = fs::metadata(&vmlinux).unwrap().len();

        // Positive control: the fixture must actually carry the
        // sections this test asserts on. If object::write silently
        // renames or drops one, the post-strip absence assertions
        // would false-pass.
        let source_data = fs::read(&vmlinux).unwrap();
        let source_elf = goblin::elf::Elf::parse(&source_data).unwrap();
        let source_section_names: Vec<&str> = source_elf
            .section_headers
            .iter()
            .filter_map(|s| source_elf.shdr_strtab.get_at(s.sh_name))
            .collect();
        // Positive control covers every section the post-strip
        // assertions inspect — kept, dropped, or deleted. A future
        // fixture regression that silently omits any of these would
        // make the corresponding post-strip check vacuous.
        for name in [
            ".debug_info",
            ".debug_str",
            ".BTF.ext",
            ".BTF",
            ".rodata",
            ".bss",
            ".symtab",
            ".strtab",
        ] {
            assert!(
                source_section_names.contains(&name),
                "fixture missing expected section {name}"
            );
        }

        let stripped = strip_vmlinux_debug(&vmlinux).unwrap();
        let stripped_path = stripped.path();
        let stripped_size = fs::metadata(stripped_path).unwrap().len();

        assert!(
            stripped_size < original_size,
            "stripped ({stripped_size}) should be smaller than original ({original_size})"
        );

        let data = fs::read(stripped_path).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();
        let section_names: Vec<&str> = elf
            .section_headers
            .iter()
            .filter_map(|s| elf.shdr_strtab.get_at(s.sh_name))
            .collect();
        // Debug sections removed.
        assert!(
            !section_names.contains(&".debug_info"),
            "should not contain .debug_info"
        );
        assert!(
            !section_names.contains(&".debug_str"),
            "should not contain .debug_str"
        );
        // .BTF.ext removed (no consumer).
        assert!(
            !section_names.contains(&".BTF.ext"),
            "should not contain .BTF.ext"
        );
        // Keep-list sections preserved (names from all three consumer
        // modules plus structural).
        for name in [".BTF", ".rodata", ".bss", ".symtab", ".strtab"] {
            assert!(section_names.contains(&name), "should preserve {name}");
        }
    }

    #[test]
    fn strip_vmlinux_debug_symtab_readable() {
        let src = TempDir::new().unwrap();
        let vmlinux = create_strip_test_fixture(src.path());

        let stripped = strip_vmlinux_debug(&vmlinux).unwrap();
        let stripped_path = stripped.path();
        let data = fs::read(stripped_path).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();

        // Smoke check: stripping a synthetic ELF produces a readable
        // symbol table whose strtab still contains our test symbol
        // names. End-to-end symbol preservation on real vmlinuxes is
        // covered by the *_preserves_monitor_symbols tests below.
        assert!(
            has_symbol(&elf, "test_text_symbol"),
            "stripped ELF should contain test_text_symbol in symtab"
        );
        // test_bss_symbol anchors .bss against Builder::delete_orphans.
        // Queryable via has_symbol because its fixture st_value is
        // nonzero (an in-bounds offset within .bss).
        assert!(
            has_symbol(&elf, "test_bss_symbol"),
            "stripped ELF should contain test_bss_symbol in symtab"
        );
    }

    /// Data sections matched by [`is_zero_data_section`] and code
    /// sections must come out as SHT_NOBITS with sh_size == 0, and
    /// symbols pointing at them must survive. Runs on the synthetic
    /// fixture so it exercises the keep-list path in CI environments
    /// without a real vmlinux.
    #[test]
    fn strip_vmlinux_debug_zeros_data_sections() {
        let src = TempDir::new().unwrap();
        let vmlinux = create_strip_test_fixture(src.path());

        // Pre-strip positive control: every zero-data section must
        // start with a non-SHT_NOBITS type AND non-zero sh_size. If
        // the fixture ever emits them as empty or already-SHT_NOBITS,
        // the post-strip assertions below become tautological and
        // would pass even if the strip pipeline regressed.
        use goblin::elf::section_header::SHT_NOBITS;
        let source_data = fs::read(&vmlinux).unwrap();
        let source_elf = goblin::elf::Elf::parse(&source_data).unwrap();
        for name_bytes in crate::monitor::symbols::VMLINUX_ZERO_DATA_SECTIONS
            .iter()
            .chain(SPECULATIVE_ZERO_DATA_SECTIONS.iter())
        {
            let name = std::str::from_utf8(name_bytes).unwrap();
            let sh = source_elf
                .section_headers
                .iter()
                .find(|s| source_elf.shdr_strtab.get_at(s.sh_name) == Some(name))
                .unwrap_or_else(|| panic!("fixture missing expected {name}"));
            assert_ne!(
                sh.sh_type,
                SHT_NOBITS,
                "fixture {name} must start non-SHT_NOBITS so the strip is observable; got sh_type={} ({})",
                sh.sh_type,
                sh_type_name(sh.sh_type),
            );
            assert!(
                sh.sh_size > 0,
                "fixture {name} must start with nonzero sh_size"
            );
        }

        let stripped = strip_vmlinux_debug(&vmlinux).unwrap();
        let stripped_path = stripped.path();
        let data = fs::read(stripped_path).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();

        let find_section = |name: &str| {
            elf.section_headers
                .iter()
                .find(|s| elf.shdr_strtab.get_at(s.sh_name) == Some(name))
                .unwrap_or_else(|| panic!("section {name} missing from stripped ELF"))
        };
        let assert_nobits_empty = |name: &str| {
            let sh = find_section(name);
            let sh_type = sh.sh_type;
            let sh_size = sh.sh_size;
            assert_eq!(
                sh_type,
                SHT_NOBITS,
                "section {name} should be SHT_NOBITS after strip, got sh_type={sh_type} ({})",
                sh_type_name(sh_type),
            );
            assert_eq!(
                sh_size, 0,
                "section {name} should have sh_size == 0 after strip, got {sh_size}",
            );
        };

        // Iterate both the consumer-declared zero-data sections and
        // the speculative retention set so the test stays in sync
        // automatically when either source changes.
        for name_bytes in crate::monitor::symbols::VMLINUX_ZERO_DATA_SECTIONS
            .iter()
            .chain(SPECULATIVE_ZERO_DATA_SECTIONS.iter())
        {
            let name = std::str::from_utf8(name_bytes).unwrap();
            assert_nobits_empty(name);
        }

        // Code sections (`.text` in the fixture) receive the same
        // SHT_NOBITS treatment so function symbols survive
        // `Builder::delete_orphans`.
        assert_nobits_empty(".text");

        // Symbols pointing at the zeroed data sections must survive.
        // Fixture symbol values are nonzero (0x20/0x30/0x40, within
        // their section bounds), so has_symbol's st_value != 0 filter
        // matches them.
        assert!(
            has_symbol(&elf, "test_data_symbol"),
            "test_data_symbol dropped by strip"
        );
        assert!(
            has_symbol(&elf, "test_percpu_symbol"),
            "test_percpu_symbol dropped by strip"
        );
        assert!(
            has_symbol(&elf, "test_initdata_symbol"),
            "test_initdata_symbol dropped by strip"
        );
    }

    /// `strip_debug_prefix` is the fallback path `strip_vmlinux_debug`
    /// hits when the keep-list strip errors out. Exercise it
    /// directly on the synthetic fixture so the success path has
    /// coverage independent of the keep-list branch.
    #[test]
    fn strip_debug_prefix_removes_debug_and_preserves_rest() {
        let src = TempDir::new().unwrap();
        let vmlinux = create_strip_test_fixture(src.path());
        let raw = fs::read(&vmlinux).unwrap();
        let processed = neutralize_relocs(&raw).unwrap();

        let stripped = strip_debug_prefix(&processed).unwrap();
        let elf = goblin::elf::Elf::parse(&stripped).unwrap();
        let names: Vec<&str> = elf
            .section_headers
            .iter()
            .filter_map(|s| elf.shdr_strtab.get_at(s.sh_name))
            .collect();

        // .debug_* sections deleted. The fallback also removes the
        // `.comment` section, but this fixture does not emit one, so
        // that branch of the delete set is not exercised here.
        assert!(
            !names.contains(&".debug_info"),
            "fallback should remove .debug_info"
        );
        assert!(
            !names.contains(&".debug_str"),
            "fallback should remove .debug_str"
        );
        // Every other section the fixture carries survives — unlike
        // the keep-list path, the fallback does not partition by
        // consumer. In particular `.BTF.ext` (which keep-list would
        // delete) remains.
        for name in [".BTF", ".BTF.ext", ".text", ".data", ".rodata", ".symtab"] {
            assert!(
                names.contains(&name),
                "fallback must preserve {name}, got sections {names:?}"
            );
        }
    }

    /// `strip_debug_prefix`'s delete filter matches on six predicates:
    /// `name.starts_with(b".debug_")`, `name == b".comment"`,
    /// `name.starts_with(b".rela.")`, `name.starts_with(b".rel.")`,
    /// `name.starts_with(b".relr.")`, and `name.starts_with(b".crel.")`.
    /// The `.debug_*` branch is exercised by
    /// `strip_debug_prefix_removes_debug_and_preserves_rest` against
    /// the shared fixture; the four reloc-name prefix arms are
    /// exercised by `strip_debug_prefix_removes_reloc_prefix_sections`.
    /// This test covers the `.comment` branch against a focused
    /// fixture that specifically emits one — the shared fixture
    /// deliberately does not, to keep the keep-list assertions
    /// scoped.
    #[test]
    fn strip_debug_prefix_removes_dot_comment() {
        use object::write;
        // Minimal ELF: one loadable .text (fallback must preserve it)
        // plus a .comment section (fallback must delete it). A symbol
        // anchors .text so the `object` writer emits .symtab/.strtab.
        let mut obj = write::Object::new(
            object::BinaryFormat::Elf,
            object::Architecture::X86_64,
            object::Endianness::Little,
        );
        let text_id = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
        obj.append_section_data(text_id, &[0xCC; 64], 1);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_text_symbol".to_vec(),
            value: 0x0,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(text_id),
            flags: object::SymbolFlags::None,
        });
        // `.comment` is ELF's standard toolchain-producer string table
        // (`object::SectionKind::OtherString`).
        // Real kernel builds carry one stamped by GCC/Clang.
        let comment_id = obj.add_section(
            Vec::new(),
            b".comment".to_vec(),
            object::SectionKind::OtherString,
        );
        obj.append_section_data(comment_id, b"GCC: (GNU) 14.2.1 20250207\0", 1);
        let data = obj.write().unwrap();

        // Positive control: the fixture must actually carry `.comment`
        // and `.text` before strip. If `object::write` silently dropped
        // either (e.g. renaming, or treating OtherString non-standardly),
        // the post-strip absence assertion on `.comment` would
        // false-pass. Mirrors the positive-control pattern in
        // `strip_vmlinux_debug_applies_keep_list`.
        let source_elf = goblin::elf::Elf::parse(&data).unwrap();
        let source_names: Vec<&str> = source_elf
            .section_headers
            .iter()
            .filter_map(|s| source_elf.shdr_strtab.get_at(s.sh_name))
            .collect();
        for name in [".comment", ".text"] {
            assert!(
                source_names.contains(&name),
                "fixture missing expected section {name}; got {source_names:?}"
            );
        }

        // `neutralize_relocs` is a no-op on this fixture (no
        // SHF_ALLOC relocation sections) — run it anyway so the test
        // exercises the exact input pipeline `strip_vmlinux_debug` uses.
        let processed = neutralize_relocs(&data).unwrap();
        let stripped = strip_debug_prefix(&processed).unwrap();
        let elf = goblin::elf::Elf::parse(&stripped).unwrap();
        let names: Vec<&str> = elf
            .section_headers
            .iter()
            .filter_map(|s| elf.shdr_strtab.get_at(s.sh_name))
            .collect();

        assert!(
            !names.contains(&".comment"),
            "fallback must remove .comment, got sections {names:?}"
        );
        // Non-comment, non-debug sections survive untouched — guards
        // against an overly broad filter that accidentally drops
        // unrelated sections.
        assert!(
            names.contains(&".text"),
            "fallback must preserve .text, got sections {names:?}"
        );
    }

    /// `strip_debug_prefix` deletes reloc-named sections via the
    /// `.rela.`, `.rel.`, `.relr.`, and `.crel.` prefix arms so the
    /// fallback output doesn't carry the zero-size ghost headers that
    /// `neutralize_relocs` left behind. Exercise each prefix on a
    /// focused fixture — a real kernel vmlinux might carry only a
    /// subset, so the synthetic shape pins every arm.
    #[test]
    fn strip_debug_prefix_removes_reloc_prefix_sections() {
        use object::elf::{SHT_REL, SHT_RELA, SHT_RELR};

        // Base ELF with .text + anchor symbol so `.symtab`/`.strtab`
        // are present.
        let mut obj = build_base_elf_with_text_symbol(object::Architecture::X86_64);
        // One section per reloc-prefix arm. `.crel.*` uses SHT_CREL
        // below. Each carries a nonzero payload so `neutralize_relocs`
        // has observable work to do before the fallback runs.
        let rela_id = obj.add_section(
            Vec::new(),
            b".rela.text".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(rela_id, &[0xA5; 24], 1);
        let rel_id = obj.add_section(
            Vec::new(),
            b".rel.data".to_vec(),
            object::SectionKind::Elf(SHT_REL),
        );
        obj.append_section_data(rel_id, &[0xC7; 16], 1);
        let relr_id = obj.add_section(
            Vec::new(),
            b".relr.dyn".to_vec(),
            object::SectionKind::Elf(SHT_RELR),
        );
        obj.append_section_data(relr_id, &[0xD3; 16], 1);
        let crel_id = obj.add_section(
            Vec::new(),
            b".crel.text".to_vec(),
            object::SectionKind::Elf(object::elf::SHT_CREL),
        );
        obj.append_section_data(crel_id, &[0xE4; 8], 1);
        let data = obj.write().unwrap();

        // Positive control: every reloc-named section must exist
        // pre-strip; a silent rename by `object::write` would false-pass
        // the post-strip absence assertions below.
        let source_elf = goblin::elf::Elf::parse(&data).unwrap();
        let source_names: Vec<&str> = source_elf
            .section_headers
            .iter()
            .filter_map(|s| source_elf.shdr_strtab.get_at(s.sh_name))
            .collect();
        for name in [
            ".rela.text",
            ".rel.data",
            ".relr.dyn",
            ".crel.text",
            ".text",
        ] {
            assert!(
                source_names.contains(&name),
                "fixture missing expected section {name}; got {source_names:?}"
            );
        }

        let processed = neutralize_relocs(&data).unwrap();
        let stripped = strip_debug_prefix(&processed).unwrap();
        let elf = goblin::elf::Elf::parse(&stripped).unwrap();
        let names: Vec<&str> = elf
            .section_headers
            .iter()
            .filter_map(|s| elf.shdr_strtab.get_at(s.sh_name))
            .collect();

        // All four reloc-prefix arms deleted.
        for name in [".rela.text", ".rel.data", ".relr.dyn", ".crel.text"] {
            assert!(
                !names.contains(&name),
                "fallback must delete {name} (prefix arm), got sections {names:?}"
            );
        }
        // Non-reloc section survives — guards against an overly broad
        // filter that would drop e.g. `.text` on an unrelated name prefix.
        assert!(
            names.contains(&".text"),
            "fallback must preserve .text, got sections {names:?}"
        );
    }

    /// `neutralize_relocs` rewrites two section-header fields on every
    /// section whose `sh_type` is `SHT_REL`, `SHT_RELA`, `SHT_RELR`, or
    /// `SHT_CREL`, regardless of the `SHF_ALLOC` flag: `sh_type`
    /// becomes `SHT_PROGBITS` and `sh_size` becomes 0. Pin the
    /// observable invariants against a focused fixture:
    ///
    /// 1a. SHF_ALLOC + SHT_RELA section has sh_type rewritten to
    ///     SHT_PROGBITS and sh_size zeroed post-call.
    /// 1b. SHF_ALLOC + SHT_REL section has sh_type rewritten to
    ///     SHT_PROGBITS and sh_size zeroed post-call.
    /// 1c. Non-ALLOC SHT_RELA section has sh_type rewritten to
    ///     SHT_PROGBITS and sh_size zeroed post-call (the SHF_ALLOC
    ///     gate was dropped — aarch64 kernels emit non-alloc rela
    ///     sections whose byte ranges trip
    ///     `object::build::elf::Builder::read`).
    /// 1d. SHT_RELR section has sh_type rewritten to SHT_PROGBITS and
    ///     sh_size zeroed post-call (defense-in-depth for arm64
    ///     kernels with `CONFIG_PIE` + `CONFIG_RELR` that emit
    ///     `.relr.dyn`).
    /// 2. Non-RELA section (e.g. `.text`) has sh_type and sh_size
    ///    preserved (guards against an accidentally-broader filter).
    ///
    /// Also pins content preservation: `neutralize_relocs` only
    /// mutates the section HEADER's `sh_type` and `sh_size`, not the
    /// section's data bytes. Raw bytes at the original sh_offset must
    /// remain bit-identical post-call.
    #[test]
    fn neutralize_relocs_zeros_sh_size_of_every_reloc_section() {
        use object::elf::{SHF_ALLOC, SHT_REL, SHT_RELA, SHT_RELR};

        // Base ELF with .text + anchor symbol (so object::write
        // emits .symtab/.strtab). Reloc sections are added below.
        let mut obj = build_base_elf_with_text_symbol(object::Architecture::X86_64);
        // .rela.kaslr — SHT_RELA + SHF_ALLOC. Shape matches what
        // CONFIG_RELOCATABLE + CONFIG_RANDOMIZE_BASE kernels emit.
        let kaslr_id = obj.add_section(
            Vec::new(),
            b".rela.kaslr".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(kaslr_id, &[0xA5; 32], 1);
        obj.section_mut(kaslr_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };
        // .rel.foo — SHT_REL + SHF_ALLOC. The fn's match arm accepts
        // both SHT_REL and SHT_RELA; exercising only SHT_RELA would
        // let a regression that dropped SHT_REL ride unnoticed.
        let rel_id = obj.add_section(
            Vec::new(),
            b".rel.foo".to_vec(),
            object::SectionKind::Elf(SHT_REL),
        );
        obj.append_section_data(rel_id, &[0xC7; 24], 1);
        obj.section_mut(rel_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };
        // .rela.debug_info — SHT_RELA WITHOUT SHF_ALLOC. After the
        // SHF_ALLOC gate was dropped, this must also be zeroed — a
        // regression that re-added the gate would preserve sh_size
        // here and fail the new invariant 1c.
        let rdbg_id = obj.add_section(
            Vec::new(),
            b".rela.debug_info".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(rdbg_id, &[0xB6; 16], 1);
        // .relr.dyn — SHT_RELR + SHF_ALLOC. Defense-in-depth for
        // arm64 kernels that emit packed relative relocations.
        let relr_id = obj.add_section(
            Vec::new(),
            b".relr.dyn".to_vec(),
            object::SectionKind::Elf(SHT_RELR),
        );
        obj.append_section_data(relr_id, &[0xD3; 24], 1);
        obj.section_mut(relr_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };

        let data = obj.write().unwrap();

        // Positive-control the fixture: the five sections we assert on
        // must actually exist in the produced ELF with the expected
        // sh_type/sh_flags/sh_size. If `object::write` renamed or
        // reshaped one, the post-call assertions would false-pass.
        let pre_elf = goblin::elf::Elf::parse(&data).unwrap();
        let mut pre_kaslr = None;
        let mut pre_rel = None;
        let mut pre_rdbg = None;
        let mut pre_relr = None;
        let mut pre_text = None;
        for sh in pre_elf.section_headers.iter() {
            let name = pre_elf.shdr_strtab.get_at(sh.sh_name).unwrap_or("");
            match name {
                ".rela.kaslr" => pre_kaslr = Some(sh.clone()),
                ".rel.foo" => pre_rel = Some(sh.clone()),
                ".rela.debug_info" => pre_rdbg = Some(sh.clone()),
                ".relr.dyn" => pre_relr = Some(sh.clone()),
                ".text" => pre_text = Some(sh.clone()),
                _ => {}
            }
        }
        let pre_kaslr = pre_kaslr.expect("fixture must carry .rela.kaslr");
        let pre_rel = pre_rel.expect("fixture must carry .rel.foo");
        let pre_rdbg = pre_rdbg.expect("fixture must carry .rela.debug_info");
        let pre_relr = pre_relr.expect("fixture must carry .relr.dyn");
        let pre_text = pre_text.expect("fixture must carry .text");
        assert_eq!(
            pre_kaslr.sh_type,
            SHT_RELA,
            ".rela.kaslr sh_type must be SHT_RELA; got sh_type={} ({})",
            pre_kaslr.sh_type,
            sh_type_name(pre_kaslr.sh_type),
        );
        assert!(
            pre_kaslr.sh_flags & u64::from(SHF_ALLOC) != 0,
            ".rela.kaslr must carry SHF_ALLOC; got sh_flags={:#x}",
            pre_kaslr.sh_flags
        );
        assert_eq!(
            pre_kaslr.sh_size, 32,
            ".rela.kaslr sh_size must match 32-byte payload"
        );
        assert_eq!(
            pre_rel.sh_type,
            SHT_REL,
            ".rel.foo sh_type must be SHT_REL; got sh_type={} ({})",
            pre_rel.sh_type,
            sh_type_name(pre_rel.sh_type),
        );
        assert!(
            pre_rel.sh_flags & u64::from(SHF_ALLOC) != 0,
            ".rel.foo must carry SHF_ALLOC; got sh_flags={:#x}",
            pre_rel.sh_flags
        );
        assert_eq!(
            pre_rel.sh_size, 24,
            ".rel.foo sh_size must match 24-byte payload"
        );
        assert_eq!(
            pre_rdbg.sh_type,
            SHT_RELA,
            ".rela.debug_info sh_type must be SHT_RELA; got sh_type={} ({})",
            pre_rdbg.sh_type,
            sh_type_name(pre_rdbg.sh_type),
        );
        assert_eq!(
            pre_rdbg.sh_flags & u64::from(SHF_ALLOC),
            0,
            ".rela.debug_info must NOT carry SHF_ALLOC; got sh_flags={:#x}",
            pre_rdbg.sh_flags
        );
        assert_eq!(
            pre_rdbg.sh_size, 16,
            ".rela.debug_info sh_size must match 16-byte payload"
        );
        assert_eq!(
            pre_relr.sh_type,
            SHT_RELR,
            ".relr.dyn sh_type must be SHT_RELR (19); got sh_type={} ({})",
            pre_relr.sh_type,
            sh_type_name(pre_relr.sh_type),
        );
        assert_eq!(
            pre_relr.sh_size, 24,
            ".relr.dyn sh_size must match 24-byte payload"
        );
        assert_eq!(
            pre_text.sh_size, 64,
            ".text sh_size must match 64-byte payload"
        );

        // Snapshot the .rela.kaslr data bytes before the call so we
        // can assert they survive the sh_size rewrite.
        let kaslr_offset = pre_kaslr.sh_offset as usize;
        let kaslr_size = pre_kaslr.sh_size as usize;
        let kaslr_original_data = data[kaslr_offset..kaslr_offset + kaslr_size].to_vec();

        let processed = neutralize_relocs(&data).unwrap();
        assert_eq!(
            processed.len(),
            data.len(),
            "neutralize_relocs must not resize the ELF; only sh_size header fields are rewritten"
        );

        let post_elf = goblin::elf::Elf::parse(&processed).unwrap();
        let mut post_kaslr = None;
        let mut post_rel = None;
        let mut post_rdbg = None;
        let mut post_relr = None;
        let mut post_text = None;
        for sh in post_elf.section_headers.iter() {
            let name = post_elf.shdr_strtab.get_at(sh.sh_name).unwrap_or("");
            match name {
                ".rela.kaslr" => post_kaslr = Some(sh.clone()),
                ".rel.foo" => post_rel = Some(sh.clone()),
                ".rela.debug_info" => post_rdbg = Some(sh.clone()),
                ".relr.dyn" => post_relr = Some(sh.clone()),
                ".text" => post_text = Some(sh.clone()),
                _ => {}
            }
        }
        let post_kaslr = post_kaslr.expect(".rela.kaslr must survive");
        let post_rel = post_rel.expect(".rel.foo must survive");
        let post_rdbg = post_rdbg.expect(".rela.debug_info must survive");
        let post_relr = post_relr.expect(".relr.dyn must survive");
        let post_text = post_text.expect(".text must survive");

        // Invariant 1a: SHF_ALLOC + SHT_RELA section has sh_size zeroed.
        assert_eq!(
            post_kaslr.sh_size, 0,
            ".rela.kaslr sh_size must be zeroed; got {}",
            post_kaslr.sh_size
        );
        // Invariant 1b: SHF_ALLOC + SHT_REL section has sh_size zeroed
        // (the SHT_REL arm of the filter).
        assert_eq!(
            post_rel.sh_size, 0,
            ".rel.foo sh_size must be zeroed; got {}",
            post_rel.sh_size
        );
        // Invariant 1c: Non-ALLOC SHT_RELA section ALSO zeroed (the
        // SHF_ALLOC gate was dropped so aarch64 non-alloc rela
        // sections get neutralized).
        assert_eq!(
            post_rdbg.sh_size, 0,
            ".rela.debug_info sh_size must be zeroed (SHF_ALLOC gate dropped); got {}",
            post_rdbg.sh_size
        );
        // Invariant 1d: SHT_RELR section zeroed (defense-in-depth
        // for arm64 CONFIG_RELR kernels).
        assert_eq!(
            post_relr.sh_size, 0,
            ".relr.dyn sh_size must be zeroed (SHT_RELR match arm); got {}",
            post_relr.sh_size
        );
        // Invariant 2: Non-RELA section preserved.
        assert_eq!(
            post_text.sh_size, pre_text.sh_size,
            ".text sh_size must be preserved (not a relocation section)"
        );

        // Content preservation: the raw bytes at the section's
        // sh_offset must be bit-identical to pre-call. Only the
        // sh_size header field was rewritten.
        assert_eq!(
            &processed[kaslr_offset..kaslr_offset + kaslr_size],
            &kaslr_original_data[..],
            ".rela.kaslr data bytes must be preserved; neutralize only rewrites sh_size"
        );

        // sh_offset and sh_flags are preserved; sh_type is rewritten
        // to SHT_PROGBITS so the Builder reads the section via the
        // opaque-data arm instead of the rel/rela parse arms that
        // break on zero-length slices with align != 1.
        assert_eq!(
            post_kaslr.sh_offset, pre_kaslr.sh_offset,
            "sh_offset must be preserved"
        );
        assert_eq!(
            post_kaslr.sh_type,
            object::elf::SHT_PROGBITS,
            "sh_type must be rewritten to SHT_PROGBITS; got sh_type={} ({})",
            post_kaslr.sh_type,
            sh_type_name(post_kaslr.sh_type),
        );
        assert_eq!(
            post_kaslr.sh_flags, pre_kaslr.sh_flags,
            "sh_flags must be preserved"
        );
        // The sibling reloc sections should also be re-typed to
        // SHT_PROGBITS (the fn applies sh_type rewrite to every
        // matching section, not just the first).
        assert_eq!(
            post_rel.sh_type,
            object::elf::SHT_PROGBITS,
            ".rel.foo sh_type must be SHT_PROGBITS"
        );
        assert_eq!(
            post_rdbg.sh_type,
            object::elf::SHT_PROGBITS,
            ".rela.debug_info sh_type must be SHT_PROGBITS"
        );
        assert_eq!(
            post_relr.sh_type,
            object::elf::SHT_PROGBITS,
            ".relr.dyn sh_type must be SHT_PROGBITS"
        );
    }

    /// For ELFs that carry no relocation sections at all,
    /// `neutralize_relocs` returns an unchanged copy —
    /// documented as the "no-op" branch in the fn docstring.
    #[test]
    fn neutralize_relocs_noop_when_no_reloc_sections() {
        // Base ELF carries only .text + anchor symbol — no reloc
        // sections at all, so the filter matches nothing.
        let data = build_base_elf_with_text_symbol(object::Architecture::X86_64)
            .write()
            .unwrap();

        let processed = neutralize_relocs(&data).unwrap();
        assert_eq!(
            processed, data,
            "neutralize_relocs must be a byte-identity no-op when no reloc sections are present"
        );
    }

    /// `neutralize_relocs` must be byte-identity idempotent:
    /// `f(f(x)) == f(x)`. The production filter inside
    /// [`neutralize_relocs`] keys on `sh_type` — which IS rewritten
    /// (to `SHT_PROGBITS`) on matching sections. Idempotence still
    /// holds because after the first pass the neutralized sections
    /// no longer match the `is_reloc` predicate (sh_type is now
    /// `SHT_PROGBITS`, not one of `SHT_REL`/`SHT_RELA`/`SHT_RELR`/
    /// `SHT_CREL`), so the second pass walks every section without
    /// touching any header field and the output is byte-identical to
    /// the first-pass output.
    ///
    /// Guards against a future mutation that rewrites sh_type to a
    /// still-matched value (e.g. flipping `SHT_REL` to `SHT_RELA` —
    /// both match `is_reloc`, which would make the second pass
    /// re-neutralize to yet another sh_type value and break
    /// idempotence).
    ///
    /// Uses the same multi-section fixture as
    /// `neutralize_relocs_zeros_sh_size_of_every_reloc_section`
    /// so every reloc-type arm of the filter (SHT_RELA with and
    /// without SHF_ALLOC, SHT_REL) and the non-RELA negative control
    /// re-walk on the second pass.
    #[test]
    fn neutralize_relocs_is_idempotent() {
        use object::elf::{SHF_ALLOC, SHT_REL, SHT_RELA};

        // Base .text + anchor symbol; the reloc sections added below
        // intentionally mirror the sibling zeros-every-reloc test's
        // fixture so the filter re-walks on the second pass.
        let mut obj = build_base_elf_with_text_symbol(object::Architecture::X86_64);
        // .rela.kaslr — SHT_RELA + SHF_ALLOC.
        let kaslr_id = obj.add_section(
            Vec::new(),
            b".rela.kaslr".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(kaslr_id, &[0xA5; 32], 1);
        obj.section_mut(kaslr_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };
        // .rel.foo — SHT_REL + SHF_ALLOC. Exercises the SHT_REL arm
        // of the filter so a regression that special-cased only
        // SHT_RELA on re-entry would surface here.
        let rel_id = obj.add_section(
            Vec::new(),
            b".rel.foo".to_vec(),
            object::SectionKind::Elf(SHT_REL),
        );
        obj.append_section_data(rel_id, &[0xC7; 24], 1);
        obj.section_mut(rel_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };
        // .rela.debug_info — SHT_RELA without SHF_ALLOC. After the
        // SHF_ALLOC gate was dropped, this gets neutralized too —
        // but must re-neutralize to byte-identical bytes on the
        // second pass.
        let rdbg_id = obj.add_section(
            Vec::new(),
            b".rela.debug_info".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(rdbg_id, &[0xB6; 16], 1);
        // flags left as SectionFlags::None — no SHF_ALLOC.

        let data = obj.write().unwrap();

        let first_pass = neutralize_relocs(&data).unwrap();
        let second_pass = neutralize_relocs(&first_pass).unwrap();

        // Non-vacuous guard: the first call must actually modify bytes
        // on this fixture (which carries reloc sections); a degenerate
        // no-op implementation of `neutralize_relocs` would
        // trivially satisfy idempotence and must not pass.
        assert_ne!(
            first_pass, data,
            "first call must modify bytes on a fixture with reloc sections; \
             if this fails, neutralize_relocs is a no-op"
        );

        // Primary idempotence assertion: byte equality between passes.
        assert_eq!(
            second_pass, first_pass,
            "neutralize_relocs must be idempotent: a second pass over its own output produces byte-identical bytes"
        );

        // Length preservation across both passes — the function only
        // rewrites in-place `sh_size` fields, never resizes the buffer.
        assert_eq!(
            first_pass.len(),
            data.len(),
            "first pass must preserve ELF length"
        );
        assert_eq!(
            second_pass.len(),
            first_pass.len(),
            "second pass must preserve ELF length"
        );

        // Re-parse post-second-pass: the ELF header and section
        // header table must still be well-formed after two rewrites.
        let post_elf = goblin::elf::Elf::parse(&second_pass)
            .expect("second-pass output must remain parseable as ELF");

        let mut post_kaslr = None;
        let mut post_rel = None;
        let mut post_rdbg = None;
        for sh in post_elf.section_headers.iter() {
            let name = post_elf.shdr_strtab.get_at(sh.sh_name).unwrap_or("");
            match name {
                ".rela.kaslr" => post_kaslr = Some(sh.clone()),
                ".rel.foo" => post_rel = Some(sh.clone()),
                ".rela.debug_info" => post_rdbg = Some(sh.clone()),
                _ => {}
            }
        }
        let post_kaslr = post_kaslr.expect(".rela.kaslr must survive second pass");
        let post_rel = post_rel.expect(".rel.foo must survive second pass");
        let post_rdbg = post_rdbg.expect(".rela.debug_info must survive second pass");

        // All reloc sections stay zeroed on the second pass,
        // regardless of SHF_ALLOC.
        assert_eq!(
            post_kaslr.sh_size, 0,
            ".rela.kaslr sh_size must remain zero after the second pass"
        );
        assert_eq!(
            post_rel.sh_size, 0,
            ".rel.foo sh_size must remain zero after the second pass"
        );
        assert_eq!(
            post_rdbg.sh_size, 0,
            ".rela.debug_info sh_size must remain zero after the second pass (SHF_ALLOC gate dropped)"
        );

        // SHF_ALLOC flag must still be set on the ALLOC sections —
        // the function touches sh_type and sh_size, never sh_flags.
        // The non-ALLOC `.rela.debug_info` likewise retains its
        // (cleared) SHF_ALLOC bit.
        assert!(
            post_kaslr.sh_flags & u64::from(SHF_ALLOC) != 0,
            ".rela.kaslr SHF_ALLOC flag must survive both passes; got sh_flags={:#x}",
            post_kaslr.sh_flags
        );
        assert!(
            post_rel.sh_flags & u64::from(SHF_ALLOC) != 0,
            ".rel.foo SHF_ALLOC flag must survive both passes; got sh_flags={:#x}",
            post_rel.sh_flags
        );
        assert_eq!(
            post_rdbg.sh_flags & u64::from(SHF_ALLOC),
            0,
            ".rela.debug_info must retain its (cleared) SHF_ALLOC flag across both passes; got sh_flags={:#x}",
            post_rdbg.sh_flags
        );
    }

    /// `neutralize_relocs` fails loudly when fed bytes that do
    /// not parse as an ELF — the goblin parse returns Err and the
    /// function wraps it in an `anyhow::anyhow!("parse vmlinux ELF
    /// for preprocess: {e}")`. Pin only the stable "parse vmlinux ELF
    /// for preprocess" wrapper in `neutralize_relocs`; the
    /// goblin-side error text is version-dependent and not part of
    /// the contract.
    ///
    /// Exercises two distinct goblin failure paths through the same
    /// anyhow wrapper:
    ///
    /// 1. Bad magic: "not an ELF..." passes the 16-byte length gate but
    ///    its first four bytes do not match `\x7fELF`, so goblin's
    ///    `TryFromCtx` for `Header` bails with `Error::BadMagic` before
    ///    inspecting any later field (see goblin 0.10 `elf/header.rs`
    ///    `try_from_ctx` at the `ident[0..SELFMAG] != ELFMAG` branch).
    /// 2. Invalid EI_CLASS: a 16-byte prefix with the correct magic but
    ///    `ident[EI_CLASS] == 0` (ELFCLASSNONE) passes BOTH the length
    ///    and magic gates, and fails on the subsequent class-dispatch
    ///    match with `Error::Malformed("invalid ELF class 0")`. This is
    ///    the "passes magic, fails deeper" path.
    ///
    /// Either failure mode flows through the same `anyhow::anyhow!`
    /// wrapper, so the test pins the wrapper string for each input
    /// without pinning the goblin-side sub-error wording.
    #[test]
    fn neutralize_relocs_rejects_invalid_elf() {
        // Table-driven so a future goblin upgrade that changes either
        // sub-error's wording still surfaces both paths distinctly.
        let cases: &[(&str, &[u8])] = &[
            ("bad magic", b"not an ELF at all, just some bytes"),
            (
                "magic ok but invalid EI_CLASS",
                &[
                    0x7f, b'E', b'L', b'F', // magic
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ident[4..16]: class=0, rest 0
                ],
            ),
        ];
        for (label, input) in cases {
            let err = neutralize_relocs(input).unwrap_err();
            let rendered = format!("{err:#}");
            assert!(
                rendered.contains("parse vmlinux ELF for preprocess"),
                "[{label}] expected error context to name the ELF parse step; got: {rendered}"
            );
        }
    }

    /// ELF32 counterpart of
    /// [`neutralize_relocs_zeros_sh_size_of_every_reloc_section`].
    ///
    /// `neutralize_relocs` dispatches on `elf.is_64` at the
    /// `sh_size` offset/width pair — 32-byte offset + 8-byte field for
    /// ELF64, 20-byte offset + 4-byte field for ELF32 (per the
    /// ELF32/ELF64 section header layouts documented at the call site).
    /// The existing fixture-driven coverage is all ELF64 (Architecture::
    /// X86_64), so a regression that swapped the `if elf.is_64` branches
    /// or hardcoded the 64-bit offsets would silently corrupt 32-bit
    /// inputs without tripping any assertion.
    ///
    /// Uses `Architecture::I386` which `object` maps to
    /// `address_size == U32` and therefore emits ELFCLASS32 via the
    /// writer's is_64=false path. goblin then parses the output with
    /// `is_64 == false`, driving `neutralize_relocs` through the
    /// else `(20, 4)` branch.
    ///
    /// Exercises BOTH arms of the
    /// `sh.sh_type == SHT_RELA || sh.sh_type == SHT_REL` filter
    /// predicate on the ELF32 code path. A regression that special-
    /// cased SHT_REL only on ELF64 (e.g. wired it to a 64-bit offset
    /// table), or dropped SHT_REL from the ELF32 filter altogether,
    /// would leave one section un-zeroed here.
    ///
    /// Invariants pinned: SHF_ALLOC + SHT_RELA AND SHF_ALLOC + SHT_REL
    /// both have their sh_size zeroed, the output remains parseable
    /// as ELF32 (is_64 stays false), and the buffer length is
    /// preserved (the fn only rewrites an in-place header field,
    /// never resizes).
    #[test]
    fn neutralize_relocs_zeros_sh_size_in_elf32_fixture() {
        use object::elf::{SHF_ALLOC, SHT_REL, SHT_RELA};

        // Base shape — .text + test_text_symbol with ELF32-sized
        // anchor — is shared with the ELF64 fixtures via the
        // helper. Passing `Architecture::I386` flips the ELF class
        // (is_64 false) and downgrades the symbol size to 4 bytes.
        let mut obj = build_base_elf_with_text_symbol(object::Architecture::I386);
        // .rela.kaslr — SHT_RELA + SHF_ALLOC. A 16-byte payload is
        // large enough that a mis-targeted 4-byte write at offset 20
        // (the correct ELF32 sh_size location) is observable via
        // goblin's post-parse sh_size read.
        let kaslr_id = obj.add_section(
            Vec::new(),
            b".rela.kaslr".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(kaslr_id, &[0xA5; 16], 1);
        obj.section_mut(kaslr_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };
        // .rel.foo — SHT_REL + SHF_ALLOC. Exercises the `SHT_REL` arm
        // of the `is_reloc` match on the ELF32 code path. A regression
        // that dropped SHT_REL from the filter on the 32-bit path
        // would leave this section's sh_size unchanged and trip the
        // post-call assertion below.
        let rel_id = obj.add_section(
            Vec::new(),
            b".rel.foo".to_vec(),
            object::SectionKind::Elf(SHT_REL),
        );
        obj.append_section_data(rel_id, &[0xC7; 12], 1);
        obj.section_mut(rel_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };

        let data = obj.write().unwrap();

        // Positive-control the fixture: the output must actually be
        // ELF32 (is_64 == false) — otherwise this test would false-pass
        // through the ELF64 code path the sibling test already covers.
        // A future object-crate change that remapped I386 to ELF64
        // would surface here rather than silently duplicating existing
        // coverage.
        let pre_elf = goblin::elf::Elf::parse(&data).unwrap();
        assert!(
            !pre_elf.is_64,
            "fixture must produce ELF32 (is_64 == false) to exercise the (20, 4) branch"
        );
        let pre_kaslr = pre_elf
            .section_headers
            .iter()
            .find(|sh| pre_elf.shdr_strtab.get_at(sh.sh_name) == Some(".rela.kaslr"))
            .expect("fixture must carry .rela.kaslr")
            .clone();
        let pre_rel = pre_elf
            .section_headers
            .iter()
            .find(|sh| pre_elf.shdr_strtab.get_at(sh.sh_name) == Some(".rel.foo"))
            .expect("fixture must carry .rel.foo")
            .clone();
        assert_eq!(
            pre_kaslr.sh_type,
            SHT_RELA,
            ".rela.kaslr sh_type must be SHT_RELA; got sh_type={} ({})",
            pre_kaslr.sh_type,
            sh_type_name(pre_kaslr.sh_type),
        );
        assert!(
            pre_kaslr.sh_flags & u64::from(SHF_ALLOC) != 0,
            ".rela.kaslr must carry SHF_ALLOC; got sh_flags={:#x}",
            pre_kaslr.sh_flags
        );
        assert_eq!(
            pre_kaslr.sh_size, 16,
            ".rela.kaslr sh_size must match 16-byte payload pre-call"
        );
        assert_eq!(
            pre_rel.sh_type,
            SHT_REL,
            ".rel.foo sh_type must be SHT_REL; got sh_type={} ({})",
            pre_rel.sh_type,
            sh_type_name(pre_rel.sh_type),
        );
        assert!(
            pre_rel.sh_flags & u64::from(SHF_ALLOC) != 0,
            ".rel.foo must carry SHF_ALLOC; got sh_flags={:#x}",
            pre_rel.sh_flags
        );
        assert_eq!(
            pre_rel.sh_size, 12,
            ".rel.foo sh_size must match 12-byte payload pre-call"
        );

        let processed = neutralize_relocs(&data).unwrap();
        assert_eq!(
            processed.len(),
            data.len(),
            "neutralize_relocs must not resize the ELF32 buffer"
        );

        let post_elf = goblin::elf::Elf::parse(&processed).unwrap();
        assert!(
            !post_elf.is_64,
            "post-call parse must still be ELF32; the fn must not alter the e_ident class byte"
        );
        let post_kaslr = post_elf
            .section_headers
            .iter()
            .find(|sh| post_elf.shdr_strtab.get_at(sh.sh_name) == Some(".rela.kaslr"))
            .expect(".rela.kaslr must survive the neutralize pass")
            .clone();
        let post_rel = post_elf
            .section_headers
            .iter()
            .find(|sh| post_elf.shdr_strtab.get_at(sh.sh_name) == Some(".rel.foo"))
            .expect(".rel.foo must survive the neutralize pass")
            .clone();
        // Primary invariants: sh_size is zeroed in the ELF32 4-byte
        // slot at offset 20 within both section header entries — one
        // per arm of the SHT_RELA || SHT_REL filter predicate.
        assert_eq!(
            post_kaslr.sh_size, 0,
            "ELF32 .rela.kaslr sh_size must be zeroed (SHT_RELA arm); got {}",
            post_kaslr.sh_size
        );
        assert_eq!(
            post_rel.sh_size, 0,
            "ELF32 .rel.foo sh_size must be zeroed (SHT_REL arm); got {}",
            post_rel.sh_size
        );
    }

    /// ELF32 counterpart of
    /// [`neutralize_relocs_noop_when_no_reloc_sections`].
    ///
    /// When the input carries no reloc sections, the ELF32 code path
    /// in `neutralize_relocs` must return a byte-identity copy
    /// of the input — same invariant as ELF64, but exercised through
    /// the `(20, 4)` offset/width branch. A regression that filled
    /// zeros even on the "no match" path, or mis-read the section
    /// header count / size on 32-bit inputs, would break byte-identity
    /// here without tripping the ELF64 sibling test.
    #[test]
    fn neutralize_relocs_noop_when_no_reloc_sections_elf32() {
        use object::write;

        let mut obj = write::Object::new(
            object::BinaryFormat::Elf,
            object::Architecture::I386,
            object::Endianness::Little,
        );
        // .text + symbol mirror the sibling fixture so object::write
        // emits a valid ELF32 with the same structural sections but
        // zero reloc entries.
        let text_id = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
        obj.append_section_data(text_id, &[0xCC; 64], 1);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_text_symbol".to_vec(),
            value: 0x0,
            size: 4,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(text_id),
            flags: object::SymbolFlags::None,
        });
        let data = obj.write().unwrap();

        // Positive-control: the fixture must parse as ELF32
        // (is_64 == false) so the no-match path through the
        // `(20, 4)` branch is what gets exercised. A future object
        // change that remapped I386 to ELF64 would turn this into a
        // duplicate of the ELF64 sibling without visible failure.
        let pre_elf = goblin::elf::Elf::parse(&data).unwrap();
        assert!(
            !pre_elf.is_64,
            "fixture must produce ELF32 (is_64 == false) to exercise the (20, 4) branch",
        );

        let processed = neutralize_relocs(&data).unwrap();
        assert_eq!(
            processed, data,
            "neutralize_relocs must be byte-identity on ELF32 when no reloc sections are present",
        );
    }

    /// ELF32 counterpart of
    /// [`neutralize_relocs_is_idempotent`].
    ///
    /// Idempotence (`f(f(x)) == f(x)`) must hold through the ELF32
    /// `(20, 4)` branch of `neutralize_relocs`. The ELF64
    /// sibling covers the `(32, 8)` branch; pinning both prevents a
    /// future offset-width mismatch where e.g. the second pass on
    /// ELF32 reads sh_size through an ELF64 offset and silently
    /// tripped idempotence on 32-bit inputs.
    ///
    /// Uses the same SHT_RELA+ALLOC / SHT_REL+ALLOC / SHT_RELA-no-
    /// ALLOC section mix as the ELF32 zeros fixture so the SHT_REL
    /// and SHT_RELA arms of the `is_reloc` match re-walk on the
    /// second pass. A no-match section is present to rule out a
    /// degenerate "zero every sh_size" implementation.
    #[test]
    fn neutralize_relocs_is_idempotent_elf32() {
        use object::elf::{SHF_ALLOC, SHT_REL, SHT_RELA};
        use object::write;

        let mut obj = write::Object::new(
            object::BinaryFormat::Elf,
            object::Architecture::I386,
            object::Endianness::Little,
        );
        let text_id = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
        obj.append_section_data(text_id, &[0xCC; 64], 1);
        let _ = obj.add_symbol(write::Symbol {
            name: b"test_text_symbol".to_vec(),
            value: 0x0,
            size: 4,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(text_id),
            flags: object::SymbolFlags::None,
        });
        // .rela.kaslr — SHT_RELA + SHF_ALLOC.
        let kaslr_id = obj.add_section(
            Vec::new(),
            b".rela.kaslr".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(kaslr_id, &[0xA5; 16], 1);
        obj.section_mut(kaslr_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };
        // .rel.foo — SHT_REL + SHF_ALLOC. Second filter arm.
        let rel_id = obj.add_section(
            Vec::new(),
            b".rel.foo".to_vec(),
            object::SectionKind::Elf(SHT_REL),
        );
        obj.append_section_data(rel_id, &[0xC7; 12], 1);
        obj.section_mut(rel_id).flags = object::SectionFlags::Elf {
            sh_flags: u64::from(SHF_ALLOC),
        };
        // .rela.debug_info — SHT_RELA without SHF_ALLOC. The
        // SHF_ALLOC gate was dropped, so this also gets neutralized
        // on both passes — a regression that re-added the gate would
        // leave sh_size preserved here but still satisfy idempotence,
        // so the post-second-pass assertions below pin the neutralized
        // value directly.
        let rdbg_id = obj.add_section(
            Vec::new(),
            b".rela.debug_info".to_vec(),
            object::SectionKind::Elf(SHT_RELA),
        );
        obj.append_section_data(rdbg_id, &[0xB6; 8], 1);

        let data = obj.write().unwrap();

        // Positive-control ELF32: any post-parse assertion depends on
        // this; a silent promotion to ELF64 would make the idempotence
        // check run through the (32, 8) branch instead.
        assert!(
            !goblin::elf::Elf::parse(&data).unwrap().is_64,
            "fixture must be ELF32 to exercise the (20, 4) idempotence path",
        );

        let first_pass = neutralize_relocs(&data).unwrap();
        let second_pass = neutralize_relocs(&first_pass).unwrap();

        // Non-vacuous guard: first pass must actually rewrite bytes on
        // this fixture. Without this the test could false-pass on a
        // degenerate no-op implementation that trivially satisfies
        // idempotence.
        assert_ne!(
            first_pass, data,
            "first pass must rewrite sh_size on ELF32 reloc sections",
        );
        assert_eq!(
            second_pass, first_pass,
            "neutralize_relocs must be byte-identity idempotent on ELF32",
        );

        // Pin the post-second-pass sh_size values directly so a
        // regression that re-added the SHF_ALLOC gate (leaving
        // `.rela.debug_info` un-zeroed) surfaces even though
        // idempotence alone would still hold.
        let post_elf = goblin::elf::Elf::parse(&second_pass).unwrap();
        for name in [".rela.kaslr", ".rel.foo", ".rela.debug_info"] {
            let sh = post_elf
                .section_headers
                .iter()
                .find(|sh| post_elf.shdr_strtab.get_at(sh.sh_name) == Some(name))
                .unwrap_or_else(|| panic!("{name} must survive second pass"));
            assert_eq!(
                sh.sh_size, 0,
                "ELF32 {name} sh_size must be zeroed after both passes (SHF_ALLOC gate dropped)"
            );
        }
    }

    #[test]
    fn strip_vmlinux_debug_nonexistent_file() {
        let result = strip_vmlinux_debug(Path::new("/nonexistent/vmlinux"));
        assert!(result.is_err());
    }

    #[test]
    fn strip_vmlinux_debug_non_elf_file() {
        let tmp = TempDir::new().unwrap();
        let path = tmp.path().join("vmlinux");
        fs::write(&path, b"not an ELF file").unwrap();
        let result = strip_vmlinux_debug(&path);
        assert!(result.is_err());
    }

    /// Build an ELF on disk matching `create_strip_test_fixture`'s
    /// shape (keep-list sections, code, debug, data sidecars, and a
    /// symtab anchor) plus one extra section provided by the caller.
    /// Returns the path.
    ///
    /// The helper is generic over the per-test extra section so each
    /// of the four end-to-end pipeline tests can focus on one failure
    /// mode (non-alloc SHT_RELA with invalid entries, non-alloc
    /// SHT_RELA with sh_size past EOF, SHT_RELR with sh_size past
    /// EOF) while sharing the rest of the fixture shape.
    ///
    /// The `mutate_header` closure receives a goblin-parsed view of
    /// the produced ELF plus a mutable byte buffer and can rewrite
    /// the section header of the extra section in-place. Tests use it
    /// to push `sh_size` or `sh_offset` past the file end — a direct
    /// rewrite is safer than trying to coax `object::write` into
    /// emitting malformed headers.
    fn build_reloc_fixture(
        dir: &Path,
        extra_section_name: &[u8],
        extra_section_sh_type: u32,
        extra_section_data: &[u8],
        mutate_header: impl FnOnce(&mut [u8]),
    ) -> PathBuf {
        use object::write;

        let mut obj = write::Object::new(
            object::BinaryFormat::Elf,
            object::Architecture::X86_64,
            object::Endianness::Little,
        );
        // .text anchors the symtab.
        let text_id = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
        obj.append_section_data(text_id, &[0xCC; 64], 16);
        let _ = obj.add_symbol(write::Symbol {
            name: b"pipeline_anchor".to_vec(),
            value: 0x10,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(text_id),
            flags: object::SymbolFlags::None,
        });
        // .BTF — kept by probe BTF keep-list.
        let btf_id = obj.add_section(Vec::new(), b".BTF".to_vec(), object::SectionKind::Other);
        obj.append_section_data(btf_id, &[0x42; 128], 1);
        // .rodata — kept by monitor CONFIG_IKCONFIG keep-list.
        let rodata_id = obj.add_section(
            Vec::new(),
            b".rodata".to_vec(),
            object::SectionKind::ReadOnlyData,
        );
        obj.append_section_data(rodata_id, &[0xAA; 256], 1);
        // Extra caller-provided section (a reloc section in the tests
        // below). Flags left as SectionFlags::None so it is non-alloc
        // — exercising the SHF_ALLOC-gate-drop path.
        let extra_id = obj.add_section(
            Vec::new(),
            extra_section_name.to_vec(),
            object::SectionKind::Elf(extra_section_sh_type),
        );
        obj.append_section_data(extra_id, extra_section_data, 1);

        let mut bytes = obj.write().unwrap();
        mutate_header(&mut bytes);
        let path = dir.join("vmlinux");
        fs::write(&path, &bytes).unwrap();
        path
    }

    /// Assert a successful [`strip_vmlinux_debug`] run on the fixture
    /// preserves the keep-list sections and deletes the extra
    /// (reloc-name) section via `strip_keep_list`'s name-based policy.
    ///
    /// This is the shared oracle for the four end-to-end pipeline
    /// tests: every variant that `strip_vmlinux_debug` accepts must
    /// yield the same output shape — keep-list sections present,
    /// reloc-name section absent.
    fn assert_stripped_preserves_keep_list_and_deletes(stripped: &Path, reloc_name: &str) {
        let data = fs::read(stripped).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();
        let names: Vec<&str> = elf
            .section_headers
            .iter()
            .filter_map(|s| elf.shdr_strtab.get_at(s.sh_name))
            .collect();
        for name in [".symtab", ".strtab", ".BTF", ".rodata"] {
            assert!(
                names.contains(&name),
                "keep-list section {name} must survive strip_vmlinux_debug; got {names:?}"
            );
        }
        assert!(
            !names.contains(&reloc_name),
            "reloc section {reloc_name} must be deleted by strip_vmlinux_debug; got {names:?}"
        );
    }

    /// Pipeline pin #1: strip_vmlinux_debug handles a non-ALLOC
    /// `SHT_RELA` section with VALID byte range but entries whose
    /// `r_info` symbol indices are garbage (`0xA5A5...`).
    ///
    /// Before the SHF_ALLOC gate was dropped from
    /// [`neutralize_relocs`], non-ALLOC reloc sections were skipped,
    /// and `object::build::elf::Builder::read` then called
    /// `section.rela()` → `data_as_array` → `read_relocations_impl`
    /// on the raw bytes. With `sh_link == 0` (no linked symbol
    /// table) the impl uses `dynamic_symbols.len() == 0` for the
    /// bounds check; any non-null symbol index fails with
    /// `"Invalid symbol index N in relocation section at index M"`
    /// and `strip_vmlinux_debug` bubbled the error up. This test
    /// FAILS on that pre-fix codepath and PASSES after neutralize
    /// rewrites `sh_type` to `SHT_PROGBITS` on every `SHT_REL`/
    /// `SHT_RELA` section regardless of `SHF_ALLOC`.
    #[test]
    fn strip_vmlinux_debug_handles_nonalloc_rela_with_invalid_entries() {
        let src = TempDir::new().unwrap();
        let vmlinux = build_reloc_fixture(
            src.path(),
            b".rela.invalid",
            object::elf::SHT_RELA,
            // 24 bytes = one Elf64_Rela entry. 0xA5 bytes give
            // r_info = 0xA5A5A5A5A5A5A5A5 — a non-null, out-of-range
            // symbol index that `read_relocations_impl`'s bounds
            // check rejects when sh_link=0 directs the parse to the
            // empty dynamic symbol table.
            &[0xA5; 24],
            |_| {},
        );
        let stripped = strip_vmlinux_debug(&vmlinux).unwrap();
        assert_stripped_preserves_keep_list_and_deletes(stripped.path(), ".rela.invalid");
    }

    /// Pipeline pin #2: strip_vmlinux_debug handles a non-ALLOC
    /// `SHT_RELA` section whose `sh_size` is not a multiple of the
    /// `Elf64_Rela` entry size (24 bytes) — a shape that passes
    /// goblin's section-bounds check but fails object-crate's
    /// `data_as_array` divisibility check with `"Invalid ELF
    /// relocation section offset or size"`.
    ///
    /// This is the realistic arm64 kernel 7.0 failure mode: the
    /// section's byte range fits inside the file (so goblin accepts
    /// it) but doesn't represent a well-formed stream of `Elf64_Rela`
    /// entries from `object::build::elf::Builder::read`'s
    /// perspective.
    ///
    /// Before the fix, `Builder::read` failed at
    /// `slice_from_all_bytes` (non-exact multiple of entry size ⇒
    /// tail bytes remaining ⇒ Err). After the fix,
    /// [`neutralize_relocs`] rewrites `sh_type` to `SHT_PROGBITS` on
    /// every reloc section before `Builder::read` sees it; the sh_type
    /// mismatch short-circuits `section.rel()`/`section.rela()` at
    /// the type-check line (object-0.37.3/src/read/elf/section.rs:829,
    /// 849) and `data_as_array` is never called.
    #[test]
    fn strip_vmlinux_debug_handles_nonalloc_rela_with_non_entsize_sh_size() {
        let src = TempDir::new().unwrap();
        let vmlinux = build_reloc_fixture(
            src.path(),
            b".rela.odd",
            object::elf::SHT_RELA,
            // 24 bytes = one valid Elf64_Rela. We'll rewrite sh_size
            // to 17 below — fits inside the file's byte range (so
            // goblin accepts it) but 17 % 24 != 0 so object-crate's
            // `slice_from_all_bytes::<Rela64>` rejects the size.
            &[0x11; 24],
            |bytes| {
                let elf = goblin::elf::Elf::parse(bytes).unwrap();
                let shoff = elf.header.e_shoff as usize;
                let shentsize = elf.header.e_shentsize as usize;
                let idx = elf
                    .section_headers
                    .iter()
                    .position(|sh| elf.shdr_strtab.get_at(sh.sh_name) == Some(".rela.odd"))
                    .expect("fixture must carry .rela.odd");
                drop(elf);
                let sh_size_off = shoff + idx * shentsize + 32;
                // sh_size = 17 bytes, not divisible by 24
                // (sizeof(Elf64_Rela)). In-bounds (section payload is
                // 24 bytes) so goblin accepts, but the Builder's
                // `slice_from_all_bytes` check leaves a 17-byte tail
                // that rejects — matching the arm64 kernel 7.0
                // failure mode.
                let bad_size: u64 = 17;
                bytes[sh_size_off..sh_size_off + 8].copy_from_slice(&bad_size.to_le_bytes());
            },
        );
        let stripped = strip_vmlinux_debug(&vmlinux).unwrap();
        assert_stripped_preserves_keep_list_and_deletes(stripped.path(), ".rela.odd");
    }

    /// Pipeline pin #3: strip_vmlinux_debug handles `SHT_RELR`
    /// sections — arm64 kernels with `CONFIG_PIE` + `CONFIG_RELR`
    /// emit `.relr.dyn` with packed relative-relocation entries.
    ///
    /// This test locks in the SHT_RELR match arm in
    /// [`neutralize_relocs`] by checking BOTH invariants:
    ///
    /// 1. **Neutralize reaches SHT_RELR**: after the first pass,
    ///    the `.relr.dyn` section's `sh_type` is `SHT_PROGBITS`
    ///    (not its original `SHT_RELR`) and `sh_size` is 0. A
    ///    regression that drops SHT_RELR from the match arm leaves
    ///    the section with `sh_type = SHT_RELR` (19) — this
    ///    assertion fires.
    ///
    /// 2. **End-to-end strip succeeds**: `strip_vmlinux_debug` runs
    ///    cleanly and the output has `.relr.dyn` removed by
    ///    keep-list policy.
    ///
    /// Even on a well-formed `.relr.dyn` payload (Builder::read
    /// handles SHT_RELR opaquely via `section.data()` with no
    /// alignment check, so a "happy-path" SHT_RELR might pass
    /// `strip_vmlinux_debug` even without neutralization), the
    /// invariant-1 check locks in the neutralize reach to guard
    /// against a future regression that silently stops rewriting
    /// SHT_RELR sections.
    #[test]
    fn strip_vmlinux_debug_handles_relr_section() {
        let src = TempDir::new().unwrap();
        let vmlinux = build_reloc_fixture(
            src.path(),
            b".relr.dyn",
            object::elf::SHT_RELR,
            // 16 bytes = two packed RELR entries (each u64).
            &[0x77; 16],
            |_| {},
        );

        // Invariant 1: `neutralize_relocs` must rewrite the .relr.dyn
        // section's sh_type to SHT_PROGBITS and zero its sh_size.
        // Checking the function output directly locks in the
        // SHT_RELR match arm — a regression that drops SHT_RELR
        // from the arm would leave sh_type == SHT_RELR here.
        let raw = fs::read(&vmlinux).unwrap();
        let neutralized = neutralize_relocs(&raw).unwrap();
        let neutralized_elf = goblin::elf::Elf::parse(&neutralized).unwrap();
        let relr_sh = neutralized_elf
            .section_headers
            .iter()
            .find(|sh| neutralized_elf.shdr_strtab.get_at(sh.sh_name) == Some(".relr.dyn"))
            .expect(".relr.dyn must survive neutralize");
        assert_eq!(
            relr_sh.sh_type,
            object::elf::SHT_PROGBITS,
            ".relr.dyn sh_type must be rewritten to SHT_PROGBITS (SHT_RELR arm of the match); got sh_type={}",
            relr_sh.sh_type,
        );
        assert_eq!(
            relr_sh.sh_size, 0,
            ".relr.dyn sh_size must be zeroed post-neutralize",
        );

        // Invariant 2: end-to-end strip succeeds and removes the
        // reloc section.
        let stripped = strip_vmlinux_debug(&vmlinux).unwrap();
        assert_stripped_preserves_keep_list_and_deletes(stripped.path(), ".relr.dyn");
    }

    /// Pipeline pin #4: after strip_vmlinux_debug succeeds on an ELF
    /// carrying BOTH a non-alloc `SHT_RELA` and a `SHT_RELR` section,
    /// the output has every keep-list section (`.symtab`, `.strtab`,
    /// `.BTF`, `.rodata`) present and every reloc-named section
    /// deleted.
    ///
    /// Guards against a regression where the fix skipped one or the
    /// other reloc type (e.g. a future refactor that splits
    /// [`neutralize_relocs`]'s match arm and drops SHT_RELR). The
    /// pipeline pins above each cover one reloc type in isolation;
    /// this combined fixture ensures the fix holds when both types
    /// appear in the same kernel image.
    #[test]
    fn strip_vmlinux_debug_deletes_reloc_sections_and_preserves_keep_list() {
        use object::write;

        let src = TempDir::new().unwrap();
        let mut obj = write::Object::new(
            object::BinaryFormat::Elf,
            object::Architecture::X86_64,
            object::Endianness::Little,
        );
        // .text anchors the symtab.
        let text_id = obj.add_section(Vec::new(), b".text".to_vec(), object::SectionKind::Text);
        obj.append_section_data(text_id, &[0xCC; 64], 16);
        let _ = obj.add_symbol(write::Symbol {
            name: b"pipeline_anchor".to_vec(),
            value: 0x10,
            size: 8,
            kind: object::SymbolKind::Data,
            scope: object::SymbolScope::Compilation,
            weak: false,
            section: write::SymbolSection::Section(text_id),
            flags: object::SymbolFlags::None,
        });
        let btf_id = obj.add_section(Vec::new(), b".BTF".to_vec(), object::SectionKind::Other);
        obj.append_section_data(btf_id, &[0x42; 128], 1);
        let rodata_id = obj.add_section(
            Vec::new(),
            b".rodata".to_vec(),
            object::SectionKind::ReadOnlyData,
        );
        obj.append_section_data(rodata_id, &[0xAA; 256], 1);
        // Two reloc sections: .rela.dbg (non-alloc SHT_RELA with
        // garbage entries) and .relr.dyn (SHT_RELR). Both must be
        // deleted from the output and neither must break the strip.
        let rela_id = obj.add_section(
            Vec::new(),
            b".rela.dbg".to_vec(),
            object::SectionKind::Elf(object::elf::SHT_RELA),
        );
        obj.append_section_data(rela_id, &[0xA5; 24], 1);
        let relr_id = obj.add_section(
            Vec::new(),
            b".relr.dyn".to_vec(),
            object::SectionKind::Elf(object::elf::SHT_RELR),
        );
        obj.append_section_data(relr_id, &[0xD3; 24], 1);

        let bytes = obj.write().unwrap();
        let vmlinux = src.path().join("vmlinux");
        fs::write(&vmlinux, &bytes).unwrap();

        // Positive control: fixture must carry all sections the
        // post-strip assertion inspects. A silent rename by
        // object::write would false-pass the absence checks.
        let source_elf = goblin::elf::Elf::parse(&bytes).unwrap();
        let source_names: Vec<&str> = source_elf
            .section_headers
            .iter()
            .filter_map(|s| source_elf.shdr_strtab.get_at(s.sh_name))
            .collect();
        for name in [
            ".text",
            ".BTF",
            ".rodata",
            ".rela.dbg",
            ".relr.dyn",
            ".symtab",
            ".strtab",
        ] {
            assert!(
                source_names.contains(&name),
                "fixture missing expected section {name}; got {source_names:?}"
            );
        }

        let stripped = strip_vmlinux_debug(&vmlinux).unwrap();
        let data = fs::read(stripped.path()).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();
        let names: Vec<&str> = elf
            .section_headers
            .iter()
            .filter_map(|s| elf.shdr_strtab.get_at(s.sh_name))
            .collect();

        // Keep-list sections survive.
        for name in [".symtab", ".strtab", ".BTF", ".rodata"] {
            assert!(
                names.contains(&name),
                "keep-list section {name} must survive strip; got {names:?}"
            );
        }
        // Both reloc sections deleted.
        for name in [".rela.dbg", ".relr.dyn"] {
            assert!(
                !names.contains(&name),
                "reloc section {name} must be deleted by strip; got {names:?}"
            );
        }
    }

    #[test]
    fn strip_vmlinux_debug_preserves_monitor_symbols() {
        let Some(path) = crate::monitor::find_test_vmlinux() else {
            skip!("no vmlinux found; {}", crate::KTSTR_KERNEL_HINT);
        };
        // find_test_vmlinux may return /sys/kernel/btf/vmlinux (raw BTF,
        // not an ELF), which strip_vmlinux_debug cannot parse.
        if path.starts_with("/sys/") {
            skip!("vmlinux is raw BTF (not ELF), cannot strip debug");
        }
        let stripped = strip_vmlinux_debug(&path).unwrap();
        let stripped_path = stripped.path();
        let syms = crate::monitor::symbols::KernelSymbols::from_vmlinux(stripped_path).unwrap();
        // `runqueues` and `per_cpu_offset` are required non-Option
        // fields on KernelSymbols; `from_vmlinux` bails via
        // `Context::context` if either symbol is absent or zero
        // (`sym_addr` filters `st_value != 0`). Reaching the unwrap
        // above therefore guarantees both are nonzero. These asserts
        // are defensive against a future regression that loosens the
        // sym_addr filter or adds a non-error-on-missing path.
        assert_ne!(
            syms.runqueues, 0,
            "runqueues symbol missing from stripped vmlinux"
        );
        assert_ne!(
            syms.per_cpu_offset, 0,
            "__per_cpu_offset symbol missing from stripped vmlinux"
        );
        // For every optional symbol KernelSymbols tracks: presence must
        // survive the strip. A symbol that is absent from the source
        // vmlinux stays absent (kernel-config-dependent); a symbol that
        // is present must still be present.
        let source_syms = crate::monitor::symbols::KernelSymbols::from_vmlinux(&path).unwrap();
        assert_eq!(
            source_syms.init_top_pgt.is_some(),
            syms.init_top_pgt.is_some(),
            "strip changed KernelSymbols init_top_pgt presence"
        );
        assert_eq!(
            source_syms.page_offset_base_kva.is_some(),
            syms.page_offset_base_kva.is_some(),
            "strip changed page_offset_base_kva presence"
        );
        assert_eq!(
            source_syms.scx_root.is_some(),
            syms.scx_root.is_some(),
            "strip changed scx_root presence"
        );
        assert_eq!(
            source_syms.pgtable_l5_enabled.is_some(),
            syms.pgtable_l5_enabled.is_some(),
            "strip changed pgtable_l5_enabled presence"
        );
        assert_eq!(
            source_syms.prog_idr.is_some(),
            syms.prog_idr.is_some(),
            "strip changed prog_idr presence"
        );
        assert_eq!(
            source_syms.scx_watchdog_timeout.is_some(),
            syms.scx_watchdog_timeout.is_some(),
            "strip changed scx_watchdog_timeout presence"
        );

        // KernelSymbols.init_top_pgt collapses init_top_pgt OR
        // swapper_pg_dir via or_else. Check both names directly against
        // the raw symbol table so a regression that keeps one while
        // dropping the other is caught.
        let source_data = fs::read(&path).unwrap();
        let source_elf = goblin::elf::Elf::parse(&source_data).unwrap();
        let stripped_data = fs::read(stripped_path).unwrap();
        let stripped_elf = goblin::elf::Elf::parse(&stripped_data).unwrap();
        assert_eq!(
            has_symbol(&source_elf, "init_top_pgt"),
            has_symbol(&stripped_elf, "init_top_pgt"),
            "strip changed raw-symtab init_top_pgt presence"
        );
        assert_eq!(
            has_symbol(&source_elf, "swapper_pg_dir"),
            has_symbol(&stripped_elf, "swapper_pg_dir"),
            "strip changed raw-symtab swapper_pg_dir presence"
        );
    }

    /// Guards against a regression where `strip_vmlinux_debug` returns
    /// `Ok` but produces output close to the source size — e.g. if
    /// `.debug_*` removal is silently skipped.
    ///
    /// Skipped when the source vmlinux carries no `.debug_info`,
    /// which is the signature of an already-stripped input: ktstr's
    /// own cache path caches pre-stripped vmlinuxes, and CI that
    /// points this test at a cache-produced vmlinux would see the
    /// DWARF sections already gone. Running strip over an
    /// already-stripped ELF produces output the same size as the
    /// input (the keep-list partition is idempotent once DWARF is
    /// gone), so the `<` inequality no longer observes the strip.
    /// Rebuild the source-tree vmlinux to exercise this test.
    #[test]
    fn strip_vmlinux_debug_shrinks_when_source_has_debug_info() {
        let Some(path) = crate::monitor::find_test_vmlinux() else {
            skip!("no vmlinux found; {}", crate::KTSTR_KERNEL_HINT);
        };
        if path.starts_with("/sys/") {
            skip!("vmlinux is raw BTF (not ELF), cannot strip debug");
        }
        let source_data = fs::read(&path).unwrap();
        let source_elf = goblin::elf::Elf::parse(&source_data).unwrap();
        let source_has_debug = source_elf
            .section_headers
            .iter()
            .any(|sh| source_elf.shdr_strtab.get_at(sh.sh_name) == Some(".debug_info"));
        if !source_has_debug {
            skip!(
                "source vmlinux has no .debug_info — already stripped \
                 (cached copy or distro-stripped); rebuild source tree \
                 to exercise the size-shrink path"
            );
        }

        let stripped = strip_vmlinux_debug(&path).unwrap();
        let source_size = fs::metadata(&path).unwrap().len();
        let stripped_size = fs::metadata(stripped.path()).unwrap().len();
        assert!(
            stripped_size < source_size,
            "stripped vmlinux ({stripped_size} bytes) should be smaller than \
             source ({source_size} bytes)"
        );
    }

    #[test]
    fn strip_vmlinux_debug_preserves_bpf_idr_symbols() {
        let Some(path) = crate::monitor::find_test_vmlinux() else {
            skip!("no vmlinux found; {}", crate::KTSTR_KERNEL_HINT);
        };
        if path.starts_with("/sys/") {
            skip!("vmlinux is raw BTF (not ELF), cannot strip debug");
        }
        let stripped = strip_vmlinux_debug(&path).unwrap();
        let stripped_path = stripped.path();
        let data = fs::read(stripped_path).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();
        assert!(
            has_symbol(&elf, "map_idr"),
            "map_idr symbol missing from stripped vmlinux"
        );
        assert!(
            has_symbol(&elf, "prog_idr"),
            "prog_idr symbol missing from stripped vmlinux"
        );
    }

    /// Function symbols (in `.text` and friends) must survive the
    /// strip so `resolve_addrs_from_elf` can resolve event addresses
    /// from the cached vmlinux. The strip preserves code-section
    /// headers as `SHT_NOBITS` to keep these symbols from being
    /// dropped by `Builder::delete_orphans`.
    #[test]
    fn strip_vmlinux_debug_preserves_function_symbols() {
        let Some(path) = crate::monitor::find_test_vmlinux() else {
            skip!("no vmlinux found; {}", crate::KTSTR_KERNEL_HINT);
        };
        if path.starts_with("/sys/") {
            skip!("vmlinux is raw BTF (not ELF), cannot strip debug");
        }
        // Skip if the source vmlinux has no `schedule` symbol -- that
        // means it was already stripped by an older build of ktstr
        // and no longer carries .text symbols. The test exercises
        // strip-preserves behavior, not whether a particular cache
        // entry was rebuilt.
        let source_data = fs::read(&path).unwrap();
        let source_elf = goblin::elf::Elf::parse(&source_data).unwrap();
        if !has_symbol(&source_elf, "schedule") {
            skip!(
                "source vmlinux has no `schedule` symbol \
                 (already stripped by older ktstr) -- rebuild the kernel \
                 cache to exercise this test"
            );
        }

        let stripped = strip_vmlinux_debug(&path).unwrap();
        let stripped_path = stripped.path();
        let data = fs::read(stripped_path).unwrap();
        let elf = goblin::elf::Elf::parse(&data).unwrap();
        assert!(
            has_symbol(&elf, "schedule"),
            "schedule function symbol dropped by strip"
        );
    }

    // -- KconfigStatus Display impl --
    //
    // Pins the three Display strings that flow through `kernel list
    // --json` as the `kconfig_status` field. CI scripts consume these
    // exact strings, so any rewording is a downstream-visible
    // contract change.

    #[test]
    fn kconfig_status_display_matches_renders_lowercase_word() {
        assert_eq!(KconfigStatus::Matches.to_string(), "matches");
    }

    #[test]
    fn kconfig_status_display_stale_renders_lowercase_word_without_hashes() {
        let s = KconfigStatus::Stale {
            cached: "deadbeef".to_string(),
            current: "cafebabe".to_string(),
        }
        .to_string();
        assert_eq!(
            s, "stale",
            "Display elides the cached/current hashes; callers that need them must match on the variant directly"
        );
    }

    #[test]
    fn kconfig_status_display_untracked_renders_lowercase_word() {
        assert_eq!(KconfigStatus::Untracked.to_string(), "untracked");
    }

    // ------------------------------------------------------------
    // Cache-entry coordination lock tests
    // ------------------------------------------------------------

    /// `acquire_shared_lock` on a fresh cache root creates the
    /// lockfile at `{root}/.locks/{key}.lock` (and the parent
    /// `.locks/` subdirectory) — guards against drift to the old
    /// sibling layout.
    #[test]
    fn acquire_shared_lock_creates_lockfile_at_expected_path() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let _guard = cache.acquire_shared_lock("some-key-123").unwrap();
        assert!(
            tmp.path().join(".locks").is_dir(),
            "parent .locks/ subdirectory must materialize on first acquire",
        );
        assert!(
            tmp.path().join(".locks").join("some-key-123.lock").exists(),
            "lockfile must materialize at {{cache_root}}/.locks/{{key}}.lock on first acquire",
        );
    }

    /// Two concurrent `acquire_shared_lock` calls on the same key
    /// both succeed — LOCK_SH coexists. Uses separate threads so
    /// each gets its own open-file-description (flock is per-OFD).
    #[test]
    fn acquire_shared_lock_permits_concurrent_readers() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};
        let tmp = TempDir::new().unwrap();
        let cache = Arc::new(CacheDir::with_root(tmp.path().to_path_buf()));
        let key = "concurrent-sh";
        let success = Arc::new(AtomicUsize::new(0));
        let mut handles = Vec::new();
        for _ in 0..4 {
            let cache = Arc::clone(&cache);
            let success = Arc::clone(&success);
            handles.push(std::thread::spawn(move || {
                let _g = cache
                    .acquire_shared_lock(key)
                    .expect("LOCK_SH must succeed");
                success.fetch_add(1, Ordering::SeqCst);
                // Hold briefly so all threads concurrently hold the
                // lock. Without this sleep, threads could serialize
                // through a narrow no-contention window and pass
                // even if the lock mistakenly rejected coexistence.
                std::thread::sleep(std::time::Duration::from_millis(50));
            }));
        }
        for h in handles {
            h.join().expect("reader thread panicked");
        }
        assert_eq!(
            success.load(Ordering::SeqCst),
            4,
            "all 4 concurrent LOCK_SH acquires must succeed",
        );
    }

    /// `try_acquire_exclusive_lock` fails with an error naming the
    /// lockfile when a concurrent reader holds LOCK_SH. A
    /// spawned thread takes LOCK_SH and sleeps; the main thread
    /// attempts `try_acquire_exclusive_lock` non-blocking and
    /// asserts the error path fires.
    #[test]
    fn try_acquire_exclusive_lock_fails_with_active_reader() {
        use std::sync::Arc;
        use std::sync::mpsc;
        let tmp = TempDir::new().unwrap();
        let cache = Arc::new(CacheDir::with_root(tmp.path().to_path_buf()));
        let key = "force-contended";
        let (ready_tx, ready_rx) = mpsc::channel();
        let (release_tx, release_rx) = mpsc::channel::<()>();
        let cache_reader = Arc::clone(&cache);
        let reader = std::thread::spawn(move || {
            let _g = cache_reader
                .acquire_shared_lock(key)
                .expect("reader LOCK_SH must succeed");
            ready_tx.send(()).unwrap();
            // Block until the main thread's non-blocking attempt
            // has had its chance to fail. Without this gate, the
            // reader could drop its lock before the main thread's
            // try_acquire_exclusive_lock ran, producing a
            // false-pass.
            release_rx.recv().unwrap();
        });
        ready_rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("reader thread did not signal ready in time");
        // Now the reader is holding LOCK_SH. A non-blocking LOCK_EX
        // must bail.
        let err = cache.try_acquire_exclusive_lock(key).unwrap_err();
        let msg = format!("{err:#}");
        assert!(
            msg.contains("is locked by active test runs") || msg.contains("holders:"),
            "error must surface the contention diagnostic; got: {msg}",
        );
        assert!(
            msg.contains("lockfile"),
            "error must name the lockfile path: {msg}",
        );
        // Release the reader so the test cleans up.
        release_tx.send(()).unwrap();
        reader.join().expect("reader thread panicked");
    }

    /// `acquire_exclusive_lock_blocking` times out with the
    /// documented wording when a concurrent reader holds LOCK_SH
    /// longer than the timeout allows. Uses a 200ms timeout + a
    /// reader that holds for >500ms to reliably trip the bail.
    #[test]
    fn acquire_exclusive_lock_blocking_times_out_on_contention() {
        use std::sync::Arc;
        use std::sync::mpsc;
        let tmp = TempDir::new().unwrap();
        let cache = Arc::new(CacheDir::with_root(tmp.path().to_path_buf()));
        let key = "blocking-timeout";
        let (ready_tx, ready_rx) = mpsc::channel();
        let (release_tx, release_rx) = mpsc::channel::<()>();
        let cache_reader = Arc::clone(&cache);
        let reader = std::thread::spawn(move || {
            let _g = cache_reader
                .acquire_shared_lock(key)
                .expect("reader LOCK_SH must succeed");
            ready_tx.send(()).unwrap();
            release_rx.recv().unwrap();
        });
        ready_rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("reader did not signal ready in time");
        let start = std::time::Instant::now();
        let err = cache
            .acquire_exclusive_lock_blocking(key, std::time::Duration::from_millis(200))
            .unwrap_err();
        let elapsed = start.elapsed();
        let msg = format!("{err:#}");
        assert!(
            msg.contains("timed out"),
            "error must mention the timeout: {msg}",
        );
        assert!(
            elapsed >= std::time::Duration::from_millis(150),
            "acquire should have waited ~timeout (150ms lower bound); \
             got {elapsed:?}",
        );
        release_tx.send(()).unwrap();
        reader.join().expect("reader thread panicked");
    }

    /// `store()` acquires its own exclusive lock and completes
    /// successfully when no readers contend. Regression pin for
    /// the internal `acquire_exclusive_lock_blocking` call.
    #[test]
    fn store_succeeds_under_internal_exclusive_lock() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().join("cache"));
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");
        let entry = cache
            .store("internal-lock", &CacheArtifacts::new(&image), &meta)
            .expect("store must succeed when no readers contend");
        assert!(entry.path.join("bzImage").exists());
        // Lockfile should exist in the .locks/ subdirectory
        // (acquired during store).
        assert!(
            tmp.path()
                .join("cache")
                .join(".locks")
                .join("internal-lock.lock")
                .exists(),
            "lockfile materialized during store must persist after \
             store returns (it's fine; the flock is released on fd \
             drop but the file stays as a reusable sentinel)",
        );
    }

    /// `store()` blocks while a reader holds LOCK_SH, then
    /// completes after the reader releases. Drives the path by
    /// spawning a reader that holds its lock while attempting
    /// store in a thread; probes that store() does NOT complete
    /// within 200ms, then releases the reader and asserts store()
    /// completes within 10s.
    #[test]
    fn store_blocks_while_reader_holds_shared_lock() {
        use std::sync::Arc;
        use std::sync::mpsc;
        let tmp = TempDir::new().unwrap();
        let cache = Arc::new(CacheDir::with_root(tmp.path().join("cache-block")));
        let key = "blocked-store";
        let (ready_tx, ready_rx) = mpsc::channel();
        let (release_tx, release_rx) = mpsc::channel::<()>();
        let cache_reader = Arc::clone(&cache);
        let reader = std::thread::spawn(move || {
            let _g = cache_reader
                .acquire_shared_lock(key)
                .expect("reader LOCK_SH must succeed");
            ready_tx.send(()).unwrap();
            release_rx.recv().unwrap();
        });
        ready_rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("reader did not signal ready in time");

        // Reader is holding LOCK_SH. A store attempt must block.
        // Spawn the store in a thread and check it hasn't
        // completed within a short window.
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());
        let meta = test_metadata("6.14.2");
        let (store_done_tx, store_done_rx) = mpsc::channel();
        let cache_store = Arc::clone(&cache);
        let image_clone = image.clone();
        let store_thread = std::thread::spawn(move || {
            let _ = cache_store.store(key, &CacheArtifacts::new(&image_clone), &meta);
            store_done_tx.send(()).unwrap();
        });
        // Short probe: store must NOT complete while reader holds lock.
        let early = store_done_rx.recv_timeout(std::time::Duration::from_millis(200));
        assert!(
            early.is_err(),
            "store() must block while reader holds LOCK_SH; got completion signal early",
        );
        // Release the reader — store should now unblock and finish.
        release_tx.send(()).unwrap();
        let finish = store_done_rx.recv_timeout(std::time::Duration::from_secs(10));
        assert!(
            finish.is_ok(),
            "store() must complete after reader releases; got timeout",
        );
        reader.join().expect("reader thread panicked");
        store_thread.join().expect("store thread panicked");
    }

    /// `lock_path` returns `{cache_root}/.locks/{key}.lock` — pins
    /// the exact on-disk shape against a refactor that relocates
    /// the lockfile. Pure path construction, no filesystem access.
    #[test]
    fn lock_path_returns_expected_shape() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let path = cache.lock_path("my-key-42");
        assert_eq!(path, tmp.path().join(".locks").join("my-key-42.lock"));
    }

    /// `.locks/` subdirectory PERSISTS after the lock guard drops.
    /// Kernel_clean and any other walker that relies on list()
    /// filtering dotfiles assumes `.locks/` outlives individual
    /// acquires. A regression that rm'd the directory on guard
    /// drop would cause next-acquire to re-`mkdir` on a different
    /// inode and invalidate any /proc/locks peer-holder lookup
    /// (the peer's inode would be stale).
    #[test]
    fn locks_subdir_persists_after_guard_drop() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        let locks_dir = tmp.path().join(".locks");
        {
            let _guard = cache
                .acquire_shared_lock("persist-test")
                .expect("acquire must succeed");
            assert!(locks_dir.is_dir(), "must exist during guard lifetime");
        }
        // Guard dropped. .locks/ must still exist.
        assert!(
            locks_dir.is_dir(),
            ".locks/ must persist after guard drop — next acquire \
             keys /proc/locks on the existing inode",
        );
    }

    /// `CacheDir::list` skips `.locks/` — pins the dotfile-filter
    /// contract in `CacheDir::list`. kernel_clean iterates what
    /// `list()` returns, so this is the same guard: `.locks/` is
    /// NEVER visible to the cleanup path. A future refactor that
    /// removed the `starts_with('.')` filter would regress
    /// through this test.
    #[test]
    fn list_skips_locks_dotfile_subdirectory() {
        let tmp = TempDir::new().unwrap();
        let cache = CacheDir::with_root(tmp.path().to_path_buf());
        // Materialize .locks/ via acquire, then list().
        let _guard = cache.acquire_shared_lock("dummy").expect("acquire");
        drop(_guard);
        assert!(
            tmp.path().join(".locks").is_dir(),
            ".locks/ must exist after acquire drop",
        );
        let entries = cache.list().expect("list must succeed");
        let keys: Vec<&str> = entries
            .iter()
            .map(|e| match e {
                ListedEntry::Valid(entry) => entry.key.as_str(),
                ListedEntry::Corrupt { key, .. } => key.as_str(),
            })
            .collect();
        assert!(
            !keys.iter().any(|k| k.starts_with('.')),
            "list() must not return dotfile children: {keys:?}",
        );
    }

    /// Empty cache root: acquire creates `.locks/` lazily.
    /// Distinct from `acquire_shared_lock_creates_lockfile_at_expected_path`
    /// above because THAT test asserts the lockfile path; this
    /// one pins the LAZY-create behavior — the cache root can be
    /// totally empty (no kernel entries) and first acquire still
    /// works.
    #[test]
    fn acquire_on_empty_root_creates_locks_dir_lazily() {
        let tmp = TempDir::new().unwrap();
        let root = tmp.path().join("pristine");
        std::fs::create_dir(&root).unwrap();
        let cache = CacheDir::with_root(root.clone());
        // Pre-acquire: no .locks/ yet.
        assert!(!root.join(".locks").exists());
        let _guard = cache
            .acquire_shared_lock("lazy-test")
            .expect("first acquire on empty root must succeed");
        assert!(
            root.join(".locks").is_dir(),
            "first acquire must materialize .locks/ lazily",
        );
    }

    /// `clean_all` MUST preserve the `.locks/` subdirectory. The
    /// `list()` filter skips dotfile children (tested elsewhere);
    /// `clean_all` removes what `list()` returns, so dotfiles — and
    /// specifically `.locks/` — survive. Without this guarantee,
    /// cleaning would delete a live SH flock's lockfile inode,
    /// leaving the next acquirer's `/proc/locks` lookup blind to
    /// the peer that still holds the (now-orphaned) fd.
    ///
    /// Repro sequence: populate an entry, acquire SH, clean_all,
    /// assert `.locks/` still exists AND the lockfile still exists
    /// inside it (the held fd keeps the inode alive even if the
    /// directory entry were removed — we're checking the directory
    /// entry specifically).
    #[test]
    fn cache_dir_clean_all_preserves_locks_subdir() {
        let tmp = TempDir::new().unwrap();
        let cache_root = tmp.path().join("cache");
        let cache = CacheDir::with_root(cache_root.clone());
        let src_dir = TempDir::new().unwrap();
        let image = create_fake_image(src_dir.path());

        // Populate a cache entry so clean_all has something to
        // remove (its job is to tell the dotfile filter apart from
        // real entries).
        cache
            .store(
                "entry-a",
                &CacheArtifacts::new(&image),
                &test_metadata("6.14.0"),
            )
            .expect("store must succeed");
        // Acquire a shared lock so .locks/{key}.lock materializes.
        let _guard = cache
            .acquire_shared_lock("entry-a")
            .expect("SH acquire must succeed");

        let locks_dir = cache_root.join(".locks");
        let lockfile = locks_dir.join("entry-a.lock");
        assert!(locks_dir.is_dir(), "precondition: .locks/ must exist");
        assert!(lockfile.exists(), "precondition: lockfile must exist");

        // Clean every entry. .locks/ is a dotfile-prefixed child
        // and must NOT be treated as a cache entry.
        let removed = cache.clean_all().expect("clean_all must succeed");
        assert_eq!(removed, 1, "clean_all must remove exactly 1 entry");

        // Post-clean: .locks/ subdirectory survives so the held SH
        // flock's inode is still the one /proc/locks points at.
        assert!(
            locks_dir.is_dir(),
            ".locks/ subdirectory must survive clean_all — the live \
             SH flock's inode would otherwise orphan",
        );
        assert!(
            lockfile.exists(),
            "lockfile must still exist under .locks/ after clean_all",
        );

        // And the entry itself is gone.
        assert!(
            !cache_root.join("entry-a").exists(),
            "cache entry must be removed by clean_all",
        );
    }

    /// `acquire_shared_lock` MUST reject cache keys containing path
    /// traversal components (`..`, `/`). Without the rejection, a
    /// key of `"../../etc/passwd"` would join against the cache
    /// root and materialize a lockfile OUTSIDE `.locks/`, which is
    /// both a security concern (attacker-controlled write through
    /// a library entry point) and a correctness failure (the lock
    /// file's inode won't match anything in subsequent enumeration).
    ///
    /// Pins the `validate_cache_key` rejection from the two path-
    /// traversal entry points — the `/` separator check and the
    /// `..` component check — with a single test input that
    /// triggers both. The error text must be actionable; asserting
    /// against the `"path"` substring in the message catches both
    /// the separator and traversal rejection arms.
    #[test]
    fn cache_dir_acquire_rejects_path_traversal_key() {
        let tmp = TempDir::new().unwrap();
        let cache_root = tmp.path().join("cache");
        let cache = CacheDir::with_root(cache_root.clone());

        // Attacker-shaped key: contains both `/` separators and
        // `..` traversal, hitting both rejection arms in
        // `validate_cache_key`.
        let err = cache
            .acquire_shared_lock("../../etc/passwd")
            .expect_err("path-traversal key must be rejected");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("path"),
            "error must mention path rejection: {msg}",
        );

        // Critically: no lockfile must have been created anywhere
        // outside `.locks/`. Walk two levels above the cache root
        // to verify nothing landed in `tmp.path()` or a traversal
        // destination. The cache root itself may or may not exist
        // (acquire creates `.locks/` lazily, but the validator
        // rejects BEFORE that materialization).
        let etc_passwd_lock = tmp.path().join("etc").join("passwd.lock");
        assert!(
            !etc_passwd_lock.exists(),
            "path traversal must NOT create a lockfile outside .locks/",
        );
        // And verify .locks/ wasn't touched either — the validator
        // rejects before any FS state is mutated.
        assert!(
            !cache_root.join(".locks").exists()
                || cache_root
                    .join(".locks")
                    .read_dir()
                    .unwrap()
                    .next()
                    .is_none(),
            ".locks/ must be empty if it exists at all — validator \
             rejects before lockfile creation",
        );
    }

    use crate::test_support::test_helpers::{EnvVarGuard, lock_env};

    // -- validate_home_for_cache direct unit tests --
    //
    // These tests pin the helper directly. The helper reads
    // `HOME` from the process environment, so each test holds
    // [`lock_env`] across the env mutation and uses
    // [`EnvVarGuard`] to scope the change. The integration-level
    // pins on the full `KTSTR_CACHE_DIR → XDG_CACHE_HOME → HOME`
    // cascade live in model.rs and cache.rs as
    // `resolve_cache_root_*` tests; this set covers the helper's
    // contract surface so a regression in the validation logic
    // surfaces against this dedicated entry point as well as the
    // integration paths.

    /// Unset `HOME` — `env::var()` returns `Err(NotPresent)` and
    /// the validator surfaces "HOME is unset" as the matching
    /// arm. Distinguished from the empty-string case below so an
    /// operator hitting either shape sees the actual misconfiguration
    /// in the diagnostic.
    #[test]
    fn validate_home_for_cache_rejects_unset() {
        let _env_lock = lock_env();
        let _home = EnvVarGuard::remove("HOME");
        let err = super::validate_home_for_cache().expect_err("unset HOME must be rejected");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("HOME is unset"),
            "diagnostic must call out the unset case specifically: {msg}",
        );
        assert!(
            !msg.contains("HOME is set to the empty string"),
            "unset HOME must NOT use the empty-string diagnostic — the two \
             cases are distinct now (NotPresent vs Ok(\"\")): {msg}",
        );
    }

    /// Empty `HOME` — explicitly assigned to the empty string.
    /// `env::var()` returns `Ok("")` and the validator surfaces
    /// "HOME is set to the empty string" so an operator can
    /// identify a Dockerfile `ENV HOME=` or shell-rc `export HOME=`
    /// typo as the cause rather than confusing it with the
    /// container-init-dropped-HOME case.
    #[test]
    fn validate_home_for_cache_rejects_empty() {
        let _env_lock = lock_env();
        let _home = EnvVarGuard::set("HOME", "");
        let err = super::validate_home_for_cache().expect_err("empty HOME must be rejected");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("HOME is set to the empty string"),
            "diagnostic must call out the empty-string case specifically: {msg}",
        );
        assert!(
            !msg.contains("HOME is unset"),
            "empty HOME must NOT use the unset diagnostic — the two \
             cases are distinct now: {msg}",
        );
    }

    /// Literal `/` — the container-init / no-home shape. Pins the
    /// dedicated arm (separate from the more general
    /// `is_empty()` check) so the operator-facing diagnostic stays
    /// specific to this case rather than collapsing into a generic
    /// "unset" message.
    #[test]
    fn validate_home_for_cache_rejects_root_slash() {
        let _env_lock = lock_env();
        let _home = EnvVarGuard::set("HOME", "/");
        let err = super::validate_home_for_cache().expect_err("HOME=/ must be rejected");
        let msg = format!("{err:#}");
        assert!(
            msg.contains("HOME is `/`"),
            "diagnostic must call out the root-slash case specifically: {msg}",
        );
        assert!(
            msg.contains("/.cache/ktstr"),
            "diagnostic must explain why (/.cache/ktstr aliases root fs): {msg}",
        );
    }

    /// Relative path — would resolve against CWD at every call,
    /// silently relocating the cache as the operator changes
    /// directories. Pins the absolute-path requirement.
    #[test]
    fn validate_home_for_cache_rejects_relative_path() {
        let _env_lock = lock_env();
        for rel in ["relative", "./relative", "home/user", "."] {
            let _home = EnvVarGuard::set("HOME", rel);
            let err = super::validate_home_for_cache()
                .expect_err(&format!("relative path '{rel}' must be rejected"));
            let msg = format!("{err:#}");
            assert!(
                msg.contains("not an absolute path"),
                "[rel={rel:?}] diagnostic must call out non-absolute: {msg}",
            );
            assert!(
                msg.contains(&format!("{rel:?}")),
                "[rel={rel:?}] diagnostic must echo the offending value verbatim: {msg}",
            );
        }
    }

    /// Acceptable shapes — absolute paths starting with `/` and
    /// longer than just `/`. Pins the happy path so a regression
    /// that tightened one of the rejection arms (e.g. a length
    /// check that accidentally rejected `/a`) surfaces here.
    /// Also pins that the returned PathBuf carries the HOME bytes
    /// verbatim — no canonicalization, no .cache/ktstr suffix.
    #[test]
    fn validate_home_for_cache_accepts_absolute_paths() {
        let _env_lock = lock_env();
        for ok in [
            "/home/user",
            "/var/empty",
            "/root",
            "/a", // shortest non-`/` absolute path
            "/home/user with spaces",
            "/home/user/.local/share",
        ] {
            let _home = EnvVarGuard::set("HOME", ok);
            let got = super::validate_home_for_cache()
                .unwrap_or_else(|e| panic!("absolute path {ok:?} must be accepted; got: {e:#}"));
            assert_eq!(
                got,
                std::path::PathBuf::from(ok),
                "returned PathBuf must equal the HOME value verbatim — \
                 helper does not append the cache suffix or canonicalize",
            );
        }
    }

    /// Edge: a path that starts with `/` but contains junk later
    /// (e.g. `//`, `/./`, `/.`). The helper does NOT canonicalize —
    /// these accept and surface the OS-level diagnostic at use
    /// time per the body comments above the helper. Pins this
    /// "intentionally not caught" boundary so a future change that
    /// adds canonicalization (which would BREAK this test) is
    /// forced to update the doc comments at the same time.
    #[test]
    fn validate_home_for_cache_does_not_canonicalize_dots_and_doubles() {
        let _env_lock = lock_env();
        for not_normalized in ["//", "/./", "/.", "/foo//bar", "/./home"] {
            let _home = EnvVarGuard::set("HOME", not_normalized);
            super::validate_home_for_cache().unwrap_or_else(|e| {
                panic!(
                    "non-normalized but absolute path {not_normalized:?} must \
                     pass the helper (downstream OS surfaces the diagnostic); \
                     got: {e:#}",
                )
            });
        }
    }
}