Skip to content

Fix a subtraction overflow in get_free_pages. #1261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/plan/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ pub trait Plan: 'static + HasSpaces + Sync + Downcast {
// the reserved pages is larger than total pages after the copying GC (the reserved pages after a GC
// may be larger than the reserved pages before a GC, as we may end up using more memory for thread local
// buffers for copy allocators).
// 3. the binding disabled GC, and we end up over-allocating beyond the total pages determined by the GC trigger.
let available_pages = total_pages.saturating_sub(reserved_pages);
trace!(
"Total pages = {}, reserved pages = {}, available pages = {}",
Expand All @@ -289,7 +290,12 @@ pub trait Plan: 'static + HasSpaces + Sync + Downcast {
/// Get the number of pages that are NOT used. This is clearly different from available pages.
/// Free pages are unused, but some of them may have been reserved for some reason.
fn get_free_pages(&self) -> usize {
self.get_total_pages() - self.get_used_pages()
let total_pages = self.get_total_pages();
let used_pages = self.get_used_pages();

// It is possible that the used pages is larger than the total pages, so we use saturating
// subtraction. See the comments in `get_available_pages`.
total_pages.saturating_sub(used_pages)
}

/// Return whether last GC was an exhaustive attempt to collect the heap.
Expand Down
Loading