File: os_library_linux_tests.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (111 lines) | stat: -rw-r--r-- 4,257 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2020-2024 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/os_interface/linux/os_library_linux.h"
#include "shared/source/os_interface/linux/sys_calls.h"
#include "shared/test/common/helpers/debug_manager_state_restore.h"
#include "shared/test/common/helpers/variable_backup.h"
#include "shared/test/common/os_interface/linux/sys_calls_linux_ult.h"

#include "gtest/gtest.h"

#include <dlfcn.h>

namespace Os {
extern const char *testDllName;
} // namespace Os

namespace NEO {

namespace SysCalls {

extern int dlOpenFlags;
extern bool dlOpenCalled;
} // namespace SysCalls

TEST(OsLibraryTest, GivenValidNameWhenGettingFullPathAndDlinfoFailsThenPathIsEmpty) {
    VariableBackup<decltype(SysCalls::sysCallsDlinfo)> mockDlinfo(&SysCalls::sysCallsDlinfo, [](void *handle, int request, void *info) -> int {
        if (request == RTLD_DI_LINKMAP) {
            return -1;
        }
        return 0;
    });
    std::unique_ptr<OsLibrary> library(OsLibrary::loadFunc({Os::testDllName}));
    EXPECT_NE(nullptr, library);
    std::string path = library->getFullPath();
    EXPECT_EQ(0u, path.size());
}

TEST(OsLibraryTest, GivenValidLibNameWhenGettingFullPathThenPathIsNotEmpty) {
    std::unique_ptr<OsLibrary> library(OsLibrary::loadFunc({Os::testDllName}));
    EXPECT_NE(nullptr, library);
    std::string path = library->getFullPath();
    EXPECT_NE(0u, path.size());
}

TEST(OsLibraryTest, WhenCreatingFullSystemPathThenProperPathIsConstructed) {
    auto fullPath = OsLibrary::createFullSystemPath("test");
    EXPECT_STREQ("test", fullPath.c_str());
}

TEST(OsLibraryTest, GivenDisableDeepBindFlagWhenOpeningLibraryThenRtldDeepBindFlagIsNotPassed) {

    DebugManagerStateRestore restorer;
    VariableBackup<int> dlOpenFlagsBackup{&NEO::SysCalls::dlOpenFlags, 0};
    VariableBackup<bool> dlOpenCalledBackup{&NEO::SysCalls::dlOpenCalled, false};

    debugManager.flags.DisableDeepBind.set(1);
    OsLibraryCreateProperties properties("_abc.so");
    auto lib = std::make_unique<Linux::OsLibrary>(properties);
    EXPECT_TRUE(NEO::SysCalls::dlOpenCalled);
    EXPECT_EQ(0, NEO::SysCalls::dlOpenFlags & RTLD_DEEPBIND);
}

TEST(OsLibraryTest, GivenInvalidLibraryWhenOpeningLibraryThenDlopenErrorIsReturned) {
    VariableBackup<bool> dlOpenCalledBackup{&NEO::SysCalls::dlOpenCalled, false};

    std::string errorValue;
    OsLibraryCreateProperties properties("_abc.so");
    properties.errorValue = &errorValue;
    auto lib = std::make_unique<Linux::OsLibrary>(properties);
    EXPECT_FALSE(errorValue.empty());
    EXPECT_TRUE(NEO::SysCalls::dlOpenCalled);
}

TEST(OsLibraryTest, GivenLoadFlagsOverwriteWhenOpeningLibraryThenDlOpenIsCalledWithExpectedFlags) {
    VariableBackup<int> dlOpenFlagsBackup{&NEO::SysCalls::dlOpenFlags, 0};
    VariableBackup<bool> dlOpenCalledBackup{&NEO::SysCalls::dlOpenCalled, false};
    VariableBackup<bool> dlOpenCaptureFileNameBackup{&NEO::SysCalls::captureDlOpenFilePath, true};

    auto expectedFlag = RTLD_LAZY | RTLD_GLOBAL;
    OsLibraryCreateProperties properties("_abc.so");
    properties.customLoadFlags = &expectedFlag;
    auto lib = std::make_unique<Linux::OsLibrary>(properties);
    EXPECT_TRUE(NEO::SysCalls::dlOpenCalled);
    EXPECT_EQ(NEO::SysCalls::dlOpenFlags, expectedFlag);
    EXPECT_EQ(properties.libraryName, NEO::SysCalls::dlOpenFilePathPassed);
}

TEST(OsLibraryTest, WhenPerformSelfOpenThenIgnoreFileNameForDlOpenCall) {
    VariableBackup<bool> dlOpenCalledBackup{&NEO::SysCalls::dlOpenCalled, false};
    VariableBackup<bool> dlOpenCaptureFileNameBackup{&NEO::SysCalls::captureDlOpenFilePath, true};

    OsLibraryCreateProperties properties("_abc.so");
    properties.performSelfLoad = false;
    auto lib = std::make_unique<Linux::OsLibrary>(properties);
    EXPECT_TRUE(NEO::SysCalls::dlOpenCalled);
    EXPECT_EQ(properties.libraryName, NEO::SysCalls::dlOpenFilePathPassed);
    NEO::SysCalls::dlOpenCalled = false;

    properties.performSelfLoad = true;
    lib = std::make_unique<Linux::OsLibrary>(properties);
    EXPECT_TRUE(NEO::SysCalls::dlOpenCalled);
    EXPECT_NE(properties.libraryName, NEO::SysCalls::dlOpenFilePathPassed);
    EXPECT_TRUE(NEO::SysCalls::dlOpenFilePathPassed.empty());
}

} // namespace NEO