t_tsan_thread_leak.sh revision 1.3 1 # Copyright (c) 2018 The NetBSD Foundation, Inc.
2 # All rights reserved.
3 #
4 # This code is derived from software contributed to The NetBSD Foundation
5 # by Yang Zheng.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27 #
28
29 test_target()
30 {
31 SUPPORT='n'
32 # Detect address space larger than 32 bits
33 maxaddress=`sysctl vm.maxaddress|awk '{print $3}'`
34 if [ $maxaddress -gt 4294967295 ]; then
35 if command -v cc >/dev/null 2>&1; then
36 if ! echo __clang__ | cc -E - | grep -q __clang__; then
37 SUPPORT='y'
38 elif ! cc -v 2>&1 | awk '/gcc version/{print $3}' | \
39 awk -F '.' '($0+0) > 9 {exit 1}'; then
40 SUPPORT='y'
41 fi
42 fi
43 fi
44 }
45
46 atf_test_case thread_leak
47 thread_leak_head() {
48 atf_set "descr" "Test thread sanitizer for thread leak condition"
49 atf_set "require.progs" "c++ paxctl"
50 }
51
52 atf_test_case thread_leak_profile
53 thread_leak_profile_head() {
54 atf_set "descr" "Test thread sanitizer for thread leak with profiling option"
55 atf_set "require.progs" "c++ paxctl"
56 }
57 atf_test_case thread_leak_pic
58 thread_leak_pic_head() {
59 atf_set "descr" "Test thread sanitizer for thread leak with position independent code (PIC) flag"
60 atf_set "require.progs" "c++ paxctl"
61 }
62 atf_test_case thread_leak_pie
63 thread_leak_pie_head() {
64 atf_set "descr" "Test thread sanitizer for thread leak with position independent execution (PIE) flag"
65 atf_set "require.progs" "c++ paxctl"
66 }
67
68 thread_leak_body(){
69 cat > test.cc << EOF
70 #include <pthread.h>
71 #include <unistd.h>
72
73 int GlobalData;
74 pthread_barrier_t barrier;
75 void *Thread(void *a) {
76 pthread_barrier_wait(&barrier);
77 return 0;
78 }
79
80 int main() {
81 pthread_t t;
82 pthread_barrier_init(&barrier, NULL, 2);
83 pthread_create(&t, NULL, Thread, NULL);
84 pthread_barrier_wait(&barrier);
85 sleep(1);
86 return 0;
87 }
88 EOF
89
90 c++ -fsanitize=thread -o test test.cc
91 paxctl +a test
92 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
93 }
94
95 thread_leak_profile_body(){
96 cat > test.cc << EOF
97 #include <pthread.h>
98 #include <unistd.h>
99
100 int GlobalData;
101 pthread_barrier_t barrier;
102 void *Thread(void *a) {
103 pthread_barrier_wait(&barrier);
104 return 0;
105 }
106
107 int main() {
108 pthread_t t;
109 pthread_barrier_init(&barrier, NULL, 2);
110 pthread_create(&t, NULL, Thread, NULL);
111 pthread_barrier_wait(&barrier);
112 sleep(1);
113 return 0;
114 }
115 EOF
116
117 c++ -fsanitize=thread -o test -pg test.cc
118 paxctl +a test
119 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
120 }
121
122 thread_leak_pic_body(){
123 cat > test.cc << EOF
124 #include <stdio.h>
125 #include <stdlib.h>
126 int help(int);
127 int main(int argc, char **argv) {return help(argc);}
128 EOF
129
130 cat > pic.cc << EOF
131 #include <pthread.h>
132 #include <unistd.h>
133
134 int GlobalData;
135 pthread_barrier_t barrier;
136 void *Thread(void *a) {
137 pthread_barrier_wait(&barrier);
138 return 0;
139 }
140
141 int help(int argc) {
142 pthread_t t;
143 pthread_barrier_init(&barrier, NULL, 2);
144 pthread_create(&t, NULL, Thread, NULL);
145 pthread_barrier_wait(&barrier);
146 sleep(1);
147 return 0;
148 }
149 EOF
150
151 c++ -fsanitize=thread -fPIC -shared -o libtest.so pic.cc
152 c++ -o test test.cc -fsanitize=thread -L. -ltest
153 paxctl +a test
154
155 export LD_LIBRARY_PATH=.
156 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
157 }
158 thread_leak_pie_body(){
159
160 #check whether -pie flag is supported on this architecture
161 if ! c++ -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
162 atf_set_skip "c++ -pie not supported on this architecture"
163 fi
164 cat > test.cc << EOF
165 #include <pthread.h>
166 #include <unistd.h>
167
168 int GlobalData;
169 pthread_barrier_t barrier;
170 void *Thread(void *a) {
171 pthread_barrier_wait(&barrier);
172 return 0;
173 }
174
175 int main() {
176 pthread_t t;
177 pthread_barrier_init(&barrier, NULL, 2);
178 pthread_create(&t, NULL, Thread, NULL);
179 pthread_barrier_wait(&barrier);
180 sleep(1);
181 return 0;
182 }
183 EOF
184
185 c++ -fsanitize=thread -o test -fpie -pie test.cc
186 paxctl +a test
187 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
188 }
189
190
191 atf_test_case target_not_supported
192 target_not_supported_head()
193 {
194 atf_set "descr" "Test forced skip"
195 }
196
197 target_not_supported_body()
198 {
199 atf_skip "Target is not supported"
200 }
201
202 atf_init_test_cases()
203 {
204 test_target
205 test $SUPPORT = 'n' && {
206 atf_add_test_case target_not_supported
207 return 0
208 }
209 atf_add_test_case thread_leak
210 atf_add_test_case thread_leak_profile
211 atf_add_test_case thread_leak_pie
212 atf_add_test_case thread_leak_pic
213 }
214