t_tsan_data_race.sh revision 1.7 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 data_race
35 data_race_head() {
36 atf_set "descr" "Test thread sanitizer for data race condition"
37 atf_set "require.progs" "cc paxctl"
38 tsan_available_archs
39 }
40
41 atf_test_case data_race_profile
42 data_race_profile_head() {
43 atf_set "descr" "Test thread sanitizer for data race with profiling option"
44 atf_set "require.progs" "cc paxctl"
45 tsan_available_archs
46 }
47 atf_test_case data_race_pic
48 data_race_pic_head() {
49 atf_set "descr" "Test thread sanitizer for data race with position independent code (PIC) flag"
50 atf_set "require.progs" "cc paxctl"
51 tsan_available_archs
52 }
53 atf_test_case data_race_pie
54 data_race_pie_head() {
55 atf_set "descr" "Test thread sanitizer for data race with position independent execution (PIE) flag"
56 atf_set "require.progs" "cc paxctl"
57 tsan_available_archs
58 }
59
60 data_race_body(){
61 cat > test.c << EOF
62 #include <pthread.h>
63 int GlobalData; pthread_barrier_t barrier;
64 void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; }
65 int main() {
66 pthread_t t;
67 pthread_barrier_init(&barrier, NULL, 2);
68 pthread_create(&t, NULL, Thread, NULL);
69 pthread_barrier_wait(&barrier);
70 GlobalData = 43;
71 pthread_join(t, NULL);
72 return 0;
73 }
74 EOF
75
76 cc -fsanitize=thread -o test test.c
77 paxctl +a test
78 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test
79 }
80
81 data_race_profile_body(){
82 atf_expect_fail "PR toolchain/55760"
83 cat > test.c << EOF
84 #include <pthread.h>
85 int GlobalData; pthread_barrier_t barrier;
86 void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; }
87 int main() {
88 pthread_t t;
89 pthread_barrier_init(&barrier, NULL, 2);
90 pthread_create(&t, NULL, Thread, NULL);
91 pthread_barrier_wait(&barrier);
92 GlobalData = 43;
93 pthread_join(t, NULL);
94 return 0;
95 }
96 EOF
97
98 cc -fsanitize=thread -static -o test -pg test.c
99 paxctl +a test
100 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test
101 }
102
103 data_race_pic_body(){
104 cat > test.c << EOF
105 #include <stdio.h>
106 #include <stdlib.h>
107 int help(int);
108 int main(int argc, char **argv) {return help(argc);}
109 EOF
110
111 cat > pic.c << EOF
112 #include <pthread.h>
113 int GlobalData; pthread_barrier_t barrier;
114 void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; }
115 int help(int argc) {
116 pthread_t t;
117 pthread_barrier_init(&barrier, NULL, 2);
118 pthread_create(&t, NULL, Thread, NULL);
119 pthread_barrier_wait(&barrier);
120 GlobalData = 43;
121 pthread_join(t, NULL);
122 return 0;
123 }
124 EOF
125
126 cc -fsanitize=thread -fPIC -shared -o libtest.so pic.c
127 cc -o test test.c -fsanitize=thread -L. -ltest
128 paxctl +a test
129
130 export LD_LIBRARY_PATH=.
131 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test
132 }
133 data_race_pie_body(){
134
135 #check whether -pie flag is supported on this architecture
136 if ! cc -pie -dM -E - < /dev/null 2>/dev/null >/dev/null; then
137 atf_set_skip "cc -pie not supported on this architecture"
138 fi
139 cat > test.c << EOF
140 #include <pthread.h>
141 int GlobalData; pthread_barrier_t barrier;
142 void *Thread(void *a) { pthread_barrier_wait(&barrier); GlobalData = 42; return 0; }
143 int main() {
144 pthread_t t;
145 pthread_barrier_init(&barrier, NULL, 2);
146 pthread_create(&t, NULL, Thread, NULL);
147 pthread_barrier_wait(&barrier);
148 GlobalData = 43;
149 pthread_join(t, NULL);
150 return 0;
151 }
152 EOF
153
154 cc -fsanitize=thread -o test -fpie -pie test.c
155 paxctl +a test
156 atf_check -s ignore -o ignore -e match:"WARNING: ThreadSanitizer: data race " ./test
157 }
158
159 atf_init_test_cases()
160 {
161 atf_add_test_case data_race
162 atf_add_test_case data_race_profile
163 atf_add_test_case data_race_pie
164 atf_add_test_case data_race_pic
165 }
166