safeptrace 0.5.8

Safe, ergonomic Rust wrappers around the Linux ptrace(2) API.
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
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

/// Force only the PIDFD_THREAD open to fail as it does before Linux 6.9.
/// Other ptracer test threads remain on their normal kernel path.
struct LegacyThreadGroup {
    pid: Pid,
    _ptracer: std::marker::PhantomData<std::rc::Rc<()>>,
}

impl LegacyThreadGroup {
    fn new(pid: Pid) -> Self {
        FORCE_LEGACY_THREAD_GROUP.with(|group| {
            assert!(group.get().is_none());
            group.set(Some(pid.into()));
        });
        Self {
            pid,
            _ptracer: std::marker::PhantomData,
        }
    }
}

impl Drop for LegacyThreadGroup {
    fn drop(&mut self) {
        FORCE_LEGACY_THREAD_GROUP.with(|group| {
            if group.get() == Some(self.pid.into()) {
                group.set(None);
            }
        });
    }
}

#[cfg(not(sanitized))]
fn run_legacy_test_outer(name: &str) -> bool {
    run_legacy_test_outer_with_outcome(name, None)
}

#[cfg(not(sanitized))]
fn run_legacy_test_outer_with_outcome(name: &str, marker: Option<&str>) -> bool {
    run_legacy_test_outer_checked(name, marker, |_| {})
}

/// [`run_legacy_test_outer_with_outcome`], and `check` then inspects the
/// inner run.
#[cfg(not(sanitized))]
fn run_legacy_test_outer_checked(
    name: &str,
    marker: Option<&str>,
    check: impl FnOnce(&BoundedSubprocess),
) -> bool {
    let Some(result) = run_legacy_test_outer_bounded(name) else {
        return false;
    };
    if let Some(marker) = marker {
        assert!(
            String::from_utf8_lossy(&result.output.stdout)
                .lines()
                .any(|line| line == marker),
            "legacy-thread test omitted actual outcome marker {marker}: {result:?}"
        );
        emit_completion_marker(marker);
    }
    check(&result);
    true
}

/// As [`run_legacy_test_outer`], and re-emits each line of the inner run's
/// stdout that starts with `prefix`, so that a passing run's report reaches
/// the outer log.
#[cfg(not(sanitized))]
fn run_legacy_test_outer_reporting(name: &str, prefix: &str) -> bool {
    let Some(result) = run_legacy_test_outer_bounded(name) else {
        return false;
    };
    for line in String::from_utf8_lossy(&result.output.stdout)
        .lines()
        .filter(|line| line.starts_with(prefix))
    {
        emit_completion_marker(line);
    }
    true
}

/// Runs the test `name` alone in a bounded child process and asserts that it
/// passed, returning its result. In that child process, returns `None`.
#[cfg(not(sanitized))]
fn run_legacy_test_outer_bounded(name: &str) -> Option<BoundedSubprocess> {
    const INNER: &str = "SAFEPTRACE_LEGACY_THREAD_INNER";
    if env::var(INNER).as_deref() == Ok(name) {
        return None;
    }
    let result = run_exact_test_bounded(
        &format!("notifier::test::{name}"),
        &[(INNER, name)],
        false,
        Duration::from_secs(5),
    )
    .expect("start bounded legacy-thread test");
    assert!(
        !result.timed_out,
        "legacy-thread test timed out: {result:?}"
    );
    assert!(
        result.output.status.success(),
        "legacy-thread test failed: {result:?}"
    );
    Some(result)
}

#[test]
fn forced_legacy_leader_preserves_pidfd_waits_and_cancellation() {
    let (pid, mut cleanup) = spawn_stopped_process(None).unwrap();
    let _force = LegacyThreadGroup::new(pid);
    let running = Running::new_on_ptracer_thread(pid.into()).unwrap();
    cleanup.bind_running_notifier(&running).unwrap();
    let terminal = running.terminal_cleanup();
    assert_eq!(terminal.has_thread_pidfd(), Ok(false));
    assert!(matches!(
        terminal.event.identity().unwrap().pidfd,
        ThreadHandle::LegacyLeader { .. }
    ));
    terminal.request_sigkill().unwrap();
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    assert_eq!(terminal.request_sigkill(), Err(Errno::ESRCH));
    assert_eq!(terminal.has_thread_pidfd(), Ok(false));
    cleanup.disarm();
    assert!(!std::path::Path::new(&format!("/proc/{pid}")).exists());
}

#[cfg(not(sanitized))]
fn spawn_legacy_thread_tracee(
    pause_thread: bool,
) -> (Pid, Stopped, TraceeCleanupGuard, LegacyThreadGroup) {
    spawn_legacy_thread_tracee_with_options(pause_thread, legacy_thread_options())
}

#[cfg(not(sanitized))]
fn legacy_thread_options() -> Options {
    Options::PTRACE_O_TRACECLONE
        | Options::PTRACE_O_TRACEEXEC
        | Options::PTRACE_O_TRACEEXIT
        | Options::PTRACE_O_EXITKILL
}

#[cfg(not(sanitized))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum LegacyAttachMethod {
    Traceme,
    Seize,
    Attach,
}

#[cfg(not(sanitized))]
fn spawn_legacy_thread_tracee_with_options(
    pause_thread: bool,
    options: Options,
) -> (Pid, Stopped, TraceeCleanupGuard, LegacyThreadGroup) {
    spawn_legacy_thread_tracee_with_method(pause_thread, options, LegacyAttachMethod::Traceme)
}

#[cfg(not(sanitized))]
fn spawn_legacy_thread_tracee_with_method(
    pause_thread: bool,
    options: Options,
    method: LegacyAttachMethod,
) -> (Pid, Stopped, TraceeCleanupGuard, LegacyThreadGroup) {
    spawn_legacy_threads(if pause_thread { &[0] } else { &[17, 23] }, options, method)
}

/// A traced process whose main thread runs one pthread per entry of
/// `modes`, each joined before the next starts: 0 pauses forever, 1 spins
/// for about 300 µs between getppid calls forever, 17 raises SIGTRAP and
/// exits 17, a value from 1000 reads one byte at a time from descriptor
/// `value - 1000` forever, and any other value exits with it.
#[cfg(not(sanitized))]
fn spawn_legacy_threads(
    modes: &'static [usize],
    options: Options,
    method: LegacyAttachMethod,
) -> (Pid, Stopped, TraceeCleanupGuard, LegacyThreadGroup) {
    let mut start = [-1, -1];
    if method != LegacyAttachMethod::Traceme {
        assert_eq!(
            unsafe { libc::pipe2(start.as_mut_ptr(), libc::O_CLOEXEC) },
            0
        );
    }
    let root = match unsafe { fork() }.unwrap() {
        ForkResult::Parent { child } => child,
        ForkResult::Child => {
            if method == LegacyAttachMethod::Traceme {
                crate::traceme_and_stop().unwrap();
            } else {
                unsafe { libc::close(start[1]) };
                let mut byte = 0u8;
                if unsafe { libc::read(start[0], (&mut byte as *mut u8).cast(), 1) } != 1 {
                    unsafe { libc::_exit(125) };
                }
                unsafe { libc::close(start[0]) };
            }
            // Raw pthread creation avoids std's locks inherited across fork
            // from the multithreaded Rust test harness.
            extern "C" fn guest_thread(argument: *mut libc::c_void) -> *mut libc::c_void {
                let mode = argument as usize;
                if mode == 0 {
                    loop {
                        unsafe { libc::pause() };
                    }
                }
                if mode == 1 {
                    let now = || {
                        let mut now = libc::timespec {
                            tv_sec: 0,
                            tv_nsec: 0,
                        };
                        // The vDSO answers without a syscall stop.
                        unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now) };
                        now.tv_sec * 1_000_000_000 + now.tv_nsec
                    };
                    loop {
                        let start = now();
                        while now() - start < 300_000 {}
                        unsafe { libc::syscall(libc::SYS_getppid) };
                    }
                }
                if mode >= 2000 {
                    // Read one byte, then exit.
                    let mut byte = 0u8;
                    unsafe { libc::read((mode - 2000) as i32, (&mut byte as *mut u8).cast(), 1) };
                    unsafe { libc::syscall(libc::SYS_exit, 0) };
                }
                if mode >= 1000 {
                    let mut byte = 0u8;
                    loop {
                        unsafe {
                            libc::read((mode - 1000) as i32, (&mut byte as *mut u8).cast(), 1)
                        };
                    }
                }
                if mode == 17 {
                    unsafe { libc::raise(libc::SIGTRAP) };
                }
                unsafe { libc::syscall(libc::SYS_exit, mode as i32) };
                std::ptr::null_mut()
            }
            for mode in modes {
                let mut thread = mem::MaybeUninit::<libc::pthread_t>::uninit();
                let created = unsafe {
                    libc::pthread_create(
                        thread.as_mut_ptr(),
                        std::ptr::null(),
                        guest_thread,
                        *mode as *mut libc::c_void,
                    )
                };
                if created != 0 {
                    unsafe { libc::_exit(126) };
                }
                let joined =
                    unsafe { libc::pthread_join(thread.assume_init(), std::ptr::null_mut()) };
                if joined != 0 {
                    unsafe { libc::_exit(127) };
                }
            }
            unsafe { libc::_exit(42) };
        }
    };
    let mut cleanup = TraceeCleanupGuard::new(root).unwrap();
    let force = LegacyThreadGroup::new(root);
    let stopped = if method == LegacyAttachMethod::Traceme {
        let status = waitpid_status_bounded(root, libc::WUNTRACED, TRACEE_WAIT_TIMEOUT).unwrap();
        assert!(libc::WIFSTOPPED(status));
        assert_eq!(libc::WSTOPSIG(status), libc::SIGSTOP);
        let stopped = Stopped::new_unchecked_on_ptracer_thread(root.into()).unwrap();
        stopped.setoptions(options).unwrap();
        stopped
    } else {
        unsafe { libc::close(start[0]) };
        let running = if method == LegacyAttachMethod::Seize {
            let running = Running::seize_on_ptracer_thread(root.into(), options).unwrap();
            running.interrupt().unwrap();
            running
        } else {
            Running::attach_on_ptracer_thread(root.into()).unwrap()
        };
        let (stopped, event) = running
            .wait_sync_on_ptracer_thread()
            .wait()
            .unwrap()
            .assume_stopped();
        if method == LegacyAttachMethod::Seize {
            assert_eq!(event, crate::Event::Stop);
        } else {
            assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
            stopped.setoptions(options).unwrap();
        }
        assert_eq!(unsafe { libc::write(start[1], b"s".as_ptr().cast(), 1) }, 1);
        unsafe { libc::close(start[1]) };
        stopped
    };
    cleanup.bind_notifier(&stopped).unwrap();
    (root, stopped, cleanup, force)
}

#[cfg(not(sanitized))]
fn legacy_child_cleanup(child: &Running) -> TraceeCleanupGuard {
    let identity = child.terminal_cleanup().event.identity().unwrap().clone();
    assert_ne!(
        child.pid(),
        identity.snapshot.tgid,
        "fixture must create a non-leader"
    );
    assert!(matches!(identity.pidfd, ThreadHandle::Procfs { .. }));
    let mut cleanup = TraceeCleanupGuard {
        pid: child.pid().into(),
        pidfd: identity.pidfd.try_clone().unwrap(),
        ownership: TraceeCleanupOwnership::PreRegistration,
        armed: true,
    };
    cleanup.bind_running_notifier(child).unwrap();
    cleanup
}

// The fixtures below have no exec path and keep the same paused pthread
// generation alive until the test's retained wait owner reaps it. This raw
// test stimulus checks notifier stop delivery; production cancellation never
// substitutes a numeric signal for a retained thread lifetime descriptor.
#[cfg(not(sanitized))]
fn request_fixture_thread_sigstop(root: Pid, tid: crate::Pid) {
    assert_eq!(
        unsafe { libc::syscall(libc::SYS_tgkill, root.as_raw(), tid.as_raw(), libc::SIGSTOP) },
        0,
        "send controlled SIGSTOP to the fixture's live original thread"
    );
}

#[cfg(not(sanitized))]
fn assert_no_pending_sigstop(stopped: &Stopped) {
    for flags in [None, Some(crate::PeekSigInfoFlags::SHARED)] {
        assert!(
            stopped
                .peeksiginfo(flags)
                .unwrap()
                .iter()
                .all(|info| info.si_signo != libc::SIGSTOP),
            "refused legacy stop queued a real SIGSTOP"
        );
    }
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_nonleader_preserves_stops_exits_and_retirement() {
    if run_legacy_test_outer("forced_legacy_nonleader_preserves_stops_exits_and_retirement") {
        return;
    }
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(false);
    let root_terminal = stopped.terminal_cleanup();
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing first pthread clone");
    };
    let first_tid = child.pid();
    let mut first_cleanup = legacy_child_cleanup(&child);
    let first_terminal = child.terminal_cleanup();
    let identity = first_terminal.event.identity().unwrap().clone();
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    let (child, event) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        child.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGTRAP));
    let mut stale_stop = Stopped::new_unchecked_on_ptracer_thread(first_tid).unwrap();
    let exit = first_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    let running = child.resume(None).unwrap();
    // ExitFuture observes the worker notification without polling wait_owned.
    let exiting = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    first_cleanup.mark_claimed_exit();
    drop(running);
    assert_eq!(exiting.pid(), first_tid);
    assert_eq!(exiting.getevent().unwrap(), 17 << 8);

    // The final status must remain unreaped while an earlier numeric request
    // owns the TID gate. A stop-only consuming wait must not release it.
    let event = Arc::clone(first_terminal.event.event());
    let held = event.hold_tid().unwrap();
    let final_running = exiting.resume_retaining(None).unwrap();
    let terminal_flags = WaitPidFlag::from_bits_retain(
        WaitPidFlag::WEXITED.bits()
            | WaitPidFlag::WNOWAIT.bits()
            | WaitPidFlag::WNOHANG.bits()
            | libc::__WALL,
    );
    let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
    let observed = loop {
        match identity.pidfd.wait_status(terminal_flags).unwrap() {
            Some(status) => break status,
            None => assert!(Instant::now() < deadline, "thread never reached final exit"),
        }
        thread::sleep(Duration::from_millis(1));
    };
    assert!(libc::WIFEXITED(observed));
    assert_eq!(libc::WEXITSTATUS(observed), 17);
    assert!(identity.pidfd_is_live().unwrap());
    assert!(!first_terminal.wait(Duration::ZERO));
    drop(held);
    let exited = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        final_running.wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_exited();
    assert_eq!(exited, (first_tid, crate::ExitStatus::Exited(17)));
    assert!(first_terminal.wait(TRACEE_WAIT_TIMEOUT));
    first_cleanup.disarm();
    assert!(!identity.pidfd_is_live().unwrap());
    // The leader is still held at its clone stop. Thread liveness must not
    // silently become the leader's liveness when the thread exits.
    assert!(
        root_terminal
            .event
            .identity()
            .unwrap()
            .pidfd_is_live()
            .unwrap()
    );

    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        parent.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing second pthread clone");
    };
    let second_tid = child.pid();
    let mut second_cleanup = legacy_child_cleanup(&child);
    let second_terminal = child.terminal_cleanup();
    // Project the stale routing number onto a real live thread while keeping
    // the old proc descriptor. This is a reuse boundary, not a claim that the
    // kernel allocated the same TID in this unprivileged test.
    let mut projected = identity.pidfd.try_clone().unwrap();
    let ThreadHandle::Procfs { pid, .. } = &mut projected else {
        unreachable!()
    };
    *pid = second_tid;
    assert_eq!(projected.wait_status(terminal_flags), Err(Errno::ECHILD));
    assert_eq!(first_terminal.request_sigkill(), Err(Errno::ESRCH));
    stale_stop.0 = second_tid;
    assert!(stale_stop.1.event().hold_tid().is_none());
    let Err(Error::Died(zombie)) = stale_stop.getregs() else {
        panic!("stale stopped token reached the replacement thread");
    };
    assert_eq!(zombie.0.1.event(), &first_terminal.event);
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(
        event,
        crate::Event::Signal(Signal::SIGSTOP),
        "stale wait stole the live stop"
    );
    child
        .getregs()
        .expect("replacement thread remains controllable");
    let second_exit = second_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    let second_running = child.resume(None).unwrap();
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, second_exit)
        .await
        .unwrap()
        .unwrap();
    second_cleanup.mark_claimed_exit();
    drop(second_running);
    assert_eq!(child.getevent().unwrap(), 23 << 8);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (second_tid, crate::ExitStatus::Exited(23)),
    );
    assert!(second_terminal.wait(TRACEE_WAIT_TIMEOUT));
    second_cleanup.disarm();
    // Keep the leader at its clone stop until this thread is reaped. Otherwise
    // the leader's exit_group(42) may replace the pending thread exit status
    // after pthread_join sees its clear_child_tid, before the worker waits.
    let parent_running = parent.resume(None).unwrap();
    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(parent.getevent().unwrap(), 42 << 8);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (root.into(), crate::ExitStatus::Exited(42)),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_nonleader_preserves_stop_notifications_and_exit() {
    if run_legacy_test_outer("forced_legacy_nonleader_preserves_stop_notifications_and_exit") {
        return;
    }
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(true);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing paused pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    assert_eq!(terminal.has_thread_pidfd(), Ok(false));
    assert_no_pending_sigstop(&child);
    for missing in [Options::PTRACE_O_TRACEEXEC, Options::PTRACE_O_TRACEEXIT] {
        child
            .setoptions(legacy_thread_options() & !missing)
            .unwrap();
        assert_eq!(terminal.request_sigstop(), Err(Errno::EOPNOTSUPP));
        assert_no_pending_sigstop(&child);
        child
            .getregs()
            .expect("option refusal must preserve the actual stopped child");
    }
    child.setoptions(legacy_thread_options()).unwrap();
    let foreign = TerminalCleanup {
        pid: terminal.pid,
        event: terminal.event.clone(),
    };
    assert_eq!(
        thread::spawn(move || foreign.request_sigstop())
            .join()
            .unwrap(),
        Err(Errno::EOPNOTSUPP),
        "a transferable cleanup handle must not signal from another host thread"
    );
    assert_no_pending_sigstop(&child);
    let child_running = child.resume(None).unwrap();
    let parent_running = parent.resume(None).unwrap();
    assert!(matches!(
        root_terminal.event.identity().unwrap().pidfd,
        ThreadHandle::LegacyLeader { .. }
    ));
    assert_eq!(root_terminal.request_sigstop(), Err(Errno::EOPNOTSUPP));
    request_fixture_thread_sigstop(root, root.into());
    let (parent, event) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        parent_running.wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped();
    assert_eq!(parent.pid(), root.into());
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    let parent_running = parent.resume(None).unwrap();
    assert_eq!(terminal.request_sigstop(), Err(Errno::EOPNOTSUPP));
    request_fixture_thread_sigstop(root, child_tid);
    let (child, event) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        child_running.wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped();
    assert_eq!(child.pid(), child_tid);
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));

    let mut exit = Box::pin(child_cleanup.exit_event_on_ptracer_thread(&child).unwrap());
    let wakes = Arc::new(WakeCounter::default());
    let waker = Waker::from(Arc::clone(&wakes));
    let mut context = Context::from_waker(&waker);
    assert!(exit.as_mut().poll(&mut context).is_pending());
    assert_eq!(wakes.0.load(Ordering::SeqCst), 0);
    terminal.request_sigkill().unwrap();
    drop(child);
    tokio::time::timeout(TRACEE_WAIT_TIMEOUT, async {
        while wakes.0.load(Ordering::SeqCst) == 0 {
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
    })
    .await
    .expect("legacy worker lost the exit notification");
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit.as_mut())
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert!(
        wakes.0.load(Ordering::SeqCst) >= 1,
        "legacy worker lost the exit notification"
    );
    assert!(
        !terminal.wait(Duration::ZERO),
        "exit stop is not a terminal acknowledgment"
    );
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    assert_eq!(terminal.request_sigstop(), Err(Errno::ESRCH));
    child_cleanup.disarm();

    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_nonleader_exec_preserves_epochs_and_exact_wait_owners() {
    if run_legacy_test_outer("forced_legacy_nonleader_exec_preserves_epochs_and_exact_wait_owners")
    {
        return;
    }
    // Reuse every assertion of the native thread-pidfd regression. Only the
    // open result differs: this case must bind the non-leader's proc descriptor.
    actual_nonleader_exec_owned_case(true).await;
}

#[cfg(not(sanitized))]
async fn kill_legacy_family(
    parent: Stopped,
    child: Stopped,
    mut root_cleanup: TraceeCleanupGuard,
    mut child_cleanup: TraceeCleanupGuard,
) {
    let root_terminal = parent.terminal_cleanup();
    let child_terminal = child.terminal_cleanup();
    let root_pid = parent.pid();
    let child_pid = child.pid();
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&parent).unwrap();
    let child_exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    root_terminal.request_sigkill().unwrap();
    drop(parent);
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child_exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_pid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        )
    );
    assert!(child_terminal.wait(TRACEE_WAIT_TIMEOUT));
    assert!(child_terminal.event.hold_tid().is_none());
    child_cleanup.disarm();
    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root_pid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        )
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_child_adoption_preserves_stops_after_option_change() {
    if run_legacy_test_outer("forced_legacy_child_adoption_preserves_stops_after_option_change") {
        return;
    }
    for supported_at_clone in [false, true] {
        let initial = if supported_at_clone {
            legacy_thread_options()
        } else {
            legacy_thread_options() & !Options::PTRACE_O_TRACEEXEC
        };
        let (root, stopped, root_cleanup, _force) =
            spawn_legacy_thread_tracee_with_options(true, initial);
        let root_terminal = stopped.terminal_cleanup();
        let running = stopped.resume(None).unwrap();
        let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
        loop {
            let queued_clone = root_terminal
                .event
                .event()
                .status
                .lock()
                .pending
                .iter()
                .any(|status| (*status >> 16) == libc::PTRACE_EVENT_CLONE);
            if queued_clone {
                break;
            }
            assert!(Instant::now() < deadline, "clone stop was not published");
            tokio::time::sleep(Duration::from_millis(1)).await;
        }
        // This is a real held clone stop, not a fabricated kernel state.
        // Changing parent options here cannot change the already-born child.
        let at_clone = Stopped::new_unchecked_on_ptracer_thread(root.into()).unwrap();
        let changed = if supported_at_clone {
            legacy_thread_options() & !Options::PTRACE_O_TRACEEXEC
        } else {
            legacy_thread_options()
        };
        at_clone.setoptions(changed).unwrap();
        drop(at_clone);
        let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped()
        else {
            panic!("missing real clone after parent option change");
        };
        // The explicit mode captures its first original anchor before
        // registration through a distinct Event. Adoption must preserve
        // the same original child generation and wait owner.
        let fresh = TerminalCleanup {
            pid: child.pid(),
            event: EventHandle::with_identity(Arc::new(
                WorkerIdentity::capture_for_policy(child.pid(), WaitPolicy::PtracerThread).unwrap(),
            )),
        };
        fresh.ensure_registered().unwrap();
        assert!(Arc::ptr_eq(child.1.event().event(), fresh.event.event()));
        let child_cleanup = legacy_child_cleanup(&child);
        let terminal = child.terminal_cleanup();
        let (child, event) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped();
        assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
        assert_no_pending_sigstop(&child);
        assert_eq!(terminal.request_sigstop(), Err(Errno::EOPNOTSUPP));
        assert_no_pending_sigstop(&child);
        child.getregs().unwrap();
        request_fixture_thread_sigstop(root, child.pid());
        let (child, event) = tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread(),
        )
        .await
        .unwrap()
        .unwrap()
        .assume_stopped();
        assert_eq!(child.pid(), terminal.pid);
        assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
        kill_legacy_family(parent, child, root_cleanup, child_cleanup).await;
    }
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_seize_and_attach_preserve_child_stops() {
    if run_legacy_test_outer("forced_legacy_seize_and_attach_preserve_child_stops") {
        return;
    }
    for method in [LegacyAttachMethod::Seize, LegacyAttachMethod::Attach] {
        let (root, stopped, root_cleanup, _force) =
            spawn_legacy_thread_tracee_with_method(true, legacy_thread_options(), method);
        let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
        )
        .await
        .unwrap()
        .unwrap()
        .assume_stopped() else {
            panic!("missing clone in {method:?} mode");
        };
        let child_cleanup = legacy_child_cleanup(&child);
        let terminal = child.terminal_cleanup();
        let (child, event) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped();
        assert_eq!(
            event,
            if method == LegacyAttachMethod::Seize {
                crate::Event::Stop
            } else {
                crate::Event::Signal(Signal::SIGSTOP)
            }
        );
        assert_eq!(terminal.request_sigstop(), Err(Errno::EOPNOTSUPP));
        assert_no_pending_sigstop(&child);
        request_fixture_thread_sigstop(root, child.pid());
        let (child, event) = tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread(),
        )
        .await
        .unwrap()
        .unwrap()
        .assume_stopped();
        assert_eq!(child.pid(), terminal.pid);
        assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
        kill_legacy_family(parent, child, root_cleanup, child_cleanup).await;
    }
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_detach_preserves_cancellation_and_failed_owner() {
    if run_legacy_test_outer("forced_legacy_detach_preserves_cancellation_and_failed_owner") {
        return;
    }
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(true);
    let terminal = stopped.terminal_cleanup();
    let foreign = Stopped::new_unchecked_on_ptracer_thread(root.into()).unwrap();
    assert!(matches!(
        thread::spawn(move || foreign.detach(None)).join().unwrap(),
        Err(Error::Errno(Errno::EPERM))
    ));
    assert!(terminal.event.hold_tid().is_some());
    stopped.getregs().unwrap();

    let event = Arc::clone(terminal.event.event());
    let identity = Arc::clone(terminal.event.identity().unwrap());
    let (captured, ready) = mpsc::sync_channel(1);
    let (begin, started) = mpsc::sync_channel(1);
    let prior_request = thread::spawn(move || {
        let held = event.hold_tid().unwrap();
        assert!(identity.pidfd_is_live().unwrap());
        captured.send(()).unwrap();
        started.recv().unwrap();
        assert_eq!(
            worker_proc_snapshot(root.into())
                .unwrap()
                .tracer_pid
                .as_raw(),
            0,
            "successful detach preserves the native read-gate facade"
        );
        assert!(identity.pidfd_is_live().unwrap());
        drop(held);
    });
    ready.recv_timeout(TRACEE_WAIT_TIMEOUT).unwrap();
    let running = stopped.detach(None).unwrap();
    begin.send(()).unwrap();
    prior_request.join().unwrap();
    assert_eq!(
        worker_proc_snapshot(root.into())
            .unwrap()
            .tracer_pid
            .as_raw(),
        0
    );
    assert!(
        terminal.event.hold_tid().is_some(),
        "detach must preserve ordinary detached-child waits and reattachment"
    );
    assert_eq!(terminal.request_sigstop(), Err(Errno::EOPNOTSUPP));
    assert!(terminal.event.identity().unwrap().pidfd_is_live().unwrap());
    terminal.request_sigkill().unwrap();
    assert_eq!(
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        )
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

#[cfg(not(sanitized))]
fn legacy_pipe() -> [OwnedFd; 2] {
    let mut descriptors = [-1; 2];
    assert_eq!(
        unsafe { libc::pipe2(descriptors.as_mut_ptr(), libc::O_CLOEXEC) },
        0
    );
    descriptors.map(|descriptor| unsafe { OwnedFd::from_raw_fd(descriptor) })
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn detach_and_reattach_preserves_live_wait_authority() {
    if run_legacy_test_outer("detach_and_reattach_preserves_live_wait_authority") {
        return;
    }
    for force_legacy in [false, true] {
        for method in [LegacyAttachMethod::Attach, LegacyAttachMethod::Seize] {
            let [start, release] = legacy_pipe();
            let root = match unsafe { fork() }.unwrap() {
                ForkResult::Parent { child } => child,
                ForkResult::Child => {
                    unsafe { libc::close(release.as_raw_fd()) };
                    crate::traceme_and_stop().unwrap();
                    let mut byte = 0u8;
                    if unsafe { libc::read(start.as_raw_fd(), (&mut byte as *mut u8).cast(), 1) }
                        != 1
                    {
                        unsafe { libc::_exit(125) };
                    }
                    unsafe { libc::_exit(42) };
                }
            };
            drop(start);
            let _force = force_legacy.then(|| LegacyThreadGroup::new(root));
            let mut cleanup = TraceeCleanupGuard::new(root).unwrap();
            let stopped = stopped_tracee_bounded(root, |pid| {
                Stopped::new_unchecked_on_ptracer_thread(pid).unwrap()
            })
            .unwrap();
            stopped.setoptions(legacy_thread_options()).unwrap();
            cleanup.bind_notifier(&stopped).unwrap();
            let old_terminal = stopped.terminal_cleanup();
            let old_event = Arc::clone(old_terminal.event.event());
            let mut exit = Box::pin(cleanup.exit_event_on_ptracer_thread(&stopped).unwrap());
            assert!(
                exit.as_mut()
                    .poll(&mut Context::from_waker(&futures::task::noop_waker()))
                    .is_pending()
            );
            let detached = stopped.detach(None).unwrap();
            assert!(
                old_terminal
                    .event
                    .identity()
                    .unwrap()
                    .pidfd_is_live()
                    .unwrap()
            );
            assert!(old_terminal.event.hold_tid().is_some());
            assert!(!old_terminal.wait(Duration::ZERO));
            assert_eq!(
                worker_proc_snapshot(root.into())
                    .unwrap()
                    .tracer_pid
                    .as_raw(),
                0
            );

            let running = match method {
                LegacyAttachMethod::Attach => {
                    Running::attach_on_ptracer_thread(root.into()).unwrap()
                }
                LegacyAttachMethod::Seize => {
                    let running =
                        Running::seize_on_ptracer_thread(root.into(), legacy_thread_options())
                            .unwrap();
                    running.interrupt().unwrap();
                    running
                }
                LegacyAttachMethod::Traceme => unreachable!(),
            };
            let terminal = running.terminal_cleanup();
            assert!(
                terminal.same_generation(&old_terminal),
                "a detached direct child still has the same live wait owner"
            );
            assert!(Arc::ptr_eq(&old_event, terminal.event.event()));
            assert_eq!(
                SPAWN_WORKER_COUNTS.lock().get(&root.into()).copied(),
                Some(1)
            );
            assert!(
                Running::new_on_ptracer_thread(root.into())
                    .unwrap()
                    .terminal_cleanup()
                    .same_generation(&terminal)
            );
            let (stopped, event) =
                tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
                    .await
                    .unwrap()
                    .unwrap()
                    .assume_stopped();
            assert_eq!(stopped.pid(), root.into());
            assert_eq!(
                event,
                if method == LegacyAttachMethod::Attach {
                    crate::Event::Signal(Signal::SIGSTOP)
                } else {
                    crate::Event::Stop
                }
            );
            stopped.setoptions(legacy_thread_options()).unwrap();
            assert_eq!(
                unsafe { libc::write(release.as_raw_fd(), c"x".as_ptr().cast(), 1) },
                1
            );
            let running = stopped.resume(None).unwrap();
            let stopped = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit.as_mut())
                .await
                .unwrap()
                .unwrap();
            cleanup.mark_claimed_exit();
            assert!(!terminal.wait(Duration::ZERO));
            assert_eq!(
                tokio::time::timeout(
                    TRACEE_WAIT_TIMEOUT,
                    stopped.resume(None).unwrap().wait_owned_on_ptracer_thread()
                )
                .await
                .unwrap()
                .unwrap()
                .assume_exited(),
                (root.into(), crate::ExitStatus::Exited(42))
            );
            assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
            assert!(old_terminal.wait(Duration::ZERO));
            assert!(old_terminal.event.hold_tid().is_none());
            cleanup.disarm();
            drop((detached, running));
        }
    }
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn nonchild_detach_reattach_preserves_one_wait_owner() {
    if run_legacy_test_outer("nonchild_detach_reattach_preserves_one_wait_owner") {
        return;
    }
    for force_legacy in [false, true] {
        for method in [LegacyAttachMethod::Attach, LegacyAttachMethod::Seize] {
            let [receipt, send_receipt] = legacy_pipe();
            let [start, release] = legacy_pipe();
            let real_parent = match unsafe { fork() }.unwrap() {
                ForkResult::Parent { child } => child,
                ForkResult::Child => {
                    unsafe {
                        libc::close(receipt.as_raw_fd());
                        libc::close(release.as_raw_fd());
                    }
                    let target = unsafe { libc::fork() };
                    if target < 0 {
                        unsafe { libc::_exit(125) };
                    }
                    if target == 0 {
                        unsafe { libc::close(send_receipt.as_raw_fd()) };
                        let mut byte = 0u8;
                        if unsafe {
                            libc::read(start.as_raw_fd(), (&mut byte as *mut u8).cast(), 1)
                        } != 1
                        {
                            unsafe { libc::_exit(126) };
                        }
                        unsafe { libc::_exit(42) };
                    }
                    unsafe { libc::close(start.as_raw_fd()) };
                    if unsafe {
                        libc::write(
                            send_receipt.as_raw_fd(),
                            (&target as *const i32).cast(),
                            mem::size_of::<i32>(),
                        )
                    } != mem::size_of::<i32>() as isize
                    {
                        unsafe { libc::_exit(127) };
                    }
                    let mut status = 0i32;
                    if unsafe { libc::waitpid(target, &mut status, 0) } != target {
                        unsafe { libc::_exit(128) };
                    }
                    if unsafe {
                        libc::write(
                            send_receipt.as_raw_fd(),
                            (&status as *const i32).cast(),
                            mem::size_of::<i32>(),
                        )
                    } != mem::size_of::<i32>() as isize
                    {
                        unsafe { libc::_exit(129) };
                    }
                    unsafe { libc::_exit(0) };
                }
            };
            drop((send_receipt, start));
            let mut parent_cleanup = TraceeCleanupGuard::new(real_parent).unwrap();
            let mut receipts = fs::File::from(receipt);
            let mut bytes = [0u8; mem::size_of::<i32>()];
            receipts.read_exact(&mut bytes).unwrap();
            let root = Pid::from_raw(i32::from_ne_bytes(bytes));
            let _force = force_legacy.then(|| LegacyThreadGroup::new(root));
            let mut old_cleanup = TraceeCleanupGuard::new(root).unwrap();
            let running =
                Running::seize_on_ptracer_thread(root.into(), legacy_thread_options()).unwrap();
            old_cleanup.bind_running_notifier(&running).unwrap();
            let old_terminal = running.terminal_cleanup();
            running.interrupt().unwrap();
            let (stopped, event) =
                tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
                    .await
                    .unwrap()
                    .unwrap()
                    .assume_stopped();
            assert_eq!(event, crate::Event::Stop);
            // The original descriptor waiter may remain blocked after a
            // nonchild detach. It is still the one wait owner, and reattach
            // must either retain it or join an already committed retirement.
            tokio::time::sleep(Duration::from_millis(20)).await;
            let detached = stopped.detach(None).unwrap();
            assert!(
                old_terminal
                    .event
                    .identity()
                    .unwrap()
                    .pidfd_is_live()
                    .unwrap()
            );

            let running = match method {
                LegacyAttachMethod::Attach => {
                    Running::attach_on_ptracer_thread(root.into()).unwrap()
                }
                LegacyAttachMethod::Seize => {
                    let running =
                        Running::seize_on_ptracer_thread(root.into(), legacy_thread_options())
                            .unwrap();
                    running.interrupt().unwrap();
                    running
                }
                LegacyAttachMethod::Traceme => unreachable!(),
            };
            let same_owner = Arc::ptr_eq(running.1.event().event(), old_terminal.event.event());
            let mut cleanup = if same_owner {
                old_cleanup
            } else {
                assert!(old_terminal.wait(Duration::ZERO));
                assert!(old_terminal.event.hold_tid().is_none());
                assert_eq!(
                    old_terminal.event.event().status.lock().terminal,
                    ECHILD_STATUS
                );
                old_cleanup.disarm();
                TraceeCleanupGuard::new(root).unwrap()
            };
            cleanup.bind_running_notifier(&running).unwrap();
            let terminal = running.terminal_cleanup();
            assert_eq!(terminal.same_generation(&old_terminal), same_owner);
            assert_eq!(
                SPAWN_WORKER_COUNTS.lock().get(&root.into()).copied(),
                Some(if same_owner { 1 } else { 2 })
            );
            assert!(
                Running::new_on_ptracer_thread(root.into())
                    .unwrap()
                    .terminal_cleanup()
                    .same_generation(&terminal)
            );
            let (stopped, event) =
                tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
                    .await
                    .unwrap()
                    .unwrap()
                    .assume_stopped();
            assert_eq!(stopped.pid(), root.into());
            assert_eq!(
                event,
                if method == LegacyAttachMethod::Attach {
                    crate::Event::Signal(Signal::SIGSTOP)
                } else {
                    crate::Event::Stop
                }
            );
            stopped.setoptions(legacy_thread_options()).unwrap();
            stopped.getregs().unwrap();
            let mut exit = Box::pin(cleanup.exit_event_on_ptracer_thread(&stopped).unwrap());
            assert_eq!(
                unsafe { libc::write(release.as_raw_fd(), c"x".as_ptr().cast(), 1) },
                1
            );
            let running = stopped.resume(None).unwrap();
            let stopped = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit.as_mut())
                .await
                .unwrap()
                .unwrap();
            cleanup.mark_claimed_exit();
            assert!(!terminal.wait(Duration::ZERO));
            assert_eq!(
                tokio::time::timeout(
                    TRACEE_WAIT_TIMEOUT,
                    stopped.resume(None).unwrap().wait_owned_on_ptracer_thread()
                )
                .await
                .unwrap()
                .unwrap()
                .assume_exited(),
                (root.into(), crate::ExitStatus::Exited(42))
            );
            assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
            cleanup.disarm();
            receipts.read_exact(&mut bytes).unwrap();
            let status = i32::from_ne_bytes(bytes);
            assert!(libc::WIFEXITED(status));
            assert_eq!(libc::WEXITSTATUS(status), 42);
            assert_eq!(
                waitpid_status_bounded(real_parent, 0, TRACEE_WAIT_TIMEOUT).unwrap(),
                0
            );
            parent_cleanup.disarm();
            assert!(old_terminal.wait(Duration::ZERO));
            if !same_owner {
                assert_eq!(
                    old_terminal.event.event().status.lock().terminal,
                    ECHILD_STATUS
                );
            }
            assert_eq!(terminal.request_sigkill(), Err(Errno::ESRCH));
            drop((detached, running));
        }
    }
}

#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_nonleader_reattach_joins_retired_owner() {
    if run_legacy_test_outer("forced_legacy_nonleader_reattach_joins_retired_owner") {
        return;
    }
    for method in [LegacyAttachMethod::Attach, LegacyAttachMethod::Seize] {
        let [receipt, send_receipt] = legacy_pipe();
        let [thread_start, thread_release] = legacy_pipe();
        let [root_start, root_release] = legacy_pipe();
        let root = match unsafe { fork() }.unwrap() {
            ForkResult::Parent { child } => child,
            ForkResult::Child => {
                unsafe {
                    libc::close(receipt.as_raw_fd());
                    libc::close(thread_release.as_raw_fd());
                    libc::close(root_release.as_raw_fd());
                }
                extern "C" fn guest_thread(argument: *mut libc::c_void) -> *mut libc::c_void {
                    let descriptors = unsafe { &*argument.cast::<[i32; 2]>() };
                    let tid = unsafe { libc::syscall(libc::SYS_gettid) } as i32;
                    if unsafe {
                        libc::write(
                            descriptors[0],
                            (&tid as *const i32).cast(),
                            mem::size_of::<i32>(),
                        )
                    } != mem::size_of::<i32>() as isize
                    {
                        unsafe { libc::_exit(125) };
                    }
                    let mut byte = 0u8;
                    if unsafe { libc::read(descriptors[1], (&mut byte as *mut u8).cast(), 1) } != 1
                    {
                        unsafe { libc::_exit(126) };
                    }
                    unsafe { libc::syscall(libc::SYS_exit, 23) };
                    std::ptr::null_mut()
                }
                let mut descriptors = [send_receipt.as_raw_fd(), thread_start.as_raw_fd()];
                let mut thread = mem::MaybeUninit::<libc::pthread_t>::uninit();
                if unsafe {
                    libc::pthread_create(
                        thread.as_mut_ptr(),
                        std::ptr::null(),
                        guest_thread,
                        descriptors.as_mut_ptr().cast(),
                    )
                } != 0
                {
                    unsafe { libc::_exit(127) };
                }
                if unsafe { libc::pthread_join(thread.assume_init(), std::ptr::null_mut()) } != 0 {
                    unsafe { libc::_exit(128) };
                }
                // pthread_join's clear-child-TID can precede the ptracer's
                // final wait. Keep the root alive until that actual reap.
                let mut byte = 0u8;
                if unsafe { libc::read(root_start.as_raw_fd(), (&mut byte as *mut u8).cast(), 1) }
                    != 1
                {
                    unsafe { libc::_exit(129) };
                }
                unsafe { libc::_exit(42) };
            }
        };
        drop((send_receipt, thread_start, root_start));
        let mut root_cleanup = TraceeCleanupGuard::new(root).unwrap();
        let mut receipts = fs::File::from(receipt);
        let mut bytes = [0u8; mem::size_of::<i32>()];
        receipts.read_exact(&mut bytes).unwrap();
        let tid = Pid::from_raw(i32::from_ne_bytes(bytes));
        assert_ne!(tid, root);
        let _force = LegacyThreadGroup::new(root);
        let running =
            Running::seize_on_ptracer_thread(tid.into(), legacy_thread_options()).unwrap();
        let mut old_cleanup = legacy_child_cleanup(&running);
        let old_terminal = running.terminal_cleanup();
        assert!(matches!(
            old_terminal.event.identity().unwrap().pidfd,
            ThreadHandle::Procfs { .. }
        ));
        running.interrupt().unwrap();
        let (stopped, event) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped();
        assert_eq!(event, crate::Event::Stop);
        let detached = stopped.detach(None).unwrap();
        assert!(old_terminal.wait(TRACEE_WAIT_TIMEOUT));
        assert!(old_terminal.event.hold_tid().is_none());
        assert!(
            old_terminal
                .event
                .identity()
                .unwrap()
                .pidfd_is_live()
                .unwrap()
        );
        assert_eq!(
            old_terminal.event.event().status.lock().terminal,
            ECHILD_STATUS
        );
        assert_eq!(
            SPAWN_WORKER_COUNTS.lock().get(&tid.into()).copied(),
            Some(1)
        );
        old_cleanup.disarm();

        let running = match method {
            LegacyAttachMethod::Attach => Running::attach_on_ptracer_thread(tid.into()).unwrap(),
            LegacyAttachMethod::Seize => {
                let running =
                    Running::seize_on_ptracer_thread(tid.into(), legacy_thread_options()).unwrap();
                running.interrupt().unwrap();
                running
            }
            LegacyAttachMethod::Traceme => unreachable!(),
        };
        let mut cleanup = legacy_child_cleanup(&running);
        let terminal = running.terminal_cleanup();
        assert!(!terminal.same_generation(&old_terminal));
        assert!(old_terminal.wait(Duration::ZERO));
        assert!(old_terminal.event.hold_tid().is_none());
        assert_eq!(
            old_terminal.event.event().status.lock().terminal,
            ECHILD_STATUS
        );
        assert_eq!(
            SPAWN_WORKER_COUNTS.lock().get(&tid.into()).copied(),
            Some(2)
        );
        assert!(
            Running::new_on_ptracer_thread(tid.into())
                .unwrap()
                .terminal_cleanup()
                .same_generation(&terminal)
        );
        let (stopped, event) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped();
        assert_eq!(stopped.pid(), tid.into());
        assert_eq!(
            event,
            if method == LegacyAttachMethod::Attach {
                crate::Event::Signal(Signal::SIGSTOP)
            } else {
                crate::Event::Stop
            }
        );
        stopped.getregs().unwrap();
        stopped.setoptions(legacy_thread_options()).unwrap();
        let mut exit = Box::pin(cleanup.exit_event_on_ptracer_thread(&stopped).unwrap());
        assert_eq!(
            unsafe { libc::write(thread_release.as_raw_fd(), c"x".as_ptr().cast(), 1) },
            1
        );
        let running = stopped.resume(None).unwrap();
        let stopped = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit.as_mut())
            .await
            .unwrap()
            .unwrap();
        cleanup.mark_claimed_exit();
        assert!(!terminal.wait(Duration::ZERO));
        assert_eq!(
            tokio::time::timeout(
                TRACEE_WAIT_TIMEOUT,
                stopped.resume(None).unwrap().wait_owned_on_ptracer_thread()
            )
            .await
            .unwrap()
            .unwrap()
            .assume_exited(),
            (tid.into(), crate::ExitStatus::Exited(23))
        );
        assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
        assert_eq!(terminal.request_sigkill(), Err(Errno::ESRCH));
        assert_eq!(
            old_terminal.event.event().status.lock().terminal,
            ECHILD_STATUS
        );
        cleanup.disarm();
        assert_eq!(
            unsafe { libc::write(root_release.as_raw_fd(), c"x".as_ptr().cast(), 1) },
            1
        );
        let status = waitpid_status_bounded(root, 0, TRACEE_WAIT_TIMEOUT).unwrap();
        assert!(libc::WIFEXITED(status));
        assert_eq!(libc::WEXITSTATUS(status), 42);
        root_cleanup.disarm();
        drop((detached, running));
    }
}

// A legacy non-leader's stops reach its waiter without waiting out the
// worker's poll period. Fast single steps: the ptracer's consume wakes the
// worker to publish. Syscall stops resumed after the ptracer was busy for
// longer than the period, each entry about 300 µs after the resumption:
// the resumption wakes the worker to observe. With 1 ms polling, every one
// of these stops took up to two whole periods.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_nonleader_steps_are_not_paced_by_the_poll_period() {
    if run_legacy_test_outer("forced_legacy_nonleader_steps_are_not_paced_by_the_poll_period") {
        return;
    }
    const FAST_STEPS: usize = 200;
    const BUSY_SYSCALLS: usize = 20;
    let (_root, stopped, _root_cleanup, _force) = spawn_legacy_threads(
        &[1],
        legacy_thread_options() | Options::PTRACE_O_TRACESYSGOOD,
        LegacyAttachMethod::Traceme,
    );
    let (_parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing spinning pthread clone");
    };
    let _child_cleanup = legacy_child_cleanup(&child);
    let event = Arc::clone(child.terminal_cleanup().event.event());
    let (mut child, sigstop) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(sigstop, crate::Event::Signal(Signal::SIGSTOP));
    let late_turns = || event.legacy_late_turns.load(Ordering::Relaxed);
    let before = late_turns();
    for _ in 0..FAST_STEPS {
        let (stepped, trap) = tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.step(None).unwrap().wait_owned_on_ptracer_thread(),
        )
        .await
        .unwrap()
        .unwrap()
        .assume_stopped();
        assert_eq!(trap, crate::Event::Signal(Signal::SIGTRAP));
        child = stepped;
    }
    let fast_late = late_turns() - before;
    assert!(
        fast_late < FAST_STEPS / 4,
        "{fast_late} of {FAST_STEPS} fast steps waited out the poll period"
    );
    let before = late_turns();
    // Entry and exit stops: every entry comes about 300 µs after its resumption.
    for _ in 0..2 * BUSY_SYSCALLS {
        // Long enough for the worker to back off to the whole period.
        thread::sleep(LEGACY_POLL_PERIOD * 3);
        let (next, stop) = tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.syscall(None).unwrap().wait_owned_on_ptracer_thread(),
        )
        .await
        .unwrap()
        .unwrap()
        .assume_stopped();
        assert_eq!(stop, crate::Event::Syscall);
        child = next;
    }
    let busy_late = late_turns() - before;
    assert!(
        busy_late < BUSY_SYSCALLS,
        "{busy_late} of {} syscall stops after a busy ptracer waited out the poll period",
        2 * BUSY_SYSCALLS
    );
}

/// What one legacy consuming attempt did on this thread.
#[cfg(not(sanitized))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct LegacyAttempt {
    /// Retained status reads.
    reads: usize,
    /// Stop proofs that authenticated the owner without a read.
    accepted: usize,
    /// Stop proofs tried for a bound owner that did not complete.
    refused: usize,
    /// Statuses this attempt consumed and queued for the worker.
    consumed: usize,
}

#[cfg(not(sanitized))]
impl LegacyAttempt {
    fn now() -> Self {
        Self {
            reads: RETAINED_STATUS_READS.with(Cell::get),
            accepted: LEGACY_STOP_PROOFS_ACCEPTED.with(Cell::get),
            refused: LEGACY_STOP_PROOFS_REFUSED.with(Cell::get),
            consumed: LEGACY_ASSIST_CONSUMPTIONS.with(Cell::get),
        }
    }

    fn since(before: Self) -> Self {
        let after = Self::now();
        Self {
            reads: after.reads - before.reads,
            accepted: after.accepted - before.accepted,
            refused: after.refused - before.refused,
            consumed: after.consumed - before.consumed,
        }
    }
}

/// Runs legacy consuming attempts on this thread until `done` accepts the
/// latest one, asserting that no attempt reads status or consumes more than
/// once, and returns what each attempt did, in order.
#[cfg(not(sanitized))]
fn legacy_attempts(
    terminal: &TerminalCleanup,
    mut done: impl FnMut(&LegacyAttempt) -> bool,
) -> Vec<LegacyAttempt> {
    let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
    let mut attempts = Vec::new();
    loop {
        let before = LegacyAttempt::now();
        terminal.event.try_assist_legacy_wait().unwrap();
        let attempt = LegacyAttempt::since(before);
        assert!(
            attempt.reads <= 1,
            "one legacy consuming attempt read status {} times",
            attempt.reads
        );
        assert!(
            attempt.consumed <= 1,
            "one legacy consuming attempt consumed {} statuses",
            attempt.consumed
        );
        attempts.push(attempt);
        if done(&attempt) {
            return attempts;
        }
        assert!(
            Instant::now() < deadline,
            "legacy consuming attempts never finished"
        );
        thread::sleep(Duration::from_millis(1));
    }
}

/// Runs legacy consuming attempts on this thread until one consumes a
/// status, and returns them all: exactly the last one consumed. The loop
/// ends on this thread's own consumption, never on the worker's publication:
/// from the worker's pop of `consumed` until its `update` returns, neither
/// `consumed` nor `pending` shows the status.
#[cfg(not(sanitized))]
fn legacy_attempts_until_consumed(terminal: &TerminalCleanup) -> Vec<LegacyAttempt> {
    legacy_attempts(terminal, |attempt| attempt.consumed == 1)
}

/// A ptracer attempt on the legacy path that finds nothing reads the target's
/// retained status once: the read follows the numeric observation, so it is
/// both that observation's postcheck and the owner's authentication. An
/// attempt that consumes a stop proves the same two things without reading
/// status: GETEVENTMSG on the number, then signal 0 through the retained
/// descriptor. The observation worker's turns that find nothing read it at
/// most once per LEGACY_CHECK_PERIOD.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_waits_read_target_status_once_per_attempt_or_period() {
    if run_legacy_test_outer("forced_legacy_waits_read_target_status_once_per_attempt_or_period") {
        return;
    }
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(true);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing paused pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    let parent_running = parent.resume(None).unwrap();
    let child_running = child.resume(None).unwrap();

    // The thread sleeps in pause(), so every worker turn finds nothing.
    // Each turn used to read status; now one read covers LEGACY_CHECK_PERIOD.
    let worker = terminal.event.event().clone();
    let counts = || {
        (
            worker.legacy_worker_turns.load(Ordering::Relaxed),
            worker.legacy_worker_status_reads.load(Ordering::Relaxed),
        )
    };
    let (turns_before, reads_before) = counts();
    let start = Instant::now();
    let deadline = start + TRACEE_WAIT_TIMEOUT;
    loop {
        thread::sleep(Duration::from_millis(20));
        let (turns, reads) = counts();
        let elapsed = start.elapsed();
        let (turns, reads) = (turns - turns_before, reads - reads_before);
        let period_bound = (elapsed.as_millis() / LEGACY_CHECK_PERIOD.as_millis()) as usize + 2;
        assert!(
            reads <= period_bound,
            "empty worker turns read status {reads} times in {turns} turns over {elapsed:?}"
        );
        // More turns than the period allows checks: some turns skipped it.
        if turns > period_bound {
            break;
        }
        assert!(
            Instant::now() < deadline,
            "worker took only {turns} turns over {elapsed:?}"
        );
    }

    // Every ptracer attempt finds nothing as well, so none runs the stop
    // proof, and each authenticates through at most one read.
    let mut attempts = 0;
    let empty = legacy_attempts(&terminal, |_| {
        attempts += 1;
        attempts == 32
    });
    assert!(
        empty
            .iter()
            .all(|attempt| attempt.accepted == 0 && attempt.refused == 0),
        "an attempt that found nothing ran the stop proof: {empty:?}"
    );
    assert!(
        empty.iter().all(|attempt| attempt.consumed == 0),
        "an attempt consumed with nothing to consume: {empty:?}"
    );
    let empty_reads: usize = empty.iter().map(|attempt| attempt.reads).sum();
    assert!(
        empty_reads > 0,
        "no empty attempt reached its authentication"
    );
    assert!(terminal.event.event().pending_is_empty());

    let state = || {
        fs::read_to_string(format!("/proc/{root}/task/{child_tid}/stat"))
            .ok()
            .and_then(|stat| {
                Some(
                    stat.rsplit_once(')')?
                        .1
                        .split_whitespace()
                        .next()?
                        .to_owned(),
                )
            })
    };
    request_fixture_thread_sigstop(root, child_tid);
    let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
    while state().as_deref() != Some("t") {
        assert!(Instant::now() < deadline, "thread never reached its stop");
        thread::sleep(Duration::from_millis(1));
    }
    // The attempt that consumes the stop authenticates its owner itself,
    // through the stop proof and without a read. No earlier attempt proved
    // anything without consuming. The wait below then returns the stop the
    // worker published.
    let mut attempts = legacy_attempts_until_consumed(&terminal);
    let evidence: usize = attempts
        .iter()
        .map(|attempt| attempt.reads + attempt.accepted)
        .sum();
    assert!(evidence > 0, "no consuming-phase attempt authenticated");
    let consuming = attempts.pop().unwrap();
    assert_eq!(
        consuming,
        LegacyAttempt {
            reads: 0,
            accepted: 1,
            refused: 0,
            consumed: 1,
        },
        "the consuming attempt did not prove its owner without a read"
    );
    assert!(
        attempts
            .iter()
            .all(|attempt| attempt.accepted == 0 && attempt.consumed == 0),
        "an attempt proved its owner or consumed before the consuming one: {attempts:?}"
    );
    let (child, event) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        child_running.wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped();
    assert_eq!(child.pid(), child_tid);
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));

    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    terminal.request_sigkill().unwrap();
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();

    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

/// Waits until the legacy worker of `event` hints at a status that nothing
/// has consumed yet, which only its own turns can do.
#[cfg(not(sanitized))]
fn wait_for_legacy_hint(event: &Event, what: &str) {
    let deadline = Instant::now() + Duration::from_secs(2);
    while event.legacy_wait.lock().observed.is_none() {
        assert!(
            Instant::now() < deadline,
            "{what}: the worker never observed it"
        );
        thread::sleep(Duration::from_micros(200));
    }
}

/// Waits until the legacy worker of `event` has started a held wait after
/// `before` of them.
#[cfg(not(sanitized))]
fn wait_for_held_wait(event: &Event, before: usize, what: &str) {
    let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
    while event.legacy_held_waits.load(Ordering::Relaxed) <= before {
        assert!(
            Instant::now() < deadline,
            "{what}: the worker never held the stop"
        );
        thread::sleep(Duration::from_micros(200));
    }
}

/// Waits until the legacy worker of `event` is parked in a held wait that
/// started after `before` of them, and after it had observed every wakeup
/// already delivered to it.
///
/// A count and its notify are separate steps: the notify follows once the
/// notifier has released its locks. A notify for a count that the worker
/// has already acted on can therefore arrive after the worker parked, and
/// end that park with one more turn. Such a held wait is not yet quiescent.
/// Every notify that woke the worker is counted, and so is every wait of the
/// worker that a notify ended, one for one (only the worker waits on
/// `legacy_wake`, and parking_lot's condvar has no spurious wakeups). A held
/// wait that began once the worker had observed as many wakes as had been
/// delivered cannot be ended by any of them. A worker still due a delivered
/// wake takes that turn and parks again, so the wait ends.
///
/// Every notifier of `event` must have returned from
/// [`Event::notify_legacy_worker`] before the call, and no other may start
/// during it: a notify still in flight is not counted yet (see there).
///
/// `while_waiting` runs on each poll that does not see such a park yet.
fn wait_for_fresh_held_wait(
    event: &Event,
    before: usize,
    what: &str,
    mut while_waiting: impl FnMut(),
) {
    let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
    loop {
        let delivered = event.legacy_notify_wakes.load(Ordering::Relaxed);
        if event.legacy_held_waits.load(Ordering::Relaxed) > before
            && event.legacy_held_wait_wakes.load(Ordering::Relaxed) >= delivered
        {
            return;
        }
        assert!(
            Instant::now() < deadline,
            "{what}: the worker never parked held after its last delivered wakeup"
        );
        while_waiting();
        thread::sleep(Duration::from_micros(200));
    }
}

/// Resumes `stopped` to its next syscall stop and returns that stop.
#[cfg(not(sanitized))]
async fn next_syscall_stop(stopped: Stopped) -> Stopped {
    let (next, stop) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped
            .syscall(None)
            .unwrap()
            .wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped();
    assert_eq!(stop, crate::Event::Syscall);
    next
}

// A resumption that races a legacy worker's hold is not lost. The held
// non-leader loops reading a pipe. Each round stops it at a read entry and
// pauses its worker at one of the two points of the race, resumes it into
// the read during the pause, and releases the worker. Only then, after the
// worker has had time to settle into whatever wait it chose, does the pipe
// get a byte: the read's exit stop must reach the worker's own turns
// although the held period outlasts the test, with nothing consuming it in
// between. Even rounds pause right after the worker published the entry
// stop, odd rounds after it decided to wait as held, before it takes the
// lock to do so. Reading the wakeup count after the publish pause, or
// waiting without comparing the counts under that lock, leaves the worker in
// a held wait here. (No pause point can separate the publication from a read
// placed right after it: the read must stay before `event.update`.) A
// cancellation signal then kills the group, and the worker must observe the
// last thread's exit stop by itself.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_resume_racing_a_held_wait_is_not_lost() {
    if run_legacy_test_outer("forced_legacy_resume_racing_a_held_wait_is_not_lost") {
        return;
    }
    legacy_resume_racing_a_held_wait(None).await;
}

// The order of a failed run of the test above, forced. The consume of a read
// exit notified the worker only after the worker had published that stop
// and parked held, and the next round's sync accepted that park. The late
// wake then gave the worker a turn on the old stop once the round had armed
// its held-wait pause, so the pause took the old stop, not the read entry.
// Here the consume before round 1, an odd round, notifies late: its notify
// waits until the worker parked held, and the woken worker is held at the
// start of the turn the notify gives it. The round's sync must wait that
// turn out.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_resume_racing_a_held_wait_waits_out_a_late_consume_notify() {
    if run_legacy_test_outer(
        "forced_legacy_resume_racing_a_held_wait_waits_out_a_late_consume_notify",
    ) {
        return;
    }
    legacy_resume_racing_a_held_wait(Some(1)).await;
}

/// The body of `forced_legacy_resume_racing_a_held_wait_is_not_lost`. With
/// `late_notify_round`, an odd round, the consume of the read exit before
/// that round notifies late ([`notify_after_held_park`]).
#[cfg(not(sanitized))]
async fn legacy_resume_racing_a_held_wait(late_notify_round: Option<usize>) {
    const ROUNDS: usize = 10;
    let [read_end, write_end] = legacy_pipe();
    let modes: &'static [usize] = Box::leak(Box::new([1000 + read_end.as_raw_fd() as usize]));
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_threads(
        modes,
        legacy_thread_options() | Options::PTRACE_O_TRACESYSGOOD,
        LegacyAttachMethod::Traceme,
    );
    drop(read_end);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let write_byte = || {
        assert_eq!(
            unsafe { libc::write(write_end.as_raw_fd(), b"r".as_ptr().cast(), 1) },
            1
        )
    };
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing reading pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let event = Arc::clone(terminal.event.event());
    *event.legacy_held_period.lock() = Some(Duration::from_secs(60));
    let (mut child, sigstop) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(sigstop, crate::Event::Signal(Signal::SIGSTOP));
    let parent_running = parent.resume(None).unwrap();
    let read_fd = modes[0] as u64 - 1000;
    let is_read_entry = |stopped: &Stopped| {
        matches!(
            stopped.getsyscallinfo().unwrap().operation,
            crate::SyscallInfoOp::Entry { number, arguments }
                if number == libc::SYS_read as u64 && arguments[0] == read_fd
        )
    };
    // Run the thread's start-up to its first read, then through that read.
    let mut entries = 0;
    while !is_read_entry(&child) {
        entries += 1;
        assert!(entries < 200, "the thread never reached its read");
        child = next_syscall_stop(child).await;
    }
    let mut held = event.legacy_held_waits.load(Ordering::Relaxed);
    let running = child.syscall(None).unwrap();
    write_byte();
    let (mut child, _) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    // The turn a late notify gave the worker, paused at its start, and the
    // worker's count of waits a notify ended, once that notify had.
    let mut late_turn: Option<mpsc::SyncSender<()>> = None;
    let mut late_wake: Option<usize> = None;
    for round in 0..ROUNDS {
        // At the previous read's exit stop: wait until the worker holds it
        // after every wake delivered to it, so that the pause below can only
        // take the next stop. The wait that returned that stop ran the
        // consume's notify on this thread, and nothing else notifies the
        // worker until the resume below.
        wait_for_fresh_held_wait(&event, held, &format!("round {round}: read exit"), || {
            if let Some(release) = late_turn.take() {
                release.send(()).unwrap_or_else(|_| {
                    panic!(
                        "round {round}: the late turn resumed by itself before the sync released it"
                    )
                });
            }
        });
        let (captured, capture) = mpsc::sync_channel(1);
        let (release, released) = mpsc::sync_channel(1);
        let pause = BoundedTestPause {
            captured,
            resume: released,
        };
        // The resume below counts a wakeup before the next publication can
        // read the count, so only a stop published earlier has a count up to
        // this one.
        let armed = event.legacy_wakeup.lock().targeted;
        let slot = if round % 2 == 0 {
            &event.legacy_publish_pause
        } else {
            &event.legacy_held_wait_pause
        };
        *slot.lock() = Some(pause);
        let check_capture = || {
            if round % 2 == 1 {
                let (status, count) = event
                    .legacy_held_wait_paused_on
                    .lock()
                    .take()
                    .unwrap_or_else(|| {
                        panic!("round {round}: the held-wait pause recorded nothing")
                    });
                assert!(
                    count > armed,
                    "round {round}: the pause captured the old stop: it held status {status:#x} \
                     published at targeted wakeup count {count}, and the pause was armed at {armed}"
                );
            }
        };
        let mut captured_before_resume = false;
        if let Some(release) = late_turn.take() {
            // The sync returned with the late turn still pending. If the
            // worker parked again after the late wake, that turn resumed by
            // itself and the schedule this test forces did not happen.
            assert!(
                event.legacy_held_wait_wakes.load(Ordering::Relaxed)
                    < event.legacy_notify_wakes.load(Ordering::Relaxed),
                "round {round}: the late turn resumed by itself (its pause's 2 s bound) before the \
                 sync could wait it out; the forced schedule did not happen"
            );
            // The sync accepted the held wait that the late notify ended. As
            // in the failed run, the late turn runs once the pause is armed
            // and before the resume. It must take the pause before anything
            // resumes the thread, so that only the old stop is there to take.
            // The bound is under the turn's own pause bound (2 s), so a turn
            // that never runs is not mistaken for one that ran.
            drop(release);
            capture
                .recv_timeout(Duration::from_secs(1))
                .unwrap_or_else(|_| {
                    panic!("round {round}: the late turn never reached the armed pause")
                });
            check_capture();
            captured_before_resume = true;
        } else if let Some(late_wake) = late_wake.take() {
            // The sync released the late turn and waited for the held wait
            // the worker started after it.
            let held_wait_wakes = event.legacy_held_wait_wakes.load(Ordering::Relaxed);
            assert!(
                held_wait_wakes >= late_wake
                    && event.legacy_worker_wakes.load(Ordering::Relaxed) == held_wait_wakes,
                "round {round}: the sync accepted a held wait that began before the late wake was \
                 observed"
            );
        }
        let entry = next_syscall_stop(child).await;
        if !captured_before_resume {
            capture
                .recv_timeout(TRACEE_WAIT_TIMEOUT)
                .unwrap_or_else(|_| panic!("round {round}: the worker never reached the pause"));
            check_capture();
        }
        assert!(
            is_read_entry(&entry),
            "round {round}: not at the read entry"
        );
        let running = entry.syscall(None).unwrap();
        release
            .send(())
            .unwrap_or_else(|_| panic!("round {round}: the pause had already resumed by itself"));
        thread::sleep(Duration::from_millis(20));
        assert!(event.legacy_wait.lock().observed.is_none());
        write_byte();
        wait_for_legacy_hint(&event, &format!("round {round}: read exit"));
        held = event.legacy_held_waits.load(Ordering::Relaxed);
        let late =
            (late_notify_round == Some(round + 1)).then(|| notify_after_held_park(&event, held));
        let (exit, stop) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped();
        assert_eq!(stop, crate::Event::Syscall);
        if let Some(late) = late {
            late_turn = Some(late.join().unwrap());
            // The helper saw the late notify end the held wait, and the
            // worker is held at the start of its next turn.
            late_wake = Some(event.legacy_worker_wakes.load(Ordering::Relaxed));
        }
        child = exit;
    }

    // The signal's wakeup, or the leader's exit-stop probe, brings the held
    // worker to the exit stop. Nothing polls `exit` until it has observed it.
    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    terminal.request_sigkill().unwrap();
    wait_for_legacy_hint(&event, "the cancellation signal");
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();
    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

// A failing forced-procfs test unwinds through its armed cleanup guards,
// and they must tear the group down promptly. The non-leader's guard kills
// it, and the thread stops at its exit stop. A procfs worker publishes only
// what its ptracer consumes, so a guard that only observed would never see
// that stop, never resume it, and wait out its 2 s bound; the leader's guard
// then waits out its own, since the group cannot finish exiting. The guard
// runs on the ptracer and drives the generation from there instead. The
// guards' lookup of the registered generation must also capture as the
// generation's owner does, not with the native policy, whose refusal on a
// thread without a thread pidfd goes to stderr: the outer run requires the
// inner stderr to omit it.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_cleanup_guards_reap_a_held_nonleader_on_unwind() {
    if run_legacy_test_outer_checked(
        "forced_legacy_cleanup_guards_reap_a_held_nonleader_on_unwind",
        None,
        |result| {
            assert!(
                !result
                    .output
                    .stderr
                    .windows(NATIVE_THREAD_PIDFD_REQUIREMENT.len())
                    .any(|window| window == NATIVE_THREAD_PIDFD_REQUIREMENT),
                "a cleanup guard opened a native thread pidfd for a procfs generation: {result:?}"
            )
        },
    ) {
        return;
    }
    let reached = Arc::new(Mutex::new(None));
    let record = Arc::clone(&reached);
    let unwind = futures::FutureExt::catch_unwind(std::panic::AssertUnwindSafe(async move {
        let (root, stopped, _root_cleanup, _force) = spawn_legacy_thread_tracee(true);
        let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
        )
        .await
        .unwrap()
        .unwrap()
        .assume_stopped() else {
            panic!("missing paused pthread clone");
        };
        let _child_cleanup = legacy_child_cleanup(&child);
        *child
            .terminal_cleanup()
            .event
            .event()
            .legacy_held_period
            .lock() = Some(Duration::from_secs(60));
        let (_child, sigstop) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped();
        assert_eq!(sigstop, crate::Event::Signal(Signal::SIGSTOP));
        let _parent_running = parent.resume(None).unwrap();
        *record.lock() = Some((root, Instant::now()));
        panic!("a failure with both cleanup guards armed");
    }))
    .await;
    assert!(unwind.is_err());
    let (root, panicked) = reached
        .lock()
        .take()
        .expect("the fixture failed before its deliberate panic");
    let unwound = panicked.elapsed();
    assert!(
        unwound < Duration::from_secs(1),
        "the armed cleanup guards took {unwound:?} to unwind"
    );
    assert!(
        !std::path::Path::new(&format!("/proc/{root}")).exists(),
        "the cleanup guards left the leader {root}"
    );
}

// While the ptracer holds a legacy non-leader in a stop, its worker waits
// for a wakeup instead of polling: no turns while held. A group SIGKILL from
// outside, which no request of the ptracer reports, still reaches the
// worker without waiting out the held period: the leader's exit stop
// probes it.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_held_worker_waits_for_a_wakeup_or_probe() {
    if run_legacy_test_outer("forced_legacy_held_worker_waits_for_a_wakeup_or_probe") {
        return;
    }
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(true);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing paused pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let worker = terminal.event.event().clone();
    *worker.legacy_held_period.lock() = Some(Duration::from_secs(60));
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    let parent_running = parent.resume(None).unwrap();

    wait_for_fresh_held_wait(&worker, 0, "the consumed SIGSTOP", || {});
    let turns = || worker.legacy_worker_turns.load(Ordering::Relaxed);
    let before = turns();
    thread::sleep(Duration::from_millis(200));
    let held_turns = turns() - before;
    assert_eq!(
        held_turns, 0,
        "a held worker took {held_turns} turns in 200 ms"
    );

    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    // Neither a ptrace request nor a cancellation signal of this ptracer.
    assert_eq!(unsafe { libc::kill(root.as_raw(), libc::SIGKILL) }, 0);
    // Nothing polls `exit` until the worker has observed the exit stop on
    // its own turns, so that a consume by the ptracer cannot stand in for
    // the probe.
    wait_for_legacy_hint(&worker, "the group kill");
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();

    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

/// Arms `slot` and returns the receiver of its capture signal and the sender
/// that releases it. Dropping the sender also releases it.
#[cfg(not(sanitized))]
fn arm_legacy_pause(
    slot: &Mutex<Option<BoundedTestPause>>,
) -> (mpsc::Receiver<()>, mpsc::SyncSender<()>) {
    let (captured, capture) = mpsc::sync_channel(1);
    let (release, resume) = mpsc::sync_channel(1);
    *slot.lock() = Some(BoundedTestPause { captured, resume });
    (capture, release)
}

/// Holds the notify of the ptracer's next consume (in
/// `try_assist_legacy_wait`) for the worker of `event` until that worker has
/// published the consumed stop and parked held after `held_before` held
/// waits, then lets the notify go with the worker's next turn paused at its
/// start. The thread returns the sender that releases that turn; dropping
/// it also does.
#[cfg(not(sanitized))]
fn notify_after_held_park(
    event: &Arc<Event>,
    held_before: usize,
) -> thread::JoinHandle<mpsc::SyncSender<()>> {
    let (notify_caught, notify_release) = arm_legacy_pause(&event.legacy_assist_notify_pause);
    let event = event.clone();
    thread::spawn(move || {
        notify_caught
            .recv_timeout(Duration::from_secs(2))
            .expect("the consume never reached its notify");
        // The ptracer counted, released `legacy_wait` and has not notified:
        // the worker publishes the stop and parks held meanwhile.
        let deadline = Instant::now() + Duration::from_millis(1500);
        while event.legacy_held_waits.load(Ordering::Relaxed) <= held_before {
            assert!(
                Instant::now() < deadline,
                "the worker never parked held before the late notify"
            );
            thread::sleep(Duration::from_micros(200));
        }
        // It counts under `legacy_wakeup` and releases it only once it is
        // queued on `legacy_wake`: holding the lock here means it is parked.
        drop(event.legacy_wakeup.lock());
        let parked = event.legacy_worker_wakes.load(Ordering::Relaxed);
        let (turn_caught, turn_release) = arm_legacy_pause(&event.legacy_worker_cycle_pause);
        notify_release.send(()).unwrap();
        turn_caught
            .recv_timeout(Duration::from_secs(2))
            .expect("the late notify did not wake the parked worker");
        assert_eq!(
            event.legacy_worker_wakes.load(Ordering::Relaxed),
            parked + 1,
            "the late notify did not end the held wait"
        );
        turn_release
    })
}

#[cfg(not(sanitized))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum ConsumeWakeup {
    /// The worker is parked when the ptracer consumes.
    Parked,
    /// The worker reaches its sleep check after the ptracer released
    /// `legacy_wait` and before it notifies.
    BetweenDropAndNotify,
}

/// The ptracer consumes a stop that the legacy worker hinted, while the
/// worker would otherwise sleep for 60 s, and the wait must resolve within
/// 2 s: the consume's wakeup is not lost. Afterwards the worker holds the
/// published stop, because the wakeup was counted before the publication.
#[cfg(not(sanitized))]
async fn legacy_consume_wakes_the_worker(case: ConsumeWakeup) {
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(true);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing paused pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let worker = terminal.event.event().clone();
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    let parent_running = parent.resume(None).unwrap();
    let running = child.resume(None).unwrap();
    // The resume counted a wakeup that ends the hold on the SIGSTOP, so no
    // held wait starts after this read until the next publication.
    let held_before = worker.legacy_held_waits.load(Ordering::Relaxed);

    // From the hint of the next stop, only a wakeup ends the worker's sleep
    // within the test's bounds.
    *worker.legacy_poll_period.lock() = Some(Duration::from_secs(60));
    let deliver = || {
        // SAFETY: tgkill takes integer arguments and touches no memory.
        let sent = unsafe {
            libc::syscall(
                libc::SYS_tgkill,
                root.as_raw(),
                child_tid.as_raw(),
                libc::SIGUSR2,
            )
        };
        assert_eq!(sent, 0);
    };
    let coordinator = match case {
        ConsumeWakeup::Parked => {
            let (parked, parking) = mpsc::sync_channel(1);
            *worker.legacy_parking_signal.lock() = Some(parked);
            deliver();
            // Sent while the worker holds `legacy_wakeup` with no new count,
            // right before it waits: the consume counts only after it parked.
            parking
                .recv_timeout(Duration::from_secs(2))
                .expect("the worker never parked after hinting the stop");
            None
        }
        ConsumeWakeup::BetweenDropAndNotify => {
            let (worker_caught, worker_release) =
                arm_legacy_pause(&worker.legacy_sleep_check_pause);
            deliver();
            worker_caught
                .recv_timeout(Duration::from_secs(2))
                .expect("the worker never hinted the stop");
            // Armed while the worker is paused, so its next publication is
            // the stop the ptracer consumes below.
            let (published, publish_release) = arm_legacy_pause(&worker.legacy_publish_pause);
            let (tracer_caught, tracer_release) =
                arm_legacy_pause(&worker.legacy_assist_notify_pause);
            Some(thread::spawn(move || {
                tracer_caught
                    .recv_timeout(Duration::from_secs(2))
                    .expect("the consume never reached its notify");
                // Released first, the worker reaches its sleep check while
                // the ptracer has not notified. The 1.5 s window ends before
                // either pause resumes by itself (2 s).
                worker_release.send(()).unwrap();
                let before_notify = published.recv_timeout(Duration::from_millis(1500)).is_ok();
                if before_notify {
                    publish_release.send(()).unwrap();
                }
                tracer_release.send(()).unwrap();
                if !before_notify {
                    let _ = published.recv_timeout(Duration::from_secs(2));
                    let _ = publish_release.send(());
                }
                before_notify
            }))
        }
    };
    // Its first poll consumes the stop through `try_assist_legacy_wait`.
    let (child, event) = tokio::time::timeout(
        Duration::from_secs(2),
        running.wait_owned_on_ptracer_thread(),
    )
    .await
    .expect("lost wakeup: the consumed stop was not published within 2 s")
    .unwrap()
    .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGUSR2));
    if let Some(coordinator) = coordinator {
        assert!(
            coordinator.join().unwrap(),
            "the worker did not publish before the consume notified: \
             its sleep check did not see the consume's wakeup count"
        );
    }
    *worker.legacy_poll_period.lock() = None;
    wait_for_held_wait(&worker, held_before, "the consumed stop");

    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    assert_eq!(unsafe { libc::kill(root.as_raw(), libc::SIGKILL) }, 0);
    wait_for_legacy_hint(&worker, "the group kill");
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();

    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

// T1: the worker is parked, sleeping for 60 s after hinting a stop, when
// the ptracer consumes that stop. The consume counts a wakeup and notifies
// after it released `legacy_wait`; the notify must reach the parked worker.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_consume_wakes_a_parked_worker() {
    if run_legacy_test_outer("forced_legacy_consume_wakes_a_parked_worker") {
        return;
    }
    legacy_consume_wakes_the_worker(ConsumeWakeup::Parked).await;
}

// T2: the worker reaches its sleep check between the consume's release of
// `legacy_wait` and its notify. The wakeup was counted under `legacy_wait`,
// so the worker sees it without the notify and publishes the stop at once,
// before the notify; it then holds that stop.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_consume_counts_its_wakeup_before_the_notify() {
    if run_legacy_test_outer("forced_legacy_consume_counts_its_wakeup_before_the_notify") {
        return;
    }
    legacy_consume_wakes_the_worker(ConsumeWakeup::BetweenDropAndNotify).await;
}

#[cfg(not(sanitized))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum HeldWaitSkip {
    /// A probe is pending when the worker decides to wait as a held worker.
    PendingProbe,
    /// The ptracer's consume counted its wakeup during the worker's turn,
    /// after the worker had last read the counts.
    CountDuringTurn,
}

/// A held worker whose wakeup counts changed since it last read them does
/// not wait: it takes another turn first. That skipped wait is not a held
/// wait and must not be counted as one. The worker is paused at its first
/// held-wait decision after the published SIGSTOP, with the wakeup pending,
/// and again at its next one: no held wait may have been counted by then.
/// It then parks, and a parked held worker takes no turns.
#[cfg(not(sanitized))]
async fn legacy_skipped_held_wait_is_not_counted(case: HeldWaitSkip) {
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(true);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing paused pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let worker = terminal.event.event().clone();
    *worker.legacy_held_period.lock() = Some(Duration::from_secs(60));
    // Taken by the first held-wait decision: one after the publication of
    // the SIGSTOP consumed below, since no stop is held before it.
    let (first_caught, first_release) = arm_legacy_pause(&worker.legacy_held_wait_pause);
    let counted = match case {
        HeldWaitSkip::PendingProbe => None,
        HeldWaitSkip::CountDuringTurn => {
            // Hold the worker at the start of a turn, after it read the
            // counts and before it takes `legacy_wait`, while the consume
            // below counts its wakeup.
            wait_for_legacy_hint(&worker, "the SIGSTOP");
            let (turn_caught, turn_release) = arm_legacy_pause(&worker.legacy_worker_cycle_pause);
            turn_caught
                .recv_timeout(Duration::from_secs(2))
                .expect("the worker never started another turn");
            let before = worker.legacy_wakeup.lock().targeted;
            let event = worker.clone();
            Some(thread::spawn(move || {
                let deadline = Instant::now() + Duration::from_millis(1500);
                while event.legacy_wakeup.lock().targeted == before {
                    assert!(
                        Instant::now() < deadline,
                        "the consume never counted its wakeup"
                    );
                    thread::sleep(Duration::from_micros(200));
                }
                turn_release.send(()).unwrap();
            }))
        }
    };
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    if let Some(counted) = counted {
        counted.join().unwrap();
    }
    let parent_running = parent.resume(None).unwrap();
    first_caught
        .recv_timeout(Duration::from_secs(2))
        .expect("the worker never decided to hold the SIGSTOP");
    let held_before = worker.legacy_held_waits.load(Ordering::Relaxed);
    if case == HeldWaitSkip::PendingProbe {
        worker.probe_legacy_worker();
    }
    // Armed while the worker is paused, so its next held-wait decision
    // takes it.
    let (second_caught, second_release) = arm_legacy_pause(&worker.legacy_held_wait_pause);
    let turns = || worker.legacy_worker_turns.load(Ordering::Relaxed);
    let turns_before = turns();
    first_release.send(()).unwrap();
    second_caught
        .recv_timeout(Duration::from_secs(2))
        .expect("the worker never decided to hold the SIGSTOP again");
    assert_eq!(
        worker.legacy_held_waits.load(Ordering::Relaxed),
        held_before,
        "{case:?}: a held wait was counted although the pending wakeup skipped it"
    );
    assert!(
        turns() > turns_before,
        "{case:?}: the pending wakeup did not give the worker another turn"
    );
    second_release.send(()).unwrap();
    wait_for_held_wait(&worker, held_before, "the SIGSTOP");
    let before = turns();
    thread::sleep(Duration::from_millis(200));
    let held_turns = turns() - before;
    assert_eq!(
        held_turns, 0,
        "a held worker took {held_turns} turns in 200 ms"
    );

    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    // Neither a ptrace request nor a cancellation signal of this ptracer.
    assert_eq!(unsafe { libc::kill(root.as_raw(), libc::SIGKILL) }, 0);
    // Nothing polls `exit` until the worker has observed the exit stop on
    // its own turns, so that a consume by the ptracer cannot stand in for
    // the probe.
    wait_for_legacy_hint(&worker, "the group kill");
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();

    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

// The worker decides to hold the published SIGSTOP while a probe is
// pending. It polls for the probe instead of waiting, and only the wait it
// parks in afterwards counts as a held wait.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_skipped_held_wait_after_a_probe_is_not_counted() {
    if run_legacy_test_outer("forced_legacy_skipped_held_wait_after_a_probe_is_not_counted") {
        return;
    }
    legacy_skipped_held_wait_is_not_counted(HeldWaitSkip::PendingProbe).await;
}

// The ptracer's consume of the SIGSTOP counts its wakeup during the
// worker's turn, after the worker last read the counts. The worker
// publishes and holds the stop, but its first held-wait decision sees that
// count as new and takes one more turn. That turn does not follow a held
// wait.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_skipped_held_wait_after_a_turn_count_is_not_counted() {
    if run_legacy_test_outer("forced_legacy_skipped_held_wait_after_a_turn_count_is_not_counted") {
        return;
    }
    legacy_skipped_held_wait_is_not_counted(HeldWaitSkip::CountDuringTurn).await;
}

// The consume's notify arrives after the worker published the consumed
// stop and parked held: the ptracer is paused between its count and its
// notify until the worker has parked, and the woken worker is then held at
// the start of the turn that notify gives it. That held wait is not
// quiescent, so the check of a held worker's turns must wait for a held
// wait that started after the late wake was observed. Once there, it takes
// no turns.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_held_turn_check_waits_out_a_late_consume_notify() {
    if run_legacy_test_outer("forced_legacy_held_turn_check_waits_out_a_late_consume_notify") {
        return;
    }
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_thread_tracee(true);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing paused pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let worker = terminal.event.event().clone();
    *worker.legacy_held_period.lock() = Some(Duration::from_secs(60));
    let held_before = worker.legacy_held_waits.load(Ordering::Relaxed);
    let delay = notify_after_held_park(&worker, held_before);
    let (child, event) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(event, crate::Event::Signal(Signal::SIGSTOP));
    let mut turn_release = Some(delay.join().unwrap());
    let parent_running = parent.resume(None).unwrap();

    // The worker is woken and held before its turn. The check must not
    // accept the held wait that the late notify ended; it lets the worker
    // take its turn once it sees that wait is stale.
    wait_for_fresh_held_wait(&worker, held_before, "the late notify", || {
        if let Some(release) = turn_release.take() {
            release.send(()).unwrap();
        }
    });
    assert_eq!(
        worker.legacy_worker_wakes.load(Ordering::Relaxed),
        worker.legacy_held_wait_wakes.load(Ordering::Relaxed),
        "the accepted held wait was already ended by a delivered notify"
    );
    drop(turn_release);
    let turns = || worker.legacy_worker_turns.load(Ordering::Relaxed);
    let before = turns();
    thread::sleep(Duration::from_millis(200));
    let held_turns = turns() - before;
    assert_eq!(
        held_turns, 0,
        "a held worker took {held_turns} turns in 200 ms"
    );

    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    // Neither a ptrace request nor a cancellation signal of this ptracer.
    assert_eq!(unsafe { libc::kill(root.as_raw(), libc::SIGKILL) }, 0);
    // Nothing polls `exit` until the worker has observed the exit stop on
    // its own turns, so that a consume by the ptracer cannot stand in for
    // the probe.
    wait_for_legacy_hint(&worker, "the group kill");
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();

    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

/// The legacy worker counters the deferred-wake tests read, at one moment.
#[cfg(not(sanitized))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct LegacyWakeCounts {
    /// Observation turns (`legacy_worker_turns`).
    turns: usize,
    /// Turns whose observation found a status.
    hints: usize,
    /// Notifies that woke the worker.
    notify_wakes: usize,
    /// Deferred wakeups that an empty assist notified.
    flushes: usize,
    /// Stops the worker found only after a held wait's timeout.
    expiry_finds: usize,
}

#[cfg(not(sanitized))]
impl LegacyWakeCounts {
    fn of(event: &Event) -> Self {
        Self {
            turns: event.legacy_worker_turns.load(Ordering::Relaxed),
            hints: event.legacy_hint_turns.load(Ordering::Relaxed),
            notify_wakes: event.legacy_notify_wakes.load(Ordering::Relaxed),
            flushes: event.legacy_deferred_flushes.load(Ordering::Relaxed),
            expiry_finds: event.legacy_held_expiry_finds.load(Ordering::Relaxed),
        }
    }

    fn since(self, before: Self) -> Self {
        Self {
            turns: self.turns - before.turns,
            hints: self.hints - before.hints,
            notify_wakes: self.notify_wakes - before.notify_wakes,
            flushes: self.flushes - before.flushes,
            expiry_finds: self.expiry_finds - before.expiry_finds,
        }
    }
}

/// How a deferred-wake round waits for the pipe-reading thread's read exit.
#[cfg(not(sanitized))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum DeferredWakePath {
    /// The async ptracer wait. For 20 ms after the resume, no worker turn or
    /// hint; then one poll with a no-op waker, which flushes the deferred
    /// wake, and the parked half ([`deferred_wake_parked_wait`]).
    Async,
    /// [`Self::Async`], also checking for those 20 ms that no notify woke the
    /// worker and that it observed nothing.
    Pinned,
    /// The synchronous ptracer wait, with the byte written 5 ms in.
    Sync,
    /// The cleanup driver's pending reservation, with the byte written 5 ms
    /// in.
    Cleanup,
}

/// How long after its stop arrives a deferred-wake test's wait may return:
/// half the production held period, so that a wakeup that reached the worker
/// only through a held wait's timeout cannot pass. The tests also set the
/// held period to 60 s.
#[cfg(not(sanitized))]
const DEFERRED_WAKE_BOUND: Duration = Duration::from_millis(50);

/// Writes one byte to the fixture thread's pipe.
#[cfg(not(sanitized))]
fn deferred_wake_write(write_end: &OwnedFd) {
    assert_eq!(
        unsafe { libc::write(write_end.as_raw_fd(), b"r".as_ptr().cast(), 1) },
        1
    );
}

/// Writes one byte to `write_end` from another thread 5 ms from now, and
/// returns when it wrote.
#[cfg(not(sanitized))]
fn deferred_wake_write_later(write_end: &Arc<OwnedFd>) -> JoinHandle<Instant> {
    let write_end = Arc::clone(write_end);
    thread::spawn(move || {
        thread::sleep(Duration::from_millis(5));
        let written = Instant::now();
        deferred_wake_write(&write_end);
        written
    })
}

/// The procfs state letter of the fixture thread `tid` in `root`'s group.
#[cfg(not(sanitized))]
fn deferred_wake_thread_state(root: Pid, tid: crate::Pid) -> Option<String> {
    fs::read_to_string(format!("/proc/{root}/task/{tid}/stat"))
        .ok()
        .and_then(|stat| {
            Some(
                stat.rsplit_once(')')?
                    .1
                    .split_whitespace()
                    .next()?
                    .to_owned(),
            )
        })
}

/// Runs a forced-legacy non-leader that loops reading a pipe up to its first
/// read entry, its worker's held period set to 60 s, then runs `rounds`
/// rounds of `round`, then kills and reaps the group. `round` gets the stop
/// at a read entry, with the worker parked in a fresh held wait, and returns
/// the thread's stop at its next read entry, with `legacy_held_waits` as read
/// right before the wait that returned that stop
/// ([`deferred_wake_next_read_entry`]).
#[cfg(not(sanitized))]
async fn with_deferred_wake_pipe_thread<F, R>(rounds: usize, mut round: F)
where
    F: FnMut(usize, Stopped, Arc<Event>, Arc<OwnedFd>, Pid) -> R,
    R: Future<Output = (Stopped, usize)>,
{
    let [read_end, write_end] = legacy_pipe();
    let modes: &'static [usize] = Box::leak(Box::new([1000 + read_end.as_raw_fd() as usize]));
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_threads(
        modes,
        legacy_thread_options() | Options::PTRACE_O_TRACESYSGOOD,
        LegacyAttachMethod::Traceme,
    );
    drop(read_end);
    let write_end = Arc::new(write_end);
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing reading pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let event = Arc::clone(terminal.event.event());
    *event.legacy_held_period.lock() = Some(Duration::from_secs(60));
    let (mut child, sigstop) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(sigstop, crate::Event::Signal(Signal::SIGSTOP));
    let parent_running = parent.resume(None).unwrap();
    let read_fd = modes[0] as u64 - 1000;
    let is_read_entry = |stopped: &Stopped| {
        matches!(
            stopped.getsyscallinfo().unwrap().operation,
            crate::SyscallInfoOp::Entry { number, arguments }
                if number == libc::SYS_read as u64 && arguments[0] == read_fd
        )
    };
    let mut entries = 0;
    let mut held = event.legacy_held_waits.load(Ordering::Relaxed);
    while !is_read_entry(&child) {
        entries += 1;
        assert!(entries < 200, "the thread never reached its read");
        held = event.legacy_held_waits.load(Ordering::Relaxed);
        child = next_syscall_stop(child).await;
    }
    for index in 0..rounds {
        wait_for_fresh_held_wait(&event, held, &format!("round {index}: read entry"), || {});
        (child, held) = round(
            index,
            child,
            Arc::clone(&event),
            Arc::clone(&write_end),
            root,
        )
        .await;
        assert!(
            is_read_entry(&child),
            "round {index}: not back at a read entry"
        );
    }

    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    terminal.request_sigkill().unwrap();
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();
    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
}

/// Resumes the fixture thread from its read exit to its next read entry.
/// Returns that stop, and `legacy_held_waits` as read right before the wait:
/// the worker's held wait for the read entry starts after that count.
#[cfg(not(sanitized))]
async fn deferred_wake_next_read_entry(exit: Stopped, event: &Event) -> (Stopped, usize) {
    let held = event.legacy_held_waits.load(Ordering::Relaxed);
    (next_syscall_stop(exit).await, held)
}

/// The prefix of the deferred-wake tests' report lines, which the outer run
/// re-emits ([`run_legacy_test_outer_reporting`]).
#[cfg(not(sanitized))]
const DEFERRED_WAKE_REPORT: &str = "deferred-wake report: ";

/// What one parked deferred-wake round's counted-wake check found
/// ([`DeferredWakeCounted::check`]): what the round lacked, if anything, how
/// many uncounted wakes the worker had up to the check, and how many of them
/// were late consume notifies.
#[cfg(not(sanitized))]
type DeferredWakeCheck = (Option<String>, usize, usize);

/// The counted-wake evidence of one parked deferred-wake round: the targeted
/// wakeup count just before and just after its request, where its entries
/// start in the worker's wake log ([`Event::legacy_wake_log`]), and
/// `legacy_held_waits` just before the request.
#[cfg(not(sanitized))]
struct DeferredWakeCounted {
    before: u64,
    after: u64,
    log_start: usize,
    held: usize,
}

/// Runs a round's resume or interrupt `request`, reading the targeted wakeup
/// count just before and just after it.
#[cfg(not(sanitized))]
fn deferred_wake_counted_request<T>(
    event: &Event,
    request: impl FnOnce() -> T,
) -> (T, DeferredWakeCounted) {
    let log_start = event.legacy_wake_log.lock().len();
    let held = event.legacy_held_waits.load(Ordering::Relaxed);
    let before = event.legacy_wakeup.lock().targeted;
    let output = request();
    let after = event.legacy_wakeup.lock().targeted;
    (
        output,
        DeferredWakeCounted {
            before,
            after,
            log_start,
            held,
        },
    )
}

/// A wait of the legacy worker that a notify ended, in a round's wake log:
/// the index of its [`LegacyWakeLogEntry::WaitBegin`] (`None` for the wait
/// the round began in, which began before the round's log), the targeted
/// count the worker had acted on, and whether the notify had moved a count.
#[cfg(not(sanitized))]
struct NotifyEndedWait {
    begin: Option<usize>,
    seen: u64,
    counted: bool,
}

/// Names a notify of the legacy worker, with the receipt of the count it
/// stands for.
#[cfg(not(sanitized))]
fn deferred_wake_notify_source(source: LegacyNotifySource) -> String {
    let receipt = |receipt: Option<LegacyCountReceipt>| match receipt {
        Some(receipt) => format!(
            "receipt {} for targeted {}",
            receipt.serial, receipt.targeted
        ),
        None => "no receipt".to_owned(),
    };
    match source {
        LegacyNotifySource::Immediate(r) => format!("an immediate wake ({})", receipt(r)),
        LegacyNotifySource::Flush(r) => format!("a deferred wake's flush ({})", receipt(r)),
        LegacyNotifySource::Probe(count) => format!("a probe (probes {count})"),
        LegacyNotifySource::Consume(r) => format!("a consume ({})", receipt(r)),
        LegacyNotifySource::Unknown => "a notify that named no source".to_owned(),
    }
}

/// Audit (d) of [`DeferredWakeCounted::check`] over a round's wake log, taken
/// once the worker is quiescent.
///
/// Every notify that names a site that counts (an immediate wake, a
/// deferral's flush, a consume) must carry the receipt of that site's own
/// count ([`LegacyCountReceipt`]): minted, and logged as
/// [`LegacyWakeLogEntry::Counted`], inside that count, and used by at most
/// one notify of the round, counted or uncounted. A notify that made no
/// count, a receipt never counted, and a receipt used twice each fail.
///
/// Each wait of the worker that a notify ended is assigned, in order, the
/// earliest unassigned [`LegacyWakeLogEntry::Notified`] entry after its
/// `WaitBegin`: a notify that woke the worker found it parked, so it came
/// after that `WaitBegin`, and in these fixtures every such notify runs on
/// the test thread, in order. An uncounted wake is allowed only as a late
/// consume notify: its notify is a consume whose receipt passed the rules
/// above, whose `Counted` entry precedes the wait's `WaitBegin`, and whose
/// count the worker had acted on (`targeted <= seen`). So the worker began
/// the wait after acting on that consume's own increment, and the consume's
/// notify, sent after it released `legacy_wait`, arrived during it.
/// Anything else is unexplained: an uncounted wake by any other notify, a
/// notify-ended wait with no notify logged, or a notify that woke the worker
/// with no wake logged.
///
/// (d) sees only notifies whose `notify_one` returned true, that is, notifies
/// that found the worker parked. A notify that lands while the worker runs
/// is invisible to it, and has no effect. So whether an extra notify is
/// caught depends on timing, round by round. It is reliable where the worker
/// sits in one long held wait, as in
/// `forced_legacy_deferred_wake_reaches_an_async_wait`. In the mutation runs
/// at the time of writing, that test caught an extra notify after the hint in
/// every round, and the exit-wait and pinned tests in most; that is an
/// observation, not a guarantee for every round.
/// `forced_legacy_counted_wake_check_refuses_extra_notifies_to_a_parked_worker`
/// is the deterministic negative control: it delivers each extra notify to a
/// worker it has proven parked.
///
/// Returns the late consume notifies and what is unexplained. `before` is
/// the targeted count when the round began.
#[cfg(not(sanitized))]
fn deferred_wake_late_consume_audit(
    log: &[LegacyWakeLogEntry],
    before: u64,
) -> (usize, Vec<String>) {
    let mut waits = Vec::new();
    // Each notify: its index, its source, whether a wait took it, and
    // whether its receipt passed (an uncounted wake needs that).
    let mut notifies = Vec::new();
    let mut counted = std::collections::HashMap::new();
    let mut used_receipts = std::collections::HashSet::new();
    let mut open = Some((None, before));
    let mut unexplained = Vec::new();
    for (at, entry) in log.iter().enumerate() {
        match *entry {
            LegacyWakeLogEntry::WaitBegin(seen) => open = Some((Some(at), seen)),
            LegacyWakeLogEntry::CountedWake | LegacyWakeLogEntry::Uncounted => match open.take() {
                Some((begin, seen)) => waits.push(NotifyEndedWait {
                    begin,
                    seen,
                    counted: *entry == LegacyWakeLogEntry::CountedWake,
                }),
                None => unexplained.push(format!("a wake at entry {at} with no wait begun")),
            },
            LegacyWakeLogEntry::Counted(receipt) => {
                counted.insert(receipt.serial, (at, receipt));
            }
            LegacyWakeLogEntry::Notified(source) => {
                let receipt = match source {
                    LegacyNotifySource::Immediate(receipt)
                    | LegacyNotifySource::Flush(receipt)
                    | LegacyNotifySource::Consume(receipt) => Some(receipt),
                    LegacyNotifySource::Probe(_) | LegacyNotifySource::Unknown => None,
                };
                let named = deferred_wake_notify_source(source);
                let receipt_passed = match receipt {
                    None => false,
                    Some(None) => {
                        unexplained.push(format!("{named} made no count"));
                        false
                    }
                    Some(Some(receipt)) => {
                        let minted = counted
                            .get(&receipt.serial)
                            .is_some_and(|(_, minted)| *minted == receipt);
                        if !minted {
                            unexplained.push(format!(
                                "receipt {} was never counted ({named})",
                                receipt.serial
                            ));
                        }
                        let first = used_receipts.insert(receipt.serial);
                        if !first {
                            unexplained.push(format!(
                                "receipt {} used by a second notify ({named})",
                                receipt.serial
                            ));
                        }
                        minted && first
                    }
                };
                notifies.push((at, source, false, receipt_passed));
            }
            _ => {}
        }
    }
    let mut late = 0;
    for wait in &waits {
        let assigned = notifies
            .iter_mut()
            .find(|(at, _, used, _)| !*used && wait.begin.is_none_or(|begin| *at > begin));
        let Some((_, source, used, receipt_passed)) = assigned else {
            unexplained.push(format!(
                "a wait begun at entry {:?} ended by a notify that was not logged",
                wait.begin
            ));
            continue;
        };
        *used = true;
        if wait.counted {
            continue;
        }
        let late_consume = match *source {
            LegacyNotifySource::Consume(Some(receipt)) => {
                *receipt_passed
                    && receipt.targeted <= wait.seen
                    && wait.begin.is_some_and(|begin| {
                        counted
                            .get(&receipt.serial)
                            .is_some_and(|(at, _)| *at < begin)
                    })
            }
            _ => false,
        };
        if late_consume {
            late += 1;
        } else {
            unexplained.push(format!(
                "an uncounted wake of the wait begun at entry {:?} (the worker had acted on \
                 targeted count {}) ended by {}",
                wait.begin,
                wait.seen,
                deferred_wake_notify_source(*source)
            ));
        }
    }
    let extra = notifies.iter().filter(|(_, _, used, _)| !*used).count();
    if extra != 0 {
        unexplained.push(format!(
            "{extra} notify(s) woke the worker with no wake logged"
        ));
    }
    (late, unexplained)
}

// The late-consume audit on synthetic logs, with no tracee. It accepts the
// real late-consume window: a consume counts, the worker acts on that count
// and parks, and only then does the consume's notify reach it. It refuses a
// consume that made no count, a receipt used twice (by two uncounted wakes,
// or by a counted and an uncounted one), a receipt never counted, an
// uncounted wake by a count the worker had not acted on, an uncounted wake
// by a flush, a wake with no notify logged, and a notify with no wake.
#[test]
#[cfg(not(sanitized))]
fn deferred_wake_audit_accepts_only_a_receipted_late_consume_notify() {
    use LegacyNotifySource::Consume;
    use LegacyNotifySource::Flush;
    use LegacyWakeLogEntry::Counted;
    use LegacyWakeLogEntry::CountedWake;
    use LegacyWakeLogEntry::Hint;
    use LegacyWakeLogEntry::Notified;
    use LegacyWakeLogEntry::Uncounted;
    use LegacyWakeLogEntry::WaitBegin;
    let r = |serial, targeted| LegacyCountReceipt { serial, targeted };
    let opening = [
        Counted(r(1, 4)),
        Notified(Flush(Some(r(1, 4)))),
        CountedWake,
        Hint,
    ];
    let with_opening = |rest: &[LegacyWakeLogEntry]| -> Vec<LegacyWakeLogEntry> {
        opening.iter().chain(rest).copied().collect()
    };
    let window = with_opening(&[
        Counted(r(2, 5)),
        WaitBegin(5),
        Notified(Consume(Some(r(2, 5)))),
        Uncounted,
    ]);
    assert_eq!(
        deferred_wake_late_consume_audit(&window, 3),
        (1, Vec::new()),
        "the real late-consume window was not accepted"
    );
    let refused = |log: &[LegacyWakeLogEntry], reason: &str, late: usize| {
        let (accepted, unexplained) = deferred_wake_late_consume_audit(log, 3);
        assert!(
            unexplained.iter().any(|what| what.contains(reason)),
            "no refusal naming {reason:?} in {unexplained:?} for {log:?}"
        );
        assert_eq!(accepted, late, "late consume notifies for {log:?}");
    };
    refused(
        &with_opening(&[WaitBegin(4), Notified(Consume(None)), Uncounted]),
        "a consume (no receipt) made no count",
        0,
    );
    let mut twice = window.clone();
    twice.extend([WaitBegin(5), Notified(Consume(Some(r(2, 5)))), Uncounted]);
    refused(&twice, "receipt 2 used by a second notify", 1);
    refused(
        &with_opening(&[
            Counted(r(2, 5)),
            Notified(Consume(Some(r(2, 5)))),
            CountedWake,
            WaitBegin(5),
            Notified(Consume(Some(r(2, 5)))),
            Uncounted,
        ]),
        "receipt 2 used by a second notify",
        0,
    );
    refused(
        &with_opening(&[WaitBegin(4), Notified(Consume(Some(r(9, 4)))), Uncounted]),
        "receipt 9 was never counted",
        0,
    );
    refused(
        &with_opening(&[
            WaitBegin(4),
            Counted(r(2, 5)),
            Notified(Consume(Some(r(2, 5)))),
            Uncounted,
        ]),
        "an uncounted wake of the wait begun at entry Some(4)",
        0,
    );
    refused(
        &[
            Counted(r(1, 4)),
            WaitBegin(4),
            Notified(Flush(Some(r(1, 4)))),
            Uncounted,
        ],
        "ended by a deferred wake's flush",
        0,
    );
    refused(
        &[WaitBegin(4), Uncounted],
        "ended by a notify that was not logged",
        0,
    );
    refused(
        &[Counted(r(1, 4)), Notified(Flush(Some(r(1, 4))))],
        "1 notify(s) woke the worker with no wake logged",
        0,
    );
}

#[cfg(not(sanitized))]
impl DeferredWakeCounted {
    /// After the round's wait returned, whether the round shows that its
    /// request's counted wakeup is what released the worker, and that every
    /// later wake of the worker is accounted for: (a) the request counted
    /// exactly one targeted wakeup; (b) no uncounted wake came before the
    /// worker's first hint of the round; (c) the round's first hold end
    /// carries exactly the request's count and precedes that hint, which
    /// precedes Ready (only the worker's hint can wake a parked wait after
    /// the byte); (d) through Ready and until the worker is quiescent, every
    /// uncounted wake is a late consume notify, matched one for one to the
    /// consume whose count the worker had already acted on
    /// ([`deferred_wake_late_consume_audit`]).
    ///
    /// The check first waits until the worker is parked held after its last
    /// delivered wake ([`wait_for_fresh_held_wait`]). Every notifier of the
    /// round has returned by then: they ran on this thread, or are the
    /// worker's own probe of itself, which returns before it waits. So the
    /// log it then reads holds the worker's response to every notify sent
    /// before it.
    ///
    /// Before the check ran to quiescence, it stopped its uncounted-wake test
    /// at the first hint, and only reported a count taken from the snapshot
    /// after Ready, `uncounted_at_check`. That count was 0 in all 820 rounds
    /// of the gate runs of 2026-11-13 at
    /// 9e3672ef93012b531fda2ccb1758de524824459e: 400 of
    /// `forced_legacy_deferred_wake_reaches_an_async_wait`, 400 of
    /// `forced_legacy_deferred_wake_reaches_an_exit_wait`, and 20 of
    /// `forced_legacy_resume_does_not_wake_a_parked_worker_until_the_first_empty_assist`.
    /// That snapshot could miss a wake the worker had not logged yet.
    fn check(&self, event: &Event, what: &str) -> DeferredWakeCheck {
        wait_for_fresh_held_wait(
            event,
            self.held,
            &format!("{what}: counted-wake check"),
            || {},
        );
        let log = event.legacy_wake_log.lock()[self.log_start..].to_vec();
        let hint = log
            .iter()
            .position(|entry| *entry == LegacyWakeLogEntry::Hint);
        let first_end = log.iter().enumerate().find_map(|(at, entry)| match entry {
            LegacyWakeLogEntry::HoldEndedByCount(count) => Some((at, *count)),
            _ => None,
        });
        let uncounted = |entries: &[LegacyWakeLogEntry]| {
            entries
                .iter()
                .filter(|entry| **entry == LegacyWakeLogEntry::Uncounted)
                .count()
        };
        let mut lacks = Vec::new();
        let counted = self.after.wrapping_sub(self.before);
        if counted != 1 {
            lacks.push(format!(
                "(a) the request counted {counted} targeted wakeups, not 1"
            ));
        }
        let early = uncounted(&log[..hint.unwrap_or(log.len())]);
        if early != 0 {
            lacks.push(format!(
                "(b) {early} uncounted wake(s) before the worker's hint"
            ));
        }
        if !matches!((first_end, hint), (Some((at, count)), Some(hint)) if count == self.after && at < hint)
        {
            lacks.push(format!(
                "(c) the hold was not ended by the request's count {} before the worker's hint",
                self.after
            ));
        }
        let (late, unexplained) = deferred_wake_late_consume_audit(&log, self.before);
        lacks.extend(unexplained.into_iter().map(|what| format!("(d) {what}")));
        let lacks = (!lacks.is_empty()).then(|| format!("{}; wake log {log:?}", lacks.join("; ")));
        (lacks, uncounted(&log), late)
    }
}

/// Prints a parked deferred-wake test's counted-wake report: its rounds, the
/// rounds that failed the check ([`DeferredWakeCounted::check`]), the
/// worker's uncounted wakes up to each check, and how many of them were late
/// consume notifies. Returns the failed rounds, for
/// [`deferred_wake_assert_counted`].
#[cfg(not(sanitized))]
fn deferred_wake_report_counted(label: &str, checks: &[DeferredWakeCheck]) -> Vec<(usize, String)> {
    let failed: Vec<(usize, String)> = checks
        .iter()
        .enumerate()
        .filter_map(|(round, (lacks, _, _))| lacks.clone().map(|lacks| (round, lacks)))
        .collect();
    let rounds: Vec<usize> = failed.iter().map(|(round, _)| *round).collect();
    let uncounted: usize = checks.iter().map(|(_, uncounted, _)| uncounted).sum();
    let late: usize = checks.iter().map(|(_, _, late)| late).sum();
    println!(
        "{DEFERRED_WAKE_REPORT}{label}: counted-wake n={} failed={} {rounds:?} \
         uncounted_to_quiescence={uncounted} late_consume_notify={late}",
        checks.len(),
        failed.len(),
    );
    failed
}

/// Fails if any of `rounds` rounds failed the counted-wake check, with what
/// each lacked.
#[cfg(not(sanitized))]
fn deferred_wake_assert_counted(label: &str, rounds: usize, failed: &[(usize, String)]) {
    assert!(
        failed.is_empty(),
        "{label}: {} of {rounds} rounds failed the counted-wake check: {failed:?}",
        failed.len(),
    );
}

/// Reports one deferred-wake test's rounds and asserts that none missed its
/// bound. `rounds` holds each round's time from the write to the return, or
/// `None` for a round that missed the bound. The report line gives the
/// missed rounds, and the median (the element at index n/2 of the sorted
/// values) and the maximum of the others.
#[cfg(not(sanitized))]
fn deferred_wake_check_rounds(label: &str, rounds: &[Option<Duration>]) {
    let missed: Vec<usize> = (0..rounds.len()).filter(|&i| rounds[i].is_none()).collect();
    let mut sorted: Vec<Duration> = rounds.iter().flatten().copied().collect();
    sorted.sort();
    let (p50, max) = match sorted.last() {
        Some(max) => (
            sorted[sorted.len() / 2].as_micros().to_string(),
            max.as_micros().to_string(),
        ),
        None => ("-".to_owned(), "-".to_owned()),
    };
    println!(
        "{DEFERRED_WAKE_REPORT}{label}: n={} missed={} {missed:?} p50_us={p50} max_us={max}",
        rounds.len(),
        missed.len(),
    );
    assert!(
        missed.is_empty(),
        "{label}: {} of {} rounds missed the {DEFERRED_WAKE_BOUND:?} bound: rounds {missed:?}",
        missed.len(),
        rounds.len()
    );
}

/// Polls `wait` each time `counter` moves past `seen`, which the caller read
/// before its last poll, until the wait is ready or `deadline` passes. It
/// never polls before the counter moves.
#[cfg(not(sanitized))]
fn deferred_wake_poll_on_wakes<F: Future>(
    mut wait: Pin<&mut F>,
    counter: &WakeCounter,
    cx: &mut Context<'_>,
    mut seen: usize,
    deadline: Instant,
) -> Option<F::Output> {
    loop {
        while counter.0.load(Ordering::SeqCst) == seen {
            if Instant::now() >= deadline {
                return None;
            }
            thread::sleep(Duration::from_micros(100));
        }
        seen = counter.0.load(Ordering::SeqCst);
        if let Poll::Ready(output) = wait.as_mut().poll(cx) {
            return Some(output);
        }
    }
}

/// The parked half of a deferred-wake round, after the first poll flushed
/// the deferred wake. Polls `wait` a second time with a counting waker and
/// asserts it is still pending and unwoken, so the waiter is parked before
/// its stop can exist. Then writes the byte, and polls only when the counter
/// moves: only the worker, released by the flush, can move it. Returns the
/// wait's output, and its time from the write if it was ready within
/// [`DEFERRED_WAKE_BOUND`] (`None` if not). A round that misses the bound is
/// recovered: the assist consumes the stop, which stays in the kernel.
#[cfg(not(sanitized))]
fn deferred_wake_parked_wait<F: Future>(
    mut wait: Pin<&mut F>,
    write_end: &OwnedFd,
    what: &str,
) -> (F::Output, Option<Duration>) {
    let counter = Arc::new(WakeCounter::default());
    let waker = Waker::from(Arc::clone(&counter));
    let mut cx = Context::from_waker(&waker);
    assert!(
        wait.as_mut().poll(&mut cx).is_pending(),
        "{what}: the second poll returned before the byte"
    );
    assert_eq!(
        counter.0.load(Ordering::SeqCst),
        0,
        "{what}: the parked waiter was woken before the byte"
    );
    let written = Instant::now();
    deferred_wake_write(write_end);
    if let Some(output) = deferred_wake_poll_on_wakes(
        wait.as_mut(),
        &counter,
        &mut cx,
        0,
        written + DEFERRED_WAKE_BOUND,
    ) {
        let latency = written.elapsed();
        assert!(
            counter.0.load(Ordering::SeqCst) >= 1,
            "{what}: ready without a wake"
        );
        return (output, (latency < DEFERRED_WAKE_BOUND).then_some(latency));
    }
    let seen = counter.0.load(Ordering::SeqCst);
    let output = match wait.as_mut().poll(&mut cx) {
        Poll::Ready(output) => output,
        Poll::Pending => deferred_wake_poll_on_wakes(
            wait.as_mut(),
            &counter,
            &mut cx,
            seen,
            Instant::now() + Duration::from_secs(2),
        )
        .unwrap_or_else(|| panic!("{what}: the missed round did not recover its stop")),
    };
    (output, None)
}

/// Checks for 20 ms after a resume that the parked worker stayed asleep: no
/// worker turn and no hint since `before`. With `strict`, also no notify that
/// woke the worker, and no observation.
#[cfg(not(sanitized))]
fn deferred_wake_pin(event: &Event, before: LegacyWakeCounts, strict: bool, what: &str) {
    let deadline = Instant::now() + Duration::from_millis(20);
    loop {
        let moved = LegacyWakeCounts::of(event).since(before);
        assert_eq!(
            (moved.turns, moved.hints),
            (0, 0),
            "{what}: the resume woke the parked worker: {moved:?}"
        );
        if strict {
            assert_eq!(
                moved.notify_wakes, 0,
                "{what}: the resume notified the parked worker: {moved:?}"
            );
            assert!(
                event.legacy_wait.lock().observed.is_none(),
                "{what}: the worker observed a status before any assist"
            );
        }
        if Instant::now() >= deadline {
            return;
        }
        thread::sleep(Duration::from_millis(1));
    }
}

/// One round of a deferred-wake test on the pipe-reading thread. From a read
/// entry, with the worker parked in a fresh held wait: resumes into the read,
/// waits on `path` for the read's exit, with the byte written as `path`
/// says, then resumes on to the next read entry. Returns that entry and its
/// held-wait count ([`deferred_wake_next_read_entry`]), and how long after
/// the byte's write the wait returned the read's exit.
#[cfg(not(sanitized))]
async fn deferred_wake_round(
    path: DeferredWakePath,
    round: usize,
    entry: Stopped,
    event: &Event,
    write_end: &Arc<OwnedFd>,
) -> (Stopped, usize, Option<Duration>, Option<DeferredWakeCheck>) {
    let what = format!("{path:?} round {round}");
    let before = LegacyWakeCounts::of(event);
    let (exit, latency, counted) = match path {
        DeferredWakePath::Async | DeferredWakePath::Pinned => {
            let (running, counted) =
                deferred_wake_counted_request(event, || entry.syscall(None).unwrap());
            deferred_wake_pin(event, before, path == DeferredWakePath::Pinned, &what);
            let mut wait = std::pin::pin!(running.wait_owned_on_ptracer_thread());
            assert!(
                wait.as_mut()
                    .poll(&mut Context::from_waker(Waker::noop()))
                    .is_pending(),
                "{what}: the read returned before its byte"
            );
            assert_eq!(
                LegacyWakeCounts::of(event).since(before).flushes,
                1,
                "{what}: the first empty assist did not flush the deferred wake"
            );
            let (wait, latency) = deferred_wake_parked_wait(wait.as_mut(), write_end, &what);
            let check = counted.check(event, &what);
            let (exit, stop) = wait.unwrap().assume_stopped();
            assert_eq!(stop, crate::Event::Syscall);
            (exit, latency, Some(check))
        }
        DeferredWakePath::Sync => {
            let running = entry.syscall(None).unwrap();
            let writer = deferred_wake_write_later(write_end);
            let mut wait = running.wait_sync_on_ptracer_thread();
            let (exit, stop) = wait.wait().unwrap().assume_stopped();
            let returned = Instant::now();
            assert_eq!(stop, crate::Event::Syscall);
            let latency = returned.saturating_duration_since(writer.join().unwrap());
            assert!(
                latency < DEFERRED_WAKE_BOUND,
                "{what}: the wait returned {latency:?} after the byte"
            );
            (exit, Some(latency), None)
        }
        DeferredWakePath::Cleanup => {
            let mut cleanup = entry.terminal_cleanup_on_ptracer_thread().into_driver();
            let running = entry.syscall(None).unwrap();
            let writer = deferred_wake_write_later(write_end);
            let pending = cleanup
                .reserve_pending_on_ptracer_thread(Duration::from_secs(2))
                .unwrap()
                .unwrap_or_else(|| panic!("{what}: no stop within 2 s"));
            let returned = Instant::now();
            let (exit, stop) = pending.decode().unwrap().assume_stopped();
            pending.commit();
            drop(running);
            assert_eq!(stop, crate::Event::Syscall);
            let latency = returned.saturating_duration_since(writer.join().unwrap());
            assert!(
                latency < DEFERRED_WAKE_BOUND,
                "{what}: the wait returned {latency:?} after the byte"
            );
            (exit, Some(latency), None)
        }
    };
    assert_eq!(
        exit.getsyscallinfo().unwrap().operation,
        crate::SyscallInfoOp::Exit {
            return_value: 1,
            is_error: false
        },
        "{what}: not the read's exit"
    );
    assert_eq!(
        event.legacy_held_expiry_finds.load(Ordering::Relaxed),
        0,
        "{what}: the worker found a stop only after a held wait's timeout"
    );
    let (entry, held) = deferred_wake_next_read_entry(exit, event).await;
    (entry, held, latency, counted)
}

/// Runs `plan`'s rounds in order on one pipe-reading thread
/// ([`with_deferred_wake_pipe_thread`]): for each entry, that many rounds of
/// [`deferred_wake_round`] on its path. Returns each entry's rounds, for
/// [`deferred_wake_check_rounds`], and each entry's counted-wake checks, for
/// [`deferred_wake_report_counted`] (the parked paths only).
#[cfg(not(sanitized))]
async fn deferred_wake_rounds(
    plan: &[(DeferredWakePath, usize)],
) -> (Vec<Vec<Option<Duration>>>, Vec<Vec<DeferredWakeCheck>>) {
    let slots: Vec<usize> = plan
        .iter()
        .enumerate()
        .flat_map(|(slot, &(_, rounds))| std::iter::repeat_n(slot, rounds))
        .collect();
    let latencies = Rc::new(std::cell::RefCell::new(vec![Vec::new(); plan.len()]));
    let checks = Rc::new(std::cell::RefCell::new(vec![Vec::new(); plan.len()]));
    with_deferred_wake_pipe_thread(slots.len(), |round, entry, event, write_end, _root| {
        let slot = slots[round];
        let path = plan[slot].0;
        let latencies = Rc::clone(&latencies);
        let checks = Rc::clone(&checks);
        async move {
            let (entry, held, latency, check) =
                deferred_wake_round(path, round, entry, &event, &write_end).await;
            latencies.borrow_mut()[slot].push(latency);
            if let Some(check) = check {
                checks.borrow_mut()[slot].push(check);
            }
            (entry, held)
        }
    })
    .await;
    (latencies.take(), checks.take())
}

/// The voluntary context switches of the fixture thread `tid` in `root`'s
/// group. A thread that enters a ptrace stop switches out voluntarily.
#[cfg(not(sanitized))]
fn deferred_wake_voluntary_switches(root: Pid, tid: crate::Pid) -> u64 {
    fs::read_to_string(format!("/proc/{root}/task/{tid}/status"))
        .unwrap()
        .lines()
        .find_map(|line| line.strip_prefix("voluntary_ctxt_switches:"))
        .unwrap()
        .trim()
        .parse()
        .unwrap()
}

/// One step of `forced_legacy_stop_found_at_first_poll_takes_one_worker_wake`.
/// Resumes `stopped` to its next syscall stop, writing the byte first if
/// `write_end` is given. Waits until that stop is in the kernel: the thread
/// has switched out voluntarily since the resume, and shows `t`. Only then
/// waits for the stop. Returns it, and `legacy_held_waits` as read before the
/// resume.
#[cfg(not(sanitized))]
async fn deferred_wake_found_step(
    stopped: Stopped,
    event: &Event,
    root: Pid,
    write_end: Option<&OwnedFd>,
    what: &str,
) -> (Stopped, usize) {
    let tid = stopped.pid();
    let held = event.legacy_held_waits.load(Ordering::Relaxed);
    let switches = deferred_wake_voluntary_switches(root, tid);
    if let Some(write_end) = write_end {
        deferred_wake_write(write_end);
    }
    let running = stopped.syscall(None).unwrap();
    let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
    while deferred_wake_voluntary_switches(root, tid) <= switches
        || deferred_wake_thread_state(root, tid).as_deref() != Some("t")
    {
        assert!(
            Instant::now() < deadline,
            "{what}: the stop never reached the kernel"
        );
        thread::sleep(Duration::from_micros(200));
    }
    let (next, stop) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, running.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(stop, crate::Event::Syscall);
    (next, held)
}

// A stop that is already in the kernel when the ptracer first waits for it
// costs the legacy worker one wake: the consume's notify, after which the
// worker takes one empty turn and parks again. The resume that led to the
// stop defers its wake, and the ptracer's first assist consumes the stop, so
// nothing flushes that wake, and the worker never observes the stop by
// itself. The pipe-reading non-leader is stepped 50 times (read exit, read
// entry, ...), each time with its worker parked in a fresh held wait first;
// for a read exit the byte is written before the resume.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_stop_found_at_first_poll_takes_one_worker_wake() {
    if run_legacy_test_outer_reporting(
        "forced_legacy_stop_found_at_first_poll_takes_one_worker_wake",
        DEFERRED_WAKE_REPORT,
    ) {
        return;
    }
    const ROUNDS: usize = 25;
    let start = Rc::new(Cell::new(None));
    let end = Rc::new(Cell::new(None));
    // Status reads by the attachment check on this ptracer thread, and
    // deferrals that woke a status waker, over the 50 steps.
    let extra = Rc::new(Cell::new((0, 0)));
    with_deferred_wake_pipe_thread(ROUNDS, |round, entry, event, write_end, root| {
        let (start, end, extra) = (Rc::clone(&start), Rc::clone(&end), Rc::clone(&extra));
        async move {
            if round == 0 {
                start.set(Some(LegacyWakeCounts::of(&event)));
                extra.set((
                    PTRACER_ATTACHMENT_STATUS_READS.with(Cell::get),
                    event.legacy_deferred_status_wakes.load(Ordering::Relaxed),
                ));
            }
            let what = format!("round {round}: read exit");
            let (exit, held) =
                deferred_wake_found_step(entry, &event, root, Some(&*write_end), &what).await;
            wait_for_fresh_held_wait(&event, held, &what, || {});
            let what = format!("round {round}: read entry");
            let (entry, held) = deferred_wake_found_step(exit, &event, root, None, &what).await;
            if round == ROUNDS - 1 {
                wait_for_fresh_held_wait(&event, held, &what, || {});
                end.set(Some(LegacyWakeCounts::of(&event)));
                let (reads, status_wakes) = extra.get();
                extra.set((
                    PTRACER_ATTACHMENT_STATUS_READS.with(Cell::get) - reads,
                    event.legacy_deferred_status_wakes.load(Ordering::Relaxed) - status_wakes,
                ));
                // No exit waiter is registered here, so no deferral wakes one.
                assert_eq!(
                    event.legacy_deferred_exit_wakes.load(Ordering::Relaxed),
                    0,
                    "a deferral woke an exit waiter, but none was registered"
                );
            }
            (entry, held)
        }
    })
    .await;
    let steps = end.get().unwrap().since(start.get().unwrap());
    let (status_reads, status_wakes) = extra.get();
    println!(
        "{DEFERRED_WAKE_REPORT}stop found at first poll, {} steps: {steps:?}, \
         status reads {status_reads}, deferral status wakes {status_wakes}",
        2 * ROUNDS
    );
    assert_eq!(
        steps,
        LegacyWakeCounts {
            turns: 2 * ROUNDS,
            hints: 0,
            notify_wakes: 2 * ROUNDS,
            flushes: 0,
            expiry_finds: 0,
        },
        "a stop found at the first poll did not cost exactly one worker wake and one turn"
    );
}

// A resume's deferred wake reaches a parked async ptracer wait (W1). Each of
// 20 rounds resumes the pipe-reading non-leader into its read with the
// worker parked held (period 60 s), and checks for 20 ms that no turn or
// hint follows. It polls the wait once (Pending; that empty assist flushes
// the wake), then again with a counting waker (Pending, unwoken), so that
// the waiter is parked before its stop can exist. Only then does it write
// the byte, and it polls again only when the waker is woken, which only the
// worker released by the flush can do. Every round must return the read's
// exit within 50 ms of the write, and the worker must never have found a
// stop through a held wait's timeout.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_deferred_wake_reaches_an_async_wait() {
    if run_legacy_test_outer_reporting(
        "forced_legacy_deferred_wake_reaches_an_async_wait",
        DEFERRED_WAKE_REPORT,
    ) {
        return;
    }
    let (rounds, checks) = deferred_wake_rounds(&[(DeferredWakePath::Async, 20)]).await;
    let failed = deferred_wake_report_counted("parked async wait", &checks[0]);
    deferred_wake_check_rounds("parked async wait, write to return", &rounds[0]);
    deferred_wake_assert_counted("parked async wait", checks[0].len(), &failed);
}

// A resume's deferred wake reaches a parked exit wait (W2). Each of 20 fresh
// non-leaders reads one byte and exits. At its read entry, with its worker
// parked held (period 60 s), it is resumed, and for 20 ms no turn or hint
// follows. Its exit wait is polled once (Pending; that empty assist flushes
// the wake), then again with a counting waker (Pending, unwoken), so that
// the waiter is parked before its stop can exist. Only then is the byte
// written, and the wait is polled again only when the waker is woken, which
// only the worker released by the flush can do. Every round's exit wait
// must return within 50 ms of the write, with no stop found through a held
// wait's timeout.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_deferred_wake_reaches_an_exit_wait() {
    if run_legacy_test_outer_reporting(
        "forced_legacy_deferred_wake_reaches_an_exit_wait",
        DEFERRED_WAKE_REPORT,
    ) {
        return;
    }
    const ROUNDS: usize = 20;
    let [read_end, write_end] = legacy_pipe();
    let modes: &'static [usize] =
        Box::leak(Box::new([2000 + read_end.as_raw_fd() as usize; ROUNDS]));
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_threads(
        modes,
        legacy_thread_options() | Options::PTRACE_O_TRACESYSGOOD,
        LegacyAttachMethod::Traceme,
    );
    drop(read_end);
    let read_fd = modes[0] as u64 - 2000;
    let is_read_entry = |stopped: &Stopped| {
        matches!(
            stopped.getsyscallinfo().unwrap().operation,
            crate::SyscallInfoOp::Entry { number, arguments }
                if number == libc::SYS_read as u64 && arguments[0] == read_fd
        )
    };
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let mut parent_running = stopped.resume(None).unwrap();
    let mut latencies = Vec::new();
    let mut checks = Vec::new();
    for round in 0..ROUNDS {
        let what = format!("exit round {round}");
        let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent_running.wait_owned_on_ptracer_thread(),
        )
        .await
        .unwrap()
        .unwrap()
        .assume_stopped() else {
            panic!("{what}: missing reading pthread clone");
        };
        let child_tid = child.pid();
        let mut child_cleanup = legacy_child_cleanup(&child);
        let terminal = child.terminal_cleanup();
        let event = Arc::clone(terminal.event.event());
        *event.legacy_held_period.lock() = Some(Duration::from_secs(60));
        let (mut child, sigstop) =
            tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
                .await
                .unwrap()
                .unwrap()
                .assume_stopped();
        assert_eq!(sigstop, crate::Event::Signal(Signal::SIGSTOP));
        let mut entries = 0;
        let mut held = event.legacy_held_waits.load(Ordering::Relaxed);
        while !is_read_entry(&child) {
            entries += 1;
            assert!(entries < 200, "{what}: the thread never reached its read");
            held = event.legacy_held_waits.load(Ordering::Relaxed);
            child = next_syscall_stop(child).await;
        }
        wait_for_fresh_held_wait(&event, held, &format!("{what}: read entry"), || {});
        let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
        let before = LegacyWakeCounts::of(&event);
        let (running, counted) =
            deferred_wake_counted_request(&event, || child.resume(None).unwrap());
        deferred_wake_pin(&event, before, false, &what);
        let mut exit = std::pin::pin!(exit);
        assert!(
            exit.as_mut()
                .poll(&mut Context::from_waker(Waker::noop()))
                .is_pending(),
            "{what}: the thread exited before its byte"
        );
        assert_eq!(
            LegacyWakeCounts::of(&event).since(before).flushes,
            1,
            "{what}: the first empty assist did not flush the deferred wake"
        );
        let (exiting, latency) = deferred_wake_parked_wait(exit.as_mut(), &write_end, &what);
        checks.push(counted.check(&event, &what));
        let exiting = exiting.unwrap();
        child_cleanup.mark_claimed_exit();
        drop(running);
        assert_eq!(exiting.pid(), child_tid);
        assert_eq!(exiting.getevent().unwrap(), 0);
        assert_eq!(
            event.legacy_held_expiry_finds.load(Ordering::Relaxed),
            0,
            "{what}: the worker found the exit stop only after a held wait's timeout"
        );
        latencies.push(latency);
        assert_eq!(
            tokio::time::timeout(
                TRACEE_WAIT_TIMEOUT,
                exiting.resume(None).unwrap().wait_owned_on_ptracer_thread()
            )
            .await
            .unwrap()
            .unwrap()
            .assume_exited(),
            (child_tid, crate::ExitStatus::Exited(0)),
        );
        assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
        child_cleanup.disarm();
        // The leader stays at its clone stop until the thread is reaped: once
        // its join returns it starts the next thread or, after the last,
        // exits the group, and a thread reaped during a group exit reports
        // the group's exit code.
        parent_running = parent.resume(None).unwrap();
    }
    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (root.into(), crate::ExitStatus::Exited(42)),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
    let failed = deferred_wake_report_counted("parked exit wait", &checks);
    deferred_wake_check_rounds("parked exit wait, write to return", &latencies);
    deferred_wake_assert_counted("parked exit wait", checks.len(), &failed);
}

// The synchronous ptracer wait (W3) and the cleanup driver's pending
// reservation (W4) meet the same bound after a deferred resume: 20 rounds
// each on the pipe-reading non-leader, its worker parked held (period
// 60 s), with the byte written by another thread 5 ms after the wait
// starts. Each wait must return the read's exit within 50 ms of the write.
// Both paths assist every 1 ms or less by themselves, so this shows their
// bound, not the flush.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_deferred_wake_meets_the_sync_and_cleanup_bounds() {
    if run_legacy_test_outer_reporting(
        "forced_legacy_deferred_wake_meets_the_sync_and_cleanup_bounds",
        DEFERRED_WAKE_REPORT,
    ) {
        return;
    }
    let (rounds, _checks) = deferred_wake_rounds(&[
        (DeferredWakePath::Sync, 20),
        (DeferredWakePath::Cleanup, 20),
    ])
    .await;
    deferred_wake_check_rounds("sync wait, write to return", &rounds[0]);
    deferred_wake_check_rounds("cleanup reservation, write to return", &rounds[1]);
}

// A resume of a legacy non-leader whose worker is parked held does not wake
// the worker: for 20 ms after it, the worker takes no turn, observes
// nothing, and no notify wakes it. The resume's count still ends the hold:
// the ptracer's first empty assist flushes the deferred wake, and that alone
// ends a parked wait. The wait is polled again with a counting waker
// (Pending, unwoken) before the byte is written, and not again until the
// waker is woken; it must return the read's exit within 50 ms of the write,
// with no stop found through a held wait's timeout (period 60 s).
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_resume_does_not_wake_a_parked_worker_until_the_first_empty_assist() {
    if run_legacy_test_outer_reporting(
        "forced_legacy_resume_does_not_wake_a_parked_worker_until_the_first_empty_assist",
        DEFERRED_WAKE_REPORT,
    ) {
        return;
    }
    let (rounds, checks) = deferred_wake_rounds(&[(DeferredWakePath::Pinned, 1)]).await;
    let failed = deferred_wake_report_counted("pinned parked async wait", &checks[0]);
    deferred_wake_check_rounds("pinned parked async wait, write to return", &rounds[0]);
    deferred_wake_assert_counted("pinned parked async wait", checks[0].len(), &failed);
}

/// The outcome line of `forced_legacy_exit_wait_parked_before_a_resume_gets_its_stop`.
#[cfg(not(sanitized))]
const PARKED_EXIT_WAIT_MARKER: &str =
    "exit wait parked before the resume: woken and ready within the bound";

// An exit wait already parked before a resume still gets its stop, as the
// exit future reverie-ptrace's run loop keeps across every resume must. A
// non-leader reads one byte and exits. At its read entry, with its worker
// parked held (period 60 s), its exit wait is polled once with a counting
// waker, before any resume: Pending. Only then is the thread resumed, with
// the Running handle kept, the leader held at its clone stop so that no
// group exit intervenes, and no other wait or assist. After the byte is
// written, nothing polls until the waker is woken. The exit stop must be
// ready within 50 ms of the write, with no stop found through a held wait's
// timeout. The resume's deferral wakes the parked exit wait, from the
// ptracer thread.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_exit_wait_parked_before_a_resume_gets_its_stop() {
    if run_legacy_test_outer_with_outcome(
        "forced_legacy_exit_wait_parked_before_a_resume_gets_its_stop",
        Some(PARKED_EXIT_WAIT_MARKER),
    ) {
        return;
    }
    let [read_end, write_end] = legacy_pipe();
    let modes: &'static [usize] = Box::leak(Box::new([2000 + read_end.as_raw_fd() as usize]));
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_threads(
        modes,
        legacy_thread_options() | Options::PTRACE_O_TRACESYSGOOD,
        LegacyAttachMethod::Traceme,
    );
    drop(read_end);
    let read_fd = modes[0] as u64 - 2000;
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing reading pthread clone");
    };
    // The leader stays at this clone stop until the thread is reaped, so no
    // group exit can end or replace the thread's stops.
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let event = Arc::clone(terminal.event.event());
    *event.legacy_held_period.lock() = Some(Duration::from_secs(60));
    let (mut child, sigstop) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(sigstop, crate::Event::Signal(Signal::SIGSTOP));
    let mut entries = 0;
    let mut held = event.legacy_held_waits.load(Ordering::Relaxed);
    while !matches!(
        child.getsyscallinfo().unwrap().operation,
        crate::SyscallInfoOp::Entry { number, arguments }
            if number == libc::SYS_read as u64 && arguments[0] == read_fd
    ) {
        entries += 1;
        assert!(entries < 200, "the thread never reached its read");
        held = event.legacy_held_waits.load(Ordering::Relaxed);
        child = next_syscall_stop(child).await;
    }
    wait_for_fresh_held_wait(&event, held, "read entry", || {});
    // Park the exit wait before the resume: one real poll, with a counting
    // waker, on this ptracer thread.
    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    let mut exit = std::pin::pin!(exit);
    let counter = ThreadWakeCounter::new();
    let waker = Waker::from(Arc::clone(&counter));
    let mut cx = Context::from_waker(&waker);
    assert!(
        exit.as_mut().poll(&mut cx).is_pending(),
        "the exit wait returned before the resume"
    );
    let exit_wakes_before = event.legacy_deferred_exit_wakes.load(Ordering::Relaxed);
    let (wakes_before, off_thread_before) = (counter.wakes(), counter.off_thread());
    let running = child.resume(None).unwrap();
    // The resume's deferral wakes the parked exit wait (`defer_legacy_wake`),
    // within the request, on this ptracer thread. The worker parked held
    // wakes nothing meanwhile; its later hint and publication wake the exit
    // wait from its own thread, as before.
    let exit_wakes = event.legacy_deferred_exit_wakes.load(Ordering::Relaxed) - exit_wakes_before;
    let request_wakes = counter.wakes() - wakes_before;
    let request_off_thread = counter.off_thread() - off_thread_before;
    let written = Instant::now();
    assert_eq!(
        unsafe { libc::write(write_end.as_raw_fd(), b"r".as_ptr().cast(), 1) },
        1
    );
    // No poll until the waker is woken; then poll on each wake, all within
    // the bound.
    let deadline = written + DEFERRED_WAKE_BOUND;
    let mut seen = 0;
    let mut ready = None;
    while ready.is_none() && Instant::now() < deadline {
        if counter.wakes() == seen {
            thread::sleep(Duration::from_micros(100));
            continue;
        }
        seen = counter.wakes();
        if let Poll::Ready(output) = exit.as_mut().poll(&mut cx) {
            ready = Some((output, written.elapsed()));
        }
    }
    let woken = counter.wakes();
    let (exiting, latency) = match ready {
        Some((output, latency)) => (output, Some(latency)),
        None => {
            // Recover the stop, which stays in the kernel, so that the group
            // is torn down cleanly before the verdict.
            let deadline = Instant::now() + Duration::from_secs(2);
            let output = loop {
                if let Poll::Ready(output) = exit.as_mut().poll(&mut cx) {
                    break output;
                }
                assert!(Instant::now() < deadline, "the exit stop was not recovered");
                thread::sleep(Duration::from_micros(100));
            };
            (output, None)
        }
    };
    let exiting = exiting.unwrap();
    child_cleanup.mark_claimed_exit();
    drop(running);
    assert_eq!(exiting.pid(), child_tid);
    assert_eq!(exiting.getevent().unwrap(), 0);
    assert_eq!(
        event.legacy_held_expiry_finds.load(Ordering::Relaxed),
        0,
        "the worker found the exit stop only after a held wait's timeout"
    );
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            exiting.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (child_tid, crate::ExitStatus::Exited(0)),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();
    let parent_running = parent.resume(None).unwrap();
    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (root.into(), crate::ExitStatus::Exited(42)),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
    println!("parked exit wait before the resume: woken {woken} times, write to ready {latency:?}");
    assert!(
        latency.is_some_and(|latency| latency < DEFERRED_WAKE_BOUND),
        "the exit wait parked before the resume was not ready within 50 ms of the byte \
         (woken {woken} times, write to ready {latency:?})"
    );
    assert_eq!(
        exit_wakes, 1,
        "the resume did not wake the parked exit wait"
    );
    assert!(
        request_wakes >= 1,
        "the resume did not wake the parked exit wait's waker"
    );
    assert_eq!(
        request_off_thread, 0,
        "the resume's wake of the parked exit wait came from another thread"
    );
    emit_completion_marker(PARKED_EXIT_WAIT_MARKER);
}

/// A waker that counts its wakes, and those that came from a thread other
/// than the one that made it.
#[cfg(not(sanitized))]
struct ThreadWakeCounter {
    home: ThreadId,
    wakes: AtomicUsize,
    off_thread: AtomicUsize,
}

#[cfg(not(sanitized))]
impl ThreadWakeCounter {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            home: thread::current().id(),
            wakes: AtomicUsize::new(0),
            off_thread: AtomicUsize::new(0),
        })
    }

    fn wakes(&self) -> usize {
        self.wakes.load(Ordering::SeqCst)
    }

    fn off_thread(&self) -> usize {
        self.off_thread.load(Ordering::SeqCst)
    }
}

#[cfg(not(sanitized))]
impl Wake for ThreadWakeCounter {
    fn wake(self: Arc<Self>) {
        self.wake_by_ref();
    }

    fn wake_by_ref(self: &Arc<Self>) {
        if thread::current().id() != self.home {
            self.off_thread.fetch_add(1, Ordering::SeqCst);
        }
        self.wakes.fetch_add(1, Ordering::SeqCst);
    }
}

// With an exit wait parked across every request, as reverie-ptrace's run
// loop keeps one, a stop found at the first poll still costs the legacy
// worker one wake, and the exit wait's extra polls take nothing from the
// waits. The pipe-reading non-leader is stepped 50 times (read exit, read
// entry, ...). One exit wait is created and polled Pending before the first
// request, and kept to the end. Each step, with the worker parked in a fresh
// held wait: the request (for a read exit, after the byte), then, once the
// stop is in the kernel, the exit wait is polled first, as the task the
// request woke polls it in the run loop's `select_biased!` order. Its assist
// consumes the stop and has the worker publish it; it stays Pending. Once
// the worker has published that ordinary stop, and before anything that
// can claim it has been polled, the exit wait is polled again: it must stay
// Pending and take nothing. Only then is the step's wait polled, and it
// must find its stop at once, in order. All 50 of those checks must run.
// Over the 50 steps the worker counts must be exactly those of
// `forced_legacy_stop_found_at_first_poll_takes_one_worker_wake`, and every
// wake of the exit wait must come from this ptracer thread. The status
// reads of the attachment check and the deferrals' status wakes are
// reported.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_stop_found_at_first_poll_with_a_parked_exit_wait_takes_one_worker_wake() {
    if run_legacy_test_outer_reporting(
        "forced_legacy_stop_found_at_first_poll_with_a_parked_exit_wait_takes_one_worker_wake",
        DEFERRED_WAKE_REPORT,
    ) {
        return;
    }
    const STEPS: usize = 50;
    let [read_end, write_end] = legacy_pipe();
    let modes: &'static [usize] = Box::leak(Box::new([1000 + read_end.as_raw_fd() as usize]));
    let (root, stopped, mut root_cleanup, _force) = spawn_legacy_threads(
        modes,
        legacy_thread_options() | Options::PTRACE_O_TRACESYSGOOD,
        LegacyAttachMethod::Traceme,
    );
    drop(read_end);
    let read_fd = modes[0] as u64 - 1000;
    let is_read_entry = |stopped: &Stopped| {
        matches!(
            stopped.getsyscallinfo().unwrap().operation,
            crate::SyscallInfoOp::Entry { number, arguments }
                if number == libc::SYS_read as u64 && arguments[0] == read_fd
        )
    };
    let root_exit = root_cleanup.exit_event_on_ptracer_thread(&stopped).unwrap();
    let root_terminal = stopped.terminal_cleanup();
    let (parent, crate::Event::NewChild(crate::ChildOp::Clone, child)) = tokio::time::timeout(
        TRACEE_WAIT_TIMEOUT,
        stopped.resume(None).unwrap().wait_owned_on_ptracer_thread(),
    )
    .await
    .unwrap()
    .unwrap()
    .assume_stopped() else {
        panic!("missing reading pthread clone");
    };
    let child_tid = child.pid();
    let mut child_cleanup = legacy_child_cleanup(&child);
    let terminal = child.terminal_cleanup();
    let event = Arc::clone(terminal.event.event());
    *event.legacy_held_period.lock() = Some(Duration::from_secs(60));
    let (mut child, sigstop) =
        tokio::time::timeout(TRACEE_WAIT_TIMEOUT, child.wait_owned_on_ptracer_thread())
            .await
            .unwrap()
            .unwrap()
            .assume_stopped();
    assert_eq!(sigstop, crate::Event::Signal(Signal::SIGSTOP));
    let parent_running = parent.resume(None).unwrap();
    let mut entries = 0;
    let mut held = event.legacy_held_waits.load(Ordering::Relaxed);
    while !is_read_entry(&child) {
        entries += 1;
        assert!(entries < 200, "the thread never reached its read");
        held = event.legacy_held_waits.load(Ordering::Relaxed);
        child = next_syscall_stop(child).await;
    }
    wait_for_fresh_held_wait(&event, held, "read entry", || {});
    // The long-lived exit wait, parked before the first request.
    let exit = child_cleanup.exit_event_on_ptracer_thread(&child).unwrap();
    let mut exit = std::pin::pin!(exit);
    let exit_counter = ThreadWakeCounter::new();
    let exit_waker = Waker::from(Arc::clone(&exit_counter));
    assert!(
        exit.as_mut()
            .poll(&mut Context::from_waker(&exit_waker))
            .is_pending(),
        "the exit wait returned before the first request"
    );
    let start = LegacyWakeCounts::of(&event);
    let exit_wakes_start = event.legacy_deferred_exit_wakes.load(Ordering::Relaxed);
    let status_wakes_start = event.legacy_deferred_status_wakes.load(Ordering::Relaxed);
    // Wakes of the exit wait within the request calls, and how many of them
    // came from another thread. The worker's own later wakes are not counted.
    let (mut request_wakes, mut request_off_thread) = (0, 0);
    let (mut exit_checks, mut status_reads) = (0, 0);
    for step in 0..STEPS {
        let what = format!("step {step}");
        let to_exit = is_read_entry(&child);
        assert_eq!(to_exit, step % 2 == 0, "{what}: out of order");
        let tid = child.pid();
        let held = event.legacy_held_waits.load(Ordering::Relaxed);
        let switches = deferred_wake_voluntary_switches(root, tid);
        if to_exit {
            deferred_wake_write(&write_end);
        }
        let reads_before = PTRACER_ATTACHMENT_STATUS_READS.with(Cell::get);
        let (wakes_before, off_thread_before) = (exit_counter.wakes(), exit_counter.off_thread());
        let running = child.syscall(None).unwrap();
        request_wakes += exit_counter.wakes() - wakes_before;
        request_off_thread += exit_counter.off_thread() - off_thread_before;
        let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
        while deferred_wake_voluntary_switches(root, tid) <= switches
            || deferred_wake_thread_state(root, tid).as_deref() != Some("t")
        {
            assert!(
                Instant::now() < deadline,
                "{what}: the stop never reached the kernel"
            );
            thread::sleep(Duration::from_micros(200));
        }
        // The task the request woke polls the exit wait first: its assist
        // consumes the stop and notifies the worker to publish it.
        assert!(
            exit.as_mut()
                .poll(&mut Context::from_waker(&exit_waker))
                .is_pending(),
            "{what}: the exit wait returned on an ordinary stop"
        );
        let deadline = Instant::now() + TRACEE_WAIT_TIMEOUT;
        while event.pending_is_empty() {
            assert!(
                Instant::now() < deadline,
                "{what}: the ordinary stop was never published"
            );
            thread::sleep(Duration::from_micros(100));
        }
        // With that stop published, and nothing that can claim it polled
        // yet, the exit wait is polled again: it stays Pending and takes
        // nothing.
        assert!(
            exit.as_mut()
                .poll(&mut Context::from_waker(&exit_waker))
                .is_pending(),
            "{what}: the exit wait returned on a published ordinary stop"
        );
        assert!(
            !event.pending_is_empty(),
            "{what}: the exit wait's poll took the published stop"
        );
        exit_checks += 1;
        // Only now is the run loop's wait polled: it finds its stop at once.
        let mut wait = std::pin::pin!(running.wait_owned_on_ptracer_thread());
        let Poll::Ready(output) = wait.as_mut().poll(&mut Context::from_waker(Waker::noop()))
        else {
            panic!("{what}: the wait lost its published stop after the exit wait's poll");
        };
        let (next, stop) = output.unwrap().assume_stopped();
        assert_eq!(stop, crate::Event::Syscall, "{what}");
        if to_exit {
            assert_eq!(
                next.getsyscallinfo().unwrap().operation,
                crate::SyscallInfoOp::Exit {
                    return_value: 1,
                    is_error: false
                },
                "{what}: not the read's exit"
            );
        } else {
            assert!(is_read_entry(&next), "{what}: not the next read entry");
        }
        status_reads += PTRACER_ATTACHMENT_STATUS_READS.with(Cell::get) - reads_before;
        child = next;
        wait_for_fresh_held_wait(&event, held, &what, || {});
    }
    let steps = LegacyWakeCounts::of(&event).since(start);
    let exit_wakes = event.legacy_deferred_exit_wakes.load(Ordering::Relaxed) - exit_wakes_start;
    let status_wakes =
        event.legacy_deferred_status_wakes.load(Ordering::Relaxed) - status_wakes_start;
    println!(
        "{DEFERRED_WAKE_REPORT}stop found at first poll with a parked exit wait, {STEPS} steps: \
         {steps:?}, deferral exit wakes {exit_wakes}, request wakes {request_wakes}, \
         request off-thread {request_off_thread}, exit checks {exit_checks}, \
         status reads {status_reads}, deferral status wakes {status_wakes}"
    );
    // The same exit wait gets the exit stop of a group SIGKILL.
    terminal.request_sigkill().unwrap();
    drop(child);
    let child = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, exit)
        .await
        .unwrap()
        .unwrap();
    child_cleanup.mark_claimed_exit();
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            child.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            child_tid,
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(terminal.wait(TRACEE_WAIT_TIMEOUT));
    child_cleanup.disarm();
    let parent = tokio::time::timeout(TRACEE_WAIT_TIMEOUT, root_exit)
        .await
        .unwrap()
        .unwrap();
    root_cleanup.mark_claimed_exit();
    drop(parent_running);
    assert_eq!(
        tokio::time::timeout(
            TRACEE_WAIT_TIMEOUT,
            parent.resume(None).unwrap().wait_owned_on_ptracer_thread()
        )
        .await
        .unwrap()
        .unwrap()
        .assume_exited(),
        (
            root.into(),
            crate::ExitStatus::Signaled(Signal::SIGKILL, false)
        ),
    );
    assert!(root_terminal.wait(TRACEE_WAIT_TIMEOUT));
    root_cleanup.disarm();
    assert_eq!(
        steps,
        LegacyWakeCounts {
            turns: STEPS,
            hints: 0,
            notify_wakes: STEPS,
            flushes: 0,
            expiry_finds: 0,
        },
        "with a parked exit wait, a stop found at the first poll did not cost exactly one \
         worker wake and one turn"
    );
    assert_eq!(
        exit_checks, STEPS,
        "not every step polled the exit wait with its ordinary stop published and unclaimed"
    );
    assert_eq!(
        request_off_thread, 0,
        "a request's wake of the exit wait came from another thread"
    );
}

/// The outcome line of
/// `forced_legacy_wait_parked_before_a_rebuilt_controllers_request_gets_its_stop`.
#[cfg(not(sanitized))]
const PARKED_STATUS_WAIT_MARKER: &str =
    "wait parked before a rebuilt controller's request: woken and ready within the bound";

/// What `forced_legacy_wait_parked_before_a_rebuilt_controllers_request_gets_its_stop`
/// saw in its round, for the verdict after the teardown.
#[cfg(not(sanitized))]
#[derive(Debug)]
struct ParkedStatusWaitRound {
    latency: Option<Duration>,
    woken: usize,
    request_wakes: usize,
    request_off_thread: usize,
    status_wakes: usize,
    flushed: usize,
    expiry_finds: usize,
}

// An ordinary wait already parked before a request that another capability
// for the same generation makes still gets its stop.
// `TraceeGeneration::assume_stopped` rebuilds a controller for a tracee the
// caller knows is stopped, while the original capability waits. On the
// pipe-reading non-leader at a read entry, with its worker parked held
// (period 60 s), the controller is rebuilt, and the original capability is
// moved into its wait, which is polled once with a counting waker: Pending,
// unwoken. Only then does the rebuilt controller resume the thread into its
// read, with its Running kept, no exit waiter, and no other wait or assist.
// The wait is polled only when its waker is woken: for the request's wake,
// before the byte (that poll's empty assist sends the deferred notify), then
// after the byte. The read's exit must be ready within 50 ms of the write,
// with no stop found through a held wait's timeout. The request's deferral
// wakes the parked wait, from the ptracer thread.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_wait_parked_before_a_rebuilt_controllers_request_gets_its_stop() {
    if run_legacy_test_outer_with_outcome(
        "forced_legacy_wait_parked_before_a_rebuilt_controllers_request_gets_its_stop",
        Some(PARKED_STATUS_WAIT_MARKER),
    ) {
        return;
    }
    let seen_round = Rc::new(std::cell::RefCell::new(None));
    with_deferred_wake_pipe_thread(1, |_round, entry, event, write_end, _root| {
        let seen_round = Rc::clone(&seen_round);
        async move {
            let controller = entry.generation().assume_stopped();
            let mut wait = std::pin::pin!(entry.wait_owned_on_ptracer_thread());
            let counter = ThreadWakeCounter::new();
            let waker = Waker::from(Arc::clone(&counter));
            let mut cx = Context::from_waker(&waker);
            assert!(
                wait.as_mut().poll(&mut cx).is_pending(),
                "the wait returned before the request"
            );
            assert_eq!(
                counter.wakes(),
                0,
                "the parked wait was woken before the request"
            );
            let status_wakes_before = event.legacy_deferred_status_wakes.load(Ordering::Relaxed);
            let flushes_before = event.legacy_deferred_flushes.load(Ordering::Relaxed);
            let running = controller.syscall(None).unwrap();
            let status_wakes =
                event.legacy_deferred_status_wakes.load(Ordering::Relaxed) - status_wakes_before;
            let (request_wakes, request_off_thread) = (counter.wakes(), counter.off_thread());
            // The woken task polls the wait, before the byte: Pending, and
            // its empty assist sends the deferred notify.
            let mut seen = counter.wakes();
            if seen > 0 {
                assert!(
                    wait.as_mut().poll(&mut cx).is_pending(),
                    "the read returned before its byte"
                );
            }
            let flushed = event.legacy_deferred_flushes.load(Ordering::Relaxed) - flushes_before;
            let written = Instant::now();
            deferred_wake_write(&write_end);
            let deadline = written + DEFERRED_WAKE_BOUND;
            let mut ready = None;
            while ready.is_none() && Instant::now() < deadline {
                if counter.wakes() == seen {
                    thread::sleep(Duration::from_micros(100));
                    continue;
                }
                seen = counter.wakes();
                if let Poll::Ready(output) = wait.as_mut().poll(&mut cx) {
                    ready = Some((output, written.elapsed()));
                }
            }
            let woken = counter.wakes();
            let (output, latency) = match ready {
                Some((output, latency)) => (output, Some(latency)),
                None => {
                    // Recover the stop, which stays in the kernel, so that
                    // the group is torn down cleanly before the verdict.
                    let deadline = Instant::now() + Duration::from_secs(2);
                    let output = loop {
                        if let Poll::Ready(output) = wait.as_mut().poll(&mut cx) {
                            break output;
                        }
                        assert!(
                            Instant::now() < deadline,
                            "the read's exit was not recovered"
                        );
                        thread::sleep(Duration::from_micros(100));
                    };
                    (output, None)
                }
            };
            drop(running);
            let (exit, stop) = output.unwrap().assume_stopped();
            assert_eq!(stop, crate::Event::Syscall);
            assert_eq!(
                exit.getsyscallinfo().unwrap().operation,
                crate::SyscallInfoOp::Exit {
                    return_value: 1,
                    is_error: false
                },
                "not the read's exit"
            );
            *seen_round.borrow_mut() = Some(ParkedStatusWaitRound {
                latency,
                woken,
                request_wakes,
                request_off_thread,
                status_wakes,
                flushed,
                expiry_finds: event.legacy_held_expiry_finds.load(Ordering::Relaxed),
            });
            deferred_wake_next_read_entry(exit, &event).await
        }
    })
    .await;
    let round = seen_round.borrow_mut().take().unwrap();
    println!("wait parked before a rebuilt controller's request: {round:?}");
    assert!(
        round
            .latency
            .is_some_and(|latency| latency < DEFERRED_WAKE_BOUND),
        "the wait parked before the rebuilt controller's request was not ready within 50 ms \
         of the byte (woken {} times, write to ready {:?})",
        round.woken,
        round.latency,
    );
    assert_eq!(
        round.expiry_finds, 0,
        "the worker found the read's exit only after a held wait's timeout"
    );
    assert_eq!(
        round.status_wakes, 1,
        "the request's deferral did not wake the parked wait"
    );
    assert!(
        round.request_wakes >= 1,
        "the request did not wake the parked wait's waker"
    );
    assert_eq!(
        round.request_off_thread, 0,
        "the request's wake of the parked wait came from another thread"
    );
    assert_eq!(
        round.flushed, 1,
        "the woken poll before the byte did not send the deferred notify"
    );
    emit_completion_marker(PARKED_STATUS_WAIT_MARKER);
}

/// The outcome line of
/// `forced_legacy_counted_wake_check_refuses_extra_notifies_to_a_parked_worker`.
#[cfg(not(sanitized))]
const EXTRA_NOTIFY_MARKER: &str = "counted-wake check: the real round passed, and each extra notify to the parked worker was refused";

/// What a delivered extra notify was, how many notifies the worker counted
/// for it, and the counted-wake check of the round after it.
#[cfg(not(sanitized))]
type ExtraNotifyVerdict = (Option<LegacyNotifySource>, usize, DeferredWakeCheck);

// The counted-wake check refuses an extra notify that reaches the parked
// worker. One parked async round, as in
// `forced_legacy_deferred_wake_reaches_an_async_wait`, must pass the check.
// Then three notifies are delivered to the worker, each while it is proven
// parked in its held wait: after the check's quiescence, the test takes
// `legacy_wakeup`, which the worker releases only inside `wait_for`. Each
// carries a source that no real notify of that round can: a consume reusing
// the receipt of the round's own deferral (already used by its flush), a
// consume with no receipt, and no source. After each, the check of the same
// round must fail, naming that notify.
#[tokio::test(flavor = "current_thread")]
#[cfg(not(sanitized))]
async fn forced_legacy_counted_wake_check_refuses_extra_notifies_to_a_parked_worker() {
    if run_legacy_test_outer_with_outcome(
        "forced_legacy_counted_wake_check_refuses_extra_notifies_to_a_parked_worker",
        Some(EXTRA_NOTIFY_MARKER),
    ) {
        return;
    }
    let verdicts = Rc::new(std::cell::RefCell::new(None));
    with_deferred_wake_pipe_thread(1, |round, entry, event, write_end, _root| {
        let verdicts = Rc::clone(&verdicts);
        async move {
            let what = format!("round {round}");
            let (running, counted) =
                deferred_wake_counted_request(&event, || entry.syscall(None).unwrap());
            let mut wait = std::pin::pin!(running.wait_owned_on_ptracer_thread());
            assert!(
                wait.as_mut()
                    .poll(&mut Context::from_waker(Waker::noop()))
                    .is_pending(),
                "{what}: the read returned before its byte"
            );
            let (output, _latency) = deferred_wake_parked_wait(wait.as_mut(), &write_end, &what);
            let real = counted.check(&event, &what);
            let flush_receipt = event.legacy_wake_log.lock()[counted.log_start..]
                .iter()
                .find_map(|entry| match entry {
                    LegacyWakeLogEntry::Notified(LegacyNotifySource::Flush(receipt)) => *receipt,
                    _ => None,
                });
            let mut injected: Vec<ExtraNotifyVerdict> = Vec::new();
            for source in [
                flush_receipt.map(|receipt| LegacyNotifySource::Consume(Some(receipt))),
                Some(LegacyNotifySource::Consume(None)),
                None,
            ] {
                let delivered = event.legacy_notify_wakes.load(Ordering::Relaxed);
                {
                    // The last check left the worker quiescent in its held
                    // wait. It holds this lock until `wait_for` parks it.
                    let _parked = event.legacy_wakeup.lock();
                    if let Some(source) = source {
                        note_legacy_notify_source(source);
                    }
                    event.notify_legacy_worker();
                }
                let delivered = event.legacy_notify_wakes.load(Ordering::Relaxed) - delivered;
                injected.push((source, delivered, counted.check(&event, &what)));
            }
            *verdicts.borrow_mut() = Some((real, flush_receipt, injected));
            let (exit, stop) = output.unwrap().assume_stopped();
            assert_eq!(stop, crate::Event::Syscall);
            deferred_wake_next_read_entry(exit, &event).await
        }
    })
    .await;
    let (real, flush_receipt, injected) = verdicts.borrow_mut().take().unwrap();
    assert!(
        real.0.is_none(),
        "the real round failed the counted-wake check: {:?}",
        real.0
    );
    let flush_receipt = flush_receipt.expect("the round's flush carried no receipt");
    let reasons = [
        format!("receipt {} used by a second notify", flush_receipt.serial),
        "a consume (no receipt) made no count".to_owned(),
        "ended by a notify that named no source".to_owned(),
    ];
    for ((source, delivered, check), reason) in injected.iter().zip(&reasons) {
        assert_eq!(
            *delivered, 1,
            "the extra notify {source:?} did not reach the parked worker"
        );
        let lacks = check.0.as_deref().unwrap_or("");
        assert!(
            lacks.contains(reason.as_str()),
            "the check did not refuse the extra notify {source:?} with {reason:?}: {lacks}"
        );
    }
    println!("extra notifies to a parked worker: {injected:?}");
    emit_completion_marker(EXTRA_NOTIFY_MARKER);
}