Skip to content

Commit c69176a

Browse files
committed
fix: segfaults in zer DDI intercepts
Signed-off-by: Vishnu Khanth <vishnu.khanth.b@intel.com>
1 parent b8ab77b commit c69176a

6 files changed

Lines changed: 144 additions & 11 deletions

File tree

scripts/templates/ldrddi_driver_ddi.cpp.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ namespace loader_driver_ddi
109109
return ${failure_return};
110110
%endif
111111
}
112-
auto dditable = reinterpret_cast<ze_handle_t*>( loader::context->defaultZerDriverHandle )->pRuntime;
112+
auto dditable = reinterpret_cast<ze_handle_t*>( *loader::context->defaultZerDriverHandle )->pRuntime;
113113
if (dditable->isValidFlag == 0) {
114114
%if ret_type == 'ze_result_t':
115115
return ${X}_RESULT_ERROR_UNINITIALIZED;

source/drivers/null/ze_null.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,17 +487,32 @@ namespace driver
487487
{
488488
if( nullptr != pExtensionProperties )
489489
{
490-
ze_driver_extension_properties_t driverExtensionProperties = {};
490+
ze_driver_extension_properties_t tracingExtension = {};
491491
#if defined(_WIN32)
492-
strcpy_s( driverExtensionProperties.name, ZET_API_TRACING_EXP_NAME );
492+
strcpy_s( tracingExtension.name, ZET_API_TRACING_EXP_NAME );
493493
#else
494-
strcpy( driverExtensionProperties.name, ZET_API_TRACING_EXP_NAME );
494+
strcpy( tracingExtension.name, ZET_API_TRACING_EXP_NAME );
495+
#endif
496+
tracingExtension.version = ZET_API_TRACING_EXP_VERSION_1_0;
497+
pExtensionProperties[0] = tracingExtension;
498+
499+
ze_driver_extension_properties_t ddiHandlesExtension = {};
500+
#if defined(_WIN32)
501+
strcpy_s( ddiHandlesExtension.name, ZE_DRIVER_DDI_HANDLES_EXT_NAME );
502+
#else
503+
strcpy( ddiHandlesExtension.name, ZE_DRIVER_DDI_HANDLES_EXT_NAME );
495504
#endif
496-
driverExtensionProperties.version = ZET_API_TRACING_EXP_VERSION_1_0;
497505

498-
*pExtensionProperties = driverExtensionProperties;
506+
auto ddi_version_env = getenv("ZEL_TEST_DDI_HANDLES_EXT_VERSION");
507+
if (ddi_version_env && strcmp(ddi_version_env, "1_0") == 0) {
508+
ddiHandlesExtension.version = ZE_DRIVER_DDI_HANDLES_EXT_VERSION_1_0;
509+
} else {
510+
ddiHandlesExtension.version = ZE_DRIVER_DDI_HANDLES_EXT_VERSION_1_1;
511+
}
512+
513+
pExtensionProperties[1] = ddiHandlesExtension;
499514
}
500-
*pCount = 1;
515+
*pCount = 2;
501516

502517
return ZE_RESULT_SUCCESS;
503518
};
@@ -629,6 +644,16 @@ namespace driver
629644
pSysman.Driver = &zesDdiTable.Driver;
630645
pSysman.isValidFlag = 1;
631646
pSysman.version = ZE_API_VERSION_CURRENT;
647+
648+
static zer_global_dditable_t runtimeDdiTable;
649+
runtimeDdiTable.pfnGetLastErrorDescription = driver::zerGetLastErrorDescription;
650+
runtimeDdiTable.pfnTranslateDeviceHandleToIdentifier = driver::zerTranslateDeviceHandleToIdentifier;
651+
runtimeDdiTable.pfnTranslateIdentifierToDeviceHandle = driver::zerTranslateIdentifierToDeviceHandle;
652+
runtimeDdiTable.pfnGetDefaultContext = driver::zerGetDefaultContext;
653+
654+
pRuntime.Global = &runtimeDdiTable;
655+
pRuntime.isValidFlag = 1;
656+
pRuntime.version = ZE_API_VERSION_CURRENT;
632657
}
633658

634659
char *context_t::setenv_var_with_driver_id(const std::string &key, uint32_t driverId)

source/drivers/null/ze_null.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ namespace driver
6464
char *setenv_var_with_driver_id(const std::string &key, uint32_t driverId);
6565
};
6666

67+
ze_result_t ZE_APICALL zerGetLastErrorDescription(const char **ppString);
68+
uint32_t ZE_APICALL zerTranslateDeviceHandleToIdentifier(ze_device_handle_t hDevice);
69+
ze_device_handle_t ZE_APICALL zerTranslateIdentifierToDeviceHandle(uint32_t identifier);
70+
ze_context_handle_t ZE_APICALL zerGetDefaultContext(void);
71+
6772
extern context_t context;
6873
} // namespace driver
6974

source/loader/zer_ldrddi_driver_ddi.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace loader_driver_ddi
3131
if (loader::context->defaultZerDriverHandle == nullptr) {
3232
return ZE_RESULT_ERROR_UNINITIALIZED;
3333
}
34-
auto dditable = reinterpret_cast<ze_handle_t*>( loader::context->defaultZerDriverHandle )->pRuntime;
34+
auto dditable = reinterpret_cast<ze_handle_t*>( *loader::context->defaultZerDriverHandle )->pRuntime;
3535
if (dditable->isValidFlag == 0) {
3636
return ZE_RESULT_ERROR_UNINITIALIZED;
3737
}
@@ -66,7 +66,7 @@ namespace loader_driver_ddi
6666
error_state::setErrorDesc("ERROR UNINITIALIZED");
6767
return UINT32_MAX;
6868
}
69-
auto dditable = reinterpret_cast<ze_handle_t*>( loader::context->defaultZerDriverHandle )->pRuntime;
69+
auto dditable = reinterpret_cast<ze_handle_t*>( *loader::context->defaultZerDriverHandle )->pRuntime;
7070
if (dditable->isValidFlag == 0) {
7171
error_state::setErrorDesc("ERROR UNINITIALIZED");
7272
return UINT32_MAX;
@@ -105,7 +105,7 @@ namespace loader_driver_ddi
105105
error_state::setErrorDesc("ERROR UNINITIALIZED");
106106
return nullptr;
107107
}
108-
auto dditable = reinterpret_cast<ze_handle_t*>( loader::context->defaultZerDriverHandle )->pRuntime;
108+
auto dditable = reinterpret_cast<ze_handle_t*>( *loader::context->defaultZerDriverHandle )->pRuntime;
109109
if (dditable->isValidFlag == 0) {
110110
error_state::setErrorDesc("ERROR UNINITIALIZED");
111111
return nullptr;
@@ -144,7 +144,7 @@ namespace loader_driver_ddi
144144
error_state::setErrorDesc("ERROR UNINITIALIZED");
145145
return nullptr;
146146
}
147-
auto dditable = reinterpret_cast<ze_handle_t*>( loader::context->defaultZerDriverHandle )->pRuntime;
147+
auto dditable = reinterpret_cast<ze_handle_t*>( *loader::context->defaultZerDriverHandle )->pRuntime;
148148
if (dditable->isValidFlag == 0) {
149149
error_state::setErrorDesc("ERROR UNINITIALIZED");
150150
return nullptr;

test/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,25 @@ set_property(TEST driver_ordering_trim_function PROPERTY ENVIRONMENT "ZE_ENABLE_
596596
add_test(NAME driver_ordering_parse_driver_order COMMAND tests --gtest_filter=DriverOrderingHelperFunctionsTest.ParseDriverOrder_*)
597597
set_property(TEST driver_ordering_parse_driver_order PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
598598

599+
add_test(NAME tests_sigle_driver_runtime_api_ddi_ext_v1_1 COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtSupportedWhenCallingRuntimeApisThenExpectNullDriverIsReachedSuccessfully)
600+
set_property(TEST tests_sigle_driver_runtime_api_ddi_ext_v1_1 PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DRIVER_DDI_PATH=1;ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
599601

602+
add_test(NAME tests_multi_driver_runtime_api_ddi_ext_v1_1 COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtSupportedWhenCallingRuntimeApisThenExpectNullDriverIsReachedSuccessfully)
603+
if (MSVC)
604+
set_property(TEST tests_multi_driver_runtime_api_ddi_ext_v1_1 APPEND PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DRIVER_DDI_PATH=1;ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_ALT_DRIVERS=${CMAKE_BINARY_DIR}/bin/$<CONFIG>/ze_null_test1.dll,${CMAKE_BINARY_DIR}/bin/$<CONFIG>/ze_null_test2.dll")
605+
else()
606+
set_property(TEST tests_multi_driver_runtime_api_ddi_ext_v1_1 APPEND PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DRIVER_DDI_PATH=1;ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_ALT_DRIVERS=${CMAKE_BINARY_DIR}/lib/libze_null_test1.so,${CMAKE_BINARY_DIR}/lib/libze_null_test2.so")
607+
endif()
608+
609+
# add_test(NAME tests_sigle_driver_runtime_api_ddi_ext_v1_0 COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtSupportedWithVersion1_0WhenCallingRuntimeApisThenExpectErrorUninitialized)
610+
# set_property(TEST tests_sigle_driver_runtime_api_ddi_ext_v1_0 PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DRIVER_DDI_PATH=1;ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
611+
612+
# add_test(NAME tests_multi_driver_runtime_api_ddi_ext_v1_0 COMMAND tests --gtest_filter=*GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtSupportedWithVersion1_0WhenCallingRuntimeApisThenExpectErrorUninitialized)
613+
# if (MSVC)
614+
# set_property(TEST tests_multi_driver_runtime_api_ddi_ext_v1_0 APPEND PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DRIVER_DDI_PATH=1;ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_ALT_DRIVERS=${CMAKE_BINARY_DIR}/bin/$<CONFIG>/ze_null_test1.dll,${CMAKE_BINARY_DIR}/bin/$<CONFIG>/ze_null_test2.dll")
615+
# else()
616+
# set_property(TEST tests_multi_driver_runtime_api_ddi_ext_v1_0 APPEND PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DRIVER_DDI_PATH=1;ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_ALT_DRIVERS=${CMAKE_BINARY_DIR}/lib/libze_null_test1.so,${CMAKE_BINARY_DIR}/lib/libze_null_test2.so")
617+
# endif()
600618

601619
# These tests are currently not supported on Windows. The reason is that the std::cerr is not being redirected to a pipe in Windows to be then checked against the expected output.
602620
if(NOT MSVC)

test/loader_api.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "loader/ze_loader.h"
1212
#include "ze_api.h"
1313
#include "zes_api.h"
14+
#include "zer_api.h"
1415

1516
#include <fstream>
1617

@@ -2287,4 +2288,88 @@ TEST_F(DriverOrderingTest,
22872288
}
22882289
}
22892290

2291+
TEST(
2292+
RuntimeApiLoaderDriverInteraction,
2293+
GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtSupportedWhenCallingRuntimeApisThenExpectNullDriverIsReachedSuccessfully)
2294+
{
2295+
uint32_t pInitDriversCount = 0;
2296+
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
2297+
desc.flags = UINT32_MAX;
2298+
desc.pNext = nullptr;
2299+
putenv_safe(const_cast<char *>("ZE_ENABLE_LOADER_INTERCEPT=1"));
2300+
putenv_safe(const_cast<char *>("ZEL_TEST_NULL_DRIVER_DISABLE_DDI_EXT=0"));
2301+
std::vector<ze_driver_handle_t> drivers;
2302+
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
2303+
drivers.resize(pInitDriversCount);
2304+
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, drivers.data(), &desc));
2305+
EXPECT_GT(pInitDriversCount, 0);
2306+
2307+
const char *errorString = nullptr;
2308+
uint32_t deviceId = 0;
2309+
2310+
ze_result_t result = zerGetLastErrorDescription(&errorString);
2311+
EXPECT_EQ(ZE_RESULT_SUCCESS, result);
2312+
EXPECT_TRUE(compare_env("zerGetLastErrorDescription", "1"));
2313+
2314+
deviceId = zerTranslateDeviceHandleToIdentifier(nullptr);
2315+
EXPECT_TRUE(compare_env("zerTranslateDeviceHandleToIdentifier", "1"));
2316+
2317+
ze_device_handle_t translatedDevice = zerTranslateIdentifierToDeviceHandle(deviceId);
2318+
EXPECT_TRUE(compare_env("zerTranslateIdentifierToDeviceHandle", "1"));
2319+
(void)translatedDevice;
2320+
2321+
ze_context_handle_t defaultContext = zerGetDefaultContext();
2322+
EXPECT_TRUE(compare_env("zerGetDefaultContext", "1"));
2323+
(void)defaultContext;
2324+
}
2325+
2326+
/*
2327+
TEST(
2328+
RuntimeApiLoaderDriverInteraction,
2329+
GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtSupportedWithVersion1_0WhenCallingRuntimeApisThenExpectErrorUninitialized)
2330+
{
2331+
uint32_t pInitDriversCount = 0;
2332+
ze_init_driver_type_desc_t desc = {ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC};
2333+
desc.flags = UINT32_MAX;
2334+
desc.pNext = nullptr;
2335+
putenv_safe(const_cast<char *>("ZE_ENABLE_LOADER_INTERCEPT=1"));
2336+
putenv_safe(const_cast<char *>("ZEL_TEST_NULL_DRIVER_DISABLE_DDI_EXT=0"));
2337+
putenv_safe(const_cast<char *>("ZEL_TEST_DDI_HANDLES_EXT_VERSION=1_0"));
2338+
std::vector<ze_driver_handle_t> drivers;
2339+
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, nullptr, &desc));
2340+
drivers.resize(pInitDriversCount);
2341+
EXPECT_EQ(ZE_RESULT_SUCCESS, zeInitDrivers(&pInitDriversCount, drivers.data(), &desc));
2342+
EXPECT_GT(pInitDriversCount, 0);
2343+
2344+
uint32_t deviceId = 0;
2345+
2346+
const char *errorDesc = nullptr;
2347+
ze_result_t errorDescResult{};
2348+
2349+
deviceId = zerTranslateDeviceHandleToIdentifier(nullptr);
2350+
EXPECT_EQ(UINT32_MAX, deviceId);
2351+
2352+
errorDescResult = zerGetLastErrorDescription(&errorDesc);
2353+
EXPECT_EQ(ZE_RESULT_SUCCESS, errorDescResult);
2354+
EXPECT_NE(errorDesc, nullptr);
2355+
EXPECT_EQ(0, strcmp(errorDesc, "ERROR UNINITIALIZED"));
2356+
2357+
ze_device_handle_t translatedDevice = zerTranslateIdentifierToDeviceHandle(deviceId);
2358+
EXPECT_EQ(nullptr, translatedDevice);
2359+
2360+
errorDescResult = zerGetLastErrorDescription(&errorDesc);
2361+
EXPECT_EQ(ZE_RESULT_SUCCESS, errorDescResult);
2362+
EXPECT_NE(errorDesc, nullptr);
2363+
EXPECT_EQ(0, strcmp(errorDesc, "ERROR UNINITIALIZED"));
2364+
2365+
ze_context_handle_t defaultContext = zerGetDefaultContext();
2366+
EXPECT_EQ(nullptr, defaultContext);
2367+
2368+
errorDescResult = zerGetLastErrorDescription(&errorDesc);
2369+
EXPECT_EQ(ZE_RESULT_SUCCESS, errorDescResult);
2370+
EXPECT_NE(errorDesc, nullptr);
2371+
EXPECT_EQ(0, strcmp(errorDesc, "ERROR UNINITIALIZED"));
2372+
}
2373+
*/
2374+
22902375
} // namespace

0 commit comments

Comments
 (0)