| @@ -400,7 +400,7 @@ pub(crate) unsafe fn dup_value(v: Value) -> Value { |
| 400 | 400 | // SAFETY: caller guarantees `v` is live. |
| 401 | 401 | unsafe { |
| 402 | 402 | let rc = (*b).header.rc; |
| 403 | | − if rc > 0 { |
| 403 | + if (1..RC_STUCK).contains(&rc) { |
| 404 | 404 | (*b).header.rc = rc + 1; |
| 405 | 405 | } else { |
| 406 | 406 | tl_dup_shared(b); |
| @@ -409,6 +409,30 @@ pub(crate) unsafe fn dup_value(v: Value) -> Value { |
| 409 | 409 | v |
| 410 | 410 | } |
| 411 | 411 | |
| 412 | +/// A local count stops here instead of overflowing (issue #217): no dup |
| 413 | +/// or drop moves it again, and the block is never freed. A bounded leak |
| 414 | +/// replaces a use after free. |
| 415 | +pub(crate) const RC_STUCK: i32 = i32::MAX; |
| 416 | + |
| 417 | +/// A shared count at or past this is saturated: every dup or drop that |
| 418 | +/// sees it puts it back to [`SHARED_STUCK`]. The gap between the two |
| 419 | +/// absorbs the dups and drops other threads have in flight, so the count |
| 420 | +/// never reaches -1 again, nor wraps, without a compare-and-swap loop. |
| 421 | +pub(crate) const SHARED_SATURATED: i32 = -(1 << 29); |
| 422 | + |
| 423 | +/// Where a saturated shared count is held. |
| 424 | +pub(crate) const SHARED_STUCK: i32 = -(1 << 30); |
| 425 | + |
| 426 | +/// Say once that a count saturated: the program now leaks that block. |
| 427 | +fn saturated() { |
| 428 | + static SAID: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); |
| 429 | + if !SAID.swap(true, Ordering::Relaxed) { |
| 430 | + eprintln!( |
| 431 | + "louise: a value has too many references to count; it will never be freed (issue #217)" |
| 432 | + ); |
| 433 | + } |
| 434 | +} |
| 435 | + |
| 412 | 436 | /// Allocate a block with `rc = 1`. Scanned fields are initialised to unit so |
| 413 | 437 | /// a block dropped before the caller fills it is still well formed. |
| 414 | 438 | /// |
| @@ -443,7 +467,8 @@ pub extern "C" fn tl_alloc( |
| 443 | 467 | b |
| 444 | 468 | } |
| 445 | 469 | |
| 446 | | −/// Increment the count of a shared (negative rc) block. |
| 470 | +/// Increment the count of a shared (negative rc) block, or of one whose |
| 471 | +/// count saturated, which stays as it is. |
| 447 | 472 | /// |
| 448 | 473 | /// # Safety |
| 449 | 474 | /// `b` must point to a live block. |
| @@ -451,12 +476,20 @@ pub extern "C" fn tl_alloc( |
| 451 | 476 | pub unsafe extern "C" fn tl_dup_shared(b: *mut Block) { |
| 452 | 477 | // SAFETY: caller guarantees `b` is live. Immortal blocks (rc 0, static |
| 453 | 478 | // literals) are never counted. |
| 454 | | − if unsafe { (*b).header.rc } == 0 { |
| 479 | + let local = unsafe { (*b).header.rc }; |
| 480 | + if local == 0 { |
| 481 | + return; |
| 482 | + } |
| 483 | + if local == RC_STUCK { |
| 484 | + saturated(); |
| 455 | 485 | return; |
| 456 | 486 | } |
| 457 | 487 | // SAFETY: shared blocks are only ever accessed atomically. |
| 458 | 488 | let rc = unsafe { AtomicI32::from_ptr(&raw mut (*b).header.rc) }; |
| 459 | | − rc.fetch_sub(1, Ordering::Relaxed); |
| 489 | + if rc.fetch_sub(1, Ordering::Relaxed) <= SHARED_SATURATED { |
| 490 | + rc.store(SHARED_STUCK, Ordering::Relaxed); |
| 491 | + saturated(); |
| 492 | + } |
| 460 | 493 | } |
| 461 | 494 | |
| 462 | 495 | /// Returns true when this call released the last reference to `b`. |
| @@ -466,8 +499,8 @@ pub unsafe extern "C" fn tl_dup_shared(b: *mut Block) { |
| 466 | 499 | unsafe fn release(b: *mut Block) -> bool { |
| 467 | 500 | // SAFETY: caller guarantees `b` is live. |
| 468 | 501 | let local = unsafe { (*b).header.rc }; |
| 469 | | − if local == 0 { |
| 470 | | − return false; // immortal (a static literal) |
| 502 | + if local == 0 || local == RC_STUCK { |
| 503 | + return false; // immortal (a static literal), or saturated |
| 471 | 504 | } |
| 472 | 505 | if local > 0 { |
| 473 | 506 | debug_assert_eq!(local, 1, "louise: release on a block with rc > 1"); |
| @@ -475,14 +508,19 @@ unsafe fn release(b: *mut Block) -> bool { |
| 475 | 508 | } |
| 476 | 509 | // SAFETY: shared blocks are only ever accessed atomically. |
| 477 | 510 | let rc = unsafe { AtomicI32::from_ptr(&raw mut (*b).header.rc) }; |
| 478 | | − if rc.fetch_add(1, Ordering::Release) == -1 { |
| 511 | + let old = rc.fetch_add(1, Ordering::Release); |
| 512 | + if old == -1 { |
| 479 | 513 | std::sync::atomic::fence(Ordering::Acquire); |
| 480 | 514 | return true; |
| 481 | 515 | } |
| 516 | + if old <= SHARED_SATURATED { |
| 517 | + rc.store(SHARED_STUCK, Ordering::Relaxed); |
| 518 | + } |
| 482 | 519 | false |
| 483 | 520 | } |
| 484 | 521 | |
| 485 | | −/// Drop a block whose local count is 1, or which is shared. |
| 522 | +/// Drop a block whose local count is 1, which is shared, or whose count |
| 523 | +/// saturated (left as it is). |
| 486 | 524 | /// |
| 487 | 525 | /// # Safety |
| 488 | 526 | /// `ctx` must be the running actor's context and `b` a live block. |
| @@ -597,7 +635,7 @@ pub unsafe fn drop_value(ctx: *mut Ctx, v: Value) { |
| 597 | 635 | // SAFETY: an owned pointer value refers to a live block. |
| 598 | 636 | unsafe { |
| 599 | 637 | let rc = (*b).header.rc; |
| 600 | | − if rc > 1 { |
| 638 | + if (2..RC_STUCK).contains(&rc) { |
| 601 | 639 | (*b).header.rc = rc - 1; |
| 602 | 640 | } else { |
| 603 | 641 | tl_drop_slow(ctx, b); |
| @@ -763,7 +801,7 @@ pub(crate) unsafe fn release_into(v: Value, pending: &mut Vec<*mut Block>) { |
| 763 | 801 | // SAFETY: caller guarantees `v` is live. |
| 764 | 802 | unsafe { |
| 765 | 803 | let rc = (*b).header.rc; |
| 766 | | − if rc > 1 { |
| 804 | + if (2..RC_STUCK).contains(&rc) { |
| 767 | 805 | (*b).header.rc = rc - 1; |
| 768 | 806 | } else if release(b) { |
| 769 | 807 | pending.push(b); |
| @@ -831,4 +869,48 @@ mod tests { |
| 831 | 869 | // SAFETY: `drops_in_steps` follows the generated-code convention. |
| 832 | 870 | assert_eq!(unsafe { crate::sched::run_test_actor(drops_in_steps) }, 0); |
| 833 | 871 | } |
| 872 | + |
| 873 | + /// A count that would overflow sticks instead (issue #217): no dup or |
| 874 | + /// drop moves it again, and the block is never freed. |
| 875 | + unsafe extern "C" fn saturating_counts(ctx: *mut Ctx) -> Value { |
| 876 | + // SAFETY: `ctx` is the root actor's; the blocks are owned here. |
| 877 | + unsafe { |
| 878 | + let local = string(ctx, "local"); |
| 879 | + let b = local as *mut Block; |
| 880 | + (*b).header.rc = RC_STUCK - 1; |
| 881 | + dup_value(local); |
| 882 | + assert_eq!((*b).header.rc, RC_STUCK); |
| 883 | + dup_value(local); |
| 884 | + for _ in 0..3 { |
| 885 | + drop_value(ctx, local); |
| 886 | + } |
| 887 | + assert_eq!((*b).header.rc, RC_STUCK); |
| 888 | + |
| 889 | + let shared = string(ctx, "shared"); |
| 890 | + let s = shared as *mut Block; |
| 891 | + (*s).header.rc = SHARED_SATURATED; |
| 892 | + dup_value(shared); |
| 893 | + assert_eq!((*s).header.rc, SHARED_STUCK); |
| 894 | + for _ in 0..1000 { |
| 895 | + drop_value(ctx, shared); |
| 896 | + } |
| 897 | + assert_eq!((*s).header.rc, SHARED_STUCK); |
| 898 | + |
| 899 | + // Back to one reference each, so the actor ends without leaks. |
| 900 | + (*b).header.rc = 1; |
| 901 | + drop_value(ctx, local); |
| 902 | + (*s).header.rc = -1; |
| 903 | + drop_value(ctx, shared); |
| 904 | + } |
| 905 | + UNIT |
| 906 | + } |
| 907 | + |
| 908 | + #[test] |
| 909 | + fn counts_saturate_instead_of_wrapping() { |
| 910 | + // SAFETY: `saturating_counts` follows the generated-code convention. |
| 911 | + assert_eq!( |
| 912 | + unsafe { crate::sched::run_test_actor(saturating_counts) }, |
| 913 | + 0 |
| 914 | + ); |
| 915 | + } |
| 834 | 916 | } |