Skip to content

Conversation

@overlookmotel
Copy link
Member

@overlookmotel overlookmotel commented Aug 14, 2025

Allocation tracker did not deal with possibility that counters could reach usize::MAX. In this case, addition would wrap around back to 0.

This problem was pointed out by @aapoalas in #13043 (review).

Introduce an AtomicCounter type which uses saturating addition, to prevent this happening.

Reaching such a high allocation count is highly unlikely on 64-bit systems, but might just about be possible on 32-bit.

Maybe this is overkill, but since cargo allocs is just an internal tool, performance is not the highest priority. So I figure on balance probably better to handle this edge case. It doesn't seem to hurt perf much.

# Before
% hyperfine -i --warmup 3 'cargo allocs'
Benchmark 1: cargo allocs
  Time (mean ± σ):     299.9 ms ±   2.4 ms    [User: 132.8 ms, System: 31.0 ms]
  Range (min … max):   297.6 ms … 303.9 ms    10 runs

# After
% hyperfine -i --warmup 3 'cargo allocs'
Benchmark 1: cargo allocs
  Time (mean ± σ):     301.3 ms ±   3.4 ms    [User: 133.3 ms, System: 31.5 ms]
  Range (min … max):   297.8 ms … 310.0 ms    10 runs

Note: The same problem in arena allocation tracker is addressed in #13043.

@github-actions github-actions bot added the C-bug Category - Bug label Aug 14, 2025
Copy link
Member Author


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@overlookmotel overlookmotel marked this pull request as ready for review August 14, 2025 10:01
@overlookmotel
Copy link
Member Author

@camchenry If you think this is ridiculous overkill, feel free to close this.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses a potential counter overflow issue in the allocation tracker by introducing an AtomicCounter type that uses saturating addition to prevent wrapping when counters reach usize::MAX. While this scenario is highly unlikely on 64-bit systems, it could potentially occur on 32-bit systems.

  • Introduces a new AtomicCounter wrapper type with saturating arithmetic
  • Replaces direct AtomicUsize usage with the new counter type
  • Updates all allocation tracking calls to use the safer increment method

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@camchenry
Copy link
Member

@overlookmotel I think it's a fair fix. Performance for this task isn't a high priority and it's fast enough for now.

@camchenry camchenry added the 0-merge Merge with Graphite Merge Queue label Aug 14, 2025
Copy link
Member

camchenry commented Aug 14, 2025

Merge activity

Allocation tracker did not deal with possibility that counters could reach `usize::MAX`. In this case, addition would wrap around back to 0.

This problem was pointed out by @aapoalas in #13043 (review).

Introduce an `AtomicCounter` type which uses saturating addition, to prevent this happening.

Reaching such a high allocation count is highly unlikely on 64-bit systems, but might just about be possible on 32-bit.

Maybe this is overkill, but since `cargo allocs` is just an internal tool, performance is not the highest priority. So I figure on balance probably better to handle this edge case. It doesn't seem to hurt perf much.

```
# Before
% hyperfine -i --warmup 3 'cargo allocs'
Benchmark 1: cargo allocs
  Time (mean ± σ):     299.9 ms ±   2.4 ms    [User: 132.8 ms, System: 31.0 ms]
  Range (min … max):   297.6 ms … 303.9 ms    10 runs

# After
% hyperfine -i --warmup 3 'cargo allocs'
Benchmark 1: cargo allocs
  Time (mean ± σ):     301.3 ms ±   3.4 ms    [User: 133.3 ms, System: 31.5 ms]
  Range (min … max):   297.8 ms … 310.0 ms    10 runs
```

Note: The same problem in arena allocation tracker is addressed in #13043.
@graphite-app graphite-app bot force-pushed the 08-14-fix_tasks_allocation_tracker_prevent_counter_overflow branch from 254a7cf to 8c141e5 Compare August 14, 2025 14:59
@graphite-app graphite-app bot merged commit 8c141e5 into main Aug 14, 2025
17 checks passed
@graphite-app graphite-app bot deleted the 08-14-fix_tasks_allocation_tracker_prevent_counter_overflow branch August 14, 2025 15:04
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Aug 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants