Skip to content

Fix underflow in range_to_pow_2_blocks - #909

Open
LI SHANGXI (WinnieSmasher) wants to merge 1 commit into
microsoft:mainfrom
WinnieSmasher:fix/range-to-pow-2-blocks-underflow
Open

LI SHANGXI (WinnieSmasher) wants to merge 1 commit into
microsoft:mainfrom
WinnieSmasher:fix/range-to-pow-2-blocks-underflow

Conversation

@WinnieSmasher

Copy link
Copy Markdown

The aligned range can be empty if length is smaller than a minimum-sized block, or if the range's ends round towards each other. pointer_diff() then underflows size_t, and the loop below calls f with ever-increasing block sizes that walk past the end of the input range. On CHERI this manifests as capability length violations (see #567).

Return early when there is no aligned range left. The check is performed before pointer_diff(), so it also avoids tripping that function's cursor >= base assertion in debug builds.

Added func-range_helpers to sweep unaligned offsets and lengths (including zero and sub-minimum-block lengths). It checks that every emitted block lies within the input range, is power-of-two sized, naturally aligned and contiguous, and that the total size matches the aligned range. The test fails without the fix (for example at offset=1 length=0).

Verified locally with GCC 15.2 on Windows in both Debug and Release; func-range_helpers-fast, func-range_helpers-check, func-bits, func-pagemap, func-sizeclass, func-fixed_region, func-fixed_region_alloc and func-malloc all pass.

Fixes #567.

The aligned range can be empty if the input is smaller than the minimum block size, or if its ends round towards each other.  In that case pointer_diff() underflows size_t and the loop emits increasingly large blocks beyond the end of the range.

Return early when there is no aligned range left, and add a unit test covering small and unaligned ranges.

Fixes microsoft#567.

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.

🟢 Approval recommended

There are no active review issues for the range-length underflow fix.

No open issues 🎉

What changed in this PR

Fixes the range-length underflow reported in #567 by stopping block generation when alignment leaves an empty range.

Changes:

  • Returns before pointer_diff() when aligned endpoints meet or cross, preventing underflow and out-of-range block generation.
  • Adds regression tests covering unaligned, zero-length, and sub-minimum-block ranges, checking block bounds, alignment, contiguity, and total size.
File Description
src/​test/​func/​range_helpers/​range_helpers.cc Adds an offset-and-length sweep validating emitted blocks.
src/​snmalloc/​backend_helpers/​range_helpers.h Guards empty aligned ranges before calculating their length.

Review effort: Balanced

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.

range_to_pow_2_blocks can underflow length (size_t)

2 participants