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 #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], NULL);
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)
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 class UseDll {
63     utils::FunctionAddress run;
64 public:
65     UseDll(utils::FunctionAddress runPtr) : run(runPtr) { }
66     void operator()( std::size_t /*id*/ ) const {
67         (*run)();
68     }
69 };
70 
71 void LoadThreadsUnload()
72 {
73     utils::LIBRARY_HANDLE lib =
74         utils::OpenLibrary(TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
75     ASSERT(lib, "Can't load " TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
76     utils::NativeParallelFor(std::size_t(4), UseDll(utils::GetAddress(lib, "callDll")));
77     utils::CloseLibrary(lib);
78 }
79 
80 struct UnloadCallback {
81     utils::LIBRARY_HANDLE lib;
82 
83     void operator() () const {
84         utils::CloseLibrary(lib);
85     }
86 };
87 
88 struct RunWithLoad {
89     static utils::SpinBarrier startBarr, endBarr;
90     static UnloadCallback unloadCallback;
91     static utils::FunctionAddress runPtr;
92 
93     void operator()(std::size_t id) const {
94         if (!id) {
95             utils::LIBRARY_HANDLE lib =
96                 utils::OpenLibrary(TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
97             ASSERT(lib, "Can't load " TEST_LIBRARY_NAME("_test_malloc_used_by_lib"));
98             runPtr = utils::GetAddress(lib, "callDll");
99             unloadCallback.lib = lib;
100         }
101         startBarr.wait();
102         (*runPtr)();
103         endBarr.wait(unloadCallback);
104     }
105 };
106 
107 utils::SpinBarrier RunWithLoad::startBarr{}, RunWithLoad::endBarr{};
108 UnloadCallback RunWithLoad::unloadCallback;
109 utils::FunctionAddress RunWithLoad::runPtr;
110 
111 void ThreadsLoadUnload() {
112     constexpr std::size_t threads = 4;
113 
114     RunWithLoad::startBarr.initialize(threads);
115     RunWithLoad::endBarr.initialize(threads);
116     RunWithLoad body{};
117     utils::NativeParallelFor(threads, body);
118 }
119 
120 //! \brief \ref error_guessing
121 TEST_CASE("use test as lib") {
122     const int ITERS = 20;
123     int i;
124     std::ptrdiff_t memory_leak = 0;
125 
126     utils::GetMemoryUsage();
127 
128     for (int run = 0; run < 2; run++) {
129         // expect that memory consumption stabilized after several runs
130         for (i = 0; i < ITERS; i++) {
131             std::size_t memory_in_use = utils::GetMemoryUsage();
132             if (run) {
133                 LoadThreadsUnload();
134             } else {
135                 ThreadsLoadUnload();
136             }
137             memory_leak = utils::GetMemoryUsage() - memory_in_use;
138             if (memory_leak == 0)  // possibly too strong?
139                 break;
140         }
141         if(i==ITERS) {
142             // not stabilized, could be leak
143             REPORT( "Error: memory leak of up to %ld bytes\n", static_cast<long>(memory_leak));
144             WARN(false);
145         }
146     }
147 }
148 
149 #endif /* Unsupported configurations */
150 #endif // _USRDLL
151