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