Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
RegionAllocator::initializebuilds the block's one initial region by passing the block size throughcreate_block_region, which runs it throughconform().conform()only ever rounds a request up (alignment padding,nearest_multiplerounding, andconform_size's floor of one alignment), and the block size is settled byBlockAllocatorwhoseminimum_block_size/maximum_block_sizeclamps run after its ownnearest_multiplerounding, 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 aMemoryRegionthat runs past the end of the block. Theblock->reservedcheck inreserve()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_allocatorharness with the default conform path (noconformcallbacks): a 500-byte block withnearest_multiple8 gets an initial region of 504 bytes, andreserve(100),reserve(100),releaseof the first, thenreserve(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 fromHL_VK_ALLOC_CONFIG/halide_vulkan_set_allocation_configtogether 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.cppthat 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 oftest/runtimebuilds 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-formatand the CMake style and file-list checks are clean.Checklist