From f766e6a4c1e9f9b58c9563afc3f8db073ccc4e86 Mon Sep 17 00:00:00 2001 From: Kamil Diedrich Date: Mon, 22 Feb 2021 11:22:38 +0100 Subject: [PATCH] Add tests for linux sys calls Signed-off-by: Kamil Diedrich --- .../source/sharings/va/va_device_shared.cpp | 2 +- opencl/test/unit_test/linux/CMakeLists.txt | 1 + opencl/test/unit_test/linux/mock_os_layer.cpp | 21 ++++++++++++++ opencl/test/unit_test/linux/mock_os_layer.h | 6 +++- opencl/test/unit_test/linux/va_tests.cpp | 29 +++++++++++++++++++ .../sharings/va/va_sharing_tests.cpp | 16 +++++----- 6 files changed, 65 insertions(+), 10 deletions(-) diff --git a/opencl/source/sharings/va/va_device_shared.cpp b/opencl/source/sharings/va/va_device_shared.cpp index a2afc30cf7..78b39b5dc6 100644 --- a/opencl/source/sharings/va/va_device_shared.cpp +++ b/opencl/source/sharings/va/va_device_shared.cpp @@ -24,7 +24,7 @@ ClDevice *VADevice::getRootDeviceFromVaDisplay(Platform *pPlatform, VADisplay va VADisplayContextP pDisplayContext_test = reinterpret_cast(vaDisplay); UNRECOVERABLE_IF(pDisplayContext_test->vadpy_magic != 0x56414430); VADriverContextP pDriverContext_test = pDisplayContext_test->pDriverContext; - int deviceFd = *(int *)pDriverContext_test->drm_state; + int deviceFd = *static_cast(pDriverContext_test->drm_state); UNRECOVERABLE_IF(deviceFd < 0); diff --git a/opencl/test/unit_test/linux/CMakeLists.txt b/opencl/test/unit_test/linux/CMakeLists.txt index 977b176f87..d711f40117 100644 --- a/opencl/test/unit_test/linux/CMakeLists.txt +++ b/opencl/test/unit_test/linux/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable(igdrcl_${target_name} ${NEO_SOURCE_DIR}/opencl/test/unit_test/os_interface/linux/create_drm_memory_manager.cpp ) if(NEO__LIBVA_FOUND) + include_directories("${NEO__LIBVA_INCLUDE_DIR}") target_sources(igdrcl_${target_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/va_tests.cpp) endif() diff --git a/opencl/test/unit_test/linux/mock_os_layer.cpp b/opencl/test/unit_test/linux/mock_os_layer.cpp index 01feb3eedb..787b4461a4 100644 --- a/opencl/test/unit_test/linux/mock_os_layer.cpp +++ b/opencl/test/unit_test/linux/mock_os_layer.cpp @@ -10,6 +10,8 @@ #include #include #include +#include +#include int (*c_open)(const char *pathname, int flags, ...) = nullptr; int (*openFull)(const char *pathname, int flags, ...) = nullptr; @@ -36,11 +38,30 @@ int failOnVirtualMemoryCreate = 0; int failOnSetPriority = 0; int failOnPreemption = 0; int failOnDrmVersion = 0; +int accessCalledTimes = 0; +int readLinkCalledTimes = 0; +int fstatCalledTimes = 0; char providedDrmVersion[5] = {'i', '9', '1', '5', '\0'}; uint64_t gpuTimestamp = 0; int ioctlSeq[8] = {0, 0, 0, 0, 0, 0, 0, 0}; size_t ioctlCnt = 0; +int fstat(int fd, struct stat *buf) { + ++fstatCalledTimes; + buf->st_rdev = 0x0; + return 0; +} + +int access(const char *pathname, int mode) { + ++accessCalledTimes; + return 0; +} + +ssize_t readlink(const char *path, char *buf, size_t bufsiz) { + ++readLinkCalledTimes; + return -1; +} + int open(const char *pathname, int flags, ...) { if (openFull != nullptr) { return openFull(pathname, flags); diff --git a/opencl/test/unit_test/linux/mock_os_layer.h b/opencl/test/unit_test/linux/mock_os_layer.h index 74aed68e92..3a56c4031d 100644 --- a/opencl/test/unit_test/linux/mock_os_layer.h +++ b/opencl/test/unit_test/linux/mock_os_layer.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017-2020 Intel Corporation + * Copyright (C) 2017-2021 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -17,6 +17,7 @@ #include #include #include +#include extern int (*c_open)(const char *pathname, int flags, ...); extern int (*openFull)(const char *pathname, int flags, ...); @@ -46,3 +47,6 @@ extern int ioctlSeq[8]; extern size_t ioctlCnt; extern bool failOnOpenDir; extern uint32_t entryIndex; +extern int accessCalledTimes; +extern int readLinkCalledTimes; +extern int fstatCalledTimes; \ No newline at end of file diff --git a/opencl/test/unit_test/linux/va_tests.cpp b/opencl/test/unit_test/linux/va_tests.cpp index 4246bfbaf8..73cb82fc49 100644 --- a/opencl/test/unit_test/linux/va_tests.cpp +++ b/opencl/test/unit_test/linux/va_tests.cpp @@ -7,9 +7,13 @@ #include "shared/test/common/helpers/variable_backup.h" +#include "opencl/source/sharings/va/va_device.h" #include "opencl/source/sharings/va/va_sharing_functions.h" +#include "opencl/test/unit_test/linux/mock_os_layer.h" #include "test.h" +#include + using namespace NEO; TEST(VaTests, whenLibvaSo2IsNotInstalledThenFail) { @@ -36,3 +40,28 @@ TEST(VaTests, whenLibvaSo2IsNotInstalledThenFail) { EXPECT_EQ(true, va.isVaLibraryAvailable()); } + +TEST(VaTests, givenVADeviceWhenGetDeviceFromVAIsCalledThenProperSyscallsAreUsed) { + VariableBackup backupAccessCalledTimes(&accessCalledTimes); + VariableBackup backupReadLinkCalledTimes(&readLinkCalledTimes); + VariableBackup backupFstatCalledTimes(&fstatCalledTimes); + accessCalledTimes = 0; + readLinkCalledTimes = 0; + fstatCalledTimes = 0; + + auto vaDisplay = std::make_unique(); + vaDisplay->vadpy_magic = 0x56414430; + auto contextPtr = std::make_unique(); + auto drmState = std::make_unique(); + vaDisplay->pDriverContext = contextPtr.get(); + contextPtr->drm_state = drmState.get(); + *static_cast(contextPtr->drm_state) = 1; + + VADevice vaDevice{}; + auto clDevice = vaDevice.getDeviceFromVA(nullptr, vaDisplay.get()); + EXPECT_EQ(clDevice, nullptr); + + EXPECT_EQ(accessCalledTimes, 1); + EXPECT_EQ(readLinkCalledTimes, 1); + EXPECT_EQ(fstatCalledTimes, 1); +} \ No newline at end of file diff --git a/opencl/test/unit_test/sharings/va/va_sharing_tests.cpp b/opencl/test/unit_test/sharings/va/va_sharing_tests.cpp index 22f9fe9c11..3e5301c6ce 100644 --- a/opencl/test/unit_test/sharings/va/va_sharing_tests.cpp +++ b/opencl/test/unit_test/sharings/va/va_sharing_tests.cpp @@ -1240,7 +1240,7 @@ TEST_F(VaDeviceTests, givenVADeviceWhenGetDeviceFromVAIsCalledThenRootDeviceIsRe auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = 1; + *static_cast(contextPtr->drm_state) = 1; auto device = pPlatform->getClDevice(0); NEO::Device *neoDevice = &device->getDevice(); @@ -1260,7 +1260,7 @@ TEST_F(VaDeviceTests, givenVADeviceAndInvalidPciPathOfClDeviceWhenGetDeviceFromV auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = 1; + *static_cast(contextPtr->drm_state) = 1; auto device = pPlatform->getClDevice(0); NEO::Device *neoDevice = &device->getDevice(); @@ -1280,7 +1280,7 @@ TEST_F(VaDeviceTests, givenVADeviceAndInvalidFDWhenGetDeviceFromVAIsCalledThenNu auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = 0; + *static_cast(contextPtr->drm_state) = 0; VADevice vaDevice{}; auto clDevice = vaDevice.getDeviceFromVA(pPlatform, vaDisplay.get()); @@ -1302,7 +1302,7 @@ TEST_F(VaDeviceTests, givenVADeviceAndNegativeFdWhenGetDeviceFromVAIsCalledThenU auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = -1; + *static_cast(contextPtr->drm_state) = -1; VADevice vaDevice{}; EXPECT_ANY_THROW(vaDevice.getDeviceFromVA(pPlatform, vaDisplay.get())); @@ -1323,7 +1323,7 @@ TEST_F(VaDeviceTests, givenVADeviceAndFakeDevicePathWhenGetDeviceFromVAIsCalledT auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = 1; + *static_cast(contextPtr->drm_state) = 1; VADevice vaDevice{}; auto clDevice = vaDevice.getDeviceFromVA(pPlatform, vaDisplay.get()); @@ -1339,7 +1339,7 @@ TEST_F(VaDeviceTests, givenVADeviceAndAbsolutePathWhenGetDeviceFromVAIsCalledThe auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = 1; + *static_cast(contextPtr->drm_state) = 1; VADevice vaDevice{}; auto clDevice = vaDevice.getDeviceFromVA(pPlatform, vaDisplay.get()); @@ -1356,7 +1356,7 @@ TEST_F(VaDeviceTests, givenValidPlatformWithInvalidVaDisplayWhenGetDeviceIdsFrom auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = 1; + *static_cast(contextPtr->drm_state) = 1; auto device = pPlatform->getClDevice(0); NEO::Device *neoDevice = &device->getDevice(); @@ -1380,7 +1380,7 @@ TEST_F(VaDeviceTests, givenValidPlatformWhenGetDeviceIdsFromVaApiMediaAdapterCal auto drmState = std::make_unique(); vaDisplay->pDriverContext = contextPtr.get(); contextPtr->drm_state = drmState.get(); - *(int *)contextPtr->drm_state = 1; + *static_cast(contextPtr->drm_state) = 1; auto device = pPlatform->getClDevice(0); NEO::Device *neoDevice = &device->getDevice();