Skip to content

Commit 5a2a19f

Browse files
Sysman Fix FirmwareUtil Cleanup
Fixed by avoiding library function access if library is unavailable. Related-To: LOCI-2719 Signed-off-by: Ranjan, Joshua Santhosh <[email protected]>
1 parent b813171 commit 5a2a19f

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

level_zero/tools/source/sysman/linux/firmware_util/firmware_util_imp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ FirmwareUtilImp::FirmwareUtilImp(const std::string &pciBDF) {
180180
};
181181

182182
FirmwareUtilImp::~FirmwareUtilImp() {
183-
deviceClose(&fwDeviceHandle);
184183
if (nullptr != libraryHandle) {
184+
deviceClose(&fwDeviceHandle);
185185
delete libraryHandle;
186186
libraryHandle = nullptr;
187187
}

level_zero/tools/test/unit_tests/sources/sysman/linux/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
# SPDX-License-Identifier: MIT
55
#
66

7+
if(igsc_FOUND)
8+
set(L0_SRCS_TOOLS_SYSMAN_LINUX_FIRMWARE_UTIL_TEST
9+
${CMAKE_CURRENT_SOURCE_DIR}/test_fw_util.cpp
10+
)
11+
endif()
12+
713
if(UNIX)
814
target_sources(${TARGET_NAME} PRIVATE
915
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
1016
${CMAKE_CURRENT_SOURCE_DIR}/test_sysman.cpp
1117
${CMAKE_CURRENT_SOURCE_DIR}/mock_sysman_fixture.h
1218
${CMAKE_CURRENT_SOURCE_DIR}/mock_procfs_access_fixture.h
1319
${CMAKE_CURRENT_SOURCE_DIR}/mock_sysfs_access_fixture.h
20+
${L0_SRCS_TOOLS_SYSMAN_LINUX_FIRMWARE_UTIL_TEST}
1421
)
1522
endif()
1623

level_zero/tools/test/unit_tests/sources/sysman/linux/mock_fw_util_fixture.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,17 @@ struct MockLinuxFwUtilInterface : public LinuxFwUtilInterface {
3636
ADDMETHOD_NOBASE_VOIDRETURN(getDeviceSupportedFwTypes, (std::vector<std::string> & fwTypes));
3737
};
3838

39+
class LinuxOsLibrary : public OsLibrary {};
40+
struct MockOsLibrary : public LinuxOsLibrary {
41+
public:
42+
virtual ~MockOsLibrary() = default;
43+
void *getProcAddress(const std::string &procName) override {
44+
return nullptr;
45+
}
46+
bool isLoaded() override {
47+
return false;
48+
}
49+
};
50+
3951
} // namespace ult
4052
} // namespace L0
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (C) 2021 Intel Corporation
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*
6+
*/
7+
8+
#include "shared/test/common/helpers/variable_backup.h"
9+
10+
#include "test.h"
11+
12+
#include "level_zero/tools/source/sysman/linux/firmware_util/firmware_util_imp.h"
13+
#include "level_zero/tools/test/unit_tests/sources/sysman/linux/mock_sysman_fixture.h"
14+
15+
extern bool sysmanUltsEnable;
16+
17+
namespace L0 {
18+
namespace ult {
19+
20+
static uint32_t mockFwUtilDeviceCloseCallCount = 0;
21+
22+
TEST(LinuxFwUtilDeleteTest, GivenLibraryWasNotSetWhenFirmwareUtilInterfaceIsDeletedThenLibraryFunctionIsNotAccessed) {
23+
24+
mockFwUtilDeviceCloseCallCount = 0;
25+
26+
if (!sysmanUltsEnable) {
27+
GTEST_SKIP();
28+
}
29+
30+
VariableBackup<decltype(deviceClose)> mockDeviceClose(&deviceClose, [](struct igsc_device_handle *handle) -> int {
31+
mockFwUtilDeviceCloseCallCount++;
32+
return 0;
33+
});
34+
35+
std::string pciBdf("0000:00:00.0");
36+
FirmwareUtilImp *pFwUtilImp = new FirmwareUtilImp(pciBdf);
37+
pFwUtilImp->libraryHandle = nullptr;
38+
delete pFwUtilImp;
39+
EXPECT_EQ(mockFwUtilDeviceCloseCallCount, 0u);
40+
}
41+
42+
TEST(LinuxFwUtilDeleteTest, GivenLibraryWasSetWhenFirmwareUtilInterfaceIsDeletedThenLibraryFunctionIsAccessed) {
43+
44+
mockFwUtilDeviceCloseCallCount = 0;
45+
46+
if (!sysmanUltsEnable) {
47+
GTEST_SKIP();
48+
}
49+
50+
VariableBackup<decltype(deviceClose)> mockDeviceClose(&deviceClose, [](struct igsc_device_handle *handle) -> int {
51+
mockFwUtilDeviceCloseCallCount++;
52+
return 0;
53+
});
54+
55+
std::string pciBdf("0000:00:00.0");
56+
FirmwareUtilImp *pFwUtilImp = new FirmwareUtilImp(pciBdf);
57+
// Prepare dummy OsLibrary for library, since no access is expected
58+
pFwUtilImp->libraryHandle = static_cast<OsLibrary *>(new MockOsLibrary());
59+
delete pFwUtilImp;
60+
EXPECT_EQ(mockFwUtilDeviceCloseCallCount, 1u);
61+
}
62+
63+
} // namespace ult
64+
} // namespace L0

0 commit comments

Comments
 (0)