Skip to content

Commit 3d0b9d1

Browse files
committed
Fix zer Global table init to be optional and zero init DDI tables
- Fix the zer Global Init to be treated like an experimental init that cannot affect the ability to use a driver. - Zero initialize the zer DDI tables to avoid uninitialized memory reads when a driver does not implement all DDI functions. Signed-off-by: Neil R. Spruit <[email protected]>
1 parent e6f47c3 commit 3d0b9d1

File tree

5 files changed

+13
-25
lines changed

5 files changed

+13
-25
lines changed

scripts/templates/ldrddi.cpp.mako

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ ${tbl['export']['name']}(
532532

533533
${x}_result_t result = ${X}_RESULT_SUCCESS;
534534

535-
%if tbl['experimental'] is False: #//Experimental Tables may not be implemented in driver
535+
%if tbl['experimental'] is False and namespace != "zer": #//Experimental Tables may not be implemented in driver
536536
bool atLeastOneDriverValid = false;
537537
%endif
538538
// Load the device-driver DDI tables
@@ -547,7 +547,7 @@ ${tbl['export']['name']}(
547547
auto getTable = reinterpret_cast<${tbl['pfn']}>(
548548
GET_FUNCTION_PTR( drv.handle, "${tbl['export']['name']}") );
549549
if(!getTable)
550-
%if th.isNewProcTable(tbl['export']['name']) is True:
550+
%if th.isNewProcTable(tbl['export']['name']) is True and namespace != "zer":
551551
{
552552
atLeastOneDriverValid = true;
553553
//It is valid to not have this proc addr table
@@ -556,15 +556,15 @@ ${tbl['export']['name']}(
556556
%else:
557557
continue;
558558
%endif
559-
%if tbl['experimental'] is False: #//Experimental Tables may not be implemented in driver
559+
%if tbl['experimental'] is False and namespace != "zer": #//Experimental Tables may not be implemented in driver
560560
auto getTableResult = getTable( version, &drv.dditable.${n}.${tbl['name']});
561561
if(getTableResult == ZE_RESULT_SUCCESS) {
562562
atLeastOneDriverValid = true;
563563
loader::context->configured_version = version;
564564
} else
565565
drv.initStatus = getTableResult;
566566
%if namespace != "zes":
567-
%if tbl['name'] == "Global":
567+
%if tbl['name'] == "Global" and namespace != "zer":
568568
if (drv.dditable.ze.Global.pfnInitDrivers) {
569569
loader::context->initDriversSupport = true;
570570
}
@@ -575,7 +575,7 @@ ${tbl['export']['name']}(
575575
%endif
576576
}
577577

578-
%if tbl['experimental'] is False: #//Experimental Tables may not be implemented in driver
578+
%if tbl['experimental'] is False and namespace != "zer": #//Experimental Tables may not be implemented in driver
579579
if(!atLeastOneDriverValid)
580580
result = ${X}_RESULT_ERROR_UNINITIALIZED;
581581
else

source/lib/ze_lib.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ namespace ze_lib
206206

207207
if ( ZE_RESULT_SUCCESS == result )
208208
{
209+
memset(&initialzeDdiTable, 0, sizeof(ze_dditable_t));
210+
memset(&initialzetDdiTable, 0, sizeof(zet_dditable_t));
211+
memset(&initialzesDdiTable, 0, sizeof(zes_dditable_t));
212+
memset(&initialzerDdiTable, 0, sizeof(zer_dditable_t));
209213
ze_lib::context->zeDdiTable.exchange(&ze_lib::context->initialzeDdiTable);
210214
ze_lib::context->zetDdiTable.exchange(&ze_lib::context->initialzetDdiTable);
211215
ze_lib::context->zesDdiTable.exchange(&ze_lib::context->initialzesDdiTable);

source/loader/zer_ldrddi.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ zerGetGlobalProcAddrTable(
141141

142142
ze_result_t result = ZE_RESULT_SUCCESS;
143143

144-
bool atLeastOneDriverValid = false;
145144
// Load the device-driver DDI tables
146145
for( auto& drv : loader::context->zeDrivers )
147146
{
@@ -151,21 +150,9 @@ zerGetGlobalProcAddrTable(
151150
GET_FUNCTION_PTR( drv.handle, "zerGetGlobalProcAddrTable") );
152151
if(!getTable)
153152
continue;
154-
auto getTableResult = getTable( version, &drv.dditable.zer.Global);
155-
if(getTableResult == ZE_RESULT_SUCCESS) {
156-
atLeastOneDriverValid = true;
157-
loader::context->configured_version = version;
158-
} else
159-
drv.initStatus = getTableResult;
160-
if (drv.dditable.ze.Global.pfnInitDrivers) {
161-
loader::context->initDriversSupport = true;
162-
}
153+
result = getTable( version, &drv.dditable.zer.Global);
163154
}
164155

165-
if(!atLeastOneDriverValid)
166-
result = ZE_RESULT_ERROR_UNINITIALIZED;
167-
else
168-
result = ZE_RESULT_SUCCESS;
169156

170157
if( ZE_RESULT_SUCCESS == result )
171158
{

test/CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ function(add_runtime_api_test test_scenario init_method driver_config)
620620
set(gtest_filter "*GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtNotSupportedWhenCallingRuntimeApisAfter${init_method}ThenExpectNullDriverIsReachedSuccessfully")
621621
elseif(test_scenario STREQUAL "ddi_ext_v1_0")
622622
set(gtest_filter "*GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndDdiExtSupportedWithVersion1_0WhenCallingRuntimeApisAfter${init_method}ThenExpectErrorUninitialized")
623-
elseif(test_scenario STREQUAL "runtime_api_unsupported")
623+
elseif(test_scenario STREQUAL "unsupported")
624624
set(gtest_filter "*GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndRuntimeApiUnsupportedWhenCallingRuntimeApisAfter${init_method}ThenExpectErrorUnsupportedFeature")
625625
endif()
626626

@@ -656,9 +656,8 @@ foreach(init_method IN LISTS init_methods)
656656
# Add multi-driver tests
657657
add_runtime_api_test(${test_scenario} ${init_method} "multi_driver")
658658
endforeach()
659-
660659
# Add runtime_api_unsupported tests for single driver
661-
if(init_method STREQUAL "ZeInitDrivers")
662-
add_runtime_api_test("runtime_api_unsupported" ${init_method} "single_driver")
660+
if(init_method STREQUAL "ZeInitDrivers" OR init_method STREQUAL "ZeInit")
661+
add_runtime_api_test("unsupported" ${init_method} "single_driver")
663662
endif()
664663
endforeach()

test/loader_api.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,7 +2557,6 @@ TEST_F(DriverOrderingTest,
25572557
EXPECT_EQ(0, strcmp(errorDesc, "ERROR UNSUPPORTED FEATURE"));
25582558
}
25592559

2560-
/*
25612560
TEST(
25622561
RuntimeApiLoaderDriverInteraction,
25632562
GivenLevelZeroLoaderPresentWithLoaderInterceptEnabledAndRuntimeApiUnsupportedWhenCallingRuntimeApisAfterZeInitThenExpectErrorUnsupportedFeature)
@@ -2599,7 +2598,6 @@ TEST_F(DriverOrderingTest,
25992598
EXPECT_NE(errorDesc, nullptr);
26002599
EXPECT_EQ(0, strcmp(errorDesc, "ERROR UNSUPPORTED FEATURE"));
26012600
}
2602-
*/
26032601

26042602

26052603
} // namespace

0 commit comments

Comments
 (0)