xref: /oneTBB/test/tbb/test_dynamic_link.cpp (revision 51c0b2f7)
1 /*
2     Copyright (c) 2005-2020 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 #include "common/test.h"
18 
19 //! \file test_dynamic_link.cpp
20 //! \brief Test for [internal] functionality
21 
22 enum FOO_TYPE {
23     FOO_DUMMY,
24     FOO_IMPLEMENTATION
25 };
26 
27 #if _WIN32 || _WIN64
28 #define TEST_EXPORT
29 #else
30 #define TEST_EXPORT extern "C"
31 #endif /* _WIN32 || _WIN64 */
32 
33 // foo "implementations".
34 TEST_EXPORT FOO_TYPE foo1() { return FOO_IMPLEMENTATION; }
35 TEST_EXPORT FOO_TYPE foo2() { return FOO_IMPLEMENTATION; }
36 // foo "dummies".
37 FOO_TYPE dummy_foo1() { return FOO_DUMMY; }
38 FOO_TYPE dummy_foo2() { return FOO_DUMMY; }
39 
40 // Handlers.
41 static FOO_TYPE (*foo1_handler)() = &dummy_foo1;
42 static FOO_TYPE (*foo2_handler)() = &dummy_foo2;
43 
44 #include "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 // Table describing how to link the handlers.
51 static const tbb::detail::r1::dynamic_link_descriptor LinkTable[] = {
52     { "foo1", (tbb::detail::r1::pointer_to_handler*)(void*)(&foo1_handler) },
53     { "foo2", (tbb::detail::r1::pointer_to_handler*)(void*)(&foo2_handler) }
54 };
55 
56 // The direct include since we want to test internal functionality.
57 #include "src/tbb/dynamic_link.cpp"
58 #include "common/utils_dynamic_libs.h"
59 
60 void test_dynamic_link(const char* lib_name) {
61 #if __TBB_DYNAMIC_LOAD_ENABLED
62 #if !_WIN32
63     // Check if the executable exports its symbols.
64     REQUIRE_MESSAGE((utils::GetAddress(utils::OpenLibrary(nullptr), "foo1") && utils::GetAddress(utils::OpenLibrary(nullptr), "foo2")),
65             "The executable doesn't export its symbols. Is the -rdynamic switch set during linking?");
66 #endif /* !_WIN32 */
67     // We want to link (or fail to link) to the symbols available from the
68     // executable so it doesn't matter what the library name is specified in
69     // the dynamic_link call - let it be an empty string.
70     // Generally speaking the test has sense only on Linux but on Windows it
71     // checks the dynamic_link graceful behavior with incorrect library name.
72     if (tbb::detail::r1::dynamic_link(lib_name, LinkTable, sizeof(LinkTable) / sizeof(LinkTable[0]))) {
73         REQUIRE_MESSAGE((foo1_handler && foo2_handler), "The symbols are corrupted by dynamic_link");
74         REQUIRE_MESSAGE((foo1_handler() == FOO_IMPLEMENTATION && foo2_handler() == FOO_IMPLEMENTATION),
75                 "dynamic_link returned the successful code but symbol(s) are wrong");
76     } else {
77         REQUIRE_MESSAGE((foo1_handler == dummy_foo1 && foo2_handler == dummy_foo2), "The symbols are corrupted by dynamic_link");
78     }
79 #endif
80 }
81 
82 //! Testing dynamic_link with non-existing library
83 //! \brief \ref error_guessing
84 TEST_CASE("Test dynamic_link with non-existing library") {
85     test_dynamic_link("tbb_unrealNAME.so");
86 }
87 
88 //! Testing dynamic_link
89 //! \brief \ref error_guessing
90 TEST_CASE("Test dynamic_link") {
91     test_dynamic_link("");
92 }
93