Skip to content

Commit 370ab18

Browse files
Changed functions to validate and return VK_ERROR_VALIDATION_FAILED_EXT when trying to allocate memory of size 0, create buffer with size 0, or image with one of the dimensions 0.
That's because vkCreateBuffer returns VK_SUCCESS for buffer with size = 0, so VMA then proceeded to allocation of size 0, which is a critical error, checked by an assert, but apparently some users don't enable asserts in VMA. Added tests: function TestInvalidAllocations.
1 parent 3a7249f commit 370ab18

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/Tests.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,56 @@ static void TestUserData()
11951195
}
11961196
}
11971197

1198+
static void TestInvalidAllocations()
1199+
{
1200+
VkResult res;
1201+
1202+
VmaAllocationCreateInfo allocCreateInfo = {};
1203+
allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
1204+
1205+
// Try to allocate 0 bytes.
1206+
{
1207+
VkMemoryRequirements memReq = {};
1208+
memReq.size = 0; // !!!
1209+
memReq.alignment = 4;
1210+
memReq.memoryTypeBits = UINT32_MAX;
1211+
VmaAllocation alloc = VK_NULL_HANDLE;
1212+
res = vmaAllocateMemory(g_hAllocator, &memReq, &allocCreateInfo, &alloc, nullptr);
1213+
TEST(res == VK_ERROR_VALIDATION_FAILED_EXT && alloc == VK_NULL_HANDLE);
1214+
}
1215+
1216+
// Try to create buffer with size = 0.
1217+
{
1218+
VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
1219+
bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
1220+
bufCreateInfo.size = 0; // !!!
1221+
VkBuffer buf = VK_NULL_HANDLE;
1222+
VmaAllocation alloc = VK_NULL_HANDLE;
1223+
res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, nullptr);
1224+
TEST(res == VK_ERROR_VALIDATION_FAILED_EXT && buf == VK_NULL_HANDLE && alloc == VK_NULL_HANDLE);
1225+
}
1226+
1227+
// Try to create image with one dimension = 0.
1228+
{
1229+
VkImageCreateInfo imageCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
1230+
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
1231+
imageCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
1232+
imageCreateInfo.extent.width = 128;
1233+
imageCreateInfo.extent.height = 0; // !!!
1234+
imageCreateInfo.extent.depth = 1;
1235+
imageCreateInfo.mipLevels = 1;
1236+
imageCreateInfo.arrayLayers = 1;
1237+
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
1238+
imageCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
1239+
imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
1240+
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
1241+
VkImage image = VK_NULL_HANDLE;
1242+
VmaAllocation alloc = VK_NULL_HANDLE;
1243+
res = vmaCreateImage(g_hAllocator, &imageCreateInfo, &allocCreateInfo, &image, &alloc, nullptr);
1244+
TEST(res == VK_ERROR_VALIDATION_FAILED_EXT && image == VK_NULL_HANDLE && alloc == VK_NULL_HANDLE);
1245+
}
1246+
}
1247+
11981248
static void TestMemoryRequirements()
11991249
{
12001250
VkResult res;
@@ -1299,6 +1349,8 @@ static void TestBasics()
12991349
}
13001350

13011351
TestUserData();
1352+
1353+
TestInvalidAllocations();
13021354
}
13031355

13041356
void TestHeapSizeLimit()

src/vk_mem_alloc.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12301,6 +12301,10 @@ VkResult VmaAllocator_T::AllocateMemory(
1230112301
{
1230212302
VMA_ASSERT(VmaIsPow2(vkMemReq.alignment));
1230312303

12304+
if(vkMemReq.size == 0)
12305+
{
12306+
return VK_ERROR_VALIDATION_FAILED_EXT;
12307+
}
1230412308
if((createInfo.flags & VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT) != 0 &&
1230512309
(createInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) != 0)
1230612310
{
@@ -14095,6 +14099,11 @@ VkResult vmaCreateBuffer(
1409514099
VmaAllocationInfo* pAllocationInfo)
1409614100
{
1409714101
VMA_ASSERT(allocator && pBufferCreateInfo && pAllocationCreateInfo && pBuffer && pAllocation);
14102+
14103+
if(pBufferCreateInfo->size == 0)
14104+
{
14105+
return VK_ERROR_VALIDATION_FAILED_EXT;
14106+
}
1409814107

1409914108
VMA_DEBUG_LOG("vmaCreateBuffer");
1410014109

@@ -14234,6 +14243,15 @@ VkResult vmaCreateImage(
1423414243
{
1423514244
VMA_ASSERT(allocator && pImageCreateInfo && pAllocationCreateInfo && pImage && pAllocation);
1423614245

14246+
if(pImageCreateInfo->extent.width == 0 ||
14247+
pImageCreateInfo->extent.height == 0 ||
14248+
pImageCreateInfo->extent.depth == 0 ||
14249+
pImageCreateInfo->mipLevels == 0 ||
14250+
pImageCreateInfo->arrayLayers == 0)
14251+
{
14252+
return VK_ERROR_VALIDATION_FAILED_EXT;
14253+
}
14254+
1423714255
VMA_DEBUG_LOG("vmaCreateImage");
1423814256

1423914257
VMA_DEBUG_GLOBAL_MUTEX_LOCK

0 commit comments

Comments
 (0)