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