From d45c16dfc26368ee957a1c3bfe1f7a687563fec5 Mon Sep 17 00:00:00 2001 From: Mateusz Jablonski Date: Wed, 28 Aug 2024 12:23:22 +0000 Subject: [PATCH] fix: add fallback for invalid handles in extension functions handle context, commandlist, driver, device, event, image and kernel handles Signed-off-by: Mateusz Jablonski --- .../public/zex_cmdlist.cpp | 5 +++- .../public/zex_context.cpp | 4 ++-- .../driver_experimental/public/zex_driver.cpp | 10 ++++---- .../driver_experimental/public/zex_event.cpp | 8 +++---- .../driver_experimental/public/zex_memory.cpp | 7 +++--- .../driver_experimental/public/zex_metric.cpp | 4 ++-- .../driver_experimental/public/zex_module.cpp | 4 ++-- .../api/extensions/public/ze_exp_ext.cpp | 4 ++-- level_zero/core/source/cmdlist/cmdlist.h | 5 +++- level_zero/core/source/context/context.h | 2 ++ level_zero/core/source/device/device.h | 5 +++- level_zero/core/source/driver/driver_handle.h | 2 ++ level_zero/core/source/event/event.h | 5 +++- level_zero/core/source/helpers/CMakeLists.txt | 5 ++-- .../core/source/helpers/api_handle_helper.h | 23 +++++++++++++++++++ level_zero/core/source/image/image.h | 5 +++- level_zero/core/source/kernel/kernel.h | 5 +++- 17 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 level_zero/core/source/helpers/api_handle_helper.h diff --git a/level_zero/api/driver_experimental/public/zex_cmdlist.cpp b/level_zero/api/driver_experimental/public/zex_cmdlist.cpp index c55fe4c2dd..e23557feb4 100644 --- a/level_zero/api/driver_experimental/public/zex_cmdlist.cpp +++ b/level_zero/api/driver_experimental/public/zex_cmdlist.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022-2023 Intel Corporation + * Copyright (C) 2022-2024 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -19,6 +19,7 @@ zexCommandListAppendWaitOnMemory( zex_event_handle_t hSignalEvent) { try { { + hCommandList = toInternalType(hCommandList); if (nullptr == hCommandList) return ZE_RESULT_ERROR_INVALID_ARGUMENT; } @@ -40,6 +41,7 @@ zexCommandListAppendWaitOnMemory64( uint64_t data, zex_event_handle_t hSignalEvent) { + hCommandList = toInternalType(hCommandList); if (!hCommandList) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; } @@ -55,6 +57,7 @@ zexCommandListAppendWriteToMemory( uint64_t data) { try { { + hCommandList = toInternalType(hCommandList); if (nullptr == hCommandList) return ZE_RESULT_ERROR_INVALID_ARGUMENT; } diff --git a/level_zero/api/driver_experimental/public/zex_context.cpp b/level_zero/api/driver_experimental/public/zex_context.cpp index bafdc12d1f..afd380ac25 100644 --- a/level_zero/api/driver_experimental/public/zex_context.cpp +++ b/level_zero/api/driver_experimental/public/zex_context.cpp @@ -13,7 +13,7 @@ namespace L0 { ZE_APIEXPORT ze_result_t ZE_APICALL zeIntelMediaCommunicationCreate(ze_context_handle_t hContext, ze_device_handle_t hDevice, ze_intel_media_communication_desc_t *desc, ze_intel_media_doorbell_handle_desc_t *phDoorbell) { - auto device = Device::fromHandle(hDevice); + auto device = Device::fromHandle(toInternalType(hDevice)); if (!device || !desc || !phDoorbell) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; @@ -28,7 +28,7 @@ ZE_APIEXPORT ze_result_t ZE_APICALL zeIntelMediaCommunicationCreate(ze_context_h } ZE_APIEXPORT ze_result_t ZE_APICALL zeIntelMediaCommunicationDestroy(ze_context_handle_t hContext, ze_device_handle_t hDevice, ze_intel_media_doorbell_handle_desc_t *phDoorbell) { - auto device = Device::fromHandle(hDevice); + auto device = Device::fromHandle(toInternalType(hDevice)); if (!device || !phDoorbell) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; diff --git a/level_zero/api/driver_experimental/public/zex_driver.cpp b/level_zero/api/driver_experimental/public/zex_driver.cpp index f094ccaa26..8871a71b36 100644 --- a/level_zero/api/driver_experimental/public/zex_driver.cpp +++ b/level_zero/api/driver_experimental/public/zex_driver.cpp @@ -23,14 +23,14 @@ zexDriverImportExternalPointer( ze_driver_handle_t hDriver, void *ptr, size_t size) { - return L0::DriverHandle::fromHandle(hDriver)->importExternalPointer(ptr, size); + return L0::DriverHandle::fromHandle(toInternalType(hDriver))->importExternalPointer(ptr, size); } ze_result_t ZE_APICALL zexDriverReleaseImportedPointer( ze_driver_handle_t hDriver, void *ptr) { - return L0::DriverHandle::fromHandle(hDriver)->releaseImportedPointer(ptr); + return L0::DriverHandle::fromHandle(toInternalType(hDriver))->releaseImportedPointer(ptr); } ze_result_t ZE_APICALL @@ -38,7 +38,7 @@ zexDriverGetHostPointerBaseAddress( ze_driver_handle_t hDriver, void *ptr, void **baseAddress) { - return L0::DriverHandle::fromHandle(hDriver)->getHostPointerBaseAddress(ptr, baseAddress); + return L0::DriverHandle::fromHandle(toInternalType(hDriver))->getHostPointerBaseAddress(ptr, baseAddress); } } // namespace L0 @@ -49,7 +49,7 @@ zeIntelGetDriverVersionString( char *pDriverVersion, size_t *pVersionSize) { ze_api_version_t apiVersion; - L0::DriverHandle::fromHandle(hDriver)->getApiVersion(&apiVersion); + L0::DriverHandle::fromHandle(toInternalType(hDriver))->getApiVersion(&apiVersion); std::string driverVersionString = std::to_string(ZE_MAJOR_VERSION(apiVersion)) + "." + std::to_string(ZE_MINOR_VERSION(apiVersion)) + "." + std::to_string(NEO_VERSION_BUILD); if (NEO_VERSION_HOTFIX > 0) { driverVersionString += "+" + std::to_string(NEO_VERSION_HOTFIX); @@ -86,4 +86,4 @@ zexDriverGetHostPointerBaseAddress( void **baseAddress) { return L0::zexDriverGetHostPointerBaseAddress(hDriver, ptr, baseAddress); } -} \ No newline at end of file +} diff --git a/level_zero/api/driver_experimental/public/zex_event.cpp b/level_zero/api/driver_experimental/public/zex_event.cpp index d2b6d092d0..c550323b7e 100644 --- a/level_zero/api/driver_experimental/public/zex_event.cpp +++ b/level_zero/api/driver_experimental/public/zex_event.cpp @@ -20,7 +20,7 @@ namespace L0 { ZE_APIEXPORT ze_result_t ZE_APICALL zexEventGetDeviceAddress(ze_event_handle_t event, uint64_t *completionValue, uint64_t *address) { - auto eventObj = Event::fromHandle(event); + auto eventObj = Event::fromHandle(toInternalType(event)); if (!eventObj || !completionValue || !address) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; @@ -58,7 +58,7 @@ zexCounterBasedEventCreate(ze_context_handle_t hContext, ze_device_handle_t hDev false, // ipcPool }; - auto device = Device::fromHandle(hDevice); + auto device = Device::fromHandle(toInternalType(hDevice)); if (!hDevice || !desc || !phEvent) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; @@ -85,7 +85,7 @@ zexCounterBasedEventCreate(ze_context_handle_t hContext, ze_device_handle_t hDev } ZE_APIEXPORT ze_result_t ZE_APICALL zexIntelAllocateNetworkInterrupt(ze_context_handle_t hContext, uint32_t &networkInterruptId) { - auto context = static_cast(L0::Context::fromHandle(hContext)); + auto context = static_cast(L0::Context::fromHandle(toInternalType(hContext))); if (!context) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; @@ -99,7 +99,7 @@ ZE_APIEXPORT ze_result_t ZE_APICALL zexIntelAllocateNetworkInterrupt(ze_context_ } ZE_APIEXPORT ze_result_t ZE_APICALL zexIntelReleaseNetworkInterrupt(ze_context_handle_t hContext, uint32_t networkInterruptId) { - auto context = static_cast(L0::Context::fromHandle(hContext)); + auto context = static_cast(L0::Context::fromHandle(toInternalType(hContext))); if (!context || !context->getDriverHandle()->getMemoryManager()->releaseInterrupt(networkInterruptId, context->rootDeviceIndices[0])) { return ZE_RESULT_ERROR_INVALID_ARGUMENT; diff --git a/level_zero/api/driver_experimental/public/zex_memory.cpp b/level_zero/api/driver_experimental/public/zex_memory.cpp index a319347b4a..d6ccfafd24 100644 --- a/level_zero/api/driver_experimental/public/zex_memory.cpp +++ b/level_zero/api/driver_experimental/public/zex_memory.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Intel Corporation + * Copyright (C) 2020-2024 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -7,6 +7,7 @@ #include "level_zero/api/driver_experimental/public/zex_api.h" #include "level_zero/core/source/context/context.h" +#include "level_zero/core/source/device/device.h" namespace L0 { @@ -16,7 +17,7 @@ zexMemGetIpcHandles( const void *ptr, uint32_t *numIpcHandles, ze_ipc_mem_handle_t *pIpcHandles) { - return L0::Context::fromHandle(hContext)->getIpcMemHandles(ptr, numIpcHandles, pIpcHandles); + return L0::Context::fromHandle(toInternalType(hContext))->getIpcMemHandles(ptr, numIpcHandles, pIpcHandles); } ze_result_t ZE_APICALL @@ -27,7 +28,7 @@ zexMemOpenIpcHandles( ze_ipc_mem_handle_t *pIpcHandles, ze_ipc_memory_flags_t flags, void **pptr) { - return L0::Context::fromHandle(hContext)->openIpcMemHandles(hDevice, numIpcHandles, pIpcHandles, flags, pptr); + return L0::Context::fromHandle(toInternalType(hContext))->openIpcMemHandles(toInternalType(hDevice), numIpcHandles, pIpcHandles, flags, pptr); } } // namespace L0 diff --git a/level_zero/api/driver_experimental/public/zex_metric.cpp b/level_zero/api/driver_experimental/public/zex_metric.cpp index a3bbb608b3..84c79331da 100644 --- a/level_zero/api/driver_experimental/public/zex_metric.cpp +++ b/level_zero/api/driver_experimental/public/zex_metric.cpp @@ -20,7 +20,7 @@ zexDeviceGetConcurrentMetricGroups( uint32_t *pConcurrentGroupCount, uint32_t *pCountPerConcurrentGroup) { - auto device = Device::fromHandle(hDevice); + auto device = Device::fromHandle(toInternalType(hDevice)); return static_cast(device->getMetricDeviceContext()).getConcurrentMetricGroups(metricGroupCount, phMetricGroups, pConcurrentGroupCount, pCountPerConcurrentGroup); } @@ -40,4 +40,4 @@ zexDeviceGetConcurrentMetricGroups( pConcurrentGroupCount, pCountPerConcurrentGroup); } -} // extern "C" \ No newline at end of file +} // extern "C" diff --git a/level_zero/api/driver_experimental/public/zex_module.cpp b/level_zero/api/driver_experimental/public/zex_module.cpp index a358681beb..befc15d261 100644 --- a/level_zero/api/driver_experimental/public/zex_module.cpp +++ b/level_zero/api/driver_experimental/public/zex_module.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Intel Corporation + * Copyright (C) 2020-2024 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -15,7 +15,7 @@ ze_result_t ZE_APICALL zexKernelGetBaseAddress( ze_kernel_handle_t hKernel, uint64_t *baseAddress) { - return L0::Kernel::fromHandle(hKernel)->getBaseAddress(baseAddress); + return L0::Kernel::fromHandle(toInternalType(hKernel))->getBaseAddress(baseAddress); } } // namespace L0 diff --git a/level_zero/api/extensions/public/ze_exp_ext.cpp b/level_zero/api/extensions/public/ze_exp_ext.cpp index 1d17561bce..4d8cb2d203 100644 --- a/level_zero/api/extensions/public/ze_exp_ext.cpp +++ b/level_zero/api/extensions/public/ze_exp_ext.cpp @@ -191,13 +191,13 @@ ze_result_t ZE_APICALL zeMemGetPitchFor2dImage( size_t imageHeight, unsigned int elementSizeInBytes, size_t *rowPitch) { - return L0::Context::fromHandle(hContext)->getPitchFor2dImage(hDevice, imageWidth, imageHeight, elementSizeInBytes, rowPitch); + return L0::Context::fromHandle(toInternalType(hContext))->getPitchFor2dImage(toInternalType(hDevice), imageWidth, imageHeight, elementSizeInBytes, rowPitch); } ze_result_t ZE_APICALL zeImageGetDeviceOffsetExp( ze_image_handle_t hImage, uint64_t *pDeviceOffset) { - return L0::Image::fromHandle(hImage)->getDeviceOffset(pDeviceOffset); + return L0::Image::fromHandle(toInternalType(hImage))->getDeviceOffset(pDeviceOffset); } } // namespace L0 diff --git a/level_zero/core/source/cmdlist/cmdlist.h b/level_zero/core/source/cmdlist/cmdlist.h index 52f02f275a..753c62e6e3 100644 --- a/level_zero/core/source/cmdlist/cmdlist.h +++ b/level_zero/core/source/cmdlist/cmdlist.h @@ -20,6 +20,7 @@ #include "shared/source/utilities/stackvec.h" #include "level_zero/core/source/cmdlist/cmdlist_launch_params.h" +#include "level_zero/core/source/helpers/api_handle_helper.h" #include #include @@ -28,7 +29,9 @@ #include #include -struct _ze_command_list_handle_t {}; +struct _ze_command_list_handle_t { + const uint64_t objMagic = objMagicValue; +}; namespace NEO { class ScratchSpaceController; diff --git a/level_zero/core/source/context/context.h b/level_zero/core/source/context/context.h index aed34492ee..bc7a68c090 100644 --- a/level_zero/core/source/context/context.h +++ b/level_zero/core/source/context/context.h @@ -10,10 +10,12 @@ #include "shared/source/memory_manager/allocation_type.h" #include "shared/source/unified_memory/unified_memory.h" +#include "level_zero/core/source/helpers/api_handle_helper.h" #include #include struct _ze_context_handle_t { + const uint64_t objMagic = objMagicValue; virtual ~_ze_context_handle_t() = default; }; diff --git a/level_zero/core/source/device/device.h b/level_zero/core/source/device/device.h index 2307d89d3e..677a70f9c4 100644 --- a/level_zero/core/source/device/device.h +++ b/level_zero/core/source/device/device.h @@ -11,6 +11,7 @@ #include "shared/source/os_interface/product_helper.h" #include "shared/source/utilities/tag_allocator.h" +#include "level_zero/core/source/helpers/api_handle_helper.h" #include #include @@ -19,7 +20,9 @@ static_assert(NEO::ProductHelper::uuidSize == ZE_MAX_DEVICE_UUID_SIZE); -struct _ze_device_handle_t {}; +struct _ze_device_handle_t { + const uint64_t objMagic = objMagicValue; +}; namespace NEO { class CommandStreamReceiver; class DebuggerL0; diff --git a/level_zero/core/source/driver/driver_handle.h b/level_zero/core/source/driver/driver_handle.h index 8bd22602e6..6b313258a5 100644 --- a/level_zero/core/source/driver/driver_handle.h +++ b/level_zero/core/source/driver/driver_handle.h @@ -7,6 +7,7 @@ #pragma once +#include "level_zero/core/source/helpers/api_handle_helper.h" #include #include @@ -15,6 +16,7 @@ #include struct _ze_driver_handle_t { + const uint64_t objMagic = objMagicValue; virtual ~_ze_driver_handle_t() = default; }; diff --git a/level_zero/core/source/event/event.h b/level_zero/core/source/event/event.h index 71980772c6..a8f7c83642 100644 --- a/level_zero/core/source/event/event.h +++ b/level_zero/core/source/event/event.h @@ -12,6 +12,7 @@ #include "shared/source/memory_manager/multi_graphics_allocation.h" #include "shared/source/os_interface/os_time.h" +#include "level_zero/core/source/helpers/api_handle_helper.h" #include #include @@ -22,7 +23,9 @@ #include #include -struct _ze_event_handle_t {}; +struct _ze_event_handle_t { + const uint64_t objMagic = objMagicValue; +}; struct _ze_event_pool_handle_t {}; diff --git a/level_zero/core/source/helpers/CMakeLists.txt b/level_zero/core/source/helpers/CMakeLists.txt index 116bf74008..a631c15242 100644 --- a/level_zero/core/source/helpers/CMakeLists.txt +++ b/level_zero/core/source/helpers/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (C) 2022-2023 Intel Corporation +# Copyright (C) 2022-2024 Intel Corporation # # SPDX-License-Identifier: MIT # @@ -7,6 +7,7 @@ target_sources(${L0_STATIC_LIB_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt + ${CMAKE_CURRENT_SOURCE_DIR}/api_handle_helper.h ${CMAKE_CURRENT_SOURCE_DIR}/api_specific_config_l0.cpp ${CMAKE_CURRENT_SOURCE_DIR}/error_code_helper_l0.cpp ${CMAKE_CURRENT_SOURCE_DIR}/error_code_helper_l0.h @@ -14,4 +15,4 @@ target_sources(${L0_STATIC_LIB_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/l0_gfx_core_helper_factory_init.inl ${CMAKE_CURRENT_SOURCE_DIR}/l0_populate_factory.h ${CMAKE_CURRENT_SOURCE_DIR}/properties_parser.h -) \ No newline at end of file +) diff --git a/level_zero/core/source/helpers/api_handle_helper.h b/level_zero/core/source/helpers/api_handle_helper.h new file mode 100644 index 0000000000..b766555ed2 --- /dev/null +++ b/level_zero/core/source/helpers/api_handle_helper.h @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2024 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include + +constexpr uint64_t objMagicValue = 0x8D7E6A5D4B3E2E1FULL; + +template +inline T toInternalType(T input) { + if (!input || input->objMagic == objMagicValue) { + return input; + } + input = *reinterpret_cast(input); + if (input->objMagic == objMagicValue) { + return input; + } + return nullptr; +} diff --git a/level_zero/core/source/image/image.h b/level_zero/core/source/image/image.h index dfb5a8c8ed..8991918925 100644 --- a/level_zero/core/source/image/image.h +++ b/level_zero/core/source/image/image.h @@ -7,9 +7,12 @@ #pragma once +#include "level_zero/core/source/helpers/api_handle_helper.h" #include -struct _ze_image_handle_t {}; +struct _ze_image_handle_t { + const uint64_t objMagic = objMagicValue; +}; namespace NEO { struct ImageInfo; diff --git a/level_zero/core/source/kernel/kernel.h b/level_zero/core/source/kernel/kernel.h index b5b3271ff8..fefe72100c 100644 --- a/level_zero/core/source/kernel/kernel.h +++ b/level_zero/core/source/kernel/kernel.h @@ -13,13 +13,16 @@ #include "shared/source/memory_manager/unified_memory_manager.h" #include "shared/source/unified_memory/unified_memory.h" +#include "level_zero/core/source/helpers/api_handle_helper.h" #include #include #include #include -struct _ze_kernel_handle_t {}; +struct _ze_kernel_handle_t { + const uint64_t objMagic = objMagicValue; +}; namespace NEO { class Device;