1 /* 2 Copyright (c) 2005-2022 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 #define __TBB_NO_IMPLICIT_LINKAGE 1 18 19 #if _USRDLL 20 21 #include <stdlib.h> 22 #include "common/config.h" 23 #include "common/utils_assert.h" 24 #include "tbb/scalable_allocator.h" 25 26 #if _WIN32||_WIN64 27 extern "C" { 28 extern __declspec(dllexport) void callDll(); 29 } 30 #endif 31 32 extern "C" void callDll() 33 { 34 static const int NUM = 20; 35 void *ptrs[NUM]; 36 37 for (int i=0; i<NUM; i++) { 38 ptrs[i] = scalable_malloc(i*1024); 39 ASSERT(ptrs[i], nullptr); 40 } 41 for (int i=0; i<NUM; i++) 42 scalable_free(ptrs[i]); 43 } 44 45 int main() {} 46 47 48 #else // _USRDLL 49 #include "common/config.h" 50 // FIXME: fix the test to support Windows* 8 Store Apps mode. 51 // For sanitizers, it fails because RUNPATH is lost: https://github.com/google/sanitizers/issues/1219 52 #if !__TBB_WIN8UI_SUPPORT && !(__GNUC__ && __GNUC__ < 10 && __TBB_USE_SANITIZERS) && __TBB_DYNAMIC_LOAD_ENABLED 53 54 #define __TBB_NO_IMPLICIT_LINKAGE 1 55 #include "common/test.h" 56 #include "common/utils.h" 57 #include "common/utils_dynamic_libs.h" 58 #include "common/utils_report.h" 59 #include "common/memory_usage.h" 60 #include "common/spin_barrier.h" 61 62 63 class UseDll { 64 utils::FunctionAddress run; 65 public: 66 UseDll(utils::FunctionAddress runPtr) : run(runPtr) { } 67 void operator()( std::size_t /*id*/ ) const { 68 (*run)(); 69 } 70 }; 71 72 void LoadThreadsUnload() 73 { 74 utils::LIBRARY_HANDLE lib = 75 utils::OpenLibrary(TEST_LIBRARY_NAME("_test_malloc_used_by_lib")); 76 ASSERT(lib, "Can't load " TEST_LIBRARY_NAME("_test_malloc_used_by_lib")); 77 utils::NativeParallelFor(std::size_t(4), UseDll(utils::GetAddress(lib, "callDll"))); 78 utils::CloseLibrary(lib); 79 } 80 81 struct UnloadCallback { 82 utils::LIBRARY_HANDLE lib; 83 84 void operator() () const { 85 utils::CloseLibrary(lib); 86 } 87 }; 88 89 struct RunWithLoad { 90 static utils::SpinBarrier startBarr, endBarr; 91 static UnloadCallback unloadCallback; 92 static utils::FunctionAddress runPtr; 93 94 void operator()(std::size_t id) const { 95 if (!id) { 96 utils::LIBRARY_HANDLE lib = 97 utils::OpenLibrary(TEST_LIBRARY_NAME("_test_malloc_used_by_lib")); 98 ASSERT(lib, "Can't load " TEST_LIBRARY_NAME("_test_malloc_used_by_lib")); 99 runPtr = utils::GetAddress(lib, "callDll"); 100 unloadCallback.lib = lib; 101 } 102 startBarr.wait(); 103 (*runPtr)(); 104 endBarr.wait(unloadCallback); 105 } 106 }; 107 108 utils::SpinBarrier RunWithLoad::startBarr{}, RunWithLoad::endBarr{}; 109 UnloadCallback RunWithLoad::unloadCallback; 110 utils::FunctionAddress RunWithLoad::runPtr; 111 112 void ThreadsLoadUnload() { 113 constexpr std::size_t threads = 4; 114 115 RunWithLoad::startBarr.initialize(threads); 116 RunWithLoad::endBarr.initialize(threads); 117 RunWithLoad body{}; 118 utils::NativeParallelFor(threads, body); 119 } 120 121 //! \brief \ref error_guessing 122 TEST_CASE("use test as lib") { 123 const int ITERS = 20; 124 int i; 125 std::ptrdiff_t memory_leak = 0; 126 127 utils::GetMemoryUsage(); 128 129 for (int run = 0; run < 2; run++) { 130 // expect that memory consumption stabilized after several runs 131 for (i = 0; i < ITERS; i++) { 132 std::size_t memory_in_use = utils::GetMemoryUsage(); 133 if (run) { 134 LoadThreadsUnload(); 135 } else { 136 ThreadsLoadUnload(); 137 } 138 memory_leak = utils::GetMemoryUsage() - memory_in_use; 139 if (memory_leak == 0) // possibly too strong? 140 break; 141 } 142 if(i==ITERS) { 143 // not stabilized, could be leak 144 REPORT( "Error: memory leak of up to %ld bytes\n", static_cast<long>(memory_leak)); 145 WARN(false); 146 } 147 } 148 } 149 #endif /* Unsupported configurations */ 150 #endif // _USRDLL 151