radv: fix null memcpy and zero-sized malloc
[mesa.git] / src / amd / vulkan / radv_descriptor_set.c
index 33947ea3726e864b6d0f27bb3f4675a0aa6a2844..e4634eed6635251f31844c5c742566f7ed4d14b0 100644 (file)
@@ -57,13 +57,14 @@ static int binding_compare(const void* av, const void *bv)
 
 static VkDescriptorSetLayoutBinding *
 create_sorted_bindings(const VkDescriptorSetLayoutBinding *bindings, unsigned count) {
-       VkDescriptorSetLayoutBinding *sorted_bindings = malloc(count * sizeof(VkDescriptorSetLayoutBinding));
+       VkDescriptorSetLayoutBinding *sorted_bindings = malloc(MAX2(count * sizeof(VkDescriptorSetLayoutBinding), 1));
        if (!sorted_bindings)
                return NULL;
 
-       memcpy(sorted_bindings, bindings, count * sizeof(VkDescriptorSetLayoutBinding));
-
-       qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), binding_compare);
+       if (count) {
+               memcpy(sorted_bindings, bindings, count * sizeof(VkDescriptorSetLayoutBinding));
+               qsort(sorted_bindings, count, sizeof(VkDescriptorSetLayoutBinding), binding_compare);
+       }
 
        return sorted_bindings;
 }
@@ -102,18 +103,21 @@ VkResult radv_CreateDescriptorSetLayout(
                }
        }
 
-       uint32_t samplers_offset = sizeof(struct radv_descriptor_set_layout) +
-               (max_binding + 1) * sizeof(set_layout->binding[0]);
+       uint32_t samplers_offset =
+                       offsetof(struct radv_descriptor_set_layout, binding[max_binding + 1]);
        size_t size = samplers_offset + immutable_sampler_count * 4 * sizeof(uint32_t);
        if (ycbcr_sampler_count > 0) {
                size += ycbcr_sampler_count * sizeof(struct radv_sampler_ycbcr_conversion) + (max_binding + 1) * sizeof(uint32_t);
        }
 
-       set_layout = vk_zalloc2(&device->alloc, pAllocator, size, 8,
+       set_layout = vk_zalloc2(&device->vk.alloc, pAllocator, size, 8,
                                VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (!set_layout)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       vk_object_base_init(&device->vk, &set_layout->base,
+                           VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT);
+
        set_layout->flags = pCreateInfo->flags;
        set_layout->layout_size = size;
 
@@ -132,7 +136,8 @@ VkResult radv_CreateDescriptorSetLayout(
        VkDescriptorSetLayoutBinding *bindings = create_sorted_bindings(pCreateInfo->pBindings,
                                                                        pCreateInfo->bindingCount);
        if (!bindings) {
-               vk_free2(&device->alloc, pAllocator, set_layout);
+               vk_object_base_finish(&set_layout->base);
+               vk_free2(&device->vk.alloc, pAllocator, set_layout);
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
        }
 
@@ -142,15 +147,13 @@ VkResult radv_CreateDescriptorSetLayout(
        set_layout->has_immutable_samplers = false;
        set_layout->size = 0;
 
-       memset(set_layout->binding, 0, size - sizeof(struct radv_descriptor_set_layout));
-
        uint32_t buffer_count = 0;
        uint32_t dynamic_offset_count = 0;
 
        for (uint32_t j = 0; j < pCreateInfo->bindingCount; j++) {
                const VkDescriptorSetLayoutBinding *binding = bindings + j;
                uint32_t b = binding->binding;
-               uint32_t alignment;
+               uint32_t alignment = 0;
                unsigned binding_buffer_count = 0;
                uint32_t descriptor_count = binding->descriptorCount;
                bool has_ycbcr_sampler = false;
@@ -214,7 +217,6 @@ VkResult radv_CreateDescriptorSetLayout(
                        descriptor_count = 1;
                        break;
                default:
-                       unreachable("unknown descriptor type\n");
                        break;
                }
 
@@ -296,7 +298,8 @@ void radv_DestroyDescriptorSetLayout(
        if (!set_layout)
                return;
 
-       vk_free2(&device->alloc, pAllocator, set_layout);
+       vk_object_base_finish(&set_layout->base);
+       vk_free2(&device->vk.alloc, pAllocator, set_layout);
 }
 
 void radv_GetDescriptorSetLayoutSupport(VkDevice device,
@@ -363,7 +366,6 @@ void radv_GetDescriptorSetLayoutSupport(VkDevice device,
                        descriptor_count = 1;
                        break;
                default:
-                       unreachable("unknown descriptor type\n");
                        break;
                }
 
@@ -410,11 +412,14 @@ VkResult radv_CreatePipelineLayout(
 
        assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO);
 
-       layout = vk_alloc2(&device->alloc, pAllocator, sizeof(*layout), 8,
+       layout = vk_alloc2(&device->vk.alloc, pAllocator, sizeof(*layout), 8,
                             VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (layout == NULL)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       vk_object_base_init(&device->vk, &layout->base,
+                           VK_OBJECT_TYPE_PIPELINE_LAYOUT);
+
        layout->num_sets = pCreateInfo->setLayoutCount;
 
        unsigned dynamic_offset_count = 0;
@@ -464,7 +469,9 @@ void radv_DestroyPipelineLayout(
 
        if (!pipeline_layout)
                return;
-       vk_free2(&device->alloc, pAllocator, pipeline_layout);
+
+       vk_object_base_finish(&pipeline_layout->base);
+       vk_free2(&device->vk.alloc, pAllocator, pipeline_layout);
 }
 
 #define EMPTY 1
@@ -498,7 +505,7 @@ radv_descriptor_set_create(struct radv_device *device,
                set = (struct radv_descriptor_set*)pool->host_memory_ptr;
                pool->host_memory_ptr += mem_size;
        } else {
-               set = vk_alloc2(&device->alloc, NULL, mem_size, 8,
+               set = vk_alloc2(&device->vk.alloc, NULL, mem_size, 8,
                                VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
 
                if (!set)
@@ -507,6 +514,9 @@ radv_descriptor_set_create(struct radv_device *device,
 
        memset(set, 0, mem_size);
 
+       vk_object_base_init(&device->vk, &set->base,
+                           VK_OBJECT_TYPE_DESCRIPTOR_SET);
+
        if (layout->dynamic_offset_count) {
                set->dynamic_descriptors = (struct radv_descriptor_range*)((uint8_t*)set + range_offset);
        }
@@ -528,7 +538,7 @@ radv_descriptor_set_create(struct radv_device *device,
                set->size = layout_size;
 
                if (!pool->host_memory_base && pool->entry_count == pool->max_entry_count) {
-                       vk_free2(&device->alloc, NULL, set);
+                       vk_free2(&device->vk.alloc, NULL, set);
                        return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
                }
 
@@ -557,7 +567,7 @@ radv_descriptor_set_create(struct radv_device *device,
                        }
 
                        if (pool->size - offset < layout_size) {
-                               vk_free2(&device->alloc, NULL, set);
+                               vk_free2(&device->vk.alloc, NULL, set);
                                return vk_error(device->instance, VK_ERROR_OUT_OF_POOL_MEMORY);
                        }
                        set->bo = pool->bo;
@@ -614,7 +624,25 @@ radv_descriptor_set_destroy(struct radv_device *device,
                        }
                }
        }
-       vk_free2(&device->alloc, NULL, set);
+       vk_object_base_finish(&set->base);
+       vk_free2(&device->vk.alloc, NULL, set);
+}
+
+static void radv_destroy_descriptor_pool(struct radv_device *device,
+                                         const VkAllocationCallbacks *pAllocator,
+                                         struct radv_descriptor_pool *pool)
+{
+       if (!pool->host_memory_base) {
+               for(int i = 0; i < pool->entry_count; ++i) {
+                       radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
+               }
+       }
+
+       if (pool->bo)
+               device->ws->buffer_destroy(pool->bo);
+
+       vk_object_base_finish(&pool->base);
+       vk_free2(&device->vk.alloc, pAllocator, pool);
 }
 
 VkResult radv_CreateDescriptorPool(
@@ -673,7 +701,6 @@ VkResult radv_CreateDescriptorPool(
                        bo_size += pCreateInfo->pPoolSizes[i].descriptorCount;
                        break;
                default:
-                       unreachable("unknown descriptor type\n");
                        break;
                }
        }
@@ -687,13 +714,16 @@ VkResult radv_CreateDescriptorPool(
                size += sizeof(struct radv_descriptor_pool_entry) * pCreateInfo->maxSets;
        }
 
-       pool = vk_alloc2(&device->alloc, pAllocator, size, 8,
+       pool = vk_alloc2(&device->vk.alloc, pAllocator, size, 8,
                         VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (!pool)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
        memset(pool, 0, sizeof(*pool));
 
+       vk_object_base_init(&device->vk, &pool->base,
+                           VK_OBJECT_TYPE_DESCRIPTOR_POOL);
+
        if (!(pCreateInfo->flags & VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT)) {
                pool->host_memory_base = (uint8_t*)pool + sizeof(struct radv_descriptor_pool);
                pool->host_memory_ptr = pool->host_memory_base;
@@ -707,7 +737,15 @@ VkResult radv_CreateDescriptorPool(
                                                     RADEON_FLAG_READ_ONLY |
                                                     RADEON_FLAG_32BIT,
                                                     RADV_BO_PRIORITY_DESCRIPTOR);
+               if (!pool->bo) {
+                       radv_destroy_descriptor_pool(device, pAllocator, pool);
+                       return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
+               }
                pool->mapped_ptr = (uint8_t*)device->ws->buffer_map(pool->bo);
+               if (!pool->mapped_ptr) {
+                       radv_destroy_descriptor_pool(device, pAllocator, pool);
+                       return vk_error(device->instance, VK_ERROR_OUT_OF_DEVICE_MEMORY);
+               }
        }
        pool->size = bo_size;
        pool->max_entry_count = pCreateInfo->maxSets;
@@ -727,15 +765,7 @@ void radv_DestroyDescriptorPool(
        if (!pool)
                return;
 
-       if (!pool->host_memory_base) {
-               for(int i = 0; i < pool->entry_count; ++i) {
-                       radv_descriptor_set_destroy(device, pool, pool->entries[i].set, false);
-               }
-       }
-
-       if (pool->bo)
-               device->ws->buffer_destroy(pool->bo);
-       vk_free2(&device->alloc, pAllocator, pool);
+       radv_destroy_descriptor_pool(device, pAllocator, pool);
 }
 
 VkResult radv_ResetDescriptorPool(
@@ -833,6 +863,11 @@ static void write_texel_buffer_descriptor(struct radv_device *device,
 {
        RADV_FROM_HANDLE(radv_buffer_view, buffer_view, _buffer_view);
 
+       if (!buffer_view) {
+               memset(dst, 0, 4 * 4);
+               return;
+       }
+
        memcpy(dst, buffer_view->state, 4 * 4);
 
        if (cmd_buffer)
@@ -848,30 +883,45 @@ static void write_buffer_descriptor(struct radv_device *device,
                                     const VkDescriptorBufferInfo *buffer_info)
 {
        RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
+
+       if (!buffer) {
+               memset(dst, 0, 4 * 4);
+               return;
+       }
+
        uint64_t va = radv_buffer_get_va(buffer->bo);
        uint32_t range = buffer_info->range;
 
        if (buffer_info->range == VK_WHOLE_SIZE)
                range = buffer->size - buffer_info->offset;
 
+       /* robustBufferAccess is relaxed enough to allow this (in combination
+        * with the alignment/size we return from vkGetBufferMemoryRequirements)
+        * and this allows the shader compiler to create more efficient 8/16-bit
+        * buffer accesses. */
+       range = align(range, 4);
+
        va += buffer_info->offset + buffer->offset;
-       dst[0] = va;
-       dst[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
-       dst[2] = range;
-       dst[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
-               S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
-               S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
-               S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
+
+       uint32_t rsrc_word3 = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
+                             S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
+                             S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
+                             S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W);
 
        if (device->physical_device->rad_info.chip_class >= GFX10) {
-               dst[3] |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
-                         S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
-                         S_008F0C_RESOURCE_LEVEL(1);
+               rsrc_word3 |= S_008F0C_FORMAT(V_008F0C_IMG_FORMAT_32_FLOAT) |
+                             S_008F0C_OOB_SELECT(V_008F0C_OOB_SELECT_RAW) |
+                             S_008F0C_RESOURCE_LEVEL(1);
        } else {
-               dst[3] |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
-                         S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
+               rsrc_word3 |= S_008F0C_NUM_FORMAT(V_008F0C_BUF_NUM_FORMAT_FLOAT) |
+                             S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
        }
 
+       dst[0] = va;
+       dst[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
+       dst[2] = range;
+       dst[3] = rsrc_word3;
+
        if (cmd_buffer)
                radv_cs_add_buffer(device->ws, cmd_buffer->cs, buffer->bo);
        else
@@ -895,12 +945,24 @@ static void write_dynamic_buffer_descriptor(struct radv_device *device,
                                             const VkDescriptorBufferInfo *buffer_info)
 {
        RADV_FROM_HANDLE(radv_buffer, buffer, buffer_info->buffer);
-       uint64_t va = radv_buffer_get_va(buffer->bo);
-       unsigned size = buffer_info->range;
+       uint64_t va;
+       unsigned size;
+
+       if (!buffer)
+               return;
+
+       va = radv_buffer_get_va(buffer->bo);
+       size = buffer_info->range;
 
        if (buffer_info->range == VK_WHOLE_SIZE)
                size = buffer->size - buffer_info->offset;
 
+       /* robustBufferAccess is relaxed enough to allow this (in combination
+        * with the alignment/size we return from vkGetBufferMemoryRequirements)
+        * and this allows the shader compiler to create more efficient 8/16-bit
+        * buffer accesses. */
+       size = align(size, 4);
+
        va += buffer_info->offset + buffer->offset;
        range->va = va;
        range->size = size;
@@ -919,6 +981,11 @@ write_image_descriptor(struct radv_device *device,
        RADV_FROM_HANDLE(radv_image_view, iview, image_info->imageView);
        union radv_descriptor *descriptor;
 
+       if (!iview) {
+               memset(dst, 0, size);
+               return;
+       }
+
        if (descriptor_type == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE) {
                descriptor = &iview->storage_descriptor;
        } else {
@@ -1050,7 +1117,6 @@ void radv_update_descriptor_sets(
                                }
                                break;
                        default:
-                               unreachable("unimplemented descriptor type");
                                break;
                        }
                        ptr += binding_layout->size / 4;
@@ -1149,10 +1215,13 @@ VkResult radv_CreateDescriptorUpdateTemplate(VkDevice _device,
        struct radv_descriptor_update_template *templ;
        uint32_t i;
 
-       templ = vk_alloc2(&device->alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+       templ = vk_alloc2(&device->vk.alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
        if (!templ)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       vk_object_base_init(&device->vk, &templ->base,
+                           VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE);
+
        templ->entry_count = entry_count;
 
        if (pCreateInfo->templateType == VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR) {
@@ -1236,7 +1305,8 @@ void radv_DestroyDescriptorUpdateTemplate(VkDevice _device,
        if (!templ)
                return;
 
-       vk_free2(&device->alloc, pAllocator, templ);
+       vk_object_base_finish(&templ->base);
+       vk_free2(&device->vk.alloc, pAllocator, templ);
 }
 
 void radv_update_descriptor_set_with_template(struct radv_device *device,
@@ -1303,7 +1373,6 @@ void radv_update_descriptor_set_with_template(struct radv_device *device,
                                        memcpy(pDst, templ->entry[i].immutable_samplers + 4 * j, 16);
                                break;
                        default:
-                               unreachable("unimplemented descriptor type");
                                break;
                        }
                        pSrc += templ->entry[i].src_stride;
@@ -1333,12 +1402,15 @@ VkResult radv_CreateSamplerYcbcrConversion(VkDevice _device,
        RADV_FROM_HANDLE(radv_device, device, _device);
        struct radv_sampler_ycbcr_conversion *conversion = NULL;
 
-       conversion = vk_zalloc2(&device->alloc, pAllocator, sizeof(*conversion), 8,
+       conversion = vk_zalloc2(&device->vk.alloc, pAllocator, sizeof(*conversion), 8,
                                VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
 
        if (conversion == NULL)
                return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
 
+       vk_object_base_init(&device->vk, &conversion->base,
+                           VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION);
+
        conversion->format = pCreateInfo->format;
        conversion->ycbcr_model = pCreateInfo->ycbcrModel;
        conversion->ycbcr_range = pCreateInfo->ycbcrRange;
@@ -1359,6 +1431,9 @@ void radv_DestroySamplerYcbcrConversion(VkDevice _device,
        RADV_FROM_HANDLE(radv_device, device, _device);
        RADV_FROM_HANDLE(radv_sampler_ycbcr_conversion, ycbcr_conversion, ycbcrConversion);
 
-       if (ycbcr_conversion)
-               vk_free2(&device->alloc, pAllocator, ycbcr_conversion);
+       if (!ycbcr_conversion)
+               return;
+
+       vk_object_base_finish(&ycbcr_conversion->base);
+       vk_free2(&device->vk.alloc, pAllocator, ycbcr_conversion);
 }