1 /*
2     Copyright (c) 2018-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 _WIN32 || _WIN64
20 #define _CRT_SECURE_NO_WARNINGS
21 #endif
22 
23 #include "common/test.h"
24 
25 #include "common/allocator_overload.h"
26 #include "common/utils_report.h"
27 #include "common/utils_env.h"
28 
29 // Disabling malloc proxy via env variable is available only on Windows for now
30 #if MALLOC_WINDOWS_OVERLOAD_ENABLED
31 
32 #define TEST_SYSTEM_COMMAND "test_malloc_overload_disable.exe"
33 
34 #include "tbb/tbbmalloc_proxy.h"
35 
36 #include "src/tbb/environment.h"
37 
38 const size_t SmallObjectSize = 16;
39 const size_t LargeObjectSize = 2*8*1024;
40 const size_t HugeObjectSize = 2*1024*1024;
41 
42 void CheckWindowsProxyDisablingViaMemSize( size_t ObjectSize ) {
43     void* ptr = malloc(ObjectSize);
44     /*
45      * If msize returns 0 - tbbmalloc doesn't contain this object in it`s memory
46      * Also msize check that proxy lib is linked
47      */
48     REQUIRE_MESSAGE(!__TBB_malloc_safer_msize(ptr,NULL), "Malloc replacement is not deactivated");
49     free(ptr);
50 }
51 
52 TEST_CASE("Disabling malloc overload") {
53     if (!tbb::detail::r1::GetBoolEnvironmentVariable("TBB_MALLOC_DISABLE_REPLACEMENT"))
54     {
55         utils::SetEnv("TBB_MALLOC_DISABLE_REPLACEMENT","1");
56         if ((system(TEST_SYSTEM_COMMAND)) != 0) {
57             REPORT("Test error: unable to run the command: %s", TEST_SYSTEM_COMMAND);
58             exit(-1);
59         }
60         // We must execute exit(0) to avoid duplicate "Done" printing.
61         exit(0);
62     }
63     else
64     {
65         // Check SMALL objects replacement disable
66         CheckWindowsProxyDisablingViaMemSize(SmallObjectSize);
67         // Check LARGE objects replacement disable
68         CheckWindowsProxyDisablingViaMemSize(LargeObjectSize);
69         // Check HUGE objects replacement disable
70         CheckWindowsProxyDisablingViaMemSize(HugeObjectSize);
71     }
72 }
73 #endif // MALLOC_WINDOWS_OVERLOAD_ENABLED
74