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