diff options
author | Matteo Franchin <matteo.franchin@arm.com> | 2022-07-04 11:11:51 +0100 |
---|---|---|
committer | Matteo Franchin <matteo.franchin@arm.com> | 2022-07-21 19:05:53 +0100 |
commit | 425b885f6e607eecd5ccb4b7f15bf77592dbe267 (patch) | |
tree | 02ecf9d15ed972c014ff6c3b3462d82bb6f65392 | |
parent | 91583f1af270f19b40d19f9423e98a7f3d745e01 (diff) | |
download | vulkan-wsi-layer-425b885f6e607eecd5ccb4b7f15bf77592dbe267.tar.gz vulkan-wsi-layer-425b885f6e607eecd5ccb4b7f15bf77592dbe267.tar.bz2 vulkan-wsi-layer-425b885f6e607eecd5ccb4b7f15bf77592dbe267.zip |
Fix issues found by static analysis
All of the issues that this patch fixes should not occur in practice,
but make the code a bit more robust. For example, this patch
default-initializes structures passed to Vulkan for initialization.
Normally, initialization should be done by the Vulkan entrypoint,
so it should be fine not initializing the structure as long as the
Vulkan API entrypoint exits with success status. It is still a good
idea to default-initialize anyway to make the behaviour deterministic
in cases where something is wrong in the system. All changes made
should have a negligible impact on performance for typical
applications.
Change-Id: Ia606ad2d3ea1627f9dfef0cadf93c7468ab568d8
Signed-off-by: Matteo Franchin <matteo.franchin@arm.com>
-rw-r--r-- | layer/layer.cpp | 42 | ||||
-rw-r--r-- | layer/swapchain_api.cpp | 2 | ||||
-rw-r--r-- | util/timed_semaphore.cpp | 5 | ||||
-rw-r--r-- | wsi/headless/swapchain.cpp | 2 | ||||
-rw-r--r-- | wsi/surface_properties.cpp | 2 | ||||
-rw-r--r-- | wsi/wsi_factory.cpp | 2 |
6 files changed, 34 insertions, 21 deletions
diff --git a/layer/layer.cpp b/layer/layer.cpp index 8eb84a6..14e1dac 100644 --- a/layer/layer.cpp +++ b/layer/layer.cpp @@ -86,20 +86,26 @@ static T get_instance_proc_addr(PFN_vkGetInstanceProcAddr fp_get_instance_proc_a VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkInstance *pInstance) { - VkLayerInstanceCreateInfo *layerCreateInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); - PFN_vkSetInstanceLoaderData loader_callback = - get_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK)->u.pfnSetInstanceLoaderData; - - if (nullptr == layerCreateInfo || nullptr == layerCreateInfo->u.pLayerInfo) + VkLayerInstanceCreateInfo *layer_link_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); + VkLayerInstanceCreateInfo *loader_data_callback = get_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK); + if (nullptr == layer_link_info || nullptr == layer_link_info->u.pLayerInfo || nullptr == loader_data_callback) { + WSI_LOG_ERROR("Unexpected NULL pointer in layer initialization structures during vkCreateInstance"); return VK_ERROR_INITIALIZATION_FAILED; } - PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = layerCreateInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr; + PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = layer_link_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; + PFN_vkSetInstanceLoaderData loader_callback = loader_data_callback->u.pfnSetInstanceLoaderData; + if (nullptr == fpGetInstanceProcAddr || nullptr == loader_callback) + { + WSI_LOG_ERROR("Unexpected NULL pointer for loader callback functions during vkCreateInstance"); + return VK_ERROR_INITIALIZATION_FAILED; + } auto fpCreateInstance = get_instance_proc_addr<PFN_vkCreateInstance>(fpGetInstanceProcAddr, "vkCreateInstance"); if (nullptr == fpCreateInstance) { + WSI_LOG_ERROR("Unexpected NULL return value from pfnNextGetInstanceProcAddr"); return VK_ERROR_INITIALIZATION_FAILED; } @@ -144,7 +150,7 @@ VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, con } /* Advance the link info for the next element on the chain. */ - layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext; + layer_link_info->u.pLayerInfo = layer_link_info->u.pLayerInfo->pNext; /* Now call create instance on the chain further down the list. * Note that we do not remove the extensions that the layer supports from modified_info.ppEnabledExtensionNames. @@ -203,27 +209,33 @@ VKAPI_ATTR VkResult create_instance(const VkInstanceCreateInfo *pCreateInfo, con VKAPI_ATTR VkResult create_device(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDevice *pDevice) { - VkLayerDeviceCreateInfo *layerCreateInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); - PFN_vkSetDeviceLoaderData loader_callback = - get_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK)->u.pfnSetDeviceLoaderData; - - if (nullptr == layerCreateInfo || nullptr == layerCreateInfo->u.pLayerInfo) + VkLayerDeviceCreateInfo *layer_link_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO); + VkLayerDeviceCreateInfo *loader_data_callback = get_chain_info(pCreateInfo, VK_LOADER_DATA_CALLBACK); + if (nullptr == layer_link_info || nullptr == layer_link_info->u.pLayerInfo || nullptr == loader_data_callback) { + WSI_LOG_ERROR("Unexpected NULL pointer in layer initialization structures during vkCreateDevice"); return VK_ERROR_INITIALIZATION_FAILED; } /* Retrieve the vkGetDeviceProcAddr and the vkCreateDevice function pointers for the next layer in the chain. */ - PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = layerCreateInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr; - PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = layerCreateInfo->u.pLayerInfo->pfnNextGetDeviceProcAddr; + PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = layer_link_info->u.pLayerInfo->pfnNextGetInstanceProcAddr; + PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = layer_link_info->u.pLayerInfo->pfnNextGetDeviceProcAddr; + PFN_vkSetDeviceLoaderData loader_callback = loader_data_callback->u.pfnSetDeviceLoaderData; + if (nullptr == fpGetInstanceProcAddr || nullptr == fpGetDeviceProcAddr || nullptr == loader_callback) + { + WSI_LOG_ERROR("Unexpected NULL pointer for loader callback functions during vkCreateDevice"); + return VK_ERROR_INITIALIZATION_FAILED; + } auto fpCreateDevice = get_instance_proc_addr<PFN_vkCreateDevice>(fpGetInstanceProcAddr, "vkCreateDevice"); if (nullptr == fpCreateDevice) { + WSI_LOG_ERROR("Unexpected NULL return value from pfnNextGetInstanceProcAddr"); return VK_ERROR_INITIALIZATION_FAILED; } /* Advance the link info for the next element on the chain. */ - layerCreateInfo->u.pLayerInfo = layerCreateInfo->u.pLayerInfo->pNext; + layer_link_info->u.pLayerInfo = layer_link_info->u.pLayerInfo->pNext; /* Enable extra extensions if needed by the layer, similarly to what done in vkCreateInstance. */ VkDeviceCreateInfo modified_info = *pCreateInfo; diff --git a/layer/swapchain_api.cpp b/layer/swapchain_api.cpp index dc48e34..e9c428a 100644 --- a/layer/swapchain_api.cpp +++ b/layer/swapchain_api.cpp @@ -296,7 +296,7 @@ wsi_layer_vkGetPhysicalDevicePresentRectanglesKHR(VkPhysicalDevice physicalDevic { *pRectCount = 1; - VkSurfaceCapabilitiesKHR surface_caps; + VkSurfaceCapabilitiesKHR surface_caps = {}; result = props->get_surface_capabilities(physicalDevice, &surface_caps); if (result != VK_SUCCESS) diff --git a/util/timed_semaphore.cpp b/util/timed_semaphore.cpp index 9b2c117..48da4a6 100644 --- a/util/timed_semaphore.cpp +++ b/util/timed_semaphore.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2019, 2021 Arm Limited. + * Copyright (c) 2017, 2019, 2021-2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -54,7 +54,8 @@ VkResult timed_semaphore::init(unsigned count) assert(res == 0 || res == ENOMEM); if (res != 0) { - pthread_condattr_destroy(&attr); + res = pthread_condattr_destroy(&attr); + assert(res == 0); return VK_ERROR_OUT_OF_HOST_MEMORY; } diff --git a/wsi/headless/swapchain.cpp b/wsi/headless/swapchain.cpp index 8fdf4d3..0d1ef54 100644 --- a/wsi/headless/swapchain.cpp +++ b/wsi/headless/swapchain.cpp @@ -123,7 +123,7 @@ VkResult swapchain::create_and_bind_swapchain_image(VkImageCreateInfo image_crea return res; } - VkMemoryRequirements memory_requirements; + VkMemoryRequirements memory_requirements = {}; m_device_data.disp.GetImageMemoryRequirements(m_device, image.image, &memory_requirements); /* Find a memory type */ diff --git a/wsi/surface_properties.cpp b/wsi/surface_properties.cpp index 9653a8c..77cace5 100644 --- a/wsi/surface_properties.cpp +++ b/wsi/surface_properties.cpp @@ -106,7 +106,7 @@ void get_surface_capabilities_common(VkPhysicalDevice physical_device, VkSurface surface_capabilities->currentExtent = { 0xffffffff, 0xffffffff }; surface_capabilities->minImageExtent = { 1, 1 }; /* Ask the device for max */ - VkPhysicalDeviceProperties dev_props; + VkPhysicalDeviceProperties dev_props = {}; layer::instance_private_data::get(physical_device).disp.GetPhysicalDeviceProperties(physical_device, &dev_props); surface_capabilities->maxImageExtent = { dev_props.limits.maxImageDimension2D, diff --git a/wsi/wsi_factory.cpp b/wsi/wsi_factory.cpp index 40d767d..90fb6a5 100644 --- a/wsi/wsi_factory.cpp +++ b/wsi/wsi_factory.cpp @@ -124,7 +124,7 @@ static VkResult get_available_device_extensions(VkPhysicalDevice physical_device { auto &instance_data = layer::instance_private_data::get(physical_device); util::vector<VkExtensionProperties> properties{available_extensions.get_allocator()}; - uint32_t count; + uint32_t count = 0; TRY(instance_data.disp.EnumerateDeviceExtensionProperties(physical_device, nullptr, &count, nullptr)); if (!properties.try_resize(count)) |