Skip to content

Use checked rounding for BFC arena allocations - #32010

Merged
Akshay Sonawane (apsonawane) merged 1 commit into
mainfrom
fix/bfc-arena-rounded-bytes-overflow
Aug 13, 2026
Merged

Akshay Sonawane (apsonawane) merged 1 commit into
mainfrom
fix/bfc-arena-rounded-bytes-overflow

Conversation

@apsonawane

Copy link
Copy Markdown
Contributor

This pull request improves the safety and robustness of memory allocation in the BFCArena allocator by adding overflow protection and corresponding tests. The most important changes are:

Overflow Protection and Safety:

  • Updated the BFCArena::RoundedBytes function in bfc_arena.cc to use SafeInt<size_t> for arithmetic operations, preventing integer overflows during memory rounding calculations.
  • Included the safeint.h header to enable safe integer operations in bfc_arena.cc.

Testing:

  • Added a new test, RoundedBytesOverflowThrows, in bfc_arena_test.cc to verify that an overflow during allocation throws an OnnxRuntimeException.
  • Included the <limits> header in bfc_arena_test.cc to support boundary value tests.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 hardens BFCArena’s allocation-size rounding logic against integer overflow by switching the rounding arithmetic to SafeInt<size_t>, and adds a unit test to ensure overflow cases throw an OnnxRuntimeException instead of silently wrapping.

Changes:

  • Updated BFCArena::RoundedBytes to use SafeInt<size_t> for checked rounding arithmetic (preventing overflow on bytes + kMinAllocationSize - 1).
  • Added a regression test validating that an overflow-sized allocation request throws OnnxRuntimeException.
  • Added required headers to support the new checked arithmetic and boundary-value test.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
onnxruntime/core/framework/bfc_arena.cc Uses SafeInt<size_t> in BFCArena::RoundedBytes to make rounding overflow-safe.
onnxruntime/test/framework/bfc_arena_test.cc Adds RoundedBytesOverflowThrows to assert overflow requests throw OnnxRuntimeException.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@apsonawane
Akshay Sonawane (apsonawane) merged commit 782ac5e into main Aug 13, 2026
88 checks passed
@apsonawane
Akshay Sonawane (apsonawane) deleted the fix/bfc-arena-rounded-bytes-overflow branch August 13, 2026 17:53
This was referenced Sep 10, 2026
This was referenced Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants