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". 38 TEST_EXPORT FOO_TYPE foo1() { return FOO_IMPLEMENTATION; } 39 TEST_EXPORT FOO_TYPE foo2() { return FOO_IMPLEMENTATION; } 40 // foo "dummies". 41 FOO_TYPE dummy_foo1() { return FOO_DUMMY; } 42 FOO_TYPE dummy_foo2() { return FOO_DUMMY; } 43 44 // Handlers. 45 static FOO_TYPE (*foo1_handler)() = &dummy_foo1; 46 static FOO_TYPE (*foo2_handler)() = &dummy_foo2; 47 48 #include "oneapi/tbb/detail/_config.h" 49 // Suppress the weak symbol mechanism to avoid surplus compiler warnings. 50 #ifdef __TBB_WEAK_SYMBOLS_PRESENT 51 #undef __TBB_WEAK_SYMBOLS_PRESENT 52 #endif 53 #include "src/tbb/dynamic_link.h" 54 // Table describing how to link the handlers. 55 static const tbb::detail::r1::dynamic_link_descriptor LinkTable[] = { 56 { "foo1", (tbb::detail::r1::pointer_to_handler*)(void*)(&foo1_handler) }, 57 { "foo2", (tbb::detail::r1::pointer_to_handler*)(void*)(&foo2_handler) } 58 }; 59 60 // The direct include since we want to test internal functionality. 61 #include "src/tbb/dynamic_link.cpp" 62 #include "common/utils_dynamic_libs.h" 63 64 void test_dynamic_link(const char* lib_name) { 65 #if __TBB_DYNAMIC_LOAD_ENABLED 66 #if !_WIN32 67 // Check if the executable exports its symbols. 68 REQUIRE_MESSAGE((utils::GetAddress(utils::OpenLibrary(nullptr), "foo1") && utils::GetAddress(utils::OpenLibrary(nullptr), "foo2")), 69 "The executable doesn't export its symbols. Is the -rdynamic switch set during linking?"); 70 #endif /* !_WIN32 */ 71 // We want to link (or fail to link) to the symbols available from the 72 // executable so it doesn't matter what the library name is specified in 73 // the dynamic_link call - let it be an empty string. 74 // Generally speaking the test has sense only on Linux but on Windows it 75 // checks the dynamic_link graceful behavior with incorrect library name. 76 if (tbb::detail::r1::dynamic_link(lib_name, LinkTable, sizeof(LinkTable) / sizeof(LinkTable[0]))) { 77 REQUIRE_MESSAGE((foo1_handler && foo2_handler), "The symbols are corrupted by dynamic_link"); 78 REQUIRE_MESSAGE((foo1_handler() == FOO_IMPLEMENTATION && foo2_handler() == FOO_IMPLEMENTATION), 79 "dynamic_link returned the successful code but symbol(s) are wrong"); 80 } else { 81 REQUIRE_MESSAGE((foo1_handler == dummy_foo1 && foo2_handler == dummy_foo2), "The symbols are corrupted by dynamic_link"); 82 } 83 #endif 84 } 85 86 //! Testing dynamic_link with non-existing library 87 //! \brief \ref error_guessing 88 TEST_CASE("Test dynamic_link with non-existing library") { 89 test_dynamic_link("tbb_unrealNAME.so"); 90 } 91 92 //! Testing dynamic_link 93 //! \brief \ref error_guessing 94 TEST_CASE("Test dynamic_link") { 95 test_dynamic_link(""); 96 } 97