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 
18 #define __TBB_NO_IMPLICIT_LINKAGE 1
19 
20 #if _USRDLL
21 #include "common/utils_assert.h"
22 
23 const char *globalCallMsg = "A TBB allocator function call is resolved into wrong implementation.";
24 
25 #if _WIN32||_WIN64
26 // must be defined in DLL for linker to not drop the dependency on the DLL.
27 extern "C" {
28     extern __declspec(dllexport) void *scalable_malloc(size_t);
29     extern __declspec(dllexport) void scalable_free (void *);
30     extern __declspec(dllexport) void safer_scalable_free (void *, void (*)(void*));
31     extern __declspec(dllexport) void *scalable_realloc(void *, size_t);
32     extern __declspec(dllexport) void *safer_scalable_realloc(void *, size_t, void *);
33     extern __declspec(dllexport) void *scalable_calloc(size_t, size_t);
34     extern __declspec(dllexport) int scalable_posix_memalign(void **, size_t, size_t);
35     extern __declspec(dllexport) void *scalable_aligned_malloc(size_t, size_t);
36     extern __declspec(dllexport) void *scalable_aligned_realloc(void *, size_t, size_t);
37     extern __declspec(dllexport) void *safer_scalable_aligned_realloc(void *, size_t, size_t, void *);
38     extern __declspec(dllexport) void scalable_aligned_free(void *);
39     extern __declspec(dllexport) size_t scalable_msize(void *);
40     extern __declspec(dllexport) size_t safer_scalable_msize (void *, size_t (*)(void*));
41     extern __declspec(dllexport) int anchor();
42 }
43 #endif
44 
anchor()45 extern "C" int anchor() {
46     return 42;
47 }
48 
49 // Those functions must not be called instead of presented in dynamic library.
scalable_malloc(size_t)50 extern "C" void *scalable_malloc(size_t)
51 {
52     ASSERT(0, globalCallMsg);
53     return nullptr;
54 }
scalable_free(void *)55 extern "C" void scalable_free (void *)
56 {
57     ASSERT(0, globalCallMsg);
58 }
safer_scalable_free(void *,void (*)(void *))59 extern "C" void safer_scalable_free (void *, void (*)(void*))
60 {
61     ASSERT(0, globalCallMsg);
62 }
scalable_realloc(void *,size_t)63 extern "C" void *scalable_realloc(void *, size_t)
64 {
65     ASSERT(0, globalCallMsg);
66     return nullptr;
67 }
safer_scalable_realloc(void *,size_t,void *)68 extern "C" void *safer_scalable_realloc(void *, size_t, void *)
69 {
70     ASSERT(0, globalCallMsg);
71     return nullptr;
72 }
scalable_calloc(size_t,size_t)73 extern "C" void *scalable_calloc(size_t, size_t)
74 {
75     ASSERT(0, globalCallMsg);
76     return nullptr;
77 }
scalable_posix_memalign(void **,size_t,size_t)78 extern "C" int scalable_posix_memalign(void **, size_t, size_t)
79 {
80     ASSERT(0, globalCallMsg);
81     return 0;
82 }
scalable_aligned_malloc(size_t,size_t)83 extern "C" void *scalable_aligned_malloc(size_t, size_t)
84 {
85     ASSERT(0, globalCallMsg);
86     return nullptr;
87 }
scalable_aligned_realloc(void *,size_t,size_t)88 extern "C" void *scalable_aligned_realloc(void *, size_t, size_t)
89 {
90     ASSERT(0, globalCallMsg);
91     return nullptr;
92 }
safer_scalable_aligned_realloc(void *,size_t,size_t,void *)93 extern "C" void *safer_scalable_aligned_realloc(void *, size_t, size_t, void *)
94 {
95     ASSERT(0, globalCallMsg);
96     return nullptr;
97 }
scalable_aligned_free(void *)98 extern "C" void scalable_aligned_free(void *)
99 {
100     ASSERT(0, globalCallMsg);
101 }
scalable_msize(void *)102 extern "C" size_t scalable_msize(void *)
103 {
104     ASSERT(0, globalCallMsg);
105     return 0;
106 }
safer_scalable_msize(void *,size_t (*)(void *))107 extern "C" size_t safer_scalable_msize (void *, size_t (*)(void*))
108 {
109     ASSERT(0, globalCallMsg);
110     return 0;
111 }
112 
main()113 int main() {}
114 
115 #else  // _USRDLL
116 
117 #include "common/config.h"
118 // harness_defs.h must be included before tbb_stddef.h to overcome exception-dependent
119 // system headers that come from tbb_stddef.h
120 #if __TBB_WIN8UI_SUPPORT || __TBB_MIC_OFFLOAD || (__GNUC__ && __GNUC__ < 10 && __TBB_USE_SANITIZERS) || __TBB_SOURCE_DIRECTLY_INCLUDED
121 // The test does not work if dynamic load is unavailable.
122 // For MIC offload, it fails because liboffload brings libiomp which observes and uses the fake scalable_* calls.
123 // For sanitizers, it fails because RUNPATH is lost: https://github.com/google/sanitizers/issues/1219
124 #else
125 #include "common/test.h"
126 #include "common/memory_usage.h"
127 #include "common/utils_dynamic_libs.h"
128 #include "common/utils_assert.h"
129 #include "common/utils_report.h"
130 #include <cstring> // memset
131 
132 extern "C" {
133 #if _WIN32||_WIN64
134     extern __declspec(dllimport)
135 #endif
136     void *scalable_malloc(size_t);
137 
138 #if _WIN32||_WIN64
139     extern __declspec(dllimport)
140 #endif
141     int anchor();
142 }
143 
144 struct Run {
operator ()Run145     void operator()( std::size_t /*id*/ ) const {
146 
147         void* (*malloc_ptr)(std::size_t);
148         void (*free_ptr)(void*);
149 
150         void* (*aligned_malloc_ptr)(size_t size, size_t alignment);
151         void  (*aligned_free_ptr)(void*);
152 
153         const char* actual_name;
154         utils::LIBRARY_HANDLE lib = utils::OpenLibrary(actual_name = MALLOCLIB_NAME1);
155         if (!lib) lib = utils::OpenLibrary(actual_name = MALLOCLIB_NAME2);
156         if (!lib) {
157             REPORT("Can't load " MALLOCLIB_NAME1 " or " MALLOCLIB_NAME2 "\n");
158             exit(1);
159         }
160         utils::GetAddress(lib, "scalable_malloc", malloc_ptr);
161         utils::GetAddress(lib, "scalable_free", free_ptr);
162         utils::GetAddress(lib, "scalable_aligned_malloc", aligned_malloc_ptr);
163         utils::GetAddress(lib, "scalable_aligned_free", aligned_free_ptr);
164 
165         for (size_t sz = 1024; sz <= 10*1024 ; sz*=10) {
166             void *p1 = aligned_malloc_ptr(sz, 16);
167             std::memset(p1, 0, sz);
168             aligned_free_ptr(p1);
169         }
170 
171         void *p = malloc_ptr(100);
172         std::memset(p, 1, 100);
173         free_ptr(p);
174 
175         utils::CloseLibrary(lib);
176 #if _WIN32 || _WIN64
177         ASSERT(GetModuleHandle(actual_name),
178                "allocator library must not be unloaded");
179 #else
180         ASSERT(dlsym(RTLD_DEFAULT, "scalable_malloc"),
181                "allocator library must not be unloaded");
182 #endif
183     }
184 };
185 
186 //! \brief \ref error_guessing
187 TEST_CASE("test unload lib") {
188     CHECK(anchor() == 42);
189 
190     // warm-up run
191     utils::NativeParallelFor( 1, Run() );
192 
193     // It seems Thread Sanitizer remembers some history information about destroyed threads,
194     // so memory consumption cannot be stabilized
195     std::ptrdiff_t memory_leak = 0;
196     {
197         /* 1st call to GetMemoryUsage() allocate some memory,
198            but it seems memory consumption stabilized after this.
199         */
200         utils::GetMemoryUsage();
201         std::size_t memory_in_use = utils::GetMemoryUsage();
202         std::size_t memory_check = utils::GetMemoryUsage();
203         REQUIRE_MESSAGE(memory_in_use == memory_check,
204             "Memory consumption should not increase after 1st GetMemoryUsage() call");
205     }
206 
207     {
208         // expect that memory consumption stabilized after several runs
209         for (;;) {
210             std::size_t memory_in_use = utils::GetMemoryUsage();
211             for (int j=0; j<10; j++)
212                 utils::NativeParallelFor( 1, Run() );
213             memory_leak = utils::GetMemoryUsage() - memory_in_use;
214             if (memory_leak == 0)
215                 return;
216         }
217     }
218 }
219 
220 #endif /* Unsupported configurations */
221 
222 #endif // _USRDLL
223