Skip to content

keep RegionAllocator's initial region inside the block - #9728

Open
Nashit-h wants to merge 1 commit into
halide:mainfrom
Nashit-h:region-allocator-block-bounds
Open

Nashit-h wants to merge 1 commit into
halide:mainfrom
Nashit-h:region-allocator-block-bounds

Conversation

@Nashit-h

Copy link
Copy Markdown
Contributor

RegionAllocator::initialize builds the block's one initial region by passing the block size through create_block_region, which runs it through conform(). conform() only ever rounds a request up (alignment padding, nearest_multiple rounding, and conform_size's floor of one alignment), and the block size is settled by BlockAllocator whose minimum_block_size/maximum_block_size clamps run after its own nearest_multiple rounding, so the block size does not have to satisfy the region constraints. The initial region can therefore describe more memory than the block owns. Every other region is split out of that one, so the tail of the last region sits outside the block allocation, and a request whose conformed size exactly matches that tail region is served without being split, handing the caller a MemoryRegion that runs past the end of the block. The block->reserved check in reserve() does not catch it, because it only sums the sizes of the regions in use and never looks at their offsets.

Reproduced in the existing runtime_internal_block_allocator harness with the default conform path (no conform callbacks): a 500-byte block with nearest_multiple 8 gets an initial region of 504 bytes, and reserve(100), reserve(100), release of the first, then reserve(296) returns a region at offset 208 with size 296, 4 bytes past the end of the block. Writing that region through the block handle is a heap-buffer-overflow under ASan (WRITE of size 1 ... 0 bytes after 500-byte region [..., ...)). The Vulkan backend is the only user of this allocator, and it sets the block size from HL_VK_ALLOC_CONFIG / halide_vulkan_set_allocation_config together with the size and alignment the driver reports for a buffer, so a block size that the region constraints then round up is not something the caller can rule out.

The initial region now spans exactly the block. Regions are only split or coalesced after that and both preserve containment, so this restores the invariant that every region lies inside its block.

Risk/edge case worth flagging: in a configuration where the block size cannot be subdivided under the region constraints, the block is now handed out as a single region rather than being split. That is the same outcome as the existing behaviour when conforming a split's free tail would overrun the block, and it only applies to configurations that previously produced an out-of-bounds region. Block sizes that are already a multiple of the region alignment and nearest_multiple, which is the normal Vulkan case, are unaffected.

Testing: added a regression case to test/runtime/block_allocator.cpp that checks the initial region spans the block and that a reserved region stays inside it. It fails on the unpatched tree (block_resource.regions->memory.allocation.size == block_resource.memory.size) and passes with the change. All of test/runtime builds and passes under ASan and UBSan, and a randomized reserve/retain/release/reclaim/collect stress run over 4000 allocator configurations stays free of overlapping or out-of-block regions with the fix (it hits them without it). clang-format and the CMake style and file-list checks are clean.

Checklist

  • Tests added or updated (not required for docs, CI config, or typo fixes)
  • Documentation updated (if public API changed)
  • Python bindings updated (if public API changed)
  • Benchmarks are included here if the change is intended to affect performance.
  • Commits include AI attribution where applicable (see Code of Conduct)

This branch has not been deployed

No deployments
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.

1 participant