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 #include "common/test.h"
20 #include "common/utils.h"
21 #include "common/spin_barrier.h"
22 #include "oneapi/tbb/detail/_utils.h"
23 #include "tbb/scalable_allocator.h"
24 #include <thread>
25 
26 static constexpr std::size_t MaxTasks = 16;
27 std::atomic<std::size_t> FinishedTasks;
28 
29 static constexpr std::size_t MaxThread = 4;
30 
31 /*--------------------------------------------------------------------*/
32 // The regression test against a bug triggered when malloc initialization
33 // and thread shutdown were called simultaneously, in which case
34 // Windows dynamic loader lock and allocator initialization/termination lock
35 // were taken in different order.
36 
37 
38 
39 class TestFunc1 {
40     utils::SpinBarrier* my_barr;
41 public:
42     TestFunc1 (utils::SpinBarrier& barr) : my_barr(&barr) {}
43     void operator() (bool do_malloc) const {
44         my_barr->wait();
45         if (do_malloc) scalable_malloc(10);
46         ++FinishedTasks;
47     }
48 };
49 
50 void Test1 () {
51     std::size_t NTasks = utils::min(MaxTasks, utils::max(std::size_t(2), MaxThread));
52     utils::SpinBarrier barr(NTasks);
53     TestFunc1 tf(barr);
54     FinishedTasks = 0;
55 
56     utils::NativeParallelFor(NTasks, [&] (std::size_t thread_idx) {
57         tf(thread_idx % 2 == 0);
58         while (FinishedTasks != NTasks) utils::yield();
59     });
60 }
61 
62 /*--------------------------------------------------------------------*/
63 // The regression test against a bug when cross-thread deallocation
64 // caused livelock at thread shutdown.
65 
66 std::atomic<void*> gPtr(nullptr);
67 
68 class TestFunc2a {
69     utils::SpinBarrier* my_barr;
70 public:
71     TestFunc2a (utils::SpinBarrier& barr) : my_barr(&barr) {}
72     void operator() (std::size_t) const {
73         gPtr = scalable_malloc(8);
74         my_barr->wait();
75         ++FinishedTasks;
76     }
77 };
78 
79 class TestFunc2b {
80     utils::SpinBarrier* my_barr;
81     std::thread& my_ward;
82 public:
83     TestFunc2b (utils::SpinBarrier& barr, std::thread& t) : my_barr(&barr), my_ward(t) {}
84     void operator() (std::size_t) const {
85         utils::SpinWaitWhileEq(gPtr, (void*)nullptr);
86         scalable_free(gPtr);
87         my_barr->wait();
88         my_ward.join();
89         ++FinishedTasks;
90     }
91 };
92 void Test2() {
93     utils::SpinBarrier barr(2);
94     TestFunc2a func2a(barr);
95     std::thread t2a;
96     TestFunc2b func2b(barr, t2a);
97     FinishedTasks = 0;
98     t2a = std::thread(func2a, std::size_t(0));
99     std::thread t2b(func2b, std::size_t(1));
100 
101     while (FinishedTasks != 2) utils::yield();
102 
103     t2b.join(); // t2a is monitored by t2b
104 
105     if (t2a.joinable()) t2a.join();
106 }
107 
108 #if _WIN32||_WIN64
109 
110 void TestKeyDtor() {}
111 
112 #else
113 
114 void *currSmall, *prevSmall, *currLarge, *prevLarge;
115 
116 extern "C" void threadDtor(void*) {
117     // First, release memory that was allocated before;
118     // it will not re-initialize the thread-local data if already deleted
119     prevSmall = currSmall;
120     scalable_free(currSmall);
121     prevLarge = currLarge;
122     scalable_free(currLarge);
123     // Then, allocate more memory.
124     // It will re-initialize the allocator data in the thread.
125     scalable_free(scalable_malloc(8));
126 }
127 
128 inline bool intersectingObjects(const void *p1, const void *p2, size_t n)
129 {
130     return p1>p2 ? ((uintptr_t)p1-(uintptr_t)p2)<n : ((uintptr_t)p2-(uintptr_t)p1)<n;
131 }
132 
133 struct TestThread: utils::NoAssign {
134     TestThread(int ) {}
135 
136     void operator()( std::size_t /*id*/ ) const {
137         pthread_key_t key;
138 
139         currSmall = scalable_malloc(8);
140         REQUIRE_MESSAGE((!prevSmall || currSmall==prevSmall), "Possible memory leak");
141         currLarge = scalable_malloc(32*1024);
142         // intersectingObjects takes into account object shuffle
143         REQUIRE_MESSAGE((!prevLarge || intersectingObjects(currLarge, prevLarge, 32*1024)), "Possible memory leak");
144         pthread_key_create( &key, &threadDtor );
145         pthread_setspecific(key, (const void*)this);
146     }
147 };
148 
149 // test releasing memory from pthread key destructor
150 void TestKeyDtor() {
151     // Allocate region for large objects to prevent whole region release
152     // on scalable_free(currLarge) call, which result in wrong assert inside intersectingObjects check
153     void* preventLargeRelease = scalable_malloc(32*1024);
154     for (int i=0; i<4; i++)
155         utils::NativeParallelFor( 1, TestThread(1) );
156     scalable_free(preventLargeRelease);
157 }
158 
159 #endif // _WIN32||_WIN64
160 
161 
162 //! \brief \ref error_guessing
163 TEST_CASE("test1") {
164     Test1(); // requires malloc initialization so should be first
165 }
166 
167 //! \brief \ref error_guessing
168 TEST_CASE("test2") {
169     Test2();
170 }
171 
172 //! \brief \ref error_guessing
173 TEST_CASE("test key dtor") {
174     TestKeyDtor();
175 }
176