From 7f02192019d2916ac2cfa0778f145f37d702b4cf Mon Sep 17 00:00:00 2001 From: Saurabh Pandey Date: Fri, 12 Jan 2024 08:57:20 +0530 Subject: [PATCH] Initial change for named resource import * Associate an optional name with the vk resource when handle type is NT * Use the name when available for importing the resource in OpenCL * Refactor ctor/dtor for WindowsSecurityAttributes class --- .../vulkan_wrapper/opencl_vulkan_wrapper.cpp | 103 +++++++++++++----- .../common/vulkan_wrapper/vulkan_wrapper.cpp | 93 ++++++++-------- .../common/vulkan_wrapper/vulkan_wrapper.hpp | 10 +- 3 files changed, 127 insertions(+), 79 deletions(-) diff --git a/test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.cpp b/test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.cpp index c137580227..610af67e13 100644 --- a/test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.cpp +++ b/test_conformance/common/vulkan_wrapper/opencl_vulkan_wrapper.cpp @@ -586,23 +586,37 @@ clExternalMemory::clExternalMemory( extMemProperties.push_back((cl_mem_properties)fd); break; case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_NT: + { #ifndef _WIN32 - ASSERT(0); + log_info(" Opaque NT handles are only supported on Windows\n"); + ASSERT(0); #else - log_info(" Opaque NT handles are only supported on Windows\n"); - handle = deviceMemory->getHandle(externalMemoryHandleType); - err = check_external_memory_handle_type( - devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); - extMemProperties.push_back( - (cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); - extMemProperties.push_back((cl_mem_properties)handle); + const std::wstring &name = deviceMemory->getName(); + if (name.size()) + { + err = check_external_memory_handle_type( + devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR); + extMemProperties.push_back( + (cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR); + extMemProperties.push_back((cl_mem_properties)name.c_str()); + } + else + { + handle = deviceMemory->getHandle(externalMemoryHandleType); + err = check_external_memory_handle_type( + devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); + extMemProperties.push_back( + (cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); + extMemProperties.push_back((cl_mem_properties)handle); + } #endif + } break; case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT: #ifndef _WIN32 + log_info("Opaque D3DKMT handles are only supported on Windows\n"); ASSERT(0); #else - log_info("Opaque D3DKMT handles are only supported on Windows\n"); handle = deviceMemory->getHandle(externalMemoryHandleType); err = check_external_memory_handle_type( devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KMT_KHR); @@ -613,8 +627,8 @@ clExternalMemory::clExternalMemory( #endif break; default: - ASSERT(0); log_error("Unsupported external memory handle type\n"); + ASSERT(0); break; } if (CL_SUCCESS != err) @@ -666,13 +680,27 @@ clExternalMemoryImage::clExternalMemoryImage( { #ifdef _WIN32 case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_NT: - log_info("Opaque NT handles are only supported on Windows\n"); - handle = deviceMemory.getHandle(externalMemoryHandleType); - errcode_ret = check_external_memory_handle_type( - devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); - extMemProperties1.push_back( - (cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); - extMemProperties1.push_back((cl_mem_properties)handle); + { + log_info("Opaque NT handles are only supported on Windows\n"); + const std::wstring &name = deviceMemory.getName(); + if (name.size()) + { + errcode_ret = check_external_memory_handle_type( + devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR); + extMemProperties1.push_back( + (cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_NAME_KHR); + extMemProperties1.push_back((cl_mem_properties)name.c_str()); + } + else + { + handle = deviceMemory.getHandle(externalMemoryHandleType); + errcode_ret = check_external_memory_handle_type( + devList[0], CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); + extMemProperties1.push_back( + (cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR); + extMemProperties1.push_back((cl_mem_properties)handle); + } + } break; case VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT: log_info("Opaque D3DKMT handles are only supported on Windows\n"); @@ -777,6 +805,10 @@ clExternalImportableSemaphore::clExternalImportableSemaphore( switch (externalSemaphoreHandleType) { case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD: +#ifdef _WIN32 + log_info(" Opaque file descriptors are not supported on Windows\n"); + ASSERT(0); +#else fd = (int)semaphore.getHandle(externalSemaphoreHandleType); err = check_external_semaphore_handle_type( devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_FD_KHR); @@ -785,25 +817,38 @@ clExternalImportableSemaphore::clExternalImportableSemaphore( sema_props.push_back((cl_semaphore_properties_khr)fd); break; case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_NT: + { #ifndef _WIN32 - ASSERT(0); + log_info(" Opaque NT handles are only supported on Windows\n"); + ASSERT(0); #else - log_info(" Opaque NT handles are only supported on Windows\n"); - handle = semaphore.getName().size() - ? NULL - : semaphore.getHandle(externalSemaphoreHandleType); - err = check_external_semaphore_handle_type( - devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR); - sema_props.push_back((cl_semaphore_properties_khr) - CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR); - sema_props.push_back((cl_semaphore_properties_khr)handle); + const std::wstring &name = semaphore.getName(); + if (name.size()) + { + err = check_external_semaphore_handle_type( + devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_NAME_KHR); + sema_props.push_back((cl_semaphore_properties_khr) + CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_NAME_KHR); + sema_props.push_back( + (cl_semaphore_properties_khr)name.c_str()); + } + else + { + handle = semaphore.getHandle(externalSemaphoreHandleType); + err = check_external_semaphore_handle_type( + devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR); + sema_props.push_back((cl_semaphore_properties_khr) + CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KHR); + sema_props.push_back((cl_semaphore_properties_khr)handle); + } #endif + } break; case VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT: #ifndef _WIN32 + log_info(" Opaque D3DKMT handles are only supported on Windows\n"); ASSERT(0); #else - log_info(" Opaque D3DKMT handles are only supported on Windows\n"); handle = semaphore.getHandle(externalSemaphoreHandleType); err = check_external_semaphore_handle_type( devList[0], CL_SEMAPHORE_HANDLE_OPAQUE_WIN32_KMT_KHR); @@ -821,8 +866,8 @@ clExternalImportableSemaphore::clExternalImportableSemaphore( sema_props.push_back(static_cast(-1)); break; default: - ASSERT(0); log_error("Unsupported external memory handle type\n"); + ASSERT(0); break; } if (CL_SUCCESS != err) diff --git a/test_conformance/common/vulkan_wrapper/vulkan_wrapper.cpp b/test_conformance/common/vulkan_wrapper/vulkan_wrapper.cpp index b187181f3f..9d4078c756 100644 --- a/test_conformance/common/vulkan_wrapper/vulkan_wrapper.cpp +++ b/test_conformance/common/vulkan_wrapper/vulkan_wrapper.cpp @@ -1905,35 +1905,42 @@ class WindowsSecurityAttributes { ~WindowsSecurityAttributes(); }; - WindowsSecurityAttributes::WindowsSecurityAttributes() { - m_winPSecurityDescriptor = (PSECURITY_DESCRIPTOR)calloc( - 1, SECURITY_DESCRIPTOR_MIN_LENGTH + 2 * sizeof(void **)); - // CHECK_NEQ(m_winPSecurityDescriptor, (PSECURITY_DESCRIPTOR)NULL); - PSID *ppSID = (PSID *)((PBYTE)m_winPSecurityDescriptor - + SECURITY_DESCRIPTOR_MIN_LENGTH); - PACL *ppACL = (PACL *)((PBYTE)ppSID + sizeof(PSID *)); - InitializeSecurityDescriptor(m_winPSecurityDescriptor, - SECURITY_DESCRIPTOR_REVISION); - SID_IDENTIFIER_AUTHORITY sidIdentifierAuthority = - SECURITY_WORLD_SID_AUTHORITY; - AllocateAndInitializeSid(&sidIdentifierAuthority, 1, SECURITY_WORLD_RID, 0, - 0, 0, 0, 0, 0, 0, ppSID); - EXPLICIT_ACCESS explicitAccess; - ZeroMemory(&explicitAccess, sizeof(EXPLICIT_ACCESS)); - explicitAccess.grfAccessPermissions = - STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL; - explicitAccess.grfAccessMode = SET_ACCESS; - explicitAccess.grfInheritance = INHERIT_ONLY; - explicitAccess.Trustee.TrusteeForm = TRUSTEE_IS_SID; - explicitAccess.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; - explicitAccess.Trustee.ptstrName = (LPTSTR)*ppSID; - SetEntriesInAcl(1, &explicitAccess, NULL, ppACL); - SetSecurityDescriptorDacl(m_winPSecurityDescriptor, TRUE, *ppACL, FALSE); - m_winSecurityAttributes.nLength = sizeof(m_winSecurityAttributes); +#define CHECK(ok, msg) \ + if (!ok) { \ + throw std::runtime_error(msg); \ + } + + BOOL ok; + HANDLE tokenHandle; + + ok = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &tokenHandle); + CHECK(ok, "Failed to open process access token"); + + DWORD tokenInformationLength = 0; + GetTokenInformation(tokenHandle, TokenDefaultDacl, NULL, 0, &tokenInformationLength); + CHECK(tokenInformationLength, "Failed to retrieve TokenDefaultDacl info buffer length"); + + m_winPSecurityDescriptor = (PSECURITY_DESCRIPTOR)calloc(1, SECURITY_DESCRIPTOR_MIN_LENGTH + tokenInformationLength); + assert(m_winPSecurityDescriptor != (PSECURITY_DESCRIPTOR)NULL); + + TOKEN_DEFAULT_DACL* pTokenDefaultDacl = reinterpret_cast((PBYTE)m_winPSecurityDescriptor + SECURITY_DESCRIPTOR_MIN_LENGTH); + ok = GetTokenInformation(tokenHandle, TokenDefaultDacl, pTokenDefaultDacl, tokenInformationLength, &tokenInformationLength); + CHECK(ok, "Failed to retrieve TokenDefaultDacl info of access token"); + + ok = InitializeSecurityDescriptor(m_winPSecurityDescriptor, SECURITY_DESCRIPTOR_REVISION); + CHECK(ok, "Failed to init security descriptor"); + + ok = SetSecurityDescriptorDacl(m_winPSecurityDescriptor, TRUE, pTokenDefaultDacl->DefaultDacl, FALSE); + CHECK(ok, "Failed to set DACL info for given security descriptor"); + + m_winSecurityAttributes.nLength = sizeof(m_winSecurityAttributes); m_winSecurityAttributes.lpSecurityDescriptor = m_winPSecurityDescriptor; - m_winSecurityAttributes.bInheritHandle = TRUE; + m_winSecurityAttributes.bInheritHandle = TRUE; + + CloseHandle(tokenHandle); +#undef CHECK_WIN } SECURITY_ATTRIBUTES *WindowsSecurityAttributes::operator&() @@ -1943,17 +1950,6 @@ SECURITY_ATTRIBUTES *WindowsSecurityAttributes::operator&() WindowsSecurityAttributes::~WindowsSecurityAttributes() { - PSID *ppSID = (PSID *)((PBYTE)m_winPSecurityDescriptor - + SECURITY_DESCRIPTOR_MIN_LENGTH); - PACL *ppACL = (PACL *)((PBYTE)ppSID + sizeof(PSID *)); - if (*ppSID) - { - FreeSid(*ppSID); - } - if (*ppACL) - { - LocalFree(*ppACL); - } free(m_winPSecurityDescriptor); } @@ -1968,8 +1964,8 @@ VulkanDeviceMemory::VulkanDeviceMemory(const VulkanDeviceMemory &deviceMemory) VulkanDeviceMemory::VulkanDeviceMemory( const VulkanDevice &device, uint64_t size, const VulkanMemoryType &memoryType, - VulkanExternalMemoryHandleType externalMemoryHandleType, const void *name) - : m_device(device), m_size(size), m_isDedicated(false) + VulkanExternalMemoryHandleType externalMemoryHandleType, const std::wstring name) + : m_device(device), m_size(size), m_isDedicated(false), m_name(name) { #if defined(_WIN32) || defined(_WIN64) WindowsSecurityAttributes winSecurityAttributes; @@ -1981,7 +1977,8 @@ VulkanDeviceMemory::VulkanDeviceMemory( vkExportMemoryWin32HandleInfoKHR.pAttributes = &winSecurityAttributes; vkExportMemoryWin32HandleInfoKHR.dwAccess = DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE; - vkExportMemoryWin32HandleInfoKHR.name = (LPCWSTR)name; + vkExportMemoryWin32HandleInfoKHR.name = + m_name.size() ? (LPCWSTR)m_name.c_str() : NULL; #endif @@ -2012,9 +2009,9 @@ VulkanDeviceMemory::VulkanDeviceMemory( VulkanDeviceMemory::VulkanDeviceMemory( const VulkanDevice &device, const VulkanImage &image, const VulkanMemoryType &memoryType, - VulkanExternalMemoryHandleType externalMemoryHandleType, const void *name) + VulkanExternalMemoryHandleType externalMemoryHandleType, const std::wstring name) : m_device(device), m_size(image.getSize()), - m_isDedicated(image.isDedicated()) + m_isDedicated(image.isDedicated()), m_name(name) { #if defined(_WIN32) || defined(_WIN64) WindowsSecurityAttributes winSecurityAttributes; @@ -2026,7 +2023,8 @@ VulkanDeviceMemory::VulkanDeviceMemory( vkExportMemoryWin32HandleInfoKHR.pAttributes = &winSecurityAttributes; vkExportMemoryWin32HandleInfoKHR.dwAccess = DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE; - vkExportMemoryWin32HandleInfoKHR.name = (LPCWSTR)name; + vkExportMemoryWin32HandleInfoKHR.name = + m_name.size() ? (LPCWSTR)m_name.c_str() : NULL; #endif @@ -2074,9 +2072,9 @@ VulkanDeviceMemory::VulkanDeviceMemory( VulkanDeviceMemory::VulkanDeviceMemory( const VulkanDevice &device, const VulkanBuffer &buffer, const VulkanMemoryType &memoryType, - VulkanExternalMemoryHandleType externalMemoryHandleType, const void *name) + VulkanExternalMemoryHandleType externalMemoryHandleType, const std::wstring name) : m_device(device), m_size(buffer.getSize()), - m_isDedicated(buffer.isDedicated()) + m_isDedicated(buffer.isDedicated()), m_name(name) { #if defined(_WIN32) || defined(_WIN64) WindowsSecurityAttributes winSecurityAttributes; @@ -2088,7 +2086,8 @@ VulkanDeviceMemory::VulkanDeviceMemory( vkExportMemoryWin32HandleInfoKHR.pAttributes = &winSecurityAttributes; vkExportMemoryWin32HandleInfoKHR.dwAccess = DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE; - vkExportMemoryWin32HandleInfoKHR.name = (LPCWSTR)name; + vkExportMemoryWin32HandleInfoKHR.name = + m_name.size() ? (LPCWSTR)m_name.c_str() : NULL; #endif @@ -2220,6 +2219,8 @@ void VulkanDeviceMemory::bindImage(const VulkanImage &image, uint64_t offset) vkBindImageMemory(m_device, image, m_vkDeviceMemory, offset); } +const std::wstring &VulkanDeviceMemory::getName() const { return m_name; } + VulkanDeviceMemory::operator VkDeviceMemory() const { return m_vkDeviceMemory; } //////////////////////////////////// diff --git a/test_conformance/common/vulkan_wrapper/vulkan_wrapper.hpp b/test_conformance/common/vulkan_wrapper/vulkan_wrapper.hpp index a520dceea7..6a8982dc2c 100644 --- a/test_conformance/common/vulkan_wrapper/vulkan_wrapper.hpp +++ b/test_conformance/common/vulkan_wrapper/vulkan_wrapper.hpp @@ -22,6 +22,7 @@ #include "vulkan_list_map.hpp" #include "vulkan_api_list.hpp" #include +#include class VulkanInstance { friend const VulkanInstance &getVulkanInstance(); @@ -539,7 +540,7 @@ class VulkanDeviceMemory { VkDeviceMemory m_vkDeviceMemory; uint64_t m_size; bool m_isDedicated; - + const std::wstring m_name; VulkanDeviceMemory(const VulkanDeviceMemory &deviceMemory); @@ -548,17 +549,17 @@ class VulkanDeviceMemory { const VulkanMemoryType &memoryType, VulkanExternalMemoryHandleType externalMemoryHandleType = VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE, - const void *name = NULL); + const std::wstring name = L""); VulkanDeviceMemory(const VulkanDevice &device, const VulkanImage &image, const VulkanMemoryType &memoryType, VulkanExternalMemoryHandleType externalMemoryHandleType = VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE, - const void *name = NULL); + const std::wstring name = L""); VulkanDeviceMemory(const VulkanDevice &device, const VulkanBuffer &buffer, const VulkanMemoryType &memoryType, VulkanExternalMemoryHandleType externalMemoryHandleType = VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE, - const void *name = NULL); + const std::wstring name = L""); virtual ~VulkanDeviceMemory(); uint64_t getSize() const; #ifdef _WIN32 @@ -573,6 +574,7 @@ class VulkanDeviceMemory { void unmap(); void bindBuffer(const VulkanBuffer &buffer, uint64_t offset = 0); void bindImage(const VulkanImage &image, uint64_t offset = 0); + const std::wstring &getName() const; operator VkDeviceMemory() const; };