Skip to content

Fixed merging of free blocks in TLSF when VMA_DEBUG_MARGIN is used - #555

Open
isakruas wants to merge 1 commit into
GPUOpen-LibrariesAndSDKs:masterfrom
isakruas:fix-tlsf-debug-margin-merge
Open

isakruas wants to merge 1 commit into
GPUOpen-LibrariesAndSDKs:masterfrom
isakruas:fix-tlsf-debug-margin-merge

Conversation

@isakruas

@isakruas isakruas commented Nov 27, 2026 •

Copy link
Copy Markdown

With VMA_DEBUG_MARGIN > 0, a block managed by the TLSF algorithm can stay "not empty" after every allocation in it was freed. The block is then never released, and destroying the pool or the allocator hits Some allocations were not freed before destruction of this memory block! although nothing leaked.

How to reproduce

VMA_DEBUG_MARGIN = 16, a custom pool with one block, two allocations made with vmaAllocateMemory:

  1. 256 bytes, alignment 16: offset 0, its margin is at 256..272.
  2. 256 bytes, alignment 32: offset 288, which leaves a 16-byte alignment gap at 272..288.
  3. Free the second allocation, then the first.

vmaCalculatePoolStatistics then reports 0 allocations and 2 unused ranges (288 bytes and the rest) instead of 1, and vmaDestroyPool asserts. Freeing in the other order, or with a gap of another size (alignment 64 or 16), is fine. With random sizes and alignments it happens sooner or later in every run I tried, because any free block of exactly VMA_DEBUG_MARGIN bytes triggers it, whether it comes from alignment or from a split.

Cause

A margin is kept as a free block of VMA_DEBUG_MARGIN bytes after each allocation, and VmaBlockMetadata_TLSF::Alloc and ::Free tell a margin from an ordinary free block by its size alone (prev->size != debugMargin). A real free block that happens to have that size is treated as a margin and is not merged with the freed block next to it. In the sequence above, freeing the second allocation skips the 16-byte gap and merges only into the null block; freeing the first then merges forward into the gap, but nothing merges the result with the null block, whose offset never returns to 0.

Fix

A margin block is recognized by what precedes it: it always follows the taken block it belongs to, and no other free block follows a taken block, since every taken block is followed by its margin. The two size checks now call a small helper, IsMarginBlock. Nothing changes when VMA_DEBUG_MARGIN is 0.

Testing

I tested with a standalone program (below) on Linux, with VMA_HEAVY_ASSERT mapped to VMA_ASSERT so that Validate() runs after every allocation and free:

  • before the change: the sequence above and 6 out of 6 random runs (20,000 operations each) end with more than one unused range and the leak assert;
  • after the change: one unused range and no assert in all of them, with VMA_DEBUG_MARGIN = 16 (with and without VMA_DEBUG_DETECT_CORRUPTION) and with VMA_DEBUG_MARGIN = 0.

I did not run src/Tests.cpp: the sample is Windows-only and I tested on Linux. I can add a case to TestDebugMargin for this if you would like one.

Standalone reproduction (the two-allocation case)
// c++ -std=c++17 -DVMA_DEBUG_MARGIN=16 -I<Vulkan-Headers>/include -I<VMA>/include repro.cpp -lvulkan
#include <cstdio>
static int gAsserts = 0;
#define VMA_ASSERT(expr) do { if (!(expr)) { ++gAsserts; std::printf("VMA_ASSERT failed: %s\n", #expr); } } while (false)
#define VMA_IMPLEMENTATION
#include <vulkan/vulkan.h>
#include "vk_mem_alloc.h"

int main() {
    VkApplicationInfo app{VK_STRUCTURE_TYPE_APPLICATION_INFO};
    app.apiVersion = VK_API_VERSION_1_3;
    VkInstanceCreateInfo instanceInfo{VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO};
    instanceInfo.pApplicationInfo = &app;
    VkInstance instance;
    vkCreateInstance(&instanceInfo, nullptr, &instance);
    uint32_t count = 1;
    VkPhysicalDevice physicalDevice;
    vkEnumeratePhysicalDevices(instance, &count, &physicalDevice);
    float priority = 1.0f;
    VkDeviceQueueCreateInfo queue{VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO};
    queue.queueCount = 1;
    queue.pQueuePriorities = &priority;
    VkDeviceCreateInfo deviceInfo{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO};
    deviceInfo.queueCreateInfoCount = 1;
    deviceInfo.pQueueCreateInfos = &queue;
    VkDevice device;
    vkCreateDevice(physicalDevice, &deviceInfo, nullptr, &device);

    VmaAllocatorCreateInfo allocatorInfo{};
    allocatorInfo.physicalDevice = physicalDevice;
    allocatorInfo.device = device;
    allocatorInfo.instance = instance;
    allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_3;
    VmaAllocator allocator;
    vmaCreateAllocator(&allocatorInfo, &allocator);

    VmaPoolCreateInfo poolInfo{};
    poolInfo.memoryTypeIndex = 0;
    poolInfo.blockSize = 1 << 20;
    poolInfo.maxBlockCount = 1;
    poolInfo.flags = VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT;
    VmaPool pool;
    vmaCreatePool(allocator, &poolInfo, &pool);

    VmaAllocationCreateInfo allocInfo{};
    allocInfo.pool = pool;
    VkMemoryRequirements requirements{256, 16, 1u << poolInfo.memoryTypeIndex};
    VmaAllocation first, second;
    vmaAllocateMemory(allocator, &requirements, &allocInfo, &first, nullptr);
    requirements.alignment = 32; // offset 288: a 16-byte gap after the first allocation's margin
    vmaAllocateMemory(allocator, &requirements, &allocInfo, &second, nullptr);

    vmaFreeMemory(allocator, second);
    vmaFreeMemory(allocator, first);

    VmaDetailedStatistics statistics{};
    vmaCalculatePoolStatistics(allocator, pool, &statistics);
    std::printf("allocations: %u, unused ranges: %u\n", statistics.statistics.allocationCount,
                statistics.unusedRangeCount); // 0 and 2 before the change, 0 and 1 after
    vmaDestroyPool(allocator, pool);          // asserts before the change
    std::printf("asserts: %d\n", gAsserts);

    vmaDestroyAllocator(allocator);
    vkDestroyDevice(device, nullptr);
    vkDestroyInstance(instance, nullptr);
    return gAsserts;
}

A margin block was recognized only by its size, so a free block of exactly
VMA_DEBUG_MARGIN bytes (left by alignment, or by a split) was taken for a
margin and never merged with a freed neighbor. After every allocation of the
block was freed, the null block did not return to offset 0: IsEmpty() stayed
false, the block was never released, and destroying the pool or the allocator
hit the "Some allocations were not freed before destruction of this memory
block!" assert with nothing leaked.

A margin block is now recognized by what precedes it: it always follows the
taken block it belongs to, and no other free block follows a taken block.
Nothing changes when VMA_DEBUG_MARGIN is 0.

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