From 9190cc9b151004ffec1c6f8c10983853c19af8d2 Mon Sep 17 00:00:00 2001 From: Hyunjun Ko Date: Wed, 1 Jul 2020 03:13:40 +0000 Subject: [PATCH] tu,radv: fix potentially wrong offset of flexible array. v2. Remove redundant memset and make the expression simpler. Signed-off-by: Hyunjun Ko Part-of: --- src/amd/vulkan/radv_descriptor_set.c | 6 ++---- src/freedreno/vulkan/tu_descriptor_set.c | 10 ++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/amd/vulkan/radv_descriptor_set.c b/src/amd/vulkan/radv_descriptor_set.c index 21b9de72efb..241ce2c33c6 100644 --- a/src/amd/vulkan/radv_descriptor_set.c +++ b/src/amd/vulkan/radv_descriptor_set.c @@ -102,8 +102,8 @@ 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); @@ -146,8 +146,6 @@ 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; diff --git a/src/freedreno/vulkan/tu_descriptor_set.c b/src/freedreno/vulkan/tu_descriptor_set.c index 314088cefd9..ec2a940e858 100644 --- a/src/freedreno/vulkan/tu_descriptor_set.c +++ b/src/freedreno/vulkan/tu_descriptor_set.c @@ -139,15 +139,16 @@ tu_CreateDescriptorSetLayout( } } - uint32_t samplers_offset = sizeof(struct tu_descriptor_set_layout) + - (max_binding + 1) * sizeof(set_layout->binding[0]); + uint32_t samplers_offset = + offsetof(struct tu_descriptor_set_layout, binding[max_binding + 1]); + /* note: only need to store TEX_SAMP_DWORDS for immutable samples, * but using struct tu_sampler makes things simpler */ uint32_t size = samplers_offset + immutable_sampler_count * sizeof(struct tu_sampler) + ycbcr_sampler_count * sizeof(struct tu_sampler_ycbcr_conversion); - set_layout = vk_alloc2(&device->alloc, pAllocator, size, 8, + set_layout = vk_zalloc2(&device->alloc, pAllocator, size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT); if (!set_layout) return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY); @@ -172,9 +173,6 @@ tu_CreateDescriptorSetLayout( set_layout->size = 0; set_layout->dynamic_ubo = 0; - memset(set_layout->binding, 0, - size - sizeof(struct tu_descriptor_set_layout)); - uint32_t dynamic_offset_count = 0; uint32_t buffer_count = 0; -- 2.30.2