Conversation
Signed-off-by: mrhapile <[email protected]>
There was a problem hiding this comment.
Pull request overview
This PR hardens the mark-sweep GC’s root tracking by preventing GcHeader::inc_roots from silently overflowing a u16 root counter, which could otherwise violate the “root_count == 0 implies collectible” invariant.
Changes:
- Update
GcHeader::inc_rootsto use checked arithmetic and panic on overflow instead of wrapping. - Add a regression test that asserts overflow triggers a panic.
- Add a conditional
extern crate std;to the mark-sweep test module.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
oscars/src/collectors/mark_sweep/internals/gc_header.rs |
Switches inc_roots to checked increment and adds a panic regression test for overflow. |
oscars/src/collectors/mark_sweep/tests.rs |
Adds conditional extern crate std; at the top of the mark-sweep tests module. |
Comments suppressed due to low confidence (1)
oscars/src/collectors/mark_sweep/internals/gc_header.rs:115
inc_rootsnow panics on overflow, butdec_rootsstill usessaturating_sub(1), which can silently mask an underflow (extra decrements) and also break theroot_count == 0 => collectibleinvariant. Consider switchingdec_rootstochecked_sub(1)(or anassert!/expect) and adding a small regression test for underflow, so root accounting bugs fail loudly in both directions.
self.root_count.set(new);
}
pub fn dec_roots(&self) {
// NOTE: if we are underflowing on subtraction, something is seriously wrong
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| #[cfg(feature = "std")] | ||
| extern crate std; | ||
|
|
There was a problem hiding this comment.
extern crate std; is gated on feature = "std", but this test module doesn’t use std anywhere and the crate already conditionally links std in src/lib.rs. Consider removing this line, or (if it’s needed to make cargo test work for a #![no_std] crate) gating it with cfg(test) instead so tests don’t depend on enabling the std feature explicitly.
| #[cfg(feature = "std")] | |
| extern crate std; |
Summary
This PR prevents a potential overflow in
GcHeader::inc_roots.root_countis currently stored asCell<u16>. The previous implementation incremented the counter using unchecked arithmetic:If more than
u16::MAXroots were created:0Silent wraparound violates the garbage collector invariant that:
If the counter wraps from
65535 → 0, a live object could incorrectly appear unreferenced and become eligible for collection.Changes
checked_addinc_rootsUpdated implementation:
Tests
Added a regression test ensuring overflow is detected:
All existing tests pass successfully.

Impact
u16storage layoutno_stdcompatibility