Skip to content

Commit

Permalink
Fix Tencent#5741 don't crash when vkCreateDevice fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Upliner committed Oct 17, 2024
1 parent a6d3ef5 commit 1f8d79a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
44 changes: 35 additions & 9 deletions src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2212,10 +2212,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 @@ -2270,8 +2267,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 @@ -2310,7 +2321,10 @@ 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 @@ -2653,6 +2667,7 @@ VulkanDevice::VulkanDevice(int device_index)
if (ret != VK_SUCCESS)
{
NCNN_LOGE("vkCreateDevice failed %d", ret);
return;
}

init_device_extension();
Expand Down Expand Up @@ -2712,7 +2727,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 @@ -2724,11 +2738,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 @@ -2753,9 +2768,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 @@ -2775,6 +2796,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 @@ -336,6 +336,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
2 changes: 1 addition & 1 deletion 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

0 comments on commit 1f8d79a

Please sign in to comment.