From 9529f21182e9b99c086988dab8c9a8fc6feb6085 Mon Sep 17 00:00:00 2001 From: "Baj, Tomasz" Date: Tue, 31 May 2022 09:36:11 +0000 Subject: [PATCH] Fix missing handle in external memory Related-To: NEO-6757 Signed-off-by: Baj, Tomasz --- opencl/source/mem_obj/buffer.cpp | 2 +- .../mem_obj/linux/buffer_linux_tests.cpp | 35 +++++++++++-------- .../test/common/mocks/mock_memory_manager.cpp | 11 ++++++ .../test/common/mocks/mock_memory_manager.h | 3 ++ 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/opencl/source/mem_obj/buffer.cpp b/opencl/source/mem_obj/buffer.cpp index 02c212b49f..b4b8a2193d 100644 --- a/opencl/source/mem_obj/buffer.cpp +++ b/opencl/source/mem_obj/buffer.cpp @@ -147,7 +147,7 @@ cl_mem Buffer::validateInputAndCreateBuffer(cl_context context, if (memoryProperties.handle) { if (validateHandleType(memoryProperties, extMem)) { - extMem.handle = &memoryProperties.handle; + extMem.handle = reinterpret_cast(memoryProperties.handle); extMem.size = size; pBuffer = UnifiedBuffer::createSharedUnifiedBuffer(pContext, flags, extMem, &retVal); } else { diff --git a/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp b/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp index 5f87ccab53..40092d7160 100644 --- a/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp +++ b/opencl/test/unit_test/mem_obj/linux/buffer_linux_tests.cpp @@ -98,24 +98,31 @@ struct ValidExportHostPtr MemoryManagementFixture::TearDown(); } - Buffer *createBuffer() { - return Buffer::create( - context.get(), - flags, - g_scTestBufferSizeInBytes, - pHostPtr, - retVal); - } - cl_int retVal = CL_INVALID_VALUE; Buffer *buffer = nullptr; }; -TEST_F(ValidExportHostPtr, givenPropertiesWithDmaBufWhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { - cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR, 0x1234, 0}; - auto buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), properties, flags, 0, g_scTestBufferSizeInBytes, nullptr, retVal); - EXPECT_EQ(retVal, CL_SUCCESS); - EXPECT_NE(nullptr, buffer); +TEST_F(ValidExportHostPtr, givenInvalidPropertiesWithDmaBufWhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { + + osHandle invalidHandle = static_cast(pClExecutionEnvironment->memoryManager.get())->invalidSharedHandle; + cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR, invalidHandle, 0}; + cl_mem buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), properties, flags, 0, g_scTestBufferSizeInBytes, nullptr, retVal); + + EXPECT_EQ(retVal, CL_INVALID_MEM_OBJECT); + EXPECT_EQ(static_cast(pClExecutionEnvironment->memoryManager.get())->capturedSharedHandle, properties[1]); + EXPECT_EQ(buffer, nullptr); + + clReleaseMemObject(buffer); +} + +TEST_F(ValidExportHostPtr, givenPropertiesWithDmaBufWhenValidateInputAndCreateBufferThenCorrectBufferIsSet) { + + cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR, 0x1234, 0}; + cl_mem buffer = BufferFunctions::validateInputAndCreateBuffer(context.get(), properties, flags, 0, g_scTestBufferSizeInBytes, nullptr, retVal); + + EXPECT_EQ(retVal, CL_SUCCESS); + EXPECT_EQ(static_cast(pClExecutionEnvironment->memoryManager.get())->capturedSharedHandle, properties[1]); + EXPECT_NE(buffer, nullptr); clReleaseMemObject(buffer); } diff --git a/shared/test/common/mocks/mock_memory_manager.cpp b/shared/test/common/mocks/mock_memory_manager.cpp index 6c2366f8be..d9f2fda7e7 100644 --- a/shared/test/common/mocks/mock_memory_manager.cpp +++ b/shared/test/common/mocks/mock_memory_manager.cpp @@ -152,6 +152,17 @@ GraphicsAllocation *MockMemoryManager::createGraphicsAllocationFromExistingStora return allocation; } +GraphicsAllocation *MockMemoryManager::createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) { + if (handle != invalidSharedHandle) { + auto allocation = OsAgnosticMemoryManager::createGraphicsAllocationFromSharedHandle(handle, properties, requireSpecificBitness, isHostIpcAllocation); + this->capturedSharedHandle = handle; + return allocation; + } else { + this->capturedSharedHandle = handle; + return nullptr; + } +} + bool MockMemoryManager::copyMemoryToAllocationBanks(GraphicsAllocation *graphicsAllocation, size_t destinationOffset, const void *memoryToCopy, size_t sizeToCopy, DeviceBitfield handleMask) { copyMemoryToAllocationBanksCalled++; copyMemoryToAllocationBanksParamsPassed.push_back({graphicsAllocation, destinationOffset, memoryToCopy, sizeToCopy, handleMask}); diff --git a/shared/test/common/mocks/mock_memory_manager.h b/shared/test/common/mocks/mock_memory_manager.h index c20253e8ec..ff13c3e709 100644 --- a/shared/test/common/mocks/mock_memory_manager.h +++ b/shared/test/common/mocks/mock_memory_manager.h @@ -82,6 +82,7 @@ class MockMemoryManager : public MemoryManagerCreate { GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties) override; GraphicsAllocation *allocateGraphicsMemoryWithProperties(const AllocationProperties &properties, const void *ptr) override; GraphicsAllocation *createGraphicsAllocationFromExistingStorage(AllocationProperties &properties, void *ptr, MultiGraphicsAllocation &multiGraphicsAllocation) override; + GraphicsAllocation *createGraphicsAllocationFromSharedHandle(osHandle handle, const AllocationProperties &properties, bool requireSpecificBitness, bool isHostIpcAllocation) override; void *allocateSystemMemory(size_t size, size_t alignment) override; @@ -220,6 +221,8 @@ class MockMemoryManager : public MemoryManagerCreate { uint32_t handleFenceCompletionCalled = 0u; uint32_t waitForEnginesCompletionCalled = 0u; uint32_t allocateGraphicsMemoryWithPropertiesCount = 0; + osHandle capturedSharedHandle = 0u; + osHandle invalidSharedHandle = -1; bool allocationCreated = false; bool allocation64kbPageCreated = false; bool allocationInDevicePoolCreated = false;