diff --git a/level_zero/core/source/device/device_imp.cpp b/level_zero/core/source/device/device_imp.cpp index 5afa26405f..7590d48fea 100644 --- a/level_zero/core/source/device/device_imp.cpp +++ b/level_zero/core/source/device/device_imp.cpp @@ -1388,8 +1388,11 @@ Device *Device::create(DriverHandle *driverHandle, NEO::Device *neoDevice, bool "Invalid SIP binary.\n"); } } - - stateSaveAreaHeader = NEO::SipKernel::getSipKernel(*neoDevice, nullptr).getStateSaveAreaHeader(); + auto &sipKernel = NEO::SipKernel::getSipKernel(*neoDevice, nullptr); + stateSaveAreaHeader = sipKernel.getStateSaveAreaHeader(); + if (sipKernel.getStateSaveAreaSize(neoDevice) == 0) { + *returnValue = ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE; + } } else { *returnValue = ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE; } diff --git a/level_zero/core/test/unit_tests/sources/device/test_l0_device.cpp b/level_zero/core/test/unit_tests/sources/device/test_l0_device.cpp index 9a83047396..4a9747ba62 100644 --- a/level_zero/core/test/unit_tests/sources/device/test_l0_device.cpp +++ b/level_zero/core/test/unit_tests/sources/device/test_l0_device.cpp @@ -53,6 +53,7 @@ #include "level_zero/core/test/unit_tests/mocks/mock_context.h" #include "level_zero/core/test/unit_tests/mocks/mock_memory_manager.h" +#include "common/StateSaveAreaHeader.h" #include "gtest/gtest.h" #include @@ -286,6 +287,32 @@ TEST(L0DeviceTest, givenDisabledPreemptionWhenCreatingDeviceThenSipKernelIsNotIn EXPECT_FALSE(NEO::MockSipData::called); } +TEST(L0DeviceTest, givenMidThreadPreemptionAndIncorrectStateSaveAreaHeaderWhenCreatingL0DeviceThenErrorIsReturned) { + + VariableBackup mockSipCalled(&NEO::MockSipData::called, false); + VariableBackup mockSipCalledType(&NEO::MockSipData::calledType, NEO::SipKernelType::count); + VariableBackup backupSipInitType(&MockSipData::useMockSip, true); + NonCopyableVariableBackup> backupSipKernel(&MockSipData::mockSipKernel, std::make_unique()); + + MockSipData::mockSipKernel->mockStateSaveAreaHeader = MockSipData::createStateSaveAreaHeader(1); + auto header = reinterpret_cast(MockSipData::mockSipKernel->mockStateSaveAreaHeader.data()); + header->versionHeader.version.major = 4u; + + std::unique_ptr driverHandle(new DriverHandleImp); + auto hwInfo = *NEO::defaultHwInfo; + hwInfo.capabilityTable.defaultPreemptionMode = NEO::PreemptionMode::MidThread; + + auto neoDevice = std::unique_ptr(NEO::MockDevice::createWithNewExecutionEnvironment(&hwInfo, 0)); + ze_result_t returnValue = ZE_RESULT_SUCCESS; + + auto device = std::unique_ptr(Device::create(driverHandle.get(), neoDevice.release(), false, &returnValue)); + EXPECT_NE(nullptr, device); + EXPECT_EQ(ZE_RESULT_ERROR_DEPENDENCY_UNAVAILABLE, returnValue); + + EXPECT_EQ(NEO::SipKernelType::csr, NEO::MockSipData::calledType); + EXPECT_TRUE(NEO::MockSipData::called); +} + TEST(L0DeviceTest, givenDeviceWithoutIGCCompilerLibraryThenInvalidDependencyIsNotReturned) { ze_result_t returnValue = ZE_RESULT_SUCCESS; diff --git a/shared/test/common/helpers/variable_backup.h b/shared/test/common/helpers/variable_backup.h index 507272cf0d..b6c24dda86 100644 --- a/shared/test/common/helpers/variable_backup.h +++ b/shared/test/common/helpers/variable_backup.h @@ -1,11 +1,12 @@ /* - * Copyright (C) 2018-2021 Intel Corporation + * Copyright (C) 2018-2024 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once +#include template class VariableBackup { @@ -36,3 +37,20 @@ class VariableBackup { T oldValue; T *pValue; }; + +template +class NonCopyableVariableBackup { + public: + NonCopyableVariableBackup(T *ptr, T &&newValue) : pValue(ptr) { + oldValue = std::move(*ptr); + *pValue = std::move(newValue); + } + + ~NonCopyableVariableBackup() { + *pValue = std::move(oldValue); + } + + private: + T oldValue; + T *pValue; +}; \ No newline at end of file