Skip to content

Commit

Permalink
Fix for out-of-bound accessing last free block in EXTENSIVE defragmen…
Browse files Browse the repository at this point in the history
…tation algorithm.

Code by @medranSolus
See #232
  • Loading branch information
adam-sawicki-a committed Mar 28, 2022
1 parent 228a1b6 commit 35e1539
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions include/vk_mem_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13289,7 +13289,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo
VMA_SWAP(vector->m_Blocks[i], vector->m_Blocks[vector->GetBlockCount() - ++m_ImmovableBlockCount]);
if (state.firstFreeBlock != SIZE_MAX)
{
if (i < state.firstFreeBlock - 1)
if (i + 1 < state.firstFreeBlock)
{
if (state.firstFreeBlock > 1)
VMA_SWAP(vector->m_Blocks[i], vector->m_Blocks[--state.firstFreeBlock]);
Expand Down Expand Up @@ -13709,6 +13709,13 @@ bool VmaDefragmentationContext_T::ComputeDefragmentation_Extensive(VmaBlockVecto
case StateExtensive::Operation::FindFreeBlockTexture:
case StateExtensive::Operation::FindFreeBlockAll:
{
// No more blocks to free, just perform fast realloc and move to cleanup
if (vectorState.firstFreeBlock == 0)
{
vectorState.operation = StateExtensive::Operation::Cleanup;
return ComputeDefragmentation_Fast(vector);
}

// No free blocks, have to clear last one
size_t last = (vectorState.firstFreeBlock == SIZE_MAX ? vector.GetBlockCount() : vectorState.firstFreeBlock) - 1;
VmaBlockMetadata* freeMetadata = vector.GetBlock(last)->m_pMetadata;
Expand Down Expand Up @@ -13777,8 +13784,7 @@ bool VmaDefragmentationContext_T::ComputeDefragmentation_Extensive(VmaBlockVecto
}
vectorState.firstFreeBlock = last;
// Nothing done, block found without reallocations, can perform another reallocs in same pass
if (prevMoveCount == m_Moves.size())
return ComputeDefragmentation_Extensive(vector, index);
return ComputeDefragmentation_Extensive(vector, index);
}
break;
}
Expand Down

0 comments on commit 35e1539

Please sign in to comment.