Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes when vkAllocateMemory fails #5754

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,12 @@ VkBufferMemory* VkBlobAllocator::fastMalloc(size_t size)
}

block->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!block->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), block->buffer, 0);
delete block;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), block->buffer, block->memory, 0);
Expand Down Expand Up @@ -1032,6 +1038,12 @@ VkImageMemory* VkBlobAllocator::fastMalloc(int w, int h, int c, size_t elemsize,

// bind at memory offset
ptr->memory = allocate_memory(new_block_size, image_memory_type_index);
if (!ptr->memory)
{
vkDestroyImage(vkdev->vkdevice(), ptr->image, 0);
delete ptr;
return 0;
}
ptr->bind_offset = 0;
ptr->bind_capacity = aligned_size;

Expand Down Expand Up @@ -1341,6 +1353,12 @@ VkBufferMemory* VkWeightAllocator::fastMalloc(size_t size)
}

block->memory = allocate_dedicated_memory(memoryRequirements2.memoryRequirements.size, buffer_memory_type_index, 0, block->buffer);
if (!block->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), block->buffer, 0);
delete block;
return 0;
}

// ignore memoryRequirements2.memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), block->buffer, block->memory, 0);
Expand Down Expand Up @@ -1400,6 +1418,12 @@ VkBufferMemory* VkWeightAllocator::fastMalloc(size_t size)
}

block->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!block->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), block->buffer, 0);
delete block;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), block->buffer, block->memory, 0);
Expand Down Expand Up @@ -1547,6 +1571,12 @@ VkImageMemory* VkWeightAllocator::fastMalloc(int w, int h, int c, size_t elemsiz

// bind memory
ptr->memory = allocate_dedicated_memory(memoryRequirements2.memoryRequirements.size, image_memory_type_index, ptr->image, 0);
if (!ptr->memory)
{
vkDestroyImage(vkdev->vkdevice(), ptr->image, 0);
delete ptr;
return 0;
}
ptr->bind_offset = 0;
ptr->bind_capacity = memoryRequirements2.memoryRequirements.size;

Expand Down Expand Up @@ -1654,6 +1684,12 @@ VkImageMemory* VkWeightAllocator::fastMalloc(int w, int h, int c, size_t elemsiz

// bind at memory offset
ptr->memory = allocate_memory(new_block_size, image_memory_type_index);
if (!ptr->memory)
{
vkDestroyImage(vkdev->vkdevice(), ptr->image, 0);
delete ptr;
return 0;
}
ptr->bind_offset = 0;
ptr->bind_capacity = aligned_size;

Expand Down Expand Up @@ -1788,6 +1824,12 @@ VkBufferMemory* VkStagingAllocator::fastMalloc(size_t size)
}

ptr->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!ptr->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), ptr->buffer, 0);
delete ptr;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), ptr->buffer, ptr->memory, 0);
Expand Down Expand Up @@ -1897,6 +1939,12 @@ VkBufferMemory* VkWeightStagingAllocator::fastMalloc(size_t size)
}

ptr->memory = allocate_memory(memoryRequirements.size, buffer_memory_type_index);
if (!ptr->memory)
{
vkDestroyBuffer(vkdev->vkdevice(), ptr->buffer, 0);
delete ptr;
return 0;
}

// ignore memoryRequirements.alignment as we always bind at zero offset
vkBindBufferMemory(vkdev->vkdevice(), ptr->buffer, ptr->memory, 0);
Expand Down
Loading