Skip to content

Commit

Permalink
Fix #5741 don't crash when vkCreateDevice fails (#5742)
Browse files Browse the repository at this point in the history
  • Loading branch information
Upliner authored Oct 18, 2024
1 parent 73d3519 commit cbd17cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
45 changes: 36 additions & 9 deletions src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2259,10 +2259,7 @@ class VkDummyCompute : public VkCompute
class VulkanDevicePrivate
{
public:
VulkanDevicePrivate(VulkanDevice* _vkdev)
: vkdev(_vkdev)
{
}
VulkanDevicePrivate(VulkanDevice* _vkdev);
VulkanDevice* const vkdev;

// dummy buffer and image
Expand Down Expand Up @@ -2317,8 +2314,22 @@ class VulkanDevicePrivate
// to pack1 | pack4 | pack8
mutable ncnn::Packing_vulkan* uop_packing[2][2][3][3][3];
mutable Mutex uop_lock;

// device is valid and sucessfully initialized
bool valid;
};

VulkanDevicePrivate::VulkanDevicePrivate(VulkanDevice* _vkdev)
: vkdev(_vkdev)
{
device = 0;
texelfetch_sampler = 0;
dummy_allocator = 0;
pipeline_cache = 0;
valid = false;
memset(uop_packing, 0, sizeof(uop_packing));
}

int VulkanDevicePrivate::create_dummy_buffer_image()
{
dummy_allocator = new VkDummyAllocator(vkdev);
Expand Down Expand Up @@ -2357,7 +2368,11 @@ void VulkanDevicePrivate::destroy_dummy_buffer_image()
dummy_image_readonly.release();
#endif

delete dummy_allocator;
if (dummy_allocator)
{
delete dummy_allocator;
dummy_allocator = 0;
}
}

const ncnn::Packing_vulkan* VulkanDevicePrivate::get_utility_operator(int storage_type_from, int storage_type_to, int cast_type_from_index, int cast_type_to_index, int packing_type_to_index) const
Expand Down Expand Up @@ -2702,6 +2717,7 @@ VulkanDevice::VulkanDevice(int device_index)
if (ret != VK_SUCCESS)
{
NCNN_LOGE("vkCreateDevice failed %d", ret);
return;
}

init_device_extension();
Expand Down Expand Up @@ -2761,7 +2777,6 @@ VulkanDevice::VulkanDevice(int device_index)
samplerCreateInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
samplerCreateInfo.unnormalizedCoordinates = VK_TRUE;

d->texelfetch_sampler = 0;
ret = vkCreateSampler(d->device, &samplerCreateInfo, 0, &d->texelfetch_sampler);
if (ret != VK_SUCCESS)
{
Expand All @@ -2773,11 +2788,12 @@ VulkanDevice::VulkanDevice(int device_index)
if (cret != 0)
{
NCNN_LOGE("VulkanDevice create_dummy_buffer_image failed %d", cret);
return;
}

d->pipeline_cache = new PipelineCache(this);

memset(d->uop_packing, 0, sizeof(d->uop_packing));
d->valid = true;
}

VulkanDevice::~VulkanDevice()
Expand All @@ -2802,9 +2818,15 @@ VulkanDevice::~VulkanDevice()
}
d->staging_allocators.clear();

delete d->pipeline_cache;
if (d->pipeline_cache)
{
delete d->pipeline_cache;
}

vkDestroyDevice(d->device, 0);
if (d->device)
{
vkDestroyDevice(d->device, 0);
}

delete d;
}
Expand All @@ -2824,6 +2846,11 @@ VkDevice VulkanDevice::vkdevice() const
return d->device;
}

bool VulkanDevice::is_valid() const
{
return d->valid;
}

VkShaderModule VulkanDevice::compile_shader_module(const uint32_t* spv_data, size_t spv_data_size) const
{
VkShaderModuleCreateInfo shaderModuleCreateInfo;
Expand Down
1 change: 1 addition & 0 deletions src/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ class NCNN_EXPORT VulkanDevice
const GpuInfo& info;

VkDevice vkdevice() const;
bool is_valid() const;

VkShaderModule compile_shader_module(const uint32_t* spv_data, size_t spv_data_size) const;

Expand Down
4 changes: 2 additions & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,7 @@ int Net::load_param(const DataReader& dr)
if (opt.use_vulkan_compute)
{
if (!d->vkdev) d->vkdev = get_gpu_device();
if (!d->vkdev) opt.use_vulkan_compute = false; // no vulkan device, fallback to cpu
if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu
}
if (opt.use_vulkan_compute)
{
Expand Down Expand Up @@ -1677,7 +1677,7 @@ int Net::load_param_bin(const DataReader& dr)
if (opt.use_vulkan_compute)
{
if (!d->vkdev) d->vkdev = get_gpu_device();
if (!d->vkdev) opt.use_vulkan_compute = false; // no vulkan device, fallback to cpu
if (!d->vkdev || !d->vkdev->is_valid()) opt.use_vulkan_compute = false; // no valid vulkan device, fallback to cpu
}
if (opt.use_vulkan_compute)
{
Expand Down

0 comments on commit cbd17cd

Please sign in to comment.