t_tsan_thread_leak.sh revision 1.5 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 tsan_available_archs()
30 {
31 atf_set "require.arch" "x86_64"
32 }
33
34 atf_test_case thread_leak
35 thread_leak_head() {
36 atf_set "descr" "Test thread sanitizer for thread leak condition"
37 atf_set "require.progs" "cc paxctl"
38 tsan_available_archs
39 }
40
41 atf_test_case thread_leak_profile
42 thread_leak_profile_head() {
43 atf_set "descr" "Test thread sanitizer for thread leak with profiling option"
44 atf_set "require.progs" "cc paxctl"
45 tsan_available_archs
46 }
47 atf_test_case thread_leak_pic
48 thread_leak_pic_head() {
49 atf_set "descr" "Test thread sanitizer for thread leak with position independent code (PIC) flag"
50 atf_set "require.progs" "cc paxctl"
51 tsan_available_archs
52 }
53 atf_test_case thread_leak_pie
54 thread_leak_pie_head() {
55 atf_set "descr" "Test thread sanitizer for thread leak with position independent execution (PIE) flag"
56 atf_set "require.progs" "cc paxctl"
57 tsan_available_archs
58 }
59
60 thread_leak_body(){
61 cat > test.c << EOF
62 #include <pthread.h>
63 #include <unistd.h>
64
65 int GlobalData;
66 pthread_barrier_t barrier;
67 void *Thread(void *a) {
68 pthread_barrier_wait(&barrier);
69 return 0;
70 }
71
72 int main() {
73 pthread_t t;
74 pthread_barrier_init(&barrier, NULL, 2);
75 pthread_create(&t, NULL, Thread, NULL);
76 pthread_barrier_wait(&barrier);
77 sleep(1);
78 return 0;
79 }
80 EOF
81
82 cc -fsanitize=thread -o test test.c
83 paxctl +a test
84 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
85 }
86
87 thread_leak_profile_body(){
88 atf_expect_fail "PR toolchain/55760"
89 cat > test.c << EOF
90 #include <pthread.h>
91 #include <unistd.h>
92
93 int GlobalData;
94 pthread_barrier_t barrier;
95 void *Thread(void *a) {
96 pthread_barrier_wait(&barrier);
97 return 0;
98 }
99
100 int main() {
101 pthread_t t;
102 pthread_barrier_init(&barrier, NULL, 2);
103 pthread_create(&t, NULL, Thread, NULL);
104 pthread_barrier_wait(&barrier);
105 sleep(1);
106 return 0;
107 }
108 EOF
109
110 cc -fsanitize=thread -o test -pg test.c
111 paxctl +a test
112 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
113 }
114
115 thread_leak_pic_body(){
116 cat > test.c << EOF
117 #include <stdio.h>
118 #include <stdlib.h>
119 int help(int);
120 int main(int argc, char **argv) {return help(argc);}
121 EOF
122
123 cat > pic.c << EOF
124 #include <pthread.h>
125 #include <unistd.h>
126
127 int GlobalData;
128 pthread_barrier_t barrier;
129 void *Thread(void *a) {
130 pthread_barrier_wait(&barrier);
131 return 0;
132 }
133
134 int help(int argc) {
135 pthread_t t;
136 pthread_barrier_init(&barrier, NULL, 2);
137 pthread_create(&t, NULL, Thread, NULL);
138 pthread_barrier_wait(&barrier);
139 sleep(1);
140 return 0;
141 }
142 EOF
143
144 cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c
145 cc -o test test.c -fsanitize=thread -L. -ltest
146 paxctl +a test
147
148 export LD_LIBRARY_PATH=.
149 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
150 }
151 thread_leak_pie_body(){
152
153 #check whether -pie flag is supported on this architecture
154 if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
155 atf_set_skip "cc -pie not supported on this architecture"
156 fi
157 cat > test.c << EOF
158 #include <pthread.h>
159 #include <unistd.h>
160
161 int GlobalData;
162 pthread_barrier_t barrier;
163 void *Thread(void *a) {
164 pthread_barrier_wait(&barrier);
165 return 0;
166 }
167
168 int main() {
169 pthread_t t;
170 pthread_barrier_init(&barrier, NULL, 2);
171 pthread_create(&t, NULL, Thread, NULL);
172 pthread_barrier_wait(&barrier);
173 sleep(1);
174 return 0;
175 }
176 EOF
177
178 cc -fsanitize=thread -o test -fpie -pie test.c
179 paxctl +a test
180 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: thread leak" ./test
181 }
182
183 atf_init_test_cases()
184 {
185 atf_add_test_case thread_leak
186 atf_add_test_case thread_leak_profile
187 atf_add_test_case thread_leak_pie
188 atf_add_test_case thread_leak_pic
189 }
190