Skip to content

Commit c4f5806

Browse files
authored
[VK] Fix crash if validation layers are absent (#218)
A first vkCreateInstance is called to enumerate devices and immediately destroyed. The resulting handle is not reset to VK_NULL_HANDLE. This means if the second vkCreateInstance fails (missing layer for ex), the dtor will call vkDestroyInstance with an already freed instance, causing a crash.
1 parent d115461 commit c4f5806

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

lib/API/VK/Device.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ class VKDevice : public offloadtest::Device {
766766

767767
class VKContext {
768768
private:
769-
VkInstance Instance;
769+
VkInstance Instance = VK_NULL_HANDLE;
770770
llvm::SmallVector<std::shared_ptr<VKDevice>> Devices;
771771

772772
VKContext() = default;
@@ -798,7 +798,7 @@ class VKContext {
798798
"Cannot find a compatible Vulkan device");
799799
if (Res)
800800
return llvm::createStringError(std::errc::no_such_device,
801-
"Unkown Vulkan initialization error");
801+
"Unknown Vulkan initialization error");
802802

803803
uint32_t DeviceCount = 0;
804804
if (vkEnumeratePhysicalDevices(Instance, &DeviceCount, nullptr))
@@ -814,6 +814,7 @@ class VKContext {
814814
AppInfo.apiVersion = TmpDev->getProps().apiVersion;
815815
}
816816
vkDestroyInstance(Instance, NULL);
817+
Instance = VK_NULL_HANDLE;
817818

818819
// TODO: This is a bit hacky but matches what I did in DX.
819820
#ifndef NDEBUG
@@ -828,7 +829,7 @@ class VKContext {
828829
"Cannot find a compatible Vulkan device");
829830
if (Res)
830831
return llvm::createStringError(std::errc::no_such_device,
831-
"Unkown Vulkan initialization error");
832+
"Unknown Vulkan initialization error");
832833

833834
DeviceCount = 0;
834835
if (vkEnumeratePhysicalDevices(Instance, &DeviceCount, nullptr))

0 commit comments

Comments
 (0)