Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions source/loader/ze_loader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (C) 2019-2025 Intel Corporation
* Copyright (C) 2019-2026 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
Expand All @@ -9,6 +9,7 @@

#include "driver_discovery.h"
#include <iostream>
#include <set>

#ifdef __linux__
#include <unistd.h>
Expand Down Expand Up @@ -824,18 +825,36 @@ namespace loader
}
}

for( auto& drv : allDrivers )
{
// Collect all unique driver handles from allDrivers, zeDrivers, and zesDrivers
// to ensure we free each library exactly once, avoiding double-free issues
std::set<HMODULE> uniqueHandles;
for (const auto& drv : allDrivers) {
if (drv.handle) {
auto free_result = FREE_DRIVER_LIBRARY( drv.handle );
auto failure = FREE_DRIVER_LIBRARY_FAILURE_CHECK(free_result);
if (debugTraceEnabled && failure) {
GET_LIBRARY_ERROR(freeLibraryErrorValue);
if (!freeLibraryErrorValue.empty()) {
std::string errorMessage = "Free Library Failed for " + drv.name + " With ";
debug_trace_message(errorMessage, freeLibraryErrorValue);
freeLibraryErrorValue.clear();
}
uniqueHandles.insert(drv.handle);
}
}
for (const auto& drv : zeDrivers) {
if (drv.handle) {
uniqueHandles.insert(drv.handle);
}
}
for (const auto& drv : zesDrivers) {
if (drv.handle) {
uniqueHandles.insert(drv.handle);
}
}

// Free each unique driver library exactly once
for (auto handle : uniqueHandles)
{
auto free_result = FREE_DRIVER_LIBRARY( handle );
auto failure = FREE_DRIVER_LIBRARY_FAILURE_CHECK(free_result);
if (debugTraceEnabled && failure) {
GET_LIBRARY_ERROR(freeLibraryErrorValue);
if (!freeLibraryErrorValue.empty()) {
std::string errorMessage = "Free Library Failed With ";
debug_trace_message(errorMessage, freeLibraryErrorValue);
freeLibraryErrorValue.clear();
}
}
}
Expand Down
30 changes: 27 additions & 3 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ add_executable(
loader_tracing_layer.cpp
)

# Only include driver_ordering_unit_tests for static builds or non-Windows platforms
# as it requires internal loader symbols that are not exported in Windows DLLs
# Only include driver_ordering_unit_tests and driver_teardown_unit_tests for static builds or non-Windows platforms
# as they require internal loader symbols that are not exported in Windows DLLs
if(BUILD_STATIC OR NOT WIN32)
target_sources(tests PRIVATE driver_ordering_unit_tests.cpp)
target_sources(tests PRIVATE
driver_ordering_unit_tests.cpp
driver_teardown_unit_tests.cpp
)
endif()

# For builds on non-Windows platforms, include init_driver_unit_tests
Expand Down Expand Up @@ -745,7 +748,28 @@ set_property(TEST driver_ordering_trim_function PROPERTY ENVIRONMENT "ZE_ENABLE_

add_test(NAME driver_ordering_parse_driver_order COMMAND tests --gtest_filter=DriverOrderingHelperFunctionsTest.ParseDriverOrder_*)
set_property(TEST driver_ordering_parse_driver_order PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
# Driver Teardown Unit Tests
add_test(NAME driver_teardown_unit_tests COMMAND tests --gtest_filter=DriverTeardownUnitTest.*)
set_property(TEST driver_teardown_unit_tests PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")

# Individual Driver Teardown Unit Tests for better granular reporting
add_test(NAME driver_teardown_basic_collection COMMAND tests --gtest_filter=DriverTeardownUnitTest.NoDrivers_*:DriverTeardownUnitTest.AllNullHandles_*:DriverTeardownUnitTest.SingleDriverInAllDrivers_*:DriverTeardownUnitTest.MultipleUniqueHandlesInAllDrivers_*)
set_property(TEST driver_teardown_basic_collection PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")

add_test(NAME driver_teardown_duplicate_handles COMMAND tests --gtest_filter=DriverTeardownUnitTest.SameHandleInAllThreeVectors_*:DriverTeardownUnitTest.MultipleDuplicateHandlesAcrossVectors_*:DriverTeardownUnitTest.SameHandleInZeAndZesOnly_*)
set_property(TEST driver_teardown_duplicate_handles PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")

add_test(NAME driver_teardown_handle_distribution COMMAND tests --gtest_filter=DriverTeardownUnitTest.OnlyZeDriversHaveHandles_*:DriverTeardownUnitTest.OnlyZesDriversHaveHandles_*:DriverTeardownUnitTest.DifferentHandlesInEachVector_*)
set_property(TEST driver_teardown_handle_distribution PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")

add_test(NAME driver_teardown_realistic_scenarios COMMAND tests --gtest_filter=DriverTeardownUnitTest.SingleDriverScenario_*:DriverTeardownUnitTest.MultipleDriversScenario_*:DriverTeardownUnitTest.PartialInitialization_*:DriverTeardownUnitTest.DifferentDriversInitializedInZeVsZes_*)
set_property(TEST driver_teardown_realistic_scenarios PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")

add_test(NAME driver_teardown_edge_cases COMMAND tests --gtest_filter=DriverTeardownUnitTest.MixedNullAndValidHandles_*:DriverTeardownUnitTest.VectorSizeMismatch_*:DriverTeardownUnitTest.LargeNumberOfDrivers_*:DriverTeardownUnitTest.CustomDriverScenario_*)
set_property(TEST driver_teardown_edge_cases PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")

add_test(NAME driver_teardown_driver_types COMMAND tests --gtest_filter=DriverTeardownUnitTest.MixedDriverTypes_*:DriverTeardownUnitTest.ComplexRealWorldScenario_*)
set_property(TEST driver_teardown_driver_types PROPERTY ENVIRONMENT "ZE_ENABLE_LOADER_DEBUG_TRACE=1;ZE_ENABLE_NULL_DRIVER=1")
# Init Driver Unit Tests
add_test(NAME init_driver_unit_tests COMMAND tests --gtest_filter=InitDriverUnitTest.*)
if (MSVC)
Expand Down
Loading
Loading