Commit f56a0b3
committed
Fix GC use-after-free via atomic try_to_owned
The GC's strong reference creation had a TOCTOU race: between
checking strong_count() > 0 and calling to_owned() (which calls
inc()), another thread could dec() the count to 0 and proceed
with deallocation. For objects without __del__, no resurrection
check occurs, so the memory is freed while GC holds a dangling
reference. The subsequent drop accesses freed memory, corrupting
malloc metadata (malloc(): unaligned tcache chunk detected).
Fix by replacing the check-then-act pattern with CAS-based
try_to_owned()/safe_inc() that atomically verifies count > 0
and increments. Apply the same fix to WeakRefList callback
collection. Also add atomic dict snapshot for list(dict) to
prevent RuntimeError during concurrent dict iteration.
Unskip test_thread_safety which now passes reliably.1 parent 62766fd commit f56a0b3
File tree
5 files changed
+40
-28
lines changed- Lib/test
- crates
- common/src
- vm/src
- object
- vm
5 files changed
+40
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4815 | 4815 | | |
4816 | 4816 | | |
4817 | 4817 | | |
4818 | | - | |
4819 | | - | |
4820 | 4818 | | |
4821 | 4819 | | |
4822 | 4820 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
| 126 | + | |
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
| 302 | + | |
309 | 303 | | |
310 | 304 | | |
311 | 305 | | |
| |||
468 | 462 | | |
469 | 463 | | |
470 | 464 | | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
471 | 470 | | |
472 | 471 | | |
473 | 472 | | |
474 | 473 | | |
475 | | - | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
| 474 | + | |
480 | 475 | | |
481 | 476 | | |
482 | 477 | | |
483 | 478 | | |
484 | 479 | | |
485 | 480 | | |
486 | 481 | | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
| 482 | + | |
492 | 483 | | |
493 | 484 | | |
494 | 485 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
576 | 576 | | |
577 | 577 | | |
578 | 578 | | |
579 | | - | |
580 | | - | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
581 | 582 | | |
582 | 583 | | |
583 | | - | |
| 584 | + | |
584 | 585 | | |
585 | 586 | | |
586 | 587 | | |
| |||
626 | 627 | | |
627 | 628 | | |
628 | 629 | | |
629 | | - | |
630 | | - | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
631 | 633 | | |
632 | 634 | | |
633 | | - | |
| 635 | + | |
634 | 636 | | |
635 | 637 | | |
636 | 638 | | |
| |||
660 | 662 | | |
661 | 663 | | |
662 | 664 | | |
663 | | - | |
664 | | - | |
| 665 | + | |
| 666 | + | |
665 | 667 | | |
666 | 668 | | |
667 | 669 | | |
| |||
952 | 954 | | |
953 | 955 | | |
954 | 956 | | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
955 | 974 | | |
956 | 975 | | |
957 | 976 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1319 | 1319 | | |
1320 | 1320 | | |
1321 | 1321 | | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
1322 | 1326 | | |
1323 | 1327 | | |
1324 | 1328 | | |
| |||
0 commit comments