dbgscope 0.3.0

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

use thiserror::Error;

use super::decode::{
    MAX_POOL_HEADER_CHUNK, PAGE_SIZE, PoolHeaderLayout, SpecialPoolHeader, VS_UNIT, big_page_hash,
    decode_descriptor_at, decode_large_requested_size, decode_lfh_subsegment, decode_pool_header,
    decode_rb_root_for, decode_slist_header_next, decode_special_pool_header, decode_vs_chunk,
    descriptor_backend, lfh_bitmap_state, read_u16, read_u32, read_u64,
    valid_descriptor_tree_signature, valid_page_segment_signature, valid_vs_signature,
};
use super::{
    HeapIdentity, PoolBackend, PoolKind, PoolSpan, PoolState,
    layout::{LayoutError, PoolLayout, SlotBackReference, VsShape},
};

type SnapshotSource = Box<dyn std::error::Error + Send + Sync>;

/// A set of chunk addresses shared by every region one heap's evidence covers.
///
/// Shared rather than owned per region: see [`PoolRegion::reusable_chunks`].
type SharedChunks = Arc<HashSet<u64>>;

#[derive(Debug, Error)]
pub(crate) enum SnapshotError {
    #[error("read at {address:#x}+{size:#x}: {source}")]
    Read {
        address: u64,
        size: usize,
        #[source]
        source: SnapshotSource,
    },
    #[error("valid-region query at {address:#x}+{size:#x}: {source}")]
    RegionQuery {
        address: u64,
        size: usize,
        #[source]
        source: SnapshotSource,
    },
    #[error(
        "sparse virtual range at {address:#x}+{size:#x} (valid {valid_base:#x}+{valid_size:#x})"
    )]
    RegionValidation {
        address: u64,
        size: usize,
        valid_base: u64,
        valid_size: usize,
    },
    #[error("snapshot layout lookup failed: {source}")]
    Layout {
        #[source]
        source: LayoutError,
    },
    #[error("pool snapshot interrupted by Ctrl+C")]
    Interrupted,
    #[error("pool snapshot ran out of its walk budget")]
    BudgetExpired,
    #[error("interrupt-status query failed: {source}")]
    InterruptQuery {
        #[source]
        source: SnapshotSource,
    },
    #[error("invalid snapshot data: {detail}")]
    InvalidData { detail: String },
}

impl SnapshotError {
    /// Whether this ends the whole walk rather than one step of it.
    ///
    /// Every other variant is local — a node that would not read, a region that is not
    /// committed — so the walk records it and carries on. These two are not: carrying on
    /// would ignore the operator's Ctrl+C, or run past the deadline that exists precisely
    /// so nobody is left waiting on a walk they have already given up on.
    ///
    /// **Every site that swallows an error into a diagnostic has to re-raise these**, or
    /// the stop signal is absorbed by the first enclosing loop and the walk keeps going —
    /// which is the failure this predicate exists to make hard to reintroduce.
    fn halts_walk(&self) -> bool {
        matches!(self, Self::Interrupted | Self::BudgetExpired)
    }
}

impl From<LayoutError> for SnapshotError {
    fn from(source: LayoutError) -> Self {
        Self::Layout { source }
    }
}

fn missing_layout(item: impl Into<String>) -> SnapshotError {
    LayoutError::Missing { item: item.into() }.into()
}

#[derive(Debug, Clone)]
pub(crate) struct PoolRegion {
    pub address: u64,
    pub size: usize,
    pub requested_size: Option<u64>,
    pub pool_kind: PoolKind,
    pub numa_node: u16,
    pub heap: HeapIdentity,
    pub subsegment: Option<u64>,
    pub backend: PoolBackend,
    pub unit_size: u32,
    /// An LFH subsegment's busy blocks, one bit per slot. Discovery reduces the allocator's own
    /// `BlockBitmap` to this with `LfhBitmap::normalise`, so nothing that walks a region has
    /// to know which allocator laid it out.
    pub bitmap: Vec<u8>,
    pub heap_key: u64,
    pub pool_header: PoolHeaderLayout,
    pub vs_header_size: usize,
    pub vs_sizes_offset: usize,
    /// Whether this heap's VS allocator keeps a user block out of the last [`VS_UNIT`] bytes of
    /// a page: `_HEAP_VS_CONTEXT.Config.Flags.PageAlignLargeAllocs`, read once at discovery.
    ///
    /// A VS chunk's user block ordinarily begins right after its `_HEAP_VS_CHUNK_HEADER`. With
    /// this set, `nt!RtlpHpVsContextAllocate` tests whether `chunk + 0x20` is page-aligned and
    /// hands out `chunk + 0x20` when it is — one unit later than usual, with the usable size one
    /// unit shorter. Read on x64 26100.33438 at `+0xf6..+0x12a`: `lea rdi,[rsi+10h]`, then
    /// `test byte ptr [rbp+4],1` / `lea rax,[rsi+20h]` / `test rax,0FFFh` / `lea rdi,[rsi+20h]` /
    /// `add ebx,0FFFFFFF0h`, `rbp` being the context and `+4` its `Config`.
    /// `RtlpHpVsContextAllocateFinalize+0x3d` does the same for a multi-allocation, and
    /// `RtlpHpVsContextFree+0xf2` is the mirror: under the flag, a page-aligned user block's
    /// chunk is taken from `block - 0x20`. The kernel needs it because `ExFreeHeapPool+0xd6`
    /// tells a chunk from a page allocation by `P & 0xfff`, and a `_POOL_HEADER` in the last
    /// sixteen bytes of a page would hand its caller a page-aligned `P`.
    ///
    /// What that looked like from here before the walk consulted it (windbg-mcp `FOLLOWUPS.md`
    /// item 101): every chunk whose header sat at page offset 0xfe0 had its pool header decoded
    /// at 0xff0, which holds whatever the chunk held when it was free — two tree pointers — so
    /// the tag was the top half of a kernel address, the span began sixteen bytes early and ran
    /// sixteen bytes long, and the real header at the page start went unclaimed. Measured
    /// 2026-10-14 on `ctf-vm`: 1,275 paged chunks under the "tag" `0x84e7ffff`, every one with
    /// its candidate at 0xff0; the chunk at `0xffffe78459203fe0` is `0x80` bytes, holds
    /// `ffffe78459207e18 ffffe78459213bc8` at `+0x10`, and has at `0xffffe78459204000` the
    /// `MmDT` header `!pool` names there (`size: 60`). Both pool heaps on that kernel carry the
    /// flag; no target without it has been measured, which is what gating on it is for.
    pub vs_skips_page_end: bool,
    pub known_tag: Option<u32>,
    /// Allocator-derived states for segment/page-range cells.
    pub states: Vec<PoolState>,
    /// VS chunk-header addresses present in the free tree.
    ///
    /// Shared, not owned: one region is created per page-range descriptor, so on a real
    /// kernel there are thousands of them, and they all carry the *same* evidence — the one
    /// set their heap's free tree produced. Cloning the set per region copied it thousands
    /// of times over, entirely off the wire and so invisible when the walk was blamed on
    /// debugger reads.
    pub reusable_chunks: SharedChunks,
    /// VS chunk-header addresses present in delay-free/lookaside lists. Shared for the
    /// reason [`Self::reusable_chunks`] is.
    pub cached_chunks: SharedChunks,
    /// Every `nt!PoolBigPageTable` entry whose allocation begins inside this region, by the
    /// address it begins at.
    ///
    /// Resolved during discovery, where the table is, so that walking a chunk needs no reads of
    /// its own. Empty on every region that holds no such allocation, which is most of them — and
    /// on every user-mode one, which has no such table.
    pub big_pool: Arc<HashMap<u64, BigPageEntry>>,
}

/// One run of address space the target's memory manager describes the same way; see
/// [`PoolMemory::committed_run`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct CommitRun {
    /// One past the last byte of the run, and always above the address it was asked about —
    /// so a caller stepping a span with it cannot fail to advance.
    pub end: u64,
    /// Whether pages exist behind it.
    pub committed: bool,
}

pub(crate) trait PoolMemory {
    fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError>;
    fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError>;
    fn interrupted(&self) -> Result<bool, SnapshotError>;

    /// Whether this walk's wall-clock deadline has passed.
    ///
    /// Defaulted to "never" so a plain memory source carries no clock; [`Budgeted`] is what
    /// puts one in front of it, and [`SnapshotWalker::walk`] is what wraps it.
    fn out_of_budget(&self) -> bool {
        false
    }

    /// What the target's **memory manager** says about the run of pages `address` lies in —
    /// a different question from whether the debugger can read it, and the only one that
    /// separates *nothing was ever here* from *we could not see what is here*.
    ///
    /// `None` means this source cannot say, and the caller then keeps the conservative
    /// reading: what did not read stays a hole in the walk's coverage. **That default is
    /// deliberate.** A source that does not override this behaves exactly as it did, so no
    /// fixture starts excusing gaps by accident — the ones that exercise
    /// [`PoolState::Uncommitted`] have to say so, which is what makes their assertions mean
    /// anything.
    fn committed_run(&self, _address: u64) -> Option<CommitRun> {
        None
    }
}

/// A memory source with a deadline attached.
///
/// The walk already polls the engine's interrupt flag, which Ctrl+C sets and so does a host
/// through [`InterruptHandle`](crate::dbgeng::InterruptHandle) — but **both need somebody
/// watching**, and a caller blocked on the query is not. Without a clock its own call never
/// returns and the engine keeps walking; since one engine serves one target one call at a
/// time, every later call to that target queues behind the walk. The session is then wedged
/// until it is killed, which on a live kernel leaves the guest halted. This is the one
/// stopper that needs nobody present.
struct Budgeted<'a, M> {
    inner: &'a M,
    /// `None` runs to completion — correct only where something else can stop the walk: an
    /// operator at a prompt, or a host that both holds an interrupt handle and knows when to
    /// use it.
    deadline: Option<Instant>,
}

impl<'a, M> Budgeted<'a, M> {
    fn new(inner: &'a M, deadline: Option<Instant>) -> Self {
        Self { inner, deadline }
    }
}

impl<M: PoolMemory> PoolMemory for Budgeted<'_, M> {
    fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
        self.inner.read_exact(address, size)
    }

    fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
        self.inner.valid_region(address, size)
    }

    fn interrupted(&self) -> Result<bool, SnapshotError> {
        self.inner.interrupted()
    }

    /// This wrapper *adds* a deadline; it does not replace whatever the source already had,
    /// so wrapping can only ever make a walk stop sooner.
    fn out_of_budget(&self) -> bool {
        self.deadline
            .is_some_and(|deadline| Instant::now() >= deadline)
            || self.inner.out_of_budget()
    }

    fn committed_run(&self, address: u64) -> Option<CommitRun> {
        self.inner.committed_run(address)
    }
}

impl PoolMemory for crate::dbgeng::DebugEngine {
    fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
        self.read_memory(address, size)
            .map_err(|source| SnapshotError::Read {
                address,
                size,
                source: Box::new(source),
            })
    }

    fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
        self.valid_virtual_region(address, size)
            .map_err(|source| SnapshotError::RegionQuery {
                address,
                size,
                source: Box::new(source),
            })
    }

    fn interrupted(&self) -> Result<bool, SnapshotError> {
        crate::dbgeng::DebugEngine::interrupted(self).map_err(|source| {
            SnapshotError::InterruptQuery {
                source: Box::new(source),
            }
        })
    }

    /// **Not asked of a kernel session at all.** `QueryVirtual` is the engine reaching
    /// `NtQueryVirtualMemory` in the debuggee, which a kernel target has no equivalent of, so
    /// every call there fails — and a kernel walk files thousands of unreadable spans, which
    /// would be thousands of failed calls to learn the same thing each time. The kernel walk
    /// therefore keeps counting every unreadable page against its coverage, which on a target
    /// trimming paged pool is the truth: those pages are committed and the walk really did
    /// miss what was in them (`windbg-mcp` FOLLOWUPS item 100).
    ///
    /// Every other answer that is not plainly *committed* or *not committed* — a query that
    /// fails, a zero-length run, a state this crate does not name — is `None` rather than a
    /// guess, because the guess is what would make this lie: read as uncommitted it excuses a
    /// gap that is real.
    fn committed_run(&self, address: u64) -> Option<CommitRun> {
        if crate::dbgeng::DebugEngine::is_kernel_target(self).ok()? {
            return None;
        }
        let region = self.virtual_region(address).ok()?;
        if !region.contains(address) {
            return None;
        }
        Some(CommitRun {
            end: region.end()?,
            committed: region.state.committed()?,
        })
    }
}

/// The one place a walk asks whether it is still allowed to run.
///
/// **The rule: every loop that issues a debugger read calls this, first thing.** Not only the
/// unbounded ones — a bounded loop is not automatically safe. The descriptor loop in
/// [`discover_segment_context`] is capped at 4096 iterations and reads twice per LFH
/// descriptor, so polling one level out in the segment loop still left ~8k reads between the
/// deadline passing and anything noticing. On a live KD link that is thousands of round
/// trips, minutes of them: the very failure this budget exists to prevent, reintroduced
/// inside the fix for it.
///
/// Reads themselves deliberately keep working past the deadline rather than failing. Having
/// [`Budgeted`] reject them would bound the walk with no rule to remember — but nearly every
/// read here is swallowed into a "cannot read ..." diagnostic, so out-of-time would surface
/// as *unreadable memory*. That is the one lie this module is built to avoid: what the walk
/// did not reach must never be reported as what is not there.
///
/// Both answers halt (see [`SnapshotError::halts_walk`]); they differ in what the caller does
/// with the halt, which [`SnapshotWalker::walk`] decides.
fn check_budget(memory: &impl PoolMemory) -> Result<(), SnapshotError> {
    if memory.interrupted()? {
        return Err(SnapshotError::Interrupted);
    }
    if memory.out_of_budget() {
        return Err(SnapshotError::BudgetExpired);
    }
    Ok(())
}

fn guarded_read(
    memory: &impl PoolMemory,
    address: u64,
    size: usize,
) -> Result<Vec<u8>, SnapshotError> {
    if size == 0 {
        return Ok(Vec::new());
    }
    let (valid_base, valid_size) = memory.valid_region(address, size)?;
    if valid_base != address || valid_size < size {
        return Err(SnapshotError::RegionValidation {
            address,
            size,
            valid_base,
            valid_size,
        });
    }
    memory.read_exact(address, size)
}

fn scalar(memory: &impl PoolMemory, address: u64, size: usize) -> Result<u64, SnapshotError> {
    let bytes = guarded_read(memory, address, size)?;
    match size {
        1 => bytes
            .first()
            .map(|&byte| byte as u64)
            .ok_or_else(|| SnapshotError::InvalidData {
                detail: "short u8".into(),
            }),
        2 => Ok(
            u16::from_le_bytes(bytes.try_into().map_err(|_| SnapshotError::InvalidData {
                detail: "short u16".into(),
            })?) as u64,
        ),
        4 => Ok(
            u32::from_le_bytes(bytes.try_into().map_err(|_| SnapshotError::InvalidData {
                detail: "short u32".into(),
            })?) as u64,
        ),
        8 => Ok(u64::from_le_bytes(bytes.try_into().map_err(|_| {
            SnapshotError::InvalidData {
                detail: "short u64".into(),
            }
        })?)),
        _ => Err(SnapshotError::InvalidData {
            detail: format!("unsupported scalar size {size}"),
        }),
    }
}

fn walk_tree_nodes(
    memory: &impl PoolMemory,
    root: u64,
    left_offset: usize,
    right_offset: usize,
    limit: usize,
    label: &str,
    diagnostics: &mut Vec<String>,
) -> Result<Vec<u64>, SnapshotError> {
    let mut nodes = Vec::new();
    let mut stack = vec![root];
    let mut seen = HashSet::new();
    // Links are followed exactly as stored. They were masked to 16 bytes, which is harmless for
    // a page-range descriptor's node and wrong for a VS free chunk's: its `Node` is at +0x8 of a
    // 16-aligned header, so masking landed every VS node on its chunk's encoded `Sizes` word,
    // read that as the left link, and took the real left link for the right one — the
    // `unreadable VS free tree node 0x84a23d…` diagnostics on ARM64 26100.1 (2026-09-23).
    while let Some(node) = stack.pop() {
        if node == 0 {
            continue;
        }
        check_budget(memory)?;
        if !seen.insert(node) {
            diagnostics.push(format!("{label} cycle detected at {node:#x}"));
            continue;
        }
        if nodes.len() >= limit {
            diagnostics.push(format!("{label} traversal limit reached"));
            break;
        }
        let size = left_offset.max(right_offset).saturating_add(8);
        match guarded_read(memory, node, size) {
            Ok(bytes) => {
                nodes.push(node);
                if let Some(right) = read_u64(&bytes, right_offset) {
                    stack.push(right);
                }
                if let Some(left) = read_u64(&bytes, left_offset) {
                    stack.push(left);
                }
            }
            Err(error) => diagnostics.push(format!("unreadable {label} node {node:#x}: {error}")),
        }
    }
    Ok(nodes)
}

fn tree_nodes(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    tree_address: u64,
    limit: usize,
    label: &str,
    diagnostics: &mut Vec<String>,
) -> Result<Vec<u64>, SnapshotError> {
    // Polled before the root and flag reads, not only inside the loop below: every caller
    // reaches here a few unpolled reads past its own poll, which is a stretch of up to five
    // between checks, and the sampled budgets in
    // `test_the_walk_stops_reading_promptly_once_its_budget_is_gone` landed on it once the
    // per-heap config read shifted them by one (2026-10-14).
    check_budget(memory)?;
    let Ok(root_offset) = layout.field("_RTL_RB_TREE", "Root") else {
        diagnostics.push(format!("cannot resolve {label} root field"));
        return Ok(Vec::new());
    };
    let root_value = match scalar(memory, tree_address + root_offset as u64, 8) {
        Ok(value) => value,
        Err(error) => {
            diagnostics.push(format!("cannot read {label} root: {error}"));
            return Ok(Vec::new());
        }
    };
    let encoded = if let Ok(encoded_offset) = layout.field("_RTL_RB_TREE", "Encoded") {
        match scalar(memory, tree_address + encoded_offset as u64, 1) {
            Ok(value) => value & 1 != 0,
            Err(error) => {
                diagnostics.push(format!("cannot read {label} encoded flag: {error}"));
                return Ok(Vec::new());
            }
        }
    } else {
        false
    };
    let decoded_root = if layout.is_user() {
        decode_rb_root_for(root_value, tree_address, encoded, true)
    } else {
        super::decode::decode_rb_root(root_value, tree_address, encoded)
    };
    let Some(root) = decoded_root else {
        diagnostics.push(format!("rejecting corrupt {label} root {root_value:#x}"));
        return Ok(Vec::new());
    };
    let Ok(left) = layout.field("_RTL_BALANCED_NODE", "Left") else {
        return Ok(Vec::new());
    };
    let Ok(right) = layout.field("_RTL_BALANCED_NODE", "Right") else {
        return Ok(Vec::new());
    };
    walk_tree_nodes(memory, root, left, right, limit, label, diagnostics)
}

fn walk_slist_nodes(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    head: u64,
    limit: usize,
    label: &str,
    diagnostics: &mut Vec<String>,
) -> Result<Vec<u64>, SnapshotError> {
    let mut nodes = Vec::new();
    let mut seen = HashSet::new();
    let Ok(header) = layout.type_layout("_SLIST_HEADER") else {
        diagnostics.push(format!("cannot resolve {label} SLIST header type"));
        return Ok(nodes);
    };
    let Ok(alignment_offset) = layout.field("_SLIST_HEADER", "Alignment") else {
        diagnostics.push(format!("cannot resolve {label} SLIST depth field"));
        return Ok(nodes);
    };
    let Ok(region_offset) = layout.field("_SLIST_HEADER", "Region") else {
        diagnostics.push(format!("cannot resolve {label} SLIST next field"));
        return Ok(nodes);
    };
    let bytes = match guarded_read(memory, head, header.size as usize) {
        Ok(value) => value,
        Err(error) => {
            diagnostics.push(format!("cannot read {label} list head: {error}"));
            return Ok(nodes);
        }
    };
    let depth = read_u16(&bytes, alignment_offset).map_or(0, usize::from);
    let mut entry = read_u64(&bytes, region_offset)
        .map(decode_slist_header_next)
        .unwrap_or(0);
    let expected = depth.min(limit);
    while entry != 0 && nodes.len() < expected {
        check_budget(memory)?;
        if !seen.insert(entry) {
            diagnostics.push(format!("{label} list cycle detected at {entry:#x}"));
            break;
        }
        nodes.push(entry);
        match scalar(memory, entry, 8) {
            Ok(next) => entry = next & !0xf,
            Err(error) => {
                diagnostics.push(format!("unreadable {label} list entry {entry:#x}: {error}"));
                break;
            }
        }
    }
    if depth > limit {
        diagnostics.push(format!("{label} list traversal limit reached"));
    } else if nodes.len() != depth {
        diagnostics.push(format!(
            "{label} list depth is {depth}, but only {} entries were readable",
            nodes.len()
        ));
    }
    Ok(nodes)
}

fn insert_cached_chunk_candidates(
    cached: &mut HashSet<u64>,
    entry: u64,
    pool_header_size: u64,
    vs_header_size: u64,
) {
    cached.insert(entry);
    let overhead = pool_header_size.saturating_add(vs_header_size);
    if let Some(header) = entry.checked_sub(overhead) {
        cached.insert(header);
        // The lookaside's half of [`PoolRegion::vs_skips_page_end`]: data sixteen bytes into a
        // page has its pool header at the page start and, under the flag, its chunk header one
        // unit before where the subtraction above puts it. Both are kept, because this is a
        // set of candidates and only a chunk decoded at one of them can match.
        if header & (PAGE_SIZE - 1) == PAGE_SIZE - pool_header_size {
            cached.insert(header.saturating_sub(VS_UNIT));
        }
    }
}

#[derive(Default)]
struct Discovery {
    regions: Vec<PoolRegion>,
    diagnostics: Vec<String>,
    /// `nt!PoolBigPageTable`, opened once for the whole walk. `None` on a target that has
    /// none — a user-mode heap, or a kernel whose symbols do not carry it.
    big_pages: Option<BigPageTable>,
}

impl Discovery {
    /// What the big-page table says about the allocation at `address`, reading whichever
    /// part of the table that question lands in and keeping it for the next one.
    fn big_page(
        &mut self,
        memory: &impl PoolMemory,
        address: u64,
    ) -> Result<Option<BigPageEntry>, SnapshotError> {
        let Self {
            diagnostics,
            big_pages,
            ..
        } = self;
        let Some(table) = big_pages.as_mut() else {
            return Ok(None);
        };
        table.lookup(memory, address, diagnostics)
    }
}

const SPECIAL_POOL_KINDS: [PoolKind; 4] = [
    PoolKind::SpecialNonPaged,
    PoolKind::SpecialNonPagedNx,
    PoolKind::SpecialPaged,
    PoolKind::SpecialPrototypePaged,
];

/// Enumerates every pool region, appending to `discovery`.
///
/// Takes `discovery` by reference rather than returning it so that a walk which halts
/// part-way still hands back the regions it *did* find. Returning `Result<Discovery, _>`
/// threw them away on the one path where they matter most — the caller can then walk what
/// discovery reached instead of reporting an empty pool.
fn discover_pool_regions(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    traversal_limit: usize,
    discovery: &mut Discovery,
) -> Result<(), SnapshotError> {
    let state_address = *layout
        .globals
        .get("ExPoolState")
        .ok_or_else(|| missing_layout("ExPoolState"))?;
    let state = layout.type_layout("_EX_POOL_HEAP_MANAGER_STATE")?;
    let node = layout.type_layout("_EX_HEAP_POOL_NODE")?;
    let number_offset = layout.field("_EX_POOL_HEAP_MANAGER_STATE", "NumberOfPools")?;
    let node_offset = layout.field("_EX_POOL_HEAP_MANAGER_STATE", "PoolNode")?;
    let special_offset = layout.field("_EX_POOL_HEAP_MANAGER_STATE", "SpecialHeaps")?;
    let heaps_offset = layout.field("_EX_HEAP_POOL_NODE", "Heaps")?;
    let lookasides_offset = layout.field("_EX_HEAP_POOL_NODE", "Lookasides").ok();
    let dynamic_lookaside_size = layout
        .type_layout("_RTL_DYNAMIC_LOOKASIDE")
        .ok()
        .map(|lookaside| lookaside.size as u64);
    let number = scalar(memory, state_address + number_offset as u64, 4)? as usize;
    if number == 0 || number > 256 {
        return Err(SnapshotError::InvalidData {
            detail: format!("implausible ExPoolState.NumberOfPools {number}"),
        });
    }
    let mut heaps = Vec::new();
    for numa_node in 0..number {
        // Four pointer reads per node, up to 256 nodes: bounded, but a thousand round trips
        // is still seconds on a live link, and the rule is uniform — a loop that reads polls.
        check_budget(memory)?;
        let Some(node_address) = state_address
            .checked_add(node_offset as u64)
            .and_then(|address| address.checked_add(numa_node as u64 * node.size as u64))
        else {
            discovery
                .diagnostics
                .push("pool-node address overflow".into());
            continue;
        };
        for heap_index in 0..4usize {
            let pointer_address = node_address + heaps_offset as u64 + heap_index as u64 * 8;
            let heap = match scalar(memory, pointer_address, 8) {
                Ok(value) => value,
                Err(error) => {
                    discovery.diagnostics.push(format!(
                        "cannot read pool node {numa_node} heap {heap_index}: {error}"
                    ));
                    continue;
                }
            };
            if heap != 0 {
                let pool_kind = match heap_index {
                    0 => PoolKind::NonPagedExecutable,
                    1 => PoolKind::NonPagedNx,
                    2 => PoolKind::Paged,
                    _ => PoolKind::PrototypePaged,
                };
                let dynamic_lookaside =
                    lookasides_offset
                        .zip(dynamic_lookaside_size)
                        .and_then(|(offset, size)| {
                            node_address.checked_add(offset as u64).and_then(|base| {
                                base.checked_add(u64::from(pool_kind.is_paged()) * size)
                            })
                        });
                heaps.push((heap, numa_node as u16, pool_kind, false, dynamic_lookaside));
            }
        }
    }
    for (special_index, pool_kind) in SPECIAL_POOL_KINDS.into_iter().enumerate() {
        let pointer_address = state_address + special_offset as u64 + special_index as u64 * 8;
        match scalar(memory, pointer_address, 8) {
            Ok(heap) if heap != 0 => heaps.push((heap, 0, pool_kind, true, None)),
            Ok(_) => {}
            Err(error) => discovery.diagnostics.push(format!(
                "cannot read special pool heap {special_index}: {error}"
            )),
        }
    }

    let globals_address = *layout
        .globals
        .get("RtlpHpHeapGlobals")
        .ok_or_else(|| missing_layout("RtlpHpHeapGlobals"))?;
    let heap_key = scalar(
        memory,
        globals_address + layout.field("_RTLP_HP_HEAP_GLOBALS", "HeapKey")? as u64,
        8,
    )?;
    let lfh_key = scalar(
        memory,
        globals_address + layout.field("_RTLP_HP_HEAP_GLOBALS", "LfhKey")? as u64,
        8,
    )?;
    // Opened before the heaps because every one of them will ask: on a kernel, an allocated
    // page range that holds no subsegment is a big-pool allocation whose tag is only here.
    discovery.big_pages = BigPageTable::open(memory, layout, &mut discovery.diagnostics)?;
    for (heap_address, numa_node, pool_kind, special, dynamic_lookaside) in heaps {
        let identity = HeapIdentity {
            pool_state: state_address,
            heap: heap_address,
            special,
        };
        if let Err(error) = discover_heap_regions(
            memory,
            layout,
            heap_address,
            numa_node,
            pool_kind,
            identity,
            dynamic_lookaside,
            heap_key,
            lfh_key,
            traversal_limit,
            discovery,
        ) {
            // A halt is about the walk, not about this heap. Recorded as a per-heap failure
            // it would read as "heap 3 is unreadable" and — worse — the loop would carry on
            // to heaps 4..n, each stopping the same way, so the deadline would bound one
            // heap instead of the walk.
            if error.halts_walk() {
                return Err(error);
            }
            discovery.diagnostics.push(format!(
                "cannot fully discover heap {heap_address:#x}: {error}"
            ));
        }
    }
    let _ = state.size;
    Ok(())
}

/// One place carrying VS free-chunk state.
struct VsRoot {
    base: u64,
    tree_offset: usize,
    delay_offset: Option<usize>,
}

/// Resolves where a VS context keeps its free-chunk state. Every shape is supported, because a
/// debugger host does not get to choose which build it is pointed at:
///
/// * **inline family** — `FreeChunkTree`/`DelayFreeContext` are in `_HEAP_VS_CONTEXT`,
///   so there is exactly one root: the context itself.
/// * **affinity-slot families** — they live in `_HEAP_VS_AFFINITY_SLOT`s reached through a slot
///   map. `SlotMapRef` and each `SlotRef` are offsets *from the context*, scaled by 64 bytes, and
///   the map holds `AffinityMask + 1` entries. Entries routinely share a slot, so the result is
///   deduplicated. The two differ only in how a slot proves it belongs to this context — see
///   [`SlotBackReference`].
///
/// Which shape it is comes from [`PoolLayout::vs_shape`] rather than from a second reading of the
/// field table here, so the walk and the provenance a caller is told about cannot disagree.
///
/// An empty vector means no shape resolved; the caller then walks no VS evidence rather than
/// guessing at an address. An `Err` means the walk itself is over — the slot map is up to 256
/// entries plus an owner read per distinct slot, all over the wire, so it polls the deadline like
/// every other read-issuing loop (see [`check_budget`]). It cannot lean on a later poll: invalid
/// slot refs yield no roots, so nothing downstream reads at all and the next check is a whole
/// segment context away.
fn vs_roots(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    context: u64,
    diagnostics: &mut Vec<String>,
) -> Result<Vec<VsRoot>, SnapshotError> {
    let shape = match layout.vs_shape() {
        Ok(shape) => shape,
        Err(detail) => {
            diagnostics.push(format!(
                "VS free-chunk state is in neither the context nor an affinity slot: {detail}"
            ));
            return Ok(Vec::new());
        }
    };

    let (slot_map_ref_offset, affinity_offset, slot_ref_offset, tree_offset, delay_offset, back) =
        match shape {
            // A context that carries the tree has no slots.
            VsShape::Inline { tree, delay } => {
                return Ok(vec![VsRoot {
                    base: context,
                    tree_offset: tree,
                    delay_offset: delay,
                }]);
            }
            VsShape::AffinitySlots {
                slot_map_ref,
                affinity_mask,
                slot_ref,
                tree,
                delay,
                back,
            } => (slot_map_ref, affinity_mask, slot_ref, tree, delay, back),
        };

    let (Ok(slot_map_ref), Ok(affinity_mask)) = (
        scalar(memory, context + slot_map_ref_offset as u64, 2),
        scalar(memory, context + affinity_offset as u64, 1),
    ) else {
        diagnostics.push(format!(
            "cannot read the VS slot map of context {context:#x}"
        ));
        return Ok(Vec::new());
    };

    let entries = affinity_mask as usize + 1;
    // A zero ref would alias the context, and the entry count is bounded by the affinity
    // mask; both would otherwise walk arbitrary memory.
    if slot_map_ref == 0 || entries > 256 {
        diagnostics.push(format!(
            "implausible VS slot map for context {context:#x}: ref {slot_map_ref:#x}, {entries} entries"
        ));
        return Ok(Vec::new());
    }

    let entry_size = layout
        .type_layout("_HEAP_VS_SLOT_MAP")
        .map_or(4, |map| map.size as u64);
    let slot_map = context + (slot_map_ref << 6);

    let mut roots = Vec::new();
    let mut seen = HashSet::new();
    for index in 0..entries {
        check_budget(memory)?;
        let entry = slot_map + index as u64 * entry_size;
        let Ok(slot_ref) = scalar(memory, entry + slot_ref_offset as u64, 2) else {
            // Skipping silently would drop that slot's free tree and delay-free list, so
            // its chunks get misclassified with nothing to say why.
            diagnostics.push(format!(
                "cannot read VS slot map entry {index} at {entry:#x}; its affinity slot is omitted"
            ));
            continue;
        };
        if slot_ref == 0 {
            continue;
        }
        let slot = context + (slot_ref << 6);
        if !seen.insert(slot) {
            continue;
        }
        // Require the slot to name the context we came from. A misdecoded SlotRef would
        // otherwise point the tree walk at unrelated memory that happens to be readable.
        //
        // Both spellings are read as the same question — *which context does this slot claim* —
        // so that a slot that disagrees produces one diagnostic shape rather than two. What the
        // stored value means differs: an address is the answer, a displacement has to be
        // subtracted from the slot to become one. `checked_sub` because a displacement larger
        // than the slot's own address is not a context, it is a rejection.
        let back_offset = match back {
            SlotBackReference::Address(offset) | SlotBackReference::Displacement(offset) => offset,
        };
        let claimed = scalar(memory, slot + back_offset as u64, 8).map(|value| match back {
            SlotBackReference::Address(_) => Some(value),
            SlotBackReference::Displacement(_) => slot.checked_sub(value),
        });
        match claimed {
            Ok(Some(owner)) if owner == context => roots.push(VsRoot {
                base: slot,
                tree_offset,
                delay_offset,
            }),
            Ok(Some(owner)) => diagnostics.push(format!(
                "VS affinity slot {slot:#x} claims context {owner:#x}, not {context:#x}; skipped"
            )),
            // A displacement that runs off the bottom of the address space names no context at
            // all; saying so as "claims none" keeps it in the same shape as any other disagreement.
            Ok(None) => diagnostics.push(format!(
                "VS affinity slot {slot:#x} claims context none, not {context:#x}; skipped"
            )),
            Err(error) => {
                diagnostics.push(format!("cannot read VS affinity slot {slot:#x}: {error}"))
            }
        }
    }
    Ok(roots)
}

/// `_RTL_HP_VS_CONFIG.Flags.PageAlignLargeAllocs`: `Pos 0, 1 Bit` of the byte at
/// `_HEAP_VS_CONTEXT.Config` on the x64 26100.33438 PDB, and the first flag the type declares.
/// The byte's offset is resolved; the bit's position inside it is this.
const VS_CONFIG_PAGE_ALIGN: u64 = 1;

/// Whether `heap_address`'s VS allocator moves a user block off the end of a page — see
/// [`PoolRegion::vs_skips_page_end`] for the rule and where it was read.
///
/// Asked once per heap, here, because what it decides is applied to every chunk. A layout that
/// did not resolve `Config`, or a context that will not read, answers **unset** and says so: that
/// is the reading the walk made before it consulted the flag at all, and on a target that does
/// carry it the diagnostic is what points at the ~1,300 misplaced headers per boot it costs.
/// An `Err` is the budget or an interrupt, polled before the read like every other read here.
fn vs_skips_page_end(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    heap_address: u64,
    diagnostics: &mut Vec<String>,
) -> Result<bool, SnapshotError> {
    let Ok(context_offset) = layout.field("_SEGMENT_HEAP", "VsContext") else {
        return Ok(false);
    };
    let Ok(config_offset) = layout.field("_HEAP_VS_CONTEXT", "Config") else {
        diagnostics.push(format!(
            "VS context of heap {heap_address:#x} has no resolved Config: a user block ending \
             a page is read at its chunk header"
        ));
        return Ok(false);
    };
    check_budget(memory)?;
    let address = heap_address + context_offset as u64 + config_offset as u64;
    Ok(match scalar(memory, address, 1) {
        Ok(flags) => flags & VS_CONFIG_PAGE_ALIGN != 0,
        Err(error) => {
            diagnostics.push(format!(
                "cannot read VS context config at {address:#x}: {error}; a user block ending a \
                 page is read at its chunk header"
            ));
            false
        }
    })
}

fn discover_vs_evidence(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    heap_address: u64,
    dynamic_lookaside: Option<u64>,
    limit: usize,
    diagnostics: &mut Vec<String>,
) -> Result<(SharedChunks, SharedChunks), SnapshotError> {
    let Ok(vs_context_offset) = layout.field("_SEGMENT_HEAP", "VsContext") else {
        return Ok(Default::default());
    };
    let context = heap_address + vs_context_offset as u64;
    let roots = vs_roots(memory, layout, context, diagnostics)?;
    if roots.is_empty() {
        return Ok(Default::default());
    }
    let tree_node_offset = layout
        .field("_HEAP_VS_CHUNK_FREE_HEADER", "TreeNode")
        .unwrap_or(0);
    let mut reusable = HashSet::new();
    for root in &roots {
        reusable.extend(
            tree_nodes(
                memory,
                layout,
                root.base + root.tree_offset as u64,
                limit,
                "VS free tree",
                diagnostics,
            )?
            .into_iter()
            .map(|node| node.saturating_sub(tree_node_offset as u64)),
        );
    }

    let mut cached = HashSet::new();
    let pool_header_size = layout
        .type_layout("_POOL_HEADER")
        .map_or(0, |value| value.size as u64);
    let vs_header_size = layout
        .type_layout("_HEAP_VS_CHUNK_HEADER")
        .map_or(0, |value| value.size as u64);
    if let Ok(list_offset) = layout.field("_HEAP_VS_DELAY_FREE_CONTEXT", "ListHead") {
        for root in &roots {
            // In the inline family the delay-free list sits beside the tree in the context;
            // in the affinity family it is per-slot. Either way it is known from `root.base`.
            let Some(delay_offset) = root.delay_offset else {
                continue;
            };
            for entry in walk_slist_nodes(
                memory,
                layout,
                root.base + delay_offset as u64 + list_offset as u64,
                limit,
                "VS delay-free",
                diagnostics,
            )? {
                insert_cached_chunk_candidates(
                    &mut cached,
                    entry,
                    pool_header_size,
                    vs_header_size,
                );
            }
        }
    }

    if let Some(dynamic) = dynamic_lookaside {
        let bucket_count = layout
            .field("_RTL_DYNAMIC_LOOKASIDE", "BucketCount")
            .ok()
            .and_then(|offset| scalar(memory, dynamic + offset as u64, 4).ok())
            .unwrap_or(0) as usize;
        let buckets_offset = layout.field("_RTL_DYNAMIC_LOOKASIDE", "Buckets").ok();
        let lookaside = layout.type_layout("_RTL_LOOKASIDE").ok();
        let list_offset = layout.field("_RTL_LOOKASIDE", "ListHead").ok();
        if bucket_count > 64 {
            diagnostics.push(format!(
                "rejecting implausible VS dynamic-lookaside bucket count {bucket_count}"
            ));
        } else if let (Some(buckets_offset), Some(lookaside), Some(list_offset)) =
            (buckets_offset, lookaside, list_offset)
        {
            for bucket in 0..bucket_count {
                check_budget(memory)?;
                let Some(bucket_address) = dynamic
                    .checked_add(buckets_offset as u64)
                    .and_then(|value| value.checked_add(bucket as u64 * u64::from(lookaside.size)))
                else {
                    diagnostics.push("VS dynamic-lookaside bucket address overflow".into());
                    break;
                };
                if let Ok(size_offset) = layout.field("_RTL_LOOKASIDE", "Size") {
                    match scalar(memory, bucket_address + size_offset as u64, 4) {
                        Ok(0) => continue,
                        Ok(size) if size > 0x1_0000 => {
                            diagnostics.push(format!(
                                "rejecting VS dynamic-lookaside bucket {bucket} size {size:#x}"
                            ));
                            continue;
                        }
                        Ok(_) => {}
                        Err(error) => {
                            diagnostics.push(format!(
                                "cannot read VS dynamic-lookaside bucket {bucket} size: {error}"
                            ));
                            continue;
                        }
                    }
                }
                for entry in walk_slist_nodes(
                    memory,
                    layout,
                    bucket_address + list_offset as u64,
                    limit,
                    "VS dynamic-lookaside",
                    diagnostics,
                )? {
                    // Lookaside links point at usable data, while delay-free links
                    // point at the VS chunk header. Keep both page-end header
                    // candidates; only a decoded chunk at that address can match.
                    insert_cached_chunk_candidates(
                        &mut cached,
                        entry,
                        pool_header_size,
                        vs_header_size,
                    );
                }
            }
        }
    }
    Ok((Arc::new(reusable), Arc::new(cached)))
}

#[allow(clippy::too_many_arguments)]
fn discover_heap_regions(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    heap_address: u64,
    numa_node: u16,
    pool_kind: PoolKind,
    identity: HeapIdentity,
    dynamic_lookaside: Option<u64>,
    heap_key: u64,
    lfh_key: u64,
    traversal_limit: usize,
    discovery: &mut Discovery,
) -> Result<(), SnapshotError> {
    let heap = layout.type_layout("_SEGMENT_HEAP")?;
    let context = layout.type_layout("_HEAP_SEG_CONTEXT")?;
    let contexts_offset = layout.field("_SEGMENT_HEAP", "SegContexts")?;
    let (reusable_chunks, cached_chunks) = discover_vs_evidence(
        memory,
        layout,
        heap_address,
        dynamic_lookaside,
        traversal_limit,
        &mut discovery.diagnostics,
    )?;
    let vs_skips_page_end =
        vs_skips_page_end(memory, layout, heap_address, &mut discovery.diagnostics)?;

    // Touch both LFH roots through guarded reads. Descriptor metadata remains the
    // authoritative source of active subsegments, while these probes make corrupt
    // bucket/affinity metadata an isolated diagnostic instead of a fatal read.
    if let Ok(lfh_offset) = layout.field("_SEGMENT_HEAP", "LfhContext") {
        let lfh = heap_address + lfh_offset as u64;
        for (field, label) in [
            ("Buckets", "LFH buckets"),
            ("AffinitySlots", "LFH affinity slots"),
        ] {
            if let Ok(offset) = layout.field("_HEAP_LFH_CONTEXT", field)
                && let Err(error) = guarded_read(memory, lfh + offset as u64, 8)
            {
                discovery
                    .diagnostics
                    .push(format!("cannot read {label}: {error}"));
            }
        }
    }

    for context_index in 0..2usize {
        check_budget(memory)?;
        let context_address =
            heap_address + contexts_offset as u64 + context_index as u64 * context.size as u64;
        if let Err(error) = discover_segment_context(
            memory,
            layout,
            context_address,
            numa_node,
            pool_kind,
            identity,
            heap_key,
            lfh_key,
            traversal_limit,
            &reusable_chunks,
            &cached_chunks,
            vs_skips_page_end,
            discovery,
        ) {
            // As in `discover_pool_regions`: a halt is about the walk, not about this
            // context. Absorbed here it would read as "context 1 is unreadable", and on the
            // *last* context index it would be absorbed entirely — `discover_heap_regions`
            // would carry on into `discover_large_allocations` and return `Ok`, so even a
            // Ctrl+C would only stop the walk if some later site happened to raise it again.
            if error.halts_walk() {
                return Err(error);
            }
            discovery.diagnostics.push(format!(
                "cannot discover segment context {context_index} at {context_address:#x}: {error}"
            ));
        }
    }
    discover_large_allocations(
        memory,
        layout,
        heap_address,
        numa_node,
        pool_kind,
        identity,
        heap_key,
        traversal_limit,
        discovery,
    )?;
    let _ = heap.size;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn discover_segment_context(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    context_address: u64,
    numa_node: u16,
    pool_kind: PoolKind,
    identity: HeapIdentity,
    heap_key: u64,
    lfh_key: u64,
    traversal_limit: usize,
    reusable_chunks: &SharedChunks,
    cached_chunks: &SharedChunks,
    vs_skips_page_end: bool,
    discovery: &mut Discovery,
) -> Result<(), SnapshotError> {
    let segment = layout.type_layout("_HEAP_PAGE_SEGMENT")?;
    let descriptor = layout.type_layout("_HEAP_PAGE_RANGE_DESCRIPTOR")?;
    let lfh_bitmap = layout.lfh_bitmap();
    let shift = scalar(
        memory,
        context_address + layout.field("_HEAP_SEG_CONTEXT", "UnitShift")? as u64,
        1,
    )? as u32;
    if !(12..=20).contains(&shift) {
        return Ok(());
    }
    let first_descriptor = scalar(
        memory,
        context_address + layout.field("_HEAP_SEG_CONTEXT", "FirstDescriptorIndex")? as u64,
        1,
    )? as usize;
    let segment_mask = scalar(
        memory,
        context_address + layout.field("_HEAP_SEG_CONTEXT", "SegmentMask")? as u64,
        8,
    )?;
    let segment_size = (!segment_mask).wrapping_add(1);
    let descriptor_count = (segment_size >> shift).min(4096) as usize;
    if descriptor_count == 0 || first_descriptor >= descriptor_count {
        return Ok(());
    }

    let free_tree = context_address + layout.field("_HEAP_SEG_CONTEXT", "FreePageRanges")? as u64;
    let free_nodes: HashSet<_> = tree_nodes(
        memory,
        layout,
        free_tree,
        traversal_limit,
        "free-page tree",
        &mut discovery.diagnostics,
    )?
    .into_iter()
    .collect();

    let list_head = context_address + layout.field("_HEAP_SEG_CONTEXT", "SegmentListHead")? as u64;
    let list_entry = layout.field("_HEAP_PAGE_SEGMENT", "ListEntry")?;
    let desc_array = layout.field("_HEAP_PAGE_SEGMENT", "DescArray")?;
    let signature_offset = layout.field("_HEAP_PAGE_SEGMENT", "Signature")?;
    let unit_offset = layout.field("_HEAP_PAGE_RANGE_DESCRIPTOR", "UnitSize")?;
    let flags_offset = layout.field("_HEAP_PAGE_RANGE_DESCRIPTOR", "RangeFlags")?;
    let tree_signature_offset = layout.field("_HEAP_PAGE_RANGE_DESCRIPTOR", "TreeSignature")?;
    let tree_node_offset = layout.field("_HEAP_PAGE_RANGE_DESCRIPTOR", "TreeNode")?;
    let metadata_size = descriptor_count
        .checked_mul(descriptor.size as usize)
        .ok_or_else(|| SnapshotError::InvalidData {
            detail: "descriptor metadata size overflow".into(),
        })?;
    // Plain `LIST_ENTRY` links, compared with the head exactly. They used to be masked to 16
    // bytes like a tree node's, and the head is only 8-aligned — `SegContexts` at +0x140 and
    // `SegmentListHead` at +0x48 of it, on x64 26100.8972 and ARM64 26100.1 `ntdll` alike — so
    // the last segment's link back to the head never matched it, and the walk read the heap's
    // own fields as a segment header (2026-09-23).
    let mut entry = scalar(memory, list_head, 8)?;
    let mut seen = HashSet::new();
    while entry != 0 && entry != list_head && seen.len() < traversal_limit {
        check_budget(memory)?;
        if !seen.insert(entry) {
            discovery
                .diagnostics
                .push(format!("segment-list cycle at {entry:#x}"));
            break;
        }
        let segment_address = entry.saturating_sub(list_entry as u64);
        let segment_header = match guarded_read(memory, segment_address, segment.size as usize) {
            Ok(bytes) => bytes,
            Err(error) => {
                discovery.diagnostics.push(format!(
                    "cannot read segment header {segment_address:#x}: {error}"
                ));
                match scalar(memory, entry, 8) {
                    Ok(next) => entry = next,
                    Err(_) => break,
                }
                continue;
            }
        };
        let signature = read_u64(&segment_header, signature_offset)
            .or_else(|| read_u32(&segment_header, signature_offset).map(u64::from))
            .unwrap_or(0);
        if !valid_page_segment_signature(signature, segment_address, context_address, heap_key) {
            discovery.diagnostics.push(format!(
                "rejecting page segment {segment_address:#x} with invalid signature {signature:#x}"
            ));
            entry = read_u64(&segment_header, list_entry).unwrap_or(0);
            continue;
        }
        let metadata_address = segment_address + desc_array as u64;
        let metadata = match guarded_read(memory, metadata_address, metadata_size) {
            Ok(bytes) => bytes,
            Err(error) => {
                discovery.diagnostics.push(format!(
                    "cannot read descriptors at {metadata_address:#x}: {error}"
                ));
                entry = read_u64(&segment_header, list_entry).unwrap_or(0);
                continue;
            }
        };
        let mut descriptor_index = first_descriptor;
        while descriptor_index < descriptor_count {
            // Up to 4096 descriptors per segment, and an LFH one costs a subsegment header
            // plus a bitmap read apiece. Polling only in the enclosing segment loop left ~8k
            // reads between the deadline passing and anything noticing.
            check_budget(memory)?;
            let offset = descriptor_index * descriptor.size as usize;
            let Some(decoded) = decode_descriptor_at(
                &metadata,
                offset,
                descriptor.size as usize,
                unit_offset,
                flags_offset,
            ) else {
                descriptor_index += 1;
                continue;
            };
            // `TreeSignature` shares its bytes with the range's tree node, so it only means
            // anything on the descriptor that *is* the range's node — its first — and not even
            // there once the range is free: a range in the free-page tree holds its node's links
            // in those bytes. Membership of the tree is that range's evidence, and asking it for
            // the signature as well dropped every free range there is (2026-09-23).
            let descriptor_node = metadata_address + offset as u64 + tree_node_offset as u64;
            if decoded.first
                && !free_nodes.contains(&descriptor_node)
                && !read_u32(&metadata, offset + tree_signature_offset)
                    .is_some_and(valid_descriptor_tree_signature)
            {
                discovery.diagnostics.push(format!(
                    "rejecting descriptor {descriptor_index} at {:#x} with invalid tree signature",
                    metadata_address + offset as u64
                ));
                descriptor_index += decoded.unit_size.max(1) as usize;
                continue;
            }
            let unit_size = decoded.unit_size as usize;
            let Some(address) = segment_address.checked_add((descriptor_index as u64) << shift)
            else {
                break;
            };
            let size = unit_size.checked_shl(shift).unwrap_or(0);
            if size == 0 {
                descriptor_index += unit_size.max(1);
                continue;
            }
            // Getting this wrong is expensive in the quietest way available: a range parsed as
            // the wrong kind of subsegment fails to decode and is dropped *during region
            // creation*, so its allocations never reach the walk at all and nothing downstream
            // can tell they existed. Verifier special pool is the one range whose flags cannot
            // answer the question — a special-pool page range is a plain allocated range
            // (`0x03`), but it holds a pool header plus fill laid out page-granularly, so it is
            // routed by the heap it came from. `walk_region` routes the same way.
            let backend = if identity.special {
                PoolBackend::Segment
            } else {
                descriptor_backend(decoded.flags)
            };
            let mut region_address = address;
            let mut region_size = size;
            let mut bitmap = Vec::new();
            let mut block_size = size.min(u32::MAX as usize) as u32;
            if backend == PoolBackend::Lfh {
                let subsegment = layout.type_layout("_HEAP_LFH_SUBSEGMENT")?;
                let offsets = layout.field("_HEAP_LFH_SUBSEGMENT", "BlockOffsets")?
                    + layout.field("_HEAP_LFH_SUBSEGMENT_ENCODED_OFFSETS", "EncodedData")?;
                let count_offset = layout.field("_HEAP_LFH_SUBSEGMENT", "BlockCount")?;
                let bitmap_offset = layout.field("_HEAP_LFH_SUBSEGMENT", "BlockBitmap")?;
                let header = match guarded_read(memory, address, subsegment.size as usize) {
                    Ok(bytes) => bytes,
                    Err(error) => {
                        discovery
                            .diagnostics
                            .push(format!("cannot read LFH subsegment {address:#x}: {error}"));
                        descriptor_index += unit_size;
                        continue;
                    }
                };
                let encoded = read_u32(&header, offsets).unwrap_or(0);
                let blocks = read_u16(&header, count_offset)
                    .map(usize::from)
                    .unwrap_or(0);
                let lfh = match decode_lfh_subsegment(
                    encoded,
                    address,
                    lfh_key as u32,
                    blocks,
                    region_size,
                ) {
                    Ok(lfh) => lfh,
                    Err(rejection) => {
                        discovery.diagnostics.push(format!(
                            "rejecting LFH subsegment {address:#x}: {rejection}"
                        ));
                        descriptor_index += unit_size;
                        continue;
                    }
                };
                block_size = lfh.block_size;
                let raw = match guarded_read(
                    memory,
                    address + bitmap_offset as u64,
                    lfh_bitmap.bytes_for(lfh.blocks),
                ) {
                    Ok(bytes) => bytes,
                    Err(error) => {
                        discovery
                            .diagnostics
                            .push(format!("cannot read LFH bitmap at {address:#x}: {error}"));
                        descriptor_index += unit_size;
                        continue;
                    }
                };
                bitmap = match lfh_bitmap.normalise(&raw, lfh.blocks) {
                    Some(busy) => busy,
                    None => {
                        discovery.diagnostics.push(format!(
                            "LFH bitmap at {address:#x} is {} bytes, short of its {} blocks",
                            raw.len(),
                            lfh.blocks
                        ));
                        descriptor_index += unit_size;
                        continue;
                    }
                };
                region_address += lfh.first as u64;
                region_size = lfh.blocks * block_size as usize;
            } else if backend == PoolBackend::Vs {
                let vs = layout.type_layout("_HEAP_VS_SUBSEGMENT")?;
                let header = match guarded_read(memory, address, vs.size as usize) {
                    Ok(bytes) => bytes,
                    Err(error) => {
                        discovery
                            .diagnostics
                            .push(format!("cannot read VS subsegment {address:#x}: {error}"));
                        descriptor_index += unit_size;
                        continue;
                    }
                };
                let signature =
                    read_u16(&header, layout.field("_HEAP_VS_SUBSEGMENT", "Signature")?)
                        .unwrap_or(0)
                        & 0x7fff;
                let declared =
                    read_u16(&header, layout.field("_HEAP_VS_SUBSEGMENT", "Size")?).unwrap_or(0);
                if !valid_vs_signature(signature ^ declared) {
                    discovery.diagnostics.push(format!(
                        "rejecting VS subsegment {address:#x} with invalid signature"
                    ));
                    descriptor_index += unit_size;
                    continue;
                }
                let first = (vs.size as usize).next_multiple_of(16);
                if first >= region_size {
                    descriptor_index += unit_size;
                    continue;
                }
                region_address += first as u64;
                region_size -= first;
                block_size = 0;
                // **The page range is not the subsegment, and is routinely larger.** The chunk
                // area is what `nt!RtlpHpVsSubsegmentInitialize` lays out — `Size = (bytes -
                // first) >> 4` sixteen-byte units starting at `+ first` — and the descriptor
                // sizes the *range that holds it*, which on 26100 is one unit more:
                //
                // ```text
                //   053 flags=0f UnitSize=11 UnitOffset=00   <- a VS range: 17 pages
                //   054..063     UnitSize=00 UnitOffset=01..10  <- its 16 continuation units
                //   064 flags=03 UnitSize=01                 <- and the next range begins
                // ```
                //
                // while the subsegment at 0x53000 declared Size 0xffd — 0xffd0 bytes of chunks,
                // plus `first`, is 0x10000. One spare page, every time, at both 0x10000 and
                // 0x20000 subsegment sizes.
                //
                // Bounding by the range is what raised glslang/dbgscope#103: where that spare
                // page happened to be committed, the walk decoded it, and since nothing there is
                // a chunk it refused a header every sixteen bytes to the end — 0x1000/16 = 256
                // per subsegment, against 248 measured. The remaining refusals on a live walk
                // were, to a rounding error, exactly this page.
                //
                // `declared` is preferred over the descriptor rather than merely compared with
                // it, because it is the number the chunks were laid out to *and* it is checked:
                // this subsegment is only accepted at all if `Signature ^ Size == 0x2bed`, so a
                // `Size` we resolved from the wrong offset cannot reach here. Clamped anyway —
                // it may only ever shrink the region, never point the walk outside the range it
                // was given.
                let declared_size = usize::from(declared) * 16;
                if declared_size == 0 || declared_size > region_size {
                    // Not the ordinary spare page: the subsegment is claiming no chunks at all,
                    // or more than its range can hold. Its signature checked out, so this is
                    // worth a line rather than a silent clamp.
                    discovery.diagnostics.push(format!(
                        "VS subsegment {address:#x} declares {declared_size:#x} of chunks where \
                         its page range leaves room for {region_size:#x}"
                    ));
                } else {
                    region_size = declared_size;
                }
            }
            // Two independent ways to be free, and either is enough: the range sits in the
            // segment's free-page tree, or its own flags say it is not in use.
            let state = if free_nodes.contains(&descriptor_node) || !decoded.allocated() {
                PoolState::ReusableFree
            } else {
                PoolState::Allocated
            };
            // **An allocated kernel page range with no subsegment in it is a big-pool
            // allocation, and big-pool allocations carry no `_POOL_HEADER` at all.**
            // `ExAllocatePoolWithTag` sends anything that will not fit inside a page to
            // `ExpAllocateBigPool`, which takes whole pages from this same segment allocator
            // and records the caller's tag and length in `nt!PoolBigPageTable` instead of in
            // a header — which is why `!pool` calls these "large page allocation" and reads
            // their tag out of that table rather than out of the page.
            //
            // Decoding one as though it began with a header reads the caller's *own first
            // sixteen bytes* as `PreviousSize`/`BlockSize`/`PoolType`/`PoolTag`, and then
            // reports the block as starting 0x10 in and 0x10 short. Measured on a live
            // 26100 kernel (`ctf-vm`, 2026-09-24): `ffffac09dd0f5000` is a 0x1000-byte
            // `CM25` registry hive bin, and the walk called it a 4080-byte block at
            // `+0x10` tagged `..N.` — from `hbin`'s own header bytes. Where those bytes
            // happened to be zero the tag came out `0x00000000`, which is how this
            // presented: as tens of thousands of untagged allocations (`FOLLOWUPS` item 99).
            //
            // Free ranges are deliberately not looked up: `ExpRemoveTagForBigPages` takes the
            // entry out as the allocation goes away, so a miss there would be the expected
            // answer rather than a finding.
            // Every big-pool allocation inside a VS subsegment, resolved here because the table
            // is here and a chunk walk has no reads to spare. `_POOL_HEADER.BlockSize` cannot
            // describe a chunk of `MAX_POOL_HEADER_CHUNK` or more, so `nt` gives those no header
            // at all and records them in `nt!PoolBigPageTable` — **wherever they came from**.
            // They are not only the plain page ranges below: a VS subsegment holds them too, and
            // every entry's `Va` is page-aligned, so one probe per page of the chunk area finds
            // each one. Measured on a live 26100 kernel, where `0xffffac09da29f000` is an
            // `MiRr` allocation of `0xe1c0` bytes inside the 17-page VS range at page 0x53 of
            // its segment (`windbg-mcp` FOLLOWUPS item 99).
            let mut big_pool = HashMap::new();
            if backend == PoolBackend::Vs && !identity.special {
                let mut page = region_address.next_multiple_of(PAGE_SIZE);
                let end = region_address.saturating_add(region_size as u64);
                while page < end {
                    if let Some(entry) = discovery.big_page(memory, page)? {
                        big_pool.insert(page, entry);
                    }
                    page = page.saturating_add(PAGE_SIZE);
                }
            }
            let big_pool = Arc::new(big_pool);
            let mut pool_header = layout.pool_header_layout()?;
            let mut known_tag = None;
            if backend == PoolBackend::Segment
                && !identity.special
                && state == PoolState::Allocated
                && let Some(big_page) = discovery.big_page(memory, region_address)?
            {
                known_tag = Some(big_page.tag);
                pool_header.size = 0;
                // The table holds the length the caller asked for; the range is that rounded
                // up to whole pages. Report the request, as `!pool` does, and leave the
                // rounding out of the walk rather than inflating the allocation by it.
                if big_page.size != 0 && big_page.size < region_size as u64 {
                    region_size = big_page.size as usize;
                    block_size = region_size.min(u32::MAX as usize) as u32;
                }
            }
            discovery.regions.push(PoolRegion {
                address: region_address,
                size: region_size,
                requested_size: None,
                pool_kind,
                numa_node,
                heap: identity,
                subsegment: Some(address),
                backend,
                unit_size: block_size,
                bitmap,
                heap_key,
                pool_header,
                vs_header_size: layout.type_layout("_HEAP_VS_CHUNK_HEADER")?.size as usize,
                vs_sizes_offset: layout.field("_HEAP_VS_CHUNK_HEADER", "Sizes")?,
                vs_skips_page_end,
                known_tag,
                states: vec![state],
                reusable_chunks: Arc::clone(reusable_chunks),
                cached_chunks: Arc::clone(cached_chunks),
                big_pool: Arc::clone(&big_pool),
            });
            descriptor_index += unit_size;
        }
        entry = read_u64(&segment_header, list_entry).unwrap_or(0);
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn discover_large_allocations(
    memory: &impl PoolMemory,
    layout: &PoolLayout,
    heap_address: u64,
    numa_node: u16,
    pool_kind: PoolKind,
    identity: HeapIdentity,
    heap_key: u64,
    traversal_limit: usize,
    discovery: &mut Discovery,
) -> Result<(), SnapshotError> {
    let Ok(tree_offset) = layout.field("_SEGMENT_HEAP", "LargeAllocMetadata") else {
        return Ok(());
    };
    let Ok(large) = layout.type_layout("_HEAP_LARGE_ALLOC_DATA") else {
        return Ok(());
    };
    let Ok(tree_node) = layout.field("_HEAP_LARGE_ALLOC_DATA", "TreeNode") else {
        return Ok(());
    };
    let Ok(virtual_offset) = layout.field("_HEAP_LARGE_ALLOC_DATA", "VirtualAddress") else {
        return Ok(());
    };
    let Ok(pages_offset) = layout.field("_HEAP_LARGE_ALLOC_DATA", "AllocatedPages") else {
        return Ok(());
    };
    let tree_address = heap_address + tree_offset as u64;
    let nodes = tree_nodes(
        memory,
        layout,
        tree_address,
        traversal_limit,
        "large-allocation tree",
        &mut discovery.diagnostics,
    )?;
    for node in nodes {
        // One metadata read per node, and `nodes` is bounded only by `traversal_limit`. The
        // nested poll in `lookup_big_page_target` covers only nodes that get that far; one
        // whose metadata will not read continues from here, reading and never asking.
        check_budget(memory)?;
        let allocation_address = node.saturating_sub(tree_node as u64);
        let allocation = match guarded_read(memory, allocation_address, large.size as usize) {
            Ok(bytes) => bytes,
            Err(error) => {
                discovery.diagnostics.push(format!(
                    "cannot read large-allocation metadata {allocation_address:#x}: {error}"
                ));
                continue;
            }
        };
        let Some((virtual_address, pages)) = read_u64(&allocation, virtual_offset)
            .zip(read_u64(&allocation, pages_offset))
            .and_then(|(virtual_address, pages)| {
                if layout.is_user() {
                    super::decode::decode_large_allocation_for(virtual_address, pages, true)
                } else {
                    super::decode::decode_large_allocation(virtual_address, pages)
                }
            })
        else {
            continue;
        };
        if pages > 0x10_0000 {
            discovery.diagnostics.push(format!(
                "rejecting implausible large allocation at {allocation_address:#x}"
            ));
            continue;
        }
        let bytes = pages.saturating_mul(PAGE_SIZE);
        // In the validated x64 layout `UnusedBytes` aliases the low 16 bits of the
        // `VirtualAddress` word. Only recover an exact request when the PDB confirms that
        // alias and the encoded value fits inside the allocation.
        let validated_unused_bytes = layout
            .field("_HEAP_LARGE_ALLOC_DATA", "UnusedBytes")
            .is_ok_and(|offset| offset == virtual_offset);
        let requested_size = read_u64(&allocation, virtual_offset)
            .and_then(|word| decode_large_requested_size(word, bytes, validated_unused_bytes));
        let (tag, tracked_size) = if layout.is_user() {
            (0, bytes)
        } else {
            match discovery.big_page(memory, virtual_address)? {
                Some(big_page) => (big_page.tag, big_page.size),
                None => {
                    discovery.diagnostics.push(format!(
                        "no validated big-page entry for large allocation {virtual_address:#x}"
                    ));
                    (0, bytes)
                }
            }
        };
        let size = tracked_size.min(bytes).min(usize::MAX as u64) as usize;
        let Ok(pool_header) = layout.pool_header_layout() else {
            continue;
        };
        discovery.regions.push(PoolRegion {
            address: virtual_address,
            size,
            requested_size: layout.is_user().then_some(requested_size).flatten(),
            pool_kind,
            numa_node,
            heap: identity,
            subsegment: None,
            backend: PoolBackend::Large,
            unit_size: size.min(u32::MAX as usize) as u32,
            bitmap: Vec::new(),
            heap_key,
            pool_header,
            vs_header_size: 0,
            vs_sizes_offset: 0,
            vs_skips_page_end: false,
            known_tag: Some(tag),
            states: vec![PoolState::Allocated],
            reusable_chunks: Arc::default(),
            cached_chunks: Arc::default(),
            big_pool: Arc::default(),
        });
    }
    Ok(())
}

/// How many entries one read of the big-page table covers.
///
/// Batches are cut on multiples of this rather than on wherever a probe started, so that the
/// second lookup landing anywhere inside a batch the first one read costs no read at all —
/// which is what makes asking this question of *every* allocated page range affordable. At
/// 32 bytes an entry a batch is 8 KiB, and a whole 26100 table — 0x8000 entries on a 4 GiB
/// guest — is 128 of them.
const BIG_PAGE_BATCH: usize = 256;

/// What `nt!PoolBigPageTable` records about one allocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct BigPageEntry {
    pub tag: u32,
    /// The length the caller asked for, which is *not* the length of the pages it was given:
    /// a 0xa270-byte request occupies 0xb000 of pool and this field says 0xa270.
    pub size: u64,
}

/// `nt!PoolBigPageTable`: where the kernel keeps the tag of every allocation too large to
/// carry a `_POOL_HEADER`, read as `nt` reads it and cached a batch at a time.
///
/// **Placement.** `nt!ExpAddTagForBigPages` hashes the address and linear-probes forward from
/// there, wrapping at the end of the table, claiming the first entry whose `Va` has bit 0
/// set; `nt!ExpRemoveTagForBigPages` repeats the same walk and compares `Va` against the
/// address **exactly**. Both were read on x64 26100.33438 (2026-09-24), and the hash is the
/// one [`big_page_hash`] mirrors.
///
/// **Bit 0 of `Va` is the free flag, and the address stays behind it.** A free is a
/// `lock inc` of the whole `Va` word, so a slot that held `0xffffac09e50f5000` reads
/// `0xffffac09e50f5001` afterwards and keeps its old tag and size until something else claims
/// it. Matching on `Va & !1` therefore answers for a *freed* allocation with the tag it used
/// to have — measured on the same kernel, where `ffffac09e50f5001` sat one slot from a live
/// entry and its page would not even read. The comparison here is `nt`'s: exact.
///
/// **A never-used slot is `1`, not `0`**, which is why the probe below stops at `Va <= 1`
/// rather than at zero, and why the zero test it used to have never once fired: a miss
/// scanned all 32,768 entries. Stopping at a never-used slot is sound because the allocator
/// would have claimed it — it is bit-0-set, and the probe that placed the entry being looked
/// for passed over this slot to reach wherever it went. Stopping at a *freed* slot would not
/// be sound, since it may well have been occupied when that entry was placed.
struct BigPageTable {
    base: u64,
    count: usize,
    entry_size: usize,
    va_offset: usize,
    tag_offset: usize,
    size_offset: usize,
    /// One slot per batch, filled on first touch. A walk that asks about nothing reads
    /// nothing; one that asks about thousands of ranges converges on the whole table.
    batches: Vec<Option<Vec<u8>>>,
}

impl BigPageTable {
    /// Resolve the table, or `None` when this target has no such thing to read — a user-mode
    /// Segment Heap, or a kernel whose symbols do not carry the tracker type or the two
    /// globals. `None` is not an error: the caller falls back to whatever the page itself
    /// says, which is what a build without the table leaves it with.
    fn open(
        memory: &impl PoolMemory,
        layout: &PoolLayout,
        diagnostics: &mut Vec<String>,
    ) -> Result<Option<Self>, SnapshotError> {
        let (Ok(entry), Ok(va_offset), Ok(tag_offset), Ok(size_offset)) = (
            layout.type_layout("_POOL_TRACKER_BIG_PAGES"),
            layout.field("_POOL_TRACKER_BIG_PAGES", "Va"),
            layout.field("_POOL_TRACKER_BIG_PAGES", "Key"),
            layout.field("_POOL_TRACKER_BIG_PAGES", "NumberOfBytes"),
        ) else {
            return Ok(None);
        };
        let (Some(&table_pointer_address), Some(&size_address)) = (
            layout.globals.get("PoolBigPageTable"),
            layout.globals.get("PoolBigPageTableSize"),
        ) else {
            return Ok(None);
        };
        // Two reads of its own, so they are polled for like any others: this runs at the head
        // of discovery, where a budget already spent on resolving the heaps would otherwise
        // buy two more reads before anything asked.
        check_budget(memory)?;
        let base = match scalar(memory, table_pointer_address, 8) {
            Ok(value) => value,
            Err(error) => {
                diagnostics.push(format!("cannot read big-page table pointer: {error}"));
                return Ok(None);
            }
        };
        let count =
            match scalar(memory, size_address, 4).or_else(|_| scalar(memory, size_address, 8)) {
                Ok(value) => value as usize,
                Err(error) => {
                    diagnostics.push(format!("cannot read big-page table size: {error}"));
                    return Ok(None);
                }
            };
        if base == 0 || count == 0 || count > 0x10_0000 || !count.is_power_of_two() {
            diagnostics.push(format!("rejecting implausible big-page table size {count}"));
            return Ok(None);
        }
        Ok(Some(Self {
            base,
            count,
            entry_size: entry.size as usize,
            va_offset,
            tag_offset,
            size_offset,
            batches: vec![None; count.div_ceil(BIG_PAGE_BATCH)],
        }))
    }

    /// The entry for `address`, or `None` when the table does not name it.
    fn lookup(
        &mut self,
        memory: &impl PoolMemory,
        address: u64,
        diagnostics: &mut Vec<String>,
    ) -> Result<Option<BigPageEntry>, SnapshotError> {
        let mut index =
            big_page_hash(address, self.count).ok_or_else(|| SnapshotError::InvalidData {
                detail: format!("invalid big-page table size {}", self.count),
            })?;
        for _ in 0..self.count {
            let entry_size = self.entry_size;
            let offset = (index % BIG_PAGE_BATCH) * entry_size;
            let (va_offset, tag_offset, size_offset) =
                (self.va_offset, self.tag_offset, self.size_offset);
            let Some(bytes) = self.batch(memory, index / BIG_PAGE_BATCH, diagnostics)? else {
                return Ok(None);
            };
            let Some(candidate) = read_u64(bytes, offset + va_offset) else {
                // The batch is short of this entry: either the read failed, or the table
                // runs past what the target will give us. Either way the chain ends here.
                return Ok(None);
            };
            // Never used, so the allocator would have put our entry here rather than past it.
            if candidate <= 1 {
                return Ok(None);
            }
            if candidate == address {
                let (Some(tag), Some(size)) = (
                    read_u32(bytes, offset + tag_offset),
                    read_u64(bytes, offset + size_offset),
                ) else {
                    diagnostics.push(format!("truncated big-page entry {index}"));
                    return Ok(None);
                };
                return Ok(Some(BigPageEntry { tag, size }));
            }
            index = (index + 1) % self.count;
        }
        Ok(None)
    }

    /// One batch of entries, read on first touch and kept.
    fn batch(
        &mut self,
        memory: &impl PoolMemory,
        batch: usize,
        diagnostics: &mut Vec<String>,
    ) -> Result<Option<&[u8]>, SnapshotError> {
        if self.batches.get(batch).is_none_or(Option::is_some) {
            return Ok(self.batches.get(batch).and_then(Option::as_deref));
        }
        check_budget(memory)?;
        let first = batch * BIG_PAGE_BATCH;
        let len = BIG_PAGE_BATCH.min(self.count - first);
        let (Some(byte_len), Some(entry_address)) = (
            self.entry_size.checked_mul(len),
            self.base.checked_add(first as u64 * self.entry_size as u64),
        ) else {
            return Err(SnapshotError::InvalidData {
                detail: "big-page batch address overflow".into(),
            });
        };
        match guarded_read(memory, entry_address, byte_len) {
            Ok(bytes) => self.batches[batch] = Some(bytes),
            Err(error) => {
                diagnostics.push(format!(
                    "cannot read big-page entries {first}..{} at {entry_address:#x}: {error}",
                    first + len
                ));
                // An empty batch is still a batch: it is cached so that the next thousand
                // allocations hashing into it do not each pay for the same failed read.
                self.batches[batch] = Some(Vec::new());
            }
        }
        Ok(self.batches[batch].as_deref())
    }
}

/// How many verbatim examples of one kind of diagnostic to keep.
pub const DIAGNOSTIC_EXAMPLES: usize = 8;

/// The shape of a diagnostic: the message with every number standing in for itself.
///
/// "unreadable VS free tree node 0xffffc00f6ec02f90" and the same complaint about a
/// different node share a shape; a genuinely different complaint does not.
fn diagnostic_shape(message: &str) -> String {
    message
        .split_whitespace()
        .map(|token| {
            if token.contains(|character: char| character.is_ascii_digit()) {
                "#"
            } else {
                token
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// One kind of complaint, and how many times the walk made it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiagnosticShape {
    /// The message with every number standing in for itself, so that the same complaint
    /// about different addresses is one shape and a different complaint is not.
    pub shape: String,
    /// How many messages of this shape the walk emitted, including any kept verbatim in
    /// [`PoolDiagnostics::examples`].
    pub total: usize,
}

/// What a walk complained about: a bounded verbatim sample, and the per-shape totals that
/// say what the sample left out.
///
/// A live target keeps allocating between the reads that make up one walk, so a single
/// stale list pointer yields one "unreadable ... node" line per node — 14k of them measured
/// on a busy kernel. Keeping them all buries the handful of *distinct* problems a reader
/// needs, and every consumer truncates the list anyway, so which ones survive gets decided
/// by position rather than by significance. Hence the cap of [`DIAGNOSTIC_EXAMPLES`] kept
/// messages per shape.
///
/// The totals are why this is a type rather than a `Vec<String>`. Flattened to text they
/// survive only as prose inside a summary line, and a consumer that counts the lines it
/// received is measuring this cap — then printing the answer as a property of the target.
/// A walk that emitted 7,700 complaints reporting "71 diagnostics" is the specific failure
/// the module exists to avoid, so the numbers travel as numbers.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PoolDiagnostics {
    examples: Vec<String>,
    shapes: Vec<DiagnosticShape>,
    /// Where each seen shape sits in `shapes`. Derived from it, not state of its own.
    positions: HashMap<String, usize>,
}

impl PoolDiagnostics {
    /// Records one complaint, keeping it verbatim only while its shape has room.
    pub(crate) fn push(&mut self, message: String) {
        let shape = diagnostic_shape(&message);
        let position = match self.positions.get(&shape) {
            Some(&position) => position,
            None => {
                self.shapes.push(DiagnosticShape {
                    shape: shape.clone(),
                    total: 0,
                });
                let position = self.shapes.len() - 1;
                self.positions.insert(shape, position);
                position
            }
        };
        let seen = &mut self.shapes[position];
        seen.total += 1;
        if seen.total <= DIAGNOSTIC_EXAMPLES {
            self.examples.push(message);
        }
    }

    /// Whether the walk complained at all.
    pub fn is_empty(&self) -> bool {
        self.shapes.is_empty()
    }

    /// How many messages the walk emitted, the ones collapsed away included.
    ///
    /// This is the number that describes the *walk*; `examples().len()` and `lines().len()`
    /// describe this struct, and on a busy target the two differ by two orders of magnitude.
    pub fn emitted(&self) -> usize {
        self.shapes.iter().map(|seen| seen.total).sum()
    }

    /// The messages kept verbatim, in the order the walk emitted them.
    ///
    /// Emission order rather than by shape because heaps are walked in order, so the tail is
    /// the most recently discovered ones — which is what a caller printing "the last few" is
    /// reaching for.
    pub fn examples(&self) -> &[String] {
        &self.examples
    }

    /// The shapes, in the order they were first seen.
    pub fn shapes(&self) -> &[DiagnosticShape] {
        &self.shapes
    }

    /// The whole thing as text: every kept example, then one summary line per shape that had
    /// more than the cap.
    ///
    /// Summaries go at the end, in the order the shapes first appeared, rather than
    /// interleaved where a shape's last kept member happened to land. For display only —
    /// [`Self::emitted`] is the count, and the length of this is not.
    pub fn lines(&self) -> Vec<String> {
        let mut lines = self.examples.clone();
        for seen in &self.shapes {
            if let Some(collapsed) = seen
                .total
                .checked_sub(DIAGNOSTIC_EXAMPLES)
                .filter(|more| *more > 0)
            {
                lines.push(format!("... and {collapsed} more like `{}`", seen.shape));
            }
        }
        lines
    }
}

impl Extend<String> for PoolDiagnostics {
    fn extend<T: IntoIterator<Item = String>>(&mut self, messages: T) {
        for message in messages {
            self.push(message);
        }
    }
}

impl FromIterator<String> for PoolDiagnostics {
    fn from_iter<T: IntoIterator<Item = String>>(messages: T) -> Self {
        let mut diagnostics = Self::default();
        diagnostics.extend(messages);
        diagnostics
    }
}

#[derive(Debug, Clone, Default)]
pub(crate) struct PoolSnapshot {
    pub layout: crate::allocator::LayoutProvenance,
    pub spans: Vec<PoolSpan>,
    pub diagnostics: PoolDiagnostics,
    pub complete: bool,
    /// Whether what stopped this walk short was its **deadline** rather than anything it found.
    ///
    /// `complete` says a walk did not cover the pool; this says why, and the two answers need
    /// opposite responses. A walk that ran out of time reaches more of the pool if it is given
    /// more of it; one that hit unreadable regions or a traversal cap will report the same gaps
    /// however long it runs. Only the walk itself can tell them apart — by the time a caller has
    /// the snapshot, both look like a short list — so the reason travels as a field rather than
    /// being inferred downstream from the diagnostic it also writes.
    pub budget_expired: bool,
    /// The match threshold that intentionally ended this walk, when one fired.
    ///
    /// This is separate from `budget_expired` and from diagnostics because all three make the
    /// snapshot partial for different reasons. A caller that asked only whether a tag exists has
    /// its answer when this is present; a caller seeing either of the others does not.
    pub stopped_after_matches: Option<usize>,
    /// Query-specific stop policy for a snapshot currently being built. It never survives into
    /// the cache: a walk that fires it clears `complete`, and the cache stores complete snapshots
    /// only.
    pub match_limit: Option<PoolMatchLimit>,
    pub matched_allocations: usize,
    /// What the walk stepped over rather than gave up on; see [`WalkStalls`].
    pub stalls: WalkStalls,
    /// Chunk headers a backend decoder refused and resynchronised past, across the whole walk.
    ///
    /// A count, because the diagnostic that names them cannot be one. `PoolDiagnostics`
    /// collapses messages by shape, and a refusal is reported once per extent — so the figure
    /// beside that line counts *extents that contained a refusal*, which reads like a chunk
    /// count and is not one. On the walk that raised glslang/dbgscope#93 the difference was
    /// between "884" and a number nothing recorded.
    ///
    /// Only `walk_vs` feeds it today; LFH subsegments are refused during discovery, one per
    /// subsegment, where the count and the message already agree.
    pub refused_chunks: u64,
    /// Committed bytes of a VS subsegment the walk declined to decode, because it could not say
    /// where a chunk began in them.
    ///
    /// The cost of [`SnapshotWalker::walk_vs`] refusing to guess, and the number that keeps that
    /// refusal honest: what it buys is that nothing fabricated enters `spans`, and what it costs
    /// is coverage, which is invisible unless it is sized. A walk that reports no refusals *and*
    /// no unplaced bytes decoded every committed byte of every subsegment it reached; one that
    /// reports a large figure here has lost the chunk chain somewhere, and the chain is the only
    /// thing that can find a variable-size header.
    pub unplaced_bytes: u64,
}

/// An explicitly partial tag query: stop after this many matching allocated chunks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PoolMatchLimit {
    raw_tag: u32,
    /// `Some(true)` means paged only, `Some(false)` nonpaged only, and `None` means both.
    paged: Option<bool>,
    matches: NonZeroUsize,
}

impl PoolMatchLimit {
    pub(crate) fn new(raw_tag: u32, paged: Option<bool>, matches: NonZeroUsize) -> Self {
        Self {
            raw_tag,
            paged,
            matches,
        }
    }

    fn accepts(self, span: &PoolSpan) -> bool {
        span.state == PoolState::Allocated
            && span.raw_tag == self.raw_tag
            && self
                .paged
                .is_none_or(|paged| span.pool_kind.is_paged() == paged)
    }
}

impl PoolSnapshot {
    fn record_span(&mut self, span: PoolSpan) {
        let matched = self.match_limit.is_some_and(|limit| limit.accepts(&span));
        self.spans.push(span);
        if !matched {
            return;
        }
        self.matched_allocations = self.matched_allocations.saturating_add(1);
        let Some(limit) = self.match_limit else {
            return;
        };
        if self.matched_allocations >= limit.matches.get() {
            self.complete = false;
            self.stopped_after_matches = Some(limit.matches.get());
        }
    }

    fn match_limit_reached(&self) -> bool {
        self.stopped_after_matches.is_some()
    }
}

/// What valid-region queries that could not advance cost the walk, and what stepping over them
/// bought.
///
/// Numbers rather than another diagnostic, because a diagnostic can only say how *often* the
/// walk stalled and that was never the question. A walk that steps over one page and a walk
/// that abandons everything behind it emit the same line the same number of times; what
/// separates them is how much memory the decision covered — which the walk knows at the moment
/// it decides, and used to throw away.
///
/// `recovered_bytes` is what the change is judged by: committed memory read *after* a stall in
/// the same region, which is precisely the coverage a walk that gave up at the first stall
/// reported as nothing at all.
///
/// **On live 26100 it has measured zero** — 1,619 stalls, 6,627,520 bytes stepped over, nothing
/// read behind any of them (glslang/dbgscope#104). That is the number saying what it says, not a
/// counter that was never wired up: `stalled_here` latches for the rest of the region and any
/// later extent adds to `recovered_bytes`, which
/// [`SnapshotWalker::walk_region`]'s own test pins at two pages. So on that target every stall
/// sits at the end of its region's readable content, and the page-stepping buys nothing there.
/// It is kept because it is bounded — at most [`MAX_CONSECUTIVE_STALLS`] queries per dead region
/// — and because the failure it replaced was losing every committed page behind one bad one. The
/// diagnostic now carries the engine's own answer at each stall, which is what a later run needs
/// to decide whether stepping can be replaced by stopping.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct WalkStalls {
    /// How many times a query could not advance and the walk stepped over a page.
    pub pages: u64,
    /// Bytes filed as unreadable by those steps.
    pub skipped_bytes: u64,
    /// Bytes of committed memory read after a stall, in the regions that stalled.
    pub recovered_bytes: u64,
}

/// What one committed extent of a VS subsegment hands the next.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct VsResume {
    /// Where the chain names the next header — past this extent when the last chunk ran
    /// over its end, which is how the chain crosses a hole at all.
    next: u64,
    /// How far the spans filed in this extent reach, when the last of them runs past its
    /// end: a big-pool chunk the table named, emitted whole although its tail is in the hole
    /// ahead. Bytes below this were accounted for, and the extent loop must not file them as
    /// a gap on its way to the next readable extent — a gap span over a chunk the index
    /// already holds would answer for its addresses in the gap's place. At most `next`.
    covered_through: u64,
}

pub(crate) struct SnapshotWalker<'a, M> {
    pub memory: &'a M,
    pub layout: &'a PoolLayout,
    pub traversal_limit: usize,
}

/// Where a special-pool block sits, and whether that could be corroborated.
struct SpecialPlacement {
    usable: u64,
    size: u64,
    /// The size could not be confirmed, so `size` is a conservative upper bound covering
    /// the rest of the page rather than the allocation's real length. Callers must not
    /// treat it as a measurement — it inflates byte totals if they do.
    approximate: bool,
}

/// Where a special-pool block sits inside its page, and whether the page corroborates it.
///
/// The block is butted against the page end at `page + PAGE_SIZE - align_up(size, 16)`; the
/// 16-byte rounding matters, since a plain `PAGE_SIZE - size` lands 8 bytes past the real
/// block for a 0x68 allocation.
///
/// The size arrives already decoded, from the thirteen bits the kernel keeps it in — see
/// [`decode_special_pool_header`]. That is what makes this a corroboration rather than the
/// load-bearing check it used to be: read as `_POOL_HEADER.PreviousSize`'s eight bits, a 0x140
/// allocation reported 0x40 and nothing but the fill could tell that from a genuine 0x40. A
/// value that cannot wrap has no such twin, so a page that fails the check below is a page we
/// misread or one being written as we read it, not an ambiguity to resolve.
///
/// Both runs of fill the kernel checks on free are checked here, against the fill byte the page
/// itself records: from the header's end up to the block, and from the block's end to the page
/// end. The second is the alignment padding — up to 15 bytes, and evidence the old check left
/// on the floor.
///
/// It declines rather than lies in the two cases that break the placement rule: a page read
/// only in part (nothing corroborates the tail), and a start-aligned page
/// (`nt!MmSpecialPoolCatchOverruns` clear), where the block sits against the *preceding* guard
/// page instead.
///
/// The fallback reports the whole page after the header. An over-broad chunk still answers
/// "was this freed, and which tag owned it"; a confidently wrong offset does not.
fn special_pool_placement(
    page: u64,
    header: SpecialPoolHeader,
    page_bytes: &[u8],
) -> SpecialPlacement {
    let available = page_bytes.len() as u64;
    let header_size = header.header_size as u64;
    // Both hold by construction — `decode_special_pool_header` returns `None` otherwise — so
    // the slicing below cannot be out of range once the page is known to be whole.
    let aligned = u64::from(header.requested).next_multiple_of(16);
    let start = (PAGE_SIZE - aligned) as usize;
    if available == PAGE_SIZE {
        let leading = &page_bytes[header.header_size..start];
        let padding = &page_bytes[start + header.requested as usize..];
        if leading
            .iter()
            .chain(padding)
            .all(|byte| *byte == header.fill)
        {
            return SpecialPlacement {
                usable: page + PAGE_SIZE - aligned,
                size: u64::from(header.requested),
                approximate: false,
            };
        }
    }
    SpecialPlacement {
        usable: page + header_size,
        size: available.saturating_sub(header_size),
        approximate: true,
    }
}

/// The share of the budget discovery may spend before the region walk claims the rest.
///
/// The two halves are useless apart: discovery finds the regions, the walk reads what is in
/// them, and a snapshot with neither half finished is an empty pool. Discovery is also the
/// pointer-chasing half — one read per free-tree node, per list entry, per subsegment — so
/// on a live link it is the half that runs long, and left unchecked it would spend the whole
/// budget and leave nothing to walk. Holding it to two thirds means a walk that runs out of
/// time still reports the chunks in the regions it did reach, which is the answer a caller
/// asked a pool query for.
const DISCOVERY_BUDGET_SHARE: (u32, u32) = (2, 3);

/// How much of one committed extent to ask for per debugger read.
///
/// Sized so a chunk is a fraction of a second even on a slow KD link — small enough that the
/// deadline is observed at a useful granularity, large enough that a megabyte-scale extent is
/// still a handful of transfers rather than hundreds. Extents at or below this are read whole,
/// so the ordinary small region pays nothing for this.
///
/// **This sets the residual overshoot, and that is the guarantee.** Checks sit *before* reads,
/// never after, so a walk can finish one transfer — at most this many bytes — past its
/// ceiling. Checking after a successful read would not improve on that: mid-walk the next
/// iteration's check fires anyway, so the only case it would change is the one where the walk
/// has just finished everything, and there it would clear `complete` and announce that "what
/// is missing is unknown, not absent" about a walk with nothing missing. Manufacturing a false
/// incompleteness is the same class of lie as reporting an unread region as empty, reached
/// from the other side. The budget is a ceiling on effort, not a promise about return latency:
/// it exists so the engine cannot run for *minutes* after its caller has given up.
const EXTENT_READ_CHUNK: usize = 256 * 1024;

/// How many pages in a row a region may refuse to advance over before the walk gives up on
/// the rest of it.
///
/// Every one of these is a debugger round trip, and on a live KD link a round trip is the
/// expensive unit — so this is not sized for how large a hole might be, but for how much
/// probing a *dead* region is worth before its remainder is written off. Eight covers the
/// shape the live samples actually show, a single unreadable page part-way through a
/// committed region, with room to spare; a region that is dead all the way down costs eight
/// queries rather than one per page of its length.
const MAX_CONSECUTIVE_STALLS: u32 = 8;

/// Splits a walk budget into the deadline discovery works to and the one the whole walk
/// works to. `None` in gives `None` out for both: no budget, no deadlines.
///
/// A budget too large for `Instant` to represent — `PoolWalk::within` takes any `Duration`,
/// so a caller can hand over `Duration::MAX` — also yields `None` for both. Adding it would
/// panic, and "longer than this machine can measure" is a request to run unbounded, so
/// answering it that way is the caller's own meaning rather than a substitution for it.
///
/// Both halves stand or fall together, keyed on the whole-walk deadline. Deciding them
/// independently would pass `Duration::MAX` as a *bounded* discovery share (two thirds of it
/// still fits) against an unbounded walk — a state with no meaning behind it.
fn budget_deadlines(
    start: Instant,
    budget: Option<Duration>,
) -> (Option<Instant>, Option<Instant>) {
    let (numerator, denominator) = DISCOVERY_BUDGET_SHARE;
    let Some(whole) = budget.and_then(|full| start.checked_add(full)) else {
        return (None, None);
    };
    (
        budget.and_then(|full| start.checked_add(full / denominator * numerator)),
        Some(whole),
    )
}

impl<'a, M: PoolMemory> SnapshotWalker<'a, M> {
    /// Walks the pool, giving up after `budget` rather than running as long as it takes.
    ///
    /// `None` runs to completion. That is right only where something *else* can stop the
    /// walk — an operator's Ctrl+C at a WinDbg prompt — and wrong for a programmatic caller,
    /// where nothing sets that flag; see [`Budgeted`].
    ///
    /// Running out of time is **not** an error. It comes back as a snapshot that says what
    /// it reached, with `complete` cleared and a diagnostic naming the budget, because a
    /// partial walk still answers "here is a chunk carrying that tag" — and the alternative,
    /// an error, discards minutes of work to say nothing at all. A Ctrl+C is the opposite
    /// and still errors: an operator who interrupts is asking for the walk to stop, not for
    /// whatever it happened to have.
    pub(crate) fn walk(&self, budget: Option<Duration>) -> Result<PoolSnapshot, SnapshotError> {
        self.walk_until(budget, None)
    }

    /// Walk with an optional query-specific match threshold.
    pub(crate) fn walk_until(
        &self,
        budget: Option<Duration>,
        match_limit: Option<PoolMatchLimit>,
    ) -> Result<PoolSnapshot, SnapshotError> {
        let (discovery_deadline, walk_deadline) = budget_deadlines(Instant::now(), budget);
        let mut snapshot = PoolSnapshot {
            diagnostics: PoolDiagnostics::from_iter([
                "per-session paged heaps are not included".to_string()
            ]),
            complete: true,
            match_limit,
            ..PoolSnapshot::default()
        };

        let discovery_clock = Budgeted::new(self.memory, discovery_deadline);
        let mut discovery = Discovery::default();
        let expired = match discover_pool_regions(
            &discovery_clock,
            self.layout,
            self.traversal_limit,
            &mut discovery,
        ) {
            Ok(()) => false,
            Err(SnapshotError::BudgetExpired) => true,
            Err(error) => return Err(error),
        };
        if !discovery.diagnostics.is_empty() {
            snapshot.complete = false;
        }
        snapshot
            .diagnostics
            .extend(std::mem::take(&mut discovery.diagnostics));

        // The full deadline, so regions found before discovery ran out of its share still get
        // walked with the time discovery did not use.
        let walk_clock = Budgeted::new(self.memory, walk_deadline);
        let walker = SnapshotWalker {
            memory: &walk_clock,
            layout: self.layout,
            traversal_limit: self.traversal_limit,
        };
        walker.walk_discovered_regions(discovery.regions, budget, expired, &mut snapshot)?;
        Ok(snapshot)
    }

    /// Walk every discovered region and record how much of the discovery was covered.
    fn walk_discovered_regions(
        &self,
        regions: Vec<PoolRegion>,
        budget: Option<Duration>,
        mut expired: bool,
        snapshot: &mut PoolSnapshot,
    ) -> Result<(), SnapshotError> {
        let discovered = regions.len();
        let mut walked = 0usize;
        for region in regions {
            let outcome = if region.backend == PoolBackend::Large {
                // No reads of its own: the span comes from metadata discovery already did.
                self.walk_large(&region, snapshot);
                Ok(())
            } else {
                self.walk_region(&region, snapshot)
            };
            match outcome {
                // Counted only when the region was walked *through*. A region abandoned
                // part-way is exactly what the caveat below is for.
                Ok(()) if snapshot.match_limit_reached() => break,
                Ok(()) => walked += 1,
                Err(SnapshotError::BudgetExpired) => {
                    expired = true;
                    break;
                }
                Err(error) => return Err(error),
            }
        }

        if expired {
            snapshot.complete = false;
            snapshot.budget_expired = true;
            let allowed = match budget {
                Some(budget) => format!("{budget:?} budget"),
                None => "walk budget".to_string(),
            };
            snapshot.diagnostics.push(format!(
                "the walk ran out of its {allowed}: {walked} of {discovered} discovered regions \
                 were walked, and region discovery itself may not have finished. What is \
                 reported was really there; what is missing is unknown, not absent. Allow a \
                 longer budget for full coverage."
            ));
        }
        snapshot
            .spans
            .sort_by_key(|span| (span.heap, span.usable_address));
        Ok(())
    }

    /// Reads one region and decodes its chunks, stopping if the walk's time runs out.
    ///
    /// Checked per *extent*, not just per region: a region the allocator left with holes
    /// costs one `valid_region` and one `read_exact` per committed run, so a fragmented
    /// region is many round trips and checking only on the way in would let one region
    /// overrun the deadline by an unbounded amount. The backend decoders below need no such
    /// check — they run over bytes already in hand, off the wire entirely.
    fn walk_region(
        &self,
        region: &PoolRegion,
        snapshot: &mut PoolSnapshot,
    ) -> Result<(), SnapshotError> {
        // **A page range `nt!PoolBigPageTable` names needs no reading at all.** It is one
        // allocation with no header: discovery took its tag and length from the table, and
        // nothing on its pages adds to that — `walk_page_ranges` emits the one span and skips
        // the header decode. Reading the pages anyway cost two things. On a live 29671 kernel
        // (lab, 2026-10-07) the live entries summed to 111 MB, every byte of it read and then
        // discarded, over a link that moves a few MB a second. And on a page the memory
        // manager had trimmed the read failed, the range was filed as an unreadable gap with
        // no tag, and the name the table had for it went unused: `!pool` called
        // 0xffffa4b05e1b5000 an 8 KB `Gcac` and this walk called it `....` (`windbg-mcp`
        // FOLLOWUPS item 99, the paged-out half). Resident or not, the table's answer is the
        // same, so it is given without asking the pages.
        if region.backend == PoolBackend::Segment
            && let Some(tag) = region.known_tag
        {
            let state = region
                .states
                .first()
                .copied()
                .unwrap_or(PoolState::Allocated);
            snapshot.record_span(self.base_span(
                region,
                region.address,
                region.address,
                region.size as u64,
                tag,
                state,
            ));
            return Ok(());
        }
        let requested_end = region.address.saturating_add(region.size as u64);
        let mut cursor = region.address;
        let mut consecutive_stalls = 0u32;
        let mut stalled_here = false;
        // Where the next VS chunk header is, carried across the committed extents of one
        // subsegment. `walk_lfh` and `walk_page_ranges` need no such thing — their slots are a
        // fixed size, so they index off the region base and are correct wherever an extent starts.
        // A VS chunk is only findable from the end of the one before it, which is a fact about
        // the region and not about the extent, so it has to live out here.
        let mut vs_chunk = Some(region.address);
        // How far the VS spans already filed reach past the extent that filed them; see
        // [`VsResume::covered_through`]. A high-water mark, because a chunk that crosses one
        // hole may cross the next as well.
        let mut vs_covered = region.address;
        while cursor < requested_end {
            check_budget(self.memory)?;
            let remaining = requested_end.saturating_sub(cursor).min(usize::MAX as u64) as usize;
            let (reported_base, reported_size) = match self.memory.valid_region(cursor, remaining) {
                Ok(valid) => valid,
                Err(error) => {
                    snapshot.diagnostics.push(format!(
                        "cannot query region {cursor:#x}+{remaining:#x}: {error}"
                    ));
                    self.unreadable(region, cursor, requested_end - cursor, snapshot);
                    break;
                }
            };
            let valid_base = reported_base.max(cursor).min(requested_end);
            // Reported only when some of that space is memory the target **has** — which,
            // before `committed_run` could be asked, was the only reading available. On a
            // healthy live user-mode heap the whole of it is reserved subsegment tails, and
            // saying "unreadable space extends" about address space holding nothing is a
            // diagnostic that both describes nothing wrong and is wrong about what it
            // describes. Emitting it after the spans rather than before them is what lets it
            // be conditional at all: only filing them establishes which kind this is.
            // Less whatever a span already covers: a VS chunk the big-page table named can
            // end inside this hole, and its span was filed from the table with nothing left
            // to learn from the pages.
            let gap_start = cursor.max(vs_covered).min(valid_base);
            if valid_base > gap_start
                && self.unreadable(region, gap_start, valid_base - gap_start, snapshot)
            {
                snapshot.diagnostics.push(format!(
                    "region {:#x}+{:#x} is only committed through {gap_start:#x}; unreadable space extends {:#x} bytes",
                    region.address,
                    region.size,
                    valid_base - gap_start
                ));
            }
            let valid_end = reported_base
                .saturating_add(reported_size as u64)
                .min(requested_end);
            if valid_end <= valid_base {
                // Two states used to arrive here as one, and only the second is a problem.
                //
                // The engine placing the next valid region beyond the end of what was asked
                // about is the region *finishing*: its tail has already been filed as
                // unreadable by the branch above, there is nothing behind it to lose, and the
                // loop is done. Reporting that as a failure to advance is what made this the
                // walk's largest diagnostic category — 3,285 lines on a live 26100 walk —
                // while describing nothing wrong.
                if valid_base >= requested_end {
                    break;
                }
                // The same answer in the engine's other encoding, and the same response. A query
                // that finds nothing valid in the span it was given may say so either by naming a
                // base past the end — handled just above — or by answering `0x0+0x0`, which is
                // not a region at zero but the absence of one. Either way the engine has already
                // reported on every byte from here to `requested_end`, so stepping a page and
                // asking again asks a question that has been answered.
                //
                // Measured on live 26100 (glslang/dbgscope#104): every stall sample carried
                // `0x0+0x0`, eight of them consecutive — one dead region stepping itself into the
                // give-up bound, eight KD round trips to learn what the first answer said. Across
                // the walk that was ~1,100 stalled pages, and `recovered_bytes` was zero on four
                // separate runs, which is what this predicts and what it costs nothing to keep
                // reporting.
                //
                // Quiet, like its sibling above and for the same reason: a region running out of
                // readable content is not a fault, and #94 was raised because saying so 3,285
                // times drowned out the diagnostics that meant something. The bytes are still
                // filed as unreadable and `complete` still clears — the tail has not been walked,
                // and nothing here claims otherwise.
                if reported_base == 0 && reported_size == 0 {
                    let gap_start = valid_base.max(vs_covered).min(requested_end);
                    self.unreadable(region, gap_start, requested_end - gap_start, snapshot);
                    break;
                }
                // The rest is a real stall: a valid region was reported inside the span and it
                // ends at or before where the cursor already is. Step over the page it stalled
                // on and ask again, rather than giving up on every committed page behind it —
                // the samples show these firing part-way through regions of 0x10fd0 and 0x3afc0
                // bytes, not at their tails.
                //
                // To the next page **boundary**, not a page's worth of bytes: a region begins
                // wherever the allocator put it (`…de6030`, `…e02130` in those same samples),
                // so a stall at a page offset of 0xfd0 would otherwise write off 0x30 bytes of
                // the page that stalled and 0xfd0 bytes of the page after it — discarding a
                // healthy page to skip a bad one, which is this whole branch's mistake in
                // miniature. Every stall after the first is aligned by construction.
                //
                // The advance is **unconditional**: the boundary is always past `valid_base`,
                // and `requested_end` is too — the check above returned otherwise — so this
                // cannot be zero. A query that keeps answering the same way would spin here
                // without end, which is the reason this used to abandon the region outright.
                let page_end = (valid_base & !(PAGE_SIZE - 1)).saturating_add(PAGE_SIZE);
                let skip = page_end.min(requested_end) - valid_base;
                // The engine's own answer travels with the complaint, because the two shapes
                // that arrive here need opposite fixes and nothing else can tell them apart: a
                // region reported *behind* the cursor (`reported_base` below `valid_base`) means
                // the query is answering about memory already walked, while a zero-length region
                // reported ahead of it means the engine found something and could not size it.
                // `PoolDiagnostics` folds numbers into `#`, so this stays one shape however many
                // times it fires, and the verbatim sample carries the real values.
                snapshot.diagnostics.push(format!(
                    "valid-region query made no progress at {valid_base:#x}: the engine answered \
                     {reported_base:#x}+{reported_size:#x}; stepping over the rest of the page"
                ));
                let gap_start = valid_base.max(vs_covered).min(valid_base + skip);
                self.unreadable(region, gap_start, valid_base + skip - gap_start, snapshot);
                snapshot.stalls.pages += 1;
                snapshot.stalls.skipped_bytes = snapshot.stalls.skipped_bytes.saturating_add(skip);
                stalled_here = true;
                consecutive_stalls += 1;
                cursor = valid_base.saturating_add(skip);
                // Bounded, so a region that is dead all the way down still costs a fixed
                // number of queries rather than one per page of its length. Consecutive:
                // the single unreadable page in the middle of a live region — the shape these
                // samples actually have — resets it and costs one extra query.
                //
                // `>=`, so the count is the number of queries the limit permits and not one
                // short of it: the check runs after the page it counts has been stepped over.
                if consecutive_stalls >= MAX_CONSECUTIVE_STALLS {
                    snapshot.diagnostics.push(format!(
                        "region {:#x}+{:#x}: giving up after consecutive pages that would not advance",
                        region.address, region.size
                    ));
                    self.unreadable(region, cursor, requested_end - cursor, snapshot);
                    break;
                }
                continue;
            }
            consecutive_stalls = 0;
            let bytes = match self.read_extent(valid_base, (valid_end - valid_base) as usize) {
                Ok(bytes) => bytes,
                // Out of time is not "this memory would not read". Left to the arm below it
                // would be filed as an unreadable span — the walk asserting a fact about the
                // target out of its own failure to look.
                Err(error) if error.halts_walk() => return Err(error),
                Err(error) => {
                    snapshot
                        .diagnostics
                        .push(format!("cannot read region {valid_base:#x}: {error}"));
                    self.unreadable(region, valid_base, valid_end - valid_base, snapshot);
                    cursor = valid_end;
                    continue;
                }
            };
            if stalled_here {
                // Committed memory read on the far side of a stall — the coverage the old
                // `break` discarded, counted so the change can be judged by what it recovers
                // rather than by whether a diagnostic category shrank.
                snapshot.stalls.recovered_bytes = snapshot
                    .stalls
                    .recovered_bytes
                    .saturating_add(bytes.len() as u64);
            }
            // Verifier special pool is page-granular and its ranges are indistinguishable
            // from any other plain page range by their flags, so this has to come *before*
            // the backend dispatch. Walked as page ranges instead, the pool header plus
            // its fill decodes as garbage and every allocation in the heap silently
            // disappears from the snapshot.
            if region.heap.special {
                self.walk_special_pool(region, valid_base, &bytes, snapshot);
                if snapshot.match_limit_reached() {
                    return Ok(());
                }
                cursor = valid_end;
                continue;
            }
            match region.backend {
                PoolBackend::Lfh => self.walk_lfh(region, valid_base, &bytes, snapshot),
                PoolBackend::Vs => {
                    let resumed = self.walk_vs(region, valid_base, &bytes, vs_chunk, snapshot);
                    vs_chunk = resumed.map(|resume| resume.next);
                    if let Some(resume) = resumed {
                        vs_covered = vs_covered.max(resume.covered_through);
                    }
                }
                PoolBackend::Segment => self.walk_page_ranges(region, valid_base, &bytes, snapshot),
                PoolBackend::Large => return Ok(()),
            }
            if snapshot.match_limit_reached() {
                return Ok(());
            }
            cursor = valid_end;
        }
        Ok(())
    }

    /// Reads one committed extent, polling the deadline as the bytes come in.
    ///
    /// A single `read_exact` of a whole extent is one synchronous transfer that cannot
    /// observe the deadline while it runs, so a large committed page range could carry the
    /// walk past its ceiling however carefully the loops around it polled.
    ///
    /// **The chunking is of the transfer, not of the decoding.** Handing each chunk to the
    /// backend decoders instead would be much simpler and quietly wrong: `walk_lfh` skips any
    /// slot straddling the end of its slice and `walk_vs` stops at a chunk that runs past it
    /// and clears `complete`, so every chunk boundary would lose an allocation or invent an
    /// incomplete walk. The decoders still see one contiguous extent, exactly as before.
    fn read_extent(&self, base: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
        if size <= EXTENT_READ_CHUNK {
            return self.memory.read_exact(base, size);
        }
        let mut bytes = Vec::with_capacity(size);
        while bytes.len() < size {
            check_budget(self.memory)?;
            let take = EXTENT_READ_CHUNK.min(size - bytes.len());
            // A failed chunk fails the extent, which is what a failed whole-extent read did.
            //
            // A *short* chunk has to fail it too, and that is specific to reading in pieces.
            // Two things go wrong without this. The next chunk's address comes from how much
            // has been collected, so a short one shifts every later chunk and assembles a
            // buffer of contiguous bytes taken from discontiguous addresses — which the
            // decoders cannot detect, and which makes them report allocations that were never
            // at those addresses. Worse, the loop then cannot finish: the last chunk asks for
            // the remainder, comes back short, and `bytes.len()` never reaches `size`.
            // Deleting this check hangs the walk rather than failing it — verified. Refusing
            // the extent reports it unreadable, which is true, and is what a short
            // whole-extent read did before.
            let chunk = self.memory.read_exact(base + bytes.len() as u64, take)?;
            if chunk.len() != take {
                return Err(SnapshotError::InvalidData {
                    detail: format!(
                        "short extent read at {:#x}: asked {take:#x}, got {:#x}",
                        base + bytes.len() as u64,
                        chunk.len()
                    ),
                });
            }
            bytes.extend_from_slice(&chunk);
        }
        Ok(bytes)
    }

    fn walk_large(&self, region: &PoolRegion, snapshot: &mut PoolSnapshot) {
        if region.size == 0 {
            return;
        }
        snapshot.record_span(
            self.base_span(
                region,
                region.address,
                region.address,
                region.size as u64,
                region.known_tag.unwrap_or(0),
                region
                    .states
                    .first()
                    .copied()
                    .unwrap_or(PoolState::Allocated),
            ),
        );
    }

    fn base_span(
        &self,
        region: &PoolRegion,
        header: u64,
        usable: u64,
        size: u64,
        tag: u32,
        state: PoolState,
    ) -> PoolSpan {
        PoolSpan {
            header_address: header,
            usable_address: usable,
            size,
            requested_size: region.requested_size,
            raw_tag: tag,
            display_tag: super::decode::display_tag(tag),
            pool_kind: region.pool_kind,
            numa_node: region.numa_node,
            heap: region.heap,
            subsegment: region.subsegment,
            backend: region.backend,
            state,
            size_class: region.unit_size,
        }
    }

    /// Files address space the walk did not decode, split by what the memory manager says is
    /// behind it.
    ///
    /// Every caller reaches this having failed to read something, which is where the walk used
    /// to stop reasoning: it recorded one [`PoolState::Unreadable`] span and cleared
    /// `complete`. That is right for a page the target has and wrong for address space that
    /// holds nothing, and the two are not distinguishable from the failure — a reserved tail
    /// and a trimmed page fail a read identically. [`PoolMemory::committed_run`] is what
    /// separates them, and it is asked **per run** rather than per page, so one reserved tail
    /// of any size costs one query.
    ///
    /// Split rather than classified whole: a span can cross the boundary — the tail of a
    /// committed run and the reserved space after it arrive here as one failure — and calling
    /// the whole of it by either name misreports the other half. Splitting also keeps the byte
    /// totals addable, which is the only way the figure downstream means anything.
    ///
    /// Answers **whether any of it counted against coverage**, so a caller whose diagnostic is
    /// about missed memory can stay quiet when nothing was missed.
    fn unreadable(
        &self,
        region: &PoolRegion,
        address: u64,
        size: u64,
        snapshot: &mut PoolSnapshot,
    ) -> bool {
        let Some(end) = (size != 0).then(|| address.saturating_add(size)) else {
            return false;
        };
        let mut cursor = address;
        let mut missed = false;
        while cursor < end {
            // `> cursor` is not a formality: a run that does not advance would spin here, and
            // the engine answering about a *preceding* run is exactly the shape `walk_region`
            // already has to defend against. An answer that cannot move the cursor is no
            // answer, so the rest of the span is filed the conservative way and the loop ends.
            let Some(run) = self
                .memory
                .committed_run(cursor)
                .filter(|run| run.end > cursor)
            else {
                return self.gap(
                    region,
                    cursor,
                    end - cursor,
                    PoolState::Unreadable,
                    snapshot,
                ) || missed;
            };
            let stop = run.end.min(end);
            let state = if run.committed {
                PoolState::Unreadable
            } else {
                PoolState::Uncommitted
            };
            missed |= self.gap(region, cursor, stop - cursor, state, snapshot);
            cursor = stop;
        }
        missed
    }

    /// Records one gap span and, when it is a gap in *coverage*, clears `complete`; answers
    /// whether it was one.
    ///
    /// The clearing is here rather than at the call sites so the two can never disagree:
    /// `complete` means "no span says something may have been missed", and
    /// [`PoolState::is_coverage_gap`] is the one definition of which states say that.
    fn gap(
        &self,
        region: &PoolRegion,
        address: u64,
        size: u64,
        state: PoolState,
        snapshot: &mut PoolSnapshot,
    ) -> bool {
        if size == 0 {
            return false;
        }
        let missed = state.is_coverage_gap();
        if missed {
            snapshot.complete = false;
        }
        snapshot.record_span(self.base_span(region, address, address, size, 0, state));
        missed
    }

    /// Decodes a Driver Verifier special-pool region, one allocation per page.
    ///
    /// Special pool gives every allocation its own page: a header at the page start, fill,
    /// then the caller's block pushed up against the page end so an overrun lands on the
    /// following guard page. The guard page is unmapped, which is why `valid_region` stops
    /// after the data page and why a *freed* allocation simply vanishes — both are the
    /// mechanism working, not read failures.
    ///
    /// The block is placed at `page + PAGE_SIZE - align_up(size, 16)`; the 16-byte
    /// alignment is easy to miss, and dropping it puts the reported address 8 bytes past
    /// the real one (`0xf98` instead of `0xf90` for a 0x68 allocation).
    ///
    /// The page is read by [`decode_special_pool_header`] rather than by the ordinary
    /// [`decode_pool_header`]: special pool reuses those header bytes for a size, a fill
    /// pattern and a tracking flag, so the generic reading's plausibility guard is testing
    /// fields that are not there.
    fn walk_special_pool(
        &self,
        region: &PoolRegion,
        base: u64,
        bytes: &[u8],
        snapshot: &mut PoolSnapshot,
    ) {
        let mut page = base.next_multiple_of(PAGE_SIZE);
        while let Some(offset) = page.checked_sub(base).map(|delta| delta as usize) {
            if offset >= bytes.len() {
                break;
            }
            let available = (bytes.len() - offset).min(PAGE_SIZE as usize);
            let page_bytes = &bytes[offset..offset + available];
            let Some(header) = decode_special_pool_header(bytes, offset, region.pool_header) else {
                // Special pool is page-granular: one undecodable page says nothing about
                // the next, so stopping here would silently drop the rest of the region —
                // and leaving `complete` set would cache that truncated result and re-serve
                // it. Every other rejection path in this file reports and continues.
                snapshot.diagnostics.push(format!(
                    "special-pool page {page:#x}: header describes no block the page could \
                     hold; page skipped"
                ));
                snapshot.complete = false;
                let Some(next) = page.checked_add(PAGE_SIZE) else {
                    break;
                };
                page = next;
                continue;
            };
            // An untagged page is not an allocation. Freed special-pool pages are
            // unmapped rather than zeroed, so anything readable here should carry a tag.
            if header.tag != 0 {
                let placement = special_pool_placement(page, header, page_bytes);
                // Say so when the size is a bound rather than a measurement. Otherwise a
                // conservative page-sized range is indistinguishable from a real
                // allocation: `tag_census` would fold it into `total_bytes` — reporting a
                // 0x68 block as 0xff0 — while the report still claimed to be complete.
                if placement.approximate {
                    snapshot.diagnostics.push(format!(
                        "special-pool page {page:#x} tag `{}`: size not corroborated by the \
                         fill pattern; reporting the rest of the page as an upper bound",
                        super::decode::display_tag(header.tag)
                    ));
                    snapshot.complete = false;
                }
                let (usable, size) = (placement.usable, placement.size);
                snapshot.record_span(self.base_span(
                    region,
                    page,
                    usable,
                    size,
                    header.tag,
                    PoolState::Allocated,
                ));
                if snapshot.match_limit_reached() {
                    break;
                }
            }
            let Some(next) = page.checked_add(PAGE_SIZE) else {
                break;
            };
            page = next;
        }
    }

    fn walk_lfh(&self, region: &PoolRegion, base: u64, bytes: &[u8], snapshot: &mut PoolSnapshot) {
        let unit = region.unit_size as usize;
        if unit < region.pool_header.size {
            snapshot.diagnostics.push(format!(
                "rejecting implausible LFH unit size {} at {base:#x}",
                region.unit_size
            ));
            snapshot.complete = false;
            return;
        }
        let slice_offset = base.saturating_sub(region.address) as usize;
        let first_slot = slice_offset.div_ceil(unit);
        let slice_end = slice_offset.saturating_add(bytes.len());
        let mut slot = first_slot;
        while let Some(slot_offset) = slot.checked_mul(unit) {
            if slot_offset.saturating_add(unit) > slice_end {
                break;
            }
            let offset = slot_offset - slice_offset;
            let address = region.address + slot_offset as u64;
            // A block never straddles a page: the allocator skips the slot that would and
            // resumes at the next one, leaving the tail of the page as slack. Verified
            // against `!pool` on Server 26100 — with a 0x1a0 unit, blocks fill a page to
            // +0xef0 and the next block sits at +0x1090, which is exactly the following
            // slot, so linear indexing stays correct across the gap.
            //
            // Ordinary layout, then, not damage. Reporting it cost ~11.6k diagnostics on
            // an idle machine — drowning out the ones that meant something — and wrongly
            // cleared `complete` on essentially every snapshot taken.
            if address / PAGE_SIZE != (address + unit as u64 - 1) / PAGE_SIZE {
                slot += 1;
                continue;
            }
            let Some(state) = lfh_bitmap_state(&region.bitmap, slot) else {
                snapshot
                    .diagnostics
                    .push(format!("truncated LFH bitmap at slot {slot}"));
                snapshot.complete = false;
                break;
            };
            let tag = decode_pool_header(bytes, offset, region.pool_header)
                .map_or(0, |header| header.tag);
            let usable = address + region.pool_header.size as u64;
            snapshot.record_span(self.base_span(
                region,
                address,
                usable,
                unit as u64 - region.pool_header.size as u64,
                tag,
                state,
            ));
            if snapshot.match_limit_reached() {
                break;
            }
            slot += 1;
        }
    }

    /// Decodes one committed extent of a VS subsegment, chunk by chunk, and says where the
    /// chunk after the last one it read begins.
    ///
    /// **A VS extent can only be decoded from a known chunk boundary.** Chunks vary in size, so
    /// unlike [`Self::walk_lfh`] and [`Self::walk_page_ranges`] — whose slots are a fixed size,
    /// making them correct at whatever offset into the region an extent happens to start — the
    /// only way to know where a header is, is to have walked the one before it. `walk_region`
    /// hands this one *committed extent* at a time, and a subsegment is routinely committed in
    /// pieces: `RtlpHpVsSubsegmentCommitPages` commits and decommits page ranges anywhere inside
    /// the subsegment and records them in `_HEAP_VS_SUBSEGMENT.CommitBitmap`, so holes between
    /// committed extents are the allocator's steady state and not damage.
    ///
    /// Starting each extent at its own first sixteen-byte boundary — which is what this did — is
    /// therefore a guess on every extent after a hole, and one of the two sources of
    /// glslang/dbgscope#103's 106,516 refusals: one per sixteen bytes, until the scan wandered
    /// onto a word that decoded plausibly. The refusals were the harmless half. Whatever the
    /// scan wandered onto was pushed into `spans` as an allocation, tag and all.
    ///
    /// It was the smaller half. A live 26100 walk with this fixed still refused 76,398 headers,
    /// and the rest were the *bound*: the page range holding a VS subsegment is a unit larger
    /// than the subsegment, so the walk was decoding a page that holds no chunks. See
    /// [`discover_segment_context`], which now sizes the region from the subsegment.
    ///
    /// The chain is what crosses the hole. The chunk before one records a size that reaches past
    /// it and so names the header on the far side, at an address this walk already knows.
    /// `expected` carries that address between extents. `None` means the walk lost it, and a walk
    /// that has lost it does not guess again.
    ///
    /// It can lose it, and the reason is worth knowing before reading `unplaced_bytes` as damage.
    /// A *decommitted* range is the interior of a free chunk — the allocator has to keep its own
    /// headers reachable — so the chain crosses those. But a hole here is not only decommitted
    /// memory: on paged pool it is also memory that is committed and **paged out**, and nothing
    /// stops a chunk header being in it. Every unreadable header on a live 26100 walk was exactly
    /// that — `!pte` on each named address gave a pagefile PTE, `Protect: 4 - ReadWrite`, not a
    /// zero one — and a KD link cannot fault a page in. So those bytes are unreachable from a
    /// live target rather than absent from the pool, and the walk reports them instead of
    /// inventing chunks in them.
    ///
    /// A refused header costs more than itself: the walk no longer knows where the next one
    /// starts, so it advances sixteen bytes and tries again, and every header after it in the
    /// extent is decoded at a *guessed* offset rather than at the end of the previous chunk.
    /// One header rewritten while we read it and one systematically misdecoded field therefore
    /// look identical from a count — which is why what is reported here is the count of
    /// **chunks** refused rather than of extents that contained a refusal, the failing
    /// predicate in its own words, and the `Sizes` word as read. It is also why a refusal ends
    /// the chain for the whole region: every offset after it rests on that guess.
    fn walk_vs(
        &self,
        region: &PoolRegion,
        base: u64,
        bytes: &[u8],
        expected: Option<u64>,
        snapshot: &mut PoolSnapshot,
    ) -> Option<VsResume> {
        let extent_end = base.saturating_add(bytes.len() as u64);
        let Some(next) = expected.filter(|next| *next >= base) else {
            // Either an earlier extent of this region lost the chain, or the header it pointed
            // at fell inside the hole just crossed. Both come to the same thing: nothing in
            // these bytes can be placed. Sized and not merely counted, because what this costs
            // is coverage — and the alternative, decoding from a guess, costs correctness.
            snapshot.unplaced_bytes = snapshot.unplaced_bytes.saturating_add(bytes.len() as u64);
            // Two causes, kept as two shapes so a walk says which — `PoolDiagnostics` folds
            // numbers but not words, so this is the only way the split survives to a live run.
            snapshot.diagnostics.push(match expected {
                Some(next) => format!(
                    "VS extent at {base:#x} does not begin on a chunk boundary: the chunk chain \
                     names {next:#x}, {:#x} bytes back inside unreadable memory; {:#x} bytes not \
                     decoded",
                    base - next,
                    bytes.len()
                ),
                None => format!(
                    "VS extent at {base:#x} cannot be placed: the chain was already lost earlier \
                     in this region; {:#x} bytes not decoded",
                    bytes.len()
                ),
            });
            snapshot.complete = false;
            return None;
        };
        if next >= extent_end {
            // A chunk that began before this extent covers all of it — an ordinary large free
            // chunk with its interior decommitted. Nothing to decode and nothing lost: the
            // expectation still names a header further on.
            return Some(VsResume {
                next,
                covered_through: base,
            });
        }
        let mut offset = (next - base) as usize;
        // Where the *next* extent resumes, kept in step with `offset` so every way out of the
        // loop below leaves it pointing at a header rather than at wherever the bytes ran out.
        let mut resume = next;
        let mut lost = false;
        let mut chunks = 0usize;
        let header_bytes = region.vs_header_size + region.pool_header.size;
        let subsegment_end = region.address.saturating_add(region.size as u64);
        let mut refused = 0u64;
        let mut resync_from = None;
        let mut chain_breaks = 0u64;
        // What the *next* header must record as its previous size, or `None` where there is
        // nothing to compare against: at the start of an extent, and after a resynchronisation,
        // where the walk no longer knows which chunk it is standing on.
        let mut previous_chunk = None;
        // See [`VsResume::covered_through`]: raised only by a span filed past the extent.
        let mut covered_through = base;
        while offset
            .saturating_add(region.vs_header_size)
            .saturating_add(region.pool_header.size)
            <= bytes.len()
            && chunks < self.traversal_limit
        {
            let header_address = base + offset as u64;
            let Some(encoded) = read_u64(bytes, offset + region.vs_sizes_offset) else {
                break;
            };
            let chunk = match decode_vs_chunk(
                encoded,
                header_address,
                region.heap_key,
                header_bytes,
                subsegment_end,
            ) {
                Ok(chunk) => chunk,
                Err(rejection) => {
                    if refused == 0 {
                        snapshot.diagnostics.push(format!(
                            "refusing VS chunk at {header_address:#x}: {rejection}"
                        ));
                    }
                    refused += 1;
                    resync_from.get_or_insert(header_address);
                    previous_chunk = None;
                    // The sixteen-byte scan below may find its way back onto real headers
                    // inside this extent, but it cannot *know* that it has — so whatever it
                    // ends on is not an address to hand the next extent. Sticky, because a
                    // chunk decoded after the scan resumes is exactly the guess in question.
                    lost = true;
                    snapshot.complete = false;
                    offset = offset.saturating_add(16);
                    continue;
                }
            };
            if let Some(expected) = previous_chunk
                && chunk.previous_size != expected
            {
                // Not a refusal: the chunk itself is plausible, and the chain disagreeing says
                // the *stride* that reached it is wrong. Reported so one live run can tell a
                // header rewritten under us from a walk decoding the wrong field, which is the
                // question a refusal count on its own cannot answer.
                if chain_breaks == 0 {
                    snapshot.diagnostics.push(format!(
                        "VS chunk at {header_address:#x} records a previous size of \
                         {:#x} where the chunk before it measured {expected:#x}",
                        chunk.previous_size
                    ));
                }
                chain_breaks += 1;
                snapshot.complete = false;
            }
            let chunk_size = chunk.size;
            let state = if region.cached_chunks.contains(&header_address) {
                PoolState::CachedFree
            } else if region.reusable_chunks.contains(&header_address) {
                PoolState::ReusableFree
            } else if chunk.allocated {
                PoolState::Allocated
            } else {
                PoolState::ReusableFree
            };
            // **A chunk too large for `_POOL_HEADER.BlockSize` carries no header**, so decoding
            // one out of it reads the caller's own first sixteen bytes as a tag. Its name is in
            // `nt!PoolBigPageTable`, which discovery resolved into `region.big_pool`.
            //
            // Matched by **containment** rather than by arithmetic on the chunk header: the
            // table's `Va` is where the allocation starts, and taking it as given costs nothing
            // and assumes nothing about what sits between the two — which is the one thing here
            // that is measured and not yet explained (`windbg-mcp` FOLLOWUPS item 99). The size
            // has to fit inside the chunk as well, so an entry can only be claimed by a chunk
            // that could really hold it.
            //
            // **Asked before the extent check below, not after it.** Everything the span
            // carries is in hand once the header has decoded: the tag and the length are the
            // table's, the state is the free tree's, and the chunk's own bytes were never going
            // to be read. So a tail that runs into a page the memory manager has trimmed takes
            // nothing from the answer — and asked afterwards, the chunk was dropped for that
            // tail and its pages filed as an untagged gap instead, which is what `pool_chunk`
            // then answered for the allocation's own address. Measured on a live 26100 kernel
            // (`ctf-vm`, 2026-10-07): the `CIcr` allocation at 0xffffa9099c86f000 sits 0x20
            // into a 0x12c0-byte chunk whose header is resident and whose tail is not, and
            // `!pool` named it from the table while this walk answered `....`.
            let big_pool = (chunk_size as u64 > MAX_POOL_HEADER_CHUNK)
                .then(|| {
                    let end = header_address.saturating_add(chunk_size as u64);
                    region.big_pool.iter().find(|(address, entry)| {
                        (header_address..end).contains(*address)
                            && address.saturating_add(entry.size) <= end
                    })
                })
                .flatten()
                .map(|(address, entry)| (*address, *entry));
            if let Some((address, entry)) = big_pool {
                let mut span =
                    self.base_span(region, address, address, entry.size, entry.tag, state);
                span.size_class = chunk_size.min(u32::MAX as usize) as u32;
                snapshot.record_span(span);
                previous_chunk = Some(chunk_size);
                offset += chunk_size;
                resume = base + offset as u64;
                // Past the extent when the tail is in the hole ahead, which is what tells the
                // extent loop not to file that hole over this span.
                covered_through = resume;
                chunks += 1;
                if snapshot.match_limit_reached() {
                    break;
                }
                continue;
            }
            // The chunk reaches past the committed extent, which after the bound check in
            // `decode_vs_chunk` can only mean a hole ahead of it inside the subsegment — so
            // this is the ordinary free chunk with a decommitted interior, not a walk running
            // out of bytes. It carries the chain over the hole, which is the whole reason the
            // expectation is returned rather than recomputed per extent.
            //
            // **What the walk loses by it depends on what is in the hole**, and the same
            // question the gap spans ask answers this one. A chunk's span is geometry and
            // state, not contents: its header was read, its size came out of that header and
            // passed the bound check, and its state comes from the free tree — so where the
            // tail holds nothing, nothing about the chunk is unknown and the span is emitted
            // below like any other. Where the tail is memory the process has, the walk really
            // did fail to see part of this chunk, and then no span is emitted and `complete`
            // clears.
            //
            // Measured on `sihost` (26200, 2026-09-24): five free chunks of 0x1660 to 0xfe70
            // bytes, each with a decommitted middle, were the *only* thing left holding that
            // walk at `Partial` once the gap spans were classified — and this site cleared
            // `complete` without a diagnostic, so nothing in the answer said why.
            if offset.saturating_add(chunk_size) > bytes.len()
                && !self
                    .memory
                    .committed_run(base.saturating_add(bytes.len() as u64))
                    .is_some_and(|run| !run.committed)
            {
                resume = header_address.saturating_add(chunk_size as u64);
                snapshot.diagnostics.push(format!(
                    "VS chunk at {header_address:#x} is {chunk_size:#x} bytes and runs \
                     {:#x} past the committed extent at {:#x}; no span is emitted for it",
                    offset.saturating_add(chunk_size) - bytes.len(),
                    base.saturating_add(bytes.len() as u64)
                ));
                snapshot.complete = false;
                break;
            }
            // Where the chunk's user block begins: the pool header on a kernel target, the
            // caller's own bytes on a user-mode one. Right after the chunk header, except where
            // that is the last unit of a page and the allocator hands out the next page start
            // instead — see [`PoolRegion::vs_skips_page_end`]. The chain is not involved: the
            // chunk's extent is what its header says, and only where the user block sits in it
            // moves.
            let mut physical_header = header_address + region.vs_header_size as u64;
            if region.vs_skips_page_end
                && physical_header.saturating_add(VS_UNIT) & (PAGE_SIZE - 1) == 0
            {
                // Only where the chunk has the unit to skip. `decode_vs_chunk` admitted this
                // chunk against its two headers alone, and a chunk exactly that size — the
                // smallest free remainder a split leaves — would otherwise have its user block
                // placed at its own end, which is the next chunk's header. The allocator asks
                // for one more unit than a request needs under the flag
                // (`RtlpHpVsContextAllocate+0x1a..+0x3c`: `and eax,1` / `inc eax` /
                // `add eax,r10d`), so a chunk here with no room is one nothing was allocated
                // from, and it is read in place; an *allocated* one contradicts that and is
                // reported.
                if chunk_size >= header_bytes.saturating_add(VS_UNIT as usize) {
                    physical_header += VS_UNIT;
                } else if chunk.allocated {
                    snapshot.diagnostics.push(format!(
                        "VS chunk at {header_address:#x} is {chunk_size:#x} bytes, too short to \
                         hold a user block moved off the page end; read in place"
                    ));
                }
            }
            let pool_offset = physical_header.saturating_sub(base) as usize;
            let tag = decode_pool_header(bytes, pool_offset, region.pool_header)
                .map_or(0, |header| header.tag);
            let overhead = physical_header
                .saturating_sub(header_address)
                .saturating_add(region.pool_header.size as u64);
            let span_header = if region.pool_header.size == 0 {
                header_address
            } else {
                physical_header
            };
            let mut span = self.base_span(
                region,
                span_header,
                physical_header + region.pool_header.size as u64,
                (chunk_size as u64).saturating_sub(overhead),
                tag,
                state,
            );
            span.size_class = chunk_size.min(u32::MAX as usize) as u32;
            snapshot.record_span(span);
            previous_chunk = Some(chunk_size);
            offset += chunk_size;
            resume = base + offset as u64;
            chunks += 1;
            if snapshot.match_limit_reached() {
                break;
            }
        }
        if let Some(from) = resync_from {
            snapshot.refused_chunks = snapshot.refused_chunks.saturating_add(refused);
            snapshot.diagnostics.push(format!(
                "{refused} VS chunk headers refused, resynchronising from {from:#x}"
            ));
        }
        if chain_breaks > 0 {
            snapshot.diagnostics.push(format!(
                "{chain_breaks} VS chunks disagreed with the size of the chunk before them, \
                 from {base:#x}"
            ));
        }
        if !snapshot.match_limit_reached() && chunks >= self.traversal_limit {
            snapshot.complete = false;
            snapshot
                .diagnostics
                .push(format!("VS traversal limit reached at {base:#x}"));
        }
        (!lost).then_some(VsResume {
            next: resume,
            covered_through,
        })
    }

    fn walk_page_ranges(
        &self,
        region: &PoolRegion,
        base: u64,
        bytes: &[u8],
        snapshot: &mut PoolSnapshot,
    ) {
        let unit = region.unit_size.max(1) as usize;
        let slice_offset = base.saturating_sub(region.address) as usize;
        let first_slot = slice_offset.div_ceil(unit);
        let slice_end = slice_offset.saturating_add(bytes.len());
        let mut slot = first_slot;
        while let Some(slot_offset) = slot.checked_mul(unit) {
            if slot_offset >= slice_end {
                break;
            }
            let offset = slot_offset - slice_offset;
            let remaining = bytes.len() - offset;
            let size = unit.min(remaining);
            let state = region
                .states
                .get(slot)
                .copied()
                .or_else(|| region.states.first().copied())
                .unwrap_or(PoolState::Unreadable);
            let tag = region.known_tag.or_else(|| {
                decode_pool_header(bytes, offset, region.pool_header).map(|header| header.tag)
            });
            let address = region.address + slot_offset as u64;
            let header_size = region.pool_header.size.min(size);
            snapshot.record_span(self.base_span(
                region,
                address,
                address + header_size as u64,
                size.saturating_sub(header_size) as u64,
                tag.unwrap_or(0),
                state,
            ));
            if snapshot.match_limit_reached() {
                break;
            }
            slot += 1;
        }
    }
}

/// Walk user-mode Segment Heap roots through the same region discovery and backend decoders
/// used for kernel pool heaps.
///
/// The adapter supplies user addresses and a zero-sized pool header through `layout`; everything
/// below the root enumeration—page segments, descriptors, LFH bitmaps, VS chains, free-state
/// evidence, large allocations, diagnostics, caps, and deadlines—is shared.
pub(crate) fn walk_user_segment_heaps<M: PoolMemory>(
    memory: &M,
    layout: &PoolLayout,
    peb: u64,
    heaps: &[u64],
    budget: Option<Duration>,
    traversal_limit: usize,
) -> Result<PoolSnapshot, SnapshotError> {
    let (discovery_deadline, walk_deadline) = budget_deadlines(Instant::now(), budget);
    let discovery_clock = Budgeted::new(memory, discovery_deadline);
    let globals_address = *layout
        .globals
        .get("RtlpHpHeapGlobals")
        .ok_or_else(|| missing_layout("RtlpHpHeapGlobals"))?;
    let heap_key = scalar(
        &discovery_clock,
        globals_address + layout.field("_RTLP_HP_HEAP_GLOBALS", "HeapKey")? as u64,
        8,
    )?;
    let lfh_key = scalar(
        &discovery_clock,
        globals_address + layout.field("_RTLP_HP_HEAP_GLOBALS", "LfhKey")? as u64,
        8,
    )?;

    let mut discovery = Discovery::default();
    let mut expired = false;
    for &heap in heaps {
        let identity = HeapIdentity {
            pool_state: peb,
            heap,
            special: false,
        };
        match discover_heap_regions(
            &discovery_clock,
            layout,
            heap,
            0,
            PoolKind::NonPagedNx,
            identity,
            None,
            heap_key,
            lfh_key,
            traversal_limit,
            &mut discovery,
        ) {
            Ok(()) => {}
            Err(SnapshotError::BudgetExpired) => {
                expired = true;
                break;
            }
            Err(error) if error.halts_walk() => return Err(error),
            Err(error) => discovery
                .diagnostics
                .push(format!("cannot fully discover heap {heap:#x}: {error}")),
        }
    }

    let mut snapshot = PoolSnapshot {
        complete: discovery.diagnostics.is_empty(),
        diagnostics: PoolDiagnostics::from_iter(std::mem::take(&mut discovery.diagnostics)),
        ..PoolSnapshot::default()
    };
    let walk_clock = Budgeted::new(memory, walk_deadline);
    let walker = SnapshotWalker {
        memory: &walk_clock,
        layout,
        traversal_limit,
    };
    walker.walk_discovered_regions(discovery.regions, budget, expired, &mut snapshot)?;
    Ok(snapshot)
}

#[cfg(test)]
mod tests {
    use std::{cell::Cell, collections::HashMap};

    use super::*;

    // ---- LFH slots that would straddle a page ---------------------------------------

    fn lfh_region(address: u64, unit_size: u32) -> PoolRegion {
        PoolRegion {
            address,
            size: 0x1000,
            requested_size: None,
            pool_kind: PoolKind::NonPagedNx,
            numa_node: 0,
            heap: HeapIdentity {
                pool_state: 0,
                heap: 0,
                special: false,
            },
            subsegment: None,
            backend: PoolBackend::Lfh,
            unit_size,
            // Eight slots' worth, all marked allocated.
            bitmap: vec![0xff],
            heap_key: 0,
            pool_header: PoolHeaderLayout {
                size: 0x10,
                previous_size: 0,
                pool_index: 1,
                block_size: 2,
                pool_type: 3,
                tag: 4,
            },
            vs_header_size: 0,
            vs_sizes_offset: 0,
            vs_skips_page_end: false,
            known_tag: None,
            states: Vec::new(),
            reusable_chunks: Arc::default(),
            cached_chunks: Arc::default(),
            big_pool: Arc::default(),
        }
    }

    /// The allocator refuses to let a block straddle a page: it skips that slot, leaves the
    /// page tail as slack, and resumes at the next slot. Confirmed against `!pool` on
    /// Server 26100. So the straddling slot must be skipped **silently** — it is ordinary
    /// layout, and reporting it once cost ~11.6k diagnostics on an idle machine.
    #[test]
    fn test_page_straddling_lfh_slot_is_skipped_without_complaint() {
        // Slot 0 at 0x1f80 spans 0x1f80..0x2080 and crosses; slot 1 at 0x2080 does not.
        let region = lfh_region(0x1f80, 0x100);
        let memory = FlatMemory::new(0x1f80, 0x200);
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };
        walker.walk_lfh(&region, 0x1f80, &memory.bytes, &mut snapshot);

        // The straddling slot is dropped, the following one is still reported: skipping
        // must not desynchronise the linear slot indexing.
        assert_eq!(snapshot.spans.len(), 1);
        assert_eq!(snapshot.spans[0].header_address, 0x2080);
        // The two properties that were wrong: silence, and an intact `complete`.
        assert!(
            snapshot.diagnostics.is_empty(),
            "normal layout must not be reported: {:?}",
            snapshot.diagnostics
        );
        assert!(
            snapshot.complete,
            "a straddling slot is expected layout and must not mark the snapshot incomplete"
        );
    }

    fn match_limited_snapshot(tag: &[u8; 4], matches: usize) -> PoolSnapshot {
        PoolSnapshot {
            complete: true,
            match_limit: Some(PoolMatchLimit::new(
                u32::from_le_bytes(*tag),
                None,
                NonZeroUsize::new(matches).expect("test match limits are nonzero"),
            )),
            ..PoolSnapshot::default()
        }
    }

    fn walk_page_tag(tag: [u8; 4], slots: usize, stop_after: usize) -> PoolSnapshot {
        let bytes = vec![0; slots * 0x20];
        let mut region = lfh_region(0x4000, 0x20);
        region.backend = PoolBackend::Segment;
        region.size = bytes.len();
        region.known_tag = Some(u32::from_le_bytes(tag));
        region.states = vec![PoolState::Allocated; slots];
        let memory = FlatMemory::new(region.address, bytes.len());
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = match_limited_snapshot(b"Tgsm", stop_after);
        walker.walk_page_ranges(&region, region.address, &bytes, &mut snapshot);
        snapshot
    }

    #[test]
    fn test_match_limit_does_not_stop_a_walk_with_zero_matches() {
        let snapshot = walk_page_tag(*b"Othr", 3, 1);
        assert_eq!(snapshot.spans.len(), 3);
        assert_eq!(snapshot.stopped_after_matches, None);
        assert!(snapshot.complete);
    }

    #[test]
    fn test_match_limit_stops_on_the_first_match() {
        let snapshot = walk_page_tag(*b"Tgsm", 3, 1);
        assert_eq!(snapshot.spans.len(), 1);
        assert_eq!(snapshot.stopped_after_matches, Some(1));
        assert_eq!(snapshot.matched_allocations, 1);
        assert!(!snapshot.complete);
    }

    #[test]
    fn test_match_limit_stops_on_the_requested_nth_match() {
        let snapshot = walk_page_tag(*b"Tgsm", 4, 2);
        assert_eq!(snapshot.spans.len(), 2);
        assert_eq!(snapshot.stopped_after_matches, Some(2));
        assert_eq!(snapshot.matched_allocations, 2);
        assert_eq!(snapshot.spans.last().unwrap().display_tag, "Tgsm");
    }

    #[test]
    fn test_match_limit_counts_only_allocated_chunks_in_scope() {
        let raw_tag = u32::from_le_bytes(*b"Tgsm");
        let mut snapshot = PoolSnapshot {
            complete: true,
            match_limit: Some(PoolMatchLimit::new(
                raw_tag,
                Some(false),
                NonZeroUsize::new(2).unwrap(),
            )),
            ..PoolSnapshot::default()
        };
        let span = |address, kind, state| {
            let mut span = PoolSpan::allocation(
                address,
                0x20,
                raw_tag,
                kind,
                HeapIdentity {
                    pool_state: 1,
                    heap: 1,
                    special: false,
                },
                PoolBackend::Lfh,
            );
            span.state = state;
            span
        };

        snapshot.record_span(span(0x1000, PoolKind::Paged, PoolState::Allocated));
        snapshot.record_span(span(0x1020, PoolKind::NonPagedNx, PoolState::ReusableFree));
        snapshot.record_span(span(0x1040, PoolKind::NonPagedNx, PoolState::Allocated));
        assert_eq!(snapshot.stopped_after_matches, None);
        snapshot.record_span(span(0x1060, PoolKind::NonPagedNx, PoolState::Allocated));
        assert_eq!(snapshot.stopped_after_matches, Some(2));
    }

    #[test]
    fn test_user_regions_never_report_payload_bytes_as_pool_tags() {
        let no_pool_header = PoolHeaderLayout {
            size: 0,
            previous_size: 0,
            pool_index: 0,
            block_size: 0,
            pool_type: 0,
            tag: 0,
        };
        let memory = FlatMemory::new(0x1000, 0x1000);
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };

        let mut lfh = lfh_region(0x1000, 0x20);
        lfh.size = 0x20;
        lfh.pool_header = no_pool_header;
        let mut lfh_bytes = vec![0; 0x20];
        lfh_bytes[..4].copy_from_slice(b"LFH!");
        walker.walk_lfh(&lfh, lfh.address, &lfh_bytes, &mut snapshot);

        let mut vs = vs_region(0x40);
        vs.pool_header = no_pool_header;
        let mut vs_bytes = vs_extent(&[(0x40, 0)]);
        vs_bytes[0x10..0x14].copy_from_slice(b"VS!!");
        walker.walk_vs(&vs, vs.address, &vs_bytes, Some(vs.address), &mut snapshot);

        let mut page = lfh_region(0x3000, 0x20);
        page.size = 0x20;
        page.backend = PoolBackend::Segment;
        page.pool_header = no_pool_header;
        page.states = vec![PoolState::Allocated];
        let mut page_bytes = vec![0; 0x20];
        page_bytes[..4].copy_from_slice(b"PAGE");
        walker.walk_page_ranges(&page, page.address, &page_bytes, &mut snapshot);

        assert_eq!(snapshot.spans.len(), 3);
        assert!(
            snapshot.spans.iter().all(|span| span.raw_tag == 0),
            "user payload bytes must not be decoded as kernel pool tags: {:?}",
            snapshot.spans
        );
    }

    // ---- VS roots: inline context vs affinity slots ----------------------------------

    /// Memory backed by one contiguous buffer. Anything outside it fails to read, the way
    /// an unmapped page does.
    struct FlatMemory {
        base: u64,
        bytes: Vec<u8>,
    }

    impl FlatMemory {
        fn new(base: u64, len: usize) -> Self {
            Self {
                base,
                bytes: vec![0; len],
            }
        }

        fn put(&mut self, address: u64, data: &[u8]) {
            let offset = (address - self.base) as usize;
            self.bytes[offset..offset + data.len()].copy_from_slice(data);
        }

        fn put_u16(&mut self, address: u64, value: u16) {
            self.put(address, &value.to_le_bytes());
        }

        fn put_u64(&mut self, address: u64, value: u64) {
            self.put(address, &value.to_le_bytes());
        }
    }

    impl PoolMemory for FlatMemory {
        fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            let offset = address
                .checked_sub(self.base)
                .and_then(|offset| usize::try_from(offset).ok())
                .ok_or_else(|| SnapshotError::InvalidData {
                    detail: format!("read below the fixture at {address:#x}"),
                })?;
            self.bytes
                .get(offset..offset + size)
                .map(<[u8]>::to_vec)
                .ok_or_else(|| SnapshotError::InvalidData {
                    detail: format!("read past the fixture at {address:#x}+{size:#x}"),
                })
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            Ok((address, size))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }
    }

    const VS_CONTEXT: u64 = 0x1000;

    /// Which shape a `vs_layout` fixture describes.
    #[derive(Clone, Copy, PartialEq, Eq)]
    enum VsFixture {
        /// The free tree is in `_HEAP_VS_CONTEXT`.
        Inline,
        /// Affinity slots, each naming its context by address (`VsContext`).
        SlotsByAddress,
        /// Affinity slots, each naming its context by displacement (`VsContextOffset`).
        SlotsByDisplacement,
    }

    /// A layout carrying only what `vs_roots` consults, in one of the three shapes a target's
    /// PDB can present. The two slot shapes differ by **one field name**, which is the whole
    /// substance of the change they cover: the same slot map, the same arithmetic, and a
    /// back-reference that is an address in one and `slot - context` in the other.
    fn vs_layout(shape: VsFixture) -> PoolLayout {
        let mut types = HashMap::new();
        types.insert(
            "_HEAP_VS_CONTEXT",
            match shape {
                VsFixture::Inline => {
                    type_layout(0x80, &[("FreeChunkTree", 0x10), ("DelayFreeContext", 0x30)])
                }
                _ => type_layout(0x60, &[("SlotMapRef", 0), ("AffinityMask", 2)]),
            },
        );
        if shape != VsFixture::Inline {
            let back = match shape {
                VsFixture::SlotsByDisplacement => "VsContextOffset",
                _ => "VsContext",
            };
            types.insert(
                "_HEAP_VS_AFFINITY_SLOT",
                type_layout(
                    0x80,
                    &[
                        (back, 0),
                        ("FreeChunkTree", 0x10),
                        ("DelayFreeContext", 0x40),
                    ],
                ),
            );
            types.insert("_HEAP_VS_SLOT_MAP", type_layout(4, &[("SlotRef", 0)]));
        }
        PoolLayout {
            key: crate::pool::layout::LayoutKey {
                image: crate::dbgeng::KernelImage::default(),
                session: 1,
            },
            globals: HashMap::new(),
            types,
        }
    }

    /// Four affinity entries over three distinct slots: two whose back-pointer agrees (one
    /// of them referenced twice, so dedup is exercised) and one that names another context.
    fn affinity_fixture() -> FlatMemory {
        let mut memory = FlatMemory::new(VS_CONTEXT, 0x1200);
        memory.put_u16(VS_CONTEXT, 0x10); // SlotMapRef -> map at ctx + 0x10*64
        memory.put(VS_CONTEXT + 2, &[3]); // AffinityMask -> 4 entries
        let map = VS_CONTEXT + 0x400;
        for (index, slot_ref) in [0x20u16, 0x30, 0x20, 0x40].into_iter().enumerate() {
            memory.put_u16(map + index as u64 * 4, slot_ref);
        }
        memory.put_u64(VS_CONTEXT + 0x800, VS_CONTEXT); // 0x20 << 6, owner agrees
        memory.put_u64(VS_CONTEXT + 0xc00, VS_CONTEXT); // 0x30 << 6, owner agrees
        memory.put_u64(VS_CONTEXT + 0x1000, 0xdead_beef); // 0x40 << 6, owner disagrees
        memory
    }

    /// The same slots, the same map, the same arithmetic — with each slot naming its context by
    /// displacement instead of by address, as `_HEAP_VS_AFFINITY_SLOT::VsContextOffset` does.
    ///
    /// Deliberately the same geometry as [`affinity_fixture`], so a test that swaps one for the
    /// other is measuring the back-reference rule and nothing else. The third slot disagrees the
    /// way a displacement can: `0x1234` is a real displacement to somewhere that is not this
    /// context.
    fn displacement_fixture() -> FlatMemory {
        let mut memory = FlatMemory::new(VS_CONTEXT, 0x1200);
        memory.put_u16(VS_CONTEXT, 0x10); // SlotMapRef -> map at ctx + 0x10*64
        memory.put(VS_CONTEXT + 2, &[3]); // AffinityMask -> 4 entries
        let map = VS_CONTEXT + 0x400;
        for (index, slot_ref) in [0x20u16, 0x30, 0x20, 0x40].into_iter().enumerate() {
            memory.put_u16(map + index as u64 * 4, slot_ref);
        }
        memory.put_u64(VS_CONTEXT + 0x800, 0x800); // 0x20 << 6, displacement agrees
        memory.put_u64(VS_CONTEXT + 0xc00, 0xc00); // 0x30 << 6, displacement agrees
        memory.put_u64(VS_CONTEXT + 0x1000, 0x1234); // 0x40 << 6, displacement disagrees
        memory
    }

    /// The slot map is up to 256 entries plus an owner read per distinct slot, all over the
    /// wire. It cannot lean on a later poll either: invalid slot refs produce no roots, so
    /// nothing downstream reads at all and the next check is a whole segment context away.
    #[test]
    fn test_vs_roots_polls_the_budget_between_slot_map_entries() {
        // Two reads resolve the slot map itself; the loop below them gets nothing.
        let memory = Impatient::over(affinity_fixture(), 2);

        let outcome = vs_roots(
            &memory,
            &vs_layout(VsFixture::SlotsByAddress),
            VS_CONTEXT,
            &mut Vec::new(),
        );

        assert!(
            matches!(outcome, Err(SnapshotError::BudgetExpired)),
            "the slot-map loop read on past the deadline, resolving {} roots",
            outcome.map_or(0, |roots| roots.len())
        );
    }

    /// Inline family: the tree is in the context, so there is exactly one root and no
    /// slot map is consulted at all.
    #[test]
    fn test_vs_roots_reads_the_legacy_in_context_shape() {
        let memory = FlatMemory::new(VS_CONTEXT, 0x100);
        let mut diagnostics = Vec::new();
        let roots = vs_roots(
            &memory,
            &vs_layout(VsFixture::Inline),
            VS_CONTEXT,
            &mut diagnostics,
        )
        .unwrap();
        assert_eq!(roots.len(), 1);
        assert_eq!(roots[0].base, VS_CONTEXT);
        assert_eq!(roots[0].tree_offset, 0x10);
        assert_eq!(roots[0].delay_offset, Some(0x30));
        assert!(diagnostics.is_empty());
    }

    /// Affinity family: `slot = VsContext + (SlotRef << 6)`, and entries routinely share a slot.
    #[test]
    fn test_vs_roots_walks_the_affinity_slots_and_dedups_them() {
        let memory = affinity_fixture();
        let mut diagnostics = Vec::new();
        let roots = vs_roots(
            &memory,
            &vs_layout(VsFixture::SlotsByAddress),
            VS_CONTEXT,
            &mut diagnostics,
        )
        .unwrap();
        let bases: Vec<u64> = roots.iter().map(|root| root.base).collect();
        // Four entries, one repeat, one rejected -> two roots.
        assert_eq!(bases, vec![VS_CONTEXT + 0x800, VS_CONTEXT + 0xc00]);
        assert!(roots.iter().all(|root| root.tree_offset == 0x10));
        assert!(roots.iter().all(|root| root.delay_offset == Some(0x40)));
    }

    /// Self-relative family: a slot names its context as `slot - context`, so the same map and
    /// the same arithmetic must produce the same roots as the address-bearing shape.
    ///
    /// The geometry is identical to [`affinity_fixture`] on purpose. What differs is one field
    /// name in the layout and the value each slot stores, which is the whole substance of the
    /// change (`windbg-mcp` FOLLOWUPS item 78).
    #[test]
    fn test_vs_roots_walks_affinity_slots_that_name_their_context_by_displacement() {
        let memory = displacement_fixture();
        let mut diagnostics = Vec::new();

        let roots = vs_roots(
            &memory,
            &vs_layout(VsFixture::SlotsByDisplacement),
            VS_CONTEXT,
            &mut diagnostics,
        )
        .unwrap();

        let bases: Vec<u64> = roots.iter().map(|root| root.base).collect();
        // The same four entries, one repeat and one rejected, as the address shape resolves.
        assert_eq!(bases, vec![VS_CONTEXT + 0x800, VS_CONTEXT + 0xc00]);
        assert!(roots.iter().all(|root| root.tree_offset == 0x10));
        assert!(roots.iter().all(|root| root.delay_offset == Some(0x40)));
    }

    /// The two back-reference rules are not interchangeable, and getting them the wrong way round
    /// does not fail loudly — it rejects every slot and walks no VS evidence at all.
    ///
    /// This is the measurement the fix rests on, as a test: the *same memory* read under the
    /// other build's rule resolves nothing. It is also what says an alias would have been the
    /// wrong fix. Confirmed live against `ntdll` on 26200, where the displacement `0xa80` read as
    /// an address produced `claims context 0xa80`.
    #[test]
    fn test_the_two_affinity_back_reference_rules_do_not_substitute_for_each_other() {
        for (fixture, wrong_shape, what) in [
            (
                displacement_fixture(),
                VsFixture::SlotsByAddress,
                "displacements read as addresses",
            ),
            (
                affinity_fixture(),
                VsFixture::SlotsByDisplacement,
                "addresses read as displacements",
            ),
        ] {
            let mut diagnostics = Vec::new();

            let roots = vs_roots(
                &fixture,
                &vs_layout(wrong_shape),
                VS_CONTEXT,
                &mut diagnostics,
            )
            .unwrap();

            assert!(
                roots.is_empty(),
                "{what}: {} slot(s) were accepted by the wrong rule, so the tree walk would read \
                 unrelated memory",
                roots.len()
            );
            assert!(
                diagnostics
                    .iter()
                    .all(|line| line.contains("claims context")),
                "{what}: every slot should be rejected for naming another context, not skipped \
                 for some other reason: {diagnostics:?}"
            );
        }
    }

    /// A displacement larger than the slot's own address names no context at all. Wrapping would
    /// hand the tree walk an address near the top of the space, which may well be readable.
    #[test]
    fn test_vs_roots_rejects_a_displacement_that_runs_off_the_bottom() {
        let mut memory = displacement_fixture();
        memory.put_u64(VS_CONTEXT + 0x800, u64::MAX);
        let mut diagnostics = Vec::new();

        let roots = vs_roots(
            &memory,
            &vs_layout(VsFixture::SlotsByDisplacement),
            VS_CONTEXT,
            &mut diagnostics,
        )
        .unwrap();

        assert_eq!(
            roots.iter().map(|root| root.base).collect::<Vec<_>>(),
            vec![VS_CONTEXT + 0xc00],
            "the slot with the impossible displacement should be the only one dropped"
        );
        assert!(
            diagnostics
                .iter()
                .any(|line| line.contains("claims context none")),
            "a displacement that cannot name a context should say so: {diagnostics:?}"
        );
    }

    /// A slot whose back-pointer names a different context is not this context's slot.
    /// Trusting it would aim the tree walk at unrelated but readable memory.
    #[test]
    fn test_vs_roots_rejects_a_slot_whose_back_pointer_disagrees() {
        let memory = affinity_fixture();
        let mut diagnostics = Vec::new();
        let roots = vs_roots(
            &memory,
            &vs_layout(VsFixture::SlotsByAddress),
            VS_CONTEXT,
            &mut diagnostics,
        )
        .unwrap();
        assert!(roots.iter().all(|root| root.base != VS_CONTEXT + 0x1000));
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].contains("claims context"));
    }

    /// A zero `SlotMapRef` would alias the context itself; refuse rather than walk it.
    #[test]
    fn test_vs_roots_refuses_an_implausible_slot_map() {
        let mut memory = affinity_fixture();
        memory.put_u16(VS_CONTEXT, 0);
        let mut diagnostics = Vec::new();
        let roots = vs_roots(
            &memory,
            &vs_layout(VsFixture::SlotsByAddress),
            VS_CONTEXT,
            &mut diagnostics,
        )
        .unwrap();
        assert!(roots.is_empty());
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].contains("implausible VS slot map"));
    }

    /// Neither shape resolving is a reportable condition, not a silently empty walk — that
    /// ambiguity is exactly what made the 26100 breakage so hard to see.
    #[test]
    fn test_vs_roots_reports_when_neither_shape_resolves() {
        let memory = FlatMemory::new(VS_CONTEXT, 0x100);
        let layout = PoolLayout {
            key: crate::pool::layout::LayoutKey {
                image: crate::dbgeng::KernelImage::default(),
                session: 1,
            },
            globals: HashMap::new(),
            types: HashMap::new(),
        };
        let mut diagnostics = Vec::new();
        let roots = vs_roots(&memory, &layout, VS_CONTEXT, &mut diagnostics).unwrap();
        assert!(roots.is_empty());
        assert_eq!(diagnostics.len(), 1);
        assert!(diagnostics[0].contains("neither the context nor an affinity slot"));
    }

    const SPECIAL_PAGE: u64 = 0xffff_8c8f_13a0_2000;
    /// The byte Driver Verifier fills unused special-pool space with. A *fixture* value now,
    /// not a constant the walker knows: the page records its own fill byte, and the decoder
    /// reads it from there.
    const VERIFIER_FILL: u8 = 0xfd;
    /// `_POOL_HEADER` as Server 26100 x64 declares it: `PreviousSize`/`PoolIndex` share the
    /// USHORT at +0, `BlockSize`/`PoolType` the one at +2, and `Ulong1` overlays all four.
    const X64_POOL_HEADER: PoolHeaderLayout = PoolHeaderLayout {
        size: 0x10,
        previous_size: 0,
        pool_index: 0,
        block_size: 2,
        pool_type: 2,
        tag: 4,
    };

    /// `(usable, size, approximate)` — flattened so the assertions below stay readable and
    /// so every case states whether the size was corroborated or merely bounded.
    fn placed(placement: SpecialPlacement) -> (u64, u64, bool) {
        (placement.usable, placement.size, placement.approximate)
    }

    /// A special-pool page as the kernel writes one: the requested size in the low thirteen
    /// bits of `Ulong1`, the fill byte in `BlockSize`'s, the tag at +4, fill from the header's
    /// end to the block, the block against the page end, and fill again over the alignment
    /// padding behind it.
    fn special_page(requested: u32, tracked: bool) -> Vec<u8> {
        let mut page = vec![VERIFIER_FILL; PAGE_SIZE as usize];
        let word = requested | if tracked { 0x4000 } else { 0 } | (u32::from(VERIFIER_FILL) << 16);
        page[..4].copy_from_slice(&word.to_le_bytes());
        page[4..8].copy_from_slice(b"Tsp1");
        let start = PAGE_SIZE as usize - (requested as usize).next_multiple_of(16);
        page[start..start + requested as usize].fill(0x41);
        page
    }

    /// Decodes a fixture page and places its block, which is the path the walk takes.
    fn place(page: &[u8]) -> SpecialPlacement {
        let header =
            decode_special_pool_header(page, 0, X64_POOL_HEADER).expect("page describes a block");
        special_pool_placement(SPECIAL_PAGE, header, page)
    }

    /// The real values read off Server 26100.32995: a 0x68 message in special pool sits at
    /// page+0xf90, not page+0xf98. Rounding the request up to 16 is what makes the
    /// difference, and getting it wrong reports an address 8 bytes into the block.
    #[test]
    fn test_special_pool_block_is_pushed_against_the_page_end() {
        assert_eq!(
            placed(place(&special_page(0x68, false))),
            (SPECIAL_PAGE + 0xf90, 0x68, false)
        );
        // Already a multiple of 16: no rounding, block ends exactly at the page end.
        assert_eq!(
            placed(place(&special_page(0x40, false))),
            (SPECIAL_PAGE + 0xfc0, 0x40, false)
        );
    }

    /// The bug behind glslang/dbgscope#83, and the reason the size is no longer read as
    /// `_POOL_HEADER.PreviousSize`: eight bits cannot hold 0x140, so that allocation used to
    /// present as a 0x40 one — a value that fits the page perfectly well and that no range
    /// check could tell from a genuine 0x40. Thirteen bits hold anything a page can, so the
    /// two are now different numbers rather than the same one.
    #[test]
    fn test_special_pool_reads_a_size_that_does_not_fit_eight_bits() {
        let page = special_page(0x140, false);
        assert_eq!(
            page[0], 0x40,
            "the low byte alone is the value that used to be read"
        );
        assert_eq!(
            placed(place(&page)),
            (SPECIAL_PAGE + 0xec0, 0x140, false),
            "the block is 0x140 bytes at page+0xec0, not 0x40 bytes at page+0xfc0"
        );
        // The largest block a page can hold, which is where the mask has to stop being generous.
        assert_eq!(
            placed(place(&special_page(0xff0, false))),
            (SPECIAL_PAGE + 0x10, 0xff0, false)
        );
    }

    /// Verifier tracking puts eight bytes of its own between the pool header and the fill, and
    /// the page says so in `Ulong1`. Reading the fill from +0x10 there would compare tracking
    /// data against the fill byte and give up on a page that is perfectly well formed.
    #[test]
    fn test_special_pool_skips_verifiers_tracking_block() {
        let mut page = special_page(0x68, true);
        page[0x10..0x18].copy_from_slice(&0xdead_beef_u64.to_le_bytes());
        let header = decode_special_pool_header(&page, 0, X64_POOL_HEADER).unwrap();
        assert_eq!(header.header_size, 0x18);
        assert_eq!(header.requested, 0x68);
        assert_eq!(header.tag, u32::from_le_bytes(*b"Tsp1"));
        assert_eq!(
            placed(special_pool_placement(SPECIAL_PAGE, header, &page)),
            (SPECIAL_PAGE + 0xf90, 0x68, false)
        );
    }

    /// The corroboration the old check left on the floor: the kernel compares the alignment
    /// padding behind the block against the fill too, and up to fifteen bytes of evidence is
    /// worth having when the alternative is trusting the leading run alone.
    #[test]
    fn test_special_pool_checks_the_padding_behind_the_block() {
        let mut page = special_page(0x68, false);
        page[PAGE_SIZE as usize - 1] = 0x41;
        assert_eq!(
            placed(place(&page)),
            (SPECIAL_PAGE + 0x10, PAGE_SIZE - 0x10, true)
        );
    }

    /// The fill byte comes off the page, so a page filled with something else still resolves
    /// precisely — and one whose fill disagrees with its own recorded byte does not.
    #[test]
    fn test_special_pool_uses_the_fill_byte_the_page_records() {
        let mut page = special_page(0x68, false);
        page[0x10..0xf90].fill(0xc0);
        page[0xff8..].fill(0xc0);
        page[2] = 0xc0;
        assert_eq!(
            placed(place(&page)),
            (SPECIAL_PAGE + 0xf90, 0x68, false),
            "0xfd is Verifier's default, not part of the format"
        );
        page[2] = VERIFIER_FILL;
        assert_eq!(
            placed(place(&page)),
            (SPECIAL_PAGE + 0x10, PAGE_SIZE - 0x10, true)
        );
    }

    /// A start-aligned page (`MmSpecialPoolCatchOverruns` clear) puts the block next to the
    /// preceding guard page, so the end-of-page formula would be wrong. The fill check
    /// declines instead of reporting a bogus address.
    #[test]
    fn test_special_pool_declines_a_start_aligned_page() {
        let mut page = special_page(0x68, false);
        page[0xf90..].fill(VERIFIER_FILL);
        page[0x10..0x78].fill(0x41); // block immediately after the header
        assert_eq!(
            placed(place(&page)),
            (SPECIAL_PAGE + 0x10, PAGE_SIZE - 0x10, true)
        );
    }

    /// A page that describes no block a page could hold is not one we read correctly, and the
    /// walk skips it rather than reporting a span from it.
    #[test]
    fn test_special_pool_header_refuses_an_impossible_block() {
        let mut page = special_page(0x68, false);
        page[..4].copy_from_slice(&(u32::from(VERIFIER_FILL) << 16).to_le_bytes());
        assert!(decode_special_pool_header(&page, 0, X64_POOL_HEADER).is_none());
        // 0x1000 bytes cannot share a 0x1000 byte page with the header in front of them, and
        // the mask reaches 0x1fff, so this is a value the field can really hold.
        page[..4].copy_from_slice(&(0x1000 | (u32::from(VERIFIER_FILL) << 16)).to_le_bytes());
        assert!(decode_special_pool_header(&page, 0, X64_POOL_HEADER).is_none());
        // Tracked, and one alignment step too large to leave room for the tracking block.
        page[..4]
            .copy_from_slice(&(0xfe8 | 0x4000 | (u32::from(VERIFIER_FILL) << 16)).to_le_bytes());
        assert!(decode_special_pool_header(&page, 0, X64_POOL_HEADER).is_none());
    }

    /// A partially readable page corroborates nothing about where the block sits, so the
    /// end-of-page placement must not be used and the size must not exceed what was read.
    #[test]
    fn test_special_pool_fallback_respects_the_readable_length() {
        let mut partial = special_page(0x68, false);
        partial.truncate(0x200);
        assert_eq!(placed(place(&partial)), (SPECIAL_PAGE + 0x10, 0x1f0, true));
    }

    fn special_region(address: u64, pages: usize) -> PoolRegion {
        PoolRegion {
            address,
            size: pages * PAGE_SIZE as usize,
            requested_size: None,
            pool_kind: PoolKind::SpecialNonPagedNx,
            numa_node: 0,
            heap: HeapIdentity {
                pool_state: 0,
                heap: 0,
                special: true,
            },
            subsegment: None,
            backend: PoolBackend::Segment,
            unit_size: PAGE_SIZE as u32,
            bitmap: Vec::new(),
            heap_key: 0,
            pool_header: X64_POOL_HEADER,
            vs_header_size: 0,
            vs_sizes_offset: 0,
            vs_skips_page_end: false,
            known_tag: None,
            states: Vec::new(),
            reusable_chunks: Arc::default(),
            cached_chunks: Arc::default(),
            big_pool: Arc::default(),
        }
    }

    const VS_BASE: u64 = 0x2000;
    const VS_HEAP_KEY: u64 = 0x1234_5678_9abc_def0;

    fn vs_region(size: usize) -> PoolRegion {
        PoolRegion {
            address: VS_BASE,
            size,
            requested_size: None,
            pool_kind: PoolKind::NonPagedNx,
            numa_node: 0,
            heap: HeapIdentity {
                pool_state: 0,
                heap: 0,
                special: false,
            },
            subsegment: Some(VS_BASE),
            backend: PoolBackend::Vs,
            unit_size: 0,
            bitmap: Vec::new(),
            heap_key: VS_HEAP_KEY,
            pool_header: X64_POOL_HEADER,
            vs_header_size: 0x10,
            vs_sizes_offset: 0,
            vs_skips_page_end: false,
            known_tag: None,
            states: Vec::new(),
            reusable_chunks: Arc::default(),
            cached_chunks: Arc::default(),
            big_pool: Arc::default(),
        }
    }

    /// VS chunks laid end to end from [`VS_BASE`]: each carries its `Sizes` word, then the pool
    /// header, then its data. `(size, previous)` are both bytes, and `previous` is written as
    /// given so the chain can be broken on purpose.
    fn vs_extent(chunks: &[(usize, usize)]) -> Vec<u8> {
        let mut bytes = vec![0u8; chunks.iter().map(|(size, _)| size).sum()];
        let mut offset = 0;
        for &(size, previous) in chunks {
            let decoded =
                ((size as u64 / 16) << 16) | ((previous as u64 / 16) << 32) | (1u64 << 48);
            let encoded = decoded ^ VS_HEAP_KEY ^ (VS_BASE + offset as u64);
            bytes[offset..offset + 8].copy_from_slice(&encoded.to_le_bytes());
            let pool = offset + 0x10;
            bytes[pool..pool + 4].copy_from_slice(&0x0001_0000u32.to_le_bytes());
            bytes[pool + 4..pool + 8].copy_from_slice(b"VS!!");
            offset += size;
        }
        bytes
    }

    fn walk_vs_extent(bytes: &[u8]) -> PoolSnapshot {
        walk_vs_region(vs_region(bytes.len()), bytes)
    }

    fn walk_vs_region(region: PoolRegion, bytes: &[u8]) -> PoolSnapshot {
        let memory = FlatMemory::new(VS_BASE, bytes.len());
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };
        walker.walk_vs(&region, VS_BASE, bytes, Some(VS_BASE), &mut snapshot);
        snapshot
    }

    /// The measured shape of a VS chunk whose header sits at page offset 0xfe0 under
    /// `PageAlignLargeAllocs` ([`PoolRegion::vs_skips_page_end`]): its user block is the next
    /// page start, and the unit before that holds whatever the chunk held while it was free —
    /// on `ctf-vm`, two tree pointers whose top half reads as a tag.
    ///
    /// Three chunks from [`VS_BASE`], the middle one at `0x2fe0` with its pool header at
    /// `0x3000` and the stale pointers at `0x2ff0`, as `dq 0xffffe78459203fe0` read them on
    /// 2026-10-14 (`c1914c0d038da756 ffffe78400000056 | ffffe78459207e18 ffffe78459213bc8 |
    /// 54446d4d03060000`).
    fn vs_extent_with_a_user_block_moved_off_the_page_end() -> Vec<u8> {
        let mut bytes = vs_extent(&[(0xfe0, 0), (0x80, 0xfe0), (0x40, 0x80)]);
        bytes[0xff0..0xff8].copy_from_slice(&0xffff_e784_5920_7e18u64.to_le_bytes());
        bytes[0xff8..0x1000].copy_from_slice(&0xffff_e784_5921_3bc8u64.to_le_bytes());
        bytes[0x1000..0x1004].copy_from_slice(&0x0306_0000u32.to_le_bytes());
        bytes[0x1004..0x1008].copy_from_slice(b"MmDT");
        bytes
    }

    fn span_of_class(snapshot: &PoolSnapshot, size_class: u32) -> &PoolSpan {
        snapshot
            .spans
            .iter()
            .find(|span| span.size_class == size_class)
            .unwrap_or_else(|| panic!("no span of class {size_class:#x}: {:?}", snapshot.spans))
    }

    /// Under the flag, a user block that would begin in a page's last unit begins the next page:
    /// the header is where `!pool` says (`size: 60`, so 0x50 usable), the tag is the real one,
    /// and the chain is untouched — the chunk after is found where the sizes put it.
    #[test]
    fn test_a_vs_user_block_that_would_end_a_page_begins_the_next_one() {
        let bytes = vs_extent_with_a_user_block_moved_off_the_page_end();
        let mut region = vs_region(bytes.len());
        region.vs_skips_page_end = true;

        let snapshot = walk_vs_region(region, &bytes);

        let moved = span_of_class(&snapshot, 0x80);
        assert_eq!(moved.header_address, VS_BASE + 0x1000);
        assert_eq!(moved.usable_address, VS_BASE + 0x1010);
        assert_eq!(moved.size, 0x50);
        assert_eq!(moved.raw_tag, u32::from_le_bytes(*b"MmDT"));
        let after = span_of_class(&snapshot, 0x40);
        assert_eq!(after.header_address, VS_BASE + 0x1060 + 0x10);
        assert_eq!(after.raw_tag, u32::from_le_bytes(*b"VS!!"));
        assert_eq!(snapshot.spans.len(), 3);
        assert!(snapshot.complete, "{:?}", snapshot.diagnostics);
    }

    /// The control, and what the flag gates: without it the same bytes read as they did before
    /// the walk consulted it — the stale pointer as the header, its top half as the tag, a span
    /// a unit early and a unit long. That is what a target **without** the flag would hold, so
    /// it is what the walk has to go on reading there.
    #[test]
    fn test_without_the_flag_a_page_end_user_block_is_read_at_its_chunk_header() {
        let bytes = vs_extent_with_a_user_block_moved_off_the_page_end();

        let snapshot = walk_vs_extent(&bytes);

        let unmoved = span_of_class(&snapshot, 0x80);
        assert_eq!(unmoved.header_address, VS_BASE + 0xff0);
        assert_eq!(unmoved.size, 0x60);
        assert_eq!(unmoved.raw_tag, 0xffff_e784);
    }

    /// The allocator's test is `chunk + 0x20` page-aligned, not `chunk + 0x10`: a user block
    /// that begins exactly on a page boundary is not in the last unit of the page before, and
    /// stays where it is.
    #[test]
    fn test_a_vs_user_block_on_a_page_boundary_is_left_where_it_is() {
        let bytes = vs_extent(&[(0xff0, 0), (0x40, 0xff0)]);
        let mut region = vs_region(bytes.len());
        region.vs_skips_page_end = true;

        let snapshot = walk_vs_region(region, &bytes);

        let on_boundary = span_of_class(&snapshot, 0x40);
        assert_eq!(on_boundary.header_address, VS_BASE + 0x1000);
        assert_eq!(on_boundary.size, 0x20);
        assert_eq!(on_boundary.raw_tag, u32::from_le_bytes(*b"VS!!"));
    }

    /// A chunk at offset 0xfe0 with no unit to skip — two headers' worth, the smallest remainder
    /// a split leaves — keeps its user block in place rather than having it placed at its own
    /// end, which is the next chunk's header; and since the fixture marks every chunk allocated,
    /// which the allocator would never do to one this short under the flag, the walk says so.
    /// Raised by Codex on dbgscope#199.
    #[test]
    fn test_a_vs_chunk_too_short_to_move_its_user_block_is_read_in_place() {
        let bytes = vs_extent(&[(0xfe0, 0), (0x20, 0xfe0), (0x40, 0x20)]);
        let mut region = vs_region(bytes.len());
        region.vs_skips_page_end = true;

        let snapshot = walk_vs_region(region, &bytes);

        let short = span_of_class(&snapshot, 0x20);
        assert_eq!(short.header_address, VS_BASE + 0xff0);
        assert_eq!(short.usable_address, VS_BASE + 0x1000);
        assert_eq!(short.size, 0);
        let after = span_of_class(&snapshot, 0x40);
        assert_eq!(after.header_address, VS_BASE + 0x1010);
        assert_eq!(after.raw_tag, u32::from_le_bytes(*b"VS!!"));
        assert_eq!(snapshot.spans.len(), 3);
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|line| line.contains("too short to hold a user block moved off the page end")),
            "{:?}",
            snapshot.diagnostics
        );
    }

    /// A VS chunk too large for `_POOL_HEADER.BlockSize` carries no header, and its tag is in
    /// the big-page table like any other big-pool allocation.
    ///
    /// `BlockSize` is eight bits of sixteen-byte units, so 4080 bytes -- header included -- is
    /// the most it can describe and anything needing a page has no header at all. The two
    /// allocators that produce such a chunk are indistinguishable by their page range
    /// descriptors, which is why the size answers this rather than the backend: measured on a
    /// live 26100 kernel, `!pool` calls `0xffffac09da29f000` an `MiRr` allocation of `0xe1c0`
    /// bytes and the walk called it 57,792 untagged bytes -- the same length, with the name
    /// lost (`windbg-mcp` FOLLOWUPS item 99).
    ///
    /// The fixture writes a *valid* pool header into the big chunk on purpose. Without one the
    /// wrong answer would be an absent tag, which a fixture of zeroes cannot tell from the right
    /// one -- and the tag it writes is the one the small chunk beside it legitimately carries,
    /// so a walk that reads the header anyway reports something entirely plausible.
    #[test]
    fn test_a_vs_chunk_too_large_for_a_pool_header_is_tagged_from_the_big_page_table() {
        // One chunk of 0x2000 -- far past what `BlockSize` can encode -- then an ordinary one.
        let mut bytes = vs_extent(&[(0x2000, 0), (0x40, 0x2000)]);
        let mut region = vs_region(bytes.len());
        // The allocation the table names: page-aligned, as every live entry's `Va` is, and
        // short of the chunk that holds it by the header space `nt` did not need.
        let allocation = (VS_BASE + 0x10).next_multiple_of(PAGE_SIZE);
        region.big_pool = Arc::new(HashMap::from([(
            allocation,
            BigPageEntry {
                tag: u32::from_le_bytes(*b"BIGV"),
                size: 0x1000,
            },
        )]));
        // And a second entry, for an allocation this extent does not hold, so the match has to
        // be by containment rather than by "the region names exactly one".
        Arc::get_mut(&mut region.big_pool)
            .unwrap()
            .insert(VS_BASE + 0x9000, BigPageEntry { tag: 0, size: 0x10 });
        let _ = &mut bytes;
        let memory = FlatMemory::new(VS_BASE, bytes.len());
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };

        walker.walk_vs(&region, VS_BASE, &bytes, Some(VS_BASE), &mut snapshot);

        let big = snapshot
            .spans
            .iter()
            .find(|span| span.size_class == 0x2000)
            .unwrap_or_else(|| panic!("the large chunk is missing: {:?}", snapshot.spans));
        assert_eq!(super::super::decode::display_tag(big.raw_tag), "BIGV");
        // The table's `Va` is where the allocation begins and `NumberOfBytes` is how long it is;
        // neither is the chunk, and the phantom header is gone from both.
        assert_eq!(big.header_address, allocation);
        assert_eq!(big.usable_address, allocation);
        assert_eq!(big.size, 0x1000);
        // The chunk beside it is under the limit, so it keeps the header it really has.
        let small = snapshot
            .spans
            .iter()
            .find(|span| span.size_class == 0x40)
            .unwrap_or_else(|| panic!("the small chunk is missing: {:?}", snapshot.spans));
        assert_eq!(super::super::decode::display_tag(small.raw_tag), "VS!!");
    }

    /// An entry a chunk could not really hold is not that chunk's, however well it lines up.
    ///
    /// Containment alone is the cheap half of the match: an address inside the chunk. The length
    /// is what says the two are the same allocation, and without it a chunk would adopt the
    /// entry of whatever happens to begin inside it -- reporting somebody else's tag against a
    /// length that does not fit, which is worse than the untagged answer it replaces.
    #[test]
    fn test_a_big_page_entry_too_long_for_its_chunk_is_not_claimed_by_it() {
        let bytes = vs_extent(&[(0x2000, 0), (0x40, 0x2000)]);
        let mut region = vs_region(bytes.len());
        let allocation = (VS_BASE + 0x10).next_multiple_of(PAGE_SIZE);
        region.big_pool = Arc::new(HashMap::from([(
            allocation,
            BigPageEntry {
                tag: u32::from_le_bytes(*b"BIGV"),
                // One byte past what the chunk holding `allocation` can contain.
                size: (VS_BASE + 0x2000 - allocation) + 1,
            },
        )]));
        let memory = FlatMemory::new(VS_BASE, bytes.len());
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };

        walker.walk_vs(&region, VS_BASE, &bytes, Some(VS_BASE), &mut snapshot);

        let big = snapshot
            .spans
            .iter()
            .find(|span| span.size_class == 0x2000)
            .unwrap_or_else(|| panic!("the large chunk is missing: {:?}", snapshot.spans));
        assert_ne!(super::super::decode::display_tag(big.raw_tag), "BIGV");
        assert_eq!(big.header_address, VS_BASE + 0x10);
    }

    /// glslang/dbgscope#93: "884x rejecting implausible VS chunk size # at #" counted the
    /// *extents* that contained a refusal, because the message was latched behind a bool. How
    /// many chunks were refused was reported nowhere — and a refusal is not free, since the
    /// walk then advances sixteen bytes at a time and decodes every later header in the extent
    /// at a guessed offset.
    #[test]
    fn test_vs_refusals_are_counted_as_chunks_not_as_extents() {
        let mut bytes = vs_extent(&[(0x40, 0), (0x40, 0x40), (0x40, 0x40)]);
        bytes.extend_from_slice(&[0u8; 0x40]); // three more header-sized strides of nothing
        let snapshot = walk_vs_extent(&bytes);

        assert_eq!(snapshot.spans.len(), 3);
        assert_eq!(snapshot.refused_chunks, 3);
        assert!(!snapshot.complete);
        let examples = snapshot.diagnostics.examples();
        assert_eq!(
            examples
                .iter()
                .filter(|message| message.starts_with("refusing VS chunk at"))
                .count(),
            1,
            "the detail is a sample, not one line per refusal: {examples:?}"
        );
        assert!(
            examples.iter().any(|message| message
                == "3 VS chunk headers refused, resynchronising from 0x20c0"),
            "{examples:?}"
        );
    }

    /// One predicate covered two different failures and printed one sentence for both, so a
    /// live run could not say which was happening. Distinct prose keeps them distinct shapes
    /// under diagnostic collapse while the numbers still fold.
    #[test]
    fn test_vs_says_which_check_a_chunk_failed() {
        let refuse = |size: usize, end: u64| {
            let decoded = (size as u64 / 16) << 16;
            decode_vs_chunk(
                decoded ^ VS_HEAP_KEY ^ VS_BASE,
                VS_BASE,
                VS_HEAP_KEY,
                0x20,
                end,
            )
            .unwrap_err()
        };
        assert_eq!(refuse(0, 0x3000).reason, "the size word decodes to zero");
        assert_eq!(
            refuse(0x10, 0x3000).reason,
            "the chunk is smaller than its own headers"
        );
        assert_eq!(
            refuse(0x100, 0x2080).reason,
            "the chunk runs past the end of its subsegment"
        );
        // The word as read travels with the refusal, so a header can be re-decoded by hand
        // against another candidate mix without another walk of the target.
        assert_eq!(
            refuse(0x10, 0x3000).encoded,
            (1u64 << 16) ^ VS_HEAP_KEY ^ VS_BASE
        );
        // A subsegment's last chunk reaches its boundary exactly, and refusing that would drop
        // a well-formed chunk from every subsegment that is fully occupied.
        assert_eq!(
            decode_vs_chunk(
                ((0x80u64 / 16) << 16) ^ VS_HEAP_KEY ^ VS_BASE,
                VS_BASE,
                VS_HEAP_KEY,
                0x20,
                VS_BASE + 0x80,
            )
            .unwrap()
            .size,
            0x80
        );
    }

    /// The corroboration the decoder had in hand and threw away (`let _ = sizes.previous_size`).
    /// It is what tells the two explanations for a refusal apart: a chain that holds either
    /// side of one bad chunk is a header rewritten while we read it, while a chain that never
    /// holds says the stride itself is wrong and the chunks behind it are fiction.
    #[test]
    fn test_vs_reports_a_chunk_that_disagrees_with_its_predecessor() {
        let snapshot = walk_vs_extent(&vs_extent(&[(0x40, 0), (0x40, 0x40), (0x40, 0x20)]));

        assert_eq!(
            snapshot.spans.len(),
            3,
            "the chunk itself is still plausible"
        );
        assert_eq!(snapshot.refused_chunks, 0);
        assert!(!snapshot.complete);
        let examples = snapshot.diagnostics.examples();
        assert!(
            examples.iter().any(|message| message
                == "VS chunk at 0x2080 records a previous size of 0x20 where the chunk before it measured 0x40"),
            "{examples:?}"
        );
        assert!(
            examples
                .iter()
                .any(|message| message.starts_with("1 VS chunks disagreed")),
            "{examples:?}"
        );
        // A chain that holds says nothing at all.
        let quiet = walk_vs_extent(&vs_extent(&[(0x40, 0), (0x40, 0x40), (0x40, 0x40)]));
        assert!(quiet.complete);
        assert!(quiet.diagnostics.is_empty(), "{:?}", quiet.diagnostics);
    }

    /// A memory source with gaps in it, answering `valid_region` two ways the debugger does.
    ///
    /// A **hole** is a page with nothing committed: the query looks past it and names where the
    /// next committed run begins, which the walk records and steps over. A **stall** is the
    /// query naming the address it was given and reporting no length with it — no answer the
    /// walk can advance on, and the shape behind the 3,285 `valid-region query made no
    /// progress` lines a live 26100 walk emitted.
    struct HoleyMemory {
        base: u64,
        bytes: Vec<u8>,
        holes: HashSet<u64>,
        stalls: HashSet<u64>,
        /// Pages where the query answers `0x0+0x0` — the engine's way of saying it found
        /// nothing valid in the whole span, as opposed to `stalls`, which model it finding
        /// something and failing to size it. The two look alike at the call site and mean
        /// opposite things about what lies behind them.
        blind: HashSet<u64>,
        /// What the *memory manager* says, which is a separate axis from every field above:
        /// those decide what reads, this decides whether there was anything there to read.
        ///
        /// `None` is a source that cannot answer — the debugger against a kernel target, or a
        /// dump recording no memory information — and is the default, so every test written
        /// before this existed keeps its old meaning. `Some` names the pages with no pages
        /// behind them.
        uncommitted: Option<HashSet<u64>>,
        queries: Cell<usize>,
        commit_queries: Cell<usize>,
    }

    impl HoleyMemory {
        fn new(base: u64, bytes: Vec<u8>) -> Self {
            Self {
                base,
                bytes,
                holes: HashSet::new(),
                stalls: HashSet::new(),
                blind: HashSet::new(),
                uncommitted: None,
                queries: Cell::new(0),
                commit_queries: Cell::new(0),
            }
        }

        /// Give this source a memory manager, telling it which pages hold nothing.
        fn with_memory_manager(mut self, uncommitted: &[u64]) -> Self {
            self.uncommitted = Some(uncommitted.iter().copied().collect());
            self
        }

        fn end(&self) -> u64 {
            self.base + self.bytes.len() as u64
        }

        fn committed(&self, page: u64) -> bool {
            page >= self.base
                && page < self.end()
                && !self.holes.contains(&page)
                && !self.stalls.contains(&page)
                && !self.blind.contains(&page)
        }
    }

    impl PoolMemory for HoleyMemory {
        fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            let offset = (address - self.base) as usize;
            self.bytes
                .get(offset..offset + size)
                .map(<[u8]>::to_vec)
                .ok_or_else(|| SnapshotError::InvalidData {
                    detail: format!("read past the fixture at {address:#x}+{size:#x}"),
                })
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            self.queries.set(self.queries.get() + 1);
            let page = address & !(PAGE_SIZE - 1);
            if self.blind.contains(&page) {
                return Ok((0, 0));
            }
            if self.stalls.contains(&page) {
                return Ok((address, 0));
            }
            let mut next = page;
            while next < self.end() && !self.committed(next) {
                next += PAGE_SIZE;
            }
            if next >= self.end() {
                // Nothing committed from here on, so the engine names a base past the span
                // that was asked about. That is the region *ending*, not a failure to advance.
                return Ok((self.end(), 0));
            }
            let base = next.max(address);
            let mut run = next;
            while self.committed(run) {
                run += PAGE_SIZE;
            }
            Ok((base, (run.min(address + size as u64) - base) as usize))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }

        /// Answers in **runs**, as the memory manager does: like pages are merged, so a walk
        /// stepping a gap of any length costs one query per run and not one per page. The
        /// counter is what lets a test say so.
        fn committed_run(&self, address: u64) -> Option<CommitRun> {
            let uncommitted = self.uncommitted.as_ref()?;
            self.commit_queries.set(self.commit_queries.get() + 1);
            let mut page = address & !(PAGE_SIZE - 1);
            let committed = !uncommitted.contains(&page);
            loop {
                page += PAGE_SIZE;
                if page >= self.end() {
                    // Past the fixture the whole rest of the address space is one run of the
                    // same kind, which keeps a run always able to advance the caller.
                    return Some(CommitRun {
                        end: u64::MAX,
                        committed,
                    });
                }
                if !uncommitted.contains(&page) != committed {
                    return Some(CommitRun {
                        end: page,
                        committed,
                    });
                }
            }
        }
    }

    /// Three special-pool pages, so each committed page the walk reaches shows up as exactly
    /// one span at a known address — which is what makes "did it walk past the gap" checkable.
    fn special_pages(count: usize) -> Vec<u8> {
        let mut bytes = Vec::new();
        for _ in 0..count {
            bytes.extend_from_slice(&special_page(0x68, false));
        }
        bytes
    }

    fn walk_holey(memory: &HoleyMemory, region: &PoolRegion) -> PoolSnapshot {
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };
        walker.walk_region(region, &mut snapshot).unwrap();
        snapshot
    }

    /// glslang/dbgscope#94. One page the query cannot advance over used to cost every
    /// committed page behind it: the walk filed the rest of the region as unreadable and
    /// `break`ed, while the sibling case one branch above — a query reporting a *gap* —
    /// recorded the hole and carried on. The samples show these firing part-way through
    /// regions tens of pages long, so what was given up on was not a tail.
    #[test]
    fn test_a_stalled_query_costs_a_page_not_the_region() {
        let mut memory = HoleyMemory::new(SPECIAL_PAGE, special_pages(4));
        memory.stalls.insert(SPECIAL_PAGE + 0x1000);
        let snapshot = walk_holey(&memory, &special_region(SPECIAL_PAGE, 4));

        let allocated: Vec<_> = snapshot
            .spans
            .iter()
            .filter(|span| span.state == PoolState::Allocated)
            .map(|span| span.usable_address)
            .collect();
        assert_eq!(
            allocated,
            [
                SPECIAL_PAGE + 0xf90,
                SPECIAL_PAGE + 0x2f90,
                SPECIAL_PAGE + 0x3f90
            ],
            "the pages behind the stalled one must still be walked"
        );
        assert!(
            snapshot
                .spans
                .iter()
                .any(|span| span.state == PoolState::Unreadable
                    && span.header_address == SPECIAL_PAGE + 0x1000
                    && span.size == PAGE_SIZE),
            "the page that stalled is still filed as unreadable"
        );
        assert!(!snapshot.complete);
        assert_eq!(
            snapshot.stalls,
            WalkStalls {
                pages: 1,
                skipped_bytes: PAGE_SIZE,
                // Two pages of committed memory on the far side of the stall — what the old
                // `break` reported as nothing at all.
                recovered_bytes: 2 * PAGE_SIZE,
            }
        );
    }

    /// A region begins wherever the allocator put it — `…de6030` and `…e02130` in the live
    /// samples — so a stall can land at any page offset. Advancing a page's *worth of bytes*
    /// from there writes off the tail of the page that stalled and the head of the page after
    /// it, discarding a healthy page in order to skip a bad one: this branch's own mistake, in
    /// miniature. The other stall tests miss it because their region is page-aligned.
    #[test]
    fn test_a_stall_part_way_through_a_page_does_not_swallow_the_next_one() {
        let mut memory = HoleyMemory::new(SPECIAL_PAGE, special_pages(3));
        memory.stalls.insert(SPECIAL_PAGE);
        // Starting 0x30 bytes short of the second page, as an unaligned region does.
        let mut region = special_region(SPECIAL_PAGE, 3);
        region.address = SPECIAL_PAGE + 0xfd0;
        region.size = 3 * PAGE_SIZE as usize - 0xfd0;
        let snapshot = walk_holey(&memory, &region);

        assert_eq!(
            snapshot.stalls.skipped_bytes, 0x30,
            "only the rest of the page that stalled is written off"
        );
        let allocated: Vec<_> = snapshot
            .spans
            .iter()
            .filter(|span| span.state == PoolState::Allocated)
            .map(|span| span.usable_address)
            .collect();
        assert_eq!(
            allocated,
            [SPECIAL_PAGE + 0x1f90, SPECIAL_PAGE + 0x2f90],
            "the page immediately after the stall is healthy and must still be walked"
        );
    }

    /// The gap spans of a walk, as `(state, address, size)`, in address order.
    fn gaps(snapshot: &PoolSnapshot) -> Vec<(PoolState, u64, u64)> {
        let mut gaps: Vec<_> = snapshot
            .spans
            .iter()
            .filter(|span| !span.state.is_chunk())
            .map(|span| (span.state, span.header_address, span.size))
            .collect();
        gaps.sort_by_key(|&(_, address, _)| address);
        gaps
    }

    /// `windbg-mcp` FOLLOWUPS item 98. The same page that does not read is a hole in the walk's
    /// coverage or is nothing at all, and **only the memory manager can say which** — so this
    /// runs one fixture past three memory managers and changes nothing else.
    ///
    /// The three answers are the whole rule. A source that says the page holds nothing files
    /// [`PoolState::Uncommitted`] and leaves `complete` alone; one that says the page is
    /// committed files [`PoolState::Unreadable`] and clears it; and one that cannot say keeps
    /// the second reading, because *unknown* has to fall on the conservative side or the
    /// excuse is being granted by the absence of evidence.
    ///
    /// Measured live before it was written: on `sihost` (26200, 4 Segment Heaps, 19,459
    /// chunks, 2026-09-24) every one of the 45 unreadable spans holding the walk at `Partial`
    /// was `MEM_RESERVE`, and both controls — an allocated chunk and a free one — were
    /// `MEM_COMMIT`.
    #[test]
    fn test_only_the_memory_manager_decides_whether_a_gap_costs_coverage() {
        let gap = SPECIAL_PAGE + PAGE_SIZE;
        let walk = |memory: HoleyMemory| {
            let mut memory = memory;
            memory.holes.insert(gap);
            let snapshot = walk_holey(&memory, &special_region(SPECIAL_PAGE, 4));
            // The pages on either side are walked whatever the answer is: this changes how a
            // gap is *described*, never how much is read.
            assert_eq!(
                snapshot
                    .spans
                    .iter()
                    .filter(|span| span.state == PoolState::Allocated)
                    .count(),
                3,
                "the gap is one page of four"
            );
            (snapshot, memory.commit_queries.get())
        };

        let (nothing_there, queries) =
            walk(HoleyMemory::new(SPECIAL_PAGE, special_pages(4)).with_memory_manager(&[gap]));
        assert_eq!(
            gaps(&nothing_there),
            [(PoolState::Uncommitted, gap, PAGE_SIZE)],
            "a page with nothing behind it is not a hole in what the walk saw"
        );
        assert!(
            nothing_there.complete,
            "and so the walk covered everything it set out to"
        );
        assert_eq!(
            queries, 1,
            "asked per run, not per page — a reserved tail of any length costs one query"
        );

        let (really_missed, _) =
            walk(HoleyMemory::new(SPECIAL_PAGE, special_pages(4)).with_memory_manager(&[]));
        assert_eq!(
            gaps(&really_missed),
            [(PoolState::Unreadable, gap, PAGE_SIZE)],
            "a committed page that will not read is memory the target has and we did not see"
        );
        assert!(!really_missed.complete);

        let (cannot_say, _) = walk(HoleyMemory::new(SPECIAL_PAGE, special_pages(4)));
        assert_eq!(
            gaps(&cannot_say),
            gaps(&really_missed),
            "a source with no answer keeps the conservative reading, not the convenient one"
        );
        assert!(!cannot_say.complete);
    }

    /// The diagnostic that goes with the gap, which is about *missed memory* and so has to be
    /// as conditional as the coverage is.
    ///
    /// It fired on every reserved tail before this, which on a live user-mode heap is all of
    /// them — a line per gap describing nothing wrong, and wrong about what it describes
    /// ("unreadable space" for address space that holds nothing). The same failure mode as
    /// glslang/dbgscope#94, where 3,285 such lines drowned out the diagnostics that meant
    /// something.
    #[test]
    fn test_a_gap_with_nothing_behind_it_is_not_worth_a_diagnostic() {
        let gap = SPECIAL_PAGE + PAGE_SIZE;
        let complains = |memory: HoleyMemory| {
            let mut memory = memory;
            memory.holes.insert(gap);
            let snapshot = walk_holey(&memory, &special_region(SPECIAL_PAGE, 4));
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("only committed through"))
        };

        assert!(
            !complains(
                HoleyMemory::new(SPECIAL_PAGE, special_pages(4)).with_memory_manager(&[gap])
            ),
            "nothing was missed, so there is nothing to report"
        );
        assert!(
            complains(HoleyMemory::new(SPECIAL_PAGE, special_pages(4)).with_memory_manager(&[])),
            "a committed page that would not read is still worth saying"
        );
        assert!(
            complains(HoleyMemory::new(SPECIAL_PAGE, special_pages(4))),
            "and so is one nothing could be asked about"
        );
    }

    /// A gap that crosses the boundary is **split**, because calling the whole of it by either
    /// name misreports the other half — and the half that is really missed still costs the
    /// walk its `complete`.
    ///
    /// This is the shape a live target actually produces: a subsegment's committed pages end
    /// part-way through the range that holds it, so the tail of a committed run and the
    /// reserved space after it reach the walk as one failed read.
    #[test]
    fn test_a_gap_straddling_the_commit_boundary_is_split_at_it() {
        let mut memory = HoleyMemory::new(SPECIAL_PAGE, special_pages(4))
            // Only the second of the two pages that will not read holds nothing.
            .with_memory_manager(&[SPECIAL_PAGE + 2 * PAGE_SIZE]);
        memory.holes.insert(SPECIAL_PAGE + PAGE_SIZE);
        memory.holes.insert(SPECIAL_PAGE + 2 * PAGE_SIZE);
        let snapshot = walk_holey(&memory, &special_region(SPECIAL_PAGE, 4));

        assert_eq!(
            gaps(&snapshot),
            [
                (PoolState::Unreadable, SPECIAL_PAGE + PAGE_SIZE, PAGE_SIZE),
                (
                    PoolState::Uncommitted,
                    SPECIAL_PAGE + 2 * PAGE_SIZE,
                    PAGE_SIZE
                ),
            ],
            "two spans, each the size of what it describes"
        );
        assert!(
            !snapshot.complete,
            "half of it was memory the target has, and that half is still missing"
        );
    }

    /// A memory manager whose answer cannot advance the walk, which is the one way this loop
    /// could hang rather than misreport.
    ///
    /// Not hypothetical: `walk_region` already defends against the engine naming a region
    /// *behind* the cursor, and the same answer reaches here. The rest of the span is filed
    /// the conservative way and the loop ends.
    ///
    /// **Deleting the guard hangs this test rather than failing it** — verified 2026-09-24,
    /// which is the point: without it a walk that met such an answer would never return, and
    /// a wedged engine process is the one failure this module cannot report its way out of.
    #[test]
    fn test_a_commit_run_that_cannot_advance_is_no_answer_at_all() {
        struct Stuck(HoleyMemory);
        impl PoolMemory for Stuck {
            fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
                self.0.read_exact(address, size)
            }
            fn valid_region(
                &self,
                address: u64,
                size: usize,
            ) -> Result<(u64, usize), SnapshotError> {
                self.0.valid_region(address, size)
            }
            fn interrupted(&self) -> Result<bool, SnapshotError> {
                Ok(false)
            }
            fn committed_run(&self, address: u64) -> Option<CommitRun> {
                // Names the address it was given, so a caller that trusted it would ask the
                // same question forever.
                Some(CommitRun {
                    end: address,
                    committed: false,
                })
            }
        }

        let mut inner = HoleyMemory::new(SPECIAL_PAGE, special_pages(4));
        inner.holes.insert(SPECIAL_PAGE + PAGE_SIZE);
        let memory = Stuck(inner);
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };
        walker
            .walk_region(&special_region(SPECIAL_PAGE, 4), &mut snapshot)
            .unwrap();

        assert_eq!(
            gaps(&snapshot),
            [(PoolState::Unreadable, SPECIAL_PAGE + PAGE_SIZE, PAGE_SIZE)],
            "an answer that cannot advance is discarded, not acted on"
        );
        assert!(!snapshot.complete);
    }

    /// A VS subsegment with a hole in it, walked in two committed extents.
    ///
    /// `bytes` tiles the whole region with chunks the way the allocator does; `holes` names the
    /// pages `valid_region` will report as uncommitted. That is what
    /// `RtlpHpVsSubsegmentCommitPages` produces on a real target — it commits and decommits page
    /// ranges anywhere inside the subsegment and tracks them in `_HEAP_VS_SUBSEGMENT.CommitBitmap`
    /// — and it is the shape the old walk had no way to survive.
    fn walk_vs_with_holes(chunks: &[(usize, usize)], holes: &[u64]) -> PoolSnapshot {
        walk_vs_holes(chunks, holes, false)
    }

    /// `walk_vs_with_holes`, plus a memory manager that confirms the holes hold nothing —
    /// which is what a live target's does, and what the walk needs before it can treat a
    /// chunk running into one as fully understood.
    fn walk_vs_holes(chunks: &[(usize, usize)], holes: &[u64], asked: bool) -> PoolSnapshot {
        let bytes = vs_extent(chunks);
        let region = vs_region(bytes.len());
        let mut memory = HoleyMemory::new(VS_BASE, bytes);
        memory.holes.extend(holes.iter().copied());
        if asked {
            memory = memory.with_memory_manager(holes);
        }
        walk_holey(&memory, &region)
    }

    /// glslang/dbgscope#103: 106,516 VS chunk headers refused on one live 26100 walk, ~196 per
    /// extent, every one of them failing the same subsegment-bound check — which read as a bound
    /// that was wrong. It was not. Nothing was wrong with any of the three predicates: the walk
    /// was handing them a *guess*, because it started every committed extent at that extent's own
    /// first sixteen-byte boundary, and only the first extent of a subsegment begins on a chunk.
    ///
    /// Here the chunk before the hole is 0x2800 bytes and lands the next header at 0x5800, in the
    /// middle of the page the walk resumes on. Starting at 0x5000 costs 128 refusals to scan back
    /// onto it; following the chain costs none.
    #[test]
    fn test_a_vs_extent_after_a_hole_resumes_on_the_chunk_the_chain_names() {
        let snapshot = walk_vs_with_holes(
            &[
                (0x800, 0),
                (0x800, 0x800),
                // Spans the decommitted page, as the free chunk whose interior was decommitted
                // always does, and ends part-way into the page after it.
                (0x2800, 0x800),
                (0x800, 0x2800),
            ],
            &[VS_BASE + 0x2000],
        );

        assert_eq!(
            snapshot.refused_chunks,
            0,
            "the chain names the header, so nothing has to be scanned for: {:?}",
            snapshot.diagnostics.examples()
        );
        assert_eq!(snapshot.unplaced_bytes, 0);
        let allocated: Vec<_> = snapshot
            .spans
            .iter()
            .filter(|span| span.state == PoolState::Allocated)
            .map(|span| span.header_address)
            .collect();
        // The chunk that spans the hole is not among them: its header was read and understood,
        // but the walk emits no span for a chunk it could not read to the end of.
        assert_eq!(
            allocated,
            [VS_BASE + 0x10, VS_BASE + 0x810, VS_BASE + 0x3810],
            "the chunk on the far side of the hole is found, and nothing else is invented"
        );
    }

    /// The other half of the same rule, and the price of it. When the header the chain names
    /// falls *inside* the hole, the walk has no way to know where a chunk begins in the extent
    /// after it — so it decodes none of it and says how much that cost. The extent here holds two
    /// perfectly good chunks; declining them loses coverage, and decoding from a guess would put
    /// whatever a garbage word decoded to into `spans` as an allocation, which is worse.
    #[test]
    fn test_a_vs_extent_whose_chunk_boundary_fell_in_the_hole_is_not_guessed_at() {
        let snapshot = walk_vs_with_holes(
            &[
                (0x1000, 0),
                (0x1000, 0x1000),
                (0x1000, 0x1000),
                (0x1000, 0x1000),
            ],
            &[VS_BASE + 0x2000],
        );

        assert_eq!(snapshot.refused_chunks, 0);
        assert_eq!(
            snapshot.unplaced_bytes, 0x1000,
            "the whole undecodable extent is sized, not just noted"
        );
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("does not begin on a chunk boundary")),
            "{:?}",
            snapshot.diagnostics.examples()
        );
        let allocated: Vec<_> = snapshot
            .spans
            .iter()
            .filter(|span| span.state == PoolState::Allocated)
            .map(|span| span.header_address)
            .collect();
        assert_eq!(
            allocated,
            [VS_BASE + 0x10, VS_BASE + 0x1010],
            "nothing from the extent the walk could not place reaches the snapshot"
        );
    }

    /// An extent entirely inside one chunk. Nothing to decode there and nothing lost: the
    /// expectation still names a header further on, so this must not be billed as coverage the
    /// walk gave up — which is the difference between a hole in a large free chunk (ordinary) and
    /// a chain the walk dropped (not).
    #[test]
    fn test_an_extent_inside_a_single_chunk_costs_nothing() {
        let snapshot = walk_vs_with_holes(&[(0x1000, 0), (0x3000, 0x1000)], &[VS_BASE + 0x2000]);

        assert_eq!(snapshot.unplaced_bytes, 0);
        assert_eq!(snapshot.refused_chunks, 0);
        assert!(
            !snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("does not begin on a chunk boundary")),
            "{:?}",
            snapshot.diagnostics.examples()
        );
    }

    /// The chunk in the test above **is** billed as coverage the walk gave up, and that is the
    /// other half of `windbg-mcp` FOLLOWUPS item 98: a free chunk whose middle the allocator
    /// decommitted runs past the committed extent, so no span was emitted for it and
    /// `complete` cleared — silently, with no diagnostic saying which chunk or why.
    ///
    /// A span is geometry and state, and both are known here: the header was read, the size
    /// came out of it and passed the subsegment bound, and the state comes from the free tree.
    /// The only thing the walk could not see is the chunk's *contents*, which no span carries
    /// — so where the tail holds nothing the chunk is emitted like any other.
    ///
    /// The control is the same fixture with no memory manager, and it must still refuse: a
    /// tail the walk cannot ask about may be memory the target has, and a chunk that large
    /// with an unread middle is a real hole in what was seen.
    ///
    /// On `sihost` (26200, 2026-09-24) this was the last thing between a healthy live walk and
    /// `Complete` — five chunks, 0x1660 to 0xfe70 bytes, after all 48 gap spans had been
    /// classified.
    #[test]
    fn test_a_chunk_running_into_nothing_is_still_a_chunk() {
        let chunks = [(0x1000usize, 0usize), (0x3000, 0x1000)];
        let holes = [VS_BASE + 0x2000];

        let asked = walk_vs_holes(&chunks, &holes, true);
        assert_eq!(
            asked
                .spans
                .iter()
                .filter(|span| span.state.is_chunk())
                .map(|span| (span.header_address, span.size))
                .collect::<Vec<_>>(),
            [(VS_BASE + 0x10, 0xfe0), (VS_BASE + 0x1010, 0x2fe0)],
            "the chunk that spans the hole is understood, so it is reported"
        );
        assert!(
            asked.complete,
            "and nothing about it is unknown, so the walk covered everything: {:?}",
            asked.diagnostics.examples()
        );

        let unasked = walk_vs_holes(&chunks, &holes, false);
        assert_eq!(
            unasked
                .spans
                .iter()
                .filter(|span| span.state.is_chunk())
                .map(|span| span.header_address)
                .collect::<Vec<_>>(),
            [VS_BASE + 0x10],
            "with no answer about the tail the chunk is not invented"
        );
        assert!(!unasked.complete);
        assert!(
            unasked
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("runs")
                    && message.contains("past the committed extent")),
            "and the walk says which chunk it dropped: {:?}",
            unasked.diagnostics.examples()
        );
    }

    /// The paged-out half of `windbg-mcp` FOLLOWUPS item 99, VS side. A big-pool chunk's tag
    /// and length are the table's and its state is the free tree's; its own pages were never
    /// going to be read. So a tail in a page the memory manager has trimmed takes nothing from
    /// the span — and the walk used to drop the chunk for that tail and file its pages as an
    /// untagged gap, which is what `pool_chunk` then answered for the allocation's own
    /// address. Measured on `ctf-vm` (26100.33438, 2026-10-07): `CIcr` at 0xffffa9099c86f000,
    /// 0x20 into a 0x12c0-byte chunk with a resident header and a trimmed tail, `!pool` naming
    /// it and this walk answering `....`, 4096 bytes, `unreadable`.
    ///
    /// The control is the same fixture with no table entry, and it must still drop the chunk:
    /// without the table, a chunk that large with an unread tail is a real hole in what was
    /// seen, and a span over it would be the walk asserting what it did not read.
    #[test]
    fn test_a_big_pool_chunk_whose_tail_is_paged_out_is_still_named_from_the_table() {
        let chunks = [(0x2000usize, 0usize), (0x40, 0x2000)];
        let hole = VS_BASE + 0x1000;
        let allocation = hole;
        let walk = |named: bool| {
            let bytes = vs_extent(&chunks);
            let mut region = vs_region(bytes.len());
            if named {
                region.big_pool = Arc::new(HashMap::from([(
                    allocation,
                    BigPageEntry {
                        tag: u32::from_le_bytes(*b"BIGV"),
                        size: 0x1000,
                    },
                )]));
            }
            let mut memory = HoleyMemory::new(VS_BASE, bytes);
            memory.holes.insert(hole);
            // Committed, so the hole is memory the target has and cannot read — a trimmed
            // page, not a decommitted interior.
            let memory = memory.with_memory_manager(&[]);
            walk_holey(&memory, &region)
        };

        let named = walk(true);
        let big = named
            .spans
            .iter()
            .find(|span| span.size_class == 0x2000)
            .unwrap_or_else(|| panic!("the chunk the table names is missing: {:?}", named.spans));
        assert_eq!(super::super::decode::display_tag(big.raw_tag), "BIGV");
        assert_eq!(big.header_address, allocation);
        assert_eq!(big.usable_address, allocation);
        assert_eq!(big.size, 0x1000);
        assert_eq!(big.state, PoolState::Allocated);
        assert!(
            !named
                .spans
                .iter()
                .any(|span| span.state == PoolState::Unreadable),
            "the trimmed page is inside a span the index holds, so it is not also a gap: {:?}",
            named.spans
        );
        assert!(
            named.spans.iter().any(|span| span.size_class == 0x40),
            "the chain crosses the hole to the chunk after it: {:?}",
            named.spans
        );
        assert_eq!(named.unplaced_bytes, 0);
        assert!(
            named.complete,
            "nothing about the chunk was unknown: {:?}",
            named.diagnostics.examples()
        );

        let unnamed = walk(false);
        assert!(
            !unnamed.spans.iter().any(|span| span.size_class == 0x2000),
            "without the table the chunk is not invented: {:?}",
            unnamed.spans
        );
        assert!(
            unnamed
                .spans
                .iter()
                .any(|span| span.state == PoolState::Unreadable && span.header_address == hole),
            "and its trimmed page is filed as the gap it is: {:?}",
            unnamed.spans
        );
        assert!(!unnamed.complete);
    }

    /// The same half, segment side. Discovery names an allocated page range from the table,
    /// and there is nothing on its pages to add: `walk_page_ranges` emits the one span and
    /// skips the header decode. Reading the pages anyway cost the allocation when they were
    /// trimmed — the range was filed as an untagged gap and the name went unused (`Gcac` at
    /// 0xffffa4b05e1b5000, lab 29671, 2026-10-07) — and cost every resident one a read whose
    /// bytes were then discarded: 111 MB of them on that kernel, summed over the live entries.
    /// So a named range is answered without asking the memory at all, which the query count
    /// pins; the control is the same range unnamed, which has to be read and cannot be.
    #[test]
    fn test_a_page_range_the_table_names_is_answered_without_reading_it() {
        let size = 0x2000usize;
        let walk = |named: bool| {
            let mut region = vs_region(size);
            region.backend = PoolBackend::Segment;
            region.unit_size = size as u32;
            region.states = vec![PoolState::Allocated];
            region.pool_header.size = 0;
            region.known_tag = named.then_some(u32::from_le_bytes(*b"BIGP"));
            let mut memory = HoleyMemory::new(VS_BASE, vec![0; size]);
            memory.holes.extend([VS_BASE, VS_BASE + 0x1000]);
            let memory = memory.with_memory_manager(&[]);
            let snapshot = walk_holey(&memory, &region);
            (snapshot, memory.queries.get())
        };

        let (named, queries) = walk(true);
        assert_eq!(queries, 0, "the table answered; the pages were not asked");
        let [span] = named.spans.as_slice() else {
            panic!("one span for one allocation: {:?}", named.spans);
        };
        assert_eq!(super::super::decode::display_tag(span.raw_tag), "BIGP");
        assert_eq!(span.header_address, VS_BASE);
        assert_eq!(span.usable_address, VS_BASE);
        assert_eq!(span.size, size as u64);
        assert_eq!(span.state, PoolState::Allocated);
        assert!(named.complete);

        let (unnamed, queries) = walk(false);
        assert!(queries > 0, "an unnamed range has to be read");
        assert!(
            unnamed
                .spans
                .iter()
                .all(|span| span.state == PoolState::Unreadable),
            "and when it cannot be, that is all the walk can say: {:?}",
            unnamed.spans
        );
        assert!(!unnamed.complete);
    }

    /// glslang/dbgscope#104, settled on live 26100: every stall the walk met answered `0x0+0x0`,
    /// and the eight verbatim samples were eight *consecutive* pages — one dead region stepping
    /// itself into the give-up bound, paying eight KD round trips to be told eight times what the
    /// first answer already said. `0x0+0x0` is not a region at zero; it is the engine reporting
    /// nothing valid in the span it was asked about, which is a statement about every byte
    /// through to the end of it.
    ///
    /// So it ends the region, exactly like the sibling answer that names a base past the end.
    /// The distinction that matters is with [`Self::test_a_stalled_query_costs_a_page_not_the_region`]:
    /// a query that reports a region and cannot size it has said nothing about what lies behind
    /// it, and there the page step stays.
    #[test]
    fn test_a_query_that_finds_nothing_ends_the_region_rather_than_stepping() {
        let mut memory = HoleyMemory::new(SPECIAL_PAGE, special_pages(8));
        for page in 4..8 {
            memory.blind.insert(SPECIAL_PAGE + page * PAGE_SIZE);
        }
        let snapshot = walk_holey(&memory, &special_region(SPECIAL_PAGE, 8));

        assert_eq!(
            memory.queries.get(),
            2,
            "one query to walk the committed run, one to be told nothing follows it — a page \
             step would have cost four more and learnt nothing"
        );
        assert_eq!(
            snapshot.stalls,
            WalkStalls::default(),
            "nothing was stepped over, so nothing is billed as stepped over"
        );
        assert!(
            !snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("made no progress")),
            "a region running out of readable content is not a fault: {:?}",
            snapshot.diagnostics.examples()
        );
        // Still accounted for, and still incomplete: those pages were not walked, and the walk
        // says so rather than reporting the region as fully covered.
        assert!(!snapshot.complete);
        let unreadable: u64 = snapshot
            .spans
            .iter()
            .filter(|span| span.state == PoolState::Unreadable)
            .map(|span| span.size)
            .sum();
        assert_eq!(unreadable, 4 * PAGE_SIZE);
        let allocated = snapshot
            .spans
            .iter()
            .filter(|span| span.state == PoolState::Allocated)
            .count();
        assert_eq!(
            allocated, 4,
            "the readable pages in front of it are still walked"
        );
    }

    /// The reason the `break` was there in the first place: the advance has to be
    /// unconditional or a query that keeps answering the same way spins forever. Unconditional
    /// *and* bounded — a region that is dead all the way down costs a fixed number of queries,
    /// not one per page of its length, which on a live KD link is the cost that matters.
    #[test]
    fn test_a_region_that_never_advances_is_given_up_on() {
        let pages = 4096;
        let mut memory = HoleyMemory::new(SPECIAL_PAGE, vec![0u8; pages * PAGE_SIZE as usize]);
        for page in 0..pages as u64 {
            memory.stalls.insert(SPECIAL_PAGE + page * PAGE_SIZE);
        }
        let snapshot = walk_holey(&memory, &special_region(SPECIAL_PAGE, pages));

        assert_eq!(
            memory.queries.get(),
            MAX_CONSECUTIVE_STALLS as usize,
            "a dead region costs the limit in queries, and not one more"
        );
        assert!(!snapshot.complete);
        assert_eq!(snapshot.stalls.pages, u64::from(MAX_CONSECUTIVE_STALLS));
        // Every byte of it is accounted for as unreadable, whether stepped over or written off.
        let unreadable: u64 = snapshot
            .spans
            .iter()
            .filter(|span| span.state == PoolState::Unreadable)
            .map(|span| span.size)
            .sum();
        assert_eq!(unreadable, pages as u64 * PAGE_SIZE);
    }

    /// The other half of the fix, and the larger half of the 3,285: a region simply running
    /// out of committed pages arrived at the same branch and was reported as a failure to
    /// advance. Nothing is behind it to lose — the hole branch has already filed the tail —
    /// so there is nothing to report.
    #[test]
    fn test_a_region_running_out_of_committed_pages_is_not_a_stall() {
        let mut memory = HoleyMemory::new(SPECIAL_PAGE, special_pages(4));
        memory.holes.insert(SPECIAL_PAGE + 0x3000);
        let snapshot = walk_holey(&memory, &special_region(SPECIAL_PAGE, 4));

        assert_eq!(snapshot.stalls, WalkStalls::default());
        assert!(
            !snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("made no progress")),
            "{:?}",
            snapshot.diagnostics.examples()
        );
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("only committed through")),
            "the tail is still reported as the unreadable space it is"
        );
        assert_eq!(
            snapshot
                .spans
                .iter()
                .filter(|span| span.state == PoolState::Allocated)
                .count(),
            3
        );
    }

    /// Special pool is page-granular, so one page that does not decode says nothing about the
    /// next. Abandoning the region there — which is what this did before glslang/dbgscope#86 —
    /// dropped every later allocation *and* left `complete` set, so the truncated snapshot was
    /// the one cached and re-served.
    #[test]
    fn test_a_bad_special_pool_page_costs_that_page_only() {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&special_page(0x68, false));
        bytes.extend_from_slice(&vec![0u8; PAGE_SIZE as usize]); // decodes as nothing
        bytes.extend_from_slice(&special_page(0x140, false));
        let region = special_region(SPECIAL_PAGE, 3);
        let memory = FlatMemory::new(SPECIAL_PAGE, bytes.len());
        let layout = vs_layout(VsFixture::Inline);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };
        walker.walk_special_pool(&region, SPECIAL_PAGE, &bytes, &mut snapshot);

        let found: Vec<_> = snapshot
            .spans
            .iter()
            .map(|span| (span.usable_address, span.size))
            .collect();
        assert_eq!(
            found,
            [
                (SPECIAL_PAGE + 0xf90, 0x68),
                (SPECIAL_PAGE + 0x2000 + 0xec0, 0x140)
            ],
            "the page after the bad one must still be walked"
        );
        assert!(!snapshot.complete, "a skipped page is not a complete walk");
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains(&format!("{:#x}", SPECIAL_PAGE + 0x1000))),
            "the diagnostic has to name the page that was skipped: {:?}",
            snapshot.diagnostics.examples()
        );
    }
    use crate::pool::{
        decode::{
            DESCRIPTOR_FLAG_ALLOCATED, DESCRIPTOR_FLAG_FIRST, DESCRIPTOR_FLAG_SUBSEGMENT,
            DESCRIPTOR_FLAG_VS,
        },
        layout::{LayoutKey, TypeLayout},
    };

    /// `RangeFlags` for an in-use page range: allocated, and the range's first descriptor.
    /// The kernel's own free path insists on both before it looks at anything else.
    const RANGE_IN_USE: u8 = DESCRIPTOR_FLAG_ALLOCATED | DESCRIPTOR_FLAG_FIRST;
    /// An LFH subsegment range, as `nt!RtlpHpFreeHeap` requires it: `0x0b`.
    const RANGE_LFH: u8 = RANGE_IN_USE | DESCRIPTOR_FLAG_SUBSEGMENT;
    /// A VS subsegment range: `0x0f`.
    const RANGE_VS: u8 = RANGE_LFH | DESCRIPTOR_FLAG_VS;

    const K: u64 = 0xffff_8000_0000_0000;
    const STATE: u64 = K + 0x10_0000;
    const GLOBALS: u64 = K + 0x11_0000;
    const BIG_TABLE_POINTER: u64 = K + 0x12_0000;
    const BIG_TABLE_COUNT: u64 = K + 0x12_0010;
    const HEAP: u64 = K + 0x20_0000;
    const POOL_NODE: u64 = STATE + 0x40;
    const DYNAMIC_LOOKASIDE: u64 = POOL_NODE + 0x20;
    const SEGMENT: u64 = K + 0x30_0000;
    const LARGE_META: u64 = K + 0x80_0000;
    const LARGE_VA: u64 = K + 0x90_0000;
    const BIG_TABLE: u64 = K + 0xa0_0000;

    /// Sparse synthetic memory, stored as coalesced contiguous runs.
    ///
    /// Both the read and the write path are shaped to keep Miri off per-byte work, and the two
    /// were found in separate rounds.
    ///
    /// Reads resolve through a binary search over runs, not the `BTreeMap<u64, u8>` this began
    /// as with one `get` per byte. Semantics are unchanged — a byte is readable iff it was
    /// written, so a read spanning a gap still fails — and that was worth 2.0x.
    ///
    /// Writes go through [`Writes`], a log replayed in bulk, rather than building that byte map
    /// at all. The read fix left the two pool snapshot tests at 398s and 369s of an 849s CI job,
    /// and the second of them is three assertions over a fixture — so what remained was never
    /// "Miri interpreting the walk", which is what the previous round of this comment concluded.
    /// It was the ~22k `BTreeMap` inserts `fill`/`put` performed to build the fixture in the
    /// first place, each a fully interpreted, borrow-checked trip through `NodeRef` and raw
    /// pointers. Bulk `copy_from_slice` over a handful of runs is one Miri operation per write.
    ///
    /// Measured, not assumed: on one machine that second test alone took 178.4s before the
    /// write fix; both tests together now take 6.1s.
    struct SyntheticMemory {
        /// `(start, bytes)` for each contiguous written range, sorted by `start` and never
        /// overlapping or abutting.
        runs: Vec<(u64, Vec<u8>)>,
        holes: Vec<(u64, u64)>,
    }

    /// The fixture's write log: `(address, bytes)` in the order written, later writes winning.
    ///
    /// A plain append-only `Vec`, one entry per `put`/`fill` call rather than per byte, so the
    /// whole fixture is a few dozen pushes and bulk copies instead of ~22k `BTreeMap` inserts.
    #[derive(Default)]
    struct Writes(Vec<(u64, Vec<u8>)>);

    impl SyntheticMemory {
        /// Overwrites one byte inside a range an earlier write already covers.
        fn poke(&mut self, address: u64, byte: u8) {
            let (start, data) = self
                .runs
                .iter_mut()
                .find(|(start, data)| address >= *start && address < *start + data.len() as u64)
                .expect("the address is inside a written run");
            data[(address - *start) as usize] = byte;
        }

        fn new(writes: Writes, holes: Vec<(u64, u64)>) -> Self {
            // Extents first: merge every written interval that overlaps or abuts a neighbour,
            // which yields exactly the runs the byte map used to produce by iterating in key
            // order. Sorting is safe here because an extent carries no value to be overwritten.
            let mut extents: Vec<(u64, u64)> = writes
                .0
                .iter()
                .filter(|(_, data)| !data.is_empty())
                .map(|(address, data)| (*address, address + data.len() as u64))
                .collect();
            extents.sort_unstable();
            let mut runs: Vec<(u64, Vec<u8>)> = Vec::new();
            for (start, end) in extents {
                match runs.last_mut() {
                    Some((run_start, data)) if start <= *run_start + data.len() as u64 => {
                        let len = (end - *run_start) as usize;
                        // A contained write leaves the run as it is; only a write reaching past
                        // the current end grows it.
                        if len > data.len() {
                            data.resize(len, 0);
                        }
                    }
                    _ => runs.push((start, vec![0; (end - start) as usize])),
                }
            }
            // Then replay in write order, so the last write to an address wins — the property
            // that re-inserting into the byte map used to provide.
            for (address, data) in writes.0.into_iter().filter(|(_, data)| !data.is_empty()) {
                let index = runs
                    .partition_point(|(start, _)| *start <= address)
                    .checked_sub(1)
                    .expect("every non-empty write contributed an extent");
                let (start, run) = &mut runs[index];
                let offset = (address - *start) as usize;
                run[offset..offset + data.len()].copy_from_slice(&data);
            }
            Self { runs, holes }
        }

        /// The run containing `address`, if any.
        fn run_at(&self, address: u64) -> Option<&(u64, Vec<u8>)> {
            let index = self
                .runs
                .partition_point(|(start, _)| *start <= address)
                .checked_sub(1)?;
            let run = &self.runs[index];
            (address < run.0 + run.1.len() as u64).then_some(run)
        }

        /// Whether a single byte was written — the fixture's own sanity checks ask this.
        fn contains(&self, address: u64) -> bool {
            self.run_at(address).is_some()
        }
    }

    struct ShortMemory;

    struct BigPageMemory {
        table: Vec<u8>,
        count: usize,
        read_calls: Cell<usize>,
        interrupt_checks: Cell<usize>,
        interrupt_after_checks: Option<usize>,
    }

    impl PoolMemory for ShortMemory {
        fn read_exact(&self, _address: u64, _size: usize) -> Result<Vec<u8>, SnapshotError> {
            Ok(Vec::new())
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            Ok((address, size))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }
    }

    impl PoolMemory for BigPageMemory {
        fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            self.read_calls.set(self.read_calls.get() + 1);
            if address == BIG_TABLE_POINTER && size == 8 {
                return Ok(BIG_TABLE.to_le_bytes().to_vec());
            }
            if address == BIG_TABLE_COUNT && size == 4 {
                return Ok((self.count as u32).to_le_bytes().to_vec());
            }
            if address == BIG_TABLE_COUNT && size == 8 {
                return Ok(((1u64 << 32) | self.count as u64).to_le_bytes().to_vec());
            }
            let offset = address
                .checked_sub(BIG_TABLE)
                .and_then(|value| usize::try_from(value).ok());
            if let Some(bytes) = offset
                .and_then(|offset| offset.checked_add(size).map(|end| (offset, end)))
                .and_then(|(offset, end)| self.table.get(offset..end))
            {
                return Ok(bytes.to_vec());
            }
            Err(SnapshotError::Read {
                address,
                size,
                source: Box::new(std::io::Error::other("sparse big-page memory")),
            })
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            Ok((address, size))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            let checks = self.interrupt_checks.get();
            self.interrupt_checks.set(checks + 1);
            Ok(self
                .interrupt_after_checks
                .is_some_and(|limit| checks >= limit))
        }
    }

    impl PoolMemory for SyntheticMemory {
        fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            let unreadable = || SnapshotError::Read {
                address,
                size,
                source: Box::new(std::io::Error::other("sparse synthetic memory")),
            };
            let (start, data) = self.run_at(address).ok_or_else(unreadable)?;
            let offset = (address - start) as usize;
            // One run must cover the whole read: runs never abut, so a read that runs off the
            // end of one has hit a gap, exactly as the per-byte lookup used to report.
            data.get(offset..offset + size)
                .map(<[u8]>::to_vec)
                .ok_or_else(unreadable)
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            let end = address.saturating_add(size as u64);
            for &(hole_start, hole_end) in &self.holes {
                if address >= hole_start && address < hole_end {
                    return Ok((hole_end, end.saturating_sub(hole_end) as usize));
                }
                if address < hole_start && end > hole_start {
                    return Ok((address, hole_start.saturating_sub(address) as usize));
                }
            }
            Ok((address, size))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }
    }

    fn type_layout(size: u32, fields: &[(&'static str, u32)]) -> TypeLayout {
        TypeLayout {
            size,
            fields: fields.iter().copied().collect(),
        }
    }

    fn synthetic_layout() -> PoolLayout {
        let mut types = HashMap::new();
        types.insert(
            "_EX_POOL_HEAP_MANAGER_STATE",
            type_layout(
                0x200,
                &[
                    ("HeapManager", 8),
                    ("PoolNode", 0x40),
                    ("NumberOfPools", 0),
                    ("SpecialHeaps", 0x20),
                ],
            ),
        );
        types.insert(
            "_EX_HEAP_POOL_NODE",
            type_layout(0x120, &[("Heaps", 0), ("Lookasides", 0x20)]),
        );
        types.insert(
            "_SEGMENT_HEAP",
            type_layout(
                0x800,
                &[
                    ("SegContexts", 0x100),
                    ("VsContext", 0x300),
                    ("LfhContext", 0x380),
                    ("LargeAllocMetadata", 0x400),
                ],
            ),
        );
        types.insert(
            "_HEAP_SEG_CONTEXT",
            type_layout(
                0x80,
                &[
                    ("SegmentListHead", 0),
                    ("FreePageRanges", 0x10),
                    ("UnitShift", 0x20),
                    ("FirstDescriptorIndex", 0x21),
                    ("SegmentMask", 0x28),
                    ("PagesPerUnitShift", 0x30),
                ],
            ),
        );
        types.insert(
            "_HEAP_PAGE_SEGMENT",
            type_layout(
                0x100,
                &[("ListEntry", 0), ("Signature", 0x20), ("DescArray", 0x100)],
            ),
        );
        types.insert(
            "_HEAP_PAGE_RANGE_DESCRIPTOR",
            type_layout(
                0x20,
                &[
                    ("UnitSize", 0x1f),
                    ("RangeFlags", 0x18),
                    ("TreeNode", 0),
                    ("TreeSignature", 0),
                ],
            ),
        );
        types.insert(
            "_HEAP_VS_CONTEXT",
            type_layout(
                0x40,
                &[
                    ("FreeChunkTree", 0),
                    ("DelayFreeContext", 0x10),
                    ("SubsegmentList", 0x30),
                    ("Config", 0x38),
                ],
            ),
        );
        types.insert(
            "_HEAP_VS_DELAY_FREE_CONTEXT",
            type_layout(0x10, &[("ListHead", 0)]),
        );
        types.insert(
            "_HEAP_VS_SUBSEGMENT",
            type_layout(0xfe0, &[("Signature", 0), ("Size", 2), ("ListEntry", 8)]),
        );
        types.insert(
            "_HEAP_VS_CHUNK_HEADER",
            type_layout(0x10, &[("Sizes", 0), ("EncodedSegmentPageOffset", 8)]),
        );
        types.insert(
            "_HEAP_VS_CHUNK_FREE_HEADER",
            type_layout(0x20, &[("TreeNode", 8)]),
        );
        types.insert("_HEAP_LFH_CONTEXT", type_layout(0x20, &[("Buckets", 0)]));
        types.insert(
            "_HEAP_LFH_SUBSEGMENT",
            type_layout(
                0x20,
                &[
                    ("BlockOffsets", 0),
                    ("BlockCount", 4),
                    ("BlockBitmap", 8),
                    ("ListEntry", 0x10),
                ],
            ),
        );
        types.insert(
            "_HEAP_LFH_SUBSEGMENT_ENCODED_OFFSETS",
            type_layout(4, &[("EncodedData", 0)]),
        );
        types.insert(
            "_RTLP_HP_HEAP_GLOBALS",
            type_layout(0x10, &[("HeapKey", 0), ("LfhKey", 8)]),
        );
        types.insert(
            "_RTL_RB_TREE",
            type_layout(0x10, &[("Root", 0), ("Encoded", 8)]),
        );
        types.insert(
            "_RTL_BALANCED_NODE",
            type_layout(0x10, &[("Left", 0), ("Right", 8)]),
        );
        types.insert(
            "_RTL_DYNAMIC_LOOKASIDE",
            type_layout(0x80, &[("BucketCount", 8), ("Buckets", 0x40)]),
        );
        types.insert(
            "_RTL_LOOKASIDE",
            type_layout(0x40, &[("ListHead", 0), ("Size", 0x30)]),
        );
        types.insert(
            "_SLIST_HEADER",
            type_layout(0x10, &[("Alignment", 0), ("Region", 8)]),
        );
        types.insert(
            "_POOL_HEADER",
            type_layout(
                0x10,
                &[
                    ("PreviousSize", 0),
                    ("PoolIndex", 0),
                    ("BlockSize", 2),
                    ("PoolType", 2),
                    ("PoolTag", 8),
                ],
            ),
        );
        types.insert(
            "_HEAP_LARGE_ALLOC_DATA",
            type_layout(
                0x28,
                &[
                    ("TreeNode", 0),
                    ("VirtualAddress", 0x18),
                    ("AllocatedPages", 0x20),
                ],
            ),
        );
        types.insert(
            "_POOL_TRACKER_BIG_PAGES",
            type_layout(0x20, &[("Va", 0), ("Key", 8), ("NumberOfBytes", 0x10)]),
        );
        PoolLayout {
            key: LayoutKey {
                image: crate::dbgeng::KernelImage {
                    base: K,
                    ..crate::dbgeng::KernelImage::default()
                },
                session: 1,
            },
            globals: [
                ("ExPoolState", STATE),
                ("RtlpHpHeapGlobals", GLOBALS),
                ("PoolBigPageTable", BIG_TABLE_POINTER),
                ("PoolBigPageTableSize", BIG_TABLE_COUNT),
            ]
            .into_iter()
            .collect(),
            types,
        }
    }

    fn put(bytes: &mut Writes, address: u64, data: &[u8]) {
        bytes.0.push((address, data.to_vec()));
    }

    fn fill(bytes: &mut Writes, address: u64, size: usize) {
        bytes.0.push((address, vec![0; size]));
    }

    fn put_u16(bytes: &mut Writes, address: u64, value: u16) {
        put(bytes, address, &value.to_le_bytes());
    }

    fn put_u32(bytes: &mut Writes, address: u64, value: u32) {
        put(bytes, address, &value.to_le_bytes());
    }

    fn put_u64(bytes: &mut Writes, address: u64, value: u64) {
        put(bytes, address, &value.to_le_bytes());
    }

    fn packed_slist_next(entry: u64) -> u64 {
        (entry << 4) | 3
    }

    fn pool_header(bytes: &mut Writes, address: u64, tag: &[u8; 4]) {
        fill(bytes, address, 0x10);
        put(bytes, address, &[1, 0, 4, 1]);
        put(bytes, address + 8, tag);
    }

    /// The fixture, with its VS subsegment declaring the chunk area its page range actually
    /// leaves — `(2 pages - sizeof(_HEAP_VS_SUBSEGMENT rounded)) / 16`, which is what
    /// `RtlpHpVsSubsegmentCreate` writes. Kept in step deliberately: the walk now cross-checks
    /// the two, so a fixture that disagreed would put a complaint in every other test's output
    /// and hide the one case that should raise it.
    fn synthetic_memory() -> SyntheticMemory {
        synthetic_memory_declaring((0x2000 - 0xfe0) / 16)
    }

    fn synthetic_memory_declaring(declared: u16) -> SyntheticMemory {
        let mut bytes = Writes::default();
        fill(&mut bytes, STATE, 0x200);
        put_u32(&mut bytes, STATE, 1);
        for heap_index in 0..4 {
            put_u64(&mut bytes, STATE + 0x40 + heap_index * 8, HEAP);
        }
        fill(&mut bytes, GLOBALS, 0x10);
        let heap_key = 0x55aa_1234_9876_0000;
        let lfh_key = 0xa5c3_1357;
        put_u64(&mut bytes, GLOBALS, heap_key);
        put_u64(&mut bytes, GLOBALS + 8, lfh_key);
        fill(&mut bytes, HEAP, 0x800);

        let context = HEAP + 0x100;
        let list_head = context;
        put_u64(&mut bytes, list_head, SEGMENT);
        put_u64(&mut bytes, context + 0x10, SEGMENT + 0x100 + 6 * 0x20);
        put(&mut bytes, context + 0x20, &[12, 1]);
        put_u64(&mut bytes, context + 0x28, !0xffffu64);

        fill(&mut bytes, SEGMENT, 0x100);
        put_u64(&mut bytes, SEGMENT, list_head);
        put_u64(
            &mut bytes,
            SEGMENT + 0x20,
            SEGMENT ^ context ^ heap_key ^ super::super::decode::PAGE_SEGMENT_SIGNATURE,
        );
        fill(&mut bytes, SEGMENT + 0x100, 16 * 0x20);
        for (index, units, flags) in [
            (1u64, 2u8, RANGE_LFH),
            (3, 2, RANGE_VS),
            (5, 1, RANGE_IN_USE),
            (6, 1, 0x00),
            (7, 1, RANGE_IN_USE),
        ] {
            let descriptor = SEGMENT + 0x100 + index * 0x20;
            if flags & DESCRIPTOR_FLAG_FIRST != 0 {
                put_u32(
                    &mut bytes,
                    descriptor,
                    super::super::decode::DESCRIPTOR_TREE_SIGNATURE,
                );
            }
            put(&mut bytes, descriptor + 0x18, &[flags]);
            put(&mut bytes, descriptor + 0x1f, &[units]);
        }

        let lfh = SEGMENT + 0x1000;
        fill(&mut bytes, lfh, 0x20);
        let decoded_offsets = u32::from(0x40u16) | (u32::from(0x40u16) << 16);
        put_u32(
            &mut bytes,
            lfh,
            decoded_offsets ^ lfh_key as u32 ^ (lfh >> 12) as u32,
        );
        put_u16(&mut bytes, lfh + 4, 4);
        put(&mut bytes, lfh + 8, &[0x49]);
        fill(&mut bytes, lfh + 0x40, 0x100);
        pool_header(&mut bytes, lfh + 0x40, b"LFH!");
        pool_header(&mut bytes, lfh + 0x80, b"LFHC");
        pool_header(&mut bytes, lfh + 0xc0, b"LFHF");
        pool_header(&mut bytes, lfh + 0x100, b"LFH2");

        let vs = SEGMENT + 0x3000;
        fill(&mut bytes, vs, 0x2000);
        put_u16(&mut bytes, vs, 0x8000 | (0x2bed ^ declared));
        put_u16(&mut bytes, vs + 2, declared);
        let first_chunk = vs + 0xfe0;
        let cached_chunk = first_chunk + 0x40;
        let free_chunk = cached_chunk + 0x40;
        for (address, allocated) in [
            (first_chunk, true),
            (cached_chunk, false),
            (free_chunk, false),
        ] {
            let decoded = (4u64 << 16) | (u64::from(allocated) << 48);
            put_u64(&mut bytes, address, decoded ^ address ^ heap_key);
        }
        pool_header(&mut bytes, first_chunk + 0x10, b"VS!!");
        let vs_context = HEAP + 0x300;
        put_u64(&mut bytes, vs_context, free_chunk + 8);
        put_u64(&mut bytes, free_chunk + 8, 0);
        put_u64(&mut bytes, free_chunk + 16, 0);
        let delay_head = vs_context + 0x10;
        put_u16(&mut bytes, delay_head, 1);
        put_u64(
            &mut bytes,
            delay_head + 8,
            packed_slist_next(cached_chunk + 0x20),
        );
        put_u64(&mut bytes, cached_chunk + 0x20, 0);

        fill(&mut bytes, DYNAMIC_LOOKASIDE, 0x80);
        put_u32(&mut bytes, DYNAMIC_LOOKASIDE + 8, 1);
        let lookaside_chunk = free_chunk + 0x40;
        let decoded = 4u64 << 16;
        put_u64(
            &mut bytes,
            lookaside_chunk,
            decoded ^ lookaside_chunk ^ heap_key,
        );
        let lookaside_head = DYNAMIC_LOOKASIDE + 0x40;
        put_u16(&mut bytes, lookaside_head, 1);
        put_u64(
            &mut bytes,
            lookaside_head + 8,
            packed_slist_next(lookaside_chunk + 0x20),
        );
        put_u64(&mut bytes, lookaside_chunk + 0x20, 0);
        put_u32(&mut bytes, lookaside_head + 0x30, 0x40);

        fill(&mut bytes, SEGMENT + 0x5000, 0x1000);
        pool_header(&mut bytes, SEGMENT + 0x5000, b"SEGM");
        fill(&mut bytes, SEGMENT + 0x6000, 0x1000);
        fill(&mut bytes, SEGMENT + 0x7000, 0x800);
        pool_header(&mut bytes, SEGMENT + 0x7000, b"SPRS");

        let large_tree = HEAP + 0x400;
        put_u64(&mut bytes, large_tree, LARGE_META ^ large_tree);
        put(&mut bytes, large_tree + 8, &[1]);
        fill(&mut bytes, LARGE_META, 0x28);
        put_u64(&mut bytes, LARGE_META + 0x18, LARGE_VA | 0x1800);
        put_u64(&mut bytes, LARGE_META + 0x20, (2u64 << 12) | 0x5a5);
        // Large allocations are described entirely by allocator metadata; leave
        // their payload absent so the walker cannot accidentally read it.
        put_u64(&mut bytes, BIG_TABLE_POINTER, BIG_TABLE);
        put_u64(&mut bytes, BIG_TABLE_COUNT, 4);
        fill(&mut bytes, BIG_TABLE, 4 * 0x20);
        let first = super::super::decode::big_page_hash(LARGE_VA, 4).unwrap();
        let adjacent = (first + 1) % 4;
        let collision = BIG_TABLE + first as u64 * 0x20;
        put_u64(&mut bytes, collision, LARGE_VA + 0x10_0000);
        let entry = BIG_TABLE + adjacent as u64 * 0x20;
        put_u64(&mut bytes, entry, LARGE_VA);
        put(&mut bytes, entry + 8, b"BIG!");
        put_u64(&mut bytes, entry + 0x10, 0x1800);

        SyntheticMemory::new(bytes, vec![(SEGMENT + 0x7800, SEGMENT + 0x8000)])
    }

    fn big_page_memory(address: u64, count: usize, collision_distance: usize) -> BigPageMemory {
        let mut table = vec![0; count * 0x20];
        let first = super::super::decode::big_page_hash(address, count).unwrap();
        for distance in 0..collision_distance {
            let index = (first + distance) % count;
            let offset = index * 0x20;
            table[offset..offset + 8]
                .copy_from_slice(&(address + (distance as u64 + 1) * 0x10_0000).to_le_bytes());
        }
        let index = (first + collision_distance) % count;
        let offset = index * 0x20;
        table[offset..offset + 8].copy_from_slice(&address.to_le_bytes());
        table[offset + 8..offset + 12].copy_from_slice(b"BTCH");
        table[offset + 0x10..offset + 0x18].copy_from_slice(&0x9000u64.to_le_bytes());
        BigPageMemory {
            table,
            count,
            read_calls: Cell::new(0),
            interrupt_checks: Cell::new(0),
            interrupt_after_checks: None,
        }
    }

    /// glslang/dbgscope#103, settled on a live 26100 kernel: the page range holding a VS
    /// subsegment is **larger than the subsegment**, by one unit every time, so a walk bounded by
    /// the descriptor decodes a page that holds no chunks and refuses a header every sixteen
    /// bytes across it. The subsegment's own `Size` is the chunk area, and it is only reachable
    /// through a signature check that would have rejected a misread — so it is preferred, and
    /// clamped so it can never point the walk outside the range it was given.
    #[test]
    fn test_a_vs_subsegment_is_bounded_by_its_own_declared_size() {
        let complaint = "declares";
        let quiet = walk_synthetic(synthetic_memory());
        assert!(
            !quiet
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains(complaint)),
            "a subsegment sized like the real ones draws no complaint: {:?}",
            quiet.diagnostics.examples()
        );
        let all = vs_chunks(&quiet);

        // The live shape: chunks end before the range does. What sits past the declared end is
        // not part of this subsegment and must not be decoded — which is the entire finding.
        let bounded = walk_synthetic(synthetic_memory_declaring(8));
        // `first` is the fixture's `_HEAP_VS_SUBSEGMENT` size rounded up, and eight units past it
        // is where the chunk area now ends — well short of the page range, exactly as a real one
        // does. Nothing at or beyond that address is part of this subsegment.
        let declared_end = SEGMENT + 0x3000 + 0xfe0 + 8 * 16;
        assert!(
            vs_chunks(&bounded) < all,
            "bounding the subsegment has to cost the chunks past its end: {:?}",
            bounded.diagnostics.examples()
        );
        assert!(
            snapshot_vs_spans(&bounded).all(|span| span.header_address < declared_end),
            "nothing past {declared_end:#x} may be decoded: {:?}",
            snapshot_vs_spans(&bounded)
                .map(|span| span.header_address)
                .collect::<Vec<_>>()
        );
        assert!(
            snapshot_vs_spans(&quiet).any(|span| span.header_address >= declared_end),
            "and the check is only worth anything if the wider bound did reach past it"
        );
        assert!(
            !bounded
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains(complaint)),
            "and that is the ordinary shape, not something to complain about once per \
             subsegment: {:?}",
            bounded.diagnostics.examples()
        );

        // Neither of these can be the chunk area, and both would be silent damage if clamped
        // without a word: no chunks at all, and more than the range can hold.
        for declared in [0, 0x200] {
            let bogus = walk_synthetic(synthetic_memory_declaring(declared));
            assert!(
                bogus
                    .diagnostics
                    .examples()
                    .iter()
                    .any(|message| message.contains(complaint)),
                "declared {declared:#x}: {:?}",
                bogus.diagnostics.examples()
            );
            assert_eq!(
                vs_chunks(&bogus),
                all,
                "an unusable declaration falls back to the descriptor's bound"
            );
        }
    }

    fn snapshot_vs_spans(snapshot: &PoolSnapshot) -> impl Iterator<Item = &PoolSpan> {
        snapshot
            .spans
            .iter()
            .filter(|span| span.backend == PoolBackend::Vs && span.state != PoolState::Unreadable)
    }

    fn vs_chunks(snapshot: &PoolSnapshot) -> usize {
        snapshot_vs_spans(snapshot).count()
    }

    fn walk_synthetic(memory: SyntheticMemory) -> PoolSnapshot {
        let layout = synthetic_layout();
        SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1024,
        }
        .walk(None)
        .unwrap()
    }

    #[test]
    fn test_pool_snapshot_walks_all_backends() {
        let memory = synthetic_memory();
        assert!(!memory.contains(LARGE_VA));
        let layout = synthetic_layout();
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1024,
        };
        let snapshot = walker.walk(None).unwrap();
        for backend in [
            PoolBackend::Lfh,
            PoolBackend::Vs,
            PoolBackend::Segment,
            PoolBackend::Large,
        ] {
            assert!(
                snapshot.spans.iter().any(|span| span.backend == backend),
                "missing {backend:?}"
            );
        }
        assert!(snapshot.spans.iter().any(|span| span.pool_kind.is_paged()));
        assert!(snapshot.spans.iter().any(|span| {
            span.raw_tag == u32::from_le_bytes(*b"LFH!") && span.state == PoolState::Allocated
        }));
        assert!(snapshot.spans.iter().any(|span| {
            span.backend == PoolBackend::Vs && span.state == PoolState::CachedFree
        }));
        assert!(snapshot.spans.iter().any(|span| {
            span.backend == PoolBackend::Vs
                && span.header_address == SEGMENT + 0x40b0
                && span.state == PoolState::CachedFree
        }));
        assert!(
            snapshot
                .spans
                .iter()
                .filter(|span| span.backend == PoolBackend::Vs)
                .all(|span| span.size_class == 0x40)
        );
        assert!(snapshot.spans.iter().any(|span| {
            span.backend == PoolBackend::Segment && span.state == PoolState::ReusableFree
        }));
        assert!(
            snapshot
                .spans
                .iter()
                .any(|span| span.state == PoolState::Unreadable)
        );
        assert!(snapshot.spans.iter().any(|span| {
            span.backend == PoolBackend::Vs
                && span.header_address == SEGMENT + 0x3ff0
                && span.usable_address == SEGMENT + 0x4000
                && span.raw_tag == u32::from_le_bytes(*b"VS!!")
        }));
        assert!(snapshot.spans.iter().any(|span| {
            span.backend == PoolBackend::Large
                && span.raw_tag == u32::from_le_bytes(*b"BIG!")
                && span.size == 0x1800
        }));
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| { message.contains("per-session paged heaps are not included") })
        );
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("only committed through"))
        );
    }

    // ---- the walk budget --------------------------------------------------------------

    /// The synthetic fixture, with a budget that runs out after a fixed number of reads.
    ///
    /// The real budget is wall-clock, which is untestable without making the test a race
    /// against the machine it runs on. Reads are the honest stand-in: they are what the
    /// budget exists to bound — over a live KD link every one crosses the wire — and their
    /// count is exactly what differs between a dump that walks in a second and a live kernel
    /// that takes minutes.
    struct Impatient<M> {
        inner: M,
        reads: Cell<usize>,
        allowance: usize,
    }

    impl Impatient<SyntheticMemory> {
        fn new(allowance: usize) -> Self {
            Self::over(synthetic_memory(), allowance)
        }
    }

    impl<M> Impatient<M> {
        fn over(inner: M, allowance: usize) -> Self {
            Self {
                inner,
                reads: Cell::new(0),
                allowance,
            }
        }
    }

    impl<M: PoolMemory> PoolMemory for Impatient<M> {
        fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            self.reads.set(self.reads.get() + 1);
            self.inner.read_exact(address, size)
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            self.inner.valid_region(address, size)
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }

        fn out_of_budget(&self) -> bool {
            self.reads.get() >= self.allowance
        }
    }

    fn walk_impatiently(allowance: usize) -> PoolSnapshot {
        let memory = Impatient::new(allowance);
        let layout = synthetic_layout();
        SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1024,
        }
        .walk(None)
        .expect("running out of budget is an outcome, not an error")
    }

    /// The wedge this budget exists to prevent: with nothing to stop it, the walk ran on
    /// past the caller's timeout and every later call to that target queued behind it. So
    /// the deadline has to produce an *answer* — a snapshot that says what it reached and
    /// admits what it did not. An `Err` would discard the work and say nothing.
    #[test]
    fn test_a_walk_that_runs_out_of_budget_reports_what_it_reached() {
        let snapshot = walk_impatiently(40);

        assert!(
            !snapshot.complete,
            "a truncated walk that claims completeness is the lie this whole API guards against"
        );
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("ran out of its")
                    && message.contains("discovered regions")),
            "the snapshot has to say why it is short: {:?}",
            snapshot.diagnostics
        );
    }

    /// Running out of time is about the walk, not about whichever unit happened to be under
    /// the cursor. Recorded as a per-unit failure it reads as "heap 3 is unreadable", and —
    /// the real damage — the enclosing loop swallows it and carries on to the next unit,
    /// each stopping the same way, so the deadline bounds one unit instead of the walk. On
    /// the *last* iteration it is absorbed outright and the walk simply continues.
    ///
    /// Swept over budgets because where the halt lands decides which handler sees it: an
    /// early one stops inside a heap's VS evidence, a later one inside a segment context.
    /// Both swallow errors into diagnostics, so both have to re-raise.
    #[test]
    fn test_running_out_of_budget_is_never_reported_as_a_broken_unit() {
        for allowance in [10, 25, 40, 80, 120, 200, 400] {
            let snapshot = walk_impatiently(allowance);
            for absorbed in [
                "cannot fully discover heap",
                "cannot discover segment context",
            ] {
                assert!(
                    !snapshot
                        .diagnostics
                        .examples()
                        .iter()
                        .any(|message| message.contains(absorbed)),
                    "budget {allowance}: a per-unit handler absorbed the halt as \
                     `{absorbed}`: {:?}",
                    snapshot.diagnostics
                );
            }
        }
    }

    /// How many reads a walk may still issue after its budget is spent.
    ///
    /// Not zero: the check sits at the top of each read-issuing loop, so the iteration
    /// already under way finishes and a few of those loops nest. Measured at 3 against this
    /// fixture, and pinned there rather than rounded up — the number this guards against is
    /// the thousands a single unpolled loop costs, so slack here buys nothing and hides the
    /// next missing check.
    const OVERSHOOT_ALLOWED: usize = 3;

    #[test]
    fn test_the_walk_stops_reading_promptly_once_its_budget_is_gone() {
        let overshoots: Vec<(usize, usize)> = [10, 25, 40, 80, 120, 200, 400]
            .into_iter()
            .map(|allowance| {
                let memory = Impatient::new(allowance);
                let layout = synthetic_layout();
                SnapshotWalker {
                    memory: &memory,
                    layout: &layout,
                    traversal_limit: 1024,
                }
                .walk(None)
                .expect("running out of budget is an outcome, not an error");
                (allowance, memory.reads.get().saturating_sub(allowance))
            })
            .collect();

        let worst = overshoots.iter().map(|(_, over)| *over).max().unwrap_or(0);
        assert!(
            worst <= OVERSHOOT_ALLOWED,
            "{worst} reads issued after the deadline (limit {OVERSHOOT_ALLOWED}) — some \
             read-issuing loop is not polling the budget. (budget, overshoot): {overshoots:?}"
        );
    }

    /// A segment holding one plain allocated page range and one free range, and nothing else.
    ///
    /// Neither is a subsegment, and that is the point: with `RangeFlags` bit `0x01` read as
    /// "LFH", both look like LFH subsegment ranges, their page contents decode as a subsegment
    /// header, and they are discarded before the walk ever sees them. The free range is
    /// deliberately *not* in the free-page tree, so its state has to come from its own flags.
    fn segment_of_plain_page_ranges(heap_key: u64) -> SyntheticMemory {
        SyntheticMemory::new(plain_page_range_writes(heap_key), Vec::new())
    }

    fn plain_page_range_writes(heap_key: u64) -> Writes {
        let mut bytes = Writes::default();
        let context = HEAP + 0x100;

        fill(&mut bytes, HEAP, 0x800);
        put_u64(&mut bytes, context, SEGMENT); // SegmentListHead
        put(&mut bytes, context + 0x20, &[12, 1]); // UnitShift, FirstDescriptorIndex
        put_u64(&mut bytes, context + 0x28, !0xffffu64); // SegmentMask: 16 descriptors

        fill(&mut bytes, SEGMENT, 0x300);
        put_u64(&mut bytes, SEGMENT, context); // ListEntry back to the head: one segment
        put_u64(
            &mut bytes,
            SEGMENT + 0x20,
            SEGMENT ^ context ^ heap_key ^ super::super::decode::PAGE_SEGMENT_SIGNATURE,
        );
        for (index, flags) in [(1u64, RANGE_IN_USE), (2, DESCRIPTOR_FLAG_FIRST)] {
            let descriptor = SEGMENT + 0x100 + index * 0x20;
            put_u32(
                &mut bytes,
                descriptor,
                super::super::decode::DESCRIPTOR_TREE_SIGNATURE,
            );
            put(&mut bytes, descriptor + 0x18, &[flags]);
            put(&mut bytes, descriptor + 0x1f, &[1]);
        }
        bytes
    }

    /// A kernel page range that `nt!PoolBigPageTable` names is a big-pool allocation, and it
    /// has no `_POOL_HEADER` for the walk to read.
    ///
    /// `ExpAllocateBigPool` takes whole pages from this same segment allocator and puts the
    /// caller's tag and length in that table instead of in a header, which is why the range
    /// descriptor cannot tell one from a plain page-range allocation: both are `RangeFlags`
    /// `0x03`. Decoding the page as though a header were there reads the caller's own first
    /// sixteen bytes as one — measured on a live 26100 kernel (`FOLLOWUPS` item 99), where a
    /// `CM25` registry hive bin came back as a 4080-byte block at `+0x10` tagged `..N.`, from
    /// the bytes of `hbin`'s own header.
    ///
    /// The fixture writes a *valid-looking* pool header at the page start on purpose: without
    /// it the wrong answer would be an empty tag, which a fixture of zeroes cannot tell from
    /// the right one.
    #[test]
    fn test_a_page_range_the_big_page_table_names_carries_no_pool_header() {
        let heap_key = 0x55aa_1234_9876_0000;
        let allocated = SEGMENT + 0x1000;
        let mut writes = plain_page_range_writes(heap_key);
        fill(&mut writes, allocated, 0x1000);
        pool_header(&mut writes, allocated, b"HBIN");
        put_u64(&mut writes, BIG_TABLE_POINTER, BIG_TABLE);
        put_u64(&mut writes, BIG_TABLE_COUNT, 8);
        fill(&mut writes, BIG_TABLE, 8 * 0x20);
        let index = super::super::decode::big_page_hash(allocated, 8).unwrap();
        let entry = BIG_TABLE + index as u64 * 0x20;
        put_u64(&mut writes, entry, allocated);
        put(&mut writes, entry + 8, b"BIGP");
        put_u64(&mut writes, entry + 0x10, 0xfc0);
        let memory = SyntheticMemory::new(writes, Vec::new());
        let layout = synthetic_layout();
        let mut discovery = Discovery {
            big_pages: BigPageTable::open(&memory, &layout, &mut Vec::new()).unwrap(),
            ..Discovery::default()
        };
        assert!(discovery.big_pages.is_some(), "the fixture has a table");

        discover_segment_context(
            &memory,
            &layout,
            HEAP + 0x100,
            0,
            PoolKind::NonPagedNx,
            HeapIdentity {
                pool_state: STATE,
                heap: HEAP,
                special: false,
            },
            heap_key,
            0xa5c3_1357,
            1024,
            &SharedChunks::default(),
            &SharedChunks::default(),
            false,
            &mut discovery,
        )
        .expect("the fixture is readable throughout");

        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1024,
        };
        let mut snapshot = PoolSnapshot {
            complete: true,
            ..PoolSnapshot::default()
        };
        walker
            .walk_discovered_regions(discovery.regions, None, false, &mut snapshot)
            .expect("the fixture is readable throughout");

        let span = snapshot
            .spans
            .iter()
            .find(|span| span.header_address == allocated)
            .unwrap_or_else(|| panic!("no span at the allocated range: {:?}", snapshot.spans));
        assert_eq!(super::super::decode::display_tag(span.raw_tag), "BIGP");
        // The block starts at the page, not 0x10 into it, and is the length the table
        // records rather than the page less a header that was never there.
        assert_eq!(span.usable_address, allocated);
        assert_eq!(span.size, 0xfc0);
    }

    /// A page range that is not a subsegment must be walked as page ranges, not rejected.
    ///
    /// This is glslang/dbgscope#90 at the smallest scale that shows it. Every allocated range
    /// carries `RangeFlags` bit `0x01`, so reading that bit as "this is an LFH subsegment"
    /// sends plain page-range allocations, VS subsegments and Verifier special pool alike
    /// through the LFH decoder, which refuses the result and drops the range — silently, and
    /// before any of its allocations can be indexed.
    #[test]
    fn test_a_plain_page_range_is_not_taken_for_an_lfh_subsegment() {
        let heap_key = 0x55aa_1234_9876_0000;
        let memory = segment_of_plain_page_ranges(heap_key);
        let layout = synthetic_layout();
        let mut discovery = Discovery::default();

        discover_segment_context(
            &memory,
            &layout,
            HEAP + 0x100,
            0,
            PoolKind::NonPagedNx,
            HeapIdentity {
                pool_state: STATE,
                heap: HEAP,
                special: false,
            },
            heap_key,
            0xa5c3_1357,
            1024,
            &SharedChunks::default(),
            &SharedChunks::default(),
            false,
            &mut discovery,
        )
        .expect("the fixture is readable throughout");

        assert!(
            discovery.diagnostics.is_empty(),
            "nothing here is a subsegment, so nothing should have been decoded as one: {:?}",
            discovery.diagnostics
        );
        let described: Vec<_> = discovery
            .regions
            .iter()
            .map(|region| (region.address, region.backend, region.states.as_slice()))
            .collect();
        assert_eq!(
            described,
            [
                (
                    SEGMENT + 0x1000,
                    PoolBackend::Segment,
                    [PoolState::Allocated].as_slice()
                ),
                (
                    SEGMENT + 0x2000,
                    PoolBackend::Segment,
                    [PoolState::ReusableFree].as_slice()
                ),
            ]
        );
    }

    /// A segment laid out the way ARM64 26100.1 `ntdll` lays one out, where two shapes the
    /// walker mishandled meet: a segment list whose head is only 8-aligned, and a free range
    /// that is a node of the free-page tree.
    ///
    /// Measured 2026-09-23 on `user_heap_smoke`'s child. `SegContexts` is at +0x140 of the heap
    /// and `SegmentListHead` at +0x48 of a context, so the head sits at +0x188; x64 26100.8972
    /// has the same offsets. And the segment's trailing free range carried no `TreeSignature`,
    /// because it is the free-page tree's only node and those bytes are its (null) links.
    fn segment_with_an_unaligned_head_and_a_free_tree_node(heap_key: u64) -> SyntheticMemory {
        let mut bytes = Writes::default();
        let context = HEAP + 0x108;
        let free_node = SEGMENT + 0x100 + 2 * 0x20;

        fill(&mut bytes, HEAP, 0x800);
        put_u64(&mut bytes, context, SEGMENT); // SegmentListHead, 8 bytes past a 16 boundary
        put_u64(&mut bytes, context + 0x10, free_node); // FreePageRanges.Root, not encoded
        put(&mut bytes, context + 0x20, &[12, 1]); // UnitShift, FirstDescriptorIndex
        put_u64(&mut bytes, context + 0x28, !0xffffu64); // SegmentMask: 16 descriptors

        fill(&mut bytes, SEGMENT, 0x300);
        put_u64(&mut bytes, SEGMENT, context); // ListEntry back to the head, exactly
        put_u64(
            &mut bytes,
            SEGMENT + 0x20,
            SEGMENT ^ context ^ heap_key ^ super::super::decode::PAGE_SEGMENT_SIGNATURE,
        );
        let allocated = SEGMENT + 0x100 + 0x20;
        put_u32(
            &mut bytes,
            allocated,
            super::super::decode::DESCRIPTOR_TREE_SIGNATURE,
        );
        put(&mut bytes, allocated + 0x18, &[RANGE_IN_USE]);
        put(&mut bytes, allocated + 0x1f, &[1]);
        // The free range: first, not in use, and a tree node whose links are all null — so
        // where an allocated range has its signature, this one has zeroes.
        put(&mut bytes, free_node + 0x18, &[DESCRIPTOR_FLAG_FIRST]);
        put(&mut bytes, free_node + 0x1f, &[1]);
        SyntheticMemory::new(bytes, Vec::new())
    }

    /// Both ranges of that segment are found, the free one as free, and nothing is said about
    /// either — where the walker used to read the heap's own fields as a second segment and
    /// drop the free range for the signature its node bytes cannot carry.
    #[test]
    fn test_an_unaligned_segment_list_head_and_a_free_tree_node_are_walked_as_ntdll_lays_them_out()
    {
        let heap_key = 0x55aa_1234_9876_0000;
        let memory = segment_with_an_unaligned_head_and_a_free_tree_node(heap_key);
        let layout = synthetic_layout();
        let mut discovery = Discovery::default();

        discover_segment_context(
            &memory,
            &layout,
            HEAP + 0x108,
            0,
            PoolKind::NonPagedNx,
            HeapIdentity {
                pool_state: STATE,
                heap: HEAP,
                special: false,
            },
            heap_key,
            0xa5c3_1357,
            1024,
            &SharedChunks::default(),
            &SharedChunks::default(),
            false,
            &mut discovery,
        )
        .expect("the fixture is readable throughout");

        assert!(
            discovery.diagnostics.is_empty(),
            "one segment, one allocated range and one free one, all well formed: {:?}",
            discovery.diagnostics
        );
        let described: Vec<_> = discovery
            .regions
            .iter()
            .map(|region| (region.address, region.backend, region.states.as_slice()))
            .collect();
        assert_eq!(
            described,
            [
                (
                    SEGMENT + 0x1000,
                    PoolBackend::Segment,
                    [PoolState::Allocated].as_slice()
                ),
                (
                    SEGMENT + 0x2000,
                    PoolBackend::Segment,
                    [PoolState::ReusableFree].as_slice()
                ),
            ]
        );
    }

    /// A tree whose nodes sit 8 bytes past a 16-byte boundary is walked node for node.
    ///
    /// That is every VS free tree: a free chunk's `Node` is at +0x8 of its 16-aligned header,
    /// and the eight bytes below it are the chunk's encoded `Sizes`. Each node here has such a
    /// word below it, pointing nowhere readable, which is what a walk that masks its links to
    /// 16 bytes follows instead of the real ones — the `unreadable VS free tree node 0x84a23d…`
    /// diagnostics measured on ARM64 26100.1, 2026-09-23.
    #[test]
    fn test_a_tree_of_nodes_eight_past_a_boundary_is_walked_exactly() {
        const ROOT: u64 = K + 0x40_0008;
        const LEFT: u64 = K + 0x40_1008;
        const RIGHT: u64 = K + 0x40_2008;
        let mut bytes = Writes::default();
        for (node, left, right) in [(ROOT, LEFT, RIGHT), (LEFT, 0, 0), (RIGHT, 0, 0)] {
            put_u64(&mut bytes, node - 8, 0x84a2_3d0f_b239_be00); // the chunk's encoded Sizes
            put_u64(&mut bytes, node, left);
            put_u64(&mut bytes, node + 8, right);
        }
        let memory = SyntheticMemory::new(bytes, Vec::new());
        let mut diagnostics = Vec::new();

        let mut nodes = walk_tree_nodes(&memory, ROOT, 0, 8, 16, "VS free tree", &mut diagnostics)
            .expect("the fixture is readable throughout");
        nodes.sort_unstable();

        assert_eq!(nodes, [ROOT, LEFT, RIGHT]);
        assert!(diagnostics.is_empty(), "{diagnostics:?}");
        let tree = K + 0x50_0000;
        assert_eq!(
            decode_rb_root_for(ROOT ^ tree, tree, true, false),
            Some(ROOT)
        );
    }

    /// One page segment whose every descriptor is a committed LFH range.
    ///
    /// The shared fixture has five descriptors in a sixteen-slot array and only one of them
    /// is LFH, so its descriptor loop issues almost no reads — a walk over it stays inside
    /// the overshoot limit *even with the loop's budget check deleted*, which is exactly how
    /// that missing check went unnoticed. A real kernel segment is the opposite: descriptors
    /// packed with committed subsegments, one header read each.
    ///
    /// The subsegment bodies are deliberately left unwritten. Reading one still costs the
    /// round trip this is counting, and what the walk makes of the bytes afterwards is not
    /// what is under test.
    fn segment_of_lfh_descriptors(heap_key: u64) -> SyntheticMemory {
        let mut bytes = Writes::default();
        let context = HEAP + 0x100;

        fill(&mut bytes, HEAP, 0x800);
        put_u64(&mut bytes, context, SEGMENT); // SegmentListHead
        put(&mut bytes, context + 0x20, &[12, 1]); // UnitShift, FirstDescriptorIndex
        put_u64(&mut bytes, context + 0x28, !0xffffu64); // SegmentMask: 16 descriptors

        fill(&mut bytes, SEGMENT, 0x300);
        put_u64(&mut bytes, SEGMENT, context); // ListEntry back to the head: one segment
        put_u64(
            &mut bytes,
            SEGMENT + 0x20,
            SEGMENT ^ context ^ heap_key ^ super::super::decode::PAGE_SEGMENT_SIGNATURE,
        );
        for index in 1..16u64 {
            let descriptor = SEGMENT + 0x100 + index * 0x20;
            put_u32(
                &mut bytes,
                descriptor,
                super::super::decode::DESCRIPTOR_TREE_SIGNATURE,
            );
            put(&mut bytes, descriptor + 0x18, &[RANGE_LFH]);
            put(&mut bytes, descriptor + 0x1f, &[1]);
        }
        SyntheticMemory::new(bytes, Vec::new())
    }

    /// The descriptor loop is bounded — 4096 iterations — and that is precisely why it was
    /// missed: "bounded" is not "cheap" when each iteration reads. Polling one level out, in
    /// the segment loop, let a single segment issue thousands of round trips after the
    /// deadline, which on a live KD link is minutes: the wedge, rebuilt inside its own fix.
    #[test]
    fn test_a_segment_full_of_descriptors_polls_the_budget_between_them() {
        // Enough to get through the context fields, the free-page tree, the segment header
        // and the descriptor array, so the halt lands *inside* the descriptor loop — which
        // is the loop under test.
        const ALLOWANCE: usize = 10;
        let heap_key = 0x55aa_1234_9876_0000;
        let memory = Impatient::over(segment_of_lfh_descriptors(heap_key), ALLOWANCE);
        let layout = synthetic_layout();
        let mut discovery = Discovery::default();

        let outcome = discover_segment_context(
            &memory,
            &layout,
            HEAP + 0x100,
            0,
            PoolKind::NonPagedNx,
            HeapIdentity {
                pool_state: STATE,
                heap: HEAP,
                special: false,
            },
            heap_key,
            0xa5c3_1357,
            1024,
            &SharedChunks::default(),
            &SharedChunks::default(),
            false,
            &mut discovery,
        );

        assert!(
            matches!(outcome, Err(SnapshotError::BudgetExpired)),
            "the descriptor loop has to report the halt: {outcome:?}"
        );
        assert!(
            memory.reads.get() <= ALLOWANCE + OVERSHOOT_ALLOWED,
            "{} reads for a budget of {ALLOWANCE}: the descriptor loop ran on past the \
             deadline instead of polling it",
            memory.reads.get()
        );
    }

    /// A large-allocation tree of `count` nodes whose metadata will not read.
    ///
    /// Each node carries only its `_RTL_BALANCED_NODE` links, so the tree walk finds them all
    /// and the metadata read that follows fails — which is the path under test. A node whose
    /// metadata *does* read reaches `lookup_big_page_target`, and that polls the budget
    /// itself, so it could never demonstrate the missing check.
    fn large_allocation_tree(count: u64) -> SyntheticMemory {
        let mut bytes = Writes::default();
        let tree = HEAP + 0x400;
        let node = |index: u64| LARGE_META + index * 0x40;

        fill(&mut bytes, tree, 0x10);
        put_u64(&mut bytes, tree, node(0)); // Root
        for index in 0..count {
            // Left chains to the next node, Right terminates. Only 0x10 bytes per node, so
            // the 0x28-byte metadata read spans into a gap and fails.
            fill(&mut bytes, node(index), 0x10);
            let next = if index + 1 < count {
                node(index + 1)
            } else {
                0
            };
            put_u64(&mut bytes, node(index), next);
        }
        SyntheticMemory::new(bytes, Vec::new())
    }

    /// The large-allocation loop reads once per node and `nodes` is capped only by
    /// `traversal_limit` — a million. Without a poll here the deadline is not consulted again
    /// until the enclosing heap.
    #[test]
    fn test_the_large_allocation_loop_polls_the_budget_between_nodes() {
        // The tree walk itself costs a read per node plus the root and encoded flag, so the
        // budget has to clear that for the halt to land in the node loop below it.
        const NODES: u64 = 12;
        const ALLOWANCE: usize = 16;
        let memory = Impatient::over(large_allocation_tree(NODES), ALLOWANCE);
        let layout = synthetic_layout();
        let mut discovery = Discovery::default();

        let outcome = discover_large_allocations(
            &memory,
            &layout,
            HEAP,
            0,
            PoolKind::NonPagedNx,
            HeapIdentity {
                pool_state: STATE,
                heap: HEAP,
                special: false,
            },
            0,
            1024,
            &mut discovery,
        );

        assert!(
            matches!(outcome, Err(SnapshotError::BudgetExpired)),
            "the node loop has to report the halt: {outcome:?}"
        );
        assert!(
            memory.reads.get() <= ALLOWANCE + OVERSHOOT_ALLOWED,
            "{} reads for a budget of {ALLOWANCE}: the node loop ran on past the deadline",
            memory.reads.get()
        );
    }

    /// One extent far larger than a single read should be, reported as entirely committed.
    struct OneHugeExtent {
        reads: Cell<usize>,
        allowance: usize,
    }

    impl PoolMemory for OneHugeExtent {
        fn read_exact(&self, _address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            self.reads.set(self.reads.get() + 1);
            Ok(vec![0; size])
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            Ok((address, size))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }

        fn out_of_budget(&self) -> bool {
            self.reads.get() >= self.allowance
        }
    }

    /// The loops around a read can poll perfectly and still overrun, because the read itself
    /// is one synchronous transfer: a large committed page range is megabytes in flight with
    /// no opportunity to notice the deadline. Chunking the transfer is what makes the ceiling
    /// hold whatever the pool's geometry turns out to be.
    #[test]
    fn test_a_large_extent_is_read_in_chunks_that_observe_the_deadline() {
        const ALLOWANCE: usize = 2;
        let memory = OneHugeExtent {
            reads: Cell::new(0),
            allowance: ALLOWANCE,
        };
        let layout = vs_layout(VsFixture::Inline);
        let mut region = lfh_region(0x1000, 0x100);
        region.size = EXTENT_READ_CHUNK * 8;
        region.bitmap = vec![0xff; 64];
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };

        let outcome = walker.walk_region(&region, &mut PoolSnapshot::default());

        assert!(
            matches!(outcome, Err(SnapshotError::BudgetExpired)),
            "an extent that outlives the deadline has to stop mid-transfer: {outcome:?}"
        );
        assert_eq!(
            memory.reads.get(),
            ALLOWANCE,
            "the extent was not chunked, so the deadline could not be observed while it \
             transferred"
        );
    }

    /// A source that answers every read one byte short.
    struct ShortChunks {
        reads: Cell<usize>,
    }

    impl PoolMemory for ShortChunks {
        fn read_exact(&self, _address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            self.reads.set(self.reads.get() + 1);
            Ok(vec![0; size.saturating_sub(1)])
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            Ok((address, size))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }
    }

    /// Reading in pieces makes a short chunk dangerous in a way a short whole-extent read
    /// never was: the next chunk's address comes from how much has been collected, so
    /// accepting a short one shifts every later chunk and assembles contiguous bytes out of
    /// discontiguous addresses. The decoders cannot tell — they would report allocations that
    /// were never at those addresses. So the extent fails, and stays honestly unreadable.
    #[test]
    fn test_a_short_chunk_fails_the_extent_instead_of_misassembling_it() {
        let memory = ShortChunks {
            reads: Cell::new(0),
        };
        let layout = vs_layout(VsFixture::Inline);
        let mut region = lfh_region(0x1000, 0x100);
        region.size = EXTENT_READ_CHUNK * 3;
        region.bitmap = vec![0xff; 64];
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };

        assert!(
            matches!(
                walker.read_extent(region.address, region.size),
                Err(SnapshotError::InvalidData { .. })
            ),
            "a short chunk has to fail the extent"
        );
        assert_eq!(
            memory.reads.get(),
            1,
            "it must not read on after the short chunk"
        );

        // And through the region walk: reported as unreadable, with nothing decoded out of a
        // buffer that was never assembled.
        let mut snapshot = PoolSnapshot::default();
        walker.walk_region(&region, &mut snapshot).unwrap();

        assert!(
            snapshot
                .spans
                .iter()
                .all(|span| span.state == PoolState::Unreadable),
            "a misassembled extent must not yield decoded allocations: {:?}",
            snapshot.spans
        );
        assert!(
            snapshot
                .diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("cannot read region")),
            "the failure has to be said out loud: {:?}",
            snapshot.diagnostics
        );
    }

    /// The common case must not pay for the uncommon one: an extent that fits in a chunk is
    /// still exactly one read.
    #[test]
    fn test_a_small_extent_is_still_a_single_read() {
        let memory = OneHugeExtent {
            reads: Cell::new(0),
            allowance: usize::MAX,
        };
        let layout = vs_layout(VsFixture::Inline);
        let region = lfh_region(0x1000, 0x100);
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };

        walker
            .walk_region(&region, &mut PoolSnapshot::default())
            .unwrap();

        assert_eq!(memory.reads.get(), 1);
    }

    /// Discovery finds the regions and the walk reads what is in them, so a budget spent
    /// entirely on the first half yields an empty pool. Two things keep that from happening:
    /// discovery is held to a share of the budget, and — pinned here — the regions it found
    /// before stopping survive the halt instead of being dropped with the error.
    #[test]
    fn test_regions_found_before_the_halt_are_not_thrown_away() {
        // Enough reads to get past VS evidence and into the segment walk, not enough to
        // finish it.
        let snapshot = walk_impatiently(120);
        let summary = snapshot
            .diagnostics
            .examples()
            .iter()
            .find(|message| message.contains("discovered regions"))
            .expect("the walk stopped short and must say so");
        let tokens: Vec<&str> = summary.split_whitespace().collect();
        let discovered: usize = tokens
            .iter()
            .position(|token| *token == "discovered")
            .and_then(|index| index.checked_sub(1))
            .and_then(|index| tokens[index].parse().ok())
            .unwrap_or_else(|| panic!("no region count in `{summary}`"));

        assert!(
            discovered > 0,
            "discovery's partial results were discarded with the halt: `{summary}`"
        );
    }

    /// Neither half of the walk is useful without the other, so discovery gets a share of
    /// the budget rather than all of it — and the whole walk still stops at the deadline it
    /// was given.
    #[test]
    fn test_discovery_gets_a_share_of_the_budget_and_the_walk_the_rest() {
        let start = Instant::now();
        let budget = Duration::from_secs(120);
        let (discovery, whole) = budget_deadlines(start, Some(budget));
        let (discovery, whole) = (discovery.unwrap(), whole.unwrap());

        assert_eq!(whole, start + budget);
        assert!(
            discovery < whole,
            "discovery must yield before the deadline"
        );
        assert!(
            discovery > start,
            "discovery must get real time, not a token slice"
        );
        assert_eq!(budget_deadlines(start, None), (None, None));
        // `PoolWalk::within` accepts any `Duration`, so a budget longer than `Instant` can
        // represent is reachable from safe public API. Adding it would panic; a budget
        // beyond measuring is a request to run unbounded, and is answered as one.
        assert_eq!(budget_deadlines(start, Some(Duration::MAX)), (None, None));
    }

    /// A budget and a Ctrl+C both halt the walk, and they must not be confused. Running out
    /// of time means "here is what I got"; an operator interrupting means "stop" — handing
    /// them a snapshot they cancelled, which then renders as a full table, is not that.
    #[test]
    fn test_an_interrupt_still_stops_the_walk_outright() {
        struct Interrupting(SyntheticMemory);

        impl PoolMemory for Interrupting {
            fn read_exact(&self, address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
                self.0.read_exact(address, size)
            }

            fn valid_region(
                &self,
                address: u64,
                size: usize,
            ) -> Result<(u64, usize), SnapshotError> {
                self.0.valid_region(address, size)
            }

            fn interrupted(&self) -> Result<bool, SnapshotError> {
                Ok(true)
            }
        }

        let memory = Interrupting(synthetic_memory());
        let layout = synthetic_layout();

        assert!(matches!(
            SnapshotWalker {
                memory: &memory,
                layout: &layout,
                traversal_limit: 1024,
            }
            .walk(Some(Duration::from_secs(600))),
            Err(SnapshotError::Interrupted)
        ));
    }

    /// Memory that hands back one small extent at a time and runs out of budget after a
    /// fixed number of them.
    ///
    /// This is the shape that makes a per-*region* check insufficient. A region the
    /// allocator left with holes is read one committed run at a time — a `valid_region` and
    /// a `read_exact` each, both over the wire on a live target — so checking only on the
    /// way in lets a single region overrun the deadline by as many round trips as it has
    /// extents.
    struct Fragmented {
        extent: usize,
        allowance: usize,
        extents_read: Cell<usize>,
    }

    impl PoolMemory for Fragmented {
        fn read_exact(&self, _address: u64, size: usize) -> Result<Vec<u8>, SnapshotError> {
            Ok(vec![0; size])
        }

        fn valid_region(&self, address: u64, size: usize) -> Result<(u64, usize), SnapshotError> {
            self.extents_read.set(self.extents_read.get() + 1);
            Ok((address, size.min(self.extent)))
        }

        fn interrupted(&self) -> Result<bool, SnapshotError> {
            Ok(false)
        }

        fn out_of_budget(&self) -> bool {
            self.extents_read.get() >= self.allowance
        }
    }

    #[test]
    fn test_a_fragmented_region_checks_the_budget_between_extents() {
        // 0x1000 of region in 0x400 extents is four round trips; the budget allows three.
        let memory = Fragmented {
            extent: 0x400,
            allowance: 3,
            extents_read: Cell::new(0),
        };
        let layout = vs_layout(VsFixture::Inline);
        let mut region = lfh_region(0x1000, 0x100);
        region.bitmap = vec![0xff; 16];
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1000,
        };

        let outcome = walker.walk_region(&region, &mut PoolSnapshot::default());

        assert!(
            matches!(outcome, Err(SnapshotError::BudgetExpired)),
            "the region walk has to report the halt, not absorb it: {outcome:?}"
        );
        assert_eq!(
            memory.extents_read.get(),
            3,
            "the region went on reading past its deadline"
        );
    }

    /// A budget nobody comes close to spending must not change the answer.
    #[test]
    fn test_a_generous_budget_walks_exactly_as_an_unbounded_one_does() {
        let memory = synthetic_memory();
        let layout = synthetic_layout();
        let walker = SnapshotWalker {
            memory: &memory,
            layout: &layout,
            traversal_limit: 1024,
        };

        let unbounded = walker.walk(None).unwrap();
        let budgeted = walker.walk(Some(Duration::from_secs(600))).unwrap();

        assert_eq!(budgeted.spans, unbounded.spans);
        assert_eq!(budgeted.complete, unbounded.complete);
        assert_eq!(budgeted.diagnostics, unbounded.diagnostics);
    }

    // ---- diagnostic floods -------------------------------------------------------------

    /// A thousand of one complaint and one of another, the shape of a real flood.
    fn flooded() -> PoolDiagnostics {
        let mut diagnostics: Vec<String> = (0..1000)
            .map(|node| format!("unreadable VS free tree node {node:#x}: sparse memory"))
            .collect();
        diagnostics.push("rejecting implausible LFH unit size 3 at 0x1000".into());
        PoolDiagnostics::from_iter(diagnostics)
    }

    /// A live target keeps allocating between the reads of one walk, so a single stale list
    /// pointer yields one complaint per node — 14k of them measured on a busy kernel, which
    /// buries the few distinct problems and leaves every consumer truncating by position.
    /// Collapsing keeps examples and, crucially, the *count*: the volume is itself the
    /// finding, so it must survive.
    #[test]
    fn test_a_flood_of_one_complaint_becomes_examples_plus_a_count() {
        let diagnostics = flooded();

        assert_eq!(
            diagnostics.examples().len(),
            DIAGNOSTIC_EXAMPLES + 1,
            "expected {DIAGNOSTIC_EXAMPLES} examples of the flood, plus the distinct message"
        );
        assert!(diagnostics.examples()[0].contains("unreadable VS free tree node 0x0"));
        assert!(
            diagnostics
                .examples()
                .iter()
                .any(|message| message.contains("rejecting implausible LFH unit size")),
            "a distinct complaint must not be collapsed away by a flood of another"
        );

        let totals: Vec<usize> = diagnostics.shapes().iter().map(|seen| seen.total).collect();
        assert_eq!(
            totals,
            vec![1000, 1],
            "shapes carry the whole count each, in the order first seen: {:?}",
            diagnostics.shapes()
        );
        assert!(
            diagnostics.lines().last().is_some_and(
                |line| line.contains(&format!("and {} more", 1000 - DIAGNOSTIC_EXAMPLES))
            ),
            "the rendering owes the reader the count too: {:?}",
            diagnostics.lines()
        );
    }

    /// The count of the *walk* is not the count of what survived the cap, and the gap is two
    /// orders of magnitude on a real target. Reporting the latter as the former — measured
    /// once as "71 diagnostics" for a walk that made ~7,700 complaints — describes the
    /// collapsing as though it were a property of the pool, which is precisely the lie this
    /// module is built to avoid.
    #[test]
    fn test_the_emitted_count_is_the_walk_not_the_sample() {
        let diagnostics = flooded();

        assert_eq!(diagnostics.emitted(), 1001, "every message is counted");
        assert!(
            diagnostics.emitted() > diagnostics.lines().len() * 50,
            "a caller that counted lines ({}) instead of messages ({}) would be off by two \
             orders of magnitude and never know",
            diagnostics.lines().len(),
            diagnostics.emitted()
        );
    }

    /// Merging is by message, not by wholesale concatenation: discovery's complaints join the
    /// walk's own shapes rather than starting fresh ones, or a flood split across the two
    /// phases would keep twice the examples and report itself as two findings.
    #[test]
    fn test_extending_merges_into_the_shapes_already_seen() {
        let mut diagnostics = flooded();
        diagnostics.extend(
            (1000..1010)
                .map(|node| format!("unreadable VS free tree node {node:#x}: sparse memory")),
        );

        assert_eq!(diagnostics.shapes().len(), 2, "no shape was duplicated");
        assert_eq!(diagnostics.shapes()[0].total, 1010);
        assert_eq!(diagnostics.emitted(), 1011);
        assert_eq!(
            diagnostics.examples().len(),
            DIAGNOSTIC_EXAMPLES + 1,
            "the cap holds across the merge"
        );
    }

    /// Grouping is by shape, so the same complaint about different addresses is one group
    /// while genuinely different complaints stay apart.
    #[test]
    fn test_diagnostic_shape_ignores_numbers_but_not_wording() {
        assert_eq!(
            diagnostic_shape("unreadable VS free tree node 0xdeadbeef: sparse"),
            diagnostic_shape("unreadable VS free tree node 0x41414141: sparse")
        );
        assert_ne!(
            diagnostic_shape("unreadable VS free tree node 0xdeadbeef"),
            diagnostic_shape("unreadable VS delay-free node 0xdeadbeef")
        );
    }

    #[test]
    fn test_special_pool_kinds_cover_all_heap_slots() {
        assert_eq!(SPECIAL_POOL_KINDS.len(), 4);
        assert_eq!(SPECIAL_POOL_KINDS[3], PoolKind::SpecialPrototypePaged);
        assert!(SPECIAL_POOL_KINDS[3].is_paged());
    }

    /// Whether a VS user block skips a page end is read off the target's own context, once per
    /// heap, and lands on every region of that heap — bit 0 of `Config` and nothing else, since
    /// bit 1 (`EnableDelayFree`) shares the byte. A layout with no `Config` reads unset and says
    /// so, which is the one way a target carrying the flag can be walked without it.
    #[test]
    fn test_discovery_reads_whether_a_vs_user_block_skips_a_page_end_from_the_context() {
        let identity = HeapIdentity {
            pool_state: STATE,
            heap: HEAP,
            special: false,
        };
        let discover = |memory: &SyntheticMemory, layout: &PoolLayout| {
            let mut discovery = Discovery::default();
            discover_heap_regions(
                memory,
                layout,
                HEAP,
                0,
                PoolKind::NonPagedNx,
                identity,
                None,
                0x55aa_1234_9876_0000,
                0xa5c3_1357,
                1024,
                &mut discovery,
            )
            .expect("the fixture is readable throughout");
            discovery
        };
        let config = HEAP + 0x300 + 0x38;
        for (flags, expected) in [(0u8, false), (1, true), (2, false), (3, true)] {
            let mut memory = synthetic_memory();
            memory.poke(config, flags);

            let discovery = discover(&memory, &synthetic_layout());

            let vs: Vec<_> = discovery
                .regions
                .iter()
                .filter(|region| region.backend == PoolBackend::Vs)
                .collect();
            assert!(!vs.is_empty(), "the fixture has a VS subsegment");
            assert!(
                vs.iter().all(|region| region.vs_skips_page_end == expected),
                "config {flags:#x} should read {expected}"
            );
            assert!(
                !discovery
                    .diagnostics
                    .iter()
                    .any(|line| line.contains("Config")),
                "{:?}",
                discovery.diagnostics
            );
        }

        let mut memory = synthetic_memory();
        memory.poke(config, 1);
        let mut layout = synthetic_layout();
        layout
            .types
            .get_mut("_HEAP_VS_CONTEXT")
            .unwrap()
            .fields
            .remove("Config");

        let discovery = discover(&memory, &layout);

        assert!(
            discovery
                .regions
                .iter()
                .all(|region| !region.vs_skips_page_end)
        );
        assert!(
            discovery
                .diagnostics
                .iter()
                .any(|line| line.contains("has no resolved Config")),
            "{:?}",
            discovery.diagnostics
        );
    }

    #[test]
    fn test_discovery_skips_missing_optional_large_layout() {
        let memory = big_page_memory(LARGE_VA, 4, 0);
        let mut layout = synthetic_layout();
        layout.types.remove("_HEAP_LARGE_ALLOC_DATA");
        let mut discovery = Discovery::default();

        discover_large_allocations(
            &memory,
            &layout,
            HEAP,
            0,
            PoolKind::NonPagedNx,
            HeapIdentity {
                pool_state: STATE,
                heap: HEAP,
                special: false,
            },
            0,
            1024,
            &mut discovery,
        )
        .unwrap();

        assert!(discovery.regions.is_empty());
        assert_eq!(memory.read_calls.get(), 0);
    }

    #[test]
    fn test_big_page_lookup_skips_missing_optional_layout() {
        let memory = big_page_memory(LARGE_VA, 4, 0);
        let mut layout = synthetic_layout();
        layout.types.remove("_POOL_TRACKER_BIG_PAGES");

        assert!(
            BigPageTable::open(&memory, &layout, &mut Vec::new())
                .unwrap()
                .is_none()
        );
        assert_eq!(memory.read_calls.get(), 0);
    }

    #[test]
    fn test_big_page_lookup_batches_collision_chain() {
        let memory = big_page_memory(LARGE_VA, 512, 300);
        let layout = synthetic_layout();
        let mut diagnostics = Vec::new();
        let mut table = BigPageTable::open(&memory, &layout, &mut diagnostics)
            .unwrap()
            .unwrap();

        assert_eq!(
            table.lookup(&memory, LARGE_VA, &mut diagnostics).unwrap(),
            Some(BigPageEntry {
                tag: u32::from_le_bytes(*b"BTCH"),
                size: 0x9000,
            })
        );
        assert!(memory.read_calls.get() <= 5);
        assert!(diagnostics.is_empty());

        // The chain crossed both batches, so asking again is pure arithmetic. This is what
        // makes the lookup affordable per *page range* rather than per large allocation.
        let reads = memory.read_calls.get();
        assert!(
            table
                .lookup(&memory, LARGE_VA, &mut diagnostics)
                .unwrap()
                .is_some()
        );
        assert_eq!(memory.read_calls.get(), reads);
    }

    #[test]
    fn test_big_page_lookup_honors_interrupt_between_batches() {
        let mut memory = big_page_memory(LARGE_VA, 512, 300);
        let layout = synthetic_layout();
        let mut table = BigPageTable::open(&memory, &layout, &mut Vec::new())
            .unwrap()
            .unwrap();
        memory.interrupt_after_checks = Some(1);

        assert!(matches!(
            table.lookup(&memory, LARGE_VA, &mut Vec::new()),
            Err(SnapshotError::Interrupted)
        ));
        assert_eq!(memory.interrupt_checks.get(), 2);
    }

    /// `nt!ExpRemoveTagForBigPages` frees an entry with `lock inc [Va]`, so a freed
    /// allocation is still in the table under `address | 1` with its old tag and size, and
    /// `nt`'s own comparison is `cmp rcx,rdi` — exact. Matching on `Va & !1`, as this did,
    /// answers for a page that has been given back with the tag it used to carry.
    #[test]
    fn test_big_page_lookup_walks_past_a_freed_entry_to_the_live_one() {
        let count = 8;
        let layout = synthetic_layout();
        let mut bytes = vec![0u8; count * 0x20];
        let first = super::super::decode::big_page_hash(LARGE_VA, count).unwrap();
        let put = |bytes: &mut Vec<u8>, index: usize, va: u64, tag: &[u8; 4], size: u64| {
            let offset = (index % count) * 0x20;
            bytes[offset..offset + 8].copy_from_slice(&va.to_le_bytes());
            bytes[offset + 8..offset + 12].copy_from_slice(tag);
            bytes[offset + 0x10..offset + 0x18].copy_from_slice(&size.to_le_bytes());
        };
        // The same address, freed, sitting where the hash lands. Then the live entry.
        put(&mut bytes, first, LARGE_VA | 1, b"DEAD", 0x3000);
        put(&mut bytes, first + 1, LARGE_VA, b"LIVE", 0x1800);
        let memory = BigPageMemory {
            table: bytes,
            count,
            read_calls: Cell::new(0),
            interrupt_checks: Cell::new(0),
            interrupt_after_checks: None,
        };
        let mut table = BigPageTable::open(&memory, &layout, &mut Vec::new())
            .unwrap()
            .unwrap();

        assert_eq!(
            table.lookup(&memory, LARGE_VA, &mut Vec::new()).unwrap(),
            Some(BigPageEntry {
                tag: u32::from_le_bytes(*b"LIVE"),
                size: 0x1800,
            })
        );
    }

    /// A slot the allocator has never used reads `1` — `POOL_BIG_TABLE_ENTRY_FREE` with no
    /// address behind it — and ends the chain, because `ExpAddTagForBigPages` claims the
    /// first bit-0-set slot from the hash and so could not have walked past this one. The
    /// test that matters is the *miss*: a lookup that scans the whole table instead costs a
    /// read of every batch in it.
    #[test]
    fn test_big_page_lookup_stops_at_a_never_used_slot() {
        let count = 512;
        let layout = synthetic_layout();
        let mut bytes = vec![0u8; count * 0x20];
        let absent = LARGE_VA + 0x20_0000;
        let first = super::super::decode::big_page_hash(absent, count).unwrap();
        // Every slot occupied, so that a walk which does not stop where it should has nothing
        // else to stop it and runs into the next batch. Each occupant is distinct from
        // `absent`, which the chain must not accidentally contain.
        for index in 0..count {
            let offset = index * 0x20;
            let occupant = LARGE_VA + 0x100_0000 + index as u64 * 0x1000;
            bytes[offset..offset + 8].copy_from_slice(&occupant.to_le_bytes());
        }
        let never_used = ((first + 3) % count) * 0x20;
        bytes[never_used..never_used + 8].copy_from_slice(&1u64.to_le_bytes());
        assert!(
            first % BIG_PAGE_BATCH + 3 < BIG_PAGE_BATCH,
            "the fixture's chain has to stay inside one batch for the read count to mean              anything"
        );
        let memory = BigPageMemory {
            table: bytes,
            count,
            read_calls: Cell::new(0),
            interrupt_checks: Cell::new(0),
            interrupt_after_checks: None,
        };
        let mut table = BigPageTable::open(&memory, &layout, &mut Vec::new())
            .unwrap()
            .unwrap();
        let reads = memory.read_calls.get();

        assert_eq!(
            table.lookup(&memory, absent, &mut Vec::new()).unwrap(),
            None
        );
        // One batch, not both: the chain ended before the walk left it.
        assert_eq!(memory.read_calls.get() - reads, 1);
    }

    #[test]
    fn test_snapshot_errors_preserve_category_and_source() {
        let memory = synthetic_memory();
        let error = memory.read_exact(0, 1).unwrap_err();
        assert!(matches!(&error, SnapshotError::Read { .. }));
        assert!(std::error::Error::source(&error).is_some());

        let error = guarded_read(&memory, SEGMENT + 0x7800, 0x10).unwrap_err();
        assert!(matches!(error, SnapshotError::RegionValidation { .. }));

        let error = scalar(&ShortMemory, 0x1000, 1).unwrap_err();
        assert!(matches!(
            error,
            SnapshotError::InvalidData { detail } if detail == "short u8"
        ));
    }
}