1 /*
2 Copyright (c) 2005-2021 Intel Corporation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 //! \file test_dynamic_link.cpp
18 //! \brief Test for [internal] functionality
19
20 #if _WIN32 || _WIN64
21 #define _CRT_SECURE_NO_WARNINGS
22 #endif
23
24 #include "common/test.h"
25
26 enum FOO_TYPE {
27 FOO_DUMMY,
28 FOO_IMPLEMENTATION
29 };
30
31 #if _WIN32 || _WIN64
32 #define TEST_EXPORT
33 #else
34 #define TEST_EXPORT extern "C"
35 #endif /* _WIN32 || _WIN64 */
36
37 // foo "implementations".
foo1()38 TEST_EXPORT FOO_TYPE foo1() { return FOO_IMPLEMENTATION; }
foo2()39 TEST_EXPORT FOO_TYPE foo2() { return FOO_IMPLEMENTATION; }
40 // foo "dummies".
dummy_foo1()41 FOO_TYPE dummy_foo1() { return FOO_DUMMY; }
dummy_foo2()42 FOO_TYPE dummy_foo2() { return FOO_DUMMY; }
43
44 #include "oneapi/tbb/detail/_config.h"
45 // Suppress the weak symbol mechanism to avoid surplus compiler warnings.
46 #ifdef __TBB_WEAK_SYMBOLS_PRESENT
47 #undef __TBB_WEAK_SYMBOLS_PRESENT
48 #endif
49 #include "src/tbb/dynamic_link.h"
50
51 #if __TBB_DYNAMIC_LOAD_ENABLED
52 // Handlers.
53 static FOO_TYPE (*foo1_handler)() = &dummy_foo1;
54 static FOO_TYPE (*foo2_handler)() = &dummy_foo2;
55
56 // Table describing how to link the handlers.
57 static const tbb::detail::r1::dynamic_link_descriptor LinkTable[] = {
58 { "foo1", (tbb::detail::r1::pointer_to_handler*)(void*)(&foo1_handler) },
59 { "foo2", (tbb::detail::r1::pointer_to_handler*)(void*)(&foo2_handler) }
60 };
61 #endif
62
63 // The direct include since we want to test internal functionality.
64 #include "src/tbb/dynamic_link.cpp"
65 #include "common/utils.h"
66 #include "common/utils_dynamic_libs.h"
67
test_dynamic_link(const char * lib_name)68 void test_dynamic_link(const char* lib_name) {
69 #if __TBB_DYNAMIC_LOAD_ENABLED
70 #if !_WIN32
71 // Check if the executable exports its symbols.
72 REQUIRE_MESSAGE((utils::GetAddress(utils::OpenLibrary(nullptr), "foo1") && utils::GetAddress(utils::OpenLibrary(nullptr), "foo2")),
73 "The executable doesn't export its symbols. Is the -rdynamic switch set during linking?");
74 #endif /* !_WIN32 */
75 // We want to link (or fail to link) to the symbols available from the
76 // executable so it doesn't matter what the library name is specified in
77 // the dynamic_link call - let it be an empty string.
78 // Generally speaking the test has sense only on Linux but on Windows it
79 // checks the dynamic_link graceful behavior with incorrect library name.
80 if (tbb::detail::r1::dynamic_link(lib_name, LinkTable, sizeof(LinkTable) / sizeof(LinkTable[0]))) {
81 REQUIRE_MESSAGE((foo1_handler && foo2_handler), "The symbols are corrupted by dynamic_link");
82 REQUIRE_MESSAGE((foo1_handler() == FOO_IMPLEMENTATION && foo2_handler() == FOO_IMPLEMENTATION),
83 "dynamic_link returned the successful code but symbol(s) are wrong");
84 } else {
85 REQUIRE_MESSAGE((foo1_handler == dummy_foo1 && foo2_handler == dummy_foo2), "The symbols are corrupted by dynamic_link");
86 }
87 #else
88 utils::suppress_unused_warning(lib_name);
89 #endif
90 }
91
92 //! Testing dynamic_link with non-existing library
93 //! \brief \ref error_guessing
94 TEST_CASE("Test dynamic_link with non-existing library") {
95 test_dynamic_link("tbb_unrealNAME.so");
96 }
97
98 //! Testing dynamic_link
99 //! \brief \ref error_guessing
100 TEST_CASE("Test dynamic_link") {
101 test_dynamic_link("");
102 }
103